package accrue.analysis.ext;

import java.util.Map;

import polyglot.ast.LocalDecl;
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.RegisterPointerStmtsVisitor;
import accrue.analysis.pointer.StmtRegistrar;

public class ExtLocalDecl extends ExtTerm {
    private static final long serialVersionUID = SerialVersionUID.generate();

    @Override
    public void registerPointerStmts(StmtRegistrar registrar,
            RegisterPointerStmtsVisitor v) {
        LocalDecl ld = (LocalDecl) this.node();
        if (ld.init() != null && ld.init().type().isReference()) {
            registrar.addAssignment(registrar.getLocalNode(ld.localInstance(),
                                                           v.currentCode(),
                                                           v.constructorContext()),
                                    v.getReferenceVariable(ld.init()),
                                    ld.position(),
                                    v.currentCode());
        }
    }

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