blob: 0bacd3dd3814404e4959581dfab81634f744f9cc [file] [log] [blame]
Jamie Madillb1a85f42014-08-19 15:23:24 -04001//
2// Copyright (c) 2002-2014 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
7//
8// Definition of the in-memory high-level intermediate representation
9// of shaders. This is a tree that parser creates.
10//
11// Nodes in the tree are defined as a hierarchy of classes derived from
12// TIntermNode. Each is a node in a tree. There is no preset branching factor;
13// each node can have it's own type of list of children.
14//
15
Geoff Lang0a73dd82014-11-19 16:18:08 -050016#ifndef COMPILER_TRANSLATOR_INTERMNODE_H_
17#define COMPILER_TRANSLATOR_INTERMNODE_H_
Jamie Madillb1a85f42014-08-19 15:23:24 -040018
19#include "GLSLANG/ShaderLang.h"
20
21#include <algorithm>
22#include <queue>
23
Jamie Madill80d934b2015-02-19 10:16:12 -050024#include "common/angleutils.h"
Jamie Madillb1a85f42014-08-19 15:23:24 -040025#include "compiler/translator/Common.h"
26#include "compiler/translator/Types.h"
27#include "compiler/translator/ConstantUnion.h"
Olli Etuaho1033e1d2015-02-12 12:03:13 +020028#include "compiler/translator/Operator.h"
Jamie Madillb1a85f42014-08-19 15:23:24 -040029
30class TIntermTraverser;
31class TIntermAggregate;
32class TIntermBinary;
33class TIntermUnary;
34class TIntermConstantUnion;
35class TIntermSelection;
Olli Etuahoa3a36662015-02-17 13:46:51 +020036class TIntermSwitch;
37class TIntermCase;
Jamie Madillb1a85f42014-08-19 15:23:24 -040038class TIntermTyped;
39class TIntermSymbol;
40class TIntermLoop;
41class TInfoSink;
Olli Etuahoa3a5cc62015-02-13 13:12:22 +020042class TInfoSinkBase;
Jamie Madillb1a85f42014-08-19 15:23:24 -040043class TIntermRaw;
44
45//
46// Base class for the tree nodes
47//
48class TIntermNode
49{
50 public:
51 POOL_ALLOCATOR_NEW_DELETE();
52 TIntermNode()
53 {
54 // TODO: Move this to TSourceLoc constructor
55 // after getting rid of TPublicType.
56 mLine.first_file = mLine.last_file = 0;
57 mLine.first_line = mLine.last_line = 0;
58 }
59 virtual ~TIntermNode() { }
60
61 const TSourceLoc &getLine() const { return mLine; }
62 void setLine(const TSourceLoc &l) { mLine = l; }
63
64 virtual void traverse(TIntermTraverser *) = 0;
65 virtual TIntermTyped *getAsTyped() { return 0; }
66 virtual TIntermConstantUnion *getAsConstantUnion() { return 0; }
67 virtual TIntermAggregate *getAsAggregate() { return 0; }
68 virtual TIntermBinary *getAsBinaryNode() { return 0; }
69 virtual TIntermUnary *getAsUnaryNode() { return 0; }
70 virtual TIntermSelection *getAsSelectionNode() { return 0; }
Olli Etuahoa3a36662015-02-17 13:46:51 +020071 virtual TIntermSwitch *getAsSwitchNode() { return 0; }
72 virtual TIntermCase *getAsCaseNode() { return 0; }
Jamie Madillb1a85f42014-08-19 15:23:24 -040073 virtual TIntermSymbol *getAsSymbolNode() { return 0; }
74 virtual TIntermLoop *getAsLoopNode() { return 0; }
75 virtual TIntermRaw *getAsRawNode() { return 0; }
76
77 // Replace a child node. Return true if |original| is a child
78 // node and it is replaced; otherwise, return false.
79 virtual bool replaceChildNode(
80 TIntermNode *original, TIntermNode *replacement) = 0;
81
Jamie Madillb1a85f42014-08-19 15:23:24 -040082 protected:
83 TSourceLoc mLine;
84};
85
86//
87// This is just to help yacc.
88//
89struct TIntermNodePair
90{
91 TIntermNode *node1;
92 TIntermNode *node2;
93};
94
95//
96// Intermediate class for nodes that have a type.
97//
98class TIntermTyped : public TIntermNode
99{
100 public:
101 TIntermTyped(const TType &t) : mType(t) { }
102 virtual TIntermTyped *getAsTyped() { return this; }
103
104 virtual bool hasSideEffects() const = 0;
105
106 void setType(const TType &t) { mType = t; }
Olli Etuahod2a67b92014-10-21 16:42:57 +0300107 void setTypePreservePrecision(const TType &t);
Jamie Madillb1a85f42014-08-19 15:23:24 -0400108 const TType &getType() const { return mType; }
109 TType *getTypePointer() { return &mType; }
110
111 TBasicType getBasicType() const { return mType.getBasicType(); }
112 TQualifier getQualifier() const { return mType.getQualifier(); }
113 TPrecision getPrecision() const { return mType.getPrecision(); }
114 int getCols() const { return mType.getCols(); }
115 int getRows() const { return mType.getRows(); }
116 int getNominalSize() const { return mType.getNominalSize(); }
117 int getSecondarySize() const { return mType.getSecondarySize(); }
118
119 bool isInterfaceBlock() const { return mType.isInterfaceBlock(); }
120 bool isMatrix() const { return mType.isMatrix(); }
121 bool isArray() const { return mType.isArray(); }
122 bool isVector() const { return mType.isVector(); }
123 bool isScalar() const { return mType.isScalar(); }
124 bool isScalarInt() const { return mType.isScalarInt(); }
125 const char *getBasicString() const { return mType.getBasicString(); }
126 const char *getQualifierString() const { return mType.getQualifierString(); }
127 TString getCompleteString() const { return mType.getCompleteString(); }
128
129 int getArraySize() const { return mType.getArraySize(); }
130
131 protected:
132 TType mType;
133};
134
135//
136// Handle for, do-while, and while loops.
137//
138enum TLoopType
139{
140 ELoopFor,
141 ELoopWhile,
142 ELoopDoWhile
143};
144
145class TIntermLoop : public TIntermNode
146{
147 public:
148 TIntermLoop(TLoopType type,
149 TIntermNode *init, TIntermTyped *cond, TIntermTyped *expr,
150 TIntermNode *body)
151 : mType(type),
152 mInit(init),
153 mCond(cond),
154 mExpr(expr),
155 mBody(body),
156 mUnrollFlag(false) { }
157
158 virtual TIntermLoop *getAsLoopNode() { return this; }
159 virtual void traverse(TIntermTraverser *);
160 virtual bool replaceChildNode(
161 TIntermNode *original, TIntermNode *replacement);
162
163 TLoopType getType() const { return mType; }
164 TIntermNode *getInit() { return mInit; }
165 TIntermTyped *getCondition() { return mCond; }
166 TIntermTyped *getExpression() { return mExpr; }
167 TIntermNode *getBody() { return mBody; }
168
169 void setUnrollFlag(bool flag) { mUnrollFlag = flag; }
170 bool getUnrollFlag() const { return mUnrollFlag; }
171
Jamie Madillb1a85f42014-08-19 15:23:24 -0400172 protected:
173 TLoopType mType;
174 TIntermNode *mInit; // for-loop initialization
175 TIntermTyped *mCond; // loop exit condition
176 TIntermTyped *mExpr; // for-loop expression
177 TIntermNode *mBody; // loop body
178
179 bool mUnrollFlag; // Whether the loop should be unrolled or not.
180};
181
182//
183// Handle break, continue, return, and kill.
184//
185class TIntermBranch : public TIntermNode
186{
187 public:
188 TIntermBranch(TOperator op, TIntermTyped *e)
189 : mFlowOp(op),
190 mExpression(e) { }
191
192 virtual void traverse(TIntermTraverser *);
193 virtual bool replaceChildNode(
194 TIntermNode *original, TIntermNode *replacement);
195
196 TOperator getFlowOp() { return mFlowOp; }
197 TIntermTyped* getExpression() { return mExpression; }
198
Jamie Madillb1a85f42014-08-19 15:23:24 -0400199protected:
200 TOperator mFlowOp;
201 TIntermTyped *mExpression; // non-zero except for "return exp;" statements
202};
203
204//
205// Nodes that correspond to symbols or constants in the source code.
206//
207class TIntermSymbol : public TIntermTyped
208{
209 public:
210 // if symbol is initialized as symbol(sym), the memory comes from the poolallocator of sym.
211 // If sym comes from per process globalpoolallocator, then it causes increased memory usage
212 // per compile it is essential to use "symbol = sym" to assign to symbol
213 TIntermSymbol(int id, const TString &symbol, const TType &type)
214 : TIntermTyped(type),
215 mId(id)
216 {
217 mSymbol = symbol;
218 }
219
220 virtual bool hasSideEffects() const { return false; }
221
222 int getId() const { return mId; }
223 const TString &getSymbol() const { return mSymbol; }
224
225 void setId(int newId) { mId = newId; }
226
227 virtual void traverse(TIntermTraverser *);
228 virtual TIntermSymbol *getAsSymbolNode() { return this; }
229 virtual bool replaceChildNode(TIntermNode *, TIntermNode *) { return false; }
230
Jamie Madillb1a85f42014-08-19 15:23:24 -0400231 protected:
232 int mId;
233 TString mSymbol;
234};
235
236// A Raw node stores raw code, that the translator will insert verbatim
237// into the output stream. Useful for transformation operations that make
238// complex code that might not fit naturally into the GLSL model.
239class TIntermRaw : public TIntermTyped
240{
241 public:
242 TIntermRaw(const TType &type, const TString &rawText)
243 : TIntermTyped(type),
244 mRawText(rawText) { }
245
246 virtual bool hasSideEffects() const { return false; }
247
248 TString getRawText() const { return mRawText; }
249
250 virtual void traverse(TIntermTraverser *);
251
252 virtual TIntermRaw *getAsRawNode() { return this; }
253 virtual bool replaceChildNode(TIntermNode *, TIntermNode *) { return false; }
Jamie Madillb1a85f42014-08-19 15:23:24 -0400254
255 protected:
256 TString mRawText;
257};
258
259class TIntermConstantUnion : public TIntermTyped
260{
261 public:
262 TIntermConstantUnion(ConstantUnion *unionPointer, const TType &type)
263 : TIntermTyped(type),
264 mUnionArrayPointer(unionPointer) { }
265
266 virtual bool hasSideEffects() const { return false; }
267
268 ConstantUnion *getUnionArrayPointer() const { return mUnionArrayPointer; }
269
270 int getIConst(size_t index) const
271 {
272 return mUnionArrayPointer ? mUnionArrayPointer[index].getIConst() : 0;
273 }
274 unsigned int getUConst(size_t index) const
275 {
276 return mUnionArrayPointer ? mUnionArrayPointer[index].getUConst() : 0;
277 }
278 float getFConst(size_t index) const
279 {
280 return mUnionArrayPointer ? mUnionArrayPointer[index].getFConst() : 0.0f;
281 }
282 bool getBConst(size_t index) const
283 {
284 return mUnionArrayPointer ? mUnionArrayPointer[index].getBConst() : false;
285 }
286
287 virtual TIntermConstantUnion *getAsConstantUnion() { return this; }
288 virtual void traverse(TIntermTraverser *);
289 virtual bool replaceChildNode(TIntermNode *, TIntermNode *) { return false; }
290
291 TIntermTyped *fold(TOperator, TIntermTyped *, TInfoSink &);
292
Jamie Madillb1a85f42014-08-19 15:23:24 -0400293 protected:
294 ConstantUnion *mUnionArrayPointer;
Arun Patole9dea48f2015-04-02 11:45:09 +0530295
296 private:
297 typedef float(*FloatTypeUnaryFunc) (float);
298 bool foldFloatTypeUnary(const ConstantUnion &parameter, FloatTypeUnaryFunc builtinFunc, TInfoSink &infoSink, ConstantUnion *result) const;
Jamie Madillb1a85f42014-08-19 15:23:24 -0400299};
300
301//
302// Intermediate class for node types that hold operators.
303//
304class TIntermOperator : public TIntermTyped
305{
306 public:
307 TOperator getOp() const { return mOp; }
308 void setOp(TOperator op) { mOp = op; }
309
310 bool isAssignment() const;
311 bool isConstructor() const;
312
313 virtual bool hasSideEffects() const { return isAssignment(); }
314
315 protected:
316 TIntermOperator(TOperator op)
317 : TIntermTyped(TType(EbtFloat, EbpUndefined)),
318 mOp(op) {}
319 TIntermOperator(TOperator op, const TType &type)
320 : TIntermTyped(type),
321 mOp(op) {}
322
323 TOperator mOp;
324};
325
326//
327// Nodes for all the basic binary math operators.
328//
329class TIntermBinary : public TIntermOperator
330{
331 public:
332 TIntermBinary(TOperator op)
333 : TIntermOperator(op),
334 mAddIndexClamp(false) {}
335
336 virtual TIntermBinary *getAsBinaryNode() { return this; }
337 virtual void traverse(TIntermTraverser *);
338 virtual bool replaceChildNode(
339 TIntermNode *original, TIntermNode *replacement);
340
341 virtual bool hasSideEffects() const
342 {
343 return isAssignment() || mLeft->hasSideEffects() || mRight->hasSideEffects();
344 }
345
346 void setLeft(TIntermTyped *node) { mLeft = node; }
347 void setRight(TIntermTyped *node) { mRight = node; }
348 TIntermTyped *getLeft() const { return mLeft; }
349 TIntermTyped *getRight() const { return mRight; }
350 bool promote(TInfoSink &);
351
352 void setAddIndexClamp() { mAddIndexClamp = true; }
353 bool getAddIndexClamp() { return mAddIndexClamp; }
354
Jamie Madillb1a85f42014-08-19 15:23:24 -0400355 protected:
356 TIntermTyped* mLeft;
357 TIntermTyped* mRight;
358
359 // If set to true, wrap any EOpIndexIndirect with a clamp to bounds.
360 bool mAddIndexClamp;
361};
362
363//
364// Nodes for unary math operators.
365//
366class TIntermUnary : public TIntermOperator
367{
368 public:
369 TIntermUnary(TOperator op, const TType &type)
370 : TIntermOperator(op, type),
371 mOperand(NULL),
372 mUseEmulatedFunction(false) {}
373 TIntermUnary(TOperator op)
374 : TIntermOperator(op),
375 mOperand(NULL),
376 mUseEmulatedFunction(false) {}
377
378 virtual void traverse(TIntermTraverser *);
379 virtual TIntermUnary *getAsUnaryNode() { return this; }
380 virtual bool replaceChildNode(
381 TIntermNode *original, TIntermNode *replacement);
382
383 virtual bool hasSideEffects() const
384 {
385 return isAssignment() || mOperand->hasSideEffects();
386 }
387
388 void setOperand(TIntermTyped *operand) { mOperand = operand; }
389 TIntermTyped *getOperand() { return mOperand; }
Olli Etuahof6c694b2015-03-26 14:50:53 +0200390 void promote(const TType *funcReturnType);
Jamie Madillb1a85f42014-08-19 15:23:24 -0400391
392 void setUseEmulatedFunction() { mUseEmulatedFunction = true; }
393 bool getUseEmulatedFunction() { return mUseEmulatedFunction; }
394
Jamie Madillb1a85f42014-08-19 15:23:24 -0400395 protected:
396 TIntermTyped *mOperand;
397
398 // If set to true, replace the built-in function call with an emulated one
399 // to work around driver bugs.
400 bool mUseEmulatedFunction;
401};
402
403typedef TVector<TIntermNode *> TIntermSequence;
404typedef TVector<int> TQualifierList;
405
406//
407// Nodes that operate on an arbitrary sized set of children.
408//
409class TIntermAggregate : public TIntermOperator
410{
411 public:
412 TIntermAggregate()
413 : TIntermOperator(EOpNull),
414 mUserDefined(false),
415 mUseEmulatedFunction(false) { }
416 TIntermAggregate(TOperator op)
417 : TIntermOperator(op),
418 mUseEmulatedFunction(false) { }
419 ~TIntermAggregate() { }
420
421 virtual TIntermAggregate *getAsAggregate() { return this; }
422 virtual void traverse(TIntermTraverser *);
423 virtual bool replaceChildNode(
424 TIntermNode *original, TIntermNode *replacement);
Olli Etuahofc0e2bc2015-04-16 13:39:56 +0300425 bool replaceChildNodeWithMultiple(TIntermNode *original, TIntermSequence replacements);
Jamie Madillb1a85f42014-08-19 15:23:24 -0400426 // Conservatively assume function calls and other aggregate operators have side-effects
427 virtual bool hasSideEffects() const { return true; }
428
429 TIntermSequence *getSequence() { return &mSequence; }
430
431 void setName(const TString &name) { mName = name; }
432 const TString &getName() const { return mName; }
433
434 void setUserDefined() { mUserDefined = true; }
435 bool isUserDefined() const { return mUserDefined; }
436
437 void setOptimize(bool optimize) { mOptimize = optimize; }
438 bool getOptimize() const { return mOptimize; }
439 void setDebug(bool debug) { mDebug = debug; }
440 bool getDebug() const { return mDebug; }
441
Corentin Wallez71d147f2015-02-11 11:15:24 -0800442 void setFunctionId(int functionId) { mFunctionId = functionId; }
443 int getFunctionId() const { return mFunctionId; }
444
Jamie Madillb1a85f42014-08-19 15:23:24 -0400445 void setUseEmulatedFunction() { mUseEmulatedFunction = true; }
446 bool getUseEmulatedFunction() { return mUseEmulatedFunction; }
447
Olli Etuahod2a67b92014-10-21 16:42:57 +0300448 void setPrecisionFromChildren();
449 void setBuiltInFunctionPrecision();
450
Jamie Madillb1a85f42014-08-19 15:23:24 -0400451 protected:
452 TIntermAggregate(const TIntermAggregate &); // disallow copy constructor
453 TIntermAggregate &operator=(const TIntermAggregate &); // disallow assignment operator
454 TIntermSequence mSequence;
455 TString mName;
456 bool mUserDefined; // used for user defined function names
Corentin Wallez71d147f2015-02-11 11:15:24 -0800457 int mFunctionId;
Jamie Madillb1a85f42014-08-19 15:23:24 -0400458
459 bool mOptimize;
460 bool mDebug;
461
462 // If set to true, replace the built-in function call with an emulated one
463 // to work around driver bugs.
464 bool mUseEmulatedFunction;
465};
466
467//
Olli Etuahoa3a36662015-02-17 13:46:51 +0200468// For if tests.
Jamie Madillb1a85f42014-08-19 15:23:24 -0400469//
470class TIntermSelection : public TIntermTyped
471{
472 public:
473 TIntermSelection(TIntermTyped *cond, TIntermNode *trueB, TIntermNode *falseB)
474 : TIntermTyped(TType(EbtVoid, EbpUndefined)),
475 mCondition(cond),
476 mTrueBlock(trueB),
477 mFalseBlock(falseB) {}
478 TIntermSelection(TIntermTyped *cond, TIntermNode *trueB, TIntermNode *falseB,
479 const TType &type)
480 : TIntermTyped(type),
481 mCondition(cond),
482 mTrueBlock(trueB),
483 mFalseBlock(falseB) {}
484
485 virtual void traverse(TIntermTraverser *);
486 virtual bool replaceChildNode(
487 TIntermNode *original, TIntermNode *replacement);
488
489 // Conservatively assume selections have side-effects
490 virtual bool hasSideEffects() const { return true; }
491
492 bool usesTernaryOperator() const { return getBasicType() != EbtVoid; }
493 TIntermNode *getCondition() const { return mCondition; }
494 TIntermNode *getTrueBlock() const { return mTrueBlock; }
495 TIntermNode *getFalseBlock() const { return mFalseBlock; }
496 TIntermSelection *getAsSelectionNode() { return this; }
497
Jamie Madillb1a85f42014-08-19 15:23:24 -0400498protected:
499 TIntermTyped *mCondition;
500 TIntermNode *mTrueBlock;
501 TIntermNode *mFalseBlock;
502};
503
Olli Etuahoa3a36662015-02-17 13:46:51 +0200504//
505// Switch statement.
506//
507class TIntermSwitch : public TIntermNode
508{
509 public:
510 TIntermSwitch(TIntermTyped *init, TIntermAggregate *statementList)
511 : TIntermNode(),
512 mInit(init),
513 mStatementList(statementList)
514 {
515 }
516
517 void traverse(TIntermTraverser *it) override;
518 bool replaceChildNode(
519 TIntermNode *original, TIntermNode *replacement) override;
520
521 TIntermSwitch *getAsSwitchNode() override { return this; }
522
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200523 TIntermAggregate *getStatementList() { return mStatementList; }
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +0200524 void setStatementList(TIntermAggregate *statementList) { mStatementList = statementList; }
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200525
Olli Etuahoa3a36662015-02-17 13:46:51 +0200526 protected:
527 TIntermTyped *mInit;
528 TIntermAggregate *mStatementList;
529};
530
531//
532// Case label.
533//
534class TIntermCase : public TIntermNode
535{
536 public:
537 TIntermCase(TIntermTyped *condition)
538 : TIntermNode(),
539 mCondition(condition)
540 {
541 }
542
543 void traverse(TIntermTraverser *it) override;
544 bool replaceChildNode(
545 TIntermNode *original, TIntermNode *replacement) override;
546
547 TIntermCase *getAsCaseNode() override { return this; }
548
549 bool hasCondition() const { return mCondition != nullptr; }
550 TIntermTyped *getCondition() const { return mCondition; }
551
552 protected:
553 TIntermTyped *mCondition;
554};
555
Jamie Madillb1a85f42014-08-19 15:23:24 -0400556enum Visit
557{
558 PreVisit,
559 InVisit,
560 PostVisit
561};
562
563//
564// For traversing the tree. User should derive from this,
565// put their traversal specific data in it, and then pass
566// it to a Traverse method.
567//
568// When using this, just fill in the methods for nodes you want visited.
569// Return false from a pre-visit to skip visiting that node's subtree.
570//
Jamie Madillf0d10f82015-03-31 12:56:52 -0400571class TIntermTraverser : angle::NonCopyable
Jamie Madillb1a85f42014-08-19 15:23:24 -0400572{
573 public:
574 POOL_ALLOCATOR_NEW_DELETE();
575 // TODO(zmo): remove default values.
576 TIntermTraverser(bool preVisit = true, bool inVisit = false, bool postVisit = false,
577 bool rightToLeft = false)
578 : preVisit(preVisit),
579 inVisit(inVisit),
580 postVisit(postVisit),
581 rightToLeft(rightToLeft),
582 mDepth(0),
583 mMaxDepth(0) {}
584 virtual ~TIntermTraverser() {}
585
586 virtual void visitSymbol(TIntermSymbol *) {}
587 virtual void visitRaw(TIntermRaw *) {}
588 virtual void visitConstantUnion(TIntermConstantUnion *) {}
589 virtual bool visitBinary(Visit, TIntermBinary *) { return true; }
590 virtual bool visitUnary(Visit, TIntermUnary *) { return true; }
591 virtual bool visitSelection(Visit, TIntermSelection *) { return true; }
Olli Etuahoa3a36662015-02-17 13:46:51 +0200592 virtual bool visitSwitch(Visit, TIntermSwitch *) { return true; }
593 virtual bool visitCase(Visit, TIntermCase *) { return true; }
Jamie Madillb1a85f42014-08-19 15:23:24 -0400594 virtual bool visitAggregate(Visit, TIntermAggregate *) { return true; }
595 virtual bool visitLoop(Visit, TIntermLoop *) { return true; }
596 virtual bool visitBranch(Visit, TIntermBranch *) { return true; }
597
598 int getMaxDepth() const { return mMaxDepth; }
599
600 void incrementDepth(TIntermNode *current)
601 {
602 mDepth++;
603 mMaxDepth = std::max(mMaxDepth, mDepth);
604 mPath.push_back(current);
605 }
606
607 void decrementDepth()
608 {
609 mDepth--;
610 mPath.pop_back();
611 }
612
613 TIntermNode *getParentNode()
614 {
615 return mPath.size() == 0 ? NULL : mPath.back();
616 }
617
618 // Return the original name if hash function pointer is NULL;
619 // otherwise return the hashed name.
620 static TString hash(const TString& name, ShHashFunction64 hashFunction);
621
622 const bool preVisit;
623 const bool inVisit;
624 const bool postVisit;
625 const bool rightToLeft;
626
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200627 // If traversers need to replace nodes, they can add the replacements in
Olli Etuahofc0e2bc2015-04-16 13:39:56 +0300628 // mReplacements/mMultiReplacements during traversal and the user of the traverser should call
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200629 // this function after traversal to perform them.
630 void updateTree();
631
Jamie Madillb1a85f42014-08-19 15:23:24 -0400632 protected:
633 int mDepth;
634 int mMaxDepth;
635
636 // All the nodes from root to the current node's parent during traversing.
637 TVector<TIntermNode *> mPath;
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200638
Olli Etuahofc0e2bc2015-04-16 13:39:56 +0300639 // To replace a single node with another on the parent node
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200640 struct NodeUpdateEntry
641 {
642 NodeUpdateEntry(TIntermNode *_parent,
643 TIntermNode *_original,
644 TIntermNode *_replacement,
645 bool _originalBecomesChildOfReplacement)
646 : parent(_parent),
647 original(_original),
648 replacement(_replacement),
649 originalBecomesChildOfReplacement(_originalBecomesChildOfReplacement) {}
650
651 TIntermNode *parent;
652 TIntermNode *original;
653 TIntermNode *replacement;
654 bool originalBecomesChildOfReplacement;
655 };
656
Olli Etuahofc0e2bc2015-04-16 13:39:56 +0300657 // To replace a single node with multiple nodes on the parent aggregate node
658 struct NodeReplaceWithMultipleEntry
659 {
660 NodeReplaceWithMultipleEntry(TIntermAggregate *_parent, TIntermNode *_original, TIntermSequence _replacements)
661 : parent(_parent),
662 original(_original),
663 replacements(_replacements)
664 {
665 }
666
667 TIntermAggregate *parent;
668 TIntermNode *original;
669 TIntermSequence replacements;
670 };
671
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200672 // During traversing, save all the changes that need to happen into
Olli Etuahofc0e2bc2015-04-16 13:39:56 +0300673 // mReplacements/mMultiReplacements, then do them by calling updateTree().
674 // Multi replacements are processed after single replacements.
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200675 std::vector<NodeUpdateEntry> mReplacements;
Olli Etuahofc0e2bc2015-04-16 13:39:56 +0300676 std::vector<NodeReplaceWithMultipleEntry> mMultiReplacements;
Jamie Madillb1a85f42014-08-19 15:23:24 -0400677};
678
679//
680// For traversing the tree, and computing max depth.
681// Takes a maximum depth limit to prevent stack overflow.
682//
683class TMaxDepthTraverser : public TIntermTraverser
684{
685 public:
686 POOL_ALLOCATOR_NEW_DELETE();
687 TMaxDepthTraverser(int depthLimit)
688 : TIntermTraverser(true, true, false, false),
689 mDepthLimit(depthLimit) { }
690
691 virtual bool visitBinary(Visit, TIntermBinary *) { return depthCheck(); }
692 virtual bool visitUnary(Visit, TIntermUnary *) { return depthCheck(); }
693 virtual bool visitSelection(Visit, TIntermSelection *) { return depthCheck(); }
694 virtual bool visitAggregate(Visit, TIntermAggregate *) { return depthCheck(); }
695 virtual bool visitLoop(Visit, TIntermLoop *) { return depthCheck(); }
696 virtual bool visitBranch(Visit, TIntermBranch *) { return depthCheck(); }
697
Jamie Madill80d934b2015-02-19 10:16:12 -0500698 protected:
Jamie Madillb1a85f42014-08-19 15:23:24 -0400699 bool depthCheck() const { return mMaxDepth < mDepthLimit; }
700
701 int mDepthLimit;
702};
703
Geoff Lang0a73dd82014-11-19 16:18:08 -0500704#endif // COMPILER_TRANSLATOR_INTERMNODE_H_