Deterministic order of descriptors preserves name indexing for different serializer runs

This commit is contained in:
Andrey Breslav
2013-06-03 18:27:13 +04:00
committed by Alexander Udalov
parent 696b627cf4
commit de1f114b27
201 changed files with 171 additions and 132 deletions
@@ -23,11 +23,26 @@ import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.TypeConstructor;
import org.jetbrains.jet.lang.types.TypeProjection;
import org.jetbrains.jet.lang.types.Variance;
import org.jetbrains.jet.renderer.DescriptorRenderer;
import java.util.Collection;
import java.util.*;
public class DescriptorSerializer {
private static final DescriptorRenderer RENDERER = DescriptorRenderer.STARTS_FROM_NAME;
private static final Comparator<DeclarationDescriptor> DESCRIPTOR_COMPARATOR = new Comparator<DeclarationDescriptor>() {
@Override
public int compare(
DeclarationDescriptor o1, DeclarationDescriptor o2
) {
int names = o1.getName().compareTo(o2.getName());
if (names != 0) return names;
String o1String = RENDERER.render(o1);
String o2String = RENDERER.render(o2);
return o1String.compareTo(o2String);
}
};
private final NameTable nameTable;
private final Interner<TypeParameterDescriptor> typeParameters;
@@ -77,7 +92,7 @@ public class DescriptorSerializer {
}
// TODO: other constructors
for (DeclarationDescriptor descriptor : classDescriptor.getDefaultType().getMemberScope().getAllDescriptors()) {
for (DeclarationDescriptor descriptor : sort(classDescriptor.getDefaultType().getMemberScope().getAllDescriptors())) {
if (descriptor instanceof CallableMemberDescriptor) {
CallableMemberDescriptor member = (CallableMemberDescriptor) descriptor;
if (member.getKind() == CallableMemberDescriptor.Kind.FAKE_OVERRIDE) continue;
@@ -86,13 +101,13 @@ public class DescriptorSerializer {
}
Collection<DeclarationDescriptor> nestedClasses = classDescriptor.getUnsubstitutedInnerClassesScope().getAllDescriptors();
for (DeclarationDescriptor descriptor : nestedClasses) {
for (DeclarationDescriptor descriptor : sort(nestedClasses)) {
ClassDescriptor nestedClass = (ClassDescriptor) descriptor;
int nameIndex = nameTable.getSimpleNameIndex(nestedClass.getName());
builder.addNestedClassNames(nameIndex);
}
for (ClassDescriptor descriptor : classDescriptor.getUnsubstitutedInnerClassesScope().getObjectDescriptors()) {
for (ClassDescriptor descriptor : sort(classDescriptor.getUnsubstitutedInnerClassesScope().getObjectDescriptors())) {
int nameIndex = nameTable.getSimpleNameIndex(descriptor.getName());
builder.addNestedObjectNames(nameIndex);
}
@@ -287,4 +302,12 @@ public class DescriptorSerializer {
private static boolean hasAnnotations(Annotated descriptor) {
return !descriptor.getAnnotations().isEmpty();
}
@NotNull
public static <T extends DeclarationDescriptor> List<T> sort(@NotNull Collection<T> descriptors) {
List<T> result = new ArrayList<T>(descriptors);
Collections.sort(result, DESCRIPTOR_COMPARATOR);
return result;
}
}