[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 {
.associate { primitivesWithImplicitCompanionObject()
it to symbolTable.lazyWrapper.referenceClass( .associate {
getClass(JS_INTERNAL_PACKAGE_FQNAME.child(Name.identifier("${it.identifier}CompanionObject"))) it to symbolTable.lazyWrapper.referenceClass(
) 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 {
getClass(FqName.fromSegments(listOf("kotlin", "reflect", "js", "internal", "PrimitiveClasses"))) symbolTable.referenceClass(
).owner getClass(FqName.fromSegments(listOf("kotlin", "reflect", "js", "internal", "PrimitiveClasses")))
).owner
}
val primitiveClassProperties = primitiveClassesObject.declarations.filterIsInstance<IrProperty>() val primitiveClassProperties by lazy { primitiveClassesObject.declarations.filterIsInstance<IrProperty>() }
val primitiveClassFunctionClass = primitiveClassesObject.declarations val primitiveClassFunctionClass by lazy {
.filterIsInstance<IrSimpleFunction>() primitiveClassesObject.declarations
.find { it.name == Name.identifier("functionClass") }!! .filterIsInstance<IrSimpleFunction>()
.find { it.name == Name.identifier("functionClass") }!!
}
val throwableClass = symbolTable.referenceClass( val throwableClass by lazy {
getClass(JsIrBackendContext.KOTLIN_PACKAGE_FQN.child(Name.identifier("Throwable"))) symbolTable.referenceClass(
).owner getClass(JsIrBackendContext.KOTLIN_PACKAGE_FQN.child(Name.identifier("Throwable")))
val throwableConstructors = throwableClass.declarations.filterIsInstance<IrConstructor>() ).owner
}
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,44 +23,47 @@ 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",
IrType::isInt to "intClass", IrType::isInt to "intClass",
IrType::isFloat to "floatClass", IrType::isFloat to "floatClass",
IrType::isDouble to "doubleClass", IrType::isDouble to "doubleClass",
IrType::isArray to "arrayClass", IrType::isArray to "arrayClass",
IrType::isString to "stringClass", IrType::isString to "stringClass",
IrType::isThrowable to "throwableClass", IrType::isThrowable to "throwableClass",
IrType::isBooleanArray to "booleanArrayClass", IrType::isBooleanArray to "booleanArrayClass",
IrType::isCharArray to "charArrayClass", IrType::isCharArray to "charArrayClass",
IrType::isByteArray to "byteArrayClass", IrType::isByteArray to "byteArrayClass",
IrType::isShortArray to "shortArrayClass", IrType::isShortArray to "shortArrayClass",
IrType::isIntArray to "intArrayClass", IrType::isIntArray to "intArrayClass",
IrType::isLongArray to "longArrayClass", IrType::isLongArray to "longArrayClass",
IrType::isFloatArray to "floatArrayClass", IrType::isFloatArray to "floatArrayClass",
IrType::isDoubleArray to "doubleArrayClass" IrType::isDoubleArray to "doubleArrayClass"
).mapValues { ).mapValues {
primitiveClassProperty(it.value).getter!! primitiveClassProperty(it.value).getter!!
}
} }
private val openPrimitiveClasses = mapOf( private val openPrimitiveClasses by lazy {
IrType::isAny to "anyClass", mapOf(
IrType::isNumber to "numberClass", IrType::isAny to "anyClass",
IrType::isNothing to "nothingClass" IrType::isNumber to "numberClass",
).mapValues { IrType::isNothing to "nothingClass"
primitiveClassProperty(it.value).getter!! ).mapValues {
primitiveClassProperty(it.value).getter!!
}
} }
private fun callGetKClassFromExpression(returnType: IrType, typeArgument: IrType, argument: IrExpression): IrExpression { private fun callGetKClassFromExpression(returnType: IrType, typeArgument: IrType, argument: IrExpression): IrExpression {