Search in sources :

Example 11 with PureExp

use of org.abs_models.frontend.ast.PureExp in project abstools by abstools.

the class AnnotationUtil method addToAnnotations.

/**
 * Add an ExpansionCall annotation, or an argument to an existing
 * annotation.  Creates or adds to [ExpansionCall : list[expansionId]]
 * annotation.
 *
 * @param annotations The list to mutate
 * @param annotationType Currently always EXPANSION_CALL
 * @param expansionId An integer to add to the list.
 */
private static void addToAnnotations(List<Annotation> annotations, TypeIdUse annotationType, int expansionId) {
    IntLiteral indexLiteral = new IntLiteral(Integer.toString(expansionId));
    Annotation toAdd = getAnnotation(annotations, annotationType);
    if (toAdd == null) {
        List<PureExp> llist = new List<>(new ListLiteral(new List<PureExp>(indexLiteral)));
        toAdd = new TypedAnnotation(new FnApp("list", llist), annotationType);
        annotations.add(toAdd);
    } else {
        PureExp value = toAdd.getValue();
        if (!(value instanceof FnApp)) {
            throw new IllegalArgumentException("Annotation list contains invalid expansion annotation");
        }
        FnApp fvalue = (FnApp) value;
        if (!fvalue.getName().equals("list")) {
            throw new IllegalArgumentException("Annotation list contains invalid expansion annotation");
        }
        ListLiteral list = (ListLiteral) fvalue.getParam(0);
        for (PureExp exp : list.getPureExps()) {
            if (exp instanceof IntLiteral) {
                IntLiteral intLiteral = (IntLiteral) exp;
                if (intLiteral.getContent().equals(indexLiteral.getContent())) {
                    return;
                }
            }
        }
        list.addPureExp(indexLiteral);
    }
}
Also used : ListLiteral(org.abs_models.frontend.ast.ListLiteral) FnApp(org.abs_models.frontend.ast.FnApp) IntLiteral(org.abs_models.frontend.ast.IntLiteral) List(org.abs_models.frontend.ast.List) TypedAnnotation(org.abs_models.frontend.ast.TypedAnnotation) PureExp(org.abs_models.frontend.ast.PureExp) Annotation(org.abs_models.frontend.ast.Annotation) TypedAnnotation(org.abs_models.frontend.ast.TypedAnnotation)

Example 12 with PureExp

use of org.abs_models.frontend.ast.PureExp in project abstools by abstools.

the class SchedulerChecker method checkNewExp.

@Override
public void checkNewExp(NewExp e) {
    Stmt stmt = CompilerUtils.findStmtForExpression(e);
    PureExp sched = AnnotationHelper.getAnnotationValueFromName(stmt.getAnnotations(), "ABS.Scheduler.Scheduler");
    if (sched != null) {
        ClassDecl decl = (ClassDecl) (e.lookup(new KindedName(KindedName.Kind.CLASS, e.getClassName())));
        checkScheduleExp(sched, decl, stmt);
    }
}
Also used : ClassDecl(org.abs_models.frontend.ast.ClassDecl) PureExp(org.abs_models.frontend.ast.PureExp) KindedName(org.abs_models.frontend.typechecker.KindedName) Stmt(org.abs_models.frontend.ast.Stmt)

Example 13 with PureExp

use of org.abs_models.frontend.ast.PureExp 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 14 with PureExp

use of org.abs_models.frontend.ast.PureExp in project abstools by abstools.

the class CostAnnotationChecker method checkStmt.

@Override
public void checkStmt(Stmt s) {
    PureExp cost = AnnotationHelper.getAnnotationValueFromName(s.getAnnotations(), "ABS.DC.Cost");
    if (cost == null)
        return;
    cost.typeCheck(errors);
    if (!cost.getType().isNumericType()) {
        errors.add(new TypeError(s, ErrorMessage.WRONG_COST_ANNOTATION_TYPE, cost.getType().getQualifiedName()));
    }
}
Also used : TypeError(org.abs_models.frontend.analyser.TypeError) PureExp(org.abs_models.frontend.ast.PureExp)

Example 15 with PureExp

use of org.abs_models.frontend.ast.PureExp in project abstools by abstools.

the class MaudeCompilerHelper method emitCostAnnotation.

public static void emitCostAnnotation(PrintStream stream, List<Annotation> annotations, int defaultValue) {
    PureExp cost = AnnotationHelper.getAnnotationValueFromName(annotations, "ABS.DC.Cost");
    if (cost != null || defaultValue > 0) {
        stream.print("$cost(");
        if (cost == null) {
            stream.print("\"num\"[" + Integer.toString(defaultValue) + "]");
        } else {
            cost.generateMaude(stream);
        }
        stream.print(") ; ");
    }
}
Also used : PureExp(org.abs_models.frontend.ast.PureExp)

Aggregations

PureExp (org.abs_models.frontend.ast.PureExp)22 FunctionDecl (org.abs_models.frontend.ast.FunctionDecl)8 FrontendTest (org.abs_models.frontend.FrontendTest)7 Model (org.abs_models.frontend.ast.Model)7 Test (org.junit.Test)7 ExpFunctionDef (org.abs_models.frontend.ast.ExpFunctionDef)5 FnApp (org.abs_models.frontend.ast.FnApp)5 IntLiteral (org.abs_models.frontend.ast.IntLiteral)5 Annotation (org.abs_models.frontend.ast.Annotation)4 Type (org.abs_models.frontend.typechecker.Type)3 ABSBool (org.abs_models.backend.java.lib.types.ABSBool)2 TypeError (org.abs_models.frontend.analyser.TypeError)2 FunctionDef (org.abs_models.frontend.ast.FunctionDef)2 List (org.abs_models.frontend.ast.List)2 MethodSig (org.abs_models.frontend.ast.MethodSig)2 TypedAnnotation (org.abs_models.frontend.ast.TypedAnnotation)2 VarUse (org.abs_models.frontend.ast.VarUse)2 AddExp (org.abs_models.frontend.ast.AddExp)1 AsyncCall (org.abs_models.frontend.ast.AsyncCall)1 ClassDecl (org.abs_models.frontend.ast.ClassDecl)1