JS IR: apply forLoopsLowering

Replaced createTemporaryVariable usages with createTmpVariables.
The latter doesn't rely on KotlinType's. Using KotlinType with
JS_IR backend causes runtime excpetions, as some expected descriptor
API is not implemented, because it avoids descriptors as much as
possible.
This commit is contained in:
Anton Bannykh
2019-11-25 17:09:35 +03:00
parent 0ea407fef9
commit 9fec2c78d1
7 changed files with 22 additions and 8 deletions
@@ -132,9 +132,9 @@ internal class UntilHandler(private val context: CommonBackendContext, private v
var additionalVariables = emptyList<IrVariable>()
if (untilArg.canHaveSideEffects) {
if (receiverValue.canHaveSideEffects) {
receiverValueVar = scope.createTemporaryVariable(receiverValue, nameHint = "untilReceiverValue")
receiverValueVar = scope.createTmpVariable(receiverValue, nameHint = "untilReceiverValue")
}
untilArgVar = scope.createTemporaryVariable(untilArgCasted, nameHint = "untilArg")
untilArgVar = scope.createTmpVariable(untilArgCasted, nameHint = "untilArg")
additionalVariables = listOfNotNull(receiverValueVar, untilArgVar)
}
@@ -572,7 +572,7 @@ internal class DefaultProgressionHandler(private val context: CommonBackendConte
override fun build(expression: IrExpression, scopeOwner: IrSymbol): HeaderInfo? =
with(context.createIrBuilder(scopeOwner, expression.startOffset, expression.endOffset)) {
// Directly use the `first/last/step` properties of the progression.
val progression = scope.createTemporaryVariable(expression, nameHint = "progression")
val progression = scope.createTmpVariable(expression, nameHint = "progression")
val progressionClass = progression.type.getClass()!!
val first = irCall(progressionClass.symbol.getPropertyGetter("first")!!).apply {
dispatchReceiver = irGet(progression)
@@ -616,7 +616,7 @@ internal abstract class IndexedGetIterationHandler(
//
// This also ensures that the semantics of re-assignment of array variables used in the loop is consistent with the semantics
// proposed in https://youtrack.jetbrains.com/issue/KT-21354.
val objectVariable = scope.createTemporaryVariable(
val objectVariable = scope.createTmpVariable(
expression, nameHint = "indexedObject"
)
@@ -747,7 +747,7 @@ internal class DefaultIterableHandler(private val context: CommonBackendContext)
dispatchReceiver = expression
}
IterableHeaderInfo(
scope.createTemporaryVariable(iterator, nameHint = "iterator")
scope.createTmpVariable(iterator, nameHint = "iterator")
)
}
}
@@ -6,6 +6,7 @@
package org.jetbrains.kotlin.backend.common.lower.loops
import org.jetbrains.kotlin.backend.common.lower.DeclarationIrBuilder
import org.jetbrains.kotlin.ir.builders.createTmpVariable
import org.jetbrains.kotlin.ir.builders.irGet
import org.jetbrains.kotlin.ir.expressions.IrConst
import org.jetbrains.kotlin.ir.expressions.IrConstKind
@@ -89,7 +90,7 @@ internal val IrExpression.constLongValue: Long?
*/
internal fun DeclarationIrBuilder.createTemporaryVariableIfNecessary(expression: IrExpression, nameHint: String? = null) =
if (expression.canHaveSideEffects) {
scope.createTemporaryVariable(expression, nameHint = nameHint).let { Pair(it, irGet(it)) }
scope.createTmpVariable(expression, nameHint = nameHint).let { Pair(it, irGet(it)) }
} else {
Pair(null, expression)
}
@@ -257,7 +257,8 @@ class JsIrBackendContext(
val newThrowableSymbol = symbolTable.referenceSimpleFunction(getJsInternalFunction("newThrowable"))
val extendThrowableSymbol = symbolTable.referenceSimpleFunction(getJsInternalFunction("extendThrowable"))
val throwISEymbol = symbolTable.referenceSimpleFunction(getFunctions(kotlinPackageFqn.child(Name.identifier("THROW_ISE"))).single())
val throwISEsymbol = symbolTable.referenceSimpleFunction(getFunctions(kotlinPackageFqn.child(Name.identifier("THROW_ISE"))).single())
val throwIAEsymbol = symbolTable.referenceSimpleFunction(getFunctions(kotlinPackageFqn.child(Name.identifier("THROW_IAE"))).single())
val suiteFun = getFunctions(FqName("kotlin.test.suite")).singleOrNull()?.let { symbolTable.referenceSimpleFunction(it) }
val testFun = getFunctions(FqName("kotlin.test.test")).singleOrNull()?.let { symbolTable.referenceSimpleFunction(it) }
@@ -8,6 +8,7 @@ package org.jetbrains.kotlin.ir.backend.js
import org.jetbrains.kotlin.backend.common.*
import org.jetbrains.kotlin.backend.common.lower.*
import org.jetbrains.kotlin.backend.common.lower.inline.FunctionInlining
import org.jetbrains.kotlin.backend.common.lower.loops.ForLoopsLowering
import org.jetbrains.kotlin.backend.common.phaser.*
import org.jetbrains.kotlin.ir.backend.js.lower.*
import org.jetbrains.kotlin.ir.backend.js.lower.calls.CallsLowering
@@ -157,6 +158,12 @@ private val returnableBlockLoweringPhase = makeJsModulePhase(
prerequisite = setOf(functionInliningPhase)
)
private val forLoopsLoweringPhase = makeJsModulePhase(
::ForLoopsLowering,
name = "ForLoopsLowering",
description = "For loops lowering"
)
private val localDelegatedPropertiesLoweringPhase = makeJsModulePhase(
{ LocalDelegatedPropertiesLowering() },
name = "LocalDelegatedPropertiesLowering",
@@ -410,6 +417,7 @@ val jsPhases = namedIrModulePhase(
enumUsageLoweringPhase then
suspendFunctionsLoweringPhase then
returnableBlockLoweringPhase then
forLoopsLoweringPhase then
privateMembersLoweringPhase then
callableReferenceLoweringPhase then
defaultArgumentStubGeneratorPhase then
@@ -334,7 +334,7 @@ class EnumClassTransformer(val context: JsIrBackendContext, private val irClass:
private val builder = context.createIrBuilder(irClass.symbol)
private val enumEntries = irClass.declarations.filterIsInstance<IrEnumEntry>()
private val enumName = irClass.name.identifier
private val throwISESymbol = context.throwISEymbol
private val throwISESymbol = context.throwISEsymbol
fun transform(): List<IrDeclaration> {
@@ -23,6 +23,7 @@ class ExceptionHelperCallsTransformer(private val context: JsIrBackendContext) :
context.irBuiltIns.checkNotNullSymbol to referenceFunction(kotlinPackageFqn.child(Name.identifier("ensureNotNull"))),
context.irBuiltIns.throwCceSymbol to referenceFunction(kotlinPackageFqn.child(Name.identifier("THROW_CCE"))),
context.irBuiltIns.throwIseSymbol to referenceFunction(kotlinPackageFqn.child(Name.identifier("THROW_ISE"))),
context.irBuiltIns.illegalArgumentExceptionSymbol to referenceFunction(kotlinPackageFqn.child(Name.identifier("THROW_IAE"))),
context.irBuiltIns.noWhenBranchMatchedExceptionSymbol to referenceFunction(kotlinPackageFqn.child(Name.identifier("noWhenBranchMatchedException")))
)
+3
View File
@@ -21,5 +21,8 @@ fun THROW_CCE(): Nothing {
fun THROW_NPE(): Nothing {
throw NullPointerException()
}
fun THROW_IAE(msg: String): Nothing {
throw IllegalArgumentException(msg)
}
fun <T:Any> ensureNotNull(v: T?): T = if (v == null) THROW_NPE() else v