Search in sources :

Example 6 with AnnotationMirrorSet

use of org.checkerframework.framework.util.AnnotationMirrorSet in project checker-framework by typetools.

the class EqualitiesSolver method mergeConstraints.

public InferredValue mergeConstraints(final TypeVariable target, final Equalities equalities, final InferenceResult solution, ConstraintMap constraintMap, AnnotatedTypeFactory typeFactory) {
    final AnnotationMirrorSet tops = new AnnotationMirrorSet(typeFactory.getQualifierHierarchy().getTopAnnotations());
    InferredValue inferred = null;
    if (!equalities.types.isEmpty()) {
        inferred = mergeTypesAndPrimaries(equalities.types, equalities.primaries, tops);
    }
    if (inferred != null) {
        return inferred;
    }
    // else
    // We did not have enough information to infer an annotation in all hierarchies for one
    // concrete type.
    // However, we have a "partial solution", one in which we know the type in some but not all
    // qualifier hierarchies.
    // Update our set of constraints with this information
    dirty |= updateTargetsWithPartiallyInferredType(equalities, constraintMap, typeFactory);
    inferred = findEqualTarget(equalities, tops);
    if (inferred == null && equalities.types.size() == 1) {
        // Still could not find an inferred type in all hierarchies, so just use what type is
        // known.
        AnnotatedTypeMirror type = equalities.types.keySet().iterator().next();
        inferred = new InferredType(type);
    }
    return inferred;
}
Also used : InferredType(org.checkerframework.framework.util.typeinference.solver.InferredValue.InferredType) AnnotationMirrorSet(org.checkerframework.framework.util.AnnotationMirrorSet) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 7 with AnnotationMirrorSet

use of org.checkerframework.framework.util.AnnotationMirrorSet in project checker-framework by typetools.

the class EqualitiesSolver method findEqualTarget.

/**
 * Attempt to find a target which is equal to this target.
 *
 * @return a target equal to this target in all hierarchies, or null
 */
public InferredTarget findEqualTarget(final Equalities equalities, AnnotationMirrorSet tops) {
    for (Map.Entry<TypeVariable, AnnotationMirrorSet> targetToHierarchies : equalities.targets.entrySet()) {
        final TypeVariable equalTarget = targetToHierarchies.getKey();
        final AnnotationMirrorSet hierarchies = targetToHierarchies.getValue();
        // Now see if target is equal to equalTarget in all hierarchies
        boolean targetIsEqualInAllHierarchies = hierarchies.size() == tops.size();
        if (targetIsEqualInAllHierarchies) {
            return new InferredTarget(equalTarget, new AnnotationMirrorSet());
        } else {
            // annos in primaries that are not covered by the target's list of equal hierarchies
            final AnnotationMirrorSet requiredPrimaries = new AnnotationMirrorSet(equalities.primaries.keySet());
            requiredPrimaries.removeAll(hierarchies);
            boolean typeWithPrimariesIsEqual = (requiredPrimaries.size() + hierarchies.size()) == tops.size();
            if (typeWithPrimariesIsEqual) {
                return new InferredTarget(equalTarget, requiredPrimaries);
            }
        }
    }
    return null;
}
Also used : InferredTarget(org.checkerframework.framework.util.typeinference.solver.InferredValue.InferredTarget) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable) TypeVariable(javax.lang.model.type.TypeVariable) AnnotationMirrorSet(org.checkerframework.framework.util.AnnotationMirrorSet) AnnotationMirrorMap(org.checkerframework.framework.util.AnnotationMirrorMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 8 with AnnotationMirrorSet

use of org.checkerframework.framework.util.AnnotationMirrorSet in project checker-framework by typetools.

the class SubtypesSolver method propagatePreviousGlbs.

/**
 * /** If the target corresponding to targetRecord must be a subtype of another target for which
 * we have already determined a GLB, add that target's GLB to the list of subtypes to be GLBed
 * for this target.
 */
protected static void propagatePreviousGlbs(final Subtypes targetSubtypes, InferenceResult solution, final Map<AnnotatedTypeMirror, AnnotationMirrorSet> subtypesOfTarget) {
    for (final Entry<TypeVariable, AnnotationMirrorSet> subtypeTarget : targetSubtypes.targets.entrySet()) {
        final InferredValue subtargetInferredGlb = solution.get(subtypeTarget.getKey());
        if (subtargetInferredGlb != null) {
            final AnnotatedTypeMirror subtargetGlbType = ((InferredType) subtargetInferredGlb).type;
            AnnotationMirrorSet subtargetAnnos = subtypesOfTarget.get(subtargetGlbType);
            if (subtargetAnnos != null) {
                // there is already an equivalent type in the list of subtypes, just add
                // any hierarchies that are not in its list but are in the supertarget's list
                subtargetAnnos.addAll(subtypeTarget.getValue());
            } else {
                subtypesOfTarget.put(subtargetGlbType, subtypeTarget.getValue());
            }
        }
    }
}
Also used : InferredType(org.checkerframework.framework.util.typeinference.solver.InferredValue.InferredType) TypeVariable(javax.lang.model.type.TypeVariable) AnnotationMirrorSet(org.checkerframework.framework.util.AnnotationMirrorSet) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Example 9 with AnnotationMirrorSet

use of org.checkerframework.framework.util.AnnotationMirrorSet in project checker-framework by typetools.

the class SubtypesSolver method glbSubtypes.

public InferenceResult glbSubtypes(final Set<TypeVariable> remainingTargets, final ConstraintMap constraints, final AnnotatedTypeFactory typeFactory) {
    final InferenceResult inferenceResult = new InferenceResult();
    final QualifierHierarchy qualifierHierarchy = typeFactory.getQualifierHierarchy();
    final Types types = typeFactory.getProcessingEnv().getTypeUtils();
    List<TypeVariable> targetsSubtypesLast = new ArrayList<>(remainingTargets);
    // If we have two type variables <A, A extends B> order them A then B
    // this is required because we will use the fact that B must be below A
    // when determining the glb of B
    Collections.sort(targetsSubtypesLast, new Comparator<TypeVariable>() {

        @Override
        public int compare(TypeVariable o1, TypeVariable o2) {
            if (types.isSubtype(o1, o2)) {
                return 1;
            } else if (types.isSubtype(o2, o1)) {
                return -1;
            }
            return 0;
        }
    });
    for (final TypeVariable target : targetsSubtypesLast) {
        Subtypes subtypes = constraints.getConstraints(target).subtypes;
        if (subtypes.types.isEmpty()) {
            continue;
        }
        propagatePreviousGlbs(subtypes, inferenceResult, subtypes.types);
        // if the subtypes size is only 1 then we need not do any GLBing on the underlying types
        // but we may have primary annotations that need to be GLBed
        AnnotationMirrorMap<AnnotationMirrorSet> primaries = subtypes.primaries;
        if (subtypes.types.size() == 1) {
            final Entry<AnnotatedTypeMirror, AnnotationMirrorSet> entry = subtypes.types.entrySet().iterator().next();
            AnnotatedTypeMirror supertype = entry.getKey().deepCopy();
            for (AnnotationMirror top : entry.getValue()) {
                final AnnotationMirrorSet superAnnos = primaries.get(top);
                // if it is null we're just going to use the anno already on supertype
                if (superAnnos != null) {
                    final AnnotationMirror supertypeAnno = supertype.getAnnotationInHierarchy(top);
                    superAnnos.add(supertypeAnno);
                }
            }
            if (!primaries.isEmpty()) {
                for (AnnotationMirror top : qualifierHierarchy.getTopAnnotations()) {
                    final AnnotationMirror glb = greatestLowerBound(subtypes.primaries.get(top), qualifierHierarchy);
                    supertype.replaceAnnotation(glb);
                }
            }
            inferenceResult.put(target, new InferredType(supertype));
        } else {
            // GLB all of the types than combine this with the GLB of primary annotation
            // constraints
            final AnnotatedTypeMirror glbType = GlbUtil.glbAll(subtypes.types, typeFactory);
            if (glbType != null) {
                if (!primaries.isEmpty()) {
                    for (AnnotationMirror top : qualifierHierarchy.getTopAnnotations()) {
                        final AnnotationMirror glb = greatestLowerBound(subtypes.primaries.get(top), qualifierHierarchy);
                        final AnnotationMirror currentAnno = glbType.getAnnotationInHierarchy(top);
                        if (currentAnno == null) {
                            glbType.addAnnotation(glb);
                        } else if (glb != null) {
                            glbType.replaceAnnotation(qualifierHierarchy.greatestLowerBound(glb, currentAnno));
                        }
                    }
                }
                inferenceResult.put(target, new InferredType(glbType));
            }
        }
    }
    return inferenceResult;
}
Also used : Types(javax.lang.model.util.Types) InferredType(org.checkerframework.framework.util.typeinference.solver.InferredValue.InferredType) ArrayList(java.util.ArrayList) Subtypes(org.checkerframework.framework.util.typeinference.solver.TargetConstraints.Subtypes) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror) AnnotationMirror(javax.lang.model.element.AnnotationMirror) TypeVariable(javax.lang.model.type.TypeVariable) QualifierHierarchy(org.checkerframework.framework.type.QualifierHierarchy) AnnotationMirrorSet(org.checkerframework.framework.util.AnnotationMirrorSet)

Example 10 with AnnotationMirrorSet

use of org.checkerframework.framework.util.AnnotationMirrorSet in project checker-framework by typetools.

the class DefaultTypeArgumentInference method clampToLowerBound.

/**
 * If we have inferred a type argument from the supertype constraints and this type argument is
 * BELOW the lower bound, make it AT the lower bound
 *
 * <p>e.g.
 *
 * <pre>{@code
 * <@Initialized T extends @Initialized Object> void id(T t) { return t; }
 * id(null);
 *
 * // The invocation of id will result in a type argument with primary annotations of @FBCBottom @Nullable
 * // but this is below the lower bound of T in the initialization hierarchy so instead replace
 * //@FBCBottom with @Initialized
 *
 * // This should happen ONLY with supertype constraints because raising the primary annotation would still
 * // be valid for these constraints (since we just LUB the arguments involved) but would violate any
 * // equality constraints
 * }</pre>
 *
 * TODO: NOTE WE ONLY DO THIS FOR InferredType results for now but we should probably include
 * targest as well
 *
 * @param fromArgSupertypes types inferred from LUBbing types from the arguments to the formal
 *     parameters
 * @param targetDeclarations the declared types of the type parameters whose arguments are being
 *     inferred
 */
private void clampToLowerBound(InferenceResult fromArgSupertypes, List<AnnotatedTypeVariable> targetDeclarations, AnnotatedTypeFactory typeFactory) {
    final QualifierHierarchy qualifierHierarchy = typeFactory.getQualifierHierarchy();
    final AnnotationMirrorSet tops = new AnnotationMirrorSet(qualifierHierarchy.getTopAnnotations());
    for (AnnotatedTypeVariable targetDecl : targetDeclarations) {
        InferredValue inferred = fromArgSupertypes.get(targetDecl.getUnderlyingType());
        if (inferred != null && inferred instanceof InferredType) {
            final AnnotatedTypeMirror lowerBoundAsArgument = targetDecl.getLowerBound();
            for (AnnotationMirror top : tops) {
                final AnnotationMirror lowerBoundAnno = lowerBoundAsArgument.getEffectiveAnnotationInHierarchy(top);
                final AnnotationMirror argAnno = ((InferredType) inferred).type.getEffectiveAnnotationInHierarchy(top);
                if (qualifierHierarchy.isSubtype(argAnno, lowerBoundAnno)) {
                    ((InferredType) inferred).type.replaceAnnotation(lowerBoundAnno);
                }
            }
        }
    }
}
Also used : AnnotationMirror(javax.lang.model.element.AnnotationMirror) InferredValue(org.checkerframework.framework.util.typeinference.solver.InferredValue) InferredType(org.checkerframework.framework.util.typeinference.solver.InferredValue.InferredType) QualifierHierarchy(org.checkerframework.framework.type.QualifierHierarchy) AnnotationMirrorSet(org.checkerframework.framework.util.AnnotationMirrorSet) AnnotatedTypeVariable(org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable) AnnotatedTypeMirror(org.checkerframework.framework.type.AnnotatedTypeMirror)

Aggregations

AnnotationMirrorSet (org.checkerframework.framework.util.AnnotationMirrorSet)24 AnnotatedTypeMirror (org.checkerframework.framework.type.AnnotatedTypeMirror)15 AnnotationMirror (javax.lang.model.element.AnnotationMirror)12 TypeVariable (javax.lang.model.type.TypeVariable)8 AnnotatedTypeVariable (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedTypeVariable)8 InferredType (org.checkerframework.framework.util.typeinference.solver.InferredValue.InferredType)7 QualifierHierarchy (org.checkerframework.framework.type.QualifierHierarchy)6 LinkedHashMap (java.util.LinkedHashMap)4 AnnotationMirrorMap (org.checkerframework.framework.util.AnnotationMirrorMap)4 Equalities (org.checkerframework.framework.util.typeinference.solver.TargetConstraints.Equalities)4 Subtypes (org.checkerframework.framework.util.typeinference.solver.TargetConstraints.Subtypes)4 ArrayList (java.util.ArrayList)3 Supertypes (org.checkerframework.framework.util.typeinference.solver.TargetConstraints.Supertypes)3 Map (java.util.Map)2 Entry (java.util.Map.Entry)2 Types (javax.lang.model.util.Types)2 AnnotatedPrimitiveType (org.checkerframework.framework.type.AnnotatedTypeMirror.AnnotatedPrimitiveType)1 TypeHierarchy (org.checkerframework.framework.type.TypeHierarchy)1 AnnotatedTypes (org.checkerframework.framework.util.AnnotatedTypes)1 TUConstraint (org.checkerframework.framework.util.typeinference.constraint.TUConstraint)1