Search in sources :

Example 6 with ParamDecl

use of abs.frontend.ast.ParamDecl 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(abs.frontend.ast.ParamDecl) ReturnStmt(abs.frontend.ast.ReturnStmt)

Example 7 with ParamDecl

use of abs.frontend.ast.ParamDecl 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(abs.frontend.ast.FieldDecl) MethodImpl(abs.frontend.ast.MethodImpl) ParamDecl(abs.frontend.ast.ParamDecl) ABSField(abs.backend.java.lib.runtime.ABSField) ABSDynamicClass(abs.backend.java.lib.runtime.ABSDynamicClass)

Example 8 with ParamDecl

use of abs.frontend.ast.ParamDecl in project abstools by abstools.

the class ErlUtil method functionHeader.

// Not used when garbage collector pause points are added to functions
// Consider removing
public static final void functionHeader(CodeStream ecs, String funName, abs.frontend.ast.List<ParamDecl> args) {
    List<String> a = new ArrayList<>(args.getNumChild());
    for (ParamDecl p : args) a.add(p.getName());
    functionHeader(ecs, funName, a, Mask.all);
}
Also used : ParamDecl(abs.frontend.ast.ParamDecl) ArrayList(java.util.ArrayList)

Example 9 with ParamDecl

use of abs.frontend.ast.ParamDecl in project abstools by abstools.

the class ErlUtil method functionHeaderParamsAsList.

public static final void functionHeaderParamsAsList(CodeStream ecs, String funName, String firstParameter, abs.frontend.ast.List<ParamDecl> args, Mask mask) {
    StringBuilder b = new StringBuilder("[");
    boolean first = true;
    for (ParamDecl p : args) {
        if (!first)
            b.append(',');
        else
            first = false;
        b.append("P_" + p.getName());
    }
    if (args.hasChildren()) {
        b.append(',');
    }
    b.append("Stack]");
    functionHeader(ecs, funName, mask, firstParameter, b.toString());
}
Also used : ParamDecl(abs.frontend.ast.ParamDecl)

Example 10 with ParamDecl

use of abs.frontend.ast.ParamDecl in project abstools by abstools.

the class JavaGeneratorHelper method generateParamArgs.

public static void generateParamArgs(PrintStream stream, String firstArg, List<ParamDecl> params) {
    stream.print("(");
    boolean first = true;
    if (firstArg != null) {
        stream.print(firstArg);
        first = false;
    }
    for (ParamDecl d : params) {
        if (!first)
            stream.print(", ");
        stream.print(JavaBackend.getVariableName(d.getName()));
        first = false;
    }
    stream.print(")");
}
Also used : ParamDecl(abs.frontend.ast.ParamDecl)

Aggregations

ParamDecl (abs.frontend.ast.ParamDecl)16 FieldDecl (abs.frontend.ast.FieldDecl)6 MethodImpl (abs.frontend.ast.MethodImpl)4 MethodSig (abs.frontend.ast.MethodSig)4 VarUse (abs.frontend.ast.VarUse)4 AbsASTBuilderUtil.createMethodSig (abs.backend.tests.AbsASTBuilderUtil.createMethodSig)3 Annotation (abs.frontend.ast.Annotation)3 AddMethodModifier (abs.frontend.ast.AddMethodModifier)2 Block (abs.frontend.ast.Block)2 ClassDecl (abs.frontend.ast.ClassDecl)2 FieldUse (abs.frontend.ast.FieldUse)2 PureExp (abs.frontend.ast.PureExp)2 ReturnStmt (abs.frontend.ast.ReturnStmt)2 ArrayList (java.util.ArrayList)2 ABSDynamicClass (abs.backend.java.lib.runtime.ABSDynamicClass)1 ABSField (abs.backend.java.lib.runtime.ABSField)1 ABSValue (abs.backend.java.lib.types.ABSValue)1 DeclNamePredicate (abs.backend.tests.AbsASTBuilderUtil.DeclNamePredicate)1 AbsASTBuilderUtil.createMethodImpl (abs.backend.tests.AbsASTBuilderUtil.createMethodImpl)1 AbsASTBuilderUtil.getCall (abs.backend.tests.AbsASTBuilderUtil.getCall)1