Search in sources :

Example 16 with TypeError

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

the class TypeCheckerHelper method typeCheckProductDecl.

public static void typeCheckProductDecl(ProductDecl prod, Map<String, Feature> featureNames, Set<String> prodNames, Map<String, DeltaDecl> deltaNames, SemanticConditionList e) {
    if (featureNames != null) {
        // Do the features exist in the PL declaration (and also check feature attributes)?
        Model m = prod.getModel();
        for (Feature f : prod.getProduct().getFeatures()) {
            if (!featureNames.containsKey(f.getName()))
                e.add(new TypeError(prod, ErrorMessage.NAME_NOT_RESOLVABLE, f.getName()));
            else {
                Collection<DeltaClause> dcs = findDeltasForFeature(m, f);
                for (int i = 0; i < f.getNumAttrAssignment(); i++) {
                    AttrAssignment aa = f.getAttrAssignment(i);
                    for (DeltaClause dc : dcs) {
                        DeltaDecl dd = m.findDelta(dc.getDeltaspec().getDeltaID());
                        DeltaParamDecl dp = dd.getParam(i);
                        // not used by this delta).
                        if (dp != null && !dp.accepts(aa.getValue())) {
                            e.add(new TypeError(aa, ErrorMessage.CANNOT_ASSIGN, aa.getValue().getName(), dp.getType().getSimpleName()));
                        }
                    }
                }
            }
        }
    }
    // Check the right side of product expression that contains in prodNames
    Set<String> productNames = new HashSet<>();
    prod.getProductExpr().setRightSideProductNames(productNames);
    for (String productName : productNames) {
        if (!prodNames.contains(productName)) {
            e.add(new TypeError(prod, ErrorMessage.UNDECLARED_PRODUCT, productName));
        }
    }
    // Check solution from getProduct()
    if (prod.getProduct() != null) {
        java.util.List<String> errors = prod.getModel().instantiateCSModel().checkSolutionWithErrors(prod.getProduct().getSolution(), prod.getModel());
        if (!errors.isEmpty()) {
            String failedConstraints = "";
            for (String s : errors) failedConstraints += "\n- " + s;
            e.add(new TypeError(prod, ErrorMessage.INVALID_PRODUCT, prod.getName(), failedConstraints));
        }
    }
}
Also used : java.util(java.util) TypeError(org.abs_models.frontend.analyser.TypeError)

Example 17 with TypeError

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

the class TypeCheckerHelper method checkForDuplicateModulesAndDeltas.

/**
 * check a list of compilation units for duplicate module names, product names, delta names
 */
public static void checkForDuplicateModulesAndDeltas(SemanticConditionList errors, Iterable<CompilationUnit> compilationUnits) {
    Map<String, ModuleDecl> seenModules = new HashMap<>();
    Map<String, DeltaDecl> seenDeltas = new HashMap<>();
    for (CompilationUnit u : compilationUnits) {
        for (ModuleDecl module : u.getModuleDecls()) {
            if (seenModules.containsKey(module.getName())) {
                ModuleDecl prev = seenModules.get(module.getName());
                String location = "";
                if (!prev.getFileName().equals(Main.UNKNOWN_FILENAME)) {
                    location = " at " + prev.getFileName() + ":" + prev.getStartLine() + ":" + prev.getStartColumn();
                }
                errors.add(new TypeError(module, ErrorMessage.DUPLICATE_MODULE_NAME, module.getName(), location));
            } else {
                seenModules.put(module.getName(), module);
            }
        }
        for (DeltaDecl d : u.getDeltaDecls()) {
            if (seenModules.containsKey(d.getName())) {
                ModuleDecl prev = seenModules.get(d.getName());
                String location = "";
                if (!prev.getFileName().equals(Main.UNKNOWN_FILENAME)) {
                    location = " at " + prev.getFileName() + ":" + prev.getStartLine() + ":" + prev.getStartColumn();
                }
                errors.add(new TypeError(d, ErrorMessage.DELTA_USES_NAME_OF_MODULE, d.getName(), location));
            }
            if (seenDeltas.containsKey(d.getName())) {
                DeltaDecl prev = seenDeltas.get(d.getName());
                String location = "";
                if (!prev.getFileName().equals(Main.UNKNOWN_FILENAME)) {
                    location = " at " + prev.getFileName() + ":" + prev.getStartLine() + ":" + prev.getStartColumn();
                }
                errors.add(new TypeError(d, ErrorMessage.DUPLICATE_DELTA, d.getName(), location));
            } else {
                seenDeltas.put(d.getName(), d);
            }
        }
    }
}
Also used : TypeError(org.abs_models.frontend.analyser.TypeError)

Example 18 with TypeError

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

the class TypeCheckerHelper method checkForDuplicatesOfVarDecl.

/**
 * checks whether the local variable v was already defined in the current function
 */
public static void checkForDuplicatesOfVarDecl(SemanticConditionList e, VarDeclStmt v) {
    String varName = v.getVarDecl().getName();
    VarOrFieldDecl otherVar = v.lookupVarOrFieldName(varName, false);
    if (otherVar != null && v.inSameMethodOrBlock(otherVar)) {
        String location = "";
        if (!otherVar.getFileName().equals(Main.UNKNOWN_FILENAME)) {
            location = " at " + otherVar.getFileName() + ":" + otherVar.getStartLine() + ":" + otherVar.getStartColumn();
        }
        e.add(new TypeError(v, ErrorMessage.VARIABLE_ALREADY_DECLARED, varName, location));
    }
}
Also used : TypeError(org.abs_models.frontend.analyser.TypeError)

Example 19 with TypeError

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

the class TypeCheckerHelper method typeCheckEqual.

public static void typeCheckEqual(SemanticConditionList l, ASTNode<?> n, java.util.List<Type> params) {
    org.abs_models.frontend.ast.List<PureExp> args = ((HasActualParams) n).getParams();
    if (params.size() != args.getNumChild()) {
        l.add(new TypeError(n, ErrorMessage.WRONG_NUMBER_OF_ARGS, params.size(), args.getNumChild()));
    } else {
        for (int i = 0; i < params.size(); i++) {
            Type argType = params.get(i);
            PureExp exp = args.getChild(i);
            int nerrors = l.getErrorCount();
            exp.typeCheck(l);
            if (nerrors == l.getErrorCount()) {
                Type expType = exp.getType();
                if (!expType.isAssignableTo(argType)) {
                    l.add(new TypeError(exp, ErrorMessage.TYPE_MISMATCH, exp.getType(), argType));
                }
            }
        }
    }
}
Also used : org.abs_models.frontend.ast(org.abs_models.frontend.ast) TypeError(org.abs_models.frontend.analyser.TypeError)

Example 20 with TypeError

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

the class TypeCheckerHelper method typeCheckEqualPattern.

private static void typeCheckEqualPattern(SemanticConditionList l, ConstructorPattern n, java.util.List<Type> params) {
    org.abs_models.frontend.ast.List<Pattern> args = n.getParams();
    if (params.size() != args.getNumChild()) {
        l.add(new TypeError(n, ErrorMessage.WRONG_NUMBER_OF_ARGS, params.size(), args.getNumChild()));
    } else {
        for (int i = 0; i < params.size(); i++) {
            Type argType = params.get(i);
            Pattern exp = args.getChild(i);
            exp.typeCheck(l, argType);
        }
    }
}
Also used : org.abs_models.frontend.ast(org.abs_models.frontend.ast) 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