blob: 57d53bf21ea0d51f53fbe696a47caa9d92c83b4e [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// 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// Build the intermediate representation.
9//
10
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000011#include <float.h>
alokp@chromium.org1bcc3fd2010-05-19 17:08:44 +000012#include <limits.h>
alokp@chromium.org32cfaf42010-08-23 21:01:13 +000013#include <algorithm>
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000014
Jamie Madillb1a85f42014-08-19 15:23:24 -040015#include "compiler/translator/Intermediate.h"
Geoff Lang17732822013-08-29 13:46:49 -040016#include "compiler/translator/SymbolTable.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000017
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000018////////////////////////////////////////////////////////////////////////////
19//
20// First set of functions are to help build the intermediate representation.
21// These functions are not member functions of the nodes.
22// They are called from parser productions.
23//
24/////////////////////////////////////////////////////////////////////////////
25
26//
27// Add a terminal node for an identifier in an expression.
28//
29// Returns the added node.
30//
Zhenyao Moe40d1e92014-07-16 17:40:36 -070031TIntermSymbol *TIntermediate::addSymbol(
32 int id, const TString &name, const TType &type, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000033{
Zhenyao Moe40d1e92014-07-16 17:40:36 -070034 TIntermSymbol *node = new TIntermSymbol(id, name, type);
alokp@chromium.org2cf17712010-03-30 20:33:18 +000035 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000036
alokp@chromium.org2cf17712010-03-30 20:33:18 +000037 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000038}
39
40//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000041// Connect two nodes through an index operator, where the left node is the base
42// of an array or struct, and the right node is a direct or indirect offset.
43//
44// Returns the added node.
45// The caller should set the type of the returned node.
46//
Olli Etuaho3272a6d2016-08-29 17:54:50 +030047TIntermTyped *TIntermediate::addIndex(TOperator op,
48 TIntermTyped *base,
49 TIntermTyped *index,
50 const TSourceLoc &line,
51 TDiagnostics *diagnostics)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000052{
Olli Etuaho3272a6d2016-08-29 17:54:50 +030053 TIntermBinary *node = new TIntermBinary(op, base, index);
alokp@chromium.org2cf17712010-03-30 20:33:18 +000054 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000055
Olli Etuaho3272a6d2016-08-29 17:54:50 +030056 TIntermTyped *folded = node->fold(diagnostics);
57 if (folded)
58 {
59 return folded;
60 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000061
alokp@chromium.org2cf17712010-03-30 20:33:18 +000062 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000063}
64
65//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000066// This is the safe way to change the operator on an aggregate, as it
67// does lots of error checking and fixing. Especially for establishing
68// a function call's operation on it's set of parameters. Sequences
69// of instructions are also aggregates, but they just direnctly set
70// their operator to EOpSequence.
71//
72// Returns an aggregate node, which could be the one passed in if
daniel@transgaming.com978702d2012-04-04 15:05:58 +000073// it was already an aggregate but no operator was set.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000074//
Zhenyao Moe40d1e92014-07-16 17:40:36 -070075TIntermAggregate *TIntermediate::setAggregateOperator(
76 TIntermNode *node, TOperator op, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000077{
Zhenyao Moe40d1e92014-07-16 17:40:36 -070078 TIntermAggregate *aggNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000079
alokp@chromium.org2cf17712010-03-30 20:33:18 +000080 //
81 // Make sure we have an aggregate. If not turn it into one.
82 //
Zhenyao Moe40d1e92014-07-16 17:40:36 -070083 if (node)
84 {
alokp@chromium.org2cf17712010-03-30 20:33:18 +000085 aggNode = node->getAsAggregate();
Zhenyao Moe40d1e92014-07-16 17:40:36 -070086 if (aggNode == NULL || aggNode->getOp() != EOpNull)
87 {
alokp@chromium.org2cf17712010-03-30 20:33:18 +000088 //
89 // Make an aggregate containing this node.
90 //
91 aggNode = new TIntermAggregate();
Zhenyao Moe40d1e92014-07-16 17:40:36 -070092 aggNode->getSequence()->push_back(node);
alokp@chromium.org2cf17712010-03-30 20:33:18 +000093 }
Zhenyao Moe40d1e92014-07-16 17:40:36 -070094 }
95 else
96 {
alokp@chromium.org2cf17712010-03-30 20:33:18 +000097 aggNode = new TIntermAggregate();
Zhenyao Moe40d1e92014-07-16 17:40:36 -070098 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000099
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000100 //
101 // Set the operator.
102 //
alokp@chromium.org58e54292010-08-24 21:40:03 +0000103 aggNode->setOp(op);
Jamie Madill075edd82013-07-08 13:30:19 -0400104 aggNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000105
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000106 return aggNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000107}
108
109//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000110// Safe way to combine two nodes into an aggregate. Works with null pointers,
111// a node that's not a aggregate yet, etc.
112//
113// Returns the resulting aggregate, unless 0 was passed in for
114// both existing nodes.
115//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700116TIntermAggregate *TIntermediate::growAggregate(
117 TIntermNode *left, TIntermNode *right, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000118{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700119 if (left == NULL && right == NULL)
120 return NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000121
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700122 TIntermAggregate *aggNode = NULL;
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000123 if (left)
124 aggNode = left->getAsAggregate();
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700125 if (!aggNode || aggNode->getOp() != EOpNull)
126 {
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000127 aggNode = new TIntermAggregate;
128 if (left)
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700129 aggNode->getSequence()->push_back(left);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000130 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000131
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000132 if (right)
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700133 aggNode->getSequence()->push_back(right);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000134
Jamie Madill075edd82013-07-08 13:30:19 -0400135 aggNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000136
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000137 return aggNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000138}
139
140//
141// Turn an existing node into an aggregate.
142//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700143// Returns an aggregate, unless NULL was passed in for the existing node.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000144//
Olli Etuaho32db19b2016-10-04 14:43:16 +0100145TIntermAggregate *TIntermediate::MakeAggregate(TIntermNode *node, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000146{
Olli Etuaho32db19b2016-10-04 14:43:16 +0100147 if (node == nullptr)
148 return nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000149
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700150 TIntermAggregate *aggNode = new TIntermAggregate;
151 aggNode->getSequence()->push_back(node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000152
Jamie Madill075edd82013-07-08 13:30:19 -0400153 aggNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000154
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000155 return aggNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000156}
157
Olli Etuaho7d7f8c42015-05-19 18:38:49 +0300158// If the input node is nullptr, return nullptr.
159// If the input node is a sequence (block) node, return it.
160// If the input node is not a sequence node, put it inside a sequence node and return that.
Olli Etuaho32db19b2016-10-04 14:43:16 +0100161TIntermAggregate *TIntermediate::EnsureSequence(TIntermNode *node)
Olli Etuaho7d7f8c42015-05-19 18:38:49 +0300162{
163 if (node == nullptr)
164 return nullptr;
165 TIntermAggregate *aggNode = node->getAsAggregate();
166 if (aggNode != nullptr && aggNode->getOp() == EOpSequence)
167 return aggNode;
168
Olli Etuaho32db19b2016-10-04 14:43:16 +0100169 aggNode = MakeAggregate(node, node->getLine());
Olli Etuaho7d7f8c42015-05-19 18:38:49 +0300170 aggNode->setOp(EOpSequence);
171 return aggNode;
172}
173
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000174// For "if" test nodes. There are three children; a condition,
175// a true path, and a false path. The two paths are in the
176// nodePair.
177//
Olli Etuaho57961272016-09-14 13:57:46 +0300178// Returns the node created.
179TIntermNode *TIntermediate::addIfElse(TIntermTyped *cond,
180 TIntermNodePair nodePair,
181 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000182{
Olli Etuaho57961272016-09-14 13:57:46 +0300183 // For compile time constant conditions, prune the code now.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000184
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200185 if (cond->getAsConstantUnion())
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700186 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +0000187 if (cond->getAsConstantUnion()->getBConst(0) == true)
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700188 {
Olli Etuaho57961272016-09-14 13:57:46 +0300189 return nodePair.node1 ? setAggregateOperator(nodePair.node1, EOpSequence,
190 nodePair.node1->getLine())
191 : nullptr;
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700192 }
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000193 else
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700194 {
Olli Etuaho57961272016-09-14 13:57:46 +0300195 return nodePair.node2 ? setAggregateOperator(nodePair.node2, EOpSequence,
196 nodePair.node2->getLine())
197 : nullptr;
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700198 }
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000199 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000200
Olli Etuaho57961272016-09-14 13:57:46 +0300201 TIntermIfElse *node =
Olli Etuaho32db19b2016-10-04 14:43:16 +0100202 new TIntermIfElse(cond, EnsureSequence(nodePair.node1), EnsureSequence(nodePair.node2));
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000203 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000204
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000205 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000206}
207
Olli Etuaho15200042015-11-04 16:56:31 +0200208TIntermTyped *TIntermediate::addComma(TIntermTyped *left,
209 TIntermTyped *right,
210 const TSourceLoc &line,
211 int shaderVersion)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000212{
Olli Etuaho15200042015-11-04 16:56:31 +0200213 TQualifier resultQualifier = EvqConst;
214 // ESSL3.00 section 12.43: The result of a sequence operator is not a constant-expression.
215 if (shaderVersion >= 300 || left->getQualifier() != EvqConst ||
216 right->getQualifier() != EvqConst)
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700217 {
Olli Etuaho15200042015-11-04 16:56:31 +0200218 resultQualifier = EvqTemporary;
219 }
220
221 TIntermTyped *commaNode = nullptr;
222 if (!left->hasSideEffects())
223 {
224 commaNode = right;
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700225 }
226 else
227 {
Olli Etuaho15200042015-11-04 16:56:31 +0200228 commaNode = growAggregate(left, right, line);
229 commaNode->getAsAggregate()->setOp(EOpComma);
230 commaNode->setType(right->getType());
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000231 }
Olli Etuaho15200042015-11-04 16:56:31 +0200232 commaNode->getTypePointer()->setQualifier(resultQualifier);
233 return commaNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000234}
235
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000236// For "?:" test nodes. There are three children; a condition,
237// a true path, and a false path. The two paths are specified
238// as separate parameters.
239//
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300240// Returns the ternary node created, or one of trueExpression and falseExpression if the expression
241// could be folded.
242TIntermTyped *TIntermediate::AddTernarySelection(TIntermTyped *cond,
243 TIntermTyped *trueExpression,
244 TIntermTyped *falseExpression,
245 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000246{
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200247 // Note that the node resulting from here can be a constant union without being qualified as
248 // constant.
249 if (cond->getAsConstantUnion())
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700250 {
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300251 TQualifier resultQualifier =
252 TIntermTernary::DetermineQualifier(cond, trueExpression, falseExpression);
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +0000253 if (cond->getAsConstantUnion()->getBConst(0))
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200254 {
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300255 trueExpression->getTypePointer()->setQualifier(resultQualifier);
256 return trueExpression;
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200257 }
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000258 else
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200259 {
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300260 falseExpression->getTypePointer()->setQualifier(resultQualifier);
261 return falseExpression;
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200262 }
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000263 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000264
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300265 // Make a ternary node.
266 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000267 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000268
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000269 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000270}
271
Olli Etuahoa3a36662015-02-17 13:46:51 +0200272TIntermSwitch *TIntermediate::addSwitch(
273 TIntermTyped *init, TIntermAggregate *statementList, const TSourceLoc &line)
274{
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200275 TIntermSwitch *node = new TIntermSwitch(init, statementList);
276 node->setLine(line);
277
278 return node;
Olli Etuahoa3a36662015-02-17 13:46:51 +0200279}
280
281TIntermCase *TIntermediate::addCase(
282 TIntermTyped *condition, const TSourceLoc &line)
283{
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200284 TIntermCase *node = new TIntermCase(condition);
285 node->setLine(line);
286
287 return node;
Olli Etuahoa3a36662015-02-17 13:46:51 +0200288}
289
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000290//
291// Constant terminal nodes. Has a union that contains bool, float or int constants
292//
293// Returns the constant union node created.
294//
295
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200296TIntermConstantUnion *TIntermediate::addConstantUnion(const TConstantUnion *constantUnion,
297 const TType &type,
298 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000299{
Jamie Madillb11e2482015-05-04 14:21:22 -0400300 TIntermConstantUnion *node = new TIntermConstantUnion(constantUnion, type);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000301 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000302
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000303 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000304}
305
Olli Etuahob6fa0432016-09-28 16:28:05 +0100306TIntermTyped *TIntermediate::AddSwizzle(TIntermTyped *baseExpression,
307 const TVectorFields &fields,
308 const TSourceLoc &dotLocation)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000309{
Olli Etuahob6fa0432016-09-28 16:28:05 +0100310 TVector<int> fieldsVector;
311 for (int i = 0; i < fields.num; ++i)
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700312 {
Olli Etuahob6fa0432016-09-28 16:28:05 +0100313 fieldsVector.push_back(fields.offsets[i]);
314 }
315 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldsVector);
316 node->setLine(dotLocation);
317
318 TIntermTyped *folded = node->fold();
319 if (folded)
320 {
321 return folded;
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000322 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000323
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000324 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000325}
326
327//
328// Create loop nodes.
329//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700330TIntermNode *TIntermediate::addLoop(
331 TLoopType type, TIntermNode *init, TIntermTyped *cond, TIntermTyped *expr,
332 TIntermNode *body, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000333{
Olli Etuaho32db19b2016-10-04 14:43:16 +0100334 TIntermNode *node = new TIntermLoop(type, init, cond, expr, EnsureSequence(body));
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000335 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000336
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000337 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000338}
339
340//
341// Add branches.
342//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700343TIntermBranch* TIntermediate::addBranch(
344 TOperator branchOp, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000345{
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000346 return addBranch(branchOp, 0, line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000347}
348
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700349TIntermBranch* TIntermediate::addBranch(
350 TOperator branchOp, TIntermTyped *expression, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000351{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700352 TIntermBranch *node = new TIntermBranch(branchOp, expression);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000353 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000354
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000355 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000356}
357
358//
359// This is to be executed once the final root is put on top by the parsing
360// process.
361//
Olli Etuahof119a262016-08-19 15:54:22 +0300362TIntermAggregate *TIntermediate::PostProcess(TIntermNode *root)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000363{
Olli Etuaho43613b02015-08-04 11:02:21 +0300364 if (root == nullptr)
365 return nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000366
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000367 //
Olli Etuaho43613b02015-08-04 11:02:21 +0300368 // Finish off the top level sequence, if any
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000369 //
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700370 TIntermAggregate *aggRoot = root->getAsAggregate();
Olli Etuaho43613b02015-08-04 11:02:21 +0300371 if (aggRoot != nullptr && aggRoot->getOp() == EOpNull)
372 {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000373 aggRoot->setOp(EOpSequence);
Olli Etuaho43613b02015-08-04 11:02:21 +0300374 }
375 else if (aggRoot == nullptr || aggRoot->getOp() != EOpSequence)
376 {
377 aggRoot = new TIntermAggregate(EOpSequence);
378 aggRoot->setLine(root->getLine());
379 aggRoot->getSequence()->push_back(root);
380 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000381
Olli Etuaho43613b02015-08-04 11:02:21 +0300382 return aggRoot;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000383}
Arun Patole274f0702015-05-05 13:33:30 +0530384
Olli Etuahof119a262016-08-19 15:54:22 +0300385TIntermTyped *TIntermediate::foldAggregateBuiltIn(TIntermAggregate *aggregate,
386 TDiagnostics *diagnostics)
Arun Patole274f0702015-05-05 13:33:30 +0530387{
Olli Etuahob43846e2015-06-02 18:18:57 +0300388 switch (aggregate->getOp())
Arun Patole274f0702015-05-05 13:33:30 +0530389 {
Olli Etuaho1d122782015-11-06 15:35:17 +0200390 case EOpAtan:
391 case EOpPow:
392 case EOpMod:
393 case EOpMin:
394 case EOpMax:
395 case EOpClamp:
396 case EOpMix:
397 case EOpStep:
398 case EOpSmoothStep:
399 case EOpMul:
400 case EOpOuterProduct:
401 case EOpLessThan:
402 case EOpLessThanEqual:
403 case EOpGreaterThan:
404 case EOpGreaterThanEqual:
405 case EOpVectorEqual:
406 case EOpVectorNotEqual:
407 case EOpDistance:
408 case EOpDot:
409 case EOpCross:
410 case EOpFaceForward:
411 case EOpReflect:
412 case EOpRefract:
Olli Etuahof119a262016-08-19 15:54:22 +0300413 return aggregate->fold(diagnostics);
Olli Etuaho1d122782015-11-06 15:35:17 +0200414 default:
415 // TODO: Add support for folding array constructors
416 if (aggregate->isConstructor() && !aggregate->isArray())
417 {
Olli Etuahof119a262016-08-19 15:54:22 +0300418 return aggregate->fold(diagnostics);
Olli Etuaho1d122782015-11-06 15:35:17 +0200419 }
420 // Constant folding not supported for the built-in.
421 return nullptr;
Arun Patole274f0702015-05-05 13:33:30 +0530422 }
Arun Patole274f0702015-05-05 13:33:30 +0530423}