Main Page   Modules   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members   Related Pages  

Changer Class Reference

A functional class to traverse an AST, and perform some transformation at each of its Nodes. More...

#include <changer.h>

Inheritance diagram for Changer::

CastRemover cfg_changer CFS_Changer clone_changer constantFoldingChanger constantPropChanger constantsChanger deadcodeChanger deadCodeEliminationChanger DismantleChanger ipConstantsChanger LocalCopyPropChanger Optimizer ref_clone_changer return_changer TreeFixer unusedVariableCleanupChanger List of all members.

Public Types

enum  Order { Preorder, Postorder, Both }
enum  Depth { Subtree, NodeOnly }

Public Methods

 Changer (Order the_order, Depth depth, bool delete_old)
 Create a new instance of a Changer. More...

Accessors
Methods to get and set fields in the class.

Order order () const
 Return the order in which Nodes of an AST should be visited. More...

Depth depth () const
 Return which Nodes of an AST should be visited. More...

bool delete_old () const
 Return whether this Changer deletes Nodes which are removed from the AST by "at_" methods. More...

"at_" methods
These methods define the functions that should be performed when different classes of Nodes are encountered in the AST. The most specific "at_" method that matches a given Node's class will be called.

virtual Nodeat_node (Node *the_node, Order ord)
virtual Nodeat_unit (unitNode *the_unit, Order ord)
virtual Nodeat_def (defNode *the_def, Order ord)
virtual Nodeat_decl (declNode *the_decl, Order ord)
virtual Nodeat_subdecl (subdeclNode *the_subdecl, Order ord)
virtual Nodeat_proc (procNode *the_proc, Order ord)
virtual Nodeat_type (typeNode *the_type, Order ord)
virtual Nodeat_prim (primNode *the_prim, Order ord)
virtual Nodeat_tdef (tdefNode *the_tdef, Order ord)
virtual Nodeat_ptr (ptrNode *the_ptr, Order ord)
virtual Nodeat_array (arrayNode *the_array, Order ord)
virtual Nodeat_func (funcNode *the_func, Order ord)
virtual Nodeat_sue (sueNode *the_sue, Order ord)
virtual Nodeat_struct (structNode *the_struct, Order ord)
virtual Nodeat_union (unionNode *the_union, Order ord)
virtual Nodeat_enum (enumNode *the_enum, Order ord)
virtual Nodeat_suespec (suespecNode *the_suespec, Order ord)
virtual Nodeat_expr (exprNode *the_expr, Order ord)
virtual Nodeat_const (constNode *the_const, Order ord)
virtual Nodeat_id (idNode *the_id, Order ord)
virtual Nodeat_binary (binaryNode *the_binary, Order ord)
virtual Nodeat_unary (unaryNode *the_unary, Order ord)
virtual Nodeat_cast (castNode *the_cast, Order ord)
virtual Nodeat_comma (commaNode *the_comma, Order ord)
virtual Nodeat_ternary (ternaryNode *the_ternary, Order ord)
virtual Nodeat_call (callNode *the_call, Order ord)
virtual Nodeat_initializer (initializerNode *the_initializer, Order ord)
virtual Nodeat_stmt (stmtNode *the_stmt, Order ord)
virtual Nodeat_block (blockNode *the_block, Order ord)
virtual Nodeat_basicblock (basicblockNode *the_basicblock, Order ord)
virtual Nodeat_exprstmt (exprstmtNode *the_exprstmt, Order ord)
virtual Nodeat_target (targetNode *the_target, Order ord)
virtual Nodeat_label (labelNode *the_label, Order ord)
virtual Nodeat_case (caseNode *the_case, Order ord)
virtual Nodeat_selection (selectionNode *the_selection, Order ord)
virtual Nodeat_if (ifNode *the_if, Order ord)
virtual Nodeat_switch (switchNode *the_switch, Order ord)
virtual Nodeat_loop (loopNode *the_loop, Order ord)
virtual Nodeat_while (whileNode *the_while, Order ord)
virtual Nodeat_do (doNode *the_do, Order ord)
virtual Nodeat_for (forNode *the_for, Order ord)
virtual Nodeat_jump (jumpNode *the_jump, Order ord)
virtual Nodeat_goto (gotoNode *the_goto, Order ord)
virtual Nodeat_continue (continueNode *the_continue, Order ord)
virtual Nodeat_break (breakNode *the_break, Order ord)
virtual Nodeat_return (returnNode *the_return, Order ord)
virtual Nodeat_attrib (attribNode *the_attrib, Order ord)
virtual Nodeat_text (textNode *the_text, Order ord)

Private Attributes

Order _order
 The order in which AST Nodes should be visited. More...

Depth _depth
 Which Nodes in the AST should be visited. More...

bool _delete_old
 Whether old Nodes should be deleted. More...


Detailed Description

A functional class to traverse an AST, and perform some transformation at each of its Nodes.

A Changer is a functional class that traverses an abstract syntax tree, and performs some transformation at each of its Nodes. At each Node, the transformation it performs depends on the class of the Node.

Corresponding to each class in the Node hierarchy, Changer defines an "at_" method. To specify the transformation that should be performed for Nodes of a particular class, create a subclass of Changer that overrides the "at_" method corresponding to that class. These "at_" methods take a Node, and return a transformed Node. By default, the "at_" methods simply return the Node they are passed. Thus, a Changer subclass only needs to override the "at_" methods corresponding to Node types that are relevant to that changer.

As the AST is traversed, the most specific "at_" method that has been defined for each Node will be called on that Node. For example, if a Changer subclass overrides "at_decl()", but not "at_subdecl()," then the "at_decl()" method wil be called for all declNodes and subDeclnodes encountered in the AST. However, if a Changer subclass overrides both "at_decl()" and "at_subdecl," then the "at_decl()" method will be called for all declNodes encountered in the AST, and the "at_subdecl()" method will be called for all subdeclNodes encountered in the AST. If you wish for both "at_subdecl()" and "at_decl()" to be called for subdeclNodes, then you should explicitly call the "at_decl()" method from "at_subdecl()."

As the changer traverses the AST, the "at_" methods for a Node can be called before its children have been visited, after its children have been visited, or both before and after its children have been visited. When the "at_" methods for a Node are called before its children, they are said to be called in "preorder." When the "at_" methods for a Node are called after its children, they are said to be called in "postorder." If "at_" methods are called both in preorder and in postorder, then the preorder call will always preceed the postorder call.

When a new Changer is created, the order(s) that Nodes are visited in should be specified. In general, all instances of a given subclass of Changer will visit Nodes in the same order. However, it may occasionally be useful to specify different orders for two different instances of the same subclass of Changer.

New instances of Changer can also specify the "depth" of the traversal. If the depth is "SubTree," then a Changer will visit the entire AST rooted at the Node that it is called on. If the depth is "NodeOnly," then the Changer will only call the "at_" method for Node it is called on, and not for any of its sub-Nodes. A Changer with a depth of NodeOnly is different from a Visitor, because the "Node.change()" method will return a transformed Node, whereas "Node.visit()" cannot return a transformed Node.

Changers may modify any fields of the Nodes it visits, including fields that contain sub-Nodes. If a field containing a sub-Node is changed in a preorder call to an "at_" method, then the AST rooted at the new sub-Node will be traversed. If a field containing a sub-Node is changed in a postorder call to an "at_" method, then the AST rooted at the new sub-Node will not be traversed.

Currently, Changers traverse the AST in a depth-first order. Preorder calls to the "at_" methods are called before descending into a Node's children, and postorder calls to the "at_" method are called after returning from the children in the traversal. At the leaves, the postorder calls to the "at_" methods immediately follow the preorder calls. However, this ordering is subject to change, and should not be relied upon. The only aspects of the AST traversal ordering which are guaranteed are:

Changers should never be used to traverse ASTs containing cycles; since they detect no mechanism to detect cycles, this would cause an infinite loop.

To use a changer "my_changer" to traverse an AST rooted at "my_node," use the code:

  my_node = my_node->change(my_changer);

See also:
Visitor , Walker , Node::change


Member Enumeration Documentation

enum Changer::Depth
 

Enumeration values:
Subtree 
NodeOnly 

enum Changer::Order
 

Enumeration values:
Preorder 
Postorder 
Both 


Constructor & Destructor Documentation

Changer::Changer Order    the_order,
Depth    depth,
bool    delete_old
[inline]
 

Create a new instance of a Changer.

This constructor is usually called by a subclass.

Parameters:
the_order  The order in which AST Nodes should be visited. Nodes can be visited before their children (Preorder), after their children (Postorder), or both (Both).
depth  Specifies which Nodes in the AST should be visited. A value of SubTree specifies that the entire AST should be traversed. A value of NodeOnly specifies that only the root Node in the AST should be visited.
delete_old  Whether this changer deletes Nodes which are removed from the AST by "at_" methods.


Member Function Documentation

virtual Node* Changer::at_array arrayNode   the_array,
Order    ord
[inline, virtual]
 

virtual Node* Changer::at_attrib attribNode   the_attrib,
Order    ord
[inline, virtual]
 

virtual Node* Changer::at_basicblock basicblockNode   the_basicblock,
Order    ord
[inline, virtual]
 

virtual Node* Changer::at_binary binaryNode   b,
Order    ord
[inline, virtual]
 

Reimplemented in constantFoldingChanger.

virtual Node* Changer::at_block blockNode   p,
Order    ord
[inline, virtual]
 

Reimplemented in InitializerDismantleChanger, and ExpressionDismantleChanger.

virtual Node* Changer::at_break breakNode   the_break,
Order    ord
[inline, virtual]
 

virtual Node* Changer::at_call callNode   the_call,
Order    ord
[inline, virtual]
 

virtual Node* Changer::at_case caseNode   n,
Order    ord
[inline, virtual]
 

Reimplemented in SelectionDismantleChanger.

virtual Node* Changer::at_cast castNode   c,
Order    ord
[inline, virtual]
 

Reimplemented in CastRemover.

virtual Node* Changer::at_comma commaNode   the_comma,
Order    ord
[inline, virtual]
 

virtual Node* Changer::at_const constNode   the_const,
Order    ord
[inline, virtual]
 

virtual Node* Changer::at_continue continueNode   the_continue,
Order    ord
[inline, virtual]
 

virtual Node* Changer::at_decl declNode   the_node,
Order    ord
[inline, virtual]
 

Reimplemented in TreeFixer.

virtual Node* Changer::at_def defNode   the_def,
Order    ord
[inline, virtual]
 

virtual Node* Changer::at_do doNode   the_do,
Order    ord
[inline, virtual]
 

virtual Node* Changer::at_enum enumNode   the_enum,
Order    ord
[inline, virtual]
 

virtual Node* Changer::at_expr exprNode   the_expr,
Order    ord
[inline, virtual]
 

Reimplemented in ipConstantsChanger, and constantsChanger.

virtual Node* Changer::at_exprstmt exprstmtNode   the_exprstmt,
Order    ord
[inline, virtual]
 

Reimplemented in deadcodeChanger.

virtual Node* Changer::at_for forNode   the_for,
Order    ord
[inline, virtual]
 

virtual Node* Changer::at_func funcNode   the_func,
Order    ord
[inline, virtual]
 

virtual Node* Changer::at_goto gotoNode   the_goto,
Order    ord
[inline, virtual]
 

virtual Node* Changer::at_id idNode   i,
Order    ord
[inline, virtual]
 

Reimplemented in constantPropChanger, and unusedVariableCleanupChanger.

virtual Node* Changer::at_if ifNode   n,
Order    o
[inline, virtual]
 

Reimplemented in IfConverterChanger, CFS_Changer, and constantsChanger.

virtual Node* Changer::at_initializer initializerNode   the_initializer,
Order    ord
[inline, virtual]
 

virtual Node* Changer::at_jump jumpNode   the_jump,
Order    ord
[inline, virtual]
 

virtual Node* Changer::at_label labelNode   l,
Order    ord
[inline, virtual]
 

Reimplemented in InitializerDismantleChanger, and FlattenDismantleChanger.

virtual Node* Changer::at_loop loopNode   p,
Order    ord
[inline, virtual]
 

Reimplemented in LoopDismantleChanger.

virtual Node* Changer::at_node Node   the_node,
Order    ord
[inline, virtual]
 

Reimplemented in clone_changer, and ref_clone_changer.

virtual Node* Changer::at_prim primNode   the_prim,
Order    ord
[inline, virtual]
 

virtual Node* Changer::at_proc procNode   p,
Order    ord
[inline, virtual]
 

Reimplemented in LoopDismantleChanger, FlattenDismantleChanger, cfg_changer, deadCodeEliminationChanger, unusedVariableCleanupChanger, LocalCopyPropChanger, CFS_Changer, and Optimizer.

virtual Node* Changer::at_ptr ptrNode   the_ptr,
Order    ord
[inline, virtual]
 

virtual Node* Changer::at_return returnNode   the_return,
Order    ord
[inline, virtual]
 

Reimplemented in LoopDismantleChanger, and return_changer.

virtual Node* Changer::at_selection selectionNode   p,
Order    ord
[inline, virtual]
 

Reimplemented in SelectionDismantleChanger.

virtual Node* Changer::at_stmt stmtNode   the_stmt,
Order    ord
[inline, virtual]
 

virtual Node* Changer::at_struct structNode   the_struct,
Order    ord
[inline, virtual]
 

virtual Node* Changer::at_subdecl subdeclNode   the_subdecl,
Order    ord
[inline, virtual]
 

virtual Node* Changer::at_sue sueNode   the_sue,
Order    ord
[inline, virtual]
 

virtual Node* Changer::at_suespec suespecNode   the_suespec,
Order    ord
[inline, virtual]
 

virtual Node* Changer::at_switch switchNode   the_switch,
Order    ord
[inline, virtual]
 

virtual Node* Changer::at_target targetNode   the_target,
Order    ord
[inline, virtual]
 

virtual Node* Changer::at_tdef tdefNode   the_tdef,
Order    ord
[inline, virtual]
 

virtual Node* Changer::at_ternary ternaryNode   the_ternary,
Order    ord
[inline, virtual]
 

virtual Node* Changer::at_text textNode   the_text,
Order    ord
[inline, virtual]
 

virtual Node* Changer::at_type typeNode   the_type,
Order    ord
[inline, virtual]
 

virtual Node* Changer::at_unary unaryNode   p,
Order    ord
[inline, virtual]
 

Reimplemented in FlattenDismantleChanger, and constantFoldingChanger.

virtual Node* Changer::at_union unionNode   the_union,
Order    ord
[inline, virtual]
 

virtual Node* Changer::at_unit unitNode   u,
Order    ord
[inline, virtual]
 

Reimplemented in Optimizer.

virtual Node* Changer::at_while whileNode   the_while,
Order    ord
[inline, virtual]
 

bool Changer::delete_old   const [inline]
 

Return whether this Changer deletes Nodes which are removed from the AST by "at_" methods.

Depth Changer::depth   const [inline]
 

Return which Nodes of an AST should be visited.

A value of SubTree specifies that the entire AST should be traversed. A value of NodeOnly specifies that only the root Node in the AST should be visited.

Order Changer::order   const [inline]
 

Return the order in which Nodes of an AST should be visited.

Nodes can be visited before their children (Preorder), after their children (Postorder), or both (Both).


Member Data Documentation

bool Changer::_delete_old [private]
 

Whether old Nodes should be deleted.

Depth Changer::_depth [private]
 

Which Nodes in the AST should be visited.

Order Changer::_order [private]
 

The order in which AST Nodes should be visited.


The documentation for this class was generated from the following file:
Generated on Thu Jan 10 12:06:25 2002 for C-Breeze by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001