John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [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 | |
Nicolas Capens | cc863da | 2015-01-21 15:50:55 -0500 | [diff] [blame] | 7 | #include "intermediate.h" |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [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 | // |
| 26 | void TIntermSymbol::traverse(TIntermTraverser* it) |
| 27 | { |
| 28 | it->visitSymbol(this); |
| 29 | } |
| 30 | |
| 31 | void TIntermConstantUnion::traverse(TIntermTraverser* it) |
| 32 | { |
| 33 | it->visitConstantUnion(this); |
| 34 | } |
| 35 | |
| 36 | // |
| 37 | // Traverse a binary node. |
| 38 | // |
| 39 | void TIntermBinary::traverse(TIntermTraverser* it) |
| 40 | { |
| 41 | bool visit = true; |
| 42 | |
| 43 | // |
| 44 | // visit the node before children if pre-visiting. |
| 45 | // |
| 46 | if(it->preVisit) |
| 47 | { |
| 48 | visit = it->visitBinary(PreVisit, this); |
| 49 | } |
| 50 | |
| 51 | // |
| 52 | // Visit the children, in the right order. |
| 53 | // |
| 54 | if(visit) |
| 55 | { |
Alexis Hetu | 76a343a | 2015-06-04 17:21:22 -0400 | [diff] [blame^] | 56 | it->incrementDepth(this); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 57 | |
| 58 | if(it->rightToLeft) |
| 59 | { |
| 60 | if(right) |
| 61 | { |
| 62 | right->traverse(it); |
| 63 | } |
| 64 | |
| 65 | if(it->inVisit) |
| 66 | { |
| 67 | visit = it->visitBinary(InVisit, this); |
| 68 | } |
| 69 | |
| 70 | if(visit && left) |
| 71 | { |
| 72 | left->traverse(it); |
| 73 | } |
| 74 | } |
| 75 | else |
| 76 | { |
| 77 | if(left) |
| 78 | { |
| 79 | left->traverse(it); |
| 80 | } |
| 81 | |
| 82 | if(it->inVisit) |
| 83 | { |
| 84 | visit = it->visitBinary(InVisit, this); |
| 85 | } |
| 86 | |
| 87 | if(visit && right) |
| 88 | { |
| 89 | right->traverse(it); |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | it->decrementDepth(); |
| 94 | } |
| 95 | |
| 96 | // |
| 97 | // Visit the node after the children, if requested and the traversal |
| 98 | // hasn't been cancelled yet. |
| 99 | // |
| 100 | if(visit && it->postVisit) |
| 101 | { |
| 102 | it->visitBinary(PostVisit, this); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // |
| 107 | // Traverse a unary node. Same comments in binary node apply here. |
| 108 | // |
| 109 | void TIntermUnary::traverse(TIntermTraverser* it) |
| 110 | { |
| 111 | bool visit = true; |
| 112 | |
| 113 | if (it->preVisit) |
| 114 | visit = it->visitUnary(PreVisit, this); |
| 115 | |
| 116 | if (visit) { |
Alexis Hetu | 76a343a | 2015-06-04 17:21:22 -0400 | [diff] [blame^] | 117 | it->incrementDepth(this); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 118 | operand->traverse(it); |
| 119 | it->decrementDepth(); |
| 120 | } |
| 121 | |
| 122 | if (visit && it->postVisit) |
| 123 | it->visitUnary(PostVisit, this); |
| 124 | } |
| 125 | |
| 126 | // |
| 127 | // Traverse an aggregate node. Same comments in binary node apply here. |
| 128 | // |
| 129 | void TIntermAggregate::traverse(TIntermTraverser* it) |
| 130 | { |
| 131 | bool visit = true; |
| 132 | |
| 133 | if(it->preVisit) |
| 134 | { |
| 135 | visit = it->visitAggregate(PreVisit, this); |
| 136 | } |
| 137 | |
| 138 | if(visit) |
| 139 | { |
Alexis Hetu | 76a343a | 2015-06-04 17:21:22 -0400 | [diff] [blame^] | 140 | it->incrementDepth(this); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 141 | |
| 142 | if(it->rightToLeft) |
| 143 | { |
| 144 | for(TIntermSequence::reverse_iterator sit = sequence.rbegin(); sit != sequence.rend(); sit++) |
| 145 | { |
| 146 | (*sit)->traverse(it); |
| 147 | |
| 148 | if(visit && it->inVisit) |
| 149 | { |
| 150 | if(*sit != sequence.front()) |
| 151 | { |
| 152 | visit = it->visitAggregate(InVisit, this); |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | else |
| 158 | { |
| 159 | for(TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++) |
| 160 | { |
| 161 | (*sit)->traverse(it); |
| 162 | |
| 163 | if(visit && it->inVisit) |
| 164 | { |
| 165 | if(*sit != sequence.back()) |
| 166 | { |
| 167 | visit = it->visitAggregate(InVisit, this); |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | it->decrementDepth(); |
| 174 | } |
| 175 | |
| 176 | if(visit && it->postVisit) |
| 177 | { |
| 178 | it->visitAggregate(PostVisit, this); |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | // |
| 183 | // Traverse a selection node. Same comments in binary node apply here. |
| 184 | // |
| 185 | void TIntermSelection::traverse(TIntermTraverser* it) |
| 186 | { |
| 187 | bool visit = true; |
| 188 | |
| 189 | if (it->preVisit) |
| 190 | visit = it->visitSelection(PreVisit, this); |
| 191 | |
| 192 | if (visit) { |
Alexis Hetu | 76a343a | 2015-06-04 17:21:22 -0400 | [diff] [blame^] | 193 | it->incrementDepth(this); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 194 | if (it->rightToLeft) { |
| 195 | if (falseBlock) |
| 196 | falseBlock->traverse(it); |
| 197 | if (trueBlock) |
| 198 | trueBlock->traverse(it); |
| 199 | condition->traverse(it); |
| 200 | } else { |
| 201 | condition->traverse(it); |
| 202 | if (trueBlock) |
| 203 | trueBlock->traverse(it); |
| 204 | if (falseBlock) |
| 205 | falseBlock->traverse(it); |
| 206 | } |
| 207 | it->decrementDepth(); |
| 208 | } |
| 209 | |
| 210 | if (visit && it->postVisit) |
| 211 | it->visitSelection(PostVisit, this); |
| 212 | } |
| 213 | |
| 214 | // |
Alexis Hetu | 76a343a | 2015-06-04 17:21:22 -0400 | [diff] [blame^] | 215 | // Traverse a switch node. Same comments in binary node apply here. |
| 216 | // |
| 217 | void TIntermSwitch::traverse(TIntermTraverser *it) |
| 218 | { |
| 219 | bool visit = true; |
| 220 | |
| 221 | if(it->preVisit) |
| 222 | visit = it->visitSwitch(PreVisit, this); |
| 223 | |
| 224 | if(visit) |
| 225 | { |
| 226 | it->incrementDepth(this); |
| 227 | if(it->rightToLeft) |
| 228 | { |
| 229 | if(mStatementList) |
| 230 | mStatementList->traverse(it); |
| 231 | if(it->inVisit) |
| 232 | visit = it->visitSwitch(InVisit, this); |
| 233 | if(visit) |
| 234 | mInit->traverse(it); |
| 235 | } |
| 236 | else |
| 237 | { |
| 238 | mInit->traverse(it); |
| 239 | if(it->inVisit) |
| 240 | visit = it->visitSwitch(InVisit, this); |
| 241 | if(visit && mStatementList) |
| 242 | mStatementList->traverse(it); |
| 243 | } |
| 244 | it->decrementDepth(); |
| 245 | } |
| 246 | |
| 247 | if(visit && it->postVisit) |
| 248 | it->visitSwitch(PostVisit, this); |
| 249 | } |
| 250 | |
| 251 | // |
| 252 | // Traverse a switch node. Same comments in binary node apply here. |
| 253 | // |
| 254 | void TIntermCase::traverse(TIntermTraverser *it) |
| 255 | { |
| 256 | bool visit = true; |
| 257 | |
| 258 | if(it->preVisit) |
| 259 | visit = it->visitCase(PreVisit, this); |
| 260 | |
| 261 | if(visit && mCondition) |
| 262 | mCondition->traverse(it); |
| 263 | |
| 264 | if(visit && it->postVisit) |
| 265 | it->visitCase(PostVisit, this); |
| 266 | } |
| 267 | |
| 268 | // |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 269 | // Traverse a loop node. Same comments in binary node apply here. |
| 270 | // |
| 271 | void TIntermLoop::traverse(TIntermTraverser* it) |
| 272 | { |
| 273 | bool visit = true; |
| 274 | |
| 275 | if(it->preVisit) |
| 276 | { |
| 277 | visit = it->visitLoop(PreVisit, this); |
| 278 | } |
| 279 | |
| 280 | if(visit) |
| 281 | { |
Alexis Hetu | 76a343a | 2015-06-04 17:21:22 -0400 | [diff] [blame^] | 282 | it->incrementDepth(this); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 283 | |
| 284 | if(it->rightToLeft) |
| 285 | { |
| 286 | if(expr) |
| 287 | { |
| 288 | expr->traverse(it); |
| 289 | } |
| 290 | |
| 291 | if(body) |
| 292 | { |
| 293 | body->traverse(it); |
| 294 | } |
| 295 | |
| 296 | if(cond) |
| 297 | { |
| 298 | cond->traverse(it); |
| 299 | } |
| 300 | } |
| 301 | else |
| 302 | { |
| 303 | if(cond) |
| 304 | { |
| 305 | cond->traverse(it); |
| 306 | } |
| 307 | |
| 308 | if(body) |
| 309 | { |
| 310 | body->traverse(it); |
| 311 | } |
| 312 | |
| 313 | if(expr) |
| 314 | { |
| 315 | expr->traverse(it); |
| 316 | } |
| 317 | } |
| 318 | |
| 319 | it->decrementDepth(); |
| 320 | } |
| 321 | |
| 322 | if(visit && it->postVisit) |
| 323 | { |
| 324 | it->visitLoop(PostVisit, this); |
| 325 | } |
| 326 | } |
| 327 | |
| 328 | // |
| 329 | // Traverse a branch node. Same comments in binary node apply here. |
| 330 | // |
| 331 | void TIntermBranch::traverse(TIntermTraverser* it) |
| 332 | { |
| 333 | bool visit = true; |
| 334 | |
| 335 | if (it->preVisit) |
| 336 | visit = it->visitBranch(PreVisit, this); |
| 337 | |
| 338 | if (visit && expression) { |
Alexis Hetu | 76a343a | 2015-06-04 17:21:22 -0400 | [diff] [blame^] | 339 | it->incrementDepth(this); |
John Bauman | 8940182 | 2014-05-06 15:04:28 -0400 | [diff] [blame] | 340 | expression->traverse(it); |
| 341 | it->decrementDepth(); |
| 342 | } |
| 343 | |
| 344 | if (visit && it->postVisit) |
| 345 | it->visitBranch(PostVisit, this); |
| 346 | } |
| 347 | |