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()) {
// 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
override fun isComputed() = _delegate.isComputed()
override fun forceResolveAllContents() {
ForceResolveUtil.forceResolveAllContents(constructor)
arguments.forEach { ForceResolveUtil.forceResolveAllContents(it.type) }
}
val lazyKotlinType = LazyWrappedType(storageManager) {
doResolvePossiblyBareType(c, typeReference).getActualType()
}
val lazyKotlinType = LazyKotlinType()
c.trace.record(resolvedTypeSlice, typeReference, 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.getValue
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.SmartSet
import org.jetbrains.kotlin.utils.addToStdlib.check
@@ -60,7 +60,7 @@ open class JvmBuiltInsSettings(
private val ownerModuleDescriptor: ModuleDescriptor by lazy(deferredOwnerModuleDescriptor)
private val mockSerializableType = createMockJavaIoSerializableType()
private val mockSerializableType = storageManager.createMockJavaIoSerializableType()
private val javaAnalogueClassesWithCustomSupertypeCache = storageManager.createCacheWithNotNullValues<FqName, ClassDescriptor>()
@@ -71,15 +71,13 @@ open class JvmBuiltInsSettings(
).let { AnnotationsImpl(listOf(it)) }
}
private fun createMockJavaIoSerializableType(): KotlinType {
private fun StorageManager.createMockJavaIoSerializableType(): KotlinType {
val mockJavaIoPackageFragment = object : PackageFragmentDescriptorImpl(moduleDescriptor, FqName("java.io")) {
override fun getMemberScope() = MemberScope.Empty
}
//NOTE: can't reference anyType right away, because this is sometimes called when JvmBuiltIns are initializing
val superTypes = listOf(object : WrappedType() {
override val delegate: KotlinType get() = moduleDescriptor.builtIns.anyType
})
val superTypes = listOf(LazyWrappedType(this) { moduleDescriptor.builtIns.anyType })
val mockSerializableClass = ClassDescriptorImpl(
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.annotations.Annotations
import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.storage.LockBasedStorageManager
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.Variance.IN_VARIANCE
import org.jetbrains.kotlin.types.Variance.OUT_VARIANCE
@@ -119,9 +120,8 @@ private fun TypeProjection.createCapturedIfNeeded(typeParameterDescriptor: TypeP
if (typeParameterDescriptor.variance == projectionKind) {
// TODO: Make star projection type lazy
return if (isStarProjection)
TypeProjectionImpl(object : WrappedType() {
override val delegate: KotlinType
get() = this@createCapturedIfNeeded.type
TypeProjectionImpl(LazyWrappedType(LockBasedStorageManager.NO_LOCKS) {
this@createCapturedIfNeeded.type
})
else
TypeProjectionImpl(this@createCapturedIfNeeded.type)
@@ -18,6 +18,7 @@ package org.jetbrains.kotlin.types
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.storage.StorageManager
abstract class DelegatingSimpleType : SimpleType() {
protected abstract val delegate: SimpleType
@@ -47,3 +48,11 @@ fun SimpleType.withAbbreviation(abbreviatedType: SimpleType): SimpleType {
if (isError) return this
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()
}