[Commonizer] PropertyCommonizer: Support annotation commonization

^KT-62028 Verification Pending
This commit is contained in:
Sebastian Sellmair
2023-09-20 09:41:44 +02:00
committed by Space Team
parent f5b107d4f0
commit 9031deeacf
5 changed files with 29 additions and 6 deletions
@@ -8,6 +8,10 @@ repositories {
} }
kotlin { kotlin {
compilerOptions {
optIn.add("kotlinx.cinterop.ExperimentalForeignApi")
}
linuxArm64("shortPlatform") { linuxArm64("shortPlatform") {
compilations.get("main").cinterops.create("intPropertyInterop") { compilations.get("main").cinterops.create("intPropertyInterop") {
header(file("libs/shortPlatform.h")) header(file("libs/shortPlatform.h"))
@@ -31,7 +31,7 @@ class FunctionOrPropertyBaseCommonizer(
val extensionReceiver: CirExtensionReceiver?, val extensionReceiver: CirExtensionReceiver?,
val returnType: CirType, val returnType: CirType,
val typeParameters: List<CirTypeParameter>, val typeParameters: List<CirTypeParameter>,
val additionalAnnotations: List<CirAnnotation>, val annotations: List<CirAnnotation>,
) )
override fun invoke(values: List<CirFunctionOrProperty>): FunctionOrProperty? { override fun invoke(values: List<CirFunctionOrProperty>): FunctionOrProperty? {
@@ -57,6 +57,9 @@ class FunctionOrPropertyBaseCommonizer(
commonizedType = returnType, commonizedType = returnType,
) )
val annotations = AnnotationsCommonizer.commonize(values.map { it.annotations }).orEmpty()
.plus(listOfNotNull(unsafeNumberAnnotation))
return FunctionOrProperty( return FunctionOrProperty(
name = values.first().name, name = values.first().name,
kind = values.singleDistinctValueOrNull { it.kind } ?: return null, kind = values.singleDistinctValueOrNull { it.kind } ?: return null,
@@ -65,7 +68,7 @@ class FunctionOrPropertyBaseCommonizer(
extensionReceiver = (extensionReceiverCommonizer(values.map { it.extensionReceiver }) ?: return null).receiver, extensionReceiver = (extensionReceiverCommonizer(values.map { it.extensionReceiver }) ?: return null).receiver,
returnType = returnTypeCommonizer(values) ?: return null, returnType = returnTypeCommonizer(values) ?: return null,
typeParameters = TypeParameterListCommonizer(typeCommonizer).commonize(values.map { it.typeParameters }) ?: return null, typeParameters = TypeParameterListCommonizer(typeCommonizer).commonize(values.map { it.typeParameters }) ?: return null,
additionalAnnotations = listOfNotNull(unsafeNumberAnnotation) annotations = annotations
) )
} }
} }
@@ -16,9 +16,7 @@ class FunctionCommonizer(
val functionOrProperty = functionOrPropertyBaseCommonizer(values) ?: return null val functionOrProperty = functionOrPropertyBaseCommonizer(values) ?: return null
val valueParametersResult = CallableValueParametersCommonizer(typeCommonizer).commonize(values) ?: return null val valueParametersResult = CallableValueParametersCommonizer(typeCommonizer).commonize(values) ?: return null
return CirFunction( return CirFunction(
annotations = AnnotationsCommonizer.commonize(values.map { it.annotations.toList() }) annotations = functionOrProperty.annotations,
?.plus(functionOrProperty.additionalAnnotations)
?: return null,
name = values.first().name, name = values.first().name,
typeParameters = functionOrProperty.typeParameters, typeParameters = functionOrProperty.typeParameters,
visibility = functionOrProperty.visibility, visibility = functionOrProperty.visibility,
@@ -29,7 +29,7 @@ class PropertyCommonizer(
val constCompileTimeInitializer = (constCommonizationState as? ConstSameValue)?.compileTimeInitializer val constCompileTimeInitializer = (constCommonizationState as? ConstSameValue)?.compileTimeInitializer
return CirProperty( return CirProperty(
annotations = functionOrPropertyBase.additionalAnnotations, annotations = functionOrPropertyBase.annotations,
name = functionOrPropertyBase.name, name = functionOrPropertyBase.name,
typeParameters = functionOrPropertyBase.typeParameters, typeParameters = functionOrPropertyBase.typeParameters,
visibility = functionOrPropertyBase.visibility, visibility = functionOrPropertyBase.visibility,
@@ -242,4 +242,22 @@ class HierarchicalPropertyCommonizationTest : AbstractInlineSourcesCommonization
""".trimIndent() """.trimIndent()
) )
} }
fun `test property with annotations`() {
val result = commonize {
outputTarget("(a, b)")
registerDependency("a", "b", "(a, b)") {
source("""
annotation class A1
annotation class A2
annotation class A3
""".trimIndent())
}
simpleSingleSourceTarget("a", """@A1 @A2 @A3 val x: Int = 42""")
simpleSingleSourceTarget("b", """@A1 @A2 val x: Int = 42""")
}
result.assertCommonized("(a, b)", """@A1 @A2 expect val x: Int""")
}
} }