package accrue.analysis.ext;

import java.util.LinkedHashMap;
import java.util.Map;

import polyglot.ast.Initializer;
import polyglot.util.InternalCompilerError;
import polyglot.util.SerialVersionUID;
import polyglot.visit.DataFlow;
import polyglot.visit.FlowGraph;
import polyglot.visit.FlowGraph.EdgeKey;
import polyglot.visit.FlowGraph.Peer;
import accrue.analysis.interprocanalysis.DataFlowDispatch;
import accrue.analysis.pointer.ConstructorContext;
import accrue.analysis.pointer.ReferenceVariable;

public class ExtInitializer extends ExtCodeDecl implements
        ExtInitializerOrFieldDecl {
    private static final long serialVersionUID = SerialVersionUID.generate();

    private Map<ConstructorContext, ReferenceVariable> thisNodes =
            new LinkedHashMap<ConstructorContext, ReferenceVariable>();

    @Override
    public <DFItem extends DataFlow.Item> Map<EdgeKey, DFItem> flowDispatch(
            DataFlowDispatch<DFItem> dataflow, DFItem dfIn,
            FlowGraph<DFItem> graph, Peer<DFItem> peer) {
        Initializer n = (Initializer) this.node();
        return dataflow.flowInitializer(n, dfIn, graph, peer);
    }

    @Override
    public void setThisNode(ConstructorContext constructorContext,
            ReferenceVariable thisNode) {
        if (this.thisNodes.containsKey(constructorContext)) {
            throw new InternalCompilerError("Already have a this node for constructor context "
                    + constructorContext);
        }
        this.thisNodes.put(constructorContext, thisNode);
    }

    @Override
    public ReferenceVariable getThisNode(ConstructorContext constructorContext) {
        return this.thisNodes.get(constructorContext);
    }

}
