implement multinamespace translation:

classes belonging to namespace are passed to Namespace.create()
This commit is contained in:
Pavel Talanov
2012-02-21 17:09:37 +04:00
parent 55cfb8ce36
commit 6ca5b732de
3 changed files with 34 additions and 14 deletions
@@ -128,7 +128,6 @@ public final class StaticContext {
return namingScope;
}
@NotNull
public JsName getNameForDescriptor(@NotNull DeclarationDescriptor descriptor) {
JsName name = names.get(descriptor.getOriginal());
@@ -136,7 +135,6 @@ public final class StaticContext {
return name;
}
private final class NameGenerator extends Generator<JsName> {
public NameGenerator() {
Rule<JsName> namesForStandardClasses = new Rule<JsName>() {
@@ -5,6 +5,7 @@ import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
import org.jetbrains.jet.lang.psi.JetClass;
import org.jetbrains.k2js.translate.context.Namer;
import org.jetbrains.k2js.translate.context.TranslationContext;
@@ -18,6 +19,8 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.jetbrains.k2js.translate.utils.DescriptorUtils.getAllClassesDefinedInNamespace;
/**
* @author Pavel Talanov
* <p/>
@@ -129,4 +132,20 @@ public final class ClassDeclarationTranslator extends AbstractTranslator {
context().aliaser().setAliasForDescriptor(descriptor, localAlias);
return localAlias;
}
@NotNull
public JsObjectLiteral classDeclarationsForNamespace(@NotNull NamespaceDescriptor namespaceDescriptor) {
JsObjectLiteral result = new JsObjectLiteral();
for (ClassDescriptor classDescriptor : getAllClassesDefinedInNamespace(namespaceDescriptor)) {
result.getPropertyInitializers().add(getClassNameToClassObject(classDescriptor));
}
return result;
}
@NotNull
private JsPropertyInitializer getClassNameToClassObject(@NotNull ClassDescriptor classDescriptor) {
JsName className = context().getNameForDescriptor(classDescriptor);
JsNameRef alreadyDefinedClassReferernce = AstUtil.qualified(className, getDeclarationsObjectName().makeRef());
return new JsPropertyInitializer(className.makeRef(), alreadyDefinedClassReferernce);
}
}
@@ -18,28 +18,29 @@ import static org.jetbrains.k2js.translate.utils.DescriptorUtils.getAllClassesDe
/**
* @author Pavel.Talanov
* <p/>
* Genereate code for a single namespace.
* Genereate code for a single descriptor.
*/
public final class NamespaceTranslator extends AbstractTranslator {
@NotNull
private final NamespaceDescriptor namespace;
private final NamespaceDescriptor descriptor;
@NotNull
private final JsName namespaceName;
@NotNull
private final ClassDeclarationTranslator classDeclarationTranslator;
@NotNull
public static JsStatement translateNamespace(@NotNull NamespaceDescriptor namespace, @NotNull TranslationContext context) {
public static JsStatement translateNamespace(@NotNull NamespaceDescriptor namespace,
@NotNull TranslationContext context) {
return (new NamespaceTranslator(namespace, context)).translateNamespace();
}
private NamespaceTranslator(@NotNull NamespaceDescriptor namespace, @NotNull TranslationContext context) {
super(context.newDeclaration(namespace));
this.namespace = namespace;
this.namespaceName = context.getNameForDescriptor(namespace);
private NamespaceTranslator(@NotNull NamespaceDescriptor descriptor, @NotNull TranslationContext context) {
super(context.newDeclaration(descriptor));
this.descriptor = descriptor;
this.namespaceName = context.getNameForDescriptor(descriptor);
this.classDeclarationTranslator = new ClassDeclarationTranslator(context(),
getAllClassesDefinedInNamespace(namespace));
getAllClassesDefinedInNamespace(descriptor));
}
@NotNull
@@ -54,10 +55,12 @@ public final class NamespaceTranslator extends AbstractTranslator {
}
//TODO: at the moment this check is very ineffective, possible solution is to cash the result of getDFN
// other solution is to determine it's not affecting performance :D
private boolean isNamespaceEmpty() {
return BindingUtils.getDeclarationsForNamespace(context().bindingContext(), namespace).isEmpty();
return BindingUtils.getDeclarationsForNamespace(context().bindingContext(), descriptor).isEmpty();
}
@NotNull
private JsStatement classDeclarationsStatement() {
return classDeclarationTranslator.getDeclarationsStatement();
}
@@ -83,7 +86,7 @@ public final class NamespaceTranslator extends AbstractTranslator {
}
private void addClassesDeclarations(@NotNull JsInvocation namespaceDeclaration) {
namespaceDeclaration.getArguments().add(classDeclarationTranslator.getDeclarationsObjectName().makeRef());
namespaceDeclaration.getArguments().add(classDeclarationTranslator.classDeclarationsForNamespace(descriptor));
}
private void addMemberDeclarations(@NotNull JsInvocation jsNamespace) {
@@ -94,8 +97,8 @@ public final class NamespaceTranslator extends AbstractTranslator {
@NotNull
private JsObjectLiteral translateNamespaceMemberDeclarations() {
List<JsPropertyInitializer> propertyList = new ArrayList<JsPropertyInitializer>();
propertyList.add(Translation.generateNamespaceInitializerMethod(namespace, context()));
propertyList.addAll(new DeclarationBodyVisitor().traverseNamespace(namespace, context()));
propertyList.add(Translation.generateNamespaceInitializerMethod(descriptor, context()));
propertyList.addAll(new DeclarationBodyVisitor().traverseNamespace(descriptor, context()));
return new JsObjectLiteral(propertyList);
}
}