polyglot.visit
Class CFGBuilder

java.lang.Object
  extended by polyglot.visit.CFGBuilder
All Implemented Interfaces:
java.lang.Cloneable, Copy

public class CFGBuilder
extends java.lang.Object
implements Copy

Class used to construct a CFG.


Nested Class Summary
protected static class CFGBuilder.EdgeKeyTermPair
           
 
Field Summary
protected  DataFlow df
          The data flow analysis for which we are constructing the graph.
protected  boolean errorEdgesToExitNode
          True if we should add edges for uncaught Errors to the exit node of the graph.
protected  FlowGraph graph
          The flowgraph under construction.
protected  Stmt innermostTarget
          The innermost loop or try-block in lexical scope.
protected  CFGBuilder outer
          The outer CFGBuilder.
protected  java.util.List path_to_finally
          List of terms on the path to the innermost finally block.
protected  boolean skipInnermostCatches
          True if we should skip the catch blocks for the innermost try when building edges for an exception throw.
protected  TypeSystem ts
          The type system.
 
Constructor Summary
CFGBuilder(TypeSystem ts, FlowGraph graph, DataFlow df)
           
 
Method Summary
 java.lang.Object copy()
          Copy the CFGBuilder.
 void edge(CFGBuilder p_visitor, Term p, Term q, FlowGraph.EdgeKey edgeKey)
           
 void edge(Term p, Term q)
          Add an edge to the CFG from p to q.
 void edge(Term p, Term q, FlowGraph.EdgeKey edgeKey)
          Add an edge to the CFG from p to q.
protected  CFGBuilder enterFinally(Term from)
          Enter a finally block.
 CFGBuilder push(Stmt n)
          Construct a new CFGBuilder with the a new innermost loop or try-block n.
 CFGBuilder push(Stmt n, boolean skipInnermostCatches)
          Construct a new CFGBuilder with the a new innermost loop or try-block n, optionally skipping innermost catch blocks.
protected static CFGBuilder tryFinally(CFGBuilder v, Term last, CFGBuilder last_visitor, Block finallyBlock)
          Create edges for the finally block of a try-finally construct.
 TypeSystem typeSystem()
          Get the type system.
 void visitBranchTarget(Branch b)
          Visit edges from a branch.
 void visitCFG(Term a, FlowGraph.EdgeKey edgeKey, java.util.List succ)
          Create edges from node a to all successors succ with the EdgeKey edgeKey for all edges created.
 void visitCFG(Term a, FlowGraph.EdgeKey edgeKey, Term succ)
          Create an edge for a node a with a single successor succ, and EdgeKey edgeKey
 void visitCFG(Term a, FlowGraph.EdgeKey edgeKey1, Term succ1, FlowGraph.EdgeKey edgeKey2, Term succ2)
          Create edges from node a to successors succ1 and succ2 with EdgeKeys edgeKey1 and edgeKey2 respecitvely.
protected  void visitCFG(Term a, java.util.List succs)
          Create edges for a node a with successors succs.
 void visitCFG(Term a, Term succ)
          Create an edge for a node a with a single successor succ.
 void visitCFGList(java.util.List elements, Term after)
          Utility function to visit all edges in a list.
 void visitGraph()
          Visit the AST, constructing the CFG.
 void visitReturn(Return r)
          Visit edges for a return statement.
 void visitThrow(Term a)
           
 void visitThrow(Term t, Type type)
          Create edges for an exception thrown from term t.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

graph

protected FlowGraph graph
The flowgraph under construction.


ts

protected TypeSystem ts
The type system.


outer

protected CFGBuilder outer
The outer CFGBuilder. We create a new inner CFGBuilder when entering a loop or try-block and when entering a finally block.


innermostTarget

protected Stmt innermostTarget
The innermost loop or try-block in lexical scope. We maintain a stack of loops and try-blocks in order to add edges for break and continue statements and for exception throws. When such a jump is encountered we traverse the stack, searching for the target of the jump.


path_to_finally

protected java.util.List path_to_finally
List of terms on the path to the innermost finally block. If we are constructing a CFG for a finally block, this is the sequence of terms that caused entry into this and lexically enclosing finally blocks. We construct a unique subgraph for each such path. The list is empty if this CFGBuilder is not constructing the CFG for a finally block.


df

protected DataFlow df
The data flow analysis for which we are constructing the graph.


skipInnermostCatches

protected boolean skipInnermostCatches
True if we should skip the catch blocks for the innermost try when building edges for an exception throw.


errorEdgesToExitNode

protected boolean errorEdgesToExitNode
True if we should add edges for uncaught Errors to the exit node of the graph. By default, we do not, but subclasses can change this behavior if needed.

Constructor Detail

CFGBuilder

public CFGBuilder(TypeSystem ts,
                  FlowGraph graph,
                  DataFlow df)
Method Detail

typeSystem

public TypeSystem typeSystem()
Get the type system.


copy

public java.lang.Object copy()
Copy the CFGBuilder.

Specified by:
copy in interface Copy

push

public CFGBuilder push(Stmt n)
Construct a new CFGBuilder with the a new innermost loop or try-block n.


push

public CFGBuilder push(Stmt n,
                       boolean skipInnermostCatches)
Construct a new CFGBuilder with the a new innermost loop or try-block n, optionally skipping innermost catch blocks.


visitBranchTarget

public void visitBranchTarget(Branch b)
Visit edges from a branch. Simulate breaking/continuing out of the loop, visiting any finally blocks encountered.


visitReturn

public void visitReturn(Return r)
Visit edges for a return statement. Simulate the return, visiting any finally blocks encountered.


visitGraph

public void visitGraph()
Visit the AST, constructing the CFG.


visitCFGList

public void visitCFGList(java.util.List elements,
                         Term after)
Utility function to visit all edges in a list.


visitCFG

public void visitCFG(Term a,
                     Term succ)
Create an edge for a node a with a single successor succ. The EdgeKey used for the edge from a to succ will be FlowGraph.EDGE_KEY_OTHER


visitCFG

public void visitCFG(Term a,
                     FlowGraph.EdgeKey edgeKey,
                     Term succ)
Create an edge for a node a with a single successor succ, and EdgeKey edgeKey


visitCFG

public void visitCFG(Term a,
                     FlowGraph.EdgeKey edgeKey1,
                     Term succ1,
                     FlowGraph.EdgeKey edgeKey2,
                     Term succ2)
Create edges from node a to successors succ1 and succ2 with EdgeKeys edgeKey1 and edgeKey2 respecitvely.


visitCFG

public void visitCFG(Term a,
                     FlowGraph.EdgeKey edgeKey,
                     java.util.List succ)
Create edges from node a to all successors succ with the EdgeKey edgeKey for all edges created.


visitCFG

protected void visitCFG(Term a,
                        java.util.List succs)
Create edges for a node a with successors succs.

Parameters:
a - the source node for the edges.
succs - a list of EdgeKeyTermPairs

visitThrow

public void visitThrow(Term a)

visitThrow

public void visitThrow(Term t,
                       Type type)
Create edges for an exception thrown from term t.


tryFinally

protected static CFGBuilder tryFinally(CFGBuilder v,
                                       Term last,
                                       CFGBuilder last_visitor,
                                       Block finallyBlock)
Create edges for the finally block of a try-finally construct.

Parameters:
v - v.innermostTarget is the Try term that the finallyBlock is assoicated with. @@@XXX
last - the last term visited before the finally block is entered.
last_visitor - @@@XXX
finallyBlock - the finally block associated with a try finally block.

enterFinally

protected CFGBuilder enterFinally(Term from)
Enter a finally block. This method returns a new CFGBuilder with the path_to_finally field pointing to a list that has the Term from appended.


edge

public void edge(Term p,
                 Term q)
Add an edge to the CFG from p to q.


edge

public void edge(Term p,
                 Term q,
                 FlowGraph.EdgeKey edgeKey)
Add an edge to the CFG from p to q.


edge

public void edge(CFGBuilder p_visitor,
                 Term p,
                 Term q,
                 FlowGraph.EdgeKey edgeKey)
Parameters:
p_visitor - The visitor used to create p ("this" is the visitor that created q)
p - The predecessor node in the forward graph
q - The successor node in the forward graph