Support a more optimized way of storing types in metadata

Together with almost every type now there's also an id which references a type
from a separate table. Storing such ids instead of Type messages will allow to
reduce the size of the metadata for files where types are used many times over
and over again. Currently only deserialization of such types is supported,
along with the former mechanism
This commit is contained in:
Alexander Udalov
2015-10-07 19:00:08 +03:00
parent bff6639e2d
commit 82d2e623d3
32 changed files with 5583 additions and 796 deletions
File diff suppressed because it is too large Load Diff
@@ -92,8 +92,8 @@ public abstract class AbstractBinaryClassAnnotationAndConstantLoader<A : Any, C
proto as ProtoBuf.Property proto as ProtoBuf.Property
val nameResolver = container.nameResolver val nameResolver = container.nameResolver
val syntheticFunctionSignature = getPropertySignature(proto, nameResolver, synthetic = true) val syntheticFunctionSignature = getPropertySignature(proto, nameResolver, container.typeTable, synthetic = true)
val fieldSignature = getPropertySignature(proto, nameResolver, field = true) val fieldSignature = getPropertySignature(proto, nameResolver, container.typeTable, field = true)
val propertyAnnotations = syntheticFunctionSignature?.let { sig -> val propertyAnnotations = syntheticFunctionSignature?.let { sig ->
findClassAndLoadMemberAnnotations(container, proto, sig) findClassAndLoadMemberAnnotations(container, proto, sig)
@@ -106,7 +106,7 @@ public abstract class AbstractBinaryClassAnnotationAndConstantLoader<A : Any, C
return loadPropertyAnnotations(propertyAnnotations, fieldAnnotations) return loadPropertyAnnotations(propertyAnnotations, fieldAnnotations)
} }
val signature = getCallableSignature(proto, container.nameResolver, kind) ?: return emptyList() val signature = getCallableSignature(proto, container.nameResolver, container.typeTable, kind) ?: return emptyList()
return transformAnnotations(findClassAndLoadMemberAnnotations(container, proto, signature)) return transformAnnotations(findClassAndLoadMemberAnnotations(container, proto, signature))
} }
@@ -138,7 +138,7 @@ public abstract class AbstractBinaryClassAnnotationAndConstantLoader<A : Any, C
parameterIndex: Int, parameterIndex: Int,
proto: ProtoBuf.ValueParameter proto: ProtoBuf.ValueParameter
): List<A> { ): List<A> {
val methodSignature = getCallableSignature(message, container.nameResolver, kind) val methodSignature = getCallableSignature(message, container.nameResolver, container.typeTable, kind)
if (methodSignature != null) { if (methodSignature != null) {
val index = if (proto.hasExtension(index)) proto.getExtension(index) else parameterIndex val index = if (proto.hasExtension(index)) proto.getExtension(index) else parameterIndex
val paramSignature = MemberSignature.fromMethodSignatureAndParameterIndex(methodSignature, index) val paramSignature = MemberSignature.fromMethodSignatureAndParameterIndex(methodSignature, index)
@@ -153,7 +153,7 @@ public abstract class AbstractBinaryClassAnnotationAndConstantLoader<A : Any, C
message: MessageLite, message: MessageLite,
kind: AnnotatedCallableKind kind: AnnotatedCallableKind
): List<A> { ): List<A> {
val methodSignature = getCallableSignature(message, container.nameResolver, kind) val methodSignature = getCallableSignature(message, container.nameResolver, container.typeTable, kind)
if (methodSignature != null) { if (methodSignature != null) {
val paramSignature = MemberSignature.fromMethodSignatureAndParameterIndex(methodSignature, 0) val paramSignature = MemberSignature.fromMethodSignatureAndParameterIndex(methodSignature, 0)
return findClassAndLoadMemberAnnotations(container, message, paramSignature) return findClassAndLoadMemberAnnotations(container, message, paramSignature)
@@ -168,7 +168,7 @@ public abstract class AbstractBinaryClassAnnotationAndConstantLoader<A : Any, C
override fun loadPropertyConstant(container: ProtoContainer, proto: ProtoBuf.Property, expectedType: JetType): C? { override fun loadPropertyConstant(container: ProtoContainer, proto: ProtoBuf.Property, expectedType: JetType): C? {
val nameResolver = container.nameResolver val nameResolver = container.nameResolver
val signature = getCallableSignature(proto, nameResolver, AnnotatedCallableKind.PROPERTY) ?: return null val signature = getCallableSignature(proto, nameResolver, container.typeTable, AnnotatedCallableKind.PROPERTY) ?: return null
val kotlinClass = findClassWithAnnotationsAndInitializers( val kotlinClass = findClassWithAnnotationsAndInitializers(
container, getImplClassName(proto, nameResolver), isStaticFieldInOuter(proto) container, getImplClassName(proto, nameResolver), isStaticFieldInOuter(proto)
@@ -279,6 +279,7 @@ public abstract class AbstractBinaryClassAnnotationAndConstantLoader<A : Any, C
private fun getPropertySignature( private fun getPropertySignature(
proto: ProtoBuf.Property, proto: ProtoBuf.Property,
nameResolver: NameResolver, nameResolver: NameResolver,
typeTable: TypeTable,
field: Boolean = false, field: Boolean = false,
synthetic: Boolean = false synthetic: Boolean = false
): MemberSignature? { ): MemberSignature? {
@@ -287,7 +288,7 @@ public abstract class AbstractBinaryClassAnnotationAndConstantLoader<A : Any, C
else return null else return null
if (field) { if (field) {
val (name, desc) = JvmProtoBufUtil.getJvmFieldSignature(proto, nameResolver) ?: return null val (name, desc) = JvmProtoBufUtil.getJvmFieldSignature(proto, nameResolver, typeTable) ?: return null
return MemberSignature.fromFieldNameAndDesc(name, desc) return MemberSignature.fromFieldNameAndDesc(name, desc)
} }
else if (synthetic && signature.hasSyntheticMethod()) { else if (synthetic && signature.hasSyntheticMethod()) {
@@ -297,20 +298,25 @@ public abstract class AbstractBinaryClassAnnotationAndConstantLoader<A : Any, C
return null return null
} }
private fun getCallableSignature(proto: MessageLite, nameResolver: NameResolver, kind: AnnotatedCallableKind): MemberSignature? { private fun getCallableSignature(
proto: MessageLite,
nameResolver: NameResolver,
typeTable: TypeTable,
kind: AnnotatedCallableKind
): MemberSignature? {
return when { return when {
proto is ProtoBuf.Constructor -> { proto is ProtoBuf.Constructor -> {
MemberSignature.fromMethodNameAndDesc(JvmProtoBufUtil.getJvmConstructorSignature(proto, nameResolver) ?: return null) MemberSignature.fromMethodNameAndDesc(JvmProtoBufUtil.getJvmConstructorSignature(proto, nameResolver, typeTable) ?: return null)
} }
proto is ProtoBuf.Function -> { proto is ProtoBuf.Function -> {
MemberSignature.fromMethodNameAndDesc(JvmProtoBufUtil.getJvmMethodSignature(proto, nameResolver) ?: return null) MemberSignature.fromMethodNameAndDesc(JvmProtoBufUtil.getJvmMethodSignature(proto, nameResolver, typeTable) ?: return null)
} }
proto is ProtoBuf.Property && proto.hasExtension(propertySignature) -> { proto is ProtoBuf.Property && proto.hasExtension(propertySignature) -> {
val signature = proto.getExtension(propertySignature) val signature = proto.getExtension(propertySignature)
when (kind) { when (kind) {
AnnotatedCallableKind.PROPERTY_GETTER -> MemberSignature.fromMethod(nameResolver, signature.getter) AnnotatedCallableKind.PROPERTY_GETTER -> MemberSignature.fromMethod(nameResolver, signature.getter)
AnnotatedCallableKind.PROPERTY_SETTER -> MemberSignature.fromMethod(nameResolver, signature.setter) AnnotatedCallableKind.PROPERTY_SETTER -> MemberSignature.fromMethod(nameResolver, signature.setter)
AnnotatedCallableKind.PROPERTY -> getPropertySignature(proto, nameResolver, true, true) AnnotatedCallableKind.PROPERTY -> getPropertySignature(proto, nameResolver, typeTable, true, true)
else -> null else -> null
} }
} }
@@ -25,7 +25,8 @@ import org.jetbrains.kotlin.resolve.jvm.JvmPrimitiveType
import org.jetbrains.kotlin.serialization.ClassData import org.jetbrains.kotlin.serialization.ClassData
import org.jetbrains.kotlin.serialization.PackageData import org.jetbrains.kotlin.serialization.PackageData
import org.jetbrains.kotlin.serialization.ProtoBuf import org.jetbrains.kotlin.serialization.ProtoBuf
import org.jetbrains.kotlin.serialization.deserialization.NameResolver import org.jetbrains.kotlin.serialization.deserialization.*
import org.jetbrains.kotlin.utils.singletonOrEmptyList
import java.io.ByteArrayInputStream import java.io.ByteArrayInputStream
public object JvmProtoBufUtil { public object JvmProtoBufUtil {
@@ -60,7 +61,11 @@ public object JvmProtoBufUtil {
} }
// returns JVM signature in the format: "equals(Ljava/lang/Object;)Z" // returns JVM signature in the format: "equals(Ljava/lang/Object;)Z"
fun getJvmMethodSignature(proto: ProtoBuf.FunctionOrBuilder, nameResolver: NameResolver): String? { fun getJvmMethodSignature(
proto: ProtoBuf.Function,
nameResolver: NameResolver,
typeTable: TypeTable
): String? {
val signature = val signature =
if (proto.hasExtension(JvmProtoBuf.methodSignature)) proto.getExtension(JvmProtoBuf.methodSignature) else null if (proto.hasExtension(JvmProtoBuf.methodSignature)) proto.getExtension(JvmProtoBuf.methodSignature) else null
val name = if (signature != null && signature.hasName()) signature.name else proto.name val name = if (signature != null && signature.hasName()) signature.name else proto.name
@@ -68,18 +73,21 @@ public object JvmProtoBufUtil {
nameResolver.getString(signature.desc) nameResolver.getString(signature.desc)
} }
else { else {
val parameterTypes = val parameterTypes = proto.receiverType(typeTable).singletonOrEmptyList() + proto.valueParameterList.map { it.type(typeTable) }
(if (proto.hasReceiverType()) listOf(proto.receiverType) else listOf()) + proto.valueParameterList.map { it.type }
val parametersDesc = parameterTypes.map { mapTypeDefault(it, nameResolver) ?: return null } val parametersDesc = parameterTypes.map { mapTypeDefault(it, nameResolver) ?: return null }
val returnTypeDesc = mapTypeDefault(proto.returnType, nameResolver) ?: return null val returnTypeDesc = mapTypeDefault(proto.returnType(typeTable), nameResolver) ?: return null
parametersDesc.joinToString(separator = "", prefix = "(", postfix = ")") + returnTypeDesc parametersDesc.joinToString(separator = "", prefix = "(", postfix = ")") + returnTypeDesc
} }
return nameResolver.getString(name) + desc return nameResolver.getString(name) + desc
} }
fun getJvmConstructorSignature(proto: ProtoBuf.ConstructorOrBuilder, nameResolver: NameResolver): String? { fun getJvmConstructorSignature(
proto: ProtoBuf.Constructor,
nameResolver: NameResolver,
typeTable: TypeTable
): String? {
val signature = val signature =
if (proto.hasExtension(JvmProtoBuf.constructorSignature)) proto.getExtension(JvmProtoBuf.constructorSignature) else null if (proto.hasExtension(JvmProtoBuf.constructorSignature)) proto.getExtension(JvmProtoBuf.constructorSignature) else null
val desc = if (signature != null && signature.hasDesc()) { val desc = if (signature != null && signature.hasDesc()) {
@@ -87,13 +95,17 @@ public object JvmProtoBufUtil {
} }
else { else {
proto.valueParameterList.map { proto.valueParameterList.map {
mapTypeDefault(it.type, nameResolver) ?: return null mapTypeDefault(it.type(typeTable), nameResolver) ?: return null
}.joinToString(separator = "", prefix = "(", postfix = ")V") }.joinToString(separator = "", prefix = "(", postfix = ")V")
} }
return "<init>" + desc return "<init>" + desc
} }
fun getJvmFieldSignature(proto: ProtoBuf.Property, nameResolver: NameResolver): PropertySignature? { fun getJvmFieldSignature(
proto: ProtoBuf.Property,
nameResolver: NameResolver,
typeTable: TypeTable
): PropertySignature? {
val signature = val signature =
if (proto.hasExtension(JvmProtoBuf.propertySignature)) proto.getExtension(JvmProtoBuf.propertySignature) else return null if (proto.hasExtension(JvmProtoBuf.propertySignature)) proto.getExtension(JvmProtoBuf.propertySignature) else return null
val field = val field =
@@ -102,7 +114,7 @@ public object JvmProtoBufUtil {
val name = if (field != null && field.hasName()) field.name else proto.name val name = if (field != null && field.hasName()) field.name else proto.name
val desc = val desc =
if (field != null && field.hasDesc()) nameResolver.getString(field.desc) if (field != null && field.hasDesc()) nameResolver.getString(field.desc)
else mapTypeDefault(proto.returnType, nameResolver) ?: return null else mapTypeDefault(proto.returnType(typeTable), nameResolver) ?: return null
return PropertySignature(nameResolver.getString(name), desc) return PropertySignature(nameResolver.getString(name), desc)
} }
+31 -4
View File
@@ -105,7 +105,10 @@ message Type {
} }
optional Projection projection = 1 [default = INV]; optional Projection projection = 1 [default = INV];
optional Type type = 2; // when projection is STAR, no type is written, otherwise type must be specified
// When projection is STAR, no type is written, otherwise type must be specified
optional Type type = 2;
optional int32 type_id = 3;
} }
repeated Argument argument = 2; repeated Argument argument = 2;
@@ -117,6 +120,7 @@ message Type {
optional int32 flexible_type_capabilities_id = 4 [(string_id_in_table) = true]; optional int32 flexible_type_capabilities_id = 4 [(string_id_in_table) = true];
optional Type flexible_upper_bound = 5; optional Type flexible_upper_bound = 5;
optional int32 flexible_upper_bound_id = 8;
// Only one of the following values should be present // Only one of the following values should be present
@@ -141,6 +145,7 @@ message TypeParameter {
optional Variance variance = 4 [default = INV]; optional Variance variance = 4 [default = INV];
repeated Type upper_bound = 5; repeated Type upper_bound = 5;
repeated int32 upper_bound_id = 6;
} }
message Class { message Class {
@@ -169,7 +174,9 @@ message Class {
optional int32 companion_object_name = 4 [(name_id_in_table) = true]; optional int32 companion_object_name = 4 [(name_id_in_table) = true];
repeated TypeParameter type_parameter = 5; repeated TypeParameter type_parameter = 5;
repeated Type supertype = 6; repeated Type supertype = 6;
repeated int32 supertype_id = 2 [packed = true];
repeated int32 nested_class_name = 7 [packed = true, (name_id_in_table) = true]; repeated int32 nested_class_name = 7 [packed = true, (name_id_in_table) = true];
@@ -179,6 +186,8 @@ message Class {
repeated int32 enum_entry = 12 [packed = true, (name_id_in_table) = true]; repeated int32 enum_entry = 12 [packed = true, (name_id_in_table) = true];
optional TypeTable type_table = 30;
extensions 100 to 199; extensions 100 to 199;
} }
@@ -186,9 +195,19 @@ message Package {
repeated Function function = 3; repeated Function function = 3;
repeated Property property = 4; repeated Property property = 4;
optional TypeTable type_table = 30;
extensions 100 to 199; extensions 100 to 199;
} }
message TypeTable {
repeated Type type = 1;
// Index starting from which all types are nullable, or nothing if all types in this table are non-null.
// Note that the 'nullable' field of Type messages is ignored and shouldn't be written because it wastes too much space
optional int32 first_nullable = 2 [default = -1];
}
message Constructor { message Constructor {
/* /*
hasAnnotations hasAnnotations
@@ -215,14 +234,18 @@ message Function {
required int32 name = 2 [(name_id_in_table) = true]; required int32 name = 2 [(name_id_in_table) = true];
required Type return_type = 3; optional Type return_type = 3;
optional int32 return_type_id = 7;
repeated TypeParameter type_parameter = 4; repeated TypeParameter type_parameter = 4;
optional Type receiver_type = 5; optional Type receiver_type = 5;
optional int32 receiver_type_id = 8;
repeated ValueParameter value_parameter = 6; repeated ValueParameter value_parameter = 6;
optional TypeTable type_table = 30;
extensions 100 to 199; extensions 100 to 199;
} }
@@ -243,11 +266,13 @@ message Property {
required int32 name = 2 [(name_id_in_table) = true]; required int32 name = 2 [(name_id_in_table) = true];
required Type return_type = 3; optional Type return_type = 3;
optional int32 return_type_id = 9;
repeated TypeParameter type_parameter = 4; repeated TypeParameter type_parameter = 4;
optional Type receiver_type = 5; optional Type receiver_type = 5;
optional int32 receiver_type_id = 10;
optional ValueParameter setter_value_parameter = 6; optional ValueParameter setter_value_parameter = 6;
@@ -272,9 +297,11 @@ message ValueParameter {
required int32 name = 2 [(name_id_in_table) = true]; required int32 name = 2 [(name_id_in_table) = true];
required Type type = 3; optional Type type = 3;
optional int32 type_id = 5;
optional Type vararg_element_type = 4; optional Type vararg_element_type = 4;
optional int32 vararg_element_type_id = 6;
extensions 100 to 199; extensions 100 to 199;
} }
File diff suppressed because it is too large Load Diff
@@ -56,7 +56,7 @@ public class ClassDeserializer(private val components: DeserializationComponents
if (!fragment.hasTopLevelClass(classId.shortClassName)) return null if (!fragment.hasTopLevelClass(classId.shortClassName)) return null
} }
components.createContext(fragment, nameResolver) components.createContext(fragment, nameResolver, TypeTable(classProto.typeTable))
} }
return DeserializedClassDescriptor(outerContext, classProto, nameResolver, sourceElement) return DeserializedClassDescriptor(outerContext, classProto, nameResolver, sourceElement)
@@ -42,25 +42,26 @@ public class MemberDeserializer(private val c: DeserializationContext) {
Flags.IS_VAR.get(flags), Flags.IS_VAR.get(flags),
c.nameResolver.getName(proto.getName()), c.nameResolver.getName(proto.getName()),
Deserialization.memberKind(Flags.MEMBER_KIND.get(flags)), Deserialization.memberKind(Flags.MEMBER_KIND.get(flags)),
Flags.IS_LATEINIT.get(flags),
Flags.IS_CONST.get(flags),
proto, proto,
c.nameResolver, c.nameResolver,
Flags.IS_LATEINIT.get(flags), c.typeTable
Flags.IS_CONST.get(flags)
) )
val local = c.childContext(property, proto.getTypeParameterList()) val local = c.childContext(property, proto.getTypeParameterList())
val hasGetter = Flags.HAS_GETTER.get(flags) val hasGetter = Flags.HAS_GETTER.get(flags)
val receiverAnnotations = if (hasGetter && proto.hasReceiverType()) val receiverAnnotations = if (hasGetter && (proto.hasReceiverType() || proto.hasReceiverTypeId()))
getReceiverParameterAnnotations(proto, AnnotatedCallableKind.PROPERTY_GETTER) getReceiverParameterAnnotations(proto, AnnotatedCallableKind.PROPERTY_GETTER)
else else
Annotations.EMPTY Annotations.EMPTY
property.setType( property.setType(
local.typeDeserializer.type(proto.getReturnType()), local.typeDeserializer.type(proto.returnType(c.typeTable)),
local.typeDeserializer.ownTypeParameters, local.typeDeserializer.ownTypeParameters,
getDispatchReceiverParameter(), getDispatchReceiverParameter(),
if (proto.hasReceiverType()) local.typeDeserializer.type(proto.getReceiverType(), receiverAnnotations) else null proto.receiverType(c.typeTable)?.let { local.typeDeserializer.type(it, receiverAnnotations) }
) )
val getter = if (hasGetter) { val getter = if (hasGetter) {
@@ -130,18 +131,18 @@ public class MemberDeserializer(private val c: DeserializationContext) {
} }
public fun loadFunction(proto: ProtoBuf.Function): FunctionDescriptor { public fun loadFunction(proto: ProtoBuf.Function): FunctionDescriptor {
val annotations = getAnnotations(proto, proto.getFlags(), AnnotatedCallableKind.FUNCTION) val annotations = getAnnotations(proto, proto.flags, AnnotatedCallableKind.FUNCTION)
val receiverAnnotations = if (proto.hasReceiverType()) val receiverAnnotations = if (proto.hasReceiverType() || proto.hasReceiverTypeId())
getReceiverParameterAnnotations(proto, AnnotatedCallableKind.FUNCTION) getReceiverParameterAnnotations(proto, AnnotatedCallableKind.FUNCTION)
else Annotations.EMPTY else Annotations.EMPTY
val function = DeserializedSimpleFunctionDescriptor.create(c.containingDeclaration, proto, c.nameResolver, annotations) val function = DeserializedSimpleFunctionDescriptor.create(c.containingDeclaration, annotations, proto, c.nameResolver, c.typeTable)
val local = c.childContext(function, proto.getTypeParameterList()) val local = c.childContext(function, proto.typeParameterList)
function.initialize( function.initialize(
if (proto.hasReceiverType()) local.typeDeserializer.type(proto.getReceiverType(), receiverAnnotations) else null, proto.receiverType(c.typeTable)?.let { local.typeDeserializer.type(it, receiverAnnotations) },
getDispatchReceiverParameter(), getDispatchReceiverParameter(),
local.typeDeserializer.ownTypeParameters, local.typeDeserializer.ownTypeParameters,
local.memberDeserializer.valueParameters(proto.valueParameterList, proto, AnnotatedCallableKind.FUNCTION), local.memberDeserializer.valueParameters(proto.valueParameterList, proto, AnnotatedCallableKind.FUNCTION),
local.typeDeserializer.type(proto.returnType), local.typeDeserializer.type(proto.returnType(c.typeTable)),
Deserialization.modality(Flags.MODALITY.get(proto.flags)), Deserialization.modality(Flags.MODALITY.get(proto.flags)),
Deserialization.visibility(Flags.VISIBILITY.get(proto.flags)) Deserialization.visibility(Flags.VISIBILITY.get(proto.flags))
) )
@@ -158,7 +159,7 @@ public class MemberDeserializer(private val c: DeserializationContext) {
val classDescriptor = c.containingDeclaration as ClassDescriptor val classDescriptor = c.containingDeclaration as ClassDescriptor
val descriptor = DeserializedConstructorDescriptor( val descriptor = DeserializedConstructorDescriptor(
classDescriptor, null, getAnnotations(proto, proto.flags, AnnotatedCallableKind.FUNCTION), classDescriptor, null, getAnnotations(proto, proto.flags, AnnotatedCallableKind.FUNCTION),
isPrimary, CallableMemberDescriptor.Kind.DECLARATION, proto, c.nameResolver isPrimary, CallableMemberDescriptor.Kind.DECLARATION, proto, c.nameResolver, c.typeTable
) )
val local = c.childContext(descriptor, listOf()) val local = c.childContext(descriptor, listOf())
descriptor.initialize( descriptor.initialize(
@@ -209,9 +210,9 @@ public class MemberDeserializer(private val c: DeserializationContext) {
callableDescriptor, null, i, callableDescriptor, null, i,
containerOfCallable?.let { getParameterAnnotations(it, callable, kind, i, proto) } ?: Annotations.EMPTY, containerOfCallable?.let { getParameterAnnotations(it, callable, kind, i, proto) } ?: Annotations.EMPTY,
c.nameResolver.getName(proto.name), c.nameResolver.getName(proto.name),
c.typeDeserializer.type(proto.type), c.typeDeserializer.type(proto.type(c.typeTable)),
Flags.DECLARES_DEFAULT_VALUE.get(flags), Flags.DECLARES_DEFAULT_VALUE.get(flags),
if (proto.hasVarargElementType()) c.typeDeserializer.type(proto.varargElementType) else null, proto.varargElementType(c.typeTable)?.let { c.typeDeserializer.type(it) },
SourceElement.NO_SOURCE SourceElement.NO_SOURCE
) )
}.toReadOnlyList() }.toReadOnlyList()
@@ -230,8 +231,8 @@ public class MemberDeserializer(private val c: DeserializationContext) {
} }
private fun DeclarationDescriptor.asProtoContainer(): ProtoContainer? = when(this) { private fun DeclarationDescriptor.asProtoContainer(): ProtoContainer? = when(this) {
is PackageFragmentDescriptor -> ProtoContainer(null, fqName, c.nameResolver) is PackageFragmentDescriptor -> ProtoContainer(null, fqName, c.nameResolver, c.typeTable)
is DeserializedClassDescriptor -> ProtoContainer(classProto, null, c.nameResolver) is DeserializedClassDescriptor -> ProtoContainer(classProto, null, c.nameResolver, c.typeTable)
else -> null // TODO: support annotations on lambdas and their parameters else -> null // TODO: support annotations on lambdas and their parameters
} }
} }
@@ -22,7 +22,8 @@ import org.jetbrains.kotlin.serialization.ProtoBuf
public data class ProtoContainer( public data class ProtoContainer(
val classProto: ProtoBuf.Class?, val classProto: ProtoBuf.Class?,
val packageFqName: FqName?, val packageFqName: FqName?,
val nameResolver: NameResolver val nameResolver: NameResolver,
val typeTable: TypeTable
) { ) {
init { init {
assert((classProto != null) xor (packageFqName != null)) assert((classProto != null) xor (packageFqName != null))
@@ -42,7 +42,7 @@ public class TypeDeserializer(
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.getId()] = DeserializedTypeParameterDescriptor(c, proto, index) result[proto.id] = DeserializedTypeParameterDescriptor(c, proto, index)
} }
result result
} }
@@ -51,18 +51,16 @@ public class TypeDeserializer(
val ownTypeParameters: List<TypeParameterDescriptor> val ownTypeParameters: List<TypeParameterDescriptor>
get() = typeParameterDescriptors().values().toReadOnlyList() get() = typeParameterDescriptors().values().toReadOnlyList()
// TODO: don't load identical types from TypeTable more than once
fun type(proto: ProtoBuf.Type, additionalAnnotations: Annotations = Annotations.EMPTY): JetType { fun type(proto: ProtoBuf.Type, additionalAnnotations: Annotations = Annotations.EMPTY): JetType {
if (proto.hasFlexibleTypeCapabilitiesId()) { if (proto.hasFlexibleTypeCapabilitiesId()) {
val id = c.nameResolver.getString(proto.getFlexibleTypeCapabilitiesId()) val id = c.nameResolver.getString(proto.flexibleTypeCapabilitiesId)
val capabilities = c.components.flexibleTypeCapabilitiesDeserializer.capabilitiesById(id) val capabilities = c.components.flexibleTypeCapabilitiesDeserializer.capabilitiesById(id) ?:
return ErrorUtils.createErrorType("${DeserializedType(c, proto)}: Capabilities not found for id $id")
if (capabilities == null) {
return ErrorUtils.createErrorType("${DeserializedType(c, proto)}: Capabilities not found for id $id")
}
return DelegatingFlexibleType.create( return DelegatingFlexibleType.create(
DeserializedType(c, proto), DeserializedType(c, proto),
DeserializedType(c, proto.getFlexibleUpperBound()), DeserializedType(c, proto.flexibleUpperBound(c.typeTable)!!),
capabilities capabilities
) )
} }
@@ -95,7 +93,7 @@ public class TypeDeserializer(
private fun computeClassDescriptor(fqNameIndex: Int): ClassDescriptor? { private fun computeClassDescriptor(fqNameIndex: Int): ClassDescriptor? {
val id = c.nameResolver.getClassId(fqNameIndex) val id = c.nameResolver.getClassId(fqNameIndex)
if (id.isLocal()) { if (id.isLocal) {
// Local classes can't be found in scopes // Local classes can't be found in scopes
return c.components.localClassResolver.resolveLocalClass(id) return c.components.localClassResolver.resolveLocalClass(id)
} }
@@ -103,12 +101,18 @@ public class TypeDeserializer(
} }
fun typeArgument(parameter: TypeParameterDescriptor?, typeArgumentProto: ProtoBuf.Type.Argument): TypeProjection { fun typeArgument(parameter: TypeParameterDescriptor?, typeArgumentProto: ProtoBuf.Type.Argument): TypeProjection {
return if (typeArgumentProto.getProjection() == ProtoBuf.Type.Argument.Projection.STAR) if (typeArgumentProto.projection == ProtoBuf.Type.Argument.Projection.STAR) {
if (parameter == null) return if (parameter == null)
TypeBasedStarProjectionImpl(c.components.moduleDescriptor.builtIns.getNullableAnyType()) TypeBasedStarProjectionImpl(c.components.moduleDescriptor.builtIns.nullableAnyType)
else else
StarProjectionImpl(parameter) StarProjectionImpl(parameter)
else TypeProjectionImpl(Deserialization.variance(typeArgumentProto.getProjection()), type(typeArgumentProto.getType())) }
val variance = Deserialization.variance(typeArgumentProto.projection)
val type = typeArgumentProto.type(c.typeTable) ?:
return TypeProjectionImpl(ErrorUtils.createErrorType("No type recorded"))
return TypeProjectionImpl(variance, type(type))
} }
override fun toString() = debugName + (if (parent == null) "" else ". Child of ${parent.debugName}") override fun toString() = debugName + (if (parent == null) "" else ". Child of ${parent.debugName}")
@@ -0,0 +1,37 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.serialization.deserialization
import org.jetbrains.kotlin.serialization.ProtoBuf
class TypeTable(typeTable: ProtoBuf.TypeTable) {
val types: List<ProtoBuf.Type> = run {
val originalTypes = typeTable.typeList
if (typeTable.hasFirstNullable()) {
val firstNullable = typeTable.firstNullable
typeTable.typeList.mapIndexed { i, type ->
if (i >= firstNullable) {
type.toBuilder().setNullable(true).build()
}
else type
}
}
else originalTypes
}
operator fun get(index: Int) = types[index]
}
@@ -24,7 +24,7 @@ import org.jetbrains.kotlin.resolve.constants.ConstantValue
import org.jetbrains.kotlin.serialization.ProtoBuf import org.jetbrains.kotlin.serialization.ProtoBuf
import org.jetbrains.kotlin.storage.StorageManager import org.jetbrains.kotlin.storage.StorageManager
public class DeserializationComponents( class DeserializationComponents(
val storageManager: StorageManager, val storageManager: StorageManager,
val moduleDescriptor: ModuleDescriptor, val moduleDescriptor: ModuleDescriptor,
val classDataFinder: ClassDataFinder, val classDataFinder: ClassDataFinder,
@@ -37,19 +37,24 @@ public class DeserializationComponents(
val typeCapabilitiesLoader: TypeCapabilitiesLoader = TypeCapabilitiesLoader.NONE, val typeCapabilitiesLoader: TypeCapabilitiesLoader = TypeCapabilitiesLoader.NONE,
val additionalSupertypes: AdditionalSupertypes = AdditionalSupertypes.None val additionalSupertypes: AdditionalSupertypes = AdditionalSupertypes.None
) { ) {
public val classDeserializer: ClassDeserializer = ClassDeserializer(this) val classDeserializer: ClassDeserializer = ClassDeserializer(this)
public fun deserializeClass(classId: ClassId): ClassDescriptor? = classDeserializer.deserializeClass(classId) fun deserializeClass(classId: ClassId): ClassDescriptor? = classDeserializer.deserializeClass(classId)
public fun createContext(descriptor: PackageFragmentDescriptor, nameResolver: NameResolver): DeserializationContext = fun createContext(
DeserializationContext(this, nameResolver, descriptor, parentTypeDeserializer = null, typeParameters = listOf()) descriptor: PackageFragmentDescriptor,
nameResolver: NameResolver,
typeTable: TypeTable
): DeserializationContext =
DeserializationContext(this, nameResolver, descriptor, typeTable, parentTypeDeserializer = null, typeParameters = listOf())
} }
public class DeserializationContext( class DeserializationContext(
public val components: DeserializationComponents, val components: DeserializationComponents,
public val nameResolver: NameResolver, val nameResolver: NameResolver,
public val containingDeclaration: DeclarationDescriptor, val containingDeclaration: DeclarationDescriptor,
val typeTable: TypeTable,
parentTypeDeserializer: TypeDeserializer?, parentTypeDeserializer: TypeDeserializer?,
typeParameters: List<ProtoBuf.TypeParameter> typeParameters: List<ProtoBuf.TypeParameter>
) { ) {
@@ -63,8 +68,11 @@ public class DeserializationContext(
fun childContext( fun childContext(
descriptor: DeclarationDescriptor, descriptor: DeclarationDescriptor,
typeParameterProtos: List<ProtoBuf.TypeParameter>, typeParameterProtos: List<ProtoBuf.TypeParameter>,
nameResolver: NameResolver = this.nameResolver nameResolver: NameResolver = this.nameResolver,
typeTable: TypeTable = this.typeTable
) = DeserializationContext( ) = DeserializationContext(
components, nameResolver, descriptor, parentTypeDeserializer = this.typeDeserializer, typeParameters = typeParameterProtos components, nameResolver, descriptor, typeTable,
parentTypeDeserializer = this.typeDeserializer,
typeParameters = typeParameterProtos
) )
} }
@@ -19,9 +19,12 @@ package org.jetbrains.kotlin.serialization.deserialization.descriptors
import com.google.protobuf.MessageLite import com.google.protobuf.MessageLite
import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor import org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
import org.jetbrains.kotlin.serialization.deserialization.NameResolver import org.jetbrains.kotlin.serialization.deserialization.NameResolver
import org.jetbrains.kotlin.serialization.deserialization.TypeTable
interface DeserializedCallableMemberDescriptor : CallableMemberDescriptor { interface DeserializedCallableMemberDescriptor : CallableMemberDescriptor {
val proto: MessageLite val proto: MessageLite
val nameResolver: NameResolver val nameResolver: NameResolver
val typeTable: TypeTable
} }
@@ -30,10 +30,7 @@ import org.jetbrains.kotlin.resolve.scopes.JetScope
import org.jetbrains.kotlin.resolve.scopes.StaticScopeForKotlinClass import org.jetbrains.kotlin.resolve.scopes.StaticScopeForKotlinClass
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.deserialization.Deserialization import org.jetbrains.kotlin.serialization.deserialization.*
import org.jetbrains.kotlin.serialization.deserialization.DeserializationContext
import org.jetbrains.kotlin.serialization.deserialization.DeserializedType
import org.jetbrains.kotlin.serialization.deserialization.NameResolver
import org.jetbrains.kotlin.types.AbstractClassTypeConstructor import org.jetbrains.kotlin.types.AbstractClassTypeConstructor
import org.jetbrains.kotlin.types.JetType import org.jetbrains.kotlin.types.JetType
import org.jetbrains.kotlin.types.upperIfFlexible import org.jetbrains.kotlin.types.upperIfFlexible
@@ -58,7 +55,7 @@ public class DeserializedClassDescriptor(
private val isCompanion = kindFromProto == ProtoBuf.Class.Kind.COMPANION_OBJECT private val isCompanion = kindFromProto == ProtoBuf.Class.Kind.COMPANION_OBJECT
private val isInner = Flags.IS_INNER.get(classProto.getFlags()) private val isInner = Flags.IS_INNER.get(classProto.getFlags())
val c = outerContext.childContext(this, classProto.getTypeParameterList(), nameResolver) val c = outerContext.childContext(this, classProto.typeParameterList, nameResolver, TypeTable(classProto.typeTable))
private val classId = nameResolver.getClassId(classProto.getFqName()) private val classId = nameResolver.getClassId(classProto.getFqName())
@@ -138,7 +135,7 @@ public class DeserializedClassDescriptor(
val result = ArrayList<JetType>(classProto.supertypeCount) val result = ArrayList<JetType>(classProto.supertypeCount)
val unresolved = ArrayList<DeserializedType>(0) val unresolved = ArrayList<DeserializedType>(0)
for (supertypeProto in classProto.supertypeList) { for (supertypeProto in classProto.supertypes(c.typeTable)) {
val supertype = c.typeDeserializer.type(supertypeProto) val supertype = c.typeDeserializer.type(supertypeProto)
if (supertype.isError) { if (supertype.isError) {
unresolved.add(supertype.upperIfFlexible() as? DeserializedType ?: error("Not a deserialized type: $supertype")) unresolved.add(supertype.upperIfFlexible() as? DeserializedType ?: error("Not a deserialized type: $supertype"))
@@ -21,6 +21,7 @@ import org.jetbrains.kotlin.descriptors.annotations.Annotations
import org.jetbrains.kotlin.descriptors.impl.ConstructorDescriptorImpl import org.jetbrains.kotlin.descriptors.impl.ConstructorDescriptorImpl
import org.jetbrains.kotlin.serialization.ProtoBuf import org.jetbrains.kotlin.serialization.ProtoBuf
import org.jetbrains.kotlin.serialization.deserialization.NameResolver import org.jetbrains.kotlin.serialization.deserialization.NameResolver
import org.jetbrains.kotlin.serialization.deserialization.TypeTable
public class DeserializedConstructorDescriptor( public class DeserializedConstructorDescriptor(
containingDeclaration: ClassDescriptor, containingDeclaration: ClassDescriptor,
@@ -29,7 +30,8 @@ public class DeserializedConstructorDescriptor(
isPrimary: Boolean, isPrimary: Boolean,
kind: CallableMemberDescriptor.Kind, kind: CallableMemberDescriptor.Kind,
override val proto: ProtoBuf.Constructor, override val proto: ProtoBuf.Constructor,
override val nameResolver: NameResolver override val nameResolver: NameResolver,
override val typeTable: TypeTable
) : ConstructorDescriptorImpl(containingDeclaration, original, annotations, isPrimary, kind, SourceElement.NO_SOURCE), ) : ConstructorDescriptorImpl(containingDeclaration, original, annotations, isPrimary, kind, SourceElement.NO_SOURCE),
DeserializedCallableMemberDescriptor { DeserializedCallableMemberDescriptor {
@@ -39,7 +41,8 @@ public class DeserializedConstructorDescriptor(
kind: CallableMemberDescriptor.Kind kind: CallableMemberDescriptor.Kind
): DeserializedConstructorDescriptor { ): DeserializedConstructorDescriptor {
return DeserializedConstructorDescriptor( return DeserializedConstructorDescriptor(
newOwner as ClassDescriptor, original as ConstructorDescriptor?, getAnnotations(), isPrimary, kind, proto, nameResolver newOwner as ClassDescriptor, original as ConstructorDescriptor?,
annotations, isPrimary, kind, proto, nameResolver, typeTable
) )
} }
} }
@@ -24,6 +24,7 @@ import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
import org.jetbrains.kotlin.resolve.scopes.JetScopeImpl import org.jetbrains.kotlin.resolve.scopes.JetScopeImpl
import org.jetbrains.kotlin.serialization.ProtoBuf import org.jetbrains.kotlin.serialization.ProtoBuf
import org.jetbrains.kotlin.serialization.deserialization.DeserializationContext import org.jetbrains.kotlin.serialization.deserialization.DeserializationContext
import org.jetbrains.kotlin.serialization.deserialization.receiverType
import org.jetbrains.kotlin.utils.Printer import org.jetbrains.kotlin.utils.Printer
import org.jetbrains.kotlin.utils.toReadOnlyList import org.jetbrains.kotlin.utils.toReadOnlyList
import java.util.* import java.util.*
@@ -38,11 +39,11 @@ public abstract class DeserializedMemberScope protected constructor(
private val functionProtos = private val functionProtos =
c.storageManager.createLazyValue { c.storageManager.createLazyValue {
groupByKey(filteredFunctionProtos(functionList), { it.name }) { it.hasReceiverType() } groupByKey(filteredFunctionProtos(functionList), { it.name }) { it.receiverType(c.typeTable) != null }
} }
private val propertyProtos = private val propertyProtos =
c.storageManager.createLazyValue { c.storageManager.createLazyValue {
groupByKey(filteredPropertyProtos(propertyList), { it.name }) { it.hasReceiverType() } groupByKey(filteredPropertyProtos(propertyList), { it.name }) { it.receiverType(c.typeTable) != null }
} }
private val functions = private val functions =
@@ -29,6 +29,7 @@ import org.jetbrains.kotlin.resolve.scopes.DescriptorKindFilter
import org.jetbrains.kotlin.serialization.ProtoBuf import org.jetbrains.kotlin.serialization.ProtoBuf
import org.jetbrains.kotlin.serialization.deserialization.DeserializationComponents import org.jetbrains.kotlin.serialization.deserialization.DeserializationComponents
import org.jetbrains.kotlin.serialization.deserialization.NameResolver import org.jetbrains.kotlin.serialization.deserialization.NameResolver
import org.jetbrains.kotlin.serialization.deserialization.TypeTable
import org.jetbrains.kotlin.storage.getValue import org.jetbrains.kotlin.storage.getValue
import org.jetbrains.kotlin.utils.addIfNotNull import org.jetbrains.kotlin.utils.addIfNotNull
@@ -38,7 +39,10 @@ public open class DeserializedPackageMemberScope(
nameResolver: NameResolver, nameResolver: NameResolver,
components: DeserializationComponents, components: DeserializationComponents,
classNames: () -> Collection<Name> classNames: () -> Collection<Name>
) : DeserializedMemberScope(components.createContext(packageDescriptor, nameResolver), proto.functionList, proto.propertyList) { ) : DeserializedMemberScope(
components.createContext(packageDescriptor, nameResolver, TypeTable(proto.typeTable)),
proto.functionList, proto.propertyList
) {
private val packageFqName = packageDescriptor.fqName private val packageFqName = packageDescriptor.fqName
internal val classNames by c.storageManager.createLazyValue { classNames().toSet() } internal val classNames by c.storageManager.createLazyValue { classNames().toSet() }
@@ -23,6 +23,7 @@ import org.jetbrains.kotlin.descriptors.impl.PropertyDescriptorImpl
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.serialization.ProtoBuf import org.jetbrains.kotlin.serialization.ProtoBuf
import org.jetbrains.kotlin.serialization.deserialization.NameResolver import org.jetbrains.kotlin.serialization.deserialization.NameResolver
import org.jetbrains.kotlin.serialization.deserialization.TypeTable
class DeserializedPropertyDescriptor( class DeserializedPropertyDescriptor(
containingDeclaration: DeclarationDescriptor, containingDeclaration: DeclarationDescriptor,
@@ -33,10 +34,11 @@ class DeserializedPropertyDescriptor(
isVar: Boolean, isVar: Boolean,
name: Name, name: Name,
kind: Kind, kind: Kind,
lateInit: Boolean,
isConst: Boolean,
override val proto: ProtoBuf.Property, override val proto: ProtoBuf.Property,
override val nameResolver: NameResolver, override val nameResolver: NameResolver,
lateInit: Boolean, override val typeTable: TypeTable
isConst: Boolean
) : DeserializedCallableMemberDescriptor, ) : DeserializedCallableMemberDescriptor,
PropertyDescriptorImpl(containingDeclaration, original, annotations, PropertyDescriptorImpl(containingDeclaration, original, annotations,
modality, visibility, isVar, name, kind, SourceElement.NO_SOURCE, lateInit, isConst) { modality, visibility, isVar, name, kind, SourceElement.NO_SOURCE, lateInit, isConst) {
@@ -49,6 +51,8 @@ class DeserializedPropertyDescriptor(
kind: Kind kind: Kind
): PropertyDescriptorImpl { ): PropertyDescriptorImpl {
return DeserializedPropertyDescriptor( return DeserializedPropertyDescriptor(
newOwner, original, annotations, newModality, newVisibility, isVar, name, kind, proto, nameResolver, isLateInit, isConst) newOwner, original, annotations, newModality, newVisibility, isVar, name, kind, isLateInit, isConst,
proto, nameResolver, typeTable
)
} }
} }
@@ -30,11 +30,13 @@ import org.jetbrains.kotlin.serialization.Flags;
import org.jetbrains.kotlin.serialization.ProtoBuf; import org.jetbrains.kotlin.serialization.ProtoBuf;
import org.jetbrains.kotlin.serialization.deserialization.Deserialization; import org.jetbrains.kotlin.serialization.deserialization.Deserialization;
import org.jetbrains.kotlin.serialization.deserialization.NameResolver; import org.jetbrains.kotlin.serialization.deserialization.NameResolver;
import org.jetbrains.kotlin.serialization.deserialization.TypeTable;
public class DeserializedSimpleFunctionDescriptor extends SimpleFunctionDescriptorImpl implements DeserializedCallableMemberDescriptor { public class DeserializedSimpleFunctionDescriptor extends SimpleFunctionDescriptorImpl implements DeserializedCallableMemberDescriptor {
private final ProtoBuf.Function proto; private final ProtoBuf.Function proto;
private final NameResolver nameResolver; private final NameResolver nameResolver;
private final TypeTable typeTable;
private DeserializedSimpleFunctionDescriptor( private DeserializedSimpleFunctionDescriptor(
@NotNull DeclarationDescriptor containingDeclaration, @NotNull DeclarationDescriptor containingDeclaration,
@@ -43,11 +45,13 @@ public class DeserializedSimpleFunctionDescriptor extends SimpleFunctionDescript
@NotNull Name name, @NotNull Name name,
@NotNull Kind kind, @NotNull Kind kind,
@NotNull ProtoBuf.Function proto, @NotNull ProtoBuf.Function proto,
@NotNull NameResolver nameResolver @NotNull NameResolver nameResolver,
@NotNull TypeTable typeTable
) { ) {
super(containingDeclaration, original, annotations, name, kind, SourceElement.NO_SOURCE); super(containingDeclaration, original, annotations, name, kind, SourceElement.NO_SOURCE);
this.proto = proto; this.proto = proto;
this.nameResolver = nameResolver; this.nameResolver = nameResolver;
this.typeTable = typeTable;
} }
@NotNull @NotNull
@@ -64,7 +68,8 @@ public class DeserializedSimpleFunctionDescriptor extends SimpleFunctionDescript
getName(), getName(),
kind, kind,
proto, proto,
nameResolver nameResolver,
typeTable
); );
} }
@@ -86,12 +91,19 @@ public class DeserializedSimpleFunctionDescriptor extends SimpleFunctionDescript
return nameResolver; return nameResolver;
} }
@NotNull
@Override
public TypeTable getTypeTable() {
return typeTable;
}
@NotNull @NotNull
public static DeserializedSimpleFunctionDescriptor create( public static DeserializedSimpleFunctionDescriptor create(
@NotNull DeclarationDescriptor containingDeclaration, @NotNull DeclarationDescriptor containingDeclaration,
@NotNull Annotations annotations,
@NotNull ProtoBuf.Function proto, @NotNull ProtoBuf.Function proto,
@NotNull NameResolver nameResolver, @NotNull NameResolver nameResolver,
@NotNull Annotations annotations @NotNull TypeTable typeTable
) { ) {
return new DeserializedSimpleFunctionDescriptor( return new DeserializedSimpleFunctionDescriptor(
containingDeclaration, containingDeclaration,
@@ -100,7 +112,8 @@ public class DeserializedSimpleFunctionDescriptor extends SimpleFunctionDescript
nameResolver.getName(proto.getName()), nameResolver.getName(proto.getName()),
Deserialization.memberKind(Flags.MEMBER_KIND.get(proto.getFlags())), Deserialization.memberKind(Flags.MEMBER_KIND.get(proto.getFlags())),
proto, proto,
nameResolver nameResolver,
typeTable
); );
} }
} }
@@ -21,13 +21,12 @@ import org.jetbrains.kotlin.descriptors.SourceElement;
import org.jetbrains.kotlin.descriptors.annotations.Annotations; import org.jetbrains.kotlin.descriptors.annotations.Annotations;
import org.jetbrains.kotlin.descriptors.impl.AbstractLazyTypeParameterDescriptor; import org.jetbrains.kotlin.descriptors.impl.AbstractLazyTypeParameterDescriptor;
import org.jetbrains.kotlin.serialization.ProtoBuf; import org.jetbrains.kotlin.serialization.ProtoBuf;
import org.jetbrains.kotlin.serialization.deserialization.Deserialization; import org.jetbrains.kotlin.serialization.deserialization.*;
import org.jetbrains.kotlin.serialization.deserialization.DeserializationContext;
import org.jetbrains.kotlin.serialization.deserialization.TypeDeserializer;
import org.jetbrains.kotlin.types.JetType; import org.jetbrains.kotlin.types.JetType;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedHashSet; import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set; import java.util.Set;
import static org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilsKt.getBuiltIns; import static org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilsKt.getBuiltIns;
@@ -35,6 +34,7 @@ import static org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilsKt.getB
public class DeserializedTypeParameterDescriptor extends AbstractLazyTypeParameterDescriptor { public class DeserializedTypeParameterDescriptor extends AbstractLazyTypeParameterDescriptor {
private final ProtoBuf.TypeParameter proto; private final ProtoBuf.TypeParameter proto;
private final TypeDeserializer typeDeserializer; private final TypeDeserializer typeDeserializer;
private final TypeTable typeTable;
public DeserializedTypeParameterDescriptor(@NotNull DeserializationContext c, @NotNull ProtoBuf.TypeParameter proto, int index) { public DeserializedTypeParameterDescriptor(@NotNull DeserializationContext c, @NotNull ProtoBuf.TypeParameter proto, int index) {
super(c.getStorageManager(), super(c.getStorageManager(),
@@ -46,16 +46,18 @@ public class DeserializedTypeParameterDescriptor extends AbstractLazyTypeParamet
SourceElement.NO_SOURCE); SourceElement.NO_SOURCE);
this.proto = proto; this.proto = proto;
this.typeDeserializer = c.getTypeDeserializer(); this.typeDeserializer = c.getTypeDeserializer();
this.typeTable = c.getTypeTable();
} }
@NotNull @NotNull
@Override @Override
protected Set<JetType> resolveUpperBounds() { protected Set<JetType> resolveUpperBounds() {
if (proto.getUpperBoundCount() == 0) { List<ProtoBuf.Type> upperBounds = ProtoTypeTableUtilKt.upperBounds(proto, typeTable);
if (upperBounds.isEmpty()) {
return Collections.singleton(getBuiltIns(this).getDefaultBound()); return Collections.singleton(getBuiltIns(this).getDefaultBound());
} }
Set<JetType> result = new LinkedHashSet<JetType>(proto.getUpperBoundCount()); Set<JetType> result = new LinkedHashSet<JetType>(upperBounds.size());
for (ProtoBuf.Type upperBound : proto.getUpperBoundList()) { for (ProtoBuf.Type upperBound : upperBounds) {
result.add(typeDeserializer.type(upperBound, Annotations.Companion.getEMPTY())); result.add(typeDeserializer.type(upperBound, Annotations.Companion.getEMPTY()));
} }
return result; return result;
@@ -0,0 +1,80 @@
/*
* Copyright 2010-2015 JetBrains s.r.o.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jetbrains.kotlin.serialization.deserialization
import org.jetbrains.kotlin.serialization.ProtoBuf
import org.jetbrains.kotlin.utils.ifEmpty
fun ProtoBuf.Class.supertypes(typeTable: TypeTable): List<ProtoBuf.Type> {
return supertypeList.ifEmpty { supertypeIdList.map { typeTable[it] } }
}
fun ProtoBuf.Type.Argument.type(typeTable: TypeTable): ProtoBuf.Type? {
return when {
hasType() -> type
hasTypeId() -> typeTable[typeId]
else -> null
}
}
fun ProtoBuf.Type.flexibleUpperBound(typeTable: TypeTable): ProtoBuf.Type? {
return when {
hasFlexibleUpperBound() -> flexibleUpperBound
hasFlexibleUpperBoundId() -> typeTable[flexibleUpperBoundId]
else -> null
}
}
fun ProtoBuf.TypeParameter.upperBounds(typeTable: TypeTable): List<ProtoBuf.Type> {
return upperBoundList.ifEmpty { upperBoundIdList.map { typeTable[it] } }
}
fun ProtoBuf.Function.returnType(typeTable: TypeTable): ProtoBuf.Type {
return if (hasReturnType()) returnType else typeTable[returnTypeId]
}
fun ProtoBuf.Function.receiverType(typeTable: TypeTable): ProtoBuf.Type? {
return when {
hasReceiverType() -> receiverType
hasReceiverTypeId() -> typeTable[receiverTypeId]
else -> null
}
}
fun ProtoBuf.Property.returnType(typeTable: TypeTable): ProtoBuf.Type {
return if (hasReturnType()) returnType else typeTable[returnTypeId]
}
fun ProtoBuf.Property.receiverType(typeTable: TypeTable): ProtoBuf.Type? {
return when {
hasReceiverType() -> receiverType
hasReceiverTypeId() -> typeTable[receiverTypeId]
else -> null
}
}
fun ProtoBuf.ValueParameter.type(typeTable: TypeTable): ProtoBuf.Type {
return if (hasType()) type else typeTable[typeId]
}
fun ProtoBuf.ValueParameter.varargElementType(typeTable: TypeTable): ProtoBuf.Type? {
return when {
hasVarargElementType() -> varargElementType
hasVarargElementTypeId() -> typeTable[varargElementTypeId]
else -> null
}
}
@@ -47,7 +47,7 @@ internal abstract class DescriptorBasedProperty<out R> protected constructor(
val jvmSignature = RuntimeTypeMapper.mapPropertySignature(descriptor) val jvmSignature = RuntimeTypeMapper.mapPropertySignature(descriptor)
when (jvmSignature) { when (jvmSignature) {
is KotlinProperty -> { is KotlinProperty -> {
JvmProtoBufUtil.getJvmFieldSignature(jvmSignature.proto, jvmSignature.nameResolver)?.let { JvmProtoBufUtil.getJvmFieldSignature(jvmSignature.proto, jvmSignature.nameResolver, jvmSignature.typeTable)?.let {
container.findFieldBySignature(jvmSignature.proto, jvmSignature.nameResolver, it.name) container.findFieldBySignature(jvmSignature.proto, jvmSignature.nameResolver, it.name)
} }
} }
@@ -37,6 +37,7 @@ import org.jetbrains.kotlin.resolve.descriptorUtil.classId
import org.jetbrains.kotlin.resolve.jvm.JvmPrimitiveType import org.jetbrains.kotlin.resolve.jvm.JvmPrimitiveType
import org.jetbrains.kotlin.serialization.ProtoBuf import org.jetbrains.kotlin.serialization.ProtoBuf
import org.jetbrains.kotlin.serialization.deserialization.NameResolver import org.jetbrains.kotlin.serialization.deserialization.NameResolver
import org.jetbrains.kotlin.serialization.deserialization.TypeTable
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedCallableMemberDescriptor import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedCallableMemberDescriptor
import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedPropertyDescriptor import org.jetbrains.kotlin.serialization.deserialization.descriptors.DeserializedPropertyDescriptor
import org.jetbrains.kotlin.serialization.jvm.JvmProtoBuf import org.jetbrains.kotlin.serialization.jvm.JvmProtoBuf
@@ -100,7 +101,8 @@ internal sealed class JvmPropertySignature {
val descriptor: PropertyDescriptor, val descriptor: PropertyDescriptor,
val proto: ProtoBuf.Property, val proto: ProtoBuf.Property,
val signature: JvmProtoBuf.JvmPropertySignature, val signature: JvmProtoBuf.JvmPropertySignature,
val nameResolver: NameResolver val nameResolver: NameResolver,
val typeTable: TypeTable
) : JvmPropertySignature() { ) : JvmPropertySignature() {
private val string: String private val string: String
@@ -110,7 +112,7 @@ internal sealed class JvmPropertySignature {
} }
else { else {
val (name, desc) = val (name, desc) =
JvmProtoBufUtil.getJvmFieldSignature(proto, nameResolver) ?: JvmProtoBufUtil.getJvmFieldSignature(proto, nameResolver, typeTable) ?:
throw KotlinReflectionInternalError("No field signature for property: $descriptor") throw KotlinReflectionInternalError("No field signature for property: $descriptor")
val moduleSuffix = val moduleSuffix =
@@ -154,12 +156,12 @@ internal object RuntimeTypeMapper {
val proto = function.proto val proto = function.proto
if (proto is ProtoBuf.Function) { if (proto is ProtoBuf.Function) {
JvmProtoBufUtil.getJvmMethodSignature(proto, function.nameResolver)?.let { signature -> JvmProtoBufUtil.getJvmMethodSignature(proto, function.nameResolver, function.typeTable)?.let { signature ->
return JvmFunctionSignature.KotlinFunction(signature) return JvmFunctionSignature.KotlinFunction(signature)
} }
} }
if (proto is ProtoBuf.Constructor) { if (proto is ProtoBuf.Constructor) {
JvmProtoBufUtil.getJvmConstructorSignature(proto, function.nameResolver)?.let { signature -> JvmProtoBufUtil.getJvmConstructorSignature(proto, function.nameResolver, function.typeTable)?.let { signature ->
return JvmFunctionSignature.KotlinConstructor(signature) return JvmFunctionSignature.KotlinConstructor(signature)
} }
} }
@@ -191,7 +193,7 @@ internal object RuntimeTypeMapper {
throw KotlinReflectionInternalError("No metadata found for $property") throw KotlinReflectionInternalError("No metadata found for $property")
} }
return JvmPropertySignature.KotlinProperty( return JvmPropertySignature.KotlinProperty(
property, proto, proto.getExtension(JvmProtoBuf.propertySignature), property.nameResolver property, proto, proto.getExtension(JvmProtoBuf.propertySignature), property.nameResolver, property.typeTable
) )
} }
else if (property is JavaPropertyDescriptor) { else if (property is JavaPropertyDescriptor) {
@@ -20,6 +20,7 @@ import org.jetbrains.kotlin.load.kotlin.JvmNameResolver
import org.jetbrains.kotlin.serialization.ProtoBuf import org.jetbrains.kotlin.serialization.ProtoBuf
import org.jetbrains.kotlin.serialization.deserialization.DeserializationContext import org.jetbrains.kotlin.serialization.deserialization.DeserializationContext
import org.jetbrains.kotlin.serialization.deserialization.MemberDeserializer import org.jetbrains.kotlin.serialization.deserialization.MemberDeserializer
import org.jetbrains.kotlin.serialization.deserialization.TypeTable
import org.jetbrains.kotlin.serialization.jvm.BitEncoding import org.jetbrains.kotlin.serialization.jvm.BitEncoding
import org.jetbrains.kotlin.serialization.jvm.JvmProtoBuf import org.jetbrains.kotlin.serialization.jvm.JvmProtoBuf
import org.jetbrains.kotlin.serialization.jvm.JvmProtoBufUtil import org.jetbrains.kotlin.serialization.jvm.JvmProtoBufUtil
@@ -43,8 +44,10 @@ public fun <R> Function<R>.reflect(): KFunction<R>? {
) )
val proto = ProtoBuf.Function.parseFrom(input, JvmProtoBufUtil.EXTENSION_REGISTRY) val proto = ProtoBuf.Function.parseFrom(input, JvmProtoBufUtil.EXTENSION_REGISTRY)
val moduleData = javaClass.getOrCreateModule() val moduleData = javaClass.getOrCreateModule()
val context = DeserializationContext(moduleData.deserialization, nameResolver, moduleData.module, val context = DeserializationContext(
parentTypeDeserializer = null, typeParameters = listOf()) moduleData.deserialization, nameResolver, moduleData.module,
typeTable = TypeTable(proto.typeTable), parentTypeDeserializer = null, typeParameters = listOf()
)
val descriptor = MemberDeserializer(context).loadFunction(proto) val descriptor = MemberDeserializer(context).loadFunction(proto)
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
return KFunctionImpl(EmptyContainerForLocal, descriptor) as KFunction<R> return KFunctionImpl(EmptyContainerForLocal, descriptor) as KFunction<R>
@@ -31,6 +31,8 @@ import org.jetbrains.kotlin.serialization.ProtoBuf.MemberKind
import org.jetbrains.kotlin.serialization.ProtoBuf.Modality import org.jetbrains.kotlin.serialization.ProtoBuf.Modality
import org.jetbrains.kotlin.serialization.deserialization.AnnotatedCallableKind import org.jetbrains.kotlin.serialization.deserialization.AnnotatedCallableKind
import org.jetbrains.kotlin.serialization.deserialization.ProtoContainer import org.jetbrains.kotlin.serialization.deserialization.ProtoContainer
import org.jetbrains.kotlin.serialization.deserialization.receiverType
import org.jetbrains.kotlin.serialization.deserialization.returnType
fun createCallableStubs( fun createCallableStubs(
parentStub: StubElement<out PsiElement>, parentStub: StubElement<out PsiElement>,
@@ -118,10 +120,10 @@ private class FunctionClsStubBuilder(
private val functionProto: ProtoBuf.Function private val functionProto: ProtoBuf.Function
) : CallableClsStubBuilder(parent, outerContext, protoContainer, functionProto.typeParameterList) { ) : CallableClsStubBuilder(parent, outerContext, protoContainer, functionProto.typeParameterList) {
override val receiverType: ProtoBuf.Type? override val receiverType: ProtoBuf.Type?
get() = if (functionProto.hasReceiverType()) functionProto.receiverType else null get() = functionProto.receiverType(c.typeTable)
override val returnType: ProtoBuf.Type? override val returnType: ProtoBuf.Type?
get() = if (functionProto.hasReturnType()) functionProto.returnType else null get() = functionProto.returnType(c.typeTable)
override fun createValueParameterList() { override fun createValueParameterList() {
typeStubBuilder.createValueParameterListStub(callableStub, functionProto, functionProto.valueParameterList, protoContainer) typeStubBuilder.createValueParameterListStub(callableStub, functionProto, functionProto.valueParameterList, protoContainer)
@@ -165,10 +167,10 @@ private class PropertyClsStubBuilder(
private val isVar = Flags.IS_VAR.get(propertyProto.flags) private val isVar = Flags.IS_VAR.get(propertyProto.flags)
override val receiverType: ProtoBuf.Type? override val receiverType: ProtoBuf.Type?
get() = if (propertyProto.hasReceiverType()) propertyProto.receiverType else null get() = propertyProto.receiverType(c.typeTable)
override val returnType: ProtoBuf.Type? override val returnType: ProtoBuf.Type?
get() = if (propertyProto.hasReturnType()) propertyProto.returnType else null get() = propertyProto.returnType(c.typeTable)
override fun createValueParameterList() { override fun createValueParameterList() {
} }
@@ -37,6 +37,8 @@ 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.deserialization.ProtoContainer import org.jetbrains.kotlin.serialization.deserialization.ProtoContainer
import org.jetbrains.kotlin.serialization.deserialization.TypeTable
import org.jetbrains.kotlin.serialization.deserialization.supertypes
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) {
ClassClsStubBuilder(parent, classProto, classId, context).build() ClassClsStubBuilder(parent, classProto, classId, context).build()
@@ -48,11 +50,11 @@ private class ClassClsStubBuilder(
private val classId: ClassId, private val classId: ClassId,
private val outerContext: ClsStubBuilderContext private val outerContext: ClsStubBuilderContext
) { ) {
private val c = outerContext.child(classProto.getTypeParameterList(), classId.getShortClassName()) private val c = outerContext.child(classProto.typeParameterList, classId.shortClassName, TypeTable(classProto.typeTable))
private val typeStubBuilder = TypeClsStubBuilder(c) private val typeStubBuilder = TypeClsStubBuilder(c)
private val classKind = Flags.CLASS_KIND[classProto.getFlags()] private val classKind = Flags.CLASS_KIND[classProto.flags]
private val supertypeIds = run { private val supertypeIds = run {
val supertypeIds = classProto.supertypeList.map { c.nameResolver.getClassId(it.className) } val supertypeIds = classProto.supertypes(c.typeTable).map { c.nameResolver.getClassId(it.className) }
//empty supertype list if single supertype is Any //empty supertype list if single supertype is Any
if (supertypeIds.singleOrNull()?.let { KotlinBuiltIns.isAny(it.asSingleFqName().toUnsafe()) } ?: false) { if (supertypeIds.singleOrNull()?.let { KotlinBuiltIns.isAny(it.asSingleFqName().toUnsafe()) } ?: false) {
listOf() listOf()
@@ -133,7 +135,7 @@ private class ClassClsStubBuilder(
val primaryConstructorProto = classProto.constructorList.find { !Flags.IS_SECONDARY.get(it.flags) } ?: return val primaryConstructorProto = classProto.constructorList.find { !Flags.IS_SECONDARY.get(it.flags) } ?: return
createConstructorStub(classOrObjectStub, primaryConstructorProto, c, ProtoContainer(classProto, null, c.nameResolver)) createConstructorStub(classOrObjectStub, primaryConstructorProto, c, ProtoContainer(classProto, null, c.nameResolver, c.typeTable))
} }
private fun createDelegationSpecifierList() { private fun createDelegationSpecifierList() {
@@ -143,7 +145,7 @@ private class ClassClsStubBuilder(
val delegationSpecifierListStub = val delegationSpecifierListStub =
KotlinPlaceHolderStubImpl<JetDelegationSpecifierList>(classOrObjectStub, JetStubElementTypes.DELEGATION_SPECIFIER_LIST) KotlinPlaceHolderStubImpl<JetDelegationSpecifierList>(classOrObjectStub, JetStubElementTypes.DELEGATION_SPECIFIER_LIST)
classProto.getSupertypeList().forEach { type -> classProto.supertypes(c.typeTable).forEach { type ->
val superClassStub = KotlinPlaceHolderStubImpl<JetDelegatorToSuperClass>( val superClassStub = KotlinPlaceHolderStubImpl<JetDelegatorToSuperClass>(
delegationSpecifierListStub, JetStubElementTypes.DELEGATOR_SUPER_CLASS delegationSpecifierListStub, JetStubElementTypes.DELEGATOR_SUPER_CLASS
) )
@@ -186,7 +188,7 @@ private class ClassClsStubBuilder(
} }
private fun createCallableMemberStubs(classBody: KotlinPlaceHolderStubImpl<JetClassBody>) { private fun createCallableMemberStubs(classBody: KotlinPlaceHolderStubImpl<JetClassBody>) {
val container = ProtoContainer(classProto, null, c.nameResolver) val container = ProtoContainer(classProto, null, c.nameResolver, c.typeTable)
for (secondaryConstructorProto in classProto.constructorList) { for (secondaryConstructorProto in classProto.constructorList) {
if (Flags.IS_SECONDARY.get(secondaryConstructorProto.flags)) { if (Flags.IS_SECONDARY.get(secondaryConstructorProto.flags)) {
@@ -216,6 +218,6 @@ private class ClassClsStubBuilder(
private fun createNestedClassStub(classBody: StubElement<out PsiElement>, nestedClassId: ClassId) { private fun createNestedClassStub(classBody: StubElement<out PsiElement>, nestedClassId: ClassId) {
val classDataWithSource = c.components.classDataFinder.findClassData(nestedClassId)!! val classDataWithSource = c.components.classDataFinder.findClassData(nestedClassId)!!
val (nameResolver, classProto) = classDataWithSource.classData val (nameResolver, classProto) = classDataWithSource.classData
createClassStub(classBody, classProto, nestedClassId, c.child(nameResolver)) createClassStub(classBody, classProto, nestedClassId, c.child(nameResolver, TypeTable(classProto.typeTable)))
} }
} }
@@ -25,10 +25,7 @@ import org.jetbrains.kotlin.name.ClassId
import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.name.Name import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.serialization.ProtoBuf import org.jetbrains.kotlin.serialization.ProtoBuf
import org.jetbrains.kotlin.serialization.deserialization.AnnotationAndConstantLoader import org.jetbrains.kotlin.serialization.deserialization.*
import org.jetbrains.kotlin.serialization.deserialization.ClassDataFinder
import org.jetbrains.kotlin.serialization.deserialization.ErrorReporter
import org.jetbrains.kotlin.serialization.deserialization.NameResolver
import org.jetbrains.kotlin.serialization.jvm.JvmProtoBuf import org.jetbrains.kotlin.serialization.jvm.JvmProtoBuf
import org.jetbrains.kotlin.storage.LockBasedStorageManager import org.jetbrains.kotlin.storage.LockBasedStorageManager
@@ -40,9 +37,10 @@ class ClsStubBuilderComponents(
) { ) {
fun createContext( fun createContext(
nameResolver: NameResolver, nameResolver: NameResolver,
packageFqName: FqName packageFqName: FqName,
typeTable: TypeTable
): ClsStubBuilderContext { ): ClsStubBuilderContext {
return ClsStubBuilderContext(this, nameResolver, packageFqName, EmptyTypeParameters) return ClsStubBuilderContext(this, nameResolver, packageFqName, EmptyTypeParameters, typeTable)
} }
} }
@@ -71,24 +69,31 @@ class ClsStubBuilderContext(
val components: ClsStubBuilderComponents, val components: ClsStubBuilderComponents,
val nameResolver: NameResolver, val nameResolver: NameResolver,
val containerFqName: FqName, val containerFqName: FqName,
val typeParameters: TypeParameters val typeParameters: TypeParameters,
val typeTable: TypeTable
) )
internal fun ClsStubBuilderContext.child(typeParameterList: List<ProtoBuf.TypeParameter>, name: Name? = null): ClsStubBuilderContext { internal fun ClsStubBuilderContext.child(
typeParameterList: List<ProtoBuf.TypeParameter>,
name: Name? = null,
typeTable: TypeTable = this.typeTable
): ClsStubBuilderContext {
return ClsStubBuilderContext( return ClsStubBuilderContext(
this.components, this.components,
this.nameResolver, this.nameResolver,
if (name != null) this.containerFqName.child(name) else this.containerFqName, if (name != null) this.containerFqName.child(name) else this.containerFqName,
this.typeParameters.child(nameResolver, typeParameterList) this.typeParameters.child(nameResolver, typeParameterList),
typeTable
) )
} }
internal fun ClsStubBuilderContext.child(nameResolver: NameResolver): ClsStubBuilderContext { internal fun ClsStubBuilderContext.child(nameResolver: NameResolver, typeTable: TypeTable): ClsStubBuilderContext {
return ClsStubBuilderContext( return ClsStubBuilderContext(
this.components, this.components,
nameResolver, nameResolver,
this.containerFqName, this.containerFqName,
this.typeParameters this.typeParameters,
typeTable
) )
} }
@@ -34,6 +34,7 @@ import org.jetbrains.kotlin.load.kotlin.header.isCompatibleMultifileClassKind
import org.jetbrains.kotlin.load.kotlin.header.isCompatiblePackageFacadeKind import org.jetbrains.kotlin.load.kotlin.header.isCompatiblePackageFacadeKind
import org.jetbrains.kotlin.name.FqName import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.JetFile import org.jetbrains.kotlin.psi.JetFile
import org.jetbrains.kotlin.serialization.deserialization.TypeTable
import org.jetbrains.kotlin.serialization.jvm.JvmProtoBufUtil import org.jetbrains.kotlin.serialization.jvm.JvmProtoBufUtil
public open class KotlinClsStubBuilder : ClsStubBuilder() { public open class KotlinClsStubBuilder : ClsStubBuilder() {
@@ -77,18 +78,18 @@ public open class KotlinClsStubBuilder : ClsStubBuilder() {
return when { return when {
header.isCompatiblePackageFacadeKind() -> { header.isCompatiblePackageFacadeKind() -> {
val (nameResolver, packageProto) = JvmProtoBufUtil.readPackageDataFrom(annotationData, strings) val (nameResolver, packageProto) = JvmProtoBufUtil.readPackageDataFrom(annotationData, strings)
val context = components.createContext(nameResolver, packageFqName) val context = components.createContext(nameResolver, packageFqName, TypeTable(packageProto.typeTable))
createPackageFacadeStub(packageProto, packageFqName, context) createPackageFacadeStub(packageProto, packageFqName, context)
} }
header.isCompatibleClassKind() -> { header.isCompatibleClassKind() -> {
if (header.isLocalClass) return null if (header.isLocalClass) return null
val (nameResolver, classProto) = JvmProtoBufUtil.readClassDataFrom(annotationData, strings) val (nameResolver, classProto) = JvmProtoBufUtil.readClassDataFrom(annotationData, strings)
val context = components.createContext(nameResolver, packageFqName) val context = components.createContext(nameResolver, packageFqName, TypeTable(classProto.typeTable))
createTopLevelClassStub(classId, classProto, context) createTopLevelClassStub(classId, classProto, context)
} }
header.isCompatibleFileFacadeKind() -> { header.isCompatibleFileFacadeKind() -> {
val (nameResolver, packageProto) = JvmProtoBufUtil.readPackageDataFrom(annotationData, strings) val (nameResolver, packageProto) = JvmProtoBufUtil.readPackageDataFrom(annotationData, strings)
val context = components.createContext(nameResolver, packageFqName) val context = components.createContext(nameResolver, packageFqName, TypeTable(packageProto.typeTable))
createFileFacadeStub(packageProto, classId.asSingleFqName(), context) createFileFacadeStub(packageProto, classId.asSingleFqName(), context)
} }
else -> throw IllegalStateException("Should have processed " + file.getPath() + " with header $header") else -> throw IllegalStateException("Should have processed " + file.getPath() + " with header $header")
@@ -31,6 +31,7 @@ import org.jetbrains.kotlin.name.FqName
import org.jetbrains.kotlin.psi.JetFile import org.jetbrains.kotlin.psi.JetFile
import org.jetbrains.kotlin.serialization.deserialization.NameResolver import org.jetbrains.kotlin.serialization.deserialization.NameResolver
import org.jetbrains.kotlin.serialization.deserialization.NameResolverImpl import org.jetbrains.kotlin.serialization.deserialization.NameResolverImpl
import org.jetbrains.kotlin.serialization.deserialization.TypeTable
import org.jetbrains.kotlin.serialization.js.KotlinJavascriptSerializedResourcePaths import org.jetbrains.kotlin.serialization.js.KotlinJavascriptSerializedResourcePaths
import org.jetbrains.kotlin.serialization.js.toClassData import org.jetbrains.kotlin.serialization.js.toClassData
import org.jetbrains.kotlin.serialization.js.toPackageData import org.jetbrains.kotlin.serialization.js.toPackageData
@@ -63,12 +64,14 @@ public open class KotlinJavaScriptStubBuilder : ClsStubBuilder() {
if (isPackageHeader) { if (isPackageHeader) {
val packageData = content.toPackageData(nameResolver) val packageData = content.toPackageData(nameResolver)
val context = components.createContext(packageData.nameResolver, packageFqName) val context = components.createContext(
packageData.nameResolver, packageFqName, TypeTable(packageData.packageProto.typeTable)
)
return createPackageFacadeStub(packageData.packageProto, packageFqName, context) return createPackageFacadeStub(packageData.packageProto, packageFqName, context)
} }
else { else {
val classData = content.toClassData(nameResolver) val classData = content.toClassData(nameResolver)
val context = components.createContext(classData.nameResolver, packageFqName) val context = components.createContext(classData.nameResolver, packageFqName, TypeTable(classData.classProto.typeTable))
val classId = JsMetaFileUtils.getClassId(file) val classId = JsMetaFileUtils.getClassId(file)
return createTopLevelClassStub(classId, classData.classProto, context) return createTopLevelClassStub(classId, classData.classProto, context)
} }
@@ -35,6 +35,9 @@ 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.type
import org.jetbrains.kotlin.serialization.deserialization.upperBounds
import org.jetbrains.kotlin.serialization.deserialization.varargElementType
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.* import java.util.*
@@ -98,12 +101,12 @@ class TypeClsStubBuilder(private val c: ClsStubBuilderContext) {
} }
val typeArgumentsListStub = KotlinPlaceHolderStubImpl<JetTypeArgumentList>(typeStub, JetStubElementTypes.TYPE_ARGUMENT_LIST) val typeArgumentsListStub = KotlinPlaceHolderStubImpl<JetTypeArgumentList>(typeStub, JetStubElementTypes.TYPE_ARGUMENT_LIST)
typeArgumentProtoList.forEach { typeArgumentProto -> typeArgumentProtoList.forEach { typeArgumentProto ->
val projectionKind = typeArgumentProto.getProjection().toProjectionKind() val projectionKind = typeArgumentProto.projection.toProjectionKind()
val typeProjection = KotlinTypeProjectionStubImpl(typeArgumentsListStub, projectionKind.ordinal()) val typeProjection = KotlinTypeProjectionStubImpl(typeArgumentsListStub, projectionKind.ordinal())
if (projectionKind != JetProjectionKind.STAR) { if (projectionKind != JetProjectionKind.STAR) {
val modifierKeywordToken = projectionKind.getToken() as? JetModifierKeywordToken val modifierKeywordToken = projectionKind.token as? JetModifierKeywordToken
createModifierListStub(typeProjection, modifierKeywordToken.singletonOrEmptyList()) createModifierListStub(typeProjection, modifierKeywordToken.singletonOrEmptyList())
createTypeReferenceStub(typeProjection, typeArgumentProto.getType()) createTypeReferenceStub(typeProjection, typeArgumentProto.type(c.typeTable)!!)
} }
} }
} }
@@ -116,12 +119,12 @@ class TypeClsStubBuilder(private val c: ClsStubBuilderContext) {
} }
private fun createFunctionTypeStub(parent: StubElement<out PsiElement>, type: Type, isExtensionFunctionType: Boolean) { private fun createFunctionTypeStub(parent: StubElement<out PsiElement>, type: Type, isExtensionFunctionType: Boolean) {
val typeArgumentList = type.getArgumentList() val typeArgumentList = type.argumentList
val functionType = KotlinPlaceHolderStubImpl<JetFunctionType>(parent, JetStubElementTypes.FUNCTION_TYPE) val functionType = KotlinPlaceHolderStubImpl<JetFunctionType>(parent, JetStubElementTypes.FUNCTION_TYPE)
if (isExtensionFunctionType) { if (isExtensionFunctionType) {
val functionTypeReceiverStub val functionTypeReceiverStub
= KotlinPlaceHolderStubImpl<JetFunctionTypeReceiver>(functionType, JetStubElementTypes.FUNCTION_TYPE_RECEIVER) = KotlinPlaceHolderStubImpl<JetFunctionTypeReceiver>(functionType, JetStubElementTypes.FUNCTION_TYPE_RECEIVER)
val receiverTypeProto = typeArgumentList.first().getType() val receiverTypeProto = typeArgumentList.first().type(c.typeTable)!!
createTypeReferenceStub(functionTypeReceiverStub, receiverTypeProto) createTypeReferenceStub(functionTypeReceiverStub, receiverTypeProto)
} }
@@ -129,11 +132,13 @@ class TypeClsStubBuilder(private val c: ClsStubBuilderContext) {
val typeArgumentsWithoutReceiverAndReturnType val typeArgumentsWithoutReceiverAndReturnType
= typeArgumentList.subList(if (isExtensionFunctionType) 1 else 0, typeArgumentList.size() - 1) = typeArgumentList.subList(if (isExtensionFunctionType) 1 else 0, typeArgumentList.size() - 1)
typeArgumentsWithoutReceiverAndReturnType.forEach { argument -> typeArgumentsWithoutReceiverAndReturnType.forEach { argument ->
val parameter = KotlinParameterStubImpl(parameterList, fqName = null, name = null, isMutable = false, hasValOrVar = false, hasDefaultValue = false) val parameter = KotlinParameterStubImpl(
createTypeReferenceStub(parameter, argument.getType()) parameterList, fqName = null, name = null, isMutable = false, hasValOrVar = false, hasDefaultValue = false
)
createTypeReferenceStub(parameter, argument.type(c.typeTable)!!)
} }
val returnType = typeArgumentList.last().getType() val returnType = typeArgumentList.last().type(c.typeTable)!!
createTypeReferenceStub(functionType, returnType) createTypeReferenceStub(functionType, returnType)
} }
@@ -154,8 +159,10 @@ class TypeClsStubBuilder(private val c: ClsStubBuilderContext) {
hasValOrVar = false, hasValOrVar = false,
isMutable = false isMutable = false
) )
val isVararg = valueParameterProto.hasVarargElementType() val varargElementType = valueParameterProto.varargElementType(c.typeTable)
val modifierList = if (isVararg) createModifierListStub(parameterStub, listOf(JetTokens.VARARG_KEYWORD)) else null val typeProto = varargElementType ?: valueParameterProto.type(c.typeTable)
val modifierList =
if (varargElementType != null) createModifierListStub(parameterStub, listOf(JetTokens.VARARG_KEYWORD)) else null
val parameterAnnotations = c.components.annotationLoader.loadValueParameterAnnotations( val parameterAnnotations = c.components.annotationLoader.loadValueParameterAnnotations(
container, callableProto, callableProto.annotatedCallableKind, index, valueParameterProto container, callableProto, callableProto.annotatedCallableKind, index, valueParameterProto
) )
@@ -163,7 +170,6 @@ class TypeClsStubBuilder(private val c: ClsStubBuilderContext) {
createAnnotationStubs(parameterAnnotations, modifierList ?: createEmptyModifierList(parameterStub)) createAnnotationStubs(parameterAnnotations, modifierList ?: createEmptyModifierList(parameterStub))
} }
val typeProto = if (isVararg) valueParameterProto.varargElementType else valueParameterProto.type
createTypeReferenceStub(parameterStub, typeProto) createTypeReferenceStub(parameterStub, typeProto)
} }
} }
@@ -185,21 +191,21 @@ class TypeClsStubBuilder(private val c: ClsStubBuilderContext) {
val typeParameterListStub = KotlinPlaceHolderStubImpl<JetTypeParameterList>(parent, JetStubElementTypes.TYPE_PARAMETER_LIST) val typeParameterListStub = KotlinPlaceHolderStubImpl<JetTypeParameterList>(parent, JetStubElementTypes.TYPE_PARAMETER_LIST)
val protosForTypeConstraintList = arrayListOf<Pair<Name, Type>>() val protosForTypeConstraintList = arrayListOf<Pair<Name, Type>>()
for (proto in typeParameterProtoList) { for (proto in typeParameterProtoList) {
val name = c.nameResolver.getName(proto.getName()) val name = c.nameResolver.getName(proto.name)
val typeParameterStub = KotlinTypeParameterStubImpl( val typeParameterStub = KotlinTypeParameterStubImpl(
typeParameterListStub, typeParameterListStub,
name = name.ref(), name = name.ref(),
isInVariance = proto.getVariance() == Variance.IN, isInVariance = proto.variance == Variance.IN,
isOutVariance = proto.getVariance() == Variance.OUT isOutVariance = proto.variance == Variance.OUT
) )
createTypeParameterModifierListStub(typeParameterStub, proto) createTypeParameterModifierListStub(typeParameterStub, proto)
val upperBoundProtos = proto.getUpperBoundList() val upperBoundProtos = proto.upperBounds(c.typeTable)
if (upperBoundProtos.isNotEmpty()) { if (upperBoundProtos.isNotEmpty()) {
val upperBound = upperBoundProtos.first() val upperBound = upperBoundProtos.first()
if (!upperBound.isDefaultUpperBound()) { if (!upperBound.isDefaultUpperBound()) {
createTypeReferenceStub(typeParameterStub, upperBound) createTypeReferenceStub(typeParameterStub, upperBound)
} }
protosForTypeConstraintList addAll upperBoundProtos.drop(1).map { Pair(name, it) } protosForTypeConstraintList.addAll(upperBoundProtos.drop(1).map { Pair(name, it) })
} }
} }
return protosForTypeConstraintList return protosForTypeConstraintList
@@ -36,6 +36,7 @@ import org.jetbrains.kotlin.serialization.Flags
import org.jetbrains.kotlin.serialization.ProtoBuf import org.jetbrains.kotlin.serialization.ProtoBuf
import org.jetbrains.kotlin.serialization.deserialization.AnnotatedCallableKind import org.jetbrains.kotlin.serialization.deserialization.AnnotatedCallableKind
import org.jetbrains.kotlin.serialization.deserialization.ProtoContainer import org.jetbrains.kotlin.serialization.deserialization.ProtoContainer
import org.jetbrains.kotlin.serialization.deserialization.TypeTable
import org.jetbrains.kotlin.serialization.jvm.JvmProtoBufUtil import org.jetbrains.kotlin.serialization.jvm.JvmProtoBufUtil
fun createTopLevelClassStub(classId: ClassId, classProto: ProtoBuf.Class, context: ClsStubBuilderContext): KotlinFileStubImpl { fun createTopLevelClassStub(classId: ClassId, classProto: ProtoBuf.Class, context: ClsStubBuilderContext): KotlinFileStubImpl {
@@ -51,7 +52,7 @@ fun createPackageFacadeStub(
): KotlinFileStubImpl { ): KotlinFileStubImpl {
val fileStub = KotlinFileStubForIde.forFile(packageFqName, packageFqName.isRoot) val fileStub = KotlinFileStubForIde.forFile(packageFqName, packageFqName.isRoot)
setupFileStub(fileStub, packageFqName) setupFileStub(fileStub, packageFqName)
createCallableStubs(fileStub, c, ProtoContainer(null, packageFqName, c.nameResolver), createCallableStubs(fileStub, c, ProtoContainer(null, packageFqName, c.nameResolver, c.typeTable),
packageProto.functionList, packageProto.propertyList) packageProto.functionList, packageProto.propertyList)
return fileStub return fileStub
} }
@@ -64,7 +65,7 @@ fun createFileFacadeStub(
val packageFqName = facadeFqName.parent() val packageFqName = facadeFqName.parent()
val fileStub = KotlinFileStubForIde.forFileFacadeStub(facadeFqName, packageFqName.isRoot) val fileStub = KotlinFileStubForIde.forFileFacadeStub(facadeFqName, packageFqName.isRoot)
setupFileStub(fileStub, packageFqName) setupFileStub(fileStub, packageFqName)
createCallableStubs(fileStub, c, ProtoContainer(null, packageFqName, c.nameResolver), createCallableStubs(fileStub, c, ProtoContainer(null, packageFqName, c.nameResolver, c.typeTable),
packageProto.functionList, packageProto.propertyList) packageProto.functionList, packageProto.propertyList)
return fileStub return fileStub
} }
@@ -82,8 +83,8 @@ fun createMultifileClassStub(
for (partFile in partFiles) { for (partFile in partFiles) {
val partHeader = partFile.classHeader val partHeader = partFile.classHeader
val (nameResolver, packageProto) = JvmProtoBufUtil.readPackageDataFrom(partHeader.annotationData!!, partHeader.strings!!) val (nameResolver, packageProto) = JvmProtoBufUtil.readPackageDataFrom(partHeader.annotationData!!, partHeader.strings!!)
val partContext = components.createContext(nameResolver, packageFqName) val partContext = components.createContext(nameResolver, packageFqName, TypeTable(packageProto.typeTable))
createCallableStubs(fileStub, partContext, ProtoContainer(null, packageFqName, partContext.nameResolver), createCallableStubs(fileStub, partContext, ProtoContainer(null, packageFqName, partContext.nameResolver, partContext.typeTable),
packageProto.functionList, packageProto.propertyList) packageProto.functionList, packageProto.propertyList)
} }
return fileStub return fileStub
@@ -39,11 +39,17 @@ open class ProtoCompareGenerated(public val oldNameResolver: NameResolver, publi
if (!checkEqualsPackageProperty(old, new)) return false if (!checkEqualsPackageProperty(old, new)) return false
if (old.hasTypeTable() != new.hasTypeTable()) return false
if (old.hasTypeTable()) {
if (!checkEquals(old.typeTable, new.typeTable)) return false
}
return true return true
} }
public enum class ProtoBufPackageKind { public enum class ProtoBufPackageKind {
FUNCTION_LIST, FUNCTION_LIST,
PROPERTY_LIST PROPERTY_LIST,
TYPE_TABLE
} }
public fun difference(old: ProtoBuf.Package, new: ProtoBuf.Package): EnumSet<ProtoBufPackageKind> { public fun difference(old: ProtoBuf.Package, new: ProtoBuf.Package): EnumSet<ProtoBufPackageKind> {
@@ -53,6 +59,11 @@ open class ProtoCompareGenerated(public val oldNameResolver: NameResolver, publi
if (!checkEqualsPackageProperty(old, new)) result.add(ProtoBufPackageKind.PROPERTY_LIST) if (!checkEqualsPackageProperty(old, new)) result.add(ProtoBufPackageKind.PROPERTY_LIST)
if (old.hasTypeTable() != new.hasTypeTable()) result.add(ProtoBufPackageKind.TYPE_TABLE)
if (old.hasTypeTable()) {
if (!checkEquals(old.typeTable, new.typeTable)) result.add(ProtoBufPackageKind.TYPE_TABLE)
}
return result return result
} }
@@ -73,6 +84,8 @@ open class ProtoCompareGenerated(public val oldNameResolver: NameResolver, publi
if (!checkEqualsClassSupertype(old, new)) return false if (!checkEqualsClassSupertype(old, new)) return false
if (!checkEqualsClassSupertypeId(old, new)) return false
if (!checkEqualsClassNestedClassName(old, new)) return false if (!checkEqualsClassNestedClassName(old, new)) return false
if (!checkEqualsClassConstructor(old, new)) return false if (!checkEqualsClassConstructor(old, new)) return false
@@ -83,6 +96,11 @@ open class ProtoCompareGenerated(public val oldNameResolver: NameResolver, publi
if (!checkEqualsClassEnumEntry(old, new)) return false if (!checkEqualsClassEnumEntry(old, new)) return false
if (old.hasTypeTable() != new.hasTypeTable()) return false
if (old.hasTypeTable()) {
if (!checkEquals(old.typeTable, new.typeTable)) return false
}
if (old.getExtensionCount(JvmProtoBuf.classAnnotation) != new.getExtensionCount(JvmProtoBuf.classAnnotation)) return false if (old.getExtensionCount(JvmProtoBuf.classAnnotation) != new.getExtensionCount(JvmProtoBuf.classAnnotation)) return false
for(i in 0..old.getExtensionCount(JvmProtoBuf.classAnnotation) - 1) { for(i in 0..old.getExtensionCount(JvmProtoBuf.classAnnotation) - 1) {
@@ -97,11 +115,13 @@ open class ProtoCompareGenerated(public val oldNameResolver: NameResolver, publi
COMPANION_OBJECT_NAME, COMPANION_OBJECT_NAME,
TYPE_PARAMETER_LIST, TYPE_PARAMETER_LIST,
SUPERTYPE_LIST, SUPERTYPE_LIST,
SUPERTYPE_ID_LIST,
NESTED_CLASS_NAME_LIST, NESTED_CLASS_NAME_LIST,
CONSTRUCTOR_LIST, CONSTRUCTOR_LIST,
FUNCTION_LIST, FUNCTION_LIST,
PROPERTY_LIST, PROPERTY_LIST,
ENUM_ENTRY_LIST, ENUM_ENTRY_LIST,
TYPE_TABLE,
CLASS_ANNOTATION_LIST CLASS_ANNOTATION_LIST
} }
@@ -124,6 +144,8 @@ open class ProtoCompareGenerated(public val oldNameResolver: NameResolver, publi
if (!checkEqualsClassSupertype(old, new)) result.add(ProtoBufClassKind.SUPERTYPE_LIST) if (!checkEqualsClassSupertype(old, new)) result.add(ProtoBufClassKind.SUPERTYPE_LIST)
if (!checkEqualsClassSupertypeId(old, new)) result.add(ProtoBufClassKind.SUPERTYPE_ID_LIST)
if (!checkEqualsClassNestedClassName(old, new)) result.add(ProtoBufClassKind.NESTED_CLASS_NAME_LIST) if (!checkEqualsClassNestedClassName(old, new)) result.add(ProtoBufClassKind.NESTED_CLASS_NAME_LIST)
if (!checkEqualsClassConstructor(old, new)) result.add(ProtoBufClassKind.CONSTRUCTOR_LIST) if (!checkEqualsClassConstructor(old, new)) result.add(ProtoBufClassKind.CONSTRUCTOR_LIST)
@@ -134,6 +156,11 @@ open class ProtoCompareGenerated(public val oldNameResolver: NameResolver, publi
if (!checkEqualsClassEnumEntry(old, new)) result.add(ProtoBufClassKind.ENUM_ENTRY_LIST) if (!checkEqualsClassEnumEntry(old, new)) result.add(ProtoBufClassKind.ENUM_ENTRY_LIST)
if (old.hasTypeTable() != new.hasTypeTable()) result.add(ProtoBufClassKind.TYPE_TABLE)
if (old.hasTypeTable()) {
if (!checkEquals(old.typeTable, new.typeTable)) result.add(ProtoBufClassKind.TYPE_TABLE)
}
if (old.getExtensionCount(JvmProtoBuf.classAnnotation) != new.getExtensionCount(JvmProtoBuf.classAnnotation)) result.add(ProtoBufClassKind.CLASS_ANNOTATION_LIST) if (old.getExtensionCount(JvmProtoBuf.classAnnotation) != new.getExtensionCount(JvmProtoBuf.classAnnotation)) result.add(ProtoBufClassKind.CLASS_ANNOTATION_LIST)
for(i in 0..old.getExtensionCount(JvmProtoBuf.classAnnotation) - 1) { for(i in 0..old.getExtensionCount(JvmProtoBuf.classAnnotation) - 1) {
@@ -151,7 +178,15 @@ open class ProtoCompareGenerated(public val oldNameResolver: NameResolver, publi
if (!checkStringEquals(old.name, new.name)) return false if (!checkStringEquals(old.name, new.name)) return false
if (!checkEquals(old.returnType, new.returnType)) return false if (old.hasReturnType() != new.hasReturnType()) return false
if (old.hasReturnType()) {
if (!checkEquals(old.returnType, new.returnType)) return false
}
if (old.hasReturnTypeId() != new.hasReturnTypeId()) return false
if (old.hasReturnTypeId()) {
if (old.returnTypeId != new.returnTypeId) return false
}
if (!checkEqualsFunctionTypeParameter(old, new)) return false if (!checkEqualsFunctionTypeParameter(old, new)) return false
@@ -160,8 +195,18 @@ open class ProtoCompareGenerated(public val oldNameResolver: NameResolver, publi
if (!checkEquals(old.receiverType, new.receiverType)) return false if (!checkEquals(old.receiverType, new.receiverType)) return false
} }
if (old.hasReceiverTypeId() != new.hasReceiverTypeId()) return false
if (old.hasReceiverTypeId()) {
if (old.receiverTypeId != new.receiverTypeId) return false
}
if (!checkEqualsFunctionValueParameter(old, new)) return false if (!checkEqualsFunctionValueParameter(old, new)) return false
if (old.hasTypeTable() != new.hasTypeTable()) return false
if (old.hasTypeTable()) {
if (!checkEquals(old.typeTable, new.typeTable)) return false
}
if (old.hasExtension(JvmProtoBuf.methodSignature) != new.hasExtension(JvmProtoBuf.methodSignature)) return false if (old.hasExtension(JvmProtoBuf.methodSignature) != new.hasExtension(JvmProtoBuf.methodSignature)) return false
if (old.hasExtension(JvmProtoBuf.methodSignature)) { if (old.hasExtension(JvmProtoBuf.methodSignature)) {
if (!checkEquals(old.getExtension(JvmProtoBuf.methodSignature), new.getExtension(JvmProtoBuf.methodSignature))) return false if (!checkEquals(old.getExtension(JvmProtoBuf.methodSignature), new.getExtension(JvmProtoBuf.methodSignature))) return false
@@ -183,7 +228,15 @@ open class ProtoCompareGenerated(public val oldNameResolver: NameResolver, publi
if (!checkStringEquals(old.name, new.name)) return false if (!checkStringEquals(old.name, new.name)) return false
if (!checkEquals(old.returnType, new.returnType)) return false if (old.hasReturnType() != new.hasReturnType()) return false
if (old.hasReturnType()) {
if (!checkEquals(old.returnType, new.returnType)) return false
}
if (old.hasReturnTypeId() != new.hasReturnTypeId()) return false
if (old.hasReturnTypeId()) {
if (old.returnTypeId != new.returnTypeId) return false
}
if (!checkEqualsPropertyTypeParameter(old, new)) return false if (!checkEqualsPropertyTypeParameter(old, new)) return false
@@ -192,6 +245,11 @@ open class ProtoCompareGenerated(public val oldNameResolver: NameResolver, publi
if (!checkEquals(old.receiverType, new.receiverType)) return false if (!checkEquals(old.receiverType, new.receiverType)) return false
} }
if (old.hasReceiverTypeId() != new.hasReceiverTypeId()) return false
if (old.hasReceiverTypeId()) {
if (old.receiverTypeId != new.receiverTypeId) return false
}
if (old.hasSetterValueParameter() != new.hasSetterValueParameter()) return false if (old.hasSetterValueParameter() != new.hasSetterValueParameter()) return false
if (old.hasSetterValueParameter()) { if (old.hasSetterValueParameter()) {
if (!checkEquals(old.setterValueParameter, new.setterValueParameter)) return false if (!checkEquals(old.setterValueParameter, new.setterValueParameter)) return false
@@ -220,6 +278,17 @@ open class ProtoCompareGenerated(public val oldNameResolver: NameResolver, publi
return true return true
} }
open fun checkEquals(old: ProtoBuf.TypeTable, new: ProtoBuf.TypeTable): Boolean {
if (!checkEqualsTypeTableType(old, new)) return false
if (old.hasFirstNullable() != new.hasFirstNullable()) return false
if (old.hasFirstNullable()) {
if (old.firstNullable != new.firstNullable) return false
}
return true
}
open fun checkEquals(old: ProtoBuf.TypeParameter, new: ProtoBuf.TypeParameter): Boolean { open fun checkEquals(old: ProtoBuf.TypeParameter, new: ProtoBuf.TypeParameter): Boolean {
if (old.id != new.id) return false if (old.id != new.id) return false
@@ -237,6 +306,8 @@ open class ProtoCompareGenerated(public val oldNameResolver: NameResolver, publi
if (!checkEqualsTypeParameterUpperBound(old, new)) return false if (!checkEqualsTypeParameterUpperBound(old, new)) return false
if (!checkEqualsTypeParameterUpperBoundId(old, new)) return false
return true return true
} }
@@ -258,6 +329,11 @@ open class ProtoCompareGenerated(public val oldNameResolver: NameResolver, publi
if (!checkEquals(old.flexibleUpperBound, new.flexibleUpperBound)) return false if (!checkEquals(old.flexibleUpperBound, new.flexibleUpperBound)) return false
} }
if (old.hasFlexibleUpperBoundId() != new.hasFlexibleUpperBoundId()) return false
if (old.hasFlexibleUpperBoundId()) {
if (old.flexibleUpperBoundId != new.flexibleUpperBoundId) return false
}
if (old.hasClassName() != new.hasClassName()) return false if (old.hasClassName() != new.hasClassName()) return false
if (old.hasClassName()) { if (old.hasClassName()) {
if (!checkClassIdEquals(old.className, new.className)) return false if (!checkClassIdEquals(old.className, new.className)) return false
@@ -314,13 +390,26 @@ open class ProtoCompareGenerated(public val oldNameResolver: NameResolver, publi
if (!checkStringEquals(old.name, new.name)) return false if (!checkStringEquals(old.name, new.name)) return false
if (!checkEquals(old.type, new.type)) return false if (old.hasType() != new.hasType()) return false
if (old.hasType()) {
if (!checkEquals(old.type, new.type)) return false
}
if (old.hasTypeId() != new.hasTypeId()) return false
if (old.hasTypeId()) {
if (old.typeId != new.typeId) return false
}
if (old.hasVarargElementType() != new.hasVarargElementType()) return false if (old.hasVarargElementType() != new.hasVarargElementType()) return false
if (old.hasVarargElementType()) { if (old.hasVarargElementType()) {
if (!checkEquals(old.varargElementType, new.varargElementType)) return false if (!checkEquals(old.varargElementType, new.varargElementType)) return false
} }
if (old.hasVarargElementTypeId() != new.hasVarargElementTypeId()) return false
if (old.hasVarargElementTypeId()) {
if (old.varargElementTypeId != new.varargElementTypeId) return false
}
if (old.hasExtension(JvmProtoBuf.index) != new.hasExtension(JvmProtoBuf.index)) return false if (old.hasExtension(JvmProtoBuf.index) != new.hasExtension(JvmProtoBuf.index)) return false
if (old.hasExtension(JvmProtoBuf.index)) { if (old.hasExtension(JvmProtoBuf.index)) {
if (old.getExtension(JvmProtoBuf.index) != new.getExtension(JvmProtoBuf.index)) return false if (old.getExtension(JvmProtoBuf.index) != new.getExtension(JvmProtoBuf.index)) return false
@@ -378,6 +467,11 @@ open class ProtoCompareGenerated(public val oldNameResolver: NameResolver, publi
if (!checkEquals(old.type, new.type)) return false if (!checkEquals(old.type, new.type)) return false
} }
if (old.hasTypeId() != new.hasTypeId()) return false
if (old.hasTypeId()) {
if (old.typeId != new.typeId) return false
}
return true return true
} }
@@ -494,6 +588,16 @@ open class ProtoCompareGenerated(public val oldNameResolver: NameResolver, publi
return true return true
} }
open fun checkEqualsClassSupertypeId(old: ProtoBuf.Class, new: ProtoBuf.Class): Boolean {
if (old.supertypeIdCount != new.supertypeIdCount) return false
for(i in 0..old.supertypeIdCount - 1) {
if (old.getSupertypeId(i) != new.getSupertypeId(i)) return false
}
return true
}
open fun checkEqualsClassNestedClassName(old: ProtoBuf.Class, new: ProtoBuf.Class): Boolean { open fun checkEqualsClassNestedClassName(old: ProtoBuf.Class, new: ProtoBuf.Class): Boolean {
if (old.nestedClassNameCount != new.nestedClassNameCount) return false if (old.nestedClassNameCount != new.nestedClassNameCount) return false
@@ -574,6 +678,16 @@ open class ProtoCompareGenerated(public val oldNameResolver: NameResolver, publi
return true return true
} }
open fun checkEqualsTypeTableType(old: ProtoBuf.TypeTable, new: ProtoBuf.TypeTable): Boolean {
if (old.typeCount != new.typeCount) return false
for(i in 0..old.typeCount - 1) {
if (!checkEquals(old.getType(i), new.getType(i))) return false
}
return true
}
open fun checkEqualsTypeParameterUpperBound(old: ProtoBuf.TypeParameter, new: ProtoBuf.TypeParameter): Boolean { open fun checkEqualsTypeParameterUpperBound(old: ProtoBuf.TypeParameter, new: ProtoBuf.TypeParameter): Boolean {
if (old.upperBoundCount != new.upperBoundCount) return false if (old.upperBoundCount != new.upperBoundCount) return false
@@ -584,6 +698,16 @@ open class ProtoCompareGenerated(public val oldNameResolver: NameResolver, publi
return true return true
} }
open fun checkEqualsTypeParameterUpperBoundId(old: ProtoBuf.TypeParameter, new: ProtoBuf.TypeParameter): Boolean {
if (old.upperBoundIdCount != new.upperBoundIdCount) return false
for(i in 0..old.upperBoundIdCount - 1) {
if (old.getUpperBoundId(i) != new.getUpperBoundId(i)) return false
}
return true
}
open fun checkEqualsTypeArgument(old: ProtoBuf.Type, new: ProtoBuf.Type): Boolean { open fun checkEqualsTypeArgument(old: ProtoBuf.Type, new: ProtoBuf.Type): Boolean {
if (old.argumentCount != new.argumentCount) return false if (old.argumentCount != new.argumentCount) return false
@@ -666,6 +790,10 @@ public fun ProtoBuf.Package.hashCode(stringIndexes: (Int) -> Int, fqNameIndexes:
hashCode = 31 * hashCode + getProperty(i).hashCode(stringIndexes, fqNameIndexes) hashCode = 31 * hashCode + getProperty(i).hashCode(stringIndexes, fqNameIndexes)
} }
if (hasTypeTable()) {
hashCode = 31 * hashCode + typeTable.hashCode(stringIndexes, fqNameIndexes)
}
return hashCode return hashCode
} }
@@ -690,6 +818,10 @@ public fun ProtoBuf.Class.hashCode(stringIndexes: (Int) -> Int, fqNameIndexes: (
hashCode = 31 * hashCode + getSupertype(i).hashCode(stringIndexes, fqNameIndexes) hashCode = 31 * hashCode + getSupertype(i).hashCode(stringIndexes, fqNameIndexes)
} }
for(i in 0..supertypeIdCount - 1) {
hashCode = 31 * hashCode + getSupertypeId(i)
}
for(i in 0..nestedClassNameCount - 1) { for(i in 0..nestedClassNameCount - 1) {
hashCode = 31 * hashCode + stringIndexes(getNestedClassName(i)) hashCode = 31 * hashCode + stringIndexes(getNestedClassName(i))
} }
@@ -710,6 +842,10 @@ public fun ProtoBuf.Class.hashCode(stringIndexes: (Int) -> Int, fqNameIndexes: (
hashCode = 31 * hashCode + stringIndexes(getEnumEntry(i)) hashCode = 31 * hashCode + stringIndexes(getEnumEntry(i))
} }
if (hasTypeTable()) {
hashCode = 31 * hashCode + typeTable.hashCode(stringIndexes, fqNameIndexes)
}
for(i in 0..getExtensionCount(JvmProtoBuf.classAnnotation) - 1) { for(i in 0..getExtensionCount(JvmProtoBuf.classAnnotation) - 1) {
hashCode = 31 * hashCode + getExtension(JvmProtoBuf.classAnnotation, i).hashCode(stringIndexes, fqNameIndexes) hashCode = 31 * hashCode + getExtension(JvmProtoBuf.classAnnotation, i).hashCode(stringIndexes, fqNameIndexes)
} }
@@ -726,7 +862,13 @@ public fun ProtoBuf.Function.hashCode(stringIndexes: (Int) -> Int, fqNameIndexes
hashCode = 31 * hashCode + stringIndexes(name) hashCode = 31 * hashCode + stringIndexes(name)
hashCode = 31 * hashCode + returnType.hashCode(stringIndexes, fqNameIndexes) if (hasReturnType()) {
hashCode = 31 * hashCode + returnType.hashCode(stringIndexes, fqNameIndexes)
}
if (hasReturnTypeId()) {
hashCode = 31 * hashCode + returnTypeId
}
for(i in 0..typeParameterCount - 1) { for(i in 0..typeParameterCount - 1) {
hashCode = 31 * hashCode + getTypeParameter(i).hashCode(stringIndexes, fqNameIndexes) hashCode = 31 * hashCode + getTypeParameter(i).hashCode(stringIndexes, fqNameIndexes)
@@ -736,10 +878,18 @@ public fun ProtoBuf.Function.hashCode(stringIndexes: (Int) -> Int, fqNameIndexes
hashCode = 31 * hashCode + receiverType.hashCode(stringIndexes, fqNameIndexes) hashCode = 31 * hashCode + receiverType.hashCode(stringIndexes, fqNameIndexes)
} }
if (hasReceiverTypeId()) {
hashCode = 31 * hashCode + receiverTypeId
}
for(i in 0..valueParameterCount - 1) { for(i in 0..valueParameterCount - 1) {
hashCode = 31 * hashCode + getValueParameter(i).hashCode(stringIndexes, fqNameIndexes) hashCode = 31 * hashCode + getValueParameter(i).hashCode(stringIndexes, fqNameIndexes)
} }
if (hasTypeTable()) {
hashCode = 31 * hashCode + typeTable.hashCode(stringIndexes, fqNameIndexes)
}
if (hasExtension(JvmProtoBuf.methodSignature)) { if (hasExtension(JvmProtoBuf.methodSignature)) {
hashCode = 31 * hashCode + getExtension(JvmProtoBuf.methodSignature).hashCode(stringIndexes, fqNameIndexes) hashCode = 31 * hashCode + getExtension(JvmProtoBuf.methodSignature).hashCode(stringIndexes, fqNameIndexes)
} }
@@ -760,7 +910,13 @@ public fun ProtoBuf.Property.hashCode(stringIndexes: (Int) -> Int, fqNameIndexes
hashCode = 31 * hashCode + stringIndexes(name) hashCode = 31 * hashCode + stringIndexes(name)
hashCode = 31 * hashCode + returnType.hashCode(stringIndexes, fqNameIndexes) if (hasReturnType()) {
hashCode = 31 * hashCode + returnType.hashCode(stringIndexes, fqNameIndexes)
}
if (hasReturnTypeId()) {
hashCode = 31 * hashCode + returnTypeId
}
for(i in 0..typeParameterCount - 1) { for(i in 0..typeParameterCount - 1) {
hashCode = 31 * hashCode + getTypeParameter(i).hashCode(stringIndexes, fqNameIndexes) hashCode = 31 * hashCode + getTypeParameter(i).hashCode(stringIndexes, fqNameIndexes)
@@ -770,6 +926,10 @@ public fun ProtoBuf.Property.hashCode(stringIndexes: (Int) -> Int, fqNameIndexes
hashCode = 31 * hashCode + receiverType.hashCode(stringIndexes, fqNameIndexes) hashCode = 31 * hashCode + receiverType.hashCode(stringIndexes, fqNameIndexes)
} }
if (hasReceiverTypeId()) {
hashCode = 31 * hashCode + receiverTypeId
}
if (hasSetterValueParameter()) { if (hasSetterValueParameter()) {
hashCode = 31 * hashCode + setterValueParameter.hashCode(stringIndexes, fqNameIndexes) hashCode = 31 * hashCode + setterValueParameter.hashCode(stringIndexes, fqNameIndexes)
} }
@@ -793,6 +953,20 @@ public fun ProtoBuf.Property.hashCode(stringIndexes: (Int) -> Int, fqNameIndexes
return hashCode return hashCode
} }
public fun ProtoBuf.TypeTable.hashCode(stringIndexes: (Int) -> Int, fqNameIndexes: (Int) -> Int): Int {
var hashCode = 1
for(i in 0..typeCount - 1) {
hashCode = 31 * hashCode + getType(i).hashCode(stringIndexes, fqNameIndexes)
}
if (hasFirstNullable()) {
hashCode = 31 * hashCode + firstNullable
}
return hashCode
}
public fun ProtoBuf.TypeParameter.hashCode(stringIndexes: (Int) -> Int, fqNameIndexes: (Int) -> Int): Int { public fun ProtoBuf.TypeParameter.hashCode(stringIndexes: (Int) -> Int, fqNameIndexes: (Int) -> Int): Int {
var hashCode = 1 var hashCode = 1
@@ -812,6 +986,10 @@ public fun ProtoBuf.TypeParameter.hashCode(stringIndexes: (Int) -> Int, fqNameIn
hashCode = 31 * hashCode + getUpperBound(i).hashCode(stringIndexes, fqNameIndexes) hashCode = 31 * hashCode + getUpperBound(i).hashCode(stringIndexes, fqNameIndexes)
} }
for(i in 0..upperBoundIdCount - 1) {
hashCode = 31 * hashCode + getUpperBoundId(i)
}
return hashCode return hashCode
} }
@@ -834,6 +1012,10 @@ public fun ProtoBuf.Type.hashCode(stringIndexes: (Int) -> Int, fqNameIndexes: (I
hashCode = 31 * hashCode + flexibleUpperBound.hashCode(stringIndexes, fqNameIndexes) hashCode = 31 * hashCode + flexibleUpperBound.hashCode(stringIndexes, fqNameIndexes)
} }
if (hasFlexibleUpperBoundId()) {
hashCode = 31 * hashCode + flexibleUpperBoundId
}
if (hasClassName()) { if (hasClassName()) {
hashCode = 31 * hashCode + fqNameIndexes(className) hashCode = 31 * hashCode + fqNameIndexes(className)
} }
@@ -892,12 +1074,22 @@ public fun ProtoBuf.ValueParameter.hashCode(stringIndexes: (Int) -> Int, fqNameI
hashCode = 31 * hashCode + stringIndexes(name) hashCode = 31 * hashCode + stringIndexes(name)
hashCode = 31 * hashCode + type.hashCode(stringIndexes, fqNameIndexes) if (hasType()) {
hashCode = 31 * hashCode + type.hashCode(stringIndexes, fqNameIndexes)
}
if (hasTypeId()) {
hashCode = 31 * hashCode + typeId
}
if (hasVarargElementType()) { if (hasVarargElementType()) {
hashCode = 31 * hashCode + varargElementType.hashCode(stringIndexes, fqNameIndexes) hashCode = 31 * hashCode + varargElementType.hashCode(stringIndexes, fqNameIndexes)
} }
if (hasVarargElementTypeId()) {
hashCode = 31 * hashCode + varargElementTypeId
}
if (hasExtension(JvmProtoBuf.index)) { if (hasExtension(JvmProtoBuf.index)) {
hashCode = 31 * hashCode + getExtension(JvmProtoBuf.index) hashCode = 31 * hashCode + getExtension(JvmProtoBuf.index)
} }
@@ -952,6 +1144,10 @@ public fun ProtoBuf.Type.Argument.hashCode(stringIndexes: (Int) -> Int, fqNameIn
hashCode = 31 * hashCode + type.hashCode(stringIndexes, fqNameIndexes) hashCode = 31 * hashCode + type.hashCode(stringIndexes, fqNameIndexes)
} }
if (hasTypeId()) {
hashCode = 31 * hashCode + typeId
}
return hashCode return hashCode
} }
@@ -210,6 +210,9 @@ private class DifferenceCalculatorForClass(oldData: ProtoMapValue, newData: Prot
names.addAll(calcDifferenceForNonPrivateMembers(ProtoBuf.Class::getPropertyList)) names.addAll(calcDifferenceForNonPrivateMembers(ProtoBuf.Class::getPropertyList))
ProtoBufClassKind.ENUM_ENTRY_LIST -> ProtoBufClassKind.ENUM_ENTRY_LIST ->
names.addAll(calcDifferenceForNames(oldProto.enumEntryList, newProto.enumEntryList)) names.addAll(calcDifferenceForNames(oldProto.enumEntryList, newProto.enumEntryList))
ProtoBufClassKind.TYPE_TABLE -> {
// TODO
}
ProtoBufClassKind.FLAGS, ProtoBufClassKind.FLAGS,
ProtoBufClassKind.FQ_NAME, ProtoBufClassKind.FQ_NAME,
ProtoBufClassKind.TYPE_PARAMETER_LIST, ProtoBufClassKind.TYPE_PARAMETER_LIST,
@@ -257,6 +260,9 @@ private class DifferenceCalculatorForPackageFacade(oldData: ProtoMapValue, newDa
names.addAll(calcDifferenceForNonPrivateMembers(ProtoBuf.Package::getFunctionList)) names.addAll(calcDifferenceForNonPrivateMembers(ProtoBuf.Package::getFunctionList))
ProtoBufPackageKind.PROPERTY_LIST -> ProtoBufPackageKind.PROPERTY_LIST ->
names.addAll(calcDifferenceForNonPrivateMembers(ProtoBuf.Package::getPropertyList)) names.addAll(calcDifferenceForNonPrivateMembers(ProtoBuf.Package::getPropertyList))
ProtoBufPackageKind.TYPE_TABLE -> {
// TODO
}
else -> else ->
throw IllegalArgumentException("Unsupported kind: $kind") throw IllegalArgumentException("Unsupported kind: $kind")
} }