Refactoring. Do not use SubtypingRepresentatives as TypeCapability.

This commit is contained in:
Stanislav Erokhin
2016-05-25 18:57:05 +03:00
parent 1eaefa7fed
commit 04e97f5058
6 changed files with 37 additions and 43 deletions
@@ -19,6 +19,7 @@ package org.jetbrains.kotlin.resolve.calls.inference
import org.jetbrains.kotlin.builtins.KotlinBuiltIns 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.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
@@ -57,27 +58,19 @@ class CapturedTypeConstructor(
} }
class CapturedType( class CapturedType(
private val typeProjection: TypeProjection private val typeProjection: TypeProjection,
): DelegatingType(), SubtypingRepresentatives { override val constructor: TypeConstructor = CapturedTypeConstructor(typeProjection),
override val isMarkedNullable: Boolean = false,
override val annotations: Annotations = Annotations.EMPTY
): AbstractKotlinType(), SubtypingRepresentatives, TypeWithCustomReplacement {
private val delegateType = run { override val arguments: List<TypeProjection>
val scope = ErrorUtils.createErrorScope( get() = listOf()
override val memberScope: MemberScope
get() = ErrorUtils.createErrorScope(
"No member resolution should be done on captured type, it used only during constraint system resolution", true) "No member resolution should be done on captured type, it used only during constraint system resolution", true)
KotlinTypeFactory.simpleType(Annotations.EMPTY, CapturedTypeConstructor(typeProjection), listOf(), false, scope) override val isError: Boolean
} get() = false
override fun getDelegate(): KotlinType = delegateType
override val capabilities: TypeCapabilities get() = object : TypeCapabilities {
override fun <T : TypeCapability> getCapability(capabilityClass: Class<T>) =
this@CapturedType.getCapability(capabilityClass)
}
override fun <T : TypeCapability> getCapability(capabilityClass: Class<T>): T? {
@Suppress("UNCHECKED_CAST")
return if (capabilityClass == SubtypingRepresentatives::class.java) this as T
else super.getCapability(capabilityClass)
}
override val subTypeRepresentative: KotlinType override val subTypeRepresentative: KotlinType
get() = representative(OUT_VARIANCE, builtIns.nullableAnyType) get() = representative(OUT_VARIANCE, builtIns.nullableAnyType)
@@ -88,9 +81,16 @@ class CapturedType(
private fun representative(variance: Variance, default: KotlinType) = private fun representative(variance: Variance, default: KotlinType) =
if (typeProjection.projectionKind == variance) typeProjection.type else default if (typeProjection.projectionKind == variance) typeProjection.type else default
override fun sameTypeConstructor(type: KotlinType) = delegateType.constructor === type.constructor override fun sameTypeConstructor(type: KotlinType) = constructor === type.constructor
override fun toString() = "Captured($typeProjection)" override fun toString() = "Captured($typeProjection)${if (isMarkedNullable) '?' else ""}"
override fun makeNullableAsSpecified(nullable: Boolean): KotlinType {
if (nullable == isMarkedNullable) return this
return CapturedType(typeProjection, constructor, nullable, annotations)
}
override fun replaceAnnotations(newAnnotations: Annotations): KotlinType = CapturedType(typeProjection, constructor, isMarkedNullable, newAnnotations)
} }
fun createCapturedType(typeProjection: TypeProjection): KotlinType fun createCapturedType(typeProjection: TypeProjection): KotlinType
@@ -56,6 +56,12 @@ fun KotlinType.unwrap(): KotlinType {
interface SimpleType : KotlinType interface SimpleType : KotlinType
interface TypeWithCustomReplacement : KotlinType {
fun makeNullableAsSpecified(nullable: Boolean): KotlinType
fun replaceAnnotations(newAnnotations: Annotations): KotlinType
}
abstract class WrappedType() : KotlinType, LazyType { abstract class WrappedType() : KotlinType, LazyType {
override val annotations: Annotations get() = delegate.annotations override val annotations: Annotations get() = delegate.annotations
override val constructor: TypeConstructor get() = delegate.constructor override val constructor: TypeConstructor get() = delegate.constructor
@@ -74,7 +74,7 @@ fun KotlinType.getCustomTypeVariable(): CustomTypeVariable? =
if (it.isTypeVariable) it else null if (it.isTypeVariable) it else null
} }
interface SubtypingRepresentatives : TypeCapability { interface SubtypingRepresentatives {
val subTypeRepresentative: KotlinType val subTypeRepresentative: KotlinType
val superTypeRepresentative: KotlinType val superTypeRepresentative: KotlinType
@@ -82,15 +82,14 @@ interface SubtypingRepresentatives : TypeCapability {
} }
fun KotlinType.getSubtypeRepresentative(): KotlinType = fun KotlinType.getSubtypeRepresentative(): KotlinType =
this.getCapability(SubtypingRepresentatives::class.java)?.subTypeRepresentative ?: this (unwrap() as? SubtypingRepresentatives)?.subTypeRepresentative ?: this
fun KotlinType.getSupertypeRepresentative(): KotlinType = fun KotlinType.getSupertypeRepresentative(): KotlinType =
this.getCapability(SubtypingRepresentatives::class.java)?.superTypeRepresentative ?: this (unwrap() as? SubtypingRepresentatives)?.superTypeRepresentative ?: this
fun sameTypeConstructors(first: KotlinType, second: KotlinType): Boolean { fun sameTypeConstructors(first: KotlinType, second: KotlinType): Boolean {
val typeRangeCapability = SubtypingRepresentatives::class.java return (first.unwrap() as? SubtypingRepresentatives)?.sameTypeConstructor(second) ?: false
return first.getCapability(typeRangeCapability)?.sameTypeConstructor(second) ?: false || (second.unwrap() as? SubtypingRepresentatives)?.sameTypeConstructor(first) ?: false
|| second.getCapability(typeRangeCapability)?.sameTypeConstructor(first) ?: false
} }
interface AbbreviatedType : TypeCapability { interface AbbreviatedType : TypeCapability {
@@ -136,8 +136,8 @@ public class TypeUtils {
@NotNull @NotNull
public static KotlinType makeNullableAsSpecified(@NotNull KotlinType type, boolean nullable) { public static KotlinType makeNullableAsSpecified(@NotNull KotlinType type, boolean nullable) {
KotlinType unwrappedType = KotlinTypeKt.unwrap(type); KotlinType unwrappedType = KotlinTypeKt.unwrap(type);
if (unwrappedType instanceof FlexibleType) { if (unwrappedType instanceof TypeWithCustomReplacement) {
return ((FlexibleType) unwrappedType).makeNullableAsSpecified(nullable); return ((TypeWithCustomReplacement) unwrappedType).makeNullableAsSpecified(nullable);
} }
else { else {
SimpleType simpleType = (SimpleType) unwrappedType; SimpleType simpleType = (SimpleType) unwrappedType;
@@ -85,7 +85,8 @@ fun TypeProjection.substitute(doSubstitute: (KotlinType) -> KotlinType): TypePro
fun KotlinType.replaceAnnotations(newAnnotations: Annotations): KotlinType { fun KotlinType.replaceAnnotations(newAnnotations: Annotations): KotlinType {
if (annotations.isEmpty() && newAnnotations.isEmpty()) return this if (annotations.isEmpty() && newAnnotations.isEmpty()) return this
if (isFlexible()) return asFlexibleType().replaceAnnotations(newAnnotations) val unwrappedType = unwrap()
if (unwrappedType is TypeWithCustomReplacement) return unwrappedType.replaceAnnotations(newAnnotations)
return asSimpleType().lazyReplaceAnnotations(newAnnotations) return asSimpleType().lazyReplaceAnnotations(newAnnotations)
} }
@@ -21,7 +21,7 @@ import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.types.checker.KotlinTypeChecker import org.jetbrains.kotlin.types.checker.KotlinTypeChecker
import org.jetbrains.kotlin.types.typeUtil.replaceAnnotations import org.jetbrains.kotlin.types.typeUtil.replaceAnnotations
interface FlexibleType : SubtypingRepresentatives { interface FlexibleType : SubtypingRepresentatives, TypeWithCustomReplacement {
// lowerBound is a subtype of upperBound // lowerBound is a subtype of upperBound
val lowerBound: SimpleType val lowerBound: SimpleType
val upperBound: SimpleType val upperBound: SimpleType
@@ -33,10 +33,6 @@ interface FlexibleType : SubtypingRepresentatives {
get() = upperBound get() = upperBound
override fun sameTypeConstructor(type: KotlinType) = false override fun sameTypeConstructor(type: KotlinType) = false
fun makeNullableAsSpecified(nullable: Boolean): KotlinType
fun replaceAnnotations(newAnnotations: Annotations): KotlinType
} }
fun KotlinType.isFlexible(): Boolean = unwrap() is FlexibleType fun KotlinType.isFlexible(): Boolean = unwrap() is FlexibleType
@@ -115,14 +111,6 @@ abstract class DelegatingFlexibleType protected constructor(
protected abstract val delegateType: KotlinType protected abstract val delegateType: KotlinType
override fun <T : TypeCapability> getCapability(capabilityClass: Class<T>): T? {
@Suppress("UNCHECKED_CAST")
return when(capabilityClass) {
SubtypingRepresentatives::class.java -> this as T
else -> super.getCapability(capabilityClass)
}
}
override fun makeNullableAsSpecified(nullable: Boolean): KotlinType { override fun makeNullableAsSpecified(nullable: Boolean): KotlinType {
return KotlinTypeFactory.flexibleType(TypeUtils.makeNullableAsSpecified(lowerBound, nullable), TypeUtils.makeNullableAsSpecified(upperBound, nullable)) return KotlinTypeFactory.flexibleType(TypeUtils.makeNullableAsSpecified(lowerBound, nullable), TypeUtils.makeNullableAsSpecified(upperBound, nullable))
} }