# HG changeset patch # User attila # Date 1430920014 -7200 # Node ID dc4a419b298218419ff9411453a8b5f78eaf70c2 # Parent 62dcad329b269a7457ad77e30e7fc90e0d3b4cd9 8079362: Enforce best practices for Node token API usage Reviewed-by: hannesw, sundar diff -r 62dcad329b26 -r dc4a419b2982 nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api/tree/IRTranslator.java --- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api/tree/IRTranslator.java Wed May 06 13:36:42 2015 +0200 +++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api/tree/IRTranslator.java Wed May 06 15:46:54 2015 +0200 @@ -113,7 +113,7 @@ final ExpressionTree srcTree = translateExpr(binaryNode.getAssignmentSource()); final ExpressionTree destTree = translateExpr(binaryNode.getAssignmentDest()); - if (binaryNode.tokenType() == TokenType.ASSIGN) { + if (binaryNode.isTokenType(TokenType.ASSIGN)) { curExpr = new AssignmentTreeImpl(binaryNode, destTree, srcTree); } else { curExpr = new CompoundAssignmentTreeImpl(binaryNode, destTree, srcTree); @@ -122,7 +122,7 @@ final ExpressionTree leftTree = translateExpr(binaryNode.lhs()); final ExpressionTree rightTree = translateExpr(binaryNode.rhs()); - if (binaryNode.tokenType() == TokenType.INSTANCEOF) { + if (binaryNode.isTokenType(TokenType.INSTANCEOF)) { curExpr = new InstanceOfTreeImpl(binaryNode, leftTree, rightTree); } else { curExpr = new BinaryTreeImpl(binaryNode, leftTree, rightTree); @@ -385,7 +385,7 @@ @Override public boolean enterUnaryNode(final UnaryNode unaryNode) { - if (unaryNode.tokenType() == TokenType.NEW) { + if (unaryNode.isTokenType(TokenType.NEW)) { curExpr = new NewTreeImpl(unaryNode, translateExpr(unaryNode.getExpression())); } else { diff -r 62dcad329b26 -r dc4a419b2982 nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api/tree/InstanceOfTreeImpl.java --- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api/tree/InstanceOfTreeImpl.java Wed May 06 13:36:42 2015 +0200 +++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api/tree/InstanceOfTreeImpl.java Wed May 06 15:46:54 2015 +0200 @@ -33,7 +33,7 @@ final ExpressionTree expr, final ExpressionTree type) { super(node, expr, type); - assert node.tokenType() == TokenType.INSTANCEOF : "instanceof expected"; + assert node.isTokenType(TokenType.INSTANCEOF) : "instanceof expected"; } @Override diff -r 62dcad329b26 -r dc4a419b2982 nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api/tree/NewTreeImpl.java --- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api/tree/NewTreeImpl.java Wed May 06 13:36:42 2015 +0200 +++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/api/tree/NewTreeImpl.java Wed May 06 15:46:54 2015 +0200 @@ -32,7 +32,7 @@ NewTreeImpl(final UnaryNode node, final ExpressionTree constrExpr) { super(node); - assert (node.tokenType() == TokenType.NEW) : "new expected"; + assert (node.isTokenType(TokenType.NEW)) : "new expected"; this.constrExpr = constrExpr; } diff -r 62dcad329b26 -r dc4a419b2982 nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/AssignSymbols.java --- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/AssignSymbols.java Wed May 06 13:36:42 2015 +0200 +++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/AssignSymbols.java Wed May 06 15:46:54 2015 +0200 @@ -84,6 +84,7 @@ import jdk.nashorn.internal.ir.VarNode; import jdk.nashorn.internal.ir.WithNode; import jdk.nashorn.internal.ir.visitor.NodeVisitor; +import jdk.nashorn.internal.parser.TokenType; import jdk.nashorn.internal.runtime.Context; import jdk.nashorn.internal.runtime.ECMAErrors; import jdk.nashorn.internal.runtime.ErrorManager; @@ -714,12 +715,10 @@ @Override public Node leaveBinaryNode(final BinaryNode binaryNode) { - switch (binaryNode.tokenType()) { - case ASSIGN: + if (binaryNode.isTokenType(TokenType.ASSIGN)) { return leaveASSIGN(binaryNode); - default: - return super.leaveBinaryNode(binaryNode); } + return super.leaveBinaryNode(binaryNode); } private Node leaveASSIGN(final BinaryNode binaryNode) { diff -r 62dcad329b26 -r dc4a419b2982 nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/LocalVariableTypesCalculator.java --- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/LocalVariableTypesCalculator.java Wed May 06 13:36:42 2015 +0200 +++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/LocalVariableTypesCalculator.java Wed May 06 15:46:54 2015 +0200 @@ -459,7 +459,7 @@ // NOTE: regardless of operator's lexical associativity, lhs is always evaluated first. final Expression lhs = binaryNode.lhs(); final LvarType lhsType; - if (!(lhs instanceof IdentNode && binaryNode.tokenType() == TokenType.ASSIGN)) { + if (!(lhs instanceof IdentNode && binaryNode.isTokenType(TokenType.ASSIGN))) { lhsType = visitExpression(lhs); } else { // Can't visit IdentNode on LHS of a simple assignment, as visits imply use, and this is def. diff -r 62dcad329b26 -r dc4a419b2982 nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/Lower.java --- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/Lower.java Wed May 06 13:36:42 2015 +0200 +++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/Lower.java Wed May 06 15:46:54 2015 +0200 @@ -45,8 +45,8 @@ import jdk.nashorn.internal.ir.CallNode; import jdk.nashorn.internal.ir.CaseNode; import jdk.nashorn.internal.ir.CatchNode; +import jdk.nashorn.internal.ir.ContinueNode; import jdk.nashorn.internal.ir.DebuggerNode; -import jdk.nashorn.internal.ir.ContinueNode; import jdk.nashorn.internal.ir.EmptyNode; import jdk.nashorn.internal.ir.Expression; import jdk.nashorn.internal.ir.ExpressionStatement; @@ -210,7 +210,7 @@ final String name = getConstantPropertyName(indexNode.getIndex()); if (name != null) { // If index node is a constant property name convert index node to access node. - assert Token.descType(indexNode.getToken()) == TokenType.LBRACKET; + assert indexNode.isIndex(); return new AccessNode(indexNode.getToken(), indexNode.getFinish(), indexNode.getBase(), name); } return super.leaveIndexNode(indexNode); diff -r 62dcad329b26 -r dc4a419b2982 nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/ir/AccessNode.java --- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/ir/AccessNode.java Wed May 06 13:36:42 2015 +0200 +++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/ir/AccessNode.java Wed May 06 15:46:54 2015 +0200 @@ -28,8 +28,6 @@ import jdk.nashorn.internal.codegen.types.Type; import jdk.nashorn.internal.ir.annotations.Immutable; import jdk.nashorn.internal.ir.visitor.NodeVisitor; -import jdk.nashorn.internal.parser.Token; -import jdk.nashorn.internal.parser.TokenType; /** * IR representation of a property access (period operator.) @@ -103,14 +101,6 @@ return property; } - /** - * Return true if this node represents an index operation normally represented as {@link IndexNode}. - * @return true if an index access. - */ - public boolean isIndex() { - return Token.descType(getToken()) == TokenType.LBRACKET; - } - private AccessNode setBase(final Expression base) { if (this.base == base) { return this; diff -r 62dcad329b26 -r dc4a419b2982 nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/ir/BaseNode.java --- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/ir/BaseNode.java Wed May 06 13:36:42 2015 +0200 +++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/ir/BaseNode.java Wed May 06 15:46:54 2015 +0200 @@ -29,6 +29,7 @@ import jdk.nashorn.internal.codegen.types.Type; import jdk.nashorn.internal.ir.annotations.Immutable; +import jdk.nashorn.internal.parser.TokenType; /** * IR base for accessing/indexing nodes. @@ -122,6 +123,14 @@ } /** + * Return true if this node represents an index operation normally represented as {@link IndexNode}. + * @return true if an index access. + */ + public boolean isIndex() { + return isTokenType(TokenType.LBRACKET); + } + + /** * Mark this node as being the callee operand of a {@link CallNode}. * @return a base node identical to this one in all aspects except with its function flag set. */ diff -r 62dcad329b26 -r dc4a419b2982 nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/ir/BinaryNode.java --- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/ir/BinaryNode.java Wed May 06 13:36:42 2015 +0200 +++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/ir/BinaryNode.java Wed May 06 15:46:54 2015 +0200 @@ -312,7 +312,7 @@ @Override public boolean isSelfModifying() { - return isAssignment() && tokenType() != TokenType.ASSIGN; + return isAssignment() && !isTokenType(TokenType.ASSIGN); } @Override @@ -529,7 +529,7 @@ final TokenType tokenType = tokenType(); if(tokenType == TokenType.ADD || tokenType == TokenType.ASSIGN_ADD) { return OPTIMISTIC_UNDECIDED_TYPE; - } else if (CAN_OVERFLOW.contains(tokenType())) { + } else if (CAN_OVERFLOW.contains(tokenType)) { return Type.INT; } return getMostPessimisticType(); diff -r 62dcad329b26 -r dc4a419b2982 nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/ir/Node.java --- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/ir/Node.java Wed May 06 13:36:42 2015 +0200 +++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/ir/Node.java Wed May 06 15:46:54 2015 +0200 @@ -235,26 +235,28 @@ } /** - * Return token tokenType from a token descriptor. + * Returns this node's token's type. If you want to check for the node having a specific token type, + * consider using {@link #isTokenType(TokenType)} instead. * - * @return Type of token. + * @return type of token. */ public TokenType tokenType() { return Token.descType(token); } /** - * Test token tokenType. + * Tests if this node has the specific token type. * - * @param type a type to check this token against + * @param type a token type to check this node's token type against * @return true if token types match. */ public boolean isTokenType(final TokenType type) { - return Token.descType(token) == type; + return tokenType() == type; } /** - * Get the token for this location + * Get the token for this node. If you want to retrieve the token's type, consider using + * {@link #tokenType()} or {@link #isTokenType(TokenType)} instead. * @return the token */ public long getToken() { diff -r 62dcad329b26 -r dc4a419b2982 nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/ir/VarNode.java --- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/ir/VarNode.java Wed May 06 13:36:42 2015 +0200 +++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/ir/VarNode.java Wed May 06 15:46:54 2015 +0200 @@ -27,7 +27,6 @@ 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. @@ -210,7 +209,7 @@ @Override public void toString(final StringBuilder sb, final boolean printType) { - sb.append(Token.descType(getToken()).getName()).append(' '); + sb.append(tokenType().getName()).append(' '); name.toString(sb, printType); if (init != null) { diff -r 62dcad329b26 -r dc4a419b2982 nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/parser/Parser.java --- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/parser/Parser.java Wed May 06 13:36:42 2015 +0200 +++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/parser/Parser.java Wed May 06 15:46:54 2015 +0200 @@ -607,7 +607,7 @@ * @return whether the ident can be used as L-value */ private static boolean checkIdentLValue(final IdentNode ident) { - return Token.descType(ident.getToken()).getKind() != TokenKind.KEYWORD; + return ident.tokenType().getKind() != TokenKind.KEYWORD; } /**