package accrue.analysis.goals;

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

import polyglot.ast.NodeFactory;
import polyglot.frontend.Job;
import polyglot.frontend.Scheduler;
import polyglot.frontend.goals.Goal;
import polyglot.frontend.goals.VisitorGoal;
import polyglot.types.TypeSystem;
import accrue.AccrueScheduler;
import accrue.analysis.visit.MoveStaticFieldInitializers;

public class MoveStaticFieldInitializersGoal extends VisitorGoal {
    public static Goal create(Scheduler scheduler, Job job, TypeSystem ts,
            NodeFactory nf) {
        return create(scheduler, job, ts, nf, false);
    }

    public static Goal create(Scheduler scheduler, Job job, TypeSystem ts,
            NodeFactory nf, boolean skipDefaultInits) {
        return scheduler.internGoal(new MoveStaticFieldInitializersGoal(job,
                                                                        ts,
                                                                        nf,
                                                                        skipDefaultInits));
    }

    protected MoveStaticFieldInitializersGoal(Job job, TypeSystem ts,
            NodeFactory nf, boolean skipDefaultInits) {
        super(job, new MoveStaticFieldInitializers(job,
                                                   ts,
                                                   nf,
                                                   true,
                                                   skipDefaultInits));
    }

    @Override
    public Collection<Goal> prerequisiteGoals(Scheduler scheduler) {
        List<Goal> l = new ArrayList<Goal>();
        l.add(scheduler.ReachabilityChecked(job));
        l.add(scheduler.ConstantsChecked(job));
        l.add(scheduler.ExceptionsChecked(job));
        l.add(scheduler.ExitPathsChecked(job));
        l.add(scheduler.InitializationsChecked(job));
        l.add(scheduler.ConstructorCallsChecked(job));
        l.add(scheduler.ForwardReferencesChecked(job));

        l.add(((AccrueScheduler) scheduler).helper()
                                           .SimplifyExpressionsGoal(job));
        l.add(((AccrueScheduler) scheduler).helper()
                                           .MakeNarrowingAssignmentsExplicit(job));
        l.add(((AccrueScheduler) scheduler).helper().InlineFinalConstants(job));
        return l;
    }
}
