From 91fb5eca76ae67e4e1b38e96a0a9affd551faa34 Mon Sep 17 00:00:00 2001 From: Sergey Bogolepov Date: Fri, 31 Jan 2020 20:03:00 +0700 Subject: [PATCH] [Interop] Drop DeclarationStubGenerator from interop IR gen The reason for it is usage of LazySymbolTable inside of its typeTranslator. It can generate LazyIrClass instances for interop types that are actually declared. It leads to incorrect behaviour of IrProviderForCEnumAndCStructStubs. --- .../kotlin/backend/konan/ToplevelPhases.kt | 2 +- .../interop/DescriptorToIrTranslationUtils.kt | 71 +++++++++++++++---- .../IrProviderForCEnumAndCStructStubs.kt | 11 ++- .../cenum/CEnumByValueFunctionGenerator.kt | 1 - .../ir/interop/cenum/CEnumClassGenerator.kt | 23 +++--- .../interop/cenum/CEnumCompanionGenerator.kt | 1 - .../interop/cenum/CEnumVarClassGenerator.kt | 1 - .../cstruct/CStructVarClassGenerator.kt | 3 +- .../cstruct/CStructVarCompanionGenerator.kt | 1 - 9 files changed, 74 insertions(+), 40 deletions(-) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ToplevelPhases.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ToplevelPhases.kt index eb52db1612b..a0cc89c6b40 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ToplevelPhases.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ToplevelPhases.kt @@ -207,7 +207,7 @@ internal val psiToIrPhase = konanUnitPhase( moduleDescriptor, symbolTable, config.configuration.languageVersionSettings ) - val irProviderForCEnumsAndCStructs = IrProviderForCEnumAndCStructStubs(generatorContext, interopBuiltIns, stubGenerator, symbols) + val irProviderForCEnumsAndCStructs = IrProviderForCEnumAndCStructStubs(generatorContext, interopBuiltIns, symbols) val irProviderForInteropStubs = IrProviderForInteropStubs(irProviderForCEnumsAndCStructs::canHandleSymbol) val irProviders = listOf( irProviderForCEnumsAndCStructs, diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/interop/DescriptorToIrTranslationUtils.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/interop/DescriptorToIrTranslationUtils.kt index 82e18a1bb54..139f577153d 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/interop/DescriptorToIrTranslationUtils.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/interop/DescriptorToIrTranslationUtils.kt @@ -38,8 +38,6 @@ internal interface DescriptorToIrTranslationMixin { val typeTranslator: TypeTranslator - val stubGenerator: DeclarationStubGenerator - fun KotlinType.toIrType() = typeTranslator.translateType(this) /** @@ -58,9 +56,7 @@ internal interface DescriptorToIrTranslationMixin { descriptor.typeConstructor.supertypes.mapTo(irClass.superTypes) { it.toIrType() } - descriptor.annotations.mapTo(irClass.annotations) { - typeTranslator.constantValueGenerator.generateAnnotationConstructorCall(it)!! - } + irClass.generateAnnotations() irClass.createParameterDeclarations() builder(irClass) createFakeOverrides(descriptor).forEach(irClass::addMember) @@ -81,19 +77,68 @@ internal interface DescriptorToIrTranslationMixin { } } - fun createConstructor(constructorDescriptor: ClassConstructorDescriptor): IrConstructor = - stubGenerator.generateMemberStub(constructorDescriptor) as IrConstructor + fun createConstructor(constructorDescriptor: ClassConstructorDescriptor): IrConstructor { + val irConstructor = symbolTable.declareConstructor( + SYNTHETIC_OFFSET, SYNTHETIC_OFFSET, + IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB, constructorDescriptor + ) + constructorDescriptor.valueParameters.mapTo(irConstructor.valueParameters) { valueParameterDescriptor -> + symbolTable.declareValueParameter( + SYNTHETIC_OFFSET, SYNTHETIC_OFFSET, IrDeclarationOrigin.DEFINED, + valueParameterDescriptor, + valueParameterDescriptor.type.toIrType()).also { + it.parent = irConstructor + } + } + irConstructor.returnType = constructorDescriptor.returnType.toIrType() + irConstructor.generateAnnotations() + return irConstructor + } - fun createProperty(propertyDescriptor: PropertyDescriptor): IrProperty = - stubGenerator.generateMemberStub(propertyDescriptor) as IrProperty + fun createProperty(propertyDescriptor: PropertyDescriptor): IrProperty { + val origin = if (propertyDescriptor.kind == CallableMemberDescriptor.Kind.FAKE_OVERRIDE) { + IrDeclarationOrigin.FAKE_OVERRIDE + } else { + IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB + } + val irProperty = symbolTable.declareProperty(SYNTHETIC_OFFSET, SYNTHETIC_OFFSET, origin, propertyDescriptor) + irProperty.getter = propertyDescriptor.getter?.let { + val irGetter = createFunction(it, origin) + irGetter.correspondingPropertySymbol = irProperty.symbol + irGetter + } + irProperty.setter = propertyDescriptor.setter?.let { + val irSetter = createFunction(it, origin) + irSetter.correspondingPropertySymbol = irProperty.symbol + irSetter + } + irProperty.generateAnnotations() + return irProperty + } fun createFunction( functionDescriptor: FunctionDescriptor, - origin: IrDeclarationOrigin? = IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB - ): IrSimpleFunction = - stubGenerator.generateFunctionStub(functionDescriptor, createPropertyIfNeeded = false).also { - if (origin != null) it.origin = origin + origin: IrDeclarationOrigin = IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB + ): IrSimpleFunction { + val irFunction = symbolTable.declareSimpleFunctionWithOverrides(SYNTHETIC_OFFSET, SYNTHETIC_OFFSET, origin, functionDescriptor) + symbolTable.withScope(functionDescriptor) { + irFunction.returnType = functionDescriptor.returnType!!.toIrType() + functionDescriptor.valueParameters.mapTo(irFunction.valueParameters) { + symbolTable.declareValueParameter(SYNTHETIC_OFFSET, SYNTHETIC_OFFSET, IrDeclarationOrigin.DEFINED, it, it.type.toIrType()) } + irFunction.dispatchReceiverParameter = functionDescriptor.dispatchReceiverParameter?.let { + symbolTable.declareValueParameter(SYNTHETIC_OFFSET, SYNTHETIC_OFFSET, IrDeclarationOrigin.DEFINED, it, it.type.toIrType()) + } + irFunction.generateAnnotations() + } + return irFunction + } + + private fun IrDeclaration.generateAnnotations() { + descriptor.annotations.mapTo(annotations) { + typeTranslator.constantValueGenerator.generateAnnotationConstructorCall(it)!! + } + } } internal fun IrBuilder.irInstanceInitializer(classSymbol: IrClassSymbol): IrExpression = diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/interop/IrProviderForCEnumAndCStructStubs.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/interop/IrProviderForCEnumAndCStructStubs.kt index c02cd5c76c0..ad49a5314c1 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/interop/IrProviderForCEnumAndCStructStubs.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/interop/IrProviderForCEnumAndCStructStubs.kt @@ -42,7 +42,6 @@ import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter internal class IrProviderForCEnumAndCStructStubs( context: GeneratorContext, private val interopBuiltIns: InteropBuiltIns, - stubGenerator: DeclarationStubGenerator, symbols: KonanSymbols ) : IrProvider { @@ -51,15 +50,15 @@ internal class IrProviderForCEnumAndCStructStubs( private val filesMap = mutableMapOf() private val cEnumByValueFunctionGenerator = - CEnumByValueFunctionGenerator(context, stubGenerator, symbols) + CEnumByValueFunctionGenerator(context, symbols) private val cEnumCompanionGenerator = - CEnumCompanionGenerator(context, stubGenerator, cEnumByValueFunctionGenerator) + CEnumCompanionGenerator(context, cEnumByValueFunctionGenerator) private val cEnumVarClassGenerator = - CEnumVarClassGenerator(context, stubGenerator, interopBuiltIns) + CEnumVarClassGenerator(context, interopBuiltIns) private val cEnumClassGenerator = - CEnumClassGenerator(context, stubGenerator, cEnumCompanionGenerator, cEnumVarClassGenerator) + CEnumClassGenerator(context, cEnumCompanionGenerator, cEnumVarClassGenerator) private val cStructClassGenerator = - CStructVarClassGenerator(context, stubGenerator, interopBuiltIns) + CStructVarClassGenerator(context, interopBuiltIns) var module: IrModuleFragment? = null set(value) { diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/interop/cenum/CEnumByValueFunctionGenerator.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/interop/cenum/CEnumByValueFunctionGenerator.kt index 9f6bc31a475..aba76b300d3 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/interop/cenum/CEnumByValueFunctionGenerator.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/interop/cenum/CEnumByValueFunctionGenerator.kt @@ -24,7 +24,6 @@ import org.jetbrains.kotlin.psi2ir.generators.GeneratorContext */ internal class CEnumByValueFunctionGenerator( context: GeneratorContext, - override val stubGenerator: DeclarationStubGenerator, private val symbols: KonanSymbols ) : DescriptorToIrTranslationMixin { diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/interop/cenum/CEnumClassGenerator.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/interop/cenum/CEnumClassGenerator.kt index a58dee29a00..95cd295045a 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/interop/cenum/CEnumClassGenerator.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/interop/cenum/CEnumClassGenerator.kt @@ -41,7 +41,6 @@ private val cEnumEntryValueTypes = setOf( internal class CEnumClassGenerator( val context: GeneratorContext, - override val stubGenerator: DeclarationStubGenerator, private val cEnumCompanionGenerator: CEnumCompanionGenerator, private val cEnumVarClassGenerator: CEnumVarClassGenerator ) : DescriptorToIrTranslationMixin { @@ -101,20 +100,16 @@ internal class CEnumClassGenerator( irExprBody(irGet(irClass.primaryConstructor!!.valueParameters[0])) } } - irProperty.getter = createFunction( - propertyDescriptor.getter!!, - IrDeclarationOrigin.DEFAULT_PROPERTY_ACCESSOR - ).also { getter -> - getter.correspondingPropertySymbol = irProperty.symbol - getter.body = irBuilder(irBuiltIns, getter.symbol, SYNTHETIC_OFFSET, SYNTHETIC_OFFSET).irBlockBody { - +irReturn( - irGetField( - irGet(getter.dispatchReceiverParameter!!), - irProperty.backingField!! - ) + } + val getter = irProperty.getter!! + getter.correspondingPropertySymbol = irProperty.symbol + getter.body = irBuilder(irBuiltIns, getter.symbol, SYNTHETIC_OFFSET, SYNTHETIC_OFFSET).irBlockBody { + +irReturn( + irGetField( + irGet(getter.dispatchReceiverParameter!!), + irProperty.backingField!! ) - } - } + ) } return irProperty } diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/interop/cenum/CEnumCompanionGenerator.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/interop/cenum/CEnumCompanionGenerator.kt index 9353317761d..673388fe1c6 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/interop/cenum/CEnumCompanionGenerator.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/interop/cenum/CEnumCompanionGenerator.kt @@ -26,7 +26,6 @@ private val cEnumEntryAliasAnnonation = FqName("kotlinx.cinterop.internal.CEnumE internal class CEnumCompanionGenerator( context: GeneratorContext, - override val stubGenerator: DeclarationStubGenerator, private val cEnumByValueFunctionGenerator: CEnumByValueFunctionGenerator ) : DescriptorToIrTranslationMixin { diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/interop/cenum/CEnumVarClassGenerator.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/interop/cenum/CEnumVarClassGenerator.kt index eafb70f7275..53d41f41e66 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/interop/cenum/CEnumVarClassGenerator.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/interop/cenum/CEnumVarClassGenerator.kt @@ -28,7 +28,6 @@ private val typeSizeAnnotation = FqName("kotlinx.cinterop.internal.CEnumVarTypeS internal class CEnumVarClassGenerator( context: GeneratorContext, - override val stubGenerator: DeclarationStubGenerator, private val interopBuiltIns: InteropBuiltIns ) : DescriptorToIrTranslationMixin { diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/interop/cstruct/CStructVarClassGenerator.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/interop/cstruct/CStructVarClassGenerator.kt index 6712917e148..9fb24a69aed 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/interop/cstruct/CStructVarClassGenerator.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/interop/cstruct/CStructVarClassGenerator.kt @@ -23,7 +23,6 @@ import org.jetbrains.kotlin.psi2ir.generators.GeneratorContext internal class CStructVarClassGenerator( context: GeneratorContext, - override val stubGenerator: DeclarationStubGenerator, private val interopBuiltIns: InteropBuiltIns ) : DescriptorToIrTranslationMixin { @@ -31,7 +30,7 @@ internal class CStructVarClassGenerator( override val symbolTable: SymbolTable = context.symbolTable override val typeTranslator: TypeTranslator = context.typeTranslator - private val companionGenerator = CStructVarCompanionGenerator(context, stubGenerator, interopBuiltIns) + private val companionGenerator = CStructVarCompanionGenerator(context, interopBuiltIns) fun findOrGenerateCStruct(classDescriptor: ClassDescriptor, parent: IrDeclarationContainer): IrClass { val irClassSymbol = symbolTable.referenceClass(classDescriptor) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/interop/cstruct/CStructVarCompanionGenerator.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/interop/cstruct/CStructVarCompanionGenerator.kt index 082f5067876..a2d3ddc69f1 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/interop/cstruct/CStructVarCompanionGenerator.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ir/interop/cstruct/CStructVarCompanionGenerator.kt @@ -26,7 +26,6 @@ private val varTypeAnnotationFqName = FqName("kotlinx.cinterop.internal.CStruct. internal class CStructVarCompanionGenerator( context: GeneratorContext, - override val stubGenerator: DeclarationStubGenerator, private val interopBuiltIns: InteropBuiltIns ) : DescriptorToIrTranslationMixin {