IntermTraverse.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. //
  2. // Copyright (C) 2002-2005 3Dlabs Inc. Ltd.
  3. // Copyright (C) 2013 LunarG, Inc.
  4. // Copyright (c) 2002-2010 The ANGLE Project Authors.
  5. //
  6. // All rights reserved.
  7. //
  8. // Redistribution and use in source and binary forms, with or without
  9. // modification, are permitted provided that the following conditions
  10. // are met:
  11. //
  12. // Redistributions of source code must retain the above copyright
  13. // notice, this list of conditions and the following disclaimer.
  14. //
  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.
  19. //
  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.
  23. //
  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.
  36. //
  37. #include "../Include/intermediate.h"
  38. namespace glslang {
  39. //
  40. // Traverse the intermediate representation tree, and
  41. // call a node type specific function for each node.
  42. // Done recursively through the member function Traverse().
  43. // Node types can be skipped if their function to call is 0,
  44. // but their subtree will still be traversed.
  45. // Nodes with children can have their whole subtree skipped
  46. // if preVisit is turned on and the type specific function
  47. // returns false.
  48. //
  49. // preVisit, postVisit, and rightToLeft control what order
  50. // nodes are visited in.
  51. //
  52. //
  53. // Traversal functions for terminals are straightforward....
  54. //
  55. void TIntermMethod::traverse(TIntermTraverser*)
  56. {
  57. // Tree should always resolve all methods as a non-method.
  58. }
  59. void TIntermSymbol::traverse(TIntermTraverser *it)
  60. {
  61. it->visitSymbol(this);
  62. }
  63. void TIntermConstantUnion::traverse(TIntermTraverser *it)
  64. {
  65. it->visitConstantUnion(this);
  66. }
  67. const TString& TIntermSymbol::getAccessName() const {
  68. if (getBasicType() == EbtBlock)
  69. return getType().getTypeName();
  70. else
  71. return getName();
  72. }
  73. //
  74. // Traverse a binary node.
  75. //
  76. void TIntermBinary::traverse(TIntermTraverser *it)
  77. {
  78. bool visit = true;
  79. //
  80. // visit the node before children if pre-visiting.
  81. //
  82. if (it->preVisit)
  83. visit = it->visitBinary(EvPreVisit, this);
  84. //
  85. // Visit the children, in the right order.
  86. //
  87. if (visit) {
  88. it->incrementDepth(this);
  89. if (it->rightToLeft) {
  90. if (right)
  91. right->traverse(it);
  92. if (it->inVisit)
  93. visit = it->visitBinary(EvInVisit, this);
  94. if (visit && left)
  95. left->traverse(it);
  96. } else {
  97. if (left)
  98. left->traverse(it);
  99. if (it->inVisit)
  100. visit = it->visitBinary(EvInVisit, this);
  101. if (visit && right)
  102. right->traverse(it);
  103. }
  104. it->decrementDepth();
  105. }
  106. //
  107. // Visit the node after the children, if requested and the traversal
  108. // hasn't been canceled yet.
  109. //
  110. if (visit && it->postVisit)
  111. it->visitBinary(EvPostVisit, this);
  112. }
  113. //
  114. // Traverse a unary node. Same comments in binary node apply here.
  115. //
  116. void TIntermUnary::traverse(TIntermTraverser *it)
  117. {
  118. bool visit = true;
  119. if (it->preVisit)
  120. visit = it->visitUnary(EvPreVisit, this);
  121. if (visit) {
  122. it->incrementDepth(this);
  123. operand->traverse(it);
  124. it->decrementDepth();
  125. }
  126. if (visit && it->postVisit)
  127. it->visitUnary(EvPostVisit, this);
  128. }
  129. //
  130. // Traverse an aggregate node. Same comments in binary node apply here.
  131. //
  132. void TIntermAggregate::traverse(TIntermTraverser *it)
  133. {
  134. bool visit = true;
  135. if (it->preVisit)
  136. visit = it->visitAggregate(EvPreVisit, this);
  137. if (visit) {
  138. it->incrementDepth(this);
  139. if (it->rightToLeft) {
  140. for (TIntermSequence::reverse_iterator sit = sequence.rbegin(); sit != sequence.rend(); sit++) {
  141. (*sit)->traverse(it);
  142. if (visit && it->inVisit) {
  143. if (*sit != sequence.front())
  144. visit = it->visitAggregate(EvInVisit, this);
  145. }
  146. }
  147. } else {
  148. for (TIntermSequence::iterator sit = sequence.begin(); sit != sequence.end(); sit++) {
  149. (*sit)->traverse(it);
  150. if (visit && it->inVisit) {
  151. if (*sit != sequence.back())
  152. visit = it->visitAggregate(EvInVisit, this);
  153. }
  154. }
  155. }
  156. it->decrementDepth();
  157. }
  158. if (visit && it->postVisit)
  159. it->visitAggregate(EvPostVisit, this);
  160. }
  161. //
  162. // Traverse a selection node. Same comments in binary node apply here.
  163. //
  164. void TIntermSelection::traverse(TIntermTraverser *it)
  165. {
  166. bool visit = true;
  167. if (it->preVisit)
  168. visit = it->visitSelection(EvPreVisit, this);
  169. if (visit) {
  170. it->incrementDepth(this);
  171. if (it->rightToLeft) {
  172. if (falseBlock)
  173. falseBlock->traverse(it);
  174. if (trueBlock)
  175. trueBlock->traverse(it);
  176. condition->traverse(it);
  177. } else {
  178. condition->traverse(it);
  179. if (trueBlock)
  180. trueBlock->traverse(it);
  181. if (falseBlock)
  182. falseBlock->traverse(it);
  183. }
  184. it->decrementDepth();
  185. }
  186. if (visit && it->postVisit)
  187. it->visitSelection(EvPostVisit, this);
  188. }
  189. //
  190. // Traverse a loop node. Same comments in binary node apply here.
  191. //
  192. void TIntermLoop::traverse(TIntermTraverser *it)
  193. {
  194. bool visit = true;
  195. if (it->preVisit)
  196. visit = it->visitLoop(EvPreVisit, this);
  197. if (visit) {
  198. it->incrementDepth(this);
  199. if (it->rightToLeft) {
  200. if (terminal)
  201. terminal->traverse(it);
  202. if (body)
  203. body->traverse(it);
  204. if (test)
  205. test->traverse(it);
  206. } else {
  207. if (test)
  208. test->traverse(it);
  209. if (body)
  210. body->traverse(it);
  211. if (terminal)
  212. terminal->traverse(it);
  213. }
  214. it->decrementDepth();
  215. }
  216. if (visit && it->postVisit)
  217. it->visitLoop(EvPostVisit, this);
  218. }
  219. //
  220. // Traverse a branch node. Same comments in binary node apply here.
  221. //
  222. void TIntermBranch::traverse(TIntermTraverser *it)
  223. {
  224. bool visit = true;
  225. if (it->preVisit)
  226. visit = it->visitBranch(EvPreVisit, this);
  227. if (visit && expression) {
  228. it->incrementDepth(this);
  229. expression->traverse(it);
  230. it->decrementDepth();
  231. }
  232. if (visit && it->postVisit)
  233. it->visitBranch(EvPostVisit, this);
  234. }
  235. //
  236. // Traverse a switch node.
  237. //
  238. void TIntermSwitch::traverse(TIntermTraverser* it)
  239. {
  240. bool visit = true;
  241. if (it->preVisit)
  242. visit = it->visitSwitch(EvPreVisit, this);
  243. if (visit) {
  244. it->incrementDepth(this);
  245. if (it->rightToLeft) {
  246. body->traverse(it);
  247. condition->traverse(it);
  248. } else {
  249. condition->traverse(it);
  250. body->traverse(it);
  251. }
  252. it->decrementDepth();
  253. }
  254. if (visit && it->postVisit)
  255. it->visitSwitch(EvPostVisit, this);
  256. }
  257. } // end namespace glslang