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() {
//TODO: hardcoded string
declarationsObject = translationContext().enclosingScope().declareName("classes");
declarationsObject = translationContext().enclosingScope().declareName(Namer.nameForClassesVariable());
assert declarationsObject != null;
declarationsStatement =
AstUtil.newAssignmentStatement(declarationsObject.makeRef(), generateDummyFunctionInvocation());
}
@@ -72,7 +72,6 @@ public final class ClassInitializerVisitor extends AbstractInitializerVisitor {
return result;
}
@Override
@NotNull
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,
@NotNull JsInvocation jsClassDeclaration) {
JsObjectLiteral jsClassDescription = translateClassDeclarations(classDeclaration);
@@ -38,11 +38,10 @@ public final class DeclarationBodyVisitor extends TranslatorVisitor<List<JsPrope
return properties;
}
//TODO
@Override
@NotNull
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();
}
@@ -108,8 +108,9 @@ public final class ExpressionVisitor extends TranslatorVisitor<JsNode> {
private boolean isConstructorInvocation(@NotNull JetCallExpression expression,
@NotNull TranslationContext context) {
ResolvedCall<?> resolvedCall =
BindingUtils.getResolvedCall(context.bindingContext(), expression.getCalleeExpression());
JetExpression calleeExpression = expression.getCalleeExpression();
assert calleeExpression != null : "JetCallExpression should have not null callee";
ResolvedCall<?> resolvedCall = BindingUtils.getResolvedCall(context.bindingContext(), calleeExpression);
if (resolvedCall == null) {
return false;
}
@@ -22,14 +22,6 @@ public final class FunctionTranslator extends AbstractTranslator {
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
JsPropertyInitializer translateAsMethod(@NotNull JetNamedFunction expression) {
JsName functionName = translationContext().getNameForElement(expression);
@@ -18,7 +18,6 @@ public final class GenerationState {
public GenerationState() {
}
//TODO method too long
@NotNull
public JsProgram compileCorrectNamespaces(@NotNull BindingContext bindingContext, @NotNull List<JetNamespace> namespaces) {
//TODO hardcoded
@@ -83,5 +83,9 @@ public final class Namer {
return AstUtil.newQualifiedNameRef("initialize");
}
public static String nameForClassesVariable() {
return "classes";
}
}
@@ -17,6 +17,8 @@ public final class NamespaceTranslator extends AbstractTranslator {
private final JetNamespace namespace;
@NotNull
private final JsName namespaceName;
@NotNull
private final ClassDeclarationTranslator classDeclarationTranslator;
@NotNull
public static JsStatement translate(@NotNull TranslationContext context,
@@ -28,23 +30,22 @@ public final class NamespaceTranslator extends AbstractTranslator {
super(context);
this.namespace = namespace;
this.namespaceName = context.getNameForElement(namespace);
this.classDeclarationTranslator = new ClassDeclarationTranslator(context, namespace);
}
@NotNull
public JsStatement translateNamespace() {
ClassDeclarationTranslator translator = new ClassDeclarationTranslator(translationContext(), namespace);
translator.generateDeclarations();
JsName declarationsObjectName = translator.getDeclarationsObjectName();
JsBlock result = new JsBlock();
JsInvocation namespaceDeclaration = namespaceCreateMethodInvocation();
namespaceDeclaration.getArguments().add(declarationsObjectName.makeRef());
addMemberDeclarations(namespaceDeclaration);
result.addStatement(translator.getDeclarationsStatement());
result.addStatement(namespaceDeclarationStatement(namespaceDeclaration));
result.addStatement(namespaceInitializeStatement());
return result;
classDeclarationTranslator.generateDeclarations();
return AstUtil.newBlock(classDeclarationsStatement(),
namespaceOwnDeclarationStatement(),
namespaceInitializeStatement());
}
private JsStatement classDeclarationsStatement() {
return classDeclarationTranslator.getDeclarationsStatement();
}
@NotNull
private JsStatement namespaceInitializeStatement() {
JsNameRef initializeMethodReference = Namer.initializeMethodReference();
AstUtil.setQualifier(initializeMethodReference, namespaceName.makeRef());
@@ -57,11 +58,18 @@ public final class NamespaceTranslator extends AbstractTranslator {
}
@NotNull
private JsStatement namespaceDeclarationStatement(@NotNull JsInvocation namespaceDeclaration) {
private JsStatement namespaceOwnDeclarationStatement() {
JsInvocation namespaceDeclaration = namespaceCreateMethodInvocation();
addMemberDeclarations(namespaceDeclaration);
addClassesDeclarations(namespaceDeclaration);
return AstUtil.newAssignmentStatement
(translationContext().getNameForElement(namespace).makeRef(), namespaceDeclaration);
}
private void addClassesDeclarations(@NotNull JsInvocation namespaceDeclaration) {
namespaceDeclaration.getArguments().add(classDeclarationTranslator.getDeclarationsObjectName().makeRef());
}
private void addMemberDeclarations(@NotNull JsInvocation jsNamespace) {
JsObjectLiteral jsClassDescription = translateNamespaceMemberDeclarations();
jsNamespace.getArguments().add(jsClassDescription);
@@ -1,6 +1,9 @@
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 org.jetbrains.annotations.NotNull;
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.";
JsExpression expressionToMatchAgainst =
Translation.translateAsExpression(patternExpression, translationContext());
//TODO: should call equals method here
return new JsBinaryOperation(JsBinaryOperator.REF_EQ, expressionToMatch, expressionToMatchAgainst);
return AstUtil.equals(expressionToMatch, expressionToMatchAgainst);
}
}
@@ -29,7 +29,6 @@ public final class ReferenceProvider {
this.requiresNamespaceQualifier = requiresNamespaceQualifier();
}
//TODO
@NotNull
public JsNameRef generateCorrectReference() {
if (requiresNamespaceQualifier) {
@@ -24,11 +24,7 @@ public class ReferenceTranslator extends AbstractTranslator {
@NotNull
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;
String name = expression.getReferencedName();
result = resolveAsPropertyAccess(expression);
if (result != null) {
return result;
@@ -13,7 +13,8 @@ import org.jetbrains.k2js.declarations.Declarations;
/**
* @author Talanov Pavel
* <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 {