Refactoring.

This commit is contained in:
Pavel Talanov
2011-11-22 21:39:55 +04:00
parent f5e5ac42dd
commit 88f061234b
13 changed files with 37 additions and 45 deletions
@@ -38,8 +38,8 @@ public final class ClassDeclarationTranslator extends AbstractTranslator {
} }
public void generateDeclarations() { public void generateDeclarations() {
//TODO: hardcoded string declarationsObject = translationContext().enclosingScope().declareName(Namer.nameForClassesVariable());
declarationsObject = translationContext().enclosingScope().declareName("classes"); assert declarationsObject != null;
declarationsStatement = declarationsStatement =
AstUtil.newAssignmentStatement(declarationsObject.makeRef(), generateDummyFunctionInvocation()); AstUtil.newAssignmentStatement(declarationsObject.makeRef(), generateDummyFunctionInvocation());
} }
@@ -72,7 +72,6 @@ public final class ClassInitializerVisitor extends AbstractInitializerVisitor {
return result; return result;
} }
@Override @Override
@NotNull @NotNull
public List<JsStatement> visitClass(@NotNull JetClass expression, @NotNull TranslationContext context) { public List<JsStatement> visitClass(@NotNull JetClass expression, @NotNull TranslationContext context) {
@@ -50,14 +50,6 @@ public final class ClassTranslator extends AbstractTranslator {
} }
} }
//TODO
// @NotNull
// private JsStatement classDeclarationStatement(@NotNull JetClass classDeclaration,
// @NotNull JsInvocation jsClassDeclaration) {
// JsNameRef unqualifiedClassNameReference = translationContext().getNameForElement(classDeclaration).makeRef();
// return AstUtil.newAssignmentStatement(unqualifiedClassNameReference, jsClassDeclaration);
// }
private void addClassOwnDeclarations(@NotNull JetClass classDeclaration, private void addClassOwnDeclarations(@NotNull JetClass classDeclaration,
@NotNull JsInvocation jsClassDeclaration) { @NotNull JsInvocation jsClassDeclaration) {
JsObjectLiteral jsClassDescription = translateClassDeclarations(classDeclaration); JsObjectLiteral jsClassDescription = translateClassDeclarations(classDeclaration);
@@ -38,11 +38,10 @@ public final class DeclarationBodyVisitor extends TranslatorVisitor<List<JsPrope
return properties; return properties;
} }
//TODO
@Override @Override
@NotNull @NotNull
public List<JsPropertyInitializer> visitClass(@NotNull JetClass expression, @NotNull TranslationContext context) { public List<JsPropertyInitializer> visitClass(@NotNull JetClass expression, @NotNull TranslationContext context) {
//return Arrays.asList(Translation.classTranslator(context).translateClass(expression)); //TODO: we are interested only in class own declarations
return Collections.emptyList(); return Collections.emptyList();
} }
@@ -108,8 +108,9 @@ public final class ExpressionVisitor extends TranslatorVisitor<JsNode> {
private boolean isConstructorInvocation(@NotNull JetCallExpression expression, private boolean isConstructorInvocation(@NotNull JetCallExpression expression,
@NotNull TranslationContext context) { @NotNull TranslationContext context) {
ResolvedCall<?> resolvedCall = JetExpression calleeExpression = expression.getCalleeExpression();
BindingUtils.getResolvedCall(context.bindingContext(), expression.getCalleeExpression()); assert calleeExpression != null : "JetCallExpression should have not null callee";
ResolvedCall<?> resolvedCall = BindingUtils.getResolvedCall(context.bindingContext(), calleeExpression);
if (resolvedCall == null) { if (resolvedCall == null) {
return false; return false;
} }
@@ -22,14 +22,6 @@ public final class FunctionTranslator extends AbstractTranslator {
super(context); super(context);
} }
@NotNull
public JsStatement translateAsFunctionDeclaration(@NotNull JetNamedFunction expression) {
JsName functionName = translationContext().getNameForElement(expression);
JsFunction function = generateFunctionObject(expression);
return AstUtil.newAssignmentStatement
(translationContext().getNamespaceQualifiedReference(functionName), function);
}
@NotNull @NotNull
JsPropertyInitializer translateAsMethod(@NotNull JetNamedFunction expression) { JsPropertyInitializer translateAsMethod(@NotNull JetNamedFunction expression) {
JsName functionName = translationContext().getNameForElement(expression); JsName functionName = translationContext().getNameForElement(expression);
@@ -18,7 +18,6 @@ public final class GenerationState {
public GenerationState() { public GenerationState() {
} }
//TODO method too long
@NotNull @NotNull
public JsProgram compileCorrectNamespaces(@NotNull BindingContext bindingContext, @NotNull List<JetNamespace> namespaces) { public JsProgram compileCorrectNamespaces(@NotNull BindingContext bindingContext, @NotNull List<JetNamespace> namespaces) {
//TODO hardcoded //TODO hardcoded
@@ -83,5 +83,9 @@ public final class Namer {
return AstUtil.newQualifiedNameRef("initialize"); return AstUtil.newQualifiedNameRef("initialize");
} }
public static String nameForClassesVariable() {
return "classes";
}
} }
@@ -17,6 +17,8 @@ public final class NamespaceTranslator extends AbstractTranslator {
private final JetNamespace namespace; private final JetNamespace namespace;
@NotNull @NotNull
private final JsName namespaceName; private final JsName namespaceName;
@NotNull
private final ClassDeclarationTranslator classDeclarationTranslator;
@NotNull @NotNull
public static JsStatement translate(@NotNull TranslationContext context, public static JsStatement translate(@NotNull TranslationContext context,
@@ -28,23 +30,22 @@ public final class NamespaceTranslator extends AbstractTranslator {
super(context); super(context);
this.namespace = namespace; this.namespace = namespace;
this.namespaceName = context.getNameForElement(namespace); this.namespaceName = context.getNameForElement(namespace);
this.classDeclarationTranslator = new ClassDeclarationTranslator(context, namespace);
} }
@NotNull @NotNull
public JsStatement translateNamespace() { public JsStatement translateNamespace() {
ClassDeclarationTranslator translator = new ClassDeclarationTranslator(translationContext(), namespace); classDeclarationTranslator.generateDeclarations();
translator.generateDeclarations(); return AstUtil.newBlock(classDeclarationsStatement(),
JsName declarationsObjectName = translator.getDeclarationsObjectName(); namespaceOwnDeclarationStatement(),
JsBlock result = new JsBlock(); namespaceInitializeStatement());
JsInvocation namespaceDeclaration = namespaceCreateMethodInvocation();
namespaceDeclaration.getArguments().add(declarationsObjectName.makeRef());
addMemberDeclarations(namespaceDeclaration);
result.addStatement(translator.getDeclarationsStatement());
result.addStatement(namespaceDeclarationStatement(namespaceDeclaration));
result.addStatement(namespaceInitializeStatement());
return result;
} }
private JsStatement classDeclarationsStatement() {
return classDeclarationTranslator.getDeclarationsStatement();
}
@NotNull
private JsStatement namespaceInitializeStatement() { private JsStatement namespaceInitializeStatement() {
JsNameRef initializeMethodReference = Namer.initializeMethodReference(); JsNameRef initializeMethodReference = Namer.initializeMethodReference();
AstUtil.setQualifier(initializeMethodReference, namespaceName.makeRef()); AstUtil.setQualifier(initializeMethodReference, namespaceName.makeRef());
@@ -57,11 +58,18 @@ public final class NamespaceTranslator extends AbstractTranslator {
} }
@NotNull @NotNull
private JsStatement namespaceDeclarationStatement(@NotNull JsInvocation namespaceDeclaration) { private JsStatement namespaceOwnDeclarationStatement() {
JsInvocation namespaceDeclaration = namespaceCreateMethodInvocation();
addMemberDeclarations(namespaceDeclaration);
addClassesDeclarations(namespaceDeclaration);
return AstUtil.newAssignmentStatement return AstUtil.newAssignmentStatement
(translationContext().getNameForElement(namespace).makeRef(), namespaceDeclaration); (translationContext().getNameForElement(namespace).makeRef(), namespaceDeclaration);
} }
private void addClassesDeclarations(@NotNull JsInvocation namespaceDeclaration) {
namespaceDeclaration.getArguments().add(classDeclarationTranslator.getDeclarationsObjectName().makeRef());
}
private void addMemberDeclarations(@NotNull JsInvocation jsNamespace) { private void addMemberDeclarations(@NotNull JsInvocation jsNamespace) {
JsObjectLiteral jsClassDescription = translateNamespaceMemberDeclarations(); JsObjectLiteral jsClassDescription = translateNamespaceMemberDeclarations();
jsNamespace.getArguments().add(jsClassDescription); jsNamespace.getArguments().add(jsClassDescription);
@@ -1,6 +1,9 @@
package org.jetbrains.k2js.translate; package org.jetbrains.k2js.translate;
import com.google.dart.compiler.backend.js.ast.*; import com.google.dart.compiler.backend.js.ast.JsExpression;
import com.google.dart.compiler.backend.js.ast.JsInvocation;
import com.google.dart.compiler.backend.js.ast.JsName;
import com.google.dart.compiler.backend.js.ast.JsNameRef;
import com.google.dart.compiler.util.AstUtil; import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.ClassDescriptor; import org.jetbrains.jet.lang.descriptors.ClassDescriptor;
@@ -99,7 +102,6 @@ public final class PatternTranslator extends AbstractTranslator {
assert patternExpression != null : "Expression patter should have an expression."; assert patternExpression != null : "Expression patter should have an expression.";
JsExpression expressionToMatchAgainst = JsExpression expressionToMatchAgainst =
Translation.translateAsExpression(patternExpression, translationContext()); Translation.translateAsExpression(patternExpression, translationContext());
//TODO: should call equals method here return AstUtil.equals(expressionToMatch, expressionToMatchAgainst);
return new JsBinaryOperation(JsBinaryOperator.REF_EQ, expressionToMatch, expressionToMatchAgainst);
} }
} }
@@ -29,7 +29,6 @@ public final class ReferenceProvider {
this.requiresNamespaceQualifier = requiresNamespaceQualifier(); this.requiresNamespaceQualifier = requiresNamespaceQualifier();
} }
//TODO
@NotNull @NotNull
public JsNameRef generateCorrectReference() { public JsNameRef generateCorrectReference() {
if (requiresNamespaceQualifier) { if (requiresNamespaceQualifier) {
@@ -24,11 +24,7 @@ public class ReferenceTranslator extends AbstractTranslator {
@NotNull @NotNull
JsExpression translateSimpleName(@NotNull JetSimpleNameExpression expression) { JsExpression translateSimpleName(@NotNull JetSimpleNameExpression expression) {
//TODO: this is only a hack for now
// Problem is that namespace properties do not generate getters and setter actually so they must be referenced
// by name
JsExpression result; JsExpression result;
String name = expression.getReferencedName();
result = resolveAsPropertyAccess(expression); result = resolveAsPropertyAccess(expression);
if (result != null) { if (result != null) {
return result; return result;
@@ -13,7 +13,8 @@ import org.jetbrains.k2js.declarations.Declarations;
/** /**
* @author Talanov Pavel * @author Talanov Pavel
* <p/> * <p/>
* This class is a factory for obtaining instances of translators. * This class provides a interface which all translators use to interact with each other.
* Goal is to simlify interaction between translators.
*/ */
public final class Translation { public final class Translation {