Search in sources :

Example 1 with F2A

use of org.checkerframework.framework.util.typeinference.constraint.F2A in project checker-framework by typetools.

the class DefaultTypeArgumentInference method createInitialAssignmentConstraints.

/**
 * Create a set of constraints between return type and any type to which it is assigned. Reduce
 * these set of constraints and remove any that is not an equality (FIsA) constraint.
 */
protected Set<FIsA> createInitialAssignmentConstraints(final AnnotatedTypeMirror assignedTo, final AnnotatedTypeMirror boxedReturnType, final AnnotatedTypeFactory typeFactory, final Set<TypeVariable> targets) {
    final Set<FIsA> result = new LinkedHashSet<>();
    if (assignedTo != null) {
        final Set<AFConstraint> reducedConstraints = new LinkedHashSet<>();
        final Queue<AFConstraint> constraints = new ArrayDeque<>();
        constraints.add(new F2A(boxedReturnType, assignedTo));
        reduceAfConstraints(typeFactory, reducedConstraints, constraints, targets);
        for (final AFConstraint reducedConstraint : reducedConstraints) {
            if (reducedConstraint instanceof FIsA) {
                result.add((FIsA) reducedConstraint);
            }
        }
    }
    return result;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) FIsA(org.checkerframework.framework.util.typeinference.constraint.FIsA) F2A(org.checkerframework.framework.util.typeinference.constraint.F2A) AFConstraint(org.checkerframework.framework.util.typeinference.constraint.AFConstraint) ArrayDeque(java.util.ArrayDeque)

Example 2 with F2A

use of org.checkerframework.framework.util.typeinference.constraint.F2A in project checker-framework by typetools.

the class DefaultTypeArgumentInference method createAssignmentConstraints.

/**
 * The first half of Step 6.
 *
 * <p>This method creates constraints:
 *
 * <ul>
 *   <li>between the bounds of types that are already inferred and their inferred arguments
 *   <li>between the assignment context and the return type of the method (with the previously
 *       inferred arguments substituted into these constraints)
 * </ul>
 */
public ConstraintMap createAssignmentConstraints(final AnnotatedTypeMirror assignedTo, final AnnotatedTypeMirror boxedReturnType, final AnnotatedExecutableType methodType, final Set<AFConstraint> afArgumentConstraints, final Map<TypeVariable, AnnotatedTypeMirror> inferredArgs, final Set<TypeVariable> targets, final AnnotatedTypeFactory typeFactory) {
    final ArrayDeque<AFConstraint> assignmentAfs = new ArrayDeque<>(2 * methodType.getTypeVariables().size() + afArgumentConstraints.size());
    for (AnnotatedTypeVariable typeParam : methodType.getTypeVariables()) {
        final TypeVariable target = typeParam.getUnderlyingType();
        final AnnotatedTypeMirror inferredType = inferredArgs.get(target);
        // the lower bound for all uninferred types Tu: Tu >> Bi and Lu >> Tu
        if (inferredType != null) {
            assignmentAfs.add(new A2F(inferredType, typeParam.getUpperBound()));
            assignmentAfs.add(new F2A(typeParam.getLowerBound(), inferredType));
        } else {
            assignmentAfs.add(new F2A(typeParam, typeParam.getUpperBound()));
            assignmentAfs.add(new A2F(typeParam.getLowerBound(), typeParam));
        }
    }
    for (AFConstraint argConstraint : afArgumentConstraints) {
        if (argConstraint instanceof F2A) {
            assignmentAfs.add(argConstraint);
        }
    }
    ArrayDeque<AFConstraint> substitutedAssignmentConstraints = new ArrayDeque<>(assignmentAfs.size() + 1);
    for (AFConstraint afConstraint : assignmentAfs) {
        substitutedAssignmentConstraints.add(afConstraint.substitute(inferredArgs));
    }
    final AnnotatedTypeMirror substitutedReturnType = TypeArgInferenceUtil.substitute(inferredArgs, boxedReturnType);
    substitutedAssignmentConstraints.add(new F2A(substitutedReturnType, assignedTo));
    final Set<AFConstraint> reducedConstraints = new LinkedHashSet<>();
    reduceAfConstraints(typeFactory, reducedConstraints, substitutedAssignmentConstraints, targets);
    final Set<TUConstraint> tuAssignmentConstraints = afToTuConstraints(reducedConstraints, targets);
    addConstraintsBetweenTargets(tuAssignmentConstraints, targets, true, typeFactory);
    return constraintMapBuilder.build(targets, tuAssignmentConstraints, typeFactory);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) F2A(org.checkerframework.framework.util.typeinference.constraint.F2A) TypeVariable(javax.lang.model.type.TypeVariable) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable) A2F(org.checkerframework.framework.util.typeinference.constraint.A2F) TUConstraint(org.checkerframework.framework.util.typeinference.constraint.TUConstraint) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) AFConstraint(org.checkerframework.framework.util.typeinference.constraint.AFConstraint) ArrayDeque(java.util.ArrayDeque)

Aggregations

ArrayDeque (java.util.ArrayDeque)2 LinkedHashSet (java.util.LinkedHashSet)2 AFConstraint (org.checkerframework.framework.util.typeinference.constraint.AFConstraint)2 F2A (org.checkerframework.framework.util.typeinference.constraint.F2A)2 TypeVariable (javax.lang.model.type.TypeVariable)1 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)1 AnnotatedTypeVariable (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)1 A2F (org.checkerframework.framework.util.typeinference.constraint.A2F)1 FIsA (org.checkerframework.framework.util.typeinference.constraint.FIsA)1 TUConstraint (org.checkerframework.framework.util.typeinference.constraint.TUConstraint)1