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),
parameters = element.parameters.map { visitFunctionParameter(it) },
origin = element.origin,
annotations = emptyList(),
annotations = mutableListOf(),
external = false,
receiver = element.receiver?.let { visitReceiverParameter(it) },
modality = element.modality,
@@ -31,7 +31,7 @@ class DeepCopyForManagedWrapper(val originalClass: ClassStub, val context: Stubs
name = element.name,
type = element.type,
origin = element.origin,
annotations = emptyList(),
annotations = mutableListOf(),
receiverType = element.receiverType,
modality = element.modality,
isOverride = element.isOverride,
@@ -231,7 +231,7 @@ private class ObjCMethodStubBuilder(
typeParameters = listOf(typeParameter),
external = true,
origin = StubOrigin.ObjCCategoryInitMethod(method),
annotations = annotations,
annotations = annotations.toMutableList(),
modality = MemberStubModality.FINAL
)
// TODO: Should we deprecate it as well?
@@ -252,7 +252,7 @@ private class ObjCMethodStubBuilder(
stubReturnType,
kotlinMethodParameters.toList(),
origin,
annotations.toList(),
annotations.toMutableList(),
external,
receiver,
modality,
@@ -645,7 +645,15 @@ private class ObjCPropertyStubBuilder(
} else {
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 modality: MemberStubModality = MemberStubModality.FINAL,
val receiverType: StubType? = null,
override val annotations: List<AnnotationStub> = emptyList(),
override val annotations: MutableList<AnnotationStub> = mutableListOf(),
val origin: StubOrigin,
val isOverride: Boolean = false
) : StubIrElement, AnnotationHolder {
@@ -345,11 +345,12 @@ sealed class ClassStub : StubContainer(), StubElementWithOrigin, AnnotationHolde
override val interfaces: List<StubType> = emptyList(),
override val properties: List<PropertyStub> = emptyList(),
override val origin: StubOrigin,
override val annotations: List<AnnotationStub> = emptyList(),
annotations: List<AnnotationStub> = emptyList(),
override val childrenClasses: List<ClassStub> = emptyList(),
override val companion: Companion? = null,
override val simpleContainers: List<SimpleStubContainer> = emptyList()
) : ClassStub() {
override val annotations = annotations.toMutableList()
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 properties: List<PropertyStub> = emptyList(),
override val origin: StubOrigin,
override val annotations: List<AnnotationStub> = emptyList(),
annotations: List<AnnotationStub> = emptyList(),
override val childrenClasses: List<ClassStub> = emptyList(),
override val companion: Companion?= null,
override val simpleContainers: List<SimpleStubContainer> = emptyList()
) : ClassStub() {
override val annotations = annotations.toMutableList()
override val functions: List<FunctionalStub> = constructors
}
@@ -506,7 +508,7 @@ data class FunctionStub(
val returnType: StubType,
override val parameters: List<FunctionParameterStub>,
override val origin: StubOrigin,
override val annotations: List<AnnotationStub>,
override val annotations: MutableList<AnnotationStub>,
val external: Boolean = false,
val receiver: ReceiverParameterStub?,
val modality: MemberStubModality,
@@ -543,7 +545,7 @@ class TypealiasStub(
val alias: Classifier,
val aliasee: StubType,
val origin: StubOrigin,
override val annotations: List<AnnotationStub> = emptyList(),
override val annotations: MutableList<AnnotationStub> = mutableListOf(),
) : StubIrElement, AnnotationHolder {
override fun <T, R> accept(visitor: StubIrVisitor<T, R>, data: T) =
@@ -95,7 +95,7 @@ internal class StructStubBuilder(
returnType = ClassifierStubType(Classifier("kotlin", "Unit")),
parameters = emptyList(),
origin = StubOrigin.Synthetic.ManagedTypeDetails,
annotations = emptyList(),
annotations = mutableListOf(),
receiver = null, // ReceiverParameterStub()
modality = MemberStubModality.FINAL
)
@@ -140,7 +140,7 @@ internal class StructStubBuilder(
}
val kind = PropertyStub.Kind.Val(getter)
// 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 {
val pointedType = fieldRefType.pointedType.toStubIrType()
val pointedTypeArgument = TypeArgumentStub(pointedType)
@@ -460,7 +460,7 @@ internal class EnumStubBuilder(
origin = StubOrigin.Synthetic.EnumByValue(enumDef),
receiver = null,
modality = MemberStubModality.FINAL,
annotations = listOf(AnnotationStub.Deprecated.deprecatedCEnumByValue)
annotations = mutableListOf(AnnotationStub.Deprecated.deprecatedCEnumByValue)
)
val companion = ClassStub.Companion(
@@ -494,7 +494,7 @@ internal class EnumStubBuilder(
ClassifierStubType(classifier),
kind = PropertyStub.Kind.Val(PropertyAccessor.Getter.GetEnumEntry(entry)),
origin = StubOrigin.EnumEntry(enumConstant),
annotations = listOfNotNull(aliasAnnotation)
annotations = listOfNotNull(aliasAnnotation).toMutableList()
)
}
@@ -782,7 +782,7 @@ internal class FunctionStubBuilder(
returnType,
parameters.toList(),
StubOrigin.Function(func),
annotations,
annotations.toMutableList(),
mustBeExternal,
null,
MemberStubModality.FINAL,
@@ -155,7 +155,7 @@ internal class ModuleMetadataEmitter(
} else {
element.copy(
external = false,
annotations = listOf(AnnotationStub.Deprecated.unableToImport)
annotations = mutableListOf(AnnotationStub.Deprecated.unableToImport)
)
}
KmFunction(function.name).also { km ->
@@ -173,7 +173,7 @@ internal class ModuleMetadataEmitter(
val property = when (val bridgeSupportedKind = element.bridgeSupportedKind) {
null -> element.copy(
kind = PropertyStub.Kind.Val(PropertyAccessor.Getter.SimpleGetter()),
annotations = listOf(AnnotationStub.Deprecated.unableToImport)
annotations = mutableListOf(AnnotationStub.Deprecated.unableToImport)
)
element.kind -> element
else -> element.copy(kind = bridgeSupportedKind)
@@ -27,7 +27,7 @@ class StubIrToMetadataTests {
returnType = intStubType,
parameters = listOf(),
origin = origin,
annotations = emptyList(),
annotations = mutableListOf(),
external = true,
receiver = null,
modality = MemberStubModality.FINAL