resolver.java
来自「Checkstyle 可寻找:·不能使用的或者多余的输入 ·空格更好的地方不使用」· Java 代码 · 共 1,833 行 · 第 1/5 页
JAVA
1,833 行
if (def != null) {
ident.setDefinition(def, location, referencePhase);
}
return result;
}
/**
* Resolves a (binary) boolean expression. The left and right sides of the
* expression
* are resolved in the process.
*
* @param expression the <code>SymTabAST</code> representing the boolean
* expression.
* @return the <code>Scope</code> for the boolean primitive type.
*/
private IClass resolveBooleanExpression(
SymTabAST expression,
Scope location,
IClass context,
boolean referencePhase) {
IClass result = null;
SymTabAST leftChild = findLeftChild(expression);
resolveExpression(leftChild, location, context, referencePhase);
SymTabAST rightChild = findRightSibling(leftChild);
resolveExpression(rightChild, location, context, referencePhase);
result = LiteralResolver.getDefinition(TokenTypes.LITERAL_BOOLEAN);
return result;
}
/**
* resolves references in an assignment expression
*
* @param expression the <code>SymTabAST</code> of the expression
* @param location the <code>Scope</code> where the expression occurs
* @param context the <code>Scope</code> in which the expression occurs
* (where the search for a defintion begins)
*
* @return the resulting scope of the expression (the type to which it evaluates)
*/
private IClass resolveAssignment(
SymTabAST expression,
Scope location,
IClass context,
boolean referencePhase) {
IClass result = null;
SymTabAST leftNode = (SymTabAST) (expression.getFirstChild());
SymTabAST rightNode = (SymTabAST) (leftNode.getNextSibling());
result = resolveExpression(leftNode, location, context, referencePhase);
resolveExpression(rightNode, location, context, referencePhase);
return result;
}
/**
* Resolves a unary expression. Returns the type of the expression,
* creating any references found along the way. Unary expressions are
* increment (x++), decrement (x--), unary plus (+x), and unary minus (-x)
*
* @param expression the <code>SymTabAST</code> of the unary expression.
* @return the <code>Scope</code> for the type to which the expression
* evalutes.
*/
private IClass resolveUnaryExpression(
SymTabAST expression,
Scope location,
IClass context,
boolean referencePhase) {
SymTabAST operatee = (SymTabAST) (expression.getFirstChild());
return resolveExpression(operatee, location, context, referencePhase);
}
/**
* Resolves an arithmetic expression. Returns the <code>Scope</code> for
* the type to which the expression resolves. Any references found during
* resolution are created and resolved.
*
* @param expression the <code>SymTabAST</code> representing the arithmetic
* expression.
*
* @return the <code>Scope</code> for the type to which the expression
* evaluates.
*/
private IClass resolveArithmeticExpression(
SymTabAST expression,
Scope location,
IClass context,
boolean referencePhase) {
IClass result = null;
SymTabAST leftChild = findLeftChild(expression);
IClass leftType =
(resolveExpression(leftChild,
location,
context,
referencePhase));
SymTabAST rightChild = findRightSibling(leftChild);
IClass rightType =
(resolveExpression(rightChild,
location,
context,
referencePhase));
result = binaryResultType(leftType, rightType);
return result;
}
/**
* Finds the left child of a binary operator, skipping parentheses.
* @param aExpression the node for the binary operator.
* @return the node for the left child.
*/
private SymTabAST findLeftChild(SymTabAST aExpression) {
SymTabAST leftChild = (SymTabAST) (aExpression.getFirstChild());
// handle Checkstyle grammar
while (leftChild.getType() == TokenTypes.LPAREN) {
leftChild = (SymTabAST) leftChild.getNextSibling();
}
return leftChild;
}
/**
* Finds the right sibling of the left child of a binary operator,
* skipping parentheses.
* @param aLeftChild the left child of a binary operator.
* @return the node of the right sibling.
*/
private SymTabAST findRightSibling(SymTabAST aLeftChild) {
SymTabAST rightChild = (SymTabAST) (aLeftChild.getNextSibling());
// handle Checkstyle grammar
while ((rightChild != null)
&& (rightChild.getType() == TokenTypes.RPAREN))
{
rightChild = (SymTabAST) rightChild.getNextSibling();
}
return rightChild;
}
/**
* Returns the <code>ClassDef</code> for the type to which arithmetic
* expressions evaluate.
*
* @param a the <code>ClassDef</code> of the first operand.
* @param b the <code>ClassDef</code> of the second operand.
*
* @return the <code>ClassDef</code> to which the expression evaluates.
*/
private IClass binaryResultType(IClass a, IClass b) {
IClass result = null;
// These may or may not be in line with the rules set forth in the java
// language specification. Not being in line would be a BadThing(r).
IClass string = new ExternalClass(java.lang.String.class);
if (a.equals(string) || b.equals(string)) {
result = string;
}
else if (a.equals(PrimitiveClasses.BOOLEAN)) {
result = PrimitiveClasses.BOOLEAN;
}
else {
result =
PrimitiveClasses.binaryPromote(
a,
b);
}
return result;
}
/**
* resolves references in an instanceof expression
*
* @param expression the <code>SymTabAST</code> of the expression
* @param location the <code>Scope</code> where the expression occurs
* @param context the <code>Scope</code> in which the expression occurs
* (where the search for a defintion begins)
*
* @return the resulting scope of the expression (the type to which it evaluates)
*/
private IClass resolveInstanceOf(
SymTabAST expression,
Scope location,
IClass context,
boolean referencePhase) {
SymTabAST leftNode = (SymTabAST) (expression.getFirstChild());
SymTabAST rightNode = (SymTabAST) (leftNode.getNextSibling());
resolveExpression(leftNode, location, context, referencePhase);
SymTabAST classNameNode = (SymTabAST) (rightNode.getFirstChild());
resolveClass(classNameNode, location, context, referencePhase);
return LiteralResolver.getDefinition(TokenTypes.LITERAL_BOOLEAN);
}
/**
* resolves references in a a break statement
*
* @param expression the <code>SymTabAST</code> for the expression
* @param location the <code>Scope</code> where the expression occurs
* @param context the <code>Scope</code> in which the expression occurs
* (where the search for a defintion begins)
*
* @return the <code>Scope</code> for the int primitive type
*/
private IClass resolveGoto(
SymTabAST expression,
Scope location,
IClass context,
boolean referencePhase) {
SymTabAST label = (SymTabAST) (expression.getFirstChild());
// handle Checkstyle grammar
if (label != null && (label.getType() != TokenTypes.SEMI)) {
LabelDef def = location.getLabelDefinition(label.getText());
if (def != null) {
label.setDefinition(def, location, referencePhase);
}
}
return null;
}
private IClass resolvePrimitiveType(
SymTabAST primitive,
Scope location,
IClass context,
boolean referencePhase) {
IClass result =
LiteralResolver.getDefinition(primitive.getType());
primitive.setDefinition(result, location, referencePhase);
return result;
}
/**
* Returns the <code>ClassDef</code> of the int primitive type. This may
* need to be amended, based on the Java Language spec, to return a long
* if the literal is larger than an int can hold.
*
* @param expression the <code>SymTabAST</code> for the integer literal
* @param location the <code>Scope</code> where the expression occurs
* @param context the <code>Scope</code> in which the expression occurs
* (where the search for a defintion begins)
*
* @return the <code>Scope</code> for the int primitive type
*/
private IClass resolveNumInt(
SymTabAST expression,
Scope location,
IClass context) {
return PrimitiveClasses.INT;
}
/**
* Returns the <code>ClassDef</code> type of the float primitive type.
* This may need to be amended, based on the Java Language spec, to return
* a double if the literal is larger than a float can hold.
*
* @param expression the <code>SymTabAST</code> for the floating point
literal
* @param location the <code>Scope</code> where the expression occurs
* @param context the <code>Scope</code> in which the expression occurs
* (where the search for a defintion begins)
*
* @return the <code>Scope</code> for the float primitive type
*/
private IClass resolveNumFloat(
SymTabAST expression,
Scope location,
IClass context) {
return PrimitiveClasses.DOUBLE;
}
/**
* Returns the <code>ClassDef</code> type of a string literal
*
* @param expression the <code>SymTabAST</code> for a string literal
* @param location the <code>Scope</code> where the expression occurs
* @param context the <code>Scope</code> in which the expression occurs
* (where the search for a defintion begins)
*
* @return the <code>Scope</code> type of a string literal
*/
private IClass resolveStringLiteral(
SymTabAST expression,
Scope location,
IClass context) {
return LiteralResolver.getDefinition(
TokenTypes.STRING_LITERAL);
}
/**
* Returns the <code>ClassDef</code> type of a character literal
*
* @param expression the <code>SymTabAST</code> for a string literal
* @param location the <code>Scope</code> where the expression occurs
* @param context the <code>Scope</code> in which the expression occurs
* (where the search for a defintion begins)
*
* @return the <code>Scope</code> type of a character literal
*/
private IClass resolveCharLiteral(
SymTabAST expression,
Scope location,
IClass context) {
return LiteralResolver.getDefinition(
TokenTypes.LITERAL_CHAR);
}
/**
* Describe <code>resolveBooleanLiteral</code> method here.
*
* @param expression the <code>SymTabAST</code> of the expression
* @param location the <code>Scope</code> where the expression occurs
* @param context the <code>Scope</code> in which the expression occurs
* (where the search for a defintion begins)
*
* @return the <code>Scope</code> for the boolean primitive.
*/
private IClass resolveBooleanLiteral(
SymTabAST expression,
Scope location,
IClass context) {
return LiteralResolver.getDefinition(TokenTypes.LITERAL_BOOLEAN);
}
private IClass resolveBooleanUnary(
SymTabAST expression,
Scope location,
IClass context,
boolean referencePhase) {
SymTabAST child = (SymTabAST) expression.getFirstChild();
resolveExpression(child, location, context, referencePhase);
return LiteralResolver.getDefinition(TokenTypes.LITERAL_BOOLEAN);
}
/**
* Resolves a constructor call.
*
* @param tree the root node of the constructor call
* @return the <code>ClassDef</code> for the class instantiated by the
* constructor
*/
private void resolveArrayInitializer(
SymTabAST initializerNode,
Scope location,
IClass context,
boolean referencePhase) {
SymTabAST child = (SymTabAST) (initializerNode.getFirstChild());
while (child != null)
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?