And more.

This commit is contained in:
Alexander Gorshenev
2019-01-25 02:02:50 +03:00
committed by alexander-gorshenev
parent 1544c1da2f
commit 478d32ef06
5 changed files with 28 additions and 27 deletions
@@ -60,7 +60,8 @@ fun ClassDescriptor.isExternalObjCClass(): Boolean = this.isObjCClass() &&
}
fun IrClass.isExternalObjCClass(): Boolean = this.isObjCClass() &&
(this as IrDeclaration).parentDeclarationsWithSelf.filterIsInstance<IrClass>().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<kotlin.Boolean>
return actual.value
}
}
error("Could not find 'designated' argument")
}
fun IrConstructor.objCConstructorIsDesignated(): Boolean =
this.getAnnotationArgumentValue<Boolean>(objCConstructorFqName, "designated")
?: error("Could not find 'designated' argument")
@Deprecated("Use IR version rather than descriptor version")
fun ConstructorDescriptor.objCConstructorIsDesignated(): Boolean {
@@ -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<String>(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) }
@@ -180,6 +180,14 @@ fun AnnotationDescriptor.getStringValueOrNull(name: String): String? {
return constantValue?.value as String?
}
fun <T> 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<FqName> {
@@ -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<IrCall>.findAnnotation(fqName: FqName): IrCall? = this.firstOrNull {
it.annotationClass.fqNameSafe == fqName
}
fun IrDeclaration.getAnnotationArgumentStringValue(fqName: FqName, argumentName: String): String? {
fun <T> 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<T>(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<String>
val actual = annotation.getValueArgument(index) as? IrConst<T>
return actual?.value
}
}
@@ -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))