[JS IR BE] Lazy initialize lowering properties

This commit is contained in:
Roman Artemev
2019-02-13 19:17:21 +03:00
committed by romanart
parent c95d99dbe0
commit b2cfef4902
3 changed files with 62 additions and 51 deletions
@@ -132,5 +132,5 @@ class LateinitLowering(val context: CommonBackendContext) : FileLoweringPass {
) )
} }
private val throwErrorFunction = context.ir.symbols.ThrowUninitializedPropertyAccessException.owner private val throwErrorFunction by lazy { context.ir.symbols.ThrowUninitializedPropertyAccessException.owner }
} }
@@ -168,12 +168,14 @@ class JsIrBackendContext(
return numbers + listOf(Name.identifier("String")) return numbers + listOf(Name.identifier("String"))
} }
val primitiveCompanionObjects = primitivesWithImplicitCompanionObject() val primitiveCompanionObjects by lazy {
primitivesWithImplicitCompanionObject()
.associate { .associate {
it to symbolTable.lazyWrapper.referenceClass( it to symbolTable.lazyWrapper.referenceClass(
getClass(JS_INTERNAL_PACKAGE_FQNAME.child(Name.identifier("${it.identifier}CompanionObject"))) getClass(JS_INTERNAL_PACKAGE_FQNAME.child(Name.identifier("${it.identifier}CompanionObject")))
) )
} }
}
val suspendFunctions = (0..22).map { symbolTable.referenceClass(builtIns.getSuspendFunction(it)) } val suspendFunctions = (0..22).map { symbolTable.referenceClass(builtIns.getSuspendFunction(it)) }
@@ -237,29 +239,35 @@ class JsIrBackendContext(
override fun shouldGenerateHandlerParameterForDefaultBodyFun() = true override fun shouldGenerateHandlerParameterForDefaultBodyFun() = true
} }
val throwISEymbol = getFunctions(kotlinPackageFqn.child(Name.identifier("THROW_CCE"))).singleOrNull()?.let { val throwISEymbol by lazy { getFunctions(kotlinPackageFqn.child(Name.identifier("THROW_CCE"))).singleOrNull()?.let {
symbolTable.referenceSimpleFunction(it) } ?: irBuiltIns.throwIseSymbol symbolTable.referenceSimpleFunction(it) } ?: irBuiltIns.throwIseSymbol }
val coroutineImplLabelProperty by lazy { ir.symbols.coroutineImpl.getPropertyDeclaration("state")!! } val coroutineImplLabelProperty by lazy { ir.symbols.coroutineImpl.getPropertyDeclaration("state")!! }
val coroutineImplResultSymbol by lazy { ir.symbols.coroutineImpl.getPropertyDeclaration("result")!! } val coroutineImplResultSymbol by lazy { ir.symbols.coroutineImpl.getPropertyDeclaration("result")!! }
val coroutineImplExceptionProperty by lazy { ir.symbols.coroutineImpl.getPropertyDeclaration("exception")!! } val coroutineImplExceptionProperty by lazy { ir.symbols.coroutineImpl.getPropertyDeclaration("exception")!! }
val coroutineImplExceptionStateProperty by lazy { ir.symbols.coroutineImpl.getPropertyDeclaration("exceptionState")!! } val coroutineImplExceptionStateProperty by lazy { ir.symbols.coroutineImpl.getPropertyDeclaration("exceptionState")!! }
val primitiveClassesObject = symbolTable.referenceClass( val primitiveClassesObject by lazy {
symbolTable.referenceClass(
getClass(FqName.fromSegments(listOf("kotlin", "reflect", "js", "internal", "PrimitiveClasses"))) getClass(FqName.fromSegments(listOf("kotlin", "reflect", "js", "internal", "PrimitiveClasses")))
).owner ).owner
}
val primitiveClassProperties = primitiveClassesObject.declarations.filterIsInstance<IrProperty>() val primitiveClassProperties by lazy { primitiveClassesObject.declarations.filterIsInstance<IrProperty>() }
val primitiveClassFunctionClass = primitiveClassesObject.declarations val primitiveClassFunctionClass by lazy {
primitiveClassesObject.declarations
.filterIsInstance<IrSimpleFunction>() .filterIsInstance<IrSimpleFunction>()
.find { it.name == Name.identifier("functionClass") }!! .find { it.name == Name.identifier("functionClass") }!!
}
val throwableClass = symbolTable.referenceClass( val throwableClass by lazy {
symbolTable.referenceClass(
getClass(JsIrBackendContext.KOTLIN_PACKAGE_FQN.child(Name.identifier("Throwable"))) getClass(JsIrBackendContext.KOTLIN_PACKAGE_FQN.child(Name.identifier("Throwable")))
).owner ).owner
val throwableConstructors = throwableClass.declarations.filterIsInstance<IrConstructor>() }
val throwableConstructors by lazy { throwableClass.declarations.filterIsInstance<IrConstructor>() }
val defaultThrowableCtor = throwableConstructors.single { it.valueParameters.size == 0 } val defaultThrowableCtor by lazy { throwableConstructors.single { it.valueParameters.size == 0 } }
private fun referenceOperators() = OperatorNames.ALL.map { name -> private fun referenceOperators() = OperatorNames.ALL.map { name ->
// TODO to replace KotlinType with IrType we need right equals on IrType // TODO to replace KotlinType with IrType we need right equals on IrType
@@ -23,17 +23,17 @@ import org.jetbrains.kotlin.ir.visitors.transformChildrenVoid
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
class ClassReferenceLowering(val context: JsIrBackendContext) : FileLoweringPass { class ClassReferenceLowering(val context: JsIrBackendContext) : FileLoweringPass {
private val intrinsics = context.intrinsics private val intrinsics by lazy { context.intrinsics }
private val primitiveClassesObject = context.primitiveClassesObject private val primitiveClassesObject by lazy { context.primitiveClassesObject }
private val primitiveClassProperties = context.primitiveClassProperties private val primitiveClassProperties by lazy { context.primitiveClassProperties }
private fun primitiveClassProperty(name: String) = private fun primitiveClassProperty(name: String) =
primitiveClassProperties.single { it.name == Name.identifier(name) } primitiveClassProperties.single { it.name == Name.identifier(name) }
private val finalPrimitiveClasses by lazy {
private val finalPrimitiveClasses = mapOf( mapOf(
IrType::isBoolean to "booleanClass", IrType::isBoolean to "booleanClass",
IrType::isByte to "byteClass", IrType::isByte to "byteClass",
IrType::isShort to "shortClass", IrType::isShort to "shortClass",
@@ -54,14 +54,17 @@ class ClassReferenceLowering(val context: JsIrBackendContext) : FileLoweringPass
).mapValues { ).mapValues {
primitiveClassProperty(it.value).getter!! primitiveClassProperty(it.value).getter!!
} }
}
private val openPrimitiveClasses = mapOf( private val openPrimitiveClasses by lazy {
mapOf(
IrType::isAny to "anyClass", IrType::isAny to "anyClass",
IrType::isNumber to "numberClass", IrType::isNumber to "numberClass",
IrType::isNothing to "nothingClass" IrType::isNothing to "nothingClass"
).mapValues { ).mapValues {
primitiveClassProperty(it.value).getter!! primitiveClassProperty(it.value).getter!!
} }
}
private fun callGetKClassFromExpression(returnType: IrType, typeArgument: IrType, argument: IrExpression): IrExpression { private fun callGetKClassFromExpression(returnType: IrType, typeArgument: IrType, argument: IrExpression): IrExpression {
val primitiveKClass = getFinalPrimitiveKClass(returnType, typeArgument) val primitiveKClass = getFinalPrimitiveKClass(returnType, typeArgument)