diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanLower.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanLower.kt index 4f406f3266d..7d097a458c1 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanLower.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanLower.kt @@ -7,6 +7,7 @@ package org.jetbrains.kotlin.backend.konan import org.jetbrains.kotlin.backend.common.runOnFilePostfix import org.jetbrains.kotlin.backend.common.lower.* +import org.jetbrains.kotlin.backend.common.lower.LocalDelegatedPropertiesLowering import org.jetbrains.kotlin.backend.konan.lower.* import org.jetbrains.kotlin.backend.konan.lower.ExpectDeclarationsRemoving import org.jetbrains.kotlin.backend.konan.lower.FinallyBlocksLowering @@ -88,13 +89,8 @@ internal class KonanLower(val context: Context, val parentPhaser: PhaseManager) phaser.phase(KonanPhase.LOWER_SHARED_VARIABLES) { SharedVariablesLowering(context).runOnFilePostfix(irFile) } - phaser.phase(KonanPhase.LOWER_DELEGATION) { - PropertyDelegationLowering(context).lower(irFile) - } - phaser.phase(KonanPhase.LOWER_CALLABLES) { - CallableReferenceLowering(context).lower(irFile) - } phaser.phase(KonanPhase.LOWER_LOCAL_FUNCTIONS) { + LocalDelegatedPropertiesLowering().lower(irFile) LocalDeclarationsLowering(context).runOnFilePostfix(irFile) } phaser.phase(KonanPhase.LOWER_TAILREC) { @@ -119,6 +115,12 @@ internal class KonanLower(val context: Context, val parentPhaser: PhaseManager) phaser.phase(KonanPhase.LOWER_ENUMS) { EnumClassLowering(context).run(irFile) } + phaser.phase(KonanPhase.LOWER_DELEGATION) { + PropertyDelegationLowering(context).lower(irFile) + } + phaser.phase(KonanPhase.LOWER_CALLABLES) { + CallableReferenceLowering(context).lower(irFile) + } phaser.phase(KonanPhase.LOWER_INTEROP_PART2) { InteropLoweringPart2(context).lower(irFile) } diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanPhases.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanPhases.kt index d26efd62271..0897e1e6321 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanPhases.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/KonanPhases.kt @@ -36,7 +36,7 @@ enum class KonanPhase(val description: String, /* ... ... */ LOWER_LATEINIT("Lateinit properties lowering", LOWER_INLINE), /* ... ... */ LOWER_SHARED_VARIABLES("Shared Variable Lowering", LOWER_INITIALIZERS), /* ... ... */ LOWER_CALLABLES("Callable references Lowering", LOWER_DELEGATION/*TODO: make weak dependence on TEST_PROCESSOR*/), - /* ... ... */ LOWER_LOCAL_FUNCTIONS("Local Function Lowering", LOWER_SHARED_VARIABLES, LOWER_CALLABLES), + /* ... ... */ LOWER_LOCAL_FUNCTIONS("Local Function Lowering", LOWER_SHARED_VARIABLES), /* ... ... */ LOWER_INTEROP_PART2("Interop lowering, part 2", LOWER_LOCAL_FUNCTIONS), /* ... ... */ LOWER_TAILREC("tailrec lowering", LOWER_LOCAL_FUNCTIONS), /* ... ... */ LOWER_FINALLY("Finally blocks lowering", LOWER_INITIALIZERS, LOWER_LOCAL_FUNCTIONS, LOWER_TAILREC), diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/CallableReferenceLowering.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/CallableReferenceLowering.kt index de345ad0e20..fa979d6f76b 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/CallableReferenceLowering.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/CallableReferenceLowering.kt @@ -26,7 +26,6 @@ import org.jetbrains.kotlin.ir.declarations.impl.IrClassImpl import org.jetbrains.kotlin.ir.declarations.impl.IrConstructorImpl import org.jetbrains.kotlin.ir.declarations.impl.IrFunctionImpl import org.jetbrains.kotlin.ir.expressions.* -import org.jetbrains.kotlin.ir.expressions.impl.IrConstImpl import org.jetbrains.kotlin.ir.expressions.impl.IrInstanceInitializerCallImpl import org.jetbrains.kotlin.ir.symbols.impl.IrClassSymbolImpl import org.jetbrains.kotlin.ir.symbols.impl.IrConstructorSymbolImpl @@ -42,6 +41,7 @@ internal class CallableReferenceLowering(val context: Context): FileLoweringPass private object DECLARATION_ORIGIN_FUNCTION_REFERENCE_IMPL : IrDeclarationOriginImpl("FUNCTION_REFERENCE_IMPL") override fun lower(irFile: IrFile) { + var generatedClasses = mutableListOf() irFile.transform(object: IrElementTransformerVoidWithContext() { private val stack = mutableListOf() @@ -61,9 +61,18 @@ internal class CallableReferenceLowering(val context: Context): FileLoweringPass } override fun visitDeclaration(declaration: IrDeclaration): IrStatement { + lateinit var tempGeneratedClasses: MutableList + if (declaration is IrClass) { + tempGeneratedClasses = generatedClasses + generatedClasses = mutableListOf() + } stack.push(declaration) val result = super.visitDeclaration(declaration) stack.pop() + if (declaration is IrClass) { + declaration.declarations += generatedClasses + generatedClasses = tempGeneratedClasses + } return result } @@ -99,20 +108,19 @@ internal class CallableReferenceLowering(val context: Context): FileLoweringPass return expression } - val parent = allScopes.map { it.irElement }.filterIsInstance().last() + val parent: IrDeclarationContainer = (currentClass?.irElement as? IrClass) ?: irFile val loweredFunctionReference = FunctionReferenceBuilder(parent, expression).build() + generatedClasses.add(loweredFunctionReference.functionReferenceClass) val irBuilder = context.createIrBuilder(currentScope!!.scope.scopeOwnerSymbol, expression.startOffset, expression.endOffset) - return irBuilder.irBlock(expression) { - +loweredFunctionReference.functionReferenceClass - +irCall(loweredFunctionReference.functionReferenceConstructor.symbol).apply { - expression.getArguments().forEachIndexed { index, argument -> - putValueArgument(index, argument.second) - } + return irBuilder.irCall(loweredFunctionReference.functionReferenceConstructor.symbol).apply { + expression.getArguments().forEachIndexed { index, argument -> + putValueArgument(index, argument.second) } } } }, null) + irFile.declarations += generatedClasses } private class BuiltFunctionReference(val functionReferenceClass: IrClass, @@ -249,16 +257,12 @@ internal class CallableReferenceLowering(val context: Context): FileLoweringPass if (!isKFunction) +irDelegatingConstructorCall(irBuiltIns.anyClass.owner.constructors.single()) else +irDelegatingConstructorCall(kFunctionImplConstructorSymbol.owner).apply { - val stringType = irBuiltIns.stringType - val name = IrConstImpl(startOffset, endOffset, stringType, - IrConstKind.String, referencedFunction.name.asString()) - putValueArgument(0, name) - val fqName = IrConstImpl(startOffset, endOffset, stringType, IrConstKind.String, - (functionReference.symbol.owner).fullName) - putValueArgument(1, fqName) - val bound = IrConstImpl.boolean(startOffset, endOffset, context.irBuiltIns.booleanType, - boundFunctionParameters.isNotEmpty()) - putValueArgument(2, bound) + // TODO: Remove as soon as IR declarations have their originalDescriptor. + val name = (referencedFunction.descriptor as? WrappedSimpleFunctionDescriptor)?.originalDescriptor?.name + ?: referencedFunction.name + putValueArgument(0, irString(name.asString())) + putValueArgument(1, irString((functionReference.symbol.owner).fullName)) + putValueArgument(2, irBoolean(boundFunctionParameters.isNotEmpty())) val needReceiver = boundFunctionParameters.singleOrNull()?.descriptor is ReceiverParameterDescriptor val receiver = if (needReceiver) irGet(valueParameters.single()) else irNull() putValueArgument(3, receiver) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/DelegationLowering.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/DelegationLowering.kt index 8598b8d2b59..14a032ef88a 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/DelegationLowering.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/lower/DelegationLowering.kt @@ -22,9 +22,7 @@ import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptorImpl import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.ir.IrStatement import org.jetbrains.kotlin.ir.builders.* -import org.jetbrains.kotlin.ir.declarations.IrDeclarationOriginImpl -import org.jetbrains.kotlin.ir.declarations.IrFile -import org.jetbrains.kotlin.ir.declarations.IrLocalDelegatedProperty +import org.jetbrains.kotlin.ir.declarations.* import org.jetbrains.kotlin.ir.declarations.impl.IrFieldImpl import org.jetbrains.kotlin.ir.expressions.IrExpression import org.jetbrains.kotlin.ir.expressions.IrLocalDelegatedPropertyReference @@ -117,20 +115,6 @@ internal class PropertyDelegationLowering(val context: Context) : FileLoweringPa irFile.transformChildrenVoid(object : IrElementTransformerVoidWithContext() { - override fun visitLocalDelegatedProperty(declaration: IrLocalDelegatedProperty): IrStatement { - declaration.transformChildrenVoid(this) - - val initializer = declaration.delegate.initializer!! - declaration.delegate.initializer = IrBlockImpl(initializer.startOffset, initializer.endOffset, initializer.type, null, - listOf( - declaration.getter, - declaration.setter, - initializer - ).filterNotNull()) - - return declaration.delegate - } - override fun visitPropertyReference(expression: IrPropertyReference): IrExpression { expression.transformChildrenVoid(this)