Remove type capability CustomTypeVariable.

This commit is contained in:
Stanislav Erokhin
2016-06-01 17:53:27 +03:00
parent a5c1e009c3
commit 4c0572fb2f
5 changed files with 25 additions and 29 deletions
@@ -124,12 +124,6 @@ private fun KotlinType.enhanceInflexible(qualifiers: (Int) -> JavaTypeQualifiers
val newSubstitution = TypeConstructorSubstitution.create(typeConstructor, enhancedArguments) val newSubstitution = TypeConstructorSubstitution.create(typeConstructor, enhancedArguments)
val newCapabilities =
if (effectiveQualifiers.isNotNullTypeParameter)
capabilities.addCapability(CustomTypeVariable::class.java, NotNullTypeParameterTypeCapability)
else
capabilities
val enhancedType = KotlinTypeImpl.create( val enhancedType = KotlinTypeImpl.create(
newAnnotations, newAnnotations,
typeConstructor, typeConstructor,
@@ -137,10 +131,11 @@ private fun KotlinType.enhanceInflexible(qualifiers: (Int) -> JavaTypeQualifiers
enhancedArguments, enhancedArguments,
if (enhancedClassifier is ClassDescriptor) if (enhancedClassifier is ClassDescriptor)
enhancedClassifier.getMemberScope(newSubstitution) enhancedClassifier.getMemberScope(newSubstitution)
else enhancedClassifier.getDefaultType().memberScope, else enhancedClassifier.getDefaultType().memberScope
newCapabilities
) )
return Result(enhancedType, subtreeSize, wereChanges = true)
val result = if (effectiveQualifiers.isNotNullTypeParameter) NotNullTypeParameter(enhancedType) else enhancedType
return Result(result, subtreeSize, wereChanges = true)
} }
private fun List<Annotations>.compositeAnnotationsOrSingle() = when (size) { private fun List<Annotations>.compositeAnnotationsOrSingle() = when (size) {
@@ -218,28 +213,31 @@ private object EnhancedTypeAnnotationDescriptor : AnnotationDescriptor {
override fun toString() = "[EnhancedType]" override fun toString() = "[EnhancedType]"
} }
internal object NotNullTypeParameterTypeCapability : CustomTypeVariable { internal class NotNullTypeParameter(private val delegate: SimpleType) : CustomTypeVariable, DelegatingType(), SimpleType {
override fun getDelegate(): KotlinType? = delegate
override val isTypeVariable: Boolean override val isTypeVariable: Boolean
get() = true get() = true
override fun substitutionResult(replacement: KotlinType): KotlinType { override fun substitutionResult(replacement: KotlinType): KotlinType {
if (!TypeUtils.isNullableType(replacement) && !replacement.isTypeParameter()) return replacement val unwrappedType = replacement.unwrap()
if (!TypeUtils.isNullableType(unwrappedType) && !unwrappedType.isTypeParameter()) return unwrappedType
if (replacement.isFlexible()) { return when (unwrappedType) {
with(replacement.asFlexibleType()) { is SimpleType -> unwrappedType.prepareReplacement()
return KotlinTypeFactory.flexibleType(lowerBound.prepareReplacement().asSimpleType(), upperBound.prepareReplacement().asSimpleType()) is FlexibleType -> KotlinTypeFactory.flexibleType(unwrappedType.lowerBound.prepareReplacement(),
} unwrappedType.upperBound.prepareReplacement())
else -> error("Incorrect type: $unwrappedType")
} }
return replacement.prepareReplacement()
} }
private fun KotlinType.prepareReplacement(): KotlinType { override val isMarkedNullable: Boolean
val result = makeNotNullable() get() = false
private fun SimpleType.prepareReplacement(): SimpleType {
val result = TypeUtils.makeNullableAsSpecified(this, false)
if (!this.isTypeParameter()) return result if (!this.isTypeParameter()) return result
return result.replace( return NotNullTypeParameter(result)
newCapabilities = capabilities.addCapability(
CustomTypeVariable::class.java, NotNullTypeParameterTypeCapability))
} }
} }
@@ -59,7 +59,7 @@ private fun KotlinType.extractQualifiers(): JavaTypeQualifiers {
return JavaTypeQualifiers( return JavaTypeQualifiers(
if (lower.isMarkedNullable) NULLABLE else if (!upper.isMarkedNullable) NOT_NULL else null, if (lower.isMarkedNullable) NULLABLE else if (!upper.isMarkedNullable) NOT_NULL else null,
if (mapping.isReadOnly(lower)) READ_ONLY else if (mapping.isMutable(upper)) MUTABLE else null, if (mapping.isReadOnly(lower)) READ_ONLY else if (mapping.isMutable(upper)) MUTABLE else null,
isNotNullTypeParameter = getCapability<CustomTypeVariable>() is NotNullTypeParameterTypeCapability) isNotNullTypeParameter = unwrap() is NotNullTypeParameter)
} }
private fun KotlinType.extractQualifiersFromAnnotations(): JavaTypeQualifiers { private fun KotlinType.extractQualifiersFromAnnotations(): JavaTypeQualifiers {
@@ -133,6 +133,7 @@ private class WrappedSimpleType(
override fun unwrap(): KotlinType { override fun unwrap(): KotlinType {
if (delegate.isError) return delegate // todo if (delegate.isError) return delegate // todo
if (delegate is CustomTypeVariable) return delegate // todo
return KotlinTypeImpl.create(annotations, constructor, isMarkedNullable, arguments, memberScope, capabilities, abbreviatedType) return KotlinTypeImpl.create(annotations, constructor, isMarkedNullable, arguments, memberScope, capabilities, abbreviatedType)
} }
@@ -53,16 +53,16 @@ inline fun <reified T : TypeCapability> KotlinType.getCapability(): T? = getCapa
// To facilitate laziness, any KotlinType implementation may inherit from this trait, // To facilitate laziness, any KotlinType implementation may inherit from this trait,
// even if it turns out that the type an instance represents is not actually a type variable // even if it turns out that the type an instance represents is not actually a type variable
// (i.e. it is not derived from a type parameter), see isTypeVariable // (i.e. it is not derived from a type parameter), see isTypeVariable
interface CustomTypeVariable : TypeCapability { interface CustomTypeVariable {
val isTypeVariable: Boolean val isTypeVariable: Boolean
// Throws an exception when isTypeVariable == false // Throws an exception when isTypeVariable == false
fun substitutionResult(replacement: KotlinType): KotlinType fun substitutionResult(replacement: KotlinType): KotlinType
} }
fun KotlinType.isCustomTypeVariable(): Boolean = this.getCapability(CustomTypeVariable::class.java)?.isTypeVariable ?: false fun KotlinType.isCustomTypeVariable(): Boolean = (unwrap() as? CustomTypeVariable)?.isTypeVariable ?: false
fun KotlinType.getCustomTypeVariable(): CustomTypeVariable? = fun KotlinType.getCustomTypeVariable(): CustomTypeVariable? =
this.getCapability(CustomTypeVariable::class.java)?.let { (unwrap() as? CustomTypeVariable)?.let {
if (it.isTypeVariable) it else null if (it.isTypeVariable) it else null
} }
@@ -132,9 +132,6 @@ class FlexibleTypeImpl(lowerBound: SimpleType, upperBound: SimpleType) : Delegat
override val delegateType: KotlinType get() = lowerBound override val delegateType: KotlinType get() = lowerBound
override fun <T : TypeCapability> getCapability(capabilityClass: Class<T>): T? { override fun <T : TypeCapability> getCapability(capabilityClass: Class<T>): T? {
@Suppress("UNCHECKED_CAST")
if (capabilityClass == CustomTypeVariable::class.java) return this as T
return super.getCapability(capabilityClass) return super.getCapability(capabilityClass)
} }