use of org.abs_models.frontend.ast.MethodImpl in project abstools by abstools.
the class ClassDeclGenerator method generateClassBody.
private void generateClassBody() {
// singleton() method instantiates strictly one dynamic class object and does initialisation,
// i.e. adds fields, constructor and methods.
stream.println("{");
stream.println("private static " + ABSDynamicClass.class.getName() + " instance;");
stream.println("public static void setInstance(" + ABSDynamicClass.class.getName() + " cls) { instance = cls; }");
stream.println("public static " + ABSDynamicClass.class.getName() + " singleton() {");
stream.println("if (instance == null) {");
stream.println("instance = new " + ABSDynamicClass.class.getName() + "();");
stream.println("instance.setName(\"" + decl.getName() + "\");");
// add class parameters
for (ParamDecl p : decl.getParams()) {
String name = JavaBackend.getVariableName(p.getName());
stream.println("instance.addField(\"" + name + "\", new " + ABSField.class.getName() + "());");
}
// add fields
for (FieldDecl f : decl.getFields()) {
String name = JavaBackend.getVariableName(f.getName());
stream.println("instance.addField(\"" + name + "\", " + className + ".field$" + name + ".singleton());");
}
// add constructor
stream.println("instance.setConstructor(" + className + ".CON$TRUCT.singleton());");
stream.print("instance.setParams(");
for (int i = 0; i < decl.getNumParam(); i++) {
if (i != 0)
stream.print(", ");
stream.print("\"" + JavaBackend.getVariableName(decl.getParam(i).getName()) + "\"");
}
stream.println(");");
// add methods
for (MethodImpl m : decl.getMethods()) {
String methodName = m.getMethodSig().getName();
stream.println("instance.addMethod(\"" + methodName + "\", " + className + "." + methodName + ".singleton());");
}
stream.println("}");
stream.println("return instance;");
stream.println("}");
// generate field inner classes
for (FieldDecl field : decl.getFields()) {
field.generateJavaDynamic(stream);
}
// generate CON$TRUCT inner class
generateConstructor();
// generate method inner classes
for (MethodImpl method : decl.getMethods()) {
method.generateJavaDynamic(stream);
// FIXME not sure how to handle FLI methods
if (method.isForeign()) {
stream.println("/* FLI method: not implemented yet */");
DynamicJavaGeneratorHelper.generateFLIMethod(stream, method);
}
}
generateCreateNewCOGMethod();
generateNewObjectMethods();
stream.println("}");
}
use of org.abs_models.frontend.ast.MethodImpl in project abstools by abstools.
the class TraitTest method addAddModifierAtRuntimeBackComp.
@Test
public void addAddModifierAtRuntimeBackComp() {
Model model = assertParse("module M;" + "class C { Unit m(){skip;} }");
ClassDecl cls = (ClassDecl) findDecl(model, "M", "C");
MethodSig sig = AbsASTBuilderUtil.createMethodSig("n", AbsASTBuilderUtil.getUnit());
MethodImpl impl = new MethodImpl(sig, new Block(new List<>(), new List<>(new SkipStmt())));
AddMethodModifier opr = new AddMethodModifier(impl);
assertNotNull(opr.getMethodImpl());
ModifyClassModifier mcn = new ModifyClassModifier();
mcn.setName("M.C");
DeltaAccess acc = new DeltaAccess(cls.getModuleDecl().getName());
DeltaDecl dd = new DeltaDecl();
dd.setName("MyDelta");
dd.setImportedModule(acc);
dd.addModuleModifier(mcn);
mcn.addModifier(opr);
mcn.setParent(dd);
acc.setParent(dd);
opr.setParent(mcn);
sig.setParent(opr);
CompilationUnit cu = model.getCompilationUnitList().getChild(0);
cu.addDeltaDecl(dd);
dd.setParent(cu);
model.applyDelta(dd);
assertEquals(2, cls.getMethods().getNumChild());
}
use of org.abs_models.frontend.ast.MethodImpl 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.MethodImpl 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.MethodImpl in project abstools by abstools.
the class FreeVarTest method fieldUse.
@Test
public void fieldUse() {
ClassDecl clazz = getFirstClassDecl(assertParse("class C {" + "Int i = 0;" + "Int m() {" + "return i + 1;" + "}" + "}"));
MethodImpl method = clazz.lookupMethod("m");
assertNotNull(method);
Stmt stmt = method.getBlock().getStmt(0);
assertTrue(stmt instanceof ReturnStmt);
ReturnStmt returnStmt = (ReturnStmt) stmt;
Exp exp = returnStmt.getRetExp();
assertEquals(exp.getFreeVars(), "i");
}
Aggregations