[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,
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,
@@ -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 =
@@ -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<PackageFragmentDescriptor, IrFile>()
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) {
@@ -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 {
@@ -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
}
@@ -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 {
@@ -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 {
@@ -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)
@@ -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 {