use of org.abs_models.frontend.ast.ASTNode in project abstools by abstools.
the class ConstraintSolver method resolveSub.
/**
* Resolve one sub constrained
* @param resolved - Already resolved vars and their type
* @param sub - The constraint to resolve
*/
private void resolveSub(Map<LocationTypeVar, LocationType> resolved, Constraint.Sub sub) {
LocationTypeVar expected = getRewritten(sub.expected);
LocationTypeVar actual = getRewritten(sub.actual);
ASTNode<?> node = sub.node;
if (expected == actual) {
return;
}
if (expected.isLocationType() && actual.isLocationType()) {
if (!expected.asLocationType().isSubtypeOf(actual.asLocationType())) {
errors.add(new LocationTypeInferException(new TypeError(node, ErrorMessage.LOCATION_TYPE_CANNOT_ASSIGN, expected.toString(), actual.toString())));
}
return;
}
if (actual == BOTTOM) {
throw new IllegalArgumentException("Cannot resolve constraint " + expected + " <: BOTTOM");
}
if (actual == NEAR) {
add(Constraint.eq(expected, NEAR, node));
return;
}
if (actual == FAR) {
add(Constraint.eq(expected, FAR, node));
return;
}
if (actual == SOMEWHERE) {
// Useless
return;
}
if (expected == SOMEWHERE) {
add(Constraint.eq(actual, SOMEWHERE, node));
return;
}
if (expected == BOTTOM) {
// Useless
return;
}
List<Constraint> expectedCs = constraints.get(expected);
List<Constraint> actualCs = constraints.get(actual);
if (expectedCs.size() <= 1 && actualCs.size() <= 1) {
add(Constraint.eq(expected, actual, node));
return;
}
if (actualCs.stream().filter(c -> c.isSub() && ((Constraint.Sub) c).actual == actual).count() == 1) {
add(Constraint.eq(expected, actual, node));
return;
}
if (expected == NEAR && actualCs.stream().anyMatch(c -> c.isSub() && ((Constraint.Sub) c).expected == FAR)) {
add(Constraint.eq(actual, SOMEWHERE, node));
return;
}
// Search for reverse (actual <: expected)
// Search in smaller list
List<Constraint> toSearch = expectedCs.size() < actualCs.size() ? expectedCs : actualCs;
if (toSearch.stream().anyMatch(c -> c.isSub() && ((Constraint.Sub) c).expected == actual && ((Constraint.Sub) c).actual == expected)) {
add(Constraint.eq(expected, actual, node));
return;
}
keep(sub);
}
use of org.abs_models.frontend.ast.ASTNode in project abstools by abstools.
the class SourcePosition method findPosition.
public static SourcePosition findPosition(ASTNode<?> node, int searchline, int searchcolumn) {
if (inNode(node, searchline, searchcolumn)) {
for (int i = 0; i < node.getNumChildNoTransform(); i++) {
ASTNode child = node.getChildNoTransform(i);
SourcePosition pos = findPosition(child, searchline, searchcolumn);
if (pos != null) {
return pos;
}
}
return new SourcePosition(node, searchline, searchcolumn);
}
return null;
}
Aggregations