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;
}
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;
}
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());
}
}
}
}
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;
}
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);
}
}
}
}
}
Aggregations