Search in sources :

Example 1 with DataConstructorExp

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

the class AnnotationTests method assertHasLocAnnotation.

private void assertHasLocAnnotation(Type t, String s) {
    List<TypeAnnotation> anns = t.getTypeAnnotations();
    TypeAnnotation a = anns.get(0);
    assertEquals("LocationType", a.getType().getSimpleName());
    assertEquals(s, ((DataConstructorExp) a.getValue()).getDecl().getName());
}
Also used : TypeAnnotation(org.abs_models.frontend.typechecker.TypeAnnotation) DataConstructorExp(org.abs_models.frontend.ast.DataConstructorExp)

Example 2 with DataConstructorExp

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

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

the class AnnotationHelper method getAnnotationsOfType.

public static java.util.List<Annotation> getAnnotationsOfType(List<Annotation> annos, String qualifiedName) {
    ArrayList<Annotation> res = new ArrayList<>();
    for (Annotation a : annos) {
        if (a.getType().getQualifiedName().equals(qualifiedName)) {
            DataConstructorExp de = (DataConstructorExp) a.getValue();
            res.add(a);
        }
    }
    return res;
}
Also used : DataConstructorExp(org.abs_models.frontend.ast.DataConstructorExp) ArrayList(java.util.ArrayList) Annotation(org.abs_models.frontend.ast.Annotation) TypedAnnotation(org.abs_models.frontend.ast.TypedAnnotation)

Example 4 with DataConstructorExp

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

the class ASTBasedABSTestRunnerGenerator method generateMainBlockAST.

private MainBlock generateMainBlockAST(List<Import> list) {
    final MainBlock block = new MainBlock();
    DataConstructorExp empty = new DataConstructorExp("EmptySet", new List<>());
    VarDeclStmt futsStatement = getVarDecl(futs, getType("Set", getFutUnitType()), empty);
    block.addStmtNoTransform(futsStatement);
    VarDeclStmt futStatement = getVarDecl(fut, getFutUnitType(), null);
    block.addStmtNoTransform(futStatement);
    Set<TypeUse> use = new HashSet<>();
    for (InterfaceDecl key : tests.keySet()) {
        for (ClassDecl clazz : tests.get(key)) {
            use.addAll(generateTestClassImplAST(key, clazz, block));
        }
    }
    block.addStmtNoTransform(generateWaitSyncAST());
    return block;
}
Also used : DataConstructorExp(org.abs_models.frontend.ast.DataConstructorExp) DataTypeUse(org.abs_models.frontend.ast.DataTypeUse) TypeUse(org.abs_models.frontend.ast.TypeUse) ParametricDataTypeUse(org.abs_models.frontend.ast.ParametricDataTypeUse) ClassDecl(org.abs_models.frontend.ast.ClassDecl) VarDeclStmt(org.abs_models.frontend.ast.VarDeclStmt) InterfaceDecl(org.abs_models.frontend.ast.InterfaceDecl) MainBlock(org.abs_models.frontend.ast.MainBlock) HashSet(java.util.HashSet)

Example 5 with DataConstructorExp

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

the class DeployInformationClass method addAnn.

public void addAnn(PureExp exp) {
    // A specification is a list (hence a FnApp with first argument being a list)
    DeployInformationClassSpecification info = new DeployInformationClassSpecification(_paramList, _paramType);
    PureExp list = ((FnApp) exp).getParam(0);
    while (((DataConstructorExp) list).hasParam() && (((DataConstructorExp) list).getParam(0) != null)) {
        // means we have a cons
        PureExp el = ((DataConstructorExp) list).getParam(0);
        list = ((DataConstructorExp) list).getParam(1);
        if (((DataConstructorExp) el).getDataConstructor().getName().equals("Cost")) {
            String name = ((StringLiteral) ((DataConstructorExp) el).getParam(0)).getContent();
            int cost = Integer.parseInt(((IntLiteral) ((DataConstructorExp) el).getParam(1)).getContent());
            info.addCost(name, cost);
            System.out.println("  Annotation is Cost(\"" + name + "\", " + cost + ")");
        } else if (((DataConstructorExp) el).getDataConstructor().getName().equals("MaxUse")) {
            int arity = Integer.parseInt(((IntLiteral) ((DataConstructorExp) el).getParam(0)).getContent());
            info.setProvide(arity);
            System.out.println("  Annotation is MaxUse(" + arity + ")");
        } else if (((DataConstructorExp) el).getDataConstructor().getName().equals("Name")) {
            String name = ((StringLiteral) ((DataConstructorExp) el).getParam(0)).getContent();
            info.addScenarioName(name);
            System.out.println("  Annotation is Name(" + name + ")");
        } else if (((DataConstructorExp) el).getDataConstructor().getName().equals("Param")) {
            String param = ((StringLiteral) ((DataConstructorExp) el).getParam(0)).getContent();
            String port = _paramType.get(param);
            PureExp spec = ((DataConstructorExp) el).getParam(1);
            if (((DataConstructorExp) spec).getDataConstructor().getName().equals("Req")) {
                System.out.print("Req");
                info.addRequirement(param);
                System.out.println("  Annotation is Req(\"" + param + "\")");
            } else if (((DataConstructorExp) spec).getDataConstructor().getName().equals("List")) {
                int arity = Integer.parseInt(((IntLiteral) ((DataConstructorExp) spec).getParam(0)).getContent());
                info.addList(param, arity);
                System.out.println("  Annotation is List(\"" + port + "\", " + arity + ")");
            } else if (((DataConstructorExp) spec).getDataConstructor().getName().equals("OptList")) {
                String value = ((StringLiteral) ((DataConstructorExp) spec).getParam(0)).getContent();
                info.addOptList(param, value);
                System.out.println("  Annotation is OptList(\"" + port + "\", " + value + ")");
            } else if (((DataConstructorExp) spec).getDataConstructor().getName().equals("Default")) {
                System.out.print("Default");
                String value = ((StringLiteral) ((DataConstructorExp) spec).getParam(0)).getContent();
                if (port != null)
                    System.out.print("(\"" + port + "\", " + value + ")");
                info.addDefault(param, value);
                System.out.println("  Annotation is Default(\"" + port + "\", " + value + ")");
            } else if (((DataConstructorExp) spec).getDataConstructor().getName().equals("User")) {
                System.out.print("User");
                if (port != null)
                    System.out.print("(\"" + port + "\")");
                info.addUser(param, port);
                System.out.println("  Annotation is User(\"" + port + "\", " + param + ")");
            }
        }
    }
    _spec.add(info);
}
Also used : DataConstructorExp(org.abs_models.frontend.ast.DataConstructorExp) FnApp(org.abs_models.frontend.ast.FnApp) StringLiteral(org.abs_models.frontend.ast.StringLiteral) IntLiteral(org.abs_models.frontend.ast.IntLiteral) PureExp(org.abs_models.frontend.ast.PureExp)

Aggregations

DataConstructorExp (org.abs_models.frontend.ast.DataConstructorExp)5 ArrayList (java.util.ArrayList)2 StringLiteral (org.abs_models.frontend.ast.StringLiteral)2 HashSet (java.util.HashSet)1 WrongProgramArgumentException (org.abs_models.common.WrongProgramArgumentException)1 SemanticConditionList (org.abs_models.frontend.analyser.SemanticConditionList)1 Annotation (org.abs_models.frontend.ast.Annotation)1 ClassDecl (org.abs_models.frontend.ast.ClassDecl)1 DataConstructor (org.abs_models.frontend.ast.DataConstructor)1 DataTypeDecl (org.abs_models.frontend.ast.DataTypeDecl)1 DataTypeUse (org.abs_models.frontend.ast.DataTypeUse)1 Decl (org.abs_models.frontend.ast.Decl)1 ExpFunctionDef (org.abs_models.frontend.ast.ExpFunctionDef)1 Feature (org.abs_models.frontend.ast.Feature)1 FnApp (org.abs_models.frontend.ast.FnApp)1 FunctionDecl (org.abs_models.frontend.ast.FunctionDecl)1 IntLiteral (org.abs_models.frontend.ast.IntLiteral)1 InterfaceDecl (org.abs_models.frontend.ast.InterfaceDecl)1 List (org.abs_models.frontend.ast.List)1 MainBlock (org.abs_models.frontend.ast.MainBlock)1