Cleanup Symbols and JvmIr.JvmSymbols

This commit is contained in:
Alexander Udalov
2019-03-12 15:29:09 +01:00
parent e9c2bc81aa
commit 4ab1916ebf
3 changed files with 22 additions and 88 deletions
@@ -39,6 +39,8 @@ abstract class Ir<out T : CommonBackendContext>(val context: T, val irModule: Ir
open fun shouldGenerateHandlerParameterForDefaultBodyFun() = false
}
// Some symbols below are used in kotlin-native, so they can't be private
@Suppress("MemberVisibilityCanBePrivate", "PropertyName")
abstract class Symbols<out T : CommonBackendContext>(val context: T, private val symbolTable: ReferenceSymbolTable) {
protected val builtIns
@@ -48,24 +50,12 @@ abstract class Symbols<out T : CommonBackendContext>(val context: T, private val
context.builtIns.builtInsModule.getPackage(FqName.fromSegments(listOf(*packageNameSegments))).memberScope
// This hack allows to disable related symbol instantiation in descendants
// TODO move relevant symbols to non-common code
open fun calc(initializer: () -> IrClassSymbol): IrClassSymbol {
return initializer()
}
/**
* Use this table to reference external dependencies.
*/
open val externalSymbolTable: ReferenceSymbolTable
get() = symbolTable
// val refClass = calc { symbolTable.referenceClass(context.getInternalClass("Ref")) }
//abstract val areEqualByValue: List<IrFunctionSymbol>
abstract val areEqual: IrFunctionSymbol
abstract val ThrowNullPointerException: IrFunctionSymbol
abstract val ThrowNoWhenBranchMatchedException: IrFunctionSymbol
abstract val ThrowTypeCastException: IrFunctionSymbol
@@ -96,11 +86,6 @@ abstract class Symbols<out T : CommonBackendContext>(val context: T, private val
val progressionClasses = listOf(charProgression, intProgression, longProgression)
val progressionClassesTypes = progressionClasses.map { it.descriptor.defaultType }.toSet()
// val checkProgressionStep = context.getInternalFunctions("checkProgressionStep")
// .map { Pair(it.returnType, symbolTable.referenceSimpleFunction(it)) }.toMap()
// val getProgressionLast = context.getInternalFunctions("getProgressionLast")
// .map { Pair(it.returnType, symbolTable.referenceSimpleFunction(it)) }.toMap()
val defaultConstructorMarker = symbolTable.referenceClass(context.getInternalClass("DefaultConstructorMarker"))
val any = symbolTable.referenceClass(builtIns.any)
@@ -158,39 +143,18 @@ abstract class Symbols<out T : CommonBackendContext>(val context: T, private val
.firstOrNull {
it.valueParameters.isEmpty()
&& (it.extensionReceiverParameter?.type?.constructor?.declarationDescriptor as? ClassDescriptor)?.defaultType == type
}
?: throw Error(type.toString())
} ?: throw Error(type.toString())
return symbolTable.referenceSimpleFunction(descriptor)
}
abstract val copyRangeTo: Map<ClassDescriptor, IrSimpleFunctionSymbol>
// val valuesForEnum = symbolTable.referenceSimpleFunction(
// context.getInternalFunctions("valuesForEnum").single())
//
// val valueOfForEnum = symbolTable.referenceSimpleFunction(
// context.getInternalFunctions("valueOfForEnum").single())
// val getContinuation = symbolTable.referenceSimpleFunction(
// context.getInternalFunctions("getContinuation").single())
abstract val coroutineImpl: IrClassSymbol
abstract val coroutineSuspendedGetter: IrSimpleFunctionSymbol
fun getFunction(parameterCount: Int) = symbolTable.referenceClass(context.builtIns.getFunction(parameterCount))
val functionReference = calc { symbolTable.referenceClass(context.getInternalClass("FunctionReference")) }
fun getFunction(name: Name, receiverType: KotlinType, vararg argTypes: KotlinType) =
symbolTable.referenceFunction(receiverType.memberScope.getContributedFunctions(name, NoLookupLocation.FROM_BACKEND)
.first {
var i = 0
it.valueParameters.size == argTypes.size && it.valueParameters.all { type -> type == argTypes[i++] }
}
)
private val binaryOperatorCache = mutableMapOf<Triple<Name, KotlinType, KotlinType>, IrFunctionSymbol>()
fun getBinaryOperator(name: Name, lhsType: KotlinType, rhsType: KotlinType): IrFunctionSymbol {
val key = Triple(name, lhsType, rhsType)
var result = binaryOperatorCache[key]
@@ -204,6 +168,7 @@ abstract class Symbols<out T : CommonBackendContext>(val context: T, private val
}
private val unaryOperatorCache = mutableMapOf<Pair<Name, KotlinType>, IrFunctionSymbol>()
fun getUnaryOperator(name: Name, receiverType: KotlinType): IrFunctionSymbol {
val key = name to receiverType
var result = unaryOperatorCache[key]
@@ -28,7 +28,6 @@ import org.jetbrains.kotlin.ir.backend.js.utils.OperatorNames
import org.jetbrains.kotlin.ir.declarations.*
import org.jetbrains.kotlin.ir.declarations.impl.IrFileImpl
import org.jetbrains.kotlin.ir.descriptors.IrBuiltIns
import org.jetbrains.kotlin.ir.symbols.IrClassSymbol
import org.jetbrains.kotlin.ir.symbols.IrEnumEntrySymbol
import org.jetbrains.kotlin.ir.symbols.IrFunctionSymbol
import org.jetbrains.kotlin.ir.symbols.IrSimpleFunctionSymbol
@@ -147,20 +146,6 @@ class JsIrBackendContext(
override val ir = object : Ir<CommonBackendContext>(this, irModuleFragment) {
override val symbols = object : Symbols<CommonBackendContext>(this@JsIrBackendContext, symbolTable.lazyWrapper) {
override fun calc(initializer: () -> IrClassSymbol): IrClassSymbol {
val v = lazy { initializer() }
return object : IrClassSymbol {
override val owner: IrClass get() = v.value.owner
override val isBound: Boolean get() = v.value.isBound
override fun bind(owner: IrClass) = v.value.bind(owner)
override val descriptor: ClassDescriptor get() = v.value.descriptor
}
}
override val areEqual
get () = TODO("not implemented")
override val ThrowNullPointerException =
symbolTable.referenceSimpleFunction(getFunctions(kotlinPackageFqn.child(Name.identifier("THROW_NPE"))).single())
@@ -11,12 +11,10 @@ import org.jetbrains.kotlin.backend.common.ir.Symbols
import org.jetbrains.kotlin.backend.common.phaser.PhaseConfig
import org.jetbrains.kotlin.backend.jvm.descriptors.JvmDeclarationFactory
import org.jetbrains.kotlin.backend.jvm.descriptors.JvmSharedVariablesManager
import org.jetbrains.kotlin.builtins.ReflectionTypes
import org.jetbrains.kotlin.codegen.state.GenerationState
import org.jetbrains.kotlin.config.CommonConfigurationKeys
import org.jetbrains.kotlin.descriptors.ClassDescriptor
import org.jetbrains.kotlin.descriptors.FunctionDescriptor
import org.jetbrains.kotlin.descriptors.NotFoundClasses
import org.jetbrains.kotlin.incremental.components.NoLookupLocation
import org.jetbrains.kotlin.ir.IrElement
import org.jetbrains.kotlin.ir.declarations.IrFile
@@ -29,7 +27,6 @@ import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.psi2ir.PsiSourceManager
import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.storage.LockBasedStorageManager
class JvmBackendContext(
val state: GenerationState,
@@ -41,9 +38,6 @@ class JvmBackendContext(
override val declarationFactory: JvmDeclarationFactory = JvmDeclarationFactory(state)
override val sharedVariablesManager = JvmSharedVariablesManager(state.module, builtIns, irBuiltIns)
// TODO: inject a correct StorageManager instance, or store NotFoundClasses inside ModuleDescriptor
internal val reflectionTypes = ReflectionTypes(state.module, NotFoundClasses(LockBasedStorageManager.NO_LOCKS, state.module))
override val ir = JvmIr(irModuleFragment, symbolTable)
val phaseConfig = PhaseConfig(jvmPhases, state.configuration)
@@ -107,34 +101,20 @@ class JvmBackendContext(
override val symbols = JvmSymbols()
inner class JvmSymbols : Symbols<JvmBackendContext>(this@JvmBackendContext, symbolTable.lazyWrapper) {
override val ThrowNullPointerException: IrSimpleFunctionSymbol
get() = symbolTable.referenceSimpleFunction(context.getInternalFunctions("ThrowNullPointerException").single())
override val areEqual
get () = symbolTable.referenceSimpleFunction(context.getInternalFunctions("areEqual").single())
override val ThrowNoWhenBranchMatchedException: IrSimpleFunctionSymbol
get() = symbolTable.referenceSimpleFunction(context.getInternalFunctions("ThrowNoWhenBranchMatchedException").single())
override val ThrowNullPointerException
get () = symbolTable.referenceSimpleFunction(
context.getInternalFunctions("ThrowNullPointerException").single()
)
override val ThrowTypeCastException: IrSimpleFunctionSymbol
get() = symbolTable.referenceSimpleFunction(context.getInternalFunctions("ThrowTypeCastException").single())
override val ThrowNoWhenBranchMatchedException
get () = symbolTable.referenceSimpleFunction(
context.getInternalFunctions("ThrowNoWhenBranchMatchedException").single()
)
override val ThrowUninitializedPropertyAccessException: IrSimpleFunctionSymbol =
symbolTable.referenceSimpleFunction(context.getInternalFunctions("ThrowUninitializedPropertyAccessException").single())
override val ThrowTypeCastException
get () = symbolTable.referenceSimpleFunction(
context.getInternalFunctions("ThrowTypeCastException").single()
)
override val ThrowUninitializedPropertyAccessException =
symbolTable.referenceSimpleFunction(
context.getInternalFunctions("ThrowUninitializedPropertyAccessException").single()
)
override val stringBuilder
get() = symbolTable.referenceClass(
context.getClass(FqName("java.lang.StringBuilder"))
)
override val stringBuilder: IrClassSymbol
get() = symbolTable.referenceClass(context.getClass(FqName("java.lang.StringBuilder")))
override val copyRangeTo: Map<ClassDescriptor, IrSimpleFunctionSymbol>
get() = TODO("not implemented") //To change initializer of created properties use File | Settings | File Templates.
@@ -143,12 +123,16 @@ class JvmBackendContext(
override val coroutineSuspendedGetter: IrSimpleFunctionSymbol
get() = TODO("not implemented") //To change initializer of created properties use File | Settings | File Templates.
val lambdaClass = calc { symbolTable.referenceClass(context.getInternalClass("Lambda")) }
val lambdaClass: IrClassSymbol =
symbolTable.referenceClass(context.getInternalClass("Lambda"))
fun getKFunction(parameterCount: Int) = symbolTable.referenceClass(reflectionTypes.getKFunction(parameterCount))
val functionReference: IrClassSymbol =
symbolTable.referenceClass(context.getInternalClass("FunctionReference"))
fun getFunction(parameterCount: Int): IrClassSymbol =
symbolTable.referenceClass(context.builtIns.getFunction(parameterCount))
}
override fun shouldGenerateHandlerParameterForDefaultBodyFun() = true
}
}