polyglot.visit
Class ExpressionFlattener

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

public class ExpressionFlattener
extends NodeVisitor

Flattens expressions and removes initializers from local variable declarations. Requires LoopNormalizer to be run first. Flattened statements are all of the following forms:

Notes: To summarize, each statement will make at most one write, and contain at most one dereference. The only write operation is the simple assignment (=).


Nested Class Summary
protected  class ExpressionFlattener.DeepCopier
          Makes a deep copy of an AST node.
 
Field Summary
protected  java.util.Stack blockStack
          Stack of nested blocks we are currently in.
protected  ExpressionFlattener.DeepCopier deepCopier
          Used to copy a whole AST subtree.
protected  java.util.Set dontFlatten
          Set of expressions not to flatten.
protected  Local dummyLocal
          Dummy value returned when there is no expression to return.
protected  Job job
           
protected  NodeFactory nf
           
protected  TypeSystem ts
           
 
Constructor Summary
ExpressionFlattener(Job job, TypeSystem ts, NodeFactory nf)
           
 
Method Summary
protected  void addDontFlatten(Expr e)
          Add e to the list of expressions not to flatten.
protected  void addStmt(Stmt s)
          Adds a statement to the current block.
protected  If createAndIf(Expr cond, Local l, Expr e, Binary original)
          Create an if statement that assigns to l the value of "cond && e", evaluating e only if "cond" is true.
protected  Eval createAssign(Expr l, Expr r)
          Create an assignment from r to l, i.e., "l = r;"
protected  Block createBlock(Stmt s)
           
protected  BooleanLit createBool(boolean val)
          Create a boolean literal
protected  If createCondIf(Expr cond, Local l, Expr e1, Expr e2, Conditional original)
          Create an if statement that assigns to l the value of "cond ? e1 : e2" i.e., "if (cond) l = e1; else l = e2"
protected  LocalDecl createDecl(Expr e, Expr init)
          Create a declaration for a local variable with the same type as the expression e, with initializing expression init.
protected  Local createDeclWithInit(Expr e, Expr val)
          Create a local declaration that can take a value of the same type as e, and initialize it to the expression val.
protected  Empty createEmpty()
           
protected  Eval createEval(Expr e)
           
protected  Eval createIncDec(Unary u)
          Convert an increment or decrement to an assignment, e.g.
protected  IntLit createInt(int val)
          Create an int literal
protected  Local createLocal(LocalDecl d)
          Create a use of the Local that is declared in the LocalDecl d
protected  If createOrIf(Expr cond, Local l, Expr e, Binary original)
          Create an if statement that assigns to l the value of "cond || e", evaluating e only if "cond" is false.
protected  Assign createSimpleAssign(Assign a)
          Convert an assignment "l op= r" to "l = l op r"
protected  Node deepCopy(Node n)
           
protected  boolean dontFlatten(Expr e)
          Returns true if e should never be flattened or if e has been added to the don't flatten list.
 NodeVisitor enter(Node n)
          Begin normal traversal of a subtree rooted at n.
protected  boolean inBlock()
          Checks to see if we are in a block.
protected  boolean isAssign(Expr e)
          Returns true for assignments, and pre and post increments and decrements.
protected  boolean isSimpleAssign(Expr e)
           
 Node leave(Node parent, Node old, Node n, NodeVisitor v)
          This method is called after all of the children of n have been visited.
protected  boolean neverFlatten(Expr e)
          Returns true if the expression e is of a type that should never be flattened.
protected  java.lang.String newId()
           
 Node override(Node parent, Node n)
          Given a tree rooted at n, the visitor has the option of overriding all traversal of the children of n.
protected  java.util.List popBlock()
          Pops a block off the stack.
protected  Node postCreate(Node n)
          Whenever a new node is created, this method is called and should do additional processing of the node as needed.
protected  void pushBlock()
          Pushes a new (nested) block onto the stack.
protected  Node translateBinary(Binary b)
           
protected  Node translateLocalDecl(LocalDecl d)
           
protected  Node translateShortCircuitBinary(Binary b)
           
protected  Type typeOf(Expr e)
           
protected  Type typeOf(LocalInstance li)
           
 
Methods inherited from class polyglot.visit.NodeVisitor
begin, copy, enter, finish, finish, leave, override, toString, visitEdge, visitEdgeNoOverride
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

job

protected final Job job

ts

protected final TypeSystem ts

nf

protected final NodeFactory nf

blockStack

protected final java.util.Stack blockStack
Stack of nested blocks we are currently in.


dontFlatten

protected final java.util.Set dontFlatten
Set of expressions not to flatten. Only applies to the expressions themselves, and not their subexpressions (unless they are also in the set explicitly).


deepCopier

protected final ExpressionFlattener.DeepCopier deepCopier
Used to copy a whole AST subtree.


dummyLocal

protected final Local dummyLocal
Dummy value returned when there is no expression to return.

Constructor Detail

ExpressionFlattener

public ExpressionFlattener(Job job,
                           TypeSystem ts,
                           NodeFactory nf)
Method Detail

override

public Node override(Node parent,
                     Node n)
Description copied from class: NodeVisitor
Given a tree rooted at n, the visitor has the option of overriding all traversal of the children of n. If no changes were made to n and the visitor wishes to prevent further traversal of the tree, then it should return n. If changes were made to the subtree, then the visitor should return a copy of n with appropriate changes. Finally, if the visitor does not wish to override traversal of the subtree rooted at n, then it should return null.

The default implementation of this method is to call override(n), as most subclasses do not need to know the parent of the node n.

Overrides:
override in class NodeVisitor
Parameters:
parent - The parent of n, null if n has no parent.
n - The root of the subtree to be traversed.
Returns:
A node if normal traversal is to stop, null if it is to continue.

enter

public NodeVisitor enter(Node n)
Description copied from class: NodeVisitor
Begin normal traversal of a subtree rooted at n. This gives the visitor the option of changing internal state or returning a new visitor which will be used to visit the children of n.

This method is typically called by the method enter(parent, n). If a subclass overrides the method enter(parent, n) then this method may not be called.

Overrides:
enter in class NodeVisitor
Parameters:
n - The root of the subtree to be traversed.
Returns:
The NodeVisitor which should be used to visit the children of n.

leave

public Node leave(Node parent,
                  Node old,
                  Node n,
                  NodeVisitor v)
Description copied from class: NodeVisitor
This method is called after all of the children of n have been visited. In this case, these children were visited by the visitor v. This is the last chance for the visitor to modify the tree rooted at n. This method will be called exactly the same number of times as entry is called. That is, for each node that is not overriden, enter and leave are each called exactly once.

Note that if old == n then the vistior should make a copy of n before modifying it. It should then return the modified copy.

The default implementation of this method is to call leave(old, n, v), as most subclasses do not need to know the parent of the node n.

Overrides:
leave in class NodeVisitor
Parameters:
parent - The parent of old, null if old has no parent.
old - The original state of root of the current subtree.
n - The current state of the root of the current subtree.
v - The NodeVisitor object used to visit the children.
Returns:
The final result of the traversal of the tree rooted at n.

translateBinary

protected Node translateBinary(Binary b)

translateShortCircuitBinary

protected Node translateShortCircuitBinary(Binary b)

translateLocalDecl

protected Node translateLocalDecl(LocalDecl d)

createDeclWithInit

protected Local createDeclWithInit(Expr e,
                                   Expr val)
Create a local declaration that can take a value of the same type as e, and initialize it to the expression val. Return the new local that was declared.


dontFlatten

protected boolean dontFlatten(Expr e)
Returns true if e should never be flattened or if e has been added to the don't flatten list. Note that is then removed from the don't flatten list.


addDontFlatten

protected void addDontFlatten(Expr e)
Add e to the list of expressions not to flatten. Note that this only applies to e itself, and not to any of its subexpressions.


neverFlatten

protected boolean neverFlatten(Expr e)
Returns true if the expression e is of a type that should never be flattened.


pushBlock

protected void pushBlock()
Pushes a new (nested) block onto the stack.


popBlock

protected java.util.List popBlock()
Pops a block off the stack.


inBlock

protected boolean inBlock()
Checks to see if we are in a block.


addStmt

protected void addStmt(Stmt s)
Adds a statement to the current block.


postCreate

protected Node postCreate(Node n)
Whenever a new node is created, this method is called and should do additional processing of the node as needed.


newId

protected java.lang.String newId()

isAssign

protected boolean isAssign(Expr e)
Returns true for assignments, and pre and post increments and decrements.


isSimpleAssign

protected boolean isSimpleAssign(Expr e)

deepCopy

protected Node deepCopy(Node n)

createBlock

protected Block createBlock(Stmt s)

createEmpty

protected Empty createEmpty()

createEval

protected Eval createEval(Expr e)

createDecl

protected LocalDecl createDecl(Expr e,
                               Expr init)
Create a declaration for a local variable with the same type as the expression e, with initializing expression init. Return the LocalDecl produced. Do NOT add the local decl to the statements


createLocal

protected Local createLocal(LocalDecl d)
Create a use of the Local that is declared in the LocalDecl d


createAssign

protected Eval createAssign(Expr l,
                            Expr r)
Create an assignment from r to l, i.e., "l = r;"

Parameters:
l -
r -
Returns:

createSimpleAssign

protected Assign createSimpleAssign(Assign a)
Convert an assignment "l op= r" to "l = l op r"


createIncDec

protected Eval createIncDec(Unary u)
Convert an increment or decrement to an assignment, e.g. "i++" to "i = i + 1"


createAndIf

protected If createAndIf(Expr cond,
                         Local l,
                         Expr e,
                         Binary original)
Create an if statement that assigns to l the value of "cond && e", evaluating e only if "cond" is true. i.e., "if (cond) l = e; else l = false;"


createOrIf

protected If createOrIf(Expr cond,
                        Local l,
                        Expr e,
                        Binary original)
Create an if statement that assigns to l the value of "cond || e", evaluating e only if "cond" is false. i.e., "if (cond) l = true; else l = e;"


createCondIf

protected If createCondIf(Expr cond,
                          Local l,
                          Expr e1,
                          Expr e2,
                          Conditional original)
Create an if statement that assigns to l the value of "cond ? e1 : e2" i.e., "if (cond) l = e1; else l = e2"


createBool

protected BooleanLit createBool(boolean val)
Create a boolean literal


createInt

protected IntLit createInt(int val)
Create an int literal


typeOf

protected Type typeOf(Expr e)

typeOf

protected Type typeOf(LocalInstance li)