package accrue.analysis.ext;

import java.util.Map;

import polyglot.ast.Expr;
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.ast.Suppress;
import accrue.analysis.interprocanalysis.DataFlowDispatch;
import accrue.analysis.pointer.RegisterPointerStmtsVisitor;
import accrue.analysis.pointer.StmtRegistrar;

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

    @Override
    public void registerPointerStmts(StmtRegistrar registrar,
            RegisterPointerStmtsVisitor v) {

        Expr e = (Expr) this.node();
        if (!e.type().isReference()) return;

        if (v.recordExprResults()) {
            Suppress n = (Suppress) e;

            registrar.createFreshReferenceVariable(n, v.constructorContext());

            // only objects that are a subtype of e.type() will succeed in the
            // cast
            // and so we can filter this assignment.
            registrar.addAssignment(v.getReferenceVariable(n),
                                    v.getReferenceVariable(n.expr()),
                                    n.position(),
                                    v.currentCode());
        }
    }

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

}
