2 // Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
3 // Copyright (C) 2013 LunarG, Inc.
4 // Copyright (c) 2002-2010 The ANGLE Project Authors.
6 // All rights reserved.
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 straightforward....
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);
74 const TString& TIntermSymbol::getAccessName() const {
75 if (getBasicType() == EbtBlock)
76 return getType().getTypeName();
82 // Traverse a binary node.
84 void TIntermBinary::traverse(TIntermTraverser *it)
89 // visit the node before children if pre-visiting.
92 visit = it->visitBinary(EvPreVisit, this);
95 // Visit the children, in the right order.
98 it->incrementDepth(this);
100 if (it->rightToLeft) {
105 visit = it->visitBinary(EvInVisit, this);
114 visit = it->visitBinary(EvInVisit, this);
120 it->decrementDepth();
124 // Visit the node after the children, if requested and the traversal
125 // hasn't been canceled yet.
127 if (visit && it->postVisit)
128 it->visitBinary(EvPostVisit, this);
132 // Traverse a unary node. Same comments in binary node apply here.
134 void TIntermUnary::traverse(TIntermTraverser *it)
139 visit = it->visitUnary(EvPreVisit, this);
142 it->incrementDepth(this);
143 operand->traverse(it);
144 it->decrementDepth();
147 if (visit && it->postVisit)
148 it->visitUnary(EvPostVisit, this);
152 // Traverse an aggregate node. Same comments in binary node apply here.
154 void TIntermAggregate::traverse(TIntermTraverser *it)
159 visit = it->visitAggregate(EvPreVisit, this);
162 it->incrementDepth(this);
164 if (it->rightToLeft) {
165 for (TIntermSequence::reverse_iterator sit = sequence.rbegin(); sit != sequence.rend(); sit++) {
166 (*sit)->traverse(it);
168 if (visit && it->inVisit) {
169 if (*sit != sequence.front())
170 visit = it->visitAggregate(EvInVisit, this);
174 for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++) {
175 (*sit)->traverse(it);
177 if (visit && it->inVisit) {
178 if (*sit != sequence.back())
179 visit = it->visitAggregate(EvInVisit, this);
184 it->decrementDepth();
187 if (visit && it->postVisit)
188 it->visitAggregate(EvPostVisit, this);
192 // Traverse a selection node. Same comments in binary node apply here.
194 void TIntermSelection::traverse(TIntermTraverser *it)
199 visit = it->visitSelection(EvPreVisit, this);
202 it->incrementDepth(this);
203 if (it->rightToLeft) {
205 falseBlock->traverse(it);
207 trueBlock->traverse(it);
208 condition->traverse(it);
210 condition->traverse(it);
212 trueBlock->traverse(it);
214 falseBlock->traverse(it);
216 it->decrementDepth();
219 if (visit && it->postVisit)
220 it->visitSelection(EvPostVisit, this);
224 // Traverse a loop node. Same comments in binary node apply here.
226 void TIntermLoop::traverse(TIntermTraverser *it)
231 visit = it->visitLoop(EvPreVisit, this);
234 it->incrementDepth(this);
236 if (it->rightToLeft) {
238 terminal->traverse(it);
253 terminal->traverse(it);
256 it->decrementDepth();
259 if (visit && it->postVisit)
260 it->visitLoop(EvPostVisit, this);
264 // Traverse a branch node. Same comments in binary node apply here.
266 void TIntermBranch::traverse(TIntermTraverser *it)
271 visit = it->visitBranch(EvPreVisit, this);
273 if (visit && expression) {
274 it->incrementDepth(this);
275 expression->traverse(it);
276 it->decrementDepth();
279 if (visit && it->postVisit)
280 it->visitBranch(EvPostVisit, this);
284 // Traverse a switch node.
286 void TIntermSwitch::traverse(TIntermTraverser* it)
291 visit = it->visitSwitch(EvPreVisit, this);
294 it->incrementDepth(this);
295 if (it->rightToLeft) {
297 condition->traverse(it);
299 condition->traverse(it);
302 it->decrementDepth();
305 if (visit && it->postVisit)
306 it->visitSwitch(EvPostVisit, this);
309 } // end namespace glslang