JS backend: added deepCopy() method to JS AST nodes

This commit is contained in:
Alexey Tsvetkov
2014-05-11 20:47:08 +04:00
committed by Zalim Bashorov
parent b880775de5
commit f076ab8f54
47 changed files with 509 additions and 1 deletions
@@ -1,4 +1,7 @@
package com.google.dart.compiler.backend.js.ast;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.NotNull;
@@ -56,4 +59,10 @@ public class ChameleonJsExpression implements JsExpression {
public void traverse(JsVisitorWithContext visitor, JsContext ctx) {
expression.traverse(visitor, ctx);
}
@NotNull
@Override
public ChameleonJsExpression deepCopy() {
return new ChameleonJsExpression(AstUtil.deepCopy(expression));
}
}
@@ -4,6 +4,9 @@
package com.google.dart.compiler.backend.js.ast;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
/**
* Represents a javascript expression for array access.
*/
@@ -55,4 +58,13 @@ public final class JsArrayAccess extends JsExpressionImpl {
}
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsArrayAccess deepCopy() {
JsExpression arrayCopy = AstUtil.deepCopy(arrayExpression);
JsExpression indexCopy = AstUtil.deepCopy(indexExpression);
return new JsArrayAccess(arrayCopy, indexCopy);
}
}
@@ -4,7 +4,9 @@
package com.google.dart.compiler.backend.js.ast;
import com.google.dart.compiler.util.AstUtil;
import com.intellij.util.SmartList;
import org.jetbrains.annotations.NotNull;
import java.util.List;
@@ -43,4 +45,10 @@ public final class JsArrayLiteral extends JsLiteral {
}
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsArrayLiteral deepCopy() {
return new JsArrayLiteral(AstUtil.deepCopy(expressions));
}
}
@@ -4,6 +4,8 @@
package com.google.dart.compiler.backend.js.ast;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public final class JsBinaryOperation extends JsExpressionImpl {
@@ -29,6 +31,14 @@ public final class JsBinaryOperation extends JsExpressionImpl {
return arg2;
}
public void setArg1(JsExpression arg1) {
this.arg1 = arg1;
}
public void setArg2(JsExpression arg2) {
this.arg2 = arg2;
}
public JsBinaryOperator getOperator() {
return op;
}
@@ -61,4 +71,10 @@ public final class JsBinaryOperation extends JsExpressionImpl {
}
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsExpression deepCopy() {
return new JsBinaryOperation(op, AstUtil.deepCopy(arg1), AstUtil.deepCopy(arg2));
}
}
@@ -4,6 +4,7 @@
package com.google.dart.compiler.backend.js.ast;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
import com.intellij.util.SmartList;
@@ -63,4 +64,10 @@ public class JsBlock extends SourceInfoAwareJsNode implements JsStatement {
}
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsBlock deepCopy() {
return new JsBlock(AstUtil.deepCopy(statements));
}
}
@@ -4,6 +4,9 @@
package com.google.dart.compiler.backend.js.ast;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
/**
* Represents the JavaScript break statement.
*/
@@ -31,4 +34,10 @@ public final class JsBreak extends JsContinue {
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsBreak deepCopy() {
return new JsBreak(AstUtil.deepCopy(label));
}
}
@@ -4,6 +4,9 @@
package com.google.dart.compiler.backend.js.ast;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
/**
* Represents the JavaScript case statement.
*/
@@ -41,4 +44,14 @@ public final class JsCase extends JsSwitchMember {
}
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsCase deepCopy() {
JsCase caseCopy = new JsCase();
caseCopy.caseExpression = AstUtil.deepCopy(caseExpression);
caseCopy.statements.addAll(AstUtil.deepCopy(statements));
return caseCopy;
}
}
@@ -4,6 +4,9 @@
package com.google.dart.compiler.backend.js.ast;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
/**
* Represents a JavaScript catch clause.
*/
@@ -72,4 +75,22 @@ public class JsCatch extends SourceInfoAwareJsNode implements HasCondition {
}
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsCatch deepCopy() {
JsCatchScope scopeCopy = scope.copy();
JsBlock bodyCopy = AstUtil.deepCopy(body);
JsExpression conditionCopy = AstUtil.deepCopy(condition);
JsParameter paramCopy = AstUtil.deepCopy(param);
return new JsCatch(scopeCopy, bodyCopy, conditionCopy, paramCopy);
}
private JsCatch(JsCatchScope scope, JsBlock body, JsExpression condition, JsParameter param) {
this.scope = scope;
this.body = body;
this.condition = condition;
this.param = param;
}
}
@@ -30,6 +30,11 @@ public class JsCatchScope extends JsScope {
return findOwnName(name) != null;
}
@NotNull
public JsCatchScope copy() {
return new JsCatchScope(getParent(), name.getIdent());
}
@Override
protected JsName findOwnName(@NotNull String ident) {
return name.getIdent().equals(ident) ? name : null;
@@ -4,6 +4,9 @@
package com.google.dart.compiler.backend.js.ast;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
public final class JsConditional extends JsExpressionImpl {
private JsExpression testExpression;
private JsExpression elseExpression;
@@ -63,4 +66,14 @@ public final class JsConditional extends JsExpressionImpl {
}
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsConditional deepCopy() {
JsExpression testCopy = AstUtil.deepCopy(testExpression);
JsExpression thenCopy = AstUtil.deepCopy(thenExpression);
JsExpression elseCopy = AstUtil.deepCopy(elseExpression);
return new JsConditional(testCopy, thenCopy, elseCopy);
}
}
@@ -4,6 +4,8 @@
package com.google.dart.compiler.backend.js.ast;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class JsContinue extends SourceInfoAwareJsNode implements JsStatement {
@@ -44,4 +46,12 @@ public class JsContinue extends SourceInfoAwareJsNode implements JsStatement {
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsContinue deepCopy() {
if (label == null) return new JsContinue();
return new JsContinue(AstUtil.deepCopy(label));
}
}
@@ -4,6 +4,8 @@
package com.google.dart.compiler.backend.js.ast;
import org.jetbrains.annotations.NotNull;
/**
* Represents a JavaScript debugger statement.
*/
@@ -26,4 +28,10 @@ public class JsDebugger extends SourceInfoAwareJsNode implements JsStatement {
v.visit(this, ctx);
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsDebugger deepCopy() {
return this;
}
}
@@ -4,6 +4,9 @@
package com.google.dart.compiler.backend.js.ast;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
/**
* Represents the default option in a JavaScript swtich statement.
*/
@@ -20,4 +23,12 @@ public final class JsDefault extends JsSwitchMember {
}
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsDefault deepCopy() {
JsDefault defaultCopy = new JsDefault();
defaultCopy.statements.addAll(AstUtil.deepCopy(statements));
return defaultCopy;
}
}
@@ -4,6 +4,9 @@
package com.google.dart.compiler.backend.js.ast;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
/**
* Represents a JavaScript do..while statement.
*/
@@ -28,4 +31,13 @@ public class JsDoWhile extends JsWhile {
}
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsDoWhile deepCopy() {
JsExpression conditionCopy = AstUtil.deepCopy(condition);
JsStatement bodyCopy = AstUtil.deepCopy(body);
return new JsDoWhile(conditionCopy, bodyCopy);
}
}
@@ -1,5 +1,7 @@
package com.google.dart.compiler.backend.js.ast;
import org.jetbrains.annotations.NotNull;
import java.util.Collections;
import java.util.Map;
@@ -30,4 +32,10 @@ public class JsDocComment extends JsExpressionImpl {
@Override
public void traverse(JsVisitorWithContext v, JsContext ctx) {
}
@NotNull
@Override
public JsDocComment deepCopy() {
return new JsDocComment(tags);
}
}
@@ -4,6 +4,8 @@
package com.google.dart.compiler.backend.js.ast;
import org.jetbrains.annotations.NotNull;
public class JsEmpty extends SourceInfoAwareJsNode implements JsStatement {
JsEmpty() {
}
@@ -18,4 +20,10 @@ public class JsEmpty extends SourceInfoAwareJsNode implements JsStatement {
v.visit(this, ctx);
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsEmpty deepCopy() {
return this;
}
}
@@ -38,4 +38,10 @@ public class JsEmptyExpression extends JsExpressionImpl {
public void traverse(JsVisitorWithContext visitor, JsContext ctx) {
throw new IllegalArgumentException("empty expression should not be here during generating Javascript code");
}
@NotNull
@Override
public JsEmptyExpression deepCopy() {
return new JsEmptyExpression();
}
}
@@ -10,4 +10,8 @@ public interface JsExpression extends JsNode {
@Override
JsExpression source(Object info);
@NotNull
@Override
JsExpression deepCopy();
}
@@ -4,6 +4,8 @@
package com.google.dart.compiler.backend.js.ast;
import org.jetbrains.annotations.NotNull;
public final class JsExpressionStatement extends AbstractNode implements JsStatement {
@NotNull
private JsExpression expression;
@@ -49,4 +51,10 @@ public final class JsExpressionStatement extends AbstractNode implements JsState
}
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsExpressionStatement deepCopy() {
return new JsExpressionStatement(expression.deepCopy());
}
}
@@ -4,6 +4,9 @@
package com.google.dart.compiler.backend.js.ast;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
/**
* A <code>for</code> statement. If specified at all, the initializer part is
* either a declaration of one or more variables, in which case
@@ -118,4 +121,24 @@ public class JsFor extends SourceInfoAwareJsNode implements JsStatement {
}
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsFor deepCopy() {
JsStatement bodyCopy = AstUtil.deepCopy(body);
JsExpression conditionCopy = AstUtil.deepCopy(condition);
JsExpression incrementalExprCopy = AstUtil.deepCopy(incrementExpression);
if (initVars != null) {
return new JsFor(initVars.deepCopy(),
conditionCopy,
incrementalExprCopy,
bodyCopy);
} else {
return new JsFor(initExpression.deepCopy(),
conditionCopy,
incrementExpression,
bodyCopy);
}
}
}
@@ -4,6 +4,9 @@
package com.google.dart.compiler.backend.js.ast;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
public class JsForIn extends SourceInfoAwareJsNode implements JsStatement {
private JsStatement body;
private JsExpression iterExpression;
@@ -20,6 +23,14 @@ public class JsForIn extends SourceInfoAwareJsNode implements JsStatement {
this.iterVarName = iterVarName;
}
public JsForIn(JsName iterVarName, JsExpression iterExpression, JsExpression objectExpression, JsStatement body) {
this.iterVarName = iterVarName;
this.iterExpression = iterExpression;
this.objectExpression = objectExpression;
this.body = body;
}
public JsStatement getBody() {
return body;
}
@@ -73,4 +84,14 @@ public class JsForIn extends SourceInfoAwareJsNode implements JsStatement {
}
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsForIn deepCopy() {
JsStatement bodyCopy = AstUtil.deepCopy(body);
JsExpression iterCopy = AstUtil.deepCopy(iterExpression);
JsExpression objectCopy = AstUtil.deepCopy(objectExpression);
return new JsForIn(iterVarName, iterCopy, objectCopy, bodyCopy);
}
}
@@ -5,6 +5,7 @@
package com.google.dart.compiler.backend.js.ast;
import com.google.dart.compiler.common.Symbol;
import com.google.dart.compiler.util.AstUtil;
import com.intellij.util.SmartList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
@@ -88,4 +89,15 @@ public final class JsFunction extends JsLiteral implements HasName {
}
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsFunction deepCopy() {
JsFunction functionCopy = new JsFunction(scope.getParent(), scope.getDescription(), name);
functionCopy.getScope().copyOwnNames(scope);
functionCopy.setBody(body.deepCopy());
functionCopy.params = AstUtil.deepCopy(params);
return functionCopy;
}
}
@@ -4,6 +4,11 @@
package com.google.dart.compiler.backend.js.ast;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
import java.util.List;
/**
* Represents a JavaScript block in the global scope.
*/
@@ -16,4 +21,13 @@ public class JsGlobalBlock extends JsBlock {
public boolean isGlobalBlock() {
return true;
}
@NotNull
@Override
public JsGlobalBlock deepCopy() {
JsGlobalBlock globalBlockCopy = new JsGlobalBlock();
List<JsStatement> statementscopy = AstUtil.deepCopy(getStatements());
globalBlockCopy.getStatements().addAll(statementscopy);
return globalBlockCopy;
}
}
@@ -4,6 +4,9 @@
package com.google.dart.compiler.backend.js.ast;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
/**
* Represents a JavaScript if statement.
*/
@@ -75,4 +78,14 @@ public final class JsIf extends SourceInfoAwareJsNode implements JsStatement {
}
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsIf deepCopy() {
JsExpression ifCopy = AstUtil.deepCopy(ifExpression);
JsStatement thenCopy = AstUtil.deepCopy(thenStatement);
JsStatement elseCopy = AstUtil.deepCopy(elseStatement);
return new JsIf(ifCopy, thenCopy, elseCopy);
}
}
@@ -4,6 +4,7 @@
package com.google.dart.compiler.backend.js.ast;
import com.google.dart.compiler.util.AstUtil;
import com.intellij.util.SmartList;
import org.jetbrains.annotations.NotNull;
@@ -68,4 +69,14 @@ public final class JsInvocation extends JsExpressionImpl.JsExpressionHasArgument
}
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsInvocation deepCopy() {
JsExpression qualifierCopy = AstUtil.deepCopy(qualifier);
List<JsExpression> argumentsCopy = AstUtil.deepCopy(arguments);
JsInvocation copy = new JsInvocation(qualifierCopy, argumentsCopy);
copy.setInlineStatus(getInlineStatus());
return copy;
}
}
@@ -5,6 +5,8 @@
package com.google.dart.compiler.backend.js.ast;
import com.google.dart.compiler.common.Symbol;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
/**
* Represents a JavaScript label statement.
@@ -58,4 +60,10 @@ public class JsLabel extends SourceInfoAwareJsNode implements JsStatement, HasNa
}
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsLabel deepCopy() {
return new JsLabel(label, AstUtil.deepCopy(statement.deepCopy()));
}
}
@@ -4,6 +4,8 @@
package com.google.dart.compiler.backend.js.ast;
import org.jetbrains.annotations.NotNull;
public abstract class JsLiteral extends JsExpressionImpl {
public static final JsValueLiteral THIS = new JsThisRef();
public static final JsNameRef UNDEFINED = new JsNameRef("undefined");
@@ -69,5 +71,11 @@ public abstract class JsLiteral extends JsExpressionImpl {
public final boolean isLeaf() {
return true;
}
@NotNull
@Override
public JsExpression deepCopy() {
return this;
}
}
}
@@ -5,6 +5,8 @@
package com.google.dart.compiler.backend.js.ast;
import com.google.dart.compiler.common.Symbol;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
/**
* Represents a JavaScript expression that references a name.
@@ -89,4 +91,14 @@ public final class JsNameRef extends JsExpressionImpl implements HasName {
}
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsNameRef deepCopy() {
JsExpression qualifierCopy = AstUtil.deepCopy(qualifier);
if (name != null) return new JsNameRef(name, qualifierCopy);
return new JsNameRef(ident, qualifierCopy);
}
}
@@ -4,7 +4,9 @@
package com.google.dart.compiler.backend.js.ast;
import com.google.dart.compiler.util.AstUtil;
import com.intellij.util.SmartList;
import org.jetbrains.annotations.NotNull;
import java.util.List;
@@ -43,4 +45,12 @@ public final class JsNew extends JsExpressionImpl.JsExpressionHasArguments {
}
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsNew deepCopy() {
JsExpression constructorCopy = AstUtil.deepCopy(constructorExpression);
List<JsExpression> argumentsCopy = AstUtil.deepCopy(arguments);
return new JsNew(constructorCopy, argumentsCopy);
}
}
@@ -4,6 +4,8 @@
package com.google.dart.compiler.backend.js.ast;
import org.jetbrains.annotations.NotNull;
public interface JsNode {
/**
* Causes this object to have the visitor visit itself and its children.
@@ -28,6 +30,9 @@ public interface JsNode {
JsNode source(Object info);
@NotNull
JsNode deepCopy();
/**
* Causes this object to have the visitor visit itself and its children.
*
@@ -4,7 +4,9 @@
package com.google.dart.compiler.backend.js.ast;
import com.google.dart.compiler.util.AstUtil;
import com.intellij.util.SmartList;
import org.jetbrains.annotations.NotNull;
import java.util.List;
@@ -55,4 +57,10 @@ public final class JsObjectLiteral extends JsLiteral {
}
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsObjectLiteral deepCopy() {
return new JsObjectLiteral(AstUtil.deepCopy(properties), multiline);
}
}
@@ -40,4 +40,12 @@ public final class JsParameter extends SourceInfoAwareJsNode implements HasName
v.visit(this, ctx);
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsParameter deepCopy() {
JsParameter parameter = new JsParameter(name);
parameter.setHasDefaultValue(hasDefaultValue);
return parameter;
}
}
@@ -4,6 +4,9 @@
package com.google.dart.compiler.backend.js.ast;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
public final class JsPostfixOperation extends JsUnaryOperation {
public JsPostfixOperation(JsUnaryOperator op) {
this(op, null);
@@ -25,4 +28,10 @@ public final class JsPostfixOperation extends JsUnaryOperation {
}
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsPostfixOperation deepCopy() {
return new JsPostfixOperation(getOperator(), AstUtil.deepCopy(getArg()));
}
}
@@ -4,6 +4,9 @@
package com.google.dart.compiler.backend.js.ast;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
public final class JsPrefixOperation extends JsUnaryOperation {
public JsPrefixOperation(JsUnaryOperator op) {
this(op, null);
@@ -25,4 +28,10 @@ public final class JsPrefixOperation extends JsUnaryOperation {
}
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsPrefixOperation deepCopy() {
return new JsPrefixOperation(getOperator(), AstUtil.deepCopy(getArg()));
}
}
@@ -8,6 +8,7 @@ import gnu.trove.TDoubleObjectHashMap;
import gnu.trove.THashMap;
import gnu.trove.TIntObjectHashMap;
import org.jetbrains.annotations.NotNull;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
import java.util.Map;
@@ -139,4 +140,10 @@ public final class JsProgram extends SourceInfoAwareJsNode {
}
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsProgram deepCopy() {
throw new NotImplementedException();
}
}
@@ -4,6 +4,9 @@
package com.google.dart.compiler.backend.js.ast;
import org.jetbrains.annotations.NotNull;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
/**
* One independently loadable fragment of a {@link JsProgram}.
*/
@@ -35,4 +38,10 @@ public class JsProgramFragment extends SourceInfoAwareJsNode {
}
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsProgramFragment deepCopy() {
throw new NotImplementedException();
}
}
@@ -53,4 +53,10 @@ public class JsPropertyInitializer extends SourceInfoAwareJsNode {
}
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsPropertyInitializer deepCopy() {
return new JsPropertyInitializer(labelExpr.deepCopy(), valueExpr.deepCopy());
}
}
@@ -4,6 +4,9 @@
package com.google.dart.compiler.backend.js.ast;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
/**
* A JavaScript return statement.
*/
@@ -46,4 +49,10 @@ public final class JsReturn extends SourceInfoAwareJsNode implements JsStatement
}
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsReturn deepCopy() {
return new JsReturn(AstUtil.deepCopy(expression));
}
}
@@ -9,6 +9,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import static com.google.dart.compiler.backend.js.ast.AstPackage.JsObjectScope;
@@ -154,6 +155,16 @@ public abstract class JsScope {
}
}
public void copyOwnNames(JsScope other) {
names = new HashMap<String, JsName>(names);
names.putAll(other.names);
}
@NotNull
public String getDescription() {
return description;
}
@NotNull
protected JsName doCreateName(String ident) {
JsName name = new JsName(this, ident);
@@ -4,5 +4,10 @@
package com.google.dart.compiler.backend.js.ast;
import org.jetbrains.annotations.NotNull;
public interface JsStatement extends JsNode {
@NotNull
@Override
JsStatement deepCopy();
}
@@ -4,6 +4,9 @@
package com.google.dart.compiler.backend.js.ast;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
import java.util.List;
@@ -12,11 +15,17 @@ import java.util.List;
*/
public class JsSwitch extends SourceInfoAwareJsNode implements JsStatement {
private final List<JsSwitchMember> cases = new ArrayList<JsSwitchMember>();
private final List<JsSwitchMember> cases;
private JsExpression expression;
public JsSwitch() {
super();
cases = new ArrayList<JsSwitchMember>();
}
public JsSwitch(JsExpression expression, List<JsSwitchMember> cases) {
this.expression = expression;
this.cases = cases;
}
public List<JsSwitchMember> getCases() {
@@ -50,4 +59,13 @@ public class JsSwitch extends SourceInfoAwareJsNode implements JsStatement {
}
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsSwitch deepCopy() {
JsExpression expressionCopy = AstUtil.deepCopy(expression);
List<JsSwitchMember> casesCopy = AstUtil.deepCopy(cases);
return new JsSwitch(expressionCopy, casesCopy);
}
}
@@ -5,6 +5,7 @@
package com.google.dart.compiler.backend.js.ast;
import com.intellij.util.SmartList;
import org.jetbrains.annotations.NotNull;
import java.util.List;
@@ -26,4 +27,8 @@ public abstract class JsSwitchMember extends SourceInfoAwareJsNode {
public void acceptChildren(JsVisitor visitor) {
visitor.acceptWithInsertRemove(statements);
}
@NotNull
@Override
public abstract JsSwitchMember deepCopy();
}
@@ -4,6 +4,9 @@
package com.google.dart.compiler.backend.js.ast;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
public class JsThrow extends SourceInfoAwareJsNode implements JsStatement {
private JsExpression expression;
@@ -39,4 +42,10 @@ public class JsThrow extends SourceInfoAwareJsNode implements JsStatement {
}
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsThrow deepCopy() {
return new JsThrow(AstUtil.deepCopy(expression));
}
}
@@ -4,7 +4,9 @@
package com.google.dart.compiler.backend.js.ast;
import com.google.dart.compiler.util.AstUtil;
import com.intellij.util.SmartList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.List;
@@ -72,4 +74,14 @@ public class JsTry extends SourceInfoAwareJsNode implements JsStatement {
}
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsTry deepCopy() {
JsBlock tryCopy = AstUtil.deepCopy(tryBlock);
List<JsCatch> catchCopy = AstUtil.deepCopy(catches);
JsBlock finallyCopy = AstUtil.deepCopy(finallyBlock);
return new JsTry(tryCopy, catchCopy, finallyCopy);
}
}
@@ -5,7 +5,9 @@
package com.google.dart.compiler.backend.js.ast;
import com.google.dart.compiler.common.Symbol;
import com.google.dart.compiler.util.AstUtil;
import com.intellij.util.SmartList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.*;
@@ -98,6 +100,14 @@ public class JsVars extends SourceInfoAwareJsNode implements JsStatement, Iterab
}
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsVar deepCopy() {
if (initExpression == null) return new JsVar(name);
return new JsVar(name, initExpression.deepCopy());
}
}
public void add(JsVar var) {
@@ -127,6 +137,10 @@ public class JsVars extends SourceInfoAwareJsNode implements JsStatement, Iterab
return vars.iterator();
}
public List<JsVar> getVars() {
return vars;
}
@Override
public void accept(JsVisitor v) {
v.visitVars(this);
@@ -144,4 +158,10 @@ public class JsVars extends SourceInfoAwareJsNode implements JsStatement, Iterab
}
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsVars deepCopy() {
return new JsVars(AstUtil.deepCopy(vars), multiline);
}
}
@@ -4,6 +4,9 @@
package com.google.dart.compiler.backend.js.ast;
import com.google.dart.compiler.util.AstUtil;
import org.jetbrains.annotations.NotNull;
/**
* A JavaScript <code>while</code> statement.
*/
@@ -54,4 +57,13 @@ public class JsWhile extends SourceInfoAwareJsNode implements JsStatement {
}
v.endVisit(this, ctx);
}
@NotNull
@Override
public JsWhile deepCopy() {
JsExpression conditionCopy = AstUtil.deepCopy(condition);
JsStatement bodyCopy = AstUtil.deepCopy(body);
return new JsWhile(conditionCopy, bodyCopy);
}
}
@@ -5,6 +5,12 @@
package com.google.dart.compiler.util;
import com.google.dart.compiler.backend.js.ast.*;
import com.intellij.util.SmartList;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.util.ArrayList;
import java.util.List;
public final class AstUtil {
private AstUtil() {
@@ -26,4 +32,24 @@ public final class AstUtil {
}
return (JsBinaryOperation) result;
}
@Nullable
public static <T extends JsNode> T deepCopy(@Nullable T node) {
if (node == null) return null;
//noinspection unchecked
return (T) node.deepCopy();
}
@NotNull
public static <T extends JsNode> List<T> deepCopy(@Nullable List<T> nodes) {
if (nodes == null) return new SmartList<T>();
List<T> nodesCopy = new ArrayList<T>(nodes.size());
for (T node : nodes) {
nodesCopy.add(deepCopy(node));
}
return nodesCopy;
}
}