Introduce LazyWrappedType.

This commit is contained in:
Stanislav Erokhin
2016-06-04 01:33:59 +03:00
parent 7a5469faa4
commit 0bec639b07
4 changed files with 18 additions and 20 deletions
@@ -112,19 +112,10 @@ class TypeResolver(
if (!c.allowBareTypes && !c.forceResolveLazyTypes && lazinessToken.isLazy()) { if (!c.allowBareTypes && !c.forceResolveLazyTypes && lazinessToken.isLazy()) {
// Bare types can be allowed only inside expressions; lazy type resolution is only relevant for declarations // Bare types can be allowed only inside expressions; lazy type resolution is only relevant for declarations
class LazyKotlinType : WrappedType(), LazyEntity {
private val _delegate = storageManager.createLazyValue { doResolvePossiblyBareType(c, typeReference).getActualType() }
override val delegate: KotlinType by _delegate val lazyKotlinType = LazyWrappedType(storageManager) {
override fun isComputed() = _delegate.isComputed() doResolvePossiblyBareType(c, typeReference).getActualType()
override fun forceResolveAllContents() {
ForceResolveUtil.forceResolveAllContents(constructor)
arguments.forEach { ForceResolveUtil.forceResolveAllContents(it.type) }
}
} }
val lazyKotlinType = LazyKotlinType()
c.trace.record(resolvedTypeSlice, typeReference, lazyKotlinType) c.trace.record(resolvedTypeSlice, typeReference, lazyKotlinType)
return type(lazyKotlinType) return type(lazyKotlinType)
} }
@@ -44,7 +44,7 @@ import org.jetbrains.kotlin.storage.LockBasedStorageManager
import org.jetbrains.kotlin.storage.StorageManager import org.jetbrains.kotlin.storage.StorageManager
import org.jetbrains.kotlin.storage.getValue import org.jetbrains.kotlin.storage.getValue
import org.jetbrains.kotlin.types.KotlinType import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.WrappedType import org.jetbrains.kotlin.types.LazyWrappedType
import org.jetbrains.kotlin.utils.DFS import org.jetbrains.kotlin.utils.DFS
import org.jetbrains.kotlin.utils.SmartSet import org.jetbrains.kotlin.utils.SmartSet
import org.jetbrains.kotlin.utils.addToStdlib.check import org.jetbrains.kotlin.utils.addToStdlib.check
@@ -60,7 +60,7 @@ open class JvmBuiltInsSettings(
private val ownerModuleDescriptor: ModuleDescriptor by lazy(deferredOwnerModuleDescriptor) private val ownerModuleDescriptor: ModuleDescriptor by lazy(deferredOwnerModuleDescriptor)
private val mockSerializableType = createMockJavaIoSerializableType() private val mockSerializableType = storageManager.createMockJavaIoSerializableType()
private val javaAnalogueClassesWithCustomSupertypeCache = storageManager.createCacheWithNotNullValues<FqName, ClassDescriptor>() private val javaAnalogueClassesWithCustomSupertypeCache = storageManager.createCacheWithNotNullValues<FqName, ClassDescriptor>()
@@ -71,15 +71,13 @@ open class JvmBuiltInsSettings(
).let { AnnotationsImpl(listOf(it)) } ).let { AnnotationsImpl(listOf(it)) }
} }
private fun createMockJavaIoSerializableType(): KotlinType { private fun StorageManager.createMockJavaIoSerializableType(): KotlinType {
val mockJavaIoPackageFragment = object : PackageFragmentDescriptorImpl(moduleDescriptor, FqName("java.io")) { val mockJavaIoPackageFragment = object : PackageFragmentDescriptorImpl(moduleDescriptor, FqName("java.io")) {
override fun getMemberScope() = MemberScope.Empty override fun getMemberScope() = MemberScope.Empty
} }
//NOTE: can't reference anyType right away, because this is sometimes called when JvmBuiltIns are initializing //NOTE: can't reference anyType right away, because this is sometimes called when JvmBuiltIns are initializing
val superTypes = listOf(object : WrappedType() { val superTypes = listOf(LazyWrappedType(this) { moduleDescriptor.builtIns.anyType })
override val delegate: KotlinType get() = moduleDescriptor.builtIns.anyType
})
val mockSerializableClass = ClassDescriptorImpl( val mockSerializableClass = ClassDescriptorImpl(
mockJavaIoPackageFragment, Name.identifier("Serializable"), Modality.ABSTRACT, ClassKind.INTERFACE, superTypes, SourceElement.NO_SOURCE mockJavaIoPackageFragment, Name.identifier("Serializable"), Modality.ABSTRACT, ClassKind.INTERFACE, superTypes, SourceElement.NO_SOURCE
@@ -20,6 +20,7 @@ import org.jetbrains.kotlin.builtins.KotlinBuiltIns
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.resolve.scopes.MemberScope import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.storage.LockBasedStorageManager
import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.Variance.IN_VARIANCE import org.jetbrains.kotlin.types.Variance.IN_VARIANCE
import org.jetbrains.kotlin.types.Variance.OUT_VARIANCE import org.jetbrains.kotlin.types.Variance.OUT_VARIANCE
@@ -119,9 +120,8 @@ private fun TypeProjection.createCapturedIfNeeded(typeParameterDescriptor: TypeP
if (typeParameterDescriptor.variance == projectionKind) { if (typeParameterDescriptor.variance == projectionKind) {
// TODO: Make star projection type lazy // TODO: Make star projection type lazy
return if (isStarProjection) return if (isStarProjection)
TypeProjectionImpl(object : WrappedType() { TypeProjectionImpl(LazyWrappedType(LockBasedStorageManager.NO_LOCKS) {
override val delegate: KotlinType this@createCapturedIfNeeded.type
get() = this@createCapturedIfNeeded.type
}) })
else else
TypeProjectionImpl(this@createCapturedIfNeeded.type) TypeProjectionImpl(this@createCapturedIfNeeded.type)
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.types
import org.jetbrains.kotlin.descriptors.annotations.Annotations import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.resolve.scopes.MemberScope import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.storage.StorageManager
abstract class DelegatingSimpleType : SimpleType() { abstract class DelegatingSimpleType : SimpleType() {
protected abstract val delegate: SimpleType protected abstract val delegate: SimpleType
@@ -47,3 +48,11 @@ fun SimpleType.withAbbreviation(abbreviatedType: SimpleType): SimpleType {
if (isError) return this if (isError) return this
return AbbreviatedType(this, abbreviatedType) return AbbreviatedType(this, abbreviatedType)
} }
class LazyWrappedType(storageManager: StorageManager, computation: () -> KotlinType): WrappedType() {
private val lazyValue = storageManager.createLazyValue(computation)
override val delegate: KotlinType get() = lazyValue()
override fun isComputed(): Boolean = lazyValue.isComputed()
}