Search in sources :

Example 1 with TypeError

use of abs.frontend.analyser.TypeError in project abstools by abstools.

the class SchedulerChecker method checkScheduleExp.

private void checkScheduleExp(PureExp sched, ClassDecl class_decl, ASTNode<?> loc) {
    if (sched == null)
        return;
    if (!(sched instanceof FnApp)) {
        errors.add(new TypeError(loc, ErrorMessage.WRONG_SCHEDULER_ANNOTATION_TYPE, sched.getType()));
        return;
    }
    FnApp s = (FnApp) sched;
    Type scheduler_type = s.getType();
    if (s.getDecl().isUnknown()) {
        errors.add(new TypeError(loc, ErrorMessage.FUNCTION_NOT_RESOLVABLE, s.getName()));
        return;
    }
    FunctionDecl sd = (FunctionDecl) s.getDecl();
    // check scheduling function return type
    boolean schedulerTypeCorrect = scheduler_type.isDataType() && ((DataTypeType) scheduler_type).getQualifiedName().equals("ABS.Scheduler.Process");
    // check scheduling function first arg, pt.1: are we a list?
    boolean schedulerFunFirstArgCorrect = sd.getNumParam() > 0 && sd.getParam(0).getType().getQualifiedName().equals("ABS.StdLib.List");
    if (schedulerFunFirstArgCorrect) {
        // check scheduling function first arg, pt.2: are we a list of
        // processes?
        DataTypeType firstArgType = (DataTypeType) sd.getParam(0).getType();
        if (firstArgType.numTypeArgs() != 1) {
            // should not happen since ABS.StdLib.List takes 1 argument
            schedulerFunFirstArgCorrect = false;
        } else {
            schedulerFunFirstArgCorrect = firstArgType.getTypeArg(0).getQualifiedName().equals("ABS.Scheduler.Process");
        }
    }
    if (!schedulerTypeCorrect || !schedulerFunFirstArgCorrect) {
        // emit two messages: one at the annotation location, one for the
        // offending scheduler function
        errors.add(new TypeError(loc, ErrorMessage.WRONG_SCHEDULER_ANNOTATION_TYPE, "dummy"));
        errors.add(new TypeError(sd, ErrorMessage.WRONG_SCHEDULER_FUN_TYPE, s.getName()));
    }
    if (s.getNumParam() == 0 || !(s.getParam(0) instanceof VarUse) || !((VarUse) s.getParam(0)).getName().equals("queue")) {
        // first arg to the scheduler expression must be the magic `queue'
        errors.add(new TypeError(loc, ErrorMessage.WRONG_SCHEDULER_FIRST_ARGUMENT, "dummy"));
    }
    if (s.getNumParam() != sd.getNumParam()) {
        errors.add(new TypeError(loc, ErrorMessage.WRONG_NUMBER_OF_ARGS, s.getNumParam(), sd.getNumParam()));
    } else {
        // start from 1; magic first parameter `queue' already checked
        for (int i = 1; i < s.getNumParam(); i++) {
            PureExp arg = s.getParam(i);
            String argname = "";
            if (!(arg instanceof VarOrFieldUse)) {
                // argument was not a plain identifier
                errors.add(new TypeError(arg, ErrorMessage.WRONG_SCHEDULER_FIELD_ARGUMENT, Integer.toString(i + 1), class_decl.getName()));
            } else {
                // Check the rest of the parameters against class
                // field/param names and argument types of the scheduling
                // function.  Parts of this could be elided if we verify
                // that `VarUse's in scheduler annotation are rewritten to
                // `FieldUse's -- then we'd just have to check for the
                // presence of `VarUse' in the parameter list to detect
                // invalid args.  But can't hurt to open-code it (and we
                // still have to check the type of all arguments vs the
                // scheduling function parameters).
                VarOrFieldUse vararg = (VarOrFieldUse) arg;
                String name = vararg.getName();
                Type argtype = UnknownType.INSTANCE;
                for (ParamDecl p : class_decl.getParamList()) {
                    if (p.getName().equals(name))
                        argtype = p.getType();
                }
                for (FieldDecl f : class_decl.getFieldList()) {
                    if (f.getName().equals(name))
                        argtype = f.getType();
                }
                if (argtype.isUnknownType()) {
                    // identifier, but unknown in the class
                    errors.add(new TypeError(arg, ErrorMessage.WRONG_SCHEDULER_FIELD_ARGUMENT, "\"" + name + "\"", class_decl.getName()));
                } else {
                    // argtype: field; paramtype: function arg
                    Type paramtype = sd.getParam(i).getType();
                    if (!argtype.isAssignableTo(paramtype)) {
                        errors.add(new TypeError(arg, ErrorMessage.TYPE_MISMATCH, argtype, paramtype));
                    }
                }
            }
        }
    }
    if (class_decl.getType().isDeploymentComponentType()) {
        errors.add(new TypeError(loc, ErrorMessage.SCHEDULER_ON_DC, "dummy"));
    }
}
Also used : PureExp(abs.frontend.ast.PureExp) VarUse(abs.frontend.ast.VarUse) FunctionDecl(abs.frontend.ast.FunctionDecl) FieldDecl(abs.frontend.ast.FieldDecl) FnApp(abs.frontend.ast.FnApp) ParamDecl(abs.frontend.ast.ParamDecl) TypeError(abs.frontend.analyser.TypeError) VarOrFieldUse(abs.frontend.ast.VarOrFieldUse)

Example 2 with TypeError

use of abs.frontend.analyser.TypeError in project abstools by abstools.

the class LocationTypeExtension method checkAssignable.

@Override
public void checkAssignable(Type adaptTo, AdaptDirection dir, Type rht, Type lht, ASTNode<?> n) {
    LocationType rhtl = getLocationType(rht);
    LocationType lhtl = getLocationType(lht);
    if (n instanceof NewExp && !((NewExp) n).hasLocal()) {
        if (!rhtl.isSubtypeOfFarAdapted(lhtl)) {
            errors.add(new TypeError(n, ErrorMessage.LOCATION_TYPE_CANNOT_ASSIGN, rhtl.toString(), lhtl.toString()));
        }
    } else {
        LocationType adaptedRht = rhtl;
        if (adaptTo != null) {
            adaptedRht = rhtl.adaptTo(getLocationType(adaptTo), dir);
        }
        if (!adaptedRht.isSubtypeOf(lhtl)) {
            errors.add(new TypeError(n, ErrorMessage.LOCATION_TYPE_CANNOT_ASSIGN, adaptedRht.toString(), lhtl.toString()));
        }
    }
}
Also used : TypeError(abs.frontend.analyser.TypeError)

Example 3 with TypeError

use of abs.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, Set<String> updateNames, 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));
        }
    }
    Set<String> seen = new HashSet<>();
// FIXME: deal with reconfigurations
// for (Reconfiguration recf : prod.getReconfigurations()) {
// if (!seen.add(recf.getTargetProductID()))
// e.add(new TypeError(recf, ErrorMessage.DUPLICATE_RECONFIGURATION, recf.getTargetProductID()));
// 
// // Does the reconfiguration target product exist?
// if (! prodNames.contains(recf.getTargetProductID()))
// e.add(new TypeError(recf, ErrorMessage.NAME_NOT_RESOLVABLE, recf.getTargetProductID()));
// // Do the deltas used for reconfiguration exist?
// for (DeltaID d : recf.getDeltaIDs()) {
// if (! deltaNames.containsKey(d.getName()))
// e.add(new TypeError(recf, ErrorMessage.NAME_NOT_RESOLVABLE, d.getName()));
// }
// // Does the update used for reconfiguration exist?
// if (! updateNames.contains(recf.getUpdateID()))
// e.add(new TypeError(recf, ErrorMessage.NAME_NOT_RESOLVABLE, recf.getUpdateID()));
// }
}
Also used : java.util(java.util) TypeError(abs.frontend.analyser.TypeError)

Example 4 with TypeError

use of abs.frontend.analyser.TypeError in project abstools by abstools.

the class TypeCheckerHelper method checkDataTypeUse.

public static void checkDataTypeUse(SemanticConditionList e, DataTypeUse use) {
    Type type = use.getType();
    if (type.getDecl() instanceof ParametricDataTypeDecl) {
        DataTypeType t = (DataTypeType) type;
        int expected = ((ParametricDataTypeDecl) type.getDecl()).getNumTypeParameter();
        if (expected != t.numTypeArgs()) {
            e.add(new TypeError(use, ErrorMessage.WRONG_NUMBER_OF_TYPE_ARGS, type.toString(), "" + expected, "" + t.numTypeArgs()));
        } else if (expected > 0) {
            if (use instanceof ParametricDataTypeUse) {
                for (TypeUse du : ((ParametricDataTypeUse) use).getParams()) {
                    du.typeCheck(e);
                }
            } else if (use.getDecl() instanceof TypeSynDecl) {
            // nothing to check as this is already checked at the TypeSynDecl
            } else {
                e.add(new TypeError(use, ErrorMessage.WRONG_NUMBER_OF_TYPE_ARGS, type.toString(), "" + expected, "0"));
            }
        }
    }
}
Also used : TypeError(abs.frontend.analyser.TypeError)

Example 5 with TypeError

use of abs.frontend.analyser.TypeError in project abstools by abstools.

the class TypeCheckerHelper method typeCheck.

public static void typeCheck(ConstructorPattern p, SemanticConditionList e, Type t) {
    DataConstructor c = p.getDataConstructor();
    if (c == null) {
        e.add(new SemanticError(p, ErrorMessage.CONSTRUCTOR_NOT_RESOLVABLE, p.getConstructor()));
        return;
    }
    String cname = c.qualifiedName();
    if (deprecatedConstructors.contains(cname) && !(cname.startsWith(p.getModuleDecl().getName()))) {
        e.add(new SemanticWarning(p, ErrorMessage.DEPRECATED_CONSTRUCTOR, c.qualifiedName()));
    }
    if (c.getNumConstructorArg() != p.getNumParam()) {
        e.add(new TypeError(p, ErrorMessage.WRONG_NUMBER_OF_ARGS, c.getNumConstructorArg(), p.getNumParam()));
        return;
    }
    // isExceptionType only for clarity, since exceptions are datatypes
    assert t.isDataType() || t.isExceptionType() : t;
    if (!t.isExceptionType()) {
        if (!t.getDecl().equals(c.getDataTypeDecl())) {
            e.add(new TypeError(p, ErrorMessage.WRONG_CONSTRUCTOR, t.toString(), p.getConstructor()));
        }
    }
    Type myType = p.getType();
    if (!(myType instanceof DataTypeType))
        return;
    if (!(t instanceof DataTypeType)) {
        e.add(new TypeError(p, ErrorMessage.TYPE_MISMATCH, myType, t));
        return;
    }
    DataTypeType myDType = (DataTypeType) myType;
    DataTypeType otherType = (DataTypeType) t;
    if (!myDType.getDecl().equals(otherType.getDecl())) {
        e.add(new TypeError(p, ErrorMessage.TYPE_MISMATCH, myDType, t));
        return;
    }
    typeCheckMatchingParamsPattern(e, p, c);
}
Also used : SemanticError(abs.frontend.analyser.SemanticError) TypeError(abs.frontend.analyser.TypeError) SemanticWarning(abs.frontend.analyser.SemanticWarning)

Aggregations

TypeError (abs.frontend.analyser.TypeError)17 FrontendTest (abs.frontend.FrontendTest)2 SemanticConditionList (abs.frontend.analyser.SemanticConditionList)2 SemanticWarning (abs.frontend.analyser.SemanticWarning)2 FieldDecl (abs.frontend.ast.FieldDecl)2 Model (abs.frontend.ast.Model)2 HashSet (java.util.HashSet)2 Test (org.junit.Test)2 ErrorMessage (abs.frontend.analyser.ErrorMessage)1 SemanticError (abs.frontend.analyser.SemanticError)1 abs.frontend.ast (abs.frontend.ast)1 Annotation (abs.frontend.ast.Annotation)1 ClassDecl (abs.frontend.ast.ClassDecl)1 FnApp (abs.frontend.ast.FnApp)1 FunctionDecl (abs.frontend.ast.FunctionDecl)1 ParamDecl (abs.frontend.ast.ParamDecl)1 PureExp (abs.frontend.ast.PureExp)1 TypedVarOrFieldDecl (abs.frontend.ast.TypedVarOrFieldDecl)1 VarOrFieldDecl (abs.frontend.ast.VarOrFieldDecl)1 VarOrFieldUse (abs.frontend.ast.VarOrFieldUse)1