Search in sources :

Example 1 with ModuleDecl

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

the class TypeCheckerTest method classParamsRewrite.

@Test
public void classParamsRewrite() {
    Model m = assertParse("class C(Bool b) { Bool m() { return b; } }");
    ModuleDecl u = m.lookupModule("UnitTest");
    ClassDecl c = (ClassDecl) u.lookup(new KindedName(KindedName.Kind.CLASS, "C"));
    MethodImpl me = c.lookupMethod("m");
    ReturnStmt r = (ReturnStmt) me.getBlock().getStmt(0);
    VarOrFieldUse vu = (VarOrFieldUse) r.getRetExp();
    ParamDecl d = (ParamDecl) vu.getDecl();
    assertThat(d.getParent().getParent(), instanceOf(ClassDecl.class));
    assertThat(vu.getClass().getName(), vu, instanceOf(FieldUse.class));
}
Also used : ClassDecl(org.abs_models.frontend.ast.ClassDecl) MethodImpl(org.abs_models.frontend.ast.MethodImpl) ParamDecl(org.abs_models.frontend.ast.ParamDecl) FieldUse(org.abs_models.frontend.ast.FieldUse) VarOrFieldUse(org.abs_models.frontend.ast.VarOrFieldUse) Model(org.abs_models.frontend.ast.Model) ModuleDecl(org.abs_models.frontend.ast.ModuleDecl) KindedName(org.abs_models.frontend.typechecker.KindedName) ReturnStmt(org.abs_models.frontend.ast.ReturnStmt) VarOrFieldUse(org.abs_models.frontend.ast.VarOrFieldUse) ABSTest(org.abs_models.ABSTest) FrontendTest(org.abs_models.frontend.FrontendTest) Test(org.junit.Test)

Example 2 with ModuleDecl

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

the class TypeCheckerTest method classParamsRewrite2.

@Test
public void classParamsRewrite2() {
    Model m = assertParse("class C(Bool b) { Bool m(Bool x) { return x; } }");
    ModuleDecl u = m.lookupModule("UnitTest");
    ClassDecl c = (ClassDecl) u.lookup(new KindedName(KindedName.Kind.CLASS, "C"));
    MethodImpl me = c.lookupMethod("m");
    ReturnStmt r = (ReturnStmt) me.getBlock().getStmt(0);
    VarOrFieldUse vu = (VarOrFieldUse) r.getRetExp();
    ParamDecl d = (ParamDecl) vu.getDecl();
    assertThat(d.getParent().getParent(), instanceOf(MethodSig.class));
    assertThat(vu.getClass().getName(), vu, instanceOf(VarUse.class));
}
Also used : MethodSig(org.abs_models.frontend.ast.MethodSig) ClassDecl(org.abs_models.frontend.ast.ClassDecl) MethodImpl(org.abs_models.frontend.ast.MethodImpl) ParamDecl(org.abs_models.frontend.ast.ParamDecl) Model(org.abs_models.frontend.ast.Model) ModuleDecl(org.abs_models.frontend.ast.ModuleDecl) KindedName(org.abs_models.frontend.typechecker.KindedName) VarUse(org.abs_models.frontend.ast.VarUse) ReturnStmt(org.abs_models.frontend.ast.ReturnStmt) VarOrFieldUse(org.abs_models.frontend.ast.VarOrFieldUse) ABSTest(org.abs_models.ABSTest) FrontendTest(org.abs_models.frontend.FrontendTest) Test(org.junit.Test)

Example 3 with ModuleDecl

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

the class AddImportsTest method addExport.

@Test
public void addExport() throws DeltaModellingException {
    Model model = assertParse("module Exporter;" + "interface I {}" + "module M;" + "class C {}" + "delta D1; uses Exporter;" + "adds export I;" + "delta D2; uses M;" + "adds import Exporter.I;" + "modifies class C { adds Exporter.I field1; } ");
    ClassDecl cls = (ClassDecl) findDecl(model, "M", "C");
    DeltaDecl d1 = findDelta(model, "D1");
    DeltaDecl d2 = findDelta(model, "D2");
    model.applyDeltas(new ArrayList<>(Arrays.asList(d1, d2)));
    ModuleDecl clsmodule = cls.getModuleDecl();
    Map<KindedName, ResolvedName> clsVisibleSymbols = clsmodule.getVisibleNames();
    KindedName symbol1 = new KindedName(KindedName.Kind.TYPE_DECL, "Exporter.I");
    assertTrue(clsVisibleSymbols.containsKey(symbol1));
}
Also used : ClassDecl(org.abs_models.frontend.ast.ClassDecl) Model(org.abs_models.frontend.ast.Model) ModuleDecl(org.abs_models.frontend.ast.ModuleDecl) ResolvedName(org.abs_models.frontend.typechecker.ResolvedName) KindedName(org.abs_models.frontend.typechecker.KindedName) DeltaDecl(org.abs_models.frontend.ast.DeltaDecl) Test(org.junit.Test)

Example 4 with ModuleDecl

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

the class AddImportsTest method addImport.

@Test
public void addImport() throws DeltaModellingException {
    Model model = assertParse("module Exporter; export *;" + "interface I {}" + "interface J {}" + "module Exporter2; export *;" + "interface K {}" + "module M;" + "class C {}" + "delta D; uses M;" + "adds import Exporter.I;" + "adds import J from Exporter;" + "adds import * from Exporter2;" + "modifies class C { adds Exporter.I field1; } " + "modifies class C { adds          J field2; } " + "modifies class C { adds          K field3; } ");
    ClassDecl cls = (ClassDecl) findDecl(model, "M", "C");
    DeltaDecl delta = findDelta(model, "D");
    model.applyDeltas(new ArrayList<>(Arrays.asList(delta)));
    ModuleDecl clsmodule = cls.getModuleDecl();
    Map<KindedName, ResolvedName> clsVisibleSymbols = clsmodule.getVisibleNames();
    KindedName symbol1 = new KindedName(KindedName.Kind.TYPE_DECL, "Exporter.I");
    KindedName symbol2 = new KindedName(KindedName.Kind.TYPE_DECL, "J");
    KindedName symbol3 = new KindedName(KindedName.Kind.TYPE_DECL, "K");
    assertTrue(clsVisibleSymbols.containsKey(symbol1));
    assertTrue(clsVisibleSymbols.containsKey(symbol2));
    assertTrue(clsVisibleSymbols.containsKey(symbol3));
}
Also used : ClassDecl(org.abs_models.frontend.ast.ClassDecl) Model(org.abs_models.frontend.ast.Model) ModuleDecl(org.abs_models.frontend.ast.ModuleDecl) ResolvedName(org.abs_models.frontend.typechecker.ResolvedName) KindedName(org.abs_models.frontend.typechecker.KindedName) DeltaDecl(org.abs_models.frontend.ast.DeltaDecl) Test(org.junit.Test)

Example 5 with ModuleDecl

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

Aggregations

ModuleDecl (org.abs_models.frontend.ast.ModuleDecl)11 Model (org.abs_models.frontend.ast.Model)8 Test (org.junit.Test)7 ClassDecl (org.abs_models.frontend.ast.ClassDecl)5 KindedName (org.abs_models.frontend.typechecker.KindedName)5 ABSTest (org.abs_models.ABSTest)4 FrontendTest (org.abs_models.frontend.FrontendTest)4 MethodImpl (org.abs_models.frontend.ast.MethodImpl)3 MethodSig (org.abs_models.frontend.ast.MethodSig)3 ParamDecl (org.abs_models.frontend.ast.ParamDecl)3 ReturnStmt (org.abs_models.frontend.ast.ReturnStmt)3 VarOrFieldUse (org.abs_models.frontend.ast.VarOrFieldUse)3 ArrayList (java.util.ArrayList)2 CompilationUnit (org.abs_models.frontend.ast.CompilationUnit)2 Decl (org.abs_models.frontend.ast.Decl)2 DeltaDecl (org.abs_models.frontend.ast.DeltaDecl)2 FunctionDecl (org.abs_models.frontend.ast.FunctionDecl)2 VarUse (org.abs_models.frontend.ast.VarUse)2 ResolvedName (org.abs_models.frontend.typechecker.ResolvedName)2 PrintWriter (java.io.PrintWriter)1