Search in sources :

Example 1 with A2F

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

the class DefaultTypeArgumentInference method createArgumentAFConstraints.

/**
 * Step 1: Create a constraint {@code Ai << Fi} for each Argument(Ai) to formal parameter(Fi).
 * Remove any constraint that does not involve a type parameter to be inferred. Reduce the
 * remaining constraints so that Fi = Tj where Tj is a type parameter with an argument to be
 * inferred. Return the resulting constraint set.
 *
 * @param typeFactory AnnotatedTypeFactory
 * @param argTypes list of annotated types corresponding to the arguments to the method
 * @param methodType annotated type of the method
 * @param targets type variables to be inferred
 * @param useNullArguments whether or not null method arguments should be considered
 * @return a set of argument constraints
 */
protected Set<AFConstraint> createArgumentAFConstraints(final AnnotatedTypeFactory typeFactory, final List<AnnotatedTypeMirror> argTypes, final AnnotatedExecutableType methodType, final Set<TypeVariable> targets, boolean useNullArguments) {
    final List<AnnotatedTypeMirror> paramTypes = AnnotatedTypes.expandVarArgsFromTypes(methodType, argTypes);
    if (argTypes.size() != paramTypes.size()) {
        ErrorReporter.errorAbort("Mismatch between formal parameter count and argument count!\n" + "paramTypes=" + PluginUtil.join(",", paramTypes) + "\n" + "argTypes=" + PluginUtil.join(",", argTypes));
    }
    final int numberOfParams = paramTypes.size();
    final ArrayDeque<AFConstraint> afConstraints = new ArrayDeque<>(numberOfParams);
    for (int i = 0; i < numberOfParams; i++) {
        if (!useNullArguments && argTypes.get(i).getKind() == TypeKind.NULL) {
            continue;
        }
        afConstraints.add(new A2F(argTypes.get(i), paramTypes.get(i)));
    }
    final Set<AFConstraint> reducedConstraints = new LinkedHashSet<>();
    reduceAfConstraints(typeFactory, reducedConstraints, afConstraints, targets);
    return reducedConstraints;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) A2F(org.checkerframework.framework.util.typeinference.constraint.A2F) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) AFConstraint(org.checkerframework.framework.util.typeinference.constraint.AFConstraint) TUConstraint(org.checkerframework.framework.util.typeinference.constraint.TUConstraint) AFConstraint(org.checkerframework.framework.util.typeinference.constraint.AFConstraint) ArrayDeque(java.util.ArrayDeque)

Example 2 with A2F

use of org.checkerframework.framework.util.typeinference.constraint.A2F 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 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)2 A2F (org.checkerframework.framework.util.typeinference.constraint.A2F)2 AFConstraint (org.checkerframework.framework.util.typeinference.constraint.AFConstraint)2 TUConstraint (org.checkerframework.framework.util.typeinference.constraint.TUConstraint)2 TypeVariable (javax.lang.model.type.TypeVariable)1 AnnotatedTypeVariable (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)1 F2A (org.checkerframework.framework.util.typeinference.constraint.F2A)1