The tree_node
class includes several methods to make it more
convenient to retrieve information that is not directly stored in a
node. The scope
method returns the symbol table for the closest
enclosing scope at a tree node. The proc
method ascends to the
root of the AST to find the symbol for the procedure containing the tree
node. Of course, that only works if the node is properly attached to an
AST that is connected to a particular procedure symbol.
Textual representations of tree nodes can easily be printed out to files
using the print
method. The optional depth
parameter
specifies the indentation level for the output. The output formats for
most kinds of tree nodes are shown in subsequent sections. For
instruction nodes, the SUIF instructions are printed directly.
All tree nodes, except for tree_proc
nodes, are stored on tree
node lists. Each tree node has a back-pointer to the list that contains
it. The parent
method is used to access this list pointer. The
parent of a tree_proc
is always NULL
. The parent list
pointers are set automatically when tree nodes are added to lists, so
you do not have to deal with maintaining these yourself.
Like the other lists in SUIF (see section Generic Lists), tree node lists
are made up of list elements. Each of these list elements contains a
pointer to a particular tree node. Because many of the list methods
operate on the list elements rather than the tree nodes, one often needs
to know the element containing a tree node. Thus the tree nodes contain
back-pointers to the list elements, which can be accessed with the
list_e
method. Like the parent pointer, the list element pointer
is automatically set when a tree node is entered in a list.
For example, the list elements are needed for inserting nodes before or
after other nodes in a list and for removing nodes from their lists.
The following code inserts the tree node new_node
before the tree
node cur_node
and then removes cur_node
from the list:
tree_node_list *tnl = cur_node->parent(); tnl->insert_before(new_node, cur_node->list_e()); tree_node_list_e *elem = tnl->remove(cur_node->list_e());