Refactoring. Removed all usages of asSimpleType except related to TypeSubstitution.

This commit is contained in:
Stanislav Erokhin
2016-06-02 15:45:07 +03:00
parent dd362f683c
commit 797ef8d143
17 changed files with 93 additions and 65 deletions
@@ -34,7 +34,7 @@ object InternalFlexibleTypeTransformer : TypeTransformerForTests() {
val descriptor = kotlinType.constructor.declarationDescriptor
if (descriptor != null && FLEXIBLE_TYPE_CLASSIFIER.asSingleFqName().toUnsafe() == DescriptorUtils.getFqName(descriptor)
&& kotlinType.arguments.size == 2) {
return KotlinTypeFactory.flexibleType(kotlinType.arguments[0].type.asSimpleType(), kotlinType.arguments[1].type.asSimpleType())
return KotlinTypeFactory.flexibleType(kotlinType.arguments[0].type.unwrap() as SimpleType, kotlinType.arguments[1].type.unwrap() as SimpleType)
}
return null
}
@@ -86,7 +86,7 @@ public class SingleAbstractMethodUtils {
assert type != null : "Substitution based on type with no projections '" + noProjectionsSamType +
"' should not end with conflict";
SimpleType simpleType = KotlinTypeKt.asSimpleType(type);
SimpleType simpleType = TypeSubstitutionKt.asSimpleType(type);
return simpleType.makeNullableAsSpecified(samType.isMarkedNullable());
}
@@ -736,16 +736,17 @@ public class DescriptorResolver {
else {
typeAliasDescriptor.initialize(
typeParameterDescriptors,
DeferredType.create(storageManager, trace, new Function0<KotlinType>() {
storageManager.createLazyValue(new Function0<SimpleType>() {
@Override
public KotlinType invoke() {
public SimpleType invoke() {
return typeResolver.resolveAbbreviatedType(scopeWithTypeParameters, typeReference, trace, true);
}
}),
DeferredType.create(storageManager, trace, new Function0<KotlinType>() {
storageManager.createLazyValue(new Function0<SimpleType>() {
@Override
public KotlinType invoke() {
return typeResolver.resolveExpandedTypeForTypeAlias(typeAliasDescriptor);
public SimpleType invoke() {
// TODO do not reparse
return typeResolver.resolveAbbreviatedType(scopeWithTypeParameters, typeReference, trace, false);
}
}));
}
@@ -94,12 +94,12 @@ public class PossiblyBareType {
KotlinType nullableActualType = TypeUtils.makeNullable(getActualType());
KotlinType abbreviatedType = SpecialTypesKt.getAbbreviatedType(getActualType());
AbbreviatedType abbreviatedType = SpecialTypesKt.getAbbreviatedType(getActualType());
if (abbreviatedType == null) {
return type(nullableActualType);
}
else {
return type(SpecialTypesKt.withAbbreviatedType(KotlinTypeKt.asSimpleType(nullableActualType), KotlinTypeKt.asSimpleType(TypeUtils.makeNullable(abbreviatedType))));
return type(abbreviatedType.makeNullableAsSpecified(true));
}
}
@@ -71,8 +71,20 @@ class TypeResolver(
return resolveType(TypeResolutionContext(scope, trace, checkBounds, false, typeReference.suppressDiagnosticsInDebugMode(), false), typeReference)
}
fun resolveAbbreviatedType(scope: LexicalScope, typeReference: KtTypeReference, trace: BindingTrace, checkBounds: Boolean): KotlinType {
return resolveType(TypeResolutionContext(scope, trace, checkBounds, false, typeReference.suppressDiagnosticsInDebugMode(), true), typeReference)
fun resolveAbbreviatedType(scope: LexicalScope, typeReference: KtTypeReference, trace: BindingTrace, abbreviated: Boolean): SimpleType {
return resolveSimpleType(
TypeResolutionContext(scope, trace, true, false, typeReference.suppressDiagnosticsInDebugMode(), abbreviated),
typeReference
)
}
private fun resolveSimpleType(c: TypeResolutionContext, typeReference: KtTypeReference): SimpleType {
val unwrappedType = resolveType(c, typeReference).unwrap()
return when (unwrappedType) {
is DynamicType -> ErrorUtils.createErrorType("dynamic type in wrong context")
is SimpleType -> unwrappedType
else -> error("Unexpected type: $unwrappedType")
}
}
fun resolveExpandedTypeForTypeAlias(typeAliasDescriptor: TypeAliasDescriptor): KotlinType {
@@ -21,11 +21,12 @@ import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.descriptors.impl.AbstractTypeAliasDescriptor
import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.BindingTrace
import org.jetbrains.kotlin.storage.NotNullLazyValue
import org.jetbrains.kotlin.storage.StorageManager
import org.jetbrains.kotlin.types.DeferredType
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.SimpleType
import org.jetbrains.kotlin.types.TypeSubstitutor
import org.jetbrains.kotlin.types.Variance
import org.jetbrains.kotlin.types.asSimpleType
class LazyTypeAliasDescriptor(
private val storageManager: StorageManager,
@@ -38,33 +39,43 @@ class LazyTypeAliasDescriptor(
) : AbstractTypeAliasDescriptor(containingDeclaration, annotations, name, sourceElement, visibility),
TypeAliasDescriptor {
override lateinit var underlyingType: KotlinType private set
override lateinit var expandedType: KotlinType private set
private lateinit var underlyingTypeImpl: NotNullLazyValue<SimpleType>
private lateinit var expandedTypeImpl: NotNullLazyValue<SimpleType>
override val underlyingType: SimpleType get() = underlyingTypeImpl()
override val expandedType: SimpleType get() = expandedTypeImpl()
fun initialize(
declaredTypeParameters: List<TypeParameterDescriptor>,
lazyUnderlyingType: NotNullLazyValue<SimpleType>,
lazyExpandedType: NotNullLazyValue<SimpleType>
) {
super.initialize(declaredTypeParameters)
this.underlyingTypeImpl = lazyUnderlyingType
this.expandedTypeImpl = lazyExpandedType
}
private val lazyTypeConstructorParameters =
storageManager.createLazyValue { this.computeConstructorTypeParameters() }
fun initialize(
declaredTypeParameters: List<TypeParameterDescriptor>,
underlyingType: KotlinType,
expandedType: KotlinType
) {
super.initialize(declaredTypeParameters)
this.underlyingType = underlyingType
this.expandedType = expandedType
}
underlyingType: SimpleType,
expandedType: SimpleType
) = initialize(declaredTypeParameters, storageManager.createLazyValue { underlyingType }, storageManager.createLazyValue { expandedType })
override fun substitute(substitutor: TypeSubstitutor): TypeAliasDescriptor {
if (substitutor.isEmpty) return this
val substituted = LazyTypeAliasDescriptor(storageManager, trace,
containingDeclaration, annotations, name, source, visibility)
substituted.initialize(declaredTypeParameters,
DeferredType.create(storageManager, trace) {
substitutor.substitute(underlyingType, Variance.INVARIANT)
},
DeferredType.create(storageManager, trace) {
substitutor.substitute(expandedType, Variance.INVARIANT)
})
storageManager.createLazyValue {
substitutor.substitute(underlyingType, Variance.INVARIANT)!!.asSimpleType()
},
storageManager.createLazyValue {
substitutor.substitute(expandedType, Variance.INVARIANT)!!.asSimpleType()
}
)
return substituted
}
@@ -377,7 +377,7 @@ public class DescriptorSerializer {
builder.addTypeParameter(typeParameter(typeParameterDescriptor));
}
KotlinType underlyingType = descriptor.getUnderlyingType();
SimpleType underlyingType = descriptor.getUnderlyingType();
if (useTypeTable()) {
builder.setUnderlyingTypeId(typeId(underlyingType));
}
@@ -385,7 +385,7 @@ public class DescriptorSerializer {
builder.setUnderlyingType(type(underlyingType));
}
KotlinType expandedType = descriptor.getExpandedType();
SimpleType expandedType = descriptor.getExpandedType();
if (useTypeTable()) {
builder.setExpandedTypeId(typeId(expandedType));
}
@@ -548,13 +548,13 @@ public class DescriptorSerializer {
builder.setNullable(type.isMarkedNullable());
}
KotlinType abbreviatedType = SpecialTypesKt.getAbbreviatedType(type);
AbbreviatedType abbreviatedType = SpecialTypesKt.getAbbreviatedType(type);
if (abbreviatedType != null) {
if (useTypeTable()) {
builder.setAbbreviatedTypeId(typeId(abbreviatedType));
builder.setAbbreviatedTypeId(typeId(abbreviatedType.getAbbreviation()));
}
else {
builder.setAbbreviatedType(type(abbreviatedType));
builder.setAbbreviatedType(type(abbreviatedType.getAbbreviation()));
}
}