/**
 * 
 */
package accrue.analysis.preciseExceptions;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import polyglot.frontend.ExtensionInfo;
import polyglot.frontend.Pass;
import polyglot.frontend.Scheduler;
import polyglot.frontend.goals.AbstractGoal;
import polyglot.frontend.goals.Goal;
import accrue.AccrueExtensionInfo;
import accrue.AccrueScheduler;

public class PreciseExGoal extends AbstractGoal {
    public static Goal singleton(ExtensionInfo extInfo, int i) {
        Scheduler scheduler = extInfo.scheduler();
        return scheduler.internGoal(new PreciseExGoal(i));
    }

    protected final int i;

    protected PreciseExGoal(int i) {
        super(null, "PreciseExceptionGoal:" + i);
        this.i = i;
    }

    @Override
    public Collection<Goal> prerequisiteGoals(Scheduler scheduler) {
        List<Goal> l = new ArrayList<Goal>();
        l.add(((AccrueScheduler) scheduler).helper().RegisterProceduresDone());
        l.add(((AccrueScheduler) scheduler).helper().PointerAnalysis());
        l.add(((AccrueScheduler) scheduler).helper()
                                           .ComputeFinallyStructureDone());
        if (i > 0) {
            l.add(((AccrueScheduler) scheduler).helper().NotNullAnalysis());
        }
        for (int j = i - 1; j >= 0; j--) {
            l.add(scheduler.internGoal(new PreciseExGoal(j)));
        }
        l.addAll(super.prerequisiteGoals(scheduler));
        return l;
    }

    @Override
    public Pass createPass(ExtensionInfo extInfo) {
        return PreciseExPass.singleton((AccrueExtensionInfo) extInfo, i);
    }
}
