Refactor API of NotFoundClasses

Move deserialization-related stuff to TypeDeserializer, because it's
going to be used in other places besides deserialization
This commit is contained in:
Alexander Udalov
2017-02-22 14:12:55 +03:00
parent 86994f81c3
commit 794cc1e3be
4 changed files with 26 additions and 29 deletions
@@ -140,7 +140,7 @@ class JavaTypeResolver(
// Note that this makes MISSING_DEPENDENCY_CLASS diagnostic messages not as precise as they could be in some corner cases.
private fun createNotFoundClass(javaType: JavaClassifierType): TypeConstructor {
val classId = parseCanonicalFqNameIgnoringTypeArguments(javaType.canonicalText)
return c.components.deserializedDescriptorResolver.components.notFoundClasses.getClass(classId, listOf(0))
return c.components.deserializedDescriptorResolver.components.notFoundClasses.getClass(classId, listOf(0)).typeConstructor
}
private fun mapKotlinClass(javaType: JavaClassifierType, attr: JavaTypeAttributes, fqName: FqName): ClassDescriptor? {
@@ -29,10 +29,12 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
import org.jetbrains.kotlin.resolve.descriptorUtil.fqNameUnsafe
import org.jetbrains.kotlin.resolve.descriptorUtil.module
import org.jetbrains.kotlin.resolve.scopes.MemberScope
import org.jetbrains.kotlin.serialization.ProtoBuf
import org.jetbrains.kotlin.storage.StorageManager
import org.jetbrains.kotlin.storage.getValue
import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.types.ClassTypeConstructorImpl
import org.jetbrains.kotlin.types.SimpleType
import org.jetbrains.kotlin.types.TypeSubstitutor
import org.jetbrains.kotlin.types.Variance
class NotFoundClasses(private val storageManager: StorageManager, private val module: ModuleDescriptor) {
/**
@@ -71,7 +73,7 @@ class NotFoundClasses(private val storageManager: StorageManager, private val mo
}
val container = classId.outerClassId?.let { outerClassId ->
getOrCreateClass(outerClassId, typeParametersCount.drop(1))
getClass(outerClassId, typeParametersCount.drop(1))
} ?: packageFragments(classId.packageFqName)
// Treat a class with a nested ClassId as inner for simplicity, otherwise the outer type cannot have generic arguments
@@ -152,22 +154,12 @@ class NotFoundClasses(private val storageManager: StorageManager, private val mo
// (This may happen when a class with the same FQ name is instantiated with different type arguments in different modules.)
// It's better than creating just one descriptor because otherwise would fail in multiple places where it's asserted that
// the number of type arguments in a type must be equal to the number of the type parameters of the class
private fun getOrCreateClass(classId: ClassId, typeParametersCount: List<Int>): ClassDescriptor {
fun getClass(classId: ClassId, typeParametersCount: List<Int>): ClassDescriptor {
return classes(ClassRequest(classId, typeParametersCount))
}
fun getClass(proto: ProtoBuf.Type, nameResolver: NameResolver, typeTable: TypeTable): TypeConstructor {
val classId = nameResolver.getClassId(proto.className)
return getOrCreateClass(classId, computeTypeParametersCount(classId, proto, typeTable)).typeConstructor
}
fun getClass(classId: ClassId, typeParametersCount: List<Int>): TypeConstructor {
return getOrCreateClass(classId, typeParametersCount).typeConstructor
}
fun getTypeAlias(proto: ProtoBuf.Type, nameResolver: NameResolver, typeTable: TypeTable): TypeConstructor {
val classId = nameResolver.getClassId(proto.typeAliasName)
return typeAliases(ClassRequest(classId, computeTypeParametersCount(classId, proto, typeTable))).typeConstructor
fun getTypeAlias(classId: ClassId, typeParametersCount: List<Int>): TypeAliasDescriptor {
return typeAliases(ClassRequest(classId, typeParametersCount))
}
}
@@ -179,12 +171,3 @@ private fun createTypeParameters(
classifierDescriptor, Annotations.EMPTY, false, Variance.INVARIANT, Name.identifier("T$index"), index
)
}
private fun computeTypeParametersCount(classId: ClassId, proto: ProtoBuf.Type, typeTable: TypeTable): List<Int> {
val typeParametersCount = generateSequence(proto) { it.outerType(typeTable) }.map { it.argumentCount }.toMutableList()
val classNestingLevel = generateSequence(classId, ClassId::getOuterClassId).count()
while (typeParametersCount.size < classNestingLevel) {
typeParametersCount.add(0)
}
return typeParametersCount
}
@@ -23,6 +23,7 @@ import org.jetbrains.kotlin.descriptors.ClassifierDescriptor
import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor
import org.jetbrains.kotlin.descriptors.annotations.AnnotationWithTarget
import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.serialization.Flags
import org.jetbrains.kotlin.serialization.ProtoBuf
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedAnnotationsWithPossibleTargets
@@ -115,7 +116,9 @@ class TypeDeserializer(
when {
proto.hasClassName() -> {
classDescriptors(proto.className)?.typeConstructor
?: c.components.notFoundClasses.getClass(proto, c.nameResolver, c.typeTable)
?: c.nameResolver.getClassId(proto.className).let { classId ->
c.components.notFoundClasses.getClass(classId, computeTypeParametersCount(classId, proto, c.typeTable)).typeConstructor
}
}
proto.hasTypeParameter() ->
typeParameterTypeConstructor(proto.typeParameter)
@@ -128,11 +131,22 @@ class TypeDeserializer(
}
proto.hasTypeAliasName() -> {
typeAliasDescriptors(proto.typeAliasName)?.typeConstructor
?: c.components.notFoundClasses.getTypeAlias(proto, c.nameResolver, c.typeTable)
?: c.nameResolver.getClassId(proto.typeAliasName).let { classId ->
c.components.notFoundClasses.getTypeAlias(classId, computeTypeParametersCount(classId, proto, c.typeTable)).typeConstructor
}
}
else -> ErrorUtils.createErrorTypeConstructor("Unknown type")
}
private fun computeTypeParametersCount(classId: ClassId, proto: ProtoBuf.Type, typeTable: TypeTable): List<Int> {
val typeParametersCount = generateSequence(proto) { it.outerType(typeTable) }.map { it.argumentCount }.toMutableList()
val classNestingLevel = generateSequence(classId, ClassId::getOuterClassId).count()
while (typeParametersCount.size < classNestingLevel) {
typeParametersCount.add(0)
}
return typeParametersCount
}
private fun createSuspendFunctionType(
annotations: Annotations,
functionTypeConstructor: TypeConstructor,
@@ -42,7 +42,7 @@ fun ModuleDescriptor.findNonGenericClassAcrossDependencies(classId: ClassId, not
// Take a list of N zeros, where N is the number of class names in the given ClassId
val typeParametersCount = generateSequence(classId, ClassId::getOuterClassId).map { 0 }.toList()
return notFoundClasses.getClass(classId, typeParametersCount).declarationDescriptor as ClassDescriptor
return notFoundClasses.getClass(classId, typeParametersCount)
}
fun ModuleDescriptor.findTypeAliasAcrossModuleDependencies(classId: ClassId): TypeAliasDescriptor? {