Writing A New Static Analysis Phase

All phases of static analysis are defined in the package com.sun.fortress.compiler.phases. To write your own static phase, add a class in this package that extends the class Phase. Let's pretend you are writing the phase UselessPhase. You will need to write the constructor UselessPhase(Phase parentPhase) and override the execute method. Here is the following code template:

public class UselessPhase extends Phase {
	
   public UselessPhase(Phase parentPhase) {
      super(parentPhase);
   }

   @Override
   public AnalyzeResult execute() throws StaticError {		
      Debug.debug( Debug.Type.FORTRESS, 1, "Start phase UselessPhase" );
      AnalyzeResult previous = parentPhase.getResult();
      ...insert code here...
      return new AnalyzeResult(previous.apis(), previous.components(), 
         IterUtil.<StaticError> empty(), previous.typeEnvAtNode());        

   }
}

While writing your own phase of static analysis, any information on the source program can be accessed through the following three fields that are defined in class Phase: Phase parentPhase, FortressRepository repository, GlobalEnvironment env, and long lastModified. The parentPhase has a method getResult() that be used to accessed the results of the previous phase of static analysis.

When returning an AnalyzeResult object, you are responsible for correctly updating all the fields of the data structure. The general rule is that if you do not update a field from the results of the previous phase of static analysis, then pass the results of the previous phase along to the next phase. Your phase of static analysis is responsible for returning an AnalyzeResult object that correctly represents the program transformations undertaken by your phase.

Next, update the enumerated type com.sun.fortress.compiler.phases.PhaseOrder. Add a new instance to this enumerated type, and then modify the method makePhaseHelper(Phase emptyPhase). Your new phase will be instantiated a constructor that accepts the previous phase of static analysis as its sole argument. Finally wire up the next phase of static analysis to to your new phase, and you are ready to test your new UselessPhase.