Search in sources :

Example 6 with TypeError

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

the class ClassKindTypeExtension method checkNewExp.

@Override
public void checkNewExp(NewExp e) {
    ClassDecl d = (ClassDecl) e.lookup(new KindedName(Kind.CLASS, e.getClassName()));
    List<Annotation> anns = AnnotationHelper.getAnnotationsOfType(d.getAnnotations(), "ABS.StdLib.ClassKindAnnotation");
    if (!anns.isEmpty()) {
        String name = ((DataConstructorExp) anns.get(0).getValue()).getDecl().getName();
        if (e.hasLocal()) {
            if (name.equals("COG")) {
                errors.add(new TypeError(e, ErrorMessage.CLASSKIND_PLAIN, d.getName()));
            }
        } else {
            if (!name.equals("COG")) {
                errors.add(new TypeError(e, ErrorMessage.CLASSKIND_COG, d.getName()));
            }
        }
    }
}
Also used : ClassDecl(org.abs_models.frontend.ast.ClassDecl) TypeError(org.abs_models.frontend.analyser.TypeError) KindedName(org.abs_models.frontend.typechecker.KindedName) Annotation(org.abs_models.frontend.ast.Annotation)

Example 7 with TypeError

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

the class FinalAnnotationTypeExtension method checkAssignStmt.

@Override
public void checkAssignStmt(AssignStmt s) {
    VarOrFieldDecl decl = s.getVar().getDecl();
    if (decl instanceof TypedVarOrFieldDecl) {
        TypedVarOrFieldDecl d = (TypedVarOrFieldDecl) decl;
        // Not sure if this code will encounter delta bodies:
        if (d.isFinal()) {
            String name = d.getName();
            boolean isField = (d instanceof FieldDecl);
            String kind = isField ? "field" : "variable";
            add(new TypeError(s, ErrorMessage.ASSIGN_TO_FINAL, kind, name));
        }
    } else {
    // It's a PatternVarDecl.  Assume these are never final.
    }
}
Also used : TypedVarOrFieldDecl(org.abs_models.frontend.ast.TypedVarOrFieldDecl) FieldDecl(org.abs_models.frontend.ast.FieldDecl) VarOrFieldDecl(org.abs_models.frontend.ast.VarOrFieldDecl) TypedVarOrFieldDecl(org.abs_models.frontend.ast.TypedVarOrFieldDecl) VarOrFieldDecl(org.abs_models.frontend.ast.VarOrFieldDecl) TypedVarOrFieldDecl(org.abs_models.frontend.ast.TypedVarOrFieldDecl) TypeError(org.abs_models.frontend.analyser.TypeError)

Example 8 with TypeError

use of org.abs_models.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 : DataTypeType(org.abs_models.frontend.typechecker.DataTypeType) PureExp(org.abs_models.frontend.ast.PureExp) VarUse(org.abs_models.frontend.ast.VarUse) FunctionDecl(org.abs_models.frontend.ast.FunctionDecl) FieldDecl(org.abs_models.frontend.ast.FieldDecl) DataTypeType(org.abs_models.frontend.typechecker.DataTypeType) Type(org.abs_models.frontend.typechecker.Type) UnknownType(org.abs_models.frontend.typechecker.UnknownType) FnApp(org.abs_models.frontend.ast.FnApp) ParamDecl(org.abs_models.frontend.ast.ParamDecl) TypeError(org.abs_models.frontend.analyser.TypeError) VarOrFieldUse(org.abs_models.frontend.ast.VarOrFieldUse)

Example 9 with TypeError

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

the class TypeCheckerHelper method typeCheckDeltaClause.

public static void typeCheckDeltaClause(DeltaClause clause, Map<String, DeltaDecl> deltaNames, Set<String> definedFeatures, SemanticConditionList e) {
    /* Does the delta exist? */
    final Deltaspec spec = clause.getDeltaspec();
    if (!deltaNames.containsKey(spec.getDeltaID()))
        e.add(new TypeError(spec, ErrorMessage.NAME_NOT_RESOLVABLE, spec.getDeltaID()));
    else {
        DeltaDecl dd = deltaNames.get(spec.getDeltaID());
        if (dd.getNumParam() != spec.getNumDeltaparam()) {
            e.add(new TypeError(spec, ErrorMessage.WRONG_NUMBER_OF_ARGS, dd.getNumParam(), spec.getNumDeltaparam()));
        } else {
            for (int i = 0; i < dd.getNumParam(); i++) {
                DeltaParamDecl formal = dd.getParam(i);
                Deltaparam actual = spec.getDeltaparam(i);
                // TODO: W00t?!
                if (actual instanceof Const) {
                    Value a = ((Const) actual).getValue();
                    if (!formal.accepts(a)) {
                        e.add(new TypeError(a, ErrorMessage.CANNOT_ASSIGN, a.getName(), formal.getType().getSimpleName()));
                    }
                }
            }
        }
    }
    /* Do the referenced features exist? */
    if (clause.hasAppCond()) {
        clause.getAppCond().typeCheck(definedFeatures, e);
    }
    if (clause.hasFromAppCond()) {
        clause.getFromAppCond().typeCheck(definedFeatures, e);
    }
    /* What about deltas mentioned in the 'after' clause? */
    for (DeltaID did : clause.getAfterDeltaIDs()) {
        if (!deltaNames.containsKey(did.getName())) {
            e.add(new TypeError(did, ErrorMessage.NAME_NOT_RESOLVABLE, did.getName()));
        }
    }
}
Also used : TypeError(org.abs_models.frontend.analyser.TypeError)

Example 10 with TypeError

use of org.abs_models.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.getQualifiedName();
    if (deprecatedConstructors.contains(cname) && !(cname.startsWith(p.getModuleDecl().getName()))) {
        e.add(new SemanticWarning(p, ErrorMessage.DEPRECATED_CONSTRUCTOR, cname));
    }
    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(org.abs_models.frontend.analyser.SemanticError) TypeError(org.abs_models.frontend.analyser.TypeError) SemanticWarning(org.abs_models.frontend.analyser.SemanticWarning)

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