Refactoring. Move flexible type creation to KotlinTypeFactory.

This commit is contained in:
Stanislav Erokhin
2016-05-25 13:15:13 +03:00
parent 3a451744c5
commit 957bae18be
22 changed files with 168 additions and 149 deletions
@@ -22,10 +22,10 @@ import org.jetbrains.kotlin.descriptors.impl.AbstractLazyTypeParameterDescriptor
import org.jetbrains.kotlin.load.java.components.TypeUsage
import org.jetbrains.kotlin.load.java.lazy.LazyJavaAnnotations
import org.jetbrains.kotlin.load.java.lazy.LazyJavaResolverContext
import org.jetbrains.kotlin.load.java.lazy.types.LazyJavaTypeResolver
import org.jetbrains.kotlin.load.java.lazy.types.toAttributes
import org.jetbrains.kotlin.load.java.structure.JavaTypeParameter
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.KotlinTypeFactory
import org.jetbrains.kotlin.types.Variance
class LazyJavaTypeParameterDescriptor(
@@ -47,7 +47,7 @@ class LazyJavaTypeParameterDescriptor(
override fun resolveUpperBounds(): List<KotlinType> {
val bounds = javaTypeParameter.upperBounds
if (bounds.isEmpty()) {
return listOf(LazyJavaTypeResolver.FlexibleJavaClassifierTypeFactory.create(
return listOf(KotlinTypeFactory.flexibleType(
c.module.builtIns.anyType,
c.module.builtIns.nullableAnyType
))
@@ -56,7 +56,7 @@ class LazyJavaTypeResolver(
}
is JavaClassifierType ->
if (attr.allowFlexible && attr.howThisTypeIsUsed != SUPERTYPE)
FlexibleJavaClassifierTypeFactory.create(
KotlinTypeFactory.flexibleType(
LazyJavaClassifierType(javaType, attr.toFlexible(FLEXIBLE_LOWER_BOUND)),
LazyJavaClassifierType(javaType, attr.toFlexible(FLEXIBLE_UPPER_BOUND))
)
@@ -75,7 +75,7 @@ class LazyJavaTypeResolver(
if (primitiveType != null) {
val jetType = c.module.builtIns.getPrimitiveArrayKotlinType(primitiveType)
return@run if (attr.allowFlexible)
FlexibleJavaClassifierTypeFactory.create(jetType, TypeUtils.makeNullable(jetType))
KotlinTypeFactory.flexibleType(jetType, TypeUtils.makeNullable(jetType))
else TypeUtils.makeNullableAsSpecified(jetType, !attr.isMarkedNotNull)
}
@@ -83,7 +83,7 @@ class LazyJavaTypeResolver(
TYPE_ARGUMENT.toAttributes(attr.allowFlexible, attr.isForAnnotationParameter))
if (attr.allowFlexible) {
return@run FlexibleJavaClassifierTypeFactory.create(
return@run KotlinTypeFactory.flexibleType(
c.module.builtIns.getArrayType(INVARIANT, componentType),
TypeUtils.makeNullable(c.module.builtIns.getArrayType(OUT_VARIANCE, componentType)))
}
@@ -280,38 +280,6 @@ class LazyJavaTypeResolver(
override val isMarkedNullable: Boolean get() = nullable()
}
object FlexibleJavaClassifierTypeFactory : FlexibleTypeFactory {
override val id: String get() = "kotlin.jvm.PlatformType"
override fun create(lowerBound: KotlinType, upperBound: KotlinType): KotlinType {
if (lowerBound == upperBound) return lowerBound
return Impl(lowerBound, upperBound)
}
private class Impl(lowerBound: KotlinType, upperBound: KotlinType) :
DelegatingFlexibleType(lowerBound, upperBound, FlexibleJavaClassifierTypeFactory), CustomTypeVariable {
override val delegateType: KotlinType get() = lowerBound
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)
}
override val isTypeVariable: Boolean get() = lowerBound.constructor.declarationDescriptor is TypeParameterDescriptor
&& lowerBound.constructor == upperBound.constructor
override fun substitutionResult(replacement: KotlinType): KotlinType {
return if (replacement.isFlexible()) replacement
else create(replacement, TypeUtils.makeNullable(replacement))
}
}
}
}
internal fun makeStarProjection(
@@ -91,7 +91,7 @@ internal object RawSubstitution : TypeSubstitution() {
is ClassDescriptor -> {
val lower = type.lowerIfFlexible()
val upper = type.upperIfFlexible()
LazyJavaTypeResolver.FlexibleJavaClassifierTypeFactory.create(
KotlinTypeFactory.flexibleType(
eraseInflexibleBasedOnClassDescriptor(lower, declaration, lowerTypeAttr),
eraseInflexibleBasedOnClassDescriptor(upper, declaration, upperTypeAttr)
)
@@ -100,7 +100,7 @@ internal object RawSubstitution : TypeSubstitution() {
}
}
private fun eraseInflexibleBasedOnClassDescriptor(type: KotlinType, declaration: ClassDescriptor, attr: JavaTypeAttributes): KotlinType {
private fun eraseInflexibleBasedOnClassDescriptor(type: KotlinType, declaration: ClassDescriptor, attr: JavaTypeAttributes): SimpleType {
if (KotlinBuiltIns.isArray(type)) {
val componentTypeProjection = type.arguments[0]
val arguments = listOf(
@@ -70,7 +70,7 @@ private fun KotlinType.enhancePossiblyFlexible(qualifiers: (Int) -> JavaTypeQual
val wereChanges = lowerResult.wereChanges || upperResult.wereChanges
Result(
if (wereChanges)
factory.create(lowerResult.type, upperResult.type)
KotlinTypeFactory.flexibleType(lowerResult.type.asSimpleType(), upperResult.type.asSimpleType())
else
this@enhancePossiblyFlexible,
lowerResult.subtreeSize,
@@ -228,7 +228,7 @@ internal object NotNullTypeParameterTypeCapability : CustomTypeVariable {
if (replacement.isFlexible()) {
with(replacement.flexibility()) {
return factory.create(lowerBound.prepareReplacement(), upperBound.prepareReplacement())
return KotlinTypeFactory.flexibleType(lowerBound.prepareReplacement().asSimpleType(), upperBound.prepareReplacement().asSimpleType())
}
}
@@ -19,9 +19,9 @@ package org.jetbrains.kotlin.load.kotlin
import org.jetbrains.kotlin.descriptors.ModuleDescriptor
import org.jetbrains.kotlin.incremental.components.LookupTracker
import org.jetbrains.kotlin.load.java.lazy.LazyJavaPackageFragmentProvider
import org.jetbrains.kotlin.load.java.lazy.types.LazyJavaTypeResolver.FlexibleJavaClassifierTypeFactory
import org.jetbrains.kotlin.serialization.deserialization.*
import org.jetbrains.kotlin.storage.StorageManager
import org.jetbrains.kotlin.types.FlexibleJavaClassifierTypeFactory
// This class is needed only for easier injection: exact types of needed components are specified in the constructor here.
// Otherwise injector generator is not smart enough to deduce, for example, which package fragment provider DeserializationComponents needs
@@ -45,9 +45,9 @@ internal class UnsafeVarianceTypeSubstitution(kotlinBuiltIns: KotlinBuiltIns) :
if (unsafeVariancePaths.isEmpty()) return this
if (isFlexible()) {
return flexibility().factory.create(
lowerIfFlexible().annotatePartsWithUnsafeVariance(subPathsWithIndex(unsafeVariancePaths, 0)),
upperIfFlexible().annotatePartsWithUnsafeVariance(subPathsWithIndex(unsafeVariancePaths, 1))
return KotlinTypeFactory.flexibleType(
lowerIfFlexible().annotatePartsWithUnsafeVariance(subPathsWithIndex(unsafeVariancePaths, 0)).asSimpleType(),
upperIfFlexible().annotatePartsWithUnsafeVariance(subPathsWithIndex(unsafeVariancePaths, 1)).asSimpleType()
)
}