dk.brics.string.java
Class ControlFlowBuilder

java.lang.Object
  extended by dk.brics.string.java.ControlFlowBuilder

public class ControlFlowBuilder
extends Object

Utility class assisting in the construction of method bodies.

The control-flow builder acts as a cursor in the control-flow graph. When a new statement is added, it becomes the successor of the statement under the cursor, and the cursor then moves to the new statement. As a result, successive calls to addStatement(dk.brics.string.intermediate.Statement) creates a chain of statement nodes.

The control-flow graph can be split with startBranch(). After calling, one or more calls to useBranch() must be made at points where the branches should join. Finally endBranch() should be called after the last useBranch(). For example, to create the following control-flow,

     A
   / | \
  B  C  D
  |  |  |
  E  |  |
   \ | /
     F
 
one could add the statements in this fashion:
 A
 startBranch()
 B
 E
 useBranch()
 C
 useBranch()
 D
 useBranch()
 endBranch()
 F
 


Constructor Summary
ControlFlowBuilder(Method method)
          Creates a builder for inserting statements in the specified method's body.
 
Method Summary
 void addStatement(Statement s)
          Adds a statement at the current position in the control flow graph being built.
 void endBranch()
          Ends the current branching.
 Pair<Statement,Statement> finish()
          Finishes the subgraph created by the builder, and returns the first and last statement in the subgraph.
 void moveToIsolation()
          Removes the cursor from the control-flow graph, so statements will be placed in an isolated subgraph of their own.
 void moveToStatement(Statement to)
          Moves the cursor to the specified statement, so new statements will be successors of the specified statement.
 void startBranch()
          Starts a new branching and opens one branch in it.
 void useBranch()
          Ends the current branch and starts a new one.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ControlFlowBuilder

public ControlFlowBuilder(Method method)
Creates a builder for inserting statements in the specified method's body. The builder's cursor will initially be in isolation, so added statements will not be reachable.

Parameters:
method - an intermediate method to add statements to.
Method Detail

addStatement

public void addStatement(Statement s)
Adds a statement at the current position in the control flow graph being built. The statement will be added to the method, set as successor to the previous "cursor" statement, and then made the "cursor" statement itself.

Parameters:
s - a statement to add.

endBranch

public void endBranch()
Ends the current branching. Must be called directly after useBranch(). See ControlFlowBuilder for a description of the branching methods.


finish

public Pair<Statement,Statement> finish()
Finishes the subgraph created by the builder, and returns the first and last statement in the subgraph. The first statement will be either: The last statement will either be the last statement added with addStatement(dk.brics.string.intermediate.Statement), or a Nop succeeding it.


moveToIsolation

public void moveToIsolation()
Removes the cursor from the control-flow graph, so statements will be placed in an isolated subgraph of their own. The statements can later be connected with the of the graph.


moveToStatement

public void moveToStatement(Statement to)
Moves the cursor to the specified statement, so new statements will be successors of the specified statement.

Parameters:
to - statement to add from.
Throws:
IllegalStateException - if a branch is currently unclosed.
IllegalArgumentException - if the specified statement belongs to a different method than the builder

startBranch

public void startBranch()
Starts a new branching and opens one branch in it. Multiple levels of branching can open at once. See ControlFlowBuilder for a description of the branching methods.


useBranch

public void useBranch()
Ends the current branch and starts a new one. See ControlFlowBuilder for a description of the branching methods.



Copyright © 2003-2009 Anders Møller, Aske Simon Christensen, Asger Feldthaus.