Support for simple cases when a namespace creates classes from other namespaces.

This commit is contained in:
Pavel V. Talanov
2012-03-19 15:44:02 +04:00
parent 6f87634b25
commit 02da9b42c1
16 changed files with 164 additions and 117 deletions
@@ -40,9 +40,20 @@ public class MultiNamespaceTest extends MultipleFilesTranslationTest {
checkFooBoxIsTrue("namespaceVariableVisibleFromOtherNamespace"); checkFooBoxIsTrue("namespaceVariableVisibleFromOtherNamespace");
} }
public void testNestedNamespaceFunctionCalledFromOtherNamespace() throws Exception { public void testNestedNamespaceFunctionCalledFromOtherNamespace() throws Exception {
checkFooBoxIsTrue("nestedNamespaceFunctionCalledFromOtherNamespace"); runMultiFileTest("nestedNamespaceFunctionCalledFromOtherNamespace", "a.foo", "box", true);
}
public void testSubnamespacesWithClashingNames() throws Exception {
runMultiFileTest("subnamespacesWithClashingNames", "a.foo", "box", true);
}
public void testSubnamespacesWithClashingNamesUsingImport() throws Exception {
runMultiFileTest("subnamespacesWithClashingNamesUsingImport", "a.foo", "box", true);
}
public void testCreateClassFromOtherNamespace() throws Exception {
runMultiFileTest("createClassFromOtherNamespace", "a.foo", "box", true);
} }
} }
@@ -37,4 +37,9 @@ public final class NamespaceTest extends SingleFileTranslationTest {
public void testDeeplyNestedNamespaceFunctionCalled() throws Exception { public void testDeeplyNestedNamespaceFunctionCalled() throws Exception {
runFunctionOutputTest("deeplyNestedNamespaceFunctionCalled.kt", "foo1.foo2.foo3.foo5.foo6.foo7.foo8", "box", true); runFunctionOutputTest("deeplyNestedNamespaceFunctionCalled.kt", "foo1.foo2.foo3.foo5.foo6.foo7.foo8", "box", true);
} }
public void testClassCreatedInDeeplyNestedNamespace() throws Exception {
runFunctionOutputTest("classCreatedInDeeplyNestedNamespace.kt", "foo1.foo2.foo3.foo5.foo6.foo7.foo8", "box", true);
}
} }
@@ -27,6 +27,7 @@ import org.jetbrains.k2js.translate.context.generator.Generator;
import org.jetbrains.k2js.translate.context.generator.Rule; import org.jetbrains.k2js.translate.context.generator.Rule;
import org.jetbrains.k2js.translate.intrinsic.Intrinsics; import org.jetbrains.k2js.translate.intrinsic.Intrinsics;
import org.jetbrains.k2js.translate.utils.AnnotationsUtils; import org.jetbrains.k2js.translate.utils.AnnotationsUtils;
import org.jetbrains.k2js.translate.utils.DescriptorUtils;
import org.jetbrains.k2js.translate.utils.JsAstUtils; import org.jetbrains.k2js.translate.utils.JsAstUtils;
import org.jetbrains.k2js.translate.utils.PredefinedAnnotation; import org.jetbrains.k2js.translate.utils.PredefinedAnnotation;
@@ -51,7 +52,7 @@ public final class StaticContext {
NamingScope scope = NamingScope.rootScope(jsRootScope); NamingScope scope = NamingScope.rootScope(jsRootScope);
Intrinsics intrinsics = Intrinsics.standardLibraryIntrinsics(library); Intrinsics intrinsics = Intrinsics.standardLibraryIntrinsics(library);
StandardClasses standardClasses = StandardClasses standardClasses =
StandardClasses.bindImplementations(namer.getKotlinScope()); StandardClasses.bindImplementations(namer.getKotlinScope());
return new StaticContext(program, bindingContext, aliaser, return new StaticContext(program, bindingContext, aliaser,
namer, intrinsics, standardClasses, scope); namer, intrinsics, standardClasses, scope);
} }
@@ -174,7 +175,7 @@ public final class StaticContext {
if (!(descriptor instanceof NamespaceDescriptor)) { if (!(descriptor instanceof NamespaceDescriptor)) {
return null; return null;
} }
String nameForNamespace = getNameForNamespace((NamespaceDescriptor) descriptor); String nameForNamespace = getNameForNamespace((NamespaceDescriptor)descriptor);
return getRootScope().declareUnobfuscatableName(nameForNamespace); return getRootScope().declareUnobfuscatableName(nameForNamespace);
} }
}; };
@@ -204,7 +205,7 @@ public final class StaticContext {
return null; return null;
} }
boolean isGetter = descriptor instanceof PropertyGetterDescriptor; boolean isGetter = descriptor instanceof PropertyGetterDescriptor;
String propertyName = ((PropertyAccessorDescriptor) descriptor).getCorrespondingProperty().getName(); String propertyName = ((PropertyAccessorDescriptor)descriptor).getCorrespondingProperty().getName();
String accessorName = Namer.getNameForAccessor(propertyName, isGetter); String accessorName = Namer.getNameForAccessor(propertyName, isGetter);
NamingScope enclosingScope = getEnclosingScope(descriptor); NamingScope enclosingScope = getEnclosingScope(descriptor);
return enclosingScope.declareObfuscatableName(accessorName); return enclosingScope.declareObfuscatableName(accessorName);
@@ -245,12 +246,11 @@ public final class StaticContext {
if (!descriptor.getName().equals("toString")) { if (!descriptor.getName().equals("toString")) {
return null; return null;
} }
if (((FunctionDescriptor) descriptor).getValueParameters().isEmpty()) { if (((FunctionDescriptor)descriptor).getValueParameters().isEmpty()) {
return getEnclosingScope(descriptor).declareUnobfuscatableName("toString"); return getEnclosingScope(descriptor).declareUnobfuscatableName("toString");
} }
return null; return null;
} }
}; };
Rule<JsName> overridingDescriptorsReferToOriginalName = new Rule<JsName>() { Rule<JsName> overridingDescriptorsReferToOriginalName = new Rule<JsName>() {
@@ -260,7 +260,7 @@ public final class StaticContext {
if (!(descriptor instanceof FunctionDescriptor)) { if (!(descriptor instanceof FunctionDescriptor)) {
return null; return null;
} }
FunctionDescriptor overriddenDescriptor = getOverriddenDescriptor((FunctionDescriptor) descriptor); FunctionDescriptor overriddenDescriptor = getOverriddenDescriptor((FunctionDescriptor)descriptor);
if (overriddenDescriptor == null) { if (overriddenDescriptor == null) {
return null; return null;
} }
@@ -295,7 +295,7 @@ public final class StaticContext {
if (!(descriptor instanceof ClassDescriptor)) { if (!(descriptor instanceof ClassDescriptor)) {
return null; return null;
} }
if (getSuperclass((ClassDescriptor) descriptor) == null) { if (getSuperclass((ClassDescriptor)descriptor) == null) {
return getRootScope().innerScope("Scope for class " + descriptor.getName()); return getRootScope().innerScope("Scope for class " + descriptor.getName());
} }
return null; return null;
@@ -307,7 +307,7 @@ public final class StaticContext {
if (!(descriptor instanceof ClassDescriptor)) { if (!(descriptor instanceof ClassDescriptor)) {
return null; return null;
} }
ClassDescriptor superclass = getSuperclass((ClassDescriptor) descriptor); ClassDescriptor superclass = getSuperclass((ClassDescriptor)descriptor);
if (superclass == null) { if (superclass == null) {
return null; return null;
} }
@@ -354,7 +354,7 @@ public final class StaticContext {
@Nullable @Nullable
public JsNameRef getQualifierForDescriptor(@NotNull DeclarationDescriptor descriptor) { public JsNameRef getQualifierForDescriptor(@NotNull DeclarationDescriptor descriptor) {
if (qualifierIsNull.get(descriptor) != null) { if (qualifierIsNull.get(descriptor.getOriginal()) != null) {
return null; return null;
} }
return qualifiers.get(descriptor.getOriginal()); return qualifiers.get(descriptor.getOriginal());
@@ -362,7 +362,7 @@ public final class StaticContext {
private final class QualifierGenerator extends Generator<JsNameRef> { private final class QualifierGenerator extends Generator<JsNameRef> {
public QualifierGenerator() { public QualifierGenerator() {
Rule<JsNameRef> namespacesHaveNoQualifiers = new Rule<JsNameRef>() { Rule<JsNameRef> standardObjectsHaveKotlinQualifier = new Rule<JsNameRef>() {
@Override @Override
public JsNameRef apply(@NotNull DeclarationDescriptor descriptor) { public JsNameRef apply(@NotNull DeclarationDescriptor descriptor) {
if (!standardClasses.isStandardObject(descriptor)) { if (!standardClasses.isStandardObject(descriptor)) {
@@ -379,7 +379,9 @@ public final class StaticContext {
return null; return null;
} }
JsName containingDeclarationName = getNameForDescriptor(containingDeclaration); JsName containingDeclarationName = getNameForDescriptor(containingDeclaration);
return containingDeclarationName.makeRef(); JsNameRef qualifier = containingDeclarationName.makeRef();
qualifier.setQualifier(getQualifierForDescriptor(containingDeclaration));
return qualifier;
} }
}; };
Rule<JsNameRef> constructorHaveTheSameQualifierAsTheClass = new Rule<JsNameRef>() { Rule<JsNameRef> constructorHaveTheSameQualifierAsTheClass = new Rule<JsNameRef>() {
@@ -405,7 +407,7 @@ public final class StaticContext {
}; };
addRule(libraryObjectsHaveKotlinQualifier); addRule(libraryObjectsHaveKotlinQualifier);
addRule(constructorHaveTheSameQualifierAsTheClass); addRule(constructorHaveTheSameQualifierAsTheClass);
addRule(namespacesHaveNoQualifiers); addRule(standardObjectsHaveKotlinQualifier);
addRule(namespaceLevelDeclarationsHaveEnclosingNamespacesNamesAsQualifier); addRule(namespaceLevelDeclarationsHaveEnclosingNamespacesNamesAsQualifier);
} }
} }
@@ -441,6 +443,19 @@ public final class StaticContext {
return true; return true;
} }
}; };
Rule<Boolean> topLevelNamespaceHaveNoQualifier = new Rule<Boolean>() {
@Override
public Boolean apply(@NotNull DeclarationDescriptor descriptor) {
if (!(descriptor instanceof NamespaceDescriptor)) {
return null;
}
if (DescriptorUtils.isTopLevelNamespace((NamespaceDescriptor)descriptor)) {
return true;
}
return null;
}
};
addRule(topLevelNamespaceHaveNoQualifier);
addRule(propertiesHaveNoQualifiers); addRule(propertiesHaveNoQualifiers);
addRule(variableAsFunctionsHaveNoQualifiers); addRule(variableAsFunctionsHaveNoQualifiers);
addRule(nativeObjectsHaveNoQualifiers); addRule(nativeObjectsHaveNoQualifiers);
@@ -76,7 +76,7 @@ public final class TranslationContext {
@NotNull @NotNull
public JsBlock getBlockForDescriptor(@NotNull DeclarationDescriptor descriptor) { public JsBlock getBlockForDescriptor(@NotNull DeclarationDescriptor descriptor) {
if (descriptor instanceof CallableDescriptor) { if (descriptor instanceof CallableDescriptor) {
return getFunctionObject((CallableDescriptor) descriptor).getBody(); return getFunctionObject((CallableDescriptor)descriptor).getBody();
} }
else { else {
return new JsBlock(); return new JsBlock();
@@ -111,7 +111,7 @@ public final class TranslationContext {
@Nullable @Nullable
public JsNameRef getQualifierForDescriptor(@NotNull DeclarationDescriptor descriptor) { public JsNameRef getQualifierForDescriptor(@NotNull DeclarationDescriptor descriptor) {
return staticContext.getQualifierForDescriptor(descriptor.getOriginal()); return staticContext.getQualifierForDescriptor(descriptor);
} }
@NotNull @NotNull
@@ -31,7 +31,6 @@ import java.util.Set;
import static org.jetbrains.k2js.translate.utils.BindingUtils.getAllNonNativeNamespaceDescriptors; import static org.jetbrains.k2js.translate.utils.BindingUtils.getAllNonNativeNamespaceDescriptors;
import static org.jetbrains.k2js.translate.utils.DescriptorUtils.getAllClassesDefinedInNamespace; import static org.jetbrains.k2js.translate.utils.DescriptorUtils.getAllClassesDefinedInNamespace;
import static org.jetbrains.k2js.translate.utils.NamespaceSortingUtils.sortNamespacesUsingQualificationOrder;
/** /**
* @author Pavel Talanov * @author Pavel Talanov
@@ -51,7 +50,7 @@ public final class NamespaceDeclarationTranslator extends AbstractTranslator {
private NamespaceDeclarationTranslator(@NotNull List<NamespaceDescriptor> namespaceDescriptors, private NamespaceDeclarationTranslator(@NotNull List<NamespaceDescriptor> namespaceDescriptors,
@NotNull TranslationContext context) { @NotNull TranslationContext context) {
super(context); super(context);
this.namespaceDescriptors = sortNamespacesUsingQualificationOrder(namespaceDescriptors); this.namespaceDescriptors = namespaceDescriptors;
this.classDeclarationTranslator = new ClassDeclarationTranslator(getAllClasses(), context); this.classDeclarationTranslator = new ClassDeclarationTranslator(getAllClasses(), context);
} }
@@ -91,7 +90,7 @@ public final class NamespaceDeclarationTranslator extends AbstractTranslator {
@NotNull @NotNull
private List<NamespaceTranslator> getTranslatorsForNonEmptyNamespaces() { private List<NamespaceTranslator> getTranslatorsForNonEmptyNamespaces() {
List<NamespaceTranslator> namespaceTranslators = Lists.newArrayList(); List<NamespaceTranslator> namespaceTranslators = Lists.newArrayList();
for (NamespaceDescriptor descriptor : namespaceDescriptors) { for (NamespaceDescriptor descriptor : filterTopLevelNamespaces(namespaceDescriptors)) {
if (!DescriptorUtils.isNamespaceEmpty(descriptor)) { if (!DescriptorUtils.isNamespaceEmpty(descriptor)) {
namespaceTranslators.add(new NamespaceTranslator(descriptor, classDeclarationTranslator, context())); namespaceTranslators.add(new NamespaceTranslator(descriptor, classDeclarationTranslator, context()));
} }
@@ -103,7 +102,7 @@ public final class NamespaceDeclarationTranslator extends AbstractTranslator {
private static List<JsStatement> declarationStatements(@NotNull List<NamespaceTranslator> namespaceTranslators) { private static List<JsStatement> declarationStatements(@NotNull List<NamespaceTranslator> namespaceTranslators) {
List<JsStatement> result = Lists.newArrayList(); List<JsStatement> result = Lists.newArrayList();
for (NamespaceTranslator translator : namespaceTranslators) { for (NamespaceTranslator translator : namespaceTranslators) {
result.add(translator.getDeclarationStatement()); result.add(translator.getDeclarationAsVar());
} }
return result; return result;
} }
@@ -116,4 +115,15 @@ public final class NamespaceDeclarationTranslator extends AbstractTranslator {
} }
return result; return result;
} }
@NotNull
private static List<NamespaceDescriptor> filterTopLevelNamespaces(@NotNull List<NamespaceDescriptor> namespaceDescriptors) {
List<NamespaceDescriptor> result = Lists.newArrayList();
for (NamespaceDescriptor descriptor : namespaceDescriptors) {
if (DescriptorUtils.isTopLevelNamespace(descriptor)) {
result.add(descriptor);
}
}
return result;
}
} }
@@ -64,25 +64,35 @@ public final class NamespaceTranslator extends AbstractTranslator {
} }
@NotNull @NotNull
private JsInvocation namespaceCreateMethodInvocation() { public JsStatement getDeclarationAsVar() {
return AstUtil.newInvocation(context().namer().namespaceCreationMethodReference()); return newVar(namespaceName, getNamespaceDeclaration());
} }
@NotNull @NotNull
public JsStatement getDeclarationStatement() { public JsPropertyInitializer getDeclarationAsInitializer() {
return new JsPropertyInitializer(namespaceName.makeRef(), getNamespaceDeclaration());
}
@NotNull
private JsInvocation getNamespaceDeclaration() {
JsInvocation namespaceDeclaration = namespaceCreateMethodInvocation(); JsInvocation namespaceDeclaration = namespaceCreateMethodInvocation();
namespaceDeclaration.getArguments().add(translateNamespaceMemberDeclarations()); namespaceDeclaration.getArguments().add(translateNamespaceMemberDeclarations());
namespaceDeclaration.getArguments().add(getClassesAndNestedNamespaces()); namespaceDeclaration.getArguments().add(getClassesAndNestedNamespaces());
return newVar(namespaceName, namespaceDeclaration); return namespaceDeclaration;
}
@NotNull
private JsInvocation namespaceCreateMethodInvocation() {
return AstUtil.newInvocation(context().namer().namespaceCreationMethodReference());
} }
@NotNull @NotNull
private JsObjectLiteral getClassesAndNestedNamespaces() { private JsObjectLiteral getClassesAndNestedNamespaces() {
JsObjectLiteral classesAndNestedNamespaces = new JsObjectLiteral(); JsObjectLiteral classesAndNestedNamespaces = new JsObjectLiteral();
classesAndNestedNamespaces.getPropertyInitializers() classesAndNestedNamespaces.getPropertyInitializers()
.addAll(getClassesDefined()); .addAll(getClassesDefined());
classesAndNestedNamespaces.getPropertyInitializers() classesAndNestedNamespaces.getPropertyInitializers()
.addAll(getNestedNamespaceDeclarations()); .addAll(getNestedNamespaceDeclarations());
return classesAndNestedNamespaces; return classesAndNestedNamespaces;
} }
@@ -96,8 +106,8 @@ public final class NamespaceTranslator extends AbstractTranslator {
List<JsPropertyInitializer> result = Lists.newArrayList(); List<JsPropertyInitializer> result = Lists.newArrayList();
List<NamespaceDescriptor> nestedNamespaces = DescriptorUtils.getNestedNamespaces(descriptor); List<NamespaceDescriptor> nestedNamespaces = DescriptorUtils.getNestedNamespaces(descriptor);
for (NamespaceDescriptor nestedNamespace : nestedNamespaces) { for (NamespaceDescriptor nestedNamespace : nestedNamespaces) {
JsName nameForDescriptor = context().getNameForDescriptor(nestedNamespace); NamespaceTranslator nestedNamespaceTranslator = new NamespaceTranslator(nestedNamespace, classDeclarationTranslator, context());
result.add(new JsPropertyInitializer(nameForDescriptor.makeRef(), nameForDescriptor.makeRef())); result.add(nestedNamespaceTranslator.getDeclarationAsInitializer());
} }
return result; return result;
} }
@@ -18,13 +18,14 @@ package org.jetbrains.k2js.translate.reference;
import com.google.dart.compiler.backend.js.ast.JsExpression; import com.google.dart.compiler.backend.js.ast.JsExpression;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.psi.JetCallExpression; import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.psi.JetExpression; import org.jetbrains.jet.lang.descriptors.DeclarationDescriptor;
import org.jetbrains.jet.lang.psi.JetQualifiedExpression; import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
import org.jetbrains.jet.lang.psi.JetSimpleNameExpression; import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.k2js.translate.context.TranslationContext; import org.jetbrains.k2js.translate.context.TranslationContext;
import static org.jetbrains.k2js.translate.general.Translation.translateAsExpression; import static org.jetbrains.k2js.translate.general.Translation.translateAsExpression;
import static org.jetbrains.k2js.translate.utils.BindingUtils.getDescriptorForReferenceExpression;
import static org.jetbrains.k2js.translate.utils.PsiUtils.getNotNullSimpleNameSelector; import static org.jetbrains.k2js.translate.utils.PsiUtils.getNotNullSimpleNameSelector;
import static org.jetbrains.k2js.translate.utils.PsiUtils.getSelector; import static org.jetbrains.k2js.translate.utils.PsiUtils.getSelector;
@@ -58,7 +59,7 @@ public final class QualifiedExpressionTranslator {
} }
@NotNull @NotNull
private static JsExpression dispatchToCorrectTranslator(@NotNull JsExpression receiver, private static JsExpression dispatchToCorrectTranslator(@Nullable JsExpression receiver,
@NotNull JetExpression selector, @NotNull JetExpression selector,
@NotNull CallType callType, @NotNull CallType callType,
@NotNull TranslationContext context) { @NotNull TranslationContext context) {
@@ -76,10 +77,31 @@ public final class QualifiedExpressionTranslator {
throw new AssertionError("Unexpected qualified expression: " + selector.getText()); throw new AssertionError("Unexpected qualified expression: " + selector.getText());
} }
//TODO: if has duplications @Nullable
@NotNull
private static JsExpression translateReceiver(@NotNull JetQualifiedExpression expression, private static JsExpression translateReceiver(@NotNull JetQualifiedExpression expression,
@NotNull TranslationContext context) { @NotNull TranslationContext context) {
return translateAsExpression(expression.getReceiverExpression(), context); JetExpression receiverExpression = expression.getReceiverExpression();
if (isFullQualifierForExpression(receiverExpression, context)) {
return null;
}
return translateAsExpression(receiverExpression, context);
}
//TODO: prove correctness
private static boolean isFullQualifierForExpression(@Nullable JetExpression receiverExpression, @NotNull TranslationContext context) {
if (receiverExpression == null) {
return false;
}
if (receiverExpression instanceof JetReferenceExpression) {
DeclarationDescriptor descriptorForReferenceExpression =
getDescriptorForReferenceExpression(context.bindingContext(), (JetReferenceExpression)receiverExpression);
if (descriptorForReferenceExpression instanceof NamespaceDescriptor) {
return true;
}
}
if (receiverExpression instanceof JetQualifiedExpression) {
return isFullQualifierForExpression(((JetQualifiedExpression)receiverExpression).getSelectorExpression(), context);
}
return false;
} }
} }
@@ -66,8 +66,8 @@ public final class DescriptorUtils {
@NotNull String name) { @NotNull String name) {
Set<FunctionDescriptor> functionDescriptors = scope.getFunctions(name); Set<FunctionDescriptor> functionDescriptors = scope.getFunctions(name);
assert functionDescriptors.size() == 1 : assert functionDescriptors.size() == 1 :
"In scope " + scope + " supposed to be exactly one " + name + " function.\n" + "In scope " + scope + " supposed to be exactly one " + name + " function.\n" +
"Found: " + functionDescriptors.size(); "Found: " + functionDescriptors.size();
//noinspection LoopStatementThatDoesntLoop //noinspection LoopStatementThatDoesntLoop
for (FunctionDescriptor descriptor : functionDescriptors) { for (FunctionDescriptor descriptor : functionDescriptors) {
return descriptor; return descriptor;
@@ -84,7 +84,7 @@ public final class DescriptorUtils {
Set<VariableDescriptor> variables = scope.getProperties(name); Set<VariableDescriptor> variables = scope.getProperties(name);
assert variables.size() == 1 : "Actual size: " + variables.size(); assert variables.size() == 1 : "Actual size: " + variables.size();
VariableDescriptor variable = variables.iterator().next(); VariableDescriptor variable = variables.iterator().next();
PropertyDescriptor descriptor = (PropertyDescriptor) variable; PropertyDescriptor descriptor = (PropertyDescriptor)variable;
assert descriptor != null : "Must have a descriptor."; assert descriptor != null : "Must have a descriptor.";
return descriptor; return descriptor;
} }
@@ -120,15 +120,15 @@ public final class DescriptorUtils {
@NotNull @NotNull
public static ClassDescriptor getClassDescriptorForType(@NotNull JetType type) { public static ClassDescriptor getClassDescriptorForType(@NotNull JetType type) {
DeclarationDescriptor superClassDescriptor = DeclarationDescriptor superClassDescriptor =
type.getConstructor().getDeclarationDescriptor(); type.getConstructor().getDeclarationDescriptor();
assert superClassDescriptor instanceof ClassDescriptor assert superClassDescriptor instanceof ClassDescriptor
: "Superclass descriptor of a type should be of type ClassDescriptor"; : "Superclass descriptor of a type should be of type ClassDescriptor";
return (ClassDescriptor) superClassDescriptor; return (ClassDescriptor)superClassDescriptor;
} }
@NotNull @NotNull
public static VariableDescriptor getVariableDescriptorForVariableAsFunction public static VariableDescriptor getVariableDescriptorForVariableAsFunction
(@NotNull VariableAsFunctionDescriptor descriptor) { (@NotNull VariableAsFunctionDescriptor descriptor) {
VariableDescriptor functionVariable = descriptor.getVariableDescriptor(); VariableDescriptor functionVariable = descriptor.getVariableDescriptor();
assert functionVariable != null; assert functionVariable != null;
return functionVariable; return functionVariable;
@@ -171,9 +171,9 @@ public final class DescriptorUtils {
@NotNull @NotNull
public static DeclarationDescriptor getDeclarationDescriptorForReceiver public static DeclarationDescriptor getDeclarationDescriptorForReceiver
(@NotNull ReceiverDescriptor receiverParameter) { (@NotNull ReceiverDescriptor receiverParameter) {
DeclarationDescriptor declarationDescriptor = DeclarationDescriptor declarationDescriptor =
receiverParameter.getType().getConstructor().getDeclarationDescriptor(); receiverParameter.getType().getConstructor().getDeclarationDescriptor();
//TODO: WHY assert? //TODO: WHY assert?
assert declarationDescriptor != null; assert declarationDescriptor != null;
return declarationDescriptor.getOriginal(); return declarationDescriptor.getOriginal();
@@ -194,7 +194,7 @@ public final class DescriptorUtils {
DeclarationDescriptor containing = descriptor.getContainingDeclaration(); DeclarationDescriptor containing = descriptor.getContainingDeclaration();
while (containing != null) { while (containing != null) {
if (containing instanceof ClassDescriptor) { if (containing instanceof ClassDescriptor) {
return (ClassDescriptor) containing; return (ClassDescriptor)containing;
} }
containing = containing.getContainingDeclaration(); containing = containing.getContainingDeclaration();
} }
@@ -206,7 +206,7 @@ public final class DescriptorUtils {
List<ClassDescriptor> classDescriptors = Lists.newArrayList(); List<ClassDescriptor> classDescriptors = Lists.newArrayList();
for (DeclarationDescriptor descriptor : getContainedDescriptorsWhichAreNotPredefined(namespaceDescriptor)) { for (DeclarationDescriptor descriptor : getContainedDescriptorsWhichAreNotPredefined(namespaceDescriptor)) {
if (descriptor instanceof ClassDescriptor) { if (descriptor instanceof ClassDescriptor) {
classDescriptors.add((ClassDescriptor) descriptor); classDescriptors.add((ClassDescriptor)descriptor);
} }
} }
return classDescriptors; return classDescriptors;
@@ -217,7 +217,7 @@ public final class DescriptorUtils {
List<NamespaceDescriptor> result = Lists.newArrayList(); List<NamespaceDescriptor> result = Lists.newArrayList();
for (DeclarationDescriptor descriptor : getContainedDescriptorsWhichAreNotPredefined(namespaceDescriptor)) { for (DeclarationDescriptor descriptor : getContainedDescriptorsWhichAreNotPredefined(namespaceDescriptor)) {
if (descriptor instanceof NamespaceDescriptor) { if (descriptor instanceof NamespaceDescriptor) {
result.add((NamespaceDescriptor) descriptor); result.add((NamespaceDescriptor)descriptor);
} }
} }
return result; return result;
@@ -235,21 +235,18 @@ public final class DescriptorUtils {
} }
} }
//TODO:? public static boolean isTopLevelNamespace(@NotNull NamespaceDescriptor namespaceDescriptor) {
// @NotNull NamespaceDescriptorParent containingDeclaration = namespaceDescriptor.getContainingDeclaration();
// public static FunctionDescriptor getOverriddenOrThis(@NotNull FunctionDescriptor functionDescriptor) { boolean containedInModule = !(containingDeclaration instanceof NamespaceDescriptor);
// FunctionDescriptor overriddenDescriptor = getOverriddenDescriptor(functionDescriptor); if (containedInModule) {
// if (overriddenDescriptor != null) { return true;
// return overriddenDescriptor; }
// } //TODO
// else { return containingDeclaration.getName().equals("<root>");
// return functionDescriptor; }
// }
// }
@NotNull @NotNull
public static List<DeclarationDescriptor> getContainedDescriptorsWhichAreNotPredefined public static List<DeclarationDescriptor> getContainedDescriptorsWhichAreNotPredefined(@NotNull NamespaceDescriptor namespace) {
(@NotNull NamespaceDescriptor namespace) {
List<DeclarationDescriptor> result = Lists.newArrayList(); List<DeclarationDescriptor> result = Lists.newArrayList();
for (DeclarationDescriptor descriptor : namespace.getMemberScope().getAllDescriptors()) { for (DeclarationDescriptor descriptor : namespace.getMemberScope().getAllDescriptors()) {
if (!AnnotationsUtils.isPredefinedObject(descriptor)) { if (!AnnotationsUtils.isPredefinedObject(descriptor)) {
@@ -264,7 +261,7 @@ public final class DescriptorUtils {
List<DeclarationDescriptor> containedDescriptors = getContainedDescriptorsWhichAreNotPredefined(namespace); List<DeclarationDescriptor> containedDescriptors = getContainedDescriptorsWhichAreNotPredefined(namespace);
for (DeclarationDescriptor descriptor : containedDescriptors) { for (DeclarationDescriptor descriptor : containedDescriptors) {
if (descriptor instanceof NamespaceDescriptor) { if (descriptor instanceof NamespaceDescriptor) {
if (!isNamespaceEmpty((NamespaceDescriptor) descriptor)) { if (!isNamespaceEmpty((NamespaceDescriptor)descriptor)) {
return false; return false;
} }
} }
@@ -282,7 +279,7 @@ public final class DescriptorUtils {
while (!current.getName().equals("<root>")) { while (!current.getName().equals("<root>")) {
result.add(current); result.add(current);
if (current.getContainingDeclaration() instanceof NamespaceDescriptor) { if (current.getContainingDeclaration() instanceof NamespaceDescriptor) {
current = (NamespaceDescriptor) current.getContainingDeclaration(); current = (NamespaceDescriptor)current.getContainingDeclaration();
assert current != null; assert current != null;
} }
else { else {
@@ -1,54 +0,0 @@
/*
* Copyright 2010-2012 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.k2js.translate.utils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptorParent;
import java.util.List;
/**
* @author Pavel Talanov
*/
public final class NamespaceSortingUtils {
private NamespaceSortingUtils() {
}
@NotNull
public static List<NamespaceDescriptor> sortNamespacesUsingQualificationOrder
(@NotNull List<NamespaceDescriptor> namespaceDescriptors) {
PartiallyOrderedSet<NamespaceDescriptor> namespaceDescriptorPartiallyOrderedSet
= new PartiallyOrderedSet<NamespaceDescriptor>(namespaceDescriptors, qualificationOrder());
return namespaceDescriptorPartiallyOrderedSet.partiallySortedElements();
}
@NotNull
private static PartiallyOrderedSet.Order<NamespaceDescriptor> qualificationOrder() {
return new PartiallyOrderedSet.Order<NamespaceDescriptor>() {
@Override
public boolean firstDependsOnSecond(@NotNull NamespaceDescriptor first, @NotNull NamespaceDescriptor second) {
NamespaceDescriptorParent containingDeclaration = first.getContainingDeclaration();
if (containingDeclaration == null) {
return false;
}
return containingDeclaration.equals(second);
}
};
}
}
@@ -0,0 +1,5 @@
package a.foo
import b.foo.*
fun box() = (A().tadada(A()))
@@ -0,0 +1,5 @@
package b.foo
class A() {
fun tadada(a : A) = true
}
@@ -0,0 +1,3 @@
package a.foo
fun box() = (b.foo.f() == 1)
@@ -0,0 +1,3 @@
package b.foo
fun f() = 1
@@ -0,0 +1,5 @@
package a.foo
import b.foo.f
fun box() = (f() == 1)
@@ -0,0 +1,3 @@
package b.foo
fun f() = 1
@@ -0,0 +1,7 @@
package foo1.foo2.foo3.foo5.foo6.foo7.foo8
fun box() = A().doBox()
class A() {
fun doBox() = true
}