Add specific ObjCMethodSpec kind for object instance getters

This helps to drop usages of obsolete "IrDeclaration.descriptor"
from ObjCExportCodeGenerator
This commit is contained in:
Svyatoslav Scherbina
2021-01-28 15:26:25 +03:00
committed by Vasily Levchenko
parent e305710d91
commit ca2df3cead
2 changed files with 17 additions and 20 deletions
@@ -1257,6 +1257,13 @@ private fun ObjCExportCodeGenerator.createTypeAdapter(
is ObjCClassMethodForKotlinEnumValues -> { is ObjCClassMethodForKotlinEnumValues -> {
classAdapters += createEnumValuesAdapter(it.valuesFunctionSymbol.owner, it.selector) classAdapters += createEnumValuesAdapter(it.valuesFunctionSymbol.owner, it.selector)
} }
is ObjCGetterForObjectInstance -> {
classAdapters += if (irClass.isUnit()) {
createUnitInstanceAdapter(it.selector)
} else {
createObjectInstanceAdapter(irClass, it.selector)
}
}
is ObjCMethodForKotlinMethod -> {} // Handled below. is ObjCMethodForKotlinMethod -> {} // Handled below.
}.let {} // Force exhaustive. }.let {} // Force exhaustive.
} }
@@ -1309,19 +1316,6 @@ private fun ObjCExportCodeGenerator.createTypeAdapter(
else -> Pair(emptyList(), -1) else -> Pair(emptyList(), -1)
} }
when (irClass.kind) {
ClassKind.OBJECT -> {
classAdapters += if (irClass.isUnit()) {
createUnitInstanceAdapter()
} else {
createObjectInstanceAdapter(irClass)
}
}
else -> {
// Nothing special.
}
}
return ObjCTypeAdapter( return ObjCTypeAdapter(
irClass, irClass,
typeInfo, typeInfo,
@@ -1464,23 +1458,20 @@ private fun ObjCExportCodeGenerator.objCToKotlinMethodAdapter(
return ObjCToKotlinMethodAdapter(selector, getEncoding(methodBridge), constPointer(imp)) return ObjCToKotlinMethodAdapter(selector, getEncoding(methodBridge), constPointer(imp))
} }
private fun ObjCExportCodeGenerator.createUnitInstanceAdapter() = private fun ObjCExportCodeGenerator.createUnitInstanceAdapter(selector: String) =
generateObjCToKotlinSyntheticGetter( generateObjCToKotlinSyntheticGetter(selector) {
namer.getObjectInstanceSelector(context.builtIns.unit)
) {
initRuntimeIfNeeded() // For instance methods it gets called when allocating. initRuntimeIfNeeded() // For instance methods it gets called when allocating.
ret(callFromBridge(context.llvm.Kotlin_ObjCExport_convertUnit, listOf(codegen.theUnitInstanceRef.llvm))) ret(callFromBridge(context.llvm.Kotlin_ObjCExport_convertUnit, listOf(codegen.theUnitInstanceRef.llvm)))
} }
private fun ObjCExportCodeGenerator.createObjectInstanceAdapter( private fun ObjCExportCodeGenerator.createObjectInstanceAdapter(
irClass: IrClass irClass: IrClass,
selector: String
): ObjCExportCodeGenerator.ObjCToKotlinMethodAdapter { ): ObjCExportCodeGenerator.ObjCToKotlinMethodAdapter {
assert(irClass.kind == ClassKind.OBJECT) assert(irClass.kind == ClassKind.OBJECT)
assert(!irClass.isUnit()) assert(!irClass.isUnit())
val selector = namer.getObjectInstanceSelector(irClass.descriptor)
return generateObjCToKotlinSyntheticGetter(selector) { return generateObjCToKotlinSyntheticGetter(selector) {
initRuntimeIfNeeded() // For instance methods it gets called when allocating. initRuntimeIfNeeded() // For instance methods it gets called when allocating.
val value = getObjectValue(irClass, startLocationInfo = null, exceptionHandler = ExceptionHandler.Caller) val value = getObjectValue(irClass, startLocationInfo = null, exceptionHandler = ExceptionHandler.Caller)
@@ -63,6 +63,10 @@ internal fun ObjCExportedInterface.createCodeSpec(symbolTable: SymbolTable): Obj
} }
} }
if (descriptor.kind == ClassKind.OBJECT) {
methods += ObjCGetterForObjectInstance(namer.getObjectInstanceSelector(descriptor))
}
if (descriptor.kind == ClassKind.ENUM_CLASS) { if (descriptor.kind == ClassKind.ENUM_CLASS) {
descriptor.enumEntries.mapTo(methods) { descriptor.enumEntries.mapTo(methods) {
ObjCGetterForKotlinEnumEntry(symbolTable.referenceEnumEntry(it)) ObjCGetterForKotlinEnumEntry(symbolTable.referenceEnumEntry(it))
@@ -112,6 +116,8 @@ internal class ObjCClassMethodForKotlinEnumValues(
val selector: String val selector: String
) : ObjCMethodSpec() ) : ObjCMethodSpec()
internal class ObjCGetterForObjectInstance(val selector: String) : ObjCMethodSpec()
internal sealed class ObjCTypeSpec(val binaryName: String) internal sealed class ObjCTypeSpec(val binaryName: String)
internal sealed class ObjCTypeForKotlinType( internal sealed class ObjCTypeForKotlinType(