Remove KotlinSirOrigin hierarchy in favour of direct AA usage #KT-65335 Fixed

Merge-request: KT-MR-14229
Merged-by: Artem Olkov <artem.olkov@jetbrains.com>
This commit is contained in:
Artem Olkov
2024-02-09 18:34:52 +00:00
committed by Space Team
parent c2e0c8b2e9
commit b6c805bbb6
94 changed files with 548 additions and 1912 deletions
@@ -7,7 +7,7 @@ package org.jetbrains.kotlin.sir.bridge
import org.jetbrains.kotlin.sir.SirCallable
import org.jetbrains.kotlin.sir.SirFunctionBody
import org.jetbrains.kotlin.sir.SirNativeCallable
import org.jetbrains.kotlin.sir.bridge.impl.*
import org.jetbrains.kotlin.sir.bridge.impl.BridgeGeneratorImpl
import org.jetbrains.kotlin.sir.bridge.impl.CBridgePrinter
import org.jetbrains.kotlin.sir.bridge.impl.KotlinBridgePrinter
@@ -23,7 +23,7 @@ import org.jetbrains.kotlin.sir.util.returnType
* @param bridgeName C name of the bridge
*/
public class BridgeRequest(
public val callable: SirNativeCallable,
public val callable: SirCallable,
public val bridgeName: String,
public val fqName: List<String>,
)
@@ -35,7 +35,7 @@ public class BridgeRequest(
* @return the generated SirFunctionBody object representing the body of the function
*/
public fun createFunctionBodyFromRequest(request: BridgeRequest): SirFunctionBody {
val callee = request.bridgeName
val callee = request.cDeclarationName()
val calleeArguments = request.callable.allParameters.map { it.name }
val callSite = "$callee(${calleeArguments.joinToString(separator = ", ")})"
val callStatement = if (request.callable.returnType.isVoid) callSite else "return $callSite"
@@ -16,10 +16,10 @@ private const val stdintHeader = "stdint.h"
internal class BridgeGeneratorImpl : BridgeGenerator {
override fun generate(request: BridgeRequest): FunctionBridge {
val (kotlinReturnType, cReturnType) = bridgeType(request.callable.returnType)
val (kotlinReturnType, _) = bridgeType(request.callable.returnType)
val parameterBridges = request.callable.allParameters.mapIndexed { index, value -> bridgeParameter(value, index) }
val cDeclaration = createCDeclaration(request.bridgeName, cReturnType, parameterBridges.map { it.c })
val cDeclaration = request.createCDeclaration()
val kotlinBridge = createKotlinBridge(request.bridgeName, request.fqName, kotlinReturnType, parameterBridges.map { it.kotlin })
return FunctionBridge(
KotlinFunctionBridge(kotlinBridge, listOf(exportAnnotationFqName)),
@@ -28,6 +28,17 @@ internal class BridgeGeneratorImpl : BridgeGenerator {
}
}
// TODO: we need to mangle C name in more elegant way. KT-64970
// problems with this approach are:
// 1. there can be limit for declaration names in Clang compiler
// 1. this name will be UGLY in the debug session
internal fun BridgeRequest.cDeclarationName(): String {
val nameSuffixForOverloadSimulation = cParameters().joinToString(separator = "_", transform = { it.type.repr })
val suffixString = if (cParameters().isNotEmpty()) "__TypesOfArguments__${nameSuffixForOverloadSimulation}__" else ""
val result = "${bridgeName}${suffixString}"
return result
}
private fun createKotlinBridge(
bridgeName: String,
functionFqName: List<String>,
@@ -61,12 +72,15 @@ private fun createKotlinDeclarationSignature(bridgeName: String, returnType: Kot
}): ${returnType.repr}"
}
private fun createCDeclaration(bridgeName: String, returnType: CType, parameters: List<CBridgeParameter>): List<String> {
val cParameters = parameters.joinToString(separator = ", ", transform = { "${it.type.repr} ${it.name}" })
val declaration = "${returnType.repr} $bridgeName($cParameters);"
private fun BridgeRequest.createCDeclaration(): List<String> {
val cParameters = cParameters().joinToString(separator = ", ", transform = { "${it.type.repr} ${it.name}" })
val declaration = "${bridgeType(callable.returnType).second.repr} ${cDeclarationName()}($cParameters);"
return listOf(declaration)
}
private fun BridgeRequest.cParameters() = callable.allParameters
.mapIndexed { index, value -> bridgeParameter(value, index) }
.map { it.c }
private fun bridgeType(type: SirType): Pair<KotlinType, CType> {
require(type is SirNominalType)
@@ -76,16 +90,19 @@ private fun bridgeType(type: SirType): Pair<KotlinType, CType> {
SirSwiftModule.bool -> (KotlinType.Boolean to CType.Bool)
SirSwiftModule.int8 -> (KotlinType.Byte to CType.Int8)
SirSwiftModule.int16 -> (KotlinType.Short to CType.Int16)
SirSwiftModule.int32 -> (KotlinType.Int to CType.Int32)
SirSwiftModule.int64 -> (KotlinType.Long to CType.Int64)
SirSwiftModule.int16 -> (KotlinType.Short to CType.Int16)
SirSwiftModule.uint8 -> (KotlinType.UByte to CType.UInt8)
SirSwiftModule.uint16 -> (KotlinType.UShort to CType.UInt16)
SirSwiftModule.uint32 -> (KotlinType.UInt to CType.UInt32)
SirSwiftModule.uint64 -> (KotlinType.ULong to CType.UInt64)
SirSwiftModule.uint16 -> (KotlinType.UShort to CType.UInt16)
else -> error("Unsupported type: ${type.type}")
SirSwiftModule.double -> (KotlinType.Double to CType.Double)
SirSwiftModule.float -> (KotlinType.Float to CType.Float)
else -> error("Unsupported type: ${type.type.name}")
}
}
@@ -129,6 +146,9 @@ public enum class CType(public val repr: String) {
UInt16("uint16_t"),
UInt32("uint32_t"),
UInt64("uint64_t"),
Float("float"),
Double("double"),
}
internal data class KotlinBridgeParameter(
@@ -150,5 +170,8 @@ internal enum class KotlinType(val repr: String) {
UShort("UShort"),
UInt("UInt"),
ULong("ULong"),
Float("Float"),
Double("Double"),
}
@@ -1,6 +1,5 @@
#include <stdint.h>
int32_t a_b_bar_bridge(int32_t param1, int64_t param2);
_Bool a_b_foo_bridge(int32_t param1, int64_t param2);
int32_t a_b_bar_bridge__TypesOfArguments__int32_t_int64_t__(int32_t param1, int64_t param2);
_Bool a_b_foo_bridge__TypesOfArguments__int32_t_int64_t__(int32_t param1, int64_t param2);
@@ -1,7 +1,7 @@
#include <stdint.h>
int32_t a(int8_t p0, int16_t p1, int32_t p2, int64_t p3);
int32_t a__TypesOfArguments__int8_t_int16_t_int32_t_int64_t__(int8_t p0, int16_t p1, int32_t p2, int64_t p3);
uint32_t b(uint8_t p0, uint16_t p1, uint32_t p2, uint64_t p3);
uint32_t b__TypesOfArguments__uint8_t_uint16_t_uint32_t_uint64_t__(uint8_t p0, uint16_t p1, uint32_t p2, uint64_t p3);
_Bool c(_Bool p0);
_Bool c__TypesOfArguments___Bool__(_Bool p0);
@@ -2,4 +2,4 @@
_Bool getter_bridge();
void setter_bridge(_Bool newValue);
void setter_bridge__TypesOfArguments___Bool__(_Bool newValue);
@@ -1,3 +1,3 @@
#include <stdint.h>
int32_t a_b_foo_bridge(int32_t param1, int64_t param2);
int32_t a_b_foo_bridge__TypesOfArguments__int32_t_int64_t__(int32_t param1, int64_t param2);