From 2e18b3de9da7efa34d8216bbf122fc15f823b8ce Mon Sep 17 00:00:00 2001 From: Michael Nedzelsky Date: Fri, 24 Jul 2015 13:38:58 +0300 Subject: [PATCH] prepare to retire Type.Constructor message in descriptors.proto after M13 --- .../kotlin/checkers/LazyOperationsLog.kt | 9 ++-- .../deserialization/TypeDeserializer.kt | 48 ++++++++++++++----- .../stubBuilder/ClassClsStubBuilder.kt | 8 ++-- .../stubBuilder/TypeClsStubBuilder.kt | 19 ++++---- 4 files changed, 58 insertions(+), 26 deletions(-) diff --git a/compiler/tests/org/jetbrains/kotlin/checkers/LazyOperationsLog.kt b/compiler/tests/org/jetbrains/kotlin/checkers/LazyOperationsLog.kt index 15a5706bb2b..78d660714fa 100644 --- a/compiler/tests/org/jetbrains/kotlin/checkers/LazyOperationsLog.kt +++ b/compiler/tests/org/jetbrains/kotlin/checkers/LazyOperationsLog.kt @@ -39,6 +39,8 @@ import org.jetbrains.kotlin.renderer.DescriptorRenderer import org.jetbrains.kotlin.resolve.calls.context.BasicCallResolutionContext import org.jetbrains.kotlin.psi.debugText.getDebugText import org.jetbrains.kotlin.resolve.calls.tasks.ResolutionCandidate +import org.jetbrains.kotlin.serialization.deserialization.TypeConstructorKind +import org.jetbrains.kotlin.serialization.deserialization.getTypeConstructorData import java.lang.reflect.GenericDeclaration import java.lang.reflect.Method import java.lang.reflect.Constructor @@ -158,9 +160,10 @@ class LazyOperationsLog( val typeDeserializer = o.field("typeDeserializer") val context = typeDeserializer.field("c") val typeProto = o.field("typeProto") - val text = when (typeProto.getConstructor().getKind()) { - ProtoBuf.Type.Constructor.Kind.CLASS -> context.nameResolver.getFqName(typeProto.getConstructor().getId()).asString() - ProtoBuf.Type.Constructor.Kind.TYPE_PARAMETER -> { + val typeConstructorData = typeProto.getTypeConstructorData() + val text = when (typeConstructorData.kind) { + TypeConstructorKind.CLASS -> context.nameResolver.getFqName(typeConstructorData.id).asString() + TypeConstructorKind.TYPE_PARAMETER -> { val classifier = (o as JetType).getConstructor().getDeclarationDescriptor()!! "" + classifier.getName() + " in " + DescriptorUtils.getFqName(classifier.getContainingDeclaration()) } diff --git a/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/TypeDeserializer.kt b/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/TypeDeserializer.kt index cb1170453ae..444f053ee67 100644 --- a/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/TypeDeserializer.kt +++ b/core/deserialization/src/org/jetbrains/kotlin/serialization/deserialization/TypeDeserializer.kt @@ -25,6 +25,31 @@ import org.jetbrains.kotlin.types.* import org.jetbrains.kotlin.utils.toReadOnlyList import java.util.LinkedHashMap +public enum class TypeConstructorKind { + CLASS, + TYPE_PARAMETER +} + +public data class TypeConstructorData(val kind: TypeConstructorKind, val id: Int) + +public fun ProtoBuf.Type.getTypeConstructorData(): TypeConstructorData { + if (hasConstructorClassName()) { + assert(!hasConstructorTypeParameter(), "constructor_class_name already presents, so constructor_type_parameter should not be here") + return TypeConstructorData(TypeConstructorKind.CLASS, constructorClassName) + } + else if (hasConstructorTypeParameter()) { + assert(!hasConstructorClassName(), "constructor_type_parameter already presents, so constructor_class_name should not be here") + return TypeConstructorData(TypeConstructorKind.TYPE_PARAMETER, constructorTypeParameter) + } + else { + return when (constructor.kind) { + ProtoBuf.Type.Constructor.Kind.CLASS -> TypeConstructorData(TypeConstructorKind.CLASS, constructor.id) + ProtoBuf.Type.Constructor.Kind.TYPE_PARAMETER -> TypeConstructorData(TypeConstructorKind.TYPE_PARAMETER, constructor.id) + else -> throw IllegalStateException("Unknown kind ${constructor.kind}") + } + } +} + public class TypeDeserializer( private val c: DeserializationContext, private val parent: TypeDeserializer?, @@ -71,25 +96,24 @@ public class TypeDeserializer( } fun typeConstructor(proto: ProtoBuf.Type): TypeConstructor { - val constructorProto = proto.getConstructor() - val id = constructorProto.getId() - return typeConstructor(constructorProto) ?: ErrorUtils.createErrorType( - if (constructorProto.getKind() == ProtoBuf.Type.Constructor.Kind.CLASS) + val typeConstructorData = proto.getTypeConstructorData() + val id = typeConstructorData.id + return typeConstructor(typeConstructorData) ?: ErrorUtils.createErrorType( + if (typeConstructorData.kind == TypeConstructorKind.CLASS) c.nameResolver.getClassId(id).asSingleFqName().asString() else "Unknown type parameter $id" - ).getConstructor() + ).constructor } - private fun typeConstructor(proto: ProtoBuf.Type.Constructor): TypeConstructor? = when (proto.getKind()) { - ProtoBuf.Type.Constructor.Kind.CLASS -> classDescriptors(proto.getId())?.getTypeConstructor() - ProtoBuf.Type.Constructor.Kind.TYPE_PARAMETER -> typeParameterTypeConstructor(proto) - else -> throw IllegalStateException("Unknown kind ${proto.getKind()}") + private fun typeConstructor(data: TypeConstructorData): TypeConstructor? = when (data.kind) { + TypeConstructorKind.CLASS -> classDescriptors(data.id)?.typeConstructor + TypeConstructorKind.TYPE_PARAMETER -> typeParameterTypeConstructor(data.id) } - private fun typeParameterTypeConstructor(proto: ProtoBuf.Type.Constructor): TypeConstructor? = - typeParameterDescriptors().get(proto.getId())?.getTypeConstructor() ?: - parent?.typeParameterTypeConstructor(proto) + private fun typeParameterTypeConstructor(typeParameterId: Int): TypeConstructor? = + typeParameterDescriptors().get(typeParameterId)?.typeConstructor ?: + parent?.typeParameterTypeConstructor(typeParameterId) private fun computeClassDescriptor(fqNameIndex: Int): ClassDescriptor? { val id = c.nameResolver.getClassId(fqNameIndex) diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/stubBuilder/ClassClsStubBuilder.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/stubBuilder/ClassClsStubBuilder.kt index 9a1abf5eed5..f5a992ebeb1 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/stubBuilder/ClassClsStubBuilder.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/stubBuilder/ClassClsStubBuilder.kt @@ -36,8 +36,9 @@ import org.jetbrains.kotlin.psi.stubs.impl.KotlinObjectStubImpl import org.jetbrains.kotlin.psi.stubs.impl.KotlinPlaceHolderStubImpl import org.jetbrains.kotlin.serialization.Flags import org.jetbrains.kotlin.serialization.ProtoBuf -import org.jetbrains.kotlin.serialization.ProtoBuf.Type import org.jetbrains.kotlin.serialization.deserialization.ProtoContainer +import org.jetbrains.kotlin.serialization.deserialization.TypeConstructorKind +import org.jetbrains.kotlin.serialization.deserialization.getTypeConstructorData fun createClassStub(parent: StubElement, classProto: ProtoBuf.Class, classId: ClassId, context: ClsStubBuilderContext) { @@ -55,8 +56,9 @@ private class ClassClsStubBuilder( private val classKind = Flags.CLASS_KIND[classProto.getFlags()] private val supertypeIds = classProto.getSupertypeList().map { type -> - assert(type.getConstructor().getKind() == Type.Constructor.Kind.CLASS) - c.nameResolver.getClassId(type.getConstructor().getId()) + val typeConstructorData = type.getTypeConstructorData() + assert(typeConstructorData.kind == TypeConstructorKind.CLASS) + c.nameResolver.getClassId(typeConstructorData.id) }.let { supertypeIds -> //empty supertype list if single supertype is Any diff --git a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/stubBuilder/TypeClsStubBuilder.kt b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/stubBuilder/TypeClsStubBuilder.kt index 66ae2034b30..38b1391d660 100644 --- a/idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/stubBuilder/TypeClsStubBuilder.kt +++ b/idea/idea-analysis/src/org/jetbrains/kotlin/idea/decompiler/stubBuilder/TypeClsStubBuilder.kt @@ -36,6 +36,8 @@ import org.jetbrains.kotlin.serialization.ProtoBuf.Type import org.jetbrains.kotlin.serialization.ProtoBuf.Type.Argument.Projection import org.jetbrains.kotlin.serialization.ProtoBuf.TypeParameter.Variance import org.jetbrains.kotlin.serialization.deserialization.ProtoContainer +import org.jetbrains.kotlin.serialization.deserialization.TypeConstructorKind +import org.jetbrains.kotlin.serialization.deserialization.getTypeConstructorData import org.jetbrains.kotlin.types.DynamicTypeCapabilities import org.jetbrains.kotlin.utils.addToStdlib.singletonOrEmptyList import java.util.ArrayList @@ -54,13 +56,14 @@ class TypeClsStubBuilder(private val c: ClsStubBuilderContext) { if (type.getNullable()) KotlinPlaceHolderStubImpl(typeReference, JetStubElementTypes.NULLABLE_TYPE) else typeReference - when (type.getConstructor().getKind()) { - Type.Constructor.Kind.CLASS -> { + val typeConstructorData = type.getTypeConstructorData() + when (typeConstructorData.kind) { + TypeConstructorKind.CLASS -> { createClassReferenceTypeStub(effectiveParent, type, annotations) } - Type.Constructor.Kind.TYPE_PARAMETER -> { + TypeConstructorKind.TYPE_PARAMETER -> { createTypeAnnotationStubs(effectiveParent, annotations) - val typeParameterName = c.typeParameters[type.getConstructor().getId()] + val typeParameterName = c.typeParameters[typeConstructorData.id] createStubForTypeName(ClassId.topLevel(FqName.topLevel(typeParameterName)), effectiveParent) } } @@ -237,11 +240,11 @@ class TypeClsStubBuilder(private val c: ClsStubBuilderContext) { } private fun Type.isDefaultUpperBound(): Boolean { - val constructor = getConstructor() - if (constructor.getKind() != Type.Constructor.Kind.CLASS) { + val typeConstructorData = getTypeConstructorData() + if (typeConstructorData.kind != TypeConstructorKind.CLASS) { return false } - val classId = c.nameResolver.getClassId(constructor.getId()) - return KotlinBuiltIns.isAny(classId.asSingleFqName().toUnsafe()) && this.getNullable() + val classId = c.nameResolver.getClassId(typeConstructorData.id) + return KotlinBuiltIns.isAny(classId.asSingleFqName().toUnsafe()) && this.nullable } }