[WASM] Implementation of Class references
This commit is contained in:
committed by
TeamCityServer
parent
d9f2bd7963
commit
ce360bb10b
+11
-4
@@ -8,7 +8,6 @@ package org.jetbrains.kotlin.ir.backend.js
|
||||
import org.jetbrains.kotlin.backend.common.BackendContext
|
||||
import org.jetbrains.kotlin.backend.common.CommonBackendContext
|
||||
import org.jetbrains.kotlin.backend.common.atMostOne
|
||||
import org.jetbrains.kotlin.backend.common.ir.Symbols
|
||||
import org.jetbrains.kotlin.backend.common.ir.isOverridableOrOverrides
|
||||
import org.jetbrains.kotlin.descriptors.ClassDescriptor
|
||||
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
|
||||
@@ -17,7 +16,9 @@ import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor
|
||||
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
|
||||
import org.jetbrains.kotlin.ir.backend.js.utils.isDispatchReceiver
|
||||
import org.jetbrains.kotlin.ir.declarations.*
|
||||
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
|
||||
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
|
||||
import org.jetbrains.kotlin.ir.types.IrDynamicType
|
||||
import org.jetbrains.kotlin.ir.types.IrType
|
||||
import org.jetbrains.kotlin.ir.util.SymbolTable
|
||||
import org.jetbrains.kotlin.ir.util.getPropertyGetter
|
||||
@@ -29,10 +30,16 @@ import org.jetbrains.kotlin.resolve.scopes.MemberScope
|
||||
interface JsCommonBackendContext : CommonBackendContext {
|
||||
override val mapping: JsMapping
|
||||
|
||||
val intrinsics: Intrinsics
|
||||
|
||||
val dynamicType: IrDynamicType
|
||||
|
||||
val inlineClassesUtils: InlineClassesUtils
|
||||
|
||||
val coroutineSymbols: JsCommonCoroutineSymbols
|
||||
|
||||
val primitiveClassesObject: IrClassSymbol
|
||||
|
||||
val catchAllThrowableType: IrType
|
||||
get() = irBuiltIns.throwableType
|
||||
|
||||
@@ -50,7 +57,7 @@ internal fun <T> BackendContext.lazy2(fn: () -> T) = lazy { irFactory.stageContr
|
||||
|
||||
class JsCommonCoroutineSymbols(
|
||||
symbolTable: SymbolTable,
|
||||
module: ModuleDescriptor,
|
||||
val module: ModuleDescriptor,
|
||||
val context: JsCommonBackendContext
|
||||
) {
|
||||
val coroutinePackage = module.getPackage(COROUTINE_PACKAGE_FQNAME)
|
||||
@@ -114,10 +121,10 @@ class JsCommonCoroutineSymbols(
|
||||
}
|
||||
}
|
||||
|
||||
internal fun findClass(memberScope: MemberScope, name: Name): ClassDescriptor =
|
||||
fun findClass(memberScope: MemberScope, name: Name): ClassDescriptor =
|
||||
memberScope.getContributedClassifier(name, NoLookupLocation.FROM_BACKEND) as ClassDescriptor
|
||||
|
||||
internal fun findFunctions(memberScope: MemberScope, name: Name): List<SimpleFunctionDescriptor> =
|
||||
fun findFunctions(memberScope: MemberScope, name: Name): List<SimpleFunctionDescriptor> =
|
||||
memberScope.getContributedFunctions(name, NoLookupLocation.FROM_BACKEND).toList()
|
||||
|
||||
interface InlineClassesUtils {
|
||||
|
||||
@@ -21,7 +21,21 @@ import org.jetbrains.kotlin.psi2ir.findSingleFunction
|
||||
import org.jetbrains.kotlin.util.capitalizeDecapitalize.toLowerCaseAsciiOnly
|
||||
import java.util.*
|
||||
|
||||
class JsIntrinsics(private val irBuiltIns: IrBuiltIns, val context: JsIrBackendContext) {
|
||||
interface Intrinsics {
|
||||
val jsGetKClassFromExpression: IrSimpleFunctionSymbol
|
||||
val jsGetKClass: IrSimpleFunctionSymbol
|
||||
val jsClass: IrSimpleFunctionSymbol
|
||||
val createKType: IrSimpleFunctionSymbol?
|
||||
val createDynamicKType: IrSimpleFunctionSymbol?
|
||||
val createKTypeParameter: IrSimpleFunctionSymbol?
|
||||
val getStarKTypeProjection: IrSimpleFunctionSymbol?
|
||||
val createCovariantKTypeProjection: IrSimpleFunctionSymbol?
|
||||
val createInvariantKTypeProjection: IrSimpleFunctionSymbol?
|
||||
val createContravariantKTypeProjection: IrSimpleFunctionSymbol?
|
||||
val arrayLiteral: IrSimpleFunctionSymbol
|
||||
}
|
||||
|
||||
class JsIntrinsics(private val irBuiltIns: IrBuiltIns, val context: JsIrBackendContext) : Intrinsics {
|
||||
|
||||
// TODO: Should we drop operator intrinsics in favor of IrDynamicOperatorExpression?
|
||||
|
||||
@@ -169,10 +183,10 @@ class JsIntrinsics(private val irBuiltIns: IrBuiltIns, val context: JsIrBackendC
|
||||
getInternalWithoutPackage("kotlin.coroutines.intrinsics.invokeSuspendSuperTypeWithReceiver")
|
||||
val jsInvokeSuspendSuperTypeWithReceiverAndParam =
|
||||
getInternalWithoutPackage("kotlin.coroutines.intrinsics.invokeSuspendSuperTypeWithReceiverAndParam")
|
||||
val jsGetKClass = getInternalWithoutPackage("getKClass")
|
||||
val jsGetKClassFromExpression = getInternalWithoutPackage("getKClassFromExpression")
|
||||
|
||||
val jsClass = getInternalFunction("jsClassIntrinsic")
|
||||
override val jsGetKClass = getInternalWithoutPackage("getKClass")
|
||||
override val jsGetKClassFromExpression = getInternalWithoutPackage("getKClassFromExpression")
|
||||
override val jsClass = getInternalFunction("jsClassIntrinsic")
|
||||
|
||||
val jsNumberRangeToNumber = getInternalFunction("numberRangeToNumber")
|
||||
val jsNumberRangeToLong = getInternalFunction("numberRangeToLong")
|
||||
@@ -235,7 +249,7 @@ class JsIntrinsics(private val irBuiltIns: IrBuiltIns, val context: JsIrBackendC
|
||||
val jsPrimitiveArrayIteratorFunctions =
|
||||
PrimitiveType.values().associate { it to getInternalFunction("${it.typeName.asString().toLowerCaseAsciiOnly()}ArrayIterator") }
|
||||
|
||||
val arrayLiteral = getInternalFunction("arrayLiteral")
|
||||
override val arrayLiteral = getInternalFunction("arrayLiteral")
|
||||
|
||||
val primitiveToTypedArrayMap = EnumMap(
|
||||
mapOf(
|
||||
@@ -247,13 +261,13 @@ class JsIntrinsics(private val irBuiltIns: IrBuiltIns, val context: JsIrBackendC
|
||||
)
|
||||
)
|
||||
|
||||
val createKType = getInternalWithoutPackageOrNull("createKType")
|
||||
val createDynamicKType = getInternalWithoutPackageOrNull("createDynamicKType")
|
||||
val createKTypeParameter = getInternalWithoutPackageOrNull("createKTypeParameter")
|
||||
val getStarKTypeProjection = getInternalWithoutPackageOrNull("getStarKTypeProjection")
|
||||
val createCovariantKTypeProjection = getInternalWithoutPackageOrNull("createCovariantKTypeProjection")
|
||||
val createInvariantKTypeProjection = getInternalWithoutPackageOrNull("createInvariantKTypeProjection")
|
||||
val createContravariantKTypeProjection = getInternalWithoutPackageOrNull("createContravariantKTypeProjection")
|
||||
override val createKType = getInternalWithoutPackageOrNull("createKType")
|
||||
override val createDynamicKType = getInternalWithoutPackageOrNull("createDynamicKType")
|
||||
override val createKTypeParameter = getInternalWithoutPackageOrNull("createKTypeParameter")
|
||||
override val getStarKTypeProjection = getInternalWithoutPackageOrNull("getStarKTypeProjection")
|
||||
override val createCovariantKTypeProjection = getInternalWithoutPackageOrNull("createCovariantKTypeProjection")
|
||||
override val createInvariantKTypeProjection = getInternalWithoutPackageOrNull("createInvariantKTypeProjection")
|
||||
override val createContravariantKTypeProjection = getInternalWithoutPackageOrNull("createContravariantKTypeProjection")
|
||||
|
||||
val primitiveToSizeConstructor =
|
||||
PrimitiveType.values().associate { type ->
|
||||
@@ -315,8 +329,6 @@ class JsIntrinsics(private val irBuiltIns: IrBuiltIns, val context: JsIrBackendC
|
||||
|
||||
val jsUndefined = getInternalFunction("jsUndefined")
|
||||
|
||||
// Helpers:
|
||||
|
||||
private fun getInternalFunction(name: String) =
|
||||
context.symbolTable.referenceSimpleFunction(context.getJsInternalFunction(name))
|
||||
|
||||
@@ -333,4 +345,4 @@ class JsIntrinsics(private val irBuiltIns: IrBuiltIns, val context: JsIrBackendC
|
||||
|
||||
private fun getInternalClassWithoutPackage(fqName: String) =
|
||||
context.symbolTable.referenceClass(context.getClass(FqName(fqName)))
|
||||
}
|
||||
}
|
||||
+4
-14
@@ -68,6 +68,7 @@ class JsIrBackendContext(
|
||||
val extractedLocalClasses: MutableSet<IrClass> = hashSetOf()
|
||||
|
||||
override val builtIns = module.builtIns
|
||||
|
||||
override val typeSystem: IrTypeSystemContext = IrTypeSystemContextImpl(irBuiltIns)
|
||||
|
||||
override val irFactory: IrFactory = symbolTable.irFactory
|
||||
@@ -156,9 +157,8 @@ class JsIrBackendContext(
|
||||
|
||||
private val internalPackage = module.getPackage(JS_PACKAGE_FQNAME)
|
||||
|
||||
|
||||
val dynamicType: IrDynamicType = IrDynamicTypeImpl(null, emptyList(), Variance.INVARIANT)
|
||||
val intrinsics = JsIntrinsics(irBuiltIns, this)
|
||||
override val dynamicType: IrDynamicType = IrDynamicTypeImpl(null, emptyList(), Variance.INVARIANT)
|
||||
override val intrinsics = JsIntrinsics(irBuiltIns, this)
|
||||
|
||||
override val catchAllThrowableType: IrType
|
||||
get() = dynamicType
|
||||
@@ -284,7 +284,7 @@ class JsIrBackendContext(
|
||||
val errorCodeSymbol: IrSimpleFunctionSymbol? =
|
||||
if (errorPolicy.allowErrors) symbolTable.referenceSimpleFunction(getJsInternalFunction("errorCode")) else null
|
||||
|
||||
val primitiveClassesObject = getIrClass(FqName("kotlin.reflect.js.internal.PrimitiveClasses"))
|
||||
override val primitiveClassesObject = getIrClass(FqName("kotlin.reflect.js.internal.PrimitiveClasses"))
|
||||
|
||||
val throwableClass = getIrClass(JsIrBackendContext.KOTLIN_PACKAGE_FQN.child(Name.identifier("Throwable")))
|
||||
|
||||
@@ -323,16 +323,6 @@ class JsIrBackendContext(
|
||||
override val suiteFun = getFunctions(FqName("kotlin.test.suite")).singleOrNull()?.let { symbolTable.referenceSimpleFunction(it) }
|
||||
override val testFun = getFunctions(FqName("kotlin.test.test")).singleOrNull()?.let { symbolTable.referenceSimpleFunction(it) }
|
||||
|
||||
val primitiveClassProperties by lazy2 {
|
||||
primitiveClassesObject.owner.declarations.filterIsInstance<IrProperty>()
|
||||
}
|
||||
|
||||
val primitiveClassFunctionClass by lazy2 {
|
||||
primitiveClassesObject.owner.declarations
|
||||
.filterIsInstance<IrSimpleFunction>()
|
||||
.find { it.name == Name.identifier("functionClass") }!!
|
||||
}
|
||||
|
||||
val throwableConstructors by lazy2 { throwableClass.owner.declarations.filterIsInstance<IrConstructor>().map { it.symbol } }
|
||||
val defaultThrowableCtor by lazy2 { throwableConstructors.single { !it.owner.isPrimary && it.owner.valueParameters.size == 0 } }
|
||||
|
||||
|
||||
+17
-11
@@ -8,8 +8,7 @@ package org.jetbrains.kotlin.ir.backend.js.lower
|
||||
import org.jetbrains.kotlin.backend.common.BodyLoweringPass
|
||||
import org.jetbrains.kotlin.backend.common.IrElementTransformerVoidWithContext
|
||||
import org.jetbrains.kotlin.backend.common.ir.Symbols
|
||||
import org.jetbrains.kotlin.ir.backend.js.JsIrBackendContext
|
||||
import org.jetbrains.kotlin.ir.backend.js.JsLoweredDeclarationOrigin
|
||||
import org.jetbrains.kotlin.ir.backend.js.*
|
||||
import org.jetbrains.kotlin.ir.backend.js.ir.JsIrBuilder
|
||||
import org.jetbrains.kotlin.ir.backend.js.utils.toJsArrayLiteral
|
||||
import org.jetbrains.kotlin.ir.declarations.*
|
||||
@@ -24,13 +23,20 @@ import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.types.*
|
||||
|
||||
class ClassReferenceLowering(val context: JsIrBackendContext) : BodyLoweringPass {
|
||||
private val intrinsics = context.intrinsics
|
||||
class ClassReferenceLowering(val context: JsCommonBackendContext) : BodyLoweringPass {
|
||||
|
||||
private val primitiveClassProperties by lazy {
|
||||
primitiveClassesObject.owner.declarations.filterIsInstance<IrProperty>()
|
||||
}
|
||||
|
||||
private val primitiveClassFunctionClass by lazy {
|
||||
primitiveClassesObject.owner.declarations
|
||||
.filterIsInstance<IrSimpleFunction>()
|
||||
.find { it.name == Name.identifier("functionClass") }!!
|
||||
}
|
||||
|
||||
private val primitiveClassesObject = context.primitiveClassesObject
|
||||
|
||||
private val primitiveClassProperties = context.primitiveClassProperties
|
||||
|
||||
private fun primitiveClassProperty(name: String) =
|
||||
primitiveClassProperties.singleOrNull { it.name == Name.identifier(name) }?.getter
|
||||
?: primitiveClassesObject.owner.declarations.filterIsInstance<IrSimpleFunction>().single { it.name == Name.special("<get-$name>") }
|
||||
@@ -74,7 +80,7 @@ class ClassReferenceLowering(val context: JsIrBackendContext) : BodyLoweringPass
|
||||
if (primitiveKClass != null)
|
||||
return JsIrBuilder.buildBlock(returnType, listOf(argument, primitiveKClass))
|
||||
|
||||
return JsIrBuilder.buildCall(intrinsics.jsGetKClassFromExpression, returnType, listOf(typeArgument)).apply {
|
||||
return JsIrBuilder.buildCall(context.intrinsics.jsGetKClassFromExpression, returnType, listOf(typeArgument)).apply {
|
||||
putValueArgument(0, argument)
|
||||
}
|
||||
}
|
||||
@@ -103,7 +109,7 @@ class ClassReferenceLowering(val context: JsIrBackendContext) : BodyLoweringPass
|
||||
if (typeArgument.isFunction()) {
|
||||
val functionInterface = typeArgument.getClass()!!
|
||||
val arity = functionInterface.typeParameters.size - 1
|
||||
return getPrimitiveClass(context.primitiveClassFunctionClass, returnType).apply {
|
||||
return getPrimitiveClass(primitiveClassFunctionClass, returnType).apply {
|
||||
putValueArgument(0, JsIrBuilder.buildInt(context.irBuiltIns.intType, arity))
|
||||
}
|
||||
}
|
||||
@@ -112,7 +118,7 @@ class ClassReferenceLowering(val context: JsIrBackendContext) : BodyLoweringPass
|
||||
}
|
||||
|
||||
private fun callGetKClass(
|
||||
returnType: IrType = intrinsics.jsGetKClass.owner.returnType,
|
||||
returnType: IrType = context.intrinsics.jsGetKClass.owner.returnType,
|
||||
typeArgument: IrType
|
||||
): IrCall {
|
||||
val primitiveKClass =
|
||||
@@ -121,7 +127,7 @@ class ClassReferenceLowering(val context: JsIrBackendContext) : BodyLoweringPass
|
||||
if (primitiveKClass != null)
|
||||
return primitiveKClass
|
||||
|
||||
return JsIrBuilder.buildCall(intrinsics.jsGetKClass, returnType, listOf(typeArgument))
|
||||
return JsIrBuilder.buildCall(context.intrinsics.jsGetKClass, returnType, listOf(typeArgument))
|
||||
.apply {
|
||||
putValueArgument(0, callJsClass(typeArgument))
|
||||
}
|
||||
@@ -129,7 +135,7 @@ class ClassReferenceLowering(val context: JsIrBackendContext) : BodyLoweringPass
|
||||
|
||||
private fun callJsClass(type: IrType) =
|
||||
JsIrBuilder.buildCall(
|
||||
intrinsics.jsClass,
|
||||
context.intrinsics.jsClass,
|
||||
typeArguments = listOf(type),
|
||||
origin = JsLoweredDeclarationOrigin.CLASS_REFERENCE
|
||||
)
|
||||
|
||||
@@ -71,7 +71,7 @@ fun IrDeclaration.hasStaticDispatch() = when (this) {
|
||||
else -> true
|
||||
}
|
||||
|
||||
fun List<IrExpression>.toJsArrayLiteral(context: JsIrBackendContext, arrayType: IrType, elementType: IrType): IrExpression {
|
||||
fun List<IrExpression>.toJsArrayLiteral(context: JsCommonBackendContext, arrayType: IrType, elementType: IrType): IrExpression {
|
||||
val irVararg = IrVarargImpl(UNDEFINED_OFFSET, UNDEFINED_OFFSET, arrayType, elementType, this)
|
||||
|
||||
return IrCallImpl(
|
||||
|
||||
+34
-2
@@ -24,17 +24,42 @@ import org.jetbrains.kotlin.ir.declarations.impl.IrExternalPackageFragmentImpl
|
||||
import org.jetbrains.kotlin.ir.declarations.impl.IrFileImpl
|
||||
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
|
||||
import org.jetbrains.kotlin.ir.symbols.impl.DescriptorlessExternalPackageFragmentSymbol
|
||||
import org.jetbrains.kotlin.ir.types.IrDynamicType
|
||||
import org.jetbrains.kotlin.ir.types.IrTypeSystemContext
|
||||
import org.jetbrains.kotlin.ir.types.IrTypeSystemContextImpl
|
||||
import org.jetbrains.kotlin.ir.util.SymbolTable
|
||||
import org.jetbrains.kotlin.name.FqName
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
|
||||
class WasmIntrinsics(private val context: WasmBackendContext) : Intrinsics {
|
||||
|
||||
override val jsGetKClass: IrSimpleFunctionSymbol get() = context.wasmSymbols.jsGetKClass
|
||||
override val jsGetKClassFromExpression: IrSimpleFunctionSymbol get() = context.wasmSymbols.jsGetKClassFromExpression
|
||||
override val jsClass: IrSimpleFunctionSymbol get() = context.wasmSymbols.jsClass
|
||||
|
||||
override val createKType: IrSimpleFunctionSymbol?
|
||||
get() = TODO("Not yet implemented")
|
||||
override val createDynamicKType: IrSimpleFunctionSymbol?
|
||||
get() = TODO("Not yet implemented")
|
||||
override val createKTypeParameter: IrSimpleFunctionSymbol?
|
||||
get() = TODO("Not yet implemented")
|
||||
override val getStarKTypeProjection: IrSimpleFunctionSymbol?
|
||||
get() = TODO("Not yet implemented")
|
||||
override val createCovariantKTypeProjection: IrSimpleFunctionSymbol?
|
||||
get() = TODO("Not yet implemented")
|
||||
override val createInvariantKTypeProjection: IrSimpleFunctionSymbol?
|
||||
get() = TODO("Not yet implemented")
|
||||
override val createContravariantKTypeProjection: IrSimpleFunctionSymbol?
|
||||
get() = TODO("Not yet implemented")
|
||||
override val arrayLiteral: IrSimpleFunctionSymbol
|
||||
get() = TODO("Not yet implemented")
|
||||
}
|
||||
|
||||
class WasmBackendContext(
|
||||
val module: ModuleDescriptor,
|
||||
override val irBuiltIns: IrBuiltIns,
|
||||
symbolTable: SymbolTable,
|
||||
irModuleFragment: IrModuleFragment,
|
||||
val symbolTable: SymbolTable,
|
||||
val irModuleFragment: IrModuleFragment,
|
||||
override val configuration: CompilerConfiguration,
|
||||
) : JsCommonBackendContext {
|
||||
override val builtIns = module.builtIns
|
||||
@@ -43,6 +68,11 @@ class WasmBackendContext(
|
||||
override val scriptMode = false
|
||||
override val irFactory: IrFactory = symbolTable.irFactory
|
||||
|
||||
//TODO
|
||||
override val dynamicType: IrDynamicType get() = TODO()
|
||||
override val primitiveClassesObject get() = wasmSymbols.primitiveClassesObject
|
||||
//TODO!!!
|
||||
|
||||
// Place to store declarations excluded from code generation
|
||||
private val excludedDeclarations = mutableMapOf<FqName, IrPackageFragment>()
|
||||
|
||||
@@ -169,4 +199,6 @@ class WasmBackendContext(
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override val intrinsics: Intrinsics = WasmIntrinsics(this)
|
||||
}
|
||||
|
||||
+9
-2
@@ -416,6 +416,12 @@ private val staticMembersLoweringPhase = makeWasmModulePhase(
|
||||
description = "Move static member declarations to top-level"
|
||||
)
|
||||
|
||||
private val classReferenceLoweringPhase = makeWasmModulePhase(
|
||||
::ClassReferenceLowering,
|
||||
name = "ClassReferenceLowering",
|
||||
description = "Handle class references"
|
||||
)
|
||||
|
||||
private val wasmVarargExpressionLoweringPhase = makeWasmModulePhase(
|
||||
::WasmVarargExpressionLowering,
|
||||
name = "WasmVarargExpressionLowering",
|
||||
@@ -521,6 +527,8 @@ val wasmPhases = NamedCompilerPhase(
|
||||
wrapInlineDeclarationsWithReifiedTypeParametersPhase then
|
||||
|
||||
functionInliningPhase then
|
||||
removeInlineDeclarationsWithReifiedTypeParametersLoweringPhase then
|
||||
|
||||
lateinitNullableFieldsPhase then
|
||||
lateinitDeclarationLoweringPhase then
|
||||
lateinitUsageLoweringPhase then
|
||||
@@ -577,11 +585,10 @@ val wasmPhases = NamedCompilerPhase(
|
||||
defaultArgumentPatchOverridesPhase then
|
||||
defaultParameterInjectorPhase then
|
||||
defaultParameterCleanerPhase then
|
||||
removeInlineDeclarationsWithReifiedTypeParametersLoweringPhase then
|
||||
|
||||
// TODO:
|
||||
// multipleCatchesLoweringPhase then
|
||||
// classReferenceLoweringPhase then
|
||||
classReferenceLoweringPhase then
|
||||
|
||||
wasmVarargExpressionLoweringPhase then
|
||||
inlineClassDeclarationLoweringPhase then
|
||||
|
||||
@@ -45,6 +45,12 @@ class WasmSymbols(
|
||||
private val kotlinTestPackage: PackageViewDescriptor =
|
||||
context.module.getPackage(FqName("kotlin.test"))
|
||||
|
||||
val jsGetKClass: IrSimpleFunctionSymbol = getInternalFunction("getKClass")
|
||||
val jsGetKClassFromExpression: IrSimpleFunctionSymbol = getInternalFunction("getKClassFromExpression")
|
||||
val jsClass: IrSimpleFunctionSymbol = getInternalFunction("wasmGetTypeInfoData")
|
||||
val wasmTypeInfoData: IrClassSymbol = getInternalClass("TypeInfoData")
|
||||
val primitiveClassesObject = getInternalClass("PrimitiveClasses")
|
||||
|
||||
override val throwNullPointerException = getInternalFunction("THROW_NPE")
|
||||
override val throwISE = getInternalFunction("THROW_ISE")
|
||||
override val throwTypeCastException = getInternalFunction("THROW_CCE")
|
||||
@@ -145,6 +151,7 @@ class WasmSymbols(
|
||||
|
||||
val wasmClassId = getInternalFunction("wasmClassId")
|
||||
val wasmInterfaceId = getInternalFunction("wasmInterfaceId")
|
||||
val wasmTypeId = getInternalFunction("wasmTypeId")
|
||||
|
||||
val getVirtualMethodId = getInternalFunction("getVirtualMethodId")
|
||||
val getInterfaceImplId = getInternalFunction("getInterfaceImplId")
|
||||
|
||||
+7
@@ -348,6 +348,13 @@ class BodyGenerator(val context: WasmFunctionCodegenContext) : IrElementVisitorV
|
||||
body.buildConstI32Symbol(context.referenceInterfaceId(irInterface.symbol))
|
||||
}
|
||||
|
||||
wasmSymbols.wasmTypeId -> {
|
||||
val type = call.getTypeArgument(0)!!.getClass()
|
||||
?: error("No class given for wasmClassId intrinsic")
|
||||
val id = if (type.isInterface) context.referenceInterfaceId(type.symbol) else context.referenceClassId(type.symbol)
|
||||
body.buildConstI32Symbol(id)
|
||||
}
|
||||
|
||||
wasmSymbols.wasmRefCast -> {
|
||||
val toType = call.getTypeArgument(0)!!
|
||||
generateTypeRTT(toType)
|
||||
|
||||
+21
@@ -22,6 +22,7 @@ import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol
|
||||
import org.jetbrains.kotlin.ir.util.*
|
||||
import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid
|
||||
import org.jetbrains.kotlin.ir.visitors.acceptVoid
|
||||
import org.jetbrains.kotlin.name.parentOrNull
|
||||
import org.jetbrains.kotlin.wasm.ir.*
|
||||
|
||||
class DeclarationGenerator(val context: WasmModuleCodegenContext) : IrElementVisitorVoid {
|
||||
@@ -274,6 +275,24 @@ class DeclarationGenerator(val context: WasmModuleCodegenContext) : IrElementVis
|
||||
|
||||
private fun binaryDataStruct(classMetadata: ClassMetadata): ConstantDataStruct {
|
||||
val invalidIndex = -1
|
||||
|
||||
val packageName = classMetadata.klass.kotlinFqName.parentOrNull()?.asString() ?: ""
|
||||
val simpleName = classMetadata.klass.kotlinFqName.shortName().asString()
|
||||
val typeInfo = ConstantDataStruct(
|
||||
"TypeInfo",
|
||||
listOf(
|
||||
ConstantDataIntField("TypePackageNameLength", packageName.length),
|
||||
ConstantDataIntField("TypePackageNamePtr", context.referenceStringLiteral(packageName)),
|
||||
ConstantDataIntField("TypeNameLength", simpleName.length),
|
||||
ConstantDataIntField("TypeNamePtr", context.referenceStringLiteral(simpleName))
|
||||
)
|
||||
)
|
||||
|
||||
val superClass = classMetadata.klass.getSuperClass(context.backendContext.irBuiltIns)
|
||||
val superTypeId = superClass?.let {
|
||||
ConstantDataIntField("SuperTypeId", context.referenceClassId(it.symbol))
|
||||
} ?: ConstantDataIntField("SuperTypeId", -1)
|
||||
|
||||
val vtableSizeField = ConstantDataIntField(
|
||||
"V-table length",
|
||||
classMetadata.virtualMethods.size
|
||||
@@ -298,6 +317,8 @@ class DeclarationGenerator(val context: WasmModuleCodegenContext) : IrElementVis
|
||||
return ConstantDataStruct(
|
||||
"Class TypeInfo: ${classMetadata.klass.fqNameWhenAvailable} ",
|
||||
listOf(
|
||||
typeInfo,
|
||||
superTypeId,
|
||||
interfaceTablePtr,
|
||||
vtableSizeField,
|
||||
vtableArray,
|
||||
|
||||
+23
-1
@@ -12,18 +12,21 @@ import org.jetbrains.kotlin.backend.common.lower.createIrBuilder
|
||||
import org.jetbrains.kotlin.backend.wasm.WasmBackendContext
|
||||
import org.jetbrains.kotlin.ir.backend.js.utils.isEqualsInheritedFromAny
|
||||
import org.jetbrains.kotlin.ir.builders.irCall
|
||||
import org.jetbrains.kotlin.ir.builders.irCallConstructor
|
||||
import org.jetbrains.kotlin.ir.builders.irComposite
|
||||
import org.jetbrains.kotlin.ir.builders.irInt
|
||||
import org.jetbrains.kotlin.ir.declarations.IrFile
|
||||
import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction
|
||||
import org.jetbrains.kotlin.ir.expressions.IrCall
|
||||
import org.jetbrains.kotlin.ir.expressions.IrExpression
|
||||
import org.jetbrains.kotlin.ir.interpreter.toIrConst
|
||||
import org.jetbrains.kotlin.ir.types.IrType
|
||||
import org.jetbrains.kotlin.ir.types.classFqName
|
||||
import org.jetbrains.kotlin.ir.types.getClass
|
||||
import org.jetbrains.kotlin.ir.types.isNullable
|
||||
import org.jetbrains.kotlin.ir.util.*
|
||||
import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
|
||||
import org.jetbrains.kotlin.util.OperatorNameConventions
|
||||
import org.jetbrains.kotlin.name.parentOrNull
|
||||
|
||||
class BuiltInsLowering(val context: WasmBackendContext) : FileLoweringPass {
|
||||
private val irBuiltins = context.irBuiltIns
|
||||
@@ -125,6 +128,25 @@ class BuiltInsLowering(val context: WasmBackendContext) : FileLoweringPass {
|
||||
val newSymbol = irBuiltins.suspendFunctionN(arity).getSimpleFunction("invoke")!!
|
||||
return irCall(call, newSymbol, argumentsAsReceivers = true)
|
||||
}
|
||||
symbols.jsClass -> {
|
||||
val infoDataCtor = symbols.wasmTypeInfoData.constructors.first()
|
||||
val type = call.getTypeArgument(0)!!
|
||||
val fqName = type.classFqName!!
|
||||
val packageName = fqName.parentOrNull()?.asString() ?: ""
|
||||
val typeName = fqName.shortName().asString()
|
||||
|
||||
return with(builder) {
|
||||
val typeId = irCall(symbols.wasmTypeId).also {
|
||||
it.putTypeArgument(0, type)
|
||||
}
|
||||
|
||||
irCallConstructor(infoDataCtor, emptyList()).also {
|
||||
it.putValueArgument(0, typeId)
|
||||
it.putValueArgument(1, packageName.toIrConst(context.irBuiltIns.stringType))
|
||||
it.putValueArgument(2, typeName.toIrConst(context.irBuiltIns.stringType))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return call
|
||||
|
||||
Reference in New Issue
Block a user