2 //Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
3 //Copyright (C) 2013 LunarG, Inc.
4 //Copyright (c) 2002-2010 The ANGLE Project Authors.
8 //Redistribution and use in source and binary forms, with or without
9 //modification, are permitted provided that the following conditions
12 // Redistributions of source code must retain the above copyright
13 // notice, this list of conditions and the following disclaimer.
15 // Redistributions in binary form must reproduce the above
16 // copyright notice, this list of conditions and the following
17 // disclaimer in the documentation and/or other materials provided
18 // with the distribution.
20 // Neither the name of 3Dlabs Inc. Ltd. nor the names of its
21 // contributors may be used to endorse or promote products derived
22 // from this software without specific prior written permission.
24 //THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 //"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 //LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 //FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
28 //COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
29 //INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
30 //BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31 //LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
32 //CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33 //LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
34 //ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 //POSSIBILITY OF SUCH DAMAGE.
38 #include "../Include/intermediate.h"
43 // Traverse the intermediate representation tree, and
44 // call a node type specific function for each node.
45 // Done recursively through the member function Traverse().
46 // Node types can be skipped if their function to call is 0,
47 // but their subtree will still be traversed.
48 // Nodes with children can have their whole subtree skipped
49 // if preVisit is turned on and the type specific function
52 // preVisit, postVisit, and rightToLeft control what order
53 // nodes are visited in.
57 // Traversal functions for terminals are straighforward....
59 void TIntermMethod::traverse(TIntermTraverser*)
61 // Tree should always resolve all methods as a non-method.
64 void TIntermSymbol::traverse(TIntermTraverser *it)
66 it->visitSymbol(this);
69 void TIntermConstantUnion::traverse(TIntermTraverser *it)
71 it->visitConstantUnion(this);
75 // Traverse a binary node.
77 void TIntermBinary::traverse(TIntermTraverser *it)
82 // visit the node before children if pre-visiting.
85 visit = it->visitBinary(EvPreVisit, this);
88 // Visit the children, in the right order.
91 it->incrementDepth(this);
93 if (it->rightToLeft) {
98 visit = it->visitBinary(EvInVisit, this);
107 visit = it->visitBinary(EvInVisit, this);
113 it->decrementDepth();
117 // Visit the node after the children, if requested and the traversal
118 // hasn't been cancelled yet.
120 if (visit && it->postVisit)
121 it->visitBinary(EvPostVisit, this);
125 // Traverse a unary node. Same comments in binary node apply here.
127 void TIntermUnary::traverse(TIntermTraverser *it)
132 visit = it->visitUnary(EvPreVisit, this);
135 it->incrementDepth(this);
136 operand->traverse(it);
137 it->decrementDepth();
140 if (visit && it->postVisit)
141 it->visitUnary(EvPostVisit, this);
145 // Traverse an aggregate node. Same comments in binary node apply here.
147 void TIntermAggregate::traverse(TIntermTraverser *it)
152 visit = it->visitAggregate(EvPreVisit, this);
155 it->incrementDepth(this);
157 if (it->rightToLeft) {
158 for (TIntermSequence::reverse_iterator sit = sequence.rbegin(); sit != sequence.rend(); sit++) {
159 (*sit)->traverse(it);
161 if (visit && it->inVisit) {
162 if (*sit != sequence.front())
163 visit = it->visitAggregate(EvInVisit, this);
167 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++) {
168 (*sit)->traverse(it);
170 if (visit && it->inVisit) {
171 if (*sit != sequence.back())
172 visit = it->visitAggregate(EvInVisit, this);
177 it->decrementDepth();
180 if (visit && it->postVisit)
181 it->visitAggregate(EvPostVisit, this);
185 // Traverse a selection node. Same comments in binary node apply here.
187 void TIntermSelection::traverse(TIntermTraverser *it)
192 visit = it->visitSelection(EvPreVisit, this);
195 it->incrementDepth(this);
196 if (it->rightToLeft) {
198 falseBlock->traverse(it);
200 trueBlock->traverse(it);
201 condition->traverse(it);
203 condition->traverse(it);
205 trueBlock->traverse(it);
207 falseBlock->traverse(it);
209 it->decrementDepth();
212 if (visit && it->postVisit)
213 it->visitSelection(EvPostVisit, this);
217 // Traverse a loop node. Same comments in binary node apply here.
219 void TIntermLoop::traverse(TIntermTraverser *it)
224 visit = it->visitLoop(EvPreVisit, this);
227 it->incrementDepth(this);
229 if (it->rightToLeft) {
231 terminal->traverse(it);
246 terminal->traverse(it);
249 it->decrementDepth();
252 if (visit && it->postVisit)
253 it->visitLoop(EvPostVisit, this);
257 // Traverse a branch node. Same comments in binary node apply here.
259 void TIntermBranch::traverse(TIntermTraverser *it)
264 visit = it->visitBranch(EvPreVisit, this);
266 if (visit && expression) {
267 it->incrementDepth(this);
268 expression->traverse(it);
269 it->decrementDepth();
272 if (visit && it->postVisit)
273 it->visitBranch(EvPostVisit, this);
277 // Traverse a switch node.
279 void TIntermSwitch::traverse(TIntermTraverser* it)
284 visit = it->visitSwitch(EvPreVisit, this);
287 it->incrementDepth(this);
288 if (it->rightToLeft) {
290 condition->traverse(it);
292 condition->traverse(it);
295 it->decrementDepth();
298 if (visit && it->postVisit)
299 it->visitSwitch(EvPostVisit, this);
302 } // end namespace glslang