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()));
}
}
@@ -16,13 +16,13 @@
package org.jetbrains.kotlin.descriptors
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.SimpleType
import org.jetbrains.kotlin.types.TypeSubstitutor
interface TypeAliasDescriptor : ClassifierDescriptorWithTypeParameters, MemberDescriptor {
val underlyingType: KotlinType
val underlyingType: SimpleType
val expandedType: KotlinType
val expandedType: SimpleType
val classDescriptor: ClassDescriptor?
@@ -123,12 +123,12 @@ internal class DescriptorRendererImpl(
}
private fun renderNormalizedType(type: KotlinType): String {
val abbreviated = type.getAbbreviatedType()
val abbreviated = (type.unwrap() as? AbbreviatedType)
if (abbreviated != null) {
// TODO nullability is lost for abbreviated type?
val abbreviatedRendered = renderNormalizedTypeAsIs(abbreviated)
val unabbreviatedRendered = renderNormalizedTypeAsIs(type)
val abbreviatedRendered = renderNormalizedTypeAsIs(abbreviated.abbreviation)
val unabbreviatedRendered = renderNormalizedTypeAsIs(abbreviated.expandedType)
return "$abbreviatedRendered [= $unabbreviatedRendered]"
}
@@ -136,7 +136,3 @@ abstract class FlexibleType(val lowerBound: SimpleType, val upperBound: SimpleTy
override fun toString(): String = DescriptorRenderer.DEBUG_TEXT.renderType(this)
}
@Deprecated("Temporary marker method for refactoring")
fun KotlinType.asSimpleType(): SimpleType {
return unwrap() as SimpleType
}
@@ -29,19 +29,21 @@ abstract class DelegatingSimpleType : SimpleType() {
override val memberScope: MemberScope get() = delegate.memberScope
}
private class AbbreviatedType(override val delegate: SimpleType, val abbreviatedType: SimpleType) : DelegatingSimpleType() {
class AbbreviatedType(override val delegate: SimpleType, val abbreviation: SimpleType) : DelegatingSimpleType() {
val expandedType: SimpleType get() = delegate
override fun replaceAnnotations(newAnnotations: Annotations)
= AbbreviatedType(delegate.replaceAnnotations(newAnnotations), abbreviatedType)
= AbbreviatedType(delegate.replaceAnnotations(newAnnotations), abbreviation)
override fun makeNullableAsSpecified(newNullability: Boolean)
= AbbreviatedType(delegate.makeNullableAsSpecified(newNullability), abbreviatedType.makeNullableAsSpecified(newNullability))
= AbbreviatedType(delegate.makeNullableAsSpecified(newNullability), abbreviation.makeNullableAsSpecified(newNullability))
override val isError: Boolean get() = false
}
fun KotlinType.getAbbreviatedType(): SimpleType? = (unwrap() as? AbbreviatedType)?.abbreviatedType
fun KotlinType.getAbbreviatedType(): AbbreviatedType? = (unwrap() as? AbbreviatedType)
fun SimpleType.withAbbreviatedType(abbreviatedType: SimpleType): SimpleType {
fun SimpleType.withAbbreviation(abbreviatedType: SimpleType): SimpleType {
if (isError) return this
return AbbreviatedType(this, abbreviatedType)
}
@@ -173,3 +173,8 @@ open class DelegatedTypeSubstitution(val substitution: TypeSubstitution): TypeSu
override fun filterAnnotations(annotations: Annotations) = substitution.filterAnnotations(annotations)
}
// This method used for transform type to simple type afler substitution
fun KotlinType.asSimpleType(): SimpleType {
return unwrap() as? SimpleType ?: error("This is should be simple type: $this")
}
@@ -152,7 +152,7 @@ public class TypeSubstitutor {
"Unexpected substituted projection kind: " + substitutedProjectionKind + "; original: " + originalProjectionKind;
KotlinType substitutedFlexibleType = KotlinTypeFactory.flexibleType(
KotlinTypeKt.asSimpleType(substitutedLower.getType()), KotlinTypeKt.asSimpleType(substitutedUpper.getType()));
TypeSubstitutionKt.asSimpleType(substitutedLower.getType()), TypeSubstitutionKt.asSimpleType(substitutedUpper.getType()));
return new TypeProjectionImpl(substitutedProjectionKind, substitutedFlexibleType);
}
@@ -22,7 +22,7 @@ import org.jetbrains.kotlin.serialization.ProtoBuf
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedAnnotationsWithPossibleTargets
import org.jetbrains.kotlin.types.AbstractLazyType
import org.jetbrains.kotlin.types.SimpleType
import org.jetbrains.kotlin.types.withAbbreviatedType
import org.jetbrains.kotlin.types.withAbbreviation
import org.jetbrains.kotlin.utils.toReadOnlyList
class DeserializedType private constructor(
@@ -58,7 +58,7 @@ class DeserializedType private constructor(
val deserializedType = DeserializedType(c, typeProto, additionalAnnotations)
val abbreviatedTypeProto = typeProto.abbreviatedType(c.typeTable) ?: return deserializedType
return deserializedType.withAbbreviatedType(DeserializedType(c, abbreviatedTypeProto, additionalAnnotations))
return deserializedType.withAbbreviation(DeserializedType(c, abbreviatedTypeProto, additionalAnnotations))
}
}
}
@@ -180,8 +180,8 @@ class MemberDeserializer(private val c: DeserializationContext) {
val local = c.childContext(typeAlias, proto.typeParameterList)
typeAlias.initialize(
local.typeDeserializer.ownTypeParameters,
local.typeDeserializer.type(proto.underlyingType(c.typeTable)),
local.typeDeserializer.type(proto.expandedType(c.typeTable))
local.typeDeserializer.simpleType(proto.underlyingType(c.typeTable)),
local.typeDeserializer.simpleType(proto.expandedType(c.typeTable))
)
return typeAlias
@@ -58,14 +58,17 @@ class TypeDeserializer(
fun type(proto: ProtoBuf.Type, additionalAnnotations: Annotations = Annotations.EMPTY): KotlinType {
if (proto.hasFlexibleTypeCapabilitiesId()) {
val id = c.nameResolver.getString(proto.flexibleTypeCapabilitiesId)
val lowerBound = DeserializedType.create(c, proto, additionalAnnotations)
val upperBound = DeserializedType.create(c, proto.flexibleUpperBound(c.typeTable)!!, additionalAnnotations)
val lowerBound = simpleType(proto, additionalAnnotations)
val upperBound = simpleType(proto.flexibleUpperBound(c.typeTable)!!, additionalAnnotations)
return c.components.flexibleTypeDeserializer.create(proto, id, lowerBound, upperBound)
}
return DeserializedType.create(c, proto, additionalAnnotations)
return simpleType(proto, additionalAnnotations)
}
fun simpleType(proto: ProtoBuf.Type, additionalAnnotations: Annotations = Annotations.EMPTY)
= DeserializedType.create(c, proto, additionalAnnotations)
fun typeConstructor(proto: ProtoBuf.Type): TypeConstructor =
when {
proto.hasClassName() -> {
@@ -24,9 +24,7 @@ import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.serialization.ProtoBuf
import org.jetbrains.kotlin.serialization.deserialization.NameResolver
import org.jetbrains.kotlin.serialization.deserialization.TypeTable
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.TypeSubstitutor
import org.jetbrains.kotlin.types.Variance
import org.jetbrains.kotlin.types.*
interface DeserializedMemberDescriptor : MemberDescriptor {
val proto: MessageLite
@@ -154,14 +152,14 @@ class DeserializedTypeAliasDescriptor(
) : AbstractTypeAliasDescriptor(containingDeclaration, annotations, name, SourceElement.NO_SOURCE, visibility),
DeserializedMemberDescriptor {
override lateinit var underlyingType: KotlinType private set
override lateinit var expandedType: KotlinType private set
override lateinit var underlyingType: SimpleType private set
override lateinit var expandedType: SimpleType private set
private lateinit var typeConstructorParameters: List<TypeParameterDescriptor>
fun initialize(
declaredTypeParameters: List<TypeParameterDescriptor>,
underlyingType: KotlinType,
expandedType: KotlinType
underlyingType: SimpleType,
expandedType: SimpleType
) {
initialize(declaredTypeParameters)
this.underlyingType = underlyingType
@@ -173,8 +171,8 @@ class DeserializedTypeAliasDescriptor(
if (substitutor.isEmpty) return this
val substituted = DeserializedTypeAliasDescriptor(containingDeclaration, annotations, name, visibility, proto, nameResolver, typeTable, containerSource)
substituted.initialize(declaredTypeParameters,
substitutor.safeSubstitute(underlyingType, Variance.INVARIANT),
substitutor.safeSubstitute(expandedType, Variance.INVARIANT))
substitutor.safeSubstitute(underlyingType, Variance.INVARIANT).asSimpleType(),
substitutor.safeSubstitute(expandedType, Variance.INVARIANT).asSimpleType())
return substituted
}