Native: make .annotations mutable for some Stub IR nodes

cinterop tool should add ExperimentalForeignApi to all generated
declarations by default. To make this possible to implement in a
non-intrusive manner, this commit enables adding annotations to some of
the existing Stub IR nodes.
After that, one can add annotations to all generated declarations with
a simple post pass.

^KT-58362
This commit is contained in:
Svyatoslav Scherbina
2023-07-06 12:31:14 +02:00
committed by Space Team
parent d118844b17
commit 93ef98cbec
6 changed files with 28 additions and 18 deletions
@@ -16,7 +16,7 @@ class DeepCopyForManagedWrapper(val originalClass: ClassStub, val context: Stubs
returnType = transformCPointerToManaged(element.returnType), returnType = transformCPointerToManaged(element.returnType),
parameters = element.parameters.map { visitFunctionParameter(it) }, parameters = element.parameters.map { visitFunctionParameter(it) },
origin = element.origin, origin = element.origin,
annotations = emptyList(), annotations = mutableListOf(),
external = false, external = false,
receiver = element.receiver?.let { visitReceiverParameter(it) }, receiver = element.receiver?.let { visitReceiverParameter(it) },
modality = element.modality, modality = element.modality,
@@ -31,7 +31,7 @@ class DeepCopyForManagedWrapper(val originalClass: ClassStub, val context: Stubs
name = element.name, name = element.name,
type = element.type, type = element.type,
origin = element.origin, origin = element.origin,
annotations = emptyList(), annotations = mutableListOf(),
receiverType = element.receiverType, receiverType = element.receiverType,
modality = element.modality, modality = element.modality,
isOverride = element.isOverride, isOverride = element.isOverride,
@@ -231,7 +231,7 @@ private class ObjCMethodStubBuilder(
typeParameters = listOf(typeParameter), typeParameters = listOf(typeParameter),
external = true, external = true,
origin = StubOrigin.ObjCCategoryInitMethod(method), origin = StubOrigin.ObjCCategoryInitMethod(method),
annotations = annotations, annotations = annotations.toMutableList(),
modality = MemberStubModality.FINAL modality = MemberStubModality.FINAL
) )
// TODO: Should we deprecate it as well? // TODO: Should we deprecate it as well?
@@ -252,7 +252,7 @@ private class ObjCMethodStubBuilder(
stubReturnType, stubReturnType,
kotlinMethodParameters.toList(), kotlinMethodParameters.toList(),
origin, origin,
annotations.toList(), annotations.toMutableList(),
external, external,
receiver, receiver,
modality, modality,
@@ -645,7 +645,15 @@ private class ObjCPropertyStubBuilder(
} else { } else {
emptyList() emptyList()
} }
return listOf(PropertyStub(mangleSimple(property.name), kotlinType.toStubIrType(), kind, modality, receiver, annotations, origin = origin)) return listOf(PropertyStub(
mangleSimple(property.name),
kotlinType.toStubIrType(),
kind,
modality,
receiver,
annotations.toMutableList(),
origin = origin
))
} }
} }
@@ -288,7 +288,7 @@ data class PropertyStub(
val kind: Kind, val kind: Kind,
val modality: MemberStubModality = MemberStubModality.FINAL, val modality: MemberStubModality = MemberStubModality.FINAL,
val receiverType: StubType? = null, val receiverType: StubType? = null,
override val annotations: List<AnnotationStub> = emptyList(), override val annotations: MutableList<AnnotationStub> = mutableListOf(),
val origin: StubOrigin, val origin: StubOrigin,
val isOverride: Boolean = false val isOverride: Boolean = false
) : StubIrElement, AnnotationHolder { ) : StubIrElement, AnnotationHolder {
@@ -345,11 +345,12 @@ sealed class ClassStub : StubContainer(), StubElementWithOrigin, AnnotationHolde
override val interfaces: List<StubType> = emptyList(), override val interfaces: List<StubType> = emptyList(),
override val properties: List<PropertyStub> = emptyList(), override val properties: List<PropertyStub> = emptyList(),
override val origin: StubOrigin, override val origin: StubOrigin,
override val annotations: List<AnnotationStub> = emptyList(), annotations: List<AnnotationStub> = emptyList(),
override val childrenClasses: List<ClassStub> = emptyList(), override val childrenClasses: List<ClassStub> = emptyList(),
override val companion: Companion? = null, override val companion: Companion? = null,
override val simpleContainers: List<SimpleStubContainer> = emptyList() override val simpleContainers: List<SimpleStubContainer> = emptyList()
) : ClassStub() { ) : ClassStub() {
override val annotations = annotations.toMutableList()
override val functions: List<FunctionalStub> = constructors + methods override val functions: List<FunctionalStub> = constructors + methods
} }
@@ -377,11 +378,12 @@ sealed class ClassStub : StubContainer(), StubElementWithOrigin, AnnotationHolde
override val interfaces: List<StubType> = emptyList(), override val interfaces: List<StubType> = emptyList(),
override val properties: List<PropertyStub> = emptyList(), override val properties: List<PropertyStub> = emptyList(),
override val origin: StubOrigin, override val origin: StubOrigin,
override val annotations: List<AnnotationStub> = emptyList(), annotations: List<AnnotationStub> = emptyList(),
override val childrenClasses: List<ClassStub> = emptyList(), override val childrenClasses: List<ClassStub> = emptyList(),
override val companion: Companion?= null, override val companion: Companion?= null,
override val simpleContainers: List<SimpleStubContainer> = emptyList() override val simpleContainers: List<SimpleStubContainer> = emptyList()
) : ClassStub() { ) : ClassStub() {
override val annotations = annotations.toMutableList()
override val functions: List<FunctionalStub> = constructors override val functions: List<FunctionalStub> = constructors
} }
@@ -506,7 +508,7 @@ data class FunctionStub(
val returnType: StubType, val returnType: StubType,
override val parameters: List<FunctionParameterStub>, override val parameters: List<FunctionParameterStub>,
override val origin: StubOrigin, override val origin: StubOrigin,
override val annotations: List<AnnotationStub>, override val annotations: MutableList<AnnotationStub>,
val external: Boolean = false, val external: Boolean = false,
val receiver: ReceiverParameterStub?, val receiver: ReceiverParameterStub?,
val modality: MemberStubModality, val modality: MemberStubModality,
@@ -543,7 +545,7 @@ class TypealiasStub(
val alias: Classifier, val alias: Classifier,
val aliasee: StubType, val aliasee: StubType,
val origin: StubOrigin, val origin: StubOrigin,
override val annotations: List<AnnotationStub> = emptyList(), override val annotations: MutableList<AnnotationStub> = mutableListOf(),
) : StubIrElement, AnnotationHolder { ) : StubIrElement, AnnotationHolder {
override fun <T, R> accept(visitor: StubIrVisitor<T, R>, data: T) = override fun <T, R> accept(visitor: StubIrVisitor<T, R>, data: T) =
@@ -95,7 +95,7 @@ internal class StructStubBuilder(
returnType = ClassifierStubType(Classifier("kotlin", "Unit")), returnType = ClassifierStubType(Classifier("kotlin", "Unit")),
parameters = emptyList(), parameters = emptyList(),
origin = StubOrigin.Synthetic.ManagedTypeDetails, origin = StubOrigin.Synthetic.ManagedTypeDetails,
annotations = emptyList(), annotations = mutableListOf(),
receiver = null, // ReceiverParameterStub() receiver = null, // ReceiverParameterStub()
modality = MemberStubModality.FINAL modality = MemberStubModality.FINAL
) )
@@ -140,7 +140,7 @@ internal class StructStubBuilder(
} }
val kind = PropertyStub.Kind.Val(getter) val kind = PropertyStub.Kind.Val(getter)
// TODO: Should receiver be added? // TODO: Should receiver be added?
PropertyStub(fieldName, type.toStubIrType(), kind, annotations = annotations, origin = origin) PropertyStub(fieldName, type.toStubIrType(), kind, annotations = annotations.toMutableList(), origin = origin)
} else { } else {
val pointedType = fieldRefType.pointedType.toStubIrType() val pointedType = fieldRefType.pointedType.toStubIrType()
val pointedTypeArgument = TypeArgumentStub(pointedType) val pointedTypeArgument = TypeArgumentStub(pointedType)
@@ -460,7 +460,7 @@ internal class EnumStubBuilder(
origin = StubOrigin.Synthetic.EnumByValue(enumDef), origin = StubOrigin.Synthetic.EnumByValue(enumDef),
receiver = null, receiver = null,
modality = MemberStubModality.FINAL, modality = MemberStubModality.FINAL,
annotations = listOf(AnnotationStub.Deprecated.deprecatedCEnumByValue) annotations = mutableListOf(AnnotationStub.Deprecated.deprecatedCEnumByValue)
) )
val companion = ClassStub.Companion( val companion = ClassStub.Companion(
@@ -494,7 +494,7 @@ internal class EnumStubBuilder(
ClassifierStubType(classifier), ClassifierStubType(classifier),
kind = PropertyStub.Kind.Val(PropertyAccessor.Getter.GetEnumEntry(entry)), kind = PropertyStub.Kind.Val(PropertyAccessor.Getter.GetEnumEntry(entry)),
origin = StubOrigin.EnumEntry(enumConstant), origin = StubOrigin.EnumEntry(enumConstant),
annotations = listOfNotNull(aliasAnnotation) annotations = listOfNotNull(aliasAnnotation).toMutableList()
) )
} }
@@ -782,7 +782,7 @@ internal class FunctionStubBuilder(
returnType, returnType,
parameters.toList(), parameters.toList(),
StubOrigin.Function(func), StubOrigin.Function(func),
annotations, annotations.toMutableList(),
mustBeExternal, mustBeExternal,
null, null,
MemberStubModality.FINAL, MemberStubModality.FINAL,
@@ -155,7 +155,7 @@ internal class ModuleMetadataEmitter(
} else { } else {
element.copy( element.copy(
external = false, external = false,
annotations = listOf(AnnotationStub.Deprecated.unableToImport) annotations = mutableListOf(AnnotationStub.Deprecated.unableToImport)
) )
} }
KmFunction(function.name).also { km -> KmFunction(function.name).also { km ->
@@ -173,7 +173,7 @@ internal class ModuleMetadataEmitter(
val property = when (val bridgeSupportedKind = element.bridgeSupportedKind) { val property = when (val bridgeSupportedKind = element.bridgeSupportedKind) {
null -> element.copy( null -> element.copy(
kind = PropertyStub.Kind.Val(PropertyAccessor.Getter.SimpleGetter()), kind = PropertyStub.Kind.Val(PropertyAccessor.Getter.SimpleGetter()),
annotations = listOf(AnnotationStub.Deprecated.unableToImport) annotations = mutableListOf(AnnotationStub.Deprecated.unableToImport)
) )
element.kind -> element element.kind -> element
else -> element.copy(kind = bridgeSupportedKind) else -> element.copy(kind = bridgeSupportedKind)
@@ -27,7 +27,7 @@ class StubIrToMetadataTests {
returnType = intStubType, returnType = intStubType,
parameters = listOf(), parameters = listOf(),
origin = origin, origin = origin,
annotations = emptyList(), annotations = mutableListOf(),
external = true, external = true,
receiver = null, receiver = null,
modality = MemberStubModality.FINAL modality = MemberStubModality.FINAL