8079362: Enforce best practices for Node token API usage

Reviewed-by: hannesw, sundar
This commit is contained in:
Attila Szegedi 2015-05-06 15:46:54 +02:00
parent 79086bc842
commit 421b276f85
12 changed files with 32 additions and 33 deletions

View File

@ -113,7 +113,7 @@ final class IRTranslator extends NodeVisitor<LexicalContext> {
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 class IRTranslator extends NodeVisitor<LexicalContext> {
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 @@ final class IRTranslator extends NodeVisitor<LexicalContext> {
@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 {

View File

@ -33,7 +33,7 @@ final class InstanceOfTreeImpl extends BinaryTreeImpl implements InstanceOfTree
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

View File

@ -32,7 +32,7 @@ final class NewTreeImpl extends ExpressionTreeImpl implements NewTree {
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;
}

View File

@ -84,6 +84,7 @@ import jdk.nashorn.internal.ir.UnaryNode;
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 @@ final class AssignSymbols extends NodeVisitor<LexicalContext> implements Loggabl
@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) {

View File

@ -459,7 +459,7 @@ final class LocalVariableTypesCalculator extends NodeVisitor<LexicalContext>{
// 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.

View File

@ -45,8 +45,8 @@ import jdk.nashorn.internal.ir.BreakNode;
import jdk.nashorn.internal.ir.CallNode;
import jdk.nashorn.internal.ir.CaseNode;
import jdk.nashorn.internal.ir.CatchNode;
import jdk.nashorn.internal.ir.DebuggerNode;
import jdk.nashorn.internal.ir.ContinueNode;
import jdk.nashorn.internal.ir.DebuggerNode;
import jdk.nashorn.internal.ir.EmptyNode;
import jdk.nashorn.internal.ir.Expression;
import jdk.nashorn.internal.ir.ExpressionStatement;
@ -210,7 +210,7 @@ final class Lower extends NodeOperatorVisitor<BlockLexicalContext> implements Lo
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);

View File

@ -28,8 +28,6 @@ package jdk.nashorn.internal.ir;
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 @@ public final class AccessNode extends BaseNode {
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;

View File

@ -29,6 +29,7 @@ import static jdk.nashorn.internal.runtime.UnwarrantedOptimismException.INVALID_
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.
@ -121,6 +122,14 @@ public abstract class BaseNode extends Expression implements FunctionCall, Optim
return true;
}
/**
* 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.

View File

@ -312,7 +312,7 @@ public final class BinaryNode extends Expression implements Assignment<Expressio
@Override
public boolean isSelfModifying() {
return isAssignment() && tokenType() != TokenType.ASSIGN;
return isAssignment() && !isTokenType(TokenType.ASSIGN);
}
@Override
@ -529,7 +529,7 @@ public final class BinaryNode extends Expression implements Assignment<Expressio
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();

View File

@ -235,26 +235,28 @@ public abstract class Node implements Cloneable, Serializable {
}
/**
* 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() {

View File

@ -27,7 +27,6 @@ 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.
@ -210,7 +209,7 @@ public final class VarNode extends Statement implements Assignment<IdentNode> {
@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) {

View File

@ -607,7 +607,7 @@ loop:
* @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;
}
/**