use of org.checkerframework.dataflow.analysis.FlowExpressions.Receiver in project checker-framework by typetools.
the class DependentTypesHelper method viewpointAdaptExecutable.
private void viewpointAdaptExecutable(ExpressionTree tree, ExpressionTree receiverTree, AnnotatedExecutableType typeFromUse, List<? extends ExpressionTree> args) {
Element element = TreeUtils.elementFromUse(tree);
AnnotatedExecutableType viewpointAdaptedType = (AnnotatedExecutableType) factory.getAnnotatedType(element);
if (!hasDependentType(viewpointAdaptedType)) {
return;
}
FlowExpressions.Receiver receiver;
if (receiverTree == null) {
receiver = FlowExpressions.internalReprOfImplicitReceiver(TreeUtils.elementFromUse(tree));
} else {
receiver = FlowExpressions.internalReprOf(factory, receiverTree);
}
List<FlowExpressions.Receiver> argReceivers = new ArrayList<>(args.size());
for (ExpressionTree argTree : args) {
argReceivers.add(FlowExpressions.internalReprOf(factory, argTree));
}
TreePath currentPath = factory.getPath(tree);
FlowExpressionContext context = new FlowExpressionContext(receiver, argReceivers, factory.getContext());
// typeForUse cannot be viewpoint adapted directly because it is the type post type variable
// substitution. Dependent type annotations on type arguments do not (and cannot) be
// viewpoint adapted along with the dependent type annotations that are on the method
// declaration. For example:
// Map<String, String> map = ...;
// List<@KeyFor("map") String> list = ...;
// list.get(0)
// If the type of List.get is viewpoint adapted for the invocation "list.get(0)", then
// typeFromUse would be @KeyFor("map") String get(int).
// Instead, use the type for the method (viewpointAdaptedType) and viewpoint adapt that
// type.
// Then copy annotations from the viewpoint adapted type to typeFromUse, if that annotation
// is not on a type that was substituted for a type variable.
standardizeDoNotUseLocals(context, currentPath, viewpointAdaptedType);
new ViewpointAdaptedCopier().visit(viewpointAdaptedType, typeFromUse);
}
use of org.checkerframework.dataflow.analysis.FlowExpressions.Receiver in project checker-framework by typetools.
the class DependentTypesHelper method standardizeVariable.
public void standardizeVariable(Tree node, AnnotatedTypeMirror type, Element ele) {
if (!hasDependentType(type)) {
return;
}
TreePath path = factory.getPath(node);
if (path == null) {
return;
}
switch(ele.getKind()) {
case PARAMETER:
Tree enclTree = TreeUtils.enclosingOfKind(path, new HashSet<>(Arrays.asList(Kind.METHOD, Kind.LAMBDA_EXPRESSION)));
if (enclTree.getKind() == Kind.METHOD) {
// If the most enclosing tree is a method, the parameter is a method parameter
MethodTree methodTree = (MethodTree) enclTree;
TypeMirror enclosingType = ElementUtils.enclosingClass(ele).asType();
FlowExpressionContext parameterContext = FlowExpressionContext.buildContextForMethodDeclaration(methodTree, enclosingType, factory.getContext());
standardizeDoNotUseLocals(parameterContext, path, type);
} else {
// Otherwise, the parameter is a lambda parameter
LambdaExpressionTree lambdaTree = (LambdaExpressionTree) enclTree;
FlowExpressionContext parameterContext = FlowExpressionContext.buildContextForLambda(lambdaTree, path, factory.getContext());
// TODO: test this.
// TODO: use path.getParentPath to prevent a StackOverflowError, see Issue
// #1027.
standardizeUseLocals(parameterContext, path.getParentPath(), type);
}
break;
case LOCAL_VARIABLE:
case RESOURCE_VARIABLE:
case EXCEPTION_PARAMETER:
TypeMirror enclosingType = ElementUtils.enclosingClass(ele).asType();
FlowExpressions.Receiver receiver = FlowExpressions.internalReprOfPseudoReceiver(path, enclosingType);
List<Receiver> params = FlowExpressions.getParametersOfEnclosingMethod(factory, path);
FlowExpressionContext localContext = new FlowExpressionContext(receiver, params, factory.getContext());
standardizeUseLocals(localContext, path, type);
break;
case FIELD:
FlowExpressions.Receiver receiverF;
if (node.getKind() == Tree.Kind.IDENTIFIER) {
FlowExpressions.Receiver r = FlowExpressions.internalReprOf(factory, (IdentifierTree) node);
receiverF = r instanceof FlowExpressions.FieldAccess ? ((FlowExpressions.FieldAccess) r).getReceiver() : r;
} else {
receiverF = FlowExpressions.internalReprOfImplicitReceiver(ele);
}
FlowExpressionContext fieldContext = new FlowExpressionContext(receiverF, null, factory.getContext());
standardizeDoNotUseLocals(fieldContext, path, type);
break;
default:
}
}
use of org.checkerframework.dataflow.analysis.FlowExpressions.Receiver in project checker-framework by typetools.
the class BaseTypeVisitor method resolveContracts.
/**
* Takes a set of contracts identified by their expression and annotation strings and resolves
* them to the correct {@link Receiver} and {@link AnnotationMirror}.
*/
private Set<Pair<Receiver, AnnotationMirror>> resolveContracts(Set<? extends Contract> contractSet, AnnotatedExecutableType method) {
Set<Pair<Receiver, AnnotationMirror>> result = new HashSet<>();
MethodTree methodTree = visitorState.getMethodTree();
TreePath path = atypeFactory.getPath(methodTree);
FlowExpressionContext flowExprContext = null;
for (Contract p : contractSet) {
String expression = p.expression;
AnnotationMirror annotation = p.annotation;
if (flowExprContext == null) {
flowExprContext = FlowExpressionContext.buildContextForMethodDeclaration(methodTree, method.getReceiverType().getUnderlyingType(), checker.getContext());
}
annotation = standardizeAnnotationFromContract(annotation, flowExprContext, path);
try {
// TODO: currently, these expressions are parsed many times.
// this could
// be optimized to store the result the first time.
// (same for other annotations)
FlowExpressions.Receiver expr = FlowExpressionParseUtil.parse(expression, flowExprContext, path, false);
result.add(Pair.of(expr, annotation));
} catch (FlowExpressionParseException e) {
// report errors here
checker.report(e.getResult(), methodTree);
}
}
return result;
}
use of org.checkerframework.dataflow.analysis.FlowExpressions.Receiver in project checker-framework by typetools.
the class ValueTransfer method processConditionalPostconditions.
@Override
protected void processConditionalPostconditions(MethodInvocationNode n, ExecutableElement methodElement, Tree tree, CFStore thenStore, CFStore elseStore) {
// For String.startsWith(String) and String.endsWith(String), refine the minimum length
// of the receiver to the minimum length of the argument.
ValueMethodIdentifier methodIdentifier = atypefactory.getMethodIdentifier();
if (methodIdentifier.isStartsWithMethod(methodElement) || methodIdentifier.isEndsWithMethod(methodElement)) {
Node argumentNode = n.getArgument(0);
AnnotationMirror argumentAnno = getArrayOrStringAnnotation(argumentNode);
int minLength = atypefactory.getMinLenValue(argumentAnno);
// Update the annotation of the receiver
if (minLength != 0) {
Receiver receiver = FlowExpressions.internalReprOf(atypefactory, n.getTarget().getReceiver());
AnnotationMirror minLenAnno = atypefactory.createArrayLenRangeAnnotation(minLength, Integer.MAX_VALUE);
thenStore.insertValue(receiver, minLenAnno);
}
}
super.processConditionalPostconditions(n, methodElement, tree, thenStore, elseStore);
}
use of org.checkerframework.dataflow.analysis.FlowExpressions.Receiver in project checker-framework by typetools.
the class ValueTransfer method refineAtLengthAccess.
/**
* Transform @IntVal or @IntRange annotations of a array or string length into an @ArrayLen
* or @ArrayLenRange annotation for the array or string.
*/
private void refineAtLengthAccess(Node lengthNode, Node receiverNode, CFStore store) {
Receiver lengthRec = FlowExpressions.internalReprOf(analysis.getTypeFactory(), lengthNode);
// not marked @Pure, then do not refine.
if (lengthRec instanceof FlowExpressions.Unknown) {
return;
}
CFValue value = store.getValue(lengthRec);
if (value == null) {
return;
}
AnnotationMirror lengthAnno = getValueAnnotation(value);
if (lengthAnno == null) {
return;
}
if (AnnotationUtils.areSameByClass(lengthAnno, BottomVal.class)) {
// If the length is bottom, then this is dead code, so the receiver type
// should also be bottom.
Receiver receiver = FlowExpressions.internalReprOf(atypefactory, receiverNode);
store.insertValue(receiver, lengthAnno);
return;
}
RangeOrListOfValues rolv;
if (atypefactory.isIntRange(lengthAnno)) {
rolv = new RangeOrListOfValues(ValueAnnotatedTypeFactory.getRange(lengthAnno));
} else if (AnnotationUtils.areSameByClass(lengthAnno, IntVal.class)) {
List<Long> lengthValues = ValueAnnotatedTypeFactory.getIntValues(lengthAnno);
rolv = new RangeOrListOfValues(RangeOrListOfValues.convertLongsToInts(lengthValues));
} else {
return;
}
AnnotationMirror newRecAnno = rolv.createAnnotation(atypefactory);
AnnotationMirror oldRecAnno = getArrayOrStringAnnotation(receiverNode);
AnnotationMirror combinedRecAnno;
// with the facts known about its length using GLB.
if (oldRecAnno == null) {
combinedRecAnno = newRecAnno;
} else {
combinedRecAnno = atypefactory.getQualifierHierarchy().greatestLowerBound(oldRecAnno, newRecAnno);
}
Receiver receiver = FlowExpressions.internalReprOf(analysis.getTypeFactory(), receiverNode);
store.insertValue(receiver, combinedRecAnno);
}
Aggregations