From df2711168d012708d91f88a945c42c09dc8ff48e Mon Sep 17 00:00:00 2001 From: Alexander Gorshenev Date: Thu, 24 Jan 2019 20:57:13 +0300 Subject: [PATCH] Search annotations in IR, than, if not found check its descriptor. Could help with external stubs. --- .../kotlin/backend/konan/cgen/CBridgeGen.kt | 4 ++-- .../backend/konan/irasdescriptors/NewIrUtils.kt | 14 +++++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/cgen/CBridgeGen.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/cgen/CBridgeGen.kt index a8ca6242cf3..edf8f97475b 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/cgen/CBridgeGen.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/cgen/CBridgeGen.kt @@ -161,7 +161,7 @@ internal fun KotlinStubs.generateCCall(expression: IrCall, builder: IrBuilderWit if (isInvoke) { callBuilder.cBridgeBodyLines.add(0, "$targetFunctionVariable = ${targetPtrParameter!!};") } else { - val cCallSymbolName = callee.annotations.getAnnotationArgumentValue(cCall, "id")!! + val cCallSymbolName = callee.getAnnotationArgumentStringValue(cCall, "id")!! cLines += "extern const $targetFunctionVariable __asm(\"$cCallSymbolName\");" // Exported from cinterop stubs. } @@ -350,7 +350,7 @@ private fun IrValueParameter.isCStringParameter() = this.annotations.hasAnnotation(cCall.child(Name.identifier("CString"))) private fun getStructSpelling(kotlinClass: IrClass): String? = - kotlinClass.annotations.getAnnotationArgumentValue(FqName("kotlinx.cinterop.internal.CStruct"), "spelling") + kotlinClass.getAnnotationArgumentStringValue(FqName("kotlinx.cinterop.internal.CStruct"), "spelling") private fun getCStructType(kotlinClass: IrClass): CType? = getStructSpelling(kotlinClass)?.let { CTypes.simple(it) } diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/irasdescriptors/NewIrUtils.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/irasdescriptors/NewIrUtils.kt index 5bdefec8cf6..3f230f2b2ed 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/irasdescriptors/NewIrUtils.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/irasdescriptors/NewIrUtils.kt @@ -6,6 +6,7 @@ package org.jetbrains.kotlin.backend.konan.irasdescriptors import org.jetbrains.kotlin.backend.common.atMostOne +import org.jetbrains.kotlin.backend.konan.descriptors.getStringValue import org.jetbrains.kotlin.backend.konan.descriptors.konanBackingField import org.jetbrains.kotlin.backend.konan.descriptors.isInterface import org.jetbrains.kotlin.builtins.KotlinBuiltIns @@ -166,12 +167,19 @@ fun List.findAnnotation(fqName: FqName): IrCall? = this.firstOrNull { it.annotationClass.fqNameSafe == fqName } -fun List.getAnnotationArgumentValue(fqName: FqName, argumentName: String): T? { - val annotation = this.findAnnotation(fqName) ?: return null +fun IrDeclaration.getAnnotationArgumentStringValue(fqName: FqName, argumentName: String): String? { + val annotation = this.annotations.findAnnotation(fqName) + if (annotation == null) { + // As a last resort try searching the descriptor. + // This is needed for a period while we don't have IR for platform libraries. + return this.descriptor.annotations + .findAnnotation(fqName) + ?.getStringValue(argumentName) + } for (index in 0 until annotation.valueArgumentsCount) { val parameter = annotation.symbol.owner.valueParameters[index] if (parameter.name == Name.identifier(argumentName)) { - val actual = annotation.getValueArgument(index) as? IrConst + val actual = annotation.getValueArgument(index) as? IrConst return actual?.value } }