diff options
Diffstat (limited to 'src/jdk/nashorn/internal/ir/VarNode.java')
-rw-r--r-- | src/jdk/nashorn/internal/ir/VarNode.java | 80 |
1 files changed, 62 insertions, 18 deletions
diff --git a/src/jdk/nashorn/internal/ir/VarNode.java b/src/jdk/nashorn/internal/ir/VarNode.java index 67648a00..1cee8cb5 100644 --- a/src/jdk/nashorn/internal/ir/VarNode.java +++ b/src/jdk/nashorn/internal/ir/VarNode.java @@ -27,12 +27,15 @@ package jdk.nashorn.internal.ir; import jdk.nashorn.internal.ir.annotations.Immutable; import jdk.nashorn.internal.ir.visitor.NodeVisitor; +import jdk.nashorn.internal.parser.Token; /** * Node represents a var/let declaration. */ @Immutable public final class VarNode extends Statement implements Assignment<IdentNode> { + private static final long serialVersionUID = 1L; + /** Var name. */ private final IdentNode name; @@ -42,13 +45,16 @@ public final class VarNode extends Statement implements Assignment<IdentNode> { /** Is this a var statement (as opposed to a "var" in a for loop statement) */ private final int flags; - /** Flag that determines if this function node is a statement */ - public static final int IS_STATEMENT = 1 << 0; + /** Flag for ES6 LET declaration */ + public static final int IS_LET = 1 << 0; + + /** Flag for ES6 CONST declaration */ + public static final int IS_CONST = 1 << 1; /** Flag that determines if this is the last function declaration in a function * This is used to micro optimize the placement of return value assignments for * a program node */ - public static final int IS_LAST_FUNCTION_DECLARATION = 1 << 1; + public static final int IS_LAST_FUNCTION_DECLARATION = 1 << 2; /** * Constructor @@ -60,7 +66,7 @@ public final class VarNode extends Statement implements Assignment<IdentNode> { * @param init init node or null if just a declaration */ public VarNode(final int lineNumber, final long token, final int finish, final IdentNode name, final Expression init) { - this(lineNumber, token, finish, name, init, IS_STATEMENT); + this(lineNumber, token, finish, name, init, 0); } private VarNode(final VarNode varNode, final IdentNode name, final Expression init, final int flags) { @@ -99,7 +105,7 @@ public final class VarNode extends Statement implements Assignment<IdentNode> { } @Override - public VarNode setAssignmentDest(IdentNode n) { + public VarNode setAssignmentDest(final IdentNode n) { return setName(n); } @@ -109,6 +115,43 @@ public final class VarNode extends Statement implements Assignment<IdentNode> { } /** + * Is this a VAR node block scoped? This returns true for ECMAScript 6 LET and CONST nodes. + * @return true if an ES6 LET or CONST node + */ + public boolean isBlockScoped() { + return getFlag(IS_LET) || getFlag(IS_CONST); + } + + /** + * Is this an ECMAScript 6 LET node? + * @return true if LET node + */ + public boolean isLet() { + return getFlag(IS_LET); + } + + /** + * Is this an ECMAScript 6 CONST node? + * @return true if CONST node + */ + public boolean isConst() { + return getFlag(IS_CONST); + } + + /** + * Return the flags to use for symbols for this declaration. + * @return the symbol flags + */ + public int getSymbolFlags() { + if (isLet()) { + return Symbol.IS_VAR | Symbol.IS_LET; + } else if (isConst()) { + return Symbol.IS_VAR | Symbol.IS_CONST; + } + return Symbol.IS_VAR; + } + + /** * Does this variable declaration have an init value * @return true if an init exists, false otherwise */ @@ -123,8 +166,9 @@ public final class VarNode extends Statement implements Assignment<IdentNode> { @Override public Node accept(final NodeVisitor<? extends LexicalContext> visitor) { if (visitor.enterVarNode(this)) { - final IdentNode newName = (IdentNode)name.accept(visitor); + // var is right associative, so visit init before name final Expression newInit = init == null ? null : (Expression)init.accept(visitor); + final IdentNode newName = (IdentNode)name.accept(visitor); final VarNode newThis; if (name != newName || init != newInit) { newThis = new VarNode(this, newName, newInit, flags); @@ -137,13 +181,13 @@ public final class VarNode extends Statement implements Assignment<IdentNode> { } @Override - public void toString(final StringBuilder sb) { - sb.append("var "); - name.toString(sb); + public void toString(final StringBuilder sb, final boolean printType) { + sb.append(Token.descType(getToken()).getName()).append(' '); + name.toString(sb, printType); if (init != null) { sb.append(" = "); - init.toString(sb); + init.toString(sb, printType); } } @@ -213,18 +257,18 @@ public final class VarNode extends Statement implements Assignment<IdentNode> { } /** - * Returns true if this is a var statement (as opposed to a var initializer in a for loop). - * @return true if this is a var statement (as opposed to a var initializer in a for loop). - */ - public boolean isStatement() { - return (flags & IS_STATEMENT) != 0; - } - - /** * Returns true if this is a function declaration. * @return true if this is a function declaration. */ public boolean isFunctionDeclaration() { return init instanceof FunctionNode && ((FunctionNode)init).isDeclared(); } + + /** + * Returns true if this is an anonymous function declaration. + * @return true if this is an anonymous function declaration. + */ + public boolean isAnonymousFunctionDeclaration() { + return isFunctionDeclaration() && ((FunctionNode)init).isAnonymous(); + } } |