daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright (c) 2002-2010 The ANGLE Project Authors. All rights reserved. |
| 3 | // Use of this source code is governed by a BSD-style license that can be |
| 4 | // found in the LICENSE file. |
| 5 | // |
| 6 | |
Jamie Madill | b1a85f4 | 2014-08-19 15:23:24 -0400 | [diff] [blame] | 7 | #include "compiler/translator/IntermNode.h" |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 8 | |
| 9 | // |
| 10 | // Traverse the intermediate representation tree, and |
| 11 | // call a node type specific function for each node. |
| 12 | // Done recursively through the member function Traverse(). |
| 13 | // Node types can be skipped if their function to call is 0, |
| 14 | // but their subtree will still be traversed. |
| 15 | // Nodes with children can have their whole subtree skipped |
| 16 | // if preVisit is turned on and the type specific function |
| 17 | // returns false. |
| 18 | // |
| 19 | // preVisit, postVisit, and rightToLeft control what order |
| 20 | // nodes are visited in. |
| 21 | // |
| 22 | |
| 23 | // |
| 24 | // Traversal functions for terminals are straighforward.... |
| 25 | // |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 26 | void TIntermSymbol::traverse(TIntermTraverser *it) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 27 | { |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 28 | it->visitSymbol(this); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 29 | } |
| 30 | |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 31 | void TIntermConstantUnion::traverse(TIntermTraverser *it) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 32 | { |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 33 | it->visitConstantUnion(this); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | // |
| 37 | // Traverse a binary node. |
| 38 | // |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 39 | void TIntermBinary::traverse(TIntermTraverser *it) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 40 | { |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 41 | bool visit = true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 42 | |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 43 | // |
| 44 | // visit the node before children if pre-visiting. |
| 45 | // |
| 46 | if (it->preVisit) |
| 47 | visit = it->visitBinary(PreVisit, this); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 48 | |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 49 | // |
| 50 | // Visit the children, in the right order. |
| 51 | // |
| 52 | if (visit) |
| 53 | { |
Zhenyao Mo | 7cab38b | 2013-10-15 12:59:30 -0700 | [diff] [blame] | 54 | it->incrementDepth(this); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 55 | |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 56 | if (it->rightToLeft) |
| 57 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 58 | if (mRight) |
| 59 | mRight->traverse(it); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 60 | |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 61 | if (it->inVisit) |
| 62 | visit = it->visitBinary(InVisit, this); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 63 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 64 | if (visit && mLeft) |
| 65 | mLeft->traverse(it); |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 66 | } |
| 67 | else |
| 68 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 69 | if (mLeft) |
| 70 | mLeft->traverse(it); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 71 | |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 72 | if (it->inVisit) |
| 73 | visit = it->visitBinary(InVisit, this); |
| 74 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 75 | if (visit && mRight) |
| 76 | mRight->traverse(it); |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | it->decrementDepth(); |
| 80 | } |
| 81 | |
| 82 | // |
| 83 | // Visit the node after the children, if requested and the traversal |
| 84 | // hasn't been cancelled yet. |
| 85 | // |
| 86 | if (visit && it->postVisit) |
| 87 | it->visitBinary(PostVisit, this); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 88 | } |
| 89 | |
| 90 | // |
| 91 | // Traverse a unary node. Same comments in binary node apply here. |
| 92 | // |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 93 | void TIntermUnary::traverse(TIntermTraverser *it) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 94 | { |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 95 | bool visit = true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 96 | |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 97 | if (it->preVisit) |
| 98 | visit = it->visitUnary(PreVisit, this); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 99 | |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 100 | if (visit) { |
Zhenyao Mo | 7cab38b | 2013-10-15 12:59:30 -0700 | [diff] [blame] | 101 | it->incrementDepth(this); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 102 | mOperand->traverse(it); |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 103 | it->decrementDepth(); |
| 104 | } |
| 105 | |
| 106 | if (visit && it->postVisit) |
| 107 | it->visitUnary(PostVisit, this); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 108 | } |
| 109 | |
| 110 | // |
| 111 | // Traverse an aggregate node. Same comments in binary node apply here. |
| 112 | // |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 113 | void TIntermAggregate::traverse(TIntermTraverser *it) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 114 | { |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 115 | bool visit = true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 116 | |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 117 | if (it->preVisit) |
| 118 | visit = it->visitAggregate(PreVisit, this); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 119 | |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 120 | if (visit) |
| 121 | { |
Zhenyao Mo | 7cab38b | 2013-10-15 12:59:30 -0700 | [diff] [blame] | 122 | it->incrementDepth(this); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 123 | |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 124 | if (it->rightToLeft) |
| 125 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 126 | for (TIntermSequence::reverse_iterator sit = mSequence.rbegin(); |
| 127 | sit != mSequence.rend(); sit++) |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 128 | { |
| 129 | (*sit)->traverse(it); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 130 | |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 131 | if (visit && it->inVisit) |
| 132 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 133 | if (*sit != mSequence.front()) |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 134 | visit = it->visitAggregate(InVisit, this); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | else |
| 139 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 140 | for (TIntermSequence::iterator sit = mSequence.begin(); |
| 141 | sit != mSequence.end(); sit++) |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 142 | { |
| 143 | (*sit)->traverse(it); |
| 144 | |
| 145 | if (visit && it->inVisit) |
| 146 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 147 | if (*sit != mSequence.back()) |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 148 | visit = it->visitAggregate(InVisit, this); |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | it->decrementDepth(); |
| 154 | } |
| 155 | |
| 156 | if (visit && it->postVisit) |
| 157 | it->visitAggregate(PostVisit, this); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | // |
| 161 | // Traverse a selection node. Same comments in binary node apply here. |
| 162 | // |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 163 | void TIntermSelection::traverse(TIntermTraverser *it) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 164 | { |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 165 | bool visit = true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 166 | |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 167 | if (it->preVisit) |
| 168 | visit = it->visitSelection(PreVisit, this); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 169 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 170 | if (visit) |
| 171 | { |
Zhenyao Mo | 7cab38b | 2013-10-15 12:59:30 -0700 | [diff] [blame] | 172 | it->incrementDepth(this); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 173 | if (it->rightToLeft) |
| 174 | { |
| 175 | if (mFalseBlock) |
| 176 | mFalseBlock->traverse(it); |
| 177 | if (mTrueBlock) |
| 178 | mTrueBlock->traverse(it); |
| 179 | mCondition->traverse(it); |
| 180 | } |
| 181 | else |
| 182 | { |
| 183 | mCondition->traverse(it); |
| 184 | if (mTrueBlock) |
| 185 | mTrueBlock->traverse(it); |
| 186 | if (mFalseBlock) |
| 187 | mFalseBlock->traverse(it); |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 188 | } |
| 189 | it->decrementDepth(); |
| 190 | } |
| 191 | |
| 192 | if (visit && it->postVisit) |
| 193 | it->visitSelection(PostVisit, this); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | // |
Olli Etuaho | a3a3666 | 2015-02-17 13:46:51 +0200 | [diff] [blame^] | 197 | // Traverse a switch node. Same comments in binary node apply here. |
| 198 | // |
| 199 | void TIntermSwitch::traverse(TIntermTraverser *it) |
| 200 | { |
| 201 | bool visit = true; |
| 202 | |
| 203 | if (it->preVisit) |
| 204 | visit = it->visitSwitch(PreVisit, this); |
| 205 | |
| 206 | if (visit) |
| 207 | { |
| 208 | it->incrementDepth(this); |
| 209 | if (it->rightToLeft) |
| 210 | { |
| 211 | if (mStatementList) |
| 212 | mStatementList->traverse(it); |
| 213 | if (it->inVisit) |
| 214 | visit = it->visitSwitch(InVisit, this); |
| 215 | if (visit) |
| 216 | mInit->traverse(it); |
| 217 | } |
| 218 | else |
| 219 | { |
| 220 | mInit->traverse(it); |
| 221 | if (it->inVisit) |
| 222 | visit = it->visitSwitch(InVisit, this); |
| 223 | if (visit && mStatementList) |
| 224 | mStatementList->traverse(it); |
| 225 | } |
| 226 | it->decrementDepth(); |
| 227 | } |
| 228 | |
| 229 | if (visit && it->postVisit) |
| 230 | it->visitSwitch(PostVisit, this); |
| 231 | } |
| 232 | |
| 233 | // |
| 234 | // Traverse a switch node. Same comments in binary node apply here. |
| 235 | // |
| 236 | void TIntermCase::traverse(TIntermTraverser *it) |
| 237 | { |
| 238 | bool visit = true; |
| 239 | |
| 240 | if (it->preVisit) |
| 241 | visit = it->visitCase(PreVisit, this); |
| 242 | |
| 243 | if (visit && mCondition) |
| 244 | mCondition->traverse(it); |
| 245 | |
| 246 | if (visit && it->postVisit) |
| 247 | it->visitCase(PostVisit, this); |
| 248 | } |
| 249 | |
| 250 | // |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 251 | // Traverse a loop node. Same comments in binary node apply here. |
| 252 | // |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 253 | void TIntermLoop::traverse(TIntermTraverser *it) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 254 | { |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 255 | bool visit = true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 256 | |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 257 | if (it->preVisit) |
| 258 | visit = it->visitLoop(PreVisit, this); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 259 | |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 260 | if (visit) |
| 261 | { |
Zhenyao Mo | 7cab38b | 2013-10-15 12:59:30 -0700 | [diff] [blame] | 262 | it->incrementDepth(this); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 263 | |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 264 | if (it->rightToLeft) |
| 265 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 266 | if (mExpr) |
| 267 | mExpr->traverse(it); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 268 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 269 | if (mBody) |
| 270 | mBody->traverse(it); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 271 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 272 | if (mCond) |
| 273 | mCond->traverse(it); |
Zhenyao Mo | 6cb95f3 | 2013-10-03 17:01:52 -0700 | [diff] [blame] | 274 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 275 | if (mInit) |
| 276 | mInit->traverse(it); |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 277 | } |
| 278 | else |
| 279 | { |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 280 | if (mInit) |
| 281 | mInit->traverse(it); |
Zhenyao Mo | 6cb95f3 | 2013-10-03 17:01:52 -0700 | [diff] [blame] | 282 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 283 | if (mCond) |
| 284 | mCond->traverse(it); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 285 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 286 | if (mBody) |
| 287 | mBody->traverse(it); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 288 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 289 | if (mExpr) |
| 290 | mExpr->traverse(it); |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 291 | } |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 292 | |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 293 | it->decrementDepth(); |
| 294 | } |
| 295 | |
| 296 | if (visit && it->postVisit) |
| 297 | it->visitLoop(PostVisit, this); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | // |
| 301 | // Traverse a branch node. Same comments in binary node apply here. |
| 302 | // |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 303 | void TIntermBranch::traverse(TIntermTraverser *it) |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 304 | { |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 305 | bool visit = true; |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 306 | |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 307 | if (it->preVisit) |
| 308 | visit = it->visitBranch(PreVisit, this); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 309 | |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 310 | if (visit && mExpression) { |
Zhenyao Mo | 7cab38b | 2013-10-15 12:59:30 -0700 | [diff] [blame] | 311 | it->incrementDepth(this); |
Zhenyao Mo | e40d1e9 | 2014-07-16 17:40:36 -0700 | [diff] [blame] | 312 | mExpression->traverse(it); |
Zhenyao Mo | e88dcaf | 2013-10-03 16:55:19 -0700 | [diff] [blame] | 313 | it->decrementDepth(); |
| 314 | } |
| 315 | |
| 316 | if (visit && it->postVisit) |
| 317 | it->visitBranch(PostVisit, this); |
daniel@transgaming.com | 4f39fd9 | 2010-03-08 20:26:45 +0000 | [diff] [blame] | 318 | } |
| 319 | |
Jamie Madill | 4cfb1e8 | 2014-07-07 12:49:23 -0400 | [diff] [blame] | 320 | void TIntermRaw::traverse(TIntermTraverser *it) |
| 321 | { |
| 322 | it->visitRaw(this); |
| 323 | } |