[IR] CommonBackendContext and Symbols refactoring

* add [suspend]functionN factory factions
 * add `getContinuation` property
 * add `internalPackageName` property
 * fix lowerings
This commit is contained in:
Roman Artemev
2019-04-08 14:46:16 +03:00
committed by romanart
parent bdab7a88f6
commit e3699fbc31
8 changed files with 22 additions and 15 deletions
@@ -21,6 +21,7 @@ import org.jetbrains.kotlin.backend.common.ir.Ir
import org.jetbrains.kotlin.backend.common.ir.SharedVariablesManager import org.jetbrains.kotlin.backend.common.ir.SharedVariablesManager
import org.jetbrains.kotlin.builtins.KotlinBuiltIns import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
import org.jetbrains.kotlin.name.FqName
interface BackendContext { interface BackendContext {
val ir: Ir<CommonBackendContext> val ir: Ir<CommonBackendContext>
@@ -28,4 +29,5 @@ interface BackendContext {
val irBuiltIns: IrBuiltIns val irBuiltIns: IrBuiltIns
val sharedVariablesManager: SharedVariablesManager val sharedVariablesManager: SharedVariablesManager
val declarationFactory: DeclarationFactory val declarationFactory: DeclarationFactory
val internalPackageFqn: FqName
} }
@@ -19,10 +19,7 @@ import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
import org.jetbrains.kotlin.ir.types.classOrNull import org.jetbrains.kotlin.ir.types.classOrNull
import org.jetbrains.kotlin.ir.util.ReferenceSymbolTable import org.jetbrains.kotlin.ir.util.*
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.name.FqName import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.calls.components.isVararg import org.jetbrains.kotlin.resolve.calls.components.isVararg
@@ -153,6 +150,8 @@ abstract class Symbols<out T : CommonBackendContext>(val context: T, private val
abstract val coroutineSuspendedGetter: IrSimpleFunctionSymbol abstract val coroutineSuspendedGetter: IrSimpleFunctionSymbol
abstract val getContinuation: IrSimpleFunctionSymbol
private val binaryOperatorCache = mutableMapOf<Triple<Name, KotlinType, KotlinType>, IrFunctionSymbol>() private val binaryOperatorCache = mutableMapOf<Triple<Name, KotlinType, KotlinType>, IrFunctionSymbol>()
fun getBinaryOperator(name: Name, lhsType: KotlinType, rhsType: KotlinType): IrFunctionSymbol { fun getBinaryOperator(name: Name, lhsType: KotlinType, rhsType: KotlinType): IrFunctionSymbol {
@@ -184,6 +183,9 @@ abstract class Symbols<out T : CommonBackendContext>(val context: T, private val
val intAnd = getBinaryOperator(OperatorNameConventions.AND, builtIns.intType, builtIns.intType) val intAnd = getBinaryOperator(OperatorNameConventions.AND, builtIns.intType, builtIns.intType)
val intPlusInt = getBinaryOperator(OperatorNameConventions.PLUS, 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 { companion object {
fun isLateinitIsInitializedPropertyGetter(symbol: IrFunctionSymbol): Boolean = fun isLateinitIsInitializedPropertyGetter(symbol: IrFunctionSymbol): Boolean =
symbol is IrSimpleFunctionSymbol && symbol.owner.let { function -> symbol is IrSimpleFunctionSymbol && symbol.owner.let { function ->
@@ -135,13 +135,10 @@ class JsIrBackendContext(
val intrinsics = JsIntrinsics(irBuiltIns, this) val intrinsics = JsIntrinsics(irBuiltIns, this)
override val internalPackageFqn = JS_PACKAGE_FQNAME
private val operatorMap = referenceOperators() 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<Name> { private fun primitivesWithImplicitCompanionObject(): List<Name> {
val numbers = PrimitiveType.NUMBER_TYPES val numbers = PrimitiveType.NUMBER_TYPES
.filter { it.name != "LONG" && it.name != "CHAR" } // skip due to they have own explicit companions .filter { it.name != "LONG" && it.name != "CHAR" } // skip due to they have own explicit companions
@@ -184,6 +181,8 @@ class JsIrBackendContext(
NoLookupLocation.FROM_BACKEND NoLookupLocation.FROM_BACKEND
).filterNot { it.isExpect }.single().getter!! ).filterNot { it.isExpect }.single().getter!!
) )
override val getContinuation = symbolTable.referenceSimpleFunction(getJsInternalFunction("getContinuation"))
} }
override fun shouldGenerateHandlerParameterForDefaultBodyFun() = true override fun shouldGenerateHandlerParameterForDefaultBodyFun() = true
@@ -210,7 +210,7 @@ class CallableReferenceLowering(val context: JsIrBackendContext) : FileLoweringP
val additionalDeclarations = generateFactoryBodyWithGuard(factoryFunction) { val additionalDeclarations = generateFactoryBodyWithGuard(factoryFunction) {
val statements = mutableListOf<IrStatement>(getterFunction) val statements = mutableListOf<IrStatement>(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 getterFunctionIrType = IrSimpleTypeImpl(getterFunctionTypeSymbol, false, emptyList(), emptyList())
val irGetReference = JsIrBuilder.buildFunctionReference(getterFunctionIrType, getterFunction.symbol) val irGetReference = JsIrBuilder.buildFunctionReference(getterFunctionIrType, getterFunction.symbol)
@@ -226,7 +226,7 @@ class CallableReferenceLowering(val context: JsIrBackendContext) : FileLoweringP
if (setterFunction != null) { if (setterFunction != null) {
statements += setterFunction 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 setterFunctionIrType = IrSimpleTypeImpl(setterFunctionTypeSymbol, false, emptyList(), emptyList())
val irSetReference = JsIrBuilder.buildFunctionReference(setterFunctionIrType, setterFunction.symbol) val irSetReference = JsIrBuilder.buildFunctionReference(setterFunctionIrType, setterFunction.symbol)
statements += JsIrBuilder.buildCall(context.intrinsics.jsSetJSField.symbol).apply { statements += JsIrBuilder.buildCall(context.intrinsics.jsSetJSField.symbol).apply {
@@ -280,7 +280,7 @@ class CallableReferenceLowering(val context: JsIrBackendContext) : FileLoweringP
val additionalDeclarations = generateFactoryBodyWithGuard(factoryFunction) { val additionalDeclarations = generateFactoryBodyWithGuard(factoryFunction) {
val statements = mutableListOf<IrStatement>(closureFunction) val statements = mutableListOf<IrStatement>(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 getterFunctionIrType = IrSimpleTypeImpl(getterFunctionTypeSymbol, false, emptyList(), emptyList())
val irGetReference = JsIrBuilder.buildFunctionReference(getterFunctionIrType, closureFunction.symbol) val irGetReference = JsIrBuilder.buildFunctionReference(getterFunctionIrType, closureFunction.symbol)
val irVar = JsIrBuilder.buildVar(getterFunctionIrType, factoryFunction, initializer = irGetReference) val irVar = JsIrBuilder.buildVar(getterFunctionIrType, factoryFunction, initializer = irGetReference)
@@ -50,8 +50,7 @@ class JsDefaultArgumentStubGenerator(override val context: JsIrBackendContext) :
private fun resolveInvoke(paramCount: Int): IrSimpleFunction { private fun resolveInvoke(paramCount: Int): IrSimpleFunction {
assert(paramCount > 0) assert(paramCount > 0)
val fqn = FqName.fromSegments(listOf("kotlin", "Function$paramCount")) val functionKlass = context.ir.symbols.functionN(paramCount).owner
val functionKlass = context.functionN(paramCount).owner
return functionKlass.declarations.filterIsInstance<IrSimpleFunction>().first { it.name == Name.identifier("invoke") } return functionKlass.declarations.filterIsInstance<IrSimpleFunction>().first { it.name == Name.identifier("invoke") }
} }
} }
@@ -61,7 +61,7 @@ class TestGenerator(val context: JsIrBackendContext) : FileLoweringPass {
putValueArgument(0, JsIrBuilder.buildString(context.irBuiltIns.stringType, name)) putValueArgument(0, JsIrBuilder.buildString(context.irBuiltIns.stringType, name))
putValueArgument(1, JsIrBuilder.buildBoolean(context.irBuiltIns.booleanType, ignored)) 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)) putValueArgument(2, JsIrBuilder.buildFunctionReference(refType, function.symbol))
} }
@@ -45,6 +45,8 @@ class JvmBackendContext(
override val configuration get() = state.configuration override val configuration get() = state.configuration
override val internalPackageFqn = FqName("kotlin.jvm")
internal fun getTopLevelClass(fqName: FqName): IrClassSymbol { internal fun getTopLevelClass(fqName: FqName): IrClassSymbol {
val descriptor = state.module.getPackage(fqName.parent()).memberScope.getContributedClassifier( val descriptor = state.module.getPackage(fqName.parent()).memberScope.getContributedClassifier(
fqName.shortName(), NoLookupLocation.FROM_BACKEND fqName.shortName(), NoLookupLocation.FROM_BACKEND
@@ -90,6 +90,9 @@ class JvmSymbols(
override val coroutineSuspendedGetter: IrSimpleFunctionSymbol override val coroutineSuspendedGetter: IrSimpleFunctionSymbol
get() = TODO("not implemented") get() = TODO("not implemented")
override val getContinuation: IrSimpleFunctionSymbol
get() = TODO("not implemented")
val javaLangClass: IrClassSymbol = val javaLangClass: IrClassSymbol =
context.getTopLevelClass(FqName("java.lang.Class")) context.getTopLevelClass(FqName("java.lang.Class"))