Make class descriptors in JvmRuntimeTypes lazy
The main reason currently is to avoid eager construction of defaultContinuationSupertype long before it's needed, which could fail without built-ins in the classpath
This commit is contained in:
@@ -33,42 +33,36 @@ import org.jetbrains.kotlin.types.TypeConstructorSubstitution
|
|||||||
import org.jetbrains.kotlin.types.TypeProjectionImpl
|
import org.jetbrains.kotlin.types.TypeProjectionImpl
|
||||||
import org.jetbrains.kotlin.types.Variance
|
import org.jetbrains.kotlin.types.Variance
|
||||||
import org.jetbrains.kotlin.types.expressions.ExpressionTypingUtils
|
import org.jetbrains.kotlin.types.expressions.ExpressionTypingUtils
|
||||||
import java.util.*
|
|
||||||
|
|
||||||
class JvmRuntimeTypes(module: ModuleDescriptor) {
|
class JvmRuntimeTypes(module: ModuleDescriptor) {
|
||||||
private val lambda: ClassDescriptor
|
private val kotlinJvmInternalPackage = MutablePackageFragmentDescriptor(module, FqName("kotlin.jvm.internal"))
|
||||||
private val functionReference: ClassDescriptor
|
|
||||||
private val propertyReferences: MutableList<ClassDescriptor>
|
|
||||||
private val mutablePropertyReferences: MutableList<ClassDescriptor>
|
|
||||||
private val localVariableReference: ClassDescriptor
|
|
||||||
private val mutableLocalVariableReference: ClassDescriptor
|
|
||||||
private val defaultContinuationSupertype: KotlinType
|
|
||||||
|
|
||||||
init {
|
private fun klass(name: String) = lazy { createClass(kotlinJvmInternalPackage, name) }
|
||||||
val kotlinJvmInternal = MutablePackageFragmentDescriptor(module, FqName("kotlin.jvm.internal"))
|
|
||||||
|
|
||||||
this.lambda = createClass(kotlinJvmInternal, "Lambda")
|
private val lambda: ClassDescriptor by klass("Lambda")
|
||||||
this.functionReference = createClass(kotlinJvmInternal, "FunctionReference")
|
private val functionReference: ClassDescriptor by klass("FunctionReference")
|
||||||
this.localVariableReference = createClass(kotlinJvmInternal, "LocalVariableReference")
|
private val localVariableReference: ClassDescriptor by klass("LocalVariableReference")
|
||||||
this.mutableLocalVariableReference = createClass(kotlinJvmInternal, "MutableLocalVariableReference")
|
private val mutableLocalVariableReference: ClassDescriptor by klass("MutableLocalVariableReference")
|
||||||
this.propertyReferences = ArrayList<ClassDescriptor>(3)
|
|
||||||
this.mutablePropertyReferences = ArrayList<ClassDescriptor>(3)
|
|
||||||
|
|
||||||
for (i in 0..2) {
|
private val propertyReferences: List<ClassDescriptor> by lazy {
|
||||||
propertyReferences.add(createClass(kotlinJvmInternal, "PropertyReference$i"))
|
(0..2).map { i -> createClass(kotlinJvmInternalPackage, "PropertyReference$i") }
|
||||||
mutablePropertyReferences.add(createClass(kotlinJvmInternal, "MutablePropertyReference$i"))
|
|
||||||
}
|
|
||||||
|
|
||||||
defaultContinuationSupertype = createNullableAnyContinuation(module)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private val mutablePropertyReferences: List<ClassDescriptor> by lazy {
|
||||||
|
(0..2).map { i -> createClass(kotlinJvmInternalPackage, "MutablePropertyReference$i") }
|
||||||
|
}
|
||||||
|
|
||||||
|
private val defaultContinuationSupertype: KotlinType by lazy { createNullableAnyContinuation(module) }
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return `Continuation<Any?>` type
|
* @return `Continuation<Any?>` type
|
||||||
*/
|
*/
|
||||||
private fun createNullableAnyContinuation(module: ModuleDescriptor): KotlinType {
|
private fun createNullableAnyContinuation(module: ModuleDescriptor): KotlinType {
|
||||||
|
// TODO: create a synthesized class descriptor here instead of looking at the classpath, or report a proper diagnostic
|
||||||
|
// This code will throw when compiling with "-no-stdlib" because no built-ins would be found in the classpath either
|
||||||
val classDescriptor = module.resolveTopLevelClass(
|
val classDescriptor = module.resolveTopLevelClass(
|
||||||
DescriptorUtils.CONTINUATION_INTERFACE_FQ_NAME, NoLookupLocation.FROM_BACKEND
|
DescriptorUtils.CONTINUATION_INTERFACE_FQ_NAME, NoLookupLocation.FROM_BACKEND
|
||||||
) ?: error("${DescriptorUtils.CONTINUATION_INTERFACE_FQ_NAME} was not found in built-ins")
|
) ?: throw AssertionError("${DescriptorUtils.CONTINUATION_INTERFACE_FQ_NAME} was not found in built-ins")
|
||||||
|
|
||||||
return TypeConstructorSubstitution.createByParametersMap(
|
return TypeConstructorSubstitution.createByParametersMap(
|
||||||
mapOf(classDescriptor.declaredTypeParameters.single() to TypeProjectionImpl(module.builtIns.nullableAnyType))
|
mapOf(classDescriptor.declaredTypeParameters.single() to TypeProjectionImpl(module.builtIns.nullableAnyType))
|
||||||
|
|||||||
Reference in New Issue
Block a user