package carray.visit;

import polyglot.ast.Node;
import polyglot.ast.NodeFactory;
import polyglot.frontend.Job;
import polyglot.types.TypeSystem;
import polyglot.util.Copy;
import polyglot.visit.ContextVisitor;
import polyglot.visit.NodeVisitor;
import carray.ast.CArrayLang;

/**
 * A visitor that translates array initializers to array creation expression.
 * Useful for translating carray to Java 5.
 */
public class ArrayInitRemover extends ContextVisitor {
    protected boolean inNewArray = false;

    public ArrayInitRemover(Job job, TypeSystem ts, NodeFactory nf) {
        super(job, ts, nf);
    }

    @Override
    public CArrayLang lang() {
        return (CArrayLang) super.lang();
    }

    public boolean inNewArray() {
        return inNewArray;
    }

    public ArrayInitRemover inNewArray(boolean inNewArray) {
        ArrayInitRemover v = Copy.Util.copy(this);
        v.inNewArray = inNewArray;
        return v;
    }

    @Override
    public ArrayInitRemover enterCall(Node n) {
        // Prepare the visitor for before visiting n by letting each AST node
        // decide what to do with the visitor.
        return lang().removeArrayInitEnter(n, this);
    }

    @Override
    public Node leaveCall(Node old, Node n, NodeVisitor v) {
        // After visiting old's children, the results of which are now children
        // of n, visit n itself to perform the operation that n's class defines
        // for this compiler pass.
        return lang().removeArrayInit(n, this);
    }
}
