prepare to retire Type.Constructor message in descriptors.proto after M13

This commit is contained in:
Michael Nedzelsky
2015-07-24 13:38:58 +03:00
parent b3659486d9
commit 2e18b3de9d
4 changed files with 58 additions and 26 deletions
@@ -39,6 +39,8 @@ import org.jetbrains.kotlin.renderer.DescriptorRenderer
import org.jetbrains.kotlin.resolve.calls.context.BasicCallResolutionContext import org.jetbrains.kotlin.resolve.calls.context.BasicCallResolutionContext
import org.jetbrains.kotlin.psi.debugText.getDebugText import org.jetbrains.kotlin.psi.debugText.getDebugText
import org.jetbrains.kotlin.resolve.calls.tasks.ResolutionCandidate 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.GenericDeclaration
import java.lang.reflect.Method import java.lang.reflect.Method
import java.lang.reflect.Constructor import java.lang.reflect.Constructor
@@ -158,9 +160,10 @@ class LazyOperationsLog(
val typeDeserializer = o.field<TypeDeserializer>("typeDeserializer") val typeDeserializer = o.field<TypeDeserializer>("typeDeserializer")
val context = typeDeserializer.field<DeserializationContext>("c") val context = typeDeserializer.field<DeserializationContext>("c")
val typeProto = o.field<ProtoBuf.Type>("typeProto") val typeProto = o.field<ProtoBuf.Type>("typeProto")
val text = when (typeProto.getConstructor().getKind()) { val typeConstructorData = typeProto.getTypeConstructorData()
ProtoBuf.Type.Constructor.Kind.CLASS -> context.nameResolver.getFqName(typeProto.getConstructor().getId()).asString() val text = when (typeConstructorData.kind) {
ProtoBuf.Type.Constructor.Kind.TYPE_PARAMETER -> { TypeConstructorKind.CLASS -> context.nameResolver.getFqName(typeConstructorData.id).asString()
TypeConstructorKind.TYPE_PARAMETER -> {
val classifier = (o as JetType).getConstructor().getDeclarationDescriptor()!! val classifier = (o as JetType).getConstructor().getDeclarationDescriptor()!!
"" + classifier.getName() + " in " + DescriptorUtils.getFqName(classifier.getContainingDeclaration()) "" + classifier.getName() + " in " + DescriptorUtils.getFqName(classifier.getContainingDeclaration())
} }
@@ -25,6 +25,31 @@ import org.jetbrains.kotlin.types.*
import org.jetbrains.kotlin.utils.toReadOnlyList import org.jetbrains.kotlin.utils.toReadOnlyList
import java.util.LinkedHashMap 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( public class TypeDeserializer(
private val c: DeserializationContext, private val c: DeserializationContext,
private val parent: TypeDeserializer?, private val parent: TypeDeserializer?,
@@ -71,25 +96,24 @@ public class TypeDeserializer(
} }
fun typeConstructor(proto: ProtoBuf.Type): TypeConstructor { fun typeConstructor(proto: ProtoBuf.Type): TypeConstructor {
val constructorProto = proto.getConstructor() val typeConstructorData = proto.getTypeConstructorData()
val id = constructorProto.getId() val id = typeConstructorData.id
return typeConstructor(constructorProto) ?: ErrorUtils.createErrorType( return typeConstructor(typeConstructorData) ?: ErrorUtils.createErrorType(
if (constructorProto.getKind() == ProtoBuf.Type.Constructor.Kind.CLASS) if (typeConstructorData.kind == TypeConstructorKind.CLASS)
c.nameResolver.getClassId(id).asSingleFqName().asString() c.nameResolver.getClassId(id).asSingleFqName().asString()
else else
"Unknown type parameter $id" "Unknown type parameter $id"
).getConstructor() ).constructor
} }
private fun typeConstructor(proto: ProtoBuf.Type.Constructor): TypeConstructor? = when (proto.getKind()) { private fun typeConstructor(data: TypeConstructorData): TypeConstructor? = when (data.kind) {
ProtoBuf.Type.Constructor.Kind.CLASS -> classDescriptors(proto.getId())?.getTypeConstructor() TypeConstructorKind.CLASS -> classDescriptors(data.id)?.typeConstructor
ProtoBuf.Type.Constructor.Kind.TYPE_PARAMETER -> typeParameterTypeConstructor(proto) TypeConstructorKind.TYPE_PARAMETER -> typeParameterTypeConstructor(data.id)
else -> throw IllegalStateException("Unknown kind ${proto.getKind()}")
} }
private fun typeParameterTypeConstructor(proto: ProtoBuf.Type.Constructor): TypeConstructor? = private fun typeParameterTypeConstructor(typeParameterId: Int): TypeConstructor? =
typeParameterDescriptors().get(proto.getId())?.getTypeConstructor() ?: typeParameterDescriptors().get(typeParameterId)?.typeConstructor ?:
parent?.typeParameterTypeConstructor(proto) 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)
@@ -36,8 +36,9 @@ import org.jetbrains.kotlin.psi.stubs.impl.KotlinObjectStubImpl
import org.jetbrains.kotlin.psi.stubs.impl.KotlinPlaceHolderStubImpl import org.jetbrains.kotlin.psi.stubs.impl.KotlinPlaceHolderStubImpl
import org.jetbrains.kotlin.serialization.Flags import org.jetbrains.kotlin.serialization.Flags
import org.jetbrains.kotlin.serialization.ProtoBuf 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.ProtoContainer
import org.jetbrains.kotlin.serialization.deserialization.TypeConstructorKind
import org.jetbrains.kotlin.serialization.deserialization.getTypeConstructorData
fun createClassStub(parent: StubElement<out PsiElement>, classProto: ProtoBuf.Class, classId: ClassId, context: ClsStubBuilderContext) { fun createClassStub(parent: StubElement<out PsiElement>, classProto: ProtoBuf.Class, classId: ClassId, context: ClsStubBuilderContext) {
@@ -55,8 +56,9 @@ private class ClassClsStubBuilder(
private val classKind = Flags.CLASS_KIND[classProto.getFlags()] private val classKind = Flags.CLASS_KIND[classProto.getFlags()]
private val supertypeIds = classProto.getSupertypeList().map { private val supertypeIds = classProto.getSupertypeList().map {
type -> type ->
assert(type.getConstructor().getKind() == Type.Constructor.Kind.CLASS) val typeConstructorData = type.getTypeConstructorData()
c.nameResolver.getClassId(type.getConstructor().getId()) assert(typeConstructorData.kind == TypeConstructorKind.CLASS)
c.nameResolver.getClassId(typeConstructorData.id)
}.let { }.let {
supertypeIds -> supertypeIds ->
//empty supertype list if single supertype is Any //empty supertype list if single supertype is Any
@@ -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.Type.Argument.Projection
import org.jetbrains.kotlin.serialization.ProtoBuf.TypeParameter.Variance import org.jetbrains.kotlin.serialization.ProtoBuf.TypeParameter.Variance
import org.jetbrains.kotlin.serialization.deserialization.ProtoContainer 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.types.DynamicTypeCapabilities
import org.jetbrains.kotlin.utils.addToStdlib.singletonOrEmptyList import org.jetbrains.kotlin.utils.addToStdlib.singletonOrEmptyList
import java.util.ArrayList import java.util.ArrayList
@@ -54,13 +56,14 @@ class TypeClsStubBuilder(private val c: ClsStubBuilderContext) {
if (type.getNullable()) KotlinPlaceHolderStubImpl<JetNullableType>(typeReference, JetStubElementTypes.NULLABLE_TYPE) if (type.getNullable()) KotlinPlaceHolderStubImpl<JetNullableType>(typeReference, JetStubElementTypes.NULLABLE_TYPE)
else typeReference else typeReference
when (type.getConstructor().getKind()) { val typeConstructorData = type.getTypeConstructorData()
Type.Constructor.Kind.CLASS -> { when (typeConstructorData.kind) {
TypeConstructorKind.CLASS -> {
createClassReferenceTypeStub(effectiveParent, type, annotations) createClassReferenceTypeStub(effectiveParent, type, annotations)
} }
Type.Constructor.Kind.TYPE_PARAMETER -> { TypeConstructorKind.TYPE_PARAMETER -> {
createTypeAnnotationStubs(effectiveParent, annotations) createTypeAnnotationStubs(effectiveParent, annotations)
val typeParameterName = c.typeParameters[type.getConstructor().getId()] val typeParameterName = c.typeParameters[typeConstructorData.id]
createStubForTypeName(ClassId.topLevel(FqName.topLevel(typeParameterName)), effectiveParent) createStubForTypeName(ClassId.topLevel(FqName.topLevel(typeParameterName)), effectiveParent)
} }
} }
@@ -237,11 +240,11 @@ class TypeClsStubBuilder(private val c: ClsStubBuilderContext) {
} }
private fun Type.isDefaultUpperBound(): Boolean { private fun Type.isDefaultUpperBound(): Boolean {
val constructor = getConstructor() val typeConstructorData = getTypeConstructorData()
if (constructor.getKind() != Type.Constructor.Kind.CLASS) { if (typeConstructorData.kind != TypeConstructorKind.CLASS) {
return false return false
} }
val classId = c.nameResolver.getClassId(constructor.getId()) val classId = c.nameResolver.getClassId(typeConstructorData.id)
return KotlinBuiltIns.isAny(classId.asSingleFqName().toUnsafe()) && this.getNullable() return KotlinBuiltIns.isAny(classId.asSingleFqName().toUnsafe()) && this.nullable
} }
} }