Search in sources :

Example 6 with ReturnStmt

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

the class JavaGeneratorHelper method generateMethodBody.

public static void generateMethodBody(PrintStream stream, final MethodImpl m, boolean isFliMethod) {
    boolean addReturn = false;
    if (m.getMethodSig().getReturnType().getType().isUnitType()) {
        if (m.getBlock().getNumStmt() == 0 || (!(m.getBlock().getStmt(m.getBlock().getNumStmt() - 1) instanceof ReturnStmt))) {
            addReturn = true;
        }
    }
    stream.println(" {");
    stream.println("__ABS_checkSameCOG(); ");
    if (!isFliMethod && m.isForeign()) {
        stream.print("return this.");
        stream.print(FLI_METHOD_PREFIX + JavaBackend.getMethodName(m.getMethodSig().getName()));
        JavaGeneratorHelper.generateParamArgs(stream, m.getMethodSig().getParams());
        stream.println(";");
    } else {
        stream.println("if (__ABS_getRuntime().debuggingEnabled()) {");
        stream.println(Task.class.getName() + "<?> __ABS_currentTask = __ABS_getRuntime().getCurrentTask();");
        stream.println("__ABS_currentTask.newStackFrame(this, \"" + m.getMethodSig().getName() + "\");");
        for (ParamDecl d : m.getMethodSig().getParams()) {
            stream.print("__ABS_currentTask.setLocalVariable(");
            stream.println("\"" + d.getName() + "\"," + d.getName() + ");");
        }
        stream.println("}");
        m.getBlock().generateJava(stream, addReturn);
    }
    stream.println("}");
}
Also used : ParamDecl(org.abs_models.frontend.ast.ParamDecl) ReturnStmt(org.abs_models.frontend.ast.ReturnStmt)

Example 7 with ReturnStmt

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

the class OtherAnalysisTests method awaitRewriteModule1.

@Test
public void awaitRewriteModule1() {
    Model m = assertParse("module A; export *; data X; module B; export *; data X; module C; import * from A; import B.X; class C { X m() { return await this!m();}}", Config.TYPE_CHECK, Config.WITHOUT_DESUGARING_AFTER_TYPECHECK);
    ClassDecl c = (ClassDecl) m.lookupModule("C").getDecl(0);
    Stmt stmt = c.getMethod(0).getBlock().getStmt(0);
    ReturnStmt ret = (ReturnStmt) stmt;
    assertThat(ret.getRetExp().getType(), instanceOf(DataTypeType.class));
    assertEquals("A.X", ret.getRetExp().getType().getQualifiedName());
    m = assertParse("module A; export *; data X; module B; export *; data X; module C; import * from A; import B.X; class C { X m() { return await this!m();}}", Config.TYPE_CHECK);
    c = (ClassDecl) m.lookupModule("C").getDecl(0);
    Stmt s = c.getMethod(0).getBlock().getStmt(0);
    VarDeclStmt b = (VarDeclStmt) s;
    Type t = ((DataTypeType) b.getVarDecl().getType()).getTypeArg(0);
    assertEquals("A.X", t.getQualifiedName());
}
Also used : Type(org.abs_models.frontend.typechecker.Type) DataTypeType(org.abs_models.frontend.typechecker.DataTypeType) ClassDecl(org.abs_models.frontend.ast.ClassDecl) VarDeclStmt(org.abs_models.frontend.ast.VarDeclStmt) Model(org.abs_models.frontend.ast.Model) DataTypeType(org.abs_models.frontend.typechecker.DataTypeType) ReturnStmt(org.abs_models.frontend.ast.ReturnStmt) Stmt(org.abs_models.frontend.ast.Stmt) ReturnStmt(org.abs_models.frontend.ast.ReturnStmt) VarDeclStmt(org.abs_models.frontend.ast.VarDeclStmt) FrontendTest(org.abs_models.frontend.FrontendTest) Test(org.junit.Test)

Example 8 with ReturnStmt

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

the class TypingTest method testThisTyping.

@Test
public void testThisTyping() {
    Model m = assertParse("class C implements I { I m() { return this; } } interface I { }");
    ClassDecl d = (ClassDecl) m.lookupModule("UnitTest").getDecl(0);
    ReturnStmt s = (ReturnStmt) d.getMethod(0).getBlock().getStmt(0);
    assertEquals(m.lookupModule("UnitTest").getDecl(1), ((UnionType) s.getRetExp().getType()).getType(0).getDecl());
}
Also used : UnionType(org.abs_models.frontend.typechecker.UnionType) ClassDecl(org.abs_models.frontend.ast.ClassDecl) Model(org.abs_models.frontend.ast.Model) ReturnStmt(org.abs_models.frontend.ast.ReturnStmt) FrontendTest(org.abs_models.frontend.FrontendTest) Test(org.junit.Test)

Example 9 with ReturnStmt

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

the class TypeCheckerTest method classParamsMethodShadowsField.

@Test
public void classParamsMethodShadowsField() {
    Model m = assertParse("class C(Bool b) { Bool m(Bool b) { 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(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 10 with ReturnStmt

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

the class OriginalCallTest method originalCall3.

@Test
public void originalCall3() throws DeltaModellingException {
    Model model = assertParse("module M;" + "interface I {}" + "class C implements I { Int one() { return 1; } }" + "delta D; uses M;" + "modifies class C { modifies Int one() { Int x = original(); return x + 1; } }");
    ClassDecl cls = (ClassDecl) findDecl(model, "M", "C");
    assertEquals(1, cls.getMethods().getNumChild());
    DeltaDecl delta1 = findDelta(model, "D");
    // Model.resolveOriginalCalls(new ArrayList<DeltaDecl>(Arrays.asList(delta1)));
    model.applyDeltas(new ArrayList<>(Arrays.asList(delta1)));
    assertEquals(2, cls.getMethods().getNumChild());
    assertTrue(cls.getMethod(0).getMethodSig().getName().equals("one"));
    // make sure method has the right body
    assertTrue(cls.getMethod(0).getBlock().getStmt(1) instanceof ReturnStmt);
    assertTrue(cls.getMethod(1).getMethodSig().getName().equals("one$ORIGIN_core"));
    // make sure method has the right body
    assertTrue(cls.getMethod(1).getBlock().getStmt(0) instanceof ReturnStmt);
}
Also used : ClassDecl(org.abs_models.frontend.ast.ClassDecl) Model(org.abs_models.frontend.ast.Model) DeltaDecl(org.abs_models.frontend.ast.DeltaDecl) ReturnStmt(org.abs_models.frontend.ast.ReturnStmt) Test(org.junit.Test)

Aggregations

ReturnStmt (org.abs_models.frontend.ast.ReturnStmt)11 ClassDecl (org.abs_models.frontend.ast.ClassDecl)10 Test (org.junit.Test)10 FrontendTest (org.abs_models.frontend.FrontendTest)9 Model (org.abs_models.frontend.ast.Model)9 KindedName (org.abs_models.frontend.typechecker.KindedName)6 MethodImpl (org.abs_models.frontend.ast.MethodImpl)4 ParamDecl (org.abs_models.frontend.ast.ParamDecl)4 ABSTest (org.abs_models.ABSTest)3 ModuleDecl (org.abs_models.frontend.ast.ModuleDecl)3 VarOrFieldUse (org.abs_models.frontend.ast.VarOrFieldUse)3 FieldDecl (org.abs_models.frontend.ast.FieldDecl)2 MethodSig (org.abs_models.frontend.ast.MethodSig)2 Stmt (org.abs_models.frontend.ast.Stmt)2 VarUse (org.abs_models.frontend.ast.VarUse)2 DeltaDecl (org.abs_models.frontend.ast.DeltaDecl)1 Exp (org.abs_models.frontend.ast.Exp)1 FieldUse (org.abs_models.frontend.ast.FieldUse)1 VarDeclStmt (org.abs_models.frontend.ast.VarDeclStmt)1 VarOrFieldDecl (org.abs_models.frontend.ast.VarOrFieldDecl)1