From 845189ca287c96b864bd1442fe31e591a9c6f883 Mon Sep 17 00:00:00 2001 From: develar Date: Sun, 30 Sep 2012 12:36:34 +0400 Subject: [PATCH 01/27] move from my kotlin fork --- .idea/.name | 1 + .idea/artifacts/dart_ast_jar.xml | 10 + .idea/compiler.xml | 23 + .idea/encodings.xml | 5 + .idea/inspectionProfiles/Project_Default.xml | 58 + .../inspectionProfiles/profiles_settings.xml | 7 + .idea/misc.xml | 10 + .idea/modules.xml | 9 + .idea/scopes/scope_settings.xml | 5 + .idea/uiDesigner.xml | 125 ++ .idea/vcs.xml | 7 + former-dast-ast.iml | 27 + src/com/google/dart/compiler/License.txt | 5 + src/com/google/dart/compiler/Source.java | 45 + .../js/JsConstructExpressionVisitor.java | 104 ++ .../backend/js/JsFirstExpressionVisitor.java | 134 ++ .../backend/js/JsPrecedenceVisitor.java | 280 ++++ .../backend/js/JsRequiresSemiVisitor.java | 158 ++ .../backend/js/JsReservedIdentifiers.java | 151 ++ .../backend/js/JsSourceGenerationVisitor.java | 37 + .../js/JsToStringGenerationVisitor.java | 1306 +++++++++++++++++ .../backend/js/ast/CanBooleanEval.java | 13 + .../compiler/backend/js/ast/HasArguments.java | 15 + .../compiler/backend/js/ast/HasCondition.java | 15 + .../dart/compiler/backend/js/ast/HasName.java | 14 + .../backend/js/ast/JsArrayAccess.java | 68 + .../backend/js/ast/JsArrayLiteral.java | 71 + .../backend/js/ast/JsBinaryOperation.java | 100 ++ .../backend/js/ast/JsBinaryOperator.java | 115 ++ .../dart/compiler/backend/js/ast/JsBlock.java | 58 + .../dart/compiler/backend/js/ast/JsBreak.java | 33 + .../dart/compiler/backend/js/ast/JsCase.java | 39 + .../dart/compiler/backend/js/ast/JsCatch.java | 66 + .../compiler/backend/js/ast/JsCatchScope.java | 36 + .../backend/js/ast/JsConditional.java | 77 + .../compiler/backend/js/ast/JsContext.java | 27 + .../compiler/backend/js/ast/JsContinue.java | 42 + .../compiler/backend/js/ast/JsDebugger.java | 25 + .../compiler/backend/js/ast/JsDefault.java | 28 + .../compiler/backend/js/ast/JsDoWhile.java | 31 + .../dart/compiler/backend/js/ast/JsEmpty.java | 25 + .../compiler/backend/js/ast/JsExprStmt.java | 32 + .../compiler/backend/js/ast/JsExpression.java | 25 + .../backend/js/ast/JsExpressionImpl.java | 27 + .../dart/compiler/backend/js/ast/JsFor.java | 99 ++ .../dart/compiler/backend/js/ast/JsForIn.java | 71 + .../compiler/backend/js/ast/JsFunction.java | 112 ++ .../backend/js/ast/JsGlobalBlock.java | 19 + .../dart/compiler/backend/js/ast/JsIf.java | 69 + .../compiler/backend/js/ast/JsInvocation.java | 83 ++ .../dart/compiler/backend/js/ast/JsLabel.java | 52 + .../compiler/backend/js/ast/JsLiteral.java | 100 ++ .../dart/compiler/backend/js/ast/JsName.java | 53 + .../compiler/backend/js/ast/JsNameRef.java | 125 ++ .../dart/compiler/backend/js/ast/JsNew.java | 66 + .../dart/compiler/backend/js/ast/JsNode.java | 9 + .../compiler/backend/js/ast/JsNodeImpl.java | 41 + .../backend/js/ast/JsNullLiteral.java | 41 + .../backend/js/ast/JsNumberLiteral.java | 78 + .../backend/js/ast/JsObjectLiteral.java | 83 ++ .../compiler/backend/js/ast/JsOperator.java | 28 + .../compiler/backend/js/ast/JsParameter.java | 39 + .../backend/js/ast/JsPostfixOperation.java | 42 + .../backend/js/ast/JsPrefixOperation.java | 69 + .../compiler/backend/js/ast/JsProgram.java | 128 ++ .../backend/js/ast/JsProgramFragment.java | 34 + .../backend/js/ast/JsPropertyInitializer.java | 54 + .../compiler/backend/js/ast/JsRegExp.java | 64 + .../compiler/backend/js/ast/JsReturn.java | 43 + .../compiler/backend/js/ast/JsRootScope.java | 42 + .../dart/compiler/backend/js/ast/JsScope.java | 176 +++ .../compiler/backend/js/ast/JsStatement.java | 8 + .../backend/js/ast/JsStringLiteral.java | 53 + .../compiler/backend/js/ast/JsSwitch.java | 47 + .../backend/js/ast/JsSwitchMember.java | 24 + .../dart/compiler/backend/js/ast/JsThrow.java | 37 + .../dart/compiler/backend/js/ast/JsTry.java | 66 + .../backend/js/ast/JsUnaryOperation.java | 53 + .../backend/js/ast/JsUnaryOperator.java | 78 + .../backend/js/ast/JsValueLiteral.java | 23 + .../dart/compiler/backend/js/ast/JsVars.java | 137 ++ .../compiler/backend/js/ast/JsVisitable.java | 18 + .../compiler/backend/js/ast/JsVisitor.java | 435 ++++++ .../dart/compiler/backend/js/ast/JsWhile.java | 51 + .../compiler/backend/js/ast/NodeKind.java | 50 + .../dart/compiler/common/AbstractNode.java | 57 + .../dart/compiler/common/HasSourceInfo.java | 55 + .../dart/compiler/common/HasSymbol.java | 16 + .../dart/compiler/common/SourceInfo.java | 59 + .../dart/compiler/common/SourceInfoImpl.java | 44 + .../google/dart/compiler/common/Symbol.java | 11 + .../google/dart/compiler/util/AstUtil.java | 49 + src/com/google/dart/compiler/util/Lists.java | 267 ++++ src/com/google/dart/compiler/util/Maps.java | 41 + .../google/dart/compiler/util/TextOutput.java | 38 + .../dart/compiler/util/TextOutputImpl.java | 174 +++ 96 files changed, 7200 insertions(+) create mode 100644 .idea/.name create mode 100644 .idea/artifacts/dart_ast_jar.xml create mode 100644 .idea/compiler.xml create mode 100644 .idea/encodings.xml create mode 100644 .idea/inspectionProfiles/Project_Default.xml create mode 100644 .idea/inspectionProfiles/profiles_settings.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/scopes/scope_settings.xml create mode 100644 .idea/uiDesigner.xml create mode 100644 .idea/vcs.xml create mode 100644 former-dast-ast.iml create mode 100644 src/com/google/dart/compiler/License.txt create mode 100644 src/com/google/dart/compiler/Source.java create mode 100644 src/com/google/dart/compiler/backend/js/JsConstructExpressionVisitor.java create mode 100644 src/com/google/dart/compiler/backend/js/JsFirstExpressionVisitor.java create mode 100644 src/com/google/dart/compiler/backend/js/JsPrecedenceVisitor.java create mode 100644 src/com/google/dart/compiler/backend/js/JsRequiresSemiVisitor.java create mode 100644 src/com/google/dart/compiler/backend/js/JsReservedIdentifiers.java create mode 100644 src/com/google/dart/compiler/backend/js/JsSourceGenerationVisitor.java create mode 100644 src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/CanBooleanEval.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/HasArguments.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/HasCondition.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/HasName.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsArrayAccess.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsArrayLiteral.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsBinaryOperation.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsBinaryOperator.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsBlock.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsBreak.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsCase.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsCatch.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsCatchScope.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsConditional.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsContext.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsContinue.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsDebugger.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsDefault.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsDoWhile.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsEmpty.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsExprStmt.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsExpression.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsExpressionImpl.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsFor.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsForIn.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsFunction.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsGlobalBlock.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsIf.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsInvocation.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsLabel.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsLiteral.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsName.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsNameRef.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsNew.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsNode.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsNodeImpl.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsNullLiteral.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsNumberLiteral.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsObjectLiteral.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsOperator.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsParameter.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsPostfixOperation.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsPrefixOperation.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsProgram.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsProgramFragment.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsPropertyInitializer.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsRegExp.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsReturn.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsRootScope.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsScope.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsStatement.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsStringLiteral.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsSwitch.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsSwitchMember.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsThrow.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsTry.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsUnaryOperation.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsUnaryOperator.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsValueLiteral.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsVars.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsVisitable.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsVisitor.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsWhile.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/NodeKind.java create mode 100644 src/com/google/dart/compiler/common/AbstractNode.java create mode 100644 src/com/google/dart/compiler/common/HasSourceInfo.java create mode 100644 src/com/google/dart/compiler/common/HasSymbol.java create mode 100644 src/com/google/dart/compiler/common/SourceInfo.java create mode 100644 src/com/google/dart/compiler/common/SourceInfoImpl.java create mode 100644 src/com/google/dart/compiler/common/Symbol.java create mode 100644 src/com/google/dart/compiler/util/AstUtil.java create mode 100644 src/com/google/dart/compiler/util/Lists.java create mode 100644 src/com/google/dart/compiler/util/Maps.java create mode 100644 src/com/google/dart/compiler/util/TextOutput.java create mode 100644 src/com/google/dart/compiler/util/TextOutputImpl.java diff --git a/.idea/.name b/.idea/.name new file mode 100644 index 00000000000..a02436dd37e --- /dev/null +++ b/.idea/.name @@ -0,0 +1 @@ +former-dast-ast \ No newline at end of file diff --git a/.idea/artifacts/dart_ast_jar.xml b/.idea/artifacts/dart_ast_jar.xml new file mode 100644 index 00000000000..501533125dd --- /dev/null +++ b/.idea/artifacts/dart_ast_jar.xml @@ -0,0 +1,10 @@ + + + $PROJECT_DIR$/out/artifacts + + + + + + + \ No newline at end of file diff --git a/.idea/compiler.xml b/.idea/compiler.xml new file mode 100644 index 00000000000..217af471a9e --- /dev/null +++ b/.idea/compiler.xml @@ -0,0 +1,23 @@ + + + + + + diff --git a/.idea/encodings.xml b/.idea/encodings.xml new file mode 100644 index 00000000000..e206d70d859 --- /dev/null +++ b/.idea/encodings.xml @@ -0,0 +1,5 @@ + + + + + diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 00000000000..9ff76d8c843 --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,58 @@ + + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/profiles_settings.xml b/.idea/inspectionProfiles/profiles_settings.xml new file mode 100644 index 00000000000..3b312839bf2 --- /dev/null +++ b/.idea/inspectionProfiles/profiles_settings.xml @@ -0,0 +1,7 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000000..97320410eec --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000000..9d41cd5a8e0 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,9 @@ + + + + + + + + + diff --git a/.idea/scopes/scope_settings.xml b/.idea/scopes/scope_settings.xml new file mode 100644 index 00000000000..922003b8433 --- /dev/null +++ b/.idea/scopes/scope_settings.xml @@ -0,0 +1,5 @@ + + + + \ No newline at end of file diff --git a/.idea/uiDesigner.xml b/.idea/uiDesigner.xml new file mode 100644 index 00000000000..3b000203088 --- /dev/null +++ b/.idea/uiDesigner.xml @@ -0,0 +1,125 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000000..275077f8255 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/former-dast-ast.iml b/former-dast-ast.iml new file mode 100644 index 00000000000..9d6c93e3d36 --- /dev/null +++ b/former-dast-ast.iml @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/com/google/dart/compiler/License.txt b/src/com/google/dart/compiler/License.txt new file mode 100644 index 00000000000..eb4e1abc135 --- /dev/null +++ b/src/com/google/dart/compiler/License.txt @@ -0,0 +1,5 @@ +Google Dart Js backend was removed - https://code.google.com/p/dart/source/detail?r=4771 + +According to http://www.apache.org/legal/3party.html we can include "Google Dart Js backend" in source form, because code license is "New BSD License" (Authorized License). + +This part of code will be removed when kotlin will be rewritten on kotlin. diff --git a/src/com/google/dart/compiler/Source.java b/src/com/google/dart/compiler/Source.java new file mode 100644 index 00000000000..f0ff5797f73 --- /dev/null +++ b/src/com/google/dart/compiler/Source.java @@ -0,0 +1,45 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler; + +import java.io.IOException; +import java.io.Reader; +import java.net.URI; +import java.util.Date; + +/** + * Abstract interface to a source file. + */ +public interface Source { + + /** + * Determines whether the given source exists. + */ + boolean exists(); + + /** + * Returns the last-modified timestamp for this source, using the same units as + * {@link Date#getTime()}. + */ + long getLastModified(); + + /** + * Gets the name of this source. + */ + String getName(); + + /** + * Gets a reader for the dart file's source code. The caller is responsible for closing the + * returned reader. + */ + Reader getSourceReader() throws IOException; + + /** + * Gets the identifier for this source. This is used to uniquely identify the + * source, but should not be used to obtain the source content. Use + * {@link #getSourceReader()} to obtain the source content. + */ + URI getUri(); +} diff --git a/src/com/google/dart/compiler/backend/js/JsConstructExpressionVisitor.java b/src/com/google/dart/compiler/backend/js/JsConstructExpressionVisitor.java new file mode 100644 index 00000000000..cb3df42ca9b --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/JsConstructExpressionVisitor.java @@ -0,0 +1,104 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js; + +import com.google.dart.compiler.backend.js.ast.*; + +/** + * Searches for method invocations in constructor expressions that would not + * normally be surrounded by parentheses. + */ +public class JsConstructExpressionVisitor extends JsVisitor { + + public static boolean exec(JsExpression expression) { + if (JsPrecedenceVisitor.exec(expression) < JsPrecedenceVisitor.PRECEDENCE_NEW) { + return true; + } + JsConstructExpressionVisitor visitor = new JsConstructExpressionVisitor(); + visitor.accept(expression); + return visitor.containsInvocation; + } + + private boolean containsInvocation = false; + + private JsConstructExpressionVisitor() { + } + + /** + * We only look at the array expression since the index has its own scope. + */ + @Override + public boolean visit(JsArrayAccess x, JsContext ctx) { + accept(x.getArrayExpr()); + return false; + } + + /** + * Array literals have their own scoping. + */ + @Override + public boolean visit(JsArrayLiteral x, JsContext ctx) { + return false; + } + + /** + * Functions have their own scoping. + */ + @Override + public boolean visit(JsFunction x, JsContext ctx) { + return false; + } + + @Override + public boolean visit(JsInvocation x, JsContext ctx) { + containsInvocation = true; + return false; + } + + @Override + public boolean visit(JsNameRef x, JsContext ctx) { + if (!x.isLeaf()) { + accept(x.getQualifier()); + } + return false; + } + + /** + * New constructs bind to the nearest set of parentheses. + */ + @Override + public boolean visit(JsNew x, JsContext ctx) { + return false; + } + + /** + * Object literals have their own scope. + */ + @Override + public boolean visit(JsObjectLiteral x, JsContext ctx) { + return false; + } + + /** + * We only look at nodes that would not normally be surrounded by parentheses. + */ + @Override + protected T doAccept(T node) { + // Assign to Object to prevent 'inconvertible types' compile errors due + // to http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6548436 + // reproducible in jdk1.6.0_02. + Object o = node; + if (o instanceof JsExpression) { + JsExpression expression = (JsExpression) o; + int precedence = JsPrecedenceVisitor.exec(expression); + // Only visit expressions that won't automatically be surrounded by + // parentheses + if (precedence < JsPrecedenceVisitor.PRECEDENCE_NEW) { + return node; + } + } + return super.doAccept(node); + } +} diff --git a/src/com/google/dart/compiler/backend/js/JsFirstExpressionVisitor.java b/src/com/google/dart/compiler/backend/js/JsFirstExpressionVisitor.java new file mode 100644 index 00000000000..0ae823c60e5 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/JsFirstExpressionVisitor.java @@ -0,0 +1,134 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js; + +import com.google.dart.compiler.backend.js.ast.JsArrayAccess; +import com.google.dart.compiler.backend.js.ast.JsArrayLiteral; +import com.google.dart.compiler.backend.js.ast.JsBinaryOperation; +import com.google.dart.compiler.backend.js.ast.JsConditional; +import com.google.dart.compiler.backend.js.ast.JsContext; +import com.google.dart.compiler.backend.js.ast.JsExprStmt; +import com.google.dart.compiler.backend.js.ast.JsExpression; +import com.google.dart.compiler.backend.js.ast.JsFunction; +import com.google.dart.compiler.backend.js.ast.JsInvocation; +import com.google.dart.compiler.backend.js.ast.JsNameRef; +import com.google.dart.compiler.backend.js.ast.JsNew; +import com.google.dart.compiler.backend.js.ast.JsObjectLiteral; +import com.google.dart.compiler.backend.js.ast.JsPostfixOperation; +import com.google.dart.compiler.backend.js.ast.JsPrefixOperation; +import com.google.dart.compiler.backend.js.ast.JsRegExp; +import com.google.dart.compiler.backend.js.ast.JsVisitor; + +/** + * Determines if an expression statement needs to be surrounded by parentheses. + *

+ * The statement or the left-most expression needs to be surrounded by + * parentheses if the left-most expression is an object literal or a function + * object. Function declarations do not need parentheses. + *

+ * For example the following require parentheses:
+ *

+ *

+ * The following do not require parentheses:
+ *

+ */ +public class JsFirstExpressionVisitor extends JsVisitor { + public static boolean exec(JsExprStmt statement) { + JsExpression expression = statement.getExpression(); + // Pure function declarations do not need parentheses + if (expression instanceof JsFunction) { + return false; + } + + JsFirstExpressionVisitor visitor = new JsFirstExpressionVisitor(); + visitor.accept(statement.getExpression()); + return visitor.needsParentheses; + } + + private boolean needsParentheses = false; + + private JsFirstExpressionVisitor() { + } + + @Override + public boolean visit(JsArrayAccess x, JsContext ctx) { + accept(x.getArrayExpr()); + return false; + } + + @Override + public boolean visit(JsArrayLiteral x, JsContext ctx) { + return false; + } + + @Override + public boolean visit(JsBinaryOperation x, JsContext ctx) { + accept(x.getArg1()); + return false; + } + + @Override + public boolean visit(JsConditional x, JsContext ctx) { + accept(x.getTestExpression()); + return false; + } + + @Override + public boolean visit(JsFunction x, JsContext ctx) { + needsParentheses = true; + return false; + } + + @Override + public boolean visit(JsInvocation x, JsContext ctx) { + accept(x.getQualifier()); + return false; + } + + @Override + public boolean visit(JsNameRef x, JsContext ctx) { + if (!x.isLeaf()) { + accept(x.getQualifier()); + } + return false; + } + + @Override + public boolean visit(JsNew x, JsContext ctx) { + return false; + } + + @Override + public boolean visit(JsObjectLiteral x, JsContext ctx) { + needsParentheses = true; + return false; + } + + @Override + public boolean visit(JsPostfixOperation x, JsContext ctx) { + accept(x.getArg()); + return false; + } + + @Override + public boolean visit(JsPrefixOperation x, JsContext ctx) { + return false; + } + + @Override + public boolean visit(JsRegExp x, JsContext ctx) { + return false; + } +} diff --git a/src/com/google/dart/compiler/backend/js/JsPrecedenceVisitor.java b/src/com/google/dart/compiler/backend/js/JsPrecedenceVisitor.java new file mode 100644 index 00000000000..a9632298819 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/JsPrecedenceVisitor.java @@ -0,0 +1,280 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js; + +import com.google.dart.compiler.backend.js.ast.*; +import com.google.dart.compiler.backend.js.ast.JsVars.JsVar; + +/** + * Precedence indices from "JavaScript - The Definitive Guide" 4th Edition (page + * 57) + * + * Precedence 17 is for indivisible primaries that either don't have children, + * or provide their own delimiters. + * + * Precedence 16 is for really important things that have their own AST classes. + * + * Precedence 15 is for the new construct. + * + * Precedence 14 is for unary operators. + * + * Precedences 12 through 4 are for non-assigning binary operators. + * + * Precedence 3 is for the tertiary conditional. + * + * Precedence 2 is for assignments. + * + * Precedence 1 is for comma operations. + */ +class JsPrecedenceVisitor extends JsVisitor { + static final int PRECEDENCE_NEW = 15; + + public static int exec(JsExpression expression) { + JsPrecedenceVisitor visitor = new JsPrecedenceVisitor(); + visitor.accept(expression); + if (visitor.answer < 0) { + throw new RuntimeException("Precedence must be >= 0!"); + } + return visitor.answer; + } + + private int answer = -1; + + private JsPrecedenceVisitor() { + } + + @Override + public boolean visit(JsArrayAccess x, JsContext ctx) { + answer = 16; + return false; + } + + @Override + public boolean visit(JsArrayLiteral x, JsContext ctx) { + answer = 17; // primary + return false; + } + + @Override + public boolean visit(JsBinaryOperation x, JsContext ctx) { + answer = x.getOperator().getPrecedence(); + return false; + } + + @Override + public boolean visit(JsBlock x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } + + @Override + public boolean visit(JsLiteral.JsBooleanLiteral x, JsContext ctx) { + answer = 17; // primary + return false; + } + + @Override + public boolean visit(JsBreak x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } + + @Override + public boolean visit(JsCase x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } + + @Override + public boolean visit(JsCatch x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } + + @Override + public boolean visit(JsConditional x, JsContext ctx) { + answer = 3; + return false; + } + + @Override + public boolean visit(JsContinue x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } + + @Override + public boolean visit(JsDebugger x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } + + @Override + public boolean visit(JsDefault x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } + + @Override + public boolean visit(JsDoWhile x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } + + @Override + public boolean visit(JsEmpty x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } + + @Override + public boolean visit(JsExprStmt x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } + + @Override + public boolean visit(JsFor x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } + + @Override + public boolean visit(JsForIn x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } + + @Override + public boolean visit(JsFunction x, JsContext ctx) { + answer = 17; // primary + return false; + } + + @Override + public boolean visit(JsIf x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } + + @Override + public boolean visit(JsInvocation x, JsContext ctx) { + answer = 16; + return false; + } + + @Override + public boolean visit(JsLabel x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } + + @Override + public boolean visit(JsNameRef x, JsContext ctx) { + if (x.isLeaf()) { + answer = 17; // primary + } else { + answer = 16; // property access + } + return false; + } + + @Override + public boolean visit(JsNew x, JsContext ctx) { + answer = PRECEDENCE_NEW; + return false; + } + + @Override + public boolean visit(JsNullLiteral x, JsContext ctx) { + answer = 17; // primary + return false; + } + + @Override + public boolean visit(JsNumberLiteral.JsIntLiteral x, JsContext ctx) { + answer = 17; // primary + return false; + } + + @Override + public boolean visit(JsNumberLiteral.JsDoubleLiteral x, JsContext ctx) { + answer = 17; // primary + return false; + } + + @Override + public boolean visit(JsObjectLiteral x, JsContext ctx) { + answer = 17; // primary + return false; + } + + @Override + public boolean visit(JsParameter x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } + + @Override + public boolean visit(JsPostfixOperation x, JsContext ctx) { + answer = x.getOperator().getPrecedence(); + return false; + } + + @Override + public boolean visit(JsPrefixOperation x, JsContext ctx) { + answer = x.getOperator().getPrecedence(); + return false; + } + + @Override + public boolean visit(JsProgram x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } + + @Override + public boolean visit(JsPropertyInitializer x, JsContext ctx) { + answer = 17; // primary + return false; + } + + @Override + public boolean visit(JsRegExp x, JsContext ctx) { + answer = 17; // primary + return false; + } + + @Override + public boolean visit(JsReturn x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } + + @Override + public boolean visit(JsStringLiteral x, JsContext ctx) { + answer = 17; // primary + return false; + } + + @Override + public boolean visit(JsSwitch x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } + + @Override + public boolean visit(JsLiteral.JsThisRef x, JsContext ctx) { + answer = 17; // primary + return false; + } + + @Override + public boolean visit(JsThrow x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } + + @Override + public boolean visit(JsTry x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } + + @Override + public boolean visit(JsVar x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } + + @Override + public boolean visit(JsVars x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } + + @Override + public boolean visit(JsWhile x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } +} diff --git a/src/com/google/dart/compiler/backend/js/JsRequiresSemiVisitor.java b/src/com/google/dart/compiler/backend/js/JsRequiresSemiVisitor.java new file mode 100644 index 00000000000..bd13ea3af72 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/JsRequiresSemiVisitor.java @@ -0,0 +1,158 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js; + +import com.google.dart.compiler.backend.js.ast.JsBlock; +import com.google.dart.compiler.backend.js.ast.JsBreak; +import com.google.dart.compiler.backend.js.ast.JsContext; +import com.google.dart.compiler.backend.js.ast.JsDebugger; +import com.google.dart.compiler.backend.js.ast.JsDoWhile; +import com.google.dart.compiler.backend.js.ast.JsEmpty; +import com.google.dart.compiler.backend.js.ast.JsExprStmt; +import com.google.dart.compiler.backend.js.ast.JsFor; +import com.google.dart.compiler.backend.js.ast.JsForIn; +import com.google.dart.compiler.backend.js.ast.JsIf; +import com.google.dart.compiler.backend.js.ast.JsLabel; +import com.google.dart.compiler.backend.js.ast.JsReturn; +import com.google.dart.compiler.backend.js.ast.JsStatement; +import com.google.dart.compiler.backend.js.ast.JsSwitch; +import com.google.dart.compiler.backend.js.ast.JsThrow; +import com.google.dart.compiler.backend.js.ast.JsTry; +import com.google.dart.compiler.backend.js.ast.JsVars; +import com.google.dart.compiler.backend.js.ast.JsVisitor; +import com.google.dart.compiler.backend.js.ast.JsWhile; + +/** + * Determines if a statement at the end of a block requires a semicolon. + * + * For example, the following statements require semicolons:
+ * + * + * The following do not require semicolons:
+ * + */ +public class JsRequiresSemiVisitor extends JsVisitor { + + public static boolean exec(JsStatement lastStatement) { + JsRequiresSemiVisitor visitor = new JsRequiresSemiVisitor(); + visitor.accept(lastStatement); + return visitor.needsSemicolon; + } + + private boolean needsSemicolon = false; + + private JsRequiresSemiVisitor() { + } + + @Override + public boolean visit(JsBlock x, JsContext ctx) { + return false; + } + + @Override + public boolean visit(JsBreak x, JsContext ctx) { + return false; + } + + @Override + public boolean visit(JsDebugger x, JsContext ctx) { + return false; + } + + @Override + public boolean visit(JsDoWhile x, JsContext ctx) { + return false; + } + + @Override + public boolean visit(JsEmpty x, JsContext ctx) { + return false; + } + + @Override + public boolean visit(JsExprStmt x, JsContext ctx) { + return false; + } + + @Override + public boolean visit(JsFor x, JsContext ctx) { + if (x.getBody() instanceof JsEmpty) { + needsSemicolon = true; + } + return false; + } + + @Override + public boolean visit(JsForIn x, JsContext ctx) { + if (x.getBody() instanceof JsEmpty) { + needsSemicolon = true; + } + return false; + } + + @Override + public boolean visit(JsIf x, JsContext ctx) { + JsStatement thenStmt = x.getThenStatement(); + JsStatement elseStmt = x.getElseStatement(); + JsStatement toCheck = thenStmt; + if (elseStmt != null) { + toCheck = elseStmt; + } + if (toCheck instanceof JsEmpty) { + needsSemicolon = true; + } else { + // Must recurse to determine last statement (possible if-else chain). + accept(toCheck); + } + return false; + } + + @Override + public boolean visit(JsLabel x, JsContext ctx) { + if (x.getStmt() instanceof JsEmpty) { + needsSemicolon = true; + } + return false; + } + + @Override + public boolean visit(JsReturn x, JsContext ctx) { + return false; + } + + @Override + public boolean visit(JsSwitch x, JsContext ctx) { + return false; + } + + @Override + public boolean visit(JsThrow x, JsContext ctx) { + return false; + } + + @Override + public boolean visit(JsTry x, JsContext ctx) { + return false; + } + + @Override + public boolean visit(JsVars x, JsContext ctx) { + return false; + } + + @Override + public boolean visit(JsWhile x, JsContext ctx) { + if (x.getBody() instanceof JsEmpty) { + needsSemicolon = true; + } + return false; + } +} diff --git a/src/com/google/dart/compiler/backend/js/JsReservedIdentifiers.java b/src/com/google/dart/compiler/backend/js/JsReservedIdentifiers.java new file mode 100644 index 00000000000..da462ef29f4 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/JsReservedIdentifiers.java @@ -0,0 +1,151 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js; + +import gnu.trove.THashSet; + +import java.util.Collections; +import java.util.Set; + +/** + * Determines whether or not a particular string is a JavaScript keyword or not. + */ +public class JsReservedIdentifiers { + public static final Set reservedGlobalSymbols; + + static { + String[] commonBuiltins = new String[] { + // 15.1.1 Value Properties of the Global Object + "NaN", "Infinity", "undefined", + + // 15.1.2 Function Properties of the Global Object + "eval", "parseInt", "parseFloat", "isNan", "isFinite", + + // 15.1.3 URI Handling Function Properties + "decodeURI", "decodeURIComponent", + "encodeURI", + "encodeURIComponent", + + // 15.1.4 Constructor Properties of the Global Object + "Object", "Function", "Array", "String", "Boolean", "Number", "Date", + "RegExp", "Error", "EvalError", "RangeError", "ReferenceError", + "SyntaxError", "TypeError", "URIError", + + // 15.1.5 Other Properties of the Global Object + "Math", + + // 10.1.6 Activation Object + "arguments", + + // B.2 Additional Properties (non-normative) + "escape", "unescape", + + // Window props (https://developer.mozilla.org/en/DOM/window) + "applicationCache", "closed", "Components", "content", "controllers", + "crypto", "defaultStatus", "dialogArguments", "directories", + "document", "frameElement", "frames", "fullScreen", "globalStorage", + "history", "innerHeight", "innerWidth", "length", + "location", "locationbar", "localStorage", "menubar", + "mozInnerScreenX", "mozInnerScreenY", "mozScreenPixelsPerCssPixel", + "name", "navigator", "opener", "outerHeight", "outerWidth", + "pageXOffset", "pageYOffset", "parent", "personalbar", "pkcs11", + "returnValue", "screen", "scrollbars", "scrollMaxX", "scrollMaxY", + "self", "sessionStorage", "sidebar", "status", "statusbar", "toolbar", + "top", "window", + + // Window methods (https://developer.mozilla.org/en/DOM/window) + "alert", "addEventListener", "atob", "back", "blur", "btoa", + "captureEvents", "clearInterval", "clearTimeout", "close", "confirm", + "disableExternalCapture", "dispatchEvent", "dump", + "enableExternalCapture", "escape", "find", "focus", "forward", + "GeckoActiveXObject", "getAttention", "getAttentionWithCycleCount", + "getComputedStyle", "getSelection", "home", "maximize", "minimize", + "moveBy", "moveTo", "open", "openDialog", "postMessage", "print", + "prompt", "QueryInterface", "releaseEvents", "removeEventListener", + "resizeBy", "resizeTo", "restore", "routeEvent", "scroll", "scrollBy", + "scrollByLines", "scrollByPages", "scrollTo", "setInterval", + "setResizeable", "setTimeout", "showModalDialog", "sizeToContent", + "stop", "uuescape", "updateCommands", "XPCNativeWrapper", + "XPCSafeJSOjbectWrapper", + + // Mozilla Window event handlers, same cite + "onabort", "onbeforeunload", "onchange", "onclick", "onclose", + "oncontextmenu", "ondragdrop", "onerror", "onfocus", "onhashchange", + "onkeydown", "onkeypress", "onkeyup", "onload", "onmousedown", + "onmousemove", "onmouseout", "onmouseover", "onmouseup", + "onmozorientation", "onpaint", "onreset", "onresize", "onscroll", + "onselect", "onsubmit", "onunload", + + // Safari Web Content Guide + // http://developer.apple.com/library/safari/#documentation/AppleApplications/Reference/SafariWebContent/SafariWebContent.pdf + // WebKit Window member data, from WebKit DOM Reference + // (http://developer.apple.com/safari/library/documentation/AppleApplications/Reference/WebKitDOMRef/DOMWindow_idl/Classes/DOMWindow/index.html) + // TODO(fredsa) Many, many more functions and member data to add + "ontouchcancel", "ontouchend", "ontouchmove", "ontouchstart", + "ongesturestart", "ongesturechange", "ongestureend", + + // extra window methods + "uneval", + + // keywords https://developer.mozilla.org/en/New_in_JavaScript_1.7, + // https://developer.mozilla.org/en/New_in_JavaScript_1.8.1 + "getPrototypeOf", "let", "yield", + + // "future reserved words" + "abstract", "int", "short", "boolean", "interface", "static", "byte", + "long", "char", "final", "native", "synchronized", "float", "package", + "throws", "goto", "private", "transient", "implements", "protected", + "volatile", "double", "public", + + // IE methods + // (http://msdn.microsoft.com/en-us/library/ms535873(VS.85).aspx#) + "attachEvent", "clientInformation", "clipboardData", "createPopup", + "dialogHeight", "dialogLeft", "dialogTop", "dialogWidth", + "onafterprint", "onbeforedeactivate", "onbeforeprint", + "oncontrolselect", "ondeactivate", "onhelp", "onresizeend", + + // Common browser-defined identifiers not defined in ECMAScript + "event", "external", "Debug", "Enumerator", "Global", "Image", + "ActiveXObject", "VBArray", "Components", + + // Functions commonly defined on Object + "toString", "getClass", "constructor", "prototype", "valueOf", + + // Client-side JavaScript identifiers, which are needed for linkers + // that don't ensure GWT's window != $wnd, document != $doc, etc. + // Taken from the Rhino book, pg 715 + "Anchor", "Applet", "Attr", "Canvas", "CanvasGradient", + "CanvasPattern", "CanvasRenderingContext2D", "CDATASection", + "CharacterData", "Comment", "CSS2Properties", "CSSRule", + "CSSStyleSheet", "Document", "DocumentFragment", "DocumentType", + "DOMException", "DOMImplementation", "DOMParser", "Element", "Event", + "ExternalInterface", "FlashPlayer", "Form", "Frame", "History", + "HTMLCollection", "HTMLDocument", "HTMLElement", "IFrame", "Image", + "Input", "JSObject", "KeyEvent", "Link", "Location", "MimeType", + "MouseEvent", "Navigator", "Node", "NodeList", "Option", "Plugin", + "ProcessingInstruction", "Range", "RangeException", "Screen", "Select", + "Table", "TableCell", "TableRow", "TableSelection", "Text", "TextArea", + "UIEvent", "Window", "XMLHttpRequest", "XMLSerializer", + "XPathException", "XPathResult", "XSLTProcessor", + + // These keywords trigger the loading of the java-plugin. For the + // next-generation plugin, this results in starting a new Java process. + "java", "Packages", "netscape", "sun", "JavaObject", "JavaClass", + "JavaArray", "JavaMember", + + // GWT-defined identifiers + "$wnd", "$doc", "$entry", "$moduleName", "$moduleBase", "$gwt_version", "$sessionId", + + // Identifiers used by JsStackEmulator; later set to obfuscatable + "$stack", "$stackDepth", "$location", + }; + + reservedGlobalSymbols = new THashSet(commonBuiltins.length); + Collections.addAll(reservedGlobalSymbols, commonBuiltins); + } + + private JsReservedIdentifiers() { + } +} diff --git a/src/com/google/dart/compiler/backend/js/JsSourceGenerationVisitor.java b/src/com/google/dart/compiler/backend/js/JsSourceGenerationVisitor.java new file mode 100644 index 00000000000..de88bdb7b74 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/JsSourceGenerationVisitor.java @@ -0,0 +1,37 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js; + +import com.google.dart.compiler.backend.js.ast.JsBlock; +import com.google.dart.compiler.backend.js.ast.JsContext; +import com.google.dart.compiler.backend.js.ast.JsProgram; +import com.google.dart.compiler.backend.js.ast.JsProgramFragment; +import com.google.dart.compiler.util.TextOutput; + +/** + * Generates JavaScript source from an AST. + */ +public class JsSourceGenerationVisitor extends JsToStringGenerationVisitor { + public JsSourceGenerationVisitor(TextOutput out) { + super(out); + } + + @Override + public boolean visit(JsProgram program, JsContext ctx) { + return true; + } + + @Override + public boolean visit(JsProgramFragment x, JsContext ctx) { + // Descend naturally. + return true; + } + + @Override + public boolean visit(JsBlock x, JsContext ctx) { + printJsBlock(x, false, true); + return false; + } +} diff --git a/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java b/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java new file mode 100644 index 00000000000..50d29b64e08 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java @@ -0,0 +1,1306 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js; + +import com.google.dart.compiler.backend.js.ast.*; +import com.google.dart.compiler.backend.js.ast.JsVars.JsVar; +import com.google.dart.compiler.util.TextOutput; +import gnu.trove.TIntArrayList; + +import java.util.*; + +import static com.google.dart.compiler.backend.js.ast.JsNumberLiteral.JsDoubleLiteral; +import static com.google.dart.compiler.backend.js.ast.JsNumberLiteral.JsIntLiteral; + +/** + * Produces text output from a JavaScript AST. + */ +public class JsToStringGenerationVisitor extends JsVisitor { + private static final char[] CHARS_BREAK = "break".toCharArray(); + private static final char[] CHARS_CASE = "case".toCharArray(); + private static final char[] CHARS_CATCH = "catch".toCharArray(); + private static final char[] CHARS_CONTINUE = "continue".toCharArray(); + private static final char[] CHARS_DEBUGGER = "debugger".toCharArray(); + private static final char[] CHARS_DEFAULT = "default".toCharArray(); + private static final char[] CHARS_DO = "do".toCharArray(); + private static final char[] CHARS_ELSE = "else".toCharArray(); + private static final char[] CHARS_FALSE = "false".toCharArray(); + private static final char[] CHARS_FINALLY = "finally".toCharArray(); + private static final char[] CHARS_FOR = "for".toCharArray(); + private static final char[] CHARS_FUNCTION = "function".toCharArray(); + private static final char[] CHARS_IF = "if".toCharArray(); + private static final char[] CHARS_IN = "in".toCharArray(); + private static final char[] CHARS_NEW = "new".toCharArray(); + private static final char[] CHARS_NULL = "null".toCharArray(); + private static final char[] CHARS_RETURN = "return".toCharArray(); + private static final char[] CHARS_SWITCH = "switch".toCharArray(); + private static final char[] CHARS_THIS = "this".toCharArray(); + private static final char[] CHARS_THROW = "throw".toCharArray(); + private static final char[] CHARS_TRUE = "true".toCharArray(); + private static final char[] CHARS_TRY = "try".toCharArray(); + private static final char[] CHARS_VAR = "var".toCharArray(); + private static final char[] CHARS_WHILE = "while".toCharArray(); + private static final char[] HEX_DIGITS = { + '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'}; + + /** + * How many lines of code to print inside of a JsBlock when printing terse. + */ + private static final int JSBLOCK_LINES_TO_PRINT = 3; + + public static CharSequence javaScriptString(String value) { + return javaScriptString(value, false); + } + + /** + * Generate JavaScript code that evaluates to the supplied string. Adapted + * from {@link org.mozilla.javascript.ScriptRuntime#escapeString(String)} + * . The difference is that we quote with either " or ' depending on + * which one is used less inside the string. + */ + @SuppressWarnings({"ConstantConditions", "UnnecessaryFullyQualifiedName", "JavadocReference"}) + public static CharSequence javaScriptString(CharSequence chars, boolean forceDoubleQuote) { + final int n = chars.length(); + int quoteCount = 0; + int aposCount = 0; + + for (int i = 0; i < n; i++) { + switch (chars.charAt(i)) { + case '"': + ++quoteCount; + break; + case '\'': + ++aposCount; + break; + } + } + + StringBuilder result = new StringBuilder(n + 16); + + char quoteChar = (quoteCount < aposCount || forceDoubleQuote) ? '"' : '\''; + result.append(quoteChar); + + for (int i = 0; i < n; i++) { + char c = chars.charAt(i); + + if (' ' <= c && c <= '~' && c != quoteChar && c != '\\') { + // an ordinary print character (like C isprint()) + result.append(c); + continue; + } + + int escape = -1; + switch (c) { + case '\b': + escape = 'b'; + break; + case '\f': + escape = 'f'; + break; + case '\n': + escape = 'n'; + break; + case '\r': + escape = 'r'; + break; + case '\t': + escape = 't'; + break; + case '"': + escape = '"'; + break; // only reach here if == quoteChar + case '\'': + escape = '\''; + break; // only reach here if == quoteChar + case '\\': + escape = '\\'; + break; + } + + if (escape >= 0) { + // an \escaped sort of character + result.append('\\'); + result.append((char) escape); + } + else { + /* + * Emit characters from 0 to 31 that don't have a single character + * escape sequence in octal where possible. This saves one or two + * characters compared to the hexadecimal format '\xXX'. + * + * These short octal sequences may only be used at the end of the string + * or where the following character is a non-digit. Otherwise, the + * following character would be incorrectly interpreted as belonging to + * the sequence. + */ + if (c < ' ' && (i == n - 1 || chars.charAt(i + 1) < '0' || chars.charAt(i + 1) > '9')) { + result.append('\\'); + if (c > 0x7) { + result.append((char) ('0' + (0x7 & (c >> 3)))); + } + result.append((char) ('0' + (0x7 & c))); + } + else { + int hexSize; + if (c < 256) { + // 2-digit hex + result.append("\\x"); + hexSize = 2; + } + else { + // Unicode. + result.append("\\u"); + hexSize = 4; + } + // append hexadecimal form of ch left-padded with 0 + for (int shift = (hexSize - 1) * 4; shift >= 0; shift -= 4) { + int digit = 0xf & (c >> shift); + result.append(HEX_DIGITS[digit]); + } + } + } + } + result.append(quoteChar); + escapeClosingTags(result); + return result; + } + + /** + * Escapes any closing XML tags embedded in str, which could + * potentially cause a parse failure in a browser, for example, embedding a + * closing <script> tag. + * + * @param str an unescaped literal; May be null + */ + private static void escapeClosingTags(StringBuilder str) { + if (str == null) { + return; + } + + int index = 0; + while ((index = str.indexOf(" globalBlocks = new HashSet(); + private final TextOutput p; + private TIntArrayList statementEnds = new TIntArrayList(); + private TIntArrayList statementStarts = new TIntArrayList(); + + public JsToStringGenerationVisitor(TextOutput out) { + this.p = out; + } + + @Override + public boolean visit(JsArrayAccess x, JsContext ctx) { + JsExpression arrayExpr = x.getArrayExpr(); + printPair(x, arrayExpr); + _lsquare(); + accept(x.getIndexExpr()); + _rsquare(); + return false; + } + + @Override + public boolean visit(JsArrayLiteral x, JsContext ctx) { + _lsquare(); + printExpressions(x.getExpressions()); + _rsquare(); + return false; + } + + private void printExpressions(List expressions) { + boolean sep = false; + for (JsExpression arg : expressions) { + sep = sepCommaOptSpace(sep); + boolean isEnclosed = parenPushIfCommaExpression(arg); + accept(arg); + if (isEnclosed) { + rightParen(); + } + } + } + + @Override + public boolean visit(JsBinaryOperation binaryOperation, JsContext ctx) { + JsBinaryOperator op = binaryOperation.getOperator(); + JsExpression arg1 = binaryOperation.getArg1(); + boolean isExpressionEnclosed = _parenPush(binaryOperation, arg1, !op.isLeftAssociative()); + + accept(arg1); + if (op.isKeyword()) { + _parenPopOrSpace(binaryOperation, arg1, !op.isLeftAssociative()); + } + else if (op != JsBinaryOperator.COMMA) { + if (isExpressionEnclosed) { + rightParen(); + } + spaceOpt(); + } + + p.print(op.getSymbol()); + + JsExpression arg2 = binaryOperation.getArg2(); + boolean isParenOpened; + if (op == JsBinaryOperator.COMMA) { + isParenOpened = false; + spaceOpt(); + } + else if (arg2 instanceof JsBinaryOperation && isBinaryOperationShouldBeEnclosed(((JsBinaryOperation) arg2).getOperator())) { + spaceOpt(); + leftParen(); + isParenOpened = true; + } + else { + if (spaceCalc(op, arg2)) { + isParenOpened = _parenPushOrSpace(binaryOperation, arg2, op.isLeftAssociative()); + } + else { + spaceOpt(); + isParenOpened = _parenPush(binaryOperation, arg2, op.isLeftAssociative()); + } + } + accept(arg2); + if (isParenOpened) { + rightParen(); + } + + return false; + } + + private static boolean isBinaryOperationShouldBeEnclosed(JsBinaryOperator operator) { + return operator == JsBinaryOperator.AND; + } + + @Override + public boolean visit(JsBlock x, JsContext ctx) { + printJsBlock(x, true, true); + return false; + } + + @Override + public boolean visit(JsLiteral.JsBooleanLiteral x, JsContext ctx) { + if (x.getValue()) { + p.print(CHARS_TRUE); + } + else { + _false(); + } + return false; + } + + @Override + public boolean visit(JsBreak x, JsContext ctx) { + _break(); + continueOrBreakLabel(x); + return false; + } + + @Override + public boolean visit(JsContinue x, JsContext ctx) { + _continue(); + continueOrBreakLabel(x); + return false; + } + + private void continueOrBreakLabel(JsContinue x) { + JsNameRef label = x.getLabel(); + if (label != null) { + space(); + _nameRef(label); + } + } + + @Override + public boolean visit(JsCase x, JsContext ctx) { + _case(); + space(); + accept(x.getCaseExpr()); + _colon(); + newlineOpt(); + + printSwitchMemberStatements(x); + return false; + } + + private void printSwitchMemberStatements(JsSwitchMember x) { + indent(); + for (JsStatement stmt : x.getStatements()) { + needSemi = true; + accept(stmt); + if (needSemi) { + semi(); + } + newlineOpt(); + } + outdent(); + needSemi = false; + } + + @Override + public boolean visit(JsCatch x, JsContext ctx) { + spaceOpt(); + _catch(); + spaceOpt(); + leftParen(); + nameDef(x.getParameter().getName()); + + // Optional catch condition. + // + JsExpression catchCond = x.getCondition(); + if (catchCond != null) { + space(); + _if(); + space(); + accept(catchCond); + } + + rightParen(); + spaceOpt(); + accept(x.getBody()); + + return false; + } + + @Override + public boolean visit(JsConditional x, JsContext ctx) { + // Associativity: for the then and else branches, it is safe to insert + // another + // ternary expression, but if the test expression is a ternary, it should + // get parentheses around it. + printPair(x, x.getTestExpression()); + spaceOpt(); + questionMark(); + spaceOpt(); + printPair(x, x.getThenExpression()); + spaceOpt(); + _colon(); + spaceOpt(); + printPair(x, x.getElseExpression()); + return false; + } + + private void printPair(JsExpression parent, JsExpression expression) { + boolean isNeedParen = parenCalc(parent, expression, false); + if (isNeedParen) { + leftParen(); + } + accept(expression); + if (isNeedParen) { + rightParen(); + } + } + + @Override + public boolean visit(JsDebugger x, JsContext ctx) { + _debugger(); + return false; + } + + @Override + public boolean visit(JsDefault x, JsContext ctx) { + _default(); + _colon(); + + printSwitchMemberStatements(x); + return false; + } + + @Override + public boolean visit(JsWhile x, JsContext ctx) { + _while(); + spaceOpt(); + leftParen(); + accept(x.getCondition()); + rightParen(); + _nestedPush(x.getBody()); + accept(x.getBody()); + _nestedPop(x.getBody()); + return false; + } + + @Override + public boolean visit(JsDoWhile x, JsContext ctx) { + _do(); + _nestedPush(x.getBody()); + accept(x.getBody()); + _nestedPop(x.getBody()); + if (needSemi) { + semi(); + newlineOpt(); + } + else { + spaceOpt(); + needSemi = true; + } + _while(); + spaceOpt(); + leftParen(); + accept(x.getCondition()); + rightParen(); + return false; + } + + @Override + public boolean visit(JsEmpty x, JsContext ctx) { + return false; + } + + @Override + public boolean visit(JsExprStmt x, JsContext ctx) { + boolean surroundWithParentheses = JsFirstExpressionVisitor.exec(x); + if (surroundWithParentheses) { + leftParen(); + } + accept(x.getExpression()); + if (surroundWithParentheses) { + rightParen(); + } + return false; + } + + @Override + public boolean visit(JsFor x, JsContext ctx) { + _for(); + spaceOpt(); + leftParen(); + + // The init expressions or var decl. + // + if (x.getInitExpr() != null) { + accept(x.getInitExpr()); + } + else if (x.getInitVars() != null) { + accept(x.getInitVars()); + } + + semi(); + + // The loop test. + // + if (x.getCondition() != null) { + spaceOpt(); + accept(x.getCondition()); + } + + semi(); + + // The incr expression. + // + if (x.getIncrExpr() != null) { + spaceOpt(); + accept(x.getIncrExpr()); + } + + rightParen(); + _nestedPush(x.getBody()); + accept(x.getBody()); + _nestedPop(x.getBody()); + return false; + } + + @Override + public boolean visit(JsForIn x, JsContext ctx) { + _for(); + spaceOpt(); + leftParen(); + + if (x.getIterVarName() != null) { + var(); + space(); + nameDef(x.getIterVarName()); + + if (x.getIterExpr() != null) { + spaceOpt(); + assignment(); + spaceOpt(); + accept(x.getIterExpr()); + } + } + else { + // Just a name ref. + // + accept(x.getIterExpr()); + } + + space(); + _in(); + space(); + accept(x.getObjExpr()); + + rightParen(); + _nestedPush(x.getBody()); + accept(x.getBody()); + _nestedPop(x.getBody()); + return false; + } + + @Override + public boolean visit(JsFunction x, JsContext ctx) { + _function(); + space(); + if (x.getName() != null) { + nameOf(x); + } + + leftParen(); + boolean sep = false; + for (Object element : x.getParameters()) { + JsParameter param = (JsParameter) element; + sep = sepCommaOptSpace(sep); + accept(param); + } + rightParen(); + space(); + + lineBreakAfterBlock = false; + accept(x.getBody()); + needSemi = true; + return false; + } + + @Override + public boolean visit(JsIf x, JsContext ctx) { + _if(); + spaceOpt(); + leftParen(); + accept(x.getIfExpression()); + rightParen(); + JsStatement thenStmt = x.getThenStatement(); + _nestedPush(thenStmt); + accept(thenStmt); + _nestedPop(thenStmt); + JsStatement elseStmt = x.getElseStatement(); + if (elseStmt != null) { + if (needSemi) { + semi(); + newlineOpt(); + } + else { + spaceOpt(); + needSemi = true; + } + _else(); + boolean elseIf = elseStmt instanceof JsIf; + if (!elseIf) { + _nestedPush(elseStmt); + } + else { + space(); + } + accept(elseStmt); + if (!elseIf) { + _nestedPop(elseStmt); + } + } + return false; + } + + @Override + public boolean visit(JsInvocation x, JsContext ctx) { + JsExpression qualifier = x.getQualifier(); + printPair(x, qualifier); + + leftParen(); + printExpressions(x.getArguments()); + rightParen(); + return false; + } + + @Override + public boolean visit(JsLabel x, JsContext ctx) { + nameOf(x); + _colon(); + spaceOpt(); + accept(x.getStmt()); + return false; + } + + @Override + public boolean visit(JsNameRef x, JsContext ctx) { + JsExpression q = x.getQualifier(); + if (q != null) { + _parenPush(x, q, false); + if (q instanceof JsNumberLiteral) { + /** + * Fix for Issue #3796. "42.foo" is not allowed, but "(42).foo" is. + */ + leftParen(); + } + accept(q); + if (q instanceof JsNumberLiteral) { + rightParen(); + } + parenPop(x, q, false); + _dot(); + } + _nameRef(x); + return false; + } + + @Override + public boolean visit(JsNew x, JsContext ctx) { + _new(); + space(); + + JsExpression ctorExpr = x.getConstructorExpression(); + boolean needsParens = JsConstructExpressionVisitor.exec(ctorExpr); + if (needsParens) { + leftParen(); + } + accept(ctorExpr); + if (needsParens) { + rightParen(); + } + + leftParen(); + printExpressions(x.getArguments()); + rightParen(); + + return false; + } + + @Override + public boolean visit(JsNullLiteral x, JsContext ctx) { + _null(); + return false; + } + + @Override + public boolean visit(JsIntLiteral x, JsContext ctx) { + p.print(x.value); + return false; + } + + @Override + public boolean visit(JsDoubleLiteral x, JsContext ctx) { + p.print(x.value); + return false; + } + + @Override + public boolean visit(JsObjectLiteral objectLiteral, JsContext context) { + _lbrace(); + if (objectLiteral.isMultiline()) { + p.indentIn(); + } + + boolean isNotFirst = false; + for (JsPropertyInitializer item : objectLiteral.getPropertyInitializers()) { + if (isNotFirst) { + p.print(','); + } + + if (objectLiteral.isMultiline()) { + newlineOpt(); + } + else if (isNotFirst) { + spaceOpt(); + } + + isNotFirst = true; + + JsExpression labelExpr = item.getLabelExpr(); + // labels can be either string, integral, or decimal literals + if (labelExpr instanceof JsNameRef) { + p.print(((JsNameRef) labelExpr).getIdent()); + } + else if (labelExpr instanceof JsStringLiteral) { + p.print(((JsStringLiteral) labelExpr).getValue()); + } + else { + accept(labelExpr); + } + + _colon(); + space(); + JsExpression valueExpr = item.getValueExpr(); + boolean wasEnclosed = parenPushIfCommaExpression(valueExpr); + accept(valueExpr); + if (wasEnclosed) { + rightParen(); + } + } + + if (objectLiteral.isMultiline()) { + p.indentOut(); + newlineOpt(); + } + + _rbrace(); + return false; + } + + @Override + public boolean visit(JsParameter x, JsContext ctx) { + nameOf(x); + return false; + } + + @Override + public boolean visit(JsPostfixOperation x, JsContext ctx) { + JsUnaryOperator op = x.getOperator(); + JsExpression arg = x.getArg(); + // unary operators always associate correctly (I think) + printPair(x, arg); + p.print(op.getSymbol()); + return false; + } + + @Override + public boolean visit(JsPrefixOperation x, JsContext ctx) { + JsUnaryOperator op = x.getOperator(); + p.print(op.getSymbol()); + JsExpression arg = x.getArg(); + if (spaceCalc(op, arg)) { + space(); + } + // unary operators always associate correctly (I think) + printPair(x, arg); + return false; + } + + @Override + public boolean visit(JsProgram x, JsContext ctx) { + p.print(""); + return false; + } + + @Override + public boolean visit(JsProgramFragment x, JsContext ctx) { + p.print(""); + return false; + } + + @Override + public boolean visit(JsPropertyInitializer x, JsContext ctx) { + // Since there are separators, we actually print the property init + // in visit(JsObjectLiteral). + // + return false; + } + + @Override + public boolean visit(JsRegExp x, JsContext ctx) { + _slash(); + p.print(x.getPattern()); + _slash(); + String flags = x.getFlags(); + if (flags != null) { + p.print(flags); + } + return false; + } + + @Override + public boolean visit(JsReturn x, JsContext ctx) { + _return(); + JsExpression expr = x.getExpr(); + if (expr != null) { + space(); + accept(expr); + } + return false; + } + + @Override + public boolean visit(JsStringLiteral x, JsContext ctx) { + printStringLiteral(x.getValue()); + return false; + } + + @Override + public boolean visit(JsSwitch x, JsContext ctx) { + _switch(); + spaceOpt(); + leftParen(); + accept(x.getExpr()); + rightParen(); + spaceOpt(); + _blockOpen(); + acceptList(x.getCases()); + _blockClose(); + return false; + } + + @Override + public boolean visit(JsLiteral.JsThisRef x, JsContext ctx) { + p.print(CHARS_THIS); + return false; + } + + @Override + public boolean visit(JsThrow x, JsContext ctx) { + p.print(CHARS_THROW); + space(); + accept(x.getExpression()); + return false; + } + + @Override + public boolean visit(JsTry x, JsContext ctx) { + p.print(CHARS_TRY); + spaceOpt(); + accept(x.getTryBlock()); + + acceptList(x.getCatches()); + + JsBlock finallyBlock = x.getFinallyBlock(); + if (finallyBlock != null) { + spaceOpt(); + _finally(); + spaceOpt(); + accept(finallyBlock); + } + + return false; + } + + @Override + public boolean visit(JsVar var, JsContext ctx) { + nameOf(var); + JsExpression initExpr = var.getInitExpression(); + if (initExpr != null) { + spaceOpt(); + assignment(); + spaceOpt(); + boolean isEnclosed = parenPushIfCommaExpression(initExpr); + accept(initExpr); + if (isEnclosed) { + rightParen(); + } + } + return false; + } + + @Override + public boolean visit(JsVars vars, JsContext context) { + var(); + space(); + boolean sep = false; + for (JsVar var : vars) { + if (sep) { + if (vars.isMultiline()) { + newlineOpt(); + } + p.print(','); + spaceOpt(); + } + else { + sep = true; + } + + accept(var); + } + return false; + } + + protected void _newline() { + p.newline(); + } + + protected void newlineOpt() { + p.newlineOpt(); + } + + protected void printJsBlock(JsBlock x, boolean truncate, boolean finalNewline) { + if (!lineBreakAfterBlock) { + finalNewline = false; + lineBreakAfterBlock = true; + } + + boolean needBraces = !x.isGlobalBlock(); + if (needBraces) { + _blockOpen(); + } + + int count = 0; + Iterator iterator = x.getStatements().iterator(); + while (iterator.hasNext()) { + boolean isGlobal = x.isGlobalBlock() || globalBlocks.contains(x); + + if (truncate && count > JSBLOCK_LINES_TO_PRINT) { + p.print("[...]"); + newlineOpt(); + break; + } + JsStatement statement = iterator.next(); + if (statement instanceof JsEmpty) { + continue; + } + + needSemi = true; + boolean shouldRecordPositions = isGlobal && !(statement instanceof JsBlock); + boolean stmtIsGlobalBlock = false; + if (isGlobal) { + if (statement instanceof JsBlock) { + // A block inside a global block is still considered global + stmtIsGlobalBlock = true; + globalBlocks.add((JsBlock) statement); + } + } + if (shouldRecordPositions) { + statementStarts.add(p.getPosition()); + } + accept(statement); + if (stmtIsGlobalBlock) { + //noinspection SuspiciousMethodCalls + globalBlocks.remove(statement); + } + if (needSemi) { + /* + * Special treatment of function declarations: If they are the only item in a + * statement (i.e. not part of an assignment operation), just give them + * a newline instead of a semi. + */ + boolean functionStmt = + statement instanceof JsExprStmt && ((JsExprStmt) statement).getExpression() instanceof JsFunction; + /* + * Special treatment of the last statement in a block: only a few + * statements at the end of a block require semicolons. + */ + boolean lastStatement = !iterator.hasNext() && needBraces && !JsRequiresSemiVisitor.exec(statement); + if (functionStmt) { + if (lastStatement) { + newlineOpt(); + } + else { + _newline(); + } + } + else { + if (lastStatement) { + semiOpt(); + } + else { + semi(); + } + newlineOpt(); + } + } + if (shouldRecordPositions) { + assert (statementStarts.size() == statementEnds.size() + 1); + statementEnds.add(p.getPosition()); + } + ++count; + } + + if (needBraces) { + // _blockClose() modified + p.indentOut(); + p.print('}'); + if (finalNewline) { + newlineOpt(); + } + } + needSemi = false; + } + + private void assignment() { + p.print('='); + } + + private void _blockClose() { + p.indentOut(); + p.print('}'); + newlineOpt(); + } + + private void _blockOpen() { + p.print('{'); + p.indentIn(); + newlineOpt(); + } + + private void _break() { + p.print(CHARS_BREAK); + } + + private void _case() { + p.print(CHARS_CASE); + } + + private void _catch() { + p.print(CHARS_CATCH); + } + + private void _colon() { + p.print(':'); + } + + private void _continue() { + p.print(CHARS_CONTINUE); + } + + private void _debugger() { + p.print(CHARS_DEBUGGER); + } + + private void _default() { + p.print(CHARS_DEFAULT); + } + + private void _do() { + p.print(CHARS_DO); + } + + private void _dot() { + p.print('.'); + } + + private void _else() { + p.print(CHARS_ELSE); + } + + private void _false() { + p.print(CHARS_FALSE); + } + + private void _finally() { + p.print(CHARS_FINALLY); + } + + private void _for() { + p.print(CHARS_FOR); + } + + private void _function() { + p.print(CHARS_FUNCTION); + } + + private void _if() { + p.print(CHARS_IF); + } + + private void _in() { + p.print(CHARS_IN); + } + + private void _lbrace() { + p.print('{'); + } + + private void leftParen() { + p.print('('); + } + + private void _lsquare() { + p.print('['); + } + + private void nameDef(JsName name) { + p.print(name.getIdent()); + } + + private void nameOf(HasName hasName) { + nameDef(hasName.getName()); + } + + private void _nameRef(JsNameRef nameRef) { + p.print(nameRef.getIdent()); + } + + private boolean _nestedPop(JsStatement statement) { + boolean pop = !(statement instanceof JsBlock); + if (pop) { + p.indentOut(); + } + return pop; + } + + private boolean _nestedPush(JsStatement statement) { + boolean push = !(statement instanceof JsBlock); + if (push) { + newlineOpt(); + p.indentIn(); + } + else { + spaceOpt(); + } + return push; + } + + private void _new() { + p.print(CHARS_NEW); + } + + private void _null() { + p.print(CHARS_NULL); + } + + private static boolean parenCalc(JsExpression parent, JsExpression child, boolean wrongAssoc) { + int parentPrec = JsPrecedenceVisitor.exec(parent); + int childPrec = JsPrecedenceVisitor.exec(child); + return parentPrec > childPrec || parentPrec == childPrec && wrongAssoc; + } + + private boolean parenPop(JsExpression parent, JsExpression child, boolean wrongAssoc) { + boolean doPop = parenCalc(parent, child, wrongAssoc); + if (doPop) { + rightParen(); + } + return doPop; + } + + private boolean _parenPopOrSpace(JsExpression parent, JsExpression child, boolean wrongAssoc) { + boolean doPop = parenCalc(parent, child, wrongAssoc); + if (doPop) { + rightParen(); + } + else { + space(); + } + return doPop; + } + + private boolean _parenPush(JsExpression parent, JsExpression child, boolean wrongAssoc) { + boolean doPush = parenCalc(parent, child, wrongAssoc); + if (doPush) { + leftParen(); + } + return doPush; + } + + private boolean parenPushIfCommaExpression(JsExpression x) { + boolean doPush = x instanceof JsBinaryOperation && ((JsBinaryOperation) x).getOperator() == JsBinaryOperator.COMMA; + if (doPush) { + leftParen(); + } + return doPush; + } + + private boolean _parenPushOrSpace(JsExpression parent, JsExpression child, boolean wrongAssoc) { + boolean doPush = parenCalc(parent, child, wrongAssoc); + if (doPush) { + leftParen(); + } + else { + space(); + } + return doPush; + } + + private void questionMark() { + p.print('?'); + } + + private void _rbrace() { + p.print('}'); + } + + private void _return() { + p.print(CHARS_RETURN); + } + + private void rightParen() { + p.print(')'); + } + + private void _rsquare() { + p.print(']'); + } + + private void semi() { + p.print(';'); + } + + private void semiOpt() { + p.printOpt(';'); + } + + private boolean sepCommaOptSpace(boolean sep) { + if (sep) { + p.print(','); + spaceOpt(); + } + return true; + } + + private void _slash() { + p.print('/'); + } + + private void space() { + p.print(' '); + } + + /** + * Decide whether, if op is printed followed by arg, + * there needs to be a space between the operator and expression. + * + * @return true if a space needs to be printed + */ + private static boolean spaceCalc(JsOperator op, JsExpression arg) { + if (op.isKeyword()) { + return true; + } + if (arg instanceof JsBinaryOperation) { + JsBinaryOperation binary = (JsBinaryOperation) arg; + /* + * If the binary operation has a higher precedence than op, then it won't + * be parenthesized, so check the first argument of the binary operation. + */ + if (binary.getOperator().getPrecedence() > op.getPrecedence()) { + return spaceCalc(op, binary.getArg1()); + } + return false; + } + if (arg instanceof JsPrefixOperation) { + JsOperator op2 = ((JsPrefixOperation) arg).getOperator(); + return (op == JsBinaryOperator.SUB || op == JsUnaryOperator.NEG) + && (op2 == JsUnaryOperator.DEC || op2 == JsUnaryOperator.NEG) + || (op == JsBinaryOperator.ADD && op2 == JsUnaryOperator.INC); + } + if (arg instanceof JsNumberLiteral && (op == JsBinaryOperator.SUB || op == JsUnaryOperator.NEG)) { + if (arg instanceof JsIntLiteral) { + return ((JsIntLiteral) arg).value < 0; + } + else { + assert arg instanceof JsDoubleLiteral; + //noinspection CastConflictsWithInstanceof + return ((JsDoubleLiteral) arg).value < 0; + } + } + return false; + } + + private void spaceOpt() { + p.printOpt(' '); + } + + private void _switch() { + p.print(CHARS_SWITCH); + } + + private void var() { + p.print(CHARS_VAR); + } + + private void _while() { + p.print(CHARS_WHILE); + } + + private void indent() { + p.indentIn(); + } + + private void outdent() { + p.indentOut(); + } + + private void printStringLiteral(String value) { + p.print(javaScriptString(value)); + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/CanBooleanEval.java b/src/com/google/dart/compiler/backend/js/ast/CanBooleanEval.java new file mode 100644 index 00000000000..4213394da22 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/CanBooleanEval.java @@ -0,0 +1,13 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +/** + * An interface that describes the boolean evaluation of an expression. + */ +public interface CanBooleanEval { + boolean isBooleanFalse(); + boolean isBooleanTrue(); +} diff --git a/src/com/google/dart/compiler/backend/js/ast/HasArguments.java b/src/com/google/dart/compiler/backend/js/ast/HasArguments.java new file mode 100644 index 00000000000..814189f2846 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/HasArguments.java @@ -0,0 +1,15 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +import java.util.List; + +/** + * Implemented by JavaScript objects that accept arguments. + */ +public interface HasArguments extends JsExpression { + + List getArguments(); +} diff --git a/src/com/google/dart/compiler/backend/js/ast/HasCondition.java b/src/com/google/dart/compiler/backend/js/ast/HasCondition.java new file mode 100644 index 00000000000..082f4e15eb3 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/HasCondition.java @@ -0,0 +1,15 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +/** + * Implemented by JavaScript objects with conditional execution. + */ +public interface HasCondition { + + JsExpression getCondition(); + + void setCondition(JsExpression condition); +} diff --git a/src/com/google/dart/compiler/backend/js/ast/HasName.java b/src/com/google/dart/compiler/backend/js/ast/HasName.java new file mode 100644 index 00000000000..bdd87970929 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/HasName.java @@ -0,0 +1,14 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +import com.google.dart.compiler.common.HasSymbol; + +/** + * Implemented by JavaScript objects that have a name. + */ +public interface HasName extends HasSymbol { + JsName getName(); +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsArrayAccess.java b/src/com/google/dart/compiler/backend/js/ast/JsArrayAccess.java new file mode 100644 index 00000000000..c85c088d1f9 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsArrayAccess.java @@ -0,0 +1,68 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +/** + * Represents a javascript expression for array access. + */ +public final class JsArrayAccess extends JsExpressionImpl { + + private JsExpression arrayExpr; + private JsExpression indexExpr; + + public JsArrayAccess() { + super(); + } + + public JsArrayAccess(JsExpression arrayExpr, JsExpression indexExpr) { + this.arrayExpr = arrayExpr; + this.indexExpr = indexExpr; + } + + public JsExpression getArrayExpr() { + return arrayExpr; + } + + public JsExpression getIndexExpr() { + return indexExpr; + } + + @Override + public boolean hasSideEffects() { + return arrayExpr.hasSideEffects() || indexExpr.hasSideEffects(); + } + + @Override + public boolean isDefinitelyNotNull() { + return false; + } + + @Override + public boolean isDefinitelyNull() { + return false; + } + + public void setArrayExpr(JsExpression arrayExpr) { + this.arrayExpr = arrayExpr; + } + + public void setIndexExpr(JsExpression indexExpr) { + this.indexExpr = indexExpr; + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + if (v.visit(this, ctx)) { + arrayExpr = v.accept(arrayExpr); + indexExpr = v.accept(indexExpr); + } + v.endVisit(this, ctx); + } + + @Override + public NodeKind getKind() { + return NodeKind.ARRAY_ACCESS; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsArrayLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsArrayLiteral.java new file mode 100644 index 00000000000..558a02245eb --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsArrayLiteral.java @@ -0,0 +1,71 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +import com.intellij.util.SmartList; + +import java.util.List; + +/** + * Represents a JavaScript expression for array literals. + */ +public final class JsArrayLiteral extends JsLiteral { + private final List expressions; + + public JsArrayLiteral() { + expressions = new SmartList(); + } + + public JsArrayLiteral(List expressions) { + this.expressions = expressions; + } + + public List getExpressions() { + return expressions; + } + + @Override + public boolean hasSideEffects() { + for (JsExpression expr : expressions) { + if (expr.hasSideEffects()) { + return true; + } + } + return false; + } + + @Override + public boolean isBooleanFalse() { + return false; + } + + @Override + public boolean isBooleanTrue() { + return true; + } + + @Override + public boolean isDefinitelyNotNull() { + return true; + } + + @Override + public boolean isDefinitelyNull() { + return false; + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + if (v.visit(this, ctx)) { + v.acceptWithInsertRemove(expressions); + } + v.endVisit(this, ctx); + } + + @Override + public NodeKind getKind() { + return NodeKind.ARRAY; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsBinaryOperation.java b/src/com/google/dart/compiler/backend/js/ast/JsBinaryOperation.java new file mode 100644 index 00000000000..fc1513a74eb --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsBinaryOperation.java @@ -0,0 +1,100 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +import org.jetbrains.annotations.Nullable; + +/** + * Represents a JavaScript binary operation. + */ +public final class JsBinaryOperation extends JsExpressionImpl { + private JsExpression arg1; + private JsExpression arg2; + private final JsBinaryOperator op; + + public JsBinaryOperation(JsBinaryOperator op) { + this(op, null, null); + } + + public JsBinaryOperation(JsBinaryOperator op, @Nullable JsExpression arg1, @Nullable JsExpression arg2) { + this.op = op; + this.arg1 = arg1; + this.arg2 = arg2; + } + + public JsExpression getArg1() { + return arg1; + } + + public JsExpression getArg2() { + return arg2; + } + + public JsBinaryOperator getOperator() { + return op; + } + + @Override + public boolean hasSideEffects() { + return op.isAssignment() || arg1.hasSideEffects() || arg2.hasSideEffects(); + } + + @Override + public boolean isDefinitelyNotNull() { + // Precarious coding, but none of these can have null results. + if (op.getPrecedence() > 5) { + return true; + } + if (op == JsBinaryOperator.OR) { + if (arg1 instanceof CanBooleanEval) { + if (((CanBooleanEval) arg1).isBooleanTrue()) { + assert arg1.isDefinitelyNotNull(); + return true; + } + } + } + // AND and OR can return nulls + if (op.isAssignment()) { + if (op == JsBinaryOperator.ASG) { + return arg2.isDefinitelyNotNull(); + } else { + // All other ASG's are math ops. + return true; + } + } + + if (op == JsBinaryOperator.COMMA) { + return arg2.isDefinitelyNotNull(); + } + + return false; + } + + @Override + public boolean isDefinitelyNull() { + if (op == JsBinaryOperator.AND) { + return arg1.isDefinitelyNull(); + } + return false; + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + if (v.visit(this, ctx)) { + if (op.isAssignment()) { + arg1 = v.acceptLvalue(arg1); + } else { + arg1 = v.accept(arg1); + } + arg2 = v.accept(arg2); + } + v.endVisit(this, ctx); + } + + @Override + public NodeKind getKind() { + return NodeKind.BINARY_OP; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsBinaryOperator.java b/src/com/google/dart/compiler/backend/js/ast/JsBinaryOperator.java new file mode 100644 index 00000000000..54ed2b7ca97 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsBinaryOperator.java @@ -0,0 +1,115 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +/** + * Represents the operator in a JavaScript binary operation. + */ +public enum JsBinaryOperator implements JsOperator { + /* + * Precedence indices from "JavaScript - The Definitive Guide" 4th Edition + * (page 57) + * + * + * Precedence 15 is for really important things that have their own AST + * classes. + * + * Precedence 14 is for unary operators. + */ + + MUL("*", 13, LEFT | INFIX), DIV("/", 13, LEFT | INFIX), MOD("%", 13, LEFT + | INFIX), + + ADD("+", 12, LEFT | INFIX), SUB("-", 12, LEFT | INFIX), + + SHL("<<", 11, LEFT | INFIX), SHR(">>", 11, LEFT | INFIX), SHRU(">>>", 11, + LEFT | INFIX), + + LT("<", 10, LEFT | INFIX), LTE("<=", 10, LEFT | INFIX), GT(">", 10, LEFT + | INFIX), GTE(">=", 10, LEFT | INFIX), INSTANCEOF("instanceof", 10, LEFT + | INFIX), INOP("in", 10, LEFT | INFIX), + + EQ("==", 9, LEFT | INFIX), NEQ("!=", 9, LEFT | INFIX), REF_EQ("===", 9, LEFT + | INFIX), REF_NEQ("!==", 9, LEFT | INFIX), + + BIT_AND("&", 8, LEFT | INFIX), + + BIT_XOR("^", 7, LEFT | INFIX), + + BIT_OR("|", 6, LEFT | INFIX), + + AND("&&", 5, LEFT | INFIX), + + OR("||", 4, LEFT | INFIX), + + // Precedence 3 is for the condition operator. + + // These assignment operators are right-associative. + ASG("=", 2, INFIX), ASG_ADD("+=", 2, INFIX), ASG_SUB("-=", 2, INFIX), ASG_MUL( + "*=", 2, INFIX), ASG_DIV("/=", 2, INFIX), ASG_MOD("%=", 2, INFIX), ASG_SHL( + "<<=", 2, INFIX), ASG_SHR(">>=", 2, INFIX), ASG_SHRU(">>>=", 2, INFIX), ASG_BIT_AND( + "&=", 2, INFIX), ASG_BIT_OR("|=", 2, INFIX), ASG_BIT_XOR("^=", 2, INFIX), + + COMMA(",", 1, LEFT | INFIX); + + private final int mask; + private final int precedence; + private final String symbol; + + private JsBinaryOperator(String symbol, int precedence, int mask) { + this.symbol = symbol; + this.precedence = precedence; + this.mask = mask; + } + + @Override + public int getPrecedence() { + return precedence; + } + + @Override + public String getSymbol() { + return symbol; + } + + public boolean isAssignment() { + return getPrecedence() == ASG.getPrecedence(); + } + + @Override + public boolean isKeyword() { + return this == INSTANCEOF || this == INOP; + } + + @Override + public boolean isLeftAssociative() { + return (mask & LEFT) != 0; + } + + @Override + public boolean isPrecedenceLessThan(JsOperator other) { + return precedence < other.getPrecedence(); + } + + @Override + public boolean isValidInfix() { + return (mask & INFIX) != 0; + } + + @Override + public boolean isValidPostfix() { + return (mask & POSTFIX) != 0; + } + + @Override + public boolean isValidPrefix() { + return (mask & PREFIX) != 0; + } + + @Override + public String toString() { + return symbol; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsBlock.java b/src/com/google/dart/compiler/backend/js/ast/JsBlock.java new file mode 100644 index 00000000000..cad730b4b5c --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsBlock.java @@ -0,0 +1,58 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +/** + * Represents a JavaScript block statement. + */ +public class JsBlock extends JsNodeImpl implements JsStatement { + private final List statements; + + public JsBlock() { + this(new ArrayList()); + } + + public JsBlock(JsStatement statement) { + this(Collections.singletonList(statement)); + } + + public JsBlock(JsStatement... statements) { + this(Arrays.asList(statements)); + } + + public JsBlock(List statements) { + this.statements = statements; + } + + public List getStatements() { + return statements; + } + + public boolean isEmpty() { + return statements.isEmpty(); + } + + public boolean isGlobalBlock() { + return false; + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + if (v.visit(this, ctx)) { + v.acceptWithInsertRemove(statements); + } + v.endVisit(this, ctx); + } + + @Override + public NodeKind getKind() { + return NodeKind.BLOCK; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsBreak.java b/src/com/google/dart/compiler/backend/js/ast/JsBreak.java new file mode 100644 index 00000000000..ce1c9e66f2d --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsBreak.java @@ -0,0 +1,33 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +/** + * Represents the JavaScript break statement. + */ +public final class JsBreak extends JsContinue { + public JsBreak() { + super(null); + } + + public JsBreak(JsNameRef label) { + super(label); + } + + @Override + public NodeKind getKind() { + return NodeKind.BREAK; + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + if (v.visit(this, ctx)) { + if (label != null) { + v.accept(label); + } + } + v.endVisit(this, ctx); + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsCase.java b/src/com/google/dart/compiler/backend/js/ast/JsCase.java new file mode 100644 index 00000000000..a8b881b41cc --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsCase.java @@ -0,0 +1,39 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +/** + * Represents the JavaScript case statement. + */ +public final class JsCase extends JsSwitchMember { + + private JsExpression caseExpr; + + public JsCase() { + super(); + } + + public JsExpression getCaseExpr() { + return caseExpr; + } + + public void setCaseExpr(JsExpression caseExpr) { + this.caseExpr = caseExpr; + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + if (v.visit(this, ctx)) { + caseExpr = v.accept(caseExpr); + v.acceptWithInsertRemove(statements); + } + v.endVisit(this, ctx); + } + + @Override + public NodeKind getKind() { + return NodeKind.CASE; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsCatch.java b/src/com/google/dart/compiler/backend/js/ast/JsCatch.java new file mode 100644 index 00000000000..fc97ad4df4c --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsCatch.java @@ -0,0 +1,66 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +/** + * Represents a JavaScript catch clause. + */ +public class JsCatch extends JsNodeImpl implements HasCondition { + + protected final JsCatchScope scope; + private JsBlock body; + private JsExpression condition; + private JsParameter param; + + public JsCatch(JsScope parent, String ident) { + super(); + assert (parent != null); + scope = new JsCatchScope(parent, ident); + param = new JsParameter(scope.findName(ident)); + } + + public JsBlock getBody() { + return body; + } + + @Override + public JsExpression getCondition() { + return condition; + } + + public JsParameter getParameter() { + return param; + } + + public JsScope getScope() { + return scope; + } + + public void setBody(JsBlock body) { + this.body = body; + } + + @Override + public void setCondition(JsExpression condition) { + this.condition = condition; + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + if (v.visit(this, ctx)) { + param = v.accept(param); + if (condition != null) { + condition = v.accept(condition); + } + body = v.accept(body); + } + v.endVisit(this, ctx); + } + + @Override + public NodeKind getKind() { + return NodeKind.CATCH; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsCatchScope.java b/src/com/google/dart/compiler/backend/js/ast/JsCatchScope.java new file mode 100644 index 00000000000..20f0add6790 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsCatchScope.java @@ -0,0 +1,36 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +import org.jetbrains.annotations.NotNull; + +/** + * A special scope used only for catch blocks. It only holds a single symbol: + * the catch argument's name. + */ +public class JsCatchScope extends JsScope { + private final JsName name; + + public JsCatchScope(JsScope parent, String ident) { + super(parent, "Catch scope"); + name = new JsName(this, ident); + } + + @Override + public JsName declareName(String identifier) { + // Declare into parent scope! + return getParent().declareName(identifier); + } + + @Override + public boolean hasOwnName(@NotNull String name) { + return findOwnName(name) != null; + } + + @Override + protected JsName findOwnName(String ident) { + return name.getIdent().equals(ident) ? name : null; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsConditional.java b/src/com/google/dart/compiler/backend/js/ast/JsConditional.java new file mode 100644 index 00000000000..5b98b278b5e --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsConditional.java @@ -0,0 +1,77 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +/** + * Represents a JavaScript conditional expression. + */ +public final class JsConditional extends JsExpressionImpl { + private JsExpression testExpr; + private JsExpression elseExpr; + private JsExpression thenExpr; + + public JsConditional() { + } + + public JsConditional(JsExpression testExpr, JsExpression thenExpr, JsExpression elseExpr) { + this.testExpr = testExpr; + this.thenExpr = thenExpr; + this.elseExpr = elseExpr; + } + + public JsExpression getElseExpression() { + return elseExpr; + } + + public JsExpression getTestExpression() { + return testExpr; + } + + public JsExpression getThenExpression() { + return thenExpr; + } + + @Override + public boolean hasSideEffects() { + return testExpr.hasSideEffects() || thenExpr.hasSideEffects() || elseExpr.hasSideEffects(); + } + + @Override + public boolean isDefinitelyNotNull() { + return thenExpr.isDefinitelyNotNull() && elseExpr.isDefinitelyNotNull(); + } + + @Override + public boolean isDefinitelyNull() { + return thenExpr.isDefinitelyNull() && elseExpr.isDefinitelyNull(); + } + + public void setElseExpression(JsExpression elseExpr) { + this.elseExpr = elseExpr; + } + + public void setTestExpression(JsExpression testExpr) { + this.testExpr = testExpr; + } + + public void setThenExpression(JsExpression thenExpr) { + this.thenExpr = thenExpr; + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + if (v.visit(this, ctx)) { + testExpr = v.accept(testExpr); + thenExpr = v.accept(thenExpr); + elseExpr = v.accept(elseExpr); + } + v.endVisit(this, ctx); + } + + @Override + public NodeKind getKind() { + return NodeKind.CONDITIONAL; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsContext.java b/src/com/google/dart/compiler/backend/js/ast/JsContext.java new file mode 100644 index 00000000000..ddb170052d3 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsContext.java @@ -0,0 +1,27 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +/** + * The context in which a JsNode visitation occurs. This represents the set of + * possible operations a JsVisitor subclass can perform on the currently visited + * node. + */ +public interface JsContext { + + boolean canInsert(); + + boolean canRemove(); + + void insertAfter(JsVisitable node); + + void insertBefore(JsVisitable node); + + boolean isLvalue(); + + void removeMe(); + + void replaceMe(JsVisitable node); +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsContinue.java b/src/com/google/dart/compiler/backend/js/ast/JsContinue.java new file mode 100644 index 00000000000..89396dc89a8 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsContinue.java @@ -0,0 +1,42 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +import org.jetbrains.annotations.Nullable; + +/** + * Represents the JavaScript continue statement. + */ +public class JsContinue extends JsNodeImpl implements JsStatement { + protected final JsNameRef label; + + public JsContinue() { + this(null); + } + + public JsContinue(@Nullable JsNameRef label) { + super(); + this.label = label; + } + + public JsNameRef getLabel() { + return label; + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + if (v.visit(this, ctx)) { + if (label != null) { + v.accept(label); + } + } + v.endVisit(this, ctx); + } + + @Override + public NodeKind getKind() { + return NodeKind.CONTINUE; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsDebugger.java b/src/com/google/dart/compiler/backend/js/ast/JsDebugger.java new file mode 100644 index 00000000000..fe9900e134a --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsDebugger.java @@ -0,0 +1,25 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +/** + * Represents a JavaScript debugger statement. + */ +public class JsDebugger extends JsNodeImpl implements JsStatement { + + public JsDebugger() { + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + v.visit(this, ctx); + v.endVisit(this, ctx); + } + + @Override + public NodeKind getKind() { + return NodeKind.DEBUGGER; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsDefault.java b/src/com/google/dart/compiler/backend/js/ast/JsDefault.java new file mode 100644 index 00000000000..89624ddd683 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsDefault.java @@ -0,0 +1,28 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +/** + * Represents the default option in a JavaScript swtich statement. + */ +public final class JsDefault extends JsSwitchMember { + + public JsDefault() { + super(); + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + if (v.visit(this, ctx)) { + v.acceptWithInsertRemove(statements); + } + v.endVisit(this, ctx); + } + + @Override + public NodeKind getKind() { + return NodeKind.DEFAULT; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsDoWhile.java b/src/com/google/dart/compiler/backend/js/ast/JsDoWhile.java new file mode 100644 index 00000000000..44f25010e07 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsDoWhile.java @@ -0,0 +1,31 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +/** + * Represents a JavaScript do..while statement. + */ +public class JsDoWhile extends JsWhile { + public JsDoWhile() { + } + + public JsDoWhile(JsExpression condition, JsStatement body) { + super(condition, body); + } + + @Override + public NodeKind getKind() { + return NodeKind.DO; + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + if (v.visit(this, ctx)) { + condition = v.accept(condition); + body = v.accept(body); + } + v.endVisit(this, ctx); + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsEmpty.java b/src/com/google/dart/compiler/backend/js/ast/JsEmpty.java new file mode 100644 index 00000000000..188676e0ddd --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsEmpty.java @@ -0,0 +1,25 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +/** + * Represents an empty statement in JavaScript. + */ +public class JsEmpty extends JsNodeImpl implements JsStatement { + // Interned by JsProgram + JsEmpty() { + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + v.visit(this, ctx); + v.endVisit(this, ctx); + } + + @Override + public NodeKind getKind() { + return NodeKind.EMPTY; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsExprStmt.java b/src/com/google/dart/compiler/backend/js/ast/JsExprStmt.java new file mode 100644 index 00000000000..0ad3a6c74b5 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsExprStmt.java @@ -0,0 +1,32 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +public final class JsExprStmt extends JsNodeImpl implements JsStatement { + private JsExpression expr; + + public JsExprStmt(JsExpression expr) { + super(); + this.expr = expr; + this.setSourceInfo(expr); + } + + public JsExpression getExpression() { + return expr; + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + if (v.visit(this, ctx)) { + expr = v.accept(expr); + } + v.endVisit(this, ctx); + } + + @Override + public NodeKind getKind() { + return NodeKind.EXPRESSION_STMT; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsExpression.java b/src/com/google/dart/compiler/backend/js/ast/JsExpression.java new file mode 100644 index 00000000000..5fb9a7e41f1 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsExpression.java @@ -0,0 +1,25 @@ +package com.google.dart.compiler.backend.js.ast; + +import com.google.dart.compiler.common.HasSourceInfo; +import com.google.dart.compiler.common.SourceInfo; + +public interface JsExpression extends JsNode, SourceInfo, HasSourceInfo, JsVisitable { + /** + * Determines whether the expression can cause side effects. + */ + boolean hasSideEffects(); + + /** + * True if the target expression is definitely not null. + */ + boolean isDefinitelyNotNull(); + + /** + * True if the target expression is definitely null. + */ + boolean isDefinitelyNull(); + + boolean isLeaf(); + + JsStatement makeStmt(); +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsExpressionImpl.java b/src/com/google/dart/compiler/backend/js/ast/JsExpressionImpl.java new file mode 100644 index 00000000000..f42ae1c26da --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsExpressionImpl.java @@ -0,0 +1,27 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +abstract class JsExpressionImpl extends JsNodeImpl implements JsExpression { + protected JsExpressionImpl() { + } + + /** + * Determines whether or not this expression is a leaf, such as a + * {@link JsNameRef}, {@link JsLiteral.JsBooleanLiteral}, and so on. Leaf expressions + * never need to be parenthesized. + */ + @Override + public boolean isLeaf() { + // Conservatively say that it isn't a leaf. + // Individual subclasses can speak for themselves if they are a leaf. + return false; + } + + @Override + public JsStatement makeStmt() { + return new JsExprStmt(this); + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsFor.java b/src/com/google/dart/compiler/backend/js/ast/JsFor.java new file mode 100644 index 00000000000..ccf61b77954 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsFor.java @@ -0,0 +1,99 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +import com.google.dart.compiler.common.SourceInfo; + +/** + * A for statement. If specified at all, the initializer part is + * either a declaration of one or more variables, in which case + * {@link #getInitVars()} is used, or an expression, in which case + * {@link #getInitExpr()} is used. In the latter case, the comma operator is + * often used to create a compound expression. + *

+ *

+ * Note that any of the parts of the for loop header can be + * null, although the body will never be null. + */ +public class JsFor extends JsNodeImpl implements JsStatement { + private JsStatement body; + private JsExpression condition; + private JsExpression incrExpr; + private JsExpression initExpr; + private JsVars initVars; + + public JsFor(JsVars initVars, JsExpression condition, JsExpression incrementExpr) { + this.initVars = initVars; + this.incrExpr = incrementExpr; + this.condition = condition; + initExpr = null; + } + + public JsFor(JsExpression initExpr, JsExpression condition, JsExpression incrementExpr) { + this.initExpr = initExpr; + this.incrExpr = incrementExpr; + this.condition = condition; + initVars = null; + } + + public JsStatement getBody() { + return body; + } + + public JsExpression getCondition() { + return condition; + } + + public JsExpression getIncrExpr() { + return incrExpr; + } + + public JsExpression getInitExpr() { + return initExpr; + } + + public JsVars getInitVars() { + return initVars; + } + + public void setBody(JsStatement body) { + this.body = body; + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + if (v.visit(this, ctx)) { + assert (!(initExpr != null && initVars != null)); + + if (initExpr != null) { + initExpr = v.accept(initExpr); + } + else if (initVars != null) { + initVars = v.accept(initVars); + } + + if (condition != null) { + condition = v.accept(condition); + } + + if (incrExpr != null) { + incrExpr = v.accept(incrExpr); + } + body = v.accept(body); + } + v.endVisit(this, ctx); + } + + @Override + public JsFor setSourceRef(SourceInfo info) { + super.setSourceRef(info); + return this; + } + + @Override + public NodeKind getKind() { + return NodeKind.FOR; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsForIn.java b/src/com/google/dart/compiler/backend/js/ast/JsForIn.java new file mode 100644 index 00000000000..048fb02b25c --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsForIn.java @@ -0,0 +1,71 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +/** + * Represents a JavaScript for..in statement. + */ +public class JsForIn extends JsNodeImpl implements JsStatement { + + private JsStatement body; + private JsExpression iterExpr; + private JsExpression objExpr; + + // Optional: the name of a new iterator variable to introduce + private final JsName iterVarName; + + public JsForIn() { + this(null); + } + + public JsForIn(JsName iterVarName) { + this.iterVarName = iterVarName; + } + + public JsStatement getBody() { + return body; + } + + public JsExpression getIterExpr() { + return iterExpr; + } + + public JsName getIterVarName() { + return iterVarName; + } + + public JsExpression getObjExpr() { + return objExpr; + } + + public void setBody(JsStatement body) { + this.body = body; + } + + public void setIterExpr(JsExpression iterExpr) { + this.iterExpr = iterExpr; + } + + public void setObjExpr(JsExpression objExpr) { + this.objExpr = objExpr; + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + if (v.visit(this, ctx)) { + if (iterExpr != null) { + iterExpr = v.acceptLvalue(iterExpr); + } + objExpr = v.accept(objExpr); + body = v.accept(body); + } + v.endVisit(this, ctx); + } + + @Override + public NodeKind getKind() { + return NodeKind.FOR_IN; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsFunction.java b/src/com/google/dart/compiler/backend/js/ast/JsFunction.java new file mode 100644 index 00000000000..8a55f90b79d --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsFunction.java @@ -0,0 +1,112 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +import com.google.dart.compiler.common.SourceInfo; +import com.google.dart.compiler.common.Symbol; +import com.intellij.util.SmartList; +import org.jetbrains.annotations.Nullable; + +import java.util.List; + +public final class JsFunction extends JsLiteral implements HasName { + private JsBlock body; + private List params; + private final JsScope scope; + private JsName name; + + public JsFunction(JsScope parentScope) { + this(parentScope, (JsName) null); + } + + public JsFunction(JsScope parentScope, JsBlock body) { + this(parentScope, (JsName) null); + this.body = body; + } + + private JsFunction(JsScope parentScope, @Nullable JsName name) { + this.name = name; + this.scope = new JsScope(parentScope, name == null ? null : name.getIdent()); + } + + public JsBlock getBody() { + return body; + } + + @Override + public JsName getName() { + return name; + } + + @Override + public Symbol getSymbol() { + return name; + } + + public List getParameters() { + if (params == null) { + params = new SmartList(); + } + return params; + } + + public JsScope getScope() { + return scope; + } + + @Override + public boolean hasSideEffects() { + // If there's a name, the name is assigned to. + return name != null; + } + + @Override + public boolean isBooleanFalse() { + return false; + } + + @Override + public boolean isBooleanTrue() { + return true; + } + + @Override + public boolean isDefinitelyNotNull() { + return true; + } + + @Override + public boolean isDefinitelyNull() { + return false; + } + + public void setBody(JsBlock body) { + this.body = body; + } + + public void setName(@Nullable JsName name) { + this.name = name; + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + if (v.visit(this, ctx)) { + v.acceptWithInsertRemove(params); + body = v.accept(body); + } + v.endVisit(this, ctx); + } + + @Override + public JsFunction setSourceRef(SourceInfo info) { + super.setSourceRef(info); + return this; + } + + @Override + public NodeKind getKind() { + return NodeKind.FUNCTION; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsGlobalBlock.java b/src/com/google/dart/compiler/backend/js/ast/JsGlobalBlock.java new file mode 100644 index 00000000000..9a14004e14c --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsGlobalBlock.java @@ -0,0 +1,19 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +/** + * Represnts a JavaScript block in the global scope. + */ +public class JsGlobalBlock extends JsBlock { + + public JsGlobalBlock() { + } + + @Override + public boolean isGlobalBlock() { + return true; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsIf.java b/src/com/google/dart/compiler/backend/js/ast/JsIf.java new file mode 100644 index 00000000000..1722bc525c7 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsIf.java @@ -0,0 +1,69 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +/** + * Represents a JavaScript if statement. + */ +public final class JsIf extends JsNodeImpl implements JsStatement { + private JsExpression ifExpression; + private JsStatement thenStatement; + private JsStatement elseStatement; + + public JsIf() { + } + + public JsIf(JsExpression ifExpression, JsStatement thenStatement, JsStatement elseStatement) { + this.ifExpression = ifExpression; + this.thenStatement = thenStatement; + this.elseStatement = elseStatement; + } + + public JsIf(JsExpression ifExpression, JsStatement thenStatement) { + this.ifExpression = ifExpression; + this.thenStatement = thenStatement; + } + + public JsStatement getElseStatement() { + return elseStatement; + } + + public JsExpression getIfExpression() { + return ifExpression; + } + + public JsStatement getThenStatement() { + return thenStatement; + } + + public void setElseStatement(JsStatement elseStmt) { + this.elseStatement = elseStmt; + } + + public void setIfExpression(JsExpression ifExpr) { + this.ifExpression = ifExpr; + } + + public void setThenStatement(JsStatement thenStmt) { + this.thenStatement = thenStmt; + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + if (v.visit(this, ctx)) { + ifExpression = v.accept(ifExpression); + thenStatement = v.accept(thenStatement); + if (elseStatement != null) { + elseStatement = v.accept(elseStatement); + } + } + v.endVisit(this, ctx); + } + + @Override + public NodeKind getKind() { + return NodeKind.IF; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsInvocation.java b/src/com/google/dart/compiler/backend/js/ast/JsInvocation.java new file mode 100644 index 00000000000..1856dcc6dad --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsInvocation.java @@ -0,0 +1,83 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +import com.intellij.util.SmartList; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; + +/** + * Represents a JavaScript invocation. + */ +public final class JsInvocation extends JsExpressionImpl implements HasArguments { + private final List arguments; + private JsExpression qualifier; + + public JsInvocation() { + arguments = new SmartList(); + } + + public JsInvocation(JsExpression qualifier, List arguments) { + this.qualifier = qualifier; + this.arguments = arguments; + } + + public JsInvocation(JsExpression qualifier, JsExpression arg) { + this(qualifier, Collections.singletonList(arg)); + } + + public JsInvocation(JsExpression qualifier, JsExpression... arguments) { + this(qualifier, Arrays.asList(arguments)); + } + + public JsInvocation(JsExpression qualifier) { + this(); + this.qualifier = qualifier; + } + + @Override + public List getArguments() { + return arguments; + } + + public JsExpression getQualifier() { + return qualifier; + } + + @Override + public boolean hasSideEffects() { + return true; + } + + @Override + public boolean isDefinitelyNotNull() { + return false; + } + + @Override + public boolean isDefinitelyNull() { + return false; + } + + public void setQualifier(JsExpression qualifier) { + this.qualifier = qualifier; + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + if (v.visit(this, ctx)) { + qualifier = v.accept(qualifier); + v.acceptList(arguments); + } + v.endVisit(this, ctx); + } + + @Override + public NodeKind getKind() { + return NodeKind.INVOKE; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsLabel.java b/src/com/google/dart/compiler/backend/js/ast/JsLabel.java new file mode 100644 index 00000000000..f9817ee3390 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsLabel.java @@ -0,0 +1,52 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +import com.google.dart.compiler.common.Symbol; + +/** + * Represents a JavaScript label statement. + */ +public class JsLabel extends JsNodeImpl implements JsStatement, HasName { + + private final JsName label; + + private JsStatement stmt; + + public JsLabel(JsName label) { + this.label = label; + } + + @Override + public JsName getName() { + return label; + } + + @Override + public Symbol getSymbol() { + return label; + } + + public JsStatement getStmt() { + return stmt; + } + + public void setStmt(JsStatement stmt) { + this.stmt = stmt; + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + if (v.visit(this, ctx)) { + stmt = v.accept(stmt); + } + v.endVisit(this, ctx); + } + + @Override + public NodeKind getKind() { + return NodeKind.LABEL; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsLiteral.java new file mode 100644 index 00000000000..eb00aab6e36 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsLiteral.java @@ -0,0 +1,100 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +public abstract class JsLiteral extends JsExpressionImpl implements CanBooleanEval { + public static final JsValueLiteral THIS = new JsThisRef(); + public static final JsNameRef UNDEFINED = new JsNameRef("undefined"); + + public static final JsNullLiteral NULL = new JsNullLiteral(); + + public static final JsBooleanLiteral TRUE = new JsBooleanLiteral(true); + public static final JsBooleanLiteral FALSE = new JsBooleanLiteral(false); + + public static JsBooleanLiteral getBoolean(boolean truth) { + return truth ? TRUE : FALSE; + } + + public static final class JsThisRef extends JsValueLiteral { + private JsThisRef() { + super(); + } + + @Override + public boolean isBooleanFalse() { + return false; + } + + @Override + public boolean isBooleanTrue() { + return true; + } + + @Override + public boolean isDefinitelyNotNull() { + return true; + } + + @Override + public boolean isDefinitelyNull() { + return false; + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + v.visit(this, ctx); + v.endVisit(this, ctx); + } + + @Override + public NodeKind getKind() { + return NodeKind.THIS; + } + } + + public static final class JsBooleanLiteral extends JsValueLiteral { + private final boolean value; + + // Should be interned by JsProgram + private JsBooleanLiteral(boolean value) { + this.value = value; + } + + public boolean getValue() { + return value; + } + + @Override + public boolean isBooleanFalse() { + return !value; + } + + @Override + public boolean isBooleanTrue() { + return value; + } + + @Override + public boolean isDefinitelyNotNull() { + return true; + } + + @Override + public boolean isDefinitelyNull() { + return false; + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + v.visit(this, ctx); + v.endVisit(this, ctx); + } + + @Override + public NodeKind getKind() { + return NodeKind.BOOLEAN; + } + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsName.java b/src/com/google/dart/compiler/backend/js/ast/JsName.java new file mode 100644 index 00000000000..44da16f6610 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsName.java @@ -0,0 +1,53 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +import com.google.dart.compiler.common.Symbol; + +/** + * An abstract base class for named JavaScript objects. + */ +public class JsName implements Symbol { + private final JsScope enclosing; + private final String ident; + + /** + * @param ident the unmangled ident to use for this name + */ + JsName(JsScope enclosing, String ident) { + this.enclosing = enclosing; + this.ident = ident; + } + + public String getIdent() { + return ident; + } + + public JsNameRef makeRef() { + return new JsNameRef(this); + } + + @Override + public String toString() { + return ident; + } + + @Override + public int hashCode() { + return ident.hashCode(); + } + + @Override + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (!(obj instanceof JsName)) { + return false; + } + JsName other = (JsName) obj; + return ident.equals(other.ident) && enclosing == other.enclosing; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNameRef.java b/src/com/google/dart/compiler/backend/js/ast/JsNameRef.java new file mode 100644 index 00000000000..54d1a923aa1 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsNameRef.java @@ -0,0 +1,125 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +import com.google.dart.compiler.common.Symbol; + +/** + * Represents a JavaScript expression that references a name. + */ +public final class JsNameRef extends JsExpressionImpl implements CanBooleanEval, HasName { + private String ident; + private JsName name; + private JsExpression qualifier; + + public JsNameRef(JsName name) { + this.name = name; + } + + public JsNameRef(String ident) { + this.ident = ident; + } + + public JsNameRef(String ident, JsExpression qualifier) { + this.ident = ident; + this.qualifier = qualifier; + } + + public JsNameRef(String ident, String qualifier) { + this(ident, new JsNameRef(qualifier)); + } + + public JsNameRef(JsName name, JsExpression qualifier) { + this.name = name; + this.qualifier = qualifier; + } + + public String getIdent() { + return (name == null) ? ident : name.getIdent(); + } + + @Override + public JsName getName() { + return name; + } + + @Override + public Symbol getSymbol() { + return name; + } + + public JsExpression getQualifier() { + return qualifier; + } + + @Override + public boolean hasSideEffects() { + if (qualifier == null) { + return false; + } + if (!qualifier.isDefinitelyNotNull()) { + // Could trigger NPE. + return true; + } + return qualifier.hasSideEffects(); + } + + @Override + public boolean isBooleanFalse() { + return isDefinitelyNull(); + } + + @Override + public boolean isBooleanTrue() { + return false; + } + + @Override + public boolean isDefinitelyNotNull() { + return false; + } + + @Override + public boolean isDefinitelyNull() { + if (name != null) { + return (JsLiteral.UNDEFINED.getName() == name); + } + return false; + } + + @Override + public boolean isLeaf() { + if (qualifier == null) { + return true; + } + else { + return false; + } + } + + public void resolve(JsName name) { + this.name = name; + this.ident = null; + } + + public void setQualifier(JsExpression qualifier) { + this.qualifier = qualifier; + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + if (v.visit(this, ctx)) { + if (qualifier != null) { + qualifier = v.accept(qualifier); + } + } + v.endVisit(this, ctx); + } + + @Override + public NodeKind getKind() { + return NodeKind.NAME_REF; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNew.java b/src/com/google/dart/compiler/backend/js/ast/JsNew.java new file mode 100644 index 00000000000..68ffb540ca5 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsNew.java @@ -0,0 +1,66 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +import com.intellij.util.SmartList; + +import java.util.List; + +/** + * Represents the JavaScript new expression. + */ +public final class JsNew extends JsExpressionImpl implements HasArguments { + private final List arguments; + private JsExpression ctorExpression; + + public JsNew(JsExpression ctorExpression) { + this(ctorExpression, new SmartList()); + } + + public JsNew(JsExpression ctorExpression, List arguments) { + this.ctorExpression = ctorExpression; + this.arguments = arguments; + } + + @Override + public List getArguments() { + return arguments; + } + + public JsExpression getConstructorExpression() { + return ctorExpression; + } + + @Override + public boolean hasSideEffects() { + return true; + } + + @Override + public boolean isDefinitelyNotNull() { + // Sadly, in JS it can be! + // TODO: analysis could probably determine most instances cannot be null. + return false; + } + + @Override + public boolean isDefinitelyNull() { + return false; + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + if (v.visit(this, ctx)) { + ctorExpression = v.accept(ctorExpression); + v.acceptList(arguments); + } + v.endVisit(this, ctx); + } + + @Override + public NodeKind getKind() { + return NodeKind.NEW; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNode.java b/src/com/google/dart/compiler/backend/js/ast/JsNode.java new file mode 100644 index 00000000000..78e78af6434 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsNode.java @@ -0,0 +1,9 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +public interface JsNode extends JsVisitable { + NodeKind getKind(); +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNodeImpl.java b/src/com/google/dart/compiler/backend/js/ast/JsNodeImpl.java new file mode 100644 index 00000000000..e96a902c178 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsNodeImpl.java @@ -0,0 +1,41 @@ +package com.google.dart.compiler.backend.js.ast; + +import com.google.dart.compiler.backend.js.JsSourceGenerationVisitor; +import com.google.dart.compiler.backend.js.JsToStringGenerationVisitor; +import com.google.dart.compiler.common.AbstractNode; +import com.google.dart.compiler.common.SourceInfo; +import com.google.dart.compiler.util.TextOutputImpl; + +abstract class JsNodeImpl extends AbstractNode implements JsNode { + protected JsNodeImpl() { + } + + // Causes source generation to delegate to the one visitor + public final String toSource() { + TextOutputImpl out = new TextOutputImpl(); + JsSourceGenerationVisitor v = new JsSourceGenerationVisitor(out); + v.accept(this); + return out.toString(); + } + + // Causes source generation to delegate to the one visitor + @Override + public String toString() { + TextOutputImpl out = new TextOutputImpl(); + JsToStringGenerationVisitor v = new JsToStringGenerationVisitor(out); + v.accept(this); + return out.toString(); + } + + @Override + public SourceInfo getSourceInfo() { + return this; + } + + public JsNode setSourceRef(SourceInfo info) { + if (info != null) { + this.setSourceInfo(info); + } + return this; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNullLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsNullLiteral.java new file mode 100644 index 00000000000..0514fb0df89 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsNullLiteral.java @@ -0,0 +1,41 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +public final class JsNullLiteral extends JsValueLiteral { + JsNullLiteral() { + } + + @Override + public boolean isBooleanFalse() { + return true; + } + + @Override + public boolean isBooleanTrue() { + return false; + } + + @Override + public boolean isDefinitelyNotNull() { + return false; + } + + @Override + public boolean isDefinitelyNull() { + return true; + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + v.visit(this, ctx); + v.endVisit(this, ctx); + } + + @Override + public NodeKind getKind() { + return NodeKind.NULL; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNumberLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsNumberLiteral.java new file mode 100644 index 00000000000..e191e7e8954 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsNumberLiteral.java @@ -0,0 +1,78 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +public abstract class JsNumberLiteral extends JsValueLiteral { + @Override + public boolean isDefinitelyNotNull() { + return true; + } + + @Override + public boolean isDefinitelyNull() { + return false; + } + + @Override + public NodeKind getKind() { + return NodeKind.NUMBER; + } + + public static final class JsDoubleLiteral extends JsNumberLiteral { + public final double value; + + JsDoubleLiteral(double value) { + this.value = value; + } + + @Override + public boolean isBooleanFalse() { + return value == 0.0; + } + + @Override + public boolean isBooleanTrue() { + return value != 0.0; + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + v.visit(this, ctx); + v.endVisit(this, ctx); + } + + public String toString() { + return String.valueOf(value); + } + } + + public static final class JsIntLiteral extends JsNumberLiteral { + public final int value; + + JsIntLiteral(int value) { + this.value = value; + } + + @Override + public boolean isBooleanFalse() { + return value == 0; + } + + @Override + public boolean isBooleanTrue() { + return value != 0; + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + v.visit(this, ctx); + v.endVisit(this, ctx); + } + + public String toString() { + return String.valueOf(value); + } + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsObjectLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsObjectLiteral.java new file mode 100644 index 00000000000..b0353a3d3bd --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsObjectLiteral.java @@ -0,0 +1,83 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +import com.intellij.util.SmartList; + +import java.util.List; + +public final class JsObjectLiteral extends JsLiteral { + private final List properties; + + private final boolean multiline; + + public JsObjectLiteral() { + this(new SmartList()); + } + + public JsObjectLiteral(boolean multiline) { + this(new SmartList(), multiline); + } + + public boolean isMultiline() { + return multiline; + } + + public JsObjectLiteral(List properties) { + this(properties, false); + } + + public JsObjectLiteral(List properties, boolean multiline) { + this.properties = properties; + this.multiline = multiline; + } + + public List getPropertyInitializers() { + return properties; + } + + @Override + public boolean hasSideEffects() { + for (JsPropertyInitializer prop : properties) { + if (prop.hasSideEffects()) { + return true; + } + } + return false; + } + + @Override + public boolean isBooleanFalse() { + return false; + } + + @Override + public boolean isBooleanTrue() { + return true; + } + + @Override + public boolean isDefinitelyNotNull() { + return true; + } + + @Override + public boolean isDefinitelyNull() { + return false; + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + if (v.visit(this, ctx)) { + v.acceptWithInsertRemove(properties); + } + v.endVisit(this, ctx); + } + + @Override + public NodeKind getKind() { + return NodeKind.OBJECT; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsOperator.java b/src/com/google/dart/compiler/backend/js/ast/JsOperator.java new file mode 100644 index 00000000000..844805fcc73 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsOperator.java @@ -0,0 +1,28 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +public interface JsOperator { + int INFIX = 0x02; + int LEFT = 0x01; + int POSTFIX = 0x04; + int PREFIX = 0x08; + + int getPrecedence(); + + String getSymbol(); + + boolean isKeyword(); + + boolean isLeftAssociative(); + + boolean isPrecedenceLessThan(JsOperator other); + + boolean isValidInfix(); + + boolean isValidPostfix(); + + boolean isValidPrefix(); +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsParameter.java b/src/com/google/dart/compiler/backend/js/ast/JsParameter.java new file mode 100644 index 00000000000..fbb0908e69f --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsParameter.java @@ -0,0 +1,39 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +import com.google.dart.compiler.common.Symbol; + +/** + * A JavaScript parameter. + */ +public final class JsParameter extends JsNodeImpl implements HasName { + private final JsName name; + + public JsParameter(JsName name) { + this.name = name; + } + + @Override + public JsName getName() { + return name; + } + + @Override + public Symbol getSymbol() { + return name; + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + v.visit(this, ctx); + v.endVisit(this, ctx); + } + + @Override + public NodeKind getKind() { + return NodeKind.PARAMETER; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsPostfixOperation.java b/src/com/google/dart/compiler/backend/js/ast/JsPostfixOperation.java new file mode 100644 index 00000000000..a33536f6c0d --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsPostfixOperation.java @@ -0,0 +1,42 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +/** + * A JavaScript postfix operation. + */ +public final class JsPostfixOperation extends JsUnaryOperation { + + public JsPostfixOperation(JsUnaryOperator op) { + this(op, null); + } + + public JsPostfixOperation(JsUnaryOperator op, JsExpression arg) { + super(op, arg); + } + + @Override + public boolean isDefinitelyNotNull() { + return true; + } + + @Override + public boolean isDefinitelyNull() { + return false; + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + if (v.visit(this, ctx)) { + super.traverse(v, ctx); + } + v.endVisit(this, ctx); + } + + @Override + public NodeKind getKind() { + return NodeKind.POSTFIX_OP; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsPrefixOperation.java b/src/com/google/dart/compiler/backend/js/ast/JsPrefixOperation.java new file mode 100644 index 00000000000..8ce12c7bacd --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsPrefixOperation.java @@ -0,0 +1,69 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +/** + * A JavaScript prefix operation. + */ +public final class JsPrefixOperation extends JsUnaryOperation implements CanBooleanEval { + + public JsPrefixOperation(JsUnaryOperator op) { + this(op, null); + } + + public JsPrefixOperation(JsUnaryOperator op, JsExpression arg) { + super(op, arg); + } + + @Override + public boolean isBooleanFalse() { + if (getOperator() == JsUnaryOperator.VOID) { + return true; + } + if (getOperator() == JsUnaryOperator.NOT && getArg() instanceof CanBooleanEval) { + CanBooleanEval eval = (CanBooleanEval) getArg(); + return eval.isBooleanTrue(); + } + return false; + } + + @Override + public boolean isBooleanTrue() { + if (getOperator() == JsUnaryOperator.NOT && getArg() instanceof CanBooleanEval) { + CanBooleanEval eval = (CanBooleanEval) getArg(); + return eval.isBooleanFalse(); + } + if (getOperator() == JsUnaryOperator.TYPEOF) { + return true; + } + return false; + } + + @Override + public boolean isDefinitelyNotNull() { + if (getOperator() == JsUnaryOperator.TYPEOF) { + return true; + } + return getOperator() != JsUnaryOperator.VOID; + } + + @Override + public boolean isDefinitelyNull() { + return getOperator() == JsUnaryOperator.VOID; + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + if (v.visit(this, ctx)) { + super.traverse(v, ctx); + } + v.endVisit(this, ctx); + } + + @Override + public NodeKind getKind() { + return NodeKind.PREFIX_OP; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsProgram.java b/src/com/google/dart/compiler/backend/js/ast/JsProgram.java new file mode 100644 index 00000000000..afea24a93ca --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsProgram.java @@ -0,0 +1,128 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +import gnu.trove.TDoubleObjectHashMap; +import gnu.trove.THashMap; +import gnu.trove.TIntObjectHashMap; +import org.jetbrains.annotations.Nullable; + +import java.util.Map; + +import static com.google.dart.compiler.backend.js.ast.JsNumberLiteral.JsDoubleLiteral; +import static com.google.dart.compiler.backend.js.ast.JsNumberLiteral.JsIntLiteral; + +/** + * A JavaScript program. + */ +public final class JsProgram extends JsNodeImpl { + private final JsEmpty emptyStmt; + + private JsProgramFragment[] fragments; + + private final TDoubleObjectHashMap doubleLiteralMap = new TDoubleObjectHashMap(); + private final TIntObjectHashMap intLiteralMap = new TIntObjectHashMap(); + + private final JsRootScope rootScope; + private final Map stringLiteralMap = new THashMap(); + private final JsScope topScope; + + /** + * Constructs a JavaScript program object. + */ + public JsProgram(String unitId) { + rootScope = new JsRootScope(this); + topScope = new JsScope(rootScope, "Global", unitId); + setFragmentCount(1); + + emptyStmt = new JsEmpty(); + } + + public JsEmpty getEmptyStmt() { + return emptyStmt; + } + + public JsBlock getFragmentBlock(int fragment) { + if (fragment < 0 || fragment >= fragments.length) { + throw new IllegalArgumentException("Invalid fragment: " + fragment); + } + return fragments[fragment].getGlobalBlock(); + } + + public JsBlock getGlobalBlock() { + return getFragmentBlock(0); + } + + public JsNumberLiteral getNumberLiteral(double value) { + JsDoubleLiteral literal = doubleLiteralMap.get(value); + if (literal == null) { + literal = new JsDoubleLiteral(value); + doubleLiteralMap.put(value, literal); + } + + return literal; + } + + public JsNumberLiteral getNumberLiteral(int value) { + JsIntLiteral literal = intLiteralMap.get(value); + if (literal == null) { + literal = new JsIntLiteral(value); + intLiteralMap.put(value, literal); + } + + return literal; + } + + /** + * Gets the quasi-mythical root scope. This is not the same as the top scope; + * all unresolvable identifiers wind up here, because they are considered + * external to the program. + */ + public JsRootScope getRootScope() { + return rootScope; + } + + /** + * Gets the top level scope. This is the scope of all the statements in the + * main program. + */ + public JsScope getScope() { + return topScope; + } + + /** + * Creates or retrieves a JsStringLiteral from an interned object pool. + */ + public JsStringLiteral getStringLiteral(String value) { + JsStringLiteral literal = stringLiteralMap.get(value); + if (literal == null) { + literal = new JsStringLiteral(value); + stringLiteralMap.put(value, literal); + } + return literal; + } + + public void setFragmentCount(int fragments) { + this.fragments = new JsProgramFragment[fragments]; + for (int i = 0; i < fragments; i++) { + this.fragments[i] = new JsProgramFragment(); + } + } + + @Override + public void traverse(JsVisitor v, @Nullable JsContext ctx) { + if (v.visit(this, ctx)) { + for (JsProgramFragment fragment : fragments) { + v.accept(fragment); + } + } + v.endVisit(this, ctx); + } + + @Override + public NodeKind getKind() { + return NodeKind.PROGRAM; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsProgramFragment.java b/src/com/google/dart/compiler/backend/js/ast/JsProgramFragment.java new file mode 100644 index 00000000000..3a291556f66 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsProgramFragment.java @@ -0,0 +1,34 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +/** + * One independently loadable fragment of a {@link JsProgram}. + */ +public class JsProgramFragment extends JsNodeImpl { + + private final JsGlobalBlock globalBlock; + + public JsProgramFragment() { + this.globalBlock = new JsGlobalBlock(); + } + + public JsBlock getGlobalBlock() { + return globalBlock; + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + if (v.visit(this, ctx)) { + v.accept(globalBlock); + } + v.endVisit(this, ctx); + } + + @Override + public NodeKind getKind() { + return NodeKind.PROGRAM_FRAGMENT; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsPropertyInitializer.java b/src/com/google/dart/compiler/backend/js/ast/JsPropertyInitializer.java new file mode 100644 index 00000000000..0a8d3e2467d --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsPropertyInitializer.java @@ -0,0 +1,54 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +import org.jetbrains.annotations.NotNull; + +/** + * Used in object literals to specify property values by name. + */ +public class JsPropertyInitializer extends JsNodeImpl { + private JsExpression labelExpr; + private JsExpression valueExpr; + + public JsPropertyInitializer(@NotNull JsExpression labelExpr) { + this.labelExpr = labelExpr; + } + + public JsPropertyInitializer(@NotNull JsExpression labelExpr, @NotNull JsExpression valueExpr) { + this(labelExpr); + this.valueExpr = valueExpr; + } + + public JsExpression getLabelExpr() { + return labelExpr; + } + + public JsExpression getValueExpr() { + return valueExpr; + } + + public boolean hasSideEffects() { + return labelExpr.hasSideEffects() || valueExpr.hasSideEffects(); + } + + public void setValueExpr(@NotNull JsExpression valueExpr) { + this.valueExpr = valueExpr; + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + if (v.visit(this, ctx)) { + labelExpr = v.accept(labelExpr); + valueExpr = v.accept(valueExpr); + } + v.endVisit(this, ctx); + } + + @Override + public NodeKind getKind() { + return NodeKind.PROPERTY_INIT; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsRegExp.java b/src/com/google/dart/compiler/backend/js/ast/JsRegExp.java new file mode 100644 index 00000000000..158c7a5e092 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsRegExp.java @@ -0,0 +1,64 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +/** + * A JavaScript regular expression. + */ +public final class JsRegExp extends JsValueLiteral { + + private String flags; + private String pattern; + + public JsRegExp() { + } + + public String getFlags() { + return flags; + } + + public String getPattern() { + return pattern; + } + + @Override + public boolean isBooleanFalse() { + return false; + } + + @Override + public boolean isBooleanTrue() { + return true; + } + + @Override + public boolean isDefinitelyNotNull() { + return true; + } + + @Override + public boolean isDefinitelyNull() { + return false; + } + + public void setFlags(String suffix) { + this.flags = suffix; + } + + public void setPattern(String re) { + this.pattern = re; + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + v.visit(this, ctx); + v.endVisit(this, ctx); + } + + @Override + public NodeKind getKind() { + return NodeKind.REGEXP; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsReturn.java b/src/com/google/dart/compiler/backend/js/ast/JsReturn.java new file mode 100644 index 00000000000..b8806c82bab --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsReturn.java @@ -0,0 +1,43 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +/** + * A JavaScript return statement. + */ +public final class JsReturn extends JsNodeImpl implements JsStatement { + + private JsExpression expr; + + public JsReturn() { + } + + public JsReturn(JsExpression expr) { + this.expr = expr; + } + + public JsExpression getExpr() { + return expr; + } + + public void setExpr(JsExpression expr) { + this.expr = expr; + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + if (v.visit(this, ctx)) { + if (expr != null) { + expr = v.accept(expr); + } + } + v.endVisit(this, ctx); + } + + @Override + public NodeKind getKind() { + return NodeKind.RETURN; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsRootScope.java b/src/com/google/dart/compiler/backend/js/ast/JsRootScope.java new file mode 100644 index 00000000000..e808eb0f576 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsRootScope.java @@ -0,0 +1,42 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +import com.google.dart.compiler.backend.js.JsReservedIdentifiers; + +/** + * The root scope is the parent of every scope. All identifiers in this scope + * are not obfuscatable. This scope is prefilled with reserved global + * JavaScript symbols. + */ +public final class JsRootScope extends JsScope { + private final JsProgram program; + + public JsRootScope(JsProgram program) { + super("Root"); + this.program = program; + } + + @Override + public JsProgram getProgram() { + return program; + } + + @Override + protected JsName findOwnName(String ident) { + JsName name = super.findOwnName(ident); + if (name == null) { + if (JsReservedIdentifiers.reservedGlobalSymbols.contains(ident)) { + /* + * Lazily add JsNames for reserved identifiers. Since a JsName for a reserved global symbol + * must report a legitimate enclosing scope, we can't simply have a shared set of symbol + * names. + */ + name = doCreateName(ident); + } + } + return name; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsScope.java b/src/com/google/dart/compiler/backend/js/ast/JsScope.java new file mode 100644 index 00000000000..406069e6890 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsScope.java @@ -0,0 +1,176 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +import com.google.dart.compiler.util.Maps; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Collections; +import java.util.Map; + +/** + * A scope is a factory for creating and allocating + * {@link JsName}s. A JavaScript AST is + * built in terms of abstract name objects without worrying about obfuscation, + * keyword/identifier blacklisting, and so on. + *

+ *

+ *

+ * Scopes are associated with + * {@link JsFunction}s, but the two are + * not equivalent. Functions have scopes, but a scope does not + * necessarily have an associated Function. Examples of this include the + * {@link JsRootScope} and synthetic + * scopes that might be created by a client. + *

+ *

+ *

+ * Scopes can have parents to provide constraints when allocating actual + * identifiers for names. Specifically, names in child scopes are chosen such + * that they do not conflict with names in their parent scopes. The ultimate + * parent is usually the global scope (see + * {@link JsProgram#getRootScope()}), + * but parentless scopes are useful for managing names that are always accessed + * with a qualifier and could therefore never be confused with the global scope + * hierarchy. + */ +public class JsScope { + @Nullable + private final String description; + private Map names = Collections.emptyMap(); + private final JsScope parent; + protected int tempIndex = 0; + private final String scopeId; + + public JsScope(JsScope parent, @Nullable String description) { + this(parent, description, null); + } + + public JsScope(JsScope parent) { + this(parent, null); + } + + public JsScope(JsScope parent, @Nullable String description, @Nullable String scopeId) { + assert (parent != null); + this.scopeId = scopeId; + this.description = description; + this.parent = parent; + } + + @NotNull + public JsScope innerScope(@Nullable String scopeName) { + return new JsScope(this, scopeName); + } + + protected JsScope(@Nullable String description) { + this.description = description; + this.parent = null; + this.scopeId = null; + } + + /** + * Gets a name object associated with the specified identifier in this scope, + * creating it if necessary.
+ * If the JsName does not exist yet, a new JsName is created. The identifier, + * short name, and original name of the newly created JsName are equal to + * the given identifier. + * + * @param identifier An identifier that is unique within this scope. + */ + public JsName declareName(String identifier) { + JsName name = findOwnName(identifier); + return name != null ? name : doCreateName(identifier); + } + + /** + * Creates a new variable with an unique ident in this scope. + * The generated JsName is guaranteed to have an identifier that does not clash with any existing variables in the scope. + * Future declarations of variables might however clash with the temporary + * (unless they use this function). + */ + @NotNull + public JsName declareFreshName(String suggestedName) { + String name = suggestedName; + int counter = 0; + while (hasOwnName(name)) { + name = suggestedName + '_' + counter++; + } + return doCreateName(name); + } + + private String getNextTempName() { + // introduced by the compiler + return "tmp$" + (scopeId != null ? scopeId + "$" : "") + tempIndex++; + } + + /** + * Creates a temporary variable with an unique name in this scope. + * The generated temporary is guaranteed to have an identifier (but not short + * name) that does not clash with any existing variables in the scope. + * Future declarations of variables might however clash with the temporary. + */ + public JsName declareTemporary() { + return declareFreshName(getNextTempName()); + } + + /** + * Attempts to find the name object for the specified ident, searching in this + * scope, and if not found, in the parent scopes. + * + * @return null if the identifier has no associated name + */ + @Nullable + public final JsName findName(String ident) { + JsName name = findOwnName(ident); + if (name == null && parent != null) { + return parent.findName(ident); + } + return name; + } + + protected boolean hasOwnName(@NotNull String name) { + return names.containsKey(name); + } + + /** + * Returns the parent scope of this scope, or null if this is the + * root scope. + */ + public final JsScope getParent() { + return parent; + } + + public JsProgram getProgram() { + assert (parent != null) : "Subclasses must override getProgram() if they do not set a parent"; + return parent.getProgram(); + } + + @Override + public final String toString() { + if (parent != null) { + return description + "->" + parent; + } + else { + return description; + } + } + + protected JsName doCreateName(String ident) { + JsName name = new JsName(this, ident); + names = Maps.put(names, ident, name); + return name; + } + + /** + * Attempts to find the name object for the specified ident, searching in this + * scope only. + * + * @return null if the identifier has no associated name + */ + protected JsName findOwnName(String ident) { + return names.get(ident); + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsStatement.java b/src/com/google/dart/compiler/backend/js/ast/JsStatement.java new file mode 100644 index 00000000000..a3ea11cec92 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsStatement.java @@ -0,0 +1,8 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +public interface JsStatement extends JsNode { +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsStringLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsStringLiteral.java new file mode 100644 index 00000000000..77e60c9b89a --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsStringLiteral.java @@ -0,0 +1,53 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +/** + * A JavaScript string literal expression. + */ +public final class JsStringLiteral extends JsValueLiteral { + + private final String value; + + // These only get created by JsProgram so that they can be interned. + JsStringLiteral(String value) { + this.value = value; + } + + public String getValue() { + return value; + } + + @Override + public boolean isBooleanFalse() { + return value.length() == 0; + } + + @Override + public boolean isBooleanTrue() { + return value.length() != 0; + } + + @Override + public boolean isDefinitelyNotNull() { + return true; + } + + @Override + public boolean isDefinitelyNull() { + return false; + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + v.visit(this, ctx); + v.endVisit(this, ctx); + } + + @Override + public NodeKind getKind() { + return NodeKind.STRING; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsSwitch.java b/src/com/google/dart/compiler/backend/js/ast/JsSwitch.java new file mode 100644 index 00000000000..4e76da8d939 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsSwitch.java @@ -0,0 +1,47 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +import java.util.ArrayList; +import java.util.List; + +/** + * A JavaScript switch statement. + */ +public class JsSwitch extends JsNodeImpl implements JsStatement { + + private final List cases = new ArrayList(); + private JsExpression expr; + + public JsSwitch() { + super(); + } + + public List getCases() { + return cases; + } + + public JsExpression getExpr() { + return expr; + } + + public void setExpr(JsExpression expr) { + this.expr = expr; + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + if (v.visit(this, ctx)) { + expr = v.accept(expr); + v.acceptWithInsertRemove(cases); + } + v.endVisit(this, ctx); + } + + @Override + public NodeKind getKind() { + return NodeKind.SWITCH; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsSwitchMember.java b/src/com/google/dart/compiler/backend/js/ast/JsSwitchMember.java new file mode 100644 index 00000000000..08265369888 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsSwitchMember.java @@ -0,0 +1,24 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +import com.intellij.util.SmartList; + +import java.util.List; + +/** + * A member/case in a JavaScript switch object. + */ +public abstract class JsSwitchMember extends JsNodeImpl { + protected final List statements = new SmartList(); + + protected JsSwitchMember() { + super(); + } + + public List getStatements() { + return statements; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsThrow.java b/src/com/google/dart/compiler/backend/js/ast/JsThrow.java new file mode 100644 index 00000000000..f6b8f1987f8 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsThrow.java @@ -0,0 +1,37 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +public class JsThrow extends JsNodeImpl implements JsStatement { + private JsExpression expression; + + public JsThrow() { + } + + public JsThrow(JsExpression expression) { + this.expression = expression; + } + + public JsExpression getExpression() { + return expression; + } + + public void setExpression(JsExpression expression) { + this.expression = expression; + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + if (v.visit(this, ctx)) { + expression = v.accept(expression); + } + v.endVisit(this, ctx); + } + + @Override + public NodeKind getKind() { + return NodeKind.THROW; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsTry.java b/src/com/google/dart/compiler/backend/js/ast/JsTry.java new file mode 100644 index 00000000000..2a3b78607b6 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsTry.java @@ -0,0 +1,66 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +import com.intellij.util.SmartList; +import org.jetbrains.annotations.Nullable; + +import java.util.List; + +/** + * A JavaScript try statement. + */ +public class JsTry extends JsNodeImpl implements JsStatement { + private final List catches; + private JsBlock finallyBlock; + private JsBlock tryBlock; + + public JsTry() { + catches = new SmartList(); + } + + public JsTry(JsBlock tryBlock, List catches, @Nullable JsBlock finallyBlock) { + this.tryBlock = tryBlock; + this.catches = catches; + this.finallyBlock = finallyBlock; + } + + public List getCatches() { + return catches; + } + + public JsBlock getFinallyBlock() { + return finallyBlock; + } + + public JsBlock getTryBlock() { + return tryBlock; + } + + public void setFinallyBlock(JsBlock block) { + this.finallyBlock = block; + } + + public void setTryBlock(JsBlock block) { + tryBlock = block; + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + if (v.visit(this, ctx)) { + tryBlock = v.accept(tryBlock); + v.acceptWithInsertRemove(catches); + if (finallyBlock != null) { + finallyBlock = v.accept(finallyBlock); + } + } + v.endVisit(this, ctx); + } + + @Override + public NodeKind getKind() { + return NodeKind.TRY; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsUnaryOperation.java b/src/com/google/dart/compiler/backend/js/ast/JsUnaryOperation.java new file mode 100644 index 00000000000..6f3ee93273d --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsUnaryOperation.java @@ -0,0 +1,53 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +/** + * A JavaScript prefix or postfix operation. + */ +public abstract class JsUnaryOperation extends JsExpressionImpl { + + private JsExpression arg; + + private final JsUnaryOperator op; + + public JsUnaryOperation(JsUnaryOperator op) { + this(op, null); + } + + public JsUnaryOperation(JsUnaryOperator op, JsExpression arg) { + super(); + this.op = op; + this.arg = arg; + } + + public JsExpression getArg() { + return arg; + } + + public JsUnaryOperator getOperator() { + return op; + } + + @Override + public final boolean hasSideEffects() { + return op.isModifying() || arg.hasSideEffects(); + } + + public void setArg(JsExpression arg) { + this.arg = arg; + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + if (op.isModifying()) { + // The delete operator is practically like an assignment of undefined, so + // for practical purposes we're treating it as an lvalue. + arg = v.acceptLvalue(arg); + } else { + arg = v.accept(arg); + } + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsUnaryOperator.java b/src/com/google/dart/compiler/backend/js/ast/JsUnaryOperator.java new file mode 100644 index 00000000000..2b854d8313e --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsUnaryOperator.java @@ -0,0 +1,78 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +/** + * A JavaScript unary operator. + */ +public enum JsUnaryOperator implements JsOperator { + + /* + * Precedence indices from "JavaScript - The Definitive Guide" 4th Edition + * (page 57) + */ + BIT_NOT("~", 14, PREFIX), DEC("--", 14, POSTFIX | PREFIX), DELETE("delete", 14, PREFIX), INC( + "++", 14, POSTFIX | PREFIX), NEG("-", 14, PREFIX), POS("+", 14, PREFIX), + NOT("!", 14, PREFIX), TYPEOF("typeof", 14, PREFIX), VOID("void", 14, PREFIX); + + private final int mask; + private final int precedence; + private final String symbol; + + private JsUnaryOperator(String symbol, int precedence, int mask) { + this.symbol = symbol; + this.precedence = precedence; + this.mask = mask; + } + + @Override + public int getPrecedence() { + return precedence; + } + + @Override + public String getSymbol() { + return symbol; + } + + @Override + public boolean isKeyword() { + return this == DELETE || this == TYPEOF || this == VOID; + } + + @Override + public boolean isLeftAssociative() { + return (mask & LEFT) != 0; + } + + public boolean isModifying() { + return this == DEC || this == INC || this == DELETE; + } + + @Override + public boolean isPrecedenceLessThan(JsOperator other) { + return precedence < other.getPrecedence(); + } + + @Override + public boolean isValidInfix() { + return (mask & INFIX) != 0; + } + + @Override + public boolean isValidPostfix() { + return (mask & POSTFIX) != 0; + } + + @Override + public boolean isValidPrefix() { + return (mask & PREFIX) != 0; + } + + @Override + public String toString() { + return symbol; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsValueLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsValueLiteral.java new file mode 100644 index 00000000000..f64eb794ad6 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsValueLiteral.java @@ -0,0 +1,23 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +/** + * A JavaScript string literal expression. + */ +public abstract class JsValueLiteral extends JsLiteral { + protected JsValueLiteral() { + } + + @Override + public final boolean hasSideEffects() { + return false; + } + + @Override + public final boolean isLeaf() { + return true; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsVars.java b/src/com/google/dart/compiler/backend/js/ast/JsVars.java new file mode 100644 index 00000000000..d02c932715a --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsVars.java @@ -0,0 +1,137 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +import com.google.dart.compiler.common.SourceInfo; +import com.google.dart.compiler.common.Symbol; +import com.intellij.util.SmartList; +import org.jetbrains.annotations.Nullable; + +import java.util.Arrays; +import java.util.Collections; +import java.util.Iterator; +import java.util.List; + +/** + * A JavaScript var statement. + */ +public class JsVars extends JsNodeImpl implements JsStatement, Iterable { + private final List vars; + + private final boolean multiline; + + public JsVars() { + this(new SmartList(), false); + } + + public JsVars(boolean multiline) { + this(new SmartList(), multiline); + } + + public JsVars(List vars, boolean multiline) { + this.vars = vars; + this.multiline = multiline; + } + + public JsVars(JsVar var) { + this(Collections.singletonList(var), false); + } + + public JsVars(JsVar... vars) { + this(Arrays.asList(vars), false); + } + + public boolean isMultiline() { + return multiline; + } + + /** + * A var declared using the JavaScript var statement. + */ + public static class JsVar extends JsNodeImpl implements HasName { + private final JsName name; + private JsExpression initExpression; + + public JsVar(JsName name) { + this.name = name; + } + + public JsVar(JsName name, @Nullable JsExpression initExpression) { + this.name = name; + this.initExpression = initExpression; + } + + public JsExpression getInitExpression() { + return initExpression; + } + + @Override + public JsName getName() { + return name; + } + + @Override + public Symbol getSymbol() { + return name; + } + + public void setInitExpression(JsExpression initExpression) { + this.initExpression = initExpression; + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + if (v.visit(this, ctx)) { + if (initExpression != null) { + initExpression = v.accept(initExpression); + } + } + v.endVisit(this, ctx); + } + + @Override + public JsVar setSourceRef(SourceInfo info) { + super.setSourceRef(info); + return this; + } + + @Override + public NodeKind getKind() { + return NodeKind.VAR; + } + } + + public void add(JsVar var) { + vars.add(var); + } + + public void addIfHasInitializer(JsVar var) { + if (var.getInitExpression() != null) { + add(var); + } + } + + public boolean isEmpty() { + return vars.isEmpty(); + } + + @Override + public Iterator iterator() { + return vars.iterator(); + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + if (v.visit(this, ctx)) { + v.acceptWithInsertRemove(vars); + } + v.endVisit(this, ctx); + } + + @Override + public NodeKind getKind() { + return NodeKind.VARS; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsVisitable.java b/src/com/google/dart/compiler/backend/js/ast/JsVisitable.java new file mode 100644 index 00000000000..bd68f477d90 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsVisitable.java @@ -0,0 +1,18 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +/** + * Abstracts the idea that a class can be traversed. + */ +public interface JsVisitable { + /** + * Causes this object to have the visitor visit itself and its children. + * + * @param visitor the visitor that should traverse this node + * @param ctx the context of an existing traversal + */ + void traverse(JsVisitor visitor, JsContext ctx); +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsVisitor.java b/src/com/google/dart/compiler/backend/js/ast/JsVisitor.java new file mode 100644 index 00000000000..df5be84598f --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsVisitor.java @@ -0,0 +1,435 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +import com.google.dart.compiler.backend.js.ast.JsVars.JsVar; + +import java.util.List; + +/** + * Implemented by nodes that will visit child nodes. + */ +public class JsVisitor { + + protected static final JsContext LVALUE_CONTEXT = new JsContext() { + + @Override + public boolean canInsert() { + return false; + } + + @Override + public boolean canRemove() { + return false; + } + + @Override + public void insertAfter(JsVisitable node) { + throw new UnsupportedOperationException(); + } + + @Override + public void insertBefore(JsVisitable node) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isLvalue() { + return true; + } + + @Override + public void removeMe() { + throw new UnsupportedOperationException(); + } + + @Override + public void replaceMe(JsVisitable node) { + throw new UnsupportedOperationException(); + } + }; + + protected static final JsContext UNMODIFIABLE_CONTEXT = new JsContext() { + + @Override + public boolean canInsert() { + return false; + } + + @Override + public boolean canRemove() { + return false; + } + + @Override + public void insertAfter(JsVisitable node) { + throw new UnsupportedOperationException(); + } + + @Override + public void insertBefore(JsVisitable node) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isLvalue() { + return false; + } + + @Override + public void removeMe() { + throw new UnsupportedOperationException(); + } + + @Override + public void replaceMe(JsVisitable node) { + throw new UnsupportedOperationException(); + } + }; + + public final T accept(T node) { + return this.doAccept(node); + } + + public final void acceptList(List collection) { + doAcceptList(collection); + } + + public JsExpression acceptLvalue(JsExpression expr) { + return doAcceptLvalue(expr); + } + + public final void acceptWithInsertRemove(List collection) { + doAcceptWithInsertRemove(collection); + } + + public boolean didChange() { + throw new UnsupportedOperationException(); + } + + public void endVisit(JsArrayAccess x, JsContext ctx) { + } + + public void endVisit(JsArrayLiteral x, JsContext ctx) { + } + + public void endVisit(JsBinaryOperation x, JsContext ctx) { + } + + public void endVisit(JsBlock x, JsContext ctx) { + } + + public void endVisit(JsLiteral.JsBooleanLiteral x, JsContext ctx) { + } + + public void endVisit(JsBreak x, JsContext ctx) { + } + + public void endVisit(JsCase x, JsContext ctx) { + } + + public void endVisit(JsCatch x, JsContext ctx) { + } + + public void endVisit(JsConditional x, JsContext ctx) { + } + + public void endVisit(JsContinue x, JsContext ctx) { + } + + public void endVisit(JsDebugger x, JsContext ctx) { + } + + public void endVisit(JsDefault x, JsContext ctx) { + } + + public void endVisit(JsDoWhile x, JsContext ctx) { + } + + public void endVisit(JsEmpty x, JsContext ctx) { + } + + public void endVisit(JsExprStmt x, JsContext ctx) { + } + + public void endVisit(JsFor x, JsContext ctx) { + } + + public void endVisit(JsForIn x, JsContext ctx) { + } + + public void endVisit(JsFunction x, JsContext ctx) { + } + + public void endVisit(JsIf x, JsContext ctx) { + } + + public void endVisit(JsInvocation x, JsContext ctx) { + } + + public void endVisit(JsLabel x, JsContext ctx) { + } + + public void endVisit(JsNameRef x, JsContext ctx) { + } + + public void endVisit(JsNew x, JsContext ctx) { + } + + public void endVisit(JsNullLiteral x, JsContext ctx) { + } + + public void endVisit(JsNumberLiteral x, JsContext ctx) { + } + + public void endVisit(JsObjectLiteral x, JsContext ctx) { + } + + public void endVisit(JsParameter x, JsContext ctx) { + } + + public void endVisit(JsPostfixOperation x, JsContext ctx) { + } + + public void endVisit(JsPrefixOperation x, JsContext ctx) { + } + + public void endVisit(JsProgram x, JsContext ctx) { + } + + public void endVisit(JsProgramFragment x, JsContext ctx) { + } + + public void endVisit(JsPropertyInitializer x, JsContext ctx) { + } + + public void endVisit(JsRegExp x, JsContext ctx) { + } + + public void endVisit(JsReturn x, JsContext ctx) { + } + + public void endVisit(JsStringLiteral x, JsContext ctx) { + } + + public void endVisit(JsSwitch x, JsContext ctx) { + } + + public void endVisit(JsLiteral.JsThisRef x, JsContext ctx) { + } + + public void endVisit(JsThrow x, JsContext ctx) { + } + + public void endVisit(JsTry x, JsContext ctx) { + } + + public void endVisit(JsVar x, JsContext ctx) { + } + + public void endVisit(JsVars x, JsContext ctx) { + } + + public void endVisit(JsWhile x, JsContext ctx) { + } + + public boolean visit(JsArrayAccess x, JsContext ctx) { + return true; + } + + public boolean visit(JsArrayLiteral x, JsContext ctx) { + return true; + } + + public boolean visit(JsBinaryOperation x, JsContext ctx) { + return true; + } + + public boolean visit(JsBlock x, JsContext ctx) { + return true; + } + + public boolean visit(JsLiteral.JsBooleanLiteral x, JsContext ctx) { + return true; + } + + public boolean visit(JsBreak x, JsContext ctx) { + return true; + } + + public boolean visit(JsCase x, JsContext ctx) { + return true; + } + + public boolean visit(JsCatch x, JsContext ctx) { + return true; + } + + public boolean visit(JsConditional x, JsContext ctx) { + return true; + } + + public boolean visit(JsContinue x, JsContext ctx) { + return true; + } + + public boolean visit(JsDebugger x, JsContext ctx) { + return true; + } + + public boolean visit(JsDefault x, JsContext ctx) { + return true; + } + + public boolean visit(JsDoWhile x, JsContext ctx) { + return true; + } + + public boolean visit(JsEmpty x, JsContext ctx) { + return true; + } + + public boolean visit(JsExprStmt x, JsContext ctx) { + return true; + } + + public boolean visit(JsFor x, JsContext ctx) { + return true; + } + + public boolean visit(JsForIn x, JsContext ctx) { + return true; + } + + public boolean visit(JsFunction x, JsContext ctx) { + return true; + } + + public boolean visit(JsIf x, JsContext ctx) { + return true; + } + + public boolean visit(JsInvocation x, JsContext ctx) { + return true; + } + + public boolean visit(JsLabel x, JsContext ctx) { + return true; + } + + public boolean visit(JsNameRef x, JsContext ctx) { + return true; + } + + public boolean visit(JsNew x, JsContext ctx) { + return true; + } + + public boolean visit(JsNullLiteral x, JsContext ctx) { + return true; + } + + public boolean visit(JsNumberLiteral.JsIntLiteral x, JsContext ctx) { + return true; + } + + public boolean visit(JsNumberLiteral.JsDoubleLiteral x, JsContext ctx) { + return true; + } + + public boolean visit(JsObjectLiteral x, JsContext ctx) { + return true; + } + + public boolean visit(JsParameter x, JsContext ctx) { + return true; + } + + public boolean visit(JsPostfixOperation x, JsContext ctx) { + return true; + } + + public boolean visit(JsPrefixOperation x, JsContext ctx) { + return true; + } + + public boolean visit(JsProgram x, JsContext ctx) { + return true; + } + + public boolean visit(JsProgramFragment x, JsContext ctx) { + return true; + } + + public boolean visit(JsPropertyInitializer x, JsContext ctx) { + return true; + } + + public boolean visit(JsRegExp x, JsContext ctx) { + return true; + } + + public boolean visit(JsReturn x, JsContext ctx) { + return true; + } + + public boolean visit(JsStringLiteral x, JsContext ctx) { + return true; + } + + public boolean visit(JsSwitch x, JsContext ctx) { + return true; + } + + public boolean visit(JsLiteral.JsThisRef x, JsContext ctx) { + return true; + } + + public boolean visit(JsThrow x, JsContext ctx) { + return true; + } + + public boolean visit(JsTry x, JsContext ctx) { + return true; + } + + public boolean visit(JsVar x, JsContext ctx) { + return true; + } + + public boolean visit(JsVars x, JsContext ctx) { + return true; + } + + public boolean visit(JsWhile x, JsContext ctx) { + return true; + } + + protected T doAccept(T node) { + doTraverse(node, UNMODIFIABLE_CONTEXT); + return node; + } + + protected void doAcceptList(List collection) { + for (T node : collection) { + doTraverse(node, UNMODIFIABLE_CONTEXT); + } + } + + protected JsExpression doAcceptLvalue(JsExpression expr) { + doTraverse(expr, LVALUE_CONTEXT); + return expr; + } + + protected void doAcceptWithInsertRemove(List collection) { + for (T node : collection) { + doTraverse(node, UNMODIFIABLE_CONTEXT); + } + } + + protected void doTraverse(JsVisitable node, JsContext ctx) { + node.traverse(this, ctx); + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsWhile.java b/src/com/google/dart/compiler/backend/js/ast/JsWhile.java new file mode 100644 index 00000000000..c3639154d0d --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsWhile.java @@ -0,0 +1,51 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +/** + * A JavaScript while statement. + */ +public class JsWhile extends JsNodeImpl implements JsStatement { + protected JsStatement body; + protected JsExpression condition; + + public JsWhile() { + } + + public JsWhile(JsExpression condition, JsStatement body) { + this.condition = condition; + this.body = body; + } + + public JsStatement getBody() { + return body; + } + + public JsExpression getCondition() { + return condition; + } + + public void setBody(JsStatement body) { + this.body = body; + } + + public void setCondition(JsExpression condition) { + this.condition = condition; + } + + @Override + public void traverse(JsVisitor v, JsContext ctx) { + if (v.visit(this, ctx)) { + condition = v.accept(condition); + body = v.accept(body); + } + v.endVisit(this, ctx); + } + + @Override + public NodeKind getKind() { + return NodeKind.WHILE; + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/NodeKind.java b/src/com/google/dart/compiler/backend/js/ast/NodeKind.java new file mode 100644 index 00000000000..0c47f2877f0 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/NodeKind.java @@ -0,0 +1,50 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.backend.js.ast; + +enum NodeKind { + ARRAY_ACCESS, + ARRAY, + BINARY_OP, + BLOCK, + BOOLEAN, + BREAK, + CASE, + CATCH, + CONDITIONAL, + CONTINUE, + DEBUGGER, + DEFAULT, + DO, + EMPTY, + EXPRESSION_STMT, + FOR, + FOR_IN, + FUNCTION, + IF, + INVOKE, + LABEL, + NAME_REF, + NEW, + NULL, + NUMBER, + OBJECT, + PARAMETER, + POSTFIX_OP, + PREFIX_OP, + PROGRAM, + PROGRAM_FRAGMENT, + PROPERTY_INIT, + REGEXP, + RETURN, + STRING, + SWITCH, + THIS, + THROW, + TRY, + VARS, + VAR, + WHILE +} diff --git a/src/com/google/dart/compiler/common/AbstractNode.java b/src/com/google/dart/compiler/common/AbstractNode.java new file mode 100644 index 00000000000..6d9eaf40090 --- /dev/null +++ b/src/com/google/dart/compiler/common/AbstractNode.java @@ -0,0 +1,57 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.common; + +import com.google.dart.compiler.Source; + +public class AbstractNode implements SourceInfo, HasSourceInfo { + protected SourceInfo sourceInfo; + + @Override + public Source getSource() { + return sourceInfo.getSource(); + } + + @Override + public int getLine() { + return sourceInfo.getLine(); + } + + @Override + public int getColumn() { + return sourceInfo.getColumn(); + } + + @Override + public int getStart() { + return sourceInfo.getStart(); + } + + @Override + public int getLength() { + return sourceInfo.getLength(); + } + + @Override + public SourceInfo getSourceInfo() { + return this; + } + + @Override + public void setSourceInfo(SourceInfo info) { + sourceInfo = info; + } + + @Override + public final void setSourceLocation(Source source, int line, int column, int startPosition, int length) { + sourceInfo = new SourceInfoImpl(source, line, column, startPosition, length); + } + + public final void setSourceRange(int startPosition, int length) { + //Preconditions.checkArgument(startPosition != -1 && length >= 0 || startPosition == -1 && length == 0); + //sourceInfo.sourceStart = startPosition; + //sourceInfo.sourceLength = length; + } +} diff --git a/src/com/google/dart/compiler/common/HasSourceInfo.java b/src/com/google/dart/compiler/common/HasSourceInfo.java new file mode 100644 index 00000000000..c7780f6acee --- /dev/null +++ b/src/com/google/dart/compiler/common/HasSourceInfo.java @@ -0,0 +1,55 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.common; + +import com.google.dart.compiler.Source; + +/** + * Abstract view of a class that has source info. + */ +public interface HasSourceInfo { + + /** + * Return the source info associated with this object. + */ + SourceInfo getSourceInfo(); + + /** + * Set the source info associated with this object. May only be called once. + * @param info + */ + void setSourceInfo(SourceInfo info); + + /** + * Sets the source range of the original source file where the source fragment + * corresponding to this node was found. + * + *

+ * Each node in the subtree (other than the contrived nodes) carries source + * range(s) information relating back to positions in the given source (the + * given source itself is not remembered with the AST). The source range + * usually begins at the first character of the first token corresponding to + * the node; leading whitespace and comments are not included. The + * source range usually extends through the last character of the last token + * corresponding to the node; trailing whitespace and comments are not + * included. There are a handful of exceptions (including the various body + * declarations). Source ranges nest properly: the source range for a child is + * always within the source range of its parent, and the source ranges of + * sibling nodes never overlap. + * + * @param source the associated source + * @param line the 1-based line index, or -1, if no source + * location is available + * @param column the 1-based column index, or -1, if no source + * location is available + * @param startPosition a 0-based character index, or -1, if no + * source location is available + * @param length a (possibly 0) length, or -1, if no source + * location is available + * @see SourceInfo#getStart() + * @see SourceInfo#getLength() + */ + void setSourceLocation(Source source, int line, int column, int startPosition, int length); +} diff --git a/src/com/google/dart/compiler/common/HasSymbol.java b/src/com/google/dart/compiler/common/HasSymbol.java new file mode 100644 index 00000000000..0128679bed1 --- /dev/null +++ b/src/com/google/dart/compiler/common/HasSymbol.java @@ -0,0 +1,16 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.common; + +/** + * @author johnlenz@google.com (John Lenz) + */ +public interface HasSymbol { + /** + * @return Return the original user visible name for a Object represented + * in a source map. + */ + Symbol getSymbol(); +} diff --git a/src/com/google/dart/compiler/common/SourceInfo.java b/src/com/google/dart/compiler/common/SourceInfo.java new file mode 100644 index 00000000000..6e0efc8e505 --- /dev/null +++ b/src/com/google/dart/compiler/common/SourceInfo.java @@ -0,0 +1,59 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.common; + +import com.google.dart.compiler.Source; + +import java.io.Serializable; + +/** + * Tracks file and line information for AST nodes. + */ +public interface SourceInfo extends Serializable { + + /** + * The source code provider. + */ + Source getSource(); + + /** + * @return A 1-based line number into the original source file indicating + * where the source fragment begins. + */ + int getLine(); + + /** + * @return A 1-based column number into the original source file indicating + * where the source fragment begins. + */ + int getColumn(); + + /** + * Returns the character index into the original source file indicating + * where the source fragment corresponding to this node begins. + * + *

+ * The parser supplies useful well-defined source ranges to the nodes it creates. + * + * @return the 0-based character index, or -1 + * if no source startPosition information is recorded for this node + * @see #getLength() + * @see HasSourceInfo#setSourceLocation(Source, int, int, int, int) + */ + int getStart(); + + /** + * Returns the length in characters of the original source file indicating + * where the source fragment corresponding to this node ends. + *

+ * The parser supplies useful well-defined source ranges to the nodes it creates. + * + * @return a (possibly 0) length, or 0 + * if no source source position information is recorded for this node + * @see #getStart() + * @see HasSourceInfo#setSourceLocation(Source, int, int, int, int) + */ + int getLength(); +} diff --git a/src/com/google/dart/compiler/common/SourceInfoImpl.java b/src/com/google/dart/compiler/common/SourceInfoImpl.java new file mode 100644 index 00000000000..450f873e3e5 --- /dev/null +++ b/src/com/google/dart/compiler/common/SourceInfoImpl.java @@ -0,0 +1,44 @@ +package com.google.dart.compiler.common; + +import com.google.dart.compiler.Source; + +public class SourceInfoImpl implements SourceInfo { + protected Source source = null; + protected int line = -1; + protected int column = -1; + protected int start = -1; + protected int length = -1; + + public SourceInfoImpl(Source source, int line, int column, int start, int length) { + this.source = source; + this.line = line; + this.column = column; + this.start = start; + this.length = length; + } + + @Override + public Source getSource() { + return source; + } + + @Override + public int getLine() { + return line; + } + + @Override + public int getColumn() { + return column; + } + + @Override + public int getStart() { + return start; + } + + @Override + public int getLength() { + return length; + } +} diff --git a/src/com/google/dart/compiler/common/Symbol.java b/src/com/google/dart/compiler/common/Symbol.java new file mode 100644 index 00000000000..6e786358883 --- /dev/null +++ b/src/com/google/dart/compiler/common/Symbol.java @@ -0,0 +1,11 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.common; + +/** + * @author johnlenz@google.com (John Lenz) + */ +public interface Symbol { +} diff --git a/src/com/google/dart/compiler/util/AstUtil.java b/src/com/google/dart/compiler/util/AstUtil.java new file mode 100644 index 00000000000..d2abe9123d6 --- /dev/null +++ b/src/com/google/dart/compiler/util/AstUtil.java @@ -0,0 +1,49 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.util; + +import com.google.dart.compiler.backend.js.ast.*; + +/** + * @author johnlenz@google.com (John Lenz) + */ +public final class AstUtil { + private AstUtil() { + } + + public static JsNameRef newQualifiedNameRef(String name) { + JsNameRef node = null; + int endPos; + int startPos = 0; + do { + endPos = name.indexOf('.', startPos); + String part = (endPos == -1 + ? name.substring(startPos) + : name.substring(startPos, endPos)); + node = new JsNameRef(part, node); + startPos = endPos + 1; + } + while (endPos != -1); + + return node; + } + + /** + * Returns a sequence of expressions (using the binary sequence operator). + * + * @param exprs - expressions to add to sequence + * @return a sequence of expressions. + */ + public static JsBinaryOperation newSequence(JsExpression... exprs) { + if (exprs.length < 2) { + throw new RuntimeException("newSequence expects at least two arguments"); + } + JsExpression result = exprs[exprs.length - 1]; + for (int i = exprs.length - 2; i >= 0; i--) { + result = new JsBinaryOperation(JsBinaryOperator.COMMA, exprs[i], result); + } + return (JsBinaryOperation) result; + } +} diff --git a/src/com/google/dart/compiler/util/Lists.java b/src/com/google/dart/compiler/util/Lists.java new file mode 100644 index 00000000000..69507ed3197 --- /dev/null +++ b/src/com/google/dart/compiler/util/Lists.java @@ -0,0 +1,267 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.util; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.Comparator; +import java.util.List; + +/** + * Utility methods for operating on memory-efficient lists. All lists of size 0 + * or 1 are assumed to be immutable. All lists of size greater than 1 are + * assumed to be mutable. + */ +public class Lists { + private Lists() { + } + + public static List add(List list, int index, T toAdd) { + switch (list.size()) { + case 0: + // Empty -> Singleton + if (index != 0) { + throw newIndexOutOfBounds(list, index); + } + return Collections.singletonList(toAdd); + case 1: { + // Singleton -> ArrayList + List result = new ArrayList(2); + switch (index) { + case 0: + result.add(toAdd); + result.add(list.get(0)); + return result; + case 1: + result.add(list.get(0)); + result.add(toAdd); + return result; + default: + throw newIndexOutOfBounds(list, index); + } + } + default: + // ArrayList + list.add(index, toAdd); + return list; + } + } + + public static List add(List list, T toAdd) { + switch (list.size()) { + case 0: + // Empty -> Singleton + return Collections.singletonList(toAdd); + case 1: { + // Singleton -> ArrayList + List result = new ArrayList(2); + result.add(list.get(0)); + result.add(toAdd); + return result; + } + default: + // ArrayList + list.add(toAdd); + return list; + } + } + + public static List addAll(List list, int index, List toAdd) { + switch (toAdd.size()) { + case 0: + // No-op. + return list; + case 1: + // Add one element. + return add(list, index, toAdd.get(0)); + default: + // True list merge, result >= 2. + switch (list.size()) { + case 0: + if (index != 0) { + throw newIndexOutOfBounds(list, index); + } + return new ArrayList(toAdd); + case 1: { + List result = new ArrayList(1 + toAdd.size()); + switch (index) { + case 0: + result.addAll(toAdd); + result.add(list.get(0)); + return result; + case 1: + result.add(list.get(0)); + result.addAll(toAdd); + return result; + default: + throw newIndexOutOfBounds(list, index); + } + } + default: + list.addAll(index, toAdd); + return list; + } + } + } + + public static List addAll(List list, List toAdd) { + switch (toAdd.size()) { + case 0: + // No-op. + return list; + case 1: + // Add one element. + return add(list, toAdd.get(0)); + default: + // True list merge, result >= 2. + switch (list.size()) { + case 0: + return new ArrayList(toAdd); + case 1: { + List result = new ArrayList(1 + toAdd.size()); + result.add(list.get(0)); + result.addAll(toAdd); + return result; + } + default: + list.addAll(toAdd); + return list; + } + } + } + + public static List addAll(List list, T... toAdd) { + switch (toAdd.length) { + case 0: + // No-op. + return list; + case 1: + // Add one element. + return add(list, toAdd[0]); + default: + // True list merge, result >= 2. + switch (list.size()) { + case 0: + return new ArrayList(Arrays.asList(toAdd)); + case 1: { + List result = new ArrayList(1 + toAdd.length); + result.add(list.get(0)); + result.addAll(Arrays.asList(toAdd)); + return result; + } + default: + list.addAll(Arrays.asList(toAdd)); + return list; + } + } + } + + public static List create() { + return Collections.emptyList(); + } + + public static List create(Collection collection) { + switch (collection.size()) { + case 0: + return create(); + default: + return new ArrayList(collection); + } + } + + public static List create(List list) { + switch (list.size()) { + case 0: + return create(); + case 1: + return create(list.get(0)); + default: + return new ArrayList(list); + } + } + + public static List create(T item) { + return Collections.singletonList(item); + } + + public static List create(T... items) { + switch (items.length) { + case 0: + return create(); + case 1: + return create(items[0]); + default: + return new ArrayList(Arrays.asList(items)); + } + } + + public static List remove(List list, int toRemove) { + switch (list.size()) { + case 0: + // Empty + throw newIndexOutOfBounds(list, toRemove); + case 1: + // Singleton -> Empty + if (toRemove == 0) { + return Collections.emptyList(); + } else { + throw newIndexOutOfBounds(list, toRemove); + } + case 2: + // ArrayList -> Singleton + switch (toRemove) { + case 0: + return Collections.singletonList(list.get(1)); + case 1: + return Collections.singletonList(list.get(0)); + default: + throw newIndexOutOfBounds(list, toRemove); + } + default: + // ArrayList + list.remove(toRemove); + return list; + } + } + + public static List set(List list, int index, T e) { + switch (list.size()) { + case 0: + // Empty + throw newIndexOutOfBounds(list, index); + case 1: + // Singleton + if (index == 0) { + return Collections.singletonList(e); + } else { + throw newIndexOutOfBounds(list, index); + } + default: + // ArrayList + list.set(index, e); + return list; + } + } + + public static > List sort(List list) { + if (list.size() > 1) { + Collections.sort(list); + } + return list; + } + + public static List sort(List list, Comparator sort) { + if (list.size() > 1) { + Collections.sort(list, sort); + } + return list; + } + + private static IndexOutOfBoundsException newIndexOutOfBounds(List list, int index) { + return new IndexOutOfBoundsException("Index: " + index + ", Size: " + list.size()); + } +} diff --git a/src/com/google/dart/compiler/util/Maps.java b/src/com/google/dart/compiler/util/Maps.java new file mode 100644 index 00000000000..5ca74c3a370 --- /dev/null +++ b/src/com/google/dart/compiler/util/Maps.java @@ -0,0 +1,41 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.util; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +/** + * Utility methods for operating on memory-efficient maps. All maps of size 0 or + * 1 are assumed to be immutable. All maps of size greater than 1 are assumed to + * be mutable. + */ +public class Maps { + private Maps() { + } + + public static Map put(Map map, K key, V value) { + switch (map.size()) { + case 0: + // Empty -> Singleton + return Collections.singletonMap(key, value); + case 1: { + if (map.containsKey(key)) { + return Collections.singletonMap(key, value); + } + // Singleton -> HashMap + Map result = new HashMap(); + result.put(map.keySet().iterator().next(), map.values().iterator().next()); + result.put(key, value); + return result; + } + default: + // HashMap + map.put(key, value); + return map; + } + } +} diff --git a/src/com/google/dart/compiler/util/TextOutput.java b/src/com/google/dart/compiler/util/TextOutput.java new file mode 100644 index 00000000000..3b42663d7dc --- /dev/null +++ b/src/com/google/dart/compiler/util/TextOutput.java @@ -0,0 +1,38 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.util; + +/** + * Interface used for printing text output. + */ +public interface TextOutput { + int getPosition(); + + int getLine(); + + int getColumn(); + + void indentIn(); + + void indentOut(); + + void newline(); + + void newlineOpt(); + + void print(char c); + void print(int v); + void print(double v); + + void print(char[] s); + + void print(CharSequence s); + + void printOpt(char c); + + void printOpt(char[] s); + + void printOpt(String s); +} diff --git a/src/com/google/dart/compiler/util/TextOutputImpl.java b/src/com/google/dart/compiler/util/TextOutputImpl.java new file mode 100644 index 00000000000..84c126da023 --- /dev/null +++ b/src/com/google/dart/compiler/util/TextOutputImpl.java @@ -0,0 +1,174 @@ +// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file +// for details. All rights reserved. Use of this source code is governed by a +// BSD-style license that can be found in the LICENSE file. + +package com.google.dart.compiler.util; + +import java.util.Arrays; + +/** + * An abstract base type to build TextOutput implementations. + */ +public class TextOutputImpl implements TextOutput { + private final boolean compact; + private int identLevel = 0; + private final static int indentGranularity = 2; + private char[][] indents = new char[][] {new char[0]}; + private boolean justNewlined; + private final StringBuilder out; + private int position = 0; + private int line = 0; + private int column = 0; + + public TextOutputImpl() { + this(false); + } + + public TextOutputImpl(boolean compact) { + this.compact = compact; + this.out = new StringBuilder(); + } + + @Override + public String toString() { + return out.toString(); + } + + @Override + public int getPosition() { + return position; + } + + @Override + public int getLine() { + return line; + } + + @Override + public int getColumn() { + return column; + } + + @Override + public void indentIn() { + ++identLevel; + if (identLevel >= indents.length) { + // Cache a new level of indentation string. + char[] newIndentLevel = new char[identLevel * indentGranularity]; + Arrays.fill(newIndentLevel, ' '); + char[][] newIndents = new char[indents.length + 1][]; + System.arraycopy(indents, 0, newIndents, 0, indents.length); + newIndents[identLevel] = newIndentLevel; + indents = newIndents; + } + } + + @Override + public void indentOut() { + --identLevel; + } + + @Override + public void newline() { + out.append('\n'); + position++; + line++; + column = 0; + justNewlined = true; + } + + @Override + public void newlineOpt() { + if (!compact) { + newline(); + } + } + + @Override + public void print(double value) { + maybeIndent(); + int oldLength = out.length(); + out.append(value); + movePosition(out.length() - oldLength); + } + + @Override + public void print(int value) { + maybeIndent(); + int oldLength = out.length(); + out.append(value); + movePosition(out.length() - oldLength); + } + + @Override + public void print(char c) { + maybeIndent(); + out.append(c); + movePosition(1); + } + + private void movePosition(int l) { + position += l; + column += l; + justNewlined = false; + } + + @Override + public void print(char[] s) { + maybeIndent(); + printAndCount(s); + justNewlined = false; + } + + @Override + public void print(CharSequence s) { + maybeIndent(); + printAndCount(s); + justNewlined = false; + } + + @Override + public void printOpt(char c) { + if (!compact) { + maybeIndent(); + out.append(c); + position++; + column++; + } + } + + @Override + public void printOpt(char[] s) { + if (!compact) { + maybeIndent(); + printAndCount(s); + } + } + + @Override + public void printOpt(String s) { + if (!compact) { + maybeIndent(); + printAndCount(s); + } + } + + private void maybeIndent() { + if (justNewlined && !compact) { + printAndCount(indents[identLevel]); + justNewlined = false; + } + } + + private void printAndCount(CharSequence charSequence) { + position += charSequence.length(); + column += charSequence.length(); + out.append(charSequence); + } + + private void printAndCount(char[] chars) { + position += chars.length; + column += chars.length; + out.append(chars); + } +} From 9b34de7c2f29cf2e2dff0f3c43dedf8ae9ea087e Mon Sep 17 00:00:00 2001 From: develar Date: Sun, 30 Sep 2012 15:05:45 +0400 Subject: [PATCH 02/27] move License to root --- src/com/google/dart/compiler/License.txt => License.txt | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/com/google/dart/compiler/License.txt => License.txt (100%) diff --git a/src/com/google/dart/compiler/License.txt b/License.txt similarity index 100% rename from src/com/google/dart/compiler/License.txt rename to License.txt From 3d15b5d83207b7c394190e3fdbb78203297af729 Mon Sep 17 00:00:00 2001 From: develar Date: Sun, 30 Sep 2012 15:06:00 +0400 Subject: [PATCH 03/27] remove unused method --- src/com/google/dart/compiler/util/AstUtil.java | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/src/com/google/dart/compiler/util/AstUtil.java b/src/com/google/dart/compiler/util/AstUtil.java index d2abe9123d6..0be554fb7af 100644 --- a/src/com/google/dart/compiler/util/AstUtil.java +++ b/src/com/google/dart/compiler/util/AstUtil.java @@ -13,23 +13,6 @@ public final class AstUtil { private AstUtil() { } - public static JsNameRef newQualifiedNameRef(String name) { - JsNameRef node = null; - int endPos; - int startPos = 0; - do { - endPos = name.indexOf('.', startPos); - String part = (endPos == -1 - ? name.substring(startPos) - : name.substring(startPos, endPos)); - node = new JsNameRef(part, node); - startPos = endPos + 1; - } - while (endPos != -1); - - return node; - } - /** * Returns a sequence of expressions (using the binary sequence operator). * From 96a52fe2d035f640679c2b144b0b5951cfb76733 Mon Sep 17 00:00:00 2001 From: develar Date: Sun, 30 Sep 2012 15:21:44 +0400 Subject: [PATCH 04/27] cleanup --- .idea/artifacts/dart_ast_jar.xml | 2 +- .idea/codeStyleSettings.xml | 9 + .idea/inspectionProfiles/Project_Default.xml | 4 + src/com/google/dart/compiler/util/Lists.java | 267 ------------------ .../dart/compiler/util/TextOutputImpl.java | 2 +- 5 files changed, 15 insertions(+), 269 deletions(-) create mode 100644 .idea/codeStyleSettings.xml delete mode 100644 src/com/google/dart/compiler/util/Lists.java diff --git a/.idea/artifacts/dart_ast_jar.xml b/.idea/artifacts/dart_ast_jar.xml index 501533125dd..22aab8d51c0 100644 --- a/.idea/artifacts/dart_ast_jar.xml +++ b/.idea/artifacts/dart_ast_jar.xml @@ -1,5 +1,5 @@ - + $PROJECT_DIR$/out/artifacts diff --git a/.idea/codeStyleSettings.xml b/.idea/codeStyleSettings.xml new file mode 100644 index 00000000000..1129f00105d --- /dev/null +++ b/.idea/codeStyleSettings.xml @@ -0,0 +1,9 @@ + + + + + + + diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml index 9ff76d8c843..3674c63d88d 100644 --- a/.idea/inspectionProfiles/Project_Default.xml +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -54,5 +54,9 @@ + + \ No newline at end of file diff --git a/src/com/google/dart/compiler/util/Lists.java b/src/com/google/dart/compiler/util/Lists.java deleted file mode 100644 index 69507ed3197..00000000000 --- a/src/com/google/dart/compiler/util/Lists.java +++ /dev/null @@ -1,267 +0,0 @@ -// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. - -package com.google.dart.compiler.util; - -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.Comparator; -import java.util.List; - -/** - * Utility methods for operating on memory-efficient lists. All lists of size 0 - * or 1 are assumed to be immutable. All lists of size greater than 1 are - * assumed to be mutable. - */ -public class Lists { - private Lists() { - } - - public static List add(List list, int index, T toAdd) { - switch (list.size()) { - case 0: - // Empty -> Singleton - if (index != 0) { - throw newIndexOutOfBounds(list, index); - } - return Collections.singletonList(toAdd); - case 1: { - // Singleton -> ArrayList - List result = new ArrayList(2); - switch (index) { - case 0: - result.add(toAdd); - result.add(list.get(0)); - return result; - case 1: - result.add(list.get(0)); - result.add(toAdd); - return result; - default: - throw newIndexOutOfBounds(list, index); - } - } - default: - // ArrayList - list.add(index, toAdd); - return list; - } - } - - public static List add(List list, T toAdd) { - switch (list.size()) { - case 0: - // Empty -> Singleton - return Collections.singletonList(toAdd); - case 1: { - // Singleton -> ArrayList - List result = new ArrayList(2); - result.add(list.get(0)); - result.add(toAdd); - return result; - } - default: - // ArrayList - list.add(toAdd); - return list; - } - } - - public static List addAll(List list, int index, List toAdd) { - switch (toAdd.size()) { - case 0: - // No-op. - return list; - case 1: - // Add one element. - return add(list, index, toAdd.get(0)); - default: - // True list merge, result >= 2. - switch (list.size()) { - case 0: - if (index != 0) { - throw newIndexOutOfBounds(list, index); - } - return new ArrayList(toAdd); - case 1: { - List result = new ArrayList(1 + toAdd.size()); - switch (index) { - case 0: - result.addAll(toAdd); - result.add(list.get(0)); - return result; - case 1: - result.add(list.get(0)); - result.addAll(toAdd); - return result; - default: - throw newIndexOutOfBounds(list, index); - } - } - default: - list.addAll(index, toAdd); - return list; - } - } - } - - public static List addAll(List list, List toAdd) { - switch (toAdd.size()) { - case 0: - // No-op. - return list; - case 1: - // Add one element. - return add(list, toAdd.get(0)); - default: - // True list merge, result >= 2. - switch (list.size()) { - case 0: - return new ArrayList(toAdd); - case 1: { - List result = new ArrayList(1 + toAdd.size()); - result.add(list.get(0)); - result.addAll(toAdd); - return result; - } - default: - list.addAll(toAdd); - return list; - } - } - } - - public static List addAll(List list, T... toAdd) { - switch (toAdd.length) { - case 0: - // No-op. - return list; - case 1: - // Add one element. - return add(list, toAdd[0]); - default: - // True list merge, result >= 2. - switch (list.size()) { - case 0: - return new ArrayList(Arrays.asList(toAdd)); - case 1: { - List result = new ArrayList(1 + toAdd.length); - result.add(list.get(0)); - result.addAll(Arrays.asList(toAdd)); - return result; - } - default: - list.addAll(Arrays.asList(toAdd)); - return list; - } - } - } - - public static List create() { - return Collections.emptyList(); - } - - public static List create(Collection collection) { - switch (collection.size()) { - case 0: - return create(); - default: - return new ArrayList(collection); - } - } - - public static List create(List list) { - switch (list.size()) { - case 0: - return create(); - case 1: - return create(list.get(0)); - default: - return new ArrayList(list); - } - } - - public static List create(T item) { - return Collections.singletonList(item); - } - - public static List create(T... items) { - switch (items.length) { - case 0: - return create(); - case 1: - return create(items[0]); - default: - return new ArrayList(Arrays.asList(items)); - } - } - - public static List remove(List list, int toRemove) { - switch (list.size()) { - case 0: - // Empty - throw newIndexOutOfBounds(list, toRemove); - case 1: - // Singleton -> Empty - if (toRemove == 0) { - return Collections.emptyList(); - } else { - throw newIndexOutOfBounds(list, toRemove); - } - case 2: - // ArrayList -> Singleton - switch (toRemove) { - case 0: - return Collections.singletonList(list.get(1)); - case 1: - return Collections.singletonList(list.get(0)); - default: - throw newIndexOutOfBounds(list, toRemove); - } - default: - // ArrayList - list.remove(toRemove); - return list; - } - } - - public static List set(List list, int index, T e) { - switch (list.size()) { - case 0: - // Empty - throw newIndexOutOfBounds(list, index); - case 1: - // Singleton - if (index == 0) { - return Collections.singletonList(e); - } else { - throw newIndexOutOfBounds(list, index); - } - default: - // ArrayList - list.set(index, e); - return list; - } - } - - public static > List sort(List list) { - if (list.size() > 1) { - Collections.sort(list); - } - return list; - } - - public static List sort(List list, Comparator sort) { - if (list.size() > 1) { - Collections.sort(list, sort); - } - return list; - } - - private static IndexOutOfBoundsException newIndexOutOfBounds(List list, int index) { - return new IndexOutOfBoundsException("Index: " + index + ", Size: " + list.size()); - } -} diff --git a/src/com/google/dart/compiler/util/TextOutputImpl.java b/src/com/google/dart/compiler/util/TextOutputImpl.java index 84c126da023..174fc429ed3 100644 --- a/src/com/google/dart/compiler/util/TextOutputImpl.java +++ b/src/com/google/dart/compiler/util/TextOutputImpl.java @@ -26,7 +26,7 @@ public class TextOutputImpl implements TextOutput { public TextOutputImpl(boolean compact) { this.compact = compact; - this.out = new StringBuilder(); + out = new StringBuilder(); } @Override From 9b6d08a6a994225cc6ef81cef7e15c8e6da20155 Mon Sep 17 00:00:00 2001 From: develar Date: Sun, 30 Sep 2012 15:50:23 +0400 Subject: [PATCH 05/27] cleanup --- .idea/artifacts/dart_ast_src.xml | 10 ++++++++++ .../js/JsToStringGenerationVisitor.java | 7 ++----- .../compiler/backend/js/ast/JsArrayAccess.java | 6 +++--- .../backend/js/ast/JsArrayLiteral.java | 6 +++--- .../backend/js/ast/JsBinaryOperation.java | 6 +++--- .../dart/compiler/backend/js/ast/JsBlock.java | 6 +++--- .../dart/compiler/backend/js/ast/JsBreak.java | 6 +++--- .../dart/compiler/backend/js/ast/JsCase.java | 6 +++--- .../dart/compiler/backend/js/ast/JsCatch.java | 6 +++--- .../compiler/backend/js/ast/JsConditional.java | 6 +++--- .../compiler/backend/js/ast/JsContinue.java | 6 +++--- .../compiler/backend/js/ast/JsDebugger.java | 6 +++--- .../compiler/backend/js/ast/JsDefault.java | 6 +++--- .../compiler/backend/js/ast/JsDoWhile.java | 6 +++--- .../compiler/backend/js/ast/JsDocComment.java | 13 +++++++++++++ .../dart/compiler/backend/js/ast/JsEmpty.java | 6 +++--- .../compiler/backend/js/ast/JsExprStmt.java | 6 +++--- .../dart/compiler/backend/js/ast/JsFor.java | 6 +++--- .../dart/compiler/backend/js/ast/JsForIn.java | 6 +++--- .../compiler/backend/js/ast/JsFunction.java | 6 +++--- .../dart/compiler/backend/js/ast/JsIf.java | 18 +++++++++--------- .../compiler/backend/js/ast/JsInvocation.java | 6 +++--- .../dart/compiler/backend/js/ast/JsLabel.java | 6 +++--- .../compiler/backend/js/ast/JsLiteral.java | 12 ++++++------ .../compiler/backend/js/ast/JsNameRef.java | 6 +++--- .../dart/compiler/backend/js/ast/JsNew.java | 6 +++--- .../compiler/backend/js/ast/JsNodeImpl.java | 15 ++------------- .../compiler/backend/js/ast/JsNullLiteral.java | 6 +++--- .../backend/js/ast/JsNumberLiteral.java | 12 ++++++------ .../backend/js/ast/JsObjectLiteral.java | 6 +++--- .../compiler/backend/js/ast/JsParameter.java | 6 +++--- .../backend/js/ast/JsPostfixOperation.java | 8 ++++---- .../backend/js/ast/JsPrefixOperation.java | 8 ++++---- .../compiler/backend/js/ast/JsProgram.java | 6 +++--- .../backend/js/ast/JsProgramFragment.java | 6 +++--- .../backend/js/ast/JsPropertyInitializer.java | 6 +++--- .../dart/compiler/backend/js/ast/JsRegExp.java | 6 +++--- .../dart/compiler/backend/js/ast/JsReturn.java | 6 +++--- .../backend/js/ast/JsStringLiteral.java | 6 +++--- .../dart/compiler/backend/js/ast/JsSwitch.java | 6 +++--- .../dart/compiler/backend/js/ast/JsThrow.java | 6 +++--- .../dart/compiler/backend/js/ast/JsTry.java | 6 +++--- .../backend/js/ast/JsUnaryOperation.java | 2 +- .../dart/compiler/backend/js/ast/JsVars.java | 12 ++++++------ .../compiler/backend/js/ast/JsVisitable.java | 4 ++-- .../compiler/backend/js/ast/JsVisitor.java | 6 +----- .../dart/compiler/backend/js/ast/JsWhile.java | 6 +++--- 47 files changed, 168 insertions(+), 163 deletions(-) create mode 100644 .idea/artifacts/dart_ast_src.xml create mode 100644 src/com/google/dart/compiler/backend/js/ast/JsDocComment.java diff --git a/.idea/artifacts/dart_ast_src.xml b/.idea/artifacts/dart_ast_src.xml new file mode 100644 index 00000000000..97361010d7c --- /dev/null +++ b/.idea/artifacts/dart_ast_src.xml @@ -0,0 +1,10 @@ + + + $PROJECT_DIR$/out/artifacts + + + + + + + \ No newline at end of file diff --git a/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java b/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java index 50d29b64e08..49b0c2bd610 100644 --- a/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java +++ b/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java @@ -200,7 +200,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { private TIntArrayList statementStarts = new TIntArrayList(); public JsToStringGenerationVisitor(TextOutput out) { - this.p = out; + p = out; } @Override @@ -1252,10 +1252,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { * If the binary operation has a higher precedence than op, then it won't * be parenthesized, so check the first argument of the binary operation. */ - if (binary.getOperator().getPrecedence() > op.getPrecedence()) { - return spaceCalc(op, binary.getArg1()); - } - return false; + return binary.getOperator().getPrecedence() > op.getPrecedence() && spaceCalc(op, binary.getArg1()); } if (arg instanceof JsPrefixOperation) { JsOperator op2 = ((JsPrefixOperation) arg).getOperator(); diff --git a/src/com/google/dart/compiler/backend/js/ast/JsArrayAccess.java b/src/com/google/dart/compiler/backend/js/ast/JsArrayAccess.java index c85c088d1f9..6a6eefdd3e0 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsArrayAccess.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsArrayAccess.java @@ -53,12 +53,12 @@ public final class JsArrayAccess extends JsExpressionImpl { } @Override - public void traverse(JsVisitor v, JsContext ctx) { - if (v.visit(this, ctx)) { + public void traverse(JsVisitor v, JsContext context) { + if (v.visit(this, context)) { arrayExpr = v.accept(arrayExpr); indexExpr = v.accept(indexExpr); } - v.endVisit(this, ctx); + v.endVisit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsArrayLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsArrayLiteral.java index 558a02245eb..5c1a8bcdb00 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsArrayLiteral.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsArrayLiteral.java @@ -57,11 +57,11 @@ public final class JsArrayLiteral extends JsLiteral { } @Override - public void traverse(JsVisitor v, JsContext ctx) { - if (v.visit(this, ctx)) { + public void traverse(JsVisitor v, JsContext context) { + if (v.visit(this, context)) { v.acceptWithInsertRemove(expressions); } - v.endVisit(this, ctx); + v.endVisit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsBinaryOperation.java b/src/com/google/dart/compiler/backend/js/ast/JsBinaryOperation.java index fc1513a74eb..9b6327f74c0 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsBinaryOperation.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsBinaryOperation.java @@ -81,8 +81,8 @@ public final class JsBinaryOperation extends JsExpressionImpl { } @Override - public void traverse(JsVisitor v, JsContext ctx) { - if (v.visit(this, ctx)) { + public void traverse(JsVisitor v, JsContext context) { + if (v.visit(this, context)) { if (op.isAssignment()) { arg1 = v.acceptLvalue(arg1); } else { @@ -90,7 +90,7 @@ public final class JsBinaryOperation extends JsExpressionImpl { } arg2 = v.accept(arg2); } - v.endVisit(this, ctx); + v.endVisit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsBlock.java b/src/com/google/dart/compiler/backend/js/ast/JsBlock.java index cad730b4b5c..633d88f72b2 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsBlock.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsBlock.java @@ -44,11 +44,11 @@ public class JsBlock extends JsNodeImpl implements JsStatement { } @Override - public void traverse(JsVisitor v, JsContext ctx) { - if (v.visit(this, ctx)) { + public void traverse(JsVisitor v, JsContext context) { + if (v.visit(this, context)) { v.acceptWithInsertRemove(statements); } - v.endVisit(this, ctx); + v.endVisit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsBreak.java b/src/com/google/dart/compiler/backend/js/ast/JsBreak.java index ce1c9e66f2d..3360bcb7b2f 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsBreak.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsBreak.java @@ -22,12 +22,12 @@ public final class JsBreak extends JsContinue { } @Override - public void traverse(JsVisitor v, JsContext ctx) { - if (v.visit(this, ctx)) { + public void traverse(JsVisitor v, JsContext context) { + if (v.visit(this, context)) { if (label != null) { v.accept(label); } } - v.endVisit(this, ctx); + v.endVisit(this, context); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsCase.java b/src/com/google/dart/compiler/backend/js/ast/JsCase.java index a8b881b41cc..e1598a509d6 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsCase.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsCase.java @@ -24,12 +24,12 @@ public final class JsCase extends JsSwitchMember { } @Override - public void traverse(JsVisitor v, JsContext ctx) { - if (v.visit(this, ctx)) { + public void traverse(JsVisitor v, JsContext context) { + if (v.visit(this, context)) { caseExpr = v.accept(caseExpr); v.acceptWithInsertRemove(statements); } - v.endVisit(this, ctx); + v.endVisit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsCatch.java b/src/com/google/dart/compiler/backend/js/ast/JsCatch.java index fc97ad4df4c..1d3846fa6f0 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsCatch.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsCatch.java @@ -48,15 +48,15 @@ public class JsCatch extends JsNodeImpl implements HasCondition { } @Override - public void traverse(JsVisitor v, JsContext ctx) { - if (v.visit(this, ctx)) { + public void traverse(JsVisitor v, JsContext context) { + if (v.visit(this, context)) { param = v.accept(param); if (condition != null) { condition = v.accept(condition); } body = v.accept(body); } - v.endVisit(this, ctx); + v.endVisit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsConditional.java b/src/com/google/dart/compiler/backend/js/ast/JsConditional.java index 5b98b278b5e..2bf2dda635e 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsConditional.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsConditional.java @@ -61,13 +61,13 @@ public final class JsConditional extends JsExpressionImpl { } @Override - public void traverse(JsVisitor v, JsContext ctx) { - if (v.visit(this, ctx)) { + public void traverse(JsVisitor v, JsContext context) { + if (v.visit(this, context)) { testExpr = v.accept(testExpr); thenExpr = v.accept(thenExpr); elseExpr = v.accept(elseExpr); } - v.endVisit(this, ctx); + v.endVisit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsContinue.java b/src/com/google/dart/compiler/backend/js/ast/JsContinue.java index 89396dc89a8..65e88f25932 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsContinue.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsContinue.java @@ -26,13 +26,13 @@ public class JsContinue extends JsNodeImpl implements JsStatement { } @Override - public void traverse(JsVisitor v, JsContext ctx) { - if (v.visit(this, ctx)) { + public void traverse(JsVisitor v, JsContext context) { + if (v.visit(this, context)) { if (label != null) { v.accept(label); } } - v.endVisit(this, ctx); + v.endVisit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsDebugger.java b/src/com/google/dart/compiler/backend/js/ast/JsDebugger.java index fe9900e134a..45db05149a8 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsDebugger.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsDebugger.java @@ -13,9 +13,9 @@ public class JsDebugger extends JsNodeImpl implements JsStatement { } @Override - public void traverse(JsVisitor v, JsContext ctx) { - v.visit(this, ctx); - v.endVisit(this, ctx); + public void traverse(JsVisitor v, JsContext context) { + v.visit(this, context); + v.endVisit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsDefault.java b/src/com/google/dart/compiler/backend/js/ast/JsDefault.java index 89624ddd683..b59dca783a9 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsDefault.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsDefault.java @@ -14,11 +14,11 @@ public final class JsDefault extends JsSwitchMember { } @Override - public void traverse(JsVisitor v, JsContext ctx) { - if (v.visit(this, ctx)) { + public void traverse(JsVisitor v, JsContext context) { + if (v.visit(this, context)) { v.acceptWithInsertRemove(statements); } - v.endVisit(this, ctx); + v.endVisit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsDoWhile.java b/src/com/google/dart/compiler/backend/js/ast/JsDoWhile.java index 44f25010e07..10253c39b55 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsDoWhile.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsDoWhile.java @@ -21,11 +21,11 @@ public class JsDoWhile extends JsWhile { } @Override - public void traverse(JsVisitor v, JsContext ctx) { - if (v.visit(this, ctx)) { + public void traverse(JsVisitor v, JsContext context) { + if (v.visit(this, context)) { condition = v.accept(condition); body = v.accept(body); } - v.endVisit(this, ctx); + v.endVisit(this, context); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsDocComment.java b/src/com/google/dart/compiler/backend/js/ast/JsDocComment.java new file mode 100644 index 00000000000..e66ea3eb2f0 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/JsDocComment.java @@ -0,0 +1,13 @@ +package com.google.dart.compiler.backend.js.ast; + +public class JsDocComment extends JsNodeImpl implements JsStatement { + @Override + public NodeKind getKind() { + return null; //To change body of implemented methods use File | Settings | File Templates. + } + + @Override + public void traverse(JsVisitor visitor, JsContext context) { + //To change body of implemented methods use File | Settings | File Templates. + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsEmpty.java b/src/com/google/dart/compiler/backend/js/ast/JsEmpty.java index 188676e0ddd..3ecc8f1d3bc 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsEmpty.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsEmpty.java @@ -13,9 +13,9 @@ public class JsEmpty extends JsNodeImpl implements JsStatement { } @Override - public void traverse(JsVisitor v, JsContext ctx) { - v.visit(this, ctx); - v.endVisit(this, ctx); + public void traverse(JsVisitor v, JsContext context) { + v.visit(this, context); + v.endVisit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsExprStmt.java b/src/com/google/dart/compiler/backend/js/ast/JsExprStmt.java index 0ad3a6c74b5..6ab649f2cb4 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsExprStmt.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsExprStmt.java @@ -18,11 +18,11 @@ public final class JsExprStmt extends JsNodeImpl implements JsStatement { } @Override - public void traverse(JsVisitor v, JsContext ctx) { - if (v.visit(this, ctx)) { + public void traverse(JsVisitor v, JsContext context) { + if (v.visit(this, context)) { expr = v.accept(expr); } - v.endVisit(this, ctx); + v.endVisit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsFor.java b/src/com/google/dart/compiler/backend/js/ast/JsFor.java index ccf61b77954..65f24607696 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsFor.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsFor.java @@ -63,8 +63,8 @@ public class JsFor extends JsNodeImpl implements JsStatement { } @Override - public void traverse(JsVisitor v, JsContext ctx) { - if (v.visit(this, ctx)) { + public void traverse(JsVisitor v, JsContext context) { + if (v.visit(this, context)) { assert (!(initExpr != null && initVars != null)); if (initExpr != null) { @@ -83,7 +83,7 @@ public class JsFor extends JsNodeImpl implements JsStatement { } body = v.accept(body); } - v.endVisit(this, ctx); + v.endVisit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsForIn.java b/src/com/google/dart/compiler/backend/js/ast/JsForIn.java index 048fb02b25c..a0e31f4af5b 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsForIn.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsForIn.java @@ -53,15 +53,15 @@ public class JsForIn extends JsNodeImpl implements JsStatement { } @Override - public void traverse(JsVisitor v, JsContext ctx) { - if (v.visit(this, ctx)) { + public void traverse(JsVisitor v, JsContext context) { + if (v.visit(this, context)) { if (iterExpr != null) { iterExpr = v.acceptLvalue(iterExpr); } objExpr = v.accept(objExpr); body = v.accept(body); } - v.endVisit(this, ctx); + v.endVisit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsFunction.java b/src/com/google/dart/compiler/backend/js/ast/JsFunction.java index 8a55f90b79d..a4e9914100d 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsFunction.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsFunction.java @@ -91,12 +91,12 @@ public final class JsFunction extends JsLiteral implements HasName { } @Override - public void traverse(JsVisitor v, JsContext ctx) { - if (v.visit(this, ctx)) { + public void traverse(JsVisitor v, JsContext context) { + if (v.visit(this, context)) { v.acceptWithInsertRemove(params); body = v.accept(body); } - v.endVisit(this, ctx); + v.endVisit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsIf.java b/src/com/google/dart/compiler/backend/js/ast/JsIf.java index 1722bc525c7..ad75b9b2a2d 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsIf.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsIf.java @@ -38,28 +38,28 @@ public final class JsIf extends JsNodeImpl implements JsStatement { return thenStatement; } - public void setElseStatement(JsStatement elseStmt) { - this.elseStatement = elseStmt; + public void setElseStatement(JsStatement elseStatement) { + this.elseStatement = elseStatement; } - public void setIfExpression(JsExpression ifExpr) { - this.ifExpression = ifExpr; + public void setIfExpression(JsExpression ifExpression) { + this.ifExpression = ifExpression; } - public void setThenStatement(JsStatement thenStmt) { - this.thenStatement = thenStmt; + public void setThenStatement(JsStatement thenStatement) { + this.thenStatement = thenStatement; } @Override - public void traverse(JsVisitor v, JsContext ctx) { - if (v.visit(this, ctx)) { + public void traverse(JsVisitor v, JsContext context) { + if (v.visit(this, context)) { ifExpression = v.accept(ifExpression); thenStatement = v.accept(thenStatement); if (elseStatement != null) { elseStatement = v.accept(elseStatement); } } - v.endVisit(this, ctx); + v.endVisit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsInvocation.java b/src/com/google/dart/compiler/backend/js/ast/JsInvocation.java index 1856dcc6dad..96b9f8e5cec 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsInvocation.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsInvocation.java @@ -68,12 +68,12 @@ public final class JsInvocation extends JsExpressionImpl implements HasArguments } @Override - public void traverse(JsVisitor v, JsContext ctx) { - if (v.visit(this, ctx)) { + public void traverse(JsVisitor v, JsContext context) { + if (v.visit(this, context)) { qualifier = v.accept(qualifier); v.acceptList(arguments); } - v.endVisit(this, ctx); + v.endVisit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsLabel.java b/src/com/google/dart/compiler/backend/js/ast/JsLabel.java index f9817ee3390..040434b95b6 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsLabel.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsLabel.java @@ -38,11 +38,11 @@ public class JsLabel extends JsNodeImpl implements JsStatement, HasName { } @Override - public void traverse(JsVisitor v, JsContext ctx) { - if (v.visit(this, ctx)) { + public void traverse(JsVisitor v, JsContext context) { + if (v.visit(this, context)) { stmt = v.accept(stmt); } - v.endVisit(this, ctx); + v.endVisit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsLiteral.java index eb00aab6e36..f8cc734d453 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsLiteral.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsLiteral.java @@ -43,9 +43,9 @@ public abstract class JsLiteral extends JsExpressionImpl implements CanBooleanEv } @Override - public void traverse(JsVisitor v, JsContext ctx) { - v.visit(this, ctx); - v.endVisit(this, ctx); + public void traverse(JsVisitor v, JsContext context) { + v.visit(this, context); + v.endVisit(this, context); } @Override @@ -87,9 +87,9 @@ public abstract class JsLiteral extends JsExpressionImpl implements CanBooleanEv } @Override - public void traverse(JsVisitor v, JsContext ctx) { - v.visit(this, ctx); - v.endVisit(this, ctx); + public void traverse(JsVisitor v, JsContext context) { + v.visit(this, context); + v.endVisit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNameRef.java b/src/com/google/dart/compiler/backend/js/ast/JsNameRef.java index 54d1a923aa1..1f12ed8c628 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsNameRef.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsNameRef.java @@ -109,13 +109,13 @@ public final class JsNameRef extends JsExpressionImpl implements CanBooleanEval, } @Override - public void traverse(JsVisitor v, JsContext ctx) { - if (v.visit(this, ctx)) { + public void traverse(JsVisitor v, JsContext context) { + if (v.visit(this, context)) { if (qualifier != null) { qualifier = v.accept(qualifier); } } - v.endVisit(this, ctx); + v.endVisit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNew.java b/src/com/google/dart/compiler/backend/js/ast/JsNew.java index 68ffb540ca5..c65bebbde66 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsNew.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsNew.java @@ -51,12 +51,12 @@ public final class JsNew extends JsExpressionImpl implements HasArguments { } @Override - public void traverse(JsVisitor v, JsContext ctx) { - if (v.visit(this, ctx)) { + public void traverse(JsVisitor v, JsContext context) { + if (v.visit(this, context)) { ctorExpression = v.accept(ctorExpression); v.acceptList(arguments); } - v.endVisit(this, ctx); + v.endVisit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNodeImpl.java b/src/com/google/dart/compiler/backend/js/ast/JsNodeImpl.java index e96a902c178..583f0e7de08 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsNodeImpl.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsNodeImpl.java @@ -1,6 +1,5 @@ package com.google.dart.compiler.backend.js.ast; -import com.google.dart.compiler.backend.js.JsSourceGenerationVisitor; import com.google.dart.compiler.backend.js.JsToStringGenerationVisitor; import com.google.dart.compiler.common.AbstractNode; import com.google.dart.compiler.common.SourceInfo; @@ -10,20 +9,10 @@ abstract class JsNodeImpl extends AbstractNode implements JsNode { protected JsNodeImpl() { } - // Causes source generation to delegate to the one visitor - public final String toSource() { - TextOutputImpl out = new TextOutputImpl(); - JsSourceGenerationVisitor v = new JsSourceGenerationVisitor(out); - v.accept(this); - return out.toString(); - } - - // Causes source generation to delegate to the one visitor @Override public String toString() { TextOutputImpl out = new TextOutputImpl(); - JsToStringGenerationVisitor v = new JsToStringGenerationVisitor(out); - v.accept(this); + new JsToStringGenerationVisitor(out).accept(this); return out.toString(); } @@ -34,7 +23,7 @@ abstract class JsNodeImpl extends AbstractNode implements JsNode { public JsNode setSourceRef(SourceInfo info) { if (info != null) { - this.setSourceInfo(info); + setSourceInfo(info); } return this; } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNullLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsNullLiteral.java index 0514fb0df89..028760e4c0d 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsNullLiteral.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsNullLiteral.java @@ -29,9 +29,9 @@ public final class JsNullLiteral extends JsValueLiteral { } @Override - public void traverse(JsVisitor v, JsContext ctx) { - v.visit(this, ctx); - v.endVisit(this, ctx); + public void traverse(JsVisitor v, JsContext context) { + v.visit(this, context); + v.endVisit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNumberLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsNumberLiteral.java index e191e7e8954..8d47eca2dcf 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsNumberLiteral.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsNumberLiteral.java @@ -38,9 +38,9 @@ public abstract class JsNumberLiteral extends JsValueLiteral { } @Override - public void traverse(JsVisitor v, JsContext ctx) { - v.visit(this, ctx); - v.endVisit(this, ctx); + public void traverse(JsVisitor v, JsContext context) { + v.visit(this, context); + v.endVisit(this, context); } public String toString() { @@ -66,9 +66,9 @@ public abstract class JsNumberLiteral extends JsValueLiteral { } @Override - public void traverse(JsVisitor v, JsContext ctx) { - v.visit(this, ctx); - v.endVisit(this, ctx); + public void traverse(JsVisitor v, JsContext context) { + v.visit(this, context); + v.endVisit(this, context); } public String toString() { diff --git a/src/com/google/dart/compiler/backend/js/ast/JsObjectLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsObjectLiteral.java index b0353a3d3bd..f1ef5b54bfd 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsObjectLiteral.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsObjectLiteral.java @@ -69,11 +69,11 @@ public final class JsObjectLiteral extends JsLiteral { } @Override - public void traverse(JsVisitor v, JsContext ctx) { - if (v.visit(this, ctx)) { + public void traverse(JsVisitor v, JsContext context) { + if (v.visit(this, context)) { v.acceptWithInsertRemove(properties); } - v.endVisit(this, ctx); + v.endVisit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsParameter.java b/src/com/google/dart/compiler/backend/js/ast/JsParameter.java index fbb0908e69f..323646468c6 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsParameter.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsParameter.java @@ -27,9 +27,9 @@ public final class JsParameter extends JsNodeImpl implements HasName { } @Override - public void traverse(JsVisitor v, JsContext ctx) { - v.visit(this, ctx); - v.endVisit(this, ctx); + public void traverse(JsVisitor v, JsContext context) { + v.visit(this, context); + v.endVisit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsPostfixOperation.java b/src/com/google/dart/compiler/backend/js/ast/JsPostfixOperation.java index a33536f6c0d..d32565dfc0b 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsPostfixOperation.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsPostfixOperation.java @@ -28,11 +28,11 @@ public final class JsPostfixOperation extends JsUnaryOperation { } @Override - public void traverse(JsVisitor v, JsContext ctx) { - if (v.visit(this, ctx)) { - super.traverse(v, ctx); + public void traverse(JsVisitor v, JsContext context) { + if (v.visit(this, context)) { + super.traverse(v, context); } - v.endVisit(this, ctx); + v.endVisit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsPrefixOperation.java b/src/com/google/dart/compiler/backend/js/ast/JsPrefixOperation.java index 8ce12c7bacd..e66e6883eb3 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsPrefixOperation.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsPrefixOperation.java @@ -55,11 +55,11 @@ public final class JsPrefixOperation extends JsUnaryOperation implements CanBool } @Override - public void traverse(JsVisitor v, JsContext ctx) { - if (v.visit(this, ctx)) { - super.traverse(v, ctx); + public void traverse(JsVisitor v, JsContext context) { + if (v.visit(this, context)) { + super.traverse(v, context); } - v.endVisit(this, ctx); + v.endVisit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsProgram.java b/src/com/google/dart/compiler/backend/js/ast/JsProgram.java index afea24a93ca..c22f4a3f542 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsProgram.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsProgram.java @@ -112,13 +112,13 @@ public final class JsProgram extends JsNodeImpl { } @Override - public void traverse(JsVisitor v, @Nullable JsContext ctx) { - if (v.visit(this, ctx)) { + public void traverse(JsVisitor v, @Nullable JsContext context) { + if (v.visit(this, context)) { for (JsProgramFragment fragment : fragments) { v.accept(fragment); } } - v.endVisit(this, ctx); + v.endVisit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsProgramFragment.java b/src/com/google/dart/compiler/backend/js/ast/JsProgramFragment.java index 3a291556f66..a89da4065a6 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsProgramFragment.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsProgramFragment.java @@ -20,11 +20,11 @@ public class JsProgramFragment extends JsNodeImpl { } @Override - public void traverse(JsVisitor v, JsContext ctx) { - if (v.visit(this, ctx)) { + public void traverse(JsVisitor v, JsContext context) { + if (v.visit(this, context)) { v.accept(globalBlock); } - v.endVisit(this, ctx); + v.endVisit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsPropertyInitializer.java b/src/com/google/dart/compiler/backend/js/ast/JsPropertyInitializer.java index 0a8d3e2467d..d2a212c9145 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsPropertyInitializer.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsPropertyInitializer.java @@ -39,12 +39,12 @@ public class JsPropertyInitializer extends JsNodeImpl { } @Override - public void traverse(JsVisitor v, JsContext ctx) { - if (v.visit(this, ctx)) { + public void traverse(JsVisitor v, JsContext context) { + if (v.visit(this, context)) { labelExpr = v.accept(labelExpr); valueExpr = v.accept(valueExpr); } - v.endVisit(this, ctx); + v.endVisit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsRegExp.java b/src/com/google/dart/compiler/backend/js/ast/JsRegExp.java index 158c7a5e092..3bcc404b892 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsRegExp.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsRegExp.java @@ -52,9 +52,9 @@ public final class JsRegExp extends JsValueLiteral { } @Override - public void traverse(JsVisitor v, JsContext ctx) { - v.visit(this, ctx); - v.endVisit(this, ctx); + public void traverse(JsVisitor v, JsContext context) { + v.visit(this, context); + v.endVisit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsReturn.java b/src/com/google/dart/compiler/backend/js/ast/JsReturn.java index b8806c82bab..a09421806ec 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsReturn.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsReturn.java @@ -27,13 +27,13 @@ public final class JsReturn extends JsNodeImpl implements JsStatement { } @Override - public void traverse(JsVisitor v, JsContext ctx) { - if (v.visit(this, ctx)) { + public void traverse(JsVisitor v, JsContext context) { + if (v.visit(this, context)) { if (expr != null) { expr = v.accept(expr); } } - v.endVisit(this, ctx); + v.endVisit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsStringLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsStringLiteral.java index 77e60c9b89a..3cff1258798 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsStringLiteral.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsStringLiteral.java @@ -41,9 +41,9 @@ public final class JsStringLiteral extends JsValueLiteral { } @Override - public void traverse(JsVisitor v, JsContext ctx) { - v.visit(this, ctx); - v.endVisit(this, ctx); + public void traverse(JsVisitor v, JsContext context) { + v.visit(this, context); + v.endVisit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsSwitch.java b/src/com/google/dart/compiler/backend/js/ast/JsSwitch.java index 4e76da8d939..683dd195941 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsSwitch.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsSwitch.java @@ -32,12 +32,12 @@ public class JsSwitch extends JsNodeImpl implements JsStatement { } @Override - public void traverse(JsVisitor v, JsContext ctx) { - if (v.visit(this, ctx)) { + public void traverse(JsVisitor v, JsContext context) { + if (v.visit(this, context)) { expr = v.accept(expr); v.acceptWithInsertRemove(cases); } - v.endVisit(this, ctx); + v.endVisit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsThrow.java b/src/com/google/dart/compiler/backend/js/ast/JsThrow.java index f6b8f1987f8..2c7867056f3 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsThrow.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsThrow.java @@ -23,11 +23,11 @@ public class JsThrow extends JsNodeImpl implements JsStatement { } @Override - public void traverse(JsVisitor v, JsContext ctx) { - if (v.visit(this, ctx)) { + public void traverse(JsVisitor v, JsContext context) { + if (v.visit(this, context)) { expression = v.accept(expression); } - v.endVisit(this, ctx); + v.endVisit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsTry.java b/src/com/google/dart/compiler/backend/js/ast/JsTry.java index 2a3b78607b6..cd0fde570f5 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsTry.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsTry.java @@ -48,15 +48,15 @@ public class JsTry extends JsNodeImpl implements JsStatement { } @Override - public void traverse(JsVisitor v, JsContext ctx) { - if (v.visit(this, ctx)) { + public void traverse(JsVisitor v, JsContext context) { + if (v.visit(this, context)) { tryBlock = v.accept(tryBlock); v.acceptWithInsertRemove(catches); if (finallyBlock != null) { finallyBlock = v.accept(finallyBlock); } } - v.endVisit(this, ctx); + v.endVisit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsUnaryOperation.java b/src/com/google/dart/compiler/backend/js/ast/JsUnaryOperation.java index 6f3ee93273d..669fe9a0a78 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsUnaryOperation.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsUnaryOperation.java @@ -41,7 +41,7 @@ public abstract class JsUnaryOperation extends JsExpressionImpl { } @Override - public void traverse(JsVisitor v, JsContext ctx) { + public void traverse(JsVisitor v, JsContext context) { if (op.isModifying()) { // The delete operator is practically like an assignment of undefined, so // for practical purposes we're treating it as an lvalue. diff --git a/src/com/google/dart/compiler/backend/js/ast/JsVars.java b/src/com/google/dart/compiler/backend/js/ast/JsVars.java index d02c932715a..18617861955 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsVars.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsVars.java @@ -82,13 +82,13 @@ public class JsVars extends JsNodeImpl implements JsStatement, Iterable T accept(T node) { - return this.doAccept(node); + return doAccept(node); } public final void acceptList(List collection) { @@ -105,10 +105,6 @@ public class JsVisitor { doAcceptWithInsertRemove(collection); } - public boolean didChange() { - throw new UnsupportedOperationException(); - } - public void endVisit(JsArrayAccess x, JsContext ctx) { } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsWhile.java b/src/com/google/dart/compiler/backend/js/ast/JsWhile.java index c3639154d0d..c00d355335d 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsWhile.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsWhile.java @@ -36,12 +36,12 @@ public class JsWhile extends JsNodeImpl implements JsStatement { } @Override - public void traverse(JsVisitor v, JsContext ctx) { - if (v.visit(this, ctx)) { + public void traverse(JsVisitor v, JsContext context) { + if (v.visit(this, context)) { condition = v.accept(condition); body = v.accept(body); } - v.endVisit(this, ctx); + v.endVisit(this, context); } @Override From 720a8f250b8278fb604c2e6fecc33f9f16481dcb Mon Sep 17 00:00:00 2001 From: develar Date: Sun, 30 Sep 2012 17:18:51 +0400 Subject: [PATCH 06/27] KT-2864 --- .../js/JsToStringGenerationVisitor.java | 87 +- .../compiler/backend/js/ast/JsDocComment.java | 48 +- .../compiler/backend/js/ast/JsVisitor.java | 770 +++++++++--------- 3 files changed, 500 insertions(+), 405 deletions(-) diff --git a/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java b/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java index 49b0c2bd610..62905cb51db 100644 --- a/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java +++ b/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java @@ -222,11 +222,11 @@ public class JsToStringGenerationVisitor extends JsVisitor { } private void printExpressions(List expressions) { - boolean sep = false; - for (JsExpression arg : expressions) { - sep = sepCommaOptSpace(sep); - boolean isEnclosed = parenPushIfCommaExpression(arg); - accept(arg); + boolean notFirst = false; + for (JsExpression expression : expressions) { + notFirst = sepCommaOptSpace(notFirst) && !(expression instanceof JsDocComment); + boolean isEnclosed = parenPushIfCommaExpression(expression); + accept(expression); if (isEnclosed) { rightParen(); } @@ -556,10 +556,10 @@ public class JsToStringGenerationVisitor extends JsVisitor { } leftParen(); - boolean sep = false; + boolean notFirst = false; for (Object element : x.getParameters()) { JsParameter param = (JsParameter) element; - sep = sepCommaOptSpace(sep); + notFirst = sepCommaOptSpace(notFirst); accept(param); } rightParen(); @@ -610,8 +610,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { @Override public boolean visit(JsInvocation x, JsContext ctx) { - JsExpression qualifier = x.getQualifier(); - printPair(x, qualifier); + printPair(x, x.getQualifier()); leftParen(); printExpressions(x.getArguments()); @@ -697,20 +696,20 @@ public class JsToStringGenerationVisitor extends JsVisitor { p.indentIn(); } - boolean isNotFirst = false; + boolean notFirst = false; for (JsPropertyInitializer item : objectLiteral.getPropertyInitializers()) { - if (isNotFirst) { + if (notFirst) { p.print(','); } if (objectLiteral.isMultiline()) { newlineOpt(); } - else if (isNotFirst) { + else if (notFirst) { spaceOpt(); } - isNotFirst = true; + notFirst = true; JsExpression labelExpr = item.getLabelExpr(); // labels can be either string, integral, or decimal literals @@ -907,7 +906,65 @@ public class JsToStringGenerationVisitor extends JsVisitor { return false; } - protected void _newline() { + @Override + public boolean visit(JsDocComment comment, JsContext context) { + boolean asSingleLine = comment.getTags().size() == 1; + if (!asSingleLine) { + newlineOpt(); + } + p.print("/**"); + if (asSingleLine) { + space(); + } + else { + newline(); + } + + boolean notFirst = false; + for (Map.Entry entry : comment.getTags().entrySet()) { + if (notFirst) { + newline(); + p.print(' '); + p.print('*'); + } + else { + notFirst = true; + } + + p.print('@'); + p.print(entry.getKey()); + Object value = entry.getValue(); + if (value != null) { + space(); + if (value instanceof CharSequence) { + p.print((CharSequence) value); + } + else { + visit((JsNameRef) value, context); + } + } + + if (!asSingleLine) { + newline(); + } + } + + if (asSingleLine) { + space(); + } + else { + newlineOpt(); + } + + p.print('*'); + p.print('/'); + if (asSingleLine) { + spaceOpt(); + } + return false; + } + + protected void newline() { p.newline(); } @@ -977,7 +1034,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { newlineOpt(); } else { - _newline(); + newline(); } } else { diff --git a/src/com/google/dart/compiler/backend/js/ast/JsDocComment.java b/src/com/google/dart/compiler/backend/js/ast/JsDocComment.java index e66ea3eb2f0..ffc8867edae 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsDocComment.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsDocComment.java @@ -1,13 +1,49 @@ package com.google.dart.compiler.backend.js.ast; -public class JsDocComment extends JsNodeImpl implements JsStatement { - @Override - public NodeKind getKind() { - return null; //To change body of implemented methods use File | Settings | File Templates. +import java.util.Collections; +import java.util.Map; + +public class JsDocComment extends JsExpressionImpl implements JsExpression { + private final Map tags; + + public JsDocComment(Map tags) { + this.tags = tags; + } + + public Map getTags() { + return tags; + } + + public JsDocComment(String tagName, JsNameRef tagValue) { + tags = Collections.singletonMap(tagName, tagValue); + } + + public JsDocComment(String tagName, String tagValue) { + tags = Collections.singletonMap(tagName, tagValue); } @Override - public void traverse(JsVisitor visitor, JsContext context) { - //To change body of implemented methods use File | Settings | File Templates. + public NodeKind getKind() { + throw new UnsupportedOperationException(); + } + + @Override + public void traverse(JsVisitor v, JsContext context) { + v.visit(this, context); + } + + @Override + public boolean hasSideEffects() { + return false; + } + + @Override + public boolean isDefinitelyNotNull() { + return true; + } + + @Override + public boolean isDefinitelyNull() { + return false; } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsVisitor.java b/src/com/google/dart/compiler/backend/js/ast/JsVisitor.java index 814ae92385b..a7575cf0766 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsVisitor.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsVisitor.java @@ -12,420 +12,422 @@ import java.util.List; * Implemented by nodes that will visit child nodes. */ public class JsVisitor { + protected static final JsContext LVALUE_CONTEXT = new JsContext() { + @Override + public boolean canInsert() { + return false; + } - protected static final JsContext LVALUE_CONTEXT = new JsContext() { + @Override + public boolean canRemove() { + return false; + } - @Override - public boolean canInsert() { - return false; + @Override + public void insertAfter(JsVisitable node) { + throw new UnsupportedOperationException(); + } + + @Override + public void insertBefore(JsVisitable node) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isLvalue() { + return true; + } + + @Override + public void removeMe() { + throw new UnsupportedOperationException(); + } + + @Override + public void replaceMe(JsVisitable node) { + throw new UnsupportedOperationException(); + } + }; + + protected static final JsContext UNMODIFIABLE_CONTEXT = new JsContext() { + + @Override + public boolean canInsert() { + return false; + } + + @Override + public boolean canRemove() { + return false; + } + + @Override + public void insertAfter(JsVisitable node) { + throw new UnsupportedOperationException(); + } + + @Override + public void insertBefore(JsVisitable node) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isLvalue() { + return false; + } + + @Override + public void removeMe() { + throw new UnsupportedOperationException(); + } + + @Override + public void replaceMe(JsVisitable node) { + throw new UnsupportedOperationException(); + } + }; + + public final T accept(T node) { + return doAccept(node); } - @Override - public boolean canRemove() { - return false; + public final void acceptList(List collection) { + doAcceptList(collection); } - @Override - public void insertAfter(JsVisitable node) { - throw new UnsupportedOperationException(); + public JsExpression acceptLvalue(JsExpression expr) { + return doAcceptLvalue(expr); } - @Override - public void insertBefore(JsVisitable node) { - throw new UnsupportedOperationException(); + public final void acceptWithInsertRemove(List collection) { + doAcceptWithInsertRemove(collection); } - @Override - public boolean isLvalue() { - return true; + public void endVisit(JsArrayAccess x, JsContext ctx) { } - @Override - public void removeMe() { - throw new UnsupportedOperationException(); + public void endVisit(JsArrayLiteral x, JsContext ctx) { } - @Override - public void replaceMe(JsVisitable node) { - throw new UnsupportedOperationException(); - } - }; - - protected static final JsContext UNMODIFIABLE_CONTEXT = new JsContext() { - - @Override - public boolean canInsert() { - return false; + public void endVisit(JsBinaryOperation x, JsContext ctx) { } - @Override - public boolean canRemove() { - return false; + public void endVisit(JsBlock x, JsContext ctx) { } - @Override - public void insertAfter(JsVisitable node) { - throw new UnsupportedOperationException(); + public void endVisit(JsLiteral.JsBooleanLiteral x, JsContext ctx) { } - @Override - public void insertBefore(JsVisitable node) { - throw new UnsupportedOperationException(); + public void endVisit(JsBreak x, JsContext ctx) { } - @Override - public boolean isLvalue() { - return false; + public void endVisit(JsCase x, JsContext ctx) { } - @Override - public void removeMe() { - throw new UnsupportedOperationException(); + public void endVisit(JsCatch x, JsContext ctx) { } - @Override - public void replaceMe(JsVisitable node) { - throw new UnsupportedOperationException(); + public void endVisit(JsConditional x, JsContext ctx) { } - }; - public final T accept(T node) { - return doAccept(node); - } - - public final void acceptList(List collection) { - doAcceptList(collection); - } - - public JsExpression acceptLvalue(JsExpression expr) { - return doAcceptLvalue(expr); - } - - public final void acceptWithInsertRemove(List collection) { - doAcceptWithInsertRemove(collection); - } - - public void endVisit(JsArrayAccess x, JsContext ctx) { - } - - public void endVisit(JsArrayLiteral x, JsContext ctx) { - } - - public void endVisit(JsBinaryOperation x, JsContext ctx) { - } - - public void endVisit(JsBlock x, JsContext ctx) { - } - - public void endVisit(JsLiteral.JsBooleanLiteral x, JsContext ctx) { - } - - public void endVisit(JsBreak x, JsContext ctx) { - } - - public void endVisit(JsCase x, JsContext ctx) { - } - - public void endVisit(JsCatch x, JsContext ctx) { - } - - public void endVisit(JsConditional x, JsContext ctx) { - } - - public void endVisit(JsContinue x, JsContext ctx) { - } - - public void endVisit(JsDebugger x, JsContext ctx) { - } - - public void endVisit(JsDefault x, JsContext ctx) { - } - - public void endVisit(JsDoWhile x, JsContext ctx) { - } - - public void endVisit(JsEmpty x, JsContext ctx) { - } - - public void endVisit(JsExprStmt x, JsContext ctx) { - } - - public void endVisit(JsFor x, JsContext ctx) { - } - - public void endVisit(JsForIn x, JsContext ctx) { - } - - public void endVisit(JsFunction x, JsContext ctx) { - } - - public void endVisit(JsIf x, JsContext ctx) { - } - - public void endVisit(JsInvocation x, JsContext ctx) { - } - - public void endVisit(JsLabel x, JsContext ctx) { - } - - public void endVisit(JsNameRef x, JsContext ctx) { - } - - public void endVisit(JsNew x, JsContext ctx) { - } - - public void endVisit(JsNullLiteral x, JsContext ctx) { - } - - public void endVisit(JsNumberLiteral x, JsContext ctx) { - } - - public void endVisit(JsObjectLiteral x, JsContext ctx) { - } - - public void endVisit(JsParameter x, JsContext ctx) { - } - - public void endVisit(JsPostfixOperation x, JsContext ctx) { - } - - public void endVisit(JsPrefixOperation x, JsContext ctx) { - } - - public void endVisit(JsProgram x, JsContext ctx) { - } - - public void endVisit(JsProgramFragment x, JsContext ctx) { - } - - public void endVisit(JsPropertyInitializer x, JsContext ctx) { - } - - public void endVisit(JsRegExp x, JsContext ctx) { - } - - public void endVisit(JsReturn x, JsContext ctx) { - } - - public void endVisit(JsStringLiteral x, JsContext ctx) { - } - - public void endVisit(JsSwitch x, JsContext ctx) { - } - - public void endVisit(JsLiteral.JsThisRef x, JsContext ctx) { - } - - public void endVisit(JsThrow x, JsContext ctx) { - } - - public void endVisit(JsTry x, JsContext ctx) { - } - - public void endVisit(JsVar x, JsContext ctx) { - } - - public void endVisit(JsVars x, JsContext ctx) { - } - - public void endVisit(JsWhile x, JsContext ctx) { - } - - public boolean visit(JsArrayAccess x, JsContext ctx) { - return true; - } - - public boolean visit(JsArrayLiteral x, JsContext ctx) { - return true; - } - - public boolean visit(JsBinaryOperation x, JsContext ctx) { - return true; - } - - public boolean visit(JsBlock x, JsContext ctx) { - return true; - } - - public boolean visit(JsLiteral.JsBooleanLiteral x, JsContext ctx) { - return true; - } - - public boolean visit(JsBreak x, JsContext ctx) { - return true; - } - - public boolean visit(JsCase x, JsContext ctx) { - return true; - } - - public boolean visit(JsCatch x, JsContext ctx) { - return true; - } - - public boolean visit(JsConditional x, JsContext ctx) { - return true; - } - - public boolean visit(JsContinue x, JsContext ctx) { - return true; - } - - public boolean visit(JsDebugger x, JsContext ctx) { - return true; - } - - public boolean visit(JsDefault x, JsContext ctx) { - return true; - } - - public boolean visit(JsDoWhile x, JsContext ctx) { - return true; - } - - public boolean visit(JsEmpty x, JsContext ctx) { - return true; - } - - public boolean visit(JsExprStmt x, JsContext ctx) { - return true; - } - - public boolean visit(JsFor x, JsContext ctx) { - return true; - } - - public boolean visit(JsForIn x, JsContext ctx) { - return true; - } - - public boolean visit(JsFunction x, JsContext ctx) { - return true; - } - - public boolean visit(JsIf x, JsContext ctx) { - return true; - } - - public boolean visit(JsInvocation x, JsContext ctx) { - return true; - } - - public boolean visit(JsLabel x, JsContext ctx) { - return true; - } - - public boolean visit(JsNameRef x, JsContext ctx) { - return true; - } - - public boolean visit(JsNew x, JsContext ctx) { - return true; - } - - public boolean visit(JsNullLiteral x, JsContext ctx) { - return true; - } - - public boolean visit(JsNumberLiteral.JsIntLiteral x, JsContext ctx) { - return true; - } - - public boolean visit(JsNumberLiteral.JsDoubleLiteral x, JsContext ctx) { - return true; - } - - public boolean visit(JsObjectLiteral x, JsContext ctx) { - return true; - } - - public boolean visit(JsParameter x, JsContext ctx) { - return true; - } - - public boolean visit(JsPostfixOperation x, JsContext ctx) { - return true; - } - - public boolean visit(JsPrefixOperation x, JsContext ctx) { - return true; - } - - public boolean visit(JsProgram x, JsContext ctx) { - return true; - } - - public boolean visit(JsProgramFragment x, JsContext ctx) { - return true; - } - - public boolean visit(JsPropertyInitializer x, JsContext ctx) { - return true; - } - - public boolean visit(JsRegExp x, JsContext ctx) { - return true; - } - - public boolean visit(JsReturn x, JsContext ctx) { - return true; - } - - public boolean visit(JsStringLiteral x, JsContext ctx) { - return true; - } - - public boolean visit(JsSwitch x, JsContext ctx) { - return true; - } - - public boolean visit(JsLiteral.JsThisRef x, JsContext ctx) { - return true; - } - - public boolean visit(JsThrow x, JsContext ctx) { - return true; - } - - public boolean visit(JsTry x, JsContext ctx) { - return true; - } - - public boolean visit(JsVar x, JsContext ctx) { - return true; - } - - public boolean visit(JsVars x, JsContext ctx) { - return true; - } - - public boolean visit(JsWhile x, JsContext ctx) { - return true; - } - - protected T doAccept(T node) { - doTraverse(node, UNMODIFIABLE_CONTEXT); - return node; - } - - protected void doAcceptList(List collection) { - for (T node : collection) { - doTraverse(node, UNMODIFIABLE_CONTEXT); + public void endVisit(JsContinue x, JsContext ctx) { } - } - protected JsExpression doAcceptLvalue(JsExpression expr) { - doTraverse(expr, LVALUE_CONTEXT); - return expr; - } - - protected void doAcceptWithInsertRemove(List collection) { - for (T node : collection) { - doTraverse(node, UNMODIFIABLE_CONTEXT); + public void endVisit(JsDebugger x, JsContext ctx) { } - } - protected void doTraverse(JsVisitable node, JsContext ctx) { - node.traverse(this, ctx); - } + public void endVisit(JsDefault x, JsContext ctx) { + } + + public void endVisit(JsDoWhile x, JsContext ctx) { + } + + public void endVisit(JsEmpty x, JsContext ctx) { + } + + public void endVisit(JsExprStmt x, JsContext ctx) { + } + + public void endVisit(JsFor x, JsContext ctx) { + } + + public void endVisit(JsForIn x, JsContext ctx) { + } + + public void endVisit(JsFunction x, JsContext ctx) { + } + + public void endVisit(JsIf x, JsContext ctx) { + } + + public void endVisit(JsInvocation x, JsContext ctx) { + } + + public void endVisit(JsLabel x, JsContext ctx) { + } + + public void endVisit(JsNameRef x, JsContext ctx) { + } + + public void endVisit(JsNew x, JsContext ctx) { + } + + public void endVisit(JsNullLiteral x, JsContext ctx) { + } + + public void endVisit(JsNumberLiteral x, JsContext ctx) { + } + + public void endVisit(JsObjectLiteral x, JsContext ctx) { + } + + public void endVisit(JsParameter x, JsContext ctx) { + } + + public void endVisit(JsPostfixOperation x, JsContext ctx) { + } + + public void endVisit(JsPrefixOperation x, JsContext ctx) { + } + + public void endVisit(JsProgram x, JsContext ctx) { + } + + public void endVisit(JsProgramFragment x, JsContext ctx) { + } + + public void endVisit(JsPropertyInitializer x, JsContext ctx) { + } + + public void endVisit(JsRegExp x, JsContext ctx) { + } + + public void endVisit(JsReturn x, JsContext ctx) { + } + + public void endVisit(JsStringLiteral x, JsContext ctx) { + } + + public void endVisit(JsSwitch x, JsContext ctx) { + } + + public void endVisit(JsLiteral.JsThisRef x, JsContext ctx) { + } + + public void endVisit(JsThrow x, JsContext ctx) { + } + + public void endVisit(JsTry x, JsContext ctx) { + } + + public void endVisit(JsVar x, JsContext ctx) { + } + + public void endVisit(JsVars x, JsContext ctx) { + } + + public void endVisit(JsWhile x, JsContext ctx) { + } + + public boolean visit(JsArrayAccess x, JsContext ctx) { + return true; + } + + public boolean visit(JsArrayLiteral x, JsContext ctx) { + return true; + } + + public boolean visit(JsBinaryOperation x, JsContext ctx) { + return true; + } + + public boolean visit(JsBlock x, JsContext ctx) { + return true; + } + + public boolean visit(JsLiteral.JsBooleanLiteral x, JsContext ctx) { + return true; + } + + public boolean visit(JsBreak x, JsContext ctx) { + return true; + } + + public boolean visit(JsCase x, JsContext ctx) { + return true; + } + + public boolean visit(JsCatch x, JsContext ctx) { + return true; + } + + public boolean visit(JsConditional x, JsContext ctx) { + return true; + } + + public boolean visit(JsContinue x, JsContext ctx) { + return true; + } + + public boolean visit(JsDebugger x, JsContext ctx) { + return true; + } + + public boolean visit(JsDefault x, JsContext ctx) { + return true; + } + + public boolean visit(JsDoWhile x, JsContext ctx) { + return true; + } + + public boolean visit(JsEmpty x, JsContext ctx) { + return true; + } + + public boolean visit(JsExprStmt x, JsContext ctx) { + return true; + } + + public boolean visit(JsFor x, JsContext ctx) { + return true; + } + + public boolean visit(JsForIn x, JsContext ctx) { + return true; + } + + public boolean visit(JsFunction x, JsContext ctx) { + return true; + } + + public boolean visit(JsIf x, JsContext ctx) { + return true; + } + + public boolean visit(JsInvocation x, JsContext ctx) { + return true; + } + + public boolean visit(JsLabel x, JsContext ctx) { + return true; + } + + public boolean visit(JsNameRef x, JsContext ctx) { + return true; + } + + public boolean visit(JsNew x, JsContext ctx) { + return true; + } + + public boolean visit(JsNullLiteral x, JsContext ctx) { + return true; + } + + public boolean visit(JsNumberLiteral.JsIntLiteral x, JsContext ctx) { + return true; + } + + public boolean visit(JsNumberLiteral.JsDoubleLiteral x, JsContext ctx) { + return true; + } + + public boolean visit(JsObjectLiteral x, JsContext ctx) { + return true; + } + + public boolean visit(JsParameter x, JsContext ctx) { + return true; + } + + public boolean visit(JsPostfixOperation x, JsContext ctx) { + return true; + } + + public boolean visit(JsPrefixOperation x, JsContext ctx) { + return true; + } + + public boolean visit(JsProgram x, JsContext ctx) { + return true; + } + + public boolean visit(JsProgramFragment x, JsContext ctx) { + return true; + } + + public boolean visit(JsPropertyInitializer x, JsContext ctx) { + return true; + } + + public boolean visit(JsRegExp x, JsContext ctx) { + return true; + } + + public boolean visit(JsReturn x, JsContext ctx) { + return true; + } + + public boolean visit(JsStringLiteral x, JsContext ctx) { + return true; + } + + public boolean visit(JsSwitch x, JsContext ctx) { + return true; + } + + public boolean visit(JsLiteral.JsThisRef x, JsContext ctx) { + return true; + } + + public boolean visit(JsThrow x, JsContext ctx) { + return true; + } + + public boolean visit(JsTry x, JsContext ctx) { + return true; + } + + public boolean visit(JsVar x, JsContext ctx) { + return true; + } + + public boolean visit(JsVars x, JsContext ctx) { + return true; + } + + public boolean visit(JsWhile x, JsContext ctx) { + return true; + } + + public boolean visit(JsDocComment x, JsContext context) { + return true; + } + + protected T doAccept(T node) { + doTraverse(node, UNMODIFIABLE_CONTEXT); + return node; + } + + protected void doAcceptList(List collection) { + for (T node : collection) { + doTraverse(node, UNMODIFIABLE_CONTEXT); + } + } + + protected JsExpression doAcceptLvalue(JsExpression expr) { + doTraverse(expr, LVALUE_CONTEXT); + return expr; + } + + protected void doAcceptWithInsertRemove(List collection) { + for (T node : collection) { + doTraverse(node, UNMODIFIABLE_CONTEXT); + } + } + + protected void doTraverse(JsVisitable node, JsContext ctx) { + node.traverse(this, ctx); + } } From 39b6acc695a477f101c9212e92eb242cfbf5fa1c Mon Sep 17 00:00:00 2001 From: develar Date: Fri, 5 Oct 2012 11:46:47 +0400 Subject: [PATCH 07/27] if open class is not referenced from own final classes, so, define it inplace --- .../backend/js/ast/ChameleonJsExpression.java | 87 +++++++++++++++++++ .../compiler/backend/js/ast/JsDocComment.java | 2 +- 2 files changed, 88 insertions(+), 1 deletion(-) create mode 100644 src/com/google/dart/compiler/backend/js/ast/ChameleonJsExpression.java diff --git a/src/com/google/dart/compiler/backend/js/ast/ChameleonJsExpression.java b/src/com/google/dart/compiler/backend/js/ast/ChameleonJsExpression.java new file mode 100644 index 00000000000..eafa8daad93 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/ChameleonJsExpression.java @@ -0,0 +1,87 @@ +package com.google.dart.compiler.backend.js.ast; + +import com.google.dart.compiler.Source; +import com.google.dart.compiler.common.SourceInfo; + +public class ChameleonJsExpression implements JsExpression { + private JsExpression expression; + + public void resolve(JsExpression expression) { + this.expression = expression; + } + + @Override + public boolean hasSideEffects() { + return expression.hasSideEffects(); + } + + @Override + public boolean isDefinitelyNotNull() { + return expression.isDefinitelyNotNull(); + } + + @Override + public boolean isDefinitelyNull() { + return expression.isDefinitelyNull(); + } + + @Override + public boolean isLeaf() { + return expression.isLeaf(); + } + + @Override + public JsStatement makeStmt() { + return expression.makeStmt(); + } + + @Override + public NodeKind getKind() { + return expression.getKind(); + } + + @Override + public void traverse(JsVisitor visitor, JsContext context) { + expression.traverse(visitor, context); + } + + @Override + public SourceInfo getSourceInfo() { + return expression.getSourceInfo(); + } + + @Override + public void setSourceInfo(SourceInfo info) { + expression.setSourceInfo(info); + } + + @Override + public void setSourceLocation(Source source, int line, int column, int startPosition, int length) { + expression.setSourceLocation(source, line, column, startPosition, length); + } + + @Override + public Source getSource() { + return expression.getSource(); + } + + @Override + public int getLine() { + return 0; + } + + @Override + public int getColumn() { + return expression.getColumn(); + } + + @Override + public int getStart() { + return expression.getStart(); + } + + @Override + public int getLength() { + return expression.getLength(); + } +} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsDocComment.java b/src/com/google/dart/compiler/backend/js/ast/JsDocComment.java index ffc8867edae..909fd225846 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsDocComment.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsDocComment.java @@ -3,7 +3,7 @@ package com.google.dart.compiler.backend.js.ast; import java.util.Collections; import java.util.Map; -public class JsDocComment extends JsExpressionImpl implements JsExpression { +public class JsDocComment extends JsExpressionImpl { private final Map tags; public JsDocComment(Map tags) { From 45dcb542ddbc30bcbf04c158beb30251fbcaef6d Mon Sep 17 00:00:00 2001 From: develar Date: Fri, 5 Oct 2012 11:48:31 +0400 Subject: [PATCH 08/27] if open class is not referenced from own final classes, so, define it inplace --- .../backend/js/ast/ChameleonJsExpression.java | 135 +++++++++--------- 1 file changed, 71 insertions(+), 64 deletions(-) diff --git a/src/com/google/dart/compiler/backend/js/ast/ChameleonJsExpression.java b/src/com/google/dart/compiler/backend/js/ast/ChameleonJsExpression.java index eafa8daad93..9e3238ae2df 100644 --- a/src/com/google/dart/compiler/backend/js/ast/ChameleonJsExpression.java +++ b/src/com/google/dart/compiler/backend/js/ast/ChameleonJsExpression.java @@ -4,84 +4,91 @@ import com.google.dart.compiler.Source; import com.google.dart.compiler.common.SourceInfo; public class ChameleonJsExpression implements JsExpression { - private JsExpression expression; + private JsExpression expression; - public void resolve(JsExpression expression) { - this.expression = expression; - } + public ChameleonJsExpression(JsExpression initialExpression) { + expression = initialExpression; + } - @Override - public boolean hasSideEffects() { - return expression.hasSideEffects(); - } + public ChameleonJsExpression() { + } - @Override - public boolean isDefinitelyNotNull() { - return expression.isDefinitelyNotNull(); - } + public void resolve(JsExpression expression) { + this.expression = expression; + } - @Override - public boolean isDefinitelyNull() { - return expression.isDefinitelyNull(); - } + @Override + public boolean hasSideEffects() { + return expression.hasSideEffects(); + } - @Override - public boolean isLeaf() { - return expression.isLeaf(); - } + @Override + public boolean isDefinitelyNotNull() { + return expression.isDefinitelyNotNull(); + } - @Override - public JsStatement makeStmt() { - return expression.makeStmt(); - } + @Override + public boolean isDefinitelyNull() { + return expression.isDefinitelyNull(); + } - @Override - public NodeKind getKind() { - return expression.getKind(); - } + @Override + public boolean isLeaf() { + return expression.isLeaf(); + } - @Override - public void traverse(JsVisitor visitor, JsContext context) { - expression.traverse(visitor, context); - } + @Override + public JsStatement makeStmt() { + return expression.makeStmt(); + } - @Override - public SourceInfo getSourceInfo() { - return expression.getSourceInfo(); - } + @Override + public NodeKind getKind() { + return expression.getKind(); + } - @Override - public void setSourceInfo(SourceInfo info) { - expression.setSourceInfo(info); - } + @Override + public void traverse(JsVisitor visitor, JsContext context) { + expression.traverse(visitor, context); + } - @Override - public void setSourceLocation(Source source, int line, int column, int startPosition, int length) { - expression.setSourceLocation(source, line, column, startPosition, length); - } + @Override + public SourceInfo getSourceInfo() { + return expression.getSourceInfo(); + } - @Override - public Source getSource() { - return expression.getSource(); - } + @Override + public void setSourceInfo(SourceInfo info) { + expression.setSourceInfo(info); + } - @Override - public int getLine() { - return 0; - } + @Override + public void setSourceLocation(Source source, int line, int column, int startPosition, int length) { + expression.setSourceLocation(source, line, column, startPosition, length); + } - @Override - public int getColumn() { - return expression.getColumn(); - } + @Override + public Source getSource() { + return expression.getSource(); + } - @Override - public int getStart() { - return expression.getStart(); - } + @Override + public int getLine() { + return 0; + } - @Override - public int getLength() { - return expression.getLength(); - } + @Override + public int getColumn() { + return expression.getColumn(); + } + + @Override + public int getStart() { + return expression.getStart(); + } + + @Override + public int getLength() { + return expression.getLength(); + } } From de32d045ec4ad7a0f5dda29fc09c54812858b1cb Mon Sep 17 00:00:00 2001 From: develar Date: Sun, 14 Oct 2012 12:38:13 +0400 Subject: [PATCH 09/27] don't print extra space before finally --- .../compiler/backend/js/JsToStringGenerationVisitor.java | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java b/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java index 62905cb51db..109664badb2 100644 --- a/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java +++ b/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java @@ -858,8 +858,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { JsBlock finallyBlock = x.getFinallyBlock(); if (finallyBlock != null) { - spaceOpt(); - _finally(); + p.print(CHARS_FINALLY); spaceOpt(); accept(finallyBlock); } @@ -1125,10 +1124,6 @@ public class JsToStringGenerationVisitor extends JsVisitor { p.print(CHARS_FALSE); } - private void _finally() { - p.print(CHARS_FINALLY); - } - private void _for() { p.print(CHARS_FOR); } From be5dc25578300fdfbc5f8433b217757a39ff0518 Mon Sep 17 00:00:00 2001 From: develar Date: Sun, 14 Oct 2012 14:02:14 +0400 Subject: [PATCH 10/27] KT-927 prepare --- .../backend/js/JsFirstExpressionVisitor.java | 20 +------ .../backend/js/JsPrecedenceVisitor.java | 2 +- .../backend/js/JsRequiresSemiVisitor.java | 23 +------- .../js/JsToStringGenerationVisitor.java | 4 +- .../compiler/backend/js/ast/AbstractNode.java | 14 +++++ .../backend/js/ast/ChameleonJsExpression.java | 31 ---------- .../compiler/backend/js/ast/JsExpression.java | 3 +- .../backend/js/ast/JsExpressionImpl.java | 2 +- ...prStmt.java => JsExpressionStatement.java} | 26 ++++++--- .../dart/compiler/backend/js/ast/JsFor.java | 8 --- .../compiler/backend/js/ast/JsFunction.java | 9 +-- .../compiler/backend/js/ast/JsNodeImpl.java | 26 +++------ .../dart/compiler/backend/js/ast/JsVars.java | 7 --- .../compiler/backend/js/ast/JsVisitable.java | 3 - .../compiler/backend/js/ast/JsVisitor.java | 4 +- .../dart/compiler/common/AbstractNode.java | 57 ------------------- .../dart/compiler/common/HasSourceInfo.java | 39 +------------ 17 files changed, 55 insertions(+), 223 deletions(-) create mode 100644 src/com/google/dart/compiler/backend/js/ast/AbstractNode.java rename src/com/google/dart/compiler/backend/js/ast/{JsExprStmt.java => JsExpressionStatement.java} (50%) delete mode 100644 src/com/google/dart/compiler/common/AbstractNode.java diff --git a/src/com/google/dart/compiler/backend/js/JsFirstExpressionVisitor.java b/src/com/google/dart/compiler/backend/js/JsFirstExpressionVisitor.java index 0ae823c60e5..9aef75583e0 100644 --- a/src/com/google/dart/compiler/backend/js/JsFirstExpressionVisitor.java +++ b/src/com/google/dart/compiler/backend/js/JsFirstExpressionVisitor.java @@ -4,22 +4,8 @@ package com.google.dart.compiler.backend.js; -import com.google.dart.compiler.backend.js.ast.JsArrayAccess; -import com.google.dart.compiler.backend.js.ast.JsArrayLiteral; -import com.google.dart.compiler.backend.js.ast.JsBinaryOperation; -import com.google.dart.compiler.backend.js.ast.JsConditional; -import com.google.dart.compiler.backend.js.ast.JsContext; -import com.google.dart.compiler.backend.js.ast.JsExprStmt; -import com.google.dart.compiler.backend.js.ast.JsExpression; -import com.google.dart.compiler.backend.js.ast.JsFunction; -import com.google.dart.compiler.backend.js.ast.JsInvocation; -import com.google.dart.compiler.backend.js.ast.JsNameRef; -import com.google.dart.compiler.backend.js.ast.JsNew; -import com.google.dart.compiler.backend.js.ast.JsObjectLiteral; -import com.google.dart.compiler.backend.js.ast.JsPostfixOperation; -import com.google.dart.compiler.backend.js.ast.JsPrefixOperation; -import com.google.dart.compiler.backend.js.ast.JsRegExp; -import com.google.dart.compiler.backend.js.ast.JsVisitor; +import com.google.dart.compiler.backend.js.ast.*; +import com.google.dart.compiler.backend.js.ast.JsExpressionStatement; /** * Determines if an expression statement needs to be surrounded by parentheses. @@ -45,7 +31,7 @@ import com.google.dart.compiler.backend.js.ast.JsVisitor; * */ public class JsFirstExpressionVisitor extends JsVisitor { - public static boolean exec(JsExprStmt statement) { + public static boolean exec(JsExpressionStatement statement) { JsExpression expression = statement.getExpression(); // Pure function declarations do not need parentheses if (expression instanceof JsFunction) { diff --git a/src/com/google/dart/compiler/backend/js/JsPrecedenceVisitor.java b/src/com/google/dart/compiler/backend/js/JsPrecedenceVisitor.java index a9632298819..bd611321d25 100644 --- a/src/com/google/dart/compiler/backend/js/JsPrecedenceVisitor.java +++ b/src/com/google/dart/compiler/backend/js/JsPrecedenceVisitor.java @@ -121,7 +121,7 @@ class JsPrecedenceVisitor extends JsVisitor { } @Override - public boolean visit(JsExprStmt x, JsContext ctx) { + public boolean visit(JsExpressionStatement x, JsContext ctx) { throw new RuntimeException("Only expressions have precedence."); } diff --git a/src/com/google/dart/compiler/backend/js/JsRequiresSemiVisitor.java b/src/com/google/dart/compiler/backend/js/JsRequiresSemiVisitor.java index bd13ea3af72..229dedc5417 100644 --- a/src/com/google/dart/compiler/backend/js/JsRequiresSemiVisitor.java +++ b/src/com/google/dart/compiler/backend/js/JsRequiresSemiVisitor.java @@ -4,25 +4,8 @@ package com.google.dart.compiler.backend.js; -import com.google.dart.compiler.backend.js.ast.JsBlock; -import com.google.dart.compiler.backend.js.ast.JsBreak; -import com.google.dart.compiler.backend.js.ast.JsContext; -import com.google.dart.compiler.backend.js.ast.JsDebugger; -import com.google.dart.compiler.backend.js.ast.JsDoWhile; -import com.google.dart.compiler.backend.js.ast.JsEmpty; -import com.google.dart.compiler.backend.js.ast.JsExprStmt; -import com.google.dart.compiler.backend.js.ast.JsFor; -import com.google.dart.compiler.backend.js.ast.JsForIn; -import com.google.dart.compiler.backend.js.ast.JsIf; -import com.google.dart.compiler.backend.js.ast.JsLabel; -import com.google.dart.compiler.backend.js.ast.JsReturn; -import com.google.dart.compiler.backend.js.ast.JsStatement; -import com.google.dart.compiler.backend.js.ast.JsSwitch; -import com.google.dart.compiler.backend.js.ast.JsThrow; -import com.google.dart.compiler.backend.js.ast.JsTry; -import com.google.dart.compiler.backend.js.ast.JsVars; -import com.google.dart.compiler.backend.js.ast.JsVisitor; -import com.google.dart.compiler.backend.js.ast.JsWhile; +import com.google.dart.compiler.backend.js.ast.*; +import com.google.dart.compiler.backend.js.ast.JsExpressionStatement; /** * Determines if a statement at the end of a block requires a semicolon. @@ -78,7 +61,7 @@ public class JsRequiresSemiVisitor extends JsVisitor { } @Override - public boolean visit(JsExprStmt x, JsContext ctx) { + public boolean visit(JsExpressionStatement x, JsContext ctx) { return false; } diff --git a/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java b/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java index 109664badb2..7e78a15e46a 100644 --- a/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java +++ b/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java @@ -459,7 +459,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public boolean visit(JsExprStmt x, JsContext ctx) { + public boolean visit(JsExpressionStatement x, JsContext ctx) { boolean surroundWithParentheses = JsFirstExpressionVisitor.exec(x); if (surroundWithParentheses) { leftParen(); @@ -1022,7 +1022,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { * a newline instead of a semi. */ boolean functionStmt = - statement instanceof JsExprStmt && ((JsExprStmt) statement).getExpression() instanceof JsFunction; + statement instanceof JsExpressionStatement && ((JsExpressionStatement) statement).getExpression() instanceof JsFunction; /* * Special treatment of the last statement in a block: only a few * statements at the end of a block require semicolons. diff --git a/src/com/google/dart/compiler/backend/js/ast/AbstractNode.java b/src/com/google/dart/compiler/backend/js/ast/AbstractNode.java new file mode 100644 index 00000000000..582b7ac1adb --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/AbstractNode.java @@ -0,0 +1,14 @@ +package com.google.dart.compiler.backend.js.ast; + +import com.google.dart.compiler.backend.js.JsToStringGenerationVisitor; +import com.google.dart.compiler.common.HasSourceInfo; +import com.google.dart.compiler.util.TextOutputImpl; + +abstract class AbstractNode implements JsNode, HasSourceInfo { + @Override + public String toString() { + TextOutputImpl out = new TextOutputImpl(); + new JsToStringGenerationVisitor(out).accept(this); + return out.toString(); + } +} \ No newline at end of file diff --git a/src/com/google/dart/compiler/backend/js/ast/ChameleonJsExpression.java b/src/com/google/dart/compiler/backend/js/ast/ChameleonJsExpression.java index 9e3238ae2df..00d1eaaf511 100644 --- a/src/com/google/dart/compiler/backend/js/ast/ChameleonJsExpression.java +++ b/src/com/google/dart/compiler/backend/js/ast/ChameleonJsExpression.java @@ -1,6 +1,5 @@ package com.google.dart.compiler.backend.js.ast; -import com.google.dart.compiler.Source; import com.google.dart.compiler.common.SourceInfo; public class ChameleonJsExpression implements JsExpression { @@ -61,34 +60,4 @@ public class ChameleonJsExpression implements JsExpression { public void setSourceInfo(SourceInfo info) { expression.setSourceInfo(info); } - - @Override - public void setSourceLocation(Source source, int line, int column, int startPosition, int length) { - expression.setSourceLocation(source, line, column, startPosition, length); - } - - @Override - public Source getSource() { - return expression.getSource(); - } - - @Override - public int getLine() { - return 0; - } - - @Override - public int getColumn() { - return expression.getColumn(); - } - - @Override - public int getStart() { - return expression.getStart(); - } - - @Override - public int getLength() { - return expression.getLength(); - } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsExpression.java b/src/com/google/dart/compiler/backend/js/ast/JsExpression.java index 5fb9a7e41f1..def914d6a70 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsExpression.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsExpression.java @@ -1,9 +1,8 @@ package com.google.dart.compiler.backend.js.ast; import com.google.dart.compiler.common.HasSourceInfo; -import com.google.dart.compiler.common.SourceInfo; -public interface JsExpression extends JsNode, SourceInfo, HasSourceInfo, JsVisitable { +public interface JsExpression extends JsNode, HasSourceInfo, JsVisitable { /** * Determines whether the expression can cause side effects. */ diff --git a/src/com/google/dart/compiler/backend/js/ast/JsExpressionImpl.java b/src/com/google/dart/compiler/backend/js/ast/JsExpressionImpl.java index f42ae1c26da..2e46db4bef9 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsExpressionImpl.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsExpressionImpl.java @@ -22,6 +22,6 @@ abstract class JsExpressionImpl extends JsNodeImpl implements JsExpression { @Override public JsStatement makeStmt() { - return new JsExprStmt(this); + return new JsExpressionStatement(this); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsExprStmt.java b/src/com/google/dart/compiler/backend/js/ast/JsExpressionStatement.java similarity index 50% rename from src/com/google/dart/compiler/backend/js/ast/JsExprStmt.java rename to src/com/google/dart/compiler/backend/js/ast/JsExpressionStatement.java index 6ab649f2cb4..1229f30c4db 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsExprStmt.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsExpressionStatement.java @@ -4,23 +4,23 @@ package com.google.dart.compiler.backend.js.ast; -public final class JsExprStmt extends JsNodeImpl implements JsStatement { - private JsExpression expr; +import com.google.dart.compiler.common.SourceInfo; - public JsExprStmt(JsExpression expr) { - super(); - this.expr = expr; - this.setSourceInfo(expr); +public final class JsExpressionStatement extends AbstractNode implements JsStatement { + private JsExpression expression; + + public JsExpressionStatement(JsExpression expression) { + this.expression = expression; } public JsExpression getExpression() { - return expr; + return expression; } @Override public void traverse(JsVisitor v, JsContext context) { if (v.visit(this, context)) { - expr = v.accept(expr); + expression = v.accept(expression); } v.endVisit(this, context); } @@ -29,4 +29,14 @@ public final class JsExprStmt extends JsNodeImpl implements JsStatement { public NodeKind getKind() { return NodeKind.EXPRESSION_STMT; } + + @Override + public SourceInfo getSourceInfo() { + return expression.getSourceInfo(); + } + + @Override + public void setSourceInfo(SourceInfo info) { + expression.setSourceInfo(info); + } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsFor.java b/src/com/google/dart/compiler/backend/js/ast/JsFor.java index 65f24607696..4efb65d93c8 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsFor.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsFor.java @@ -4,8 +4,6 @@ package com.google.dart.compiler.backend.js.ast; -import com.google.dart.compiler.common.SourceInfo; - /** * A for statement. If specified at all, the initializer part is * either a declaration of one or more variables, in which case @@ -86,12 +84,6 @@ public class JsFor extends JsNodeImpl implements JsStatement { v.endVisit(this, context); } - @Override - public JsFor setSourceRef(SourceInfo info) { - super.setSourceRef(info); - return this; - } - @Override public NodeKind getKind() { return NodeKind.FOR; diff --git a/src/com/google/dart/compiler/backend/js/ast/JsFunction.java b/src/com/google/dart/compiler/backend/js/ast/JsFunction.java index a4e9914100d..1d4bd4da308 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsFunction.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsFunction.java @@ -4,7 +4,6 @@ package com.google.dart.compiler.backend.js.ast; -import com.google.dart.compiler.common.SourceInfo; import com.google.dart.compiler.common.Symbol; import com.intellij.util.SmartList; import org.jetbrains.annotations.Nullable; @@ -28,7 +27,7 @@ public final class JsFunction extends JsLiteral implements HasName { private JsFunction(JsScope parentScope, @Nullable JsName name) { this.name = name; - this.scope = new JsScope(parentScope, name == null ? null : name.getIdent()); + scope = new JsScope(parentScope, name == null ? null : name.getIdent()); } public JsBlock getBody() { @@ -99,12 +98,6 @@ public final class JsFunction extends JsLiteral implements HasName { v.endVisit(this, context); } - @Override - public JsFunction setSourceRef(SourceInfo info) { - super.setSourceRef(info); - return this; - } - @Override public NodeKind getKind() { return NodeKind.FUNCTION; diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNodeImpl.java b/src/com/google/dart/compiler/backend/js/ast/JsNodeImpl.java index 583f0e7de08..6128475681e 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsNodeImpl.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsNodeImpl.java @@ -1,30 +1,20 @@ package com.google.dart.compiler.backend.js.ast; -import com.google.dart.compiler.backend.js.JsToStringGenerationVisitor; -import com.google.dart.compiler.common.AbstractNode; import com.google.dart.compiler.common.SourceInfo; -import com.google.dart.compiler.util.TextOutputImpl; -abstract class JsNodeImpl extends AbstractNode implements JsNode { +abstract class JsNodeImpl extends AbstractNode { + private SourceInfo sourceInfo; + protected JsNodeImpl() { } @Override - public String toString() { - TextOutputImpl out = new TextOutputImpl(); - new JsToStringGenerationVisitor(out).accept(this); - return out.toString(); + public SourceInfo getSourceInfo() { + return sourceInfo; } @Override - public SourceInfo getSourceInfo() { - return this; + public void setSourceInfo(SourceInfo info) { + sourceInfo = info; } - - public JsNode setSourceRef(SourceInfo info) { - if (info != null) { - setSourceInfo(info); - } - return this; - } -} +} \ No newline at end of file diff --git a/src/com/google/dart/compiler/backend/js/ast/JsVars.java b/src/com/google/dart/compiler/backend/js/ast/JsVars.java index 18617861955..638a50ef496 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsVars.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsVars.java @@ -4,7 +4,6 @@ package com.google.dart.compiler.backend.js.ast; -import com.google.dart.compiler.common.SourceInfo; import com.google.dart.compiler.common.Symbol; import com.intellij.util.SmartList; import org.jetbrains.annotations.Nullable; @@ -91,12 +90,6 @@ public class JsVars extends JsNodeImpl implements JsStatement, Iterable= 0 || startPosition == -1 && length == 0); - //sourceInfo.sourceStart = startPosition; - //sourceInfo.sourceLength = length; - } -} diff --git a/src/com/google/dart/compiler/common/HasSourceInfo.java b/src/com/google/dart/compiler/common/HasSourceInfo.java index c7780f6acee..524ed667467 100644 --- a/src/com/google/dart/compiler/common/HasSourceInfo.java +++ b/src/com/google/dart/compiler/common/HasSourceInfo.java @@ -4,52 +4,15 @@ package com.google.dart.compiler.common; -import com.google.dart.compiler.Source; - -/** - * Abstract view of a class that has source info. - */ public interface HasSourceInfo { - /** * Return the source info associated with this object. */ SourceInfo getSourceInfo(); /** - * Set the source info associated with this object. May only be called once. + * Set the source info associated with this object. * @param info */ void setSourceInfo(SourceInfo info); - - /** - * Sets the source range of the original source file where the source fragment - * corresponding to this node was found. - * - *

- * Each node in the subtree (other than the contrived nodes) carries source - * range(s) information relating back to positions in the given source (the - * given source itself is not remembered with the AST). The source range - * usually begins at the first character of the first token corresponding to - * the node; leading whitespace and comments are not included. The - * source range usually extends through the last character of the last token - * corresponding to the node; trailing whitespace and comments are not - * included. There are a handful of exceptions (including the various body - * declarations). Source ranges nest properly: the source range for a child is - * always within the source range of its parent, and the source ranges of - * sibling nodes never overlap. - * - * @param source the associated source - * @param line the 1-based line index, or -1, if no source - * location is available - * @param column the 1-based column index, or -1, if no source - * location is available - * @param startPosition a 0-based character index, or -1, if no - * source location is available - * @param length a (possibly 0) length, or -1, if no source - * location is available - * @see SourceInfo#getStart() - * @see SourceInfo#getLength() - */ - void setSourceLocation(Source source, int line, int column, int startPosition, int length); } From 0f9ae4f70c015b073ee8a84f6fb7ac4aede9512d Mon Sep 17 00:00:00 2001 From: develar Date: Sun, 14 Oct 2012 17:49:33 +0400 Subject: [PATCH 11/27] KT-927 prepare --- .../backend/js/JsSourceGenerationVisitor.java | 11 +- .../js/JsToStringGenerationVisitor.java | 63 +++++-- .../backend/js/ast/ChameleonJsExpression.java | 4 +- .../compiler/backend/js/ast/JsContext.java | 1 - .../backend/js/ast/JsExpressionStatement.java | 4 +- .../backend/js/ast/JsGlobalBlock.java | 2 +- .../compiler/backend/js/ast/JsNodeImpl.java | 6 +- .../compiler/backend/js/ast/JsVisitable.java | 4 +- .../compiler/backend/js/ast/JsVisitor.java | 6 +- .../dart/compiler/common/HasSourceInfo.java | 4 +- .../google/dart/compiler/util/TextOutput.java | 32 ++-- .../dart/compiler/util/TextOutputImpl.java | 11 +- .../js/compiler/SourceMapBuilder.java | 166 ++++++++++++++++++ 13 files changed, 255 insertions(+), 59 deletions(-) create mode 100644 src/org/jetbrains/js/compiler/SourceMapBuilder.java diff --git a/src/com/google/dart/compiler/backend/js/JsSourceGenerationVisitor.java b/src/com/google/dart/compiler/backend/js/JsSourceGenerationVisitor.java index de88bdb7b74..91a3e98f337 100644 --- a/src/com/google/dart/compiler/backend/js/JsSourceGenerationVisitor.java +++ b/src/com/google/dart/compiler/backend/js/JsSourceGenerationVisitor.java @@ -4,18 +4,17 @@ package com.google.dart.compiler.backend.js; -import com.google.dart.compiler.backend.js.ast.JsBlock; -import com.google.dart.compiler.backend.js.ast.JsContext; -import com.google.dart.compiler.backend.js.ast.JsProgram; -import com.google.dart.compiler.backend.js.ast.JsProgramFragment; +import com.google.dart.compiler.backend.js.ast.*; import com.google.dart.compiler.util.TextOutput; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.js.compiler.SourceMapBuilder; /** * Generates JavaScript source from an AST. */ public class JsSourceGenerationVisitor extends JsToStringGenerationVisitor { - public JsSourceGenerationVisitor(TextOutput out) { - super(out); + public JsSourceGenerationVisitor(TextOutput out, @Nullable SourceMapBuilder sourceMapBuilder) { + super(out, sourceMapBuilder); } @Override diff --git a/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java b/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java index 7e78a15e46a..c254e8eb311 100644 --- a/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java +++ b/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java @@ -7,9 +7,14 @@ package com.google.dart.compiler.backend.js; import com.google.dart.compiler.backend.js.ast.*; import com.google.dart.compiler.backend.js.ast.JsVars.JsVar; import com.google.dart.compiler.util.TextOutput; -import gnu.trove.TIntArrayList; +import gnu.trove.THashSet; +import org.jetbrains.annotations.Nullable; +import org.jetbrains.js.compiler.SourceMapBuilder; -import java.util.*; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Set; import static com.google.dart.compiler.backend.js.ast.JsNumberLiteral.JsDoubleLiteral; import static com.google.dart.compiler.backend.js.ast.JsNumberLiteral.JsIntLiteral; @@ -194,13 +199,19 @@ public class JsToStringGenerationVisitor extends JsVisitor { * because the statements designated by statementEnds and statementStarts are * those that appear directly within these global blocks. */ - private Set globalBlocks = new HashSet(); + private Set globalBlocks = new THashSet(); private final TextOutput p; - private TIntArrayList statementEnds = new TIntArrayList(); - private TIntArrayList statementStarts = new TIntArrayList(); + + @Nullable + private final SourceMapBuilder sourceMapBuilder; public JsToStringGenerationVisitor(TextOutput out) { + this(out, null); + } + + public JsToStringGenerationVisitor(TextOutput out, @Nullable SourceMapBuilder sourceMapBuilder) { p = out; + this.sourceMapBuilder = sourceMapBuilder; } @Override @@ -963,12 +974,17 @@ public class JsToStringGenerationVisitor extends JsVisitor { return false; } - protected void newline() { + protected final void newline() { + if (sourceMapBuilder != null) { + sourceMapBuilder.newLine(); + } p.newline(); } - protected void newlineOpt() { - p.newlineOpt(); + protected final void newlineOpt() { + if (!p.isCompact()) { + newline(); + } } protected void printJsBlock(JsBlock x, boolean truncate, boolean finalNewline) { @@ -998,7 +1014,6 @@ public class JsToStringGenerationVisitor extends JsVisitor { } needSemi = true; - boolean shouldRecordPositions = isGlobal && !(statement instanceof JsBlock); boolean stmtIsGlobalBlock = false; if (isGlobal) { if (statement instanceof JsBlock) { @@ -1007,9 +1022,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { globalBlocks.add((JsBlock) statement); } } - if (shouldRecordPositions) { - statementStarts.add(p.getPosition()); - } + accept(statement); if (stmtIsGlobalBlock) { //noinspection SuspiciousMethodCalls @@ -1046,10 +1059,6 @@ public class JsToStringGenerationVisitor extends JsVisitor { newlineOpt(); } } - if (shouldRecordPositions) { - assert (statementStarts.size() == statementEnds.size() + 1); - statementEnds.add(p.getPosition()); - } ++count; } @@ -1352,4 +1361,26 @@ public class JsToStringGenerationVisitor extends JsVisitor { private void printStringLiteral(String value) { p.print(javaScriptString(value)); } + + @Override + protected void doTraverse(JsVisitable node, JsContext context) { + super.doTraverse(node, context); + + if (sourceMapBuilder == null) { + return; + } + + Object sourceInfo = node.getSourceInfo(); + if (sourceInfo != null) { + sourceMapBuilder.processSourceInfo(sourceInfo); + } + } + + @Override + public void endVisit(JsProgram x, JsContext context) { + super.endVisit(x, context); + if (sourceMapBuilder != null) { + sourceMapBuilder.addLink(); + } + } } diff --git a/src/com/google/dart/compiler/backend/js/ast/ChameleonJsExpression.java b/src/com/google/dart/compiler/backend/js/ast/ChameleonJsExpression.java index 00d1eaaf511..65047589936 100644 --- a/src/com/google/dart/compiler/backend/js/ast/ChameleonJsExpression.java +++ b/src/com/google/dart/compiler/backend/js/ast/ChameleonJsExpression.java @@ -52,12 +52,12 @@ public class ChameleonJsExpression implements JsExpression { } @Override - public SourceInfo getSourceInfo() { + public Object getSourceInfo() { return expression.getSourceInfo(); } @Override - public void setSourceInfo(SourceInfo info) { + public void setSourceInfo(Object info) { expression.setSourceInfo(info); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsContext.java b/src/com/google/dart/compiler/backend/js/ast/JsContext.java index ddb170052d3..15371c8ec41 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsContext.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsContext.java @@ -10,7 +10,6 @@ package com.google.dart.compiler.backend.js.ast; * node. */ public interface JsContext { - boolean canInsert(); boolean canRemove(); diff --git a/src/com/google/dart/compiler/backend/js/ast/JsExpressionStatement.java b/src/com/google/dart/compiler/backend/js/ast/JsExpressionStatement.java index 1229f30c4db..1e24b31894d 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsExpressionStatement.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsExpressionStatement.java @@ -31,12 +31,12 @@ public final class JsExpressionStatement extends AbstractNode implements JsState } @Override - public SourceInfo getSourceInfo() { + public Object getSourceInfo() { return expression.getSourceInfo(); } @Override - public void setSourceInfo(SourceInfo info) { + public void setSourceInfo(Object info) { expression.setSourceInfo(info); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsGlobalBlock.java b/src/com/google/dart/compiler/backend/js/ast/JsGlobalBlock.java index 9a14004e14c..11bbd10d86e 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsGlobalBlock.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsGlobalBlock.java @@ -5,7 +5,7 @@ package com.google.dart.compiler.backend.js.ast; /** - * Represnts a JavaScript block in the global scope. + * Represents a JavaScript block in the global scope. */ public class JsGlobalBlock extends JsBlock { diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNodeImpl.java b/src/com/google/dart/compiler/backend/js/ast/JsNodeImpl.java index 6128475681e..164300c68e4 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsNodeImpl.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsNodeImpl.java @@ -3,18 +3,18 @@ package com.google.dart.compiler.backend.js.ast; import com.google.dart.compiler.common.SourceInfo; abstract class JsNodeImpl extends AbstractNode { - private SourceInfo sourceInfo; + private Object sourceInfo; protected JsNodeImpl() { } @Override - public SourceInfo getSourceInfo() { + public Object getSourceInfo() { return sourceInfo; } @Override - public void setSourceInfo(SourceInfo info) { + public void setSourceInfo(Object info) { sourceInfo = info; } } \ No newline at end of file diff --git a/src/com/google/dart/compiler/backend/js/ast/JsVisitable.java b/src/com/google/dart/compiler/backend/js/ast/JsVisitable.java index be539d3e775..8471d466e0e 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsVisitable.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsVisitable.java @@ -4,7 +4,9 @@ package com.google.dart.compiler.backend.js.ast; -public interface JsVisitable { +import com.google.dart.compiler.common.HasSourceInfo; + +public interface JsVisitable extends HasSourceInfo { /** * Causes this object to have the visitor visit itself and its children. * diff --git a/src/com/google/dart/compiler/backend/js/ast/JsVisitor.java b/src/com/google/dart/compiler/backend/js/ast/JsVisitor.java index 11685df7925..3f0e5f3e7d5 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsVisitor.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsVisitor.java @@ -11,7 +11,7 @@ import java.util.List; /** * Implemented by nodes that will visit child nodes. */ -public class JsVisitor { +abstract public class JsVisitor { protected static final JsContext LVALUE_CONTEXT = new JsContext() { @Override public boolean canInsert() { @@ -427,7 +427,7 @@ public class JsVisitor { } } - protected void doTraverse(JsVisitable node, JsContext ctx) { - node.traverse(this, ctx); + protected void doTraverse(JsVisitable node, JsContext context) { + node.traverse(this, context); } } diff --git a/src/com/google/dart/compiler/common/HasSourceInfo.java b/src/com/google/dart/compiler/common/HasSourceInfo.java index 524ed667467..251f72b71f7 100644 --- a/src/com/google/dart/compiler/common/HasSourceInfo.java +++ b/src/com/google/dart/compiler/common/HasSourceInfo.java @@ -8,11 +8,11 @@ public interface HasSourceInfo { /** * Return the source info associated with this object. */ - SourceInfo getSourceInfo(); + Object getSourceInfo(); /** * Set the source info associated with this object. * @param info */ - void setSourceInfo(SourceInfo info); + void setSourceInfo(Object info); } diff --git a/src/com/google/dart/compiler/util/TextOutput.java b/src/com/google/dart/compiler/util/TextOutput.java index 3b42663d7dc..16134ad5d60 100644 --- a/src/com/google/dart/compiler/util/TextOutput.java +++ b/src/com/google/dart/compiler/util/TextOutput.java @@ -8,31 +8,33 @@ package com.google.dart.compiler.util; * Interface used for printing text output. */ public interface TextOutput { - int getPosition(); + int getPosition(); - int getLine(); + int getLine(); - int getColumn(); + int getColumn(); - void indentIn(); + void indentIn(); - void indentOut(); + void indentOut(); - void newline(); + void newline(); - void newlineOpt(); + void print(char c); - void print(char c); - void print(int v); - void print(double v); + void print(int v); - void print(char[] s); + void print(double v); - void print(CharSequence s); + void print(char[] s); - void printOpt(char c); + void print(CharSequence s); - void printOpt(char[] s); + void printOpt(char c); - void printOpt(String s); + void printOpt(char[] s); + + void printOpt(String s); + + boolean isCompact(); } diff --git a/src/com/google/dart/compiler/util/TextOutputImpl.java b/src/com/google/dart/compiler/util/TextOutputImpl.java index 174fc429ed3..6cb76be1da3 100644 --- a/src/com/google/dart/compiler/util/TextOutputImpl.java +++ b/src/com/google/dart/compiler/util/TextOutputImpl.java @@ -24,6 +24,10 @@ public class TextOutputImpl implements TextOutput { this(false); } + public boolean isCompact() { + return compact; + } + public TextOutputImpl(boolean compact) { this.compact = compact; out = new StringBuilder(); @@ -77,13 +81,6 @@ public class TextOutputImpl implements TextOutput { justNewlined = true; } - @Override - public void newlineOpt() { - if (!compact) { - newline(); - } - } - @Override public void print(double value) { maybeIndent(); diff --git a/src/org/jetbrains/js/compiler/SourceMapBuilder.java b/src/org/jetbrains/js/compiler/SourceMapBuilder.java new file mode 100644 index 00000000000..1723a407ecc --- /dev/null +++ b/src/org/jetbrains/js/compiler/SourceMapBuilder.java @@ -0,0 +1,166 @@ +package org.jetbrains.js.compiler; + +import com.google.dart.compiler.common.SourceInfo; +import com.google.dart.compiler.util.TextOutput; +import com.intellij.util.PairConsumer; +import gnu.trove.TObjectIntHashMap; + +import java.util.ArrayList; +import java.util.List; + +public class SourceMapBuilder { + private final StringBuilder out = new StringBuilder(); + private final String generatedFilename; + private final TextOutput textOutput; + private final PairConsumer sourceInfoConsumer; + + private String lastSource; + private int lastSourceIndex; + + private final TObjectIntHashMap sources = new TObjectIntHashMap() { + @Override + public int get(String key) { + int index = index(key); + return index < 0 ? -1 : _values[index]; + } + }; + + private final List orderedSources = new ArrayList(); + + private int previousGeneratedColumn; + private int previousSourceIndex; + private int previousSourceLine; + private int previousSourceColumn; + + public SourceMapBuilder(String generatedFilename, TextOutput textOutput, PairConsumer sourceInfoConsumer) { + this.generatedFilename = generatedFilename; + this.textOutput = textOutput; + this.sourceInfoConsumer = sourceInfoConsumer; + } + + public String getOutFilename() { + return generatedFilename + ".map"; + } + + public String build() { + StringBuilder sb = new StringBuilder(); + sb.append("{\"version\":3,\"file\":\"").append(generatedFilename).append('"').append(','); + appendSources(sb); + sb.append(",\"names\":["); + sb.append("],\"mappings\":\""); + sb.append(out); + sb.append("\"}"); + return sb.toString(); + } + + private void appendSources(StringBuilder sb) { + boolean isNotFirst = false; + sb.append('"').append("sources").append("\":["); + for (String source : orderedSources) { + if (isNotFirst) { + sb.append(','); + } + else { + isNotFirst = true; + } + sb.append('"').append(source).append('"'); + } + sb.append(']'); + } + + public void newLine() { + out.append(';'); + previousGeneratedColumn = 0; + } + + public void processSourceInfo(Object sourceInfo) { + if (sourceInfo instanceof SourceInfo) { + throw new UnsupportedOperationException("SourceInfo is not yet supported"); + } + sourceInfoConsumer.consume(this, sourceInfo); + } + + private int getSourceIndex(String source) { + if (source.equals(lastSource)) { + return lastSourceIndex; + } + + int sourceIndex = sources.get(source); + if (sourceIndex == -1) { + sourceIndex = sources.put(source, orderedSources.size()); + orderedSources.add(source); + } + + lastSource = source; + lastSourceIndex = sourceIndex; + + return sourceIndex; + } + + public void addMapping(String source, int sourceLine, int sourceColumn) { + if (previousGeneratedColumn != 0) { + out.append(','); + } + + Base64VLQ.encode(out, textOutput.getColumn() - previousGeneratedColumn); + previousGeneratedColumn = textOutput.getColumn(); + + int sourceIndex = getSourceIndex(source); + Base64VLQ.encode(out, sourceIndex - previousSourceIndex); + previousSourceIndex = sourceIndex; + + Base64VLQ.encode(out, sourceLine - previousSourceLine); + previousSourceLine = sourceLine; + + Base64VLQ.encode(out, sourceColumn - previousSourceColumn); + previousSourceColumn = sourceColumn; + } + + private static int toVLQSigned(int value) { + return value < 0 ? ((-value) << 1) + 1 : value << 1; + } + + public void addLink() { + textOutput.print("\n//@ sourceMappingURL="); + textOutput.print(getOutFilename()); + } + + private static final class Base64VLQ { + // A Base64 VLQ digit can represent 5 bits, so it is base-32. + private static final int VLQ_BASE_SHIFT = 5; + private static final int VLQ_BASE = 1 << VLQ_BASE_SHIFT; + + // A mask of bits for a VLQ digit (11111), 31 decimal. + private static final int VLQ_BASE_MASK = VLQ_BASE - 1; + + // The continuation bit is the 6th bit. + private static final int VLQ_CONTINUATION_BIT = VLQ_BASE; + + /** + * A map used to convert integer values in the range 0-63 to their base64 + * values. + */ + private static final String BASE64_MAP = + "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + + "abcdefghijklmnopqrstuvwxyz" + + "0123456789+/"; + + public static void encode(StringBuilder out, int value) { + value = toVLQSigned(value); + do { + int digit = value & VLQ_BASE_MASK; + value >>>= VLQ_BASE_SHIFT; + if (value > 0) { + digit |= VLQ_CONTINUATION_BIT; + } + out.append(toBase64(digit)); + } + while (value > 0); + } + + public static char toBase64(int value) { + assert (value <= 63 && value >= 0) : "value out of range:" + value; + return BASE64_MAP.charAt(value); + } + } +} From b2b953abdea4c8c844a27acb917544b2ed6f3589 Mon Sep 17 00:00:00 2001 From: develar Date: Thu, 18 Oct 2012 10:48:18 +0400 Subject: [PATCH 12/27] extract interface and move impl to kotlin sources --- .../js/compiler/SourceMapBuilder.java | 165 +----------------- 1 file changed, 8 insertions(+), 157 deletions(-) diff --git a/src/org/jetbrains/js/compiler/SourceMapBuilder.java b/src/org/jetbrains/js/compiler/SourceMapBuilder.java index 1723a407ecc..72b6c36f59c 100644 --- a/src/org/jetbrains/js/compiler/SourceMapBuilder.java +++ b/src/org/jetbrains/js/compiler/SourceMapBuilder.java @@ -1,166 +1,17 @@ package org.jetbrains.js.compiler; -import com.google.dart.compiler.common.SourceInfo; -import com.google.dart.compiler.util.TextOutput; -import com.intellij.util.PairConsumer; -import gnu.trove.TObjectIntHashMap; +import java.io.File; -import java.util.ArrayList; -import java.util.List; +public interface SourceMapBuilder { + void newLine(); -public class SourceMapBuilder { - private final StringBuilder out = new StringBuilder(); - private final String generatedFilename; - private final TextOutput textOutput; - private final PairConsumer sourceInfoConsumer; + void addMapping(String source, int sourceLine, int sourceColumn); - private String lastSource; - private int lastSourceIndex; + void processSourceInfo(Object info); - private final TObjectIntHashMap sources = new TObjectIntHashMap() { - @Override - public int get(String key) { - int index = index(key); - return index < 0 ? -1 : _values[index]; - } - }; + void addLink(); - private final List orderedSources = new ArrayList(); + File getOutFile(); - private int previousGeneratedColumn; - private int previousSourceIndex; - private int previousSourceLine; - private int previousSourceColumn; - - public SourceMapBuilder(String generatedFilename, TextOutput textOutput, PairConsumer sourceInfoConsumer) { - this.generatedFilename = generatedFilename; - this.textOutput = textOutput; - this.sourceInfoConsumer = sourceInfoConsumer; - } - - public String getOutFilename() { - return generatedFilename + ".map"; - } - - public String build() { - StringBuilder sb = new StringBuilder(); - sb.append("{\"version\":3,\"file\":\"").append(generatedFilename).append('"').append(','); - appendSources(sb); - sb.append(",\"names\":["); - sb.append("],\"mappings\":\""); - sb.append(out); - sb.append("\"}"); - return sb.toString(); - } - - private void appendSources(StringBuilder sb) { - boolean isNotFirst = false; - sb.append('"').append("sources").append("\":["); - for (String source : orderedSources) { - if (isNotFirst) { - sb.append(','); - } - else { - isNotFirst = true; - } - sb.append('"').append(source).append('"'); - } - sb.append(']'); - } - - public void newLine() { - out.append(';'); - previousGeneratedColumn = 0; - } - - public void processSourceInfo(Object sourceInfo) { - if (sourceInfo instanceof SourceInfo) { - throw new UnsupportedOperationException("SourceInfo is not yet supported"); - } - sourceInfoConsumer.consume(this, sourceInfo); - } - - private int getSourceIndex(String source) { - if (source.equals(lastSource)) { - return lastSourceIndex; - } - - int sourceIndex = sources.get(source); - if (sourceIndex == -1) { - sourceIndex = sources.put(source, orderedSources.size()); - orderedSources.add(source); - } - - lastSource = source; - lastSourceIndex = sourceIndex; - - return sourceIndex; - } - - public void addMapping(String source, int sourceLine, int sourceColumn) { - if (previousGeneratedColumn != 0) { - out.append(','); - } - - Base64VLQ.encode(out, textOutput.getColumn() - previousGeneratedColumn); - previousGeneratedColumn = textOutput.getColumn(); - - int sourceIndex = getSourceIndex(source); - Base64VLQ.encode(out, sourceIndex - previousSourceIndex); - previousSourceIndex = sourceIndex; - - Base64VLQ.encode(out, sourceLine - previousSourceLine); - previousSourceLine = sourceLine; - - Base64VLQ.encode(out, sourceColumn - previousSourceColumn); - previousSourceColumn = sourceColumn; - } - - private static int toVLQSigned(int value) { - return value < 0 ? ((-value) << 1) + 1 : value << 1; - } - - public void addLink() { - textOutput.print("\n//@ sourceMappingURL="); - textOutput.print(getOutFilename()); - } - - private static final class Base64VLQ { - // A Base64 VLQ digit can represent 5 bits, so it is base-32. - private static final int VLQ_BASE_SHIFT = 5; - private static final int VLQ_BASE = 1 << VLQ_BASE_SHIFT; - - // A mask of bits for a VLQ digit (11111), 31 decimal. - private static final int VLQ_BASE_MASK = VLQ_BASE - 1; - - // The continuation bit is the 6th bit. - private static final int VLQ_CONTINUATION_BIT = VLQ_BASE; - - /** - * A map used to convert integer values in the range 0-63 to their base64 - * values. - */ - private static final String BASE64_MAP = - "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + - "abcdefghijklmnopqrstuvwxyz" + - "0123456789+/"; - - public static void encode(StringBuilder out, int value) { - value = toVLQSigned(value); - do { - int digit = value & VLQ_BASE_MASK; - value >>>= VLQ_BASE_SHIFT; - if (value > 0) { - digit |= VLQ_CONTINUATION_BIT; - } - out.append(toBase64(digit)); - } - while (value > 0); - } - - public static char toBase64(int value) { - assert (value <= 63 && value >= 0) : "value out of range:" + value; - return BASE64_MAP.charAt(value); - } - } + String build(); } From 5ba7a0a8fa4993a3131c3fc33007cd2ea6a9779d Mon Sep 17 00:00:00 2001 From: develar Date: Thu, 18 Oct 2012 18:05:12 +0400 Subject: [PATCH 13/27] KT-927 --- .../js/JsConstructExpressionVisitor.java | 11 +- .../backend/js/JsFirstExpressionVisitor.java | 2 +- .../backend/js/JsSourceGenerationVisitor.java | 36 --- .../js/JsToStringGenerationVisitor.java | 246 ++++-------------- .../backend/js/ast/JsArrayAccess.java | 33 ++- .../backend/js/ast/JsExpressionStatement.java | 6 +- .../compiler/backend/js/ast/JsVisitor.java | 38 +-- .../google/dart/compiler/util/TextOutput.java | 10 + .../dart/compiler/util/TextOutputImpl.java | 26 +- 9 files changed, 118 insertions(+), 290 deletions(-) delete mode 100644 src/com/google/dart/compiler/backend/js/JsSourceGenerationVisitor.java diff --git a/src/com/google/dart/compiler/backend/js/JsConstructExpressionVisitor.java b/src/com/google/dart/compiler/backend/js/JsConstructExpressionVisitor.java index cb3df42ca9b..5225780e8f4 100644 --- a/src/com/google/dart/compiler/backend/js/JsConstructExpressionVisitor.java +++ b/src/com/google/dart/compiler/backend/js/JsConstructExpressionVisitor.java @@ -31,7 +31,7 @@ public class JsConstructExpressionVisitor extends JsVisitor { */ @Override public boolean visit(JsArrayAccess x, JsContext ctx) { - accept(x.getArrayExpr()); + accept(x.getArrayExpression()); return false; } @@ -85,13 +85,12 @@ public class JsConstructExpressionVisitor extends JsVisitor { * We only look at nodes that would not normally be surrounded by parentheses. */ @Override - protected T doAccept(T node) { + public T accept(T node) { // Assign to Object to prevent 'inconvertible types' compile errors due // to http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6548436 // reproducible in jdk1.6.0_02. - Object o = node; - if (o instanceof JsExpression) { - JsExpression expression = (JsExpression) o; + if (node instanceof JsExpression) { + JsExpression expression = (JsExpression) node; int precedence = JsPrecedenceVisitor.exec(expression); // Only visit expressions that won't automatically be surrounded by // parentheses @@ -99,6 +98,6 @@ public class JsConstructExpressionVisitor extends JsVisitor { return node; } } - return super.doAccept(node); + return super.accept(node); } } diff --git a/src/com/google/dart/compiler/backend/js/JsFirstExpressionVisitor.java b/src/com/google/dart/compiler/backend/js/JsFirstExpressionVisitor.java index 9aef75583e0..ece5af6699e 100644 --- a/src/com/google/dart/compiler/backend/js/JsFirstExpressionVisitor.java +++ b/src/com/google/dart/compiler/backend/js/JsFirstExpressionVisitor.java @@ -50,7 +50,7 @@ public class JsFirstExpressionVisitor extends JsVisitor { @Override public boolean visit(JsArrayAccess x, JsContext ctx) { - accept(x.getArrayExpr()); + accept(x.getArrayExpression()); return false; } diff --git a/src/com/google/dart/compiler/backend/js/JsSourceGenerationVisitor.java b/src/com/google/dart/compiler/backend/js/JsSourceGenerationVisitor.java deleted file mode 100644 index 91a3e98f337..00000000000 --- a/src/com/google/dart/compiler/backend/js/JsSourceGenerationVisitor.java +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. - -package com.google.dart.compiler.backend.js; - -import com.google.dart.compiler.backend.js.ast.*; -import com.google.dart.compiler.util.TextOutput; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.js.compiler.SourceMapBuilder; - -/** - * Generates JavaScript source from an AST. - */ -public class JsSourceGenerationVisitor extends JsToStringGenerationVisitor { - public JsSourceGenerationVisitor(TextOutput out, @Nullable SourceMapBuilder sourceMapBuilder) { - super(out, sourceMapBuilder); - } - - @Override - public boolean visit(JsProgram program, JsContext ctx) { - return true; - } - - @Override - public boolean visit(JsProgramFragment x, JsContext ctx) { - // Descend naturally. - return true; - } - - @Override - public boolean visit(JsBlock x, JsContext ctx) { - printJsBlock(x, false, true); - return false; - } -} diff --git a/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java b/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java index c254e8eb311..85b1ae6f09c 100644 --- a/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java +++ b/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java @@ -8,8 +8,6 @@ import com.google.dart.compiler.backend.js.ast.*; import com.google.dart.compiler.backend.js.ast.JsVars.JsVar; import com.google.dart.compiler.util.TextOutput; import gnu.trove.THashSet; -import org.jetbrains.annotations.Nullable; -import org.jetbrains.js.compiler.SourceMapBuilder; import java.util.Iterator; import java.util.List; @@ -200,35 +198,26 @@ public class JsToStringGenerationVisitor extends JsVisitor { * those that appear directly within these global blocks. */ private Set globalBlocks = new THashSet(); - private final TextOutput p; - - @Nullable - private final SourceMapBuilder sourceMapBuilder; + protected final TextOutput p; public JsToStringGenerationVisitor(TextOutput out) { - this(out, null); - } - - public JsToStringGenerationVisitor(TextOutput out, @Nullable SourceMapBuilder sourceMapBuilder) { p = out; - this.sourceMapBuilder = sourceMapBuilder; } @Override - public boolean visit(JsArrayAccess x, JsContext ctx) { - JsExpression arrayExpr = x.getArrayExpr(); - printPair(x, arrayExpr); - _lsquare(); - accept(x.getIndexExpr()); - _rsquare(); + public boolean visit(JsArrayAccess x, JsContext context) { + printPair(x, x.getArrayExpression()); + leftSquare(); + accept(x.getIndexExpression()); + rightSquare(); return false; } @Override public boolean visit(JsArrayLiteral x, JsContext ctx) { - _lsquare(); + leftSquare(); printExpressions(x.getExpressions()); - _rsquare(); + rightSquare(); return false; } @@ -245,42 +234,42 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public boolean visit(JsBinaryOperation binaryOperation, JsContext ctx) { - JsBinaryOperator op = binaryOperation.getOperator(); + public boolean visit(JsBinaryOperation binaryOperation, JsContext context) { + JsBinaryOperator operator = binaryOperation.getOperator(); JsExpression arg1 = binaryOperation.getArg1(); - boolean isExpressionEnclosed = _parenPush(binaryOperation, arg1, !op.isLeftAssociative()); + boolean isExpressionEnclosed = parenPush(binaryOperation, arg1, !operator.isLeftAssociative()); accept(arg1); - if (op.isKeyword()) { - _parenPopOrSpace(binaryOperation, arg1, !op.isLeftAssociative()); + if (operator.isKeyword()) { + _parenPopOrSpace(binaryOperation, arg1, !operator.isLeftAssociative()); } - else if (op != JsBinaryOperator.COMMA) { + else if (operator != JsBinaryOperator.COMMA) { if (isExpressionEnclosed) { rightParen(); } spaceOpt(); } - p.print(op.getSymbol()); + p.print(operator.getSymbol()); JsExpression arg2 = binaryOperation.getArg2(); boolean isParenOpened; - if (op == JsBinaryOperator.COMMA) { + if (operator == JsBinaryOperator.COMMA) { isParenOpened = false; spaceOpt(); } - else if (arg2 instanceof JsBinaryOperation && isBinaryOperationShouldBeEnclosed(((JsBinaryOperation) arg2).getOperator())) { + else if (arg2 instanceof JsBinaryOperation && ((JsBinaryOperation) arg2).getOperator() == JsBinaryOperator.AND) { spaceOpt(); leftParen(); isParenOpened = true; } else { - if (spaceCalc(op, arg2)) { - isParenOpened = _parenPushOrSpace(binaryOperation, arg2, op.isLeftAssociative()); + if (spaceCalc(operator, arg2)) { + isParenOpened = _parenPushOrSpace(binaryOperation, arg2, operator.isLeftAssociative()); } else { spaceOpt(); - isParenOpened = _parenPush(binaryOperation, arg2, op.isLeftAssociative()); + isParenOpened = parenPush(binaryOperation, arg2, operator.isLeftAssociative()); } } accept(arg2); @@ -291,10 +280,6 @@ public class JsToStringGenerationVisitor extends JsVisitor { return false; } - private static boolean isBinaryOperationShouldBeEnclosed(JsBinaryOperator operator) { - return operator == JsBinaryOperator.AND; - } - @Override public boolean visit(JsBlock x, JsContext ctx) { printJsBlock(x, true, true); @@ -307,21 +292,21 @@ public class JsToStringGenerationVisitor extends JsVisitor { p.print(CHARS_TRUE); } else { - _false(); + p.print(CHARS_FALSE); } return false; } @Override public boolean visit(JsBreak x, JsContext ctx) { - _break(); + p.print(CHARS_BREAK); continueOrBreakLabel(x); return false; } @Override public boolean visit(JsContinue x, JsContext ctx) { - _continue(); + p.print(CHARS_CONTINUE); continueOrBreakLabel(x); return false; } @@ -336,7 +321,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { @Override public boolean visit(JsCase x, JsContext ctx) { - _case(); + p.print(CHARS_CASE); space(); accept(x.getCaseExpr()); _colon(); @@ -347,7 +332,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { } private void printSwitchMemberStatements(JsSwitchMember x) { - indent(); + p.indentIn(); for (JsStatement stmt : x.getStatements()) { needSemi = true; accept(stmt); @@ -356,14 +341,14 @@ public class JsToStringGenerationVisitor extends JsVisitor { } newlineOpt(); } - outdent(); + p.indentOut(); needSemi = false; } @Override public boolean visit(JsCatch x, JsContext ctx) { spaceOpt(); - _catch(); + p.print(CHARS_CATCH); spaceOpt(); leftParen(); nameDef(x.getParameter().getName()); @@ -416,13 +401,13 @@ public class JsToStringGenerationVisitor extends JsVisitor { @Override public boolean visit(JsDebugger x, JsContext ctx) { - _debugger(); + p.print(CHARS_DEBUGGER); return false; } @Override public boolean visit(JsDefault x, JsContext ctx) { - _default(); + p.print(CHARS_DEFAULT); _colon(); printSwitchMemberStatements(x); @@ -444,7 +429,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { @Override public boolean visit(JsDoWhile x, JsContext ctx) { - _do(); + p.print(CHARS_DO); _nestedPush(x.getBody()); accept(x.getBody()); _nestedPop(x.getBody()); @@ -547,7 +532,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { } space(); - _in(); + p.print(CHARS_IN); space(); accept(x.getObjExpr()); @@ -560,7 +545,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { @Override public boolean visit(JsFunction x, JsContext ctx) { - _function(); + p.print(CHARS_FUNCTION); space(); if (x.getName() != null) { nameOf(x); @@ -593,8 +578,8 @@ public class JsToStringGenerationVisitor extends JsVisitor { _nestedPush(thenStmt); accept(thenStmt); _nestedPop(thenStmt); - JsStatement elseStmt = x.getElseStatement(); - if (elseStmt != null) { + JsStatement elseStatement = x.getElseStatement(); + if (elseStatement != null) { if (needSemi) { semi(); newlineOpt(); @@ -603,17 +588,17 @@ public class JsToStringGenerationVisitor extends JsVisitor { spaceOpt(); needSemi = true; } - _else(); - boolean elseIf = elseStmt instanceof JsIf; + p.print(CHARS_ELSE); + boolean elseIf = elseStatement instanceof JsIf; if (!elseIf) { - _nestedPush(elseStmt); + _nestedPush(elseStatement); } else { space(); } - accept(elseStmt); + accept(elseStatement); if (!elseIf) { - _nestedPop(elseStmt); + _nestedPop(elseStatement); } } return false; @@ -642,7 +627,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { public boolean visit(JsNameRef x, JsContext ctx) { JsExpression q = x.getQualifier(); if (q != null) { - _parenPush(x, q, false); + parenPush(x, q, false); if (q instanceof JsNumberLiteral) { /** * Fix for Issue #3796. "42.foo" is not allowed, but "(42).foo" is. @@ -654,7 +639,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { rightParen(); } parenPop(x, q, false); - _dot(); + p.print('.'); } _nameRef(x); return false; @@ -662,7 +647,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { @Override public boolean visit(JsNew x, JsContext ctx) { - _new(); + p.print(CHARS_NEW); space(); JsExpression ctorExpr = x.getConstructorExpression(); @@ -684,7 +669,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { @Override public boolean visit(JsNullLiteral x, JsContext ctx) { - _null(); + p.print(CHARS_NULL); return false; } @@ -702,7 +687,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { @Override public boolean visit(JsObjectLiteral objectLiteral, JsContext context) { - _lbrace(); + p.print('{'); if (objectLiteral.isMultiline()) { p.indentIn(); } @@ -749,7 +734,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { newlineOpt(); } - _rbrace(); + p.print('}'); return false; } @@ -816,7 +801,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { @Override public boolean visit(JsReturn x, JsContext ctx) { - _return(); + p.print(CHARS_RETURN); JsExpression expr = x.getExpr(); if (expr != null) { space(); @@ -827,13 +812,13 @@ public class JsToStringGenerationVisitor extends JsVisitor { @Override public boolean visit(JsStringLiteral x, JsContext ctx) { - printStringLiteral(x.getValue()); + p.print(javaScriptString(x.getValue())); return false; } @Override public boolean visit(JsSwitch x, JsContext ctx) { - _switch(); + p.print(CHARS_SWITCH); spaceOpt(); leftParen(); accept(x.getExpr()); @@ -927,13 +912,13 @@ public class JsToStringGenerationVisitor extends JsVisitor { space(); } else { - newline(); + p.newline(); } boolean notFirst = false; for (Map.Entry entry : comment.getTags().entrySet()) { if (notFirst) { - newline(); + p.newline(); p.print(' '); p.print('*'); } @@ -955,7 +940,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { } if (!asSingleLine) { - newline(); + p.newline(); } } @@ -974,16 +959,9 @@ public class JsToStringGenerationVisitor extends JsVisitor { return false; } - protected final void newline() { - if (sourceMapBuilder != null) { - sourceMapBuilder.newLine(); - } - p.newline(); - } - protected final void newlineOpt() { if (!p.isCompact()) { - newline(); + p.newline(); } } @@ -1046,12 +1024,12 @@ public class JsToStringGenerationVisitor extends JsVisitor { newlineOpt(); } else { - newline(); + p.newline(); } } else { if (lastStatement) { - semiOpt(); + p.printOpt(';'); } else { semi(); @@ -1089,75 +1067,23 @@ public class JsToStringGenerationVisitor extends JsVisitor { newlineOpt(); } - private void _break() { - p.print(CHARS_BREAK); - } - - private void _case() { - p.print(CHARS_CASE); - } - - private void _catch() { - p.print(CHARS_CATCH); - } - private void _colon() { p.print(':'); } - private void _continue() { - p.print(CHARS_CONTINUE); - } - - private void _debugger() { - p.print(CHARS_DEBUGGER); - } - - private void _default() { - p.print(CHARS_DEFAULT); - } - - private void _do() { - p.print(CHARS_DO); - } - - private void _dot() { - p.print('.'); - } - - private void _else() { - p.print(CHARS_ELSE); - } - - private void _false() { - p.print(CHARS_FALSE); - } - private void _for() { p.print(CHARS_FOR); } - private void _function() { - p.print(CHARS_FUNCTION); - } - private void _if() { p.print(CHARS_IF); } - private void _in() { - p.print(CHARS_IN); - } - - private void _lbrace() { - p.print('{'); - } - private void leftParen() { p.print('('); } - private void _lsquare() { + private void leftSquare() { p.print('['); } @@ -1193,14 +1119,6 @@ public class JsToStringGenerationVisitor extends JsVisitor { return push; } - private void _new() { - p.print(CHARS_NEW); - } - - private void _null() { - p.print(CHARS_NULL); - } - private static boolean parenCalc(JsExpression parent, JsExpression child, boolean wrongAssoc) { int parentPrec = JsPrecedenceVisitor.exec(parent); int childPrec = JsPrecedenceVisitor.exec(child); @@ -1226,7 +1144,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { return doPop; } - private boolean _parenPush(JsExpression parent, JsExpression child, boolean wrongAssoc) { + private boolean parenPush(JsExpression parent, JsExpression child, boolean wrongAssoc) { boolean doPush = parenCalc(parent, child, wrongAssoc); if (doPush) { leftParen(); @@ -1257,19 +1175,11 @@ public class JsToStringGenerationVisitor extends JsVisitor { p.print('?'); } - private void _rbrace() { - p.print('}'); - } - - private void _return() { - p.print(CHARS_RETURN); - } - private void rightParen() { p.print(')'); } - private void _rsquare() { + private void rightSquare() { p.print(']'); } @@ -1277,10 +1187,6 @@ public class JsToStringGenerationVisitor extends JsVisitor { p.print(';'); } - private void semiOpt() { - p.printOpt(';'); - } - private boolean sepCommaOptSpace(boolean sep) { if (sep) { p.print(','); @@ -1338,10 +1244,6 @@ public class JsToStringGenerationVisitor extends JsVisitor { p.printOpt(' '); } - private void _switch() { - p.print(CHARS_SWITCH); - } - private void var() { p.print(CHARS_VAR); } @@ -1349,38 +1251,4 @@ public class JsToStringGenerationVisitor extends JsVisitor { private void _while() { p.print(CHARS_WHILE); } - - private void indent() { - p.indentIn(); - } - - private void outdent() { - p.indentOut(); - } - - private void printStringLiteral(String value) { - p.print(javaScriptString(value)); - } - - @Override - protected void doTraverse(JsVisitable node, JsContext context) { - super.doTraverse(node, context); - - if (sourceMapBuilder == null) { - return; - } - - Object sourceInfo = node.getSourceInfo(); - if (sourceInfo != null) { - sourceMapBuilder.processSourceInfo(sourceInfo); - } - } - - @Override - public void endVisit(JsProgram x, JsContext context) { - super.endVisit(x, context); - if (sourceMapBuilder != null) { - sourceMapBuilder.addLink(); - } - } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsArrayAccess.java b/src/com/google/dart/compiler/backend/js/ast/JsArrayAccess.java index 6a6eefdd3e0..ec08121c755 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsArrayAccess.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsArrayAccess.java @@ -8,30 +8,29 @@ package com.google.dart.compiler.backend.js.ast; * Represents a javascript expression for array access. */ public final class JsArrayAccess extends JsExpressionImpl { - - private JsExpression arrayExpr; - private JsExpression indexExpr; + private JsExpression arrayExpression; + private JsExpression indexExpression; public JsArrayAccess() { super(); } - public JsArrayAccess(JsExpression arrayExpr, JsExpression indexExpr) { - this.arrayExpr = arrayExpr; - this.indexExpr = indexExpr; + public JsArrayAccess(JsExpression arrayExpression, JsExpression indexExpression) { + this.arrayExpression = arrayExpression; + this.indexExpression = indexExpression; } - public JsExpression getArrayExpr() { - return arrayExpr; + public JsExpression getArrayExpression() { + return arrayExpression; } - public JsExpression getIndexExpr() { - return indexExpr; + public JsExpression getIndexExpression() { + return indexExpression; } @Override public boolean hasSideEffects() { - return arrayExpr.hasSideEffects() || indexExpr.hasSideEffects(); + return arrayExpression.hasSideEffects() || indexExpression.hasSideEffects(); } @Override @@ -44,19 +43,19 @@ public final class JsArrayAccess extends JsExpressionImpl { return false; } - public void setArrayExpr(JsExpression arrayExpr) { - this.arrayExpr = arrayExpr; + public void setArrayExpression(JsExpression arrayExpression) { + this.arrayExpression = arrayExpression; } - public void setIndexExpr(JsExpression indexExpr) { - this.indexExpr = indexExpr; + public void setIndexExpression(JsExpression indexExpression) { + this.indexExpression = indexExpression; } @Override public void traverse(JsVisitor v, JsContext context) { if (v.visit(this, context)) { - arrayExpr = v.accept(arrayExpr); - indexExpr = v.accept(indexExpr); + arrayExpression = v.accept(arrayExpression); + indexExpression = v.accept(indexExpression); } v.endVisit(this, context); } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsExpressionStatement.java b/src/com/google/dart/compiler/backend/js/ast/JsExpressionStatement.java index 1e24b31894d..ba5a1b6f26f 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsExpressionStatement.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsExpressionStatement.java @@ -4,8 +4,6 @@ package com.google.dart.compiler.backend.js.ast; -import com.google.dart.compiler.common.SourceInfo; - public final class JsExpressionStatement extends AbstractNode implements JsStatement { private JsExpression expression; @@ -32,11 +30,11 @@ public final class JsExpressionStatement extends AbstractNode implements JsState @Override public Object getSourceInfo() { - return expression.getSourceInfo(); + return null; } @Override public void setSourceInfo(Object info) { - expression.setSourceInfo(info); + throw new IllegalStateException("You must not set source info for JsExpressionStatement, set for expression"); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsVisitor.java b/src/com/google/dart/compiler/backend/js/ast/JsVisitor.java index 3f0e5f3e7d5..dbcd5e1b9ce 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsVisitor.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsVisitor.java @@ -87,20 +87,26 @@ abstract public class JsVisitor { } }; - public final T accept(T node) { - return doAccept(node); + public T accept(T node) { + doTraverse(node, UNMODIFIABLE_CONTEXT); + return node; } public final void acceptList(List collection) { - doAcceptList(collection); + for (T node : collection) { + doTraverse(node, UNMODIFIABLE_CONTEXT); + } } public JsExpression acceptLvalue(JsExpression expr) { - return doAcceptLvalue(expr); + doTraverse(expr, LVALUE_CONTEXT); + return expr; } public final void acceptWithInsertRemove(List collection) { - doAcceptWithInsertRemove(collection); + for (T node : collection) { + doTraverse(node, UNMODIFIABLE_CONTEXT); + } } public void endVisit(JsArrayAccess x, JsContext ctx) { @@ -405,28 +411,6 @@ abstract public class JsVisitor { return true; } - protected T doAccept(T node) { - doTraverse(node, UNMODIFIABLE_CONTEXT); - return node; - } - - protected void doAcceptList(List collection) { - for (T node : collection) { - doTraverse(node, UNMODIFIABLE_CONTEXT); - } - } - - protected JsExpression doAcceptLvalue(JsExpression expr) { - doTraverse(expr, LVALUE_CONTEXT); - return expr; - } - - protected void doAcceptWithInsertRemove(List collection) { - for (T node : collection) { - doTraverse(node, UNMODIFIABLE_CONTEXT); - } - } - protected void doTraverse(JsVisitable node, JsContext context) { node.traverse(this, context); } diff --git a/src/com/google/dart/compiler/util/TextOutput.java b/src/com/google/dart/compiler/util/TextOutput.java index 16134ad5d60..fdacbc4a284 100644 --- a/src/com/google/dart/compiler/util/TextOutput.java +++ b/src/com/google/dart/compiler/util/TextOutput.java @@ -37,4 +37,14 @@ public interface TextOutput { void printOpt(String s); boolean isCompact(); + + boolean isJustNewlined(); + + void setOutListener(OutListener outListener); + + public interface OutListener { + void newLined(); + + void indentedAfterNewLine(); + } } diff --git a/src/com/google/dart/compiler/util/TextOutputImpl.java b/src/com/google/dart/compiler/util/TextOutputImpl.java index 6cb76be1da3..beab9ea72f5 100644 --- a/src/com/google/dart/compiler/util/TextOutputImpl.java +++ b/src/com/google/dart/compiler/util/TextOutputImpl.java @@ -6,9 +6,6 @@ package com.google.dart.compiler.util; import java.util.Arrays; -/** - * An abstract base type to build TextOutput implementations. - */ public class TextOutputImpl implements TextOutput { private final boolean compact; private int identLevel = 0; @@ -20,6 +17,8 @@ public class TextOutputImpl implements TextOutput { private int line = 0; private int column = 0; + private OutListener outListener; + public TextOutputImpl() { this(false); } @@ -107,30 +106,24 @@ public class TextOutputImpl implements TextOutput { private void movePosition(int l) { position += l; column += l; - justNewlined = false; } @Override public void print(char[] s) { maybeIndent(); printAndCount(s); - justNewlined = false; } @Override public void print(CharSequence s) { maybeIndent(); printAndCount(s); - justNewlined = false; } @Override public void printOpt(char c) { if (!compact) { - maybeIndent(); - out.append(c); - position++; - column++; + print(c); } } @@ -154,6 +147,9 @@ public class TextOutputImpl implements TextOutput { if (justNewlined && !compact) { printAndCount(indents[identLevel]); justNewlined = false; + if (outListener != null) { + outListener.indentedAfterNewLine(); + } } } @@ -168,4 +164,14 @@ public class TextOutputImpl implements TextOutput { column += chars.length; out.append(chars); } + + @Override + public boolean isJustNewlined() { + return justNewlined && !compact; + } + + @Override + public void setOutListener(OutListener outListener) { + this.outListener = outListener; + } } From af75857523ef438aea74de5b4855041ea2976a02 Mon Sep 17 00:00:00 2001 From: develar Date: Thu, 18 Oct 2012 18:35:04 +0400 Subject: [PATCH 14/27] KT-2864 --- src/com/google/dart/compiler/util/TextOutputImpl.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/com/google/dart/compiler/util/TextOutputImpl.java b/src/com/google/dart/compiler/util/TextOutputImpl.java index beab9ea72f5..f1a85ee4240 100644 --- a/src/com/google/dart/compiler/util/TextOutputImpl.java +++ b/src/com/google/dart/compiler/util/TextOutputImpl.java @@ -78,6 +78,9 @@ public class TextOutputImpl implements TextOutput { line++; column = 0; justNewlined = true; + if (outListener != null) { + outListener.newLined(); + } } @Override From 536803e314251cffe08652afa5cc7a48902637c6 Mon Sep 17 00:00:00 2001 From: develar Date: Tue, 23 Oct 2012 18:57:54 +0400 Subject: [PATCH 15/27] KT-2986 --- .../backend/js/JsRequiresSemiVisitor.java | 2 +- .../js/JsToStringGenerationVisitor.java | 14 ++-- .../dart/compiler/backend/js/ast/JsBreak.java | 8 +- .../compiler/backend/js/ast/JsContinue.java | 12 +-- .../dart/compiler/backend/js/ast/JsFor.java | 36 ++++----- .../dart/compiler/backend/js/ast/JsLabel.java | 74 ++++++++++--------- 6 files changed, 71 insertions(+), 75 deletions(-) diff --git a/src/com/google/dart/compiler/backend/js/JsRequiresSemiVisitor.java b/src/com/google/dart/compiler/backend/js/JsRequiresSemiVisitor.java index 229dedc5417..b2205d3160b 100644 --- a/src/com/google/dart/compiler/backend/js/JsRequiresSemiVisitor.java +++ b/src/com/google/dart/compiler/backend/js/JsRequiresSemiVisitor.java @@ -100,7 +100,7 @@ public class JsRequiresSemiVisitor extends JsVisitor { @Override public boolean visit(JsLabel x, JsContext ctx) { - if (x.getStmt() instanceof JsEmpty) { + if (x.getStatement() instanceof JsEmpty) { needsSemicolon = true; } return false; diff --git a/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java b/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java index 85b1ae6f09c..6476775a129 100644 --- a/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java +++ b/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java @@ -312,10 +312,10 @@ public class JsToStringGenerationVisitor extends JsVisitor { } private void continueOrBreakLabel(JsContinue x) { - JsNameRef label = x.getLabel(); + String label = x.getLabel(); if (label != null) { space(); - _nameRef(label); + p.print(label); } } @@ -475,8 +475,8 @@ public class JsToStringGenerationVisitor extends JsVisitor { // The init expressions or var decl. // - if (x.getInitExpr() != null) { - accept(x.getInitExpr()); + if (x.getInitExpression() != null) { + accept(x.getInitExpression()); } else if (x.getInitVars() != null) { accept(x.getInitVars()); @@ -495,9 +495,9 @@ public class JsToStringGenerationVisitor extends JsVisitor { // The incr expression. // - if (x.getIncrExpr() != null) { + if (x.getIncrementExpression() != null) { spaceOpt(); - accept(x.getIncrExpr()); + accept(x.getIncrementExpression()); } rightParen(); @@ -619,7 +619,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { nameOf(x); _colon(); spaceOpt(); - accept(x.getStmt()); + accept(x.getStatement()); return false; } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsBreak.java b/src/com/google/dart/compiler/backend/js/ast/JsBreak.java index 3360bcb7b2f..50a23c051e2 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsBreak.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsBreak.java @@ -12,7 +12,7 @@ public final class JsBreak extends JsContinue { super(null); } - public JsBreak(JsNameRef label) { + public JsBreak(String label) { super(label); } @@ -23,11 +23,7 @@ public final class JsBreak extends JsContinue { @Override public void traverse(JsVisitor v, JsContext context) { - if (v.visit(this, context)) { - if (label != null) { - v.accept(label); - } - } + v.visit(this, context); v.endVisit(this, context); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsContinue.java b/src/com/google/dart/compiler/backend/js/ast/JsContinue.java index 65e88f25932..dcec38018f7 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsContinue.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsContinue.java @@ -10,28 +10,24 @@ import org.jetbrains.annotations.Nullable; * Represents the JavaScript continue statement. */ public class JsContinue extends JsNodeImpl implements JsStatement { - protected final JsNameRef label; + protected final String label; public JsContinue() { this(null); } - public JsContinue(@Nullable JsNameRef label) { + public JsContinue(@Nullable String label) { super(); this.label = label; } - public JsNameRef getLabel() { + public String getLabel() { return label; } @Override public void traverse(JsVisitor v, JsContext context) { - if (v.visit(this, context)) { - if (label != null) { - v.accept(label); - } - } + v.visit(this, context); v.endVisit(this, context); } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsFor.java b/src/com/google/dart/compiler/backend/js/ast/JsFor.java index 4efb65d93c8..fe6b4ad3ca5 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsFor.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsFor.java @@ -8,7 +8,7 @@ package com.google.dart.compiler.backend.js.ast; * A for statement. If specified at all, the initializer part is * either a declaration of one or more variables, in which case * {@link #getInitVars()} is used, or an expression, in which case - * {@link #getInitExpr()} is used. In the latter case, the comma operator is + * {@link #getInitExpression()} is used. In the latter case, the comma operator is * often used to create a compound expression. *

*

@@ -18,20 +18,20 @@ package com.google.dart.compiler.backend.js.ast; public class JsFor extends JsNodeImpl implements JsStatement { private JsStatement body; private JsExpression condition; - private JsExpression incrExpr; - private JsExpression initExpr; + private JsExpression incrementExpression; + private JsExpression initExpression; private JsVars initVars; - public JsFor(JsVars initVars, JsExpression condition, JsExpression incrementExpr) { + public JsFor(JsVars initVars, JsExpression condition, JsExpression incrementExpression) { this.initVars = initVars; - this.incrExpr = incrementExpr; + this.incrementExpression = incrementExpression; this.condition = condition; - initExpr = null; + initExpression = null; } - public JsFor(JsExpression initExpr, JsExpression condition, JsExpression incrementExpr) { - this.initExpr = initExpr; - this.incrExpr = incrementExpr; + public JsFor(JsExpression initExpression, JsExpression condition, JsExpression incrementExpression) { + this.initExpression = initExpression; + this.incrementExpression = incrementExpression; this.condition = condition; initVars = null; } @@ -44,12 +44,12 @@ public class JsFor extends JsNodeImpl implements JsStatement { return condition; } - public JsExpression getIncrExpr() { - return incrExpr; + public JsExpression getIncrementExpression() { + return incrementExpression; } - public JsExpression getInitExpr() { - return initExpr; + public JsExpression getInitExpression() { + return initExpression; } public JsVars getInitVars() { @@ -63,10 +63,10 @@ public class JsFor extends JsNodeImpl implements JsStatement { @Override public void traverse(JsVisitor v, JsContext context) { if (v.visit(this, context)) { - assert (!(initExpr != null && initVars != null)); + assert (!(initExpression != null && initVars != null)); - if (initExpr != null) { - initExpr = v.accept(initExpr); + if (initExpression != null) { + initExpression = v.accept(initExpression); } else if (initVars != null) { initVars = v.accept(initVars); @@ -76,8 +76,8 @@ public class JsFor extends JsNodeImpl implements JsStatement { condition = v.accept(condition); } - if (incrExpr != null) { - incrExpr = v.accept(incrExpr); + if (incrementExpression != null) { + incrementExpression = v.accept(incrementExpression); } body = v.accept(body); } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsLabel.java b/src/com/google/dart/compiler/backend/js/ast/JsLabel.java index 040434b95b6..3dbe28bc361 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsLabel.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsLabel.java @@ -10,43 +10,47 @@ import com.google.dart.compiler.common.Symbol; * Represents a JavaScript label statement. */ public class JsLabel extends JsNodeImpl implements JsStatement, HasName { + private final JsName label; - private final JsName label; + private JsStatement statement; - private JsStatement stmt; - - public JsLabel(JsName label) { - this.label = label; - } - - @Override - public JsName getName() { - return label; - } - - @Override - public Symbol getSymbol() { - return label; - } - - public JsStatement getStmt() { - return stmt; - } - - public void setStmt(JsStatement stmt) { - this.stmt = stmt; - } - - @Override - public void traverse(JsVisitor v, JsContext context) { - if (v.visit(this, context)) { - stmt = v.accept(stmt); + public JsLabel(JsName label) { + this.label = label; } - v.endVisit(this, context); - } - @Override - public NodeKind getKind() { - return NodeKind.LABEL; - } + public JsLabel(JsName label, JsStatement statement) { + this.label = label; + this.statement = statement; + } + + @Override + public JsName getName() { + return label; + } + + @Override + public Symbol getSymbol() { + return label; + } + + public JsStatement getStatement() { + return statement; + } + + public void setStatement(JsStatement statement) { + this.statement = statement; + } + + @Override + public void traverse(JsVisitor v, JsContext context) { + if (v.visit(this, context)) { + statement = v.accept(statement); + } + v.endVisit(this, context); + } + + @Override + public NodeKind getKind() { + return NodeKind.LABEL; + } } From a5b212e975bfcdb8c1b8896031e958bf3b672cfa Mon Sep 17 00:00:00 2001 From: develar Date: Wed, 24 Oct 2012 11:36:21 +0400 Subject: [PATCH 16/27] JsFor constructors --- src/com/google/dart/compiler/backend/js/ast/JsFor.java | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/com/google/dart/compiler/backend/js/ast/JsFor.java b/src/com/google/dart/compiler/backend/js/ast/JsFor.java index fe6b4ad3ca5..2eb846e6b6f 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsFor.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsFor.java @@ -23,16 +23,26 @@ public class JsFor extends JsNodeImpl implements JsStatement { private JsVars initVars; public JsFor(JsVars initVars, JsExpression condition, JsExpression incrementExpression) { + this(initVars, condition, incrementExpression, null); + } + + public JsFor(JsVars initVars, JsExpression condition, JsExpression incrementExpression, JsStatement body) { this.initVars = initVars; this.incrementExpression = incrementExpression; this.condition = condition; + this.body = body; initExpression = null; } public JsFor(JsExpression initExpression, JsExpression condition, JsExpression incrementExpression) { + this(initExpression, condition, incrementExpression, null); + } + + public JsFor(JsExpression initExpression, JsExpression condition, JsExpression incrementExpression, JsStatement body) { this.initExpression = initExpression; this.incrementExpression = incrementExpression; this.condition = condition; + this.body = body; initVars = null; } From c41bf918bfa37a926751a204bd21e471a6ec9772 Mon Sep 17 00:00:00 2001 From: develar Date: Thu, 25 Oct 2012 08:42:06 +0400 Subject: [PATCH 17/27] simplify AST (idea like) --- .../js/JsConstructExpressionVisitor.java | 165 ++++--- .../backend/js/JsFirstExpressionVisitor.java | 5 - .../backend/js/JsPrecedenceVisitor.java | 446 +++++++++--------- .../backend/js/JsRequiresSemiVisitor.java | 5 - .../js/JsToStringGenerationVisitor.java | 28 +- .../backend/js/ast/ChameleonJsExpression.java | 11 +- .../compiler/backend/js/ast/HasArguments.java | 1 - .../backend/js/ast/JsArrayAccess.java | 108 ++--- .../backend/js/ast/JsArrayLiteral.java | 12 +- .../backend/js/ast/JsBinaryOperation.java | 143 +++--- .../dart/compiler/backend/js/ast/JsBlock.java | 12 +- .../dart/compiler/backend/js/ast/JsBreak.java | 3 +- .../dart/compiler/backend/js/ast/JsCase.java | 51 +- .../dart/compiler/backend/js/ast/JsCatch.java | 104 ++-- .../backend/js/ast/JsConditional.java | 16 +- .../compiler/backend/js/ast/JsContext.java | 6 +- .../compiler/backend/js/ast/JsContinue.java | 11 +- .../compiler/backend/js/ast/JsDebugger.java | 10 +- .../compiler/backend/js/ast/JsDefault.java | 12 +- .../compiler/backend/js/ast/JsDoWhile.java | 3 +- .../compiler/backend/js/ast/JsDocComment.java | 7 +- .../dart/compiler/backend/js/ast/JsEmpty.java | 29 +- .../compiler/backend/js/ast/JsExpression.java | 2 +- .../backend/js/ast/JsExpressionImpl.java | 15 + .../backend/js/ast/JsExpressionStatement.java | 12 +- .../dart/compiler/backend/js/ast/JsFor.java | 38 +- .../dart/compiler/backend/js/ast/JsForIn.java | 115 ++--- .../compiler/backend/js/ast/JsFunction.java | 14 +- .../dart/compiler/backend/js/ast/JsIf.java | 20 +- .../compiler/backend/js/ast/JsInvocation.java | 21 +- .../dart/compiler/backend/js/ast/JsLabel.java | 12 +- .../compiler/backend/js/ast/JsLiteral.java | 28 +- .../compiler/backend/js/ast/JsNameRef.java | 39 +- .../dart/compiler/backend/js/ast/JsNew.java | 39 +- .../dart/compiler/backend/js/ast/JsNode.java | 12 +- .../compiler/backend/js/ast/JsNodeImpl.java | 2 - .../backend/js/ast/JsNullLiteral.java | 5 +- .../backend/js/ast/JsNumberLiteral.java | 8 +- .../backend/js/ast/JsObjectLiteral.java | 12 +- .../compiler/backend/js/ast/JsParameter.java | 46 +- .../backend/js/ast/JsPostfixOperation.java | 7 +- .../backend/js/ast/JsPrefixOperation.java | 20 +- .../compiler/backend/js/ast/JsProgram.java | 25 +- .../backend/js/ast/JsProgramFragment.java | 41 +- .../backend/js/ast/JsPropertyInitializer.java | 14 +- .../compiler/backend/js/ast/JsRegExp.java | 84 ++-- .../compiler/backend/js/ast/JsReturn.java | 57 +-- .../dart/compiler/backend/js/ast/JsScope.java | 4 +- .../backend/js/ast/JsStringLiteral.java | 5 +- .../compiler/backend/js/ast/JsSwitch.java | 60 +-- .../backend/js/ast/JsSwitchMember.java | 5 + .../dart/compiler/backend/js/ast/JsThrow.java | 12 +- .../dart/compiler/backend/js/ast/JsTry.java | 20 +- .../backend/js/ast/JsUnaryOperation.java | 77 +-- .../backend/js/ast/JsValueLiteral.java | 23 - .../dart/compiler/backend/js/ast/JsVars.java | 26 +- .../compiler/backend/js/ast/JsVisitable.java | 17 - .../compiler/backend/js/ast/JsVisitor.java | 174 +------ .../dart/compiler/backend/js/ast/JsWhile.java | 14 +- .../backend/js/ast/RecursiveJsVisitor.java | 4 + 60 files changed, 1085 insertions(+), 1232 deletions(-) delete mode 100644 src/com/google/dart/compiler/backend/js/ast/JsValueLiteral.java delete mode 100644 src/com/google/dart/compiler/backend/js/ast/JsVisitable.java create mode 100644 src/com/google/dart/compiler/backend/js/ast/RecursiveJsVisitor.java diff --git a/src/com/google/dart/compiler/backend/js/JsConstructExpressionVisitor.java b/src/com/google/dart/compiler/backend/js/JsConstructExpressionVisitor.java index 5225780e8f4..38882310ca0 100644 --- a/src/com/google/dart/compiler/backend/js/JsConstructExpressionVisitor.java +++ b/src/com/google/dart/compiler/backend/js/JsConstructExpressionVisitor.java @@ -11,93 +11,92 @@ import com.google.dart.compiler.backend.js.ast.*; * normally be surrounded by parentheses. */ public class JsConstructExpressionVisitor extends JsVisitor { - - public static boolean exec(JsExpression expression) { - if (JsPrecedenceVisitor.exec(expression) < JsPrecedenceVisitor.PRECEDENCE_NEW) { - return true; + public static boolean exec(JsExpression expression) { + if (JsPrecedenceVisitor.exec(expression) < JsPrecedenceVisitor.PRECEDENCE_NEW) { + return true; + } + JsConstructExpressionVisitor visitor = new JsConstructExpressionVisitor(); + visitor.accept(expression); + return visitor.containsInvocation; } - JsConstructExpressionVisitor visitor = new JsConstructExpressionVisitor(); - visitor.accept(expression); - return visitor.containsInvocation; - } - private boolean containsInvocation = false; + private boolean containsInvocation; - private JsConstructExpressionVisitor() { - } - - /** - * We only look at the array expression since the index has its own scope. - */ - @Override - public boolean visit(JsArrayAccess x, JsContext ctx) { - accept(x.getArrayExpression()); - return false; - } - - /** - * Array literals have their own scoping. - */ - @Override - public boolean visit(JsArrayLiteral x, JsContext ctx) { - return false; - } - - /** - * Functions have their own scoping. - */ - @Override - public boolean visit(JsFunction x, JsContext ctx) { - return false; - } - - @Override - public boolean visit(JsInvocation x, JsContext ctx) { - containsInvocation = true; - return false; - } - - @Override - public boolean visit(JsNameRef x, JsContext ctx) { - if (!x.isLeaf()) { - accept(x.getQualifier()); + private JsConstructExpressionVisitor() { } - return false; - } - /** - * New constructs bind to the nearest set of parentheses. - */ - @Override - public boolean visit(JsNew x, JsContext ctx) { - return false; - } - - /** - * Object literals have their own scope. - */ - @Override - public boolean visit(JsObjectLiteral x, JsContext ctx) { - return false; - } - - /** - * We only look at nodes that would not normally be surrounded by parentheses. - */ - @Override - public T accept(T node) { - // Assign to Object to prevent 'inconvertible types' compile errors due - // to http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6548436 - // reproducible in jdk1.6.0_02. - if (node instanceof JsExpression) { - JsExpression expression = (JsExpression) node; - int precedence = JsPrecedenceVisitor.exec(expression); - // Only visit expressions that won't automatically be surrounded by - // parentheses - if (precedence < JsPrecedenceVisitor.PRECEDENCE_NEW) { - return node; - } + /** + * We only look at the array expression since the index has its own scope. + */ + @Override + public boolean visit(JsArrayAccess x, JsContext ctx) { + accept(x.getArrayExpression()); + return false; + } + + /** + * Array literals have their own scoping. + */ + @Override + public boolean visit(JsArrayLiteral x, JsContext ctx) { + return false; + } + + /** + * Functions have their own scoping. + */ + @Override + public boolean visit(JsFunction x, JsContext ctx) { + return false; + } + + @Override + public boolean visit(JsInvocation x, JsContext ctx) { + containsInvocation = true; + return false; + } + + @Override + public boolean visit(JsNameRef x, JsContext ctx) { + if (!x.isLeaf()) { + accept(x.getQualifier()); + } + return false; + } + + /** + * New constructs bind to the nearest set of parentheses. + */ + @Override + public boolean visit(JsNew x, JsContext ctx) { + return false; + } + + /** + * Object literals have their own scope. + */ + @Override + public boolean visit(JsObjectLiteral x, JsContext ctx) { + return false; + } + + /** + * We only look at nodes that would not normally be surrounded by parentheses. + */ + @Override + public T accept(T node) { + // Assign to Object to prevent 'inconvertible types' compile errors due + // to http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6548436 + // reproducible in jdk1.6.0_02. + if (node instanceof JsExpression) { + JsExpression expression = (JsExpression) node; + int precedence = JsPrecedenceVisitor.exec(expression); + // Only visit expressions that won't automatically be surrounded by + // parentheses + if (precedence < JsPrecedenceVisitor.PRECEDENCE_NEW) { + return node; + } + } + return super.accept(node); } - return super.accept(node); - } } diff --git a/src/com/google/dart/compiler/backend/js/JsFirstExpressionVisitor.java b/src/com/google/dart/compiler/backend/js/JsFirstExpressionVisitor.java index ece5af6699e..4b6a80ef4af 100644 --- a/src/com/google/dart/compiler/backend/js/JsFirstExpressionVisitor.java +++ b/src/com/google/dart/compiler/backend/js/JsFirstExpressionVisitor.java @@ -112,9 +112,4 @@ public class JsFirstExpressionVisitor extends JsVisitor { public boolean visit(JsPrefixOperation x, JsContext ctx) { return false; } - - @Override - public boolean visit(JsRegExp x, JsContext ctx) { - return false; - } } diff --git a/src/com/google/dart/compiler/backend/js/JsPrecedenceVisitor.java b/src/com/google/dart/compiler/backend/js/JsPrecedenceVisitor.java index bd611321d25..80698165d3c 100644 --- a/src/com/google/dart/compiler/backend/js/JsPrecedenceVisitor.java +++ b/src/com/google/dart/compiler/backend/js/JsPrecedenceVisitor.java @@ -10,271 +10,267 @@ import com.google.dart.compiler.backend.js.ast.JsVars.JsVar; /** * Precedence indices from "JavaScript - The Definitive Guide" 4th Edition (page * 57) - * + *

* Precedence 17 is for indivisible primaries that either don't have children, * or provide their own delimiters. - * + *

* Precedence 16 is for really important things that have their own AST classes. - * + *

* Precedence 15 is for the new construct. - * + *

* Precedence 14 is for unary operators. - * + *

* Precedences 12 through 4 are for non-assigning binary operators. - * + *

* Precedence 3 is for the tertiary conditional. - * + *

* Precedence 2 is for assignments. - * + *

* Precedence 1 is for comma operations. */ class JsPrecedenceVisitor extends JsVisitor { - static final int PRECEDENCE_NEW = 15; + static final int PRECEDENCE_NEW = 15; - public static int exec(JsExpression expression) { - JsPrecedenceVisitor visitor = new JsPrecedenceVisitor(); - visitor.accept(expression); - if (visitor.answer < 0) { - throw new RuntimeException("Precedence must be >= 0!"); + public static int exec(JsExpression expression) { + JsPrecedenceVisitor visitor = new JsPrecedenceVisitor(); + visitor.accept(expression); + if (visitor.answer < 0) { + throw new RuntimeException("Precedence must be >= 0!"); + } + return visitor.answer; } - return visitor.answer; - } - private int answer = -1; + private int answer = -1; - private JsPrecedenceVisitor() { - } - - @Override - public boolean visit(JsArrayAccess x, JsContext ctx) { - answer = 16; - return false; - } - - @Override - public boolean visit(JsArrayLiteral x, JsContext ctx) { - answer = 17; // primary - return false; - } - - @Override - public boolean visit(JsBinaryOperation x, JsContext ctx) { - answer = x.getOperator().getPrecedence(); - return false; - } - - @Override - public boolean visit(JsBlock x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } - - @Override - public boolean visit(JsLiteral.JsBooleanLiteral x, JsContext ctx) { - answer = 17; // primary - return false; - } - - @Override - public boolean visit(JsBreak x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } - - @Override - public boolean visit(JsCase x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } - - @Override - public boolean visit(JsCatch x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } - - @Override - public boolean visit(JsConditional x, JsContext ctx) { - answer = 3; - return false; - } - - @Override - public boolean visit(JsContinue x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } - - @Override - public boolean visit(JsDebugger x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } - - @Override - public boolean visit(JsDefault x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } - - @Override - public boolean visit(JsDoWhile x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } - - @Override - public boolean visit(JsEmpty x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } - - @Override - public boolean visit(JsExpressionStatement x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } - - @Override - public boolean visit(JsFor x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } - - @Override - public boolean visit(JsForIn x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } - - @Override - public boolean visit(JsFunction x, JsContext ctx) { - answer = 17; // primary - return false; - } - - @Override - public boolean visit(JsIf x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } - - @Override - public boolean visit(JsInvocation x, JsContext ctx) { - answer = 16; - return false; - } - - @Override - public boolean visit(JsLabel x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } - - @Override - public boolean visit(JsNameRef x, JsContext ctx) { - if (x.isLeaf()) { - answer = 17; // primary - } else { - answer = 16; // property access + private JsPrecedenceVisitor() { } - return false; - } - - @Override - public boolean visit(JsNew x, JsContext ctx) { - answer = PRECEDENCE_NEW; - return false; - } - - @Override - public boolean visit(JsNullLiteral x, JsContext ctx) { - answer = 17; // primary - return false; - } @Override - public boolean visit(JsNumberLiteral.JsIntLiteral x, JsContext ctx) { + public boolean visit(JsArrayAccess x, JsContext ctx) { + answer = 16; + return false; + } + + @Override + public boolean visit(JsArrayLiteral x, JsContext ctx) { answer = 17; // primary return false; } @Override - public boolean visit(JsNumberLiteral.JsDoubleLiteral x, JsContext ctx) { + public boolean visit(JsBinaryOperation x, JsContext ctx) { + answer = x.getOperator().getPrecedence(); + return false; + } + + @Override + public boolean visit(JsBlock x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } + + @Override + public boolean visit(JsLiteral.JsBooleanLiteral x, JsContext ctx) { answer = 17; // primary return false; } - @Override - public boolean visit(JsObjectLiteral x, JsContext ctx) { - answer = 17; // primary - return false; - } + @Override + public boolean visit(JsBreak x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } - @Override - public boolean visit(JsParameter x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } + @Override + public boolean visit(JsCase x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } - @Override - public boolean visit(JsPostfixOperation x, JsContext ctx) { - answer = x.getOperator().getPrecedence(); - return false; - } + @Override + public boolean visit(JsCatch x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } - @Override - public boolean visit(JsPrefixOperation x, JsContext ctx) { - answer = x.getOperator().getPrecedence(); - return false; - } + @Override + public boolean visit(JsConditional x, JsContext ctx) { + answer = 3; + return false; + } - @Override - public boolean visit(JsProgram x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } + @Override + public boolean visit(JsContinue x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } - @Override - public boolean visit(JsPropertyInitializer x, JsContext ctx) { - answer = 17; // primary - return false; - } + @Override + public boolean visit(JsDebugger x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } - @Override - public boolean visit(JsRegExp x, JsContext ctx) { - answer = 17; // primary - return false; - } + @Override + public boolean visit(JsDefault x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } - @Override - public boolean visit(JsReturn x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } + @Override + public boolean visit(JsDoWhile x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } - @Override - public boolean visit(JsStringLiteral x, JsContext ctx) { - answer = 17; // primary - return false; - } + @Override + public void visit(JsEmpty x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } - @Override - public boolean visit(JsSwitch x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } + @Override + public boolean visit(JsExpressionStatement x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } - @Override - public boolean visit(JsLiteral.JsThisRef x, JsContext ctx) { - answer = 17; // primary - return false; - } + @Override + public boolean visit(JsFor x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } - @Override - public boolean visit(JsThrow x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } + @Override + public boolean visit(JsForIn x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } - @Override - public boolean visit(JsTry x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } + @Override + public boolean visit(JsFunction x, JsContext ctx) { + answer = 17; // primary + return false; + } - @Override - public boolean visit(JsVar x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } + @Override + public boolean visit(JsIf x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } - @Override - public boolean visit(JsVars x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } + @Override + public boolean visit(JsInvocation x, JsContext ctx) { + answer = 16; + return false; + } - @Override - public boolean visit(JsWhile x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } + @Override + public boolean visit(JsLabel x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } + + @Override + public boolean visit(JsNameRef x, JsContext ctx) { + if (x.isLeaf()) { + answer = 17; // primary + } + else { + answer = 16; // property access + } + return false; + } + + @Override + public boolean visit(JsNew x, JsContext ctx) { + answer = PRECEDENCE_NEW; + return false; + } + + @Override + public void visit(JsNullLiteral x, JsContext ctx) { + answer = 17; // primary + } + + @Override + public void visit(JsNumberLiteral.JsIntLiteral x, JsContext ctx) { + answer = 17; // primary + } + + @Override + public void visit(JsNumberLiteral.JsDoubleLiteral x, JsContext ctx) { + answer = 17; // primary + } + + @Override + public boolean visit(JsObjectLiteral x, JsContext ctx) { + answer = 17; // primary + return false; + } + + @Override + public boolean visit(JsParameter x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } + + @Override + public boolean visit(JsPostfixOperation x, JsContext ctx) { + answer = x.getOperator().getPrecedence(); + return false; + } + + @Override + public boolean visit(JsPrefixOperation x, JsContext ctx) { + answer = x.getOperator().getPrecedence(); + return false; + } + + @Override + public boolean visit(JsProgram x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } + + @Override + public boolean visit(JsPropertyInitializer x, JsContext ctx) { + answer = 17; // primary + return false; + } + + @Override + public void visit(JsRegExp x, JsContext ctx) { + answer = 17; // primary + } + + @Override + public boolean visit(JsReturn x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } + + @Override + public void visit(JsStringLiteral x, JsContext ctx) { + answer = 17; // primary + } + + @Override + public boolean visit(JsSwitch x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } + + @Override + public boolean visit(JsLiteral.JsThisRef x, JsContext ctx) { + answer = 17; // primary + return false; + } + + @Override + public boolean visit(JsThrow x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } + + @Override + public boolean visit(JsTry x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } + + @Override + public boolean visit(JsVar x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } + + @Override + public boolean visit(JsVars x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } + + @Override + public boolean visit(JsWhile x, JsContext ctx) { + throw new RuntimeException("Only expressions have precedence."); + } } diff --git a/src/com/google/dart/compiler/backend/js/JsRequiresSemiVisitor.java b/src/com/google/dart/compiler/backend/js/JsRequiresSemiVisitor.java index b2205d3160b..7bc6e95da5a 100644 --- a/src/com/google/dart/compiler/backend/js/JsRequiresSemiVisitor.java +++ b/src/com/google/dart/compiler/backend/js/JsRequiresSemiVisitor.java @@ -55,11 +55,6 @@ public class JsRequiresSemiVisitor extends JsVisitor { return false; } - @Override - public boolean visit(JsEmpty x, JsContext ctx) { - return false; - } - @Override public boolean visit(JsExpressionStatement x, JsContext ctx) { return false; diff --git a/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java b/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java index 6476775a129..b4c0bb5cf4d 100644 --- a/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java +++ b/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java @@ -323,7 +323,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { public boolean visit(JsCase x, JsContext ctx) { p.print(CHARS_CASE); space(); - accept(x.getCaseExpr()); + accept(x.getCaseExpression()); _colon(); newlineOpt(); @@ -450,8 +450,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public boolean visit(JsEmpty x, JsContext ctx) { - return false; + public void visit(JsEmpty x, JsContext ctx) { } @Override @@ -650,12 +649,12 @@ public class JsToStringGenerationVisitor extends JsVisitor { p.print(CHARS_NEW); space(); - JsExpression ctorExpr = x.getConstructorExpression(); - boolean needsParens = JsConstructExpressionVisitor.exec(ctorExpr); + JsExpression constructorExpression = x.getConstructorExpression(); + boolean needsParens = JsConstructExpressionVisitor.exec(constructorExpression); if (needsParens) { leftParen(); } - accept(ctorExpr); + accept(constructorExpression); if (needsParens) { rightParen(); } @@ -668,21 +667,18 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public boolean visit(JsNullLiteral x, JsContext ctx) { + public void visit(JsNullLiteral x, JsContext ctx) { p.print(CHARS_NULL); - return false; } @Override - public boolean visit(JsIntLiteral x, JsContext ctx) { + public void visit(JsIntLiteral x, JsContext ctx) { p.print(x.value); - return false; } @Override - public boolean visit(JsDoubleLiteral x, JsContext ctx) { + public void visit(JsDoubleLiteral x, JsContext ctx) { p.print(x.value); - return false; } @Override @@ -788,7 +784,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public boolean visit(JsRegExp x, JsContext ctx) { + public void visit(JsRegExp x, JsContext ctx) { _slash(); p.print(x.getPattern()); _slash(); @@ -796,13 +792,12 @@ public class JsToStringGenerationVisitor extends JsVisitor { if (flags != null) { p.print(flags); } - return false; } @Override public boolean visit(JsReturn x, JsContext ctx) { p.print(CHARS_RETURN); - JsExpression expr = x.getExpr(); + JsExpression expr = x.getExpression(); if (expr != null) { space(); accept(expr); @@ -811,9 +806,8 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public boolean visit(JsStringLiteral x, JsContext ctx) { + public void visit(JsStringLiteral x, JsContext ctx) { p.print(javaScriptString(x.getValue())); - return false; } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/ChameleonJsExpression.java b/src/com/google/dart/compiler/backend/js/ast/ChameleonJsExpression.java index 65047589936..d926ef6ec46 100644 --- a/src/com/google/dart/compiler/backend/js/ast/ChameleonJsExpression.java +++ b/src/com/google/dart/compiler/backend/js/ast/ChameleonJsExpression.java @@ -1,7 +1,5 @@ package com.google.dart.compiler.backend.js.ast; -import com.google.dart.compiler.common.SourceInfo; - public class ChameleonJsExpression implements JsExpression { private JsExpression expression; @@ -47,8 +45,13 @@ public class ChameleonJsExpression implements JsExpression { } @Override - public void traverse(JsVisitor visitor, JsContext context) { - expression.traverse(visitor, context); + public void accept(JsVisitor visitor, JsContext context) { + expression.accept(visitor, context); + } + + @Override + public void acceptChildren(JsVisitor visitor, JsContext context) { + expression.acceptChildren(visitor, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/HasArguments.java b/src/com/google/dart/compiler/backend/js/ast/HasArguments.java index 814189f2846..29b12859174 100644 --- a/src/com/google/dart/compiler/backend/js/ast/HasArguments.java +++ b/src/com/google/dart/compiler/backend/js/ast/HasArguments.java @@ -10,6 +10,5 @@ import java.util.List; * Implemented by JavaScript objects that accept arguments. */ public interface HasArguments extends JsExpression { - List getArguments(); } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsArrayAccess.java b/src/com/google/dart/compiler/backend/js/ast/JsArrayAccess.java index ec08121c755..3e668bf3fe1 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsArrayAccess.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsArrayAccess.java @@ -8,60 +8,62 @@ package com.google.dart.compiler.backend.js.ast; * Represents a javascript expression for array access. */ public final class JsArrayAccess extends JsExpressionImpl { - private JsExpression arrayExpression; - private JsExpression indexExpression; + private JsExpression arrayExpression; + private JsExpression indexExpression; - public JsArrayAccess() { - super(); - } - - public JsArrayAccess(JsExpression arrayExpression, JsExpression indexExpression) { - this.arrayExpression = arrayExpression; - this.indexExpression = indexExpression; - } - - public JsExpression getArrayExpression() { - return arrayExpression; - } - - public JsExpression getIndexExpression() { - return indexExpression; - } - - @Override - public boolean hasSideEffects() { - return arrayExpression.hasSideEffects() || indexExpression.hasSideEffects(); - } - - @Override - public boolean isDefinitelyNotNull() { - return false; - } - - @Override - public boolean isDefinitelyNull() { - return false; - } - - public void setArrayExpression(JsExpression arrayExpression) { - this.arrayExpression = arrayExpression; - } - - public void setIndexExpression(JsExpression indexExpression) { - this.indexExpression = indexExpression; - } - - @Override - public void traverse(JsVisitor v, JsContext context) { - if (v.visit(this, context)) { - arrayExpression = v.accept(arrayExpression); - indexExpression = v.accept(indexExpression); + public JsArrayAccess() { + super(); } - v.endVisit(this, context); - } - @Override - public NodeKind getKind() { - return NodeKind.ARRAY_ACCESS; - } + public JsArrayAccess(JsExpression arrayExpression, JsExpression indexExpression) { + this.arrayExpression = arrayExpression; + this.indexExpression = indexExpression; + } + + public JsExpression getArrayExpression() { + return arrayExpression; + } + + public JsExpression getIndexExpression() { + return indexExpression; + } + + @Override + public boolean hasSideEffects() { + return arrayExpression.hasSideEffects() || indexExpression.hasSideEffects(); + } + + @Override + public boolean isDefinitelyNotNull() { + return false; + } + + @Override + public boolean isDefinitelyNull() { + return false; + } + + public void setArrayExpression(JsExpression arrayExpression) { + this.arrayExpression = arrayExpression; + } + + public void setIndexExpression(JsExpression indexExpression) { + this.indexExpression = indexExpression; + } + + @Override + public void accept(JsVisitor v, JsContext context) { + v.visit(this, context); + } + + @Override + public void acceptChildren(JsVisitor visitor, JsContext context) { + arrayExpression = visitor.accept(arrayExpression); + indexExpression = visitor.accept(indexExpression); + } + + @Override + public NodeKind getKind() { + return NodeKind.ARRAY_ACCESS; + } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsArrayLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsArrayLiteral.java index 5c1a8bcdb00..50603f0bfbc 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsArrayLiteral.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsArrayLiteral.java @@ -57,11 +57,13 @@ public final class JsArrayLiteral extends JsLiteral { } @Override - public void traverse(JsVisitor v, JsContext context) { - if (v.visit(this, context)) { - v.acceptWithInsertRemove(expressions); - } - v.endVisit(this, context); + public void accept(JsVisitor v, JsContext context) { + v.visit(this, context); + } + + @Override + public void acceptChildren(JsVisitor visitor, JsContext context) { + visitor.acceptWithInsertRemove(expressions); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsBinaryOperation.java b/src/com/google/dart/compiler/backend/js/ast/JsBinaryOperation.java index 9b6327f74c0..aea44e2e35c 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsBinaryOperation.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsBinaryOperation.java @@ -10,91 +10,82 @@ import org.jetbrains.annotations.Nullable; * Represents a JavaScript binary operation. */ public final class JsBinaryOperation extends JsExpressionImpl { - private JsExpression arg1; - private JsExpression arg2; - private final JsBinaryOperator op; + private JsExpression arg1; + private JsExpression arg2; + private final JsBinaryOperator op; - public JsBinaryOperation(JsBinaryOperator op) { - this(op, null, null); - } - - public JsBinaryOperation(JsBinaryOperator op, @Nullable JsExpression arg1, @Nullable JsExpression arg2) { - this.op = op; - this.arg1 = arg1; - this.arg2 = arg2; - } - - public JsExpression getArg1() { - return arg1; - } - - public JsExpression getArg2() { - return arg2; - } - - public JsBinaryOperator getOperator() { - return op; - } - - @Override - public boolean hasSideEffects() { - return op.isAssignment() || arg1.hasSideEffects() || arg2.hasSideEffects(); - } - - @Override - public boolean isDefinitelyNotNull() { - // Precarious coding, but none of these can have null results. - if (op.getPrecedence() > 5) { - return true; + public JsBinaryOperation(JsBinaryOperator op) { + this(op, null, null); } - if (op == JsBinaryOperator.OR) { - if (arg1 instanceof CanBooleanEval) { - if (((CanBooleanEval) arg1).isBooleanTrue()) { - assert arg1.isDefinitelyNotNull(); - return true; + + public JsBinaryOperation(JsBinaryOperator op, @Nullable JsExpression arg1, @Nullable JsExpression arg2) { + this.op = op; + this.arg1 = arg1; + this.arg2 = arg2; + } + + public JsExpression getArg1() { + return arg1; + } + + public JsExpression getArg2() { + return arg2; + } + + public JsBinaryOperator getOperator() { + return op; + } + + @Override + public boolean hasSideEffects() { + return op.isAssignment() || arg1.hasSideEffects() || arg2.hasSideEffects(); + } + + @Override + public boolean isDefinitelyNotNull() { + // Precarious coding, but none of these can have null results. + if (op.getPrecedence() > 5) { + return true; } - } - } - // AND and OR can return nulls - if (op.isAssignment()) { - if (op == JsBinaryOperator.ASG) { - return arg2.isDefinitelyNotNull(); - } else { - // All other ASG's are math ops. - return true; - } + if (op == JsBinaryOperator.OR) { + if (arg1 instanceof CanBooleanEval) { + if (((CanBooleanEval) arg1).isBooleanTrue()) { + assert arg1.isDefinitelyNotNull(); + return true; + } + } + } + // AND and OR can return nulls + if (op.isAssignment()) { + return op != JsBinaryOperator.ASG || arg2.isDefinitelyNotNull(); + } + + return op == JsBinaryOperator.COMMA && arg2.isDefinitelyNotNull(); } - if (op == JsBinaryOperator.COMMA) { - return arg2.isDefinitelyNotNull(); + @Override + public boolean isDefinitelyNull() { + return op == JsBinaryOperator.AND && arg1.isDefinitelyNull(); } - return false; - } - - @Override - public boolean isDefinitelyNull() { - if (op == JsBinaryOperator.AND) { - return arg1.isDefinitelyNull(); + @Override + public void accept(JsVisitor v, JsContext context) { + v.visit(this, context); } - return false; - } - @Override - public void traverse(JsVisitor v, JsContext context) { - if (v.visit(this, context)) { - if (op.isAssignment()) { - arg1 = v.acceptLvalue(arg1); - } else { - arg1 = v.accept(arg1); - } - arg2 = v.accept(arg2); + @Override + public void acceptChildren(JsVisitor visitor, JsContext context) { + if (op.isAssignment()) { + arg1 = visitor.acceptLvalue(arg1); + } + else { + arg1 = visitor.accept(arg1); + } + arg2 = visitor.accept(arg2); } - v.endVisit(this, context); - } - @Override - public NodeKind getKind() { - return NodeKind.BINARY_OP; - } + @Override + public NodeKind getKind() { + return NodeKind.BINARY_OP; + } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsBlock.java b/src/com/google/dart/compiler/backend/js/ast/JsBlock.java index 633d88f72b2..7b6874df47c 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsBlock.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsBlock.java @@ -44,11 +44,13 @@ public class JsBlock extends JsNodeImpl implements JsStatement { } @Override - public void traverse(JsVisitor v, JsContext context) { - if (v.visit(this, context)) { - v.acceptWithInsertRemove(statements); - } - v.endVisit(this, context); + public void accept(JsVisitor v, JsContext context) { + v.visit(this, context); + } + + @Override + public void acceptChildren(JsVisitor visitor, JsContext context) { + visitor.acceptWithInsertRemove(statements); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsBreak.java b/src/com/google/dart/compiler/backend/js/ast/JsBreak.java index 50a23c051e2..8932788e3fe 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsBreak.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsBreak.java @@ -22,8 +22,7 @@ public final class JsBreak extends JsContinue { } @Override - public void traverse(JsVisitor v, JsContext context) { + public void accept(JsVisitor v, JsContext context) { v.visit(this, context); - v.endVisit(this, context); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsCase.java b/src/com/google/dart/compiler/backend/js/ast/JsCase.java index e1598a509d6..feaaa5d847c 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsCase.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsCase.java @@ -8,32 +8,33 @@ package com.google.dart.compiler.backend.js.ast; * Represents the JavaScript case statement. */ public final class JsCase extends JsSwitchMember { + private JsExpression caseExpression; - private JsExpression caseExpr; - - public JsCase() { - super(); - } - - public JsExpression getCaseExpr() { - return caseExpr; - } - - public void setCaseExpr(JsExpression caseExpr) { - this.caseExpr = caseExpr; - } - - @Override - public void traverse(JsVisitor v, JsContext context) { - if (v.visit(this, context)) { - caseExpr = v.accept(caseExpr); - v.acceptWithInsertRemove(statements); + public JsCase() { + super(); } - v.endVisit(this, context); - } - @Override - public NodeKind getKind() { - return NodeKind.CASE; - } + public JsExpression getCaseExpression() { + return caseExpression; + } + + public void setCaseExpression(JsExpression caseExpression) { + this.caseExpression = caseExpression; + } + + @Override + public void accept(JsVisitor v, JsContext context) { + v.visit(this, context); + } + + @Override + public void acceptChildren(JsVisitor visitor, JsContext context) { + caseExpression = visitor.accept(caseExpression); + super.acceptChildren(visitor, context); + } + + @Override + public NodeKind getKind() { + return NodeKind.CASE; + } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsCatch.java b/src/com/google/dart/compiler/backend/js/ast/JsCatch.java index 1d3846fa6f0..24a729051f8 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsCatch.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsCatch.java @@ -9,58 +9,60 @@ package com.google.dart.compiler.backend.js.ast; */ public class JsCatch extends JsNodeImpl implements HasCondition { - protected final JsCatchScope scope; - private JsBlock body; - private JsExpression condition; - private JsParameter param; + protected final JsCatchScope scope; + private JsBlock body; + private JsExpression condition; + private JsParameter param; - public JsCatch(JsScope parent, String ident) { - super(); - assert (parent != null); - scope = new JsCatchScope(parent, ident); - param = new JsParameter(scope.findName(ident)); - } - - public JsBlock getBody() { - return body; - } - - @Override - public JsExpression getCondition() { - return condition; - } - - public JsParameter getParameter() { - return param; - } - - public JsScope getScope() { - return scope; - } - - public void setBody(JsBlock body) { - this.body = body; - } - - @Override - public void setCondition(JsExpression condition) { - this.condition = condition; - } - - @Override - public void traverse(JsVisitor v, JsContext context) { - if (v.visit(this, context)) { - param = v.accept(param); - if (condition != null) { - condition = v.accept(condition); - } - body = v.accept(body); + public JsCatch(JsScope parent, String ident) { + super(); + assert (parent != null); + scope = new JsCatchScope(parent, ident); + param = new JsParameter(scope.findName(ident)); } - v.endVisit(this, context); - } - @Override - public NodeKind getKind() { - return NodeKind.CATCH; - } + public JsBlock getBody() { + return body; + } + + @Override + public JsExpression getCondition() { + return condition; + } + + public JsParameter getParameter() { + return param; + } + + public JsScope getScope() { + return scope; + } + + public void setBody(JsBlock body) { + this.body = body; + } + + @Override + public void setCondition(JsExpression condition) { + this.condition = condition; + } + + @Override + public void accept(JsVisitor v, JsContext context) { + v.visit(this, context); + } + + @Override + public void acceptChildren(JsVisitor visitor, JsContext context) { + param = visitor.accept(param); + if (condition != null) { + condition = visitor.accept(condition); + } + body = visitor.accept(body); + } + + @Override + public NodeKind getKind() { + return NodeKind.CATCH; + } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsConditional.java b/src/com/google/dart/compiler/backend/js/ast/JsConditional.java index 2bf2dda635e..7c82a97726d 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsConditional.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsConditional.java @@ -61,13 +61,15 @@ public final class JsConditional extends JsExpressionImpl { } @Override - public void traverse(JsVisitor v, JsContext context) { - if (v.visit(this, context)) { - testExpr = v.accept(testExpr); - thenExpr = v.accept(thenExpr); - elseExpr = v.accept(elseExpr); - } - v.endVisit(this, context); + public void accept(JsVisitor v, JsContext context) { + v.visit(this, context); + } + + @Override + public void acceptChildren(JsVisitor visitor, JsContext context) { + testExpr = visitor.accept(testExpr); + thenExpr = visitor.accept(thenExpr); + elseExpr = visitor.accept(elseExpr); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsContext.java b/src/com/google/dart/compiler/backend/js/ast/JsContext.java index 15371c8ec41..35b8c74769e 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsContext.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsContext.java @@ -14,13 +14,13 @@ public interface JsContext { boolean canRemove(); - void insertAfter(JsVisitable node); + void insertAfter(JsNode node); - void insertBefore(JsVisitable node); + void insertBefore(JsNode node); boolean isLvalue(); void removeMe(); - void replaceMe(JsVisitable node); + void replaceMe(JsNode node); } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsContinue.java b/src/com/google/dart/compiler/backend/js/ast/JsContinue.java index dcec38018f7..bd7aebbf5ca 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsContinue.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsContinue.java @@ -6,9 +6,6 @@ package com.google.dart.compiler.backend.js.ast; import org.jetbrains.annotations.Nullable; -/** - * Represents the JavaScript continue statement. - */ public class JsContinue extends JsNodeImpl implements JsStatement { protected final String label; @@ -26,9 +23,13 @@ public class JsContinue extends JsNodeImpl implements JsStatement { } @Override - public void traverse(JsVisitor v, JsContext context) { + public void accept(JsVisitor v, JsContext context) { v.visit(this, context); - v.endVisit(this, context); + } + + @Override + public void acceptChildren(JsVisitor visitor, JsContext context) { + } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsDebugger.java b/src/com/google/dart/compiler/backend/js/ast/JsDebugger.java index 45db05149a8..6d48cef820e 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsDebugger.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsDebugger.java @@ -13,12 +13,16 @@ public class JsDebugger extends JsNodeImpl implements JsStatement { } @Override - public void traverse(JsVisitor v, JsContext context) { + public void accept(JsVisitor v, JsContext context) { v.visit(this, context); - v.endVisit(this, context); } - @Override + @Override + public void acceptChildren(JsVisitor visitor, JsContext context) { + + } + + @Override public NodeKind getKind() { return NodeKind.DEBUGGER; } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsDefault.java b/src/com/google/dart/compiler/backend/js/ast/JsDefault.java index b59dca783a9..227228df2e1 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsDefault.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsDefault.java @@ -8,17 +8,9 @@ package com.google.dart.compiler.backend.js.ast; * Represents the default option in a JavaScript swtich statement. */ public final class JsDefault extends JsSwitchMember { - - public JsDefault() { - super(); - } - @Override - public void traverse(JsVisitor v, JsContext context) { - if (v.visit(this, context)) { - v.acceptWithInsertRemove(statements); - } - v.endVisit(this, context); + public void accept(JsVisitor v, JsContext context) { + v.visit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsDoWhile.java b/src/com/google/dart/compiler/backend/js/ast/JsDoWhile.java index 10253c39b55..e860a699f8b 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsDoWhile.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsDoWhile.java @@ -21,11 +21,10 @@ public class JsDoWhile extends JsWhile { } @Override - public void traverse(JsVisitor v, JsContext context) { + public void accept(JsVisitor v, JsContext context) { if (v.visit(this, context)) { condition = v.accept(condition); body = v.accept(body); } - v.endVisit(this, context); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsDocComment.java b/src/com/google/dart/compiler/backend/js/ast/JsDocComment.java index 909fd225846..b432b45a6d1 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsDocComment.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsDocComment.java @@ -28,10 +28,15 @@ public class JsDocComment extends JsExpressionImpl { } @Override - public void traverse(JsVisitor v, JsContext context) { + public void accept(JsVisitor v, JsContext context) { v.visit(this, context); } + @Override + public void acceptChildren(JsVisitor visitor, JsContext context) { + + } + @Override public boolean hasSideEffects() { return false; diff --git a/src/com/google/dart/compiler/backend/js/ast/JsEmpty.java b/src/com/google/dart/compiler/backend/js/ast/JsEmpty.java index 3ecc8f1d3bc..31ab7908cd0 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsEmpty.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsEmpty.java @@ -4,22 +4,21 @@ package com.google.dart.compiler.backend.js.ast; -/** - * Represents an empty statement in JavaScript. - */ public class JsEmpty extends JsNodeImpl implements JsStatement { - // Interned by JsProgram - JsEmpty() { - } + JsEmpty() { + } - @Override - public void traverse(JsVisitor v, JsContext context) { - v.visit(this, context); - v.endVisit(this, context); - } + @Override + public void accept(JsVisitor v, JsContext context) { + v.visit(this, context); + } - @Override - public NodeKind getKind() { - return NodeKind.EMPTY; - } + @Override + public void acceptChildren(JsVisitor visitor, JsContext context) { + } + + @Override + public NodeKind getKind() { + return NodeKind.EMPTY; + } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsExpression.java b/src/com/google/dart/compiler/backend/js/ast/JsExpression.java index def914d6a70..5cbc2bd4284 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsExpression.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsExpression.java @@ -2,7 +2,7 @@ package com.google.dart.compiler.backend.js.ast; import com.google.dart.compiler.common.HasSourceInfo; -public interface JsExpression extends JsNode, HasSourceInfo, JsVisitable { +public interface JsExpression extends JsNode, HasSourceInfo { /** * Determines whether the expression can cause side effects. */ diff --git a/src/com/google/dart/compiler/backend/js/ast/JsExpressionImpl.java b/src/com/google/dart/compiler/backend/js/ast/JsExpressionImpl.java index 2e46db4bef9..8ae907a92ad 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsExpressionImpl.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsExpressionImpl.java @@ -4,6 +4,8 @@ package com.google.dart.compiler.backend.js.ast; +import java.util.List; + abstract class JsExpressionImpl extends JsNodeImpl implements JsExpression { protected JsExpressionImpl() { } @@ -24,4 +26,17 @@ abstract class JsExpressionImpl extends JsNodeImpl implements JsExpression { public JsStatement makeStmt() { return new JsExpressionStatement(this); } + + protected abstract static class JsExpressionHasArguments extends JsExpressionImpl implements HasArguments { + protected final List arguments; + + public JsExpressionHasArguments(List arguments) { + this.arguments = arguments; + } + + @Override + public List getArguments() { + return arguments; + } + } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsExpressionStatement.java b/src/com/google/dart/compiler/backend/js/ast/JsExpressionStatement.java index ba5a1b6f26f..deb413ee43d 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsExpressionStatement.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsExpressionStatement.java @@ -16,11 +16,13 @@ public final class JsExpressionStatement extends AbstractNode implements JsState } @Override - public void traverse(JsVisitor v, JsContext context) { - if (v.visit(this, context)) { - expression = v.accept(expression); - } - v.endVisit(this, context); + public void accept(JsVisitor v, JsContext context) { + v.visit(this, context); + } + + @Override + public void acceptChildren(JsVisitor visitor, JsContext context) { + expression = visitor.accept(expression); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsFor.java b/src/com/google/dart/compiler/backend/js/ast/JsFor.java index 2eb846e6b6f..d76f7ec50f9 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsFor.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsFor.java @@ -71,27 +71,29 @@ public class JsFor extends JsNodeImpl implements JsStatement { } @Override - public void traverse(JsVisitor v, JsContext context) { - if (v.visit(this, context)) { - assert (!(initExpression != null && initVars != null)); + public void accept(JsVisitor v, JsContext context) { + v.visit(this, context); + } - if (initExpression != null) { - initExpression = v.accept(initExpression); - } - else if (initVars != null) { - initVars = v.accept(initVars); - } + @Override + public void acceptChildren(JsVisitor visitor, JsContext context) { + assert (!(initExpression != null && initVars != null)); - if (condition != null) { - condition = v.accept(condition); - } - - if (incrementExpression != null) { - incrementExpression = v.accept(incrementExpression); - } - body = v.accept(body); + if (initExpression != null) { + initExpression = visitor.accept(initExpression); } - v.endVisit(this, context); + else if (initVars != null) { + initVars = visitor.accept(initVars); + } + + if (condition != null) { + condition = visitor.accept(condition); + } + + if (incrementExpression != null) { + incrementExpression = visitor.accept(incrementExpression); + } + body = visitor.accept(body); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsForIn.java b/src/com/google/dart/compiler/backend/js/ast/JsForIn.java index a0e31f4af5b..7fa3edd4fa9 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsForIn.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsForIn.java @@ -8,64 +8,67 @@ package com.google.dart.compiler.backend.js.ast; * Represents a JavaScript for..in statement. */ public class JsForIn extends JsNodeImpl implements JsStatement { + private JsStatement body; + private JsExpression iterExpr; + private JsExpression objExpr; - private JsStatement body; - private JsExpression iterExpr; - private JsExpression objExpr; + // Optional: the name of a new iterator variable to introduce + private final JsName iterVarName; - // Optional: the name of a new iterator variable to introduce - private final JsName iterVarName; - - public JsForIn() { - this(null); - } - - public JsForIn(JsName iterVarName) { - this.iterVarName = iterVarName; - } - - public JsStatement getBody() { - return body; - } - - public JsExpression getIterExpr() { - return iterExpr; - } - - public JsName getIterVarName() { - return iterVarName; - } - - public JsExpression getObjExpr() { - return objExpr; - } - - public void setBody(JsStatement body) { - this.body = body; - } - - public void setIterExpr(JsExpression iterExpr) { - this.iterExpr = iterExpr; - } - - public void setObjExpr(JsExpression objExpr) { - this.objExpr = objExpr; - } - - @Override - public void traverse(JsVisitor v, JsContext context) { - if (v.visit(this, context)) { - if (iterExpr != null) { - iterExpr = v.acceptLvalue(iterExpr); - } - objExpr = v.accept(objExpr); - body = v.accept(body); + public JsForIn() { + this(null); } - v.endVisit(this, context); - } - @Override - public NodeKind getKind() { - return NodeKind.FOR_IN; - } + public JsForIn(JsName iterVarName) { + this.iterVarName = iterVarName; + } + + public JsStatement getBody() { + return body; + } + + public JsExpression getIterExpr() { + return iterExpr; + } + + public JsName getIterVarName() { + return iterVarName; + } + + public JsExpression getObjExpr() { + return objExpr; + } + + public void setBody(JsStatement body) { + this.body = body; + } + + public void setIterExpr(JsExpression iterExpr) { + this.iterExpr = iterExpr; + } + + public void setObjExpr(JsExpression objExpr) { + this.objExpr = objExpr; + } + + @Override + public void accept(JsVisitor v, JsContext context) { + if (v.visit(this, context)) { + + } + } + + @Override + public void acceptChildren(JsVisitor visitor, JsContext context) { + if (iterExpr != null) { + iterExpr = visitor.acceptLvalue(iterExpr); + } + objExpr = visitor.accept(objExpr); + body = visitor.accept(body); + } + + @Override + public NodeKind getKind() { + return NodeKind.FOR_IN; + } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsFunction.java b/src/com/google/dart/compiler/backend/js/ast/JsFunction.java index 1d4bd4da308..ae1a1ef85b2 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsFunction.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsFunction.java @@ -90,12 +90,14 @@ public final class JsFunction extends JsLiteral implements HasName { } @Override - public void traverse(JsVisitor v, JsContext context) { - if (v.visit(this, context)) { - v.acceptWithInsertRemove(params); - body = v.accept(body); - } - v.endVisit(this, context); + public void accept(JsVisitor v, JsContext context) { + v.visit(this, context); + } + + @Override + public void acceptChildren(JsVisitor visitor, JsContext context) { + visitor.acceptWithInsertRemove(params); + body = visitor.accept(body); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsIf.java b/src/com/google/dart/compiler/backend/js/ast/JsIf.java index ad75b9b2a2d..d7e93226d57 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsIf.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsIf.java @@ -51,15 +51,17 @@ public final class JsIf extends JsNodeImpl implements JsStatement { } @Override - public void traverse(JsVisitor v, JsContext context) { - if (v.visit(this, context)) { - ifExpression = v.accept(ifExpression); - thenStatement = v.accept(thenStatement); - if (elseStatement != null) { - elseStatement = v.accept(elseStatement); - } - } - v.endVisit(this, context); + public void accept(JsVisitor v, JsContext context) { + v.visit(this, context); + } + + @Override + public void acceptChildren(JsVisitor visitor, JsContext context) { + ifExpression = visitor.accept(ifExpression); + thenStatement = visitor.accept(thenStatement); + if (elseStatement != null) { + elseStatement = visitor.accept(elseStatement); + } } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsInvocation.java b/src/com/google/dart/compiler/backend/js/ast/JsInvocation.java index 96b9f8e5cec..3c9bc107e87 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsInvocation.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsInvocation.java @@ -13,17 +13,16 @@ import java.util.List; /** * Represents a JavaScript invocation. */ -public final class JsInvocation extends JsExpressionImpl implements HasArguments { - private final List arguments; +public final class JsInvocation extends JsExpressionImpl.JsExpressionHasArguments { private JsExpression qualifier; public JsInvocation() { - arguments = new SmartList(); + super(new SmartList()); } public JsInvocation(JsExpression qualifier, List arguments) { + super(arguments); this.qualifier = qualifier; - this.arguments = arguments; } public JsInvocation(JsExpression qualifier, JsExpression arg) { @@ -68,12 +67,14 @@ public final class JsInvocation extends JsExpressionImpl implements HasArguments } @Override - public void traverse(JsVisitor v, JsContext context) { - if (v.visit(this, context)) { - qualifier = v.accept(qualifier); - v.acceptList(arguments); - } - v.endVisit(this, context); + public void accept(JsVisitor v, JsContext context) { + v.visit(this, context); + } + + @Override + public void acceptChildren(JsVisitor visitor, JsContext context) { + qualifier = visitor.accept(qualifier); + visitor.acceptList(arguments); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsLabel.java b/src/com/google/dart/compiler/backend/js/ast/JsLabel.java index 3dbe28bc361..b4dd4ca2355 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsLabel.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsLabel.java @@ -42,12 +42,14 @@ public class JsLabel extends JsNodeImpl implements JsStatement, HasName { } @Override - public void traverse(JsVisitor v, JsContext context) { - if (v.visit(this, context)) { - statement = v.accept(statement); - } - v.endVisit(this, context); + public void accept(JsVisitor v, JsContext context) { + v.visit(this, context); } + + @Override + public void acceptChildren(JsVisitor visitor, JsContext context) { + statement = visitor.accept(statement); + } @Override public NodeKind getKind() { diff --git a/src/com/google/dart/compiler/backend/js/ast/JsLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsLiteral.java index f8cc734d453..bc9eee97cd1 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsLiteral.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsLiteral.java @@ -43,9 +43,8 @@ public abstract class JsLiteral extends JsExpressionImpl implements CanBooleanEv } @Override - public void traverse(JsVisitor v, JsContext context) { + public void accept(JsVisitor v, JsContext context) { v.visit(this, context); - v.endVisit(this, context); } @Override @@ -87,9 +86,8 @@ public abstract class JsLiteral extends JsExpressionImpl implements CanBooleanEv } @Override - public void traverse(JsVisitor v, JsContext context) { + public void accept(JsVisitor v, JsContext context) { v.visit(this, context); - v.endVisit(this, context); } @Override @@ -97,4 +95,26 @@ public abstract class JsLiteral extends JsExpressionImpl implements CanBooleanEv return NodeKind.BOOLEAN; } } + + /** + * A JavaScript string literal expression. + */ + public abstract static class JsValueLiteral extends JsLiteral { + protected JsValueLiteral() { + } + + @Override + public final boolean hasSideEffects() { + return false; + } + + @Override + public final boolean isLeaf() { + return true; + } + + @Override + public void acceptChildren(JsVisitor visitor, JsContext context) { + } + } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNameRef.java b/src/com/google/dart/compiler/backend/js/ast/JsNameRef.java index 1f12ed8c628..c877630db1c 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsNameRef.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsNameRef.java @@ -56,14 +56,7 @@ public final class JsNameRef extends JsExpressionImpl implements CanBooleanEval, @Override public boolean hasSideEffects() { - if (qualifier == null) { - return false; - } - if (!qualifier.isDefinitelyNotNull()) { - // Could trigger NPE. - return true; - } - return qualifier.hasSideEffects(); + return qualifier != null && (!qualifier.isDefinitelyNotNull() || qualifier.hasSideEffects()); } @Override @@ -83,25 +76,17 @@ public final class JsNameRef extends JsExpressionImpl implements CanBooleanEval, @Override public boolean isDefinitelyNull() { - if (name != null) { - return (JsLiteral.UNDEFINED.getName() == name); - } - return false; + return name != null && (JsLiteral.UNDEFINED.getName() == name); } @Override public boolean isLeaf() { - if (qualifier == null) { - return true; - } - else { - return false; - } + return qualifier == null; } public void resolve(JsName name) { this.name = name; - this.ident = null; + ident = null; } public void setQualifier(JsExpression qualifier) { @@ -109,13 +94,15 @@ public final class JsNameRef extends JsExpressionImpl implements CanBooleanEval, } @Override - public void traverse(JsVisitor v, JsContext context) { - if (v.visit(this, context)) { - if (qualifier != null) { - qualifier = v.accept(qualifier); - } - } - v.endVisit(this, context); + public void accept(JsVisitor v, JsContext context) { + v.visit(this, context); + } + + @Override + public void acceptChildren(JsVisitor visitor, JsContext context) { + if (qualifier != null) { + qualifier = visitor.accept(qualifier); + } } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNew.java b/src/com/google/dart/compiler/backend/js/ast/JsNew.java index c65bebbde66..dac28938a62 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsNew.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsNew.java @@ -8,29 +8,20 @@ import com.intellij.util.SmartList; import java.util.List; -/** - * Represents the JavaScript new expression. - */ -public final class JsNew extends JsExpressionImpl implements HasArguments { - private final List arguments; - private JsExpression ctorExpression; +public final class JsNew extends JsExpressionImpl.JsExpressionHasArguments { + private JsExpression constructorExpression; - public JsNew(JsExpression ctorExpression) { - this(ctorExpression, new SmartList()); + public JsNew(JsExpression constructorExpression) { + this(constructorExpression, new SmartList()); } - public JsNew(JsExpression ctorExpression, List arguments) { - this.ctorExpression = ctorExpression; - this.arguments = arguments; - } - - @Override - public List getArguments() { - return arguments; + public JsNew(JsExpression constructorExpression, List arguments) { + super(arguments); + this.constructorExpression = constructorExpression; } public JsExpression getConstructorExpression() { - return ctorExpression; + return constructorExpression; } @Override @@ -51,12 +42,14 @@ public final class JsNew extends JsExpressionImpl implements HasArguments { } @Override - public void traverse(JsVisitor v, JsContext context) { - if (v.visit(this, context)) { - ctorExpression = v.accept(ctorExpression); - v.acceptList(arguments); - } - v.endVisit(this, context); + public void accept(JsVisitor v, JsContext context) { + v.visit(this, context); + } + + @Override + public void acceptChildren(JsVisitor visitor, JsContext context) { + constructorExpression = visitor.accept(constructorExpression); + visitor.acceptList(arguments); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNode.java b/src/com/google/dart/compiler/backend/js/ast/JsNode.java index 78e78af6434..83fcb376af5 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsNode.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsNode.java @@ -4,6 +4,16 @@ package com.google.dart.compiler.backend.js.ast; -public interface JsNode extends JsVisitable { +public interface JsNode { NodeKind getKind(); + + /** + * Causes this object to have the visitor visit itself and its children. + * + * @param visitor the visitor that should traverse this node + * @param context the context of an existing traversal + */ + void accept(JsVisitor visitor, JsContext context); + + void acceptChildren(JsVisitor visitor, JsContext context); } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNodeImpl.java b/src/com/google/dart/compiler/backend/js/ast/JsNodeImpl.java index 164300c68e4..b41e793dd4a 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsNodeImpl.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsNodeImpl.java @@ -1,7 +1,5 @@ package com.google.dart.compiler.backend.js.ast; -import com.google.dart.compiler.common.SourceInfo; - abstract class JsNodeImpl extends AbstractNode { private Object sourceInfo; diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNullLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsNullLiteral.java index 028760e4c0d..3974b1a5965 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsNullLiteral.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsNullLiteral.java @@ -4,7 +4,7 @@ package com.google.dart.compiler.backend.js.ast; -public final class JsNullLiteral extends JsValueLiteral { +public final class JsNullLiteral extends JsLiteral.JsValueLiteral { JsNullLiteral() { } @@ -29,9 +29,8 @@ public final class JsNullLiteral extends JsValueLiteral { } @Override - public void traverse(JsVisitor v, JsContext context) { + public void accept(JsVisitor v, JsContext context) { v.visit(this, context); - v.endVisit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNumberLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsNumberLiteral.java index 8d47eca2dcf..8df65c40b7e 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsNumberLiteral.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsNumberLiteral.java @@ -4,7 +4,7 @@ package com.google.dart.compiler.backend.js.ast; -public abstract class JsNumberLiteral extends JsValueLiteral { +public abstract class JsNumberLiteral extends JsLiteral.JsValueLiteral { @Override public boolean isDefinitelyNotNull() { return true; @@ -38,9 +38,8 @@ public abstract class JsNumberLiteral extends JsValueLiteral { } @Override - public void traverse(JsVisitor v, JsContext context) { + public void accept(JsVisitor v, JsContext context) { v.visit(this, context); - v.endVisit(this, context); } public String toString() { @@ -66,9 +65,8 @@ public abstract class JsNumberLiteral extends JsValueLiteral { } @Override - public void traverse(JsVisitor v, JsContext context) { + public void accept(JsVisitor v, JsContext context) { v.visit(this, context); - v.endVisit(this, context); } public String toString() { diff --git a/src/com/google/dart/compiler/backend/js/ast/JsObjectLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsObjectLiteral.java index f1ef5b54bfd..bbae493479a 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsObjectLiteral.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsObjectLiteral.java @@ -69,11 +69,13 @@ public final class JsObjectLiteral extends JsLiteral { } @Override - public void traverse(JsVisitor v, JsContext context) { - if (v.visit(this, context)) { - v.acceptWithInsertRemove(properties); - } - v.endVisit(this, context); + public void accept(JsVisitor v, JsContext context) { + v.visit(this, context); + } + + @Override + public void acceptChildren(JsVisitor visitor, JsContext context) { + visitor.acceptWithInsertRemove(properties); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsParameter.java b/src/com/google/dart/compiler/backend/js/ast/JsParameter.java index 323646468c6..5d87432ce7f 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsParameter.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsParameter.java @@ -10,30 +10,34 @@ import com.google.dart.compiler.common.Symbol; * A JavaScript parameter. */ public final class JsParameter extends JsNodeImpl implements HasName { - private final JsName name; + private final JsName name; - public JsParameter(JsName name) { - this.name = name; - } + public JsParameter(JsName name) { + this.name = name; + } - @Override - public JsName getName() { - return name; - } + @Override + public JsName getName() { + return name; + } - @Override - public Symbol getSymbol() { - return name; - } + @Override + public Symbol getSymbol() { + return name; + } - @Override - public void traverse(JsVisitor v, JsContext context) { - v.visit(this, context); - v.endVisit(this, context); - } + @Override + public void accept(JsVisitor v, JsContext context) { + v.visit(this, context); + } - @Override - public NodeKind getKind() { - return NodeKind.PARAMETER; - } + @Override + public void acceptChildren(JsVisitor visitor, JsContext context) { + + } + + @Override + public NodeKind getKind() { + return NodeKind.PARAMETER; + } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsPostfixOperation.java b/src/com/google/dart/compiler/backend/js/ast/JsPostfixOperation.java index d32565dfc0b..177b6f79b60 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsPostfixOperation.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsPostfixOperation.java @@ -28,11 +28,8 @@ public final class JsPostfixOperation extends JsUnaryOperation { } @Override - public void traverse(JsVisitor v, JsContext context) { - if (v.visit(this, context)) { - super.traverse(v, context); - } - v.endVisit(this, context); + public void accept(JsVisitor v, JsContext context) { + v.visit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsPrefixOperation.java b/src/com/google/dart/compiler/backend/js/ast/JsPrefixOperation.java index e66e6883eb3..6c0724af93d 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsPrefixOperation.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsPrefixOperation.java @@ -4,9 +4,6 @@ package com.google.dart.compiler.backend.js.ast; -/** - * A JavaScript prefix operation. - */ public final class JsPrefixOperation extends JsUnaryOperation implements CanBooleanEval { public JsPrefixOperation(JsUnaryOperator op) { @@ -35,18 +32,12 @@ public final class JsPrefixOperation extends JsUnaryOperation implements CanBool CanBooleanEval eval = (CanBooleanEval) getArg(); return eval.isBooleanFalse(); } - if (getOperator() == JsUnaryOperator.TYPEOF) { - return true; - } - return false; + return getOperator() == JsUnaryOperator.TYPEOF; } @Override public boolean isDefinitelyNotNull() { - if (getOperator() == JsUnaryOperator.TYPEOF) { - return true; - } - return getOperator() != JsUnaryOperator.VOID; + return getOperator() == JsUnaryOperator.TYPEOF || getOperator() != JsUnaryOperator.VOID; } @Override @@ -55,11 +46,8 @@ public final class JsPrefixOperation extends JsUnaryOperation implements CanBool } @Override - public void traverse(JsVisitor v, JsContext context) { - if (v.visit(this, context)) { - super.traverse(v, context); - } - v.endVisit(this, context); + public void accept(JsVisitor v, JsContext context) { + v.visit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsProgram.java b/src/com/google/dart/compiler/backend/js/ast/JsProgram.java index c22f4a3f542..6b71bea47a2 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsProgram.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsProgram.java @@ -18,7 +18,7 @@ import static com.google.dart.compiler.backend.js.ast.JsNumberLiteral.JsIntLiter * A JavaScript program. */ public final class JsProgram extends JsNodeImpl { - private final JsEmpty emptyStmt; + private final JsEmpty emptyStatement; private JsProgramFragment[] fragments; @@ -29,19 +29,16 @@ public final class JsProgram extends JsNodeImpl { private final Map stringLiteralMap = new THashMap(); private final JsScope topScope; - /** - * Constructs a JavaScript program object. - */ public JsProgram(String unitId) { rootScope = new JsRootScope(this); topScope = new JsScope(rootScope, "Global", unitId); setFragmentCount(1); - emptyStmt = new JsEmpty(); + emptyStatement = new JsEmpty(); } - public JsEmpty getEmptyStmt() { - return emptyStmt; + public JsEmpty getEmptyStatement() { + return emptyStatement; } public JsBlock getFragmentBlock(int fragment) { @@ -112,13 +109,15 @@ public final class JsProgram extends JsNodeImpl { } @Override - public void traverse(JsVisitor v, @Nullable JsContext context) { - if (v.visit(this, context)) { - for (JsProgramFragment fragment : fragments) { - v.accept(fragment); - } + public void accept(JsVisitor v, @Nullable JsContext context) { + v.visit(this, context); + } + + @Override + public void acceptChildren(JsVisitor visitor, JsContext context) { + for (JsProgramFragment fragment : fragments) { + visitor.accept(fragment); } - v.endVisit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsProgramFragment.java b/src/com/google/dart/compiler/backend/js/ast/JsProgramFragment.java index a89da4065a6..3de7dfda2a7 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsProgramFragment.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsProgramFragment.java @@ -8,27 +8,28 @@ package com.google.dart.compiler.backend.js.ast; * One independently loadable fragment of a {@link JsProgram}. */ public class JsProgramFragment extends JsNodeImpl { + private final JsGlobalBlock globalBlock; - private final JsGlobalBlock globalBlock; - - public JsProgramFragment() { - this.globalBlock = new JsGlobalBlock(); - } - - public JsBlock getGlobalBlock() { - return globalBlock; - } - - @Override - public void traverse(JsVisitor v, JsContext context) { - if (v.visit(this, context)) { - v.accept(globalBlock); + public JsProgramFragment() { + globalBlock = new JsGlobalBlock(); } - v.endVisit(this, context); - } - @Override - public NodeKind getKind() { - return NodeKind.PROGRAM_FRAGMENT; - } + public JsBlock getGlobalBlock() { + return globalBlock; + } + + @Override + public void accept(JsVisitor v, JsContext context) { + v.visit(this, context); + } + + @Override + public void acceptChildren(JsVisitor visitor, JsContext context) { + visitor.accept(globalBlock); + } + + @Override + public NodeKind getKind() { + return NodeKind.PROGRAM_FRAGMENT; + } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsPropertyInitializer.java b/src/com/google/dart/compiler/backend/js/ast/JsPropertyInitializer.java index d2a212c9145..e057f6e8ecf 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsPropertyInitializer.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsPropertyInitializer.java @@ -39,12 +39,14 @@ public class JsPropertyInitializer extends JsNodeImpl { } @Override - public void traverse(JsVisitor v, JsContext context) { - if (v.visit(this, context)) { - labelExpr = v.accept(labelExpr); - valueExpr = v.accept(valueExpr); - } - v.endVisit(this, context); + public void accept(JsVisitor v, JsContext context) { + v.visit(this, context); + } + + @Override + public void acceptChildren(JsVisitor visitor, JsContext context) { + labelExpr = visitor.accept(labelExpr); + valueExpr = visitor.accept(valueExpr); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsRegExp.java b/src/com/google/dart/compiler/backend/js/ast/JsRegExp.java index 3bcc404b892..18a7ca46b56 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsRegExp.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsRegExp.java @@ -7,58 +7,56 @@ package com.google.dart.compiler.backend.js.ast; /** * A JavaScript regular expression. */ -public final class JsRegExp extends JsValueLiteral { +public final class JsRegExp extends JsLiteral.JsValueLiteral { + private String flags; + private String pattern; - private String flags; - private String pattern; + public JsRegExp() { + } - public JsRegExp() { - } + public String getFlags() { + return flags; + } - public String getFlags() { - return flags; - } + public String getPattern() { + return pattern; + } - public String getPattern() { - return pattern; - } + @Override + public boolean isBooleanFalse() { + return false; + } - @Override - public boolean isBooleanFalse() { - return false; - } + @Override + public boolean isBooleanTrue() { + return true; + } - @Override - public boolean isBooleanTrue() { - return true; - } + @Override + public boolean isDefinitelyNotNull() { + return true; + } - @Override - public boolean isDefinitelyNotNull() { - return true; - } + @Override + public boolean isDefinitelyNull() { + return false; + } - @Override - public boolean isDefinitelyNull() { - return false; - } + public void setFlags(String suffix) { + flags = suffix; + } - public void setFlags(String suffix) { - this.flags = suffix; - } + public void setPattern(String re) { + pattern = re; + } - public void setPattern(String re) { - this.pattern = re; - } + @Override + public void accept(JsVisitor v, JsContext context) { + v.visit(this, context); + } - @Override - public void traverse(JsVisitor v, JsContext context) { - v.visit(this, context); - v.endVisit(this, context); - } - - @Override - public NodeKind getKind() { - return NodeKind.REGEXP; - } + @Override + public NodeKind getKind() { + return NodeKind.REGEXP; + } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsReturn.java b/src/com/google/dart/compiler/backend/js/ast/JsReturn.java index a09421806ec..a347f8bd547 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsReturn.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsReturn.java @@ -8,36 +8,37 @@ package com.google.dart.compiler.backend.js.ast; * A JavaScript return statement. */ public final class JsReturn extends JsNodeImpl implements JsStatement { + private JsExpression expression; - private JsExpression expr; - - public JsReturn() { - } - - public JsReturn(JsExpression expr) { - this.expr = expr; - } - - public JsExpression getExpr() { - return expr; - } - - public void setExpr(JsExpression expr) { - this.expr = expr; - } - - @Override - public void traverse(JsVisitor v, JsContext context) { - if (v.visit(this, context)) { - if (expr != null) { - expr = v.accept(expr); - } + public JsReturn() { + } + + public JsReturn(JsExpression expression) { + this.expression = expression; + } + + public JsExpression getExpression() { + return expression; + } + + public void setExpression(JsExpression expression) { + this.expression = expression; } - v.endVisit(this, context); - } @Override - public NodeKind getKind() { - return NodeKind.RETURN; - } + public void accept(JsVisitor v, JsContext context) { + v.visit(this, context); + } + + @Override + public void acceptChildren(JsVisitor visitor, JsContext context) { + if (expression != null) { + expression = visitor.accept(expression); + } + } + + @Override + public NodeKind getKind() { + return NodeKind.RETURN; + } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsScope.java b/src/com/google/dart/compiler/backend/js/ast/JsScope.java index 406069e6890..abe81550df7 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsScope.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsScope.java @@ -67,8 +67,8 @@ public class JsScope { protected JsScope(@Nullable String description) { this.description = description; - this.parent = null; - this.scopeId = null; + parent = null; + scopeId = null; } /** diff --git a/src/com/google/dart/compiler/backend/js/ast/JsStringLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsStringLiteral.java index 3cff1258798..d063843a626 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsStringLiteral.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsStringLiteral.java @@ -7,7 +7,7 @@ package com.google.dart.compiler.backend.js.ast; /** * A JavaScript string literal expression. */ -public final class JsStringLiteral extends JsValueLiteral { +public final class JsStringLiteral extends JsLiteral.JsValueLiteral { private final String value; @@ -41,9 +41,8 @@ public final class JsStringLiteral extends JsValueLiteral { } @Override - public void traverse(JsVisitor v, JsContext context) { + public void accept(JsVisitor v, JsContext context) { v.visit(this, context); - v.endVisit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsSwitch.java b/src/com/google/dart/compiler/backend/js/ast/JsSwitch.java index 683dd195941..c4b3a3c1ae6 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsSwitch.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsSwitch.java @@ -12,36 +12,38 @@ import java.util.List; */ public class JsSwitch extends JsNodeImpl implements JsStatement { - private final List cases = new ArrayList(); - private JsExpression expr; + private final List cases = new ArrayList(); + private JsExpression expr; - public JsSwitch() { - super(); - } - - public List getCases() { - return cases; - } - - public JsExpression getExpr() { - return expr; - } - - public void setExpr(JsExpression expr) { - this.expr = expr; - } - - @Override - public void traverse(JsVisitor v, JsContext context) { - if (v.visit(this, context)) { - expr = v.accept(expr); - v.acceptWithInsertRemove(cases); + public JsSwitch() { + super(); } - v.endVisit(this, context); - } - @Override - public NodeKind getKind() { - return NodeKind.SWITCH; - } + public List getCases() { + return cases; + } + + public JsExpression getExpr() { + return expr; + } + + public void setExpr(JsExpression expr) { + this.expr = expr; + } + + @Override + public void accept(JsVisitor v, JsContext context) { + v.visit(this, context); + } + + @Override + public void acceptChildren(JsVisitor visitor, JsContext context) { + expr = visitor.accept(expr); + visitor.acceptWithInsertRemove(cases); + } + + @Override + public NodeKind getKind() { + return NodeKind.SWITCH; + } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsSwitchMember.java b/src/com/google/dart/compiler/backend/js/ast/JsSwitchMember.java index 08265369888..89b8ffdebba 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsSwitchMember.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsSwitchMember.java @@ -21,4 +21,9 @@ public abstract class JsSwitchMember extends JsNodeImpl { public List getStatements() { return statements; } + + @Override + public void acceptChildren(JsVisitor visitor, JsContext context) { + visitor.acceptWithInsertRemove(statements); + } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsThrow.java b/src/com/google/dart/compiler/backend/js/ast/JsThrow.java index 2c7867056f3..9345d31d15f 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsThrow.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsThrow.java @@ -23,11 +23,13 @@ public class JsThrow extends JsNodeImpl implements JsStatement { } @Override - public void traverse(JsVisitor v, JsContext context) { - if (v.visit(this, context)) { - expression = v.accept(expression); - } - v.endVisit(this, context); + public void accept(JsVisitor v, JsContext context) { + v.visit(this, context); + } + + @Override + public void acceptChildren(JsVisitor visitor, JsContext context) { + visitor.accept(expression); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsTry.java b/src/com/google/dart/compiler/backend/js/ast/JsTry.java index cd0fde570f5..ebd3d1b2888 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsTry.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsTry.java @@ -40,7 +40,7 @@ public class JsTry extends JsNodeImpl implements JsStatement { } public void setFinallyBlock(JsBlock block) { - this.finallyBlock = block; + finallyBlock = block; } public void setTryBlock(JsBlock block) { @@ -48,15 +48,17 @@ public class JsTry extends JsNodeImpl implements JsStatement { } @Override - public void traverse(JsVisitor v, JsContext context) { - if (v.visit(this, context)) { - tryBlock = v.accept(tryBlock); - v.acceptWithInsertRemove(catches); - if (finallyBlock != null) { - finallyBlock = v.accept(finallyBlock); - } + public void accept(JsVisitor v, JsContext context) { + v.visit(this, context); + } + + @Override + public void acceptChildren(JsVisitor visitor, JsContext context) { + tryBlock = visitor.accept(tryBlock); + visitor.acceptWithInsertRemove(catches); + if (finallyBlock != null) { + finallyBlock = visitor.accept(finallyBlock); } - v.endVisit(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsUnaryOperation.java b/src/com/google/dart/compiler/backend/js/ast/JsUnaryOperation.java index 669fe9a0a78..8f72f6f07d5 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsUnaryOperation.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsUnaryOperation.java @@ -9,45 +9,46 @@ package com.google.dart.compiler.backend.js.ast; */ public abstract class JsUnaryOperation extends JsExpressionImpl { - private JsExpression arg; + private JsExpression arg; - private final JsUnaryOperator op; + private final JsUnaryOperator op; - public JsUnaryOperation(JsUnaryOperator op) { - this(op, null); - } - - public JsUnaryOperation(JsUnaryOperator op, JsExpression arg) { - super(); - this.op = op; - this.arg = arg; - } - - public JsExpression getArg() { - return arg; - } - - public JsUnaryOperator getOperator() { - return op; - } - - @Override - public final boolean hasSideEffects() { - return op.isModifying() || arg.hasSideEffects(); - } - - public void setArg(JsExpression arg) { - this.arg = arg; - } - - @Override - public void traverse(JsVisitor v, JsContext context) { - if (op.isModifying()) { - // The delete operator is practically like an assignment of undefined, so - // for practical purposes we're treating it as an lvalue. - arg = v.acceptLvalue(arg); - } else { - arg = v.accept(arg); + public JsUnaryOperation(JsUnaryOperator op) { + this(op, null); + } + + public JsUnaryOperation(JsUnaryOperator op, JsExpression arg) { + super(); + this.op = op; + this.arg = arg; + } + + public JsExpression getArg() { + return arg; + } + + public JsUnaryOperator getOperator() { + return op; + } + + @Override + public final boolean hasSideEffects() { + return op.isModifying() || arg.hasSideEffects(); + } + + public void setArg(JsExpression arg) { + this.arg = arg; + } + + @Override + public void acceptChildren(JsVisitor visitor, JsContext context) { + if (op.isModifying()) { + // The delete operator is practically like an assignment of undefined, so + // for practical purposes we're treating it as an lvalue. + arg = visitor.acceptLvalue(arg); + } + else { + arg = visitor.accept(arg); + } } - } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsValueLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsValueLiteral.java deleted file mode 100644 index f64eb794ad6..00000000000 --- a/src/com/google/dart/compiler/backend/js/ast/JsValueLiteral.java +++ /dev/null @@ -1,23 +0,0 @@ -// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. - -package com.google.dart.compiler.backend.js.ast; - -/** - * A JavaScript string literal expression. - */ -public abstract class JsValueLiteral extends JsLiteral { - protected JsValueLiteral() { - } - - @Override - public final boolean hasSideEffects() { - return false; - } - - @Override - public final boolean isLeaf() { - return true; - } -} diff --git a/src/com/google/dart/compiler/backend/js/ast/JsVars.java b/src/com/google/dart/compiler/backend/js/ast/JsVars.java index 638a50ef496..359199e77b3 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsVars.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsVars.java @@ -81,13 +81,15 @@ public class JsVars extends JsNodeImpl implements JsStatement, Iterable T accept(T node) { + public T accept(T node) { doTraverse(node, UNMODIFIABLE_CONTEXT); return node; } - public final void acceptList(List collection) { + public final void acceptList(List collection) { for (T node : collection) { doTraverse(node, UNMODIFIABLE_CONTEXT); } @@ -103,138 +99,12 @@ abstract public class JsVisitor { return expr; } - public final void acceptWithInsertRemove(List collection) { + public final void acceptWithInsertRemove(List collection) { for (T node : collection) { doTraverse(node, UNMODIFIABLE_CONTEXT); } } - public void endVisit(JsArrayAccess x, JsContext ctx) { - } - - public void endVisit(JsArrayLiteral x, JsContext ctx) { - } - - public void endVisit(JsBinaryOperation x, JsContext ctx) { - } - - public void endVisit(JsBlock x, JsContext ctx) { - } - - public void endVisit(JsLiteral.JsBooleanLiteral x, JsContext ctx) { - } - - public void endVisit(JsBreak x, JsContext ctx) { - } - - public void endVisit(JsCase x, JsContext ctx) { - } - - public void endVisit(JsCatch x, JsContext ctx) { - } - - public void endVisit(JsConditional x, JsContext ctx) { - } - - public void endVisit(JsContinue x, JsContext ctx) { - } - - public void endVisit(JsDebugger x, JsContext ctx) { - } - - public void endVisit(JsDefault x, JsContext ctx) { - } - - public void endVisit(JsDoWhile x, JsContext ctx) { - } - - public void endVisit(JsEmpty x, JsContext ctx) { - } - - public void endVisit(JsExpressionStatement x, JsContext ctx) { - } - - public void endVisit(JsFor x, JsContext ctx) { - } - - public void endVisit(JsForIn x, JsContext ctx) { - } - - public void endVisit(JsFunction x, JsContext ctx) { - } - - public void endVisit(JsIf x, JsContext ctx) { - } - - public void endVisit(JsInvocation x, JsContext ctx) { - } - - public void endVisit(JsLabel x, JsContext ctx) { - } - - public void endVisit(JsNameRef x, JsContext ctx) { - } - - public void endVisit(JsNew x, JsContext ctx) { - } - - public void endVisit(JsNullLiteral x, JsContext ctx) { - } - - public void endVisit(JsNumberLiteral x, JsContext ctx) { - } - - public void endVisit(JsObjectLiteral x, JsContext ctx) { - } - - public void endVisit(JsParameter x, JsContext ctx) { - } - - public void endVisit(JsPostfixOperation x, JsContext ctx) { - } - - public void endVisit(JsPrefixOperation x, JsContext ctx) { - } - - public void endVisit(JsProgram x, JsContext ctx) { - } - - public void endVisit(JsProgramFragment x, JsContext ctx) { - } - - public void endVisit(JsPropertyInitializer x, JsContext ctx) { - } - - public void endVisit(JsRegExp x, JsContext ctx) { - } - - public void endVisit(JsReturn x, JsContext ctx) { - } - - public void endVisit(JsStringLiteral x, JsContext ctx) { - } - - public void endVisit(JsSwitch x, JsContext ctx) { - } - - public void endVisit(JsLiteral.JsThisRef x, JsContext ctx) { - } - - public void endVisit(JsThrow x, JsContext ctx) { - } - - public void endVisit(JsTry x, JsContext ctx) { - } - - public void endVisit(JsVar x, JsContext ctx) { - } - - public void endVisit(JsVars x, JsContext ctx) { - } - - public void endVisit(JsWhile x, JsContext ctx) { - } - public boolean visit(JsArrayAccess x, JsContext ctx) { return true; } @@ -287,8 +157,7 @@ abstract public class JsVisitor { return true; } - public boolean visit(JsEmpty x, JsContext ctx) { - return true; + public void visit(JsEmpty x, JsContext ctx) { } public boolean visit(JsExpressionStatement x, JsContext ctx) { @@ -327,16 +196,13 @@ abstract public class JsVisitor { return true; } - public boolean visit(JsNullLiteral x, JsContext ctx) { - return true; + public void visit(JsNullLiteral x, JsContext ctx) { } - public boolean visit(JsNumberLiteral.JsIntLiteral x, JsContext ctx) { - return true; + public void visit(JsNumberLiteral.JsIntLiteral x, JsContext ctx) { } - public boolean visit(JsNumberLiteral.JsDoubleLiteral x, JsContext ctx) { - return true; + public void visit(JsNumberLiteral.JsDoubleLiteral x, JsContext ctx) { } public boolean visit(JsObjectLiteral x, JsContext ctx) { @@ -367,16 +233,14 @@ abstract public class JsVisitor { return true; } - public boolean visit(JsRegExp x, JsContext ctx) { - return true; + public void visit(JsRegExp x, JsContext ctx) { } public boolean visit(JsReturn x, JsContext ctx) { return true; } - public boolean visit(JsStringLiteral x, JsContext ctx) { - return true; + public void visit(JsStringLiteral x, JsContext ctx) { } public boolean visit(JsSwitch x, JsContext ctx) { @@ -411,7 +275,7 @@ abstract public class JsVisitor { return true; } - protected void doTraverse(JsVisitable node, JsContext context) { - node.traverse(this, context); + protected void doTraverse(JsNode node, JsContext context) { + node.accept(this, context); } -} +} \ No newline at end of file diff --git a/src/com/google/dart/compiler/backend/js/ast/JsWhile.java b/src/com/google/dart/compiler/backend/js/ast/JsWhile.java index c00d355335d..6e7d24d0d61 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsWhile.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsWhile.java @@ -36,12 +36,14 @@ public class JsWhile extends JsNodeImpl implements JsStatement { } @Override - public void traverse(JsVisitor v, JsContext context) { - if (v.visit(this, context)) { - condition = v.accept(condition); - body = v.accept(body); - } - v.endVisit(this, context); + public void accept(JsVisitor v, JsContext context) { + v.visit(this, context); + } + + @Override + public void acceptChildren(JsVisitor visitor, JsContext context) { + condition = visitor.accept(condition); + body = visitor.accept(body); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/RecursiveJsVisitor.java b/src/com/google/dart/compiler/backend/js/ast/RecursiveJsVisitor.java new file mode 100644 index 00000000000..6f3b9c4f495 --- /dev/null +++ b/src/com/google/dart/compiler/backend/js/ast/RecursiveJsVisitor.java @@ -0,0 +1,4 @@ +package com.google.dart.compiler.backend.js.ast; + +public abstract class RecursiveJsVisitor extends JsVisitor { +} From 4b2c170c000621757eac3bdff7bc8d581870f4a2 Mon Sep 17 00:00:00 2001 From: develar Date: Thu, 25 Oct 2012 09:27:11 +0400 Subject: [PATCH 18/27] simplify AST (idea like) --- .../js/JsConstructExpressionVisitor.java | 23 +-- .../backend/js/JsFirstExpressionVisitor.java | 35 ++-- .../backend/js/JsPrecedenceVisitor.java | 177 +++-------------- .../backend/js/JsRequiresSemiVisitor.java | 148 +++++--------- .../js/JsToStringGenerationVisitor.java | 137 +++++-------- .../backend/js/ast/JsArrayAccess.java | 2 +- .../backend/js/ast/JsArrayLiteral.java | 2 +- .../backend/js/ast/JsBinaryOperation.java | 2 +- .../dart/compiler/backend/js/ast/JsBlock.java | 2 +- .../dart/compiler/backend/js/ast/JsBreak.java | 2 +- .../dart/compiler/backend/js/ast/JsCase.java | 2 +- .../dart/compiler/backend/js/ast/JsCatch.java | 2 +- .../backend/js/ast/JsConditional.java | 2 +- .../compiler/backend/js/ast/JsContinue.java | 2 +- .../compiler/backend/js/ast/JsDebugger.java | 2 +- .../compiler/backend/js/ast/JsDefault.java | 2 +- .../compiler/backend/js/ast/JsDoWhile.java | 5 +- .../compiler/backend/js/ast/JsDocComment.java | 3 +- .../dart/compiler/backend/js/ast/JsEmpty.java | 2 +- .../backend/js/ast/JsExpressionStatement.java | 2 +- .../dart/compiler/backend/js/ast/JsFor.java | 2 +- .../dart/compiler/backend/js/ast/JsForIn.java | 4 +- .../compiler/backend/js/ast/JsFunction.java | 2 +- .../dart/compiler/backend/js/ast/JsIf.java | 2 +- .../compiler/backend/js/ast/JsInvocation.java | 2 +- .../dart/compiler/backend/js/ast/JsLabel.java | 2 +- .../compiler/backend/js/ast/JsLiteral.java | 4 +- .../compiler/backend/js/ast/JsNameRef.java | 2 +- .../dart/compiler/backend/js/ast/JsNew.java | 2 +- .../backend/js/ast/JsNullLiteral.java | 2 +- .../backend/js/ast/JsNumberLiteral.java | 4 +- .../backend/js/ast/JsObjectLiteral.java | 2 +- .../compiler/backend/js/ast/JsParameter.java | 2 +- .../backend/js/ast/JsPostfixOperation.java | 2 +- .../backend/js/ast/JsPrefixOperation.java | 2 +- .../compiler/backend/js/ast/JsProgram.java | 2 +- .../backend/js/ast/JsProgramFragment.java | 2 +- .../backend/js/ast/JsPropertyInitializer.java | 2 +- .../compiler/backend/js/ast/JsRegExp.java | 2 +- .../compiler/backend/js/ast/JsReturn.java | 2 +- .../backend/js/ast/JsStringLiteral.java | 2 +- .../dart/compiler/backend/js/ast/JsThrow.java | 2 +- .../dart/compiler/backend/js/ast/JsTry.java | 2 +- .../dart/compiler/backend/js/ast/JsVars.java | 2 +- .../compiler/backend/js/ast/JsVisitor.java | 183 +++++++++--------- .../dart/compiler/backend/js/ast/JsWhile.java | 2 +- .../backend/js/ast/RecursiveJsVisitor.java | 4 + 47 files changed, 278 insertions(+), 519 deletions(-) diff --git a/src/com/google/dart/compiler/backend/js/JsConstructExpressionVisitor.java b/src/com/google/dart/compiler/backend/js/JsConstructExpressionVisitor.java index 38882310ca0..e6422c43d2a 100644 --- a/src/com/google/dart/compiler/backend/js/JsConstructExpressionVisitor.java +++ b/src/com/google/dart/compiler/backend/js/JsConstructExpressionVisitor.java @@ -10,7 +10,7 @@ import com.google.dart.compiler.backend.js.ast.*; * Searches for method invocations in constructor expressions that would not * normally be surrounded by parentheses. */ -public class JsConstructExpressionVisitor extends JsVisitor { +public class JsConstructExpressionVisitor extends RecursiveJsVisitor { public static boolean exec(JsExpression expression) { if (JsPrecedenceVisitor.exec(expression) < JsPrecedenceVisitor.PRECEDENCE_NEW) { return true; @@ -29,55 +29,48 @@ public class JsConstructExpressionVisitor extends JsVisitor { * We only look at the array expression since the index has its own scope. */ @Override - public boolean visit(JsArrayAccess x, JsContext ctx) { + public void visitArrayAccess(JsArrayAccess x, JsContext ctx) { accept(x.getArrayExpression()); - return false; } /** * Array literals have their own scoping. */ @Override - public boolean visit(JsArrayLiteral x, JsContext ctx) { - return false; + public void visitArray(JsArrayLiteral x, JsContext ctx) { } /** * Functions have their own scoping. */ @Override - public boolean visit(JsFunction x, JsContext ctx) { - return false; + public void visitFunction(JsFunction x, JsContext ctx) { } @Override - public boolean visit(JsInvocation x, JsContext ctx) { + public void visitInvocation(JsInvocation x, JsContext ctx) { containsInvocation = true; - return false; } @Override - public boolean visit(JsNameRef x, JsContext ctx) { + public void visitNameRef(JsNameRef x, JsContext ctx) { if (!x.isLeaf()) { accept(x.getQualifier()); } - return false; } /** * New constructs bind to the nearest set of parentheses. */ @Override - public boolean visit(JsNew x, JsContext ctx) { - return false; + public void visitNew(JsNew x, JsContext ctx) { } /** * Object literals have their own scope. */ @Override - public boolean visit(JsObjectLiteral x, JsContext ctx) { - return false; + public void visitObjectLiteral(JsObjectLiteral x, JsContext ctx) { } /** diff --git a/src/com/google/dart/compiler/backend/js/JsFirstExpressionVisitor.java b/src/com/google/dart/compiler/backend/js/JsFirstExpressionVisitor.java index 4b6a80ef4af..4901391d1c4 100644 --- a/src/com/google/dart/compiler/backend/js/JsFirstExpressionVisitor.java +++ b/src/com/google/dart/compiler/backend/js/JsFirstExpressionVisitor.java @@ -30,7 +30,7 @@ import com.google.dart.compiler.backend.js.ast.JsExpressionStatement; *

  • function() {}
  • * */ -public class JsFirstExpressionVisitor extends JsVisitor { +public class JsFirstExpressionVisitor extends RecursiveJsVisitor { public static boolean exec(JsExpressionStatement statement) { JsExpression expression = statement.getExpression(); // Pure function declarations do not need parentheses @@ -49,67 +49,56 @@ public class JsFirstExpressionVisitor extends JsVisitor { } @Override - public boolean visit(JsArrayAccess x, JsContext ctx) { + public void visitArrayAccess(JsArrayAccess x, JsContext ctx) { accept(x.getArrayExpression()); - return false; } @Override - public boolean visit(JsArrayLiteral x, JsContext ctx) { - return false; + public void visitArray(JsArrayLiteral x, JsContext ctx) { } @Override - public boolean visit(JsBinaryOperation x, JsContext ctx) { + public void visitBinaryExpression(JsBinaryOperation x, JsContext ctx) { accept(x.getArg1()); - return false; } @Override - public boolean visit(JsConditional x, JsContext ctx) { + public void visitConditional(JsConditional x, JsContext ctx) { accept(x.getTestExpression()); - return false; } @Override - public boolean visit(JsFunction x, JsContext ctx) { + public void visitFunction(JsFunction x, JsContext ctx) { needsParentheses = true; - return false; } @Override - public boolean visit(JsInvocation x, JsContext ctx) { + public void visitInvocation(JsInvocation x, JsContext ctx) { accept(x.getQualifier()); - return false; } @Override - public boolean visit(JsNameRef x, JsContext ctx) { + public void visitNameRef(JsNameRef x, JsContext ctx) { if (!x.isLeaf()) { accept(x.getQualifier()); } - return false; } @Override - public boolean visit(JsNew x, JsContext ctx) { - return false; + public void visitNew(JsNew x, JsContext ctx) { } @Override - public boolean visit(JsObjectLiteral x, JsContext ctx) { + public void visitObjectLiteral(JsObjectLiteral x, JsContext ctx) { needsParentheses = true; - return false; } @Override - public boolean visit(JsPostfixOperation x, JsContext ctx) { + public void visitPostfixOperation(JsPostfixOperation x, JsContext ctx) { accept(x.getArg()); - return false; } @Override - public boolean visit(JsPrefixOperation x, JsContext ctx) { - return false; + public void visitPrefixOperation(JsPrefixOperation x, JsContext ctx) { } } diff --git a/src/com/google/dart/compiler/backend/js/JsPrecedenceVisitor.java b/src/com/google/dart/compiler/backend/js/JsPrecedenceVisitor.java index 80698165d3c..28625e07e85 100644 --- a/src/com/google/dart/compiler/backend/js/JsPrecedenceVisitor.java +++ b/src/com/google/dart/compiler/backend/js/JsPrecedenceVisitor.java @@ -5,7 +5,6 @@ package com.google.dart.compiler.backend.js; import com.google.dart.compiler.backend.js.ast.*; -import com.google.dart.compiler.backend.js.ast.JsVars.JsVar; /** * Precedence indices from "JavaScript - The Definitive Guide" 4th Edition (page @@ -31,6 +30,11 @@ import com.google.dart.compiler.backend.js.ast.JsVars.JsVar; class JsPrecedenceVisitor extends JsVisitor { static final int PRECEDENCE_NEW = 15; + private int answer = -1; + + private JsPrecedenceVisitor() { + } + public static int exec(JsExpression expression) { JsPrecedenceVisitor visitor = new JsPrecedenceVisitor(); visitor.accept(expression); @@ -40,237 +44,108 @@ class JsPrecedenceVisitor extends JsVisitor { return visitor.answer; } - private int answer = -1; - - private JsPrecedenceVisitor() { - } - @Override - public boolean visit(JsArrayAccess x, JsContext ctx) { + public void visitArrayAccess(JsArrayAccess x, JsContext ctx) { answer = 16; - return false; } @Override - public boolean visit(JsArrayLiteral x, JsContext ctx) { + public void visitArray(JsArrayLiteral x, JsContext ctx) { answer = 17; // primary - return false; } @Override - public boolean visit(JsBinaryOperation x, JsContext ctx) { + public void visitBinaryExpression(JsBinaryOperation x, JsContext ctx) { answer = x.getOperator().getPrecedence(); - return false; } @Override - public boolean visit(JsBlock x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } - - @Override - public boolean visit(JsLiteral.JsBooleanLiteral x, JsContext ctx) { + public void visitBoolean(JsLiteral.JsBooleanLiteral x, JsContext ctx) { answer = 17; // primary - return false; } @Override - public boolean visit(JsBreak x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } - - @Override - public boolean visit(JsCase x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } - - @Override - public boolean visit(JsCatch x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } - - @Override - public boolean visit(JsConditional x, JsContext ctx) { + public void visitConditional(JsConditional x, JsContext ctx) { answer = 3; - return false; } @Override - public boolean visit(JsContinue x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } - - @Override - public boolean visit(JsDebugger x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } - - @Override - public boolean visit(JsDefault x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } - - @Override - public boolean visit(JsDoWhile x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } - - @Override - public void visit(JsEmpty x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } - - @Override - public boolean visit(JsExpressionStatement x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } - - @Override - public boolean visit(JsFor x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } - - @Override - public boolean visit(JsForIn x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } - - @Override - public boolean visit(JsFunction x, JsContext ctx) { + public void visitFunction(JsFunction x, JsContext ctx) { answer = 17; // primary - return false; } @Override - public boolean visit(JsIf x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } - - @Override - public boolean visit(JsInvocation x, JsContext ctx) { + public void visitInvocation(JsInvocation x, JsContext ctx) { answer = 16; - return false; } @Override - public boolean visit(JsLabel x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } - - @Override - public boolean visit(JsNameRef x, JsContext ctx) { + public void visitNameRef(JsNameRef x, JsContext ctx) { if (x.isLeaf()) { answer = 17; // primary } else { answer = 16; // property access } - return false; } @Override - public boolean visit(JsNew x, JsContext ctx) { + public void visitNew(JsNew x, JsContext ctx) { answer = PRECEDENCE_NEW; - return false; } @Override - public void visit(JsNullLiteral x, JsContext ctx) { + public void visitNull(JsNullLiteral x, JsContext ctx) { answer = 17; // primary } @Override - public void visit(JsNumberLiteral.JsIntLiteral x, JsContext ctx) { + public void visitInt(JsNumberLiteral.JsIntLiteral x, JsContext ctx) { answer = 17; // primary } @Override - public void visit(JsNumberLiteral.JsDoubleLiteral x, JsContext ctx) { + public void visitDouble(JsNumberLiteral.JsDoubleLiteral x, JsContext ctx) { answer = 17; // primary } @Override - public boolean visit(JsObjectLiteral x, JsContext ctx) { + public void visitObjectLiteral(JsObjectLiteral x, JsContext ctx) { answer = 17; // primary - return false; } @Override - public boolean visit(JsParameter x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } - - @Override - public boolean visit(JsPostfixOperation x, JsContext ctx) { + public void visitPostfixOperation(JsPostfixOperation x, JsContext ctx) { answer = x.getOperator().getPrecedence(); - return false; } @Override - public boolean visit(JsPrefixOperation x, JsContext ctx) { + public void visitPrefixOperation(JsPrefixOperation x, JsContext ctx) { answer = x.getOperator().getPrecedence(); - return false; } @Override - public boolean visit(JsProgram x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } - - @Override - public boolean visit(JsPropertyInitializer x, JsContext ctx) { - answer = 17; // primary - return false; - } - - @Override - public void visit(JsRegExp x, JsContext ctx) { + public void visitPropertyInitializer(JsPropertyInitializer x, JsContext ctx) { answer = 17; // primary } @Override - public boolean visit(JsReturn x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } - - @Override - public void visit(JsStringLiteral x, JsContext ctx) { + public void visitRegExp(JsRegExp x, JsContext ctx) { answer = 17; // primary } @Override - public boolean visit(JsSwitch x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } - - @Override - public boolean visit(JsLiteral.JsThisRef x, JsContext ctx) { + public void visitString(JsStringLiteral x, JsContext ctx) { answer = 17; // primary - return false; } @Override - public boolean visit(JsThrow x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); + public void visitThis(JsLiteral.JsThisRef x, JsContext ctx) { + answer = 17; // primary } @Override - public boolean visit(JsTry x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } - - @Override - public boolean visit(JsVar x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } - - @Override - public boolean visit(JsVars x, JsContext ctx) { - throw new RuntimeException("Only expressions have precedence."); - } - - @Override - public boolean visit(JsWhile x, JsContext ctx) { + protected void visitElement(JsNode node, JsContext context) { throw new RuntimeException("Only expressions have precedence."); } } diff --git a/src/com/google/dart/compiler/backend/js/JsRequiresSemiVisitor.java b/src/com/google/dart/compiler/backend/js/JsRequiresSemiVisitor.java index 7bc6e95da5a..9cebe950c2b 100644 --- a/src/com/google/dart/compiler/backend/js/JsRequiresSemiVisitor.java +++ b/src/com/google/dart/compiler/backend/js/JsRequiresSemiVisitor.java @@ -5,17 +5,16 @@ package com.google.dart.compiler.backend.js; import com.google.dart.compiler.backend.js.ast.*; -import com.google.dart.compiler.backend.js.ast.JsExpressionStatement; /** * Determines if a statement at the end of a block requires a semicolon. - * + *

    * For example, the following statements require semicolons:
    *

      *
    • if (cond);
    • *
    • while (cond);
    • *
    - * + *

    * The following do not require semicolons:
    *

      *
    • return 1
    • @@ -23,114 +22,59 @@ import com.google.dart.compiler.backend.js.ast.JsExpressionStatement; *
    */ public class JsRequiresSemiVisitor extends JsVisitor { + private boolean needsSemicolon; - public static boolean exec(JsStatement lastStatement) { - JsRequiresSemiVisitor visitor = new JsRequiresSemiVisitor(); - visitor.accept(lastStatement); - return visitor.needsSemicolon; - } - - private boolean needsSemicolon = false; - - private JsRequiresSemiVisitor() { - } - - @Override - public boolean visit(JsBlock x, JsContext ctx) { - return false; - } - - @Override - public boolean visit(JsBreak x, JsContext ctx) { - return false; - } - - @Override - public boolean visit(JsDebugger x, JsContext ctx) { - return false; - } - - @Override - public boolean visit(JsDoWhile x, JsContext ctx) { - return false; - } - - @Override - public boolean visit(JsExpressionStatement x, JsContext ctx) { - return false; - } - - @Override - public boolean visit(JsFor x, JsContext ctx) { - if (x.getBody() instanceof JsEmpty) { - needsSemicolon = true; + private JsRequiresSemiVisitor() { } - return false; - } - @Override - public boolean visit(JsForIn x, JsContext ctx) { - if (x.getBody() instanceof JsEmpty) { - needsSemicolon = true; + public static boolean exec(JsStatement lastStatement) { + JsRequiresSemiVisitor visitor = new JsRequiresSemiVisitor(); + visitor.accept(lastStatement); + return visitor.needsSemicolon; } - return false; - } - @Override - public boolean visit(JsIf x, JsContext ctx) { - JsStatement thenStmt = x.getThenStatement(); - JsStatement elseStmt = x.getElseStatement(); - JsStatement toCheck = thenStmt; - if (elseStmt != null) { - toCheck = elseStmt; + @Override + public void visitFor(JsFor x, JsContext ctx) { + if (x.getBody() instanceof JsEmpty) { + needsSemicolon = true; + } } - if (toCheck instanceof JsEmpty) { - needsSemicolon = true; - } else { - // Must recurse to determine last statement (possible if-else chain). - accept(toCheck); + + @Override + public void visitForIn(JsForIn x, JsContext ctx) { + if (x.getBody() instanceof JsEmpty) { + needsSemicolon = true; + } } - return false; - } - @Override - public boolean visit(JsLabel x, JsContext ctx) { - if (x.getStatement() instanceof JsEmpty) { - needsSemicolon = true; + @Override + public void visitIf(JsIf x, JsContext ctx) { + JsStatement thenStmt = x.getThenStatement(); + JsStatement elseStmt = x.getElseStatement(); + JsStatement toCheck = thenStmt; + if (elseStmt != null) { + toCheck = elseStmt; + } + if (toCheck instanceof JsEmpty) { + needsSemicolon = true; + } + else { + // Must recurse to determine last statement (possible if-else chain). + accept(toCheck); + } } - return false; - } - @Override - public boolean visit(JsReturn x, JsContext ctx) { - return false; - } - - @Override - public boolean visit(JsSwitch x, JsContext ctx) { - return false; - } - - @Override - public boolean visit(JsThrow x, JsContext ctx) { - return false; - } - - @Override - public boolean visit(JsTry x, JsContext ctx) { - return false; - } - - @Override - public boolean visit(JsVars x, JsContext ctx) { - return false; - } - - @Override - public boolean visit(JsWhile x, JsContext ctx) { - if (x.getBody() instanceof JsEmpty) { - needsSemicolon = true; + @Override + public void visitLabel(JsLabel x, JsContext ctx) { + if (x.getStatement() instanceof JsEmpty) { + needsSemicolon = true; + } + } + + @Override + public void visitWhile(JsWhile x, JsContext ctx) { + if (x.getBody() instanceof JsEmpty) { + needsSemicolon = true; + } } - return false; - } } diff --git a/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java b/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java index b4c0bb5cf4d..09d06545a94 100644 --- a/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java +++ b/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java @@ -205,20 +205,18 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public boolean visit(JsArrayAccess x, JsContext context) { + public void visitArrayAccess(JsArrayAccess x, JsContext context) { printPair(x, x.getArrayExpression()); leftSquare(); accept(x.getIndexExpression()); rightSquare(); - return false; } @Override - public boolean visit(JsArrayLiteral x, JsContext ctx) { + public void visitArray(JsArrayLiteral x, JsContext ctx) { leftSquare(); printExpressions(x.getExpressions()); rightSquare(); - return false; } private void printExpressions(List expressions) { @@ -234,7 +232,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public boolean visit(JsBinaryOperation binaryOperation, JsContext context) { + public void visitBinaryExpression(JsBinaryOperation binaryOperation, JsContext context) { JsBinaryOperator operator = binaryOperation.getOperator(); JsExpression arg1 = binaryOperation.getArg1(); boolean isExpressionEnclosed = parenPush(binaryOperation, arg1, !operator.isLeftAssociative()); @@ -276,39 +274,33 @@ public class JsToStringGenerationVisitor extends JsVisitor { if (isParenOpened) { rightParen(); } - - return false; } @Override - public boolean visit(JsBlock x, JsContext ctx) { + public void visitBlock(JsBlock x, JsContext ctx) { printJsBlock(x, true, true); - return false; } @Override - public boolean visit(JsLiteral.JsBooleanLiteral x, JsContext ctx) { + public void visitBoolean(JsLiteral.JsBooleanLiteral x, JsContext ctx) { if (x.getValue()) { p.print(CHARS_TRUE); } else { p.print(CHARS_FALSE); } - return false; } @Override - public boolean visit(JsBreak x, JsContext ctx) { + public void visitBreak(JsBreak x, JsContext ctx) { p.print(CHARS_BREAK); continueOrBreakLabel(x); - return false; } @Override - public boolean visit(JsContinue x, JsContext ctx) { + public void visitContinue(JsContinue x, JsContext ctx) { p.print(CHARS_CONTINUE); continueOrBreakLabel(x); - return false; } private void continueOrBreakLabel(JsContinue x) { @@ -320,7 +312,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public boolean visit(JsCase x, JsContext ctx) { + public void visitCase(JsCase x, JsContext ctx) { p.print(CHARS_CASE); space(); accept(x.getCaseExpression()); @@ -328,7 +320,6 @@ public class JsToStringGenerationVisitor extends JsVisitor { newlineOpt(); printSwitchMemberStatements(x); - return false; } private void printSwitchMemberStatements(JsSwitchMember x) { @@ -346,7 +337,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public boolean visit(JsCatch x, JsContext ctx) { + public void visitCatch(JsCatch x, JsContext ctx) { spaceOpt(); p.print(CHARS_CATCH); spaceOpt(); @@ -366,12 +357,10 @@ public class JsToStringGenerationVisitor extends JsVisitor { rightParen(); spaceOpt(); accept(x.getBody()); - - return false; } @Override - public boolean visit(JsConditional x, JsContext ctx) { + public void visitConditional(JsConditional x, JsContext ctx) { // Associativity: for the then and else branches, it is safe to insert // another // ternary expression, but if the test expression is a ternary, it should @@ -385,7 +374,6 @@ public class JsToStringGenerationVisitor extends JsVisitor { _colon(); spaceOpt(); printPair(x, x.getElseExpression()); - return false; } private void printPair(JsExpression parent, JsExpression expression) { @@ -400,22 +388,20 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public boolean visit(JsDebugger x, JsContext ctx) { + public void visitDebugger(JsDebugger x, JsContext ctx) { p.print(CHARS_DEBUGGER); - return false; } @Override - public boolean visit(JsDefault x, JsContext ctx) { + public void visitDefault(JsDefault x, JsContext ctx) { p.print(CHARS_DEFAULT); _colon(); printSwitchMemberStatements(x); - return false; } @Override - public boolean visit(JsWhile x, JsContext ctx) { + public void visitWhile(JsWhile x, JsContext ctx) { _while(); spaceOpt(); leftParen(); @@ -424,11 +410,10 @@ public class JsToStringGenerationVisitor extends JsVisitor { _nestedPush(x.getBody()); accept(x.getBody()); _nestedPop(x.getBody()); - return false; } @Override - public boolean visit(JsDoWhile x, JsContext ctx) { + public void visitDoWhile(JsDoWhile x, JsContext ctx) { p.print(CHARS_DO); _nestedPush(x.getBody()); accept(x.getBody()); @@ -446,15 +431,14 @@ public class JsToStringGenerationVisitor extends JsVisitor { leftParen(); accept(x.getCondition()); rightParen(); - return false; } @Override - public void visit(JsEmpty x, JsContext ctx) { + public void visitEmpty(JsEmpty x, JsContext ctx) { } @Override - public boolean visit(JsExpressionStatement x, JsContext ctx) { + public void visitExpressionStatement(JsExpressionStatement x, JsContext ctx) { boolean surroundWithParentheses = JsFirstExpressionVisitor.exec(x); if (surroundWithParentheses) { leftParen(); @@ -463,11 +447,10 @@ public class JsToStringGenerationVisitor extends JsVisitor { if (surroundWithParentheses) { rightParen(); } - return false; } @Override - public boolean visit(JsFor x, JsContext ctx) { + public void visitFor(JsFor x, JsContext ctx) { _for(); spaceOpt(); leftParen(); @@ -503,11 +486,10 @@ public class JsToStringGenerationVisitor extends JsVisitor { _nestedPush(x.getBody()); accept(x.getBody()); _nestedPop(x.getBody()); - return false; } @Override - public boolean visit(JsForIn x, JsContext ctx) { + public void visitForIn(JsForIn x, JsContext ctx) { _for(); spaceOpt(); leftParen(); @@ -539,11 +521,10 @@ public class JsToStringGenerationVisitor extends JsVisitor { _nestedPush(x.getBody()); accept(x.getBody()); _nestedPop(x.getBody()); - return false; } @Override - public boolean visit(JsFunction x, JsContext ctx) { + public void visitFunction(JsFunction x, JsContext ctx) { p.print(CHARS_FUNCTION); space(); if (x.getName() != null) { @@ -563,11 +544,10 @@ public class JsToStringGenerationVisitor extends JsVisitor { lineBreakAfterBlock = false; accept(x.getBody()); needSemi = true; - return false; } @Override - public boolean visit(JsIf x, JsContext ctx) { + public void visitIf(JsIf x, JsContext ctx) { _if(); spaceOpt(); leftParen(); @@ -600,30 +580,27 @@ public class JsToStringGenerationVisitor extends JsVisitor { _nestedPop(elseStatement); } } - return false; } @Override - public boolean visit(JsInvocation x, JsContext ctx) { + public void visitInvocation(JsInvocation x, JsContext ctx) { printPair(x, x.getQualifier()); leftParen(); printExpressions(x.getArguments()); rightParen(); - return false; } @Override - public boolean visit(JsLabel x, JsContext ctx) { + public void visitLabel(JsLabel x, JsContext ctx) { nameOf(x); _colon(); spaceOpt(); accept(x.getStatement()); - return false; } @Override - public boolean visit(JsNameRef x, JsContext ctx) { + public void visitNameRef(JsNameRef x, JsContext ctx) { JsExpression q = x.getQualifier(); if (q != null) { parenPush(x, q, false); @@ -641,11 +618,10 @@ public class JsToStringGenerationVisitor extends JsVisitor { p.print('.'); } _nameRef(x); - return false; } @Override - public boolean visit(JsNew x, JsContext ctx) { + public void visitNew(JsNew x, JsContext ctx) { p.print(CHARS_NEW); space(); @@ -662,27 +638,25 @@ public class JsToStringGenerationVisitor extends JsVisitor { leftParen(); printExpressions(x.getArguments()); rightParen(); - - return false; } @Override - public void visit(JsNullLiteral x, JsContext ctx) { + public void visitNull(JsNullLiteral x, JsContext ctx) { p.print(CHARS_NULL); } @Override - public void visit(JsIntLiteral x, JsContext ctx) { + public void visitInt(JsIntLiteral x, JsContext ctx) { p.print(x.value); } @Override - public void visit(JsDoubleLiteral x, JsContext ctx) { + public void visitDouble(JsDoubleLiteral x, JsContext ctx) { p.print(x.value); } @Override - public boolean visit(JsObjectLiteral objectLiteral, JsContext context) { + public void visitObjectLiteral(JsObjectLiteral objectLiteral, JsContext context) { p.print('{'); if (objectLiteral.isMultiline()) { p.indentIn(); @@ -731,27 +705,24 @@ public class JsToStringGenerationVisitor extends JsVisitor { } p.print('}'); - return false; } @Override - public boolean visit(JsParameter x, JsContext ctx) { + public void visitParameter(JsParameter x, JsContext ctx) { nameOf(x); - return false; } @Override - public boolean visit(JsPostfixOperation x, JsContext ctx) { + public void visitPostfixOperation(JsPostfixOperation x, JsContext ctx) { JsUnaryOperator op = x.getOperator(); JsExpression arg = x.getArg(); // unary operators always associate correctly (I think) printPair(x, arg); p.print(op.getSymbol()); - return false; } @Override - public boolean visit(JsPrefixOperation x, JsContext ctx) { + public void visitPrefixOperation(JsPrefixOperation x, JsContext ctx) { JsUnaryOperator op = x.getOperator(); p.print(op.getSymbol()); JsExpression arg = x.getArg(); @@ -760,31 +731,20 @@ public class JsToStringGenerationVisitor extends JsVisitor { } // unary operators always associate correctly (I think) printPair(x, arg); - return false; } @Override - public boolean visit(JsProgram x, JsContext ctx) { + public void visitProgram(JsProgram x, JsContext ctx) { p.print(""); - return false; } @Override - public boolean visit(JsProgramFragment x, JsContext ctx) { + public void visitProgramFragment(JsProgramFragment x, JsContext ctx) { p.print(""); - return false; } @Override - public boolean visit(JsPropertyInitializer x, JsContext ctx) { - // Since there are separators, we actually print the property init - // in visit(JsObjectLiteral). - // - return false; - } - - @Override - public void visit(JsRegExp x, JsContext ctx) { + public void visitRegExp(JsRegExp x, JsContext ctx) { _slash(); p.print(x.getPattern()); _slash(); @@ -795,23 +755,22 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public boolean visit(JsReturn x, JsContext ctx) { + public void visitReturn(JsReturn x, JsContext ctx) { p.print(CHARS_RETURN); JsExpression expr = x.getExpression(); if (expr != null) { space(); accept(expr); } - return false; } @Override - public void visit(JsStringLiteral x, JsContext ctx) { + public void visitString(JsStringLiteral x, JsContext ctx) { p.print(javaScriptString(x.getValue())); } @Override - public boolean visit(JsSwitch x, JsContext ctx) { + public void visit(JsSwitch x, JsContext ctx) { p.print(CHARS_SWITCH); spaceOpt(); leftParen(); @@ -821,25 +780,22 @@ public class JsToStringGenerationVisitor extends JsVisitor { _blockOpen(); acceptList(x.getCases()); _blockClose(); - return false; } @Override - public boolean visit(JsLiteral.JsThisRef x, JsContext ctx) { + public void visitThis(JsLiteral.JsThisRef x, JsContext ctx) { p.print(CHARS_THIS); - return false; } @Override - public boolean visit(JsThrow x, JsContext ctx) { + public void visitThrow(JsThrow x, JsContext ctx) { p.print(CHARS_THROW); space(); accept(x.getExpression()); - return false; } @Override - public boolean visit(JsTry x, JsContext ctx) { + public void visitTry(JsTry x, JsContext ctx) { p.print(CHARS_TRY); spaceOpt(); accept(x.getTryBlock()); @@ -852,12 +808,10 @@ public class JsToStringGenerationVisitor extends JsVisitor { spaceOpt(); accept(finallyBlock); } - - return false; } @Override - public boolean visit(JsVar var, JsContext ctx) { + public void visit(JsVar var, JsContext ctx) { nameOf(var); JsExpression initExpr = var.getInitExpression(); if (initExpr != null) { @@ -870,11 +824,10 @@ public class JsToStringGenerationVisitor extends JsVisitor { rightParen(); } } - return false; } @Override - public boolean visit(JsVars vars, JsContext context) { + public void visitVars(JsVars vars, JsContext context) { var(); space(); boolean sep = false; @@ -892,11 +845,10 @@ public class JsToStringGenerationVisitor extends JsVisitor { accept(var); } - return false; } @Override - public boolean visit(JsDocComment comment, JsContext context) { + public void visitDocComment(JsDocComment comment, JsContext context) { boolean asSingleLine = comment.getTags().size() == 1; if (!asSingleLine) { newlineOpt(); @@ -929,7 +881,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { p.print((CharSequence) value); } else { - visit((JsNameRef) value, context); + visitNameRef((JsNameRef) value, context); } } @@ -950,7 +902,6 @@ public class JsToStringGenerationVisitor extends JsVisitor { if (asSingleLine) { spaceOpt(); } - return false; } protected final void newlineOpt() { diff --git a/src/com/google/dart/compiler/backend/js/ast/JsArrayAccess.java b/src/com/google/dart/compiler/backend/js/ast/JsArrayAccess.java index 3e668bf3fe1..617282de797 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsArrayAccess.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsArrayAccess.java @@ -53,7 +53,7 @@ public final class JsArrayAccess extends JsExpressionImpl { @Override public void accept(JsVisitor v, JsContext context) { - v.visit(this, context); + v.visitArrayAccess(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsArrayLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsArrayLiteral.java index 50603f0bfbc..fc79e5be9b0 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsArrayLiteral.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsArrayLiteral.java @@ -58,7 +58,7 @@ public final class JsArrayLiteral extends JsLiteral { @Override public void accept(JsVisitor v, JsContext context) { - v.visit(this, context); + v.visitArray(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsBinaryOperation.java b/src/com/google/dart/compiler/backend/js/ast/JsBinaryOperation.java index aea44e2e35c..36a6084d738 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsBinaryOperation.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsBinaryOperation.java @@ -70,7 +70,7 @@ public final class JsBinaryOperation extends JsExpressionImpl { @Override public void accept(JsVisitor v, JsContext context) { - v.visit(this, context); + v.visitBinaryExpression(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsBlock.java b/src/com/google/dart/compiler/backend/js/ast/JsBlock.java index 7b6874df47c..2f41e6dee11 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsBlock.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsBlock.java @@ -45,7 +45,7 @@ public class JsBlock extends JsNodeImpl implements JsStatement { @Override public void accept(JsVisitor v, JsContext context) { - v.visit(this, context); + v.visitBlock(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsBreak.java b/src/com/google/dart/compiler/backend/js/ast/JsBreak.java index 8932788e3fe..117e2e72964 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsBreak.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsBreak.java @@ -23,6 +23,6 @@ public final class JsBreak extends JsContinue { @Override public void accept(JsVisitor v, JsContext context) { - v.visit(this, context); + v.visitBreak(this, context); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsCase.java b/src/com/google/dart/compiler/backend/js/ast/JsCase.java index feaaa5d847c..dabd7329d40 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsCase.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsCase.java @@ -24,7 +24,7 @@ public final class JsCase extends JsSwitchMember { @Override public void accept(JsVisitor v, JsContext context) { - v.visit(this, context); + v.visitCase(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsCatch.java b/src/com/google/dart/compiler/backend/js/ast/JsCatch.java index 24a729051f8..a779f797da9 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsCatch.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsCatch.java @@ -49,7 +49,7 @@ public class JsCatch extends JsNodeImpl implements HasCondition { @Override public void accept(JsVisitor v, JsContext context) { - v.visit(this, context); + v.visitCatch(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsConditional.java b/src/com/google/dart/compiler/backend/js/ast/JsConditional.java index 7c82a97726d..24e30ffa8b7 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsConditional.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsConditional.java @@ -62,7 +62,7 @@ public final class JsConditional extends JsExpressionImpl { @Override public void accept(JsVisitor v, JsContext context) { - v.visit(this, context); + v.visitConditional(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsContinue.java b/src/com/google/dart/compiler/backend/js/ast/JsContinue.java index bd7aebbf5ca..00706fd77fb 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsContinue.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsContinue.java @@ -24,7 +24,7 @@ public class JsContinue extends JsNodeImpl implements JsStatement { @Override public void accept(JsVisitor v, JsContext context) { - v.visit(this, context); + v.visitContinue(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsDebugger.java b/src/com/google/dart/compiler/backend/js/ast/JsDebugger.java index 6d48cef820e..0a3a45b8733 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsDebugger.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsDebugger.java @@ -14,7 +14,7 @@ public class JsDebugger extends JsNodeImpl implements JsStatement { @Override public void accept(JsVisitor v, JsContext context) { - v.visit(this, context); + v.visitDebugger(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsDefault.java b/src/com/google/dart/compiler/backend/js/ast/JsDefault.java index 227228df2e1..f20fab4572f 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsDefault.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsDefault.java @@ -10,7 +10,7 @@ package com.google.dart.compiler.backend.js.ast; public final class JsDefault extends JsSwitchMember { @Override public void accept(JsVisitor v, JsContext context) { - v.visit(this, context); + v.visitDefault(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsDoWhile.java b/src/com/google/dart/compiler/backend/js/ast/JsDoWhile.java index e860a699f8b..97523c3d605 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsDoWhile.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsDoWhile.java @@ -22,9 +22,6 @@ public class JsDoWhile extends JsWhile { @Override public void accept(JsVisitor v, JsContext context) { - if (v.visit(this, context)) { - condition = v.accept(condition); - body = v.accept(body); - } + v.visitDoWhile(this, context); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsDocComment.java b/src/com/google/dart/compiler/backend/js/ast/JsDocComment.java index b432b45a6d1..92ec026b93b 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsDocComment.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsDocComment.java @@ -29,12 +29,11 @@ public class JsDocComment extends JsExpressionImpl { @Override public void accept(JsVisitor v, JsContext context) { - v.visit(this, context); + v.visitDocComment(this, context); } @Override public void acceptChildren(JsVisitor visitor, JsContext context) { - } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsEmpty.java b/src/com/google/dart/compiler/backend/js/ast/JsEmpty.java index 31ab7908cd0..8229617fe6d 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsEmpty.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsEmpty.java @@ -10,7 +10,7 @@ public class JsEmpty extends JsNodeImpl implements JsStatement { @Override public void accept(JsVisitor v, JsContext context) { - v.visit(this, context); + v.visitEmpty(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsExpressionStatement.java b/src/com/google/dart/compiler/backend/js/ast/JsExpressionStatement.java index deb413ee43d..ae680e65afa 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsExpressionStatement.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsExpressionStatement.java @@ -17,7 +17,7 @@ public final class JsExpressionStatement extends AbstractNode implements JsState @Override public void accept(JsVisitor v, JsContext context) { - v.visit(this, context); + v.visitExpressionStatement(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsFor.java b/src/com/google/dart/compiler/backend/js/ast/JsFor.java index d76f7ec50f9..c7bca577adb 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsFor.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsFor.java @@ -72,7 +72,7 @@ public class JsFor extends JsNodeImpl implements JsStatement { @Override public void accept(JsVisitor v, JsContext context) { - v.visit(this, context); + v.visitFor(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsForIn.java b/src/com/google/dart/compiler/backend/js/ast/JsForIn.java index 7fa3edd4fa9..68e7d598be4 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsForIn.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsForIn.java @@ -53,9 +53,7 @@ public class JsForIn extends JsNodeImpl implements JsStatement { @Override public void accept(JsVisitor v, JsContext context) { - if (v.visit(this, context)) { - - } + v.visitForIn(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsFunction.java b/src/com/google/dart/compiler/backend/js/ast/JsFunction.java index ae1a1ef85b2..21b0fdbe98e 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsFunction.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsFunction.java @@ -91,7 +91,7 @@ public final class JsFunction extends JsLiteral implements HasName { @Override public void accept(JsVisitor v, JsContext context) { - v.visit(this, context); + v.visitFunction(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsIf.java b/src/com/google/dart/compiler/backend/js/ast/JsIf.java index d7e93226d57..aa4bc22bc22 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsIf.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsIf.java @@ -52,7 +52,7 @@ public final class JsIf extends JsNodeImpl implements JsStatement { @Override public void accept(JsVisitor v, JsContext context) { - v.visit(this, context); + v.visitIf(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsInvocation.java b/src/com/google/dart/compiler/backend/js/ast/JsInvocation.java index 3c9bc107e87..3e887940b0f 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsInvocation.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsInvocation.java @@ -68,7 +68,7 @@ public final class JsInvocation extends JsExpressionImpl.JsExpressionHasArgument @Override public void accept(JsVisitor v, JsContext context) { - v.visit(this, context); + v.visitInvocation(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsLabel.java b/src/com/google/dart/compiler/backend/js/ast/JsLabel.java index b4dd4ca2355..dbe63cc237e 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsLabel.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsLabel.java @@ -43,7 +43,7 @@ public class JsLabel extends JsNodeImpl implements JsStatement, HasName { @Override public void accept(JsVisitor v, JsContext context) { - v.visit(this, context); + v.visitLabel(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsLiteral.java index bc9eee97cd1..edfd17a1a90 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsLiteral.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsLiteral.java @@ -44,7 +44,7 @@ public abstract class JsLiteral extends JsExpressionImpl implements CanBooleanEv @Override public void accept(JsVisitor v, JsContext context) { - v.visit(this, context); + v.visitThis(this, context); } @Override @@ -87,7 +87,7 @@ public abstract class JsLiteral extends JsExpressionImpl implements CanBooleanEv @Override public void accept(JsVisitor v, JsContext context) { - v.visit(this, context); + v.visitBoolean(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNameRef.java b/src/com/google/dart/compiler/backend/js/ast/JsNameRef.java index c877630db1c..ce9eeca248b 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsNameRef.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsNameRef.java @@ -95,7 +95,7 @@ public final class JsNameRef extends JsExpressionImpl implements CanBooleanEval, @Override public void accept(JsVisitor v, JsContext context) { - v.visit(this, context); + v.visitNameRef(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNew.java b/src/com/google/dart/compiler/backend/js/ast/JsNew.java index dac28938a62..e5adf766a7f 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsNew.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsNew.java @@ -43,7 +43,7 @@ public final class JsNew extends JsExpressionImpl.JsExpressionHasArguments { @Override public void accept(JsVisitor v, JsContext context) { - v.visit(this, context); + v.visitNew(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNullLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsNullLiteral.java index 3974b1a5965..a17ac877ef6 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsNullLiteral.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsNullLiteral.java @@ -30,7 +30,7 @@ public final class JsNullLiteral extends JsLiteral.JsValueLiteral { @Override public void accept(JsVisitor v, JsContext context) { - v.visit(this, context); + v.visitNull(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNumberLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsNumberLiteral.java index 8df65c40b7e..e77dceefb17 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsNumberLiteral.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsNumberLiteral.java @@ -39,7 +39,7 @@ public abstract class JsNumberLiteral extends JsLiteral.JsValueLiteral { @Override public void accept(JsVisitor v, JsContext context) { - v.visit(this, context); + v.visitDouble(this, context); } public String toString() { @@ -66,7 +66,7 @@ public abstract class JsNumberLiteral extends JsLiteral.JsValueLiteral { @Override public void accept(JsVisitor v, JsContext context) { - v.visit(this, context); + v.visitInt(this, context); } public String toString() { diff --git a/src/com/google/dart/compiler/backend/js/ast/JsObjectLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsObjectLiteral.java index bbae493479a..7cc65fc7b8a 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsObjectLiteral.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsObjectLiteral.java @@ -70,7 +70,7 @@ public final class JsObjectLiteral extends JsLiteral { @Override public void accept(JsVisitor v, JsContext context) { - v.visit(this, context); + v.visitObjectLiteral(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsParameter.java b/src/com/google/dart/compiler/backend/js/ast/JsParameter.java index 5d87432ce7f..68e3b313375 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsParameter.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsParameter.java @@ -28,7 +28,7 @@ public final class JsParameter extends JsNodeImpl implements HasName { @Override public void accept(JsVisitor v, JsContext context) { - v.visit(this, context); + v.visitParameter(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsPostfixOperation.java b/src/com/google/dart/compiler/backend/js/ast/JsPostfixOperation.java index 177b6f79b60..72efa7868c4 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsPostfixOperation.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsPostfixOperation.java @@ -29,7 +29,7 @@ public final class JsPostfixOperation extends JsUnaryOperation { @Override public void accept(JsVisitor v, JsContext context) { - v.visit(this, context); + v.visitPostfixOperation(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsPrefixOperation.java b/src/com/google/dart/compiler/backend/js/ast/JsPrefixOperation.java index 6c0724af93d..ba0e3458c76 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsPrefixOperation.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsPrefixOperation.java @@ -47,7 +47,7 @@ public final class JsPrefixOperation extends JsUnaryOperation implements CanBool @Override public void accept(JsVisitor v, JsContext context) { - v.visit(this, context); + v.visitPrefixOperation(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsProgram.java b/src/com/google/dart/compiler/backend/js/ast/JsProgram.java index 6b71bea47a2..5eaee9c19c3 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsProgram.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsProgram.java @@ -110,7 +110,7 @@ public final class JsProgram extends JsNodeImpl { @Override public void accept(JsVisitor v, @Nullable JsContext context) { - v.visit(this, context); + v.visitProgram(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsProgramFragment.java b/src/com/google/dart/compiler/backend/js/ast/JsProgramFragment.java index 3de7dfda2a7..3f1b8d0ce77 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsProgramFragment.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsProgramFragment.java @@ -20,7 +20,7 @@ public class JsProgramFragment extends JsNodeImpl { @Override public void accept(JsVisitor v, JsContext context) { - v.visit(this, context); + v.visitProgramFragment(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsPropertyInitializer.java b/src/com/google/dart/compiler/backend/js/ast/JsPropertyInitializer.java index e057f6e8ecf..5e184f071f6 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsPropertyInitializer.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsPropertyInitializer.java @@ -40,7 +40,7 @@ public class JsPropertyInitializer extends JsNodeImpl { @Override public void accept(JsVisitor v, JsContext context) { - v.visit(this, context); + v.visitPropertyInitializer(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsRegExp.java b/src/com/google/dart/compiler/backend/js/ast/JsRegExp.java index 18a7ca46b56..2015a5f8b8c 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsRegExp.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsRegExp.java @@ -52,7 +52,7 @@ public final class JsRegExp extends JsLiteral.JsValueLiteral { @Override public void accept(JsVisitor v, JsContext context) { - v.visit(this, context); + v.visitRegExp(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsReturn.java b/src/com/google/dart/compiler/backend/js/ast/JsReturn.java index a347f8bd547..ee798d5573f 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsReturn.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsReturn.java @@ -27,7 +27,7 @@ public final class JsReturn extends JsNodeImpl implements JsStatement { @Override public void accept(JsVisitor v, JsContext context) { - v.visit(this, context); + v.visitReturn(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsStringLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsStringLiteral.java index d063843a626..68448bda891 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsStringLiteral.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsStringLiteral.java @@ -42,7 +42,7 @@ public final class JsStringLiteral extends JsLiteral.JsValueLiteral { @Override public void accept(JsVisitor v, JsContext context) { - v.visit(this, context); + v.visitString(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsThrow.java b/src/com/google/dart/compiler/backend/js/ast/JsThrow.java index 9345d31d15f..164d2dae0f2 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsThrow.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsThrow.java @@ -24,7 +24,7 @@ public class JsThrow extends JsNodeImpl implements JsStatement { @Override public void accept(JsVisitor v, JsContext context) { - v.visit(this, context); + v.visitThrow(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsTry.java b/src/com/google/dart/compiler/backend/js/ast/JsTry.java index ebd3d1b2888..3625bd0ebca 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsTry.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsTry.java @@ -49,7 +49,7 @@ public class JsTry extends JsNodeImpl implements JsStatement { @Override public void accept(JsVisitor v, JsContext context) { - v.visit(this, context); + v.visitTry(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsVars.java b/src/com/google/dart/compiler/backend/js/ast/JsVars.java index 359199e77b3..c88016f0de7 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsVars.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsVars.java @@ -119,7 +119,7 @@ public class JsVars extends JsNodeImpl implements JsStatement, Iterable T accept(T node) { - doTraverse(node, UNMODIFIABLE_CONTEXT); + doAccept(node, UNMODIFIABLE_CONTEXT); return node; } public final void acceptList(List collection) { for (T node : collection) { - doTraverse(node, UNMODIFIABLE_CONTEXT); + doAccept(node, UNMODIFIABLE_CONTEXT); } } public JsExpression acceptLvalue(JsExpression expr) { - doTraverse(expr, LVALUE_CONTEXT); + doAccept(expr, LVALUE_CONTEXT); return expr; } public final void acceptWithInsertRemove(List collection) { for (T node : collection) { - doTraverse(node, UNMODIFIABLE_CONTEXT); + doAccept(node, UNMODIFIABLE_CONTEXT); } } - public boolean visit(JsArrayAccess x, JsContext ctx) { - return true; + public void visitArrayAccess(JsArrayAccess x, JsContext context) { + visitElement(x, context); } - public boolean visit(JsArrayLiteral x, JsContext ctx) { - return true; + public void visitArray(JsArrayLiteral x, JsContext context) { + visitElement(x, context); } - public boolean visit(JsBinaryOperation x, JsContext ctx) { - return true; + public void visitBinaryExpression(JsBinaryOperation x, JsContext context) { + visitElement(x, context); } - public boolean visit(JsBlock x, JsContext ctx) { - return true; + public void visitBlock(JsBlock x, JsContext context) { + visitElement(x, context); } - public boolean visit(JsLiteral.JsBooleanLiteral x, JsContext ctx) { - return true; + public void visitBoolean(JsLiteral.JsBooleanLiteral x, JsContext context) { + visitElement(x, context); } - public boolean visit(JsBreak x, JsContext ctx) { - return true; + public void visitBreak(JsBreak x, JsContext context) { + visitElement(x, context); } - public boolean visit(JsCase x, JsContext ctx) { - return true; + public void visitCase(JsCase x, JsContext ctx) { + visitElement(x, ctx); } - public boolean visit(JsCatch x, JsContext ctx) { - return true; + public void visitCatch(JsCatch x, JsContext ctx) { + visitElement(x, ctx); } - public boolean visit(JsConditional x, JsContext ctx) { - return true; + public void visitConditional(JsConditional x, JsContext ctx) { + visitElement(x, ctx); } - public boolean visit(JsContinue x, JsContext ctx) { - return true; + public void visitContinue(JsContinue x, JsContext ctx) { + visitElement(x, ctx); } - public boolean visit(JsDebugger x, JsContext ctx) { - return true; + public void visitDebugger(JsDebugger x, JsContext ctx) { + visitElement(x, ctx); } - public boolean visit(JsDefault x, JsContext ctx) { - return true; + public void visitDefault(JsDefault x, JsContext ctx) { + visitElement(x, ctx); } - public boolean visit(JsDoWhile x, JsContext ctx) { - return true; + public void visitDoWhile(JsDoWhile x, JsContext ctx) { + visitElement(x, ctx); } - public void visit(JsEmpty x, JsContext ctx) { + public void visitEmpty(JsEmpty x, JsContext ctx) { + visitElement(x, ctx); } - public boolean visit(JsExpressionStatement x, JsContext ctx) { - return true; + public void visitExpressionStatement(JsExpressionStatement x, JsContext context) { + visitElement(x, context); } - public boolean visit(JsFor x, JsContext ctx) { - return true; + public void visitFor(JsFor x, JsContext ctx) { + visitElement(x, ctx); } - public boolean visit(JsForIn x, JsContext ctx) { - return true; + public void visitForIn(JsForIn x, JsContext ctx) { + visitElement(x, ctx); } - public boolean visit(JsFunction x, JsContext ctx) { - return true; + public void visitFunction(JsFunction x, JsContext ctx) { + visitElement(x, ctx); } - public boolean visit(JsIf x, JsContext ctx) { - return true; + public void visitIf(JsIf x, JsContext ctx) { + visitElement(x, ctx); } - public boolean visit(JsInvocation x, JsContext ctx) { - return true; + public void visitInvocation(JsInvocation x, JsContext ctx) { + visitElement(x, ctx); } - public boolean visit(JsLabel x, JsContext ctx) { - return true; + public void visitLabel(JsLabel x, JsContext ctx) { + visitElement(x, ctx); } - public boolean visit(JsNameRef x, JsContext ctx) { - return true; + public void visitNameRef(JsNameRef x, JsContext ctx) { + visitElement(x, ctx); } - public boolean visit(JsNew x, JsContext ctx) { - return true; + public void visitNew(JsNew x, JsContext ctx) { + visitElement(x, ctx); } - public void visit(JsNullLiteral x, JsContext ctx) { + public void visitNull(JsNullLiteral x, JsContext ctx) { + visitElement(x, ctx); } - public void visit(JsNumberLiteral.JsIntLiteral x, JsContext ctx) { + public void visitInt(JsNumberLiteral.JsIntLiteral x, JsContext context) { + visitElement(x, context); } - public void visit(JsNumberLiteral.JsDoubleLiteral x, JsContext ctx) { + public void visitDouble(JsNumberLiteral.JsDoubleLiteral x, JsContext context) { + visitElement(x, context); } - public boolean visit(JsObjectLiteral x, JsContext ctx) { - return true; + public void visitObjectLiteral(JsObjectLiteral x, JsContext context) { + visitElement(x, context); } - public boolean visit(JsParameter x, JsContext ctx) { - return true; + public void visitParameter(JsParameter x, JsContext context) { + visitElement(x, context); } - public boolean visit(JsPostfixOperation x, JsContext ctx) { - return true; + public void visitPostfixOperation(JsPostfixOperation x, JsContext context) { + visitElement(x, context); } - public boolean visit(JsPrefixOperation x, JsContext ctx) { - return true; + public void visitPrefixOperation(JsPrefixOperation x, JsContext context) { + visitElement(x, context); } - public boolean visit(JsProgram x, JsContext ctx) { - return true; + public void visitProgram(JsProgram x, JsContext context) { + visitElement(x, context); } - public boolean visit(JsProgramFragment x, JsContext ctx) { - return true; + public void visitProgramFragment(JsProgramFragment x, JsContext context) { + visitElement(x, context); } - public boolean visit(JsPropertyInitializer x, JsContext ctx) { - return true; + public void visitPropertyInitializer(JsPropertyInitializer x, JsContext context) { + visitElement(x, context); } - public void visit(JsRegExp x, JsContext ctx) { + public void visitRegExp(JsRegExp x, JsContext context) { + visitElement(x, context); } - public boolean visit(JsReturn x, JsContext ctx) { - return true; + public void visitReturn(JsReturn x, JsContext context) { + visitElement(x, context); } - public void visit(JsStringLiteral x, JsContext ctx) { + public void visitString(JsStringLiteral x, JsContext context) { + visitElement(x, context); } - public boolean visit(JsSwitch x, JsContext ctx) { - return true; + public void visit(JsSwitch x, JsContext context) { + visitElement(x, context); } - public boolean visit(JsLiteral.JsThisRef x, JsContext ctx) { - return true; + public void visitThis(JsLiteral.JsThisRef x, JsContext context) { + visitElement(x, context); } - public boolean visit(JsThrow x, JsContext ctx) { - return true; + public void visitThrow(JsThrow x, JsContext context) { + visitElement(x, context); } - public boolean visit(JsTry x, JsContext ctx) { - return true; + public void visitTry(JsTry x, JsContext context) { + visitElement(x, context); } - public boolean visit(JsVar x, JsContext ctx) { - return true; + public void visit(JsVar x, JsContext context) { + visitElement(x, context); } - public boolean visit(JsVars x, JsContext ctx) { - return true; + public void visitVars(JsVars x, JsContext context) { + visitElement(x, context); } - public boolean visit(JsWhile x, JsContext ctx) { - return true; + public void visitWhile(JsWhile x, JsContext context) { + visitElement(x, context); } - public boolean visit(JsDocComment x, JsContext context) { - return true; + public void visitDocComment(JsDocComment comment, JsContext context) { + visitElement(comment, context); } - protected void doTraverse(JsNode node, JsContext context) { + protected void visitElement(JsNode node, JsContext context) { + } + + protected void doAccept(JsNode node, JsContext context) { node.accept(this, context); } } \ No newline at end of file diff --git a/src/com/google/dart/compiler/backend/js/ast/JsWhile.java b/src/com/google/dart/compiler/backend/js/ast/JsWhile.java index 6e7d24d0d61..646870dae1a 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsWhile.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsWhile.java @@ -37,7 +37,7 @@ public class JsWhile extends JsNodeImpl implements JsStatement { @Override public void accept(JsVisitor v, JsContext context) { - v.visit(this, context); + v.visitWhile(this, context); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/RecursiveJsVisitor.java b/src/com/google/dart/compiler/backend/js/ast/RecursiveJsVisitor.java index 6f3b9c4f495..c148969f425 100644 --- a/src/com/google/dart/compiler/backend/js/ast/RecursiveJsVisitor.java +++ b/src/com/google/dart/compiler/backend/js/ast/RecursiveJsVisitor.java @@ -1,4 +1,8 @@ package com.google.dart.compiler.backend.js.ast; public abstract class RecursiveJsVisitor extends JsVisitor { + @Override + protected void visitElement(JsNode node, JsContext context) { + node.acceptChildren(this, context); + } } From d23bca091e74aca3e9f8d88d55f4bf6ae91e2e19 Mon Sep 17 00:00:00 2001 From: develar Date: Thu, 25 Oct 2012 09:34:01 +0400 Subject: [PATCH 19/27] simplify AST (idea like) --- .../js/JsToStringGenerationVisitor.java | 16 ++++++------ .../dart/compiler/backend/js/ast/JsForIn.java | 26 +++++++++---------- .../dart/compiler/backend/js/ast/JsNode.java | 4 ++- .../compiler/backend/js/ast/JsVisitor.java | 12 ++++----- 4 files changed, 30 insertions(+), 28 deletions(-) diff --git a/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java b/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java index 09d06545a94..e85b2eea51e 100644 --- a/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java +++ b/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java @@ -388,12 +388,12 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public void visitDebugger(JsDebugger x, JsContext ctx) { + public void visitDebugger(JsDebugger x, JsContext context) { p.print(CHARS_DEBUGGER); } @Override - public void visitDefault(JsDefault x, JsContext ctx) { + public void visitDefault(JsDefault x, JsContext context) { p.print(CHARS_DEFAULT); _colon(); @@ -413,7 +413,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public void visitDoWhile(JsDoWhile x, JsContext ctx) { + public void visitDoWhile(JsDoWhile x, JsContext context) { p.print(CHARS_DO); _nestedPush(x.getBody()); accept(x.getBody()); @@ -499,23 +499,23 @@ public class JsToStringGenerationVisitor extends JsVisitor { space(); nameDef(x.getIterVarName()); - if (x.getIterExpr() != null) { + if (x.getIterExpression() != null) { spaceOpt(); assignment(); spaceOpt(); - accept(x.getIterExpr()); + accept(x.getIterExpression()); } } else { // Just a name ref. // - accept(x.getIterExpr()); + accept(x.getIterExpression()); } space(); p.print(CHARS_IN); space(); - accept(x.getObjExpr()); + accept(x.getObjectExpression()); rightParen(); _nestedPush(x.getBody()); @@ -734,7 +734,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public void visitProgram(JsProgram x, JsContext ctx) { + public void visitProgram(JsProgram x, JsContext context) { p.print(""); } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsForIn.java b/src/com/google/dart/compiler/backend/js/ast/JsForIn.java index 68e7d598be4..f635252dc0f 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsForIn.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsForIn.java @@ -9,8 +9,8 @@ package com.google.dart.compiler.backend.js.ast; */ public class JsForIn extends JsNodeImpl implements JsStatement { private JsStatement body; - private JsExpression iterExpr; - private JsExpression objExpr; + private JsExpression iterExpression; + private JsExpression objectExpression; // Optional: the name of a new iterator variable to introduce private final JsName iterVarName; @@ -27,28 +27,28 @@ public class JsForIn extends JsNodeImpl implements JsStatement { return body; } - public JsExpression getIterExpr() { - return iterExpr; + public JsExpression getIterExpression() { + return iterExpression; } public JsName getIterVarName() { return iterVarName; } - public JsExpression getObjExpr() { - return objExpr; + public JsExpression getObjectExpression() { + return objectExpression; } public void setBody(JsStatement body) { this.body = body; } - public void setIterExpr(JsExpression iterExpr) { - this.iterExpr = iterExpr; + public void setIterExpression(JsExpression iterExpression) { + this.iterExpression = iterExpression; } - public void setObjExpr(JsExpression objExpr) { - this.objExpr = objExpr; + public void setObjectExpression(JsExpression objectExpression) { + this.objectExpression = objectExpression; } @Override @@ -58,10 +58,10 @@ public class JsForIn extends JsNodeImpl implements JsStatement { @Override public void acceptChildren(JsVisitor visitor, JsContext context) { - if (iterExpr != null) { - iterExpr = visitor.acceptLvalue(iterExpr); + if (iterExpression != null) { + iterExpression = visitor.acceptLvalue(iterExpression); } - objExpr = visitor.accept(objExpr); + objectExpression = visitor.accept(objectExpression); body = visitor.accept(body); } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNode.java b/src/com/google/dart/compiler/backend/js/ast/JsNode.java index 83fcb376af5..cd7e268fdb5 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsNode.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsNode.java @@ -4,7 +4,9 @@ package com.google.dart.compiler.backend.js.ast; -public interface JsNode { +import com.google.dart.compiler.common.HasSourceInfo; + +public interface JsNode extends HasSourceInfo { NodeKind getKind(); /** diff --git a/src/com/google/dart/compiler/backend/js/ast/JsVisitor.java b/src/com/google/dart/compiler/backend/js/ast/JsVisitor.java index 66f9cf30410..1d837c78a17 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsVisitor.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsVisitor.java @@ -145,16 +145,16 @@ public abstract class JsVisitor { visitElement(x, ctx); } - public void visitDebugger(JsDebugger x, JsContext ctx) { - visitElement(x, ctx); + public void visitDebugger(JsDebugger x, JsContext context) { + visitElement(x, context); } - public void visitDefault(JsDefault x, JsContext ctx) { - visitElement(x, ctx); + public void visitDefault(JsDefault x, JsContext context) { + visitElement(x, context); } - public void visitDoWhile(JsDoWhile x, JsContext ctx) { - visitElement(x, ctx); + public void visitDoWhile(JsDoWhile x, JsContext context) { + visitElement(x, context); } public void visitEmpty(JsEmpty x, JsContext ctx) { From ca016fcb32c612f21c8ac8bde9ac6b9157cf593a Mon Sep 17 00:00:00 2001 From: develar Date: Thu, 25 Oct 2012 09:43:19 +0400 Subject: [PATCH 20/27] simplify AST (idea like) --- .../backend/js/ast/ChameleonJsExpression.java | 5 -- .../backend/js/ast/JsArrayAccess.java | 5 -- .../backend/js/ast/JsArrayLiteral.java | 5 -- .../backend/js/ast/JsBinaryOperation.java | 5 -- .../dart/compiler/backend/js/ast/JsBlock.java | 5 -- .../dart/compiler/backend/js/ast/JsBreak.java | 5 -- .../dart/compiler/backend/js/ast/JsCase.java | 5 -- .../dart/compiler/backend/js/ast/JsCatch.java | 5 -- .../backend/js/ast/JsConditional.java | 5 -- .../compiler/backend/js/ast/JsContinue.java | 5 -- .../compiler/backend/js/ast/JsDebugger.java | 18 ++--- .../compiler/backend/js/ast/JsDefault.java | 13 +--- .../compiler/backend/js/ast/JsDoWhile.java | 5 -- .../compiler/backend/js/ast/JsDocComment.java | 5 -- .../dart/compiler/backend/js/ast/JsEmpty.java | 5 -- .../backend/js/ast/JsExpressionStatement.java | 5 -- .../dart/compiler/backend/js/ast/JsFor.java | 5 -- .../dart/compiler/backend/js/ast/JsForIn.java | 5 -- .../compiler/backend/js/ast/JsFunction.java | 5 -- .../dart/compiler/backend/js/ast/JsIf.java | 5 -- .../compiler/backend/js/ast/JsInvocation.java | 5 -- .../dart/compiler/backend/js/ast/JsLabel.java | 9 +-- .../compiler/backend/js/ast/JsLiteral.java | 10 --- .../compiler/backend/js/ast/JsNameRef.java | 5 -- .../dart/compiler/backend/js/ast/JsNew.java | 5 -- .../dart/compiler/backend/js/ast/JsNode.java | 2 - .../backend/js/ast/JsNullLiteral.java | 5 -- .../backend/js/ast/JsNumberLiteral.java | 5 -- .../backend/js/ast/JsObjectLiteral.java | 5 -- .../compiler/backend/js/ast/JsParameter.java | 5 -- .../backend/js/ast/JsPostfixOperation.java | 5 -- .../backend/js/ast/JsPrefixOperation.java | 75 +++++++++---------- .../compiler/backend/js/ast/JsProgram.java | 5 -- .../backend/js/ast/JsProgramFragment.java | 5 -- .../backend/js/ast/JsPropertyInitializer.java | 5 -- .../compiler/backend/js/ast/JsRegExp.java | 5 -- .../compiler/backend/js/ast/JsReturn.java | 9 +-- .../backend/js/ast/JsStringLiteral.java | 5 -- .../compiler/backend/js/ast/JsSwitch.java | 5 -- .../dart/compiler/backend/js/ast/JsThrow.java | 5 -- .../dart/compiler/backend/js/ast/JsTry.java | 5 -- .../dart/compiler/backend/js/ast/JsVars.java | 10 --- .../dart/compiler/backend/js/ast/JsWhile.java | 7 +- .../compiler/backend/js/ast/NodeKind.java | 50 ------------- 44 files changed, 50 insertions(+), 323 deletions(-) delete mode 100644 src/com/google/dart/compiler/backend/js/ast/NodeKind.java diff --git a/src/com/google/dart/compiler/backend/js/ast/ChameleonJsExpression.java b/src/com/google/dart/compiler/backend/js/ast/ChameleonJsExpression.java index d926ef6ec46..e3c0a06c85a 100644 --- a/src/com/google/dart/compiler/backend/js/ast/ChameleonJsExpression.java +++ b/src/com/google/dart/compiler/backend/js/ast/ChameleonJsExpression.java @@ -39,11 +39,6 @@ public class ChameleonJsExpression implements JsExpression { return expression.makeStmt(); } - @Override - public NodeKind getKind() { - return expression.getKind(); - } - @Override public void accept(JsVisitor visitor, JsContext context) { expression.accept(visitor, context); diff --git a/src/com/google/dart/compiler/backend/js/ast/JsArrayAccess.java b/src/com/google/dart/compiler/backend/js/ast/JsArrayAccess.java index 617282de797..338ab95c08d 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsArrayAccess.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsArrayAccess.java @@ -61,9 +61,4 @@ public final class JsArrayAccess extends JsExpressionImpl { arrayExpression = visitor.accept(arrayExpression); indexExpression = visitor.accept(indexExpression); } - - @Override - public NodeKind getKind() { - return NodeKind.ARRAY_ACCESS; - } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsArrayLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsArrayLiteral.java index fc79e5be9b0..6358d3c34e7 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsArrayLiteral.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsArrayLiteral.java @@ -65,9 +65,4 @@ public final class JsArrayLiteral extends JsLiteral { public void acceptChildren(JsVisitor visitor, JsContext context) { visitor.acceptWithInsertRemove(expressions); } - - @Override - public NodeKind getKind() { - return NodeKind.ARRAY; - } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsBinaryOperation.java b/src/com/google/dart/compiler/backend/js/ast/JsBinaryOperation.java index 36a6084d738..1c82f99bc97 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsBinaryOperation.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsBinaryOperation.java @@ -83,9 +83,4 @@ public final class JsBinaryOperation extends JsExpressionImpl { } arg2 = visitor.accept(arg2); } - - @Override - public NodeKind getKind() { - return NodeKind.BINARY_OP; - } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsBlock.java b/src/com/google/dart/compiler/backend/js/ast/JsBlock.java index 2f41e6dee11..a6b133071f3 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsBlock.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsBlock.java @@ -52,9 +52,4 @@ public class JsBlock extends JsNodeImpl implements JsStatement { public void acceptChildren(JsVisitor visitor, JsContext context) { visitor.acceptWithInsertRemove(statements); } - - @Override - public NodeKind getKind() { - return NodeKind.BLOCK; - } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsBreak.java b/src/com/google/dart/compiler/backend/js/ast/JsBreak.java index 117e2e72964..4513a153a45 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsBreak.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsBreak.java @@ -16,11 +16,6 @@ public final class JsBreak extends JsContinue { super(label); } - @Override - public NodeKind getKind() { - return NodeKind.BREAK; - } - @Override public void accept(JsVisitor v, JsContext context) { v.visitBreak(this, context); diff --git a/src/com/google/dart/compiler/backend/js/ast/JsCase.java b/src/com/google/dart/compiler/backend/js/ast/JsCase.java index dabd7329d40..1ccc4c1349b 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsCase.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsCase.java @@ -32,9 +32,4 @@ public final class JsCase extends JsSwitchMember { caseExpression = visitor.accept(caseExpression); super.acceptChildren(visitor, context); } - - @Override - public NodeKind getKind() { - return NodeKind.CASE; - } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsCatch.java b/src/com/google/dart/compiler/backend/js/ast/JsCatch.java index a779f797da9..9fb2f5ae653 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsCatch.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsCatch.java @@ -60,9 +60,4 @@ public class JsCatch extends JsNodeImpl implements HasCondition { } body = visitor.accept(body); } - - @Override - public NodeKind getKind() { - return NodeKind.CATCH; - } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsConditional.java b/src/com/google/dart/compiler/backend/js/ast/JsConditional.java index 24e30ffa8b7..36c73480ce1 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsConditional.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsConditional.java @@ -71,9 +71,4 @@ public final class JsConditional extends JsExpressionImpl { thenExpr = visitor.accept(thenExpr); elseExpr = visitor.accept(elseExpr); } - - @Override - public NodeKind getKind() { - return NodeKind.CONDITIONAL; - } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsContinue.java b/src/com/google/dart/compiler/backend/js/ast/JsContinue.java index 00706fd77fb..3aa329aa51c 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsContinue.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsContinue.java @@ -31,9 +31,4 @@ public class JsContinue extends JsNodeImpl implements JsStatement { public void acceptChildren(JsVisitor visitor, JsContext context) { } - - @Override - public NodeKind getKind() { - return NodeKind.CONTINUE; - } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsDebugger.java b/src/com/google/dart/compiler/backend/js/ast/JsDebugger.java index 0a3a45b8733..973bc5210dc 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsDebugger.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsDebugger.java @@ -8,22 +8,16 @@ package com.google.dart.compiler.backend.js.ast; * Represents a JavaScript debugger statement. */ public class JsDebugger extends JsNodeImpl implements JsStatement { + public JsDebugger() { + } - public JsDebugger() { - } - - @Override - public void accept(JsVisitor v, JsContext context) { - v.visitDebugger(this, context); - } + @Override + public void accept(JsVisitor v, JsContext context) { + v.visitDebugger(this, context); + } @Override public void acceptChildren(JsVisitor visitor, JsContext context) { } - - @Override - public NodeKind getKind() { - return NodeKind.DEBUGGER; - } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsDefault.java b/src/com/google/dart/compiler/backend/js/ast/JsDefault.java index f20fab4572f..4a83b7c4979 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsDefault.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsDefault.java @@ -8,13 +8,8 @@ package com.google.dart.compiler.backend.js.ast; * Represents the default option in a JavaScript swtich statement. */ public final class JsDefault extends JsSwitchMember { - @Override - public void accept(JsVisitor v, JsContext context) { - v.visitDefault(this, context); - } - - @Override - public NodeKind getKind() { - return NodeKind.DEFAULT; - } + @Override + public void accept(JsVisitor v, JsContext context) { + v.visitDefault(this, context); + } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsDoWhile.java b/src/com/google/dart/compiler/backend/js/ast/JsDoWhile.java index 97523c3d605..87dbf9a7636 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsDoWhile.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsDoWhile.java @@ -15,11 +15,6 @@ public class JsDoWhile extends JsWhile { super(condition, body); } - @Override - public NodeKind getKind() { - return NodeKind.DO; - } - @Override public void accept(JsVisitor v, JsContext context) { v.visitDoWhile(this, context); diff --git a/src/com/google/dart/compiler/backend/js/ast/JsDocComment.java b/src/com/google/dart/compiler/backend/js/ast/JsDocComment.java index 92ec026b93b..2c5ee580316 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsDocComment.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsDocComment.java @@ -22,11 +22,6 @@ public class JsDocComment extends JsExpressionImpl { tags = Collections.singletonMap(tagName, tagValue); } - @Override - public NodeKind getKind() { - throw new UnsupportedOperationException(); - } - @Override public void accept(JsVisitor v, JsContext context) { v.visitDocComment(this, context); diff --git a/src/com/google/dart/compiler/backend/js/ast/JsEmpty.java b/src/com/google/dart/compiler/backend/js/ast/JsEmpty.java index 8229617fe6d..0e50c6108aa 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsEmpty.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsEmpty.java @@ -16,9 +16,4 @@ public class JsEmpty extends JsNodeImpl implements JsStatement { @Override public void acceptChildren(JsVisitor visitor, JsContext context) { } - - @Override - public NodeKind getKind() { - return NodeKind.EMPTY; - } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsExpressionStatement.java b/src/com/google/dart/compiler/backend/js/ast/JsExpressionStatement.java index ae680e65afa..5c1f439b0cc 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsExpressionStatement.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsExpressionStatement.java @@ -25,11 +25,6 @@ public final class JsExpressionStatement extends AbstractNode implements JsState expression = visitor.accept(expression); } - @Override - public NodeKind getKind() { - return NodeKind.EXPRESSION_STMT; - } - @Override public Object getSourceInfo() { return null; diff --git a/src/com/google/dart/compiler/backend/js/ast/JsFor.java b/src/com/google/dart/compiler/backend/js/ast/JsFor.java index c7bca577adb..533bb0abfc9 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsFor.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsFor.java @@ -95,9 +95,4 @@ public class JsFor extends JsNodeImpl implements JsStatement { } body = visitor.accept(body); } - - @Override - public NodeKind getKind() { - return NodeKind.FOR; - } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsForIn.java b/src/com/google/dart/compiler/backend/js/ast/JsForIn.java index f635252dc0f..52697eda9c7 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsForIn.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsForIn.java @@ -64,9 +64,4 @@ public class JsForIn extends JsNodeImpl implements JsStatement { objectExpression = visitor.accept(objectExpression); body = visitor.accept(body); } - - @Override - public NodeKind getKind() { - return NodeKind.FOR_IN; - } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsFunction.java b/src/com/google/dart/compiler/backend/js/ast/JsFunction.java index 21b0fdbe98e..472b0433945 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsFunction.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsFunction.java @@ -99,9 +99,4 @@ public final class JsFunction extends JsLiteral implements HasName { visitor.acceptWithInsertRemove(params); body = visitor.accept(body); } - - @Override - public NodeKind getKind() { - return NodeKind.FUNCTION; - } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsIf.java b/src/com/google/dart/compiler/backend/js/ast/JsIf.java index aa4bc22bc22..2ebbc45a728 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsIf.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsIf.java @@ -63,9 +63,4 @@ public final class JsIf extends JsNodeImpl implements JsStatement { elseStatement = visitor.accept(elseStatement); } } - - @Override - public NodeKind getKind() { - return NodeKind.IF; - } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsInvocation.java b/src/com/google/dart/compiler/backend/js/ast/JsInvocation.java index 3e887940b0f..ec771107033 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsInvocation.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsInvocation.java @@ -76,9 +76,4 @@ public final class JsInvocation extends JsExpressionImpl.JsExpressionHasArgument qualifier = visitor.accept(qualifier); visitor.acceptList(arguments); } - - @Override - public NodeKind getKind() { - return NodeKind.INVOKE; - } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsLabel.java b/src/com/google/dart/compiler/backend/js/ast/JsLabel.java index dbe63cc237e..8a73b4318c0 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsLabel.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsLabel.java @@ -45,14 +45,9 @@ public class JsLabel extends JsNodeImpl implements JsStatement, HasName { public void accept(JsVisitor v, JsContext context) { v.visitLabel(this, context); } - - @Override - public void acceptChildren(JsVisitor visitor, JsContext context) { - statement = visitor.accept(statement); - } @Override - public NodeKind getKind() { - return NodeKind.LABEL; + public void acceptChildren(JsVisitor visitor, JsContext context) { + statement = visitor.accept(statement); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsLiteral.java index edfd17a1a90..dd691ddf905 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsLiteral.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsLiteral.java @@ -46,11 +46,6 @@ public abstract class JsLiteral extends JsExpressionImpl implements CanBooleanEv public void accept(JsVisitor v, JsContext context) { v.visitThis(this, context); } - - @Override - public NodeKind getKind() { - return NodeKind.THIS; - } } public static final class JsBooleanLiteral extends JsValueLiteral { @@ -89,11 +84,6 @@ public abstract class JsLiteral extends JsExpressionImpl implements CanBooleanEv public void accept(JsVisitor v, JsContext context) { v.visitBoolean(this, context); } - - @Override - public NodeKind getKind() { - return NodeKind.BOOLEAN; - } } /** diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNameRef.java b/src/com/google/dart/compiler/backend/js/ast/JsNameRef.java index ce9eeca248b..8ccb13d8367 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsNameRef.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsNameRef.java @@ -104,9 +104,4 @@ public final class JsNameRef extends JsExpressionImpl implements CanBooleanEval, qualifier = visitor.accept(qualifier); } } - - @Override - public NodeKind getKind() { - return NodeKind.NAME_REF; - } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNew.java b/src/com/google/dart/compiler/backend/js/ast/JsNew.java index e5adf766a7f..da61fffd653 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsNew.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsNew.java @@ -51,9 +51,4 @@ public final class JsNew extends JsExpressionImpl.JsExpressionHasArguments { constructorExpression = visitor.accept(constructorExpression); visitor.acceptList(arguments); } - - @Override - public NodeKind getKind() { - return NodeKind.NEW; - } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNode.java b/src/com/google/dart/compiler/backend/js/ast/JsNode.java index cd7e268fdb5..308b71393b8 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsNode.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsNode.java @@ -7,8 +7,6 @@ package com.google.dart.compiler.backend.js.ast; import com.google.dart.compiler.common.HasSourceInfo; public interface JsNode extends HasSourceInfo { - NodeKind getKind(); - /** * Causes this object to have the visitor visit itself and its children. * diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNullLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsNullLiteral.java index a17ac877ef6..226c8a3bc49 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsNullLiteral.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsNullLiteral.java @@ -32,9 +32,4 @@ public final class JsNullLiteral extends JsLiteral.JsValueLiteral { public void accept(JsVisitor v, JsContext context) { v.visitNull(this, context); } - - @Override - public NodeKind getKind() { - return NodeKind.NULL; - } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNumberLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsNumberLiteral.java index e77dceefb17..1cf02b66aa6 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsNumberLiteral.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsNumberLiteral.java @@ -15,11 +15,6 @@ public abstract class JsNumberLiteral extends JsLiteral.JsValueLiteral { return false; } - @Override - public NodeKind getKind() { - return NodeKind.NUMBER; - } - public static final class JsDoubleLiteral extends JsNumberLiteral { public final double value; diff --git a/src/com/google/dart/compiler/backend/js/ast/JsObjectLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsObjectLiteral.java index 7cc65fc7b8a..1d3eb811bc0 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsObjectLiteral.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsObjectLiteral.java @@ -77,9 +77,4 @@ public final class JsObjectLiteral extends JsLiteral { public void acceptChildren(JsVisitor visitor, JsContext context) { visitor.acceptWithInsertRemove(properties); } - - @Override - public NodeKind getKind() { - return NodeKind.OBJECT; - } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsParameter.java b/src/com/google/dart/compiler/backend/js/ast/JsParameter.java index 68e3b313375..da2c2942520 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsParameter.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsParameter.java @@ -35,9 +35,4 @@ public final class JsParameter extends JsNodeImpl implements HasName { public void acceptChildren(JsVisitor visitor, JsContext context) { } - - @Override - public NodeKind getKind() { - return NodeKind.PARAMETER; - } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsPostfixOperation.java b/src/com/google/dart/compiler/backend/js/ast/JsPostfixOperation.java index 72efa7868c4..b7c644cba3f 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsPostfixOperation.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsPostfixOperation.java @@ -31,9 +31,4 @@ public final class JsPostfixOperation extends JsUnaryOperation { public void accept(JsVisitor v, JsContext context) { v.visitPostfixOperation(this, context); } - - @Override - public NodeKind getKind() { - return NodeKind.POSTFIX_OP; - } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsPrefixOperation.java b/src/com/google/dart/compiler/backend/js/ast/JsPrefixOperation.java index ba0e3458c76..e08cd06876e 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsPrefixOperation.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsPrefixOperation.java @@ -6,52 +6,47 @@ package com.google.dart.compiler.backend.js.ast; public final class JsPrefixOperation extends JsUnaryOperation implements CanBooleanEval { - public JsPrefixOperation(JsUnaryOperator op) { - this(op, null); - } - - public JsPrefixOperation(JsUnaryOperator op, JsExpression arg) { - super(op, arg); - } - - @Override - public boolean isBooleanFalse() { - if (getOperator() == JsUnaryOperator.VOID) { - return true; + public JsPrefixOperation(JsUnaryOperator op) { + this(op, null); } - if (getOperator() == JsUnaryOperator.NOT && getArg() instanceof CanBooleanEval) { - CanBooleanEval eval = (CanBooleanEval) getArg(); - return eval.isBooleanTrue(); + + public JsPrefixOperation(JsUnaryOperator op, JsExpression arg) { + super(op, arg); } - return false; - } - @Override - public boolean isBooleanTrue() { - if (getOperator() == JsUnaryOperator.NOT && getArg() instanceof CanBooleanEval) { - CanBooleanEval eval = (CanBooleanEval) getArg(); - return eval.isBooleanFalse(); + @Override + public boolean isBooleanFalse() { + if (getOperator() == JsUnaryOperator.VOID) { + return true; + } + if (getOperator() == JsUnaryOperator.NOT && getArg() instanceof CanBooleanEval) { + CanBooleanEval eval = (CanBooleanEval) getArg(); + return eval.isBooleanTrue(); + } + return false; } - return getOperator() == JsUnaryOperator.TYPEOF; - } - @Override - public boolean isDefinitelyNotNull() { - return getOperator() == JsUnaryOperator.TYPEOF || getOperator() != JsUnaryOperator.VOID; - } + @Override + public boolean isBooleanTrue() { + if (getOperator() == JsUnaryOperator.NOT && getArg() instanceof CanBooleanEval) { + CanBooleanEval eval = (CanBooleanEval) getArg(); + return eval.isBooleanFalse(); + } + return getOperator() == JsUnaryOperator.TYPEOF; + } - @Override - public boolean isDefinitelyNull() { - return getOperator() == JsUnaryOperator.VOID; - } + @Override + public boolean isDefinitelyNotNull() { + return getOperator() == JsUnaryOperator.TYPEOF || getOperator() != JsUnaryOperator.VOID; + } - @Override - public void accept(JsVisitor v, JsContext context) { - v.visitPrefixOperation(this, context); - } + @Override + public boolean isDefinitelyNull() { + return getOperator() == JsUnaryOperator.VOID; + } - @Override - public NodeKind getKind() { - return NodeKind.PREFIX_OP; - } + @Override + public void accept(JsVisitor v, JsContext context) { + v.visitPrefixOperation(this, context); + } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsProgram.java b/src/com/google/dart/compiler/backend/js/ast/JsProgram.java index 5eaee9c19c3..277c74a0b7c 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsProgram.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsProgram.java @@ -119,9 +119,4 @@ public final class JsProgram extends JsNodeImpl { visitor.accept(fragment); } } - - @Override - public NodeKind getKind() { - return NodeKind.PROGRAM; - } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsProgramFragment.java b/src/com/google/dart/compiler/backend/js/ast/JsProgramFragment.java index 3f1b8d0ce77..0f684a581f4 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsProgramFragment.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsProgramFragment.java @@ -27,9 +27,4 @@ public class JsProgramFragment extends JsNodeImpl { public void acceptChildren(JsVisitor visitor, JsContext context) { visitor.accept(globalBlock); } - - @Override - public NodeKind getKind() { - return NodeKind.PROGRAM_FRAGMENT; - } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsPropertyInitializer.java b/src/com/google/dart/compiler/backend/js/ast/JsPropertyInitializer.java index 5e184f071f6..48abb4d607e 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsPropertyInitializer.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsPropertyInitializer.java @@ -48,9 +48,4 @@ public class JsPropertyInitializer extends JsNodeImpl { labelExpr = visitor.accept(labelExpr); valueExpr = visitor.accept(valueExpr); } - - @Override - public NodeKind getKind() { - return NodeKind.PROPERTY_INIT; - } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsRegExp.java b/src/com/google/dart/compiler/backend/js/ast/JsRegExp.java index 2015a5f8b8c..a9ef973af14 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsRegExp.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsRegExp.java @@ -54,9 +54,4 @@ public final class JsRegExp extends JsLiteral.JsValueLiteral { public void accept(JsVisitor v, JsContext context) { v.visitRegExp(this, context); } - - @Override - public NodeKind getKind() { - return NodeKind.REGEXP; - } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsReturn.java b/src/com/google/dart/compiler/backend/js/ast/JsReturn.java index ee798d5573f..456dced912f 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsReturn.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsReturn.java @@ -33,12 +33,7 @@ public final class JsReturn extends JsNodeImpl implements JsStatement { @Override public void acceptChildren(JsVisitor visitor, JsContext context) { if (expression != null) { - expression = visitor.accept(expression); - } - } - - @Override - public NodeKind getKind() { - return NodeKind.RETURN; + expression = visitor.accept(expression); + } } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsStringLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsStringLiteral.java index 68448bda891..bdc15456722 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsStringLiteral.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsStringLiteral.java @@ -44,9 +44,4 @@ public final class JsStringLiteral extends JsLiteral.JsValueLiteral { public void accept(JsVisitor v, JsContext context) { v.visitString(this, context); } - - @Override - public NodeKind getKind() { - return NodeKind.STRING; - } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsSwitch.java b/src/com/google/dart/compiler/backend/js/ast/JsSwitch.java index c4b3a3c1ae6..ed41056624c 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsSwitch.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsSwitch.java @@ -41,9 +41,4 @@ public class JsSwitch extends JsNodeImpl implements JsStatement { expr = visitor.accept(expr); visitor.acceptWithInsertRemove(cases); } - - @Override - public NodeKind getKind() { - return NodeKind.SWITCH; - } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsThrow.java b/src/com/google/dart/compiler/backend/js/ast/JsThrow.java index 164d2dae0f2..69e7ee4a085 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsThrow.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsThrow.java @@ -31,9 +31,4 @@ public class JsThrow extends JsNodeImpl implements JsStatement { public void acceptChildren(JsVisitor visitor, JsContext context) { visitor.accept(expression); } - - @Override - public NodeKind getKind() { - return NodeKind.THROW; - } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsTry.java b/src/com/google/dart/compiler/backend/js/ast/JsTry.java index 3625bd0ebca..613f73da92e 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsTry.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsTry.java @@ -60,9 +60,4 @@ public class JsTry extends JsNodeImpl implements JsStatement { finallyBlock = visitor.accept(finallyBlock); } } - - @Override - public NodeKind getKind() { - return NodeKind.TRY; - } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsVars.java b/src/com/google/dart/compiler/backend/js/ast/JsVars.java index c88016f0de7..f955fa88331 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsVars.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsVars.java @@ -91,11 +91,6 @@ public class JsVars extends JsNodeImpl implements JsStatement, Iterable Date: Thu, 25 Oct 2012 09:44:26 +0400 Subject: [PATCH 21/27] simplify AST (idea like) --- .../backend/js/ast/JsConditional.java | 44 +++++++++---------- 1 file changed, 22 insertions(+), 22 deletions(-) diff --git a/src/com/google/dart/compiler/backend/js/ast/JsConditional.java b/src/com/google/dart/compiler/backend/js/ast/JsConditional.java index 36c73480ce1..6ac57c2dc54 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsConditional.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsConditional.java @@ -8,56 +8,56 @@ package com.google.dart.compiler.backend.js.ast; * Represents a JavaScript conditional expression. */ public final class JsConditional extends JsExpressionImpl { - private JsExpression testExpr; - private JsExpression elseExpr; - private JsExpression thenExpr; + private JsExpression testExpression; + private JsExpression elseExpression; + private JsExpression thenExpression; public JsConditional() { } - public JsConditional(JsExpression testExpr, JsExpression thenExpr, JsExpression elseExpr) { - this.testExpr = testExpr; - this.thenExpr = thenExpr; - this.elseExpr = elseExpr; + public JsConditional(JsExpression testExpression, JsExpression thenExpression, JsExpression elseExpression) { + this.testExpression = testExpression; + this.thenExpression = thenExpression; + this.elseExpression = elseExpression; } public JsExpression getElseExpression() { - return elseExpr; + return elseExpression; } public JsExpression getTestExpression() { - return testExpr; + return testExpression; } public JsExpression getThenExpression() { - return thenExpr; + return thenExpression; } @Override public boolean hasSideEffects() { - return testExpr.hasSideEffects() || thenExpr.hasSideEffects() || elseExpr.hasSideEffects(); + return testExpression.hasSideEffects() || thenExpression.hasSideEffects() || elseExpression.hasSideEffects(); } @Override public boolean isDefinitelyNotNull() { - return thenExpr.isDefinitelyNotNull() && elseExpr.isDefinitelyNotNull(); + return thenExpression.isDefinitelyNotNull() && elseExpression.isDefinitelyNotNull(); } @Override public boolean isDefinitelyNull() { - return thenExpr.isDefinitelyNull() && elseExpr.isDefinitelyNull(); + return thenExpression.isDefinitelyNull() && elseExpression.isDefinitelyNull(); } - public void setElseExpression(JsExpression elseExpr) { - this.elseExpr = elseExpr; + public void setElseExpression(JsExpression elseExpression) { + this.elseExpression = elseExpression; } - public void setTestExpression(JsExpression testExpr) { - this.testExpr = testExpr; + public void setTestExpression(JsExpression testExpression) { + this.testExpression = testExpression; } - public void setThenExpression(JsExpression thenExpr) { - this.thenExpr = thenExpr; + public void setThenExpression(JsExpression thenExpression) { + this.thenExpression = thenExpression; } @Override @@ -67,8 +67,8 @@ public final class JsConditional extends JsExpressionImpl { @Override public void acceptChildren(JsVisitor visitor, JsContext context) { - testExpr = visitor.accept(testExpr); - thenExpr = visitor.accept(thenExpr); - elseExpr = visitor.accept(elseExpr); + testExpression = visitor.accept(testExpression); + thenExpression = visitor.accept(thenExpression); + elseExpression = visitor.accept(elseExpression); } } From 6307201006b2672f28e9a14522288574464e1797 Mon Sep 17 00:00:00 2001 From: develar Date: Thu, 25 Oct 2012 09:52:19 +0400 Subject: [PATCH 22/27] simplify AST (idea like) --- .../dart/compiler/backend/js/ast/JsConditional.java | 3 --- .../google/dart/compiler/backend/js/ast/JsContinue.java | 5 ----- .../google/dart/compiler/backend/js/ast/JsDocComment.java | 4 ---- src/com/google/dart/compiler/backend/js/ast/JsEmpty.java | 4 ---- .../google/dart/compiler/backend/js/ast/JsFunction.java | 2 +- src/com/google/dart/compiler/backend/js/ast/JsIf.java | 8 ++++---- .../google/dart/compiler/backend/js/ast/JsLiteral.java | 4 ---- .../google/dart/compiler/backend/js/ast/JsNameRef.java | 4 ++-- .../google/dart/compiler/backend/js/ast/JsNodeImpl.java | 4 ++++ .../google/dart/compiler/backend/js/ast/JsParameter.java | 5 ----- .../compiler/backend/js/ast/JsPropertyInitializer.java | 2 +- 11 files changed, 12 insertions(+), 33 deletions(-) diff --git a/src/com/google/dart/compiler/backend/js/ast/JsConditional.java b/src/com/google/dart/compiler/backend/js/ast/JsConditional.java index 6ac57c2dc54..e89b7f40a19 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsConditional.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsConditional.java @@ -4,9 +4,6 @@ package com.google.dart.compiler.backend.js.ast; -/** - * Represents a JavaScript conditional expression. - */ public final class JsConditional extends JsExpressionImpl { private JsExpression testExpression; private JsExpression elseExpression; diff --git a/src/com/google/dart/compiler/backend/js/ast/JsContinue.java b/src/com/google/dart/compiler/backend/js/ast/JsContinue.java index 3aa329aa51c..3b5b5fc8070 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsContinue.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsContinue.java @@ -26,9 +26,4 @@ public class JsContinue extends JsNodeImpl implements JsStatement { public void accept(JsVisitor v, JsContext context) { v.visitContinue(this, context); } - - @Override - public void acceptChildren(JsVisitor visitor, JsContext context) { - - } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsDocComment.java b/src/com/google/dart/compiler/backend/js/ast/JsDocComment.java index 2c5ee580316..da90d894862 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsDocComment.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsDocComment.java @@ -27,10 +27,6 @@ public class JsDocComment extends JsExpressionImpl { v.visitDocComment(this, context); } - @Override - public void acceptChildren(JsVisitor visitor, JsContext context) { - } - @Override public boolean hasSideEffects() { return false; diff --git a/src/com/google/dart/compiler/backend/js/ast/JsEmpty.java b/src/com/google/dart/compiler/backend/js/ast/JsEmpty.java index 0e50c6108aa..0835eaf3439 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsEmpty.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsEmpty.java @@ -12,8 +12,4 @@ public class JsEmpty extends JsNodeImpl implements JsStatement { public void accept(JsVisitor v, JsContext context) { v.visitEmpty(this, context); } - - @Override - public void acceptChildren(JsVisitor visitor, JsContext context) { - } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsFunction.java b/src/com/google/dart/compiler/backend/js/ast/JsFunction.java index 472b0433945..b44cfc8140a 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsFunction.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsFunction.java @@ -97,6 +97,6 @@ public final class JsFunction extends JsLiteral implements HasName { @Override public void acceptChildren(JsVisitor visitor, JsContext context) { visitor.acceptWithInsertRemove(params); - body = visitor.accept(body); + body = visitor.accept(body); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsIf.java b/src/com/google/dart/compiler/backend/js/ast/JsIf.java index 2ebbc45a728..6d45f4ef1e1 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsIf.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsIf.java @@ -58,9 +58,9 @@ public final class JsIf extends JsNodeImpl implements JsStatement { @Override public void acceptChildren(JsVisitor visitor, JsContext context) { ifExpression = visitor.accept(ifExpression); - thenStatement = visitor.accept(thenStatement); - if (elseStatement != null) { - elseStatement = visitor.accept(elseStatement); - } + thenStatement = visitor.accept(thenStatement); + if (elseStatement != null) { + elseStatement = visitor.accept(elseStatement); + } } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsLiteral.java index dd691ddf905..2e1925abf30 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsLiteral.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsLiteral.java @@ -102,9 +102,5 @@ public abstract class JsLiteral extends JsExpressionImpl implements CanBooleanEv public final boolean isLeaf() { return true; } - - @Override - public void acceptChildren(JsVisitor visitor, JsContext context) { - } } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNameRef.java b/src/com/google/dart/compiler/backend/js/ast/JsNameRef.java index 8ccb13d8367..31974d93339 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsNameRef.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsNameRef.java @@ -101,7 +101,7 @@ public final class JsNameRef extends JsExpressionImpl implements CanBooleanEval, @Override public void acceptChildren(JsVisitor visitor, JsContext context) { if (qualifier != null) { - qualifier = visitor.accept(qualifier); - } + qualifier = visitor.accept(qualifier); + } } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNodeImpl.java b/src/com/google/dart/compiler/backend/js/ast/JsNodeImpl.java index b41e793dd4a..83fe04c8936 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsNodeImpl.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsNodeImpl.java @@ -15,4 +15,8 @@ abstract class JsNodeImpl extends AbstractNode { public void setSourceInfo(Object info) { sourceInfo = info; } + + @Override + public void acceptChildren(JsVisitor visitor, JsContext context) { + } } \ No newline at end of file diff --git a/src/com/google/dart/compiler/backend/js/ast/JsParameter.java b/src/com/google/dart/compiler/backend/js/ast/JsParameter.java index da2c2942520..1af5537ef7f 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsParameter.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsParameter.java @@ -30,9 +30,4 @@ public final class JsParameter extends JsNodeImpl implements HasName { public void accept(JsVisitor v, JsContext context) { v.visitParameter(this, context); } - - @Override - public void acceptChildren(JsVisitor visitor, JsContext context) { - - } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsPropertyInitializer.java b/src/com/google/dart/compiler/backend/js/ast/JsPropertyInitializer.java index 48abb4d607e..caaef16b4f0 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsPropertyInitializer.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsPropertyInitializer.java @@ -46,6 +46,6 @@ public class JsPropertyInitializer extends JsNodeImpl { @Override public void acceptChildren(JsVisitor visitor, JsContext context) { labelExpr = visitor.accept(labelExpr); - valueExpr = visitor.accept(valueExpr); + valueExpr = visitor.accept(valueExpr); } } From c1cc409b29ba2d565721a5b7dadef21b60b73570 Mon Sep 17 00:00:00 2001 From: develar Date: Thu, 25 Oct 2012 10:11:32 +0400 Subject: [PATCH 23/27] simplify AST (idea like) --- .../js/JsConstructExpressionVisitor.java | 6 +- .../js/JsToStringGenerationVisitor.java | 56 +++++++++---------- .../backend/js/ast/JsArrayAccess.java | 4 +- .../backend/js/ast/JsBinaryOperation.java | 9 +-- .../dart/compiler/backend/js/ast/JsCase.java | 2 +- .../dart/compiler/backend/js/ast/JsCatch.java | 6 +- .../backend/js/ast/JsConditional.java | 6 +- .../backend/js/ast/JsExpressionStatement.java | 2 +- .../dart/compiler/backend/js/ast/JsFor.java | 10 ++-- .../dart/compiler/backend/js/ast/JsForIn.java | 9 +-- .../compiler/backend/js/ast/JsFunction.java | 2 +- .../dart/compiler/backend/js/ast/JsIf.java | 6 +- .../compiler/backend/js/ast/JsInvocation.java | 2 +- .../dart/compiler/backend/js/ast/JsLabel.java | 2 +- .../compiler/backend/js/ast/JsNameRef.java | 2 +- .../dart/compiler/backend/js/ast/JsNew.java | 2 +- .../backend/js/ast/JsPostfixOperation.java | 4 -- .../backend/js/ast/JsPropertyInitializer.java | 4 +- .../compiler/backend/js/ast/JsReturn.java | 2 +- .../compiler/backend/js/ast/JsSwitch.java | 12 ++-- .../dart/compiler/backend/js/ast/JsTry.java | 4 +- .../backend/js/ast/JsUnaryOperation.java | 7 +-- .../dart/compiler/backend/js/ast/JsVars.java | 2 +- .../compiler/backend/js/ast/JsVisitor.java | 8 +-- .../dart/compiler/backend/js/ast/JsWhile.java | 4 +- 25 files changed, 77 insertions(+), 96 deletions(-) diff --git a/src/com/google/dart/compiler/backend/js/JsConstructExpressionVisitor.java b/src/com/google/dart/compiler/backend/js/JsConstructExpressionVisitor.java index e6422c43d2a..c0b1a24254e 100644 --- a/src/com/google/dart/compiler/backend/js/JsConstructExpressionVisitor.java +++ b/src/com/google/dart/compiler/backend/js/JsConstructExpressionVisitor.java @@ -77,7 +77,7 @@ public class JsConstructExpressionVisitor extends RecursiveJsVisitor { * We only look at nodes that would not normally be surrounded by parentheses. */ @Override - public T accept(T node) { + public void accept(T node) { // Assign to Object to prevent 'inconvertible types' compile errors due // to http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6548436 // reproducible in jdk1.6.0_02. @@ -87,9 +87,9 @@ public class JsConstructExpressionVisitor extends RecursiveJsVisitor { // Only visit expressions that won't automatically be surrounded by // parentheses if (precedence < JsPrecedenceVisitor.PRECEDENCE_NEW) { - return node; + return; } } - return super.accept(node); + super.accept(node); } } diff --git a/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java b/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java index e85b2eea51e..1ebf6c822cf 100644 --- a/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java +++ b/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java @@ -367,7 +367,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { // get parentheses around it. printPair(x, x.getTestExpression()); spaceOpt(); - questionMark(); + p.print('?'); spaceOpt(); printPair(x, x.getThenExpression()); spaceOpt(); @@ -407,17 +407,17 @@ public class JsToStringGenerationVisitor extends JsVisitor { leftParen(); accept(x.getCondition()); rightParen(); - _nestedPush(x.getBody()); + nestedPush(x.getBody()); accept(x.getBody()); - _nestedPop(x.getBody()); + nestedPop(x.getBody()); } @Override public void visitDoWhile(JsDoWhile x, JsContext context) { p.print(CHARS_DO); - _nestedPush(x.getBody()); + nestedPush(x.getBody()); accept(x.getBody()); - _nestedPop(x.getBody()); + nestedPop(x.getBody()); if (needSemi) { semi(); newlineOpt(); @@ -483,9 +483,9 @@ public class JsToStringGenerationVisitor extends JsVisitor { } rightParen(); - _nestedPush(x.getBody()); + nestedPush(x.getBody()); accept(x.getBody()); - _nestedPop(x.getBody()); + nestedPop(x.getBody()); } @Override @@ -518,9 +518,9 @@ public class JsToStringGenerationVisitor extends JsVisitor { accept(x.getObjectExpression()); rightParen(); - _nestedPush(x.getBody()); + nestedPush(x.getBody()); accept(x.getBody()); - _nestedPop(x.getBody()); + nestedPop(x.getBody()); } @Override @@ -554,9 +554,9 @@ public class JsToStringGenerationVisitor extends JsVisitor { accept(x.getIfExpression()); rightParen(); JsStatement thenStmt = x.getThenStatement(); - _nestedPush(thenStmt); + nestedPush(thenStmt); accept(thenStmt); - _nestedPop(thenStmt); + nestedPop(thenStmt); JsStatement elseStatement = x.getElseStatement(); if (elseStatement != null) { if (needSemi) { @@ -570,14 +570,14 @@ public class JsToStringGenerationVisitor extends JsVisitor { p.print(CHARS_ELSE); boolean elseIf = elseStatement instanceof JsIf; if (!elseIf) { - _nestedPush(elseStatement); + nestedPush(elseStatement); } else { space(); } accept(elseStatement); if (!elseIf) { - _nestedPop(elseStatement); + nestedPop(elseStatement); } } } @@ -617,7 +617,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { parenPop(x, q, false); p.print('.'); } - _nameRef(x); + nameRef(x); } @Override @@ -745,9 +745,9 @@ public class JsToStringGenerationVisitor extends JsVisitor { @Override public void visitRegExp(JsRegExp x, JsContext ctx) { - _slash(); + slash(); p.print(x.getPattern()); - _slash(); + slash(); String flags = x.getFlags(); if (flags != null) { p.print(flags); @@ -774,12 +774,12 @@ public class JsToStringGenerationVisitor extends JsVisitor { p.print(CHARS_SWITCH); spaceOpt(); leftParen(); - accept(x.getExpr()); + accept(x.getExpression()); rightParen(); spaceOpt(); - _blockOpen(); + blockOpen(); acceptList(x.getCases()); - _blockClose(); + blockClose(); } @Override @@ -918,7 +918,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { boolean needBraces = !x.isGlobalBlock(); if (needBraces) { - _blockOpen(); + blockOpen(); } int count = 0; @@ -1000,13 +1000,13 @@ public class JsToStringGenerationVisitor extends JsVisitor { p.print('='); } - private void _blockClose() { + private void blockClose() { p.indentOut(); p.print('}'); newlineOpt(); } - private void _blockOpen() { + private void blockOpen() { p.print('{'); p.indentIn(); newlineOpt(); @@ -1040,11 +1040,11 @@ public class JsToStringGenerationVisitor extends JsVisitor { nameDef(hasName.getName()); } - private void _nameRef(JsNameRef nameRef) { + private void nameRef(JsNameRef nameRef) { p.print(nameRef.getIdent()); } - private boolean _nestedPop(JsStatement statement) { + private boolean nestedPop(JsStatement statement) { boolean pop = !(statement instanceof JsBlock); if (pop) { p.indentOut(); @@ -1052,7 +1052,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { return pop; } - private boolean _nestedPush(JsStatement statement) { + private boolean nestedPush(JsStatement statement) { boolean push = !(statement instanceof JsBlock); if (push) { newlineOpt(); @@ -1116,10 +1116,6 @@ public class JsToStringGenerationVisitor extends JsVisitor { return doPush; } - private void questionMark() { - p.print('?'); - } - private void rightParen() { p.print(')'); } @@ -1140,7 +1136,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { return true; } - private void _slash() { + private void slash() { p.print('/'); } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsArrayAccess.java b/src/com/google/dart/compiler/backend/js/ast/JsArrayAccess.java index 338ab95c08d..7b53b3f5351 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsArrayAccess.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsArrayAccess.java @@ -58,7 +58,7 @@ public final class JsArrayAccess extends JsExpressionImpl { @Override public void acceptChildren(JsVisitor visitor, JsContext context) { - arrayExpression = visitor.accept(arrayExpression); - indexExpression = visitor.accept(indexExpression); + visitor.accept(arrayExpression); + visitor.accept(indexExpression); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsBinaryOperation.java b/src/com/google/dart/compiler/backend/js/ast/JsBinaryOperation.java index 1c82f99bc97..d29fffc5808 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsBinaryOperation.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsBinaryOperation.java @@ -6,9 +6,6 @@ package com.google.dart.compiler.backend.js.ast; import org.jetbrains.annotations.Nullable; -/** - * Represents a JavaScript binary operation. - */ public final class JsBinaryOperation extends JsExpressionImpl { private JsExpression arg1; private JsExpression arg2; @@ -76,11 +73,11 @@ public final class JsBinaryOperation extends JsExpressionImpl { @Override public void acceptChildren(JsVisitor visitor, JsContext context) { if (op.isAssignment()) { - arg1 = visitor.acceptLvalue(arg1); + visitor.acceptLvalue(arg1); } else { - arg1 = visitor.accept(arg1); + visitor.accept(arg1); } - arg2 = visitor.accept(arg2); + visitor.accept(arg2); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsCase.java b/src/com/google/dart/compiler/backend/js/ast/JsCase.java index 1ccc4c1349b..a03aa745892 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsCase.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsCase.java @@ -29,7 +29,7 @@ public final class JsCase extends JsSwitchMember { @Override public void acceptChildren(JsVisitor visitor, JsContext context) { - caseExpression = visitor.accept(caseExpression); + visitor.accept(caseExpression); super.acceptChildren(visitor, context); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsCatch.java b/src/com/google/dart/compiler/backend/js/ast/JsCatch.java index 9fb2f5ae653..99f3bd11f3c 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsCatch.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsCatch.java @@ -54,10 +54,10 @@ public class JsCatch extends JsNodeImpl implements HasCondition { @Override public void acceptChildren(JsVisitor visitor, JsContext context) { - param = visitor.accept(param); + visitor.accept(param); if (condition != null) { - condition = visitor.accept(condition); + visitor.accept(condition); } - body = visitor.accept(body); + visitor.accept(body); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsConditional.java b/src/com/google/dart/compiler/backend/js/ast/JsConditional.java index e89b7f40a19..bbf013065a4 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsConditional.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsConditional.java @@ -64,8 +64,8 @@ public final class JsConditional extends JsExpressionImpl { @Override public void acceptChildren(JsVisitor visitor, JsContext context) { - testExpression = visitor.accept(testExpression); - thenExpression = visitor.accept(thenExpression); - elseExpression = visitor.accept(elseExpression); + visitor.accept(testExpression); + visitor.accept(thenExpression); + visitor.accept(elseExpression); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsExpressionStatement.java b/src/com/google/dart/compiler/backend/js/ast/JsExpressionStatement.java index 5c1f439b0cc..c60cc252e88 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsExpressionStatement.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsExpressionStatement.java @@ -22,7 +22,7 @@ public final class JsExpressionStatement extends AbstractNode implements JsState @Override public void acceptChildren(JsVisitor visitor, JsContext context) { - expression = visitor.accept(expression); + visitor.accept(expression); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsFor.java b/src/com/google/dart/compiler/backend/js/ast/JsFor.java index 533bb0abfc9..186bb056caa 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsFor.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsFor.java @@ -80,19 +80,19 @@ public class JsFor extends JsNodeImpl implements JsStatement { assert (!(initExpression != null && initVars != null)); if (initExpression != null) { - initExpression = visitor.accept(initExpression); + visitor.accept(initExpression); } else if (initVars != null) { - initVars = visitor.accept(initVars); + visitor.accept(initVars); } if (condition != null) { - condition = visitor.accept(condition); + visitor.accept(condition); } if (incrementExpression != null) { - incrementExpression = visitor.accept(incrementExpression); + visitor.accept(incrementExpression); } - body = visitor.accept(body); + visitor.accept(body); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsForIn.java b/src/com/google/dart/compiler/backend/js/ast/JsForIn.java index 52697eda9c7..68bbc3155aa 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsForIn.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsForIn.java @@ -4,9 +4,6 @@ package com.google.dart.compiler.backend.js.ast; -/** - * Represents a JavaScript for..in statement. - */ public class JsForIn extends JsNodeImpl implements JsStatement { private JsStatement body; private JsExpression iterExpression; @@ -59,9 +56,9 @@ public class JsForIn extends JsNodeImpl implements JsStatement { @Override public void acceptChildren(JsVisitor visitor, JsContext context) { if (iterExpression != null) { - iterExpression = visitor.acceptLvalue(iterExpression); + visitor.acceptLvalue(iterExpression); } - objectExpression = visitor.accept(objectExpression); - body = visitor.accept(body); + visitor.accept(objectExpression); + visitor.accept(body); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsFunction.java b/src/com/google/dart/compiler/backend/js/ast/JsFunction.java index b44cfc8140a..12ac1d58e34 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsFunction.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsFunction.java @@ -97,6 +97,6 @@ public final class JsFunction extends JsLiteral implements HasName { @Override public void acceptChildren(JsVisitor visitor, JsContext context) { visitor.acceptWithInsertRemove(params); - body = visitor.accept(body); + visitor.accept(body); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsIf.java b/src/com/google/dart/compiler/backend/js/ast/JsIf.java index 6d45f4ef1e1..344c2ec5352 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsIf.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsIf.java @@ -57,10 +57,10 @@ public final class JsIf extends JsNodeImpl implements JsStatement { @Override public void acceptChildren(JsVisitor visitor, JsContext context) { - ifExpression = visitor.accept(ifExpression); - thenStatement = visitor.accept(thenStatement); + visitor.accept(ifExpression); + visitor.accept(thenStatement); if (elseStatement != null) { - elseStatement = visitor.accept(elseStatement); + visitor.accept(elseStatement); } } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsInvocation.java b/src/com/google/dart/compiler/backend/js/ast/JsInvocation.java index ec771107033..08ad8f77df0 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsInvocation.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsInvocation.java @@ -73,7 +73,7 @@ public final class JsInvocation extends JsExpressionImpl.JsExpressionHasArgument @Override public void acceptChildren(JsVisitor visitor, JsContext context) { - qualifier = visitor.accept(qualifier); + visitor.accept(qualifier); visitor.acceptList(arguments); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsLabel.java b/src/com/google/dart/compiler/backend/js/ast/JsLabel.java index 8a73b4318c0..3f4fa2b11df 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsLabel.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsLabel.java @@ -48,6 +48,6 @@ public class JsLabel extends JsNodeImpl implements JsStatement, HasName { @Override public void acceptChildren(JsVisitor visitor, JsContext context) { - statement = visitor.accept(statement); + visitor.accept(statement); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNameRef.java b/src/com/google/dart/compiler/backend/js/ast/JsNameRef.java index 31974d93339..97edc3e8728 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsNameRef.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsNameRef.java @@ -101,7 +101,7 @@ public final class JsNameRef extends JsExpressionImpl implements CanBooleanEval, @Override public void acceptChildren(JsVisitor visitor, JsContext context) { if (qualifier != null) { - qualifier = visitor.accept(qualifier); + visitor.accept(qualifier); } } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNew.java b/src/com/google/dart/compiler/backend/js/ast/JsNew.java index da61fffd653..66af5cbf4f5 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsNew.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsNew.java @@ -48,7 +48,7 @@ public final class JsNew extends JsExpressionImpl.JsExpressionHasArguments { @Override public void acceptChildren(JsVisitor visitor, JsContext context) { - constructorExpression = visitor.accept(constructorExpression); + visitor.accept(constructorExpression); visitor.acceptList(arguments); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsPostfixOperation.java b/src/com/google/dart/compiler/backend/js/ast/JsPostfixOperation.java index b7c644cba3f..b77826b9fb8 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsPostfixOperation.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsPostfixOperation.java @@ -4,11 +4,7 @@ package com.google.dart.compiler.backend.js.ast; -/** - * A JavaScript postfix operation. - */ public final class JsPostfixOperation extends JsUnaryOperation { - public JsPostfixOperation(JsUnaryOperator op) { this(op, null); } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsPropertyInitializer.java b/src/com/google/dart/compiler/backend/js/ast/JsPropertyInitializer.java index caaef16b4f0..e756c47466a 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsPropertyInitializer.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsPropertyInitializer.java @@ -45,7 +45,7 @@ public class JsPropertyInitializer extends JsNodeImpl { @Override public void acceptChildren(JsVisitor visitor, JsContext context) { - labelExpr = visitor.accept(labelExpr); - valueExpr = visitor.accept(valueExpr); + visitor.accept(labelExpr); + visitor.accept(valueExpr); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsReturn.java b/src/com/google/dart/compiler/backend/js/ast/JsReturn.java index 456dced912f..5f504488778 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsReturn.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsReturn.java @@ -33,7 +33,7 @@ public final class JsReturn extends JsNodeImpl implements JsStatement { @Override public void acceptChildren(JsVisitor visitor, JsContext context) { if (expression != null) { - expression = visitor.accept(expression); + visitor.accept(expression); } } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsSwitch.java b/src/com/google/dart/compiler/backend/js/ast/JsSwitch.java index ed41056624c..fe83caabbf5 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsSwitch.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsSwitch.java @@ -13,7 +13,7 @@ import java.util.List; public class JsSwitch extends JsNodeImpl implements JsStatement { private final List cases = new ArrayList(); - private JsExpression expr; + private JsExpression expression; public JsSwitch() { super(); @@ -23,12 +23,12 @@ public class JsSwitch extends JsNodeImpl implements JsStatement { return cases; } - public JsExpression getExpr() { - return expr; + public JsExpression getExpression() { + return expression; } - public void setExpr(JsExpression expr) { - this.expr = expr; + public void setExpression(JsExpression expression) { + this.expression = expression; } @Override @@ -38,7 +38,7 @@ public class JsSwitch extends JsNodeImpl implements JsStatement { @Override public void acceptChildren(JsVisitor visitor, JsContext context) { - expr = visitor.accept(expr); + visitor.accept(expression); visitor.acceptWithInsertRemove(cases); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsTry.java b/src/com/google/dart/compiler/backend/js/ast/JsTry.java index 613f73da92e..d09fc842867 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsTry.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsTry.java @@ -54,10 +54,10 @@ public class JsTry extends JsNodeImpl implements JsStatement { @Override public void acceptChildren(JsVisitor visitor, JsContext context) { - tryBlock = visitor.accept(tryBlock); + visitor.accept(tryBlock); visitor.acceptWithInsertRemove(catches); if (finallyBlock != null) { - finallyBlock = visitor.accept(finallyBlock); + visitor.accept(finallyBlock); } } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsUnaryOperation.java b/src/com/google/dart/compiler/backend/js/ast/JsUnaryOperation.java index 8f72f6f07d5..4918c4f9713 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsUnaryOperation.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsUnaryOperation.java @@ -4,9 +4,6 @@ package com.google.dart.compiler.backend.js.ast; -/** - * A JavaScript prefix or postfix operation. - */ public abstract class JsUnaryOperation extends JsExpressionImpl { private JsExpression arg; @@ -45,10 +42,10 @@ public abstract class JsUnaryOperation extends JsExpressionImpl { if (op.isModifying()) { // The delete operator is practically like an assignment of undefined, so // for practical purposes we're treating it as an lvalue. - arg = visitor.acceptLvalue(arg); + visitor.acceptLvalue(arg); } else { - arg = visitor.accept(arg); + visitor.accept(arg); } } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsVars.java b/src/com/google/dart/compiler/backend/js/ast/JsVars.java index f955fa88331..fb2f48551e1 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsVars.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsVars.java @@ -88,7 +88,7 @@ public class JsVars extends JsNodeImpl implements JsStatement, Iterable T accept(T node) { + public void accept(T node) { doAccept(node, UNMODIFIABLE_CONTEXT); - return node; } public final void acceptList(List collection) { @@ -94,9 +93,8 @@ public abstract class JsVisitor { } } - public JsExpression acceptLvalue(JsExpression expr) { - doAccept(expr, LVALUE_CONTEXT); - return expr; + public void acceptLvalue(JsExpression expression) { + doAccept(expression, LVALUE_CONTEXT); } public final void acceptWithInsertRemove(List collection) { diff --git a/src/com/google/dart/compiler/backend/js/ast/JsWhile.java b/src/com/google/dart/compiler/backend/js/ast/JsWhile.java index 9d401658595..7f5a2a80abd 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsWhile.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsWhile.java @@ -42,7 +42,7 @@ public class JsWhile extends JsNodeImpl implements JsStatement { @Override public void acceptChildren(JsVisitor visitor, JsContext context) { - condition = visitor.accept(condition); - body = visitor.accept(body); + visitor.accept(condition); + visitor.accept(body); } } \ No newline at end of file From b5a3fbeca43054712a0bb75c918ffd515476eaba Mon Sep 17 00:00:00 2001 From: develar Date: Thu, 25 Oct 2012 10:17:03 +0400 Subject: [PATCH 24/27] simplify AST (idea like) --- .../google/dart/compiler/backend/js/ast/AbstractNode.java | 3 +-- src/com/google/dart/compiler/backend/js/ast/JsBlock.java | 2 +- src/com/google/dart/compiler/backend/js/ast/JsCatch.java | 2 +- src/com/google/dart/compiler/backend/js/ast/JsContinue.java | 2 +- src/com/google/dart/compiler/backend/js/ast/JsDebugger.java | 2 +- src/com/google/dart/compiler/backend/js/ast/JsEmpty.java | 2 +- .../dart/compiler/backend/js/ast/JsExpressionImpl.java | 5 +---- src/com/google/dart/compiler/backend/js/ast/JsFor.java | 2 +- src/com/google/dart/compiler/backend/js/ast/JsForIn.java | 2 +- src/com/google/dart/compiler/backend/js/ast/JsIf.java | 2 +- src/com/google/dart/compiler/backend/js/ast/JsLabel.java | 2 +- src/com/google/dart/compiler/backend/js/ast/JsParameter.java | 2 +- src/com/google/dart/compiler/backend/js/ast/JsProgram.java | 2 +- .../dart/compiler/backend/js/ast/JsProgramFragment.java | 2 +- .../dart/compiler/backend/js/ast/JsPropertyInitializer.java | 2 +- src/com/google/dart/compiler/backend/js/ast/JsReturn.java | 2 +- src/com/google/dart/compiler/backend/js/ast/JsSwitch.java | 2 +- .../google/dart/compiler/backend/js/ast/JsSwitchMember.java | 2 +- src/com/google/dart/compiler/backend/js/ast/JsThrow.java | 2 +- src/com/google/dart/compiler/backend/js/ast/JsTry.java | 2 +- src/com/google/dart/compiler/backend/js/ast/JsVars.java | 4 ++-- src/com/google/dart/compiler/backend/js/ast/JsWhile.java | 2 +- .../js/ast/{JsNodeImpl.java => SourceInfoAwareJsNode.java} | 5 +---- 23 files changed, 24 insertions(+), 31 deletions(-) rename src/com/google/dart/compiler/backend/js/ast/{JsNodeImpl.java => SourceInfoAwareJsNode.java} (80%) diff --git a/src/com/google/dart/compiler/backend/js/ast/AbstractNode.java b/src/com/google/dart/compiler/backend/js/ast/AbstractNode.java index 582b7ac1adb..d2d906be326 100644 --- a/src/com/google/dart/compiler/backend/js/ast/AbstractNode.java +++ b/src/com/google/dart/compiler/backend/js/ast/AbstractNode.java @@ -1,10 +1,9 @@ package com.google.dart.compiler.backend.js.ast; import com.google.dart.compiler.backend.js.JsToStringGenerationVisitor; -import com.google.dart.compiler.common.HasSourceInfo; import com.google.dart.compiler.util.TextOutputImpl; -abstract class AbstractNode implements JsNode, HasSourceInfo { +abstract class AbstractNode implements JsNode { @Override public String toString() { TextOutputImpl out = new TextOutputImpl(); diff --git a/src/com/google/dart/compiler/backend/js/ast/JsBlock.java b/src/com/google/dart/compiler/backend/js/ast/JsBlock.java index a6b133071f3..4538f88cf7a 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsBlock.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsBlock.java @@ -12,7 +12,7 @@ import java.util.List; /** * Represents a JavaScript block statement. */ -public class JsBlock extends JsNodeImpl implements JsStatement { +public class JsBlock extends SourceInfoAwareJsNode implements JsStatement { private final List statements; public JsBlock() { diff --git a/src/com/google/dart/compiler/backend/js/ast/JsCatch.java b/src/com/google/dart/compiler/backend/js/ast/JsCatch.java index 99f3bd11f3c..af1a322e0ee 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsCatch.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsCatch.java @@ -7,7 +7,7 @@ package com.google.dart.compiler.backend.js.ast; /** * Represents a JavaScript catch clause. */ -public class JsCatch extends JsNodeImpl implements HasCondition { +public class JsCatch extends SourceInfoAwareJsNode implements HasCondition { protected final JsCatchScope scope; private JsBlock body; diff --git a/src/com/google/dart/compiler/backend/js/ast/JsContinue.java b/src/com/google/dart/compiler/backend/js/ast/JsContinue.java index 3b5b5fc8070..a3fd84cc43d 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsContinue.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsContinue.java @@ -6,7 +6,7 @@ package com.google.dart.compiler.backend.js.ast; import org.jetbrains.annotations.Nullable; -public class JsContinue extends JsNodeImpl implements JsStatement { +public class JsContinue extends SourceInfoAwareJsNode implements JsStatement { protected final String label; public JsContinue() { diff --git a/src/com/google/dart/compiler/backend/js/ast/JsDebugger.java b/src/com/google/dart/compiler/backend/js/ast/JsDebugger.java index 973bc5210dc..c27240a2468 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsDebugger.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsDebugger.java @@ -7,7 +7,7 @@ package com.google.dart.compiler.backend.js.ast; /** * Represents a JavaScript debugger statement. */ -public class JsDebugger extends JsNodeImpl implements JsStatement { +public class JsDebugger extends SourceInfoAwareJsNode implements JsStatement { public JsDebugger() { } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsEmpty.java b/src/com/google/dart/compiler/backend/js/ast/JsEmpty.java index 0835eaf3439..7298da8e4e9 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsEmpty.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsEmpty.java @@ -4,7 +4,7 @@ package com.google.dart.compiler.backend.js.ast; -public class JsEmpty extends JsNodeImpl implements JsStatement { +public class JsEmpty extends SourceInfoAwareJsNode implements JsStatement { JsEmpty() { } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsExpressionImpl.java b/src/com/google/dart/compiler/backend/js/ast/JsExpressionImpl.java index 8ae907a92ad..8dba62f3d04 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsExpressionImpl.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsExpressionImpl.java @@ -6,10 +6,7 @@ package com.google.dart.compiler.backend.js.ast; import java.util.List; -abstract class JsExpressionImpl extends JsNodeImpl implements JsExpression { - protected JsExpressionImpl() { - } - +abstract class JsExpressionImpl extends SourceInfoAwareJsNode implements JsExpression { /** * Determines whether or not this expression is a leaf, such as a * {@link JsNameRef}, {@link JsLiteral.JsBooleanLiteral}, and so on. Leaf expressions diff --git a/src/com/google/dart/compiler/backend/js/ast/JsFor.java b/src/com/google/dart/compiler/backend/js/ast/JsFor.java index 186bb056caa..1b582cca10e 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsFor.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsFor.java @@ -15,7 +15,7 @@ package com.google.dart.compiler.backend.js.ast; * Note that any of the parts of the for loop header can be * null, although the body will never be null. */ -public class JsFor extends JsNodeImpl implements JsStatement { +public class JsFor extends SourceInfoAwareJsNode implements JsStatement { private JsStatement body; private JsExpression condition; private JsExpression incrementExpression; diff --git a/src/com/google/dart/compiler/backend/js/ast/JsForIn.java b/src/com/google/dart/compiler/backend/js/ast/JsForIn.java index 68bbc3155aa..bb06baceb9f 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsForIn.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsForIn.java @@ -4,7 +4,7 @@ package com.google.dart.compiler.backend.js.ast; -public class JsForIn extends JsNodeImpl implements JsStatement { +public class JsForIn extends SourceInfoAwareJsNode implements JsStatement { private JsStatement body; private JsExpression iterExpression; private JsExpression objectExpression; diff --git a/src/com/google/dart/compiler/backend/js/ast/JsIf.java b/src/com/google/dart/compiler/backend/js/ast/JsIf.java index 344c2ec5352..f7ec9fb6196 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsIf.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsIf.java @@ -7,7 +7,7 @@ package com.google.dart.compiler.backend.js.ast; /** * Represents a JavaScript if statement. */ -public final class JsIf extends JsNodeImpl implements JsStatement { +public final class JsIf extends SourceInfoAwareJsNode implements JsStatement { private JsExpression ifExpression; private JsStatement thenStatement; private JsStatement elseStatement; diff --git a/src/com/google/dart/compiler/backend/js/ast/JsLabel.java b/src/com/google/dart/compiler/backend/js/ast/JsLabel.java index 3f4fa2b11df..70966f7609c 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsLabel.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsLabel.java @@ -9,7 +9,7 @@ import com.google.dart.compiler.common.Symbol; /** * Represents a JavaScript label statement. */ -public class JsLabel extends JsNodeImpl implements JsStatement, HasName { +public class JsLabel extends SourceInfoAwareJsNode implements JsStatement, HasName { private final JsName label; private JsStatement statement; diff --git a/src/com/google/dart/compiler/backend/js/ast/JsParameter.java b/src/com/google/dart/compiler/backend/js/ast/JsParameter.java index 1af5537ef7f..6080c39fda1 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsParameter.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsParameter.java @@ -9,7 +9,7 @@ import com.google.dart.compiler.common.Symbol; /** * A JavaScript parameter. */ -public final class JsParameter extends JsNodeImpl implements HasName { +public final class JsParameter extends SourceInfoAwareJsNode implements HasName { private final JsName name; public JsParameter(JsName name) { diff --git a/src/com/google/dart/compiler/backend/js/ast/JsProgram.java b/src/com/google/dart/compiler/backend/js/ast/JsProgram.java index 277c74a0b7c..a8f10523557 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsProgram.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsProgram.java @@ -17,7 +17,7 @@ import static com.google.dart.compiler.backend.js.ast.JsNumberLiteral.JsIntLiter /** * A JavaScript program. */ -public final class JsProgram extends JsNodeImpl { +public final class JsProgram extends SourceInfoAwareJsNode { private final JsEmpty emptyStatement; private JsProgramFragment[] fragments; diff --git a/src/com/google/dart/compiler/backend/js/ast/JsProgramFragment.java b/src/com/google/dart/compiler/backend/js/ast/JsProgramFragment.java index 0f684a581f4..61fde23eb8e 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsProgramFragment.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsProgramFragment.java @@ -7,7 +7,7 @@ package com.google.dart.compiler.backend.js.ast; /** * One independently loadable fragment of a {@link JsProgram}. */ -public class JsProgramFragment extends JsNodeImpl { +public class JsProgramFragment extends SourceInfoAwareJsNode { private final JsGlobalBlock globalBlock; public JsProgramFragment() { diff --git a/src/com/google/dart/compiler/backend/js/ast/JsPropertyInitializer.java b/src/com/google/dart/compiler/backend/js/ast/JsPropertyInitializer.java index e756c47466a..47d040b7178 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsPropertyInitializer.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsPropertyInitializer.java @@ -9,7 +9,7 @@ import org.jetbrains.annotations.NotNull; /** * Used in object literals to specify property values by name. */ -public class JsPropertyInitializer extends JsNodeImpl { +public class JsPropertyInitializer extends SourceInfoAwareJsNode { private JsExpression labelExpr; private JsExpression valueExpr; diff --git a/src/com/google/dart/compiler/backend/js/ast/JsReturn.java b/src/com/google/dart/compiler/backend/js/ast/JsReturn.java index 5f504488778..311f4532a5c 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsReturn.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsReturn.java @@ -7,7 +7,7 @@ package com.google.dart.compiler.backend.js.ast; /** * A JavaScript return statement. */ -public final class JsReturn extends JsNodeImpl implements JsStatement { +public final class JsReturn extends SourceInfoAwareJsNode implements JsStatement { private JsExpression expression; public JsReturn() { diff --git a/src/com/google/dart/compiler/backend/js/ast/JsSwitch.java b/src/com/google/dart/compiler/backend/js/ast/JsSwitch.java index fe83caabbf5..d9f379ddfef 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsSwitch.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsSwitch.java @@ -10,7 +10,7 @@ import java.util.List; /** * A JavaScript switch statement. */ -public class JsSwitch extends JsNodeImpl implements JsStatement { +public class JsSwitch extends SourceInfoAwareJsNode implements JsStatement { private final List cases = new ArrayList(); private JsExpression expression; diff --git a/src/com/google/dart/compiler/backend/js/ast/JsSwitchMember.java b/src/com/google/dart/compiler/backend/js/ast/JsSwitchMember.java index 89b8ffdebba..8227c24c3cb 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsSwitchMember.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsSwitchMember.java @@ -11,7 +11,7 @@ import java.util.List; /** * A member/case in a JavaScript switch object. */ -public abstract class JsSwitchMember extends JsNodeImpl { +public abstract class JsSwitchMember extends SourceInfoAwareJsNode { protected final List statements = new SmartList(); protected JsSwitchMember() { diff --git a/src/com/google/dart/compiler/backend/js/ast/JsThrow.java b/src/com/google/dart/compiler/backend/js/ast/JsThrow.java index 69e7ee4a085..88dfd185ed1 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsThrow.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsThrow.java @@ -4,7 +4,7 @@ package com.google.dart.compiler.backend.js.ast; -public class JsThrow extends JsNodeImpl implements JsStatement { +public class JsThrow extends SourceInfoAwareJsNode implements JsStatement { private JsExpression expression; public JsThrow() { diff --git a/src/com/google/dart/compiler/backend/js/ast/JsTry.java b/src/com/google/dart/compiler/backend/js/ast/JsTry.java index d09fc842867..40f5e04f19e 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsTry.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsTry.java @@ -12,7 +12,7 @@ import java.util.List; /** * A JavaScript try statement. */ -public class JsTry extends JsNodeImpl implements JsStatement { +public class JsTry extends SourceInfoAwareJsNode implements JsStatement { private final List catches; private JsBlock finallyBlock; private JsBlock tryBlock; diff --git a/src/com/google/dart/compiler/backend/js/ast/JsVars.java b/src/com/google/dart/compiler/backend/js/ast/JsVars.java index fb2f48551e1..1e72f4aeaa2 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsVars.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsVars.java @@ -16,7 +16,7 @@ import java.util.List; /** * A JavaScript var statement. */ -public class JsVars extends JsNodeImpl implements JsStatement, Iterable { +public class JsVars extends SourceInfoAwareJsNode implements JsStatement, Iterable { private final List vars; private final boolean multiline; @@ -49,7 +49,7 @@ public class JsVars extends JsNodeImpl implements JsStatement, Iterablevar statement. */ - public static class JsVar extends JsNodeImpl implements HasName { + public static class JsVar extends SourceInfoAwareJsNode implements HasName { private final JsName name; private JsExpression initExpression; diff --git a/src/com/google/dart/compiler/backend/js/ast/JsWhile.java b/src/com/google/dart/compiler/backend/js/ast/JsWhile.java index 7f5a2a80abd..7b6d1f9fb58 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsWhile.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsWhile.java @@ -7,7 +7,7 @@ package com.google.dart.compiler.backend.js.ast; /** * A JavaScript while statement. */ -public class JsWhile extends JsNodeImpl implements JsStatement { +public class JsWhile extends SourceInfoAwareJsNode implements JsStatement { protected JsStatement body; protected JsExpression condition; diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNodeImpl.java b/src/com/google/dart/compiler/backend/js/ast/SourceInfoAwareJsNode.java similarity index 80% rename from src/com/google/dart/compiler/backend/js/ast/JsNodeImpl.java rename to src/com/google/dart/compiler/backend/js/ast/SourceInfoAwareJsNode.java index 83fe04c8936..d72ff609ad3 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsNodeImpl.java +++ b/src/com/google/dart/compiler/backend/js/ast/SourceInfoAwareJsNode.java @@ -1,11 +1,8 @@ package com.google.dart.compiler.backend.js.ast; -abstract class JsNodeImpl extends AbstractNode { +abstract class SourceInfoAwareJsNode extends AbstractNode { private Object sourceInfo; - protected JsNodeImpl() { - } - @Override public Object getSourceInfo() { return sourceInfo; From f43e6d272a67ecf37da204e9419c81849993b600 Mon Sep 17 00:00:00 2001 From: develar Date: Thu, 25 Oct 2012 17:41:41 +0400 Subject: [PATCH 25/27] simplify AST --- .../backend/js/ast/CanBooleanEval.java | 13 ----- .../backend/js/ast/ChameleonJsExpression.java | 15 ------ .../backend/js/ast/JsArrayAccess.java | 15 ------ .../backend/js/ast/JsArrayLiteral.java | 30 ------------ .../backend/js/ast/JsBinaryOperation.java | 32 ------------- .../backend/js/ast/JsConditional.java | 15 ------ .../compiler/backend/js/ast/JsDocComment.java | 15 ------ .../compiler/backend/js/ast/JsExpression.java | 15 ------ .../compiler/backend/js/ast/JsFunction.java | 26 ---------- .../compiler/backend/js/ast/JsInvocation.java | 15 ------ .../compiler/backend/js/ast/JsLiteral.java | 47 +------------------ .../compiler/backend/js/ast/JsNameRef.java | 27 +---------- .../dart/compiler/backend/js/ast/JsNew.java | 17 ------- .../backend/js/ast/JsNullLiteral.java | 20 -------- .../backend/js/ast/JsNumberLiteral.java | 30 ------------ .../backend/js/ast/JsObjectLiteral.java | 30 ------------ .../backend/js/ast/JsPostfixOperation.java | 10 ---- .../backend/js/ast/JsPrefixOperation.java | 34 +------------- .../backend/js/ast/JsPropertyInitializer.java | 4 -- .../compiler/backend/js/ast/JsRegExp.java | 20 -------- .../backend/js/ast/JsStringLiteral.java | 23 --------- .../backend/js/ast/JsUnaryOperation.java | 5 -- 22 files changed, 3 insertions(+), 455 deletions(-) delete mode 100644 src/com/google/dart/compiler/backend/js/ast/CanBooleanEval.java diff --git a/src/com/google/dart/compiler/backend/js/ast/CanBooleanEval.java b/src/com/google/dart/compiler/backend/js/ast/CanBooleanEval.java deleted file mode 100644 index 4213394da22..00000000000 --- a/src/com/google/dart/compiler/backend/js/ast/CanBooleanEval.java +++ /dev/null @@ -1,13 +0,0 @@ -// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. - -package com.google.dart.compiler.backend.js.ast; - -/** - * An interface that describes the boolean evaluation of an expression. - */ -public interface CanBooleanEval { - boolean isBooleanFalse(); - boolean isBooleanTrue(); -} diff --git a/src/com/google/dart/compiler/backend/js/ast/ChameleonJsExpression.java b/src/com/google/dart/compiler/backend/js/ast/ChameleonJsExpression.java index e3c0a06c85a..4229c344dd0 100644 --- a/src/com/google/dart/compiler/backend/js/ast/ChameleonJsExpression.java +++ b/src/com/google/dart/compiler/backend/js/ast/ChameleonJsExpression.java @@ -14,21 +14,6 @@ public class ChameleonJsExpression implements JsExpression { this.expression = expression; } - @Override - public boolean hasSideEffects() { - return expression.hasSideEffects(); - } - - @Override - public boolean isDefinitelyNotNull() { - return expression.isDefinitelyNotNull(); - } - - @Override - public boolean isDefinitelyNull() { - return expression.isDefinitelyNull(); - } - @Override public boolean isLeaf() { return expression.isLeaf(); diff --git a/src/com/google/dart/compiler/backend/js/ast/JsArrayAccess.java b/src/com/google/dart/compiler/backend/js/ast/JsArrayAccess.java index 7b53b3f5351..94e7470095c 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsArrayAccess.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsArrayAccess.java @@ -28,21 +28,6 @@ public final class JsArrayAccess extends JsExpressionImpl { return indexExpression; } - @Override - public boolean hasSideEffects() { - return arrayExpression.hasSideEffects() || indexExpression.hasSideEffects(); - } - - @Override - public boolean isDefinitelyNotNull() { - return false; - } - - @Override - public boolean isDefinitelyNull() { - return false; - } - public void setArrayExpression(JsExpression arrayExpression) { this.arrayExpression = arrayExpression; } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsArrayLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsArrayLiteral.java index 6358d3c34e7..8d43ed05758 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsArrayLiteral.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsArrayLiteral.java @@ -26,36 +26,6 @@ public final class JsArrayLiteral extends JsLiteral { return expressions; } - @Override - public boolean hasSideEffects() { - for (JsExpression expr : expressions) { - if (expr.hasSideEffects()) { - return true; - } - } - return false; - } - - @Override - public boolean isBooleanFalse() { - return false; - } - - @Override - public boolean isBooleanTrue() { - return true; - } - - @Override - public boolean isDefinitelyNotNull() { - return true; - } - - @Override - public boolean isDefinitelyNull() { - return false; - } - @Override public void accept(JsVisitor v, JsContext context) { v.visitArray(this, context); diff --git a/src/com/google/dart/compiler/backend/js/ast/JsBinaryOperation.java b/src/com/google/dart/compiler/backend/js/ast/JsBinaryOperation.java index d29fffc5808..e0e0405da4e 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsBinaryOperation.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsBinaryOperation.java @@ -33,38 +33,6 @@ public final class JsBinaryOperation extends JsExpressionImpl { return op; } - @Override - public boolean hasSideEffects() { - return op.isAssignment() || arg1.hasSideEffects() || arg2.hasSideEffects(); - } - - @Override - public boolean isDefinitelyNotNull() { - // Precarious coding, but none of these can have null results. - if (op.getPrecedence() > 5) { - return true; - } - if (op == JsBinaryOperator.OR) { - if (arg1 instanceof CanBooleanEval) { - if (((CanBooleanEval) arg1).isBooleanTrue()) { - assert arg1.isDefinitelyNotNull(); - return true; - } - } - } - // AND and OR can return nulls - if (op.isAssignment()) { - return op != JsBinaryOperator.ASG || arg2.isDefinitelyNotNull(); - } - - return op == JsBinaryOperator.COMMA && arg2.isDefinitelyNotNull(); - } - - @Override - public boolean isDefinitelyNull() { - return op == JsBinaryOperator.AND && arg1.isDefinitelyNull(); - } - @Override public void accept(JsVisitor v, JsContext context) { v.visitBinaryExpression(this, context); diff --git a/src/com/google/dart/compiler/backend/js/ast/JsConditional.java b/src/com/google/dart/compiler/backend/js/ast/JsConditional.java index bbf013065a4..97695c35b8d 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsConditional.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsConditional.java @@ -30,21 +30,6 @@ public final class JsConditional extends JsExpressionImpl { return thenExpression; } - @Override - public boolean hasSideEffects() { - return testExpression.hasSideEffects() || thenExpression.hasSideEffects() || elseExpression.hasSideEffects(); - } - - @Override - public boolean isDefinitelyNotNull() { - return thenExpression.isDefinitelyNotNull() && elseExpression.isDefinitelyNotNull(); - } - - @Override - public boolean isDefinitelyNull() { - return thenExpression.isDefinitelyNull() && elseExpression.isDefinitelyNull(); - } - public void setElseExpression(JsExpression elseExpression) { this.elseExpression = elseExpression; } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsDocComment.java b/src/com/google/dart/compiler/backend/js/ast/JsDocComment.java index da90d894862..60f83d394f8 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsDocComment.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsDocComment.java @@ -26,19 +26,4 @@ public class JsDocComment extends JsExpressionImpl { public void accept(JsVisitor v, JsContext context) { v.visitDocComment(this, context); } - - @Override - public boolean hasSideEffects() { - return false; - } - - @Override - public boolean isDefinitelyNotNull() { - return true; - } - - @Override - public boolean isDefinitelyNull() { - return false; - } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsExpression.java b/src/com/google/dart/compiler/backend/js/ast/JsExpression.java index 5cbc2bd4284..b4f8828c751 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsExpression.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsExpression.java @@ -3,21 +3,6 @@ package com.google.dart.compiler.backend.js.ast; import com.google.dart.compiler.common.HasSourceInfo; public interface JsExpression extends JsNode, HasSourceInfo { - /** - * Determines whether the expression can cause side effects. - */ - boolean hasSideEffects(); - - /** - * True if the target expression is definitely not null. - */ - boolean isDefinitelyNotNull(); - - /** - * True if the target expression is definitely null. - */ - boolean isDefinitelyNull(); - boolean isLeaf(); JsStatement makeStmt(); diff --git a/src/com/google/dart/compiler/backend/js/ast/JsFunction.java b/src/com/google/dart/compiler/backend/js/ast/JsFunction.java index 12ac1d58e34..d9a60fc4e4a 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsFunction.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsFunction.java @@ -55,32 +55,6 @@ public final class JsFunction extends JsLiteral implements HasName { return scope; } - @Override - public boolean hasSideEffects() { - // If there's a name, the name is assigned to. - return name != null; - } - - @Override - public boolean isBooleanFalse() { - return false; - } - - @Override - public boolean isBooleanTrue() { - return true; - } - - @Override - public boolean isDefinitelyNotNull() { - return true; - } - - @Override - public boolean isDefinitelyNull() { - return false; - } - public void setBody(JsBlock body) { this.body = body; } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsInvocation.java b/src/com/google/dart/compiler/backend/js/ast/JsInvocation.java index 08ad8f77df0..234f7b16e11 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsInvocation.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsInvocation.java @@ -47,21 +47,6 @@ public final class JsInvocation extends JsExpressionImpl.JsExpressionHasArgument return qualifier; } - @Override - public boolean hasSideEffects() { - return true; - } - - @Override - public boolean isDefinitelyNotNull() { - return false; - } - - @Override - public boolean isDefinitelyNull() { - return false; - } - public void setQualifier(JsExpression qualifier) { this.qualifier = qualifier; } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsLiteral.java index 2e1925abf30..574a7e99e94 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsLiteral.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsLiteral.java @@ -4,7 +4,7 @@ package com.google.dart.compiler.backend.js.ast; -public abstract class JsLiteral extends JsExpressionImpl implements CanBooleanEval { +public abstract class JsLiteral extends JsExpressionImpl { public static final JsValueLiteral THIS = new JsThisRef(); public static final JsNameRef UNDEFINED = new JsNameRef("undefined"); @@ -22,26 +22,6 @@ public abstract class JsLiteral extends JsExpressionImpl implements CanBooleanEv super(); } - @Override - public boolean isBooleanFalse() { - return false; - } - - @Override - public boolean isBooleanTrue() { - return true; - } - - @Override - public boolean isDefinitelyNotNull() { - return true; - } - - @Override - public boolean isDefinitelyNull() { - return false; - } - @Override public void accept(JsVisitor v, JsContext context) { v.visitThis(this, context); @@ -60,26 +40,6 @@ public abstract class JsLiteral extends JsExpressionImpl implements CanBooleanEv return value; } - @Override - public boolean isBooleanFalse() { - return !value; - } - - @Override - public boolean isBooleanTrue() { - return value; - } - - @Override - public boolean isDefinitelyNotNull() { - return true; - } - - @Override - public boolean isDefinitelyNull() { - return false; - } - @Override public void accept(JsVisitor v, JsContext context) { v.visitBoolean(this, context); @@ -93,11 +53,6 @@ public abstract class JsLiteral extends JsExpressionImpl implements CanBooleanEv protected JsValueLiteral() { } - @Override - public final boolean hasSideEffects() { - return false; - } - @Override public final boolean isLeaf() { return true; diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNameRef.java b/src/com/google/dart/compiler/backend/js/ast/JsNameRef.java index 97edc3e8728..3dcfea61d98 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsNameRef.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsNameRef.java @@ -9,7 +9,7 @@ import com.google.dart.compiler.common.Symbol; /** * Represents a JavaScript expression that references a name. */ -public final class JsNameRef extends JsExpressionImpl implements CanBooleanEval, HasName { +public final class JsNameRef extends JsExpressionImpl implements HasName { private String ident; private JsName name; private JsExpression qualifier; @@ -54,31 +54,6 @@ public final class JsNameRef extends JsExpressionImpl implements CanBooleanEval, return qualifier; } - @Override - public boolean hasSideEffects() { - return qualifier != null && (!qualifier.isDefinitelyNotNull() || qualifier.hasSideEffects()); - } - - @Override - public boolean isBooleanFalse() { - return isDefinitelyNull(); - } - - @Override - public boolean isBooleanTrue() { - return false; - } - - @Override - public boolean isDefinitelyNotNull() { - return false; - } - - @Override - public boolean isDefinitelyNull() { - return name != null && (JsLiteral.UNDEFINED.getName() == name); - } - @Override public boolean isLeaf() { return qualifier == null; diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNew.java b/src/com/google/dart/compiler/backend/js/ast/JsNew.java index 66af5cbf4f5..a5eecda234c 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsNew.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsNew.java @@ -24,23 +24,6 @@ public final class JsNew extends JsExpressionImpl.JsExpressionHasArguments { return constructorExpression; } - @Override - public boolean hasSideEffects() { - return true; - } - - @Override - public boolean isDefinitelyNotNull() { - // Sadly, in JS it can be! - // TODO: analysis could probably determine most instances cannot be null. - return false; - } - - @Override - public boolean isDefinitelyNull() { - return false; - } - @Override public void accept(JsVisitor v, JsContext context) { v.visitNew(this, context); diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNullLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsNullLiteral.java index 226c8a3bc49..4d9cd17a333 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsNullLiteral.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsNullLiteral.java @@ -8,26 +8,6 @@ public final class JsNullLiteral extends JsLiteral.JsValueLiteral { JsNullLiteral() { } - @Override - public boolean isBooleanFalse() { - return true; - } - - @Override - public boolean isBooleanTrue() { - return false; - } - - @Override - public boolean isDefinitelyNotNull() { - return false; - } - - @Override - public boolean isDefinitelyNull() { - return true; - } - @Override public void accept(JsVisitor v, JsContext context) { v.visitNull(this, context); diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNumberLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsNumberLiteral.java index 1cf02b66aa6..9ee62587920 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsNumberLiteral.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsNumberLiteral.java @@ -5,16 +5,6 @@ package com.google.dart.compiler.backend.js.ast; public abstract class JsNumberLiteral extends JsLiteral.JsValueLiteral { - @Override - public boolean isDefinitelyNotNull() { - return true; - } - - @Override - public boolean isDefinitelyNull() { - return false; - } - public static final class JsDoubleLiteral extends JsNumberLiteral { public final double value; @@ -22,16 +12,6 @@ public abstract class JsNumberLiteral extends JsLiteral.JsValueLiteral { this.value = value; } - @Override - public boolean isBooleanFalse() { - return value == 0.0; - } - - @Override - public boolean isBooleanTrue() { - return value != 0.0; - } - @Override public void accept(JsVisitor v, JsContext context) { v.visitDouble(this, context); @@ -49,16 +29,6 @@ public abstract class JsNumberLiteral extends JsLiteral.JsValueLiteral { this.value = value; } - @Override - public boolean isBooleanFalse() { - return value == 0; - } - - @Override - public boolean isBooleanTrue() { - return value != 0; - } - @Override public void accept(JsVisitor v, JsContext context) { v.visitInt(this, context); diff --git a/src/com/google/dart/compiler/backend/js/ast/JsObjectLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsObjectLiteral.java index 1d3eb811bc0..9aa35198399 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsObjectLiteral.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsObjectLiteral.java @@ -38,36 +38,6 @@ public final class JsObjectLiteral extends JsLiteral { return properties; } - @Override - public boolean hasSideEffects() { - for (JsPropertyInitializer prop : properties) { - if (prop.hasSideEffects()) { - return true; - } - } - return false; - } - - @Override - public boolean isBooleanFalse() { - return false; - } - - @Override - public boolean isBooleanTrue() { - return true; - } - - @Override - public boolean isDefinitelyNotNull() { - return true; - } - - @Override - public boolean isDefinitelyNull() { - return false; - } - @Override public void accept(JsVisitor v, JsContext context) { v.visitObjectLiteral(this, context); diff --git a/src/com/google/dart/compiler/backend/js/ast/JsPostfixOperation.java b/src/com/google/dart/compiler/backend/js/ast/JsPostfixOperation.java index b77826b9fb8..f86231c8ff6 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsPostfixOperation.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsPostfixOperation.java @@ -13,16 +13,6 @@ public final class JsPostfixOperation extends JsUnaryOperation { super(op, arg); } - @Override - public boolean isDefinitelyNotNull() { - return true; - } - - @Override - public boolean isDefinitelyNull() { - return false; - } - @Override public void accept(JsVisitor v, JsContext context) { v.visitPostfixOperation(this, context); diff --git a/src/com/google/dart/compiler/backend/js/ast/JsPrefixOperation.java b/src/com/google/dart/compiler/backend/js/ast/JsPrefixOperation.java index e08cd06876e..9c943e20e02 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsPrefixOperation.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsPrefixOperation.java @@ -4,8 +4,7 @@ package com.google.dart.compiler.backend.js.ast; -public final class JsPrefixOperation extends JsUnaryOperation implements CanBooleanEval { - +public final class JsPrefixOperation extends JsUnaryOperation { public JsPrefixOperation(JsUnaryOperator op) { this(op, null); } @@ -14,37 +13,6 @@ public final class JsPrefixOperation extends JsUnaryOperation implements CanBool super(op, arg); } - @Override - public boolean isBooleanFalse() { - if (getOperator() == JsUnaryOperator.VOID) { - return true; - } - if (getOperator() == JsUnaryOperator.NOT && getArg() instanceof CanBooleanEval) { - CanBooleanEval eval = (CanBooleanEval) getArg(); - return eval.isBooleanTrue(); - } - return false; - } - - @Override - public boolean isBooleanTrue() { - if (getOperator() == JsUnaryOperator.NOT && getArg() instanceof CanBooleanEval) { - CanBooleanEval eval = (CanBooleanEval) getArg(); - return eval.isBooleanFalse(); - } - return getOperator() == JsUnaryOperator.TYPEOF; - } - - @Override - public boolean isDefinitelyNotNull() { - return getOperator() == JsUnaryOperator.TYPEOF || getOperator() != JsUnaryOperator.VOID; - } - - @Override - public boolean isDefinitelyNull() { - return getOperator() == JsUnaryOperator.VOID; - } - @Override public void accept(JsVisitor v, JsContext context) { v.visitPrefixOperation(this, context); diff --git a/src/com/google/dart/compiler/backend/js/ast/JsPropertyInitializer.java b/src/com/google/dart/compiler/backend/js/ast/JsPropertyInitializer.java index 47d040b7178..9679fd02bb1 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsPropertyInitializer.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsPropertyInitializer.java @@ -30,10 +30,6 @@ public class JsPropertyInitializer extends SourceInfoAwareJsNode { return valueExpr; } - public boolean hasSideEffects() { - return labelExpr.hasSideEffects() || valueExpr.hasSideEffects(); - } - public void setValueExpr(@NotNull JsExpression valueExpr) { this.valueExpr = valueExpr; } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsRegExp.java b/src/com/google/dart/compiler/backend/js/ast/JsRegExp.java index a9ef973af14..568e9ed82c6 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsRegExp.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsRegExp.java @@ -22,26 +22,6 @@ public final class JsRegExp extends JsLiteral.JsValueLiteral { return pattern; } - @Override - public boolean isBooleanFalse() { - return false; - } - - @Override - public boolean isBooleanTrue() { - return true; - } - - @Override - public boolean isDefinitelyNotNull() { - return true; - } - - @Override - public boolean isDefinitelyNull() { - return false; - } - public void setFlags(String suffix) { flags = suffix; } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsStringLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsStringLiteral.java index bdc15456722..9da55039453 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsStringLiteral.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsStringLiteral.java @@ -4,9 +4,6 @@ package com.google.dart.compiler.backend.js.ast; -/** - * A JavaScript string literal expression. - */ public final class JsStringLiteral extends JsLiteral.JsValueLiteral { private final String value; @@ -20,26 +17,6 @@ public final class JsStringLiteral extends JsLiteral.JsValueLiteral { return value; } - @Override - public boolean isBooleanFalse() { - return value.length() == 0; - } - - @Override - public boolean isBooleanTrue() { - return value.length() != 0; - } - - @Override - public boolean isDefinitelyNotNull() { - return true; - } - - @Override - public boolean isDefinitelyNull() { - return false; - } - @Override public void accept(JsVisitor v, JsContext context) { v.visitString(this, context); diff --git a/src/com/google/dart/compiler/backend/js/ast/JsUnaryOperation.java b/src/com/google/dart/compiler/backend/js/ast/JsUnaryOperation.java index 4918c4f9713..c428c7d7f69 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsUnaryOperation.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsUnaryOperation.java @@ -28,11 +28,6 @@ public abstract class JsUnaryOperation extends JsExpressionImpl { return op; } - @Override - public final boolean hasSideEffects() { - return op.isModifying() || arg.hasSideEffects(); - } - public void setArg(JsExpression arg) { this.arg = arg; } From 633e2a142a10c6cb9a302b81707b7aa70cd482f2 Mon Sep 17 00:00:00 2001 From: develar Date: Thu, 25 Oct 2012 20:18:04 +0400 Subject: [PATCH 26/27] simplify AST --- .../js/JsConstructExpressionVisitor.java | 18 +- .../backend/js/JsFirstExpressionVisitor.java | 26 +- .../backend/js/JsPrecedenceVisitor.java | 42 +-- .../backend/js/JsRequiresSemiVisitor.java | 10 +- .../js/JsToStringGenerationVisitor.java | 126 ++++----- .../backend/js/ast/ChameleonJsExpression.java | 8 +- .../backend/js/ast/JsArrayAccess.java | 6 +- .../backend/js/ast/JsArrayLiteral.java | 6 +- .../backend/js/ast/JsBinaryOperation.java | 6 +- .../dart/compiler/backend/js/ast/JsBlock.java | 6 +- .../dart/compiler/backend/js/ast/JsBreak.java | 4 +- .../dart/compiler/backend/js/ast/JsCase.java | 8 +- .../dart/compiler/backend/js/ast/JsCatch.java | 6 +- .../backend/js/ast/JsConditional.java | 6 +- .../compiler/backend/js/ast/JsContinue.java | 4 +- .../compiler/backend/js/ast/JsDebugger.java | 6 +- .../compiler/backend/js/ast/JsDefault.java | 4 +- .../compiler/backend/js/ast/JsDoWhile.java | 4 +- .../compiler/backend/js/ast/JsDocComment.java | 4 +- .../dart/compiler/backend/js/ast/JsEmpty.java | 4 +- .../backend/js/ast/JsExpressionStatement.java | 6 +- .../dart/compiler/backend/js/ast/JsFor.java | 6 +- .../dart/compiler/backend/js/ast/JsForIn.java | 6 +- .../compiler/backend/js/ast/JsFunction.java | 6 +- .../dart/compiler/backend/js/ast/JsIf.java | 6 +- .../compiler/backend/js/ast/JsInvocation.java | 6 +- .../dart/compiler/backend/js/ast/JsLabel.java | 6 +- .../compiler/backend/js/ast/JsLiteral.java | 8 +- .../compiler/backend/js/ast/JsNameRef.java | 6 +- .../dart/compiler/backend/js/ast/JsNew.java | 6 +- .../dart/compiler/backend/js/ast/JsNode.java | 6 +- .../backend/js/ast/JsNullLiteral.java | 4 +- .../backend/js/ast/JsNumberLiteral.java | 8 +- .../backend/js/ast/JsObjectLiteral.java | 6 +- .../compiler/backend/js/ast/JsParameter.java | 4 +- .../backend/js/ast/JsPostfixOperation.java | 4 +- .../backend/js/ast/JsPrefixOperation.java | 4 +- .../compiler/backend/js/ast/JsProgram.java | 7 +- .../backend/js/ast/JsProgramFragment.java | 6 +- .../backend/js/ast/JsPropertyInitializer.java | 6 +- .../compiler/backend/js/ast/JsRegExp.java | 7 +- .../compiler/backend/js/ast/JsReturn.java | 6 +- .../backend/js/ast/JsStringLiteral.java | 4 +- .../compiler/backend/js/ast/JsSwitch.java | 6 +- .../backend/js/ast/JsSwitchMember.java | 2 +- .../dart/compiler/backend/js/ast/JsThrow.java | 6 +- .../dart/compiler/backend/js/ast/JsTry.java | 6 +- .../backend/js/ast/JsUnaryOperation.java | 2 +- .../dart/compiler/backend/js/ast/JsVars.java | 12 +- .../compiler/backend/js/ast/JsVisitor.java | 264 ++++++------------ .../dart/compiler/backend/js/ast/JsWhile.java | 6 +- .../backend/js/ast/RecursiveJsVisitor.java | 4 +- .../backend/js/ast/SourceInfoAwareJsNode.java | 2 +- 53 files changed, 329 insertions(+), 419 deletions(-) diff --git a/src/com/google/dart/compiler/backend/js/JsConstructExpressionVisitor.java b/src/com/google/dart/compiler/backend/js/JsConstructExpressionVisitor.java index c0b1a24254e..5524261a515 100644 --- a/src/com/google/dart/compiler/backend/js/JsConstructExpressionVisitor.java +++ b/src/com/google/dart/compiler/backend/js/JsConstructExpressionVisitor.java @@ -29,7 +29,7 @@ public class JsConstructExpressionVisitor extends RecursiveJsVisitor { * We only look at the array expression since the index has its own scope. */ @Override - public void visitArrayAccess(JsArrayAccess x, JsContext ctx) { + public void visitArrayAccess(JsArrayAccess x) { accept(x.getArrayExpression()); } @@ -37,25 +37,25 @@ public class JsConstructExpressionVisitor extends RecursiveJsVisitor { * Array literals have their own scoping. */ @Override - public void visitArray(JsArrayLiteral x, JsContext ctx) { + public void visitArray(JsArrayLiteral x) { } /** * Functions have their own scoping. */ @Override - public void visitFunction(JsFunction x, JsContext ctx) { + public void visitFunction(JsFunction x) { } @Override - public void visitInvocation(JsInvocation x, JsContext ctx) { + public void visitInvocation(JsInvocation x) { containsInvocation = true; } @Override - public void visitNameRef(JsNameRef x, JsContext ctx) { - if (!x.isLeaf()) { - accept(x.getQualifier()); + public void visitNameRef(JsNameRef nameRef) { + if (!nameRef.isLeaf()) { + accept(nameRef.getQualifier()); } } @@ -63,14 +63,14 @@ public class JsConstructExpressionVisitor extends RecursiveJsVisitor { * New constructs bind to the nearest set of parentheses. */ @Override - public void visitNew(JsNew x, JsContext ctx) { + public void visitNew(JsNew x) { } /** * Object literals have their own scope. */ @Override - public void visitObjectLiteral(JsObjectLiteral x, JsContext ctx) { + public void visitObjectLiteral(JsObjectLiteral x) { } /** diff --git a/src/com/google/dart/compiler/backend/js/JsFirstExpressionVisitor.java b/src/com/google/dart/compiler/backend/js/JsFirstExpressionVisitor.java index 4901391d1c4..e2308a0344b 100644 --- a/src/com/google/dart/compiler/backend/js/JsFirstExpressionVisitor.java +++ b/src/com/google/dart/compiler/backend/js/JsFirstExpressionVisitor.java @@ -49,56 +49,56 @@ public class JsFirstExpressionVisitor extends RecursiveJsVisitor { } @Override - public void visitArrayAccess(JsArrayAccess x, JsContext ctx) { + public void visitArrayAccess(JsArrayAccess x) { accept(x.getArrayExpression()); } @Override - public void visitArray(JsArrayLiteral x, JsContext ctx) { + public void visitArray(JsArrayLiteral x) { } @Override - public void visitBinaryExpression(JsBinaryOperation x, JsContext ctx) { + public void visitBinaryExpression(JsBinaryOperation x) { accept(x.getArg1()); } @Override - public void visitConditional(JsConditional x, JsContext ctx) { + public void visitConditional(JsConditional x) { accept(x.getTestExpression()); } @Override - public void visitFunction(JsFunction x, JsContext ctx) { + public void visitFunction(JsFunction x) { needsParentheses = true; } @Override - public void visitInvocation(JsInvocation x, JsContext ctx) { + public void visitInvocation(JsInvocation x) { accept(x.getQualifier()); } @Override - public void visitNameRef(JsNameRef x, JsContext ctx) { - if (!x.isLeaf()) { - accept(x.getQualifier()); + public void visitNameRef(JsNameRef nameRef) { + if (!nameRef.isLeaf()) { + accept(nameRef.getQualifier()); } } @Override - public void visitNew(JsNew x, JsContext ctx) { + public void visitNew(JsNew x) { } @Override - public void visitObjectLiteral(JsObjectLiteral x, JsContext ctx) { + public void visitObjectLiteral(JsObjectLiteral x) { needsParentheses = true; } @Override - public void visitPostfixOperation(JsPostfixOperation x, JsContext ctx) { + public void visitPostfixOperation(JsPostfixOperation x) { accept(x.getArg()); } @Override - public void visitPrefixOperation(JsPrefixOperation x, JsContext ctx) { + public void visitPrefixOperation(JsPrefixOperation x) { } } diff --git a/src/com/google/dart/compiler/backend/js/JsPrecedenceVisitor.java b/src/com/google/dart/compiler/backend/js/JsPrecedenceVisitor.java index 28625e07e85..77fcf9b83f6 100644 --- a/src/com/google/dart/compiler/backend/js/JsPrecedenceVisitor.java +++ b/src/com/google/dart/compiler/backend/js/JsPrecedenceVisitor.java @@ -45,43 +45,43 @@ class JsPrecedenceVisitor extends JsVisitor { } @Override - public void visitArrayAccess(JsArrayAccess x, JsContext ctx) { + public void visitArrayAccess(JsArrayAccess x) { answer = 16; } @Override - public void visitArray(JsArrayLiteral x, JsContext ctx) { + public void visitArray(JsArrayLiteral x) { answer = 17; // primary } @Override - public void visitBinaryExpression(JsBinaryOperation x, JsContext ctx) { + public void visitBinaryExpression(JsBinaryOperation x) { answer = x.getOperator().getPrecedence(); } @Override - public void visitBoolean(JsLiteral.JsBooleanLiteral x, JsContext ctx) { + public void visitBoolean(JsLiteral.JsBooleanLiteral x) { answer = 17; // primary } @Override - public void visitConditional(JsConditional x, JsContext ctx) { + public void visitConditional(JsConditional x) { answer = 3; } @Override - public void visitFunction(JsFunction x, JsContext ctx) { + public void visitFunction(JsFunction x) { answer = 17; // primary } @Override - public void visitInvocation(JsInvocation x, JsContext ctx) { + public void visitInvocation(JsInvocation x) { answer = 16; } @Override - public void visitNameRef(JsNameRef x, JsContext ctx) { - if (x.isLeaf()) { + public void visitNameRef(JsNameRef nameRef) { + if (nameRef.isLeaf()) { answer = 17; // primary } else { @@ -90,62 +90,62 @@ class JsPrecedenceVisitor extends JsVisitor { } @Override - public void visitNew(JsNew x, JsContext ctx) { + public void visitNew(JsNew x) { answer = PRECEDENCE_NEW; } @Override - public void visitNull(JsNullLiteral x, JsContext ctx) { + public void visitNull(JsNullLiteral x) { answer = 17; // primary } @Override - public void visitInt(JsNumberLiteral.JsIntLiteral x, JsContext ctx) { + public void visitInt(JsNumberLiteral.JsIntLiteral x) { answer = 17; // primary } @Override - public void visitDouble(JsNumberLiteral.JsDoubleLiteral x, JsContext ctx) { + public void visitDouble(JsNumberLiteral.JsDoubleLiteral x) { answer = 17; // primary } @Override - public void visitObjectLiteral(JsObjectLiteral x, JsContext ctx) { + public void visitObjectLiteral(JsObjectLiteral x) { answer = 17; // primary } @Override - public void visitPostfixOperation(JsPostfixOperation x, JsContext ctx) { + public void visitPostfixOperation(JsPostfixOperation x) { answer = x.getOperator().getPrecedence(); } @Override - public void visitPrefixOperation(JsPrefixOperation x, JsContext ctx) { + public void visitPrefixOperation(JsPrefixOperation x) { answer = x.getOperator().getPrecedence(); } @Override - public void visitPropertyInitializer(JsPropertyInitializer x, JsContext ctx) { + public void visitPropertyInitializer(JsPropertyInitializer x) { answer = 17; // primary } @Override - public void visitRegExp(JsRegExp x, JsContext ctx) { + public void visitRegExp(JsRegExp x) { answer = 17; // primary } @Override - public void visitString(JsStringLiteral x, JsContext ctx) { + public void visitString(JsStringLiteral x) { answer = 17; // primary } @Override - public void visitThis(JsLiteral.JsThisRef x, JsContext ctx) { + public void visitThis(JsLiteral.JsThisRef x) { answer = 17; // primary } @Override - protected void visitElement(JsNode node, JsContext context) { + protected void visitElement(JsNode node) { throw new RuntimeException("Only expressions have precedence."); } } diff --git a/src/com/google/dart/compiler/backend/js/JsRequiresSemiVisitor.java b/src/com/google/dart/compiler/backend/js/JsRequiresSemiVisitor.java index 9cebe950c2b..b5a13be9400 100644 --- a/src/com/google/dart/compiler/backend/js/JsRequiresSemiVisitor.java +++ b/src/com/google/dart/compiler/backend/js/JsRequiresSemiVisitor.java @@ -34,21 +34,21 @@ public class JsRequiresSemiVisitor extends JsVisitor { } @Override - public void visitFor(JsFor x, JsContext ctx) { + public void visitFor(JsFor x) { if (x.getBody() instanceof JsEmpty) { needsSemicolon = true; } } @Override - public void visitForIn(JsForIn x, JsContext ctx) { + public void visitForIn(JsForIn x) { if (x.getBody() instanceof JsEmpty) { needsSemicolon = true; } } @Override - public void visitIf(JsIf x, JsContext ctx) { + public void visitIf(JsIf x) { JsStatement thenStmt = x.getThenStatement(); JsStatement elseStmt = x.getElseStatement(); JsStatement toCheck = thenStmt; @@ -65,14 +65,14 @@ public class JsRequiresSemiVisitor extends JsVisitor { } @Override - public void visitLabel(JsLabel x, JsContext ctx) { + public void visitLabel(JsLabel x) { if (x.getStatement() instanceof JsEmpty) { needsSemicolon = true; } } @Override - public void visitWhile(JsWhile x, JsContext ctx) { + public void visitWhile(JsWhile x) { if (x.getBody() instanceof JsEmpty) { needsSemicolon = true; } diff --git a/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java b/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java index 1ebf6c822cf..7a02a4274e5 100644 --- a/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java +++ b/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java @@ -205,7 +205,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public void visitArrayAccess(JsArrayAccess x, JsContext context) { + public void visitArrayAccess(JsArrayAccess x) { printPair(x, x.getArrayExpression()); leftSquare(); accept(x.getIndexExpression()); @@ -213,7 +213,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public void visitArray(JsArrayLiteral x, JsContext ctx) { + public void visitArray(JsArrayLiteral x) { leftSquare(); printExpressions(x.getExpressions()); rightSquare(); @@ -232,7 +232,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public void visitBinaryExpression(JsBinaryOperation binaryOperation, JsContext context) { + public void visitBinaryExpression(JsBinaryOperation binaryOperation) { JsBinaryOperator operator = binaryOperation.getOperator(); JsExpression arg1 = binaryOperation.getArg1(); boolean isExpressionEnclosed = parenPush(binaryOperation, arg1, !operator.isLeftAssociative()); @@ -277,12 +277,12 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public void visitBlock(JsBlock x, JsContext ctx) { + public void visitBlock(JsBlock x) { printJsBlock(x, true, true); } @Override - public void visitBoolean(JsLiteral.JsBooleanLiteral x, JsContext ctx) { + public void visitBoolean(JsLiteral.JsBooleanLiteral x) { if (x.getValue()) { p.print(CHARS_TRUE); } @@ -292,13 +292,13 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public void visitBreak(JsBreak x, JsContext ctx) { + public void visitBreak(JsBreak x) { p.print(CHARS_BREAK); continueOrBreakLabel(x); } @Override - public void visitContinue(JsContinue x, JsContext ctx) { + public void visitContinue(JsContinue x) { p.print(CHARS_CONTINUE); continueOrBreakLabel(x); } @@ -312,7 +312,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public void visitCase(JsCase x, JsContext ctx) { + public void visitCase(JsCase x) { p.print(CHARS_CASE); space(); accept(x.getCaseExpression()); @@ -337,7 +337,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public void visitCatch(JsCatch x, JsContext ctx) { + public void visitCatch(JsCatch x) { spaceOpt(); p.print(CHARS_CATCH); spaceOpt(); @@ -360,7 +360,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public void visitConditional(JsConditional x, JsContext ctx) { + public void visitConditional(JsConditional x) { // Associativity: for the then and else branches, it is safe to insert // another // ternary expression, but if the test expression is a ternary, it should @@ -388,12 +388,12 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public void visitDebugger(JsDebugger x, JsContext context) { + public void visitDebugger(JsDebugger x) { p.print(CHARS_DEBUGGER); } @Override - public void visitDefault(JsDefault x, JsContext context) { + public void visitDefault(JsDefault x) { p.print(CHARS_DEFAULT); _colon(); @@ -401,7 +401,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public void visitWhile(JsWhile x, JsContext ctx) { + public void visitWhile(JsWhile x) { _while(); spaceOpt(); leftParen(); @@ -413,7 +413,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public void visitDoWhile(JsDoWhile x, JsContext context) { + public void visitDoWhile(JsDoWhile x) { p.print(CHARS_DO); nestedPush(x.getBody()); accept(x.getBody()); @@ -434,11 +434,11 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public void visitEmpty(JsEmpty x, JsContext ctx) { + public void visitEmpty(JsEmpty x) { } @Override - public void visitExpressionStatement(JsExpressionStatement x, JsContext ctx) { + public void visitExpressionStatement(JsExpressionStatement x) { boolean surroundWithParentheses = JsFirstExpressionVisitor.exec(x); if (surroundWithParentheses) { leftParen(); @@ -450,7 +450,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public void visitFor(JsFor x, JsContext ctx) { + public void visitFor(JsFor x) { _for(); spaceOpt(); leftParen(); @@ -489,7 +489,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public void visitForIn(JsForIn x, JsContext ctx) { + public void visitForIn(JsForIn x) { _for(); spaceOpt(); leftParen(); @@ -524,7 +524,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public void visitFunction(JsFunction x, JsContext ctx) { + public void visitFunction(JsFunction x) { p.print(CHARS_FUNCTION); space(); if (x.getName() != null) { @@ -547,7 +547,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public void visitIf(JsIf x, JsContext ctx) { + public void visitIf(JsIf x) { _if(); spaceOpt(); leftParen(); @@ -583,7 +583,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public void visitInvocation(JsInvocation x, JsContext ctx) { + public void visitInvocation(JsInvocation x) { printPair(x, x.getQualifier()); leftParen(); @@ -592,7 +592,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public void visitLabel(JsLabel x, JsContext ctx) { + public void visitLabel(JsLabel x) { nameOf(x); _colon(); spaceOpt(); @@ -600,28 +600,32 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public void visitNameRef(JsNameRef x, JsContext ctx) { - JsExpression q = x.getQualifier(); - if (q != null) { - parenPush(x, q, false); - if (q instanceof JsNumberLiteral) { - /** - * Fix for Issue #3796. "42.foo" is not allowed, but "(42).foo" is. - */ + public void visitNameRef(JsNameRef nameRef) { + JsExpression qualifier = nameRef.getQualifier(); + if (qualifier != null) { + final boolean enclose; + if (qualifier instanceof JsLiteral.JsValueLiteral) { + // "42.foo" is not allowed, but "(42).foo" is. + enclose = qualifier instanceof JsNumberLiteral; + } + else { + enclose = parenCalc(nameRef, qualifier, false); + } + + if (enclose) { leftParen(); } - accept(q); - if (q instanceof JsNumberLiteral) { + accept(qualifier); + if (enclose) { rightParen(); } - parenPop(x, q, false); p.print('.'); } - nameRef(x); + p.print(nameRef.getIdent()); } @Override - public void visitNew(JsNew x, JsContext ctx) { + public void visitNew(JsNew x) { p.print(CHARS_NEW); space(); @@ -641,22 +645,22 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public void visitNull(JsNullLiteral x, JsContext ctx) { + public void visitNull(JsNullLiteral x) { p.print(CHARS_NULL); } @Override - public void visitInt(JsIntLiteral x, JsContext ctx) { + public void visitInt(JsIntLiteral x) { p.print(x.value); } @Override - public void visitDouble(JsDoubleLiteral x, JsContext ctx) { + public void visitDouble(JsDoubleLiteral x) { p.print(x.value); } @Override - public void visitObjectLiteral(JsObjectLiteral objectLiteral, JsContext context) { + public void visitObjectLiteral(JsObjectLiteral objectLiteral) { p.print('{'); if (objectLiteral.isMultiline()) { p.indentIn(); @@ -708,12 +712,12 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public void visitParameter(JsParameter x, JsContext ctx) { + public void visitParameter(JsParameter x) { nameOf(x); } @Override - public void visitPostfixOperation(JsPostfixOperation x, JsContext ctx) { + public void visitPostfixOperation(JsPostfixOperation x) { JsUnaryOperator op = x.getOperator(); JsExpression arg = x.getArg(); // unary operators always associate correctly (I think) @@ -722,7 +726,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public void visitPrefixOperation(JsPrefixOperation x, JsContext ctx) { + public void visitPrefixOperation(JsPrefixOperation x) { JsUnaryOperator op = x.getOperator(); p.print(op.getSymbol()); JsExpression arg = x.getArg(); @@ -734,17 +738,17 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public void visitProgram(JsProgram x, JsContext context) { + public void visitProgram(JsProgram x) { p.print(""); } @Override - public void visitProgramFragment(JsProgramFragment x, JsContext ctx) { + public void visitProgramFragment(JsProgramFragment x) { p.print(""); } @Override - public void visitRegExp(JsRegExp x, JsContext ctx) { + public void visitRegExp(JsRegExp x) { slash(); p.print(x.getPattern()); slash(); @@ -755,7 +759,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public void visitReturn(JsReturn x, JsContext ctx) { + public void visitReturn(JsReturn x) { p.print(CHARS_RETURN); JsExpression expr = x.getExpression(); if (expr != null) { @@ -765,12 +769,12 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public void visitString(JsStringLiteral x, JsContext ctx) { + public void visitString(JsStringLiteral x) { p.print(javaScriptString(x.getValue())); } @Override - public void visit(JsSwitch x, JsContext ctx) { + public void visit(JsSwitch x) { p.print(CHARS_SWITCH); spaceOpt(); leftParen(); @@ -783,19 +787,19 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public void visitThis(JsLiteral.JsThisRef x, JsContext ctx) { + public void visitThis(JsLiteral.JsThisRef x) { p.print(CHARS_THIS); } @Override - public void visitThrow(JsThrow x, JsContext ctx) { + public void visitThrow(JsThrow x) { p.print(CHARS_THROW); space(); accept(x.getExpression()); } @Override - public void visitTry(JsTry x, JsContext ctx) { + public void visitTry(JsTry x) { p.print(CHARS_TRY); spaceOpt(); accept(x.getTryBlock()); @@ -811,7 +815,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public void visit(JsVar var, JsContext ctx) { + public void visit(JsVar var) { nameOf(var); JsExpression initExpr = var.getInitExpression(); if (initExpr != null) { @@ -827,7 +831,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public void visitVars(JsVars vars, JsContext context) { + public void visitVars(JsVars vars) { var(); space(); boolean sep = false; @@ -848,7 +852,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public void visitDocComment(JsDocComment comment, JsContext context) { + public void visitDocComment(JsDocComment comment) { boolean asSingleLine = comment.getTags().size() == 1; if (!asSingleLine) { newlineOpt(); @@ -881,7 +885,7 @@ public class JsToStringGenerationVisitor extends JsVisitor { p.print((CharSequence) value); } else { - visitNameRef((JsNameRef) value, context); + visitNameRef((JsNameRef) value); } } @@ -1040,10 +1044,6 @@ public class JsToStringGenerationVisitor extends JsVisitor { nameDef(hasName.getName()); } - private void nameRef(JsNameRef nameRef) { - p.print(nameRef.getIdent()); - } - private boolean nestedPop(JsStatement statement) { boolean pop = !(statement instanceof JsBlock); if (pop) { @@ -1070,14 +1070,6 @@ public class JsToStringGenerationVisitor extends JsVisitor { return parentPrec > childPrec || parentPrec == childPrec && wrongAssoc; } - private boolean parenPop(JsExpression parent, JsExpression child, boolean wrongAssoc) { - boolean doPop = parenCalc(parent, child, wrongAssoc); - if (doPop) { - rightParen(); - } - return doPop; - } - private boolean _parenPopOrSpace(JsExpression parent, JsExpression child, boolean wrongAssoc) { boolean doPop = parenCalc(parent, child, wrongAssoc); if (doPop) { diff --git a/src/com/google/dart/compiler/backend/js/ast/ChameleonJsExpression.java b/src/com/google/dart/compiler/backend/js/ast/ChameleonJsExpression.java index 4229c344dd0..89fa1f4ada6 100644 --- a/src/com/google/dart/compiler/backend/js/ast/ChameleonJsExpression.java +++ b/src/com/google/dart/compiler/backend/js/ast/ChameleonJsExpression.java @@ -25,13 +25,13 @@ public class ChameleonJsExpression implements JsExpression { } @Override - public void accept(JsVisitor visitor, JsContext context) { - expression.accept(visitor, context); + public void accept(JsVisitor visitor) { + expression.accept(visitor); } @Override - public void acceptChildren(JsVisitor visitor, JsContext context) { - expression.acceptChildren(visitor, context); + public void acceptChildren(JsVisitor visitor) { + expression.acceptChildren(visitor); } @Override diff --git a/src/com/google/dart/compiler/backend/js/ast/JsArrayAccess.java b/src/com/google/dart/compiler/backend/js/ast/JsArrayAccess.java index 94e7470095c..4a0475f224a 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsArrayAccess.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsArrayAccess.java @@ -37,12 +37,12 @@ public final class JsArrayAccess extends JsExpressionImpl { } @Override - public void accept(JsVisitor v, JsContext context) { - v.visitArrayAccess(this, context); + public void accept(JsVisitor v) { + v.visitArrayAccess(this); } @Override - public void acceptChildren(JsVisitor visitor, JsContext context) { + public void acceptChildren(JsVisitor visitor) { visitor.accept(arrayExpression); visitor.accept(indexExpression); } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsArrayLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsArrayLiteral.java index 8d43ed05758..3398d8ee345 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsArrayLiteral.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsArrayLiteral.java @@ -27,12 +27,12 @@ public final class JsArrayLiteral extends JsLiteral { } @Override - public void accept(JsVisitor v, JsContext context) { - v.visitArray(this, context); + public void accept(JsVisitor v) { + v.visitArray(this); } @Override - public void acceptChildren(JsVisitor visitor, JsContext context) { + public void acceptChildren(JsVisitor visitor) { visitor.acceptWithInsertRemove(expressions); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsBinaryOperation.java b/src/com/google/dart/compiler/backend/js/ast/JsBinaryOperation.java index e0e0405da4e..d13cd077aab 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsBinaryOperation.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsBinaryOperation.java @@ -34,12 +34,12 @@ public final class JsBinaryOperation extends JsExpressionImpl { } @Override - public void accept(JsVisitor v, JsContext context) { - v.visitBinaryExpression(this, context); + public void accept(JsVisitor v) { + v.visitBinaryExpression(this); } @Override - public void acceptChildren(JsVisitor visitor, JsContext context) { + public void acceptChildren(JsVisitor visitor) { if (op.isAssignment()) { visitor.acceptLvalue(arg1); } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsBlock.java b/src/com/google/dart/compiler/backend/js/ast/JsBlock.java index 4538f88cf7a..f27a4cae049 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsBlock.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsBlock.java @@ -44,12 +44,12 @@ public class JsBlock extends SourceInfoAwareJsNode implements JsStatement { } @Override - public void accept(JsVisitor v, JsContext context) { - v.visitBlock(this, context); + public void accept(JsVisitor v) { + v.visitBlock(this); } @Override - public void acceptChildren(JsVisitor visitor, JsContext context) { + public void acceptChildren(JsVisitor visitor) { visitor.acceptWithInsertRemove(statements); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsBreak.java b/src/com/google/dart/compiler/backend/js/ast/JsBreak.java index 4513a153a45..82f53548696 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsBreak.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsBreak.java @@ -17,7 +17,7 @@ public final class JsBreak extends JsContinue { } @Override - public void accept(JsVisitor v, JsContext context) { - v.visitBreak(this, context); + public void accept(JsVisitor v) { + v.visitBreak(this); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsCase.java b/src/com/google/dart/compiler/backend/js/ast/JsCase.java index a03aa745892..0f9cf3270de 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsCase.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsCase.java @@ -23,13 +23,13 @@ public final class JsCase extends JsSwitchMember { } @Override - public void accept(JsVisitor v, JsContext context) { - v.visitCase(this, context); + public void accept(JsVisitor v) { + v.visitCase(this); } @Override - public void acceptChildren(JsVisitor visitor, JsContext context) { + public void acceptChildren(JsVisitor visitor) { visitor.accept(caseExpression); - super.acceptChildren(visitor, context); + super.acceptChildren(visitor); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsCatch.java b/src/com/google/dart/compiler/backend/js/ast/JsCatch.java index af1a322e0ee..41d431cb2d3 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsCatch.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsCatch.java @@ -48,12 +48,12 @@ public class JsCatch extends SourceInfoAwareJsNode implements HasCondition { } @Override - public void accept(JsVisitor v, JsContext context) { - v.visitCatch(this, context); + public void accept(JsVisitor v) { + v.visitCatch(this); } @Override - public void acceptChildren(JsVisitor visitor, JsContext context) { + public void acceptChildren(JsVisitor visitor) { visitor.accept(param); if (condition != null) { visitor.accept(condition); diff --git a/src/com/google/dart/compiler/backend/js/ast/JsConditional.java b/src/com/google/dart/compiler/backend/js/ast/JsConditional.java index 97695c35b8d..597891e1e7d 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsConditional.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsConditional.java @@ -43,12 +43,12 @@ public final class JsConditional extends JsExpressionImpl { } @Override - public void accept(JsVisitor v, JsContext context) { - v.visitConditional(this, context); + public void accept(JsVisitor v) { + v.visitConditional(this); } @Override - public void acceptChildren(JsVisitor visitor, JsContext context) { + public void acceptChildren(JsVisitor visitor) { visitor.accept(testExpression); visitor.accept(thenExpression); visitor.accept(elseExpression); diff --git a/src/com/google/dart/compiler/backend/js/ast/JsContinue.java b/src/com/google/dart/compiler/backend/js/ast/JsContinue.java index a3fd84cc43d..d5b5d60ab60 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsContinue.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsContinue.java @@ -23,7 +23,7 @@ public class JsContinue extends SourceInfoAwareJsNode implements JsStatement { } @Override - public void accept(JsVisitor v, JsContext context) { - v.visitContinue(this, context); + public void accept(JsVisitor v) { + v.visitContinue(this); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsDebugger.java b/src/com/google/dart/compiler/backend/js/ast/JsDebugger.java index c27240a2468..cbf6063e986 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsDebugger.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsDebugger.java @@ -12,12 +12,12 @@ public class JsDebugger extends SourceInfoAwareJsNode implements JsStatement { } @Override - public void accept(JsVisitor v, JsContext context) { - v.visitDebugger(this, context); + public void accept(JsVisitor v) { + v.visitDebugger(this); } @Override - public void acceptChildren(JsVisitor visitor, JsContext context) { + public void acceptChildren(JsVisitor visitor) { } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsDefault.java b/src/com/google/dart/compiler/backend/js/ast/JsDefault.java index 4a83b7c4979..330005ddd57 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsDefault.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsDefault.java @@ -9,7 +9,7 @@ package com.google.dart.compiler.backend.js.ast; */ public final class JsDefault extends JsSwitchMember { @Override - public void accept(JsVisitor v, JsContext context) { - v.visitDefault(this, context); + public void accept(JsVisitor v) { + v.visitDefault(this); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsDoWhile.java b/src/com/google/dart/compiler/backend/js/ast/JsDoWhile.java index 87dbf9a7636..f23b08c26e4 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsDoWhile.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsDoWhile.java @@ -16,7 +16,7 @@ public class JsDoWhile extends JsWhile { } @Override - public void accept(JsVisitor v, JsContext context) { - v.visitDoWhile(this, context); + public void accept(JsVisitor v) { + v.visitDoWhile(this); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsDocComment.java b/src/com/google/dart/compiler/backend/js/ast/JsDocComment.java index 60f83d394f8..292c9070ab8 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsDocComment.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsDocComment.java @@ -23,7 +23,7 @@ public class JsDocComment extends JsExpressionImpl { } @Override - public void accept(JsVisitor v, JsContext context) { - v.visitDocComment(this, context); + public void accept(JsVisitor v) { + v.visitDocComment(this); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsEmpty.java b/src/com/google/dart/compiler/backend/js/ast/JsEmpty.java index 7298da8e4e9..602e1e63308 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsEmpty.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsEmpty.java @@ -9,7 +9,7 @@ public class JsEmpty extends SourceInfoAwareJsNode implements JsStatement { } @Override - public void accept(JsVisitor v, JsContext context) { - v.visitEmpty(this, context); + public void accept(JsVisitor v) { + v.visitEmpty(this); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsExpressionStatement.java b/src/com/google/dart/compiler/backend/js/ast/JsExpressionStatement.java index c60cc252e88..3df96ca3c86 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsExpressionStatement.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsExpressionStatement.java @@ -16,12 +16,12 @@ public final class JsExpressionStatement extends AbstractNode implements JsState } @Override - public void accept(JsVisitor v, JsContext context) { - v.visitExpressionStatement(this, context); + public void accept(JsVisitor v) { + v.visitExpressionStatement(this); } @Override - public void acceptChildren(JsVisitor visitor, JsContext context) { + public void acceptChildren(JsVisitor visitor) { visitor.accept(expression); } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsFor.java b/src/com/google/dart/compiler/backend/js/ast/JsFor.java index 1b582cca10e..b7387ec0101 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsFor.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsFor.java @@ -71,12 +71,12 @@ public class JsFor extends SourceInfoAwareJsNode implements JsStatement { } @Override - public void accept(JsVisitor v, JsContext context) { - v.visitFor(this, context); + public void accept(JsVisitor v) { + v.visitFor(this); } @Override - public void acceptChildren(JsVisitor visitor, JsContext context) { + public void acceptChildren(JsVisitor visitor) { assert (!(initExpression != null && initVars != null)); if (initExpression != null) { diff --git a/src/com/google/dart/compiler/backend/js/ast/JsForIn.java b/src/com/google/dart/compiler/backend/js/ast/JsForIn.java index bb06baceb9f..3fc4a915abf 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsForIn.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsForIn.java @@ -49,12 +49,12 @@ public class JsForIn extends SourceInfoAwareJsNode implements JsStatement { } @Override - public void accept(JsVisitor v, JsContext context) { - v.visitForIn(this, context); + public void accept(JsVisitor v) { + v.visitForIn(this); } @Override - public void acceptChildren(JsVisitor visitor, JsContext context) { + public void acceptChildren(JsVisitor visitor) { if (iterExpression != null) { visitor.acceptLvalue(iterExpression); } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsFunction.java b/src/com/google/dart/compiler/backend/js/ast/JsFunction.java index d9a60fc4e4a..1bc7b301b8a 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsFunction.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsFunction.java @@ -64,12 +64,12 @@ public final class JsFunction extends JsLiteral implements HasName { } @Override - public void accept(JsVisitor v, JsContext context) { - v.visitFunction(this, context); + public void accept(JsVisitor v) { + v.visitFunction(this); } @Override - public void acceptChildren(JsVisitor visitor, JsContext context) { + public void acceptChildren(JsVisitor visitor) { visitor.acceptWithInsertRemove(params); visitor.accept(body); } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsIf.java b/src/com/google/dart/compiler/backend/js/ast/JsIf.java index f7ec9fb6196..18af5ffb414 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsIf.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsIf.java @@ -51,12 +51,12 @@ public final class JsIf extends SourceInfoAwareJsNode implements JsStatement { } @Override - public void accept(JsVisitor v, JsContext context) { - v.visitIf(this, context); + public void accept(JsVisitor v) { + v.visitIf(this); } @Override - public void acceptChildren(JsVisitor visitor, JsContext context) { + public void acceptChildren(JsVisitor visitor) { visitor.accept(ifExpression); visitor.accept(thenStatement); if (elseStatement != null) { diff --git a/src/com/google/dart/compiler/backend/js/ast/JsInvocation.java b/src/com/google/dart/compiler/backend/js/ast/JsInvocation.java index 234f7b16e11..34da9bb7191 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsInvocation.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsInvocation.java @@ -52,12 +52,12 @@ public final class JsInvocation extends JsExpressionImpl.JsExpressionHasArgument } @Override - public void accept(JsVisitor v, JsContext context) { - v.visitInvocation(this, context); + public void accept(JsVisitor v) { + v.visitInvocation(this); } @Override - public void acceptChildren(JsVisitor visitor, JsContext context) { + public void acceptChildren(JsVisitor visitor) { visitor.accept(qualifier); visitor.acceptList(arguments); } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsLabel.java b/src/com/google/dart/compiler/backend/js/ast/JsLabel.java index 70966f7609c..a3abb3392bc 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsLabel.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsLabel.java @@ -42,12 +42,12 @@ public class JsLabel extends SourceInfoAwareJsNode implements JsStatement, HasNa } @Override - public void accept(JsVisitor v, JsContext context) { - v.visitLabel(this, context); + public void accept(JsVisitor v) { + v.visitLabel(this); } @Override - public void acceptChildren(JsVisitor visitor, JsContext context) { + public void acceptChildren(JsVisitor visitor) { visitor.accept(statement); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsLiteral.java index 574a7e99e94..8823d12b557 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsLiteral.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsLiteral.java @@ -23,8 +23,8 @@ public abstract class JsLiteral extends JsExpressionImpl { } @Override - public void accept(JsVisitor v, JsContext context) { - v.visitThis(this, context); + public void accept(JsVisitor v) { + v.visitThis(this); } } @@ -41,8 +41,8 @@ public abstract class JsLiteral extends JsExpressionImpl { } @Override - public void accept(JsVisitor v, JsContext context) { - v.visitBoolean(this, context); + public void accept(JsVisitor v) { + v.visitBoolean(this); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNameRef.java b/src/com/google/dart/compiler/backend/js/ast/JsNameRef.java index 3dcfea61d98..6fb7bc3e5ae 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsNameRef.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsNameRef.java @@ -69,12 +69,12 @@ public final class JsNameRef extends JsExpressionImpl implements HasName { } @Override - public void accept(JsVisitor v, JsContext context) { - v.visitNameRef(this, context); + public void accept(JsVisitor v) { + v.visitNameRef(this); } @Override - public void acceptChildren(JsVisitor visitor, JsContext context) { + public void acceptChildren(JsVisitor visitor) { if (qualifier != null) { visitor.accept(qualifier); } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNew.java b/src/com/google/dart/compiler/backend/js/ast/JsNew.java index a5eecda234c..a5283f284c0 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsNew.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsNew.java @@ -25,12 +25,12 @@ public final class JsNew extends JsExpressionImpl.JsExpressionHasArguments { } @Override - public void accept(JsVisitor v, JsContext context) { - v.visitNew(this, context); + public void accept(JsVisitor v) { + v.visitNew(this); } @Override - public void acceptChildren(JsVisitor visitor, JsContext context) { + public void acceptChildren(JsVisitor visitor) { visitor.accept(constructorExpression); visitor.acceptList(arguments); } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNode.java b/src/com/google/dart/compiler/backend/js/ast/JsNode.java index 308b71393b8..10b9dc6e73e 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsNode.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsNode.java @@ -11,9 +11,9 @@ public interface JsNode extends HasSourceInfo { * Causes this object to have the visitor visit itself and its children. * * @param visitor the visitor that should traverse this node - * @param context the context of an existing traversal + * */ - void accept(JsVisitor visitor, JsContext context); + void accept(JsVisitor visitor); - void acceptChildren(JsVisitor visitor, JsContext context); + void acceptChildren(JsVisitor visitor); } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNullLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsNullLiteral.java index 4d9cd17a333..8aec6b41e16 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsNullLiteral.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsNullLiteral.java @@ -9,7 +9,7 @@ public final class JsNullLiteral extends JsLiteral.JsValueLiteral { } @Override - public void accept(JsVisitor v, JsContext context) { - v.visitNull(this, context); + public void accept(JsVisitor v) { + v.visitNull(this); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNumberLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsNumberLiteral.java index 9ee62587920..6dc3d156754 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsNumberLiteral.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsNumberLiteral.java @@ -13,8 +13,8 @@ public abstract class JsNumberLiteral extends JsLiteral.JsValueLiteral { } @Override - public void accept(JsVisitor v, JsContext context) { - v.visitDouble(this, context); + public void accept(JsVisitor v) { + v.visitDouble(this); } public String toString() { @@ -30,8 +30,8 @@ public abstract class JsNumberLiteral extends JsLiteral.JsValueLiteral { } @Override - public void accept(JsVisitor v, JsContext context) { - v.visitInt(this, context); + public void accept(JsVisitor v) { + v.visitInt(this); } public String toString() { diff --git a/src/com/google/dart/compiler/backend/js/ast/JsObjectLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsObjectLiteral.java index 9aa35198399..efa53053914 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsObjectLiteral.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsObjectLiteral.java @@ -39,12 +39,12 @@ public final class JsObjectLiteral extends JsLiteral { } @Override - public void accept(JsVisitor v, JsContext context) { - v.visitObjectLiteral(this, context); + public void accept(JsVisitor v) { + v.visitObjectLiteral(this); } @Override - public void acceptChildren(JsVisitor visitor, JsContext context) { + public void acceptChildren(JsVisitor visitor) { visitor.acceptWithInsertRemove(properties); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsParameter.java b/src/com/google/dart/compiler/backend/js/ast/JsParameter.java index 6080c39fda1..9167a30ece9 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsParameter.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsParameter.java @@ -27,7 +27,7 @@ public final class JsParameter extends SourceInfoAwareJsNode implements HasName } @Override - public void accept(JsVisitor v, JsContext context) { - v.visitParameter(this, context); + public void accept(JsVisitor v) { + v.visitParameter(this); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsPostfixOperation.java b/src/com/google/dart/compiler/backend/js/ast/JsPostfixOperation.java index f86231c8ff6..df59b0d1f5a 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsPostfixOperation.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsPostfixOperation.java @@ -14,7 +14,7 @@ public final class JsPostfixOperation extends JsUnaryOperation { } @Override - public void accept(JsVisitor v, JsContext context) { - v.visitPostfixOperation(this, context); + public void accept(JsVisitor v) { + v.visitPostfixOperation(this); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsPrefixOperation.java b/src/com/google/dart/compiler/backend/js/ast/JsPrefixOperation.java index 9c943e20e02..552d2e9eec2 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsPrefixOperation.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsPrefixOperation.java @@ -14,7 +14,7 @@ public final class JsPrefixOperation extends JsUnaryOperation { } @Override - public void accept(JsVisitor v, JsContext context) { - v.visitPrefixOperation(this, context); + public void accept(JsVisitor v) { + v.visitPrefixOperation(this); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsProgram.java b/src/com/google/dart/compiler/backend/js/ast/JsProgram.java index a8f10523557..2954c33321d 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsProgram.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsProgram.java @@ -7,7 +7,6 @@ package com.google.dart.compiler.backend.js.ast; import gnu.trove.TDoubleObjectHashMap; import gnu.trove.THashMap; import gnu.trove.TIntObjectHashMap; -import org.jetbrains.annotations.Nullable; import java.util.Map; @@ -109,12 +108,12 @@ public final class JsProgram extends SourceInfoAwareJsNode { } @Override - public void accept(JsVisitor v, @Nullable JsContext context) { - v.visitProgram(this, context); + public void accept(JsVisitor v) { + v.visitProgram(this); } @Override - public void acceptChildren(JsVisitor visitor, JsContext context) { + public void acceptChildren(JsVisitor visitor) { for (JsProgramFragment fragment : fragments) { visitor.accept(fragment); } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsProgramFragment.java b/src/com/google/dart/compiler/backend/js/ast/JsProgramFragment.java index 61fde23eb8e..e9691878c11 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsProgramFragment.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsProgramFragment.java @@ -19,12 +19,12 @@ public class JsProgramFragment extends SourceInfoAwareJsNode { } @Override - public void accept(JsVisitor v, JsContext context) { - v.visitProgramFragment(this, context); + public void accept(JsVisitor v) { + v.visitProgramFragment(this); } @Override - public void acceptChildren(JsVisitor visitor, JsContext context) { + public void acceptChildren(JsVisitor visitor) { visitor.accept(globalBlock); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsPropertyInitializer.java b/src/com/google/dart/compiler/backend/js/ast/JsPropertyInitializer.java index 9679fd02bb1..f38885b7316 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsPropertyInitializer.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsPropertyInitializer.java @@ -35,12 +35,12 @@ public class JsPropertyInitializer extends SourceInfoAwareJsNode { } @Override - public void accept(JsVisitor v, JsContext context) { - v.visitPropertyInitializer(this, context); + public void accept(JsVisitor v) { + v.visitPropertyInitializer(this); } @Override - public void acceptChildren(JsVisitor visitor, JsContext context) { + public void acceptChildren(JsVisitor visitor) { visitor.accept(labelExpr); visitor.accept(valueExpr); } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsRegExp.java b/src/com/google/dart/compiler/backend/js/ast/JsRegExp.java index 568e9ed82c6..e958b368f39 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsRegExp.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsRegExp.java @@ -4,9 +4,6 @@ package com.google.dart.compiler.backend.js.ast; -/** - * A JavaScript regular expression. - */ public final class JsRegExp extends JsLiteral.JsValueLiteral { private String flags; private String pattern; @@ -31,7 +28,7 @@ public final class JsRegExp extends JsLiteral.JsValueLiteral { } @Override - public void accept(JsVisitor v, JsContext context) { - v.visitRegExp(this, context); + public void accept(JsVisitor v) { + v.visitRegExp(this); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsReturn.java b/src/com/google/dart/compiler/backend/js/ast/JsReturn.java index 311f4532a5c..93c0106d802 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsReturn.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsReturn.java @@ -26,12 +26,12 @@ public final class JsReturn extends SourceInfoAwareJsNode implements JsStatement } @Override - public void accept(JsVisitor v, JsContext context) { - v.visitReturn(this, context); + public void accept(JsVisitor v) { + v.visitReturn(this); } @Override - public void acceptChildren(JsVisitor visitor, JsContext context) { + public void acceptChildren(JsVisitor visitor) { if (expression != null) { visitor.accept(expression); } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsStringLiteral.java b/src/com/google/dart/compiler/backend/js/ast/JsStringLiteral.java index 9da55039453..08d9d40fbf7 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsStringLiteral.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsStringLiteral.java @@ -18,7 +18,7 @@ public final class JsStringLiteral extends JsLiteral.JsValueLiteral { } @Override - public void accept(JsVisitor v, JsContext context) { - v.visitString(this, context); + public void accept(JsVisitor v) { + v.visitString(this); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsSwitch.java b/src/com/google/dart/compiler/backend/js/ast/JsSwitch.java index d9f379ddfef..763c6c1feeb 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsSwitch.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsSwitch.java @@ -32,12 +32,12 @@ public class JsSwitch extends SourceInfoAwareJsNode implements JsStatement { } @Override - public void accept(JsVisitor v, JsContext context) { - v.visit(this, context); + public void accept(JsVisitor v) { + v.visit(this); } @Override - public void acceptChildren(JsVisitor visitor, JsContext context) { + public void acceptChildren(JsVisitor visitor) { visitor.accept(expression); visitor.acceptWithInsertRemove(cases); } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsSwitchMember.java b/src/com/google/dart/compiler/backend/js/ast/JsSwitchMember.java index 8227c24c3cb..225369525ff 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsSwitchMember.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsSwitchMember.java @@ -23,7 +23,7 @@ public abstract class JsSwitchMember extends SourceInfoAwareJsNode { } @Override - public void acceptChildren(JsVisitor visitor, JsContext context) { + public void acceptChildren(JsVisitor visitor) { visitor.acceptWithInsertRemove(statements); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsThrow.java b/src/com/google/dart/compiler/backend/js/ast/JsThrow.java index 88dfd185ed1..c7814d73c58 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsThrow.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsThrow.java @@ -23,12 +23,12 @@ public class JsThrow extends SourceInfoAwareJsNode implements JsStatement { } @Override - public void accept(JsVisitor v, JsContext context) { - v.visitThrow(this, context); + public void accept(JsVisitor v) { + v.visitThrow(this); } @Override - public void acceptChildren(JsVisitor visitor, JsContext context) { + public void acceptChildren(JsVisitor visitor) { visitor.accept(expression); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsTry.java b/src/com/google/dart/compiler/backend/js/ast/JsTry.java index 40f5e04f19e..f559b340fd6 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsTry.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsTry.java @@ -48,12 +48,12 @@ public class JsTry extends SourceInfoAwareJsNode implements JsStatement { } @Override - public void accept(JsVisitor v, JsContext context) { - v.visitTry(this, context); + public void accept(JsVisitor v) { + v.visitTry(this); } @Override - public void acceptChildren(JsVisitor visitor, JsContext context) { + public void acceptChildren(JsVisitor visitor) { visitor.accept(tryBlock); visitor.acceptWithInsertRemove(catches); if (finallyBlock != null) { diff --git a/src/com/google/dart/compiler/backend/js/ast/JsUnaryOperation.java b/src/com/google/dart/compiler/backend/js/ast/JsUnaryOperation.java index c428c7d7f69..9e5d1b360c3 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsUnaryOperation.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsUnaryOperation.java @@ -33,7 +33,7 @@ public abstract class JsUnaryOperation extends JsExpressionImpl { } @Override - public void acceptChildren(JsVisitor visitor, JsContext context) { + public void acceptChildren(JsVisitor visitor) { if (op.isModifying()) { // The delete operator is practically like an assignment of undefined, so // for practical purposes we're treating it as an lvalue. diff --git a/src/com/google/dart/compiler/backend/js/ast/JsVars.java b/src/com/google/dart/compiler/backend/js/ast/JsVars.java index 1e72f4aeaa2..3e9de8bf4ec 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsVars.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsVars.java @@ -81,12 +81,12 @@ public class JsVars extends SourceInfoAwareJsNode implements JsStatement, Iterab } @Override - public void accept(JsVisitor v, JsContext context) { - v.visit(this, context); + public void accept(JsVisitor v) { + v.visit(this); } @Override - public void acceptChildren(JsVisitor visitor, JsContext context) { + public void acceptChildren(JsVisitor visitor) { if (initExpression != null) { visitor.accept(initExpression); } @@ -113,12 +113,12 @@ public class JsVars extends SourceInfoAwareJsNode implements JsStatement, Iterab } @Override - public void accept(JsVisitor v, JsContext context) { - v.visitVars(this, context); + public void accept(JsVisitor v) { + v.visitVars(this); } @Override - public void acceptChildren(JsVisitor visitor, JsContext context) { + public void acceptChildren(JsVisitor visitor) { visitor.acceptWithInsertRemove(vars); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsVisitor.java b/src/com/google/dart/compiler/backend/js/ast/JsVisitor.java index e0791440238..68df956b151 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsVisitor.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsVisitor.java @@ -9,280 +9,202 @@ import com.google.dart.compiler.backend.js.ast.JsVars.JsVar; import java.util.List; public abstract class JsVisitor { - protected static final JsContext LVALUE_CONTEXT = new JsContext() { - @Override - public boolean canInsert() { - return false; - } - - @Override - public boolean canRemove() { - return false; - } - - @Override - public void insertAfter(JsNode node) { - throw new UnsupportedOperationException(); - } - - @Override - public void insertBefore(JsNode node) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean isLvalue() { - return true; - } - - @Override - public void removeMe() { - throw new UnsupportedOperationException(); - } - - @Override - public void replaceMe(JsNode node) { - throw new UnsupportedOperationException(); - } - }; - - protected static final JsContext UNMODIFIABLE_CONTEXT = new JsContext() { - @Override - public boolean canInsert() { - return false; - } - - @Override - public boolean canRemove() { - return false; - } - - @Override - public void insertAfter(JsNode node) { - throw new UnsupportedOperationException(); - } - - @Override - public void insertBefore(JsNode node) { - throw new UnsupportedOperationException(); - } - - @Override - public boolean isLvalue() { - return false; - } - - @Override - public void removeMe() { - throw new UnsupportedOperationException(); - } - - @Override - public void replaceMe(JsNode node) { - throw new UnsupportedOperationException(); - } - }; - public void accept(T node) { - doAccept(node, UNMODIFIABLE_CONTEXT); + node.accept(this); } public final void acceptList(List collection) { for (T node : collection) { - doAccept(node, UNMODIFIABLE_CONTEXT); + accept(node); } } public void acceptLvalue(JsExpression expression) { - doAccept(expression, LVALUE_CONTEXT); + accept(expression); } public final void acceptWithInsertRemove(List collection) { for (T node : collection) { - doAccept(node, UNMODIFIABLE_CONTEXT); + accept(node); } } - public void visitArrayAccess(JsArrayAccess x, JsContext context) { - visitElement(x, context); + public void visitArrayAccess(JsArrayAccess x) { + visitElement(x); } - public void visitArray(JsArrayLiteral x, JsContext context) { - visitElement(x, context); + public void visitArray(JsArrayLiteral x) { + visitElement(x); } - public void visitBinaryExpression(JsBinaryOperation x, JsContext context) { - visitElement(x, context); + public void visitBinaryExpression(JsBinaryOperation x) { + visitElement(x); } - public void visitBlock(JsBlock x, JsContext context) { - visitElement(x, context); + public void visitBlock(JsBlock x) { + visitElement(x); } - public void visitBoolean(JsLiteral.JsBooleanLiteral x, JsContext context) { - visitElement(x, context); + public void visitBoolean(JsLiteral.JsBooleanLiteral x) { + visitElement(x); } - public void visitBreak(JsBreak x, JsContext context) { - visitElement(x, context); + public void visitBreak(JsBreak x) { + visitElement(x); } - public void visitCase(JsCase x, JsContext ctx) { - visitElement(x, ctx); + public void visitCase(JsCase x) { + visitElement(x); } - public void visitCatch(JsCatch x, JsContext ctx) { - visitElement(x, ctx); + public void visitCatch(JsCatch x) { + visitElement(x); } - public void visitConditional(JsConditional x, JsContext ctx) { - visitElement(x, ctx); + public void visitConditional(JsConditional x) { + visitElement(x); } - public void visitContinue(JsContinue x, JsContext ctx) { - visitElement(x, ctx); + public void visitContinue(JsContinue x) { + visitElement(x); } - public void visitDebugger(JsDebugger x, JsContext context) { - visitElement(x, context); + public void visitDebugger(JsDebugger x) { + visitElement(x); } - public void visitDefault(JsDefault x, JsContext context) { - visitElement(x, context); + public void visitDefault(JsDefault x) { + visitElement(x); } - public void visitDoWhile(JsDoWhile x, JsContext context) { - visitElement(x, context); + public void visitDoWhile(JsDoWhile x) { + visitElement(x); } - public void visitEmpty(JsEmpty x, JsContext ctx) { - visitElement(x, ctx); + public void visitEmpty(JsEmpty x) { + visitElement(x); } - public void visitExpressionStatement(JsExpressionStatement x, JsContext context) { - visitElement(x, context); + public void visitExpressionStatement(JsExpressionStatement x) { + visitElement(x); } - public void visitFor(JsFor x, JsContext ctx) { - visitElement(x, ctx); + public void visitFor(JsFor x) { + visitElement(x); } - public void visitForIn(JsForIn x, JsContext ctx) { - visitElement(x, ctx); + public void visitForIn(JsForIn x) { + visitElement(x); } - public void visitFunction(JsFunction x, JsContext ctx) { - visitElement(x, ctx); + public void visitFunction(JsFunction x) { + visitElement(x); } - public void visitIf(JsIf x, JsContext ctx) { - visitElement(x, ctx); + public void visitIf(JsIf x) { + visitElement(x); } - public void visitInvocation(JsInvocation x, JsContext ctx) { - visitElement(x, ctx); + public void visitInvocation(JsInvocation x) { + visitElement(x); } - public void visitLabel(JsLabel x, JsContext ctx) { - visitElement(x, ctx); + public void visitLabel(JsLabel x) { + visitElement(x); } - public void visitNameRef(JsNameRef x, JsContext ctx) { - visitElement(x, ctx); + public void visitNameRef(JsNameRef nameRef) { + visitElement(nameRef); } - public void visitNew(JsNew x, JsContext ctx) { - visitElement(x, ctx); + public void visitNew(JsNew x) { + visitElement(x); } - public void visitNull(JsNullLiteral x, JsContext ctx) { - visitElement(x, ctx); + public void visitNull(JsNullLiteral x) { + visitElement(x); } - public void visitInt(JsNumberLiteral.JsIntLiteral x, JsContext context) { - visitElement(x, context); + public void visitInt(JsNumberLiteral.JsIntLiteral x) { + visitElement(x); } - public void visitDouble(JsNumberLiteral.JsDoubleLiteral x, JsContext context) { - visitElement(x, context); + public void visitDouble(JsNumberLiteral.JsDoubleLiteral x) { + visitElement(x); } - public void visitObjectLiteral(JsObjectLiteral x, JsContext context) { - visitElement(x, context); + public void visitObjectLiteral(JsObjectLiteral x) { + visitElement(x); } - public void visitParameter(JsParameter x, JsContext context) { - visitElement(x, context); + public void visitParameter(JsParameter x) { + visitElement(x); } - public void visitPostfixOperation(JsPostfixOperation x, JsContext context) { - visitElement(x, context); + public void visitPostfixOperation(JsPostfixOperation x) { + visitElement(x); } - public void visitPrefixOperation(JsPrefixOperation x, JsContext context) { - visitElement(x, context); + public void visitPrefixOperation(JsPrefixOperation x) { + visitElement(x); } - public void visitProgram(JsProgram x, JsContext context) { - visitElement(x, context); + public void visitProgram(JsProgram x) { + visitElement(x); } - public void visitProgramFragment(JsProgramFragment x, JsContext context) { - visitElement(x, context); + public void visitProgramFragment(JsProgramFragment x) { + visitElement(x); } - public void visitPropertyInitializer(JsPropertyInitializer x, JsContext context) { - visitElement(x, context); + public void visitPropertyInitializer(JsPropertyInitializer x) { + visitElement(x); } - public void visitRegExp(JsRegExp x, JsContext context) { - visitElement(x, context); + public void visitRegExp(JsRegExp x) { + visitElement(x); } - public void visitReturn(JsReturn x, JsContext context) { - visitElement(x, context); + public void visitReturn(JsReturn x) { + visitElement(x); } - public void visitString(JsStringLiteral x, JsContext context) { - visitElement(x, context); + public void visitString(JsStringLiteral x) { + visitElement(x); } - public void visit(JsSwitch x, JsContext context) { - visitElement(x, context); + public void visit(JsSwitch x) { + visitElement(x); } - public void visitThis(JsLiteral.JsThisRef x, JsContext context) { - visitElement(x, context); + public void visitThis(JsLiteral.JsThisRef x) { + visitElement(x); } - public void visitThrow(JsThrow x, JsContext context) { - visitElement(x, context); + public void visitThrow(JsThrow x) { + visitElement(x); } - public void visitTry(JsTry x, JsContext context) { - visitElement(x, context); + public void visitTry(JsTry x) { + visitElement(x); } - public void visit(JsVar x, JsContext context) { - visitElement(x, context); + public void visit(JsVar x) { + visitElement(x); } - public void visitVars(JsVars x, JsContext context) { - visitElement(x, context); + public void visitVars(JsVars x) { + visitElement(x); } - public void visitWhile(JsWhile x, JsContext context) { - visitElement(x, context); + public void visitWhile(JsWhile x) { + visitElement(x); } - public void visitDocComment(JsDocComment comment, JsContext context) { - visitElement(comment, context); + public void visitDocComment(JsDocComment comment) { + visitElement(comment); } - protected void visitElement(JsNode node, JsContext context) { - } - - protected void doAccept(JsNode node, JsContext context) { - node.accept(this, context); + protected void visitElement(JsNode node) { } } \ No newline at end of file diff --git a/src/com/google/dart/compiler/backend/js/ast/JsWhile.java b/src/com/google/dart/compiler/backend/js/ast/JsWhile.java index 7b6d1f9fb58..aeaba3921e7 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsWhile.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsWhile.java @@ -36,12 +36,12 @@ public class JsWhile extends SourceInfoAwareJsNode implements JsStatement { } @Override - public void accept(JsVisitor v, JsContext context) { - v.visitWhile(this, context); + public void accept(JsVisitor v) { + v.visitWhile(this); } @Override - public void acceptChildren(JsVisitor visitor, JsContext context) { + public void acceptChildren(JsVisitor visitor) { visitor.accept(condition); visitor.accept(body); } diff --git a/src/com/google/dart/compiler/backend/js/ast/RecursiveJsVisitor.java b/src/com/google/dart/compiler/backend/js/ast/RecursiveJsVisitor.java index c148969f425..56cb331b789 100644 --- a/src/com/google/dart/compiler/backend/js/ast/RecursiveJsVisitor.java +++ b/src/com/google/dart/compiler/backend/js/ast/RecursiveJsVisitor.java @@ -2,7 +2,7 @@ package com.google.dart.compiler.backend.js.ast; public abstract class RecursiveJsVisitor extends JsVisitor { @Override - protected void visitElement(JsNode node, JsContext context) { - node.acceptChildren(this, context); + protected void visitElement(JsNode node) { + node.acceptChildren(this); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/SourceInfoAwareJsNode.java b/src/com/google/dart/compiler/backend/js/ast/SourceInfoAwareJsNode.java index d72ff609ad3..0c5cffa1e77 100644 --- a/src/com/google/dart/compiler/backend/js/ast/SourceInfoAwareJsNode.java +++ b/src/com/google/dart/compiler/backend/js/ast/SourceInfoAwareJsNode.java @@ -14,6 +14,6 @@ abstract class SourceInfoAwareJsNode extends AbstractNode { } @Override - public void acceptChildren(JsVisitor visitor, JsContext context) { + public void acceptChildren(JsVisitor visitor) { } } \ No newline at end of file From 623fb94649e693df77dd44c5b5ed39a187b00dbb Mon Sep 17 00:00:00 2001 From: develar Date: Fri, 26 Oct 2012 13:39:20 +0400 Subject: [PATCH 27/27] KT-927 --- .../js/JsConstructExpressionVisitor.java | 2 +- .../backend/js/JsFirstExpressionVisitor.java | 4 ++-- .../backend/js/JsPrecedenceVisitor.java | 2 +- .../js/JsToStringGenerationVisitor.java | 12 +++++++++--- .../backend/js/ast/ChameleonJsExpression.java | 13 +++++++++---- .../compiler/backend/js/ast/JsExpression.java | 7 ++++--- .../backend/js/ast/JsExpressionImpl.java | 6 ++++++ .../backend/js/ast/JsExpressionStatement.java | 9 +++++++-- .../compiler/backend/js/ast/JsInvocation.java | 3 --- .../dart/compiler/backend/js/ast/JsNode.java | 19 +++++++++++++++---- .../compiler/backend/js/ast/JsVisitor.java | 4 ++-- .../backend/js/ast/SourceInfoAwareJsNode.java | 16 +++++++++++----- .../dart/compiler/common/HasSourceInfo.java | 18 ------------------ .../google/dart/compiler/util/TextOutput.java | 2 ++ .../dart/compiler/util/TextOutputImpl.java | 3 ++- 15 files changed, 71 insertions(+), 49 deletions(-) delete mode 100644 src/com/google/dart/compiler/common/HasSourceInfo.java diff --git a/src/com/google/dart/compiler/backend/js/JsConstructExpressionVisitor.java b/src/com/google/dart/compiler/backend/js/JsConstructExpressionVisitor.java index 5524261a515..4cfd42b55c7 100644 --- a/src/com/google/dart/compiler/backend/js/JsConstructExpressionVisitor.java +++ b/src/com/google/dart/compiler/backend/js/JsConstructExpressionVisitor.java @@ -48,7 +48,7 @@ public class JsConstructExpressionVisitor extends RecursiveJsVisitor { } @Override - public void visitInvocation(JsInvocation x) { + public void visitInvocation(JsInvocation invocation) { containsInvocation = true; } diff --git a/src/com/google/dart/compiler/backend/js/JsFirstExpressionVisitor.java b/src/com/google/dart/compiler/backend/js/JsFirstExpressionVisitor.java index e2308a0344b..6789cd2bd71 100644 --- a/src/com/google/dart/compiler/backend/js/JsFirstExpressionVisitor.java +++ b/src/com/google/dart/compiler/backend/js/JsFirstExpressionVisitor.java @@ -73,8 +73,8 @@ public class JsFirstExpressionVisitor extends RecursiveJsVisitor { } @Override - public void visitInvocation(JsInvocation x) { - accept(x.getQualifier()); + public void visitInvocation(JsInvocation invocation) { + accept(invocation.getQualifier()); } @Override diff --git a/src/com/google/dart/compiler/backend/js/JsPrecedenceVisitor.java b/src/com/google/dart/compiler/backend/js/JsPrecedenceVisitor.java index 77fcf9b83f6..3a2d8115da9 100644 --- a/src/com/google/dart/compiler/backend/js/JsPrecedenceVisitor.java +++ b/src/com/google/dart/compiler/backend/js/JsPrecedenceVisitor.java @@ -75,7 +75,7 @@ class JsPrecedenceVisitor extends JsVisitor { } @Override - public void visitInvocation(JsInvocation x) { + public void visitInvocation(JsInvocation invocation) { answer = 16; } diff --git a/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java b/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java index 7a02a4274e5..6977040853b 100644 --- a/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java +++ b/src/com/google/dart/compiler/backend/js/JsToStringGenerationVisitor.java @@ -583,11 +583,11 @@ public class JsToStringGenerationVisitor extends JsVisitor { } @Override - public void visitInvocation(JsInvocation x) { - printPair(x, x.getQualifier()); + public void visitInvocation(JsInvocation invocation) { + printPair(invocation, invocation.getQualifier()); leftParen(); - printExpressions(x.getArguments()); + printExpressions(invocation.getArguments()); rightParen(); } @@ -621,9 +621,15 @@ public class JsToStringGenerationVisitor extends JsVisitor { } p.print('.'); } + + p.maybeIndent(); + beforeNodePrinted(nameRef); p.print(nameRef.getIdent()); } + protected void beforeNodePrinted(JsNode node) { + } + @Override public void visitNew(JsNew x) { p.print(CHARS_NEW); diff --git a/src/com/google/dart/compiler/backend/js/ast/ChameleonJsExpression.java b/src/com/google/dart/compiler/backend/js/ast/ChameleonJsExpression.java index 89fa1f4ada6..c50d40e8166 100644 --- a/src/com/google/dart/compiler/backend/js/ast/ChameleonJsExpression.java +++ b/src/com/google/dart/compiler/backend/js/ast/ChameleonJsExpression.java @@ -35,12 +35,17 @@ public class ChameleonJsExpression implements JsExpression { } @Override - public Object getSourceInfo() { - return expression.getSourceInfo(); + public Object getSource() { + return expression.getSource(); } @Override - public void setSourceInfo(Object info) { - expression.setSourceInfo(info); + public void setSource(Object info) { + expression.setSource(info); + } + + @Override + public JsExpression source(Object info) { + return expression.source(info); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsExpression.java b/src/com/google/dart/compiler/backend/js/ast/JsExpression.java index b4f8828c751..5209a21eb64 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsExpression.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsExpression.java @@ -1,9 +1,10 @@ package com.google.dart.compiler.backend.js.ast; -import com.google.dart.compiler.common.HasSourceInfo; - -public interface JsExpression extends JsNode, HasSourceInfo { +public interface JsExpression extends JsNode { boolean isLeaf(); JsStatement makeStmt(); + + @Override + JsExpression source(Object info); } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsExpressionImpl.java b/src/com/google/dart/compiler/backend/js/ast/JsExpressionImpl.java index 8dba62f3d04..f1f5bed3399 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsExpressionImpl.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsExpressionImpl.java @@ -36,4 +36,10 @@ abstract class JsExpressionImpl extends SourceInfoAwareJsNode implements JsExpre return arguments; } } + + @Override + public JsExpression source(Object info) { + setSource(info); + return this; + } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsExpressionStatement.java b/src/com/google/dart/compiler/backend/js/ast/JsExpressionStatement.java index 3df96ca3c86..85a7fe6bee4 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsExpressionStatement.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsExpressionStatement.java @@ -26,12 +26,17 @@ public final class JsExpressionStatement extends AbstractNode implements JsState } @Override - public Object getSourceInfo() { + public Object getSource() { return null; } @Override - public void setSourceInfo(Object info) { + public void setSource(Object info) { + throw new IllegalStateException("You must not set source info for JsExpressionStatement, set for expression"); + } + + @Override + public JsNode source(Object info) { throw new IllegalStateException("You must not set source info for JsExpressionStatement, set for expression"); } } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsInvocation.java b/src/com/google/dart/compiler/backend/js/ast/JsInvocation.java index 34da9bb7191..d7569b12f4b 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsInvocation.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsInvocation.java @@ -10,9 +10,6 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; -/** - * Represents a JavaScript invocation. - */ public final class JsInvocation extends JsExpressionImpl.JsExpressionHasArguments { private JsExpression qualifier; diff --git a/src/com/google/dart/compiler/backend/js/ast/JsNode.java b/src/com/google/dart/compiler/backend/js/ast/JsNode.java index 10b9dc6e73e..6d2ad279985 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsNode.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsNode.java @@ -4,16 +4,27 @@ package com.google.dart.compiler.backend.js.ast; -import com.google.dart.compiler.common.HasSourceInfo; - -public interface JsNode extends HasSourceInfo { +public interface JsNode { /** * Causes this object to have the visitor visit itself and its children. * * @param visitor the visitor that should traverse this node - * */ void accept(JsVisitor visitor); void acceptChildren(JsVisitor visitor); + + /** + * Return the source info associated with this object. + */ + Object getSource(); + + /** + * Set the source info associated with this object. + * + * @param info + */ + void setSource(Object info); + + JsNode source(Object info); } diff --git a/src/com/google/dart/compiler/backend/js/ast/JsVisitor.java b/src/com/google/dart/compiler/backend/js/ast/JsVisitor.java index 68df956b151..6d66d655981 100644 --- a/src/com/google/dart/compiler/backend/js/ast/JsVisitor.java +++ b/src/com/google/dart/compiler/backend/js/ast/JsVisitor.java @@ -105,8 +105,8 @@ public abstract class JsVisitor { visitElement(x); } - public void visitInvocation(JsInvocation x) { - visitElement(x); + public void visitInvocation(JsInvocation invocation) { + visitElement(invocation); } public void visitLabel(JsLabel x) { diff --git a/src/com/google/dart/compiler/backend/js/ast/SourceInfoAwareJsNode.java b/src/com/google/dart/compiler/backend/js/ast/SourceInfoAwareJsNode.java index 0c5cffa1e77..bfed21105a7 100644 --- a/src/com/google/dart/compiler/backend/js/ast/SourceInfoAwareJsNode.java +++ b/src/com/google/dart/compiler/backend/js/ast/SourceInfoAwareJsNode.java @@ -1,19 +1,25 @@ package com.google.dart.compiler.backend.js.ast; abstract class SourceInfoAwareJsNode extends AbstractNode { - private Object sourceInfo; + private Object source; @Override - public Object getSourceInfo() { - return sourceInfo; + public Object getSource() { + return source; } @Override - public void setSourceInfo(Object info) { - sourceInfo = info; + public void setSource(Object info) { + source = info; } @Override public void acceptChildren(JsVisitor visitor) { } + + @Override + public JsNode source(Object info) { + source = info; + return this; + } } \ No newline at end of file diff --git a/src/com/google/dart/compiler/common/HasSourceInfo.java b/src/com/google/dart/compiler/common/HasSourceInfo.java deleted file mode 100644 index 251f72b71f7..00000000000 --- a/src/com/google/dart/compiler/common/HasSourceInfo.java +++ /dev/null @@ -1,18 +0,0 @@ -// Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file -// for details. All rights reserved. Use of this source code is governed by a -// BSD-style license that can be found in the LICENSE file. - -package com.google.dart.compiler.common; - -public interface HasSourceInfo { - /** - * Return the source info associated with this object. - */ - Object getSourceInfo(); - - /** - * Set the source info associated with this object. - * @param info - */ - void setSourceInfo(Object info); -} diff --git a/src/com/google/dart/compiler/util/TextOutput.java b/src/com/google/dart/compiler/util/TextOutput.java index fdacbc4a284..9b6772551db 100644 --- a/src/com/google/dart/compiler/util/TextOutput.java +++ b/src/com/google/dart/compiler/util/TextOutput.java @@ -42,6 +42,8 @@ public interface TextOutput { void setOutListener(OutListener outListener); + void maybeIndent(); + public interface OutListener { void newLined(); diff --git a/src/com/google/dart/compiler/util/TextOutputImpl.java b/src/com/google/dart/compiler/util/TextOutputImpl.java index f1a85ee4240..2f1d041056f 100644 --- a/src/com/google/dart/compiler/util/TextOutputImpl.java +++ b/src/com/google/dart/compiler/util/TextOutputImpl.java @@ -146,7 +146,8 @@ public class TextOutputImpl implements TextOutput { } } - private void maybeIndent() { + @Override + public void maybeIndent() { if (justNewlined && !compact) { printAndCount(indents[identLevel]); justNewlined = false;