summaryrefslogtreecommitdiff
path: root/ir_hierarchical_visitor.h
blob: daf220906ad107f30b611259b310db47d9724016 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/* -*- c++ -*- */
/*
 * Copyright © 2010 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#pragma once
#ifndef IR_HIERARCHICAL_VISITOR_H
#define IR_HIERARCHICAL_VISITOR_H

/**
 * Enumeration values returned by visit methods to guide processing
 */
enum ir_visitor_status {
   visit_continue,		/**< Continue visiting as normal. */
   visit_continue_with_parent,	/**< Don't visit siblings, continue w/parent. */
   visit_stop			/**< Stop visiting immediately. */
};


/**
 * Base class of hierarchical visitors of IR instruction trees
 *
 * Hierarchical visitors differ from traditional visitors in a couple of
 * important ways.  Rather than having a single \c visit method for each
 * subclass in the composite, there are three kinds of visit methods.
 * Leaf-node classes have a traditional \c visit method.  Internal-node
 * classes have a \c visit_enter method, which is invoked just before
 * processing child nodes, and a \c visit_leave method which is invoked just
 * after processing child nodes.
 *
 * In addition, each visit method and the \c accept methods in the composite
 * have a return value which guides the navigation.  Any of the visit methods
 * can choose to continue visiting the tree as normal (by returning \c
 * visit_continue), terminate visiting any further nodes immediately (by
 * returning \c visit_stop), or stop visiting sibling nodes (by returning \c
 * visit_continue_with_parent).
 *
 * These two changes combine to allow nagivation of children to be implemented
 * in the composite's \c accept method.  The \c accept method for a leaf-node
 * class will simply call the \c visit method, as usual, and pass its return
 * value on.  The \c accept method for internal-node classes will call the \c
 * visit_enter method, call the \c accpet method of each child node, and,
 * finally, call the \c visit_leave method.  If any of these return a value
 * other that \c visit_continue, the correct action must be taken.
 *
 * The final benefit is that the hierarchical visitor base class need not be
 * abstract.  Default implementations of every \c visit, \c visit_enter, and
 * \c visit_leave method can be provided.  By default each of these methods
 * simply returns \c visit_continue.  This allows a significant reduction in
 * derived class code.
 *
 * For more information about hierarchical visitors, see:
 *
 *    http://c2.com/cgi/wiki?HierarchicalVisitorPattern
 *    http://c2.com/cgi/wiki?HierarchicalVisitorDiscussion
 */

class ir_hierarchical_visitor {
public:
   /**
    * \name Visit methods for leaf-node classes
    */
   /*@{*/
   virtual ir_visitor_status visit(class ir_variable *);
   virtual ir_visitor_status visit(class ir_constant *);
   virtual ir_visitor_status visit(class ir_loop_jump *);
   /*@}*/

   /**
    * \name Visit methods for internal-node classes
    */
   /*@{*/
   virtual ir_visitor_status visit_enter(class ir_loop *);
   virtual ir_visitor_status visit_leave(class ir_loop *);
   virtual ir_visitor_status visit_enter(class ir_function_signature *);
   virtual ir_visitor_status visit_leave(class ir_function_signature *);
   virtual ir_visitor_status visit_enter(class ir_function *);
   virtual ir_visitor_status visit_leave(class ir_function *);
   virtual ir_visitor_status visit_enter(class ir_expression *);
   virtual ir_visitor_status visit_leave(class ir_expression *);
   virtual ir_visitor_status visit_enter(class ir_swizzle *);
   virtual ir_visitor_status visit_leave(class ir_swizzle *);
   virtual ir_visitor_status visit_enter(class ir_dereference *);
   virtual ir_visitor_status visit_leave(class ir_dereference *);
   virtual ir_visitor_status visit_enter(class ir_assignment *);
   virtual ir_visitor_status visit_leave(class ir_assignment *);
   virtual ir_visitor_status visit_enter(class ir_call *);
   virtual ir_visitor_status visit_leave(class ir_call *);
   virtual ir_visitor_status visit_enter(class ir_return *);
   virtual ir_visitor_status visit_leave(class ir_return *);
   virtual ir_visitor_status visit_enter(class ir_if *);
   virtual ir_visitor_status visit_leave(class ir_if *);
   /*@}*/


   /**
    * Utility function to process a linked list of instructions with a visitor
    */
   void run(struct exec_list *instructions);
};

#endif /* IR_HIERARCHICAL_VISITOR_H */