Search in sources :

Example 21 with TypeSystemError

use of org.checkerframework.javacutil.TypeSystemError in project checker-framework by typetools.

the class ValueTransfer method calculateRangeUnaryOp.

/**
 * Calculate the result range after a unary operation of a numerical type node.
 *
 * @param operand the node that represents the operand
 * @param op the operator type
 * @param p the transfer input
 * @return the result annotation mirror
 */
private Range calculateRangeUnaryOp(Node operand, NumericalUnaryOps op, TransferInput<CFValue, CFStore> p) {
    if (TypesUtils.isIntegralPrimitive(operand.getType())) {
        Range range = getIntRange(operand, p);
        Range resultRange;
        switch(op) {
            case PLUS:
                resultRange = range.unaryPlus();
                break;
            case MINUS:
                resultRange = range.unaryMinus();
                break;
            case BITWISE_COMPLEMENT:
                resultRange = range.bitwiseComplement();
                break;
            default:
                throw new TypeSystemError("ValueTransfer: unsupported operation: " + op);
        }
        // operations.
        return operand.getType().getKind() == TypeKind.LONG ? resultRange : resultRange.intRange();
    } else {
        return Range.EVERYTHING;
    }
}
Also used : TypeSystemError(org.checkerframework.javacutil.TypeSystemError) ArrayLenRange(org.checkerframework.common.value.qual.ArrayLenRange) Range(org.checkerframework.common.value.util.Range)

Example 22 with TypeSystemError

use of org.checkerframework.javacutil.TypeSystemError in project checker-framework by typetools.

the class ValueTransfer method calculateBinaryComparison.

private List<Boolean> calculateBinaryComparison(Node leftNode, CFValue leftValue, Node rightNode, CFValue rightValue, ComparisonOperators op, CFStore thenStore, CFStore elseStore) {
    AnnotationMirror leftAnno = getValueAnnotation(leftValue);
    AnnotationMirror rightAnno = getValueAnnotation(rightValue);
    if (atypeFactory.isIntRange(leftAnno) || atypeFactory.isIntRange(rightAnno) || isIntegralUnknownVal(rightNode, rightAnno) || isIntegralUnknownVal(leftNode, leftAnno)) {
        // of values to a range. (This could be implemented in the future.)
        return refineIntRanges(leftNode, leftAnno, rightNode, rightAnno, op, thenStore, elseStore);
    }
    List<? extends Number> lefts = getNumericalValues(leftNode, leftAnno);
    List<? extends Number> rights = getNumericalValues(rightNode, rightAnno);
    if (lefts == null || rights == null) {
        // Appropriately handle bottom when something is compared to bottom.
        if (AnnotationUtils.areSame(leftAnno, atypeFactory.BOTTOMVAL) || AnnotationUtils.areSame(rightAnno, atypeFactory.BOTTOMVAL)) {
            return Collections.emptyList();
        }
        return null;
    }
    // This is a list of all the values that the expression can evaluate to.
    List<Boolean> resultValues = new ArrayList<>();
    // These lists are used to refine the values in the store based on the results of the
    // comparison.
    List<Number> thenLeftVals = new ArrayList<>();
    List<Number> elseLeftVals = new ArrayList<>();
    List<Number> thenRightVals = new ArrayList<>();
    List<Number> elseRightVals = new ArrayList<>();
    for (Number left : lefts) {
        NumberMath<?> nmLeft = NumberMath.getNumberMath(left);
        for (Number right : rights) {
            Boolean result;
            switch(op) {
                case EQUAL:
                    result = nmLeft.equalTo(right);
                    break;
                case GREATER_THAN:
                    result = nmLeft.greaterThan(right);
                    break;
                case GREATER_THAN_EQ:
                    result = nmLeft.greaterThanEq(right);
                    break;
                case LESS_THAN:
                    result = nmLeft.lessThan(right);
                    break;
                case LESS_THAN_EQ:
                    result = nmLeft.lessThanEq(right);
                    break;
                case NOT_EQUAL:
                    result = nmLeft.notEqualTo(right);
                    break;
                default:
                    throw new TypeSystemError("ValueTransfer: unsupported operation: " + op);
            }
            resultValues.add(result);
            if (result) {
                thenLeftVals.add(left);
                thenRightVals.add(right);
            } else {
                elseLeftVals.add(left);
                elseRightVals.add(right);
            }
        }
    }
    createAnnotationFromResultsAndAddToStore(thenStore, thenLeftVals, leftNode);
    createAnnotationFromResultsAndAddToStore(elseStore, elseLeftVals, leftNode);
    createAnnotationFromResultsAndAddToStore(thenStore, thenRightVals, rightNode);
    createAnnotationFromResultsAndAddToStore(elseStore, elseRightVals, rightNode);
    return resultValues;
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) ArrayList(java.util.ArrayList) TypeSystemError(org.checkerframework.javacutil.TypeSystemError)

Example 23 with TypeSystemError

use of org.checkerframework.javacutil.TypeSystemError in project checker-framework by typetools.

the class ValueTransfer method calculateRangeBinaryOp.

/**
 * Calculate the result range after a binary operation between two numerical type nodes.
 */
private Range calculateRangeBinaryOp(Node leftNode, Node rightNode, NumericalBinaryOps op, TransferInput<CFValue, CFStore> p) {
    if (TypesUtils.isIntegralPrimitive(leftNode.getType()) && TypesUtils.isIntegralPrimitive(rightNode.getType())) {
        Range leftRange = getIntRange(leftNode, p);
        Range rightRange = getIntRange(rightNode, p);
        Range resultRange;
        switch(op) {
            case ADDITION:
                resultRange = leftRange.plus(rightRange);
                break;
            case SUBTRACTION:
                resultRange = leftRange.minus(rightRange);
                break;
            case MULTIPLICATION:
                resultRange = leftRange.times(rightRange);
                break;
            case DIVISION:
                resultRange = leftRange.divide(rightRange);
                break;
            case REMAINDER:
                resultRange = leftRange.remainder(rightRange);
                break;
            case SHIFT_LEFT:
                resultRange = leftRange.shiftLeft(rightRange);
                break;
            case SIGNED_SHIFT_RIGHT:
                resultRange = leftRange.signedShiftRight(rightRange);
                break;
            case UNSIGNED_SHIFT_RIGHT:
                resultRange = leftRange.unsignedShiftRight(rightRange);
                break;
            case BITWISE_AND:
                resultRange = leftRange.bitwiseAnd(rightRange);
                break;
            case BITWISE_OR:
                resultRange = leftRange.bitwiseOr(rightRange);
                break;
            case BITWISE_XOR:
                resultRange = leftRange.bitwiseXor(rightRange);
                break;
            default:
                throw new TypeSystemError("ValueTransfer: unsupported operation: " + op);
        }
        // operations.
        return leftNode.getType().getKind() == TypeKind.LONG || rightNode.getType().getKind() == TypeKind.LONG ? resultRange : resultRange.intRange();
    } else {
        return Range.EVERYTHING;
    }
}
Also used : TypeSystemError(org.checkerframework.javacutil.TypeSystemError) ArrayLenRange(org.checkerframework.common.value.qual.ArrayLenRange) Range(org.checkerframework.common.value.util.Range)

Aggregations

TypeSystemError (org.checkerframework.javacutil.TypeSystemError)23 AnnotationMirror (javax.lang.model.element.AnnotationMirror)6 ExecutableElement (javax.lang.model.element.ExecutableElement)4 Range (org.checkerframework.common.value.util.Range)4 Element (javax.lang.model.element.Element)3 VariableElement (javax.lang.model.element.VariableElement)3 RequiresNonNull (org.checkerframework.checker.nullness.qual.RequiresNonNull)3 ArrayLenRange (org.checkerframework.common.value.qual.ArrayLenRange)3 MethodTree (com.sun.source.tree.MethodTree)2 ArrayList (java.util.ArrayList)2 TypeElement (javax.lang.model.element.TypeElement)2 MustCallAnnotatedTypeFactory (org.checkerframework.checker.mustcall.MustCallAnnotatedTypeFactory)2 MethodInvocationNode (org.checkerframework.dataflow.cfg.node.MethodInvocationNode)2 ObjectCreationNode (org.checkerframework.dataflow.cfg.node.ObjectCreationNode)2 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)2 AnnotationBuilder (org.checkerframework.javacutil.AnnotationBuilder)2 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)1 ExpressionTree (com.sun.source.tree.ExpressionTree)1 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)1 MemberReferenceTree (com.sun.source.tree.MemberReferenceTree)1