Search in sources :

Example 6 with PureExp

use of abs.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 : 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 7 with PureExp

use of abs.frontend.ast.PureExp in project abstools by abstools.

the class JavaGeneratorHelper method generateAsyncCall.

private static void generateAsyncCall(PrintStream stream, final String calleeString, final PureExp callee, final Type calleeType, final List<PureExp> args, final List<ParamDecl> params, final MethodSig sig, final List<Annotation> annotations) {
    final java.util.List<Type> paramTypes = sig.getTypes();
    stream.print(ABSRuntime.class.getName() + ".getCurrentRuntime().asyncCall(");
    String targetType = JavaBackend.getQualifiedString(calleeType);
    stream.println("new " + AbstractAsyncCallRT.class.getName() + "<" + targetType + ">(");
    stream.println("this,");
    if (callee instanceof ThisExp) {
        if (calleeString != null)
            stream.print(calleeString);
        else
            callee.generateJava(stream);
    } else {
        stream.print(ABSRuntime.class.getName() + ".checkForNull(");
        if (calleeString != null)
            stream.print(calleeString);
        else
            callee.generateJava(stream);
        stream.print(")");
    }
    stream.println(",");
    PureExp rtAttr;
    rtAttr = CompilerUtils.getAnnotationValueFromSimpleName(annotations, "Deadline");
    if (rtAttr == null)
        stream.print("new ABS.StdLib.Duration_InfDuration()");
    else
        rtAttr.generateJava(stream);
    stream.println(",");
    rtAttr = CompilerUtils.getAnnotationValueFromSimpleName(annotations, "Cost");
    if (rtAttr == null)
        stream.print("new ABS.StdLib.Duration_InfDuration()");
    else
        rtAttr.generateJava(stream);
    stream.println(",");
    rtAttr = CompilerUtils.getAnnotationValueFromSimpleName(annotations, "Critical");
    if (rtAttr == null)
        stream.print(ABSBool.class.getName() + ".FALSE");
    else
        rtAttr.generateJava(stream);
    stream.println(") {");
    int i = 0;
    for (Type t : paramTypes) {
        stream.println(JavaBackend.getQualifiedString(t) + " arg" + i + ";");
        i++;
    }
    generateTaskGetArgsMethod(stream, paramTypes.size());
    generateTaskInitMethod(stream, paramTypes);
    stream.println("public java.lang.String methodName() {");
    stream.println("return \"" + sig.getName() + "\";");
    stream.println("}");
    stream.println("public Object execute() {");
    stream.print("return target." + JavaBackend.getMethodName(sig.getName()) + "(");
    for (i = 0; i < paramTypes.size(); i++) {
        if (i > 0)
            stream.print(",");
        stream.println("arg" + i);
        if (paramTypes.get(i).isIntType())
            stream.print(".truncate()");
    }
    stream.println(");");
    stream.println("}");
    stream.print("}.init");
    if (args != null)
        JavaGeneratorHelper.generateArgs(stream, args, paramTypes);
    else
        JavaGeneratorHelper.generateParamArgs(stream, params);
    stream.println(")");
}
Also used : AbstractAsyncCallRT(abs.backend.java.lib.runtime.AbstractAsyncCallRT) Type(abs.frontend.typechecker.Type) ABSBool(abs.backend.java.lib.types.ABSBool) PureExp(abs.frontend.ast.PureExp) ThisExp(abs.frontend.ast.ThisExp)

Example 8 with PureExp

use of abs.frontend.ast.PureExp in project abstools by abstools.

the class JavaGeneratorHelper method generateArgs.

public static void generateArgs(PrintStream stream, String firstArg, List<PureExp> args, java.util.List<Type> types) {
    stream.print("(");
    boolean first = true;
    if (firstArg != null) {
        stream.print(firstArg);
        first = false;
    }
    for (int i = 0; i < args.getNumChild(); i++) {
        PureExp e = args.getChild(i);
        if (!first)
            stream.print(", ");
        e.generateJava(stream);
        if (types.get(i).isIntType() && e.getType().isRatType())
            stream.print(".truncate()");
        first = false;
    }
    stream.print(")");
}
Also used : PureExp(abs.frontend.ast.PureExp)

Example 9 with PureExp

use of abs.frontend.ast.PureExp in project abstools by abstools.

the class JavaGeneratorHelper method generateAsyncCall.

public static void generateAsyncCall(PrintStream stream, AsyncCall call) {
    final PureExp callee = call.getCallee();
    final List<PureExp> params = call.getParams();
    final MethodSig sig = call.getMethodSig();
    final List<Annotation> annotations = call.getAnnotations();
    generateAsyncCall(stream, null, callee, callee.getType(), params, null, sig, annotations);
}
Also used : MethodSig(abs.frontend.ast.MethodSig) PureExp(abs.frontend.ast.PureExp) Annotation(abs.frontend.ast.Annotation)

Example 10 with PureExp

use of abs.frontend.ast.PureExp in project abstools by abstools.

the class TreeUtilsTest method recurseForSome.

@Test
public void recurseForSome() {
    Model model = assertParseOkStdLib("def Int test(Int i) = 1 + test(2);");
    FunctionDecl functionDecl = getLastFunctionDecl(model);
    assertEquals("test", functionDecl.getName());
    FunctionDef def = functionDecl.getFunctionDef();
    Stream<PureExp> children = def.findChildren(cast(PureExp.class), AddExp.class::isInstance);
    assertNotNull(children);
    List<PureExp> result = children.distinct().collect(Collectors.toList());
    // expecting AddExp, IntLiteral(1), FnApp, NOT IntLiteral(2)
    assertEquals(3, result.size());
    for (PureExp exp : result) {
        assertFalse(exp instanceof IntLiteral && ((IntLiteral) exp).getContent().equals("2"));
    }
}
Also used : AddExp(abs.frontend.ast.AddExp) Model(abs.frontend.ast.Model) FunctionDef(abs.frontend.ast.FunctionDef) ExpFunctionDef(abs.frontend.ast.ExpFunctionDef) IntLiteral(abs.frontend.ast.IntLiteral) PureExp(abs.frontend.ast.PureExp) FunctionDecl(abs.frontend.ast.FunctionDecl) Test(org.junit.Test) FrontendTest(abs.frontend.FrontendTest)

Aggregations

PureExp (abs.frontend.ast.PureExp)26 FunctionDecl (abs.frontend.ast.FunctionDecl)8 FrontendTest (abs.frontend.FrontendTest)7 Model (abs.frontend.ast.Model)7 Test (org.junit.Test)7 VarUse (abs.frontend.ast.VarUse)6 ExpFunctionDef (abs.frontend.ast.ExpFunctionDef)5 ABSData (apet.testCases.ABSData)5 Annotation (abs.frontend.ast.Annotation)4 FnApp (abs.frontend.ast.FnApp)4 IntLiteral (abs.frontend.ast.IntLiteral)4 ABSRef (apet.testCases.ABSRef)3 PreviousCall (apet.testCases.PreviousCall)3 ABSBool (abs.backend.java.lib.types.ABSBool)2 DeclNamePredicate (abs.backend.tests.AbsASTBuilderUtil.DeclNamePredicate)2 AbsASTBuilderUtil.getCall (abs.backend.tests.AbsASTBuilderUtil.getCall)2 Call (abs.frontend.ast.Call)2 ClassDecl (abs.frontend.ast.ClassDecl)2 DataTypeUse (abs.frontend.ast.DataTypeUse)2 EqExp (abs.frontend.ast.EqExp)2