blob: 81231c054875b345f76fefd94fd5bec4d84dd4a1 [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),
Olli Etuaho78174db2015-04-21 16:14:00 +0300215 mId(id),
216 mInternal(false)
Jamie Madillb1a85f42014-08-19 15:23:24 -0400217 {
218 mSymbol = symbol;
219 }
220
221 virtual bool hasSideEffects() const { return false; }
222
223 int getId() const { return mId; }
224 const TString &getSymbol() const { return mSymbol; }
225
226 void setId(int newId) { mId = newId; }
227
Olli Etuaho78174db2015-04-21 16:14:00 +0300228 bool isInternal() const { return mInternal; }
229 void setInternal(bool isInternal) { mInternal = isInternal; }
230
Jamie Madillb1a85f42014-08-19 15:23:24 -0400231 virtual void traverse(TIntermTraverser *);
232 virtual TIntermSymbol *getAsSymbolNode() { return this; }
233 virtual bool replaceChildNode(TIntermNode *, TIntermNode *) { return false; }
234
Jamie Madillb1a85f42014-08-19 15:23:24 -0400235 protected:
236 int mId;
Olli Etuaho78174db2015-04-21 16:14:00 +0300237 bool mInternal;
Jamie Madillb1a85f42014-08-19 15:23:24 -0400238 TString mSymbol;
239};
240
241// A Raw node stores raw code, that the translator will insert verbatim
242// into the output stream. Useful for transformation operations that make
243// complex code that might not fit naturally into the GLSL model.
244class TIntermRaw : public TIntermTyped
245{
246 public:
247 TIntermRaw(const TType &type, const TString &rawText)
248 : TIntermTyped(type),
249 mRawText(rawText) { }
250
251 virtual bool hasSideEffects() const { return false; }
252
253 TString getRawText() const { return mRawText; }
254
255 virtual void traverse(TIntermTraverser *);
256
257 virtual TIntermRaw *getAsRawNode() { return this; }
258 virtual bool replaceChildNode(TIntermNode *, TIntermNode *) { return false; }
Jamie Madillb1a85f42014-08-19 15:23:24 -0400259
260 protected:
261 TString mRawText;
262};
263
264class TIntermConstantUnion : public TIntermTyped
265{
266 public:
267 TIntermConstantUnion(ConstantUnion *unionPointer, const TType &type)
268 : TIntermTyped(type),
269 mUnionArrayPointer(unionPointer) { }
270
271 virtual bool hasSideEffects() const { return false; }
272
273 ConstantUnion *getUnionArrayPointer() const { return mUnionArrayPointer; }
274
275 int getIConst(size_t index) const
276 {
277 return mUnionArrayPointer ? mUnionArrayPointer[index].getIConst() : 0;
278 }
279 unsigned int getUConst(size_t index) const
280 {
281 return mUnionArrayPointer ? mUnionArrayPointer[index].getUConst() : 0;
282 }
283 float getFConst(size_t index) const
284 {
285 return mUnionArrayPointer ? mUnionArrayPointer[index].getFConst() : 0.0f;
286 }
287 bool getBConst(size_t index) const
288 {
289 return mUnionArrayPointer ? mUnionArrayPointer[index].getBConst() : false;
290 }
291
292 virtual TIntermConstantUnion *getAsConstantUnion() { return this; }
293 virtual void traverse(TIntermTraverser *);
294 virtual bool replaceChildNode(TIntermNode *, TIntermNode *) { return false; }
295
296 TIntermTyped *fold(TOperator, TIntermTyped *, TInfoSink &);
297
Jamie Madillb1a85f42014-08-19 15:23:24 -0400298 protected:
299 ConstantUnion *mUnionArrayPointer;
Arun Patole9dea48f2015-04-02 11:45:09 +0530300
301 private:
302 typedef float(*FloatTypeUnaryFunc) (float);
303 bool foldFloatTypeUnary(const ConstantUnion &parameter, FloatTypeUnaryFunc builtinFunc, TInfoSink &infoSink, ConstantUnion *result) const;
Jamie Madillb1a85f42014-08-19 15:23:24 -0400304};
305
306//
307// Intermediate class for node types that hold operators.
308//
309class TIntermOperator : public TIntermTyped
310{
311 public:
312 TOperator getOp() const { return mOp; }
313 void setOp(TOperator op) { mOp = op; }
314
315 bool isAssignment() const;
316 bool isConstructor() const;
317
318 virtual bool hasSideEffects() const { return isAssignment(); }
319
320 protected:
321 TIntermOperator(TOperator op)
322 : TIntermTyped(TType(EbtFloat, EbpUndefined)),
323 mOp(op) {}
324 TIntermOperator(TOperator op, const TType &type)
325 : TIntermTyped(type),
326 mOp(op) {}
327
328 TOperator mOp;
329};
330
331//
332// Nodes for all the basic binary math operators.
333//
334class TIntermBinary : public TIntermOperator
335{
336 public:
337 TIntermBinary(TOperator op)
338 : TIntermOperator(op),
339 mAddIndexClamp(false) {}
340
341 virtual TIntermBinary *getAsBinaryNode() { return this; }
342 virtual void traverse(TIntermTraverser *);
343 virtual bool replaceChildNode(
344 TIntermNode *original, TIntermNode *replacement);
345
346 virtual bool hasSideEffects() const
347 {
348 return isAssignment() || mLeft->hasSideEffects() || mRight->hasSideEffects();
349 }
350
351 void setLeft(TIntermTyped *node) { mLeft = node; }
352 void setRight(TIntermTyped *node) { mRight = node; }
353 TIntermTyped *getLeft() const { return mLeft; }
354 TIntermTyped *getRight() const { return mRight; }
355 bool promote(TInfoSink &);
356
357 void setAddIndexClamp() { mAddIndexClamp = true; }
358 bool getAddIndexClamp() { return mAddIndexClamp; }
359
Jamie Madillb1a85f42014-08-19 15:23:24 -0400360 protected:
361 TIntermTyped* mLeft;
362 TIntermTyped* mRight;
363
364 // If set to true, wrap any EOpIndexIndirect with a clamp to bounds.
365 bool mAddIndexClamp;
366};
367
368//
369// Nodes for unary math operators.
370//
371class TIntermUnary : public TIntermOperator
372{
373 public:
374 TIntermUnary(TOperator op, const TType &type)
375 : TIntermOperator(op, type),
376 mOperand(NULL),
377 mUseEmulatedFunction(false) {}
378 TIntermUnary(TOperator op)
379 : TIntermOperator(op),
380 mOperand(NULL),
381 mUseEmulatedFunction(false) {}
382
383 virtual void traverse(TIntermTraverser *);
384 virtual TIntermUnary *getAsUnaryNode() { return this; }
385 virtual bool replaceChildNode(
386 TIntermNode *original, TIntermNode *replacement);
387
388 virtual bool hasSideEffects() const
389 {
390 return isAssignment() || mOperand->hasSideEffects();
391 }
392
393 void setOperand(TIntermTyped *operand) { mOperand = operand; }
394 TIntermTyped *getOperand() { return mOperand; }
Olli Etuahof6c694b2015-03-26 14:50:53 +0200395 void promote(const TType *funcReturnType);
Jamie Madillb1a85f42014-08-19 15:23:24 -0400396
397 void setUseEmulatedFunction() { mUseEmulatedFunction = true; }
398 bool getUseEmulatedFunction() { return mUseEmulatedFunction; }
399
Jamie Madillb1a85f42014-08-19 15:23:24 -0400400 protected:
401 TIntermTyped *mOperand;
402
403 // If set to true, replace the built-in function call with an emulated one
404 // to work around driver bugs.
405 bool mUseEmulatedFunction;
406};
407
408typedef TVector<TIntermNode *> TIntermSequence;
409typedef TVector<int> TQualifierList;
410
411//
412// Nodes that operate on an arbitrary sized set of children.
413//
414class TIntermAggregate : public TIntermOperator
415{
416 public:
417 TIntermAggregate()
418 : TIntermOperator(EOpNull),
419 mUserDefined(false),
420 mUseEmulatedFunction(false) { }
421 TIntermAggregate(TOperator op)
422 : TIntermOperator(op),
423 mUseEmulatedFunction(false) { }
424 ~TIntermAggregate() { }
425
426 virtual TIntermAggregate *getAsAggregate() { return this; }
427 virtual void traverse(TIntermTraverser *);
428 virtual bool replaceChildNode(
429 TIntermNode *original, TIntermNode *replacement);
Olli Etuahofc0e2bc2015-04-16 13:39:56 +0300430 bool replaceChildNodeWithMultiple(TIntermNode *original, TIntermSequence replacements);
Jamie Madillb1a85f42014-08-19 15:23:24 -0400431 // Conservatively assume function calls and other aggregate operators have side-effects
432 virtual bool hasSideEffects() const { return true; }
433
434 TIntermSequence *getSequence() { return &mSequence; }
435
436 void setName(const TString &name) { mName = name; }
437 const TString &getName() const { return mName; }
438
439 void setUserDefined() { mUserDefined = true; }
440 bool isUserDefined() const { return mUserDefined; }
441
442 void setOptimize(bool optimize) { mOptimize = optimize; }
443 bool getOptimize() const { return mOptimize; }
444 void setDebug(bool debug) { mDebug = debug; }
445 bool getDebug() const { return mDebug; }
446
Corentin Wallez71d147f2015-02-11 11:15:24 -0800447 void setFunctionId(int functionId) { mFunctionId = functionId; }
448 int getFunctionId() const { return mFunctionId; }
449
Jamie Madillb1a85f42014-08-19 15:23:24 -0400450 void setUseEmulatedFunction() { mUseEmulatedFunction = true; }
451 bool getUseEmulatedFunction() { return mUseEmulatedFunction; }
452
Olli Etuahod2a67b92014-10-21 16:42:57 +0300453 void setPrecisionFromChildren();
454 void setBuiltInFunctionPrecision();
455
Jamie Madillb1a85f42014-08-19 15:23:24 -0400456 protected:
457 TIntermAggregate(const TIntermAggregate &); // disallow copy constructor
458 TIntermAggregate &operator=(const TIntermAggregate &); // disallow assignment operator
459 TIntermSequence mSequence;
460 TString mName;
461 bool mUserDefined; // used for user defined function names
Corentin Wallez71d147f2015-02-11 11:15:24 -0800462 int mFunctionId;
Jamie Madillb1a85f42014-08-19 15:23:24 -0400463
464 bool mOptimize;
465 bool mDebug;
466
467 // If set to true, replace the built-in function call with an emulated one
468 // to work around driver bugs.
469 bool mUseEmulatedFunction;
470};
471
472//
Olli Etuahoa3a36662015-02-17 13:46:51 +0200473// For if tests.
Jamie Madillb1a85f42014-08-19 15:23:24 -0400474//
475class TIntermSelection : public TIntermTyped
476{
477 public:
478 TIntermSelection(TIntermTyped *cond, TIntermNode *trueB, TIntermNode *falseB)
479 : TIntermTyped(TType(EbtVoid, EbpUndefined)),
480 mCondition(cond),
481 mTrueBlock(trueB),
482 mFalseBlock(falseB) {}
483 TIntermSelection(TIntermTyped *cond, TIntermNode *trueB, TIntermNode *falseB,
484 const TType &type)
485 : TIntermTyped(type),
486 mCondition(cond),
487 mTrueBlock(trueB),
488 mFalseBlock(falseB) {}
489
490 virtual void traverse(TIntermTraverser *);
491 virtual bool replaceChildNode(
492 TIntermNode *original, TIntermNode *replacement);
493
494 // Conservatively assume selections have side-effects
495 virtual bool hasSideEffects() const { return true; }
496
497 bool usesTernaryOperator() const { return getBasicType() != EbtVoid; }
498 TIntermNode *getCondition() const { return mCondition; }
499 TIntermNode *getTrueBlock() const { return mTrueBlock; }
500 TIntermNode *getFalseBlock() const { return mFalseBlock; }
501 TIntermSelection *getAsSelectionNode() { return this; }
502
Jamie Madillb1a85f42014-08-19 15:23:24 -0400503protected:
504 TIntermTyped *mCondition;
505 TIntermNode *mTrueBlock;
506 TIntermNode *mFalseBlock;
507};
508
Olli Etuahoa3a36662015-02-17 13:46:51 +0200509//
510// Switch statement.
511//
512class TIntermSwitch : public TIntermNode
513{
514 public:
515 TIntermSwitch(TIntermTyped *init, TIntermAggregate *statementList)
516 : TIntermNode(),
517 mInit(init),
518 mStatementList(statementList)
519 {
520 }
521
522 void traverse(TIntermTraverser *it) override;
523 bool replaceChildNode(
524 TIntermNode *original, TIntermNode *replacement) override;
525
526 TIntermSwitch *getAsSwitchNode() override { return this; }
527
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200528 TIntermAggregate *getStatementList() { return mStatementList; }
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +0200529 void setStatementList(TIntermAggregate *statementList) { mStatementList = statementList; }
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200530
Olli Etuahoa3a36662015-02-17 13:46:51 +0200531 protected:
532 TIntermTyped *mInit;
533 TIntermAggregate *mStatementList;
534};
535
536//
537// Case label.
538//
539class TIntermCase : public TIntermNode
540{
541 public:
542 TIntermCase(TIntermTyped *condition)
543 : TIntermNode(),
544 mCondition(condition)
545 {
546 }
547
548 void traverse(TIntermTraverser *it) override;
549 bool replaceChildNode(
550 TIntermNode *original, TIntermNode *replacement) override;
551
552 TIntermCase *getAsCaseNode() override { return this; }
553
554 bool hasCondition() const { return mCondition != nullptr; }
555 TIntermTyped *getCondition() const { return mCondition; }
556
557 protected:
558 TIntermTyped *mCondition;
559};
560
Jamie Madillb1a85f42014-08-19 15:23:24 -0400561enum Visit
562{
563 PreVisit,
564 InVisit,
565 PostVisit
566};
567
568//
569// For traversing the tree. User should derive from this,
570// put their traversal specific data in it, and then pass
571// it to a Traverse method.
572//
573// When using this, just fill in the methods for nodes you want visited.
574// Return false from a pre-visit to skip visiting that node's subtree.
575//
Jamie Madillf0d10f82015-03-31 12:56:52 -0400576class TIntermTraverser : angle::NonCopyable
Jamie Madillb1a85f42014-08-19 15:23:24 -0400577{
578 public:
579 POOL_ALLOCATOR_NEW_DELETE();
580 // TODO(zmo): remove default values.
581 TIntermTraverser(bool preVisit = true, bool inVisit = false, bool postVisit = false,
582 bool rightToLeft = false)
583 : preVisit(preVisit),
584 inVisit(inVisit),
585 postVisit(postVisit),
586 rightToLeft(rightToLeft),
587 mDepth(0),
588 mMaxDepth(0) {}
589 virtual ~TIntermTraverser() {}
590
591 virtual void visitSymbol(TIntermSymbol *) {}
592 virtual void visitRaw(TIntermRaw *) {}
593 virtual void visitConstantUnion(TIntermConstantUnion *) {}
594 virtual bool visitBinary(Visit, TIntermBinary *) { return true; }
595 virtual bool visitUnary(Visit, TIntermUnary *) { return true; }
596 virtual bool visitSelection(Visit, TIntermSelection *) { return true; }
Olli Etuahoa3a36662015-02-17 13:46:51 +0200597 virtual bool visitSwitch(Visit, TIntermSwitch *) { return true; }
598 virtual bool visitCase(Visit, TIntermCase *) { return true; }
Jamie Madillb1a85f42014-08-19 15:23:24 -0400599 virtual bool visitAggregate(Visit, TIntermAggregate *) { return true; }
600 virtual bool visitLoop(Visit, TIntermLoop *) { return true; }
601 virtual bool visitBranch(Visit, TIntermBranch *) { return true; }
602
603 int getMaxDepth() const { return mMaxDepth; }
604
605 void incrementDepth(TIntermNode *current)
606 {
607 mDepth++;
608 mMaxDepth = std::max(mMaxDepth, mDepth);
609 mPath.push_back(current);
610 }
611
612 void decrementDepth()
613 {
614 mDepth--;
615 mPath.pop_back();
616 }
617
618 TIntermNode *getParentNode()
619 {
620 return mPath.size() == 0 ? NULL : mPath.back();
621 }
622
623 // Return the original name if hash function pointer is NULL;
624 // otherwise return the hashed name.
625 static TString hash(const TString& name, ShHashFunction64 hashFunction);
626
627 const bool preVisit;
628 const bool inVisit;
629 const bool postVisit;
630 const bool rightToLeft;
631
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200632 // If traversers need to replace nodes, they can add the replacements in
Olli Etuahofc0e2bc2015-04-16 13:39:56 +0300633 // mReplacements/mMultiReplacements during traversal and the user of the traverser should call
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200634 // this function after traversal to perform them.
635 void updateTree();
636
Jamie Madillb1a85f42014-08-19 15:23:24 -0400637 protected:
638 int mDepth;
639 int mMaxDepth;
640
641 // All the nodes from root to the current node's parent during traversing.
642 TVector<TIntermNode *> mPath;
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200643
Olli Etuahofc0e2bc2015-04-16 13:39:56 +0300644 // To replace a single node with another on the parent node
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200645 struct NodeUpdateEntry
646 {
647 NodeUpdateEntry(TIntermNode *_parent,
648 TIntermNode *_original,
649 TIntermNode *_replacement,
650 bool _originalBecomesChildOfReplacement)
651 : parent(_parent),
652 original(_original),
653 replacement(_replacement),
654 originalBecomesChildOfReplacement(_originalBecomesChildOfReplacement) {}
655
656 TIntermNode *parent;
657 TIntermNode *original;
658 TIntermNode *replacement;
659 bool originalBecomesChildOfReplacement;
660 };
661
Olli Etuahofc0e2bc2015-04-16 13:39:56 +0300662 // To replace a single node with multiple nodes on the parent aggregate node
663 struct NodeReplaceWithMultipleEntry
664 {
665 NodeReplaceWithMultipleEntry(TIntermAggregate *_parent, TIntermNode *_original, TIntermSequence _replacements)
666 : parent(_parent),
667 original(_original),
668 replacements(_replacements)
669 {
670 }
671
672 TIntermAggregate *parent;
673 TIntermNode *original;
674 TIntermSequence replacements;
675 };
676
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200677 // During traversing, save all the changes that need to happen into
Olli Etuahofc0e2bc2015-04-16 13:39:56 +0300678 // mReplacements/mMultiReplacements, then do them by calling updateTree().
679 // Multi replacements are processed after single replacements.
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200680 std::vector<NodeUpdateEntry> mReplacements;
Olli Etuahofc0e2bc2015-04-16 13:39:56 +0300681 std::vector<NodeReplaceWithMultipleEntry> mMultiReplacements;
Jamie Madillb1a85f42014-08-19 15:23:24 -0400682};
683
684//
685// For traversing the tree, and computing max depth.
686// Takes a maximum depth limit to prevent stack overflow.
687//
688class TMaxDepthTraverser : public TIntermTraverser
689{
690 public:
691 POOL_ALLOCATOR_NEW_DELETE();
692 TMaxDepthTraverser(int depthLimit)
693 : TIntermTraverser(true, true, false, false),
694 mDepthLimit(depthLimit) { }
695
696 virtual bool visitBinary(Visit, TIntermBinary *) { return depthCheck(); }
697 virtual bool visitUnary(Visit, TIntermUnary *) { return depthCheck(); }
698 virtual bool visitSelection(Visit, TIntermSelection *) { return depthCheck(); }
699 virtual bool visitAggregate(Visit, TIntermAggregate *) { return depthCheck(); }
700 virtual bool visitLoop(Visit, TIntermLoop *) { return depthCheck(); }
701 virtual bool visitBranch(Visit, TIntermBranch *) { return depthCheck(); }
702
Jamie Madill80d934b2015-02-19 10:16:12 -0500703 protected:
Jamie Madillb1a85f42014-08-19 15:23:24 -0400704 bool depthCheck() const { return mMaxDepth < mDepthLimit; }
705
706 int mDepthLimit;
707};
708
Geoff Lang0a73dd82014-11-19 16:18:08 -0500709#endif // COMPILER_TRANSLATOR_INTERMNODE_H_