blob: 9676016b9c13741625e67fe810e91df9065f5f7f [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
Jamie Madill6b9cb252013-10-17 10:45:47 -04007#include "compiler/translator/ParseContext.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +00008
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00009#include <stdarg.h>
apatrick@chromium.org8187fa82010-06-15 22:09:28 +000010#include <stdio.h>
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000011
daniel@transgaming.comb401a922012-10-26 18:58:24 +000012#include "compiler/preprocessor/SourceLocation.h"
Dmitry Skiba01971112015-07-10 14:54:00 -040013#include "compiler/translator/Cache.h"
Olli Etuahoac5274d2015-02-20 10:19:08 +020014#include "compiler/translator/glslang.h"
15#include "compiler/translator/ValidateSwitch.h"
Olli Etuahob0c645e2015-05-12 14:25:36 +030016#include "compiler/translator/ValidateGlobalInitializer.h"
Olli Etuaho37ad4742015-04-27 13:18:50 +030017#include "compiler/translator/util.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000018
alokp@chromium.org8b851c62012-06-15 16:25:11 +000019///////////////////////////////////////////////////////////////////////
20//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000021// Sub- vector and matrix fields
22//
23////////////////////////////////////////////////////////////////////////
24
25//
26// Look at a '.' field selector string and change it into offsets
27// for a vector.
28//
Jamie Madillb98c3a82015-07-23 14:26:04 -040029bool TParseContext::parseVectorFields(const TString &compString,
30 int vecSize,
31 TVectorFields &fields,
Arun Patole7e7e68d2015-05-22 12:02:25 +053032 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000033{
Jamie Madillb98c3a82015-07-23 14:26:04 -040034 fields.num = (int)compString.size();
Arun Patole7e7e68d2015-05-22 12:02:25 +053035 if (fields.num > 4)
36 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000037 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000038 return false;
39 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000040
Jamie Madillb98c3a82015-07-23 14:26:04 -040041 enum
42 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000043 exyzw,
44 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +000045 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000046 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000047
Arun Patole7e7e68d2015-05-22 12:02:25 +053048 for (int i = 0; i < fields.num; ++i)
49 {
50 switch (compString[i])
51 {
Jamie Madillb98c3a82015-07-23 14:26:04 -040052 case 'x':
53 fields.offsets[i] = 0;
54 fieldSet[i] = exyzw;
55 break;
56 case 'r':
57 fields.offsets[i] = 0;
58 fieldSet[i] = ergba;
59 break;
60 case 's':
61 fields.offsets[i] = 0;
62 fieldSet[i] = estpq;
63 break;
64 case 'y':
65 fields.offsets[i] = 1;
66 fieldSet[i] = exyzw;
67 break;
68 case 'g':
69 fields.offsets[i] = 1;
70 fieldSet[i] = ergba;
71 break;
72 case 't':
73 fields.offsets[i] = 1;
74 fieldSet[i] = estpq;
75 break;
76 case 'z':
77 fields.offsets[i] = 2;
78 fieldSet[i] = exyzw;
79 break;
80 case 'b':
81 fields.offsets[i] = 2;
82 fieldSet[i] = ergba;
83 break;
84 case 'p':
85 fields.offsets[i] = 2;
86 fieldSet[i] = estpq;
87 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053088
Jamie Madillb98c3a82015-07-23 14:26:04 -040089 case 'w':
90 fields.offsets[i] = 3;
91 fieldSet[i] = exyzw;
92 break;
93 case 'a':
94 fields.offsets[i] = 3;
95 fieldSet[i] = ergba;
96 break;
97 case 'q':
98 fields.offsets[i] = 3;
99 fieldSet[i] = estpq;
100 break;
101 default:
102 error(line, "illegal vector field selection", compString.c_str());
103 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000104 }
105 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000106
Arun Patole7e7e68d2015-05-22 12:02:25 +0530107 for (int i = 0; i < fields.num; ++i)
108 {
109 if (fields.offsets[i] >= vecSize)
110 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400111 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000112 return false;
113 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000114
Arun Patole7e7e68d2015-05-22 12:02:25 +0530115 if (i > 0)
116 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400117 if (fieldSet[i] != fieldSet[i - 1])
Arun Patole7e7e68d2015-05-22 12:02:25 +0530118 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400119 error(line, "illegal - vector component fields not from the same set",
120 compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000121 return false;
122 }
123 }
124 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000125
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000126 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000127}
128
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000129///////////////////////////////////////////////////////////////////////
130//
131// Errors
132//
133////////////////////////////////////////////////////////////////////////
134
135//
136// Track whether errors have occurred.
137//
138void TParseContext::recover()
139{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000140}
141
142//
143// Used by flex/bison to output all syntax and parsing errors.
144//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530145void TParseContext::error(const TSourceLoc &loc,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400146 const char *reason,
147 const char *token,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530148 const char *extraInfo)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000149{
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000150 pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -0400151 srcLoc.file = loc.first_file;
152 srcLoc.line = loc.first_line;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400153 mDiagnostics.writeInfo(pp::Diagnostics::PP_ERROR, srcLoc, reason, token, extraInfo);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000154}
155
Arun Patole7e7e68d2015-05-22 12:02:25 +0530156void TParseContext::warning(const TSourceLoc &loc,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400157 const char *reason,
158 const char *token,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530159 const char *extraInfo)
160{
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000161 pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -0400162 srcLoc.file = loc.first_file;
163 srcLoc.line = loc.first_line;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400164 mDiagnostics.writeInfo(pp::Diagnostics::PP_WARNING, srcLoc, reason, token, extraInfo);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000165}
166
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200167void TParseContext::outOfRangeError(bool isError,
168 const TSourceLoc &loc,
169 const char *reason,
170 const char *token,
171 const char *extraInfo)
172{
173 if (isError)
174 {
175 error(loc, reason, token, extraInfo);
176 recover();
177 }
178 else
179 {
180 warning(loc, reason, token, extraInfo);
181 }
182}
183
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000184//
185// Same error message for all places assignments don't work.
186//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530187void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000188{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000189 std::stringstream extraInfoStream;
190 extraInfoStream << "cannot convert from '" << right << "' to '" << left << "'";
191 std::string extraInfo = extraInfoStream.str();
192 error(line, "", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000193}
194
195//
196// Same error message for all places unary operations don't work.
197//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530198void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000199{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000200 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400201 extraInfoStream << "no operation '" << op << "' exists that takes an operand of type "
202 << operand << " (or there is no acceptable conversion)";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000203 std::string extraInfo = extraInfoStream.str();
204 error(line, " wrong operand type", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000205}
206
207//
208// Same error message for all binary operations don't work.
209//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400210void TParseContext::binaryOpError(const TSourceLoc &line,
211 const char *op,
212 TString left,
213 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000214{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000215 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400216 extraInfoStream << "no operation '" << op << "' exists that takes a left-hand operand of type '"
217 << left << "' and a right operand of type '" << right
218 << "' (or there is no acceptable conversion)";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000219 std::string extraInfo = extraInfoStream.str();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530220 error(line, " wrong operand types ", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000221}
222
Jamie Madillb98c3a82015-07-23 14:26:04 -0400223bool TParseContext::precisionErrorCheck(const TSourceLoc &line,
224 TPrecision precision,
225 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530226{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400227 if (!mChecksPrecisionErrors)
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000228 return false;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400229 switch (type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530230 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400231 case EbtFloat:
232 if (precision == EbpUndefined)
233 {
234 error(line, "No precision specified for (float)", "");
235 return true;
236 }
237 break;
238 case EbtInt:
239 if (precision == EbpUndefined)
240 {
241 error(line, "No precision specified (int)", "");
242 return true;
243 }
244 break;
245 default:
246 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000247 }
248 return false;
249}
250
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000251//
252// Both test and if necessary, spit out an error, to see if the node is really
253// an l-value that can be operated on this way.
254//
255// Returns true if the was an error.
256//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530257bool TParseContext::lValueErrorCheck(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000258{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400259 TIntermSymbol *symNode = node->getAsSymbolNode();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530260 TIntermBinary *binaryNode = node->getAsBinaryNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000261
Arun Patole7e7e68d2015-05-22 12:02:25 +0530262 if (binaryNode)
263 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000264 bool errorReturn;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000265
Jamie Madillb98c3a82015-07-23 14:26:04 -0400266 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530267 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400268 case EOpIndexDirect:
269 case EOpIndexIndirect:
270 case EOpIndexDirectStruct:
271 case EOpIndexDirectInterfaceBlock:
272 return lValueErrorCheck(line, op, binaryNode->getLeft());
273 case EOpVectorSwizzle:
274 errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft());
275 if (!errorReturn)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530276 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400277 int offset[4] = {0, 0, 0, 0};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000278
Jamie Madillb98c3a82015-07-23 14:26:04 -0400279 TIntermTyped *rightNode = binaryNode->getRight();
280 TIntermAggregate *aggrNode = rightNode->getAsAggregate();
281
282 for (TIntermSequence::iterator p = aggrNode->getSequence()->begin();
283 p != aggrNode->getSequence()->end(); p++)
284 {
285 int value = (*p)->getAsTyped()->getAsConstantUnion()->getIConst(0);
286 offset[value]++;
287 if (offset[value] > 1)
288 {
289 error(line, " l-value of swizzle cannot have duplicate components", op);
290
291 return true;
292 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000293 }
294 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000295
Jamie Madillb98c3a82015-07-23 14:26:04 -0400296 return errorReturn;
297 default:
298 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000299 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000300 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000301
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000302 return true;
303 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000304
Arun Patole7e7e68d2015-05-22 12:02:25 +0530305 const char *symbol = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000306 if (symNode != 0)
307 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000308
Arun Patole7e7e68d2015-05-22 12:02:25 +0530309 const char *message = 0;
310 switch (node->getQualifier())
311 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400312 case EvqConst:
313 message = "can't modify a const";
314 break;
315 case EvqConstReadOnly:
316 message = "can't modify a const";
317 break;
318 case EvqAttribute:
319 message = "can't modify an attribute";
320 break;
321 case EvqFragmentIn:
322 message = "can't modify an input";
323 break;
324 case EvqVertexIn:
325 message = "can't modify an input";
326 break;
327 case EvqUniform:
328 message = "can't modify a uniform";
329 break;
330 case EvqVaryingIn:
331 message = "can't modify a varying";
332 break;
333 case EvqFragCoord:
334 message = "can't modify gl_FragCoord";
335 break;
336 case EvqFrontFacing:
337 message = "can't modify gl_FrontFacing";
338 break;
339 case EvqPointCoord:
340 message = "can't modify gl_PointCoord";
341 break;
342 default:
343 //
344 // Type that can't be written to?
345 //
346 if (node->getBasicType() == EbtVoid)
347 {
348 message = "can't modify void";
349 }
350 if (IsSampler(node->getBasicType()))
351 {
352 message = "can't modify a sampler";
353 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000354 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000355
Arun Patole7e7e68d2015-05-22 12:02:25 +0530356 if (message == 0 && binaryNode == 0 && symNode == 0)
357 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000358 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000359
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000360 return true;
361 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000362
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000363 //
364 // Everything else is okay, no error.
365 //
366 if (message == 0)
367 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000368
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000369 //
370 // If we get here, we have an error and a message.
371 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530372 if (symNode)
373 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000374 std::stringstream extraInfoStream;
375 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
376 std::string extraInfo = extraInfoStream.str();
377 error(line, " l-value required", op, extraInfo.c_str());
378 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530379 else
380 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000381 std::stringstream extraInfoStream;
382 extraInfoStream << "(" << message << ")";
383 std::string extraInfo = extraInfoStream.str();
384 error(line, " l-value required", op, extraInfo.c_str());
385 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000386
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000387 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000388}
389
390//
391// Both test, and if necessary spit out an error, to see if the node is really
392// a constant.
393//
394// Returns true if the was an error.
395//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530396bool TParseContext::constErrorCheck(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000397{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000398 if (node->getQualifier() == EvqConst)
399 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000400
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000401 error(node->getLine(), "constant expression required", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000402
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000403 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000404}
405
406//
407// Both test, and if necessary spit out an error, to see if the node is really
408// an integer.
409//
410// Returns true if the was an error.
411//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530412bool TParseContext::integerErrorCheck(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000413{
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000414 if (node->isScalarInt())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000415 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000416
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000417 error(node->getLine(), "integer expression required", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000418
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000419 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000420}
421
422//
423// Both test, and if necessary spit out an error, to see if we are currently
424// globally scoped.
425//
426// Returns true if the was an error.
427//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530428bool TParseContext::globalErrorCheck(const TSourceLoc &line, bool global, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000429{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000430 if (global)
431 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000432
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000433 error(line, "only allowed at global scope", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000434
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000435 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000436}
437
438//
439// For now, keep it simple: if it starts "gl_", it's reserved, independent
440// of scope. Except, if the symbol table is at the built-in push-level,
441// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000442// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
443// webgl shader.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000444//
445// Returns true if there was an error.
446//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530447bool TParseContext::reservedErrorCheck(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000448{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530449 static const char *reservedErrMsg = "reserved built-in name";
450 if (!symbolTable.atBuiltInLevel())
451 {
452 if (identifier.compare(0, 3, "gl_") == 0)
453 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000454 error(line, reservedErrMsg, "gl_");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000455 return true;
456 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530457 if (IsWebGLBasedSpec(mShaderSpec))
458 {
459 if (identifier.compare(0, 6, "webgl_") == 0)
460 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000461 error(line, reservedErrMsg, "webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000462 return true;
463 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530464 if (identifier.compare(0, 7, "_webgl_") == 0)
465 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000466 error(line, reservedErrMsg, "_webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000467 return true;
468 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530469 if (mShaderSpec == SH_CSS_SHADERS_SPEC && identifier.compare(0, 4, "css_") == 0)
470 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000471 error(line, reservedErrMsg, "css_");
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000472 return true;
473 }
alokp@chromium.org613ef312010-07-21 18:54:22 +0000474 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530475 if (identifier.find("__") != TString::npos)
476 {
477 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400478 "identifiers containing two consecutive underscores (__) are reserved as "
479 "possible future keywords",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530480 identifier.c_str());
daniel@transgaming.combeadd5d2012-04-12 02:35:31 +0000481 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000482 }
483 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000484
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000485 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000486}
487
488//
489// Make sure there is enough data provided to the constructor to build
490// something of the type of the constructor. Also returns the type of
491// the constructor.
492//
493// Returns true if there was an error in construction.
494//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400495bool TParseContext::constructorErrorCheck(const TSourceLoc &line,
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200496 TIntermNode *argumentsNode,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400497 TFunction &function,
498 TOperator op,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530499 TType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000500{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000501 *type = function.getReturnType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000502
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000503 bool constructingMatrix = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400504 switch (op)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530505 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400506 case EOpConstructMat2:
507 case EOpConstructMat2x3:
508 case EOpConstructMat2x4:
509 case EOpConstructMat3x2:
510 case EOpConstructMat3:
511 case EOpConstructMat3x4:
512 case EOpConstructMat4x2:
513 case EOpConstructMat4x3:
514 case EOpConstructMat4:
515 constructingMatrix = true;
516 break;
517 default:
518 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000519 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000520
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000521 //
522 // Note: It's okay to have too many components available, but not okay to have unused
523 // arguments. 'full' will go to true when enough args have been seen. If we loop
524 // again, there is an extra argument, so 'overfull' will become true.
525 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000526
Jamie Madillb98c3a82015-07-23 14:26:04 -0400527 size_t size = 0;
528 bool constType = true;
529 bool full = false;
530 bool overFull = false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000531 bool matrixInMatrix = false;
532 bool arrayArg = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530533 for (size_t i = 0; i < function.getParamCount(); ++i)
534 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700535 const TConstParameter &param = function.getParam(i);
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000536 size += param.type->getObjectSize();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530537
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000538 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000539 matrixInMatrix = true;
540 if (full)
541 overFull = true;
542 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
543 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000544 if (param.type->getQualifier() != EvqConst)
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000545 constType = false;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000546 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000547 arrayArg = true;
548 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530549
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000550 if (constType)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000551 type->setQualifier(EvqConst);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000552
Olli Etuaho376f1b52015-04-13 13:23:41 +0300553 if (type->isArray())
554 {
555 if (type->isUnsizedArray())
556 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700557 type->setArraySize(static_cast<int>(function.getParamCount()));
Olli Etuaho376f1b52015-04-13 13:23:41 +0300558 }
559 else if (static_cast<size_t>(type->getArraySize()) != function.getParamCount())
560 {
561 error(line, "array constructor needs one argument per array element", "constructor");
562 return true;
563 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000564 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000565
Arun Patole7e7e68d2015-05-22 12:02:25 +0530566 if (arrayArg && op != EOpConstructStruct)
567 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000568 error(line, "constructing from a non-dereferenced array", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000569 return true;
570 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000571
Arun Patole7e7e68d2015-05-22 12:02:25 +0530572 if (matrixInMatrix && !type->isArray())
573 {
574 if (function.getParamCount() != 1)
575 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400576 error(line, "constructing matrix from matrix can only take one argument",
577 "constructor");
578 return true;
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000579 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000580 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000581
Arun Patole7e7e68d2015-05-22 12:02:25 +0530582 if (overFull)
583 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000584 error(line, "too many arguments", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000585 return true;
586 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530587
Jamie Madillb98c3a82015-07-23 14:26:04 -0400588 if (op == EOpConstructStruct && !type->isArray() &&
589 type->getStruct()->fields().size() != function.getParamCount())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530590 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400591 error(line,
592 "Number of constructor parameters does not match the number of structure fields",
593 "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000594 return true;
595 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000596
Arun Patole7e7e68d2015-05-22 12:02:25 +0530597 if (!type->isMatrix() || !matrixInMatrix)
598 {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000599 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
Arun Patole7e7e68d2015-05-22 12:02:25 +0530600 (op == EOpConstructStruct && size < type->getObjectSize()))
601 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000602 error(line, "not enough data provided for construction", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000603 return true;
604 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000605 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000606
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200607 if (argumentsNode == nullptr)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530608 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200609 error(line, "constructor does not have any arguments", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000610 return true;
611 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200612
613 TIntermAggregate *argumentsAgg = argumentsNode->getAsAggregate();
614 for (TIntermNode *&argNode : *argumentsAgg->getSequence())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530615 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200616 TIntermTyped *argTyped = argNode->getAsTyped();
617 ASSERT(argTyped != nullptr);
618 if (op != EOpConstructStruct && IsSampler(argTyped->getBasicType()))
619 {
620 error(line, "cannot convert a sampler", "constructor");
621 return true;
622 }
623 if (argTyped->getBasicType() == EbtVoid)
624 {
625 error(line, "cannot convert a void", "constructor");
626 return true;
627 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000628 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000629
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000630 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000631}
632
Jamie Madillb98c3a82015-07-23 14:26:04 -0400633// This function checks to see if a void variable has been declared and raise an error message for
634// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000635//
636// returns true in case of an error
637//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400638bool TParseContext::voidErrorCheck(const TSourceLoc &line,
639 const TString &identifier,
640 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000641{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300642 if (type == EbtVoid)
643 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000644 error(line, "illegal use of type 'void'", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000645 return true;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300646 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000647
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000648 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000649}
650
Jamie Madillb98c3a82015-07-23 14:26:04 -0400651// This function checks to see if the node (for the expression) contains a scalar boolean expression
652// or not
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000653//
654// returns true in case of an error
655//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530656bool TParseContext::boolErrorCheck(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000657{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530658 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector())
659 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000660 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000661 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530662 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000663
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000664 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000665}
666
Jamie Madillb98c3a82015-07-23 14:26:04 -0400667// This function checks to see if the node (for the expression) contains a scalar boolean expression
668// or not
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000669//
670// returns true in case of an error
671//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530672bool TParseContext::boolErrorCheck(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000673{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530674 if (pType.type != EbtBool || pType.isAggregate())
675 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000676 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000677 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530678 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000679
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000680 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000681}
682
Jamie Madillb98c3a82015-07-23 14:26:04 -0400683bool TParseContext::samplerErrorCheck(const TSourceLoc &line,
684 const TPublicType &pType,
685 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000686{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530687 if (pType.type == EbtStruct)
688 {
689 if (containsSampler(*pType.userDef))
690 {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000691 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530692
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000693 return true;
694 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530695
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000696 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530697 }
698 else if (IsSampler(pType.type))
699 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000700 error(line, reason, getBasicString(pType.type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000701
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000702 return true;
703 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000704
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000705 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000706}
707
Arun Patole7e7e68d2015-05-22 12:02:25 +0530708bool TParseContext::locationDeclaratorListCheck(const TSourceLoc &line, const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400709{
710 if (pType.layoutQualifier.location != -1)
711 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400712 error(line, "location must only be specified for a single input or output variable",
713 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400714 return true;
715 }
716
717 return false;
718}
719
Jamie Madillb98c3a82015-07-23 14:26:04 -0400720bool TParseContext::parameterSamplerErrorCheck(const TSourceLoc &line,
721 TQualifier qualifier,
722 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000723{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400724 if ((qualifier == EvqOut || qualifier == EvqInOut) && type.getBasicType() != EbtStruct &&
725 IsSampler(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530726 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000727 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000728 return true;
729 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000730
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000731 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000732}
733
Arun Patole7e7e68d2015-05-22 12:02:25 +0530734bool TParseContext::containsSampler(const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000735{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000736 if (IsSampler(type.getBasicType()))
737 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000738
Arun Patole7e7e68d2015-05-22 12:02:25 +0530739 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock())
740 {
741 const TFieldList &fields = type.getStruct()->fields();
742 for (unsigned int i = 0; i < fields.size(); ++i)
743 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400744 if (containsSampler(*fields[i]->type()))
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000745 return true;
746 }
747 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000748
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000749 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000750}
751
752//
753// Do size checking for an array type's size.
754//
755// Returns true if there was an error.
756//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530757bool TParseContext::arraySizeErrorCheck(const TSourceLoc &line, TIntermTyped *expr, int &size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000758{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530759 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000760
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200761 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
762 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
763 // fold as array size.
764 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000765 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000766 error(line, "array size must be a constant integer expression", "");
Olli Etuahoe7847b02015-03-16 11:56:12 +0200767 size = 1;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000768 return true;
769 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000770
Nicolas Capens906744a2014-06-06 15:18:07 -0400771 unsigned int unsignedSize = 0;
772
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000773 if (constant->getBasicType() == EbtUInt)
774 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400775 unsignedSize = constant->getUConst(0);
Jamie Madillb98c3a82015-07-23 14:26:04 -0400776 size = static_cast<int>(unsignedSize);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000777 }
778 else
779 {
780 size = constant->getIConst(0);
781
Nicolas Capens906744a2014-06-06 15:18:07 -0400782 if (size < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000783 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400784 error(line, "array size must be non-negative", "");
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000785 size = 1;
786 return true;
787 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400788
789 unsignedSize = static_cast<unsigned int>(size);
790 }
791
792 if (size == 0)
793 {
794 error(line, "array size must be greater than zero", "");
795 size = 1;
796 return true;
797 }
798
799 // The size of arrays is restricted here to prevent issues further down the
800 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
801 // 4096 registers so this should be reasonable even for aggressively optimizable code.
802 const unsigned int sizeLimit = 65536;
803
804 if (unsignedSize > sizeLimit)
805 {
806 error(line, "array size too large", "");
807 size = 1;
808 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000809 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000810
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000811 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000812}
813
814//
815// See if this qualifier can be an array.
816//
817// Returns true if there is an error.
818//
Olli Etuaho3739d232015-04-08 12:23:44 +0300819bool TParseContext::arrayQualifierErrorCheck(const TSourceLoc &line, const TPublicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000820{
Olli Etuaho3739d232015-04-08 12:23:44 +0300821 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqVertexIn) ||
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400822 (type.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300823 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400824 error(line, "cannot declare arrays of this qualifier",
825 TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000826 return true;
827 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000828
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000829 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000830}
831
832//
833// See if this type can be an array.
834//
835// Returns true if there is an error.
836//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530837bool TParseContext::arrayTypeErrorCheck(const TSourceLoc &line, const TPublicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000838{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000839 //
840 // Can the type be an array?
841 //
Jamie Madill06145232015-05-13 13:10:01 -0400842 if (type.array)
843 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000844 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000845 return true;
846 }
Olli Etuahocc36b982015-07-10 14:14:18 +0300847 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
848 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
849 // 4.3.4).
850 if (mShaderVersion >= 300 && type.type == EbtStruct && sh::IsVarying(type.qualifier))
851 {
852 error(line, "cannot declare arrays of structs of this qualifier",
853 TType(type).getCompleteString().c_str());
854 return true;
855 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000856
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000857 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000858}
859
860//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000861// Enforce non-initializer type/qualifier rules.
862//
863// Returns true if there was an error.
864//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400865bool TParseContext::nonInitErrorCheck(const TSourceLoc &line,
866 const TString &identifier,
867 TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000868{
Olli Etuaho3739d232015-04-08 12:23:44 +0300869 ASSERT(type != nullptr);
870 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000871 {
872 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300873 type->qualifier = EvqTemporary;
874
875 // Generate informative error messages for ESSL1.
876 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400877 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000878 {
Arun Patole7e7e68d2015-05-22 12:02:25 +0530879 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400880 "structures containing arrays may not be declared constant since they cannot be "
881 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530882 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000883 }
884 else
885 {
886 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
887 }
888
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000889 return true;
890 }
Olli Etuaho376f1b52015-04-13 13:23:41 +0300891 if (type->isUnsizedArray())
892 {
893 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
894 return true;
895 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000896 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000897}
898
Olli Etuaho2935c582015-04-08 14:32:06 +0300899// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000900// and update the symbol table.
901//
Olli Etuaho2935c582015-04-08 14:32:06 +0300902// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000903//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400904bool TParseContext::declareVariable(const TSourceLoc &line,
905 const TString &identifier,
906 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +0300907 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000908{
Olli Etuaho2935c582015-04-08 14:32:06 +0300909 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000910
Olli Etuaho2935c582015-04-08 14:32:06 +0300911 bool needsReservedErrorCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000912
Olli Etuaho2935c582015-04-08 14:32:06 +0300913 // gl_LastFragData may be redeclared with a new precision qualifier
914 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
915 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400916 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
917 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho2935c582015-04-08 14:32:06 +0300918 if (type.getArraySize() == maxDrawBuffers->getConstPointer()->getIConst())
919 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400920 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +0300921 {
922 needsReservedErrorCheck = extensionErrorCheck(line, builtInSymbol->getExtension());
923 }
924 }
925 else
926 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400927 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
928 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +0300929 return false;
930 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000931 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000932
Olli Etuaho2935c582015-04-08 14:32:06 +0300933 if (needsReservedErrorCheck && reservedErrorCheck(line, identifier))
934 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000935
Olli Etuaho2935c582015-04-08 14:32:06 +0300936 (*variable) = new TVariable(&identifier, type);
937 if (!symbolTable.declare(*variable))
938 {
939 error(line, "redefinition", identifier.c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -0400940 *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +0300941 return false;
942 }
943
944 if (voidErrorCheck(line, identifier, type.getBasicType()))
945 return false;
946
947 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000948}
949
Jamie Madillb98c3a82015-07-23 14:26:04 -0400950bool TParseContext::paramErrorCheck(const TSourceLoc &line,
951 TQualifier qualifier,
952 TQualifier paramQualifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530953 TType *type)
954{
955 if (qualifier != EvqConst && qualifier != EvqTemporary)
956 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000957 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000958 return true;
959 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530960 if (qualifier == EvqConst && paramQualifier != EvqIn)
961 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400962 error(line, "qualifier not allowed with ", getQualifierString(qualifier),
963 getQualifierString(paramQualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000964 return true;
965 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000966
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000967 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000968 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000969 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000970 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000971
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000972 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000973}
974
Arun Patole7e7e68d2015-05-22 12:02:25 +0530975bool TParseContext::extensionErrorCheck(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000976{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400977 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000978 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +0530979 if (iter == extBehavior.end())
980 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000981 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000982 return true;
983 }
zmo@google.comf5450912011-09-09 01:37:19 +0000984 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +0530985 if (iter->second == EBhDisable || iter->second == EBhUndefined)
986 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000987 error(line, "extension", extension.c_str(), "is disabled");
988 return true;
989 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530990 if (iter->second == EBhWarn)
991 {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000992 warning(line, "extension", extension.c_str(), "is being used");
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000993 return false;
994 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000995
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000996 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000997}
998
Jamie Madillb98c3a82015-07-23 14:26:04 -0400999// These checks are common for all declarations starting a declarator list, and declarators that
1000// follow an empty declaration.
Olli Etuahofa33d582015-04-09 14:33:12 +03001001//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001002bool TParseContext::singleDeclarationErrorCheck(const TPublicType &publicType,
1003 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001004{
Olli Etuahofa33d582015-04-09 14:33:12 +03001005 switch (publicType.qualifier)
1006 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001007 case EvqVaryingIn:
1008 case EvqVaryingOut:
1009 case EvqAttribute:
1010 case EvqVertexIn:
1011 case EvqFragmentOut:
1012 if (publicType.type == EbtStruct)
1013 {
1014 error(identifierLocation, "cannot be used with a structure",
1015 getQualifierString(publicType.qualifier));
1016 return true;
1017 }
Olli Etuahofa33d582015-04-09 14:33:12 +03001018
Jamie Madillb98c3a82015-07-23 14:26:04 -04001019 default:
1020 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001021 }
1022
Jamie Madillb98c3a82015-07-23 14:26:04 -04001023 if (publicType.qualifier != EvqUniform &&
1024 samplerErrorCheck(identifierLocation, publicType, "samplers must be uniform"))
Olli Etuahofa33d582015-04-09 14:33:12 +03001025 {
Jamie Madilla5efff92013-06-06 11:56:47 -04001026 return true;
Olli Etuahofa33d582015-04-09 14:33:12 +03001027 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001028
1029 // check for layout qualifier issues
1030 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
1031
1032 if (layoutQualifier.matrixPacking != EmpUnspecified)
1033 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001034 error(identifierLocation, "layout qualifier",
1035 getMatrixPackingString(layoutQualifier.matrixPacking),
Olli Etuahofa33d582015-04-09 14:33:12 +03001036 "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -04001037 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001038 }
1039
1040 if (layoutQualifier.blockStorage != EbsUnspecified)
1041 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001042 error(identifierLocation, "layout qualifier",
1043 getBlockStorageString(layoutQualifier.blockStorage),
Olli Etuahofa33d582015-04-09 14:33:12 +03001044 "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -04001045 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001046 }
1047
Olli Etuahofa33d582015-04-09 14:33:12 +03001048 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut &&
1049 layoutLocationErrorCheck(identifierLocation, publicType.layoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001050 {
Jamie Madill51a53c72013-06-19 09:24:43 -04001051 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001052 }
1053
1054 return false;
1055}
1056
Jamie Madillb98c3a82015-07-23 14:26:04 -04001057bool TParseContext::layoutLocationErrorCheck(const TSourceLoc &location,
1058 const TLayoutQualifier &layoutQualifier)
Jamie Madilla5efff92013-06-06 11:56:47 -04001059{
1060 if (layoutQualifier.location != -1)
1061 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001062 error(location, "invalid layout qualifier:", "location",
1063 "only valid on program inputs and outputs");
Jamie Madilla5efff92013-06-06 11:56:47 -04001064 return true;
1065 }
1066
1067 return false;
1068}
1069
Jamie Madillb98c3a82015-07-23 14:26:04 -04001070bool TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate,
1071 TIntermAggregate *aggregate)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001072{
1073 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1074 {
1075 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1076 if (qual == EvqOut || qual == EvqInOut)
1077 {
1078 TIntermTyped *node = (*(aggregate->getSequence()))[i]->getAsTyped();
1079 if (lValueErrorCheck(node->getLine(), "assign", node))
1080 {
1081 error(node->getLine(),
Jamie Madillb98c3a82015-07-23 14:26:04 -04001082 "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error");
Olli Etuahob6e07a62015-02-16 12:22:10 +02001083 recover();
1084 return true;
1085 }
1086 }
1087 }
1088 return false;
1089}
1090
Jamie Madillb98c3a82015-07-23 14:26:04 -04001091void TParseContext::es3InvariantErrorCheck(const TQualifier qualifier,
1092 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001093{
1094 if (!sh::IsVaryingOut(qualifier) && qualifier != EvqFragmentOut)
1095 {
1096 error(invariantLocation, "Only out variables can be invariant.", "invariant");
1097 recover();
1098 }
1099}
1100
Arun Patole7e7e68d2015-05-22 12:02:25 +05301101bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001102{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001103 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001104 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1105 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001106}
1107
Arun Patole7e7e68d2015-05-22 12:02:25 +05301108bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001109{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001110 return ::IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001111}
1112
Jamie Madillb98c3a82015-07-23 14:26:04 -04001113void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1114 const char *extName,
1115 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001116{
1117 pp::SourceLocation srcLoc;
1118 srcLoc.file = loc.first_file;
1119 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001120 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001121}
1122
Jamie Madillb98c3a82015-07-23 14:26:04 -04001123void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1124 const char *name,
1125 const char *value,
1126 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001127{
1128 pp::SourceLocation srcLoc;
1129 srcLoc.file = loc.first_file;
1130 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001131 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001132}
1133
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001134/////////////////////////////////////////////////////////////////////////////////
1135//
1136// Non-Errors.
1137//
1138/////////////////////////////////////////////////////////////////////////////////
1139
Jamie Madill5c097022014-08-20 16:38:32 -04001140const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1141 const TString *name,
1142 const TSymbol *symbol)
1143{
1144 const TVariable *variable = NULL;
1145
1146 if (!symbol)
1147 {
1148 error(location, "undeclared identifier", name->c_str());
1149 recover();
1150 }
1151 else if (!symbol->isVariable())
1152 {
1153 error(location, "variable expected", name->c_str());
1154 recover();
1155 }
1156 else
1157 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001158 variable = static_cast<const TVariable *>(symbol);
Jamie Madill5c097022014-08-20 16:38:32 -04001159
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001160 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
Jamie Madill5c097022014-08-20 16:38:32 -04001161 !variable->getExtension().empty() &&
1162 extensionErrorCheck(location, variable->getExtension()))
1163 {
1164 recover();
1165 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001166
1167 // Reject shaders using both gl_FragData and gl_FragColor
1168 TQualifier qualifier = variable->getType().getQualifier();
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001169 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001170 {
1171 mUsesFragData = true;
1172 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001173 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001174 {
1175 mUsesFragColor = true;
1176 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001177 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1178 {
1179 mUsesSecondaryOutputs = true;
1180 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001181
1182 // This validation is not quite correct - it's only an error to write to
1183 // both FragData and FragColor. For simplicity, and because users shouldn't
1184 // be rewarded for reading from undefined varaibles, return an error
1185 // if they are both referenced, rather than assigned.
1186 if (mUsesFragData && mUsesFragColor)
1187 {
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001188 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1189 if (mUsesSecondaryOutputs)
1190 {
1191 errorMessage =
1192 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1193 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1194 }
1195 error(location, errorMessage, name->c_str());
Jamie Madill14e95b32015-05-07 10:10:41 -04001196 recover();
1197 }
Jamie Madill5c097022014-08-20 16:38:32 -04001198 }
1199
1200 if (!variable)
1201 {
1202 TType type(EbtFloat, EbpUndefined);
1203 TVariable *fakeVariable = new TVariable(name, type);
1204 symbolTable.declare(fakeVariable);
1205 variable = fakeVariable;
1206 }
1207
1208 return variable;
1209}
1210
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001211TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1212 const TString *name,
1213 const TSymbol *symbol)
1214{
1215 const TVariable *variable = getNamedVariable(location, name, symbol);
1216
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001217 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001218 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001219 const TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001220 return intermediate.addConstantUnion(constArray, variable->getType(), location);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001221 }
1222 else
1223 {
1224 return intermediate.addSymbol(variable->getUniqueId(), variable->getName(),
1225 variable->getType(), location);
1226 }
1227}
1228
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001229//
1230// Look up a function name in the symbol table, and make sure it is a function.
1231//
1232// Return the function symbol if found, otherwise 0.
1233//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001234const TFunction *TParseContext::findFunction(const TSourceLoc &line,
1235 TFunction *call,
1236 int inputShaderVersion,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301237 bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001238{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001239 // First find by unmangled name to check whether the function name has been
1240 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001241 // If a function is found, check for one with a matching argument list.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301242 const TSymbol *symbol = symbolTable.find(call->getName(), inputShaderVersion, builtIn);
1243 if (symbol == 0 || symbol->isFunction())
1244 {
Austin Kinross3ae64652015-01-26 15:51:39 -08001245 symbol = symbolTable.find(call->getMangledName(), inputShaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001246 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001247
Arun Patole7e7e68d2015-05-22 12:02:25 +05301248 if (symbol == 0)
1249 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001250 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001251 return 0;
1252 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001253
Arun Patole7e7e68d2015-05-22 12:02:25 +05301254 if (!symbol->isFunction())
1255 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001256 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001257 return 0;
1258 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001259
Jamie Madillb98c3a82015-07-23 14:26:04 -04001260 return static_cast<const TFunction *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001261}
1262
1263//
1264// Initializers show up in several places in the grammar. Have one set of
1265// code to handle them here.
1266//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001267// Returns true on error, false if no error
1268//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001269bool TParseContext::executeInitializer(const TSourceLoc &line,
1270 const TString &identifier,
1271 const TPublicType &pType,
1272 TIntermTyped *initializer,
1273 TIntermNode **intermNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001274{
Olli Etuahoe7847b02015-03-16 11:56:12 +02001275 ASSERT(intermNode != nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001276 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001277
Olli Etuaho2935c582015-04-08 14:32:06 +03001278 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001279 if (type.isUnsizedArray())
1280 {
1281 type.setArraySize(initializer->getArraySize());
1282 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001283 if (!declareVariable(line, identifier, type, &variable))
1284 {
1285 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001286 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001287
Olli Etuahob0c645e2015-05-12 14:25:36 +03001288 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001289 if (symbolTable.atGlobalLevel() &&
1290 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001291 {
1292 // Error message does not completely match behavior with ESSL 1.00, but
1293 // we want to steer developers towards only using constant expressions.
1294 error(line, "global variable initializers must be constant expressions", "=");
1295 return true;
1296 }
1297 if (globalInitWarning)
1298 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001299 warning(
1300 line,
1301 "global variable initializers should be constant expressions "
1302 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1303 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001304 }
1305
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001306 //
1307 // identifier must be of type constant, a global, or a temporary
1308 //
1309 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301310 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1311 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001312 error(line, " cannot initialize this type of qualifier ",
1313 variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001314 return true;
1315 }
1316 //
1317 // test for and propagate constant
1318 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001319
Arun Patole7e7e68d2015-05-22 12:02:25 +05301320 if (qualifier == EvqConst)
1321 {
1322 if (qualifier != initializer->getType().getQualifier())
1323 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001324 std::stringstream extraInfoStream;
1325 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1326 std::string extraInfo = extraInfoStream.str();
1327 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001328 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001329 return true;
1330 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301331 if (type != initializer->getType())
1332 {
1333 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001334 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001335 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001336 return true;
1337 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001338
1339 // Save the constant folded value to the variable if possible. For example array
1340 // initializers are not folded, since that way copying the array literal to multiple places
1341 // in the shader is avoided.
1342 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1343 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301344 if (initializer->getAsConstantUnion())
1345 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001346 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001347 *intermNode = nullptr;
1348 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301349 }
1350 else if (initializer->getAsSymbolNode())
1351 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001352 const TSymbol *symbol =
1353 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1354 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001355
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001356 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001357 if (constArray)
1358 {
1359 variable->shareConstPointer(constArray);
1360 *intermNode = nullptr;
1361 return false;
1362 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001363 }
1364 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001365
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001366 TIntermSymbol *intermSymbol = intermediate.addSymbol(
1367 variable->getUniqueId(), variable->getName(), variable->getType(), line);
1368 *intermNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1369 if (*intermNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001370 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001371 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1372 return true;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001373 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001374
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001375 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001376}
1377
Jamie Madillb98c3a82015-07-23 14:26:04 -04001378TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier,
1379 bool invariant,
1380 TLayoutQualifier layoutQualifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301381 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001382{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001383 TPublicType returnType = typeSpecifier;
1384 returnType.qualifier = qualifier;
1385 returnType.invariant = invariant;
Jamie Madilla5efff92013-06-06 11:56:47 -04001386 returnType.layoutQualifier = layoutQualifier;
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001387
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001388 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001389 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001390 if (typeSpecifier.array)
1391 {
1392 error(typeSpecifier.line, "not supported", "first-class array");
1393 recover();
1394 returnType.clearArrayness();
1395 }
1396
Jamie Madillb98c3a82015-07-23 14:26:04 -04001397 if (qualifier == EvqAttribute &&
1398 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001399 {
1400 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1401 recover();
1402 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001403
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001404 if ((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1405 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1406 {
1407 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1408 recover();
1409 }
1410 }
1411 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001412 {
Olli Etuahoabb0c382015-07-13 12:01:12 +03001413 if (!layoutQualifier.isEmpty())
1414 {
1415 if (globalErrorCheck(typeSpecifier.line, symbolTable.atGlobalLevel(), "layout"))
1416 {
1417 recover();
1418 }
1419 }
Olli Etuahocc36b982015-07-10 14:14:18 +03001420 if (sh::IsVarying(qualifier) || qualifier == EvqVertexIn || qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001421 {
Olli Etuahocc36b982015-07-10 14:14:18 +03001422 es3InputOutputTypeCheck(qualifier, typeSpecifier, typeSpecifier.line);
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001423 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001424 }
1425
1426 return returnType;
1427}
1428
Olli Etuahocc36b982015-07-10 14:14:18 +03001429void TParseContext::es3InputOutputTypeCheck(const TQualifier qualifier,
1430 const TPublicType &type,
1431 const TSourceLoc &qualifierLocation)
1432{
1433 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
1434 if (type.type == EbtBool)
1435 {
1436 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
1437 recover();
1438 }
1439
1440 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
1441 switch (qualifier)
1442 {
1443 case EvqVertexIn:
1444 // ESSL 3.00 section 4.3.4
1445 if (type.array)
1446 {
1447 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
1448 recover();
1449 }
1450 // Vertex inputs with a struct type are disallowed in singleDeclarationErrorCheck
1451 return;
1452 case EvqFragmentOut:
1453 // ESSL 3.00 section 4.3.6
1454 if (type.isMatrix())
1455 {
1456 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
1457 recover();
1458 }
1459 // Fragment outputs with a struct type are disallowed in singleDeclarationErrorCheck
1460 return;
1461 default:
1462 break;
1463 }
1464
1465 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
1466 // restrictions.
1467 bool typeContainsIntegers =
1468 (type.type == EbtInt || type.type == EbtUInt || type.isStructureContainingType(EbtInt) ||
1469 type.isStructureContainingType(EbtUInt));
1470 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
1471 {
1472 error(qualifierLocation, "must use 'flat' interpolation here",
1473 getQualifierString(qualifier));
1474 recover();
1475 }
1476
1477 if (type.type == EbtStruct)
1478 {
1479 // ESSL 3.00 sections 4.3.4 and 4.3.6.
1480 // These restrictions are only implied by the ESSL 3.00 spec, but
1481 // the ESSL 3.10 spec lists these restrictions explicitly.
1482 if (type.array)
1483 {
1484 error(qualifierLocation, "cannot be an array of structures",
1485 getQualifierString(qualifier));
1486 recover();
1487 }
1488 if (type.isStructureContainingArrays())
1489 {
1490 error(qualifierLocation, "cannot be a structure containing an array",
1491 getQualifierString(qualifier));
1492 recover();
1493 }
1494 if (type.isStructureContainingType(EbtStruct))
1495 {
1496 error(qualifierLocation, "cannot be a structure containing a structure",
1497 getQualifierString(qualifier));
1498 recover();
1499 }
1500 if (type.isStructureContainingType(EbtBool))
1501 {
1502 error(qualifierLocation, "cannot be a structure containing a bool",
1503 getQualifierString(qualifier));
1504 recover();
1505 }
1506 }
1507}
1508
Olli Etuahofa33d582015-04-09 14:33:12 +03001509TIntermAggregate *TParseContext::parseSingleDeclaration(TPublicType &publicType,
1510 const TSourceLoc &identifierOrTypeLocation,
1511 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001512{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001513 TIntermSymbol *symbol =
1514 intermediate.addSymbol(0, identifier, TType(publicType), identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001515
Olli Etuahobab4c082015-04-24 16:38:49 +03001516 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03001517
Olli Etuahobab4c082015-04-24 16:38:49 +03001518 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1519
1520 if (emptyDeclaration)
1521 {
1522 if (publicType.isUnsizedArray())
1523 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001524 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1525 // error. It is assumed that this applies to empty declarations as well.
1526 error(identifierOrTypeLocation, "empty array declaration needs to specify a size",
1527 identifier.c_str());
Olli Etuahobab4c082015-04-24 16:38:49 +03001528 }
1529 }
1530 else
Jamie Madill60ed9812013-06-06 11:56:46 -04001531 {
Olli Etuahofa33d582015-04-09 14:33:12 +03001532 if (singleDeclarationErrorCheck(publicType, identifierOrTypeLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001533 recover();
1534
Olli Etuaho376f1b52015-04-13 13:23:41 +03001535 if (nonInitErrorCheck(identifierOrTypeLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001536 recover();
1537
Olli Etuaho2935c582015-04-08 14:32:06 +03001538 TVariable *variable = nullptr;
Olli Etuahofa33d582015-04-09 14:33:12 +03001539 if (!declareVariable(identifierOrTypeLocation, identifier, TType(publicType), &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001540 recover();
1541
1542 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001543 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001544 }
1545
Olli Etuahoe7847b02015-03-16 11:56:12 +02001546 return intermediate.makeAggregate(symbol, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001547}
1548
Olli Etuahoe7847b02015-03-16 11:56:12 +02001549TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1550 const TSourceLoc &identifierLocation,
1551 const TString &identifier,
1552 const TSourceLoc &indexLocation,
1553 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001554{
Olli Etuahofa33d582015-04-09 14:33:12 +03001555 mDeferredSingleDeclarationErrorCheck = false;
1556
1557 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001558 recover();
1559
Olli Etuaho376f1b52015-04-13 13:23:41 +03001560 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001561 recover();
1562
Jamie Madillb98c3a82015-07-23 14:26:04 -04001563 if (arrayTypeErrorCheck(indexLocation, publicType) ||
1564 arrayQualifierErrorCheck(indexLocation, publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001565 {
1566 recover();
1567 }
1568
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001569 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001570
1571 int size;
1572 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1573 {
1574 recover();
1575 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001576 // Make the type an array even if size check failed.
1577 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1578 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001579
Olli Etuaho2935c582015-04-08 14:32:06 +03001580 TVariable *variable = nullptr;
1581 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001582 recover();
1583
Olli Etuahoe7847b02015-03-16 11:56:12 +02001584 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001585 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001586 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001587
Olli Etuahoe7847b02015-03-16 11:56:12 +02001588 return intermediate.makeAggregate(symbol, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001589}
1590
Jamie Madill06145232015-05-13 13:10:01 -04001591TIntermAggregate *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001592 const TSourceLoc &identifierLocation,
1593 const TString &identifier,
1594 const TSourceLoc &initLocation,
1595 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001596{
Olli Etuahofa33d582015-04-09 14:33:12 +03001597 mDeferredSingleDeclarationErrorCheck = false;
1598
1599 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001600 recover();
1601
Olli Etuahoe7847b02015-03-16 11:56:12 +02001602 TIntermNode *intermNode = nullptr;
1603 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04001604 {
1605 //
1606 // Build intermediate representation
1607 //
Olli Etuahoe7847b02015-03-16 11:56:12 +02001608 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001609 }
1610 else
1611 {
1612 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001613 return nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001614 }
1615}
1616
Jamie Madillb98c3a82015-07-23 14:26:04 -04001617TIntermAggregate *TParseContext::parseSingleArrayInitDeclaration(
1618 TPublicType &publicType,
1619 const TSourceLoc &identifierLocation,
1620 const TString &identifier,
1621 const TSourceLoc &indexLocation,
1622 TIntermTyped *indexExpression,
1623 const TSourceLoc &initLocation,
1624 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001625{
1626 mDeferredSingleDeclarationErrorCheck = false;
1627
1628 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1629 recover();
1630
Jamie Madillb98c3a82015-07-23 14:26:04 -04001631 if (arrayTypeErrorCheck(indexLocation, publicType) ||
1632 arrayQualifierErrorCheck(indexLocation, publicType))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001633 {
1634 recover();
1635 }
1636
1637 TPublicType arrayType(publicType);
1638
Olli Etuaho376f1b52015-04-13 13:23:41 +03001639 int size = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001640 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
1641 // the initializer.
1642 if (indexExpression != nullptr &&
1643 arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001644 {
1645 recover();
1646 }
1647 // Make the type an array even if size check failed.
1648 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1649 arrayType.setArraySize(size);
1650
1651 // initNode will correspond to the whole of "type b[n] = initializer".
1652 TIntermNode *initNode = nullptr;
1653 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1654 {
1655 return initNode ? intermediate.makeAggregate(initNode, initLocation) : nullptr;
1656 }
1657 else
1658 {
1659 recover();
1660 return nullptr;
1661 }
1662}
1663
Olli Etuahoe7847b02015-03-16 11:56:12 +02001664TIntermAggregate *TParseContext::parseInvariantDeclaration(const TSourceLoc &invariantLoc,
Jamie Madill47e3ec02014-08-20 16:38:33 -04001665 const TSourceLoc &identifierLoc,
1666 const TString *identifier,
1667 const TSymbol *symbol)
1668{
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001669 // invariant declaration
Jamie Madill47e3ec02014-08-20 16:38:33 -04001670 if (globalErrorCheck(invariantLoc, symbolTable.atGlobalLevel(), "invariant varying"))
1671 {
1672 recover();
1673 }
1674
1675 if (!symbol)
1676 {
1677 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
1678 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001679 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001680 }
1681 else
1682 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001683 const TString kGlFrontFacing("gl_FrontFacing");
1684 if (*identifier == kGlFrontFacing)
1685 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001686 error(identifierLoc, "identifier should not be declared as invariant",
1687 identifier->c_str());
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001688 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001689 return nullptr;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001690 }
Jamie Madill2c433252014-12-03 12:36:54 -05001691 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001692 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1693 ASSERT(variable);
1694 const TType &type = variable->getType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04001695 TIntermSymbol *intermSymbol =
1696 intermediate.addSymbol(variable->getUniqueId(), *identifier, type, identifierLoc);
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001697
1698 TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc);
1699 aggregate->setOp(EOpInvariantDeclaration);
1700 return aggregate;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001701 }
1702}
1703
Jamie Madillb98c3a82015-07-23 14:26:04 -04001704TIntermAggregate *TParseContext::parseDeclarator(TPublicType &publicType,
1705 TIntermAggregate *aggregateDeclaration,
1706 const TSourceLoc &identifierLocation,
1707 const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001708{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001709 // If the declaration starting this declarator list was empty (example: int,), some checks were
1710 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001711 if (mDeferredSingleDeclarationErrorCheck)
1712 {
1713 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1714 recover();
1715 mDeferredSingleDeclarationErrorCheck = false;
1716 }
1717
Jamie Madill0bd18df2013-06-20 11:55:52 -04001718 if (locationDeclaratorListCheck(identifierLocation, publicType))
1719 recover();
1720
Olli Etuaho376f1b52015-04-13 13:23:41 +03001721 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001722 recover();
1723
Olli Etuaho2935c582015-04-08 14:32:06 +03001724 TVariable *variable = nullptr;
1725 if (!declareVariable(identifierLocation, identifier, TType(publicType), &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001726 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001727
Jamie Madillb98c3a82015-07-23 14:26:04 -04001728 TIntermSymbol *symbol =
1729 intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001730 if (variable && symbol)
Jamie Madill502d66f2013-06-20 11:55:52 -04001731 symbol->setId(variable->getUniqueId());
1732
Olli Etuahoe7847b02015-03-16 11:56:12 +02001733 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001734}
1735
Jamie Madillb98c3a82015-07-23 14:26:04 -04001736TIntermAggregate *TParseContext::parseArrayDeclarator(TPublicType &publicType,
1737 TIntermAggregate *aggregateDeclaration,
1738 const TSourceLoc &identifierLocation,
1739 const TString &identifier,
1740 const TSourceLoc &arrayLocation,
1741 TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001742{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001743 // If the declaration starting this declarator list was empty (example: int,), some checks were
1744 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001745 if (mDeferredSingleDeclarationErrorCheck)
1746 {
1747 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1748 recover();
1749 mDeferredSingleDeclarationErrorCheck = false;
1750 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001751
Jamie Madill0bd18df2013-06-20 11:55:52 -04001752 if (locationDeclaratorListCheck(identifierLocation, publicType))
1753 recover();
1754
Olli Etuaho376f1b52015-04-13 13:23:41 +03001755 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001756 recover();
1757
Jamie Madillb98c3a82015-07-23 14:26:04 -04001758 if (arrayTypeErrorCheck(arrayLocation, publicType) ||
1759 arrayQualifierErrorCheck(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001760 {
1761 recover();
1762 }
Olli Etuaho93a90fd2015-04-07 18:14:07 +03001763 else
Jamie Madill502d66f2013-06-20 11:55:52 -04001764 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001765 TType arrayType = TType(publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04001766 int size;
1767 if (arraySizeErrorCheck(arrayLocation, indexExpression, size))
Olli Etuahoe7847b02015-03-16 11:56:12 +02001768 {
Jamie Madill502d66f2013-06-20 11:55:52 -04001769 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001770 }
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001771 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001772
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001773 TVariable *variable = nullptr;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001774 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001775 recover();
Jamie Madill502d66f2013-06-20 11:55:52 -04001776
Jamie Madillb98c3a82015-07-23 14:26:04 -04001777 TIntermSymbol *symbol =
1778 intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001779 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001780 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001781
1782 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001783 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001784
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001785 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001786}
1787
Jamie Madillb98c3a82015-07-23 14:26:04 -04001788TIntermAggregate *TParseContext::parseInitDeclarator(const TPublicType &publicType,
1789 TIntermAggregate *aggregateDeclaration,
1790 const TSourceLoc &identifierLocation,
1791 const TString &identifier,
1792 const TSourceLoc &initLocation,
1793 TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001794{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001795 // If the declaration starting this declarator list was empty (example: int,), some checks were
1796 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001797 if (mDeferredSingleDeclarationErrorCheck)
1798 {
1799 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1800 recover();
1801 mDeferredSingleDeclarationErrorCheck = false;
1802 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001803
Jamie Madill0bd18df2013-06-20 11:55:52 -04001804 if (locationDeclaratorListCheck(identifierLocation, publicType))
1805 recover();
1806
Olli Etuahoe7847b02015-03-16 11:56:12 +02001807 TIntermNode *intermNode = nullptr;
1808 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04001809 {
1810 //
1811 // build the intermediate representation
1812 //
1813 if (intermNode)
1814 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001815 return intermediate.growAggregate(aggregateDeclaration, intermNode, initLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001816 }
1817 else
1818 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001819 return aggregateDeclaration;
Jamie Madill502d66f2013-06-20 11:55:52 -04001820 }
1821 }
1822 else
1823 {
1824 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001825 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001826 }
1827}
1828
Jamie Madill06145232015-05-13 13:10:01 -04001829TIntermAggregate *TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001830 TIntermAggregate *aggregateDeclaration,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301831 const TSourceLoc &identifierLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001832 const TString &identifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301833 const TSourceLoc &indexLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001834 TIntermTyped *indexExpression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001835 const TSourceLoc &initLocation,
1836 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001837{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001838 // If the declaration starting this declarator list was empty (example: int,), some checks were
1839 // not performed.
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001840 if (mDeferredSingleDeclarationErrorCheck)
1841 {
1842 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1843 recover();
1844 mDeferredSingleDeclarationErrorCheck = false;
1845 }
1846
1847 if (locationDeclaratorListCheck(identifierLocation, publicType))
1848 recover();
1849
Jamie Madillb98c3a82015-07-23 14:26:04 -04001850 if (arrayTypeErrorCheck(indexLocation, publicType) ||
1851 arrayQualifierErrorCheck(indexLocation, publicType))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001852 {
1853 recover();
1854 }
1855
1856 TPublicType arrayType(publicType);
1857
Olli Etuaho376f1b52015-04-13 13:23:41 +03001858 int size = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001859 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
1860 // the initializer.
1861 if (indexExpression != nullptr &&
1862 arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001863 {
1864 recover();
1865 }
1866 // Make the type an array even if size check failed.
1867 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1868 arrayType.setArraySize(size);
1869
1870 // initNode will correspond to the whole of "b[n] = initializer".
1871 TIntermNode *initNode = nullptr;
1872 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1873 {
1874 if (initNode)
1875 {
1876 return intermediate.growAggregate(aggregateDeclaration, initNode, initLocation);
1877 }
1878 else
1879 {
1880 return aggregateDeclaration;
1881 }
1882 }
1883 else
1884 {
1885 recover();
1886 return nullptr;
1887 }
1888}
1889
Jamie Madilla295edf2013-06-06 11:56:48 -04001890void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1891{
1892 if (typeQualifier.qualifier != EvqUniform)
1893 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001894 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
1895 "global layout must be uniform");
Jamie Madilla295edf2013-06-06 11:56:48 -04001896 recover();
1897 return;
1898 }
1899
1900 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1901 ASSERT(!layoutQualifier.isEmpty());
1902
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001903 if (mShaderVersion < 300)
Jamie Madilla295edf2013-06-06 11:56:48 -04001904 {
1905 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1906 recover();
1907 return;
1908 }
1909
1910 if (layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1911 {
1912 recover();
1913 return;
1914 }
1915
Jamie Madill099c0f32013-06-20 11:55:52 -04001916 if (layoutQualifier.matrixPacking != EmpUnspecified)
1917 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001918 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04001919 }
1920
Geoff Langc6856732014-02-11 09:38:55 -05001921 if (layoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04001922 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001923 mDefaultBlockStorage = layoutQualifier.blockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04001924 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001925}
1926
Jamie Madill185fb402015-06-12 15:48:48 -04001927void TParseContext::parseFunctionPrototype(const TSourceLoc &location,
1928 TFunction *function,
1929 TIntermAggregate **aggregateOut)
1930{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001931 const TSymbol *builtIn =
1932 symbolTable.findBuiltIn(function->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04001933
1934 if (builtIn)
1935 {
1936 error(location, "built-in functions cannot be redefined", function->getName().c_str());
1937 recover();
1938 }
1939
Jamie Madillb98c3a82015-07-23 14:26:04 -04001940 TFunction *prevDec =
1941 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Jamie Madill185fb402015-06-12 15:48:48 -04001942 //
1943 // Note: 'prevDec' could be 'function' if this is the first time we've seen function
1944 // as it would have just been put in the symbol table. Otherwise, we're looking up
1945 // an earlier occurance.
1946 //
1947 if (prevDec->isDefined())
1948 {
1949 // Then this function already has a body.
1950 error(location, "function already has a body", function->getName().c_str());
1951 recover();
1952 }
1953 prevDec->setDefined();
1954 //
1955 // Overload the unique ID of the definition to be the same unique ID as the declaration.
1956 // Eventually we will probably want to have only a single definition and just swap the
1957 // arguments to be the definition's arguments.
1958 //
1959 function->setUniqueId(prevDec->getUniqueId());
1960
1961 // Raise error message if main function takes any parameters or return anything other than void
1962 if (function->getName() == "main")
1963 {
1964 if (function->getParamCount() > 0)
1965 {
1966 error(location, "function cannot take any parameter(s)", function->getName().c_str());
1967 recover();
1968 }
1969 if (function->getReturnType().getBasicType() != EbtVoid)
1970 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001971 error(location, "", function->getReturnType().getBasicString(),
1972 "main function cannot return a value");
Jamie Madill185fb402015-06-12 15:48:48 -04001973 recover();
1974 }
1975 }
1976
1977 //
1978 // Remember the return type for later checking for RETURN statements.
1979 //
1980 setCurrentFunctionType(&(prevDec->getReturnType()));
1981 setFunctionReturnsValue(false);
1982
1983 //
1984 // Insert parameters into the symbol table.
1985 // If the parameter has no name, it's not an error, just don't insert it
1986 // (could be used for unused args).
1987 //
1988 // Also, accumulate the list of parameters into the HIL, so lower level code
1989 // knows where to find parameters.
1990 //
1991 TIntermAggregate *paramNodes = new TIntermAggregate;
1992 for (size_t i = 0; i < function->getParamCount(); i++)
1993 {
1994 const TConstParameter &param = function->getParam(i);
1995 if (param.name != 0)
1996 {
1997 TVariable *variable = new TVariable(param.name, *param.type);
1998 //
1999 // Insert the parameters with name in the symbol table.
2000 //
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002001 if (!symbolTable.declare(variable))
2002 {
Jamie Madill185fb402015-06-12 15:48:48 -04002003 error(location, "redefinition", variable->getName().c_str());
2004 recover();
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002005 paramNodes = intermediate.growAggregate(
2006 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
2007 continue;
Jamie Madill185fb402015-06-12 15:48:48 -04002008 }
2009
2010 //
2011 // Add the parameter to the HIL
2012 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002013 TIntermSymbol *symbol = intermediate.addSymbol(
2014 variable->getUniqueId(), variable->getName(), variable->getType(), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002015
2016 paramNodes = intermediate.growAggregate(paramNodes, symbol, location);
2017 }
2018 else
2019 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002020 paramNodes = intermediate.growAggregate(
2021 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002022 }
2023 }
2024 intermediate.setAggregateOperator(paramNodes, EOpParameters, location);
2025 *aggregateOut = paramNodes;
2026 setLoopNestingLevel(0);
2027}
2028
Jamie Madillb98c3a82015-07-23 14:26:04 -04002029TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002030{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002031 //
2032 // Multiple declarations of the same function are allowed.
2033 //
2034 // If this is a definition, the definition production code will check for redefinitions
2035 // (we don't know at this point if it's a definition or not).
2036 //
2037 // Redeclarations are allowed. But, return types and parameter qualifiers must match.
2038 //
2039 TFunction *prevDec =
2040 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
2041 if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002042 {
2043 if (prevDec->getReturnType() != function->getReturnType())
2044 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002045 error(location, "overloaded functions must have the same return type",
Jamie Madill185fb402015-06-12 15:48:48 -04002046 function->getReturnType().getBasicString());
2047 recover();
2048 }
2049 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2050 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002051 if (prevDec->getParam(i).type->getQualifier() !=
2052 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002053 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002054 error(location, "overloaded functions must have the same parameter qualifiers",
Jamie Madill185fb402015-06-12 15:48:48 -04002055 function->getParam(i).type->getQualifierString());
2056 recover();
2057 }
2058 }
2059 }
2060
2061 //
2062 // Check for previously declared variables using the same name.
2063 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002064 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002065 if (prevSym)
2066 {
2067 if (!prevSym->isFunction())
2068 {
2069 error(location, "redefinition", function->getName().c_str(), "function");
2070 recover();
2071 }
2072 }
2073 else
2074 {
2075 // Insert the unmangled name to detect potential future redefinition as a variable.
Jamie Madillb98c3a82015-07-23 14:26:04 -04002076 TFunction *newFunction =
2077 new TFunction(NewPoolTString(function->getName().c_str()), &function->getReturnType());
Jamie Madill185fb402015-06-12 15:48:48 -04002078 symbolTable.getOuterLevel()->insertUnmangled(newFunction);
2079 }
2080
2081 // We're at the inner scope level of the function's arguments and body statement.
2082 // Add the function prototype to the surrounding scope instead.
2083 symbolTable.getOuterLevel()->insert(function);
2084
2085 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002086 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2087 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002088 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2089 //
2090 return function;
2091}
2092
Jamie Madill06145232015-05-13 13:10:01 -04002093TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002094{
Jamie Madill06145232015-05-13 13:10:01 -04002095 TPublicType publicType = publicTypeIn;
Olli Etuahobd163f62015-11-13 12:15:38 +02002096 if (publicType.isStructSpecifier)
2097 {
2098 error(publicType.line, "constructor can't be a structure definition",
2099 getBasicString(publicType.type));
2100 recover();
2101 }
2102
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002103 TOperator op = EOpNull;
2104 if (publicType.userDef)
2105 {
2106 op = EOpConstructStruct;
2107 }
2108 else
2109 {
2110 switch (publicType.type)
2111 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002112 case EbtFloat:
2113 if (publicType.isMatrix())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002114 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002115 switch (publicType.getCols())
Alexis Hetu07e57df2015-06-16 16:55:52 -04002116 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002117 case 2:
2118 switch (publicType.getRows())
2119 {
2120 case 2:
2121 op = EOpConstructMat2;
2122 break;
2123 case 3:
2124 op = EOpConstructMat2x3;
2125 break;
2126 case 4:
2127 op = EOpConstructMat2x4;
2128 break;
2129 }
2130 break;
2131 case 3:
2132 switch (publicType.getRows())
2133 {
2134 case 2:
2135 op = EOpConstructMat3x2;
2136 break;
2137 case 3:
2138 op = EOpConstructMat3;
2139 break;
2140 case 4:
2141 op = EOpConstructMat3x4;
2142 break;
2143 }
2144 break;
2145 case 4:
2146 switch (publicType.getRows())
2147 {
2148 case 2:
2149 op = EOpConstructMat4x2;
2150 break;
2151 case 3:
2152 op = EOpConstructMat4x3;
2153 break;
2154 case 4:
2155 op = EOpConstructMat4;
2156 break;
2157 }
2158 break;
Alexis Hetu07e57df2015-06-16 16:55:52 -04002159 }
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002160 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04002161 else
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002162 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002163 switch (publicType.getNominalSize())
2164 {
2165 case 1:
2166 op = EOpConstructFloat;
2167 break;
2168 case 2:
2169 op = EOpConstructVec2;
2170 break;
2171 case 3:
2172 op = EOpConstructVec3;
2173 break;
2174 case 4:
2175 op = EOpConstructVec4;
2176 break;
2177 }
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002178 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04002179 break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002180
Jamie Madillb98c3a82015-07-23 14:26:04 -04002181 case EbtInt:
2182 switch (publicType.getNominalSize())
2183 {
2184 case 1:
2185 op = EOpConstructInt;
2186 break;
2187 case 2:
2188 op = EOpConstructIVec2;
2189 break;
2190 case 3:
2191 op = EOpConstructIVec3;
2192 break;
2193 case 4:
2194 op = EOpConstructIVec4;
2195 break;
2196 }
2197 break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002198
Jamie Madillb98c3a82015-07-23 14:26:04 -04002199 case EbtUInt:
2200 switch (publicType.getNominalSize())
2201 {
2202 case 1:
2203 op = EOpConstructUInt;
2204 break;
2205 case 2:
2206 op = EOpConstructUVec2;
2207 break;
2208 case 3:
2209 op = EOpConstructUVec3;
2210 break;
2211 case 4:
2212 op = EOpConstructUVec4;
2213 break;
2214 }
2215 break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002216
Jamie Madillb98c3a82015-07-23 14:26:04 -04002217 case EbtBool:
2218 switch (publicType.getNominalSize())
2219 {
2220 case 1:
2221 op = EOpConstructBool;
2222 break;
2223 case 2:
2224 op = EOpConstructBVec2;
2225 break;
2226 case 3:
2227 op = EOpConstructBVec3;
2228 break;
2229 case 4:
2230 op = EOpConstructBVec4;
2231 break;
2232 }
2233 break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002234
Jamie Madillb98c3a82015-07-23 14:26:04 -04002235 default:
2236 break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002237 }
2238
2239 if (op == EOpNull)
2240 {
2241 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
2242 recover();
2243 publicType.type = EbtFloat;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002244 op = EOpConstructFloat;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002245 }
2246 }
2247
2248 TString tempString;
Dmitry Skiba7f17a502015-06-22 15:08:39 -07002249 const TType *type = new TType(publicType);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002250 return new TFunction(&tempString, type, op);
2251}
2252
Jamie Madillb98c3a82015-07-23 14:26:04 -04002253// This function is used to test for the correctness of the parameters passed to various constructor
2254// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002255//
2256// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
2257//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002258TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments,
2259 TType *type,
2260 TOperator op,
2261 TFunction *fnCall,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302262 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002263{
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002264 TIntermAggregate *constructor = arguments->getAsAggregate();
2265 ASSERT(constructor != nullptr);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002266
Olli Etuahof40319e2015-03-10 14:33:00 +02002267 if (type->isArray())
2268 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002269 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
2270 // the array.
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002271 TIntermSequence *args = constructor->getSequence();
Olli Etuahof40319e2015-03-10 14:33:00 +02002272 for (size_t i = 0; i < args->size(); i++)
2273 {
2274 const TType &argType = (*args)[i]->getAsTyped()->getType();
2275 // It has already been checked that the argument is not an array.
2276 ASSERT(!argType.isArray());
2277 if (!argType.sameElementType(*type))
2278 {
2279 error(line, "Array constructor argument has an incorrect type", "Error");
2280 recover();
2281 return nullptr;
2282 }
2283 }
2284 }
2285 else if (op == EOpConstructStruct)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002286 {
2287 const TFieldList &fields = type->getStruct()->fields();
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002288 TIntermSequence *args = constructor->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002289
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002290 for (size_t i = 0; i < fields.size(); i++)
2291 {
Nicolas Capensffd73872014-08-21 13:49:16 -04002292 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002293 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002294 error(line, "Structure constructor arguments do not match structure fields",
2295 "Error");
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002296 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002297
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002298 return 0;
2299 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002300 }
2301 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002302
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002303 // Turn the argument list itself into a constructor
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002304 constructor->setOp(op);
2305 constructor->setLine(line);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002306 ASSERT(constructor->isConstructor());
2307
2308 // Need to set type before setPrecisionFromChildren() because bool doesn't have precision.
2309 constructor->setType(*type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002310
Olli Etuaho21203702014-11-13 16:16:21 +02002311 // Structs should not be precision qualified, the individual members may be.
2312 // Built-in types on the other hand should be precision qualified.
2313 if (op != EOpConstructStruct)
2314 {
2315 constructor->setPrecisionFromChildren();
2316 type->setPrecision(constructor->getPrecision());
2317 }
2318
Olli Etuaho1d122782015-11-06 15:35:17 +02002319 TIntermTyped *constConstructor = intermediate.foldAggregateBuiltIn(constructor);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002320 if (constConstructor)
2321 {
2322 return constConstructor;
2323 }
2324
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002325 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002326}
2327
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002328//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002329// This function returns the tree representation for the vector field(s) being accessed from contant
2330// vector.
2331// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a
2332// contant node is returned, else an aggregate node is returned (for v.xy). The input to this
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002333// function could either be the symbol node or it could be the intermediate tree representation of
2334// accessing fields in a constant structure or column of a constant matrix.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002335//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002336TIntermTyped *TParseContext::addConstVectorNode(TVectorFields &fields,
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002337 TIntermConstantUnion *node,
2338 const TSourceLoc &line,
2339 bool outOfRangeIndexIsError)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002340{
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002341 const TConstantUnion *unionArray = node->getUnionArrayPointer();
2342 ASSERT(unionArray);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002343
Arun Patole7e7e68d2015-05-22 12:02:25 +05302344 TConstantUnion *constArray = new TConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002345
Arun Patole7e7e68d2015-05-22 12:02:25 +05302346 for (int i = 0; i < fields.num; i++)
2347 {
2348 if (fields.offsets[i] >= node->getType().getNominalSize())
2349 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002350 std::stringstream extraInfoStream;
2351 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
2352 std::string extraInfo = extraInfoStream.str();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002353 outOfRangeError(outOfRangeIndexIsError, line, "", "[", extraInfo.c_str());
2354 fields.offsets[i] = node->getType().getNominalSize() - 1;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002355 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302356
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002357 constArray[i] = unionArray[fields.offsets[i]];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302358 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002359 return intermediate.addConstantUnion(constArray, node->getType(), line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002360}
2361
2362//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002363// This function returns the column being accessed from a constant matrix. The values are retrieved
2364// from the symbol table and parse-tree is built for a vector (each column of a matrix is a vector).
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002365// The input to the function could either be a symbol node (m[0] where m is a constant matrix)that
2366// represents a constant matrix or it could be the tree representation of the constant matrix
2367// (s.m1[0] where s is a constant structure)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002368//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002369TIntermTyped *TParseContext::addConstMatrixNode(int index,
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002370 TIntermConstantUnion *node,
2371 const TSourceLoc &line,
2372 bool outOfRangeIndexIsError)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002373{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302374 if (index >= node->getType().getCols())
2375 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002376 std::stringstream extraInfoStream;
2377 extraInfoStream << "matrix field selection out of range '" << index << "'";
2378 std::string extraInfo = extraInfoStream.str();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002379 outOfRangeError(outOfRangeIndexIsError, line, "", "[", extraInfo.c_str());
2380 index = node->getType().getCols() - 1;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002381 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002382
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002383 const TConstantUnion *unionArray = node->getUnionArrayPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002384 int size = node->getType().getCols();
2385 return intermediate.addConstantUnion(&unionArray[size * index], node->getType(), line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002386}
2387
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002388//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002389// This function returns an element of an array accessed from a constant array. The values are
2390// retrieved from the symbol table and parse-tree is built for the type of the element. The input
Arun Patole7e7e68d2015-05-22 12:02:25 +05302391// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
Jamie Madillb98c3a82015-07-23 14:26:04 -04002392// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a
2393// constant structure)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002394//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002395TIntermTyped *TParseContext::addConstArrayNode(int index,
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002396 TIntermConstantUnion *node,
2397 const TSourceLoc &line,
2398 bool outOfRangeIndexIsError)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002399{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002400 TType arrayElementType = node->getType();
2401 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002402
Arun Patole7e7e68d2015-05-22 12:02:25 +05302403 if (index >= node->getType().getArraySize())
2404 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002405 std::stringstream extraInfoStream;
2406 extraInfoStream << "array field selection out of range '" << index << "'";
2407 std::string extraInfo = extraInfoStream.str();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002408 outOfRangeError(outOfRangeIndexIsError, line, "", "[", extraInfo.c_str());
2409 index = node->getType().getArraySize() - 1;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002410 }
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002411 size_t arrayElementSize = arrayElementType.getObjectSize();
2412 const TConstantUnion *unionArray = node->getUnionArrayPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002413 return intermediate.addConstantUnion(&unionArray[arrayElementSize * index], node->getType(),
2414 line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002415}
2416
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002417//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002418// This function returns the value of a particular field inside a constant structure from the symbol
2419// table.
2420// If there is an embedded/nested struct, it appropriately calls addConstStructNested or
2421// addConstStructFromAggr function and returns the parse-tree with the values of the embedded/nested
2422// struct.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002423//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002424TIntermTyped *TParseContext::addConstStruct(const TString &identifier,
2425 TIntermTyped *node,
2426 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002427{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302428 const TFieldList &fields = node->getType().getStruct()->fields();
Jamie Madillb98c3a82015-07-23 14:26:04 -04002429 size_t instanceSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002430
Arun Patole7e7e68d2015-05-22 12:02:25 +05302431 for (size_t index = 0; index < fields.size(); ++index)
2432 {
2433 if (fields[index]->name() == identifier)
2434 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002435 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302436 }
2437 else
2438 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002439 instanceSize += fields[index]->type()->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002440 }
2441 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002442
Jamie Madill94bf7f22013-07-08 13:31:15 -04002443 TIntermTyped *typedNode;
2444 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
Arun Patole7e7e68d2015-05-22 12:02:25 +05302445 if (tempConstantNode)
2446 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002447 const TConstantUnion *constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002448
Jamie Madillb98c3a82015-07-23 14:26:04 -04002449 // type will be changed in the calling function
2450 typedNode = intermediate.addConstantUnion(constArray + instanceSize,
2451 tempConstantNode->getType(), line);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302452 }
2453 else
2454 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002455 error(line, "Cannot offset into the structure", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002456 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002457
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002458 return 0;
2459 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002460
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002461 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002462}
2463
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002464//
2465// Interface/uniform blocks
2466//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002467TIntermAggregate *TParseContext::addInterfaceBlock(const TPublicType &typeQualifier,
2468 const TSourceLoc &nameLine,
2469 const TString &blockName,
2470 TFieldList *fieldList,
2471 const TString *instanceName,
2472 const TSourceLoc &instanceLine,
2473 TIntermTyped *arrayIndex,
2474 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002475{
2476 if (reservedErrorCheck(nameLine, blockName))
2477 recover();
2478
2479 if (typeQualifier.qualifier != EvqUniform)
2480 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302481 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
2482 "interface blocks must be uniform");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002483 recover();
2484 }
2485
Jamie Madill099c0f32013-06-20 11:55:52 -04002486 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
2487 if (layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002488 {
2489 recover();
2490 }
2491
Jamie Madill099c0f32013-06-20 11:55:52 -04002492 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2493 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002494 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002495 }
2496
Jamie Madill1566ef72013-06-20 11:55:54 -04002497 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2498 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002499 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002500 }
2501
Arun Patole7e7e68d2015-05-22 12:02:25 +05302502 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2503 if (!symbolTable.declare(blockNameSymbol))
2504 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002505 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
2506 recover();
2507 }
2508
Jamie Madill98493dd2013-07-08 14:39:03 -04002509 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302510 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2511 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002512 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302513 TType *fieldType = field->type();
2514 if (IsSampler(fieldType->getBasicType()))
2515 {
2516 error(field->line(), "unsupported type", fieldType->getBasicString(),
2517 "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002518 recover();
2519 }
2520
Jamie Madill98493dd2013-07-08 14:39:03 -04002521 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002522 switch (qualifier)
2523 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002524 case EvqGlobal:
2525 case EvqUniform:
2526 break;
2527 default:
2528 error(field->line(), "invalid qualifier on interface block member",
2529 getQualifierString(qualifier));
2530 recover();
2531 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002532 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002533
2534 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002535 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
2536 if (layoutLocationErrorCheck(field->line(), fieldLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002537 {
2538 recover();
2539 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002540
Jamie Madill98493dd2013-07-08 14:39:03 -04002541 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002542 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002543 error(field->line(), "invalid layout qualifier:",
2544 getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04002545 recover();
2546 }
2547
Jamie Madill98493dd2013-07-08 14:39:03 -04002548 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002549 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002550 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002551 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002552 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002553 {
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002554 warning(field->line(), "extraneous layout qualifier:",
Jamie Madillb98c3a82015-07-23 14:26:04 -04002555 getMatrixPackingString(fieldLayoutQualifier.matrixPacking),
2556 "only has an effect on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04002557 }
2558
Jamie Madill98493dd2013-07-08 14:39:03 -04002559 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002560 }
2561
Jamie Madill98493dd2013-07-08 14:39:03 -04002562 // add array index
2563 int arraySize = 0;
2564 if (arrayIndex != NULL)
2565 {
2566 if (arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize))
2567 recover();
2568 }
2569
Jamie Madillb98c3a82015-07-23 14:26:04 -04002570 TInterfaceBlock *interfaceBlock =
2571 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2572 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
2573 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002574
2575 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04002576 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002577
Jamie Madill98493dd2013-07-08 14:39:03 -04002578 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002579 {
2580 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002581 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2582 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002583 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302584 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002585
2586 // set parent pointer of the field variable
2587 fieldType->setInterfaceBlock(interfaceBlock);
2588
Arun Patole7e7e68d2015-05-22 12:02:25 +05302589 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002590 fieldVariable->setQualifier(typeQualifier.qualifier);
2591
Arun Patole7e7e68d2015-05-22 12:02:25 +05302592 if (!symbolTable.declare(fieldVariable))
2593 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002594 error(field->line(), "redefinition", field->name().c_str(),
2595 "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002596 recover();
2597 }
2598 }
2599 }
2600 else
2601 {
Olli Etuahoe0f623a2015-07-10 11:58:30 +03002602 if (reservedErrorCheck(instanceLine, *instanceName))
2603 recover();
2604
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002605 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302606 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002607 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002608
Arun Patole7e7e68d2015-05-22 12:02:25 +05302609 if (!symbolTable.declare(instanceTypeDef))
2610 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002611 error(instanceLine, "redefinition", instanceName->c_str(),
2612 "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002613 recover();
2614 }
2615
Jamie Madillb98c3a82015-07-23 14:26:04 -04002616 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002617 symbolName = instanceTypeDef->getName();
2618 }
2619
Jamie Madillb98c3a82015-07-23 14:26:04 -04002620 TIntermAggregate *aggregate = intermediate.makeAggregate(
2621 intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line),
2622 nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002623 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04002624
2625 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002626 return aggregate;
2627}
2628
Arun Patole7e7e68d2015-05-22 12:02:25 +05302629bool TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002630{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002631 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002632
2633 // Embedded structure definitions are not supported per GLSL ES spec.
2634 // They aren't allowed in GLSL either, but we need to detect this here
2635 // so we don't rely on the GLSL compiler to catch it.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302636 if (mStructNestingLevel > 1)
2637 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002638 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002639 return true;
2640 }
2641
2642 return false;
2643}
2644
2645void TParseContext::exitStructDeclaration()
2646{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002647 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002648}
2649
Jamie Madillb98c3a82015-07-23 14:26:04 -04002650namespace
2651{
kbr@chromium.org476541f2011-10-27 21:14:51 +00002652const int kWebGLMaxStructNesting = 4;
2653
2654} // namespace
2655
Arun Patole7e7e68d2015-05-22 12:02:25 +05302656bool TParseContext::structNestingErrorCheck(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002657{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302658 if (!IsWebGLBasedSpec(mShaderSpec))
2659 {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002660 return false;
2661 }
2662
Arun Patole7e7e68d2015-05-22 12:02:25 +05302663 if (field.type()->getBasicType() != EbtStruct)
2664 {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002665 return false;
2666 }
2667
2668 // We're already inside a structure definition at this point, so add
2669 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302670 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2671 {
Jamie Madill41a49272014-03-18 16:10:13 -04002672 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002673 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
2674 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04002675 std::string reason = reasonStream.str();
2676 error(line, reason.c_str(), field.name().c_str(), "");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002677 return true;
2678 }
2679
2680 return false;
2681}
2682
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002683//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002684// Parse an array index expression
2685//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002686TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
2687 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302688 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002689{
2690 TIntermTyped *indexedExpression = NULL;
2691
2692 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2693 {
2694 if (baseExpression->getAsSymbolNode())
2695 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302696 error(location, " left of '[' is not of type array, matrix, or vector ",
2697 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002698 }
2699 else
2700 {
2701 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2702 }
2703 recover();
2704 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002705
Jamie Madill21c1e452014-12-29 11:33:41 -05002706 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2707
Olli Etuaho36b05142015-11-12 13:10:42 +02002708 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
2709 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
2710 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
2711 // index is a constant expression.
2712 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
2713 {
2714 if (baseExpression->isInterfaceBlock())
2715 {
2716 error(
2717 location, "", "[",
2718 "array indexes for interface blocks arrays must be constant integral expressions");
2719 recover();
2720 }
2721 else if (baseExpression->getQualifier() == EvqFragmentOut)
2722 {
2723 error(location, "", "[",
2724 "array indexes for fragment outputs must be constant integral expressions");
2725 recover();
2726 }
2727 }
2728
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002729 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002730 {
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002731 // If the index is not qualified as constant, the behavior in the spec is undefined. This
2732 // applies even if ANGLE has been able to constant fold it (ANGLE may constant fold
2733 // expressions that are not constant expressions). The most compatible way to handle this
2734 // case is to report a warning instead of an error and force the index to be in the
2735 // correct range.
2736 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Jamie Madill21c1e452014-12-29 11:33:41 -05002737 int index = indexConstantUnion->getIConst(0);
Jamie Madill7164cf42013-07-08 13:30:59 -04002738 if (index < 0)
2739 {
2740 std::stringstream infoStream;
2741 infoStream << index;
2742 std::string info = infoStream.str();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002743 outOfRangeError(outOfRangeIndexIsError, location, "negative index", info.c_str());
Jamie Madill7164cf42013-07-08 13:30:59 -04002744 index = 0;
2745 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002746 TIntermConstantUnion *baseConstantUnion = baseExpression->getAsConstantUnion();
2747 if (baseConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002748 {
2749 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002750 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002751 // constant folding for array indexing
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002752 indexedExpression =
2753 addConstArrayNode(index, baseConstantUnion, location, outOfRangeIndexIsError);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002754 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002755 else if (baseExpression->isVector())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002756 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002757 // constant folding for vector indexing
Jamie Madill7164cf42013-07-08 13:30:59 -04002758 TVectorFields fields;
2759 fields.num = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002760 fields.offsets[0] =
2761 index; // need to do it this way because v.xy sends fields integer array
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002762 indexedExpression =
2763 addConstVectorNode(fields, baseConstantUnion, location, outOfRangeIndexIsError);
Jamie Madill7164cf42013-07-08 13:30:59 -04002764 }
2765 else if (baseExpression->isMatrix())
2766 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002767 // constant folding for matrix indexing
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002768 indexedExpression =
2769 addConstMatrixNode(index, baseConstantUnion, location, outOfRangeIndexIsError);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002770 }
2771 }
2772 else
2773 {
Jamie Madillb11e2482015-05-04 14:21:22 -04002774 int safeIndex = -1;
2775
Jamie Madill7164cf42013-07-08 13:30:59 -04002776 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002777 {
Jamie Madill18464b52013-07-08 14:01:55 -04002778 if (index >= baseExpression->getType().getArraySize())
Jamie Madill7164cf42013-07-08 13:30:59 -04002779 {
2780 std::stringstream extraInfoStream;
2781 extraInfoStream << "array index out of range '" << index << "'";
2782 std::string extraInfo = extraInfoStream.str();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002783 outOfRangeError(outOfRangeIndexIsError, location, "", "[", extraInfo.c_str());
Jamie Madillb11e2482015-05-04 14:21:22 -04002784 safeIndex = baseExpression->getType().getArraySize() - 1;
Jamie Madill7164cf42013-07-08 13:30:59 -04002785 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302786 else if (baseExpression->getQualifier() == EvqFragData && index > 0 &&
2787 !isExtensionEnabled("GL_EXT_draw_buffers"))
Jamie Madill5d287f52013-07-12 15:38:19 -04002788 {
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002789 outOfRangeError(
2790 outOfRangeIndexIsError, location, "", "[",
2791 "array indexes for gl_FragData must be zero when GL_EXT_draw_buffers is "
2792 "disabled");
Jamie Madillb11e2482015-05-04 14:21:22 -04002793 safeIndex = 0;
Jamie Madill5d287f52013-07-12 15:38:19 -04002794 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002795 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302796 else if ((baseExpression->isVector() || baseExpression->isMatrix()) &&
Jamie Madillb98c3a82015-07-23 14:26:04 -04002797 baseExpression->getType().getNominalSize() <= index)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002798 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002799 std::stringstream extraInfoStream;
2800 extraInfoStream << "field selection out of range '" << index << "'";
2801 std::string extraInfo = extraInfoStream.str();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002802 outOfRangeError(outOfRangeIndexIsError, location, "", "[", extraInfo.c_str());
Jamie Madillb11e2482015-05-04 14:21:22 -04002803 safeIndex = baseExpression->getType().getNominalSize() - 1;
Jamie Madill46131a32013-06-20 11:55:50 -04002804 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002805
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002806 // Data of constant unions can't be changed, because it may be shared with other
2807 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
2808 // sanitized object.
Jamie Madillb11e2482015-05-04 14:21:22 -04002809 if (safeIndex != -1)
2810 {
2811 TConstantUnion *safeConstantUnion = new TConstantUnion();
2812 safeConstantUnion->setIConst(safeIndex);
2813 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
2814 }
2815
Jamie Madillb98c3a82015-07-23 14:26:04 -04002816 indexedExpression =
2817 intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002818 }
2819 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002820 else
2821 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002822 indexedExpression =
2823 intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
Jamie Madill7164cf42013-07-08 13:30:59 -04002824 }
2825
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002826 if (indexedExpression == 0)
2827 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002828 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002829 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002830 indexedExpression =
2831 intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002832 }
2833 else if (baseExpression->isArray())
2834 {
Olli Etuahob3fbd862015-09-30 17:55:02 +03002835 TType indexedType = baseExpression->getType();
2836 indexedType.clearArrayness();
2837 indexedExpression->setType(indexedType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002838 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002839 else if (baseExpression->isMatrix())
2840 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002841 indexedExpression->setType(TType(baseExpression->getBasicType(),
Olli Etuahob3fbd862015-09-30 17:55:02 +03002842 baseExpression->getPrecision(), EvqTemporary,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002843 static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002844 }
2845 else if (baseExpression->isVector())
2846 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002847 indexedExpression->setType(
Olli Etuahob3fbd862015-09-30 17:55:02 +03002848 TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002849 }
2850 else
2851 {
2852 indexedExpression->setType(baseExpression->getType());
2853 }
2854
Olli Etuahob3fbd862015-09-30 17:55:02 +03002855 if (baseExpression->getType().getQualifier() == EvqConst &&
2856 indexExpression->getType().getQualifier() == EvqConst)
2857 {
2858 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2859 }
2860 else
2861 {
2862 indexedExpression->getTypePointer()->setQualifier(EvqTemporary);
2863 }
2864
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002865 return indexedExpression;
2866}
2867
Jamie Madillb98c3a82015-07-23 14:26:04 -04002868TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
2869 const TSourceLoc &dotLocation,
2870 const TString &fieldString,
2871 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002872{
2873 TIntermTyped *indexedExpression = NULL;
2874
2875 if (baseExpression->isArray())
2876 {
2877 error(fieldLocation, "cannot apply dot operator to an array", ".");
2878 recover();
2879 }
2880
2881 if (baseExpression->isVector())
2882 {
2883 TVectorFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002884 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields,
2885 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002886 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002887 fields.num = 1;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002888 fields.offsets[0] = 0;
2889 recover();
2890 }
2891
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002892 if (baseExpression->getAsConstantUnion())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002893 {
2894 // constant folding for vector fields
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002895 indexedExpression = addConstVectorNode(fields, baseExpression->getAsConstantUnion(),
2896 fieldLocation, true);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002897 }
2898 else
2899 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302900 TIntermTyped *index = intermediate.addSwizzle(fields, fieldLocation);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002901 indexedExpression =
2902 intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002903 }
2904 if (indexedExpression == nullptr)
2905 {
2906 recover();
2907 indexedExpression = baseExpression;
2908 }
2909 else
2910 {
2911 // Note that the qualifier set here will be corrected later.
Jamie Madillb98c3a82015-07-23 14:26:04 -04002912 indexedExpression->setType(TType(baseExpression->getBasicType(),
2913 baseExpression->getPrecision(), EvqTemporary,
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002914 (unsigned char)(fieldString).size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002915 }
2916 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002917 else if (baseExpression->getBasicType() == EbtStruct)
2918 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002919 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302920 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04002921 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002922 {
2923 error(dotLocation, "structure has no fields", "Internal Error");
2924 recover();
2925 indexedExpression = baseExpression;
2926 }
2927 else
2928 {
2929 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002930 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002931 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002932 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002933 {
2934 fieldFound = true;
2935 break;
2936 }
2937 }
2938 if (fieldFound)
2939 {
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002940 if (baseExpression->getAsConstantUnion())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002941 {
2942 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2943 if (indexedExpression == 0)
2944 {
2945 recover();
2946 indexedExpression = baseExpression;
2947 }
2948 else
2949 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002950 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002951 }
2952 }
2953 else
2954 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002955 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002956 unionArray->setIConst(i);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002957 TIntermTyped *index = intermediate.addConstantUnion(
2958 unionArray, *fields[i]->type(), fieldLocation);
2959 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression,
2960 index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002961 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002962 }
2963 }
2964 else
2965 {
2966 error(dotLocation, " no such field in structure", fieldString.c_str());
2967 recover();
2968 indexedExpression = baseExpression;
2969 }
2970 }
2971 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002972 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002973 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002974 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302975 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04002976 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002977 {
2978 error(dotLocation, "interface block has no fields", "Internal Error");
2979 recover();
2980 indexedExpression = baseExpression;
2981 }
2982 else
2983 {
2984 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002985 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002986 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002987 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002988 {
2989 fieldFound = true;
2990 break;
2991 }
2992 }
2993 if (fieldFound)
2994 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002995 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002996 unionArray->setIConst(i);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002997 TIntermTyped *index =
2998 intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
2999 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock,
3000 baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04003001 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003002 }
3003 else
3004 {
3005 error(dotLocation, " no such field in interface block", fieldString.c_str());
3006 recover();
3007 indexedExpression = baseExpression;
3008 }
3009 }
3010 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003011 else
3012 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003013 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003014 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03003015 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303016 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003017 }
3018 else
3019 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303020 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003021 " field selection requires structure, vector, or interface block on left hand "
3022 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303023 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003024 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003025 recover();
3026 indexedExpression = baseExpression;
3027 }
3028
Olli Etuahob1edc4f2015-11-02 17:20:03 +02003029 if (baseExpression->getQualifier() == EvqConst)
3030 {
3031 indexedExpression->getTypePointer()->setQualifier(EvqConst);
3032 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003033 else
3034 {
3035 indexedExpression->getTypePointer()->setQualifier(EvqTemporary);
3036 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02003037
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003038 return indexedExpression;
3039}
3040
Jamie Madillb98c3a82015-07-23 14:26:04 -04003041TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3042 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003043{
Jamie Madilla5efff92013-06-06 11:56:47 -04003044 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003045
Jamie Madillb98c3a82015-07-23 14:26:04 -04003046 qualifier.location = -1;
Jamie Madilla5efff92013-06-06 11:56:47 -04003047 qualifier.matrixPacking = EmpUnspecified;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003048 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003049
3050 if (qualifierType == "shared")
3051 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003052 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003053 }
3054 else if (qualifierType == "packed")
3055 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003056 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003057 }
3058 else if (qualifierType == "std140")
3059 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003060 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003061 }
3062 else if (qualifierType == "row_major")
3063 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003064 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003065 }
3066 else if (qualifierType == "column_major")
3067 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003068 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003069 }
3070 else if (qualifierType == "location")
3071 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003072 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
3073 "location requires an argument");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003074 recover();
3075 }
3076 else
3077 {
3078 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
3079 recover();
3080 }
3081
Jamie Madilla5efff92013-06-06 11:56:47 -04003082 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003083}
3084
Jamie Madillb98c3a82015-07-23 14:26:04 -04003085TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3086 const TSourceLoc &qualifierTypeLine,
3087 const TString &intValueString,
3088 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303089 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003090{
Jamie Madilla5efff92013-06-06 11:56:47 -04003091 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003092
Jamie Madillb98c3a82015-07-23 14:26:04 -04003093 qualifier.location = -1;
Jamie Madilla5efff92013-06-06 11:56:47 -04003094 qualifier.matrixPacking = EmpUnspecified;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003095 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003096
3097 if (qualifierType != "location")
3098 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303099 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
3100 "only location may have arguments");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003101 recover();
3102 }
3103 else
3104 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003105 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003106 if (intValue < 0)
3107 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003108 error(intValueLine, "out of range:", intValueString.c_str(),
3109 "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003110 recover();
3111 }
3112 else
3113 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003114 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003115 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003116 }
3117
Jamie Madilla5efff92013-06-06 11:56:47 -04003118 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003119}
3120
Jamie Madillb98c3a82015-07-23 14:26:04 -04003121TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
3122 TLayoutQualifier rightQualifier)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003123{
Jamie Madilla5efff92013-06-06 11:56:47 -04003124 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003125
Jamie Madilla5efff92013-06-06 11:56:47 -04003126 if (rightQualifier.location != -1)
3127 {
3128 joinedQualifier.location = rightQualifier.location;
3129 }
3130 if (rightQualifier.matrixPacking != EmpUnspecified)
3131 {
3132 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
3133 }
3134 if (rightQualifier.blockStorage != EbsUnspecified)
3135 {
3136 joinedQualifier.blockStorage = rightQualifier.blockStorage;
3137 }
3138
3139 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003140}
3141
Arun Patole7e7e68d2015-05-22 12:02:25 +05303142TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc,
3143 TQualifier interpolationQualifier,
3144 const TSourceLoc &storageLoc,
3145 TQualifier storageQualifier)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003146{
3147 TQualifier mergedQualifier = EvqSmoothIn;
3148
Arun Patole7e7e68d2015-05-22 12:02:25 +05303149 if (storageQualifier == EvqFragmentIn)
3150 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003151 if (interpolationQualifier == EvqSmooth)
3152 mergedQualifier = EvqSmoothIn;
3153 else if (interpolationQualifier == EvqFlat)
3154 mergedQualifier = EvqFlatIn;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003155 else
3156 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003157 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303158 else if (storageQualifier == EvqCentroidIn)
3159 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003160 if (interpolationQualifier == EvqSmooth)
3161 mergedQualifier = EvqCentroidIn;
3162 else if (interpolationQualifier == EvqFlat)
3163 mergedQualifier = EvqFlatIn;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003164 else
3165 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003166 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303167 else if (storageQualifier == EvqVertexOut)
3168 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003169 if (interpolationQualifier == EvqSmooth)
3170 mergedQualifier = EvqSmoothOut;
3171 else if (interpolationQualifier == EvqFlat)
3172 mergedQualifier = EvqFlatOut;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003173 else
3174 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003175 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303176 else if (storageQualifier == EvqCentroidOut)
3177 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003178 if (interpolationQualifier == EvqSmooth)
3179 mergedQualifier = EvqCentroidOut;
3180 else if (interpolationQualifier == EvqFlat)
3181 mergedQualifier = EvqFlatOut;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003182 else
3183 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003184 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303185 else
3186 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003187 error(interpolationLoc,
3188 "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303189 getInterpolationString(interpolationQualifier));
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003190 recover();
3191
3192 mergedQualifier = storageQualifier;
3193 }
3194
3195 TPublicType type;
3196 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
3197 return type;
3198}
3199
Jamie Madillb98c3a82015-07-23 14:26:04 -04003200TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3201 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003202{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03003203 if (voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier.type))
3204 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003205 recover();
3206 }
3207
Arun Patole7e7e68d2015-05-22 12:02:25 +05303208 for (unsigned int i = 0; i < fieldList->size(); ++i)
3209 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003210 //
3211 // Careful not to replace already known aspects of type, like array-ness
3212 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303213 TType *type = (*fieldList)[i]->type();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003214 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003215 type->setPrimarySize(typeSpecifier.primarySize);
3216 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003217 type->setPrecision(typeSpecifier.precision);
3218 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003219 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003220
3221 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303222 if (type->isArray())
3223 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003224 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
3225 recover();
3226 }
3227 if (typeSpecifier.array)
3228 type->setArraySize(typeSpecifier.arraySize);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303229 if (typeSpecifier.userDef)
3230 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003231 type->setStruct(typeSpecifier.userDef->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003232 }
3233
Arun Patole7e7e68d2015-05-22 12:02:25 +05303234 if (structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i]))
3235 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003236 recover();
3237 }
3238 }
3239
Jamie Madill98493dd2013-07-08 14:39:03 -04003240 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003241}
3242
Jamie Madillb98c3a82015-07-23 14:26:04 -04003243TPublicType TParseContext::addStructure(const TSourceLoc &structLine,
3244 const TSourceLoc &nameLine,
3245 const TString *structName,
3246 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003247{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303248 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003249 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003250
Jamie Madill9b820842015-02-12 10:40:10 -05003251 // Store a bool in the struct if we're at global scope, to allow us to
3252 // skip the local struct scoping workaround in HLSL.
Jamie Madillb960cc42015-02-12 15:33:20 +00003253 structure->setUniqueId(TSymbolTable::nextUniqueId());
Jamie Madill9b820842015-02-12 10:40:10 -05003254 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003255
Jamie Madill98493dd2013-07-08 14:39:03 -04003256 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003257 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003258 if (reservedErrorCheck(nameLine, *structName))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003259 {
3260 recover();
3261 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303262 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
3263 if (!symbolTable.declare(userTypeDef))
3264 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003265 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003266 recover();
3267 }
3268 }
3269
3270 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04003271 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003272 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003273 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04003274 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003275 switch (qualifier)
3276 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003277 case EvqGlobal:
3278 case EvqTemporary:
3279 break;
3280 default:
3281 error(field.line(), "invalid qualifier on struct member",
3282 getQualifierString(qualifier));
3283 recover();
3284 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003285 }
3286 }
3287
3288 TPublicType publicType;
3289 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003290 publicType.userDef = structureType;
Olli Etuahobd163f62015-11-13 12:15:38 +02003291 publicType.isStructSpecifier = true;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003292 exitStructDeclaration();
3293
3294 return publicType;
3295}
3296
Jamie Madillb98c3a82015-07-23 14:26:04 -04003297TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
3298 TIntermAggregate *statementList,
3299 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02003300{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003301 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003302 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02003303 init->isVector())
3304 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003305 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
3306 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003307 recover();
3308 return nullptr;
3309 }
3310
Olli Etuahoac5274d2015-02-20 10:19:08 +02003311 if (statementList)
3312 {
3313 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
3314 {
3315 recover();
3316 return nullptr;
3317 }
3318 }
3319
Olli Etuahoa3a36662015-02-17 13:46:51 +02003320 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3321 if (node == nullptr)
3322 {
3323 error(loc, "erroneous switch statement", "switch");
3324 recover();
3325 return nullptr;
3326 }
3327 return node;
3328}
3329
3330TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3331{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003332 if (mSwitchNestingLevel == 0)
3333 {
3334 error(loc, "case labels need to be inside switch statements", "case");
3335 recover();
3336 return nullptr;
3337 }
3338 if (condition == nullptr)
3339 {
3340 error(loc, "case label must have a condition", "case");
3341 recover();
3342 return nullptr;
3343 }
3344 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04003345 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02003346 {
3347 error(condition->getLine(), "case label must be a scalar integer", "case");
3348 recover();
3349 }
3350 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003351 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
3352 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
3353 // fold in case labels.
3354 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02003355 {
3356 error(condition->getLine(), "case label must be constant", "case");
3357 recover();
3358 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003359 TIntermCase *node = intermediate.addCase(condition, loc);
3360 if (node == nullptr)
3361 {
3362 error(loc, "erroneous case statement", "case");
3363 recover();
3364 return nullptr;
3365 }
3366 return node;
3367}
3368
3369TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3370{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003371 if (mSwitchNestingLevel == 0)
3372 {
3373 error(loc, "default labels need to be inside switch statements", "default");
3374 recover();
3375 return nullptr;
3376 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003377 TIntermCase *node = intermediate.addCase(nullptr, loc);
3378 if (node == nullptr)
3379 {
3380 error(loc, "erroneous default statement", "default");
3381 recover();
3382 return nullptr;
3383 }
3384 return node;
3385}
3386
Jamie Madillb98c3a82015-07-23 14:26:04 -04003387TIntermTyped *TParseContext::createUnaryMath(TOperator op,
3388 TIntermTyped *child,
3389 const TSourceLoc &loc,
3390 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003391{
3392 if (child == nullptr)
3393 {
3394 return nullptr;
3395 }
3396
3397 switch (op)
3398 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003399 case EOpLogicalNot:
3400 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
3401 child->isVector())
3402 {
3403 return nullptr;
3404 }
3405 break;
3406 case EOpBitwiseNot:
3407 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3408 child->isMatrix() || child->isArray())
3409 {
3410 return nullptr;
3411 }
3412 break;
3413 case EOpPostIncrement:
3414 case EOpPreIncrement:
3415 case EOpPostDecrement:
3416 case EOpPreDecrement:
3417 case EOpNegative:
3418 case EOpPositive:
3419 if (child->getBasicType() == EbtStruct || child->getBasicType() == EbtBool ||
3420 child->isArray())
3421 {
3422 return nullptr;
3423 }
3424 // Operators for built-ins are already type checked against their prototype.
3425 default:
3426 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003427 }
3428
Olli Etuahof6c694b2015-03-26 14:50:53 +02003429 return intermediate.addUnaryMath(op, child, loc, funcReturnType);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003430}
3431
Olli Etuaho09b22472015-02-11 11:47:26 +02003432TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3433{
Olli Etuahof6c694b2015-03-26 14:50:53 +02003434 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003435 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003436 {
3437 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
3438 recover();
3439 return child;
3440 }
3441 return node;
3442}
3443
Jamie Madillb98c3a82015-07-23 14:26:04 -04003444TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
3445 TIntermTyped *child,
3446 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003447{
3448 if (lValueErrorCheck(loc, GetOperatorString(op), child))
3449 recover();
3450 return addUnaryMath(op, child, loc);
3451}
3452
Jamie Madillb98c3a82015-07-23 14:26:04 -04003453bool TParseContext::binaryOpCommonCheck(TOperator op,
3454 TIntermTyped *left,
3455 TIntermTyped *right,
3456 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003457{
3458 if (left->isArray() || right->isArray())
3459 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003460 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003461 {
3462 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3463 return false;
3464 }
3465
3466 if (left->isArray() != right->isArray())
3467 {
3468 error(loc, "array / non-array mismatch", GetOperatorString(op));
3469 return false;
3470 }
3471
3472 switch (op)
3473 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003474 case EOpEqual:
3475 case EOpNotEqual:
3476 case EOpAssign:
3477 case EOpInitialize:
3478 break;
3479 default:
3480 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3481 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02003482 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003483 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003484 if (left->getArraySize() != right->getArraySize())
3485 {
3486 error(loc, "array size mismatch", GetOperatorString(op));
3487 return false;
3488 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003489 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003490
3491 // Check ops which require integer / ivec parameters
3492 bool isBitShift = false;
3493 switch (op)
3494 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003495 case EOpBitShiftLeft:
3496 case EOpBitShiftRight:
3497 case EOpBitShiftLeftAssign:
3498 case EOpBitShiftRightAssign:
3499 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3500 // check that the basic type is an integer type.
3501 isBitShift = true;
3502 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3503 {
3504 return false;
3505 }
3506 break;
3507 case EOpBitwiseAnd:
3508 case EOpBitwiseXor:
3509 case EOpBitwiseOr:
3510 case EOpBitwiseAndAssign:
3511 case EOpBitwiseXorAssign:
3512 case EOpBitwiseOrAssign:
3513 // It is enough to check the type of only one operand, since later it
3514 // is checked that the operand types match.
3515 if (!IsInteger(left->getBasicType()))
3516 {
3517 return false;
3518 }
3519 break;
3520 default:
3521 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003522 }
3523
3524 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3525 // So the basic type should usually match.
3526 if (!isBitShift && left->getBasicType() != right->getBasicType())
3527 {
3528 return false;
3529 }
3530
Olli Etuaho9dd217b2015-03-20 14:24:31 +02003531 // Check that type sizes match exactly on ops that require that.
Olli Etuahoff699002015-03-23 14:38:42 +02003532 // Also check restrictions for structs that contain arrays or samplers.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003533 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003534 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003535 case EOpAssign:
3536 case EOpInitialize:
3537 case EOpEqual:
3538 case EOpNotEqual:
3539 // ESSL 1.00 sections 5.7, 5.8, 5.9
3540 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
3541 {
3542 error(loc, "undefined operation for structs containing arrays",
3543 GetOperatorString(op));
3544 return false;
3545 }
3546 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3547 // we interpret the spec so that this extends to structs containing samplers,
3548 // similarly to ESSL 1.00 spec.
3549 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
3550 left->getType().isStructureContainingSamplers())
3551 {
3552 error(loc, "undefined operation for structs containing samplers",
3553 GetOperatorString(op));
3554 return false;
3555 }
3556 case EOpLessThan:
3557 case EOpGreaterThan:
3558 case EOpLessThanEqual:
3559 case EOpGreaterThanEqual:
3560 if ((left->getNominalSize() != right->getNominalSize()) ||
3561 (left->getSecondarySize() != right->getSecondarySize()))
3562 {
3563 return false;
3564 }
3565 default:
3566 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003567 }
3568
Olli Etuahod6b14282015-03-17 14:31:35 +02003569 return true;
3570}
3571
Jamie Madillb98c3a82015-07-23 14:26:04 -04003572TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
3573 TIntermTyped *left,
3574 TIntermTyped *right,
3575 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02003576{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003577 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003578 return nullptr;
3579
Olli Etuahofc1806e2015-03-17 13:03:11 +02003580 switch (op)
3581 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003582 case EOpEqual:
3583 case EOpNotEqual:
3584 break;
3585 case EOpLessThan:
3586 case EOpGreaterThan:
3587 case EOpLessThanEqual:
3588 case EOpGreaterThanEqual:
3589 ASSERT(!left->isArray() && !right->isArray());
3590 if (left->isMatrix() || left->isVector() || left->getBasicType() == EbtStruct)
3591 {
3592 return nullptr;
3593 }
3594 break;
3595 case EOpLogicalOr:
3596 case EOpLogicalXor:
3597 case EOpLogicalAnd:
3598 ASSERT(!left->isArray() && !right->isArray());
3599 if (left->getBasicType() != EbtBool || left->isMatrix() || left->isVector())
3600 {
3601 return nullptr;
3602 }
3603 break;
3604 case EOpAdd:
3605 case EOpSub:
3606 case EOpDiv:
3607 case EOpMul:
3608 ASSERT(!left->isArray() && !right->isArray());
3609 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool)
3610 {
3611 return nullptr;
3612 }
3613 break;
3614 case EOpIMod:
3615 ASSERT(!left->isArray() && !right->isArray());
3616 // Note that this is only for the % operator, not for mod()
3617 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool ||
3618 left->getBasicType() == EbtFloat)
3619 {
3620 return nullptr;
3621 }
3622 break;
3623 // Note that for bitwise ops, type checking is done in promote() to
3624 // share code between ops and compound assignment
3625 default:
3626 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003627 }
3628
Olli Etuahofc1806e2015-03-17 13:03:11 +02003629 return intermediate.addBinaryMath(op, left, right, loc);
3630}
3631
Jamie Madillb98c3a82015-07-23 14:26:04 -04003632TIntermTyped *TParseContext::addBinaryMath(TOperator op,
3633 TIntermTyped *left,
3634 TIntermTyped *right,
3635 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003636{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003637 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003638 if (node == 0)
3639 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003640 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3641 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003642 recover();
3643 return left;
3644 }
3645 return node;
3646}
3647
Jamie Madillb98c3a82015-07-23 14:26:04 -04003648TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
3649 TIntermTyped *left,
3650 TIntermTyped *right,
3651 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003652{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003653 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003654 if (node == 0)
3655 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003656 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3657 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003658 recover();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003659 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02003660 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003661 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
3662 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003663 }
3664 return node;
3665}
3666
Jamie Madillb98c3a82015-07-23 14:26:04 -04003667TIntermTyped *TParseContext::createAssign(TOperator op,
3668 TIntermTyped *left,
3669 TIntermTyped *right,
3670 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003671{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003672 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003673 {
3674 return intermediate.addAssign(op, left, right, loc);
3675 }
3676 return nullptr;
3677}
3678
Jamie Madillb98c3a82015-07-23 14:26:04 -04003679TIntermTyped *TParseContext::addAssign(TOperator op,
3680 TIntermTyped *left,
3681 TIntermTyped *right,
3682 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003683{
3684 TIntermTyped *node = createAssign(op, left, right, loc);
3685 if (node == nullptr)
3686 {
3687 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
3688 recover();
3689 return left;
3690 }
3691 return node;
3692}
3693
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003694TIntermTyped *TParseContext::addComma(TIntermTyped *left,
3695 TIntermTyped *right,
3696 const TSourceLoc &loc)
3697{
Olli Etuaho15200042015-11-04 16:56:31 +02003698 return intermediate.addComma(left, right, loc, mShaderVersion);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003699}
3700
Olli Etuaho49300862015-02-20 14:54:49 +02003701TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3702{
3703 switch (op)
3704 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003705 case EOpContinue:
3706 if (mLoopNestingLevel <= 0)
3707 {
3708 error(loc, "continue statement only allowed in loops", "");
3709 recover();
3710 }
3711 break;
3712 case EOpBreak:
3713 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
3714 {
3715 error(loc, "break statement only allowed in loops and switch statements", "");
3716 recover();
3717 }
3718 break;
3719 case EOpReturn:
3720 if (mCurrentFunctionType->getBasicType() != EbtVoid)
3721 {
3722 error(loc, "non-void function must return a value", "return");
3723 recover();
3724 }
3725 break;
3726 default:
3727 // No checks for discard
3728 break;
Olli Etuaho49300862015-02-20 14:54:49 +02003729 }
3730 return intermediate.addBranch(op, loc);
3731}
3732
Jamie Madillb98c3a82015-07-23 14:26:04 -04003733TIntermBranch *TParseContext::addBranch(TOperator op,
3734 TIntermTyped *returnValue,
3735 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02003736{
3737 ASSERT(op == EOpReturn);
3738 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003739 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02003740 {
3741 error(loc, "void function cannot return a value", "return");
3742 recover();
3743 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003744 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02003745 {
3746 error(loc, "function return is not matching type:", "return");
3747 recover();
3748 }
3749 return intermediate.addBranch(op, returnValue, loc);
3750}
3751
Jamie Madillb98c3a82015-07-23 14:26:04 -04003752TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
3753 TIntermNode *paramNode,
3754 TIntermNode *thisNode,
3755 const TSourceLoc &loc,
3756 bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003757{
Jamie Madillb98c3a82015-07-23 14:26:04 -04003758 *fatalError = false;
3759 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003760 TIntermTyped *callNode = nullptr;
3761
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003762 if (thisNode != nullptr)
3763 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003764 TConstantUnion *unionArray = new TConstantUnion[1];
Jamie Madillb98c3a82015-07-23 14:26:04 -04003765 int arraySize = 0;
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003766 TIntermTyped *typedThis = thisNode->getAsTyped();
3767 if (fnCall->getName() != "length")
3768 {
3769 error(loc, "invalid method", fnCall->getName().c_str());
3770 recover();
3771 }
3772 else if (paramNode != nullptr)
3773 {
3774 error(loc, "method takes no parameters", "length");
3775 recover();
3776 }
3777 else if (typedThis == nullptr || !typedThis->isArray())
3778 {
3779 error(loc, "length can only be called on arrays", "length");
3780 recover();
3781 }
3782 else
3783 {
Olli Etuaho96e67382015-04-23 14:27:02 +03003784 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03003785 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003786 {
Olli Etuaho39282e12015-04-23 15:41:48 +03003787 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003788 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03003789 // (func()).length()
3790 // (int[3](0, 1, 2)).length()
Jamie Madillb98c3a82015-07-23 14:26:04 -04003791 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
3792 // expression.
3793 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
3794 // spec section 5.9 which allows "An array, vector or matrix expression with the
3795 // length method applied".
3796 error(loc, "length can only be called on array names, not on array expressions",
3797 "length");
Olli Etuaho39282e12015-04-23 15:41:48 +03003798 recover();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003799 }
3800 }
Olli Etuaho96e67382015-04-23 14:27:02 +03003801 unionArray->setIConst(arraySize);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003802 callNode =
3803 intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003804 }
3805 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003806 {
3807 //
3808 // Then this should be a constructor.
3809 // Don't go through the symbol table for constructors.
3810 // Their parameters will be verified algorithmically.
3811 //
3812 TType type(EbtVoid, EbpUndefined); // use this to get the type back
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003813 if (!constructorErrorCheck(loc, paramNode, *fnCall, op, &type))
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003814 {
3815 //
3816 // It's a constructor, of type 'type'.
3817 //
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003818 callNode = addConstructor(paramNode, &type, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003819 }
Olli Etuaho72ba85b2015-03-04 14:23:26 +02003820
3821 if (callNode == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003822 {
3823 recover();
3824 callNode = intermediate.setAggregateOperator(nullptr, op, loc);
3825 }
3826 callNode->setType(type);
3827 }
3828 else
3829 {
3830 //
3831 // Not a constructor. Find it in the symbol table.
3832 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303833 const TFunction *fnCandidate;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003834 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003835 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003836 if (fnCandidate)
3837 {
3838 //
3839 // A declared function.
3840 //
3841 if (builtIn && !fnCandidate->getExtension().empty() &&
3842 extensionErrorCheck(loc, fnCandidate->getExtension()))
3843 {
3844 recover();
3845 }
3846 op = fnCandidate->getBuiltInOp();
3847 if (builtIn && op != EOpNull)
3848 {
3849 //
3850 // A function call mapped to a built-in operation.
3851 //
3852 if (fnCandidate->getParamCount() == 1)
3853 {
3854 //
3855 // Treat it like a built-in unary operator.
3856 //
Olli Etuaho15c2ac32015-11-09 15:51:43 +02003857 TIntermAggregate *paramAgg = paramNode->getAsAggregate();
3858 paramNode = paramAgg->getSequence()->front();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003859 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc,
3860 &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003861 if (callNode == nullptr)
3862 {
3863 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003864 extraInfoStream
3865 << "built in unary operator function. Type: "
3866 << static_cast<TIntermTyped *>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003867 std::string extraInfo = extraInfoStream.str();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003868 error(paramNode->getLine(), " wrong operand type", "Internal Error",
3869 extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003870 *fatalError = true;
3871 return nullptr;
3872 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003873 }
3874 else
3875 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003876 TIntermAggregate *aggregate =
3877 intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003878 aggregate->setType(fnCandidate->getReturnType());
3879 aggregate->setPrecisionFromChildren();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02003880 if (aggregate->areChildrenConstQualified())
3881 {
3882 aggregate->getTypePointer()->setQualifier(EvqConst);
3883 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003884
3885 // Some built-in functions have out parameters too.
3886 functionCallLValueErrorCheck(fnCandidate, aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05303887
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003888 // See if we can constant fold a built-in. Note that this may be possible even
3889 // if it is not const-qualified.
Olli Etuahob43846e2015-06-02 18:18:57 +03003890 TIntermTyped *foldedNode = intermediate.foldAggregateBuiltIn(aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05303891 if (foldedNode)
3892 {
Arun Patole274f0702015-05-05 13:33:30 +05303893 callNode = foldedNode;
3894 }
Olli Etuahob43846e2015-06-02 18:18:57 +03003895 else
3896 {
3897 callNode = aggregate;
3898 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003899 }
3900 }
3901 else
3902 {
3903 // This is a real function call
Jamie Madillb98c3a82015-07-23 14:26:04 -04003904 TIntermAggregate *aggregate =
3905 intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003906 aggregate->setType(fnCandidate->getReturnType());
3907
Jamie Madillb98c3a82015-07-23 14:26:04 -04003908 // this is how we know whether the given function is a builtIn function or a user
3909 // defined function
3910 // if builtIn == false, it's a userDefined -> could be an overloaded
3911 // builtIn function also
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003912 // if builtIn == true, it's definitely a builtIn function with EOpNull
3913 if (!builtIn)
3914 aggregate->setUserDefined();
3915 aggregate->setName(fnCandidate->getMangledName());
Corentin Wallez71d147f2015-02-11 11:15:24 -08003916 aggregate->setFunctionId(fnCandidate->getUniqueId());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003917
3918 // This needs to happen after the name is set
3919 if (builtIn)
3920 aggregate->setBuiltInFunctionPrecision();
3921
3922 callNode = aggregate;
3923
3924 functionCallLValueErrorCheck(fnCandidate, aggregate);
3925 }
3926 }
3927 else
3928 {
3929 // error message was put out by findFunction()
3930 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003931 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003932 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003933 callNode = intermediate.addConstantUnion(unionArray,
3934 TType(EbtFloat, EbpUndefined, EvqConst), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003935 recover();
3936 }
3937 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003938 return callNode;
3939}
3940
Jamie Madillb98c3a82015-07-23 14:26:04 -04003941TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
3942 TIntermTyped *trueBlock,
3943 TIntermTyped *falseBlock,
Olli Etuaho52901742015-04-15 13:42:45 +03003944 const TSourceLoc &loc)
3945{
3946 if (boolErrorCheck(loc, cond))
3947 recover();
3948
3949 if (trueBlock->getType() != falseBlock->getType())
3950 {
3951 binaryOpError(loc, ":", trueBlock->getCompleteString(), falseBlock->getCompleteString());
3952 recover();
3953 return falseBlock;
3954 }
Olli Etuahoa2d53032015-04-15 14:14:44 +03003955 // ESSL1 sections 5.2 and 5.7:
3956 // ESSL3 section 5.7:
3957 // Ternary operator is not among the operators allowed for structures/arrays.
3958 if (trueBlock->isArray() || trueBlock->getBasicType() == EbtStruct)
3959 {
3960 error(loc, "ternary operator is not allowed for structures or arrays", ":");
3961 recover();
3962 return falseBlock;
3963 }
Olli Etuaho52901742015-04-15 13:42:45 +03003964 return intermediate.addSelection(cond, trueBlock, falseBlock, loc);
3965}
Olli Etuaho49300862015-02-20 14:54:49 +02003966
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003967//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003968// Parse an array of strings using yyparse.
3969//
3970// Returns 0 for success.
3971//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003972int PaParseStrings(size_t count,
3973 const char *const string[],
3974 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05303975 TParseContext *context)
3976{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003977 if ((count == 0) || (string == NULL))
3978 return 1;
3979
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003980 if (glslang_initialize(context))
3981 return 1;
3982
alokp@chromium.org408c45e2012-04-05 15:54:43 +00003983 int error = glslang_scan(count, string, length, context);
3984 if (!error)
3985 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003986
alokp@chromium.org73bc2982012-06-19 18:48:05 +00003987 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00003988
alokp@chromium.org6b495712012-06-29 00:06:58 +00003989 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003990}