package net.folab.fo.ast;

import java.util.ArrayList;
import java.util.List;

import net.folab.fo.bytecode.Access;
import net.folab.fo.bytecode.JavaType;
import net.folab.fo.jast.Block;
import net.folab.fo.jast.Statement;

public class FunctionDeclaration {

    public final ClassGenerator cg;

    public final Access access;

    public final JavaType returnType;

    public final String name;

    public final JavaType[] parameterTypes;

    public final Block block;

    public FunctionDeclaration(ClassGenerator cg, Access access,
            JavaType returnType, String name, JavaType[] parameterTypes,
            boolean isStatic, List<Statement> statements) {
        this.cg = cg;
        this.access = access;
        this.returnType = returnType;
        this.name = name;
        this.parameterTypes = parameterTypes;
        this.block = new Block(isStatic, statements);
    }

    public static FunctionDeclaration build(String name) {
        return new FunctionDeclaration(null, Access.PUBLIC, JavaType.VOID,
                name, new JavaType[0], false, new ArrayList<Statement>());
    }

    public void accept(AstVisitor cv) {
        cv.visitMethod(this);
    }

    public FunctionDeclaration setClassGenerator(ClassGenerator cg) {
        return new FunctionDeclaration(cg, access, returnType, name,
                parameterTypes, block.isStatic, block.statements);
    }

    public FunctionDeclaration setAccess(Access access) {
        return new FunctionDeclaration(cg, access, returnType, name,
                parameterTypes, block.isStatic, block.statements);
    }

    public FunctionDeclaration setReturnType(JavaType returnType) {
        return new FunctionDeclaration(cg, access, returnType, name,
                parameterTypes, block.isStatic, block.statements);
    }

    public FunctionDeclaration setParameterTypes(JavaType... parameterTypes) {
        return new FunctionDeclaration(cg, access, returnType, name,
                parameterTypes, block.isStatic, block.statements);
    }

    public FunctionDeclaration addStatement(Statement statement) {
        List<Statement> statements = new ArrayList<Statement>(block.statements);
        statements.add(statement);
        return new FunctionDeclaration(cg, access, returnType, name,
                parameterTypes, block.isStatic, statements);
    }

}

@@ -257,45 +257,8 @@
  %7B%0A%0A
-    public final ClassGenerator cg;%0A%0A
     
@@ -467,27 +467,8 @@
 ion(
-ClassGenerator cg, 
 Acce
@@ -477,28 +477,16 @@
  access,
-%0A           
  JavaTyp
@@ -498,16 +498,28 @@
 urnType,
+%0A           
  String 
@@ -550,28 +550,16 @@
 erTypes,
-%0A           
  boolean
@@ -568,16 +568,28 @@
 sStatic,
+%0A           
  List%3CSt
@@ -615,30 +615,8 @@
 ) %7B%0A
-        this.cg = cg;%0A
     
@@ -914,14 +914,8 @@
 ion(
-null, 
 Acce
@@ -939,16 +939,22 @@
 pe.VOID,
+ name,
 %0A       
@@ -961,22 +961,16 @@
         
- name,
  new Jav
@@ -1200,36 +1200,32 @@
 tionDeclaration(
-cg, 
 access, returnTy
@@ -1233,32 +1233,16 @@
 e, name,
-%0A               
  paramet
@@ -1241,32 +1241,48 @@
  parameterTypes,
+%0A               
  block.isStatic,
@@ -1397,36 +1397,32 @@
 tionDeclaration(
-cg, 
 access, returnTy
@@ -1430,32 +1430,16 @@
 e, name,
-%0A               
  paramet
@@ -1438,32 +1438,48 @@
  parameterTypes,
+%0A               
  block.isStatic,
@@ -1604,36 +1604,32 @@
 tionDeclaration(
-cg, 
 access, returnTy
@@ -1637,32 +1637,16 @@
 e, name,
-%0A               
  paramet
@@ -1645,32 +1645,48 @@
  parameterTypes,
+%0A               
  block.isStatic,
@@ -1822,36 +1822,32 @@
 tionDeclaration(
-cg, 
 access, returnTy
@@ -1855,32 +1855,16 @@
 e, name,
-%0A               
  paramet
@@ -1863,32 +1863,48 @@
  parameterTypes,
+%0A               
  block.isStatic,
@@ -2156,12 +2156,8 @@
 ion(
-cg, 
 acce
@@ -2177,32 +2177,16 @@
 e, name,
-%0A               
  paramet
@@ -2193,16 +2193,32 @@
 erTypes,
+%0A               
  block.i

Remove ClassGenerator field