Search in sources :

Example 1 with MethodImpl

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("}");
}
Also used : FieldDecl(org.abs_models.frontend.ast.FieldDecl) MethodImpl(org.abs_models.frontend.ast.MethodImpl) ParamDecl(org.abs_models.frontend.ast.ParamDecl) ABSField(org.abs_models.backend.java.lib.runtime.ABSField) ABSDynamicClass(org.abs_models.backend.java.lib.runtime.ABSDynamicClass)

Example 2 with MethodImpl

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());
}
Also used : CompilationUnit(org.abs_models.frontend.ast.CompilationUnit) MethodSig(org.abs_models.frontend.ast.MethodSig) ClassDecl(org.abs_models.frontend.ast.ClassDecl) MethodImpl(org.abs_models.frontend.ast.MethodImpl) DeltaAccess(org.abs_models.frontend.ast.DeltaAccess) AddMethodModifier(org.abs_models.frontend.ast.AddMethodModifier) Model(org.abs_models.frontend.ast.Model) Block(org.abs_models.frontend.ast.Block) List(org.abs_models.frontend.ast.List) DeltaDecl(org.abs_models.frontend.ast.DeltaDecl) SkipStmt(org.abs_models.frontend.ast.SkipStmt) ModifyClassModifier(org.abs_models.frontend.ast.ModifyClassModifier) Test(org.junit.Test)

Example 3 with MethodImpl

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));
}
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 4 with MethodImpl

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));
}
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 5 with MethodImpl

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");
}
Also used : ClassDecl(org.abs_models.frontend.ast.ClassDecl) MethodImpl(org.abs_models.frontend.ast.MethodImpl) Exp(org.abs_models.frontend.ast.Exp) ReturnStmt(org.abs_models.frontend.ast.ReturnStmt) Stmt(org.abs_models.frontend.ast.Stmt) ReturnStmt(org.abs_models.frontend.ast.ReturnStmt) FrontendTest(org.abs_models.frontend.FrontendTest) Test(org.junit.Test)

Aggregations

MethodImpl (org.abs_models.frontend.ast.MethodImpl)8 ClassDecl (org.abs_models.frontend.ast.ClassDecl)6 Test (org.junit.Test)6 MethodSig (org.abs_models.frontend.ast.MethodSig)5 Model (org.abs_models.frontend.ast.Model)5 ParamDecl (org.abs_models.frontend.ast.ParamDecl)5 FrontendTest (org.abs_models.frontend.FrontendTest)4 ReturnStmt (org.abs_models.frontend.ast.ReturnStmt)4 ABSTest (org.abs_models.ABSTest)3 ModuleDecl (org.abs_models.frontend.ast.ModuleDecl)3 VarOrFieldUse (org.abs_models.frontend.ast.VarOrFieldUse)3 KindedName (org.abs_models.frontend.typechecker.KindedName)3 Block (org.abs_models.frontend.ast.Block)2 CompilationUnit (org.abs_models.frontend.ast.CompilationUnit)2 DeltaAccess (org.abs_models.frontend.ast.DeltaAccess)2 DeltaDecl (org.abs_models.frontend.ast.DeltaDecl)2 List (org.abs_models.frontend.ast.List)2 ModifyClassModifier (org.abs_models.frontend.ast.ModifyClassModifier)2 SkipStmt (org.abs_models.frontend.ast.SkipStmt)2 VarUse (org.abs_models.frontend.ast.VarUse)2