KT-63264: Introduce a new ExportedBridge annotation
Swift Export Frontend generates a Kotlin file/library that contains a set of simple bridging functions that connect Swift wrappers to their Kotlin counterparts. There are certain requirements for these wrappers: 1. They should not be DCEd when compiling a binary library. In other words, these functions are roots. 2. They should provide a stable simple binary name. 3. Their signatures should be much simpler and restricted comparing to other Kotlin functions. Altogether, these requirements should be covered by introducing the new ExportedBridge annotation. Note: Frontend checks of ExportedBridge functions are not implemented yet.
This commit is contained in:
committed by
Space Team
parent
8be547b834
commit
ce4eecebf4
+1
@@ -8,6 +8,7 @@ object RuntimeNames {
|
||||
val cnameAnnotation = NativeRuntimeNames.Annotations.cNameClassId.asSingleFqName()
|
||||
val frozenAnnotation = FqName("kotlin.native.internal.Frozen")
|
||||
val exportForCppRuntime = NativeRuntimeNames.Annotations.exportForCppRuntimeClassId.asSingleFqName()
|
||||
val exportedBridge = NativeRuntimeNames.Annotations.exportedBridgeClassId.asSingleFqName()
|
||||
val exportForCompilerAnnotation = NativeRuntimeNames.Annotations.exportForCompilerClassId.asSingleFqName()
|
||||
val exportTypeInfoAnnotation = FqName("kotlin.native.internal.ExportTypeInfo")
|
||||
val cCall = FqName("kotlinx.cinterop.internal.CCall")
|
||||
|
||||
+11
-5
@@ -6,19 +6,21 @@
|
||||
package org.jetbrains.kotlin.backend.konan.llvm
|
||||
|
||||
import llvm.LLVMTypeRef
|
||||
import org.jetbrains.kotlin.backend.common.serialization.mangle.MangleConstant
|
||||
import org.jetbrains.kotlin.backend.common.serialization.mangle.SpecialDeclarationType
|
||||
import org.jetbrains.kotlin.backend.konan.RuntimeNames
|
||||
import org.jetbrains.kotlin.backend.konan.descriptors.externalSymbolOrThrow
|
||||
import org.jetbrains.kotlin.ir.util.getAnnotationStringValue
|
||||
import org.jetbrains.kotlin.backend.konan.descriptors.isAbstract
|
||||
import org.jetbrains.kotlin.backend.konan.ir.isUnit
|
||||
import org.jetbrains.kotlin.backend.konan.serialization.AbstractKonanIrMangler
|
||||
import org.jetbrains.kotlin.ir.declarations.IrClass
|
||||
import org.jetbrains.kotlin.ir.declarations.IrDeclaration
|
||||
import org.jetbrains.kotlin.ir.declarations.IrField
|
||||
import org.jetbrains.kotlin.ir.declarations.IrFunction
|
||||
import org.jetbrains.kotlin.ir.objcinterop.isExternalObjCClass
|
||||
import org.jetbrains.kotlin.ir.objcinterop.isKotlinObjCClass
|
||||
import org.jetbrains.kotlin.backend.konan.serialization.AbstractKonanIrMangler
|
||||
import org.jetbrains.kotlin.ir.declarations.*
|
||||
import org.jetbrains.kotlin.ir.util.findAnnotation
|
||||
import org.jetbrains.kotlin.ir.util.fqNameForIrSerialization
|
||||
import org.jetbrains.kotlin.ir.util.getAnnotationStringValue
|
||||
import org.jetbrains.kotlin.ir.util.render
|
||||
import org.jetbrains.kotlin.konan.library.KonanLibrary
|
||||
import org.jetbrains.kotlin.library.uniqueName
|
||||
@@ -62,6 +64,10 @@ object KonanBinaryInterface {
|
||||
|
||||
private fun withPrefix(prefix: String, mangle: String) = "$prefix:$mangle"
|
||||
|
||||
private fun IrFunction.findManglingAnnotation() =
|
||||
this.annotations.findAnnotation(RuntimeNames.exportForCppRuntime)
|
||||
?: this.annotations.findAnnotation(RuntimeNames.exportedBridge)
|
||||
|
||||
private fun IrFunction.funSymbolNameImpl(containerName: String?): String {
|
||||
if (isExternal) {
|
||||
this.externalSymbolOrThrow()?.let {
|
||||
@@ -69,7 +75,7 @@ object KonanBinaryInterface {
|
||||
}
|
||||
}
|
||||
|
||||
this.annotations.findAnnotation(RuntimeNames.exportForCppRuntime)?.let {
|
||||
this.findManglingAnnotation()?.let {
|
||||
val name = it.getAnnotationStringValue() ?: this.name.asString()
|
||||
return name // no wrapping currently required
|
||||
}
|
||||
|
||||
+2
@@ -114,6 +114,8 @@ internal inline fun generateFunction(
|
||||
val llvmFunction = codegen.llvmFunction(function)
|
||||
|
||||
val isCToKotlinBridge = function.origin == CBridgeOrigin.C_TO_KOTLIN_BRIDGE
|
||||
// TODO: Alternative approach: lowering that changes origin of such functions to C_TO_KOTLIN_BRIDGE?
|
||||
|| function.hasAnnotation(RuntimeNames.exportedBridge)
|
||||
|
||||
val functionGenerationContext = DefaultFunctionGenerationContext(
|
||||
llvmFunction,
|
||||
|
||||
+2
@@ -5,6 +5,7 @@
|
||||
|
||||
package org.jetbrains.kotlin.backend.konan.llvm
|
||||
|
||||
import org.jetbrains.kotlin.backend.konan.RuntimeNames
|
||||
import org.jetbrains.kotlin.ir.util.getAnnotationStringValue
|
||||
import org.jetbrains.kotlin.ir.declarations.IrFunction
|
||||
import org.jetbrains.kotlin.ir.util.findAnnotation
|
||||
@@ -18,5 +19,6 @@ internal fun IrFunction.retainAnnotation(target: KonanTarget): Boolean {
|
||||
if (this.annotations.findAnnotation(retainAnnotationName) != null) return true
|
||||
val forTarget = this.annotations.findAnnotation(retainForTargetAnnotationName)
|
||||
if (forTarget != null && forTarget.getAnnotationStringValue() == target.name) return true
|
||||
if (this.annotations.findAnnotation(RuntimeNames.exportedBridge) != null) return true
|
||||
return false
|
||||
}
|
||||
|
||||
+1
@@ -603,6 +603,7 @@ internal object DataFlowIR {
|
||||
if (returnsNothing)
|
||||
attributes = attributes or FunctionAttributes.RETURNS_NOTHING
|
||||
if (it.hasAnnotation(RuntimeNames.exportForCppRuntime)
|
||||
|| it.hasAnnotation(RuntimeNames.exportedBridge)
|
||||
|| it.getExternalObjCMethodInfo() != null // TODO-DCE-OBJC-INIT
|
||||
|| it.hasAnnotation(RuntimeNames.objCMethodImp)) {
|
||||
attributes = attributes or FunctionAttributes.EXPLICITLY_EXPORTED
|
||||
|
||||
Reference in New Issue
Block a user