From 43d14ed9540213ac70cfa67349d747843f77b9cc Mon Sep 17 00:00:00 2001 From: Roman Artemev Date: Wed, 24 Oct 2018 17:31:08 +0300 Subject: [PATCH] [JS IR BE] Put closure function declaration in container (factory function) --- .../kotlin/ir/backend/js/JsIrBackendContext.kt | 2 ++ .../ir/backend/js/lower/CallableReferenceLowering.kt | 11 +++++++---- .../irToJs/IrElementToJsStatementTransformer.kt | 8 ++++++++ 3 files changed, 17 insertions(+), 4 deletions(-) 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 66685a918a7..9a213984ece 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 @@ -103,6 +103,8 @@ class JsIrBackendContext( // TODO: due to name clash those weird suffix is required, remove it once `NameGenerator` is implemented private val COROUTINE_SUSPEND_OR_RETURN_JS_NAME = "suspendCoroutineUninterceptedOrReturnJS" private val GET_COROUTINE_CONTEXT_NAME = "getCoroutineContext" + + val callableClosureOrigin = object : IrDeclarationOriginImpl("CALLABLE_CLOSURE_DECLARATION") {} } private val internalPackage = module.getPackage(JS_PACKAGE_FQNAME) diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/CallableReferenceLowering.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/CallableReferenceLowering.kt index be34f303ec1..792269a92e5 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/CallableReferenceLowering.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/CallableReferenceLowering.kt @@ -218,7 +218,7 @@ class CallableReferenceLowering(val context: JsIrBackendContext) { putValueArgument(1, callableNameConst) putValueArgument(2, JsIrBuilder.buildString(context.irBuiltIns.stringType, getReferenceName(declaration))) } - Pair(listOf(irVar, irSetName), irVar.symbol) + Pair(listOf(refClosureFunction, irVar, irSetName), irVar.symbol) } @@ -254,7 +254,7 @@ class CallableReferenceLowering(val context: JsIrBackendContext) { val setterFunction = propertyReference.setter?.let { buildClosureFunction(it.owner, refGetFunction, propertyReference) } val additionalDeclarations = generateGetterBodyWithGuard(refGetFunction) { - val statements = mutableListOf() + val statements = mutableListOf(getterDeclaration) val getterFunctionType = context.builtIns.getFunction(getterFunction.valueParameters.size + 1) val type = getterFunctionType.toIrType(symbolTable = context.symbolTable) @@ -270,6 +270,7 @@ class CallableReferenceLowering(val context: JsIrBackendContext) { } if (setterFunction != null) { + statements += setterFunction val setterFunctionType = context.builtIns.getFunction(setterFunction.valueParameters.size + 1) val irSetReference = JsIrBuilder.buildFunctionReference( setterFunctionType.toIrType(symbolTable = context.symbolTable), @@ -324,7 +325,7 @@ class CallableReferenceLowering(val context: JsIrBackendContext) { val getterFunction = buildClosureFunction(context.irBuiltIns.throwIseFun, refGetFunction, propertyReference) val additionalDeclarations = generateGetterBodyWithGuard(refGetFunction) { - val statements = mutableListOf() + val statements = mutableListOf(getterFunction) val getterFunctionType = context.builtIns.getFunction(getterFunction.valueParameters.size + 1) val type = getterFunctionType.toIrType(symbolTable = context.symbolTable) @@ -520,7 +521,9 @@ class CallableReferenceLowering(val context: JsIrBackendContext) { reference: IrCallableReference ): IrFunction { val closureName = createClosureInstanceName(declaration) - val refClosureDeclaration = JsIrBuilder.buildFunction(closureName, Visibilities.LOCAL).also { it.parent = refGetFunction } + val refClosureDeclaration = + JsIrBuilder.buildFunction(closureName, Visibilities.LOCAL, origin = JsIrBackendContext.callableClosureOrigin) + .also { it.parent = refGetFunction } // the params which are passed to closure val closureParamDeclarations = generateSignatureForClosure(declaration, refGetFunction, refClosureDeclaration, reference) diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/transformers/irToJs/IrElementToJsStatementTransformer.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/transformers/irToJs/IrElementToJsStatementTransformer.kt index b4ed02d04fd..913f17b4e0b 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/transformers/irToJs/IrElementToJsStatementTransformer.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/transformers/irToJs/IrElementToJsStatementTransformer.kt @@ -5,8 +5,10 @@ package org.jetbrains.kotlin.ir.backend.js.transformers.irToJs +import org.jetbrains.kotlin.ir.backend.js.JsIrBackendContext import org.jetbrains.kotlin.ir.backend.js.lower.coroutines.COROUTINE_SWITCH import org.jetbrains.kotlin.ir.backend.js.utils.JsGenerationContext +import org.jetbrains.kotlin.ir.declarations.IrFunction import org.jetbrains.kotlin.ir.declarations.IrVariable import org.jetbrains.kotlin.ir.expressions.* import org.jetbrains.kotlin.ir.types.isAny @@ -16,6 +18,12 @@ import org.jetbrains.kotlin.js.backend.ast.* @Suppress("PARAMETER_NAME_CHANGED_ON_OVERRIDE") class IrElementToJsStatementTransformer : BaseIrElementToJsNodeTransformer { + override fun visitFunction(declaration: IrFunction, data: JsGenerationContext) = JsEmpty.also { + assert(declaration.origin == JsIrBackendContext.callableClosureOrigin) { + "The only possible Function Declarartion is one composed in Callable Reference Lowering" + } + } + override fun visitBlockBody(body: IrBlockBody, context: JsGenerationContext): JsStatement { return JsBlock(body.statements.map { it.accept(this, context) }) }