Search annotations in IR, than, if not found check its descriptor.

Could help with external stubs.
This commit is contained in:
Alexander Gorshenev
2019-01-24 20:57:13 +03:00
committed by alexander-gorshenev
parent 5fa37aa800
commit df2711168d
2 changed files with 13 additions and 5 deletions
@@ -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<String>(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) }
@@ -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<IrCall>.findAnnotation(fqName: FqName): IrCall? = this.firstOrNull {
it.annotationClass.fqNameSafe == fqName
}
fun <T> List<IrCall>.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<T>
val actual = annotation.getValueArgument(index) as? IrConst<String>
return actual?.value
}
}