/** Read a source file and compile it up to the the current job's last barrier. */
  public boolean readSource(FileSource source) {
    // Add a new SourceJob for the given source. If a Job for the source
    // already exists, then we will be given the existing job.
    SourceJob job = addJob(source);

    if (job == null) {
      // addJob returns null if the job has already been completed, in
      // which case we can just ignore the request to read in the source.
      return true;
    }

    // Run the new job up to the currentJob's SourceJob's last barrier, to
    // make sure that dependencies are satisfied.
    Pass.ID barrier;

    if (currentJob != null) {
      if (currentJob.sourceJob().lastBarrier() == null) {
        throw new InternalCompilerError(
            "A Source Job which has "
                + "not reached a barrier cannot read another "
                + "source file.");
      }

      barrier = currentJob.sourceJob().lastBarrier().id();
    } else {
      barrier = Pass.FIRST_BARRIER;
    }

    // Make sure we reach at least the first barrier defined
    // in the base compiler.  This forces types to be constructed.
    // If FIRST_BARRIER is before "barrier",
    // then the second runToPass will just return true.
    return runToPass(job, barrier) && runToPass(job, Pass.FIRST_BARRIER);
  }
 /** Adds a dependency from the current job to the given Source. */
 public void addDependencyToCurrentJob(Source s) {
   if (s == null) return;
   if (currentJob != null) {
     Object o = jobs.get(s);
     if (o != COMPLETED_JOB) {
       if (Report.should_report(Report.frontend, 2)) {
         Report.report(2, "Adding dependency from " + currentJob.source() + " to " + s);
       }
       currentJob.sourceJob().addDependency(s);
     }
   } else {
     throw new InternalCompilerError("No current job!");
   }
 }
  /**
   * Before running <code>Pass pass</code> on <code>SourceJob job</code> make sure that all
   * appropriate scheduling invariants are satisfied, to ensure that all passes of other jobs that
   * <code>job</code> depends on will have already been done.
   */
  protected void enforceInvariants(Job job, Pass pass) throws CyclicDependencyException {
    SourceJob srcJob = job.sourceJob();
    if (srcJob == null) {
      return;
    }

    BarrierPass lastBarrier = srcJob.lastBarrier();
    if (lastBarrier != null) {
      // make sure that _all_ dependent jobs have completed at least up to
      // the last barrier (not just children).
      //
      // Ideally the invariant should be that only the source jobs that
      // job _depends on_ should be brought up to the last barrier.
      // This is work to be done in the future...
      List allDependentSrcs = new ArrayList(srcJob.dependencies());
      Iterator i = allDependentSrcs.iterator();
      while (i.hasNext()) {
        Source s = (Source) i.next();
        Object o = jobs.get(s);
        if (o == COMPLETED_JOB) continue;
        if (o == null) {
          throw new InternalCompilerError("Unknown source " + s);
        }
        SourceJob sj = (SourceJob) o;
        if (sj.pending(lastBarrier.id())) {
          // Make the job run up to the last barrier.
          // We ignore the return result, since even if the job
          // fails, we will keep on going and see
          // how far we get...
          if (Report.should_report(Report.frontend, 3)) {
            Report.report(3, "Running " + sj + " to " + lastBarrier.id() + " for " + srcJob);
          }
          runToPass(sj, lastBarrier.id());
        }
      }
    }

    if (pass instanceof GlobalBarrierPass) {
      // need to make sure that _all_ jobs have completed just up to
      // this global barrier.

      // If we hit a cyclic dependency, ignore it and run the other
      // jobs up to that pass.  Then try again to run the cyclic
      // pass.  If we hit the cycle again for the same job, stop.
      LinkedList barrierWorklist = new LinkedList(jobs.values());

      while (!barrierWorklist.isEmpty()) {
        Object o = barrierWorklist.removeFirst();
        if (o == COMPLETED_JOB) continue;
        SourceJob sj = (SourceJob) o;
        if (sj.completed(pass.id()) || sj.nextPass() == sj.passByID(pass.id())) {
          // the source job has either done this global pass
          // (which is possible if the job was loaded late in the
          // game), or is right up to the global barrier.
          continue;
        }

        // Make the job run up to just before the global barrier.
        // We ignore the return result, since even if the job
        // fails, we will keep on going and see
        // how far we get...
        Pass beforeGlobal = sj.getPreviousTo(pass.id());
        if (Report.should_report(Report.frontend, 3)) {
          Report.report(3, "Running " + sj + " to " + beforeGlobal.id() + " for " + srcJob);
        }

        // Don't use runToPass, since that catches the
        // CyclicDependencyException that we should report
        // back to the caller.
        while (!sj.pendingPasses().isEmpty()) {
          Pass p = (Pass) sj.pendingPasses().get(0);

          runPass(sj, p);

          if (p == beforeGlobal) {
            break;
          }
        }
      }
    }
  }