Define __builtin_va_list as a built-in type.
[suif.git] / html / suif1_21.html
blobe84adfa729a0de1a49090cdf072765ac7d77eb58
1 <HTML>
2 <HEAD>
3 <!-- This HTML file has been created by texi2html 1.54
4 from suif1.texi on 28 April 1999 -->
6 <TITLE>The SUIF Version 1 Library - Mapping Subtrees</TITLE>
7 <link href="suif1_22.html" rel=Next>
8 <link href="suif1_20.html" rel=Previous>
9 <link href="suif1_toc.html" rel=ToC>
11 </HEAD>
12 <BODY>
13 <p>Go to the <A HREF="suif1_1.html">first</A>, <A HREF="suif1_20.html">previous</A>, <A HREF="suif1_22.html">next</A>, <A HREF="suif1_113.html">last</A> section, <A HREF="suif1_toc.html">table of contents</A>.
14 <P><HR><P>
17 <H2><A NAME="SEC21" HREF="suif1_toc.html#TOC21">Mapping Functions Over Subtrees</A></H2>
18 <P>
19 <A NAME="IDX128"></A>
21 </P>
22 <P>
23 <A NAME="IDX129"></A>
24 <A NAME="IDX130"></A>
25 Many SUIF programs contain code that visits all of the nodes in an AST
26 to perform various operations. Rather than having every programmer
27 duplicate the code for this traversal, the SUIF library includes methods
28 to map functions over ASTs. The <CODE>tree_node</CODE> and
29 <CODE>tree_node_list</CODE> classes both provide these <CODE>map</CODE> methods.
31 </P>
32 <P>
33 The function to be mapped must be of type <CODE>tree_map_f</CODE>. For a tree
34 node, the <CODE>map</CODE> method applies the function to the tree node and
35 all of its children. The <CODE>preorder</CODE> parameter is used to select
36 either preorder or postorder traversals; the default is preorder. For
37 preorder traversals, the function is applied to the tree node before it
38 is applied to the children. For postorder, the function is first
39 applied to the tree_node's children and descendants and then last
40 applied to the tree node itself.
42 </P>
43 <P>
44 The <CODE>map</CODE> method for a tree node list calls <CODE>map</CODE> for each
45 node in the list. The nodes are visited in order from first to last
46 regardless of the <CODE>preorder</CODE> parameter. The <CODE>tree_node_list</CODE>
47 version of the <CODE>map</CODE> method has an extra optional parameter that
48 allows you to only map the function over the entries in the list without
49 recursively visiting their children.
51 </P>
52 <P>
53 The <CODE>tree_map_f</CODE> functions have two parameters: a pointer to the
54 tree node and a <CODE>void*</CODE> value passed on from the <CODE>map</CODE> method.
55 Additional parameters can be passed by making the <CODE>void*</CODE> value a
56 pointer to a structure containing the parameters. For example, the
57 following code counts the number of <CODE>tree_for</CODE> and <CODE>tree_loop</CODE>
58 nodes in a procedure:
60 </P>
62 <PRE>
63 struct count_result {
64 int num_fors;
65 int num_loops;
68 void count_loops (tree_node *t, void *x)
70 count_result *results = (count_result *)x;
72 if (t-&#62;kind() == TREE_FOR) results-&#62;num_fors++;
73 else if (t-&#62;kind() == TREE_LOOP) results-&#62;num_loops++;
77 * Return the number of tree_fors and tree_loops in the procedure.
80 void counter (tree_proc *p, int *for_count, int *loop_count)
82 count_result results;
83 results.num_fors = 0;
84 results.num_loops = 0;
86 p-&#62;map(count_loops, (void *)&#38;results);
88 *for_count = results.num_fors;
89 *loop_count = results.num_loops;
91 </PRE>
93 <P><HR><P>
94 <p>Go to the <A HREF="suif1_1.html">first</A>, <A HREF="suif1_20.html">previous</A>, <A HREF="suif1_22.html">next</A>, <A HREF="suif1_113.html">last</A> section, <A HREF="suif1_toc.html">table of contents</A>.
95 </BODY>
96 </HTML>