use of org.checkerframework.javacutil.BugInCF in project checker-framework by typetools.
the class CFGTranslationPhaseOne method visitLiteral.
@Override
public Node visitLiteral(LiteralTree tree, Void p) {
Node r = null;
switch(tree.getKind()) {
case BOOLEAN_LITERAL:
r = new BooleanLiteralNode(tree);
break;
case CHAR_LITERAL:
r = new CharacterLiteralNode(tree);
break;
case DOUBLE_LITERAL:
r = new DoubleLiteralNode(tree);
break;
case FLOAT_LITERAL:
r = new FloatLiteralNode(tree);
break;
case INT_LITERAL:
r = new IntegerLiteralNode(tree);
break;
case LONG_LITERAL:
r = new LongLiteralNode(tree);
break;
case NULL_LITERAL:
r = new NullLiteralNode(tree);
break;
case STRING_LITERAL:
r = new StringLiteralNode(tree);
break;
default:
throw new BugInCF("unexpected literal tree");
}
assert r != null : "unexpected literal tree";
extendWithNode(r);
return r;
}
use of org.checkerframework.javacutil.BugInCF in project checker-framework by typetools.
the class CFGTranslationPhaseOne method visitCompoundAssignment.
@Override
public Node visitCompoundAssignment(CompoundAssignmentTree tree, Void p) {
// According the JLS 15.26.2, E1 op= E2 is equivalent to
// E1 = (T) ((E1) op (E2)), where T is the type of E1,
// except that E1 is evaluated only once.
//
Tree.Kind kind = tree.getKind();
switch(kind) {
case DIVIDE_ASSIGNMENT:
case MULTIPLY_ASSIGNMENT:
case REMAINDER_ASSIGNMENT:
{
// see JLS 15.17 and 15.26.2
Node targetLHS = scan(tree.getVariable(), p);
Node value = scan(tree.getExpression(), p);
TypeMirror exprType = TreeUtils.typeOf(tree);
TypeMirror leftType = TreeUtils.typeOf(tree.getVariable());
TypeMirror rightType = TreeUtils.typeOf(tree.getExpression());
TypeMirror promotedType = binaryPromotedType(leftType, rightType);
Node targetRHS = binaryNumericPromotion(targetLHS, promotedType);
value = binaryNumericPromotion(value, promotedType);
BinaryTree operTree = treeBuilder.buildBinary(promotedType, withoutAssignment(kind), tree.getVariable(), tree.getExpression());
handleArtificialTree(operTree);
Node operNode;
if (kind == Tree.Kind.MULTIPLY_ASSIGNMENT) {
operNode = new NumericalMultiplicationNode(operTree, targetRHS, value);
} else if (kind == Tree.Kind.DIVIDE_ASSIGNMENT) {
if (TypesUtils.isIntegralPrimitive(exprType)) {
operNode = new IntegerDivisionNode(operTree, targetRHS, value);
extendWithNodeWithException(operNode, arithmeticExceptionType);
} else {
operNode = new FloatingDivisionNode(operTree, targetRHS, value);
}
} else {
assert kind == Tree.Kind.REMAINDER_ASSIGNMENT;
if (TypesUtils.isIntegralPrimitive(exprType)) {
operNode = new IntegerRemainderNode(operTree, targetRHS, value);
extendWithNodeWithException(operNode, arithmeticExceptionType);
} else {
operNode = new FloatingRemainderNode(operTree, targetRHS, value);
}
}
extendWithNode(operNode);
TypeCastTree castTree = treeBuilder.buildTypeCast(leftType, operTree);
handleArtificialTree(castTree);
TypeCastNode castNode = new TypeCastNode(castTree, operNode, leftType, types);
castNode.setInSource(false);
extendWithNode(castNode);
AssignmentNode assignNode = new AssignmentNode(tree, targetLHS, castNode);
extendWithNode(assignNode);
return assignNode;
}
case MINUS_ASSIGNMENT:
case PLUS_ASSIGNMENT:
{
// see JLS 15.18 and 15.26.2
Node targetLHS = scan(tree.getVariable(), p);
Node value = scan(tree.getExpression(), p);
TypeMirror leftType = TreeUtils.typeOf(tree.getVariable());
TypeMirror rightType = TreeUtils.typeOf(tree.getExpression());
if (TypesUtils.isString(leftType) || TypesUtils.isString(rightType)) {
assert (kind == Tree.Kind.PLUS_ASSIGNMENT);
Node targetRHS = stringConversion(targetLHS);
value = stringConversion(value);
Node r = new StringConcatenateAssignmentNode(tree, targetRHS, value);
extendWithNode(r);
return r;
} else {
TypeMirror promotedType = binaryPromotedType(leftType, rightType);
Node targetRHS = binaryNumericPromotion(targetLHS, promotedType);
value = binaryNumericPromotion(value, promotedType);
BinaryTree operTree = treeBuilder.buildBinary(promotedType, withoutAssignment(kind), tree.getVariable(), tree.getExpression());
handleArtificialTree(operTree);
Node operNode;
if (kind == Tree.Kind.PLUS_ASSIGNMENT) {
operNode = new NumericalAdditionNode(operTree, targetRHS, value);
} else {
assert kind == Tree.Kind.MINUS_ASSIGNMENT;
operNode = new NumericalSubtractionNode(operTree, targetRHS, value);
}
extendWithNode(operNode);
TypeCastTree castTree = treeBuilder.buildTypeCast(leftType, operTree);
handleArtificialTree(castTree);
TypeCastNode castNode = new TypeCastNode(castTree, operNode, leftType, types);
castNode.setInSource(false);
extendWithNode(castNode);
// Map the compound assignment tree to an assignment node, which
// will have the correct type.
AssignmentNode assignNode = new AssignmentNode(tree, targetLHS, castNode);
extendWithNode(assignNode);
return assignNode;
}
}
case LEFT_SHIFT_ASSIGNMENT:
case RIGHT_SHIFT_ASSIGNMENT:
case UNSIGNED_RIGHT_SHIFT_ASSIGNMENT:
{
// see JLS 15.19 and 15.26.2
Node targetLHS = scan(tree.getVariable(), p);
Node value = scan(tree.getExpression(), p);
TypeMirror leftType = TreeUtils.typeOf(tree.getVariable());
Node targetRHS = unaryNumericPromotion(targetLHS);
value = unaryNumericPromotion(value);
BinaryTree operTree = treeBuilder.buildBinary(leftType, withoutAssignment(kind), tree.getVariable(), tree.getExpression());
handleArtificialTree(operTree);
Node operNode;
if (kind == Tree.Kind.LEFT_SHIFT_ASSIGNMENT) {
operNode = new LeftShiftNode(operTree, targetRHS, value);
} else if (kind == Tree.Kind.RIGHT_SHIFT_ASSIGNMENT) {
operNode = new SignedRightShiftNode(operTree, targetRHS, value);
} else {
assert kind == Tree.Kind.UNSIGNED_RIGHT_SHIFT_ASSIGNMENT;
operNode = new UnsignedRightShiftNode(operTree, targetRHS, value);
}
extendWithNode(operNode);
TypeCastTree castTree = treeBuilder.buildTypeCast(leftType, operTree);
handleArtificialTree(castTree);
TypeCastNode castNode = new TypeCastNode(castTree, operNode, leftType, types);
castNode.setInSource(false);
extendWithNode(castNode);
AssignmentNode assignNode = new AssignmentNode(tree, targetLHS, castNode);
extendWithNode(assignNode);
return assignNode;
}
case AND_ASSIGNMENT:
case OR_ASSIGNMENT:
case XOR_ASSIGNMENT:
// see JLS 15.22
Node targetLHS = scan(tree.getVariable(), p);
Node value = scan(tree.getExpression(), p);
TypeMirror leftType = TreeUtils.typeOf(tree.getVariable());
TypeMirror rightType = TreeUtils.typeOf(tree.getExpression());
Node targetRHS = null;
if (isNumericOrBoxed(leftType) && isNumericOrBoxed(rightType)) {
TypeMirror promotedType = binaryPromotedType(leftType, rightType);
targetRHS = binaryNumericPromotion(targetLHS, promotedType);
value = binaryNumericPromotion(value, promotedType);
} else if (TypesUtils.isBooleanType(leftType) && TypesUtils.isBooleanType(rightType)) {
targetRHS = unbox(targetLHS);
value = unbox(value);
} else {
throw new BugInCF("Both arguments to logical operation must be numeric or boolean");
}
BinaryTree operTree = treeBuilder.buildBinary(leftType, withoutAssignment(kind), tree.getVariable(), tree.getExpression());
handleArtificialTree(operTree);
Node operNode;
if (kind == Tree.Kind.AND_ASSIGNMENT) {
operNode = new BitwiseAndNode(operTree, targetRHS, value);
} else if (kind == Tree.Kind.OR_ASSIGNMENT) {
operNode = new BitwiseOrNode(operTree, targetRHS, value);
} else {
assert kind == Tree.Kind.XOR_ASSIGNMENT;
operNode = new BitwiseXorNode(operTree, targetRHS, value);
}
extendWithNode(operNode);
TypeCastTree castTree = treeBuilder.buildTypeCast(leftType, operTree);
handleArtificialTree(castTree);
TypeCastNode castNode = new TypeCastNode(castTree, operNode, leftType, types);
castNode.setInSource(false);
extendWithNode(castNode);
AssignmentNode assignNode = new AssignmentNode(tree, targetLHS, castNode);
extendWithNode(assignNode);
return assignNode;
default:
throw new BugInCF("unexpected compound assignment type");
}
}
use of org.checkerframework.javacutil.BugInCF in project checker-framework by typetools.
the class CFGTranslationPhaseOne method visitUnary.
@Override
public Node visitUnary(UnaryTree tree, Void p) {
Node result = null;
Tree.Kind kind = tree.getKind();
switch(kind) {
case BITWISE_COMPLEMENT:
case UNARY_MINUS:
case UNARY_PLUS:
{
// see JLS 15.14 and 15.15
Node expr = scan(tree.getExpression(), p);
expr = unaryNumericPromotion(expr);
switch(kind) {
case BITWISE_COMPLEMENT:
result = new BitwiseComplementNode(tree, expr);
break;
case UNARY_MINUS:
result = new NumericalMinusNode(tree, expr);
break;
case UNARY_PLUS:
result = new NumericalPlusNode(tree, expr);
break;
default:
throw new BugInCF("Unexpected kind: " + kind);
}
extendWithNode(result);
break;
}
case LOGICAL_COMPLEMENT:
{
// see JLS 15.15.6
Node expr = scan(tree.getExpression(), p);
result = new ConditionalNotNode(tree, unbox(expr));
extendWithNode(result);
break;
}
case POSTFIX_DECREMENT:
case POSTFIX_INCREMENT:
case PREFIX_DECREMENT:
case PREFIX_INCREMENT:
{
ExpressionTree exprTree = tree.getExpression();
Node expr = scan(exprTree, p);
boolean isIncrement = kind == Tree.Kind.POSTFIX_INCREMENT || kind == Tree.Kind.PREFIX_INCREMENT;
boolean isPostfix = kind == Tree.Kind.POSTFIX_INCREMENT || kind == Tree.Kind.POSTFIX_DECREMENT;
AssignmentNode unaryAssign = createIncrementOrDecrementAssign(isPostfix ? null : tree, expr, isIncrement);
addToUnaryAssignLookupMap(tree, unaryAssign);
if (isPostfix) {
TypeMirror exprType = TreeUtils.typeOf(exprTree);
VariableTree tempVarDecl = treeBuilder.buildVariableDecl(exprType, uniqueName("tempPostfix"), findOwner(), tree.getExpression());
handleArtificialTree(tempVarDecl);
VariableDeclarationNode tempVarDeclNode = new VariableDeclarationNode(tempVarDecl);
tempVarDeclNode.setInSource(false);
extendWithNode(tempVarDeclNode);
Tree tempVar = treeBuilder.buildVariableUse(tempVarDecl);
handleArtificialTree(tempVar);
Node tempVarNode = new LocalVariableNode(tempVar);
tempVarNode.setInSource(false);
extendWithNode(tempVarNode);
AssignmentNode tempAssignNode = new AssignmentNode(tree, tempVarNode, expr);
tempAssignNode.setInSource(false);
extendWithNode(tempAssignNode);
Tree resultExpr = treeBuilder.buildVariableUse(tempVarDecl);
handleArtificialTree(resultExpr);
result = new LocalVariableNode(resultExpr);
result.setInSource(false);
extendWithNode(result);
} else {
result = unaryAssign;
}
break;
}
case OTHER:
default:
// special node NLLCHK
if (tree.toString().startsWith("<*nullchk*>")) {
Node expr = scan(tree.getExpression(), p);
result = new NullChkNode(tree, expr);
extendWithNode(result);
break;
}
throw new BugInCF("Unknown kind (" + kind + ") of unary expression: " + tree);
}
return result;
}
use of org.checkerframework.javacutil.BugInCF in project checker-framework by typetools.
the class JavaExpression method fromVariableElement.
/**
* Returns the Java expression corresponding to the given variable element {@code ele}.
*
* @param typeOfEle the type of {@code ele}
* @param ele element whose JavaExpression is returned
* @return the Java expression corresponding to the given variable element {@code ele}
*/
private static JavaExpression fromVariableElement(TypeMirror typeOfEle, Element ele) {
switch(ele.getKind()) {
case LOCAL_VARIABLE:
case RESOURCE_VARIABLE:
case EXCEPTION_PARAMETER:
case PARAMETER:
return new LocalVariable(ele);
case FIELD:
case ENUM_CONSTANT:
// Implicit access expression, such as "this" or a class name
JavaExpression fieldAccessExpression;
// a field has enclosing class
@SuppressWarnings("nullness:dereference.of.nullable") TypeMirror enclosingTypeElement = ElementUtils.enclosingTypeElement(ele).asType();
if (ElementUtils.isStatic(ele)) {
fieldAccessExpression = new ClassName(enclosingTypeElement);
} else {
fieldAccessExpression = new ThisReference(enclosingTypeElement);
}
return new FieldAccess(fieldAccessExpression, typeOfEle, (VariableElement) ele);
default:
if (ElementUtils.isBindingVariable(ele)) {
return new LocalVariable(ele);
}
throw new BugInCF("Unexpected kind of VariableTree: kind: %s element: %s", ele.getKind(), ele);
}
}
use of org.checkerframework.javacutil.BugInCF in project checker-framework by typetools.
the class PerDirectorySuite method getParametersList.
/**
* Returns a list of one-element arrays, each containing a Java File.
*/
// JUnit needs to be annotated
@SuppressWarnings("nullness")
private List<List<File>> getParametersList(TestClass klass) throws Throwable {
FrameworkMethod method = getParametersMethod(klass);
// or getParametersMethod would fail.
if (method == null) {
throw new BugInCF("no method annotated with @Parameters");
}
if (!method.getReturnType().isArray()) {
throw new BugInCF("@Parameters annotation on method that does not return an array: " + method);
}
String[] dirs = (String[]) method.invokeExplosively(null);
return TestUtilities.findJavaFilesPerDirectory(new File("tests"), dirs);
}
Aggregations