[Interop][StubIr] Add origin to TypealiasStub

This commit is contained in:
Sergey Bogolepov
2019-11-19 12:27:51 +07:00
committed by Sergey Bogolepov
parent a4486de7fa
commit 0712a2ae48
2 changed files with 21 additions and 14 deletions
@@ -105,6 +105,7 @@ sealed class StubOrigin {
class Function(val function: FunctionDecl) : StubOrigin() class Function(val function: FunctionDecl) : StubOrigin()
// TODO: Unused, remove.
class FunctionParameter(val parameter: Parameter) : StubOrigin() class FunctionParameter(val parameter: Parameter) : StubOrigin()
class Struct(val struct: StructDecl) : StubOrigin() class Struct(val struct: StructDecl) : StubOrigin()
@@ -112,6 +113,10 @@ sealed class StubOrigin {
class Constant(val constantDef: ConstantDef): StubOrigin() class Constant(val constantDef: ConstantDef): StubOrigin()
class Global(val global: GlobalDecl) : StubOrigin() class Global(val global: GlobalDecl) : StubOrigin()
class TypeDef(val typedefDef: TypedefDef) : StubOrigin()
class VarOf(val typeOrigin: StubOrigin) : StubOrigin()
} }
interface StubElementWithOrigin : StubIrElement { interface StubElementWithOrigin : StubIrElement {
@@ -427,7 +432,8 @@ class EnumEntryStub(
class TypealiasStub( class TypealiasStub(
val alias: Classifier, val alias: Classifier,
val aliasee: StubType val aliasee: StubType,
val origin: StubOrigin
) : StubIrElement { ) : StubIrElement {
override fun <T, R> accept(visitor: StubIrVisitor<T, R>, data: T) = override fun <T, R> accept(visitor: StubIrVisitor<T, R>, data: T) =
@@ -235,26 +235,26 @@ internal class EnumStubBuilder(
/** /**
* Produces to [out] the Kotlin definitions for given enum which shouldn't be represented as Kotlin enum. * Produces to [out] the Kotlin definitions for given enum which shouldn't be represented as Kotlin enum.
*/ */
private fun generateEnumAsConstants(e: EnumDef): List<StubIrElement> { private fun generateEnumAsConstants(enumDef: EnumDef): List<StubIrElement> {
// TODO: if this enum defines e.g. a type of struct field, then it should be generated inside the struct class // 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 // to prevent name clashing
val entries = mutableListOf<PropertyStub>() val entries = mutableListOf<PropertyStub>()
val typealiases = mutableListOf<TypealiasStub>() val typealiases = mutableListOf<TypealiasStub>()
val constants = e.constants.filter { val constants = enumDef.constants.filter {
// Macro "overrides" the original enum constant. // Macro "overrides" the original enum constant.
it.name !in context.macroConstantsByName it.name !in context.macroConstantsByName
} }
val kotlinType: KotlinType val kotlinType: KotlinType
val baseKotlinType = context.mirror(e.baseType).argType val baseKotlinType = context.mirror(enumDef.baseType).argType
val meta = if (e.isAnonymous) { val meta = if (enumDef.isAnonymous) {
kotlinType = baseKotlinType kotlinType = baseKotlinType
StubContainerMeta(textAtStart = if (constants.isNotEmpty()) "// ${e.spelling}:" else "") StubContainerMeta(textAtStart = if (constants.isNotEmpty()) "// ${enumDef.spelling}:" else "")
} else { } else {
val typeMirror = context.mirror(EnumType(e)) val typeMirror = context.mirror(EnumType(enumDef))
if (typeMirror !is TypeMirror.ByValue) { if (typeMirror !is TypeMirror.ByValue) {
error("unexpected enum type mirror: $typeMirror") error("unexpected enum type mirror: $typeMirror")
} }
@@ -262,15 +262,16 @@ internal class EnumStubBuilder(
val varTypeName = typeMirror.info.constructPointedType(typeMirror.valueType) val varTypeName = typeMirror.info.constructPointedType(typeMirror.valueType)
val varTypeClassifier = typeMirror.pointedType.classifier val varTypeClassifier = typeMirror.pointedType.classifier
val valueTypeClassifier = typeMirror.valueType.classifier val valueTypeClassifier = typeMirror.valueType.classifier
typealiases += TypealiasStub(varTypeClassifier, varTypeName.toStubIrType()) val origin = StubOrigin.Enum(enumDef)
typealiases += TypealiasStub(valueTypeClassifier, baseKotlinType.toStubIrType()) typealiases += TypealiasStub(varTypeClassifier, varTypeName.toStubIrType(), StubOrigin.VarOf(origin))
typealiases += TypealiasStub(valueTypeClassifier, baseKotlinType.toStubIrType(), origin)
kotlinType = typeMirror.valueType kotlinType = typeMirror.valueType
StubContainerMeta() StubContainerMeta()
} }
for (constant in constants) { 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 getter = PropertyAccessor.Getter.SimpleGetter(constant = literal)
val kind = PropertyStub.Kind.Val(getter) val kind = PropertyStub.Kind.Val(getter)
entries += PropertyStub( entries += PropertyStub(
@@ -505,21 +506,21 @@ internal class TypedefStubBuilder(
override fun build(): List<StubIrElement> { override fun build(): List<StubIrElement> {
val mirror = context.mirror(Typedef(typedefDef)) val mirror = context.mirror(Typedef(typedefDef))
val baseMirror = context.mirror(typedefDef.aliased) val baseMirror = context.mirror(typedefDef.aliased)
val varType = mirror.pointedType.classifier val varType = mirror.pointedType.classifier
val origin = StubOrigin.TypeDef(typedefDef)
return when (baseMirror) { return when (baseMirror) {
is TypeMirror.ByValue -> { is TypeMirror.ByValue -> {
val valueType = (mirror as TypeMirror.ByValue).valueType val valueType = (mirror as TypeMirror.ByValue).valueType
val varTypeAliasee = mirror.info.constructPointedType(valueType) val varTypeAliasee = mirror.info.constructPointedType(valueType)
val valueTypeAliasee = baseMirror.valueType val valueTypeAliasee = baseMirror.valueType
listOf( listOf(
TypealiasStub(varType, varTypeAliasee.toStubIrType()), TypealiasStub(varType, varTypeAliasee.toStubIrType(), StubOrigin.VarOf(origin)),
TypealiasStub(valueType.classifier, valueTypeAliasee.toStubIrType()) TypealiasStub(valueType.classifier, valueTypeAliasee.toStubIrType(), origin)
) )
} }
is TypeMirror.ByRef -> { is TypeMirror.ByRef -> {
val varTypeAliasee = baseMirror.pointedType val varTypeAliasee = baseMirror.pointedType
listOf(TypealiasStub(varType, varTypeAliasee.toStubIrType())) listOf(TypealiasStub(varType, varTypeAliasee.toStubIrType(), origin))
} }
} }
} }