/* This file was generated by SableCC (http://www.sablecc.org/). 
 * Then modified.
 */
package ast.node;

import java.util.*;
import ast.visitor.*;

@SuppressWarnings("nls")
public final class CallStatement extends IStatement
{
    private IExp _exp_;
    private String _id_;
    private final LinkedList<IExp> _args_ = new LinkedList<IExp>();

    public CallStatement(int _line_, int _pos_, IExp _exp_, 
            String _id_, List<IExp> _args_)
    {
        super(_line_, _pos_);
        
        setExp(_exp_);

        setId(_id_);

        setArgs(_args_);

    }

    @Override
    public int getNumExpChildren() { return 1+this._args_.size(); }
    

    @Override
    public Object clone()
    {
        return new CallStatement(
                this.getLine(),
                this.getPos(),
                cloneNode(this._exp_),
                this._id_,
                cloneList(this._args_));
    }

    public void accept(Visitor v)
    {
        v.visitCallStatement(this);
    }

    public IExp getExp()
    {
        return this._exp_;
    }

    public void setExp(IExp node)
    {
        if(this._exp_ != null)
        {
            this._exp_.parent(null);
        }

        if(node != null)
        {
            if(node.parent() != null)
            {
                node.parent().removeChild(node);
            }

            node.parent(this);
        }

        this._exp_ = node;
    }

    public String getId()
    {
        return this._id_;
    }

    public void setId(String id)
    {
        this._id_ = id;
    }

    public LinkedList<IExp> getArgs()
    {
        return this._args_;
    }

    public void setArgs(List<IExp> list)
    {
        this._args_.clear();
        this._args_.addAll(list);
        for(IExp e : list)
        {
            if(e.parent() != null)
            {
                e.parent().removeChild(e);
            }

            e.parent(this);
        }
    }

    @Override
    void removeChild(Node child)
    {
        // Remove child
        if(this._exp_ == child)
        {
            this._exp_ = null;
            return;
        }

        if(this._args_.remove(child))
        {
            return;
        }

        throw new RuntimeException("Not a child.");
    }

    @Override
    void replaceChild(Node oldChild, Node newChild)
    {
        // Replace child
        if(this._exp_ == oldChild)
        {
            setExp((IExp) newChild);
            return;
        }

        for(ListIterator<IExp> i = this._args_.listIterator(); i.hasNext();)
        {
            if(i.next() == oldChild)
            {
                if(newChild != null)
                {
                    i.set((IExp) newChild);
                    newChild.parent(this);
                    oldChild.parent(null);
                    return;
                }

                i.remove();
                oldChild.parent(null);
                return;
            }
        }

        throw new RuntimeException("Not a child.");
    }
}
