Store type parameter names sometimes instead of ids

This helps to reuse instances of types when TypeTable is enabled in cases when
a file or a class contains a lot of functions with type parameters with the
same name (like Maps.kt in the stdlib with "Map<K, V>")
This commit is contained in:
Alexander Udalov
2015-10-09 04:47:16 +03:00
parent fb5d8de84b
commit 4c21142648
7 changed files with 358 additions and 93 deletions
@@ -39,17 +39,20 @@ import java.util.*;
import static org.jetbrains.kotlin.resolve.DescriptorUtils.isEnumEntry;
public class DescriptorSerializer {
private final DeclarationDescriptor containingDeclaration;
private final Interner<TypeParameterDescriptor> typeParameters;
private final SerializerExtension extension;
private final MutableTypeTable typeTable;
private final boolean serializeTypeTableToFunction;
private DescriptorSerializer(
@Nullable DeclarationDescriptor containingDeclaration,
@NotNull Interner<TypeParameterDescriptor> typeParameters,
@NotNull SerializerExtension extension,
@NotNull MutableTypeTable typeTable,
boolean serializeTypeTableToFunction
) {
this.containingDeclaration = containingDeclaration;
this.typeParameters = typeParameters;
this.extension = extension;
this.typeTable = typeTable;
@@ -71,12 +74,12 @@ public class DescriptorSerializer {
@NotNull
public static DescriptorSerializer createTopLevel(@NotNull SerializerExtension extension) {
return new DescriptorSerializer(new Interner<TypeParameterDescriptor>(), extension, new MutableTypeTable(), false);
return new DescriptorSerializer(null, new Interner<TypeParameterDescriptor>(), extension, new MutableTypeTable(), false);
}
@NotNull
public static DescriptorSerializer createForLambda(@NotNull SerializerExtension extension) {
return new DescriptorSerializer(new Interner<TypeParameterDescriptor>(), extension, new MutableTypeTable(), true);
return new DescriptorSerializer(null, new Interner<TypeParameterDescriptor>(), extension, new MutableTypeTable(), true);
}
@NotNull
@@ -91,6 +94,7 @@ public class DescriptorSerializer {
// serializing outer classes before nested classes.
// Otherwise our interner can get wrong ids because we may serialize classes in any order.
DescriptorSerializer serializer = new DescriptorSerializer(
descriptor,
new Interner<TypeParameterDescriptor>(parentSerializer.typeParameters),
parentSerializer.extension,
new MutableTypeTable(),
@@ -103,8 +107,8 @@ public class DescriptorSerializer {
}
@NotNull
private DescriptorSerializer createChildSerializer() {
return new DescriptorSerializer(new Interner<TypeParameterDescriptor>(typeParameters), extension, typeTable, false);
private DescriptorSerializer createChildSerializer(@NotNull CallableDescriptor callable) {
return new DescriptorSerializer(callable, new Interner<TypeParameterDescriptor>(typeParameters), extension, typeTable, false);
}
@NotNull
@@ -191,7 +195,7 @@ public class DescriptorSerializer {
public ProtoBuf.Property.Builder propertyProto(@NotNull PropertyDescriptor descriptor) {
ProtoBuf.Property.Builder builder = ProtoBuf.Property.newBuilder();
DescriptorSerializer local = createChildSerializer();
DescriptorSerializer local = createChildSerializer(descriptor);
boolean hasGetter = false;
boolean hasSetter = false;
@@ -228,8 +232,9 @@ public class DescriptorSerializer {
}
if (!setter.isDefault()) {
DescriptorSerializer setterLocal = local.createChildSerializer(setter);
for (ValueParameterDescriptor valueParameterDescriptor : setter.getValueParameters()) {
builder.setSetterValueParameter(local.valueParameter(valueParameterDescriptor));
builder.setSetterValueParameter(setterLocal.valueParameter(valueParameterDescriptor));
}
}
}
@@ -274,7 +279,7 @@ public class DescriptorSerializer {
public ProtoBuf.Function.Builder functionProto(@NotNull FunctionDescriptor descriptor) {
ProtoBuf.Function.Builder builder = ProtoBuf.Function.newBuilder();
DescriptorSerializer local = createChildSerializer();
DescriptorSerializer local = createChildSerializer(descriptor);
int flags = Flags.getFunctionFlags(
hasAnnotations(descriptor), descriptor.getVisibility(), descriptor.getModality(), descriptor.getKind(),
@@ -329,7 +334,7 @@ public class DescriptorSerializer {
public ProtoBuf.Constructor.Builder constructorProto(@NotNull ConstructorDescriptor descriptor) {
ProtoBuf.Constructor.Builder builder = ProtoBuf.Constructor.newBuilder();
DescriptorSerializer local = createChildSerializer();
DescriptorSerializer local = createChildSerializer(descriptor);
int flags = Flags.getConstructorFlags(hasAnnotations(descriptor), descriptor.getVisibility(), !descriptor.isPrimary());
if (flags != builder.getFlags()) {
@@ -459,7 +464,13 @@ public class DescriptorSerializer {
builder.setClassName(getClassId((ClassDescriptor) descriptor));
}
if (descriptor instanceof TypeParameterDescriptor) {
builder.setTypeParameter(getTypeParameterId((TypeParameterDescriptor) descriptor));
TypeParameterDescriptor typeParameter = (TypeParameterDescriptor) descriptor;
if (typeParameter.getContainingDeclaration() == containingDeclaration) {
builder.setTypeParameterName(getSimpleNameIndex(typeParameter.getName()));
}
else {
builder.setTypeParameter(getTypeParameterId(typeParameter));
}
}
for (TypeProjection projection : type.getArguments()) {