Search in sources :

Example 6 with Receiver

use of org.checkerframework.dataflow.analysis.FlowExpressions.Receiver in project checker-framework by typetools.

the class FlowExpressionParseUtil method parse.

/**
 * Parse a string and return its representation as a {@link Receiver}, or throw an {@link
 * FlowExpressionParseException}.
 *
 * @param expression flow expression to parse
 * @param context information about any receiver and arguments
 * @param localScope path to local scope to use
 * @param useLocalScope whether {@code localScope} should be used to resolve identifiers
 */
public static FlowExpressions.Receiver parse(String expression, FlowExpressionContext context, TreePath localScope, boolean useLocalScope) throws FlowExpressionParseException {
    context.useLocalScope = useLocalScope;
    FlowExpressions.Receiver result = parseHelper(expression, context, localScope);
    if (result instanceof ClassName && !expression.endsWith("class")) {
        throw constructParserException(expression, "a class name cannot terminate a flow expression string");
    }
    return result;
}
Also used : ClassName(org.checkerframework.dataflow.analysis.FlowExpressions.ClassName) FlowExpressions(org.checkerframework.dataflow.analysis.FlowExpressions) Receiver(org.checkerframework.dataflow.analysis.FlowExpressions.Receiver)

Example 7 with Receiver

use of org.checkerframework.dataflow.analysis.FlowExpressions.Receiver in project checker-framework by typetools.

the class FlowExpressionParseUtil method parseArray.

private static Receiver parseArray(String s, FlowExpressionContext context, TreePath path) throws FlowExpressionParseException {
    Pair<Pair<String, String>, String> array = parseArray(s);
    if (array == null) {
        return null;
    }
    String receiverStr = array.first.first;
    String indexStr = array.first.second;
    Receiver receiver = parseHelper(receiverStr, context, path);
    FlowExpressionContext contextForIndex = context.copyAndUseOuterReceiver();
    Receiver index = parseHelper(indexStr, contextForIndex, path);
    TypeMirror receiverType = receiver.getType();
    if (!(receiverType instanceof ArrayType)) {
        throw constructParserException(s, String.format("receiver not an array: %s : %s", receiver, receiverType));
    }
    TypeMirror componentType = ((ArrayType) receiverType).getComponentType();
    ArrayAccess result = new ArrayAccess(componentType, receiver, index);
    return result;
}
Also used : ArrayType(com.sun.tools.javac.code.Type.ArrayType) ArrayAccess(org.checkerframework.dataflow.analysis.FlowExpressions.ArrayAccess) TypeMirror(javax.lang.model.type.TypeMirror) Receiver(org.checkerframework.dataflow.analysis.FlowExpressions.Receiver) Pair(org.checkerframework.javacutil.Pair)

Example 8 with Receiver

use of org.checkerframework.dataflow.analysis.FlowExpressions.Receiver in project checker-framework by typetools.

the class SameLenTransfer method refineEq.

/**
 * Handles refinement of equality comparisons (cases 2 and 3). After "a.length == b.length"
 * evaluates to true, a and b have SameLen of each other in the store.
 */
private void refineEq(Node left, Node right, CFStore store) {
    List<Receiver> receivers = new ArrayList<>();
    List<AnnotationMirror> annos = new ArrayList<>();
    for (Node internal : splitAssignments(left)) {
        receivers.add(FlowExpressions.internalReprOf(analysis.getTypeFactory(), internal));
        annos.add(getAnno(internal));
    }
    for (Node internal : splitAssignments(right)) {
        receivers.add(FlowExpressions.internalReprOf(analysis.getTypeFactory(), internal));
        annos.add(getAnno(internal));
    }
    AnnotationMirror combinedSameLen = aTypeFactory.createCombinedSameLen(receivers, annos);
    propagateCombinedSameLen(combinedSameLen, left, store);
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) ArrayCreationNode(org.checkerframework.dataflow.cfg.node.ArrayCreationNode) MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) AssignmentNode(org.checkerframework.dataflow.cfg.node.AssignmentNode) FieldAccessNode(org.checkerframework.dataflow.cfg.node.FieldAccessNode) Node(org.checkerframework.dataflow.cfg.node.Node) ArrayList(java.util.ArrayList) Receiver(org.checkerframework.dataflow.analysis.FlowExpressions.Receiver)

Example 9 with Receiver

use of org.checkerframework.dataflow.analysis.FlowExpressions.Receiver in project checker-framework by typetools.

the class SameLenTransfer method visitAssignment.

/**
 * Handles case 1
 */
@Override
public TransferResult<CFValue, CFStore> visitAssignment(AssignmentNode node, TransferInput<CFValue, CFStore> in) {
    TransferResult<CFValue, CFStore> result = super.visitAssignment(node, in);
    // Handle b = new T[a.length]
    if (node.getExpression() instanceof ArrayCreationNode) {
        ArrayCreationNode acNode = (ArrayCreationNode) node.getExpression();
        if (acNode.getDimensions().size() == 1) {
            Node lengthNode = acNode.getDimension(0);
            Node lengthNodeReceiver = getLengthNodeReceiver(lengthNode);
            if (lengthNodeReceiver != null) {
                // "new T[a.length]" or "new T[s.length()]" is the right hand side of the
                // assignment.  lengthNode is known to be "lengthNodeReceiver.length" or
                // "lengthNodeReceiver.length()"
                // targetRec is the receiver for the left hand side of the assignment.
                Receiver targetRec = FlowExpressions.internalReprOf(analysis.getTypeFactory(), node.getTarget());
                Receiver otherRec = FlowExpressions.internalReprOf(analysis.getTypeFactory(), lengthNodeReceiver);
                AnnotationMirror lengthNodeAnnotation = aTypeFactory.getAnnotatedType(lengthNodeReceiver.getTree()).getAnnotationInHierarchy(UNKNOWN);
                AnnotationMirror combinedSameLen = aTypeFactory.createCombinedSameLen(targetRec, otherRec, UNKNOWN, lengthNodeAnnotation);
                propagateCombinedSameLen(combinedSameLen, node, result.getRegularStore());
                return result;
            }
        }
    }
    AnnotationMirror rightAnno = aTypeFactory.getAnnotatedType(node.getExpression().getTree()).getAnnotationInHierarchy(UNKNOWN);
    // If the left side of the assignment is an array or a string, then have both the right and
    // left side be SameLen of each other.
    Receiver targetRec = FlowExpressions.internalReprOf(analysis.getTypeFactory(), node.getTarget());
    Receiver exprRec = FlowExpressions.internalReprOf(analysis.getTypeFactory(), node.getExpression());
    if (IndexUtil.isSequenceType(node.getTarget().getType()) || (rightAnno != null && AnnotationUtils.areSameByClass(rightAnno, SameLen.class))) {
        AnnotationMirror rightAnnoOrUnknown = rightAnno == null ? UNKNOWN : rightAnno;
        AnnotationMirror combinedSameLen = aTypeFactory.createCombinedSameLen(targetRec, exprRec, UNKNOWN, rightAnnoOrUnknown);
        propagateCombinedSameLen(combinedSameLen, node, result.getRegularStore());
    }
    return result;
}
Also used : CFValue(org.checkerframework.framework.flow.CFValue) AnnotationMirror(javax.lang.model.element.AnnotationMirror) CFStore(org.checkerframework.framework.flow.CFStore) SameLen(org.checkerframework.checker.index.qual.SameLen) ArrayCreationNode(org.checkerframework.dataflow.cfg.node.ArrayCreationNode) MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) AssignmentNode(org.checkerframework.dataflow.cfg.node.AssignmentNode) FieldAccessNode(org.checkerframework.dataflow.cfg.node.FieldAccessNode) Node(org.checkerframework.dataflow.cfg.node.Node) Receiver(org.checkerframework.dataflow.analysis.FlowExpressions.Receiver) ArrayCreationNode(org.checkerframework.dataflow.cfg.node.ArrayCreationNode)

Example 10 with Receiver

use of org.checkerframework.dataflow.analysis.FlowExpressions.Receiver in project checker-framework by typetools.

the class OffsetEquation method standardizeAndViewpointAdaptExpressions.

/**
 * Standardizes and viewpoint-adapts string terms in the list based on the supplied context.
 * Terms that evaluate to a integer constant are removed from the list, and the constants are
 * added to or subtracted from the intValue field.
 */
private void standardizeAndViewpointAdaptExpressions(List<String> terms, boolean subtract, FlowExpressionContext context, TreePath scope, boolean useLocalScope) throws FlowExpressionParseException {
    // Standardize all terms and remove constants
    int length = terms.size(), j = 0;
    for (int i = 0; i < length; ++i) {
        String term = terms.get(i);
        Receiver receiver = FlowExpressionParseUtil.parse(term, context, scope, useLocalScope);
        Integer termConstant = evalConstantTerm(receiver);
        if (termConstant == null) {
            terms.set(j, receiver.toString());
            ++j;
        } else if (subtract) {
            intValue -= termConstant;
        } else {
            intValue += termConstant;
        }
    }
    // Remove remaining elements from the end of the list
    terms.subList(j, length).clear();
}
Also used : Receiver(org.checkerframework.dataflow.analysis.FlowExpressions.Receiver)

Aggregations

Receiver (org.checkerframework.dataflow.analysis.FlowExpressions.Receiver)55 AnnotationMirror (javax.lang.model.element.AnnotationMirror)19 Node (org.checkerframework.dataflow.cfg.node.Node)15 MethodInvocationNode (org.checkerframework.dataflow.cfg.node.MethodInvocationNode)14 FieldAccessNode (org.checkerframework.dataflow.cfg.node.FieldAccessNode)10 CFValue (org.checkerframework.framework.flow.CFValue)9 FlowExpressions (org.checkerframework.dataflow.analysis.FlowExpressions)8 FieldAccess (org.checkerframework.dataflow.analysis.FlowExpressions.FieldAccess)8 NumericalAdditionNode (org.checkerframework.dataflow.cfg.node.NumericalAdditionNode)8 NumericalSubtractionNode (org.checkerframework.dataflow.cfg.node.NumericalSubtractionNode)8 ArrayList (java.util.ArrayList)7 TypeMirror (javax.lang.model.type.TypeMirror)7 ClassName (org.checkerframework.dataflow.analysis.FlowExpressions.ClassName)7 AssignmentNode (org.checkerframework.dataflow.cfg.node.AssignmentNode)7 Tree (com.sun.source.tree.Tree)6 TreePath (com.sun.source.util.TreePath)6 ArrayCreationNode (org.checkerframework.dataflow.cfg.node.ArrayCreationNode)6 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)6 MethodTree (com.sun.source.tree.MethodTree)5 CaseNode (org.checkerframework.dataflow.cfg.node.CaseNode)5