From d7e3f826bb005b91e776cc2692dc9a0dcaa3d240 Mon Sep 17 00:00:00 2001 From: Zalim Bashorov Date: Thu, 17 Dec 2020 01:35:38 +0300 Subject: [PATCH] [JS IR] Generate jsClass function inside the compiler instead of relying on declaration inside stdlib We want to make its type parameter reified to do it we have to make it inline and non-external. But we don't want to inline it in call sites and want prevent using it anywhere except places generated by the compiler. In user code, including stdlib, we should use `K::class.js` instead. Separately we have to intrinsify `K::class.js` to reduce overhead. --- .../kotlin/ir/backend/js/JsIntrinsics.kt | 23 +++++++++++++++---- .../stdlib/js-ir/src/kotlin/jsClass_js-ir.kt | 8 ------- 2 files changed, 19 insertions(+), 12 deletions(-) delete mode 100644 libraries/stdlib/js-ir/src/kotlin/jsClass_js-ir.kt diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsIntrinsics.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsIntrinsics.kt index f58a6310878..d20400aab4a 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsIntrinsics.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsIntrinsics.kt @@ -17,11 +17,9 @@ import org.jetbrains.kotlin.ir.declarations.IrSimpleFunction import org.jetbrains.kotlin.ir.declarations.impl.IrExternalPackageFragmentImpl import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol -import org.jetbrains.kotlin.ir.types.IrType -import org.jetbrains.kotlin.ir.types.defaultType +import org.jetbrains.kotlin.ir.types.* import org.jetbrains.kotlin.ir.types.impl.IrSimpleTypeBuilder import org.jetbrains.kotlin.ir.types.impl.buildSimpleType -import org.jetbrains.kotlin.ir.types.isLong import org.jetbrains.kotlin.ir.util.constructors import org.jetbrains.kotlin.ir.util.findDeclaration import org.jetbrains.kotlin.ir.util.kotlinPackageFqn @@ -169,7 +167,9 @@ class JsIntrinsics(private val irBuiltIns: IrBuiltIns, val context: JsIrBackendC val jsGetContinuation = getInternalFunction("getContinuation") val jsGetKClass = getInternalWithoutPackage("getKClass") val jsGetKClassFromExpression = getInternalWithoutPackage("getKClassFromExpression") - val jsClass = getInternalFunction("jsClass") + + private val jsClassClassSymbol = getInternalClassWithoutPackage("kotlin.js.JsClass") + val jsClass = defineJsClassIntrinsic().symbol val jsNumberRangeToNumber = getInternalFunction("numberRangeToNumber") val jsNumberRangeToLong = getInternalFunction("numberRangeToLong") @@ -448,6 +448,21 @@ class JsIntrinsics(private val irBuiltIns: IrBuiltIns, val context: JsIrBackendC } } + private fun defineJsClassIntrinsic(): IrSimpleFunction { + return irFactory.addFunction(externalPackageFragment) { + name = Name.identifier("jsClass") + origin = JsLoweredDeclarationOrigin.JS_INTRINSICS_STUB + isInline = true + }.apply { + val typeParameter = addTypeParameter { + name = Name.identifier("T") + isReified = true + superTypes += irBuiltIns.anyType + } + returnType = jsClassClassSymbol.typeWithParameters(listOf(typeParameter)) + } + } + private fun unOp(name: String, returnType: IrType = irBuiltIns.anyNType) = irBuiltIns.run { defineOperator(name, returnType, listOf(anyNType)) } diff --git a/libraries/stdlib/js-ir/src/kotlin/jsClass_js-ir.kt b/libraries/stdlib/js-ir/src/kotlin/jsClass_js-ir.kt deleted file mode 100644 index a538c7268d8..00000000000 --- a/libraries/stdlib/js-ir/src/kotlin/jsClass_js-ir.kt +++ /dev/null @@ -1,8 +0,0 @@ -/* - * Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors. - * Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file. - */ - -package kotlin.js - -internal inline fun jsClass(): JsClass = throw NotImplementedError("Implemented as intrinsic")