Minor. Reformat TypeDeserializer.kt

This commit is contained in:
Denis Zharkov
2018-06-21 11:09:07 +03:00
parent 3fcf2f9c0d
commit 9deb9473d2
@@ -19,24 +19,24 @@ import org.jetbrains.kotlin.types.*
import java.util.* import java.util.*
class TypeDeserializer( class TypeDeserializer(
private val c: DeserializationContext, private val c: DeserializationContext,
private val parent: TypeDeserializer?, private val parent: TypeDeserializer?,
typeParameterProtos: List<ProtoBuf.TypeParameter>, typeParameterProtos: List<ProtoBuf.TypeParameter>,
private val debugName: String private val debugName: String
) { ) {
private val classDescriptors: (Int) -> ClassDescriptor? = c.storageManager.createMemoizedFunctionWithNullableValues { private val classDescriptors: (Int) -> ClassDescriptor? = c.storageManager.createMemoizedFunctionWithNullableValues { fqNameIndex ->
fqNameIndex -> computeClassDescriptor(fqNameIndex) computeClassDescriptor(fqNameIndex)
} }
private val typeAliasDescriptors: (Int) -> ClassifierDescriptor? = c.storageManager.createMemoizedFunctionWithNullableValues { private val typeAliasDescriptors: (Int) -> ClassifierDescriptor? =
fqNameIndex -> computeTypeAliasDescriptor(fqNameIndex) c.storageManager.createMemoizedFunctionWithNullableValues { fqNameIndex ->
} computeTypeAliasDescriptor(fqNameIndex)
}
private val typeParameterDescriptors = private val typeParameterDescriptors =
if (typeParameterProtos.isEmpty()) { if (typeParameterProtos.isEmpty()) {
mapOf<Int, TypeParameterDescriptor>() mapOf<Int, TypeParameterDescriptor>()
} } else {
else {
val result = LinkedHashMap<Int, TypeParameterDescriptor>() val result = LinkedHashMap<Int, TypeParameterDescriptor>()
for ((index, proto) in typeParameterProtos.withIndex()) { for ((index, proto) in typeParameterProtos.withIndex()) {
result[proto.id] = DeserializedTypeParameterDescriptor(c, proto, index) result[proto.id] = DeserializedTypeParameterDescriptor(c, proto, index)
@@ -45,7 +45,7 @@ class TypeDeserializer(
} }
val ownTypeParameters: List<TypeParameterDescriptor> val ownTypeParameters: List<TypeParameterDescriptor>
get() = typeParameterDescriptors.values.toList() get() = typeParameterDescriptors.values.toList()
// TODO: don't load identical types from TypeTable more than once // TODO: don't load identical types from TypeTable more than once
fun type(proto: ProtoBuf.Type, additionalAnnotations: Annotations = Annotations.EMPTY): KotlinType { fun type(proto: ProtoBuf.Type, additionalAnnotations: Annotations = Annotations.EMPTY): KotlinType {
@@ -75,13 +75,13 @@ class TypeDeserializer(
val annotations = DeserializedAnnotationsWithPossibleTargets(c.storageManager) { val annotations = DeserializedAnnotationsWithPossibleTargets(c.storageManager) {
c.components.annotationAndConstantLoader.loadTypeAnnotations(proto, c.nameResolver) c.components.annotationAndConstantLoader.loadTypeAnnotations(proto, c.nameResolver)
.map { AnnotationWithTarget(it, null) } .map { AnnotationWithTarget(it, null) }
.plus(additionalAnnotations.getAllAnnotations()) .plus(additionalAnnotations.getAllAnnotations())
.toList() .toList()
} }
fun ProtoBuf.Type.collectAllArguments(): List<ProtoBuf.Type.Argument> = fun ProtoBuf.Type.collectAllArguments(): List<ProtoBuf.Type.Argument> =
argumentList + outerType(c.typeTable)?.collectAllArguments().orEmpty() argumentList + outerType(c.typeTable)?.collectAllArguments().orEmpty()
val arguments = proto.collectAllArguments().mapIndexed { index, proto -> val arguments = proto.collectAllArguments().mapIndexed { index, proto ->
typeArgument(constructor.parameters.getOrNull(index), proto) typeArgument(constructor.parameters.getOrNull(index), proto)
@@ -89,8 +89,7 @@ class TypeDeserializer(
val simpleType = if (Flags.SUSPEND_TYPE.get(proto.flags)) { val simpleType = if (Flags.SUSPEND_TYPE.get(proto.flags)) {
createSuspendFunctionType(annotations, constructor, arguments, proto.nullable, c.components.configuration.releaseCoroutines) createSuspendFunctionType(annotations, constructor, arguments, proto.nullable, c.components.configuration.releaseCoroutines)
} } else {
else {
KotlinTypeFactory.simpleType(annotations, constructor, arguments, proto.nullable) KotlinTypeFactory.simpleType(annotations, constructor, arguments, proto.nullable)
} }
@@ -113,7 +112,7 @@ class TypeDeserializer(
proto.hasClassName() -> (classDescriptors(proto.className) ?: notFoundClass(proto.className)).typeConstructor proto.hasClassName() -> (classDescriptors(proto.className) ?: notFoundClass(proto.className)).typeConstructor
proto.hasTypeParameter() -> proto.hasTypeParameter() ->
typeParameterTypeConstructor(proto.typeParameter) typeParameterTypeConstructor(proto.typeParameter)
?: ErrorUtils.createErrorTypeConstructor("Unknown type parameter ${proto.typeParameter}") ?: ErrorUtils.createErrorTypeConstructor("Unknown type parameter ${proto.typeParameter}")
proto.hasTypeParameterName() -> { proto.hasTypeParameterName() -> {
val container = c.containingDeclaration val container = c.containingDeclaration
val name = c.nameResolver.getString(proto.typeParameterName) val name = c.nameResolver.getString(proto.typeParameterName)
@@ -142,20 +141,26 @@ class TypeDeserializer(
1 -> { 1 -> {
val arity = arguments.size - 1 val arity = arguments.size - 1
if (arity >= 0) { if (arity >= 0) {
KotlinTypeFactory.simpleType(annotations, functionTypeConstructor.builtIns.getSuspendFunction(arity).typeConstructor, arguments, nullable) KotlinTypeFactory.simpleType(
} annotations,
else { functionTypeConstructor.builtIns.getSuspendFunction(arity).typeConstructor,
arguments,
nullable
)
} else {
null null
} }
} }
else -> null else -> null
} }
return result ?: ErrorUtils.createErrorTypeWithArguments("Bad suspend function in metadata with constructor: $functionTypeConstructor", arguments) return result ?: ErrorUtils.createErrorTypeWithArguments(
"Bad suspend function in metadata with constructor: $functionTypeConstructor",
arguments
)
} }
private fun typeParameterTypeConstructor(typeParameterId: Int): TypeConstructor? = private fun typeParameterTypeConstructor(typeParameterId: Int): TypeConstructor? =
typeParameterDescriptors.get(typeParameterId)?.typeConstructor ?: typeParameterDescriptors.get(typeParameterId)?.typeConstructor ?: parent?.typeParameterTypeConstructor(typeParameterId)
parent?.typeParameterTypeConstructor(typeParameterId)
private fun computeClassDescriptor(fqNameIndex: Int): ClassDescriptor? { private fun computeClassDescriptor(fqNameIndex: Int): ClassDescriptor? {
val id = c.nameResolver.getClassId(fqNameIndex) val id = c.nameResolver.getClassId(fqNameIndex)
@@ -178,8 +183,7 @@ class TypeDeserializer(
return if (id.isLocal) { return if (id.isLocal) {
// TODO: support deserialization of local type aliases (see KT-13692) // TODO: support deserialization of local type aliases (see KT-13692)
return null return null
} } else {
else {
c.components.moduleDescriptor.findTypeAliasAcrossModuleDependencies(id) c.components.moduleDescriptor.findTypeAliasAcrossModuleDependencies(id)
} }
} }
@@ -193,8 +197,7 @@ class TypeDeserializer(
} }
val projection = ProtoEnumFlags.variance(typeArgumentProto.projection) val projection = ProtoEnumFlags.variance(typeArgumentProto.projection)
val type = typeArgumentProto.type(c.typeTable) ?: val type = typeArgumentProto.type(c.typeTable) ?: return TypeProjectionImpl(ErrorUtils.createErrorType("No type recorded"))
return TypeProjectionImpl(ErrorUtils.createErrorType("No type recorded"))
return TypeProjectionImpl(projection, type(type)) return TypeProjectionImpl(projection, type(type))
} }