use of org.checkerframework.dataflow.cfg.node.StringConversionNode in project checker-framework by typetools.
the class CFGTranslationPhaseOne method stringConversion.
/**
* Convert the input node to String type, if it isn't already.
*
* @param node an input node
* @return a Node with the value promoted to String, which may be the input node
*/
protected Node stringConversion(Node node) {
// For string conversion, see JLS 5.1.11
if (!TypesUtils.isString(node.getType())) {
Node converted = new StringConversionNode(node.getTree(), node, stringType);
addToConvertedLookupMap(converted);
insertNodeAfter(converted, node);
return converted;
} else {
return node;
}
}
use of org.checkerframework.dataflow.cfg.node.StringConversionNode in project checker-framework by typetools.
the class JavaExpression method fromNode.
/**
* We ignore operations such as widening and narrowing when computing the internal representation.
*
* @param receiverNode a node to convert to a JavaExpression
* @return the internal representation of the given node. Might contain {@link Unknown}.
*/
public static JavaExpression fromNode(Node receiverNode) {
JavaExpression result = null;
if (receiverNode instanceof FieldAccessNode) {
result = fromNodeFieldAccess((FieldAccessNode) receiverNode);
} else if (receiverNode instanceof ExplicitThisNode) {
result = new ThisReference(receiverNode.getType());
} else if (receiverNode instanceof ThisNode) {
result = new ThisReference(receiverNode.getType());
} else if (receiverNode instanceof SuperNode) {
result = new ThisReference(receiverNode.getType());
} else if (receiverNode instanceof LocalVariableNode) {
LocalVariableNode lv = (LocalVariableNode) receiverNode;
result = new LocalVariable(lv);
} else if (receiverNode instanceof ArrayAccessNode) {
ArrayAccessNode a = (ArrayAccessNode) receiverNode;
result = fromArrayAccess(a);
} else if (receiverNode instanceof StringConversionNode) {
// ignore string conversion
return fromNode(((StringConversionNode) receiverNode).getOperand());
} else if (receiverNode instanceof WideningConversionNode) {
// ignore widening
return fromNode(((WideningConversionNode) receiverNode).getOperand());
} else if (receiverNode instanceof NarrowingConversionNode) {
// ignore narrowing
return fromNode(((NarrowingConversionNode) receiverNode).getOperand());
} else if (receiverNode instanceof UnaryOperationNode) {
UnaryOperationNode uopn = (UnaryOperationNode) receiverNode;
return new UnaryOperation(uopn, fromNode(uopn.getOperand()));
} else if (receiverNode instanceof BinaryOperationNode) {
BinaryOperationNode bopn = (BinaryOperationNode) receiverNode;
return new BinaryOperation(bopn, fromNode(bopn.getLeftOperand()), fromNode(bopn.getRightOperand()));
} else if (receiverNode instanceof ClassNameNode) {
ClassNameNode cn = (ClassNameNode) receiverNode;
result = new ClassName(cn.getType());
} else if (receiverNode instanceof ValueLiteralNode) {
ValueLiteralNode vn = (ValueLiteralNode) receiverNode;
result = new ValueLiteral(vn.getType(), vn);
} else if (receiverNode instanceof ArrayCreationNode) {
ArrayCreationNode an = (ArrayCreationNode) receiverNode;
List<@Nullable JavaExpression> dimensions = CollectionsPlume.mapList(JavaExpression::fromNode, an.getDimensions());
List<JavaExpression> initializers = CollectionsPlume.mapList(JavaExpression::fromNode, an.getInitializers());
result = new ArrayCreation(an.getType(), dimensions, initializers);
} else if (receiverNode instanceof MethodInvocationNode) {
MethodInvocationNode mn = (MethodInvocationNode) receiverNode;
MethodInvocationTree t = mn.getTree();
if (t == null) {
throw new BugInCF("Unexpected null tree for node: " + mn);
}
assert TreeUtils.isUseOfElement(t) : "@AssumeAssertion(nullness): tree kind";
ExecutableElement invokedMethod = TreeUtils.elementFromUse(t);
// Note that the method might be nondeterministic.
List<JavaExpression> parameters = CollectionsPlume.mapList(JavaExpression::fromNode, mn.getArguments());
JavaExpression methodReceiver;
if (ElementUtils.isStatic(invokedMethod)) {
methodReceiver = new ClassName(mn.getTarget().getReceiver().getType());
} else {
methodReceiver = fromNode(mn.getTarget().getReceiver());
}
result = new MethodCall(mn.getType(), invokedMethod, methodReceiver, parameters);
}
if (result == null) {
result = new Unknown(receiverNode);
}
return result;
}
use of org.checkerframework.dataflow.cfg.node.StringConversionNode in project checker-framework by typetools.
the class ValueTransfer method getStringLengthRange.
/**
* Returns a range of possible lengths for {@code subNode}, as casted to a String.
*
* @param subNode some subnode of {@code p}
* @param p TransferInput
* @return a range of possible lengths for {@code subNode}, as casted to a String
*/
private Range getStringLengthRange(Node subNode, TransferInput<CFValue, CFStore> p) {
CFValue value = p.getValueOfSubNode(subNode);
AnnotationMirror anno = getValueAnnotation(value);
if (anno == null) {
return null;
}
String annoName = AnnotationUtils.annotationName(anno);
if (annoName.equals(ValueAnnotatedTypeFactory.ARRAYLENRANGE_NAME)) {
return atypeFactory.getRange(anno);
} else if (annoName.equals(ValueAnnotatedTypeFactory.BOTTOMVAL_NAME)) {
return Range.NOTHING;
}
TypeKind subNodeTypeKind = subNode.getType().getKind();
// handle values converted to string (ints, longs, longs with @IntRange)
if (subNode instanceof StringConversionNode) {
return getStringLengthRange(((StringConversionNode) subNode).getOperand(), p);
} else if (isIntRange(subNode, p)) {
return getIntRangeStringLengthRange(subNode, p);
} else if (subNodeTypeKind == TypeKind.INT) {
// ints are between 1 and 11 characters long
return Range.create(1, 11);
} else if (subNodeTypeKind == TypeKind.LONG) {
// longs are between 1 and 20 characters long
return Range.create(1, 20);
}
return Range.create(0, Integer.MAX_VALUE);
}
Aggregations