blob: d018260a95d2116c2491bf372495a01ca7e2cebd [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//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700145TIntermAggregate *TIntermediate::makeAggregate(
146 TIntermNode *node, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000147{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700148 if (node == NULL)
149 return NULL;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000150
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700151 TIntermAggregate *aggNode = new TIntermAggregate;
152 aggNode->getSequence()->push_back(node);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000153
Jamie Madill075edd82013-07-08 13:30:19 -0400154 aggNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000155
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000156 return aggNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000157}
158
Olli Etuaho7d7f8c42015-05-19 18:38:49 +0300159// If the input node is nullptr, return nullptr.
160// If the input node is a sequence (block) node, return it.
161// If the input node is not a sequence node, put it inside a sequence node and return that.
162TIntermAggregate *TIntermediate::ensureSequence(TIntermNode *node)
163{
164 if (node == nullptr)
165 return nullptr;
166 TIntermAggregate *aggNode = node->getAsAggregate();
167 if (aggNode != nullptr && aggNode->getOp() == EOpSequence)
168 return aggNode;
169
170 aggNode = makeAggregate(node, node->getLine());
171 aggNode->setOp(EOpSequence);
172 return aggNode;
173}
174
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000175// For "if" test nodes. There are three children; a condition,
176// a true path, and a false path. The two paths are in the
177// nodePair.
178//
Olli Etuaho57961272016-09-14 13:57:46 +0300179// Returns the node created.
180TIntermNode *TIntermediate::addIfElse(TIntermTyped *cond,
181 TIntermNodePair nodePair,
182 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000183{
Olli Etuaho57961272016-09-14 13:57:46 +0300184 // For compile time constant conditions, prune the code now.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000185
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200186 if (cond->getAsConstantUnion())
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700187 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +0000188 if (cond->getAsConstantUnion()->getBConst(0) == true)
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700189 {
Olli Etuaho57961272016-09-14 13:57:46 +0300190 return nodePair.node1 ? setAggregateOperator(nodePair.node1, EOpSequence,
191 nodePair.node1->getLine())
192 : nullptr;
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700193 }
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000194 else
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700195 {
Olli Etuaho57961272016-09-14 13:57:46 +0300196 return nodePair.node2 ? setAggregateOperator(nodePair.node2, EOpSequence,
197 nodePair.node2->getLine())
198 : nullptr;
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700199 }
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000200 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000201
Olli Etuaho57961272016-09-14 13:57:46 +0300202 TIntermIfElse *node =
203 new TIntermIfElse(cond, ensureSequence(nodePair.node1), ensureSequence(nodePair.node2));
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000204 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000205
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000206 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000207}
208
Olli Etuaho15200042015-11-04 16:56:31 +0200209TIntermTyped *TIntermediate::addComma(TIntermTyped *left,
210 TIntermTyped *right,
211 const TSourceLoc &line,
212 int shaderVersion)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000213{
Olli Etuaho15200042015-11-04 16:56:31 +0200214 TQualifier resultQualifier = EvqConst;
215 // ESSL3.00 section 12.43: The result of a sequence operator is not a constant-expression.
216 if (shaderVersion >= 300 || left->getQualifier() != EvqConst ||
217 right->getQualifier() != EvqConst)
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700218 {
Olli Etuaho15200042015-11-04 16:56:31 +0200219 resultQualifier = EvqTemporary;
220 }
221
222 TIntermTyped *commaNode = nullptr;
223 if (!left->hasSideEffects())
224 {
225 commaNode = right;
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700226 }
227 else
228 {
Olli Etuaho15200042015-11-04 16:56:31 +0200229 commaNode = growAggregate(left, right, line);
230 commaNode->getAsAggregate()->setOp(EOpComma);
231 commaNode->setType(right->getType());
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000232 }
Olli Etuaho15200042015-11-04 16:56:31 +0200233 commaNode->getTypePointer()->setQualifier(resultQualifier);
234 return commaNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000235}
236
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000237// For "?:" test nodes. There are three children; a condition,
238// a true path, and a false path. The two paths are specified
239// as separate parameters.
240//
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300241// Returns the ternary node created, or one of trueExpression and falseExpression if the expression
242// could be folded.
243TIntermTyped *TIntermediate::AddTernarySelection(TIntermTyped *cond,
244 TIntermTyped *trueExpression,
245 TIntermTyped *falseExpression,
246 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000247{
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200248 // Note that the node resulting from here can be a constant union without being qualified as
249 // constant.
250 if (cond->getAsConstantUnion())
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700251 {
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300252 TQualifier resultQualifier =
253 TIntermTernary::DetermineQualifier(cond, trueExpression, falseExpression);
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +0000254 if (cond->getAsConstantUnion()->getBConst(0))
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200255 {
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300256 trueExpression->getTypePointer()->setQualifier(resultQualifier);
257 return trueExpression;
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200258 }
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000259 else
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200260 {
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300261 falseExpression->getTypePointer()->setQualifier(resultQualifier);
262 return falseExpression;
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200263 }
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000264 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000265
Olli Etuahod0bad2c2016-09-09 18:01:16 +0300266 // Make a ternary node.
267 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000268 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000269
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000270 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000271}
272
Olli Etuahoa3a36662015-02-17 13:46:51 +0200273TIntermSwitch *TIntermediate::addSwitch(
274 TIntermTyped *init, TIntermAggregate *statementList, const TSourceLoc &line)
275{
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200276 TIntermSwitch *node = new TIntermSwitch(init, statementList);
277 node->setLine(line);
278
279 return node;
Olli Etuahoa3a36662015-02-17 13:46:51 +0200280}
281
282TIntermCase *TIntermediate::addCase(
283 TIntermTyped *condition, const TSourceLoc &line)
284{
Olli Etuaho3c1dfb52015-02-20 11:34:03 +0200285 TIntermCase *node = new TIntermCase(condition);
286 node->setLine(line);
287
288 return node;
Olli Etuahoa3a36662015-02-17 13:46:51 +0200289}
290
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000291//
292// Constant terminal nodes. Has a union that contains bool, float or int constants
293//
294// Returns the constant union node created.
295//
296
Olli Etuaho5c0e0232015-11-11 15:55:59 +0200297TIntermConstantUnion *TIntermediate::addConstantUnion(const TConstantUnion *constantUnion,
298 const TType &type,
299 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000300{
Jamie Madillb11e2482015-05-04 14:21:22 -0400301 TIntermConstantUnion *node = new TIntermConstantUnion(constantUnion, type);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000302 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000303
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000304 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000305}
306
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700307TIntermTyped *TIntermediate::addSwizzle(
308 TVectorFields &fields, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000309{
310
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700311 TIntermAggregate *node = new TIntermAggregate(EOpSequence);
Olli Etuaho3272a6d2016-08-29 17:54:50 +0300312 node->getTypePointer()->setQualifier(EvqConst);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000313
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000314 node->setLine(line);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700315 TIntermConstantUnion *constIntNode;
316 TIntermSequence *sequenceVector = node->getSequence();
Jamie Madill6ba6ead2015-05-04 14:21:21 -0400317 TConstantUnion *unionArray;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000318
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700319 for (int i = 0; i < fields.num; i++)
320 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -0400321 unionArray = new TConstantUnion[1];
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000322 unionArray->setIConst(fields.offsets[i]);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700323 constIntNode = addConstantUnion(
324 unionArray, TType(EbtInt, EbpUndefined, EvqConst), line);
325 sequenceVector->push_back(constIntNode);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000326 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000327
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000328 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000329}
330
331//
332// Create loop nodes.
333//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700334TIntermNode *TIntermediate::addLoop(
335 TLoopType type, TIntermNode *init, TIntermTyped *cond, TIntermTyped *expr,
336 TIntermNode *body, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000337{
Olli Etuaho7d7f8c42015-05-19 18:38:49 +0300338 TIntermNode *node = new TIntermLoop(type, init, cond, expr, ensureSequence(body));
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000339 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000340
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000341 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000342}
343
344//
345// Add branches.
346//
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700347TIntermBranch* TIntermediate::addBranch(
348 TOperator branchOp, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000349{
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000350 return addBranch(branchOp, 0, line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000351}
352
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700353TIntermBranch* TIntermediate::addBranch(
354 TOperator branchOp, TIntermTyped *expression, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000355{
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700356 TIntermBranch *node = new TIntermBranch(branchOp, expression);
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000357 node->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000358
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000359 return node;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000360}
361
362//
363// This is to be executed once the final root is put on top by the parsing
364// process.
365//
Olli Etuahof119a262016-08-19 15:54:22 +0300366TIntermAggregate *TIntermediate::PostProcess(TIntermNode *root)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000367{
Olli Etuaho43613b02015-08-04 11:02:21 +0300368 if (root == nullptr)
369 return nullptr;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000370
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000371 //
Olli Etuaho43613b02015-08-04 11:02:21 +0300372 // Finish off the top level sequence, if any
alokp@chromium.org2cf17712010-03-30 20:33:18 +0000373 //
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700374 TIntermAggregate *aggRoot = root->getAsAggregate();
Olli Etuaho43613b02015-08-04 11:02:21 +0300375 if (aggRoot != nullptr && aggRoot->getOp() == EOpNull)
376 {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000377 aggRoot->setOp(EOpSequence);
Olli Etuaho43613b02015-08-04 11:02:21 +0300378 }
379 else if (aggRoot == nullptr || aggRoot->getOp() != EOpSequence)
380 {
381 aggRoot = new TIntermAggregate(EOpSequence);
382 aggRoot->setLine(root->getLine());
383 aggRoot->getSequence()->push_back(root);
384 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000385
Olli Etuaho43613b02015-08-04 11:02:21 +0300386 return aggRoot;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000387}
Arun Patole274f0702015-05-05 13:33:30 +0530388
Olli Etuahof119a262016-08-19 15:54:22 +0300389TIntermTyped *TIntermediate::foldAggregateBuiltIn(TIntermAggregate *aggregate,
390 TDiagnostics *diagnostics)
Arun Patole274f0702015-05-05 13:33:30 +0530391{
Olli Etuahob43846e2015-06-02 18:18:57 +0300392 switch (aggregate->getOp())
Arun Patole274f0702015-05-05 13:33:30 +0530393 {
Olli Etuaho1d122782015-11-06 15:35:17 +0200394 case EOpAtan:
395 case EOpPow:
396 case EOpMod:
397 case EOpMin:
398 case EOpMax:
399 case EOpClamp:
400 case EOpMix:
401 case EOpStep:
402 case EOpSmoothStep:
403 case EOpMul:
404 case EOpOuterProduct:
405 case EOpLessThan:
406 case EOpLessThanEqual:
407 case EOpGreaterThan:
408 case EOpGreaterThanEqual:
409 case EOpVectorEqual:
410 case EOpVectorNotEqual:
411 case EOpDistance:
412 case EOpDot:
413 case EOpCross:
414 case EOpFaceForward:
415 case EOpReflect:
416 case EOpRefract:
Olli Etuahof119a262016-08-19 15:54:22 +0300417 return aggregate->fold(diagnostics);
Olli Etuaho1d122782015-11-06 15:35:17 +0200418 default:
419 // TODO: Add support for folding array constructors
420 if (aggregate->isConstructor() && !aggregate->isArray())
421 {
Olli Etuahof119a262016-08-19 15:54:22 +0300422 return aggregate->fold(diagnostics);
Olli Etuaho1d122782015-11-06 15:35:17 +0200423 }
424 // Constant folding not supported for the built-in.
425 return nullptr;
Arun Patole274f0702015-05-05 13:33:30 +0530426 }
Arun Patole274f0702015-05-05 13:33:30 +0530427}