From e3699fbc31ca317fa4d83b3d73d1cee09d99d281 Mon Sep 17 00:00:00 2001 From: Roman Artemev Date: Mon, 8 Apr 2019 14:46:16 +0300 Subject: [PATCH] [IR] `CommonBackendContext` and `Symbols` refactoring * add [suspend]functionN factory factions * add `getContinuation` property * add `internalPackageName` property * fix lowerings --- .../jetbrains/kotlin/backend/common/BackendContext.kt | 2 ++ .../src/org/jetbrains/kotlin/backend/common/ir/Ir.kt | 10 ++++++---- .../kotlin/ir/backend/js/JsIrBackendContext.kt | 9 ++++----- .../ir/backend/js/lower/CallableReferenceLowering.kt | 6 +++--- .../backend/js/lower/JsDefaultArgumentStubGenerator.kt | 3 +-- .../kotlin/ir/backend/js/lower/TestGenerator.kt | 2 +- .../jetbrains/kotlin/backend/jvm/JvmBackendContext.kt | 2 ++ .../src/org/jetbrains/kotlin/backend/jvm/JvmSymbols.kt | 3 +++ 8 files changed, 22 insertions(+), 15 deletions(-) diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/BackendContext.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/BackendContext.kt index 2aaf9b4ab3d..a28ba189fa7 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/BackendContext.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/BackendContext.kt @@ -21,6 +21,7 @@ import org.jetbrains.kotlin.backend.common.ir.Ir import org.jetbrains.kotlin.backend.common.ir.SharedVariablesManager import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns +import org.jetbrains.kotlin.name.FqName interface BackendContext { val ir: Ir @@ -28,4 +29,5 @@ interface BackendContext { val irBuiltIns: IrBuiltIns val sharedVariablesManager: SharedVariablesManager val declarationFactory: DeclarationFactory + val internalPackageFqn: FqName } diff --git a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/ir/Ir.kt b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/ir/Ir.kt index 96f85e0e283..afac31b72dd 100644 --- a/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/ir/Ir.kt +++ b/compiler/ir/backend.common/src/org/jetbrains/kotlin/backend/common/ir/Ir.kt @@ -19,10 +19,7 @@ import org.jetbrains.kotlin.ir.symbols.IrClassSymbol import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol import org.jetbrains.kotlin.ir.types.classOrNull -import org.jetbrains.kotlin.ir.util.ReferenceSymbolTable -import org.jetbrains.kotlin.ir.util.fqNameWhenAvailable -import org.jetbrains.kotlin.ir.util.getPackageFragment -import org.jetbrains.kotlin.ir.util.referenceFunction +import org.jetbrains.kotlin.ir.util.* import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.resolve.calls.components.isVararg @@ -153,6 +150,8 @@ abstract class Symbols(val context: T, private val abstract val coroutineSuspendedGetter: IrSimpleFunctionSymbol + abstract val getContinuation: IrSimpleFunctionSymbol + private val binaryOperatorCache = mutableMapOf, IrFunctionSymbol>() fun getBinaryOperator(name: Name, lhsType: KotlinType, rhsType: KotlinType): IrFunctionSymbol { @@ -184,6 +183,9 @@ abstract class Symbols(val context: T, private val val intAnd = getBinaryOperator(OperatorNameConventions.AND, builtIns.intType, builtIns.intType) val intPlusInt = getBinaryOperator(OperatorNameConventions.PLUS, builtIns.intType, builtIns.intType) + fun functionN(n: Int): IrClassSymbol = symbolTable.referenceClass(builtIns.getFunction(n)) + fun suspendFunctionN(n: Int): IrClassSymbol = symbolTable.referenceClass(builtIns.getSuspendFunction(n)) + companion object { fun isLateinitIsInitializedPropertyGetter(symbol: IrFunctionSymbol): Boolean = symbol is IrSimpleFunctionSymbol && symbol.owner.let { function -> diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsIrBackendContext.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsIrBackendContext.kt index b1465681df5..f58a5302994 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsIrBackendContext.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/JsIrBackendContext.kt @@ -135,13 +135,10 @@ class JsIrBackendContext( val intrinsics = JsIntrinsics(irBuiltIns, this) + override val internalPackageFqn = JS_PACKAGE_FQNAME + private val operatorMap = referenceOperators() - // TODO: get rid of this - - fun functionN(n: Int) = symbolTable.lazyWrapper.referenceClass(builtIns.getFunction(n)) - fun suspendFunctionN(n: Int) = symbolTable.lazyWrapper.referenceClass(builtIns.getSuspendFunction(n)) - private fun primitivesWithImplicitCompanionObject(): List { val numbers = PrimitiveType.NUMBER_TYPES .filter { it.name != "LONG" && it.name != "CHAR" } // skip due to they have own explicit companions @@ -184,6 +181,8 @@ class JsIrBackendContext( NoLookupLocation.FROM_BACKEND ).filterNot { it.isExpect }.single().getter!! ) + + override val getContinuation = symbolTable.referenceSimpleFunction(getJsInternalFunction("getContinuation")) } override fun shouldGenerateHandlerParameterForDefaultBodyFun() = true diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/CallableReferenceLowering.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/CallableReferenceLowering.kt index de7ec6d4577..06f3e85f632 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/CallableReferenceLowering.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/CallableReferenceLowering.kt @@ -210,7 +210,7 @@ class CallableReferenceLowering(val context: JsIrBackendContext) : FileLoweringP val additionalDeclarations = generateFactoryBodyWithGuard(factoryFunction) { val statements = mutableListOf(getterFunction) - val getterFunctionTypeSymbol = context.functionN(getterFunction.valueParameters.size + 1) + val getterFunctionTypeSymbol = context.ir.symbols.functionN(getterFunction.valueParameters.size + 1) val getterFunctionIrType = IrSimpleTypeImpl(getterFunctionTypeSymbol, false, emptyList(), emptyList()) val irGetReference = JsIrBuilder.buildFunctionReference(getterFunctionIrType, getterFunction.symbol) @@ -226,7 +226,7 @@ class CallableReferenceLowering(val context: JsIrBackendContext) : FileLoweringP if (setterFunction != null) { statements += setterFunction - val setterFunctionTypeSymbol = context.functionN(setterFunction.valueParameters.size + 1) + val setterFunctionTypeSymbol = context.ir.symbols.functionN(setterFunction.valueParameters.size + 1) val setterFunctionIrType = IrSimpleTypeImpl(setterFunctionTypeSymbol, false, emptyList(), emptyList()) val irSetReference = JsIrBuilder.buildFunctionReference(setterFunctionIrType, setterFunction.symbol) statements += JsIrBuilder.buildCall(context.intrinsics.jsSetJSField.symbol).apply { @@ -280,7 +280,7 @@ class CallableReferenceLowering(val context: JsIrBackendContext) : FileLoweringP val additionalDeclarations = generateFactoryBodyWithGuard(factoryFunction) { val statements = mutableListOf(closureFunction) - val getterFunctionTypeSymbol = context.functionN(closureFunction.valueParameters.size + 1) + val getterFunctionTypeSymbol = context.ir.symbols.functionN(closureFunction.valueParameters.size + 1) val getterFunctionIrType = IrSimpleTypeImpl(getterFunctionTypeSymbol, false, emptyList(), emptyList()) val irGetReference = JsIrBuilder.buildFunctionReference(getterFunctionIrType, closureFunction.symbol) val irVar = JsIrBuilder.buildVar(getterFunctionIrType, factoryFunction, initializer = irGetReference) diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/JsDefaultArgumentStubGenerator.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/JsDefaultArgumentStubGenerator.kt index d5b20762070..f8229554e26 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/JsDefaultArgumentStubGenerator.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/JsDefaultArgumentStubGenerator.kt @@ -50,8 +50,7 @@ class JsDefaultArgumentStubGenerator(override val context: JsIrBackendContext) : private fun resolveInvoke(paramCount: Int): IrSimpleFunction { assert(paramCount > 0) - val fqn = FqName.fromSegments(listOf("kotlin", "Function$paramCount")) - val functionKlass = context.functionN(paramCount).owner + val functionKlass = context.ir.symbols.functionN(paramCount).owner return functionKlass.declarations.filterIsInstance().first { it.name == Name.identifier("invoke") } } } diff --git a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/TestGenerator.kt b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/TestGenerator.kt index fdc724bcad6..78a669a427b 100644 --- a/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/TestGenerator.kt +++ b/compiler/ir/backend.js/src/org/jetbrains/kotlin/ir/backend/js/lower/TestGenerator.kt @@ -61,7 +61,7 @@ class TestGenerator(val context: JsIrBackendContext) : FileLoweringPass { putValueArgument(0, JsIrBuilder.buildString(context.irBuiltIns.stringType, name)) putValueArgument(1, JsIrBuilder.buildBoolean(context.irBuiltIns.booleanType, ignored)) - val refType = IrSimpleTypeImpl(context.functionN(0), false, emptyList(), emptyList()) + val refType = IrSimpleTypeImpl(context.ir.symbols.functionN(0), false, emptyList(), emptyList()) putValueArgument(2, JsIrBuilder.buildFunctionReference(refType, function.symbol)) } diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmBackendContext.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmBackendContext.kt index b60c49e476e..422c9061782 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmBackendContext.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmBackendContext.kt @@ -45,6 +45,8 @@ class JvmBackendContext( override val configuration get() = state.configuration + override val internalPackageFqn = FqName("kotlin.jvm") + internal fun getTopLevelClass(fqName: FqName): IrClassSymbol { val descriptor = state.module.getPackage(fqName.parent()).memberScope.getContributedClassifier( fqName.shortName(), NoLookupLocation.FROM_BACKEND diff --git a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmSymbols.kt b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmSymbols.kt index 92454eff899..864810c13eb 100644 --- a/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmSymbols.kt +++ b/compiler/ir/backend.jvm/src/org/jetbrains/kotlin/backend/jvm/JvmSymbols.kt @@ -90,6 +90,9 @@ class JvmSymbols( override val coroutineSuspendedGetter: IrSimpleFunctionSymbol get() = TODO("not implemented") + override val getContinuation: IrSimpleFunctionSymbol + get() = TODO("not implemented") + val javaLangClass: IrClassSymbol = context.getTopLevelClass(FqName("java.lang.Class"))