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));
}
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));
}
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));
}
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));
}
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();
}
Aggregations