コード例 #1
0
ファイル: InitChecker.java プロジェクト: tianhuat/polyglot
  /**
   * Check that the conditions of initialization are not broken.
   *
   * <p>To summarize the conditions: - Local variables must be initialized before use, (i.e. min
   * count > 0) - Final local variables (including Formals) cannot be assigned to more than once
   * (i.e. max count <= 1) - Final non-static fields whose target is this cannot be assigned to more
   * than once - Final static fields whose target is the current class cannot be assigned to more
   * than once
   *
   * <p>This method is also responsible for maintaining state between the dataflows over
   * Initializers, by copying back the appropriate MinMaxInitCounts to the map
   * currClassFinalFieldInitCounts.
   */
  public void check(FlowGraph graph, Term n, Item inItem, Map outItems) throws SemanticException {
    DataFlowItem dfIn = (DataFlowItem) inItem;
    if (dfIn == null) {
      // There is no input data flow item. This can happen if we are
      // checking an unreachable term, and so no Items have flowed
      // through the term. For example, in the code fragment:
      //     a: do { break a; } while (++i < 10);
      // the expression "++i < 10" is unreachable, but the as there is
      // no unreachable statement, the Java Language Spec permits it.

      // Set inItem to a default Item
      dfIn = createInitDFI();
    }

    DataFlowItem dfOut = null;
    if (outItems != null && !outItems.isEmpty()) {
      // due to the flow equations, all DataFlowItems in the outItems map
      // are the same, so just take the first one.
      dfOut = (DataFlowItem) outItems.values().iterator().next();
    }

    if (n instanceof Local) {
      checkLocal(graph, (Local) n, dfIn, dfOut);
    } else if (n instanceof LocalAssign) {
      checkLocalAssign(graph, (LocalAssign) n, dfIn, dfOut);
    } else if (n instanceof FieldAssign) {
      checkFieldAssign(graph, (FieldAssign) n, dfIn, dfOut);
    } else if (n instanceof ClassBody) {
      // we need to check that the locals used inside this class body
      // have all been defined at this point.
      Set localsUsed = (Set) currCBI.localsUsedInClassBodies.get(n);

      if (localsUsed != null) {
        checkLocalsUsedByInnerClass(graph, (ClassBody) n, localsUsed, dfIn, dfOut);
      }
    }

    if (n == graph.finishNode()) {
      if (currCBI.currCodeDecl instanceof Initializer) {
        finishInitializer(graph, (Initializer) currCBI.currCodeDecl, dfIn, dfOut);
      }
      if (currCBI.currCodeDecl instanceof ConstructorDecl) {
        finishConstructorDecl(graph, (ConstructorDecl) currCBI.currCodeDecl, dfIn, dfOut);
      }
    }
  }
コード例 #2
0
  /**
   * 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;
          }
        }
      }
    }
  }