#include <walker.h>
Inheritance diagram for Walker::
Public Types | |
enum | Order { Preorder, Postorder, Both } |
The order in which AST Nodes should be visited. More... | |
enum | Depth { Subtree, NodeOnly } |
Which Nodes in the AST tree should be visited. More... | |
Public Methods | |
Walker (Order the_order, Depth depth) | |
Create a new instance of a Walker. 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... | |
"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 void | at_node (Node *the_node, Order ord) |
virtual void | at_unit (unitNode *the_unit, Order ord) |
virtual void | at_def (defNode *the_def, Order ord) |
virtual void | at_decl (declNode *the_decl, Order ord) |
virtual void | at_subdecl (subdeclNode *the_subdecl, Order ord) |
virtual void | at_proc (procNode *the_proc, Order ord) |
virtual void | at_type (typeNode *the_type, Order ord) |
virtual void | at_prim (primNode *the_prim, Order ord) |
virtual void | at_tdef (tdefNode *the_tdef, Order ord) |
virtual void | at_ptr (ptrNode *the_ptr, Order ord) |
virtual void | at_array (arrayNode *the_array, Order ord) |
virtual void | at_func (funcNode *the_func, Order ord) |
virtual void | at_sue (sueNode *the_sue, Order ord) |
virtual void | at_struct (structNode *the_struct, Order ord) |
virtual void | at_union (unionNode *the_union, Order ord) |
virtual void | at_enum (enumNode *the_enum, Order ord) |
virtual void | at_suespec (suespecNode *the_suespec, Order ord) |
virtual void | at_expr (exprNode *the_expr, Order ord) |
virtual void | at_const (constNode *the_const, Order ord) |
virtual void | at_id (idNode *the_id, Order ord) |
virtual void | at_binary (binaryNode *the_binary, Order ord) |
virtual void | at_unary (unaryNode *the_unary, Order ord) |
virtual void | at_cast (castNode *the_cast, Order ord) |
virtual void | at_comma (commaNode *the_comma, Order ord) |
virtual void | at_ternary (ternaryNode *the_ternary, Order ord) |
virtual void | at_call (callNode *the_call, Order ord) |
virtual void | at_initializer (initializerNode *the_initializer, Order ord) |
virtual void | at_stmt (stmtNode *the_stmt, Order ord) |
virtual void | at_block (blockNode *the_block, Order ord) |
virtual void | at_basicblock (basicblockNode *the_basicblock, Order ord) |
virtual void | at_exprstmt (exprstmtNode *the_exprstmt, Order ord) |
virtual void | at_target (targetNode *the_target, Order ord) |
virtual void | at_label (labelNode *the_label, Order ord) |
virtual void | at_case (caseNode *the_case, Order ord) |
virtual void | at_selection (selectionNode *the_selection, Order ord) |
virtual void | at_if (ifNode *the_if, Order ord) |
virtual void | at_switch (switchNode *the_switch, Order ord) |
virtual void | at_loop (loopNode *the_loop, Order ord) |
virtual void | at_while (whileNode *the_while, Order ord) |
virtual void | at_do (doNode *the_do, Order ord) |
virtual void | at_for (forNode *the_for, Order ord) |
virtual void | at_jump (jumpNode *the_jump, Order ord) |
virtual void | at_goto (gotoNode *the_goto, Order ord) |
virtual void | at_continue (continueNode *the_continue, Order ord) |
virtual void | at_break (breakNode *the_break, Order ord) |
virtual void | at_return (returnNode *the_return, Order ord) |
virtual void | at_attrib (attribNode *the_attrib, Order ord) |
virtual void | at_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... |
A Walker is a functional class that traverses an abstract syntax tree, and performs some function at each of its Nodes. At each Node, the function it performs depends on the class of the Node.
Corresponding to each class in the Node hierarchy, Walker defines an "at_" method. To specify the function that should be performed for Nodes of a particular class, create a subclass of Walker that overrides the "at_" method corresponding to that class. By default, the "at_" methods perform no action. Thus, a Walker subclass only needs to override the "at_" methods corresponding to Node types that are relevant to that walker.
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 Walker 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 Walker 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 walker 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 Walker is created, the order(s) that Nodes are visited in should be specified. In general, all instances of a given subclass of Walker 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 Walker.
New instances of Walker can also specify the "depth" of the traversal. If the depth is "SubTree," then a Walker will visit the entire AST rooted at the Node that it is called on. If the depth is "NodeOnly," then the Walker will only call the "at_" method for Node it is called on, and not for any of its sub-Nodes. Thus, a Walker with a depth of NodeOnly functions as if it were a Visitor. If a Walker subclass will always use a depth of NodeOnly, then it should be written as a Visitor instead. The depth value is provided because it can occasionally be useful to use a Walker subclass that was designed to traverse an entire AST to simply visit one Node. In this case, you can simply instantiate the Walker subclass with a depth of NodeOnly.
It is possible for a Walker to modify the fields of the Nodes it visits. However, Walkers should not modify the structure of the AST that is being traversed. In other words, Walkers should only modify Node fields that do not contain sub-Nodes. If the structure of the traversed AST needs to be modified as it is traversed, then a Changer should be used instead of a Walker.
Currently, Walkers 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:
To use a walker "my_walker" to traverse an AST rooted at "my_node," use the code:
my_node->walk(my_walker);
|
Which Nodes in the AST tree should be visited.
|
|
The order in which AST Nodes should be visited.
|
|
Create a new instance of a Walker. This constructor is usually called by a subclass.
|
|
Reimplemented in vcgASTWalker, and print_walker. |
|
Reimplemented in vcgASTWalker, and print_walker. |
|
Reimplemented in fixPointerWalker, DefUseWalker, livenessRemover, reachingDefinitionsWalker, and reachingGenKillWalker. |
|
Reimplemented in vcgASTWalker, ComplicatedWalker, gcWalker, id_lookup_walker, print_walker, and Assignment_walker. |
|
Reimplemented in vcgASTWalker, scope_walker, and print_walker. |
|
Reimplemented in vcgASTWalker, ref_fix_walker, set_container_walker, and print_walker. |
|
Reimplemented in vcgASTWalker, vcgCCGWalker, ComplicatedWalker, id_lookup_walker, Linker, ref_fix_walker, print_walker, and callgraph_walker. |
|
Reimplemented in vcgASTWalker, ref_fix_walker, set_container_walker, and print_walker. |
|
Reimplemented in vcgASTWalker, ComplicatedWalker, and print_walker. |
|
Reimplemented in vcgASTWalker, ComplicatedWalker, and print_walker. |
|
Reimplemented in vcgASTWalker, print_walker, and count_walker. |
|
Reimplemented in vcgASTWalker, ref_fix_walker, set_container_walker, and print_walker. |
|
Reimplemented in vcgASTWalker, id_lookup_walker, name_mangle_walker, print_walker, and has_struct_walker. |
|
|
|
Reimplemented in vcgASTWalker, and print_walker. |
|
Reimplemented in enum_value_walker. |
|
Reimplemented in remove_stale_type_walker. |
|
Reimplemented in vcgASTWalker, semcheck_walker, and print_walker. |
|
Reimplemented in vcgASTWalker, and print_walker. |
|
Reimplemented in vcgASTWalker, scope_walker, print_walker, and has_struct_walker. |
|
Reimplemented in vcgASTWalker, UsedLabelWalker, goto_label_walker, ref_fix_walker, and print_walker. |
|
Reimplemented in vcgASTWalker, callGraph, clear_ids_walker, id_lookup_walker, Linker, name_mangle_walker, ref_fix_walker, print_walker, udChainRemover, renumber_walker, and count_walker. |
|
Reimplemented in vcgASTWalker, and print_walker. |
|
Reimplemented in vcgASTWalker, and print_walker. |
|
|
|
Reimplemented in vcgASTWalker, goto_label_walker, ref_fix_walker, and print_walker. |
|
Reimplemented in set_container_walker, and init_flowproblem_walker. |
|
Reimplemented in gcWalker, goto_label_walker, set_container_walker, sue_complete_walker, df_number_walker, and CleanGenKillWalker. |
|
Reimplemented in vcgASTWalker, and print_walker. |
|
|
Reimplemented in vcgASTWalker, and print_walker. |
|
Reimplemented in vcgASTWalker, ref_fix_walker, set_container_walker, and print_walker. |
|
|
|
Reimplemented in init_flowproblem_walker, and GetDefsWalker. |
|
|
|
|
|
Reimplemented in vcgASTWalker, print_walker, and has_struct_walker. |
|
Reimplemented in vcgASTWalker, scope_walker, semcheck_walker, sue_complete_walker, and print_walker. |
|
Reimplemented in vcgASTWalker, ref_fix_walker, set_container_walker, print_walker, and init_flowproblem_walker. |
|
Reimplemented in init_flowproblem_walker. |
|
Reimplemented in vcgASTWalker, clear_ids_walker, id_lookup_walker, ref_fix_walker, and print_walker. |
|
Reimplemented in vcgASTWalker, ComplicatedWalker, and print_walker. |
|
Reimplemented in vcgASTWalker, and print_walker. |
|
Reimplemented in discard_const_walker. |
|
Reimplemented in vcgASTWalker, ComplicatedWalker, print_walker, and fixPointerWalker. |
|
|
|
Reimplemented in vcgASTWalker, gcWalker, print_walker, and reachingGenKillWalker. |
|
Reimplemented in vcgASTWalker, and print_walker. |
|
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. |
|
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). |
|
Which Nodes in the AST should be visited.
|
|
The order in which AST Nodes should be visited.
|