Search in sources :

Example 86 with AnnotatedTypeMirror

use of org.checkerframework.framework.type.AnnotatedTypeMirror in project checker-framework by typetools.

the class UnitsVisitor method visitCompoundAssignment.

@Override
public Void visitCompoundAssignment(CompoundAssignmentTree node, Void p) {
    ExpressionTree var = node.getVariable();
    ExpressionTree expr = node.getExpression();
    AnnotatedTypeMirror varType = atypeFactory.getAnnotatedType(var);
    AnnotatedTypeMirror exprType = atypeFactory.getAnnotatedType(expr);
    Kind kind = node.getKind();
    if ((kind == Kind.PLUS_ASSIGNMENT || kind == Kind.MINUS_ASSIGNMENT)) {
        if (!atypeFactory.getTypeHierarchy().isSubtype(exprType, varType)) {
            checker.report(Result.failure("compound.assignment.type.incompatible", varType, exprType), node);
        }
    } else if (exprType.getAnnotation(UnknownUnits.class) == null) {
        // Only allow mul/div with unqualified units
        checker.report(Result.failure("compound.assignment.type.incompatible", varType, exprType), node);
    }
    // super.visitCompoundAssignment(node, p);
    return null;
}
Also used : Kind(com.sun.source.tree.Tree.Kind) ExpressionTree(com.sun.source.tree.ExpressionTree) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 87 with AnnotatedTypeMirror

use of org.checkerframework.framework.type.AnnotatedTypeMirror in project checker-framework by typetools.

the class FenumVisitor method visitSwitch.

@Override
public Void visitSwitch(SwitchTree node, Void p) {
    ExpressionTree expr = node.getExpression();
    AnnotatedTypeMirror exprType = atypeFactory.getAnnotatedType(expr);
    for (CaseTree caseExpr : node.getCases()) {
        ExpressionTree realCaseExpr = caseExpr.getExpression();
        if (realCaseExpr != null) {
            AnnotatedTypeMirror caseType = atypeFactory.getAnnotatedType(realCaseExpr);
            this.commonAssignmentCheck(exprType, caseType, caseExpr, "switch.type.incompatible");
        }
    }
    return super.visitSwitch(node, p);
}
Also used : CaseTree(com.sun.source.tree.CaseTree) ExpressionTree(com.sun.source.tree.ExpressionTree) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 88 with AnnotatedTypeMirror

use of org.checkerframework.framework.type.AnnotatedTypeMirror in project checker-framework by typetools.

the class GuiEffectVisitor method visitReturn.

// Returning a lambda expression also triggers inference based on the method's return type.
@Override
public Void visitReturn(ReturnTree node, Void p) {
    Void result = super.visitReturn(node, p);
    if (node.getExpression() != null && node.getExpression().getKind() == Tree.Kind.LAMBDA_EXPRESSION) {
        // Unfortunatelly, need to duplicate a fair bit of BaseTypeVisitor.visitReturn after lambdas have been
        // inferred.
        Pair<Tree, AnnotatedTypeMirror> preAssCtxt = visitorState.getAssignmentContext();
        try {
            Tree enclosing = TreeUtils.enclosingMethodOrLambda(getCurrentPath());
            AnnotatedTypeMirror ret = null;
            if (enclosing.getKind() == Tree.Kind.METHOD) {
                MethodTree enclosingMethod = (MethodTree) enclosing;
                boolean valid = validateTypeOf(enclosing);
                if (valid) {
                    ret = atypeFactory.getMethodReturnType(enclosingMethod, node);
                }
            } else {
                ret = atypeFactory.getFnInterfaceFromTree((LambdaExpressionTree) enclosing).second.getReturnType();
            }
            if (ret != null) {
                visitorState.setAssignmentContext(Pair.of((Tree) node, ret));
                lambdaAssignmentCheck(ret, (LambdaExpressionTree) node.getExpression(), "return.type.incompatible");
            }
        } finally {
            visitorState.setAssignmentContext(preAssCtxt);
        }
    }
    return result;
}
Also used : LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) MethodTree(com.sun.source.tree.MethodTree) ReturnTree(com.sun.source.tree.ReturnTree) MethodTree(com.sun.source.tree.MethodTree) VariableTree(com.sun.source.tree.VariableTree) MethodInvocationTree(com.sun.source.tree.MethodInvocationTree) AssignmentTree(com.sun.source.tree.AssignmentTree) LambdaExpressionTree(com.sun.source.tree.LambdaExpressionTree) Tree(com.sun.source.tree.Tree) ExpressionTree(com.sun.source.tree.ExpressionTree) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 89 with AnnotatedTypeMirror

use of org.checkerframework.framework.type.AnnotatedTypeMirror in project checker-framework by typetools.

the class GuiEffectVisitor method lambdaAssignmentCheck.

// For assignments and local variable initialization:
// Check for @UI annotations on the lhs, use them to infer @UI types on lambda expressions in the rhs
private void lambdaAssignmentCheck(AnnotatedTypeMirror varType, LambdaExpressionTree lambdaExp, @CompilerMessageKey String errorKey) {
    AnnotatedTypeMirror lambdaType = atypeFactory.getAnnotatedType(lambdaExp);
    assert lambdaType != null : "null type for expression: " + lambdaExp;
    commonAssignmentCheck(varType, lambdaType, lambdaExp, errorKey);
}
Also used : AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 90 with AnnotatedTypeMirror

use of org.checkerframework.framework.type.AnnotatedTypeMirror in project checker-framework by typetools.

the class IndexUtil method getMinValue.

/**
 * Finds the minimum value in a Value Checker type. If there is no information (such as when the
 * list of possible values is empty or null), returns null. Otherwise, returns the smallest
 * value in the list of possible values.
 */
public static Long getMinValue(Tree tree, ValueAnnotatedTypeFactory factory) {
    AnnotatedTypeMirror valueType = factory.getAnnotatedType(tree);
    Range possibleValues = getPossibleValues(valueType, factory);
    if (possibleValues != null) {
        return possibleValues.from;
    } else {
        return null;
    }
}
Also used : Range(org.checkerframework.common.value.util.Range) IntRange(org.checkerframework.common.value.qual.IntRange) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Aggregations

AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)188 AnnotationMirror (javax.lang.model.element.AnnotationMirror)42 ExpressionTree (com.sun.source.tree.ExpressionTree)32 AnnotatedDeclaredType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedDeclaredType)27 Tree (com.sun.source.tree.Tree)25 AnnotatedTypeVariable (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)25 VariableTree (com.sun.source.tree.VariableTree)22 ArrayList (java.util.ArrayList)22 AnnotatedExecutableType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedExecutableType)21 MethodTree (com.sun.source.tree.MethodTree)20 TypeVariable (javax.lang.model.type.TypeVariable)19 MethodInvocationTree (com.sun.source.tree.MethodInvocationTree)18 AnnotatedArrayType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedArrayType)17 LambdaExpressionTree (com.sun.source.tree.LambdaExpressionTree)16 TypeMirror (javax.lang.model.type.TypeMirror)16 VariableElement (javax.lang.model.element.VariableElement)15 ConditionalExpressionTree (com.sun.source.tree.ConditionalExpressionTree)13 MemberSelectTree (com.sun.source.tree.MemberSelectTree)13 NewClassTree (com.sun.source.tree.NewClassTree)13 Element (javax.lang.model.element.Element)13