Search in sources :

Example 1 with Decl

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

the class AbstractPartialFunctionTest method assertHasFunction.

protected FunctionDecl assertHasFunction(Model model, String regex) {
    FunctionDecl result = getFunction(model, Pattern.compile(regex));
    String errorMessage = "No expanded function with name " + regex + " created" + " (functions: " + getFunctions(model) + ")";
    assertNotNull(errorMessage, result);
    Decl decl = model.lookup(new KindedName(KindedName.Kind.FUN, result.getName()));
    assertFalse("Could not lookup function " + result.getName(), decl.isUnknown());
    return result;
}
Also used : FunctionDecl(org.abs_models.frontend.ast.FunctionDecl) PartialFunctionDecl(org.abs_models.frontend.ast.PartialFunctionDecl) Decl(org.abs_models.frontend.ast.Decl) KindedName(org.abs_models.frontend.typechecker.KindedName) FunctionDecl(org.abs_models.frontend.ast.FunctionDecl) PartialFunctionDecl(org.abs_models.frontend.ast.PartialFunctionDecl)

Example 2 with Decl

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

the class DeltaAddFunctionalTest method addTypeSyn.

@Test
public void addTypeSyn() throws DeltaModellingException {
    Model model = assertParse("module M;" + "type X = Int;" + "delta I; uses M;" + "adds type Y = X;");
    Decl typeX = findDecl(model, "M", "X");
    assertNotNull(typeX);
    assertThat(typeX, instanceOf(TypeSynDecl.class));
    DeltaDecl delta = findDelta(model, "I");
    assertNotNull(delta);
    assertThat(delta, instanceOf(DeltaDecl.class));
    Decl typeY = findDecl(model, "M", "Y");
    assertNull(typeY);
    model.applyDelta(delta);
    typeY = findDecl(model, "M", "Y");
    assertNotNull(typeY);
    assertThat(typeY, instanceOf(TypeSynDecl.class));
}
Also used : Model(org.abs_models.frontend.ast.Model) DeltaDecl(org.abs_models.frontend.ast.DeltaDecl) FunctionDecl(org.abs_models.frontend.ast.FunctionDecl) TypeSynDecl(org.abs_models.frontend.ast.TypeSynDecl) Decl(org.abs_models.frontend.ast.Decl) DataTypeDecl(org.abs_models.frontend.ast.DataTypeDecl) ParametricDataTypeDecl(org.abs_models.frontend.ast.ParametricDataTypeDecl) DeltaDecl(org.abs_models.frontend.ast.DeltaDecl) TypeSynDecl(org.abs_models.frontend.ast.TypeSynDecl) Test(org.junit.Test)

Example 3 with Decl

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

the class DeltaAddFunctionalTest method addDataType.

@Test
public void addDataType() throws DeltaModellingException {
    Model model = assertParse("module M;" + "data O = O;" + "delta I; uses M;" + "adds data X<A> = X(A a) | N;" + "adds data Y = K | Y(Int i);");
    Decl dataO = findDecl(model, "M", "O");
    assertNotNull(dataO);
    assertThat(dataO, instanceOf(DataTypeDecl.class));
    DeltaDecl delta = findDelta(model, "I");
    assertNotNull(delta);
    assertThat(delta, instanceOf(DeltaDecl.class));
    Decl dataX = findDecl(model, "M", "X");
    assertNull(dataX);
    Decl dataY = findDecl(model, "M", "Y");
    assertNull(dataY);
    model.applyDelta(delta);
    dataX = findDecl(model, "M", "X");
    assertNotNull(dataX);
    assertThat(dataX, instanceOf(ParametricDataTypeDecl.class));
    dataY = findDecl(model, "M", "Y");
    assertNotNull(dataY);
    assertThat(dataY, instanceOf(DataTypeDecl.class));
}
Also used : Model(org.abs_models.frontend.ast.Model) DeltaDecl(org.abs_models.frontend.ast.DeltaDecl) FunctionDecl(org.abs_models.frontend.ast.FunctionDecl) TypeSynDecl(org.abs_models.frontend.ast.TypeSynDecl) Decl(org.abs_models.frontend.ast.Decl) DataTypeDecl(org.abs_models.frontend.ast.DataTypeDecl) ParametricDataTypeDecl(org.abs_models.frontend.ast.ParametricDataTypeDecl) DeltaDecl(org.abs_models.frontend.ast.DeltaDecl) ParametricDataTypeDecl(org.abs_models.frontend.ast.ParametricDataTypeDecl) DataTypeDecl(org.abs_models.frontend.ast.DataTypeDecl) ParametricDataTypeDecl(org.abs_models.frontend.ast.ParametricDataTypeDecl) Test(org.junit.Test)

Example 4 with Decl

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

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

the class DeltaAddFunctionalTest method addFun.

@Test
public void addFun() throws DeltaModellingException {
    Model model = assertParse("module M;" + "def Int i() = 1;" + "delta I; uses M;" + "adds def Int j<A>(A a) = 2;" + "adds def Int h() = 2;");
    Decl funI = findDecl(model, "M", "i");
    assertNotNull(funI);
    assertThat(funI, instanceOf(FunctionDecl.class));
    DeltaDecl delta = findDelta(model, "I");
    assertNotNull(delta);
    assertThat(delta, instanceOf(DeltaDecl.class));
    Decl funj = findDecl(model, "M", "j");
    assertNull(funj);
    Decl funh = findDecl(model, "M", "h");
    assertNull(funh);
    model.applyDelta(delta);
    funj = findDecl(model, "M", "j");
    assertNotNull(funj);
    assertThat(funj, instanceOf(FunctionDecl.class));
    funh = findDecl(model, "M", "h");
    assertNotNull(funh);
    assertThat(funh, instanceOf(FunctionDecl.class));
}
Also used : Model(org.abs_models.frontend.ast.Model) DeltaDecl(org.abs_models.frontend.ast.DeltaDecl) FunctionDecl(org.abs_models.frontend.ast.FunctionDecl) TypeSynDecl(org.abs_models.frontend.ast.TypeSynDecl) Decl(org.abs_models.frontend.ast.Decl) DataTypeDecl(org.abs_models.frontend.ast.DataTypeDecl) ParametricDataTypeDecl(org.abs_models.frontend.ast.ParametricDataTypeDecl) DeltaDecl(org.abs_models.frontend.ast.DeltaDecl) FunctionDecl(org.abs_models.frontend.ast.FunctionDecl) Test(org.junit.Test)

Aggregations

Decl (org.abs_models.frontend.ast.Decl)6 FunctionDecl (org.abs_models.frontend.ast.FunctionDecl)6 DataTypeDecl (org.abs_models.frontend.ast.DataTypeDecl)4 Model (org.abs_models.frontend.ast.Model)4 Test (org.junit.Test)4 DeltaDecl (org.abs_models.frontend.ast.DeltaDecl)3 ParametricDataTypeDecl (org.abs_models.frontend.ast.ParametricDataTypeDecl)3 TypeSynDecl (org.abs_models.frontend.ast.TypeSynDecl)3 ModuleDecl (org.abs_models.frontend.ast.ModuleDecl)2 ArrayList (java.util.ArrayList)1 WrongProgramArgumentException (org.abs_models.common.WrongProgramArgumentException)1 SemanticConditionList (org.abs_models.frontend.analyser.SemanticConditionList)1 DataConstructor (org.abs_models.frontend.ast.DataConstructor)1 DataConstructorExp (org.abs_models.frontend.ast.DataConstructorExp)1 ExpFunctionDef (org.abs_models.frontend.ast.ExpFunctionDef)1 Feature (org.abs_models.frontend.ast.Feature)1 List (org.abs_models.frontend.ast.List)1 PartialFunctionDecl (org.abs_models.frontend.ast.PartialFunctionDecl)1 ProductDecl (org.abs_models.frontend.ast.ProductDecl)1 ProductLine (org.abs_models.frontend.ast.ProductLine)1