blob: c5ddfd741e5899fbd9d4d0aeb1fba6e6c202ac86 [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"
Jamie Madillb1a85f42014-08-19 15:23:24 -040026#include "compiler/translator/ConstantUnion.h"
Olli Etuaho1033e1d2015-02-12 12:03:13 +020027#include "compiler/translator/Operator.h"
Jamie Madill6ba6ead2015-05-04 14:21:21 -040028#include "compiler/translator/Types.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(); }
Jamie Madillb1a85f42014-08-19 15:23:24 -0400126 TString getCompleteString() const { return mType.getCompleteString(); }
127
128 int getArraySize() const { return mType.getArraySize(); }
129
130 protected:
131 TType mType;
132};
133
134//
135// Handle for, do-while, and while loops.
136//
137enum TLoopType
138{
139 ELoopFor,
140 ELoopWhile,
141 ELoopDoWhile
142};
143
144class TIntermLoop : public TIntermNode
145{
146 public:
147 TIntermLoop(TLoopType type,
148 TIntermNode *init, TIntermTyped *cond, TIntermTyped *expr,
149 TIntermNode *body)
150 : mType(type),
151 mInit(init),
152 mCond(cond),
153 mExpr(expr),
154 mBody(body),
155 mUnrollFlag(false) { }
156
157 virtual TIntermLoop *getAsLoopNode() { return this; }
158 virtual void traverse(TIntermTraverser *);
159 virtual bool replaceChildNode(
160 TIntermNode *original, TIntermNode *replacement);
161
162 TLoopType getType() const { return mType; }
163 TIntermNode *getInit() { return mInit; }
164 TIntermTyped *getCondition() { return mCond; }
165 TIntermTyped *getExpression() { return mExpr; }
166 TIntermNode *getBody() { return mBody; }
167
168 void setUnrollFlag(bool flag) { mUnrollFlag = flag; }
169 bool getUnrollFlag() const { return mUnrollFlag; }
170
Jamie Madillb1a85f42014-08-19 15:23:24 -0400171 protected:
172 TLoopType mType;
173 TIntermNode *mInit; // for-loop initialization
174 TIntermTyped *mCond; // loop exit condition
175 TIntermTyped *mExpr; // for-loop expression
176 TIntermNode *mBody; // loop body
177
178 bool mUnrollFlag; // Whether the loop should be unrolled or not.
179};
180
181//
182// Handle break, continue, return, and kill.
183//
184class TIntermBranch : public TIntermNode
185{
186 public:
187 TIntermBranch(TOperator op, TIntermTyped *e)
188 : mFlowOp(op),
189 mExpression(e) { }
190
191 virtual void traverse(TIntermTraverser *);
192 virtual bool replaceChildNode(
193 TIntermNode *original, TIntermNode *replacement);
194
195 TOperator getFlowOp() { return mFlowOp; }
196 TIntermTyped* getExpression() { return mExpression; }
197
Jamie Madillb1a85f42014-08-19 15:23:24 -0400198protected:
199 TOperator mFlowOp;
200 TIntermTyped *mExpression; // non-zero except for "return exp;" statements
201};
202
203//
204// Nodes that correspond to symbols or constants in the source code.
205//
206class TIntermSymbol : public TIntermTyped
207{
208 public:
209 // if symbol is initialized as symbol(sym), the memory comes from the poolallocator of sym.
210 // If sym comes from per process globalpoolallocator, then it causes increased memory usage
211 // per compile it is essential to use "symbol = sym" to assign to symbol
212 TIntermSymbol(int id, const TString &symbol, const TType &type)
213 : TIntermTyped(type),
Olli Etuaho78174db2015-04-21 16:14:00 +0300214 mId(id),
215 mInternal(false)
Jamie Madillb1a85f42014-08-19 15:23:24 -0400216 {
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
Olli Etuaho78174db2015-04-21 16:14:00 +0300227 bool isInternal() const { return mInternal; }
228 void setInternal(bool isInternal) { mInternal = isInternal; }
229
Jamie Madillb1a85f42014-08-19 15:23:24 -0400230 virtual void traverse(TIntermTraverser *);
231 virtual TIntermSymbol *getAsSymbolNode() { return this; }
232 virtual bool replaceChildNode(TIntermNode *, TIntermNode *) { return false; }
233
Jamie Madillb1a85f42014-08-19 15:23:24 -0400234 protected:
235 int mId;
Olli Etuaho78174db2015-04-21 16:14:00 +0300236 bool mInternal;
Jamie Madillb1a85f42014-08-19 15:23:24 -0400237 TString mSymbol;
238};
239
240// A Raw node stores raw code, that the translator will insert verbatim
241// into the output stream. Useful for transformation operations that make
242// complex code that might not fit naturally into the GLSL model.
243class TIntermRaw : public TIntermTyped
244{
245 public:
246 TIntermRaw(const TType &type, const TString &rawText)
247 : TIntermTyped(type),
248 mRawText(rawText) { }
249
250 virtual bool hasSideEffects() const { return false; }
251
252 TString getRawText() const { return mRawText; }
253
254 virtual void traverse(TIntermTraverser *);
255
256 virtual TIntermRaw *getAsRawNode() { return this; }
257 virtual bool replaceChildNode(TIntermNode *, TIntermNode *) { return false; }
Jamie Madillb1a85f42014-08-19 15:23:24 -0400258
259 protected:
260 TString mRawText;
261};
262
263class TIntermConstantUnion : public TIntermTyped
264{
265 public:
Jamie Madill6ba6ead2015-05-04 14:21:21 -0400266 TIntermConstantUnion(TConstantUnion *unionPointer, const TType &type)
Jamie Madillb1a85f42014-08-19 15:23:24 -0400267 : TIntermTyped(type),
268 mUnionArrayPointer(unionPointer) { }
269
270 virtual bool hasSideEffects() const { return false; }
271
Jamie Madillb11e2482015-05-04 14:21:22 -0400272 const TConstantUnion *getUnionArrayPointer() const { return mUnionArrayPointer; }
273 TConstantUnion *getUnionArrayPointer() { return mUnionArrayPointer; }
Jamie Madillb1a85f42014-08-19 15:23:24 -0400274
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
Jamie Madillb11e2482015-05-04 14:21:22 -0400292 void replaceConstantUnion(TConstantUnion *safeConstantUnion)
293 {
294 // Previous union pointer freed on pool deallocation.
295 mUnionArrayPointer = safeConstantUnion;
296 }
297
Jamie Madillb1a85f42014-08-19 15:23:24 -0400298 virtual TIntermConstantUnion *getAsConstantUnion() { return this; }
299 virtual void traverse(TIntermTraverser *);
300 virtual bool replaceChildNode(TIntermNode *, TIntermNode *) { return false; }
301
Olli Etuaho2cb7b832015-04-23 20:27:44 +0300302 TIntermTyped *fold(TOperator op, TIntermConstantUnion *rightNode, TInfoSink &infoSink);
Jamie Madillb1a85f42014-08-19 15:23:24 -0400303
Arun Patolebf790422015-05-18 17:53:04 +0530304 static TIntermTyped *FoldAggregateBuiltIn(TOperator op, TIntermAggregate *aggregate, TInfoSink &infoSink);
Arun Patole274f0702015-05-05 13:33:30 +0530305
Jamie Madillb1a85f42014-08-19 15:23:24 -0400306 protected:
Jamie Madill6ba6ead2015-05-04 14:21:21 -0400307 TConstantUnion *mUnionArrayPointer;
Arun Patole9dea48f2015-04-02 11:45:09 +0530308
309 private:
310 typedef float(*FloatTypeUnaryFunc) (float);
Jamie Madill6ba6ead2015-05-04 14:21:21 -0400311 bool foldFloatTypeUnary(const TConstantUnion &parameter, FloatTypeUnaryFunc builtinFunc, TInfoSink &infoSink, TConstantUnion *result) const;
Jamie Madillb1a85f42014-08-19 15:23:24 -0400312};
313
314//
315// Intermediate class for node types that hold operators.
316//
317class TIntermOperator : public TIntermTyped
318{
319 public:
320 TOperator getOp() const { return mOp; }
321 void setOp(TOperator op) { mOp = op; }
322
323 bool isAssignment() const;
324 bool isConstructor() const;
325
326 virtual bool hasSideEffects() const { return isAssignment(); }
327
328 protected:
329 TIntermOperator(TOperator op)
330 : TIntermTyped(TType(EbtFloat, EbpUndefined)),
331 mOp(op) {}
332 TIntermOperator(TOperator op, const TType &type)
333 : TIntermTyped(type),
334 mOp(op) {}
335
336 TOperator mOp;
337};
338
339//
340// Nodes for all the basic binary math operators.
341//
342class TIntermBinary : public TIntermOperator
343{
344 public:
345 TIntermBinary(TOperator op)
346 : TIntermOperator(op),
347 mAddIndexClamp(false) {}
348
349 virtual TIntermBinary *getAsBinaryNode() { return this; }
350 virtual void traverse(TIntermTraverser *);
351 virtual bool replaceChildNode(
352 TIntermNode *original, TIntermNode *replacement);
353
354 virtual bool hasSideEffects() const
355 {
356 return isAssignment() || mLeft->hasSideEffects() || mRight->hasSideEffects();
357 }
358
359 void setLeft(TIntermTyped *node) { mLeft = node; }
360 void setRight(TIntermTyped *node) { mRight = node; }
361 TIntermTyped *getLeft() const { return mLeft; }
362 TIntermTyped *getRight() const { return mRight; }
363 bool promote(TInfoSink &);
364
365 void setAddIndexClamp() { mAddIndexClamp = true; }
366 bool getAddIndexClamp() { return mAddIndexClamp; }
367
Jamie Madillb1a85f42014-08-19 15:23:24 -0400368 protected:
369 TIntermTyped* mLeft;
370 TIntermTyped* mRight;
371
372 // If set to true, wrap any EOpIndexIndirect with a clamp to bounds.
373 bool mAddIndexClamp;
374};
375
376//
377// Nodes for unary math operators.
378//
379class TIntermUnary : public TIntermOperator
380{
381 public:
382 TIntermUnary(TOperator op, const TType &type)
383 : TIntermOperator(op, type),
384 mOperand(NULL),
385 mUseEmulatedFunction(false) {}
386 TIntermUnary(TOperator op)
387 : TIntermOperator(op),
388 mOperand(NULL),
389 mUseEmulatedFunction(false) {}
390
391 virtual void traverse(TIntermTraverser *);
392 virtual TIntermUnary *getAsUnaryNode() { return this; }
393 virtual bool replaceChildNode(
394 TIntermNode *original, TIntermNode *replacement);
395
396 virtual bool hasSideEffects() const
397 {
398 return isAssignment() || mOperand->hasSideEffects();
399 }
400
401 void setOperand(TIntermTyped *operand) { mOperand = operand; }
402 TIntermTyped *getOperand() { return mOperand; }
Olli Etuahof6c694b2015-03-26 14:50:53 +0200403 void promote(const TType *funcReturnType);
Jamie Madillb1a85f42014-08-19 15:23:24 -0400404
405 void setUseEmulatedFunction() { mUseEmulatedFunction = true; }
406 bool getUseEmulatedFunction() { return mUseEmulatedFunction; }
407
Jamie Madillb1a85f42014-08-19 15:23:24 -0400408 protected:
409 TIntermTyped *mOperand;
410
411 // If set to true, replace the built-in function call with an emulated one
412 // to work around driver bugs.
413 bool mUseEmulatedFunction;
414};
415
416typedef TVector<TIntermNode *> TIntermSequence;
417typedef TVector<int> TQualifierList;
418
419//
420// Nodes that operate on an arbitrary sized set of children.
421//
422class TIntermAggregate : public TIntermOperator
423{
424 public:
425 TIntermAggregate()
426 : TIntermOperator(EOpNull),
427 mUserDefined(false),
428 mUseEmulatedFunction(false) { }
429 TIntermAggregate(TOperator op)
430 : TIntermOperator(op),
431 mUseEmulatedFunction(false) { }
432 ~TIntermAggregate() { }
433
434 virtual TIntermAggregate *getAsAggregate() { return this; }
435 virtual void traverse(TIntermTraverser *);
436 virtual bool replaceChildNode(
437 TIntermNode *original, TIntermNode *replacement);
Olli Etuahofc0e2bc2015-04-16 13:39:56 +0300438 bool replaceChildNodeWithMultiple(TIntermNode *original, TIntermSequence replacements);
Olli Etuahoa6f22092015-05-08 18:31:10 +0300439 bool insertChildNodes(TIntermSequence::size_type position, TIntermSequence insertions);
Jamie Madillb1a85f42014-08-19 15:23:24 -0400440 // Conservatively assume function calls and other aggregate operators have side-effects
441 virtual bool hasSideEffects() const { return true; }
442
443 TIntermSequence *getSequence() { return &mSequence; }
444
445 void setName(const TString &name) { mName = name; }
446 const TString &getName() const { return mName; }
447
448 void setUserDefined() { mUserDefined = true; }
449 bool isUserDefined() const { return mUserDefined; }
450
451 void setOptimize(bool optimize) { mOptimize = optimize; }
452 bool getOptimize() const { return mOptimize; }
453 void setDebug(bool debug) { mDebug = debug; }
454 bool getDebug() const { return mDebug; }
455
Corentin Wallez71d147f2015-02-11 11:15:24 -0800456 void setFunctionId(int functionId) { mFunctionId = functionId; }
457 int getFunctionId() const { return mFunctionId; }
458
Jamie Madillb1a85f42014-08-19 15:23:24 -0400459 void setUseEmulatedFunction() { mUseEmulatedFunction = true; }
460 bool getUseEmulatedFunction() { return mUseEmulatedFunction; }
461
Olli Etuahod2a67b92014-10-21 16:42:57 +0300462 void setPrecisionFromChildren();
463 void setBuiltInFunctionPrecision();
464
Jamie Madillb1a85f42014-08-19 15:23:24 -0400465 protected:
466 TIntermAggregate(const TIntermAggregate &); // disallow copy constructor
467 TIntermAggregate &operator=(const TIntermAggregate &); // disallow assignment operator
468 TIntermSequence mSequence;
469 TString mName;
470 bool mUserDefined; // used for user defined function names
Corentin Wallez71d147f2015-02-11 11:15:24 -0800471 int mFunctionId;
Jamie Madillb1a85f42014-08-19 15:23:24 -0400472
473 bool mOptimize;
474 bool mDebug;
475
476 // If set to true, replace the built-in function call with an emulated one
477 // to work around driver bugs.
478 bool mUseEmulatedFunction;
479};
480
481//
Olli Etuahoa3a36662015-02-17 13:46:51 +0200482// For if tests.
Jamie Madillb1a85f42014-08-19 15:23:24 -0400483//
484class TIntermSelection : public TIntermTyped
485{
486 public:
487 TIntermSelection(TIntermTyped *cond, TIntermNode *trueB, TIntermNode *falseB)
488 : TIntermTyped(TType(EbtVoid, EbpUndefined)),
489 mCondition(cond),
490 mTrueBlock(trueB),
491 mFalseBlock(falseB) {}
492 TIntermSelection(TIntermTyped *cond, TIntermNode *trueB, TIntermNode *falseB,
493 const TType &type)
494 : TIntermTyped(type),
495 mCondition(cond),
496 mTrueBlock(trueB),
497 mFalseBlock(falseB) {}
498
499 virtual void traverse(TIntermTraverser *);
500 virtual bool replaceChildNode(
501 TIntermNode *original, TIntermNode *replacement);
502
503 // Conservatively assume selections have side-effects
504 virtual bool hasSideEffects() const { return true; }
505
506 bool usesTernaryOperator() const { return getBasicType() != EbtVoid; }
507 TIntermNode *getCondition() const { return mCondition; }
508 TIntermNode *getTrueBlock() const { return mTrueBlock; }
509 TIntermNode *getFalseBlock() const { return mFalseBlock; }
510 TIntermSelection *getAsSelectionNode() { return this; }
511
Jamie Madillb1a85f42014-08-19 15:23:24 -0400512protected:
513 TIntermTyped *mCondition;
514 TIntermNode *mTrueBlock;
515 TIntermNode *mFalseBlock;
516};
517
Olli Etuahoa3a36662015-02-17 13:46:51 +0200518//
519// Switch statement.
520//
521class TIntermSwitch : public TIntermNode
522{
523 public:
524 TIntermSwitch(TIntermTyped *init, TIntermAggregate *statementList)
525 : TIntermNode(),
526 mInit(init),
527 mStatementList(statementList)
528 {
529 }
530
531 void traverse(TIntermTraverser *it) override;
532 bool replaceChildNode(
533 TIntermNode *original, TIntermNode *replacement) override;
534
535 TIntermSwitch *getAsSwitchNode() override { return this; }
536
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200537 TIntermAggregate *getStatementList() { return mStatementList; }
Olli Etuaho2cd7a0e2015-02-27 13:57:32 +0200538 void setStatementList(TIntermAggregate *statementList) { mStatementList = statementList; }
Olli Etuaho01cd8af2015-02-20 10:39:20 +0200539
Olli Etuahoa3a36662015-02-17 13:46:51 +0200540 protected:
541 TIntermTyped *mInit;
542 TIntermAggregate *mStatementList;
543};
544
545//
546// Case label.
547//
548class TIntermCase : public TIntermNode
549{
550 public:
551 TIntermCase(TIntermTyped *condition)
552 : TIntermNode(),
553 mCondition(condition)
554 {
555 }
556
557 void traverse(TIntermTraverser *it) override;
558 bool replaceChildNode(
559 TIntermNode *original, TIntermNode *replacement) override;
560
561 TIntermCase *getAsCaseNode() override { return this; }
562
563 bool hasCondition() const { return mCondition != nullptr; }
564 TIntermTyped *getCondition() const { return mCondition; }
565
566 protected:
567 TIntermTyped *mCondition;
568};
569
Jamie Madillb1a85f42014-08-19 15:23:24 -0400570enum Visit
571{
572 PreVisit,
573 InVisit,
574 PostVisit
575};
576
577//
578// For traversing the tree. User should derive from this,
579// put their traversal specific data in it, and then pass
580// it to a Traverse method.
581//
582// When using this, just fill in the methods for nodes you want visited.
583// Return false from a pre-visit to skip visiting that node's subtree.
584//
Jamie Madillf0d10f82015-03-31 12:56:52 -0400585class TIntermTraverser : angle::NonCopyable
Jamie Madillb1a85f42014-08-19 15:23:24 -0400586{
587 public:
588 POOL_ALLOCATOR_NEW_DELETE();
589 // TODO(zmo): remove default values.
590 TIntermTraverser(bool preVisit = true, bool inVisit = false, bool postVisit = false,
591 bool rightToLeft = false)
592 : preVisit(preVisit),
593 inVisit(inVisit),
594 postVisit(postVisit),
595 rightToLeft(rightToLeft),
596 mDepth(0),
597 mMaxDepth(0) {}
598 virtual ~TIntermTraverser() {}
599
600 virtual void visitSymbol(TIntermSymbol *) {}
601 virtual void visitRaw(TIntermRaw *) {}
602 virtual void visitConstantUnion(TIntermConstantUnion *) {}
603 virtual bool visitBinary(Visit, TIntermBinary *) { return true; }
604 virtual bool visitUnary(Visit, TIntermUnary *) { return true; }
605 virtual bool visitSelection(Visit, TIntermSelection *) { return true; }
Olli Etuahoa3a36662015-02-17 13:46:51 +0200606 virtual bool visitSwitch(Visit, TIntermSwitch *) { return true; }
607 virtual bool visitCase(Visit, TIntermCase *) { return true; }
Jamie Madillb1a85f42014-08-19 15:23:24 -0400608 virtual bool visitAggregate(Visit, TIntermAggregate *) { return true; }
609 virtual bool visitLoop(Visit, TIntermLoop *) { return true; }
610 virtual bool visitBranch(Visit, TIntermBranch *) { return true; }
611
612 int getMaxDepth() const { return mMaxDepth; }
613
614 void incrementDepth(TIntermNode *current)
615 {
616 mDepth++;
617 mMaxDepth = std::max(mMaxDepth, mDepth);
618 mPath.push_back(current);
619 }
620
621 void decrementDepth()
622 {
623 mDepth--;
624 mPath.pop_back();
625 }
626
627 TIntermNode *getParentNode()
628 {
629 return mPath.size() == 0 ? NULL : mPath.back();
630 }
631
Olli Etuaho56eea882015-05-18 12:41:03 +0300632 void pushParentBlock(TIntermAggregate *node);
633 void incrementParentBlockPos();
634 void popParentBlock();
635
Jamie Madillb1a85f42014-08-19 15:23:24 -0400636 // Return the original name if hash function pointer is NULL;
637 // otherwise return the hashed name.
638 static TString hash(const TString& name, ShHashFunction64 hashFunction);
639
640 const bool preVisit;
641 const bool inVisit;
642 const bool postVisit;
643 const bool rightToLeft;
644
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200645 // If traversers need to replace nodes, they can add the replacements in
Olli Etuahofc0e2bc2015-04-16 13:39:56 +0300646 // mReplacements/mMultiReplacements during traversal and the user of the traverser should call
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200647 // this function after traversal to perform them.
648 void updateTree();
649
Jamie Madillb1a85f42014-08-19 15:23:24 -0400650 protected:
651 int mDepth;
652 int mMaxDepth;
653
654 // All the nodes from root to the current node's parent during traversing.
655 TVector<TIntermNode *> mPath;
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200656
Olli Etuahofc0e2bc2015-04-16 13:39:56 +0300657 // To replace a single node with another on the parent node
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200658 struct NodeUpdateEntry
659 {
660 NodeUpdateEntry(TIntermNode *_parent,
661 TIntermNode *_original,
662 TIntermNode *_replacement,
663 bool _originalBecomesChildOfReplacement)
664 : parent(_parent),
665 original(_original),
666 replacement(_replacement),
667 originalBecomesChildOfReplacement(_originalBecomesChildOfReplacement) {}
668
669 TIntermNode *parent;
670 TIntermNode *original;
671 TIntermNode *replacement;
672 bool originalBecomesChildOfReplacement;
673 };
674
Olli Etuahofc0e2bc2015-04-16 13:39:56 +0300675 // To replace a single node with multiple nodes on the parent aggregate node
676 struct NodeReplaceWithMultipleEntry
677 {
678 NodeReplaceWithMultipleEntry(TIntermAggregate *_parent, TIntermNode *_original, TIntermSequence _replacements)
679 : parent(_parent),
680 original(_original),
681 replacements(_replacements)
682 {
683 }
684
685 TIntermAggregate *parent;
686 TIntermNode *original;
687 TIntermSequence replacements;
688 };
689
Olli Etuahoa6f22092015-05-08 18:31:10 +0300690 // To insert multiple nodes on the parent aggregate node
691 struct NodeInsertMultipleEntry
692 {
693 NodeInsertMultipleEntry(TIntermAggregate *_parent, TIntermSequence::size_type _position, TIntermSequence _insertions)
694 : parent(_parent),
695 position(_position),
696 insertions(_insertions)
697 {
698 }
699
700 TIntermAggregate *parent;
701 TIntermSequence::size_type position;
702 TIntermSequence insertions;
703 };
704
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200705 // During traversing, save all the changes that need to happen into
Olli Etuahofc0e2bc2015-04-16 13:39:56 +0300706 // mReplacements/mMultiReplacements, then do them by calling updateTree().
707 // Multi replacements are processed after single replacements.
Olli Etuaho853dc1a2014-11-06 17:25:48 +0200708 std::vector<NodeUpdateEntry> mReplacements;
Olli Etuahofc0e2bc2015-04-16 13:39:56 +0300709 std::vector<NodeReplaceWithMultipleEntry> mMultiReplacements;
Olli Etuahoa6f22092015-05-08 18:31:10 +0300710 std::vector<NodeInsertMultipleEntry> mInsertions;
Olli Etuaho56eea882015-05-18 12:41:03 +0300711
712 // Helper to insert statements in the parent block (sequence) of the node currently being traversed.
713 // The statements will be inserted before the node being traversed once updateTree is called.
714 // Should only be called during PreVisit or PostVisit from sequence nodes.
715 // Note that inserting more than one set of nodes to the same parent node on a single updateTree call is not
716 // supported.
717 void insertStatementsInParentBlock(const TIntermSequence &insertions);
718
719 private:
720 struct ParentBlock
721 {
722 ParentBlock(TIntermAggregate *nodeIn, TIntermSequence::size_type posIn)
723 : node(nodeIn),
724 pos(posIn)
725 {
726 }
727
728 TIntermAggregate *node;
729 TIntermSequence::size_type pos;
730 };
731 // All the code blocks from the root to the current node's parent during traversal.
732 std::vector<ParentBlock> mParentBlockStack;
Jamie Madillb1a85f42014-08-19 15:23:24 -0400733};
734
735//
736// For traversing the tree, and computing max depth.
737// Takes a maximum depth limit to prevent stack overflow.
738//
739class TMaxDepthTraverser : public TIntermTraverser
740{
741 public:
742 POOL_ALLOCATOR_NEW_DELETE();
743 TMaxDepthTraverser(int depthLimit)
744 : TIntermTraverser(true, true, false, false),
745 mDepthLimit(depthLimit) { }
746
747 virtual bool visitBinary(Visit, TIntermBinary *) { return depthCheck(); }
748 virtual bool visitUnary(Visit, TIntermUnary *) { return depthCheck(); }
749 virtual bool visitSelection(Visit, TIntermSelection *) { return depthCheck(); }
750 virtual bool visitAggregate(Visit, TIntermAggregate *) { return depthCheck(); }
751 virtual bool visitLoop(Visit, TIntermLoop *) { return depthCheck(); }
752 virtual bool visitBranch(Visit, TIntermBranch *) { return depthCheck(); }
753
Jamie Madill80d934b2015-02-19 10:16:12 -0500754 protected:
Jamie Madillb1a85f42014-08-19 15:23:24 -0400755 bool depthCheck() const { return mMaxDepth < mDepthLimit; }
756
757 int mDepthLimit;
758};
759
Geoff Lang0a73dd82014-11-19 16:18:08 -0500760#endif // COMPILER_TRANSLATOR_INTERMNODE_H_