package accrue.analysis.readwritesets;

import java.util.HashMap;
import java.util.Map;

import polyglot.frontend.ExtensionInfo;
import polyglot.frontend.goals.Goal;
import accrue.AccrueExtensionInfo;
import accrue.analysis.goals.RegisterProceduresGoal;
import accrue.analysis.interprocanalysis.AnalysisFactory;
import accrue.analysis.interprocanalysis.InterProcAnalysisPass;
import accrue.analysis.interprocanalysis.Registrar;

/**
 * Analysis pass for analysis that gathers the locations read and written by 
 * procedures 
 */
public class ReadWriteSetsPass extends InterProcAnalysisPass<ReadWriteSet> {

    /**
     * Singleton passes
     */
    static private Map<AccrueExtensionInfo, ReadWriteSetsPass> singletons =
            new HashMap<AccrueExtensionInfo, ReadWriteSetsPass>();

    /**
     * Create a singleton pass
     * 
     * @param extInfo 
     *          Accrue extension information
     * @param g
     *          Goal for the analysis pass
     * 
     * @return new analysis pass
     */
    static public ReadWriteSetsPass singleton(AccrueExtensionInfo extInfo,
            Goal g) {
        ReadWriteSetsPass singleton = singletons.get(extInfo);
        if (singleton == null) {
            singleton =
                    new ReadWriteSetsPass(extInfo,
                                          g,
                                          RegisterProceduresGoal.registrar(extInfo),
                                          new ReadWriteSetsAnalysisFactory(extInfo));
            singletons.put(extInfo, singleton);
        }
        return singleton;
    }

    /**
     * Create a new analysis pass
     * 
     * @param extInfo
     *          Accrue extension information
     * @param g
     *          Goal for the analysis pass
     * @param registrar
     *          Registers analysis results
     * @param analysisFactory
     *          {@link AnalysisFactory} for this type of analysis
     */
    private ReadWriteSetsPass(ExtensionInfo extInfo, Goal g,
            Registrar registrar, AnalysisFactory<ReadWriteSet> analysisFactory) {
        super(extInfo, g, registrar, analysisFactory);
    }
}
