From fd2caf5acf657b0d415d7bef710f4be5e5ae2539 Mon Sep 17 00:00:00 2001 From: Erokhin Stanislav Date: Thu, 22 Aug 2013 20:30:37 +0400 Subject: [PATCH] JS backend: fix order class and trait in list supertypes --- .../jet/lang/resolve/DescriptorUtils.java | 18 +++--- .../declaration/ClassTranslator.java | 61 ++++++++----------- 2 files changed, 35 insertions(+), 44 deletions(-) diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/DescriptorUtils.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/DescriptorUtils.java index de499da73c1..2641bee5a09 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/DescriptorUtils.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/DescriptorUtils.java @@ -35,10 +35,7 @@ import org.jetbrains.jet.lang.resolve.name.Name; import org.jetbrains.jet.lang.resolve.scopes.FilteringScope; import org.jetbrains.jet.lang.resolve.scopes.JetScope; import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverValue; -import org.jetbrains.jet.lang.types.DescriptorSubstitutor; -import org.jetbrains.jet.lang.types.JetType; -import org.jetbrains.jet.lang.types.TypeUtils; -import org.jetbrains.jet.lang.types.Variance; +import org.jetbrains.jet.lang.types.*; import org.jetbrains.jet.lang.types.checker.JetTypeChecker; import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns; import org.jetbrains.jet.renderer.DescriptorRenderer; @@ -300,7 +297,7 @@ public class DescriptorUtils { List superClassDescriptors = new ArrayList(); for (JetType type : superclassTypes) { ClassDescriptor result = getClassDescriptorForType(type); - if (isNotAny(result)) { + if (!isAny(result)) { superClassDescriptors.add(result); } } @@ -309,15 +306,20 @@ public class DescriptorUtils { @NotNull public static ClassDescriptor getClassDescriptorForType(@NotNull JetType type) { + return getClassDescriptorForTypeConstructor(type.getConstructor()); + } + + @NotNull + public static ClassDescriptor getClassDescriptorForTypeConstructor(@NotNull TypeConstructor typeConstructor) { DeclarationDescriptor superClassDescriptor = - type.getConstructor().getDeclarationDescriptor(); + typeConstructor.getDeclarationDescriptor(); assert superClassDescriptor instanceof ClassDescriptor : "Superclass descriptor of a type should be of type ClassDescriptor"; return (ClassDescriptor)superClassDescriptor; } - public static boolean isNotAny(@NotNull DeclarationDescriptor superClassDescriptor) { - return !superClassDescriptor.equals(KotlinBuiltIns.getInstance().getAny()); + public static boolean isAny(@NotNull DeclarationDescriptor superClassDescriptor) { + return superClassDescriptor.equals(KotlinBuiltIns.getInstance().getAny()); } public static boolean inStaticContext(@NotNull DeclarationDescriptor descriptor) { diff --git a/js/js.translator/src/org/jetbrains/k2js/translate/declaration/ClassTranslator.java b/js/js.translator/src/org/jetbrains/k2js/translate/declaration/ClassTranslator.java index 90edbbd3234..9f26e5ba771 100644 --- a/js/js.translator/src/org/jetbrains/k2js/translate/declaration/ClassTranslator.java +++ b/js/js.translator/src/org/jetbrains/k2js/translate/declaration/ClassTranslator.java @@ -29,21 +29,18 @@ import org.jetbrains.jet.lang.psi.JetClassOrObject; import org.jetbrains.jet.lang.psi.JetObjectDeclaration; import org.jetbrains.jet.lang.psi.JetParameter; import org.jetbrains.jet.lang.types.JetType; +import org.jetbrains.jet.lang.types.TypeConstructor; import org.jetbrains.k2js.translate.LabelGenerator; import org.jetbrains.k2js.translate.context.Namer; import org.jetbrains.k2js.translate.context.TranslationContext; import org.jetbrains.k2js.translate.general.AbstractTranslator; import org.jetbrains.k2js.translate.initializer.ClassInitializerTranslator; -import org.jetbrains.k2js.translate.utils.AnnotationsUtils; import org.jetbrains.k2js.translate.utils.JsAstUtils; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.List; +import java.util.*; -import static org.jetbrains.jet.lang.resolve.DescriptorUtils.getClassDescriptorForType; -import static org.jetbrains.jet.lang.resolve.DescriptorUtils.isNotAny; +import static org.jetbrains.jet.lang.resolve.DescriptorUtils.*; +import static org.jetbrains.jet.lang.types.TypeUtils.topologicallySortSuperclassesAndRecordAllInstances; import static org.jetbrains.k2js.translate.expression.LiteralFunctionTranslator.createPlace; import static org.jetbrains.k2js.translate.initializer.InitializerUtils.createPropertyInitializer; import static org.jetbrains.k2js.translate.utils.BindingUtils.getClassDescriptor; @@ -257,40 +254,32 @@ public final class ClassTranslator extends AbstractTranslator { if (supertypes.isEmpty()) { return Collections.emptyList(); } + if (supertypes.size() == 1) { + JetType type = supertypes.iterator().next(); + ClassDescriptor supertypeDescriptor = getClassDescriptorForType(type); + if (isAny(supertypeDescriptor)) { + return Collections.emptyList(); + } + return Collections.singletonList(getClassReference(supertypeDescriptor)); + } - JsExpression base = null; - List list = null; + Set supertypeConstructors = new HashSet(); for (JetType type : supertypes) { - ClassDescriptor result = getClassDescriptorForType(type); - if (isNotAny(result) && !AnnotationsUtils.isNativeObject(result)) { - switch (result.getKind()) { - case CLASS: - base = getClassReference(result); - break; - case TRAIT: - if (list == null) { - list = new ArrayList(); - } - list.add(getClassReference(result)); - break; - case ENUM_CLASS: - base = getClassReference(result); - break; - - default: - throw new UnsupportedOperationException("unsupported super class kind " + result.getKind().name()); + supertypeConstructors.add(type.getConstructor()); + } + List sortedAllSuperTypes = topologicallySortSuperclassesAndRecordAllInstances(descriptor.getDefaultType(), + new HashMap>(), + new HashSet()); + List supertypesRefs = new ArrayList(); + for (TypeConstructor typeConstructor : sortedAllSuperTypes) { + if (supertypeConstructors.contains(typeConstructor)) { + ClassDescriptor supertypeDescriptor = getClassDescriptorForTypeConstructor(typeConstructor); + if (!isAny(supertypeDescriptor)) { + supertypesRefs.add(getClassReference(supertypeDescriptor)); } } } - - if (list == null) { - return base == null ? Collections.emptyList() : Collections.singletonList(base); - } - else if (base != null) { - list.add(0, base); - } - - return list; + return supertypesRefs; } @NotNull