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
+31 -4
View File
@@ -105,7 +105,10 @@ message Type {
}
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;
@@ -117,6 +120,7 @@ message Type {
optional int32 flexible_type_capabilities_id = 4 [(string_id_in_table) = true];
optional Type flexible_upper_bound = 5;
optional int32 flexible_upper_bound_id = 8;
// Only one of the following values should be present
@@ -141,6 +145,7 @@ message TypeParameter {
optional Variance variance = 4 [default = INV];
repeated Type upper_bound = 5;
repeated int32 upper_bound_id = 6;
}
message Class {
@@ -169,7 +174,9 @@ message Class {
optional int32 companion_object_name = 4 [(name_id_in_table) = true];
repeated TypeParameter type_parameter = 5;
repeated Type supertype = 6;
repeated int32 supertype_id = 2 [packed = 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];
optional TypeTable type_table = 30;
extensions 100 to 199;
}
@@ -186,9 +195,19 @@ message Package {
repeated Function function = 3;
repeated Property property = 4;
optional TypeTable type_table = 30;
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 {
/*
hasAnnotations
@@ -215,14 +234,18 @@ message Function {
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;
optional Type receiver_type = 5;
optional int32 receiver_type_id = 8;
repeated ValueParameter value_parameter = 6;
optional TypeTable type_table = 30;
extensions 100 to 199;
}
@@ -243,11 +266,13 @@ message Property {
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;
optional Type receiver_type = 5;
optional int32 receiver_type_id = 10;
optional ValueParameter setter_value_parameter = 6;
@@ -272,9 +297,11 @@ message ValueParameter {
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 int32 vararg_element_type_id = 6;
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
}
components.createContext(fragment, nameResolver)
components.createContext(fragment, nameResolver, TypeTable(classProto.typeTable))
}
return DeserializedClassDescriptor(outerContext, classProto, nameResolver, sourceElement)
@@ -42,25 +42,26 @@ public class MemberDeserializer(private val c: DeserializationContext) {
Flags.IS_VAR.get(flags),
c.nameResolver.getName(proto.getName()),
Deserialization.memberKind(Flags.MEMBER_KIND.get(flags)),
Flags.IS_LATEINIT.get(flags),
Flags.IS_CONST.get(flags),
proto,
c.nameResolver,
Flags.IS_LATEINIT.get(flags),
Flags.IS_CONST.get(flags)
c.typeTable
)
val local = c.childContext(property, proto.getTypeParameterList())
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)
else
Annotations.EMPTY
property.setType(
local.typeDeserializer.type(proto.getReturnType()),
local.typeDeserializer.type(proto.returnType(c.typeTable)),
local.typeDeserializer.ownTypeParameters,
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) {
@@ -130,18 +131,18 @@ public class MemberDeserializer(private val c: DeserializationContext) {
}
public fun loadFunction(proto: ProtoBuf.Function): FunctionDescriptor {
val annotations = getAnnotations(proto, proto.getFlags(), AnnotatedCallableKind.FUNCTION)
val receiverAnnotations = if (proto.hasReceiverType())
val annotations = getAnnotations(proto, proto.flags, AnnotatedCallableKind.FUNCTION)
val receiverAnnotations = if (proto.hasReceiverType() || proto.hasReceiverTypeId())
getReceiverParameterAnnotations(proto, AnnotatedCallableKind.FUNCTION)
else Annotations.EMPTY
val function = DeserializedSimpleFunctionDescriptor.create(c.containingDeclaration, proto, c.nameResolver, annotations)
val local = c.childContext(function, proto.getTypeParameterList())
val function = DeserializedSimpleFunctionDescriptor.create(c.containingDeclaration, annotations, proto, c.nameResolver, c.typeTable)
val local = c.childContext(function, proto.typeParameterList)
function.initialize(
if (proto.hasReceiverType()) local.typeDeserializer.type(proto.getReceiverType(), receiverAnnotations) else null,
proto.receiverType(c.typeTable)?.let { local.typeDeserializer.type(it, receiverAnnotations) },
getDispatchReceiverParameter(),
local.typeDeserializer.ownTypeParameters,
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.visibility(Flags.VISIBILITY.get(proto.flags))
)
@@ -158,7 +159,7 @@ public class MemberDeserializer(private val c: DeserializationContext) {
val classDescriptor = c.containingDeclaration as ClassDescriptor
val descriptor = DeserializedConstructorDescriptor(
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())
descriptor.initialize(
@@ -209,9 +210,9 @@ public class MemberDeserializer(private val c: DeserializationContext) {
callableDescriptor, null, i,
containerOfCallable?.let { getParameterAnnotations(it, callable, kind, i, proto) } ?: Annotations.EMPTY,
c.nameResolver.getName(proto.name),
c.typeDeserializer.type(proto.type),
c.typeDeserializer.type(proto.type(c.typeTable)),
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
)
}.toReadOnlyList()
@@ -230,8 +231,8 @@ public class MemberDeserializer(private val c: DeserializationContext) {
}
private fun DeclarationDescriptor.asProtoContainer(): ProtoContainer? = when(this) {
is PackageFragmentDescriptor -> ProtoContainer(null, fqName, c.nameResolver)
is DeserializedClassDescriptor -> ProtoContainer(classProto, null, c.nameResolver)
is PackageFragmentDescriptor -> ProtoContainer(null, fqName, c.nameResolver, c.typeTable)
is DeserializedClassDescriptor -> ProtoContainer(classProto, null, c.nameResolver, c.typeTable)
else -> null // TODO: support annotations on lambdas and their parameters
}
}
@@ -22,7 +22,8 @@ import org.jetbrains.kotlin.serialization.ProtoBuf
public data class ProtoContainer(
val classProto: ProtoBuf.Class?,
val packageFqName: FqName?,
val nameResolver: NameResolver
val nameResolver: NameResolver,
val typeTable: TypeTable
) {
init {
assert((classProto != null) xor (packageFqName != null))
@@ -42,7 +42,7 @@ public class TypeDeserializer(
else {
val result = LinkedHashMap<Int, TypeParameterDescriptor>()
for ((index, proto) in typeParameterProtos.withIndex()) {
result[proto.getId()] = DeserializedTypeParameterDescriptor(c, proto, index)
result[proto.id] = DeserializedTypeParameterDescriptor(c, proto, index)
}
result
}
@@ -51,18 +51,16 @@ public class TypeDeserializer(
val ownTypeParameters: List<TypeParameterDescriptor>
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 {
if (proto.hasFlexibleTypeCapabilitiesId()) {
val id = c.nameResolver.getString(proto.getFlexibleTypeCapabilitiesId())
val capabilities = c.components.flexibleTypeCapabilitiesDeserializer.capabilitiesById(id)
if (capabilities == null) {
return ErrorUtils.createErrorType("${DeserializedType(c, proto)}: Capabilities not found for id $id")
}
val id = c.nameResolver.getString(proto.flexibleTypeCapabilitiesId)
val capabilities = c.components.flexibleTypeCapabilitiesDeserializer.capabilitiesById(id) ?:
return ErrorUtils.createErrorType("${DeserializedType(c, proto)}: Capabilities not found for id $id")
return DelegatingFlexibleType.create(
DeserializedType(c, proto),
DeserializedType(c, proto.getFlexibleUpperBound()),
DeserializedType(c, proto.flexibleUpperBound(c.typeTable)!!),
capabilities
)
}
@@ -95,7 +93,7 @@ public class TypeDeserializer(
private fun computeClassDescriptor(fqNameIndex: Int): ClassDescriptor? {
val id = c.nameResolver.getClassId(fqNameIndex)
if (id.isLocal()) {
if (id.isLocal) {
// Local classes can't be found in scopes
return c.components.localClassResolver.resolveLocalClass(id)
}
@@ -103,12 +101,18 @@ public class TypeDeserializer(
}
fun typeArgument(parameter: TypeParameterDescriptor?, typeArgumentProto: ProtoBuf.Type.Argument): TypeProjection {
return if (typeArgumentProto.getProjection() == ProtoBuf.Type.Argument.Projection.STAR)
if (parameter == null)
TypeBasedStarProjectionImpl(c.components.moduleDescriptor.builtIns.getNullableAnyType())
if (typeArgumentProto.projection == ProtoBuf.Type.Argument.Projection.STAR) {
return if (parameter == null)
TypeBasedStarProjectionImpl(c.components.moduleDescriptor.builtIns.nullableAnyType)
else
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}")
@@ -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.storage.StorageManager
public class DeserializationComponents(
class DeserializationComponents(
val storageManager: StorageManager,
val moduleDescriptor: ModuleDescriptor,
val classDataFinder: ClassDataFinder,
@@ -37,19 +37,24 @@ public class DeserializationComponents(
val typeCapabilitiesLoader: TypeCapabilitiesLoader = TypeCapabilitiesLoader.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 =
DeserializationContext(this, nameResolver, descriptor, parentTypeDeserializer = null, typeParameters = listOf())
fun createContext(
descriptor: PackageFragmentDescriptor,
nameResolver: NameResolver,
typeTable: TypeTable
): DeserializationContext =
DeserializationContext(this, nameResolver, descriptor, typeTable, parentTypeDeserializer = null, typeParameters = listOf())
}
public class DeserializationContext(
public val components: DeserializationComponents,
public val nameResolver: NameResolver,
public val containingDeclaration: DeclarationDescriptor,
class DeserializationContext(
val components: DeserializationComponents,
val nameResolver: NameResolver,
val containingDeclaration: DeclarationDescriptor,
val typeTable: TypeTable,
parentTypeDeserializer: TypeDeserializer?,
typeParameters: List<ProtoBuf.TypeParameter>
) {
@@ -63,8 +68,11 @@ public class DeserializationContext(
fun childContext(
descriptor: DeclarationDescriptor,
typeParameterProtos: List<ProtoBuf.TypeParameter>,
nameResolver: NameResolver = this.nameResolver
nameResolver: NameResolver = this.nameResolver,
typeTable: TypeTable = this.typeTable
) = 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 org.jetbrains.kotlin.descriptors.CallableMemberDescriptor
import org.jetbrains.kotlin.serialization.deserialization.NameResolver
import org.jetbrains.kotlin.serialization.deserialization.TypeTable
interface DeserializedCallableMemberDescriptor : CallableMemberDescriptor {
val proto: MessageLite
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.serialization.Flags
import org.jetbrains.kotlin.serialization.ProtoBuf
import org.jetbrains.kotlin.serialization.deserialization.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.serialization.deserialization.*
import org.jetbrains.kotlin.types.AbstractClassTypeConstructor
import org.jetbrains.kotlin.types.JetType
import org.jetbrains.kotlin.types.upperIfFlexible
@@ -58,7 +55,7 @@ public class DeserializedClassDescriptor(
private val isCompanion = kindFromProto == ProtoBuf.Class.Kind.COMPANION_OBJECT
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())
@@ -138,7 +135,7 @@ public class DeserializedClassDescriptor(
val result = ArrayList<JetType>(classProto.supertypeCount)
val unresolved = ArrayList<DeserializedType>(0)
for (supertypeProto in classProto.supertypeList) {
for (supertypeProto in classProto.supertypes(c.typeTable)) {
val supertype = c.typeDeserializer.type(supertypeProto)
if (supertype.isError) {
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.serialization.ProtoBuf
import org.jetbrains.kotlin.serialization.deserialization.NameResolver
import org.jetbrains.kotlin.serialization.deserialization.TypeTable
public class DeserializedConstructorDescriptor(
containingDeclaration: ClassDescriptor,
@@ -29,7 +30,8 @@ public class DeserializedConstructorDescriptor(
isPrimary: Boolean,
kind: CallableMemberDescriptor.Kind,
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),
DeserializedCallableMemberDescriptor {
@@ -39,7 +41,8 @@ public class DeserializedConstructorDescriptor(
kind: CallableMemberDescriptor.Kind
): 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.serialization.ProtoBuf
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.toReadOnlyList
import java.util.*
@@ -38,11 +39,11 @@ public abstract class DeserializedMemberScope protected constructor(
private val functionProtos =
c.storageManager.createLazyValue {
groupByKey(filteredFunctionProtos(functionList), { it.name }) { it.hasReceiverType() }
groupByKey(filteredFunctionProtos(functionList), { it.name }) { it.receiverType(c.typeTable) != null }
}
private val propertyProtos =
c.storageManager.createLazyValue {
groupByKey(filteredPropertyProtos(propertyList), { it.name }) { it.hasReceiverType() }
groupByKey(filteredPropertyProtos(propertyList), { it.name }) { it.receiverType(c.typeTable) != null }
}
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.deserialization.DeserializationComponents
import org.jetbrains.kotlin.serialization.deserialization.NameResolver
import org.jetbrains.kotlin.serialization.deserialization.TypeTable
import org.jetbrains.kotlin.storage.getValue
import org.jetbrains.kotlin.utils.addIfNotNull
@@ -38,7 +39,10 @@ public open class DeserializedPackageMemberScope(
nameResolver: NameResolver,
components: DeserializationComponents,
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
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.serialization.ProtoBuf
import org.jetbrains.kotlin.serialization.deserialization.NameResolver
import org.jetbrains.kotlin.serialization.deserialization.TypeTable
class DeserializedPropertyDescriptor(
containingDeclaration: DeclarationDescriptor,
@@ -33,10 +34,11 @@ class DeserializedPropertyDescriptor(
isVar: Boolean,
name: Name,
kind: Kind,
lateInit: Boolean,
isConst: Boolean,
override val proto: ProtoBuf.Property,
override val nameResolver: NameResolver,
lateInit: Boolean,
isConst: Boolean
override val typeTable: TypeTable
) : DeserializedCallableMemberDescriptor,
PropertyDescriptorImpl(containingDeclaration, original, annotations,
modality, visibility, isVar, name, kind, SourceElement.NO_SOURCE, lateInit, isConst) {
@@ -49,6 +51,8 @@ class DeserializedPropertyDescriptor(
kind: Kind
): PropertyDescriptorImpl {
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.deserialization.Deserialization;
import org.jetbrains.kotlin.serialization.deserialization.NameResolver;
import org.jetbrains.kotlin.serialization.deserialization.TypeTable;
public class DeserializedSimpleFunctionDescriptor extends SimpleFunctionDescriptorImpl implements DeserializedCallableMemberDescriptor {
private final ProtoBuf.Function proto;
private final NameResolver nameResolver;
private final TypeTable typeTable;
private DeserializedSimpleFunctionDescriptor(
@NotNull DeclarationDescriptor containingDeclaration,
@@ -43,11 +45,13 @@ public class DeserializedSimpleFunctionDescriptor extends SimpleFunctionDescript
@NotNull Name name,
@NotNull Kind kind,
@NotNull ProtoBuf.Function proto,
@NotNull NameResolver nameResolver
@NotNull NameResolver nameResolver,
@NotNull TypeTable typeTable
) {
super(containingDeclaration, original, annotations, name, kind, SourceElement.NO_SOURCE);
this.proto = proto;
this.nameResolver = nameResolver;
this.typeTable = typeTable;
}
@NotNull
@@ -64,7 +68,8 @@ public class DeserializedSimpleFunctionDescriptor extends SimpleFunctionDescript
getName(),
kind,
proto,
nameResolver
nameResolver,
typeTable
);
}
@@ -86,12 +91,19 @@ public class DeserializedSimpleFunctionDescriptor extends SimpleFunctionDescript
return nameResolver;
}
@NotNull
@Override
public TypeTable getTypeTable() {
return typeTable;
}
@NotNull
public static DeserializedSimpleFunctionDescriptor create(
@NotNull DeclarationDescriptor containingDeclaration,
@NotNull Annotations annotations,
@NotNull ProtoBuf.Function proto,
@NotNull NameResolver nameResolver,
@NotNull Annotations annotations
@NotNull TypeTable typeTable
) {
return new DeserializedSimpleFunctionDescriptor(
containingDeclaration,
@@ -100,7 +112,8 @@ public class DeserializedSimpleFunctionDescriptor extends SimpleFunctionDescript
nameResolver.getName(proto.getName()),
Deserialization.memberKind(Flags.MEMBER_KIND.get(proto.getFlags())),
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.impl.AbstractLazyTypeParameterDescriptor;
import org.jetbrains.kotlin.serialization.ProtoBuf;
import org.jetbrains.kotlin.serialization.deserialization.Deserialization;
import org.jetbrains.kotlin.serialization.deserialization.DeserializationContext;
import org.jetbrains.kotlin.serialization.deserialization.TypeDeserializer;
import org.jetbrains.kotlin.serialization.deserialization.*;
import org.jetbrains.kotlin.types.JetType;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
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 {
private final ProtoBuf.TypeParameter proto;
private final TypeDeserializer typeDeserializer;
private final TypeTable typeTable;
public DeserializedTypeParameterDescriptor(@NotNull DeserializationContext c, @NotNull ProtoBuf.TypeParameter proto, int index) {
super(c.getStorageManager(),
@@ -46,16 +46,18 @@ public class DeserializedTypeParameterDescriptor extends AbstractLazyTypeParamet
SourceElement.NO_SOURCE);
this.proto = proto;
this.typeDeserializer = c.getTypeDeserializer();
this.typeTable = c.getTypeTable();
}
@NotNull
@Override
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());
}
Set<JetType> result = new LinkedHashSet<JetType>(proto.getUpperBoundCount());
for (ProtoBuf.Type upperBound : proto.getUpperBoundList()) {
Set<JetType> result = new LinkedHashSet<JetType>(upperBounds.size());
for (ProtoBuf.Type upperBound : upperBounds) {
result.add(typeDeserializer.type(upperBound, Annotations.Companion.getEMPTY()));
}
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
}
}