Search in sources :

Example 1 with ASTNode

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);
}
Also used : LocationTypeVar(org.abs_models.frontend.typechecker.locationtypes.LocationTypeVar) java.util(java.util) SemanticConditionList(org.abs_models.frontend.analyser.SemanticConditionList) TypeError(org.abs_models.frontend.analyser.TypeError) AdaptDirection(org.abs_models.frontend.typechecker.ext.AdaptDirection) ASTNode(org.abs_models.frontend.ast.ASTNode) ErrorMessage(org.abs_models.frontend.analyser.ErrorMessage) TypeError(org.abs_models.frontend.analyser.TypeError) LocationTypeVar(org.abs_models.frontend.typechecker.locationtypes.LocationTypeVar)

Example 2 with ASTNode

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;
}
Also used : ASTNode(org.abs_models.frontend.ast.ASTNode)

Aggregations

ASTNode (org.abs_models.frontend.ast.ASTNode)2 java.util (java.util)1 ErrorMessage (org.abs_models.frontend.analyser.ErrorMessage)1 SemanticConditionList (org.abs_models.frontend.analyser.SemanticConditionList)1 TypeError (org.abs_models.frontend.analyser.TypeError)1 AdaptDirection (org.abs_models.frontend.typechecker.ext.AdaptDirection)1 LocationTypeVar (org.abs_models.frontend.typechecker.locationtypes.LocationTypeVar)1