diff --git a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/StubIr.kt b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/StubIr.kt index 9f743bd467d..d013e474d2f 100644 --- a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/StubIr.kt +++ b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/StubIr.kt @@ -105,6 +105,7 @@ sealed class StubOrigin { class Function(val function: FunctionDecl) : StubOrigin() + // TODO: Unused, remove. class FunctionParameter(val parameter: Parameter) : StubOrigin() class Struct(val struct: StructDecl) : StubOrigin() @@ -112,6 +113,10 @@ sealed class StubOrigin { class Constant(val constantDef: ConstantDef): StubOrigin() class Global(val global: GlobalDecl) : StubOrigin() + + class TypeDef(val typedefDef: TypedefDef) : StubOrigin() + + class VarOf(val typeOrigin: StubOrigin) : StubOrigin() } interface StubElementWithOrigin : StubIrElement { @@ -427,7 +432,8 @@ class EnumEntryStub( class TypealiasStub( val alias: Classifier, - val aliasee: StubType + val aliasee: StubType, + val origin: StubOrigin ) : StubIrElement { override fun accept(visitor: StubIrVisitor, data: T) = diff --git a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/StubIrElementBuilders.kt b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/StubIrElementBuilders.kt index 9aff6dcfa66..aef2a0fe172 100644 --- a/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/StubIrElementBuilders.kt +++ b/Interop/StubGenerator/src/main/kotlin/org/jetbrains/kotlin/native/interop/gen/StubIrElementBuilders.kt @@ -235,26 +235,26 @@ internal class EnumStubBuilder( /** * Produces to [out] the Kotlin definitions for given enum which shouldn't be represented as Kotlin enum. */ - private fun generateEnumAsConstants(e: EnumDef): List { + private fun generateEnumAsConstants(enumDef: EnumDef): List { // TODO: if this enum defines e.g. a type of struct field, then it should be generated inside the struct class // to prevent name clashing val entries = mutableListOf() val typealiases = mutableListOf() - val constants = e.constants.filter { + val constants = enumDef.constants.filter { // Macro "overrides" the original enum constant. it.name !in context.macroConstantsByName } val kotlinType: KotlinType - val baseKotlinType = context.mirror(e.baseType).argType - val meta = if (e.isAnonymous) { + val baseKotlinType = context.mirror(enumDef.baseType).argType + val meta = if (enumDef.isAnonymous) { kotlinType = baseKotlinType - StubContainerMeta(textAtStart = if (constants.isNotEmpty()) "// ${e.spelling}:" else "") + StubContainerMeta(textAtStart = if (constants.isNotEmpty()) "// ${enumDef.spelling}:" else "") } else { - val typeMirror = context.mirror(EnumType(e)) + val typeMirror = context.mirror(EnumType(enumDef)) if (typeMirror !is TypeMirror.ByValue) { error("unexpected enum type mirror: $typeMirror") } @@ -262,15 +262,16 @@ internal class EnumStubBuilder( val varTypeName = typeMirror.info.constructPointedType(typeMirror.valueType) val varTypeClassifier = typeMirror.pointedType.classifier val valueTypeClassifier = typeMirror.valueType.classifier - typealiases += TypealiasStub(varTypeClassifier, varTypeName.toStubIrType()) - typealiases += TypealiasStub(valueTypeClassifier, baseKotlinType.toStubIrType()) + val origin = StubOrigin.Enum(enumDef) + typealiases += TypealiasStub(varTypeClassifier, varTypeName.toStubIrType(), StubOrigin.VarOf(origin)) + typealiases += TypealiasStub(valueTypeClassifier, baseKotlinType.toStubIrType(), origin) kotlinType = typeMirror.valueType StubContainerMeta() } for (constant in constants) { - val literal = context.tryCreateIntegralStub(e.baseType, constant.value) ?: continue + val literal = context.tryCreateIntegralStub(enumDef.baseType, constant.value) ?: continue val getter = PropertyAccessor.Getter.SimpleGetter(constant = literal) val kind = PropertyStub.Kind.Val(getter) entries += PropertyStub( @@ -505,21 +506,21 @@ internal class TypedefStubBuilder( override fun build(): List { val mirror = context.mirror(Typedef(typedefDef)) val baseMirror = context.mirror(typedefDef.aliased) - val varType = mirror.pointedType.classifier + val origin = StubOrigin.TypeDef(typedefDef) return when (baseMirror) { is TypeMirror.ByValue -> { val valueType = (mirror as TypeMirror.ByValue).valueType val varTypeAliasee = mirror.info.constructPointedType(valueType) val valueTypeAliasee = baseMirror.valueType listOf( - TypealiasStub(varType, varTypeAliasee.toStubIrType()), - TypealiasStub(valueType.classifier, valueTypeAliasee.toStubIrType()) + TypealiasStub(varType, varTypeAliasee.toStubIrType(), StubOrigin.VarOf(origin)), + TypealiasStub(valueType.classifier, valueTypeAliasee.toStubIrType(), origin) ) } is TypeMirror.ByRef -> { val varTypeAliasee = baseMirror.pointedType - listOf(TypealiasStub(varType, varTypeAliasee.toStubIrType())) + listOf(TypealiasStub(varType, varTypeAliasee.toStubIrType(), origin)) } } }