Search in sources :

Example 6 with ArrayAccessNode

use of org.checkerframework.dataflow.cfg.node.ArrayAccessNode in project checker-framework by typetools.

the class CFAbstractTransfer method getValueFromFactory.

/**
 * Returns the abstract value of a non-leaf tree {@code tree}, as computed by the {@link
 * AnnotatedTypeFactory}.
 *
 * @return the abstract value of a non-leaf tree {@code tree}, as computed by the {@link
 *     AnnotatedTypeFactory}
 */
protected V getValueFromFactory(Tree tree, Node node) {
    GenericAnnotatedTypeFactory<V, S, T, ? extends CFAbstractAnalysis<V, S, T>> factory = analysis.atypeFactory;
    Tree preTree = analysis.getCurrentTree();
    analysis.setCurrentTree(tree);
    AnnotatedTypeMirror at;
    if (node instanceof MethodInvocationNode && ((MethodInvocationNode) node).getIterableExpression() != null) {
        ExpressionTree iter = ((MethodInvocationNode) node).getIterableExpression();
        at = factory.getIterableElementType(iter);
    } else if (node instanceof ArrayAccessNode && ((ArrayAccessNode) node).getArrayExpression() != null) {
        ExpressionTree array = ((ArrayAccessNode) node).getArrayExpression();
        at = factory.getIterableElementType(array);
    } else {
        at = factory.getAnnotatedType(tree);
    }
    analysis.setCurrentTree(preTree);
    return analysis.createAbstractValue(at);
}
Also used : UnderlyingAST(org.checkerframework.dataflow.cfg.UnderlyingAST) MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) ArrayAccessNode(org.checkerframework.dataflow.cfg.node.ArrayAccessNode) MethodTree(com.sun.source.tree.MethodTree) Tree(com.sun.source.tree.Tree) ClassTree(com.sun.source.tree.ClassTree) ExpressionTree(com.sun.source.tree.ExpressionTree) ExpressionTree(com.sun.source.tree.ExpressionTree) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 7 with ArrayAccessNode

use of org.checkerframework.dataflow.cfg.node.ArrayAccessNode 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;
}
Also used : ExecutableElement(javax.lang.model.element.ExecutableElement) ClassNameNode(org.checkerframework.dataflow.cfg.node.ClassNameNode) StringConversionNode(org.checkerframework.dataflow.cfg.node.StringConversionNode) ExplicitThisNode(org.checkerframework.dataflow.cfg.node.ExplicitThisNode) ArrayCreationNode(org.checkerframework.dataflow.cfg.node.ArrayCreationNode) WideningConversionNode(org.checkerframework.dataflow.cfg.node.WideningConversionNode) BinaryOperationNode(org.checkerframework.dataflow.cfg.node.BinaryOperationNode) NarrowingConversionNode(org.checkerframework.dataflow.cfg.node.NarrowingConversionNode) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) List(java.util.List) ArrayList(java.util.ArrayList) MethodInvocationNode(org.checkerframework.dataflow.cfg.node.MethodInvocationNode) SuperNode(org.checkerframework.dataflow.cfg.node.SuperNode) ArrayAccessNode(org.checkerframework.dataflow.cfg.node.ArrayAccessNode) BugInCF(org.checkerframework.javacutil.BugInCF) FieldAccessNode(org.checkerframework.dataflow.cfg.node.FieldAccessNode) LocalVariableNode(org.checkerframework.dataflow.cfg.node.LocalVariableNode) UnaryOperationNode(org.checkerframework.dataflow.cfg.node.UnaryOperationNode) ValueLiteralNode(org.checkerframework.dataflow.cfg.node.ValueLiteralNode) ThisNode(org.checkerframework.dataflow.cfg.node.ThisNode) ExplicitThisNode(org.checkerframework.dataflow.cfg.node.ExplicitThisNode) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Aggregations

ArrayAccessNode (org.checkerframework.dataflow.cfg.node.ArrayAccessNode)7 MethodInvocationNode (org.checkerframework.dataflow.cfg.node.MethodInvocationNode)7 FieldAccessNode (org.checkerframework.dataflow.cfg.node.FieldAccessNode)6 LocalVariableNode (org.checkerframework.dataflow.cfg.node.LocalVariableNode)6 ArrayCreationNode (org.checkerframework.dataflow.cfg.node.ArrayCreationNode)5 ClassNameNode (org.checkerframework.dataflow.cfg.node.ClassNameNode)5 NarrowingConversionNode (org.checkerframework.dataflow.cfg.node.NarrowingConversionNode)5 Node (org.checkerframework.dataflow.cfg.node.Node)5 StringConversionNode (org.checkerframework.dataflow.cfg.node.StringConversionNode)5 SuperNode (org.checkerframework.dataflow.cfg.node.SuperNode)5 ValueLiteralNode (org.checkerframework.dataflow.cfg.node.ValueLiteralNode)5 WideningConversionNode (org.checkerframework.dataflow.cfg.node.WideningConversionNode)5 AssignmentNode (org.checkerframework.dataflow.cfg.node.AssignmentNode)3 EqualToNode (org.checkerframework.dataflow.cfg.node.EqualToNode)3 ExplicitThisNode (org.checkerframework.dataflow.cfg.node.ExplicitThisNode)3 FunctionalInterfaceNode (org.checkerframework.dataflow.cfg.node.FunctionalInterfaceNode)3 InstanceOfNode (org.checkerframework.dataflow.cfg.node.InstanceOfNode)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 ExecutableElement (javax.lang.model.element.ExecutableElement)2