From 478d32ef06f78884cfee7766b495715488caac61 Mon Sep 17 00:00:00 2001 From: Alexander Gorshenev Date: Fri, 25 Jan 2019 02:02:50 +0300 Subject: [PATCH] And more. --- .../kotlin/backend/konan/ObjCInterop.kt | 17 +++++------------ .../kotlin/backend/konan/cgen/CBridgeGen.kt | 4 ++-- .../descriptors/LegacyDescriptorUtils.kt | 8 ++++++++ .../konan/irasdescriptors/NewIrUtils.kt | 7 ++++--- .../llvm/KotlinObjCClassInfoGenerator.kt | 19 +++++++++---------- 5 files changed, 28 insertions(+), 27 deletions(-) diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ObjCInterop.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ObjCInterop.kt index 1ae31e0fc0e..9f59154b817 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ObjCInterop.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/ObjCInterop.kt @@ -60,7 +60,8 @@ fun ClassDescriptor.isExternalObjCClass(): Boolean = this.isObjCClass() && } fun IrClass.isExternalObjCClass(): Boolean = this.isObjCClass() && (this as IrDeclaration).parentDeclarationsWithSelf.filterIsInstance().any { - it.annotations.findAnnotation(externalObjCClassFqName) != null + it.annotations.hasAnnotation(externalObjCClassFqName) || + it.descriptor.annotations.hasAnnotation(externalObjCClassFqName) } fun ClassDescriptor.isObjCMetaClass(): Boolean = this.getAllSuperClassifiers().any { @@ -224,17 +225,9 @@ class ObjCOverridabilityCondition : ExternalOverridabilityCondition { } -fun IrConstructor.objCConstructorIsDesignated(): Boolean { - val annotation = this.annotations.findAnnotation(objCConstructorFqName)!! - for (index in 0 until annotation.valueArgumentsCount) { - val parameter = annotation.symbol.owner.valueParameters[index] - if (parameter.name == Name.identifier("designated")) { - val actual = annotation.getValueArgument(index) as IrConst - return actual.value - } - } - error("Could not find 'designated' argument") -} +fun IrConstructor.objCConstructorIsDesignated(): Boolean = + this.getAnnotationArgumentValue(objCConstructorFqName, "designated") + ?: error("Could not find 'designated' argument") @Deprecated("Use IR version rather than descriptor version") fun ConstructorDescriptor.objCConstructorIsDesignated(): Boolean { 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 dffb425fef4..e7c2423d8b1 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.getAnnotationArgumentStringValue(cCall, "id")!! + val cCallSymbolName = callee.getAnnotationArgumentValue(cCall, "id")!! cLines += "extern const $targetFunctionVariable __asm(\"$cCallSymbolName\");" // Exported from cinterop stubs. } @@ -354,7 +354,7 @@ private fun IrValueParameter.isCStringParameter() = this.descriptor.annotations.hasAnnotation(cCall.child(Name.identifier("CString"))) private fun getStructSpelling(kotlinClass: IrClass): String? = - kotlinClass.getAnnotationArgumentStringValue(FqName("kotlinx.cinterop.internal.CStruct"), "spelling") + kotlinClass.getAnnotationArgumentValue(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/descriptors/LegacyDescriptorUtils.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/descriptors/LegacyDescriptorUtils.kt index 8e3f81c2383..3328b1ba46b 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/descriptors/LegacyDescriptorUtils.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/descriptors/LegacyDescriptorUtils.kt @@ -180,6 +180,14 @@ fun AnnotationDescriptor.getStringValueOrNull(name: String): String? { return constantValue?.value as String? } +fun AnnotationDescriptor.getArgumentValueOrNull(name: String): T? { + val constantValue = this.allValueArguments.entries.atMostOne { + it.key.asString() == name + }?.value + return constantValue?.value as T? +} + + fun AnnotationDescriptor.getStringValue(name: String): String = this.getStringValueOrNull(name)!! private fun getPackagesFqNames(module: ModuleDescriptor): Set { 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 3f230f2b2ed..37dc4a15341 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.getArgumentValueOrNull import org.jetbrains.kotlin.backend.konan.descriptors.getStringValue import org.jetbrains.kotlin.backend.konan.descriptors.konanBackingField import org.jetbrains.kotlin.backend.konan.descriptors.isInterface @@ -167,19 +168,19 @@ fun List.findAnnotation(fqName: FqName): IrCall? = this.firstOrNull { it.annotationClass.fqNameSafe == fqName } -fun IrDeclaration.getAnnotationArgumentStringValue(fqName: FqName, argumentName: String): String? { +fun IrDeclaration.getAnnotationArgumentValue(fqName: FqName, argumentName: String): T? { 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) + ?.getArgumentValueOrNull(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 } } diff --git a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/KotlinObjCClassInfoGenerator.kt b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/KotlinObjCClassInfoGenerator.kt index e56a2274b7d..6e8c40b7215 100644 --- a/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/KotlinObjCClassInfoGenerator.kt +++ b/backend.native/compiler/ir/backend.native/src/org/jetbrains/kotlin/backend/konan/llvm/KotlinObjCClassInfoGenerator.kt @@ -90,16 +90,15 @@ internal class KotlinObjCClassInfoGenerator(override val context: Context) : Con } private fun selectClassName(irClass: IrClass): String? { - val exportObjCClassAnnotation = - irClass.descriptor.annotations.findAnnotation(context.interopBuiltIns.exportObjCClass.fqNameSafe) - - return if (exportObjCClassAnnotation != null) { - exportObjCClassAnnotation.getStringValueOrNull("name") ?: irClass.name.asString() - } else if (irClass.isExported()) { - irClass.fqNameSafe.asString() - } else { - null // Generate as anonymous. - } + val exportObjCClassAnnotation = context.interopBuiltIns.exportObjCClass.fqNameSafe + return irClass.getAnnotationArgumentValue(exportObjCClassAnnotation, "name") + ?: if (irClass.annotations.hasAnnotation(exportObjCClassAnnotation)) + irClass.name.asString() + else if (irClass.isExported()) { + irClass.fqNameSafe.asString() + } else { + null // Generate as anonymous. + } } private val impType = pointerType(functionType(int8TypePtr, true, int8TypePtr, int8TypePtr))