[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.
This commit is contained in:
Sergey Bogolepov
2020-01-31 20:03:00 +07:00
committed by Sergey Bogolepov
parent 2e594bb19c
commit 91fb5eca76
9 changed files with 74 additions and 40 deletions
@@ -207,7 +207,7 @@ internal val psiToIrPhase = konanUnitPhase(
moduleDescriptor, symbolTable, moduleDescriptor, symbolTable,
config.configuration.languageVersionSettings config.configuration.languageVersionSettings
) )
val irProviderForCEnumsAndCStructs = IrProviderForCEnumAndCStructStubs(generatorContext, interopBuiltIns, stubGenerator, symbols) val irProviderForCEnumsAndCStructs = IrProviderForCEnumAndCStructStubs(generatorContext, interopBuiltIns, symbols)
val irProviderForInteropStubs = IrProviderForInteropStubs(irProviderForCEnumsAndCStructs::canHandleSymbol) val irProviderForInteropStubs = IrProviderForInteropStubs(irProviderForCEnumsAndCStructs::canHandleSymbol)
val irProviders = listOf( val irProviders = listOf(
irProviderForCEnumsAndCStructs, irProviderForCEnumsAndCStructs,
@@ -38,8 +38,6 @@ internal interface DescriptorToIrTranslationMixin {
val typeTranslator: TypeTranslator val typeTranslator: TypeTranslator
val stubGenerator: DeclarationStubGenerator
fun KotlinType.toIrType() = typeTranslator.translateType(this) fun KotlinType.toIrType() = typeTranslator.translateType(this)
/** /**
@@ -58,9 +56,7 @@ internal interface DescriptorToIrTranslationMixin {
descriptor.typeConstructor.supertypes.mapTo(irClass.superTypes) { descriptor.typeConstructor.supertypes.mapTo(irClass.superTypes) {
it.toIrType() it.toIrType()
} }
descriptor.annotations.mapTo(irClass.annotations) { irClass.generateAnnotations()
typeTranslator.constantValueGenerator.generateAnnotationConstructorCall(it)!!
}
irClass.createParameterDeclarations() irClass.createParameterDeclarations()
builder(irClass) builder(irClass)
createFakeOverrides(descriptor).forEach(irClass::addMember) createFakeOverrides(descriptor).forEach(irClass::addMember)
@@ -81,19 +77,68 @@ internal interface DescriptorToIrTranslationMixin {
} }
} }
fun createConstructor(constructorDescriptor: ClassConstructorDescriptor): IrConstructor = fun createConstructor(constructorDescriptor: ClassConstructorDescriptor): IrConstructor {
stubGenerator.generateMemberStub(constructorDescriptor) as 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 = fun createProperty(propertyDescriptor: PropertyDescriptor): IrProperty {
stubGenerator.generateMemberStub(propertyDescriptor) as 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( fun createFunction(
functionDescriptor: FunctionDescriptor, functionDescriptor: FunctionDescriptor,
origin: IrDeclarationOrigin? = IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB origin: IrDeclarationOrigin = IrDeclarationOrigin.IR_EXTERNAL_DECLARATION_STUB
): IrSimpleFunction = ): IrSimpleFunction {
stubGenerator.generateFunctionStub(functionDescriptor, createPropertyIfNeeded = false).also { val irFunction = symbolTable.declareSimpleFunctionWithOverrides(SYNTHETIC_OFFSET, SYNTHETIC_OFFSET, origin, functionDescriptor)
if (origin != null) it.origin = origin 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 = internal fun IrBuilder.irInstanceInitializer(classSymbol: IrClassSymbol): IrExpression =
@@ -42,7 +42,6 @@ import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
internal class IrProviderForCEnumAndCStructStubs( internal class IrProviderForCEnumAndCStructStubs(
context: GeneratorContext, context: GeneratorContext,
private val interopBuiltIns: InteropBuiltIns, private val interopBuiltIns: InteropBuiltIns,
stubGenerator: DeclarationStubGenerator,
symbols: KonanSymbols symbols: KonanSymbols
) : IrProvider { ) : IrProvider {
@@ -51,15 +50,15 @@ internal class IrProviderForCEnumAndCStructStubs(
private val filesMap = mutableMapOf<PackageFragmentDescriptor, IrFile>() private val filesMap = mutableMapOf<PackageFragmentDescriptor, IrFile>()
private val cEnumByValueFunctionGenerator = private val cEnumByValueFunctionGenerator =
CEnumByValueFunctionGenerator(context, stubGenerator, symbols) CEnumByValueFunctionGenerator(context, symbols)
private val cEnumCompanionGenerator = private val cEnumCompanionGenerator =
CEnumCompanionGenerator(context, stubGenerator, cEnumByValueFunctionGenerator) CEnumCompanionGenerator(context, cEnumByValueFunctionGenerator)
private val cEnumVarClassGenerator = private val cEnumVarClassGenerator =
CEnumVarClassGenerator(context, stubGenerator, interopBuiltIns) CEnumVarClassGenerator(context, interopBuiltIns)
private val cEnumClassGenerator = private val cEnumClassGenerator =
CEnumClassGenerator(context, stubGenerator, cEnumCompanionGenerator, cEnumVarClassGenerator) CEnumClassGenerator(context, cEnumCompanionGenerator, cEnumVarClassGenerator)
private val cStructClassGenerator = private val cStructClassGenerator =
CStructVarClassGenerator(context, stubGenerator, interopBuiltIns) CStructVarClassGenerator(context, interopBuiltIns)
var module: IrModuleFragment? = null var module: IrModuleFragment? = null
set(value) { set(value) {
@@ -24,7 +24,6 @@ import org.jetbrains.kotlin.psi2ir.generators.GeneratorContext
*/ */
internal class CEnumByValueFunctionGenerator( internal class CEnumByValueFunctionGenerator(
context: GeneratorContext, context: GeneratorContext,
override val stubGenerator: DeclarationStubGenerator,
private val symbols: KonanSymbols private val symbols: KonanSymbols
) : DescriptorToIrTranslationMixin { ) : DescriptorToIrTranslationMixin {
@@ -41,7 +41,6 @@ private val cEnumEntryValueTypes = setOf(
internal class CEnumClassGenerator( internal class CEnumClassGenerator(
val context: GeneratorContext, val context: GeneratorContext,
override val stubGenerator: DeclarationStubGenerator,
private val cEnumCompanionGenerator: CEnumCompanionGenerator, private val cEnumCompanionGenerator: CEnumCompanionGenerator,
private val cEnumVarClassGenerator: CEnumVarClassGenerator private val cEnumVarClassGenerator: CEnumVarClassGenerator
) : DescriptorToIrTranslationMixin { ) : DescriptorToIrTranslationMixin {
@@ -101,20 +100,16 @@ internal class CEnumClassGenerator(
irExprBody(irGet(irClass.primaryConstructor!!.valueParameters[0])) irExprBody(irGet(irClass.primaryConstructor!!.valueParameters[0]))
} }
} }
irProperty.getter = createFunction( }
propertyDescriptor.getter!!, val getter = irProperty.getter!!
IrDeclarationOrigin.DEFAULT_PROPERTY_ACCESSOR getter.correspondingPropertySymbol = irProperty.symbol
).also { getter -> getter.body = irBuilder(irBuiltIns, getter.symbol, SYNTHETIC_OFFSET, SYNTHETIC_OFFSET).irBlockBody {
getter.correspondingPropertySymbol = irProperty.symbol +irReturn(
getter.body = irBuilder(irBuiltIns, getter.symbol, SYNTHETIC_OFFSET, SYNTHETIC_OFFSET).irBlockBody { irGetField(
+irReturn( irGet(getter.dispatchReceiverParameter!!),
irGetField( irProperty.backingField!!
irGet(getter.dispatchReceiverParameter!!),
irProperty.backingField!!
)
) )
} )
}
} }
return irProperty return irProperty
} }
@@ -26,7 +26,6 @@ private val cEnumEntryAliasAnnonation = FqName("kotlinx.cinterop.internal.CEnumE
internal class CEnumCompanionGenerator( internal class CEnumCompanionGenerator(
context: GeneratorContext, context: GeneratorContext,
override val stubGenerator: DeclarationStubGenerator,
private val cEnumByValueFunctionGenerator: CEnumByValueFunctionGenerator private val cEnumByValueFunctionGenerator: CEnumByValueFunctionGenerator
) : DescriptorToIrTranslationMixin { ) : DescriptorToIrTranslationMixin {
@@ -28,7 +28,6 @@ private val typeSizeAnnotation = FqName("kotlinx.cinterop.internal.CEnumVarTypeS
internal class CEnumVarClassGenerator( internal class CEnumVarClassGenerator(
context: GeneratorContext, context: GeneratorContext,
override val stubGenerator: DeclarationStubGenerator,
private val interopBuiltIns: InteropBuiltIns private val interopBuiltIns: InteropBuiltIns
) : DescriptorToIrTranslationMixin { ) : DescriptorToIrTranslationMixin {
@@ -23,7 +23,6 @@ import org.jetbrains.kotlin.psi2ir.generators.GeneratorContext
internal class CStructVarClassGenerator( internal class CStructVarClassGenerator(
context: GeneratorContext, context: GeneratorContext,
override val stubGenerator: DeclarationStubGenerator,
private val interopBuiltIns: InteropBuiltIns private val interopBuiltIns: InteropBuiltIns
) : DescriptorToIrTranslationMixin { ) : DescriptorToIrTranslationMixin {
@@ -31,7 +30,7 @@ internal class CStructVarClassGenerator(
override val symbolTable: SymbolTable = context.symbolTable override val symbolTable: SymbolTable = context.symbolTable
override val typeTranslator: TypeTranslator = context.typeTranslator 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 { fun findOrGenerateCStruct(classDescriptor: ClassDescriptor, parent: IrDeclarationContainer): IrClass {
val irClassSymbol = symbolTable.referenceClass(classDescriptor) val irClassSymbol = symbolTable.referenceClass(classDescriptor)
@@ -26,7 +26,6 @@ private val varTypeAnnotationFqName = FqName("kotlinx.cinterop.internal.CStruct.
internal class CStructVarCompanionGenerator( internal class CStructVarCompanionGenerator(
context: GeneratorContext, context: GeneratorContext,
override val stubGenerator: DeclarationStubGenerator,
private val interopBuiltIns: InteropBuiltIns private val interopBuiltIns: InteropBuiltIns
) : DescriptorToIrTranslationMixin { ) : DescriptorToIrTranslationMixin {