Search in sources :

Example 11 with FunctionDecl

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

the class Main method rewriteModel.

/**
 * Perform various rewrites that cannot be done in JastAdd.
 *
 * JastAdd rewrite rules can only rewrite the current node using
 * node-local information.  ("The code in the body of the rewrite may
 * access and rearrange the nodes in the subtree rooted at A, but not any
 * other nodes in the AST. Furthermore, the code may not have any other
 * side effects." --
 * http://jastadd.org/web/documentation/reference-manual.php#Rewrites)
 *
 * We use this method to generate Exception constructors and the
 * information in ABS.Productline.
 *
 * @param m the model.
 * @param productname The name of the product or null.
 * @throws WrongProgramArgumentException
 */
private static void rewriteModel(Model m, String productname) throws WrongProgramArgumentException {
    // Generate reflective constructors for all features
    ProductLine pl = m.getProductLine();
    if (pl != null) {
        // Let's assume the module and datatype names in abslang.abs did
        // not get changed, and just crash otherwise.  If you're here
        // because of a NPE: Hi!  Make the standard library and this code
        // agree about what the feature reflection module is called.
        ModuleDecl modProductline = null;
        DataTypeDecl featureDecl = null;
        FunctionDecl currentFeatureFun = null;
        FunctionDecl productNameFun = null;
        for (ModuleDecl d : m.getModuleDecls()) {
            if (d.getName().equals(Constants.PL_NAME)) {
                modProductline = d;
                break;
            }
        }
        if (modProductline == null) {
            throw new WrongProgramArgumentException("Internal error: did not find module " + Constants.PL_NAME + "(should have been defined in the abslang.abs standard library)");
        }
        for (Decl d : modProductline.getDecls()) {
            if (d instanceof DataTypeDecl && d.getName().equals("Feature")) {
                featureDecl = (DataTypeDecl) d;
            } else if (d instanceof FunctionDecl && d.getName().equals("product_features")) {
                currentFeatureFun = (FunctionDecl) d;
            } else if (d instanceof FunctionDecl && d.getName().equals("product_name")) {
                productNameFun = (FunctionDecl) d;
            }
        }
        // Adjust Feature datatype
        featureDecl.setDataConstructorList(new List<>());
        for (Feature f : pl.getFeatures()) {
            // TODO: when/if we incorporate feature parameters into the
            // productline feature declarations (as we should), we need to
            // adjust the DataConstructor arguments here.
            featureDecl.addDataConstructorNoTransform(new DataConstructor(f.getName(), new List<>()));
        }
        // Adjust product_name() function
        productNameFun.setFunctionDef(new ExpFunctionDef(new StringLiteral(productname)));
        // Adjust product_features() function
        ProductDecl p = null;
        if (productname != null)
            p = m.findProduct(productname);
        if (p != null) {
            DataConstructorExp feature_arglist = new DataConstructorExp("Cons", new List<>());
            DataConstructorExp current = feature_arglist;
            for (Feature f : p.getProduct().getFeatures()) {
                DataConstructorExp next = new DataConstructorExp("Cons", new List<>());
                // TODO: when/if we incorporate feature parameters into
                // the productline feature declarations (as we should), we
                // need to adjust the DataConstructorExp arguments here.
                current.addParamNoTransform(new DataConstructorExp(f.getName(), new List<>()));
                current.addParamNoTransform(next);
                current = next;
            }
            current.setConstructor("Nil");
            currentFeatureFun.setFunctionDef(new ExpFunctionDef(feature_arglist));
        }
    }
    m.flushTreeCache();
}
Also used : ProductDecl(org.abs_models.frontend.ast.ProductDecl) WrongProgramArgumentException(org.abs_models.common.WrongProgramArgumentException) ModuleDecl(org.abs_models.frontend.ast.ModuleDecl) FunctionDecl(org.abs_models.frontend.ast.FunctionDecl) Decl(org.abs_models.frontend.ast.Decl) DataTypeDecl(org.abs_models.frontend.ast.DataTypeDecl) ProductDecl(org.abs_models.frontend.ast.ProductDecl) DataConstructor(org.abs_models.frontend.ast.DataConstructor) Feature(org.abs_models.frontend.ast.Feature) FunctionDecl(org.abs_models.frontend.ast.FunctionDecl) DataConstructorExp(org.abs_models.frontend.ast.DataConstructorExp) StringLiteral(org.abs_models.frontend.ast.StringLiteral) ModuleDecl(org.abs_models.frontend.ast.ModuleDecl) SemanticConditionList(org.abs_models.frontend.analyser.SemanticConditionList) ArrayList(java.util.ArrayList) List(org.abs_models.frontend.ast.List) ProductLine(org.abs_models.frontend.ast.ProductLine) DataTypeDecl(org.abs_models.frontend.ast.DataTypeDecl) ExpFunctionDef(org.abs_models.frontend.ast.ExpFunctionDef)

Example 12 with FunctionDecl

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

the class AnnotationUtil method annotateCall.

/**
 * <p>Annotates the parent Statement or Function node with an ExpansionCall annotation.</p>
 *
 * <p>If said node already has an annotation with the ExpansionCall type, the expansionIndex will be added to the
 * existing annotation.</p>
 *
 * @param call the call to use as a starting point to look for a Stmt or FunctionDecl parent
 * @param expansionId the ID of the called Expansion
 * @throws IllegalArgumentException if there is no Stmt or FunctionDecl parent
 */
public static void annotateCall(FnApp call, int expansionId) {
    Stmt parent = call.closestParent(Stmt.class);
    if (parent == null) {
        FunctionDecl parentFunction = call.closestParent(FunctionDecl.class);
        if (parentFunction == null) {
            throw new IllegalArgumentException("Function call has no parent Statement or FunctionDecl: " + call);
        }
        addToAnnotations(parentFunction.getAnnotations(), expansionCallType(), expansionId);
    } else {
        addToAnnotations(parent.getAnnotations(), expansionCallType(), expansionId);
    }
}
Also used : Stmt(org.abs_models.frontend.ast.Stmt) FunctionDecl(org.abs_models.frontend.ast.FunctionDecl)

Example 13 with FunctionDecl

use of org.abs_models.frontend.ast.FunctionDecl 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 FunctionDecl

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

the class JavaGeneratorHelper method generateBuiltInFnApp.

public static void generateBuiltInFnApp(PrintStream stream, FnApp app) {
    FunctionDecl d = (FunctionDecl) app.getDecl();
    String name = builtInFunctionJavaName(d.getName());
    if (name == null) {
        throw new NotImplementedYetException(app, "The built in function '" + d.getName() + "' is not implemented in the Java backend.");
    }
    // if builtin function returns a non-builtin type, cast the returned value to that type
    boolean returnsGeneratedDataType = !JavaBackend.isBuiltinDataType(app.getType());
    if (returnsGeneratedDataType)
        stream.print("((" + JavaBackend.getQualifiedString(app.getType()) + ")");
    stream.print(ABSBuiltInFunctions.class.getName() + "." + name);
    String firstArgs = null;
    if (Constants.isFunctionalBreakPointFunctionName(d.getModuleDecl().getName() + "." + name))
        firstArgs = generateFunctionalBreakPointArgs(app);
    generateArgs(stream, firstArgs, app.getParams(), d.getTypes());
    if (returnsGeneratedDataType)
        stream.print(")");
}
Also used : NotImplementedYetException(org.abs_models.common.NotImplementedYetException) FunctionDecl(org.abs_models.frontend.ast.FunctionDecl)

Example 15 with FunctionDecl

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

the class TreeUtilsTest method closestParentNotFound.

@Test
public void closestParentNotFound() {
    Model model = assertParse("def Int test() = 1;");
    FunctionDecl functionDecl = getLastFunctionDecl(model);
    assertNull(functionDecl.closestParent(FunctionDecl.class));
}
Also used : Model(org.abs_models.frontend.ast.Model) FunctionDecl(org.abs_models.frontend.ast.FunctionDecl) FrontendTest(org.abs_models.frontend.FrontendTest) Test(org.junit.Test)

Aggregations

FunctionDecl (org.abs_models.frontend.ast.FunctionDecl)18 Model (org.abs_models.frontend.ast.Model)12 Test (org.junit.Test)12 FrontendTest (org.abs_models.frontend.FrontendTest)10 PureExp (org.abs_models.frontend.ast.PureExp)8 ExpFunctionDef (org.abs_models.frontend.ast.ExpFunctionDef)7 Decl (org.abs_models.frontend.ast.Decl)4 FnApp (org.abs_models.frontend.ast.FnApp)3 FunctionDef (org.abs_models.frontend.ast.FunctionDef)3 IntLiteral (org.abs_models.frontend.ast.IntLiteral)2 ModuleDecl (org.abs_models.frontend.ast.ModuleDecl)2 Stmt (org.abs_models.frontend.ast.Stmt)2 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 NotImplementedYetException (org.abs_models.common.NotImplementedYetException)1 WrongProgramArgumentException (org.abs_models.common.WrongProgramArgumentException)1 SemanticConditionList (org.abs_models.frontend.analyser.SemanticConditionList)1 TypeError (org.abs_models.frontend.analyser.TypeError)1 ASTNode (org.abs_models.frontend.ast.ASTNode)1 AddExp (org.abs_models.frontend.ast.AddExp)1