Search in sources :

Example 1 with TypeError

use of org.abs_models.frontend.analyser.TypeError in project abstools by abstools.

the class DeltaSamplesTest method test_ticket329_missingLineNo.

@Test
public void test_ticket329_missingLineNo() throws Exception {
    Model m = assertParseFileOk("abssamples/deltas/bug329.abs");
    SemanticConditionList errs = m.typeCheck();
    /* We are expecting a missing delta in product M.PL: */
    assertThat(errs.getFirstError(), instanceOf(TypeError.class));
    TypeError te = (TypeError) errs.getFirstError();
    Assert.assertEquals(ErrorMessage.NAME_NOT_RESOLVABLE, te.msg);
    Assert.assertEquals(10, te.getLine());
}
Also used : SemanticConditionList(org.abs_models.frontend.analyser.SemanticConditionList) Model(org.abs_models.frontend.ast.Model) TypeError(org.abs_models.frontend.analyser.TypeError) FrontendTest(org.abs_models.frontend.FrontendTest) Test(org.junit.Test)

Example 2 with TypeError

use of org.abs_models.frontend.analyser.TypeError 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 3 with TypeError

use of org.abs_models.frontend.analyser.TypeError in project abstools by abstools.

the class NullCheckerExtension method checkClassDecl.

@Override
public void checkClassDecl(ClassDecl decl) {
    for (ParamDecl p : decl.getParams()) {
        setAnnotatedType(p.getType(), p);
    }
    List<FieldDecl> nonNullFields = new ArrayList<>();
    for (FieldDecl f : decl.getFields()) {
        setAnnotatedType(f.getType(), f);
        if (f.nonnull() && !f.hasInitExp()) {
            nonNullFields.add(f);
        }
    }
    if (nonNullFields.isEmpty())
        return;
    if (!decl.hasInitBlock()) {
        errors.add(new TypeError(nonNullFields.get(0), ErrorMessage.NULLABLE_TYPE_MISMATCH, NullableType.Nonnull.toString(), NullableType.Nullable.toString()));
        return;
    }
    // Get all fields that are nonNull at the end of the init block
    BitVec<VarOrFieldDecl> out = decl.getInitBlock().exit().nonnull_in();
    for (FieldDecl f : nonNullFields) {
        if (!out.contains(f)) {
            errors.add(new TypeError(f, ErrorMessage.NULLABLE_TYPE_MISMATCH, NullableType.Nonnull.toString(), NullableType.Nullable.toString()));
        }
    }
}
Also used : ArrayList(java.util.ArrayList) TypeError(org.abs_models.frontend.analyser.TypeError)

Example 4 with TypeError

use of org.abs_models.frontend.analyser.TypeError in project abstools by abstools.

the class NullCheckerExtension method getNullableTypeFromAnnotation.

public static NullableType getNullableTypeFromAnnotation(Type t) {
    NullableType res = null;
    for (TypeAnnotation an : t.getTypeAnnotations()) {
        if (an.getType().getQualifiedName().equals("ABS.StdLib.NullableType")) {
            DataConstructorExp de = (DataConstructorExp) an.getValue();
            String name = de.getDecl().getName();
            if (res != null) {
                throw new NullCheckerException(new TypeError(an.getValue(), ErrorMessage.NULLABLE_TYPE_MULTIPLE, new String[0]));
            } else {
                if (!shouldHaveNullableType(t)) {
                    throw new NullCheckerException(new TypeError(an.getValue(), ErrorMessage.NULLABLE_TYPE_ONLY_REF_OR_FUT, t.toString()));
                }
                res = NullableType.fromName(name);
            }
        }
    }
    return res;
}
Also used : TypeAnnotation(org.abs_models.frontend.typechecker.TypeAnnotation) TypeError(org.abs_models.frontend.analyser.TypeError)

Example 5 with TypeError

use of org.abs_models.frontend.analyser.TypeError in project abstools by abstools.

the class LocationTypeExtension method getLocationTypeFromAnnotations.

public static LocationType getLocationTypeFromAnnotations(Type t) {
    LocationType res = null;
    for (TypeAnnotation an : t.getTypeAnnotations()) {
        if (an.getType().getQualifiedName().equals("ABS.StdLib.LocationType")) {
            DataConstructorExp de = (DataConstructorExp) an.getValue();
            String name = de.getDecl().getName();
            if (res != null) {
                throw new LocationTypeCheckerException(new TypeError(an.getValue(), ErrorMessage.LOCATION_TYPE_MULTIPLE, new String[0]));
            } else {
                res = LocationType.createFromName(name);
            }
        }
    }
    return res;
}
Also used : TypeAnnotation(org.abs_models.frontend.typechecker.TypeAnnotation) TypeError(org.abs_models.frontend.analyser.TypeError)

Aggregations

TypeError (org.abs_models.frontend.analyser.TypeError)25 SemanticConditionList (org.abs_models.frontend.analyser.SemanticConditionList)3 Model (org.abs_models.frontend.ast.Model)3 LocationTypeVar (org.abs_models.frontend.typechecker.locationtypes.LocationTypeVar)3 java.util (java.util)2 HashSet (java.util.HashSet)2 FrontendTest (org.abs_models.frontend.FrontendTest)2 ErrorMessage (org.abs_models.frontend.analyser.ErrorMessage)2 SemanticWarning (org.abs_models.frontend.analyser.SemanticWarning)2 org.abs_models.frontend.ast (org.abs_models.frontend.ast)2 FieldDecl (org.abs_models.frontend.ast.FieldDecl)2 PureExp (org.abs_models.frontend.ast.PureExp)2 Type (org.abs_models.frontend.typechecker.Type)2 TypeAnnotation (org.abs_models.frontend.typechecker.TypeAnnotation)2 AdaptDirection (org.abs_models.frontend.typechecker.ext.AdaptDirection)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 SemanticError (org.abs_models.frontend.analyser.SemanticError)1