From 9fec2c78d1e1bf8f454c7bbecd85b7259cc3316b Mon Sep 17 00:00:00 2001 From: Anton Bannykh Date: Mon, 25 Nov 2019 17:09:35 +0300 Subject: [PATCH] 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. --- .../backend/common/lower/loops/ProgressionHandlers.kt | 10 +++++----- .../kotlin/backend/common/lower/loops/Utils.kt | 3 ++- .../kotlin/ir/backend/js/JsIrBackendContext.kt | 3 ++- .../jetbrains/kotlin/ir/backend/js/JsLoweringPhases.kt | 8 ++++++++ .../kotlin/ir/backend/js/lower/EnumClassLowering.kt | 2 +- .../js/lower/calls/ExceptionHelperCallsTransformer.kt | 1 + libraries/stdlib/js-ir/runtime/hacks.kt | 3 +++ 7 files changed, 22 insertions(+), 8 deletions(-) diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/ProgressionHandlers.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/ProgressionHandlers.kt index 5b58759e794..276a6386352 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/ProgressionHandlers.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/ProgressionHandlers.kt @@ -132,9 +132,9 @@ internal class UntilHandler(private val context: CommonBackendContext, private v var additionalVariables = emptyList() 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") ) } } diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/Utils.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/Utils.kt index 7627165db06..812f76c78cf 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/Utils.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/lower/loops/Utils.kt @@ -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) } \ No newline at end of file diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsIrBackendContext.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsIrBackendContext.kt index 0600f90c1f1..35362cefb89 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsIrBackendContext.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsIrBackendContext.kt @@ -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) } diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsLoweringPhases.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsLoweringPhases.kt index 15d7fa7492e..648aa6f84a1 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsLoweringPhases.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsLoweringPhases.kt @@ -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 diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/EnumClassLowering.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/EnumClassLowering.kt index fdf83e6061d..645429f713f 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/EnumClassLowering.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/EnumClassLowering.kt @@ -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() private val enumName = irClass.name.identifier - private val throwISESymbol = context.throwISEymbol + private val throwISESymbol = context.throwISEsymbol fun transform(): List { diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/calls/ExceptionHelperCallsTransformer.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/calls/ExceptionHelperCallsTransformer.kt index 4c5a7a93f75..82082ae9426 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/calls/ExceptionHelperCallsTransformer.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/calls/ExceptionHelperCallsTransformer.kt @@ -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"))) ) diff --git a/libraries/stdlib/js-ir/runtime/hacks.kt b/libraries/stdlib/js-ir/runtime/hacks.kt index 0a399dede67..5ad0bfc94c4 100644 --- a/libraries/stdlib/js-ir/runtime/hacks.kt +++ b/libraries/stdlib/js-ir/runtime/hacks.kt @@ -21,5 +21,8 @@ fun THROW_CCE(): Nothing { fun THROW_NPE(): Nothing { throw NullPointerException() } +fun THROW_IAE(msg: String): Nothing { + throw IllegalArgumentException(msg) +} fun ensureNotNull(v: T?): T = if (v == null) THROW_NPE() else v \ No newline at end of file