blob: 33ec8c093ecb69f54ff7e1fd0f2a8b0fff80618b [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;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200229 if (precision == EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530230 {
Olli Etuaho183d7e22015-11-20 15:59:09 +0200231 switch (type)
232 {
233 case EbtFloat:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400234 error(line, "No precision specified for (float)", "");
235 return true;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200236 case EbtInt:
237 case EbtUInt:
238 UNREACHABLE(); // there's always a predeclared qualifier
Jamie Madillb98c3a82015-07-23 14:26:04 -0400239 error(line, "No precision specified (int)", "");
240 return true;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200241 default:
242 if (IsSampler(type))
243 {
244 error(line, "No precision specified (sampler)", "");
245 return true;
246 }
247 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000248 }
249 return false;
250}
251
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000252//
253// Both test and if necessary, spit out an error, to see if the node is really
254// an l-value that can be operated on this way.
255//
256// Returns true if the was an error.
257//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530258bool TParseContext::lValueErrorCheck(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000259{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400260 TIntermSymbol *symNode = node->getAsSymbolNode();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530261 TIntermBinary *binaryNode = node->getAsBinaryNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000262
Arun Patole7e7e68d2015-05-22 12:02:25 +0530263 if (binaryNode)
264 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000265 bool errorReturn;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000266
Jamie Madillb98c3a82015-07-23 14:26:04 -0400267 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530268 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400269 case EOpIndexDirect:
270 case EOpIndexIndirect:
271 case EOpIndexDirectStruct:
272 case EOpIndexDirectInterfaceBlock:
273 return lValueErrorCheck(line, op, binaryNode->getLeft());
274 case EOpVectorSwizzle:
275 errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft());
276 if (!errorReturn)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530277 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400278 int offset[4] = {0, 0, 0, 0};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000279
Jamie Madillb98c3a82015-07-23 14:26:04 -0400280 TIntermTyped *rightNode = binaryNode->getRight();
281 TIntermAggregate *aggrNode = rightNode->getAsAggregate();
282
283 for (TIntermSequence::iterator p = aggrNode->getSequence()->begin();
284 p != aggrNode->getSequence()->end(); p++)
285 {
286 int value = (*p)->getAsTyped()->getAsConstantUnion()->getIConst(0);
287 offset[value]++;
288 if (offset[value] > 1)
289 {
290 error(line, " l-value of swizzle cannot have duplicate components", op);
291
292 return true;
293 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000294 }
295 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000296
Jamie Madillb98c3a82015-07-23 14:26:04 -0400297 return errorReturn;
298 default:
299 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000300 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000301 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000302
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000303 return true;
304 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000305
Arun Patole7e7e68d2015-05-22 12:02:25 +0530306 const char *symbol = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000307 if (symNode != 0)
308 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000309
Arun Patole7e7e68d2015-05-22 12:02:25 +0530310 const char *message = 0;
311 switch (node->getQualifier())
312 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400313 case EvqConst:
314 message = "can't modify a const";
315 break;
316 case EvqConstReadOnly:
317 message = "can't modify a const";
318 break;
319 case EvqAttribute:
320 message = "can't modify an attribute";
321 break;
322 case EvqFragmentIn:
323 message = "can't modify an input";
324 break;
325 case EvqVertexIn:
326 message = "can't modify an input";
327 break;
328 case EvqUniform:
329 message = "can't modify a uniform";
330 break;
331 case EvqVaryingIn:
332 message = "can't modify a varying";
333 break;
334 case EvqFragCoord:
335 message = "can't modify gl_FragCoord";
336 break;
337 case EvqFrontFacing:
338 message = "can't modify gl_FrontFacing";
339 break;
340 case EvqPointCoord:
341 message = "can't modify gl_PointCoord";
342 break;
Martin Radev802abe02016-08-04 17:48:32 +0300343 case EvqComputeIn:
344 message = "can't modify work group size variable";
345 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400346 default:
347 //
348 // Type that can't be written to?
349 //
350 if (node->getBasicType() == EbtVoid)
351 {
352 message = "can't modify void";
353 }
354 if (IsSampler(node->getBasicType()))
355 {
356 message = "can't modify a sampler";
357 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000358 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000359
Arun Patole7e7e68d2015-05-22 12:02:25 +0530360 if (message == 0 && binaryNode == 0 && symNode == 0)
361 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000362 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000363
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000364 return true;
365 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000366
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000367 //
368 // Everything else is okay, no error.
369 //
370 if (message == 0)
371 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000372
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000373 //
374 // If we get here, we have an error and a message.
375 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530376 if (symNode)
377 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000378 std::stringstream extraInfoStream;
379 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
380 std::string extraInfo = extraInfoStream.str();
381 error(line, " l-value required", op, extraInfo.c_str());
382 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530383 else
384 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000385 std::stringstream extraInfoStream;
386 extraInfoStream << "(" << message << ")";
387 std::string extraInfo = extraInfoStream.str();
388 error(line, " l-value required", op, extraInfo.c_str());
389 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000390
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000391 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000392}
393
394//
395// Both test, and if necessary spit out an error, to see if the node is really
396// a constant.
397//
398// Returns true if the was an error.
399//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530400bool TParseContext::constErrorCheck(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000401{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000402 if (node->getQualifier() == EvqConst)
403 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000404
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000405 error(node->getLine(), "constant expression required", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000406
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000407 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000408}
409
410//
411// Both test, and if necessary spit out an error, to see if the node is really
412// an integer.
413//
414// Returns true if the was an error.
415//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530416bool TParseContext::integerErrorCheck(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000417{
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000418 if (node->isScalarInt())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000419 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000420
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000421 error(node->getLine(), "integer expression required", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000422
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000423 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000424}
425
426//
427// Both test, and if necessary spit out an error, to see if we are currently
428// globally scoped.
429//
430// Returns true if the was an error.
431//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530432bool TParseContext::globalErrorCheck(const TSourceLoc &line, bool global, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000433{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000434 if (global)
435 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000436
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000437 error(line, "only allowed at global scope", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000438
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000439 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000440}
441
442//
443// For now, keep it simple: if it starts "gl_", it's reserved, independent
444// of scope. Except, if the symbol table is at the built-in push-level,
445// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000446// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
447// webgl shader.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000448//
449// Returns true if there was an error.
450//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530451bool TParseContext::reservedErrorCheck(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000452{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530453 static const char *reservedErrMsg = "reserved built-in name";
454 if (!symbolTable.atBuiltInLevel())
455 {
456 if (identifier.compare(0, 3, "gl_") == 0)
457 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000458 error(line, reservedErrMsg, "gl_");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000459 return true;
460 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530461 if (IsWebGLBasedSpec(mShaderSpec))
462 {
463 if (identifier.compare(0, 6, "webgl_") == 0)
464 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000465 error(line, reservedErrMsg, "webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000466 return true;
467 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530468 if (identifier.compare(0, 7, "_webgl_") == 0)
469 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000470 error(line, reservedErrMsg, "_webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000471 return true;
472 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530473 if (mShaderSpec == SH_CSS_SHADERS_SPEC && identifier.compare(0, 4, "css_") == 0)
474 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000475 error(line, reservedErrMsg, "css_");
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000476 return true;
477 }
alokp@chromium.org613ef312010-07-21 18:54:22 +0000478 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530479 if (identifier.find("__") != TString::npos)
480 {
481 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400482 "identifiers containing two consecutive underscores (__) are reserved as "
483 "possible future keywords",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530484 identifier.c_str());
daniel@transgaming.combeadd5d2012-04-12 02:35:31 +0000485 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000486 }
487 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000488
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000489 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000490}
491
492//
493// Make sure there is enough data provided to the constructor to build
494// something of the type of the constructor. Also returns the type of
495// the constructor.
496//
497// Returns true if there was an error in construction.
498//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400499bool TParseContext::constructorErrorCheck(const TSourceLoc &line,
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200500 TIntermNode *argumentsNode,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400501 TFunction &function,
502 TOperator op,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530503 TType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000504{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000505 *type = function.getReturnType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000506
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000507 bool constructingMatrix = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400508 switch (op)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530509 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400510 case EOpConstructMat2:
511 case EOpConstructMat2x3:
512 case EOpConstructMat2x4:
513 case EOpConstructMat3x2:
514 case EOpConstructMat3:
515 case EOpConstructMat3x4:
516 case EOpConstructMat4x2:
517 case EOpConstructMat4x3:
518 case EOpConstructMat4:
519 constructingMatrix = true;
520 break;
521 default:
522 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000523 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000524
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000525 //
526 // Note: It's okay to have too many components available, but not okay to have unused
527 // arguments. 'full' will go to true when enough args have been seen. If we loop
528 // again, there is an extra argument, so 'overfull' will become true.
529 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000530
Jamie Madillb98c3a82015-07-23 14:26:04 -0400531 size_t size = 0;
532 bool constType = true;
533 bool full = false;
534 bool overFull = false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000535 bool matrixInMatrix = false;
536 bool arrayArg = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530537 for (size_t i = 0; i < function.getParamCount(); ++i)
538 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700539 const TConstParameter &param = function.getParam(i);
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000540 size += param.type->getObjectSize();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530541
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000542 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000543 matrixInMatrix = true;
544 if (full)
545 overFull = true;
546 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
547 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000548 if (param.type->getQualifier() != EvqConst)
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000549 constType = false;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000550 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000551 arrayArg = true;
552 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530553
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000554 if (constType)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000555 type->setQualifier(EvqConst);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000556
Olli Etuaho376f1b52015-04-13 13:23:41 +0300557 if (type->isArray())
558 {
559 if (type->isUnsizedArray())
560 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700561 type->setArraySize(static_cast<int>(function.getParamCount()));
Olli Etuaho376f1b52015-04-13 13:23:41 +0300562 }
563 else if (static_cast<size_t>(type->getArraySize()) != function.getParamCount())
564 {
565 error(line, "array constructor needs one argument per array element", "constructor");
566 return true;
567 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000568 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000569
Arun Patole7e7e68d2015-05-22 12:02:25 +0530570 if (arrayArg && op != EOpConstructStruct)
571 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000572 error(line, "constructing from a non-dereferenced array", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000573 return true;
574 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000575
Arun Patole7e7e68d2015-05-22 12:02:25 +0530576 if (matrixInMatrix && !type->isArray())
577 {
578 if (function.getParamCount() != 1)
579 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400580 error(line, "constructing matrix from matrix can only take one argument",
581 "constructor");
582 return true;
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000583 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000584 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000585
Arun Patole7e7e68d2015-05-22 12:02:25 +0530586 if (overFull)
587 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000588 error(line, "too many arguments", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000589 return true;
590 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530591
Jamie Madillb98c3a82015-07-23 14:26:04 -0400592 if (op == EOpConstructStruct && !type->isArray() &&
593 type->getStruct()->fields().size() != function.getParamCount())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530594 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400595 error(line,
596 "Number of constructor parameters does not match the number of structure fields",
597 "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000598 return true;
599 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000600
Arun Patole7e7e68d2015-05-22 12:02:25 +0530601 if (!type->isMatrix() || !matrixInMatrix)
602 {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000603 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
Arun Patole7e7e68d2015-05-22 12:02:25 +0530604 (op == EOpConstructStruct && size < type->getObjectSize()))
605 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000606 error(line, "not enough data provided for construction", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000607 return true;
608 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000609 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000610
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200611 if (argumentsNode == nullptr)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530612 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200613 error(line, "constructor does not have any arguments", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000614 return true;
615 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200616
617 TIntermAggregate *argumentsAgg = argumentsNode->getAsAggregate();
618 for (TIntermNode *&argNode : *argumentsAgg->getSequence())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530619 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200620 TIntermTyped *argTyped = argNode->getAsTyped();
621 ASSERT(argTyped != nullptr);
622 if (op != EOpConstructStruct && IsSampler(argTyped->getBasicType()))
623 {
624 error(line, "cannot convert a sampler", "constructor");
625 return true;
626 }
627 if (argTyped->getBasicType() == EbtVoid)
628 {
629 error(line, "cannot convert a void", "constructor");
630 return true;
631 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000632 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000633
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000634 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000635}
636
Jamie Madillb98c3a82015-07-23 14:26:04 -0400637// This function checks to see if a void variable has been declared and raise an error message for
638// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000639//
640// returns true in case of an error
641//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400642bool TParseContext::voidErrorCheck(const TSourceLoc &line,
643 const TString &identifier,
644 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000645{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300646 if (type == EbtVoid)
647 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000648 error(line, "illegal use of type 'void'", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000649 return true;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300650 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000651
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000652 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000653}
654
Jamie Madillb98c3a82015-07-23 14:26:04 -0400655// This function checks to see if the node (for the expression) contains a scalar boolean expression
656// or not
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000657//
658// returns true in case of an error
659//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530660bool TParseContext::boolErrorCheck(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000661{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530662 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector())
663 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000664 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000665 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530666 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000667
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000668 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000669}
670
Jamie Madillb98c3a82015-07-23 14:26:04 -0400671// This function checks to see if the node (for the expression) contains a scalar boolean expression
672// or not
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000673//
674// returns true in case of an error
675//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530676bool TParseContext::boolErrorCheck(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000677{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530678 if (pType.type != EbtBool || pType.isAggregate())
679 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000680 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000681 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530682 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000683
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000684 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000685}
686
Jamie Madillb98c3a82015-07-23 14:26:04 -0400687bool TParseContext::samplerErrorCheck(const TSourceLoc &line,
688 const TPublicType &pType,
689 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000690{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530691 if (pType.type == EbtStruct)
692 {
693 if (containsSampler(*pType.userDef))
694 {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000695 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530696
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000697 return true;
698 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530699
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000700 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530701 }
702 else if (IsSampler(pType.type))
703 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000704 error(line, reason, getBasicString(pType.type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000705
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000706 return true;
707 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000708
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000709 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000710}
711
Arun Patole7e7e68d2015-05-22 12:02:25 +0530712bool TParseContext::locationDeclaratorListCheck(const TSourceLoc &line, const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400713{
714 if (pType.layoutQualifier.location != -1)
715 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400716 error(line, "location must only be specified for a single input or output variable",
717 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400718 return true;
719 }
720
721 return false;
722}
723
Jamie Madillb98c3a82015-07-23 14:26:04 -0400724bool TParseContext::parameterSamplerErrorCheck(const TSourceLoc &line,
725 TQualifier qualifier,
726 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000727{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400728 if ((qualifier == EvqOut || qualifier == EvqInOut) && type.getBasicType() != EbtStruct &&
729 IsSampler(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530730 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000731 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000732 return true;
733 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000734
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000735 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000736}
737
Arun Patole7e7e68d2015-05-22 12:02:25 +0530738bool TParseContext::containsSampler(const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000739{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000740 if (IsSampler(type.getBasicType()))
741 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000742
Arun Patole7e7e68d2015-05-22 12:02:25 +0530743 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock())
744 {
745 const TFieldList &fields = type.getStruct()->fields();
746 for (unsigned int i = 0; i < fields.size(); ++i)
747 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400748 if (containsSampler(*fields[i]->type()))
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000749 return true;
750 }
751 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000752
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000753 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000754}
755
756//
757// Do size checking for an array type's size.
758//
759// Returns true if there was an error.
760//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530761bool TParseContext::arraySizeErrorCheck(const TSourceLoc &line, TIntermTyped *expr, int &size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000762{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530763 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000764
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200765 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
766 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
767 // fold as array size.
768 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000769 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000770 error(line, "array size must be a constant integer expression", "");
Olli Etuahoe7847b02015-03-16 11:56:12 +0200771 size = 1;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000772 return true;
773 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000774
Nicolas Capens906744a2014-06-06 15:18:07 -0400775 unsigned int unsignedSize = 0;
776
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000777 if (constant->getBasicType() == EbtUInt)
778 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400779 unsignedSize = constant->getUConst(0);
Jamie Madillb98c3a82015-07-23 14:26:04 -0400780 size = static_cast<int>(unsignedSize);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000781 }
782 else
783 {
784 size = constant->getIConst(0);
785
Nicolas Capens906744a2014-06-06 15:18:07 -0400786 if (size < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000787 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400788 error(line, "array size must be non-negative", "");
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000789 size = 1;
790 return true;
791 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400792
793 unsignedSize = static_cast<unsigned int>(size);
794 }
795
796 if (size == 0)
797 {
798 error(line, "array size must be greater than zero", "");
799 size = 1;
800 return true;
801 }
802
803 // The size of arrays is restricted here to prevent issues further down the
804 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
805 // 4096 registers so this should be reasonable even for aggressively optimizable code.
806 const unsigned int sizeLimit = 65536;
807
808 if (unsignedSize > sizeLimit)
809 {
810 error(line, "array size too large", "");
811 size = 1;
812 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000813 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000814
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000815 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000816}
817
818//
819// See if this qualifier can be an array.
820//
821// Returns true if there is an error.
822//
Olli Etuaho3739d232015-04-08 12:23:44 +0300823bool TParseContext::arrayQualifierErrorCheck(const TSourceLoc &line, const TPublicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000824{
Olli Etuaho3739d232015-04-08 12:23:44 +0300825 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqVertexIn) ||
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400826 (type.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300827 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400828 error(line, "cannot declare arrays of this qualifier",
829 TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000830 return true;
831 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000832
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000833 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000834}
835
836//
837// See if this type can be an array.
838//
839// Returns true if there is an error.
840//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530841bool TParseContext::arrayTypeErrorCheck(const TSourceLoc &line, const TPublicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000842{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000843 //
844 // Can the type be an array?
845 //
Jamie Madill06145232015-05-13 13:10:01 -0400846 if (type.array)
847 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000848 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000849 return true;
850 }
Olli Etuahocc36b982015-07-10 14:14:18 +0300851 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
852 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
853 // 4.3.4).
854 if (mShaderVersion >= 300 && type.type == EbtStruct && sh::IsVarying(type.qualifier))
855 {
856 error(line, "cannot declare arrays of structs of this qualifier",
857 TType(type).getCompleteString().c_str());
858 return true;
859 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000860
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000861 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000862}
863
864//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000865// Enforce non-initializer type/qualifier rules.
866//
867// Returns true if there was an error.
868//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400869bool TParseContext::nonInitErrorCheck(const TSourceLoc &line,
870 const TString &identifier,
871 TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000872{
Olli Etuaho3739d232015-04-08 12:23:44 +0300873 ASSERT(type != nullptr);
874 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000875 {
876 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300877 type->qualifier = EvqTemporary;
878
879 // Generate informative error messages for ESSL1.
880 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400881 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000882 {
Arun Patole7e7e68d2015-05-22 12:02:25 +0530883 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400884 "structures containing arrays may not be declared constant since they cannot be "
885 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530886 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000887 }
888 else
889 {
890 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
891 }
892
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000893 return true;
894 }
Olli Etuaho376f1b52015-04-13 13:23:41 +0300895 if (type->isUnsizedArray())
896 {
897 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
898 return true;
899 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000900 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000901}
902
Olli Etuaho2935c582015-04-08 14:32:06 +0300903// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000904// and update the symbol table.
905//
Olli Etuaho2935c582015-04-08 14:32:06 +0300906// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000907//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400908bool TParseContext::declareVariable(const TSourceLoc &line,
909 const TString &identifier,
910 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +0300911 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000912{
Olli Etuaho2935c582015-04-08 14:32:06 +0300913 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000914
Olli Etuaho2935c582015-04-08 14:32:06 +0300915 bool needsReservedErrorCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000916
Olli Etuaho2935c582015-04-08 14:32:06 +0300917 // gl_LastFragData may be redeclared with a new precision qualifier
918 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
919 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400920 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
921 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho2935c582015-04-08 14:32:06 +0300922 if (type.getArraySize() == maxDrawBuffers->getConstPointer()->getIConst())
923 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400924 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +0300925 {
926 needsReservedErrorCheck = extensionErrorCheck(line, builtInSymbol->getExtension());
927 }
928 }
929 else
930 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400931 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
932 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +0300933 return false;
934 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000935 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000936
Olli Etuaho2935c582015-04-08 14:32:06 +0300937 if (needsReservedErrorCheck && reservedErrorCheck(line, identifier))
938 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000939
Olli Etuaho2935c582015-04-08 14:32:06 +0300940 (*variable) = new TVariable(&identifier, type);
941 if (!symbolTable.declare(*variable))
942 {
943 error(line, "redefinition", identifier.c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -0400944 *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +0300945 return false;
946 }
947
948 if (voidErrorCheck(line, identifier, type.getBasicType()))
949 return false;
950
951 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000952}
953
Jamie Madillb98c3a82015-07-23 14:26:04 -0400954bool TParseContext::paramErrorCheck(const TSourceLoc &line,
955 TQualifier qualifier,
956 TQualifier paramQualifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530957 TType *type)
958{
959 if (qualifier != EvqConst && qualifier != EvqTemporary)
960 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000961 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000962 return true;
963 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530964 if (qualifier == EvqConst && paramQualifier != EvqIn)
965 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400966 error(line, "qualifier not allowed with ", getQualifierString(qualifier),
967 getQualifierString(paramQualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000968 return true;
969 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000970
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000971 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000972 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000973 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000974 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000975
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000976 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000977}
978
Arun Patole7e7e68d2015-05-22 12:02:25 +0530979bool TParseContext::extensionErrorCheck(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000980{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400981 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000982 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +0530983 if (iter == extBehavior.end())
984 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000985 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000986 return true;
987 }
zmo@google.comf5450912011-09-09 01:37:19 +0000988 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +0530989 if (iter->second == EBhDisable || iter->second == EBhUndefined)
990 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000991 error(line, "extension", extension.c_str(), "is disabled");
992 return true;
993 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530994 if (iter->second == EBhWarn)
995 {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000996 warning(line, "extension", extension.c_str(), "is being used");
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000997 return false;
998 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000999
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001000 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001001}
1002
Jamie Madillb98c3a82015-07-23 14:26:04 -04001003// These checks are common for all declarations starting a declarator list, and declarators that
1004// follow an empty declaration.
Olli Etuahofa33d582015-04-09 14:33:12 +03001005//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001006bool TParseContext::singleDeclarationErrorCheck(const TPublicType &publicType,
1007 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001008{
Olli Etuahofa33d582015-04-09 14:33:12 +03001009 switch (publicType.qualifier)
1010 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001011 case EvqVaryingIn:
1012 case EvqVaryingOut:
1013 case EvqAttribute:
1014 case EvqVertexIn:
1015 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001016 case EvqComputeIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -04001017 if (publicType.type == EbtStruct)
1018 {
1019 error(identifierLocation, "cannot be used with a structure",
1020 getQualifierString(publicType.qualifier));
1021 return true;
1022 }
Olli Etuahofa33d582015-04-09 14:33:12 +03001023
Jamie Madillb98c3a82015-07-23 14:26:04 -04001024 default:
1025 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001026 }
1027
Jamie Madillb98c3a82015-07-23 14:26:04 -04001028 if (publicType.qualifier != EvqUniform &&
1029 samplerErrorCheck(identifierLocation, publicType, "samplers must be uniform"))
Olli Etuahofa33d582015-04-09 14:33:12 +03001030 {
Jamie Madilla5efff92013-06-06 11:56:47 -04001031 return true;
Olli Etuahofa33d582015-04-09 14:33:12 +03001032 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001033
1034 // check for layout qualifier issues
1035 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
1036
1037 if (layoutQualifier.matrixPacking != EmpUnspecified)
1038 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001039 error(identifierLocation, "layout qualifier",
1040 getMatrixPackingString(layoutQualifier.matrixPacking),
Olli Etuahofa33d582015-04-09 14:33:12 +03001041 "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -04001042 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001043 }
1044
1045 if (layoutQualifier.blockStorage != EbsUnspecified)
1046 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001047 error(identifierLocation, "layout qualifier",
1048 getBlockStorageString(layoutQualifier.blockStorage),
Olli Etuahofa33d582015-04-09 14:33:12 +03001049 "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -04001050 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001051 }
1052
Olli Etuahofa33d582015-04-09 14:33:12 +03001053 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut &&
1054 layoutLocationErrorCheck(identifierLocation, publicType.layoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001055 {
Jamie Madill51a53c72013-06-19 09:24:43 -04001056 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001057 }
1058
1059 return false;
1060}
1061
Jamie Madillb98c3a82015-07-23 14:26:04 -04001062bool TParseContext::layoutLocationErrorCheck(const TSourceLoc &location,
1063 const TLayoutQualifier &layoutQualifier)
Jamie Madilla5efff92013-06-06 11:56:47 -04001064{
1065 if (layoutQualifier.location != -1)
1066 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001067 error(location, "invalid layout qualifier:", "location",
1068 "only valid on program inputs and outputs");
Jamie Madilla5efff92013-06-06 11:56:47 -04001069 return true;
1070 }
1071
1072 return false;
1073}
1074
Martin Radev802abe02016-08-04 17:48:32 +03001075void TParseContext::layoutSupportedErrorCheck(const TSourceLoc &location,
1076 const TString &layoutQualifierName,
1077 int versionRequired)
1078{
1079
1080 if (mShaderVersion < versionRequired)
1081 {
1082 error(location, "invalid layout qualifier:", layoutQualifierName.c_str(), "not supported");
1083 recover();
1084 }
1085}
1086
1087bool TParseContext::layoutWorkGroupSizeErrorCheck(const TSourceLoc &location,
1088 const TLayoutQualifier &layoutQualifier)
1089{
1090 const TLocalSize &localSize = layoutQualifier.localSize;
1091 for (size_t i = 0u; i < localSize.size(); ++i)
1092 {
1093 if (localSize[i] != -1)
1094 {
1095 error(location, "invalid layout qualifier:", getLocalSizeString(i),
1096 "only valid when used with 'in' in a compute shader global layout declaration");
1097 return true;
1098 }
1099 }
1100
1101 return false;
1102}
1103
Jamie Madillb98c3a82015-07-23 14:26:04 -04001104bool TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate,
1105 TIntermAggregate *aggregate)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001106{
1107 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1108 {
1109 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1110 if (qual == EvqOut || qual == EvqInOut)
1111 {
1112 TIntermTyped *node = (*(aggregate->getSequence()))[i]->getAsTyped();
1113 if (lValueErrorCheck(node->getLine(), "assign", node))
1114 {
1115 error(node->getLine(),
Jamie Madillb98c3a82015-07-23 14:26:04 -04001116 "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error");
Olli Etuahob6e07a62015-02-16 12:22:10 +02001117 recover();
1118 return true;
1119 }
1120 }
1121 }
1122 return false;
1123}
1124
Jamie Madillb98c3a82015-07-23 14:26:04 -04001125void TParseContext::es3InvariantErrorCheck(const TQualifier qualifier,
1126 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001127{
1128 if (!sh::IsVaryingOut(qualifier) && qualifier != EvqFragmentOut)
1129 {
1130 error(invariantLocation, "Only out variables can be invariant.", "invariant");
1131 recover();
1132 }
1133}
1134
Arun Patole7e7e68d2015-05-22 12:02:25 +05301135bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001136{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001137 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001138 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1139 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001140}
1141
Arun Patole7e7e68d2015-05-22 12:02:25 +05301142bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001143{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001144 return ::IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001145}
1146
Jamie Madillb98c3a82015-07-23 14:26:04 -04001147void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1148 const char *extName,
1149 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001150{
1151 pp::SourceLocation srcLoc;
1152 srcLoc.file = loc.first_file;
1153 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001154 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001155}
1156
Jamie Madillb98c3a82015-07-23 14:26:04 -04001157void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1158 const char *name,
1159 const char *value,
1160 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001161{
1162 pp::SourceLocation srcLoc;
1163 srcLoc.file = loc.first_file;
1164 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001165 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001166}
1167
Martin Radev802abe02016-08-04 17:48:32 +03001168TLocalSize TParseContext::getComputeShaderLocalSize() const
1169{
1170 TLocalSize result;
1171 for (size_t i = 0u; i < result.size(); ++i)
1172 {
1173 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1174 {
1175 result[i] = 1;
1176 }
1177 else
1178 {
1179 result[i] = mComputeShaderLocalSize[i];
1180 }
1181 }
1182 return result;
1183}
1184
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001185/////////////////////////////////////////////////////////////////////////////////
1186//
1187// Non-Errors.
1188//
1189/////////////////////////////////////////////////////////////////////////////////
1190
Jamie Madill5c097022014-08-20 16:38:32 -04001191const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1192 const TString *name,
1193 const TSymbol *symbol)
1194{
1195 const TVariable *variable = NULL;
1196
1197 if (!symbol)
1198 {
1199 error(location, "undeclared identifier", name->c_str());
1200 recover();
1201 }
1202 else if (!symbol->isVariable())
1203 {
1204 error(location, "variable expected", name->c_str());
1205 recover();
1206 }
1207 else
1208 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001209 variable = static_cast<const TVariable *>(symbol);
Jamie Madill5c097022014-08-20 16:38:32 -04001210
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001211 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
Jamie Madill5c097022014-08-20 16:38:32 -04001212 !variable->getExtension().empty() &&
1213 extensionErrorCheck(location, variable->getExtension()))
1214 {
1215 recover();
1216 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001217
1218 // Reject shaders using both gl_FragData and gl_FragColor
1219 TQualifier qualifier = variable->getType().getQualifier();
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001220 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001221 {
1222 mUsesFragData = true;
1223 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001224 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001225 {
1226 mUsesFragColor = true;
1227 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001228 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1229 {
1230 mUsesSecondaryOutputs = true;
1231 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001232
1233 // This validation is not quite correct - it's only an error to write to
1234 // both FragData and FragColor. For simplicity, and because users shouldn't
1235 // be rewarded for reading from undefined varaibles, return an error
1236 // if they are both referenced, rather than assigned.
1237 if (mUsesFragData && mUsesFragColor)
1238 {
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001239 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1240 if (mUsesSecondaryOutputs)
1241 {
1242 errorMessage =
1243 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1244 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1245 }
1246 error(location, errorMessage, name->c_str());
Jamie Madill14e95b32015-05-07 10:10:41 -04001247 recover();
1248 }
Jamie Madill5c097022014-08-20 16:38:32 -04001249 }
1250
1251 if (!variable)
1252 {
1253 TType type(EbtFloat, EbpUndefined);
1254 TVariable *fakeVariable = new TVariable(name, type);
1255 symbolTable.declare(fakeVariable);
1256 variable = fakeVariable;
1257 }
1258
1259 return variable;
1260}
1261
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001262TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1263 const TString *name,
1264 const TSymbol *symbol)
1265{
1266 const TVariable *variable = getNamedVariable(location, name, symbol);
1267
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001268 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001269 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001270 const TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001271 return intermediate.addConstantUnion(constArray, variable->getType(), location);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001272 }
1273 else
1274 {
1275 return intermediate.addSymbol(variable->getUniqueId(), variable->getName(),
1276 variable->getType(), location);
1277 }
1278}
1279
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001280//
1281// Look up a function name in the symbol table, and make sure it is a function.
1282//
1283// Return the function symbol if found, otherwise 0.
1284//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001285const TFunction *TParseContext::findFunction(const TSourceLoc &line,
1286 TFunction *call,
1287 int inputShaderVersion,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301288 bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001289{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001290 // First find by unmangled name to check whether the function name has been
1291 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001292 // If a function is found, check for one with a matching argument list.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301293 const TSymbol *symbol = symbolTable.find(call->getName(), inputShaderVersion, builtIn);
1294 if (symbol == 0 || symbol->isFunction())
1295 {
Austin Kinross3ae64652015-01-26 15:51:39 -08001296 symbol = symbolTable.find(call->getMangledName(), inputShaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001297 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001298
Arun Patole7e7e68d2015-05-22 12:02:25 +05301299 if (symbol == 0)
1300 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001301 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001302 return 0;
1303 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001304
Arun Patole7e7e68d2015-05-22 12:02:25 +05301305 if (!symbol->isFunction())
1306 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001307 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001308 return 0;
1309 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001310
Jamie Madillb98c3a82015-07-23 14:26:04 -04001311 return static_cast<const TFunction *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001312}
1313
1314//
1315// Initializers show up in several places in the grammar. Have one set of
1316// code to handle them here.
1317//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001318// Returns true on error, false if no error
1319//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001320bool TParseContext::executeInitializer(const TSourceLoc &line,
1321 const TString &identifier,
1322 const TPublicType &pType,
1323 TIntermTyped *initializer,
1324 TIntermNode **intermNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001325{
Olli Etuahoe7847b02015-03-16 11:56:12 +02001326 ASSERT(intermNode != nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001327 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001328
Olli Etuaho2935c582015-04-08 14:32:06 +03001329 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001330 if (type.isUnsizedArray())
1331 {
1332 type.setArraySize(initializer->getArraySize());
1333 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001334 if (!declareVariable(line, identifier, type, &variable))
1335 {
1336 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001337 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001338
Olli Etuahob0c645e2015-05-12 14:25:36 +03001339 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001340 if (symbolTable.atGlobalLevel() &&
1341 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001342 {
1343 // Error message does not completely match behavior with ESSL 1.00, but
1344 // we want to steer developers towards only using constant expressions.
1345 error(line, "global variable initializers must be constant expressions", "=");
1346 return true;
1347 }
1348 if (globalInitWarning)
1349 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001350 warning(
1351 line,
1352 "global variable initializers should be constant expressions "
1353 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1354 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001355 }
1356
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001357 //
1358 // identifier must be of type constant, a global, or a temporary
1359 //
1360 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301361 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1362 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001363 error(line, " cannot initialize this type of qualifier ",
1364 variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001365 return true;
1366 }
1367 //
1368 // test for and propagate constant
1369 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001370
Arun Patole7e7e68d2015-05-22 12:02:25 +05301371 if (qualifier == EvqConst)
1372 {
1373 if (qualifier != initializer->getType().getQualifier())
1374 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001375 std::stringstream extraInfoStream;
1376 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1377 std::string extraInfo = extraInfoStream.str();
1378 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001379 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001380 return true;
1381 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301382 if (type != initializer->getType())
1383 {
1384 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001385 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001386 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001387 return true;
1388 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001389
1390 // Save the constant folded value to the variable if possible. For example array
1391 // initializers are not folded, since that way copying the array literal to multiple places
1392 // in the shader is avoided.
1393 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1394 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301395 if (initializer->getAsConstantUnion())
1396 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001397 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001398 *intermNode = nullptr;
1399 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301400 }
1401 else if (initializer->getAsSymbolNode())
1402 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001403 const TSymbol *symbol =
1404 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1405 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001406
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001407 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001408 if (constArray)
1409 {
1410 variable->shareConstPointer(constArray);
1411 *intermNode = nullptr;
1412 return false;
1413 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001414 }
1415 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001416
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001417 TIntermSymbol *intermSymbol = intermediate.addSymbol(
1418 variable->getUniqueId(), variable->getName(), variable->getType(), line);
1419 *intermNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1420 if (*intermNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001421 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001422 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1423 return true;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001424 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001425
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001426 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001427}
1428
Jamie Madillb98c3a82015-07-23 14:26:04 -04001429TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier,
1430 bool invariant,
1431 TLayoutQualifier layoutQualifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301432 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001433{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001434 TPublicType returnType = typeSpecifier;
1435 returnType.qualifier = qualifier;
1436 returnType.invariant = invariant;
Jamie Madilla5efff92013-06-06 11:56:47 -04001437 returnType.layoutQualifier = layoutQualifier;
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001438
Martin Radev802abe02016-08-04 17:48:32 +03001439 if (layoutWorkGroupSizeErrorCheck(typeSpecifier.line, layoutQualifier))
1440 {
1441 recover();
1442 }
1443
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001444 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001445 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001446 if (typeSpecifier.array)
1447 {
1448 error(typeSpecifier.line, "not supported", "first-class array");
1449 recover();
1450 returnType.clearArrayness();
1451 }
1452
Jamie Madillb98c3a82015-07-23 14:26:04 -04001453 if (qualifier == EvqAttribute &&
1454 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001455 {
1456 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1457 recover();
1458 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001459
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001460 if ((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1461 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1462 {
1463 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1464 recover();
1465 }
1466 }
1467 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001468 {
Olli Etuahoabb0c382015-07-13 12:01:12 +03001469 if (!layoutQualifier.isEmpty())
1470 {
1471 if (globalErrorCheck(typeSpecifier.line, symbolTable.atGlobalLevel(), "layout"))
1472 {
1473 recover();
1474 }
1475 }
Olli Etuahocc36b982015-07-10 14:14:18 +03001476 if (sh::IsVarying(qualifier) || qualifier == EvqVertexIn || qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001477 {
Olli Etuahocc36b982015-07-10 14:14:18 +03001478 es3InputOutputTypeCheck(qualifier, typeSpecifier, typeSpecifier.line);
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001479 }
Martin Radev802abe02016-08-04 17:48:32 +03001480 if (qualifier == EvqComputeIn)
1481 {
1482 error(typeSpecifier.line, "'in' can be only used to specify the local group size",
1483 "in");
1484 recover();
1485 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001486 }
1487
1488 return returnType;
1489}
1490
Olli Etuahocc36b982015-07-10 14:14:18 +03001491void TParseContext::es3InputOutputTypeCheck(const TQualifier qualifier,
1492 const TPublicType &type,
1493 const TSourceLoc &qualifierLocation)
1494{
1495 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
1496 if (type.type == EbtBool)
1497 {
1498 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
1499 recover();
1500 }
1501
1502 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
1503 switch (qualifier)
1504 {
1505 case EvqVertexIn:
1506 // ESSL 3.00 section 4.3.4
1507 if (type.array)
1508 {
1509 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
1510 recover();
1511 }
1512 // Vertex inputs with a struct type are disallowed in singleDeclarationErrorCheck
1513 return;
1514 case EvqFragmentOut:
1515 // ESSL 3.00 section 4.3.6
1516 if (type.isMatrix())
1517 {
1518 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
1519 recover();
1520 }
1521 // Fragment outputs with a struct type are disallowed in singleDeclarationErrorCheck
1522 return;
1523 default:
1524 break;
1525 }
1526
1527 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
1528 // restrictions.
1529 bool typeContainsIntegers =
1530 (type.type == EbtInt || type.type == EbtUInt || type.isStructureContainingType(EbtInt) ||
1531 type.isStructureContainingType(EbtUInt));
1532 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
1533 {
1534 error(qualifierLocation, "must use 'flat' interpolation here",
1535 getQualifierString(qualifier));
1536 recover();
1537 }
1538
1539 if (type.type == EbtStruct)
1540 {
1541 // ESSL 3.00 sections 4.3.4 and 4.3.6.
1542 // These restrictions are only implied by the ESSL 3.00 spec, but
1543 // the ESSL 3.10 spec lists these restrictions explicitly.
1544 if (type.array)
1545 {
1546 error(qualifierLocation, "cannot be an array of structures",
1547 getQualifierString(qualifier));
1548 recover();
1549 }
1550 if (type.isStructureContainingArrays())
1551 {
1552 error(qualifierLocation, "cannot be a structure containing an array",
1553 getQualifierString(qualifier));
1554 recover();
1555 }
1556 if (type.isStructureContainingType(EbtStruct))
1557 {
1558 error(qualifierLocation, "cannot be a structure containing a structure",
1559 getQualifierString(qualifier));
1560 recover();
1561 }
1562 if (type.isStructureContainingType(EbtBool))
1563 {
1564 error(qualifierLocation, "cannot be a structure containing a bool",
1565 getQualifierString(qualifier));
1566 recover();
1567 }
1568 }
1569}
1570
Olli Etuahofa33d582015-04-09 14:33:12 +03001571TIntermAggregate *TParseContext::parseSingleDeclaration(TPublicType &publicType,
1572 const TSourceLoc &identifierOrTypeLocation,
1573 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001574{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001575 TIntermSymbol *symbol =
1576 intermediate.addSymbol(0, identifier, TType(publicType), identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001577
Olli Etuahobab4c082015-04-24 16:38:49 +03001578 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03001579
Olli Etuahobab4c082015-04-24 16:38:49 +03001580 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1581
1582 if (emptyDeclaration)
1583 {
1584 if (publicType.isUnsizedArray())
1585 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001586 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1587 // error. It is assumed that this applies to empty declarations as well.
1588 error(identifierOrTypeLocation, "empty array declaration needs to specify a size",
1589 identifier.c_str());
Olli Etuahobab4c082015-04-24 16:38:49 +03001590 }
1591 }
1592 else
Jamie Madill60ed9812013-06-06 11:56:46 -04001593 {
Olli Etuahofa33d582015-04-09 14:33:12 +03001594 if (singleDeclarationErrorCheck(publicType, identifierOrTypeLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001595 recover();
1596
Olli Etuaho376f1b52015-04-13 13:23:41 +03001597 if (nonInitErrorCheck(identifierOrTypeLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001598 recover();
1599
Olli Etuaho2935c582015-04-08 14:32:06 +03001600 TVariable *variable = nullptr;
Olli Etuahofa33d582015-04-09 14:33:12 +03001601 if (!declareVariable(identifierOrTypeLocation, identifier, TType(publicType), &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001602 recover();
1603
1604 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001605 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001606 }
1607
Olli Etuahoe7847b02015-03-16 11:56:12 +02001608 return intermediate.makeAggregate(symbol, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001609}
1610
Olli Etuahoe7847b02015-03-16 11:56:12 +02001611TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1612 const TSourceLoc &identifierLocation,
1613 const TString &identifier,
1614 const TSourceLoc &indexLocation,
1615 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001616{
Olli Etuahofa33d582015-04-09 14:33:12 +03001617 mDeferredSingleDeclarationErrorCheck = false;
1618
1619 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001620 recover();
1621
Olli Etuaho376f1b52015-04-13 13:23:41 +03001622 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001623 recover();
1624
Jamie Madillb98c3a82015-07-23 14:26:04 -04001625 if (arrayTypeErrorCheck(indexLocation, publicType) ||
1626 arrayQualifierErrorCheck(indexLocation, publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001627 {
1628 recover();
1629 }
1630
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001631 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001632
1633 int size;
1634 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1635 {
1636 recover();
1637 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001638 // Make the type an array even if size check failed.
1639 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1640 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001641
Olli Etuaho2935c582015-04-08 14:32:06 +03001642 TVariable *variable = nullptr;
1643 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001644 recover();
1645
Olli Etuahoe7847b02015-03-16 11:56:12 +02001646 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001647 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001648 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001649
Olli Etuahoe7847b02015-03-16 11:56:12 +02001650 return intermediate.makeAggregate(symbol, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001651}
1652
Jamie Madill06145232015-05-13 13:10:01 -04001653TIntermAggregate *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001654 const TSourceLoc &identifierLocation,
1655 const TString &identifier,
1656 const TSourceLoc &initLocation,
1657 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001658{
Olli Etuahofa33d582015-04-09 14:33:12 +03001659 mDeferredSingleDeclarationErrorCheck = false;
1660
1661 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001662 recover();
1663
Olli Etuahoe7847b02015-03-16 11:56:12 +02001664 TIntermNode *intermNode = nullptr;
1665 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04001666 {
1667 //
1668 // Build intermediate representation
1669 //
Olli Etuahoe7847b02015-03-16 11:56:12 +02001670 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001671 }
1672 else
1673 {
1674 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001675 return nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001676 }
1677}
1678
Jamie Madillb98c3a82015-07-23 14:26:04 -04001679TIntermAggregate *TParseContext::parseSingleArrayInitDeclaration(
1680 TPublicType &publicType,
1681 const TSourceLoc &identifierLocation,
1682 const TString &identifier,
1683 const TSourceLoc &indexLocation,
1684 TIntermTyped *indexExpression,
1685 const TSourceLoc &initLocation,
1686 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001687{
1688 mDeferredSingleDeclarationErrorCheck = false;
1689
1690 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1691 recover();
1692
Jamie Madillb98c3a82015-07-23 14:26:04 -04001693 if (arrayTypeErrorCheck(indexLocation, publicType) ||
1694 arrayQualifierErrorCheck(indexLocation, publicType))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001695 {
1696 recover();
1697 }
1698
1699 TPublicType arrayType(publicType);
1700
Olli Etuaho376f1b52015-04-13 13:23:41 +03001701 int size = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001702 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
1703 // the initializer.
1704 if (indexExpression != nullptr &&
1705 arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001706 {
1707 recover();
1708 }
1709 // Make the type an array even if size check failed.
1710 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1711 arrayType.setArraySize(size);
1712
1713 // initNode will correspond to the whole of "type b[n] = initializer".
1714 TIntermNode *initNode = nullptr;
1715 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1716 {
1717 return initNode ? intermediate.makeAggregate(initNode, initLocation) : nullptr;
1718 }
1719 else
1720 {
1721 recover();
1722 return nullptr;
1723 }
1724}
1725
Olli Etuahoe7847b02015-03-16 11:56:12 +02001726TIntermAggregate *TParseContext::parseInvariantDeclaration(const TSourceLoc &invariantLoc,
Jamie Madill47e3ec02014-08-20 16:38:33 -04001727 const TSourceLoc &identifierLoc,
1728 const TString *identifier,
1729 const TSymbol *symbol)
1730{
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001731 // invariant declaration
Jamie Madill47e3ec02014-08-20 16:38:33 -04001732 if (globalErrorCheck(invariantLoc, symbolTable.atGlobalLevel(), "invariant varying"))
1733 {
1734 recover();
1735 }
1736
1737 if (!symbol)
1738 {
1739 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
1740 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001741 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001742 }
1743 else
1744 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001745 const TString kGlFrontFacing("gl_FrontFacing");
1746 if (*identifier == kGlFrontFacing)
1747 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001748 error(identifierLoc, "identifier should not be declared as invariant",
1749 identifier->c_str());
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001750 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001751 return nullptr;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001752 }
Jamie Madill2c433252014-12-03 12:36:54 -05001753 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001754 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1755 ASSERT(variable);
1756 const TType &type = variable->getType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04001757 TIntermSymbol *intermSymbol =
1758 intermediate.addSymbol(variable->getUniqueId(), *identifier, type, identifierLoc);
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001759
1760 TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc);
1761 aggregate->setOp(EOpInvariantDeclaration);
1762 return aggregate;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001763 }
1764}
1765
Jamie Madillb98c3a82015-07-23 14:26:04 -04001766TIntermAggregate *TParseContext::parseDeclarator(TPublicType &publicType,
1767 TIntermAggregate *aggregateDeclaration,
1768 const TSourceLoc &identifierLocation,
1769 const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001770{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001771 // If the declaration starting this declarator list was empty (example: int,), some checks were
1772 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001773 if (mDeferredSingleDeclarationErrorCheck)
1774 {
1775 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1776 recover();
1777 mDeferredSingleDeclarationErrorCheck = false;
1778 }
1779
Jamie Madill0bd18df2013-06-20 11:55:52 -04001780 if (locationDeclaratorListCheck(identifierLocation, publicType))
1781 recover();
1782
Olli Etuaho376f1b52015-04-13 13:23:41 +03001783 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001784 recover();
1785
Olli Etuaho2935c582015-04-08 14:32:06 +03001786 TVariable *variable = nullptr;
1787 if (!declareVariable(identifierLocation, identifier, TType(publicType), &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001788 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001789
Jamie Madillb98c3a82015-07-23 14:26:04 -04001790 TIntermSymbol *symbol =
1791 intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001792 if (variable && symbol)
Jamie Madill502d66f2013-06-20 11:55:52 -04001793 symbol->setId(variable->getUniqueId());
1794
Olli Etuahoe7847b02015-03-16 11:56:12 +02001795 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001796}
1797
Jamie Madillb98c3a82015-07-23 14:26:04 -04001798TIntermAggregate *TParseContext::parseArrayDeclarator(TPublicType &publicType,
1799 TIntermAggregate *aggregateDeclaration,
1800 const TSourceLoc &identifierLocation,
1801 const TString &identifier,
1802 const TSourceLoc &arrayLocation,
1803 TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001804{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001805 // If the declaration starting this declarator list was empty (example: int,), some checks were
1806 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001807 if (mDeferredSingleDeclarationErrorCheck)
1808 {
1809 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1810 recover();
1811 mDeferredSingleDeclarationErrorCheck = false;
1812 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001813
Jamie Madill0bd18df2013-06-20 11:55:52 -04001814 if (locationDeclaratorListCheck(identifierLocation, publicType))
1815 recover();
1816
Olli Etuaho376f1b52015-04-13 13:23:41 +03001817 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001818 recover();
1819
Jamie Madillb98c3a82015-07-23 14:26:04 -04001820 if (arrayTypeErrorCheck(arrayLocation, publicType) ||
1821 arrayQualifierErrorCheck(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001822 {
1823 recover();
1824 }
Olli Etuaho93a90fd2015-04-07 18:14:07 +03001825 else
Jamie Madill502d66f2013-06-20 11:55:52 -04001826 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001827 TType arrayType = TType(publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04001828 int size;
1829 if (arraySizeErrorCheck(arrayLocation, indexExpression, size))
Olli Etuahoe7847b02015-03-16 11:56:12 +02001830 {
Jamie Madill502d66f2013-06-20 11:55:52 -04001831 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001832 }
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001833 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001834
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001835 TVariable *variable = nullptr;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001836 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001837 recover();
Jamie Madill502d66f2013-06-20 11:55:52 -04001838
Jamie Madillb98c3a82015-07-23 14:26:04 -04001839 TIntermSymbol *symbol =
1840 intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001841 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001842 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001843
1844 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001845 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001846
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001847 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001848}
1849
Jamie Madillb98c3a82015-07-23 14:26:04 -04001850TIntermAggregate *TParseContext::parseInitDeclarator(const TPublicType &publicType,
1851 TIntermAggregate *aggregateDeclaration,
1852 const TSourceLoc &identifierLocation,
1853 const TString &identifier,
1854 const TSourceLoc &initLocation,
1855 TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001856{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001857 // If the declaration starting this declarator list was empty (example: int,), some checks were
1858 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001859 if (mDeferredSingleDeclarationErrorCheck)
1860 {
1861 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1862 recover();
1863 mDeferredSingleDeclarationErrorCheck = false;
1864 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001865
Jamie Madill0bd18df2013-06-20 11:55:52 -04001866 if (locationDeclaratorListCheck(identifierLocation, publicType))
1867 recover();
1868
Olli Etuahoe7847b02015-03-16 11:56:12 +02001869 TIntermNode *intermNode = nullptr;
1870 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04001871 {
1872 //
1873 // build the intermediate representation
1874 //
1875 if (intermNode)
1876 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001877 return intermediate.growAggregate(aggregateDeclaration, intermNode, initLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001878 }
1879 else
1880 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001881 return aggregateDeclaration;
Jamie Madill502d66f2013-06-20 11:55:52 -04001882 }
1883 }
1884 else
1885 {
1886 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001887 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001888 }
1889}
1890
Jamie Madill06145232015-05-13 13:10:01 -04001891TIntermAggregate *TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001892 TIntermAggregate *aggregateDeclaration,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301893 const TSourceLoc &identifierLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001894 const TString &identifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301895 const TSourceLoc &indexLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001896 TIntermTyped *indexExpression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001897 const TSourceLoc &initLocation,
1898 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001899{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001900 // If the declaration starting this declarator list was empty (example: int,), some checks were
1901 // not performed.
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001902 if (mDeferredSingleDeclarationErrorCheck)
1903 {
1904 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1905 recover();
1906 mDeferredSingleDeclarationErrorCheck = false;
1907 }
1908
1909 if (locationDeclaratorListCheck(identifierLocation, publicType))
1910 recover();
1911
Jamie Madillb98c3a82015-07-23 14:26:04 -04001912 if (arrayTypeErrorCheck(indexLocation, publicType) ||
1913 arrayQualifierErrorCheck(indexLocation, publicType))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001914 {
1915 recover();
1916 }
1917
1918 TPublicType arrayType(publicType);
1919
Olli Etuaho376f1b52015-04-13 13:23:41 +03001920 int size = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001921 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
1922 // the initializer.
1923 if (indexExpression != nullptr &&
1924 arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001925 {
1926 recover();
1927 }
1928 // Make the type an array even if size check failed.
1929 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1930 arrayType.setArraySize(size);
1931
1932 // initNode will correspond to the whole of "b[n] = initializer".
1933 TIntermNode *initNode = nullptr;
1934 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1935 {
1936 if (initNode)
1937 {
1938 return intermediate.growAggregate(aggregateDeclaration, initNode, initLocation);
1939 }
1940 else
1941 {
1942 return aggregateDeclaration;
1943 }
1944 }
1945 else
1946 {
1947 recover();
1948 return nullptr;
1949 }
1950}
1951
Jamie Madilla295edf2013-06-06 11:56:48 -04001952void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1953{
Jamie Madilla295edf2013-06-06 11:56:48 -04001954
1955 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04001956
1957 // It should never be the case, but some strange parser errors can send us here.
1958 if (layoutQualifier.isEmpty())
1959 {
1960 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
1961 recover();
1962 return;
1963 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001964
Martin Radev802abe02016-08-04 17:48:32 +03001965 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04001966 {
Martin Radev802abe02016-08-04 17:48:32 +03001967 error(typeQualifier.line, "invalid combination:", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04001968 recover();
1969 return;
1970 }
1971
Martin Radev802abe02016-08-04 17:48:32 +03001972 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04001973 {
Martin Radev802abe02016-08-04 17:48:32 +03001974 if (mComputeShaderLocalSizeDeclared &&
1975 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
1976 {
1977 error(typeQualifier.line, "Work group size does not match the previous declaration",
1978 "layout");
1979 recover();
1980 return;
1981 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001982
Martin Radev802abe02016-08-04 17:48:32 +03001983 if (mShaderVersion < 310)
1984 {
1985 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
1986 recover();
1987 return;
1988 }
Jamie Madill099c0f32013-06-20 11:55:52 -04001989
Martin Radev802abe02016-08-04 17:48:32 +03001990 if (!layoutQualifier.isGroupSizeSpecified())
1991 {
1992 error(typeQualifier.line, "No local work group size specified", "layout");
1993 recover();
1994 return;
1995 }
1996
1997 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
1998 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
1999
2000 const TConstantUnion *maxComputeWorkGroupSizeData =
2001 maxComputeWorkGroupSize->getConstPointer();
2002
2003 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
2004 {
2005 if (layoutQualifier.localSize[i] != -1)
2006 {
2007 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
2008 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
2009 if (mComputeShaderLocalSize[i] < 1 ||
2010 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
2011 {
2012 std::stringstream errorMessageStream;
2013 errorMessageStream << "Value must be at least 1 and no greater than "
2014 << maxComputeWorkGroupSizeValue;
2015 const std::string &errorMessage = errorMessageStream.str();
2016
2017 error(typeQualifier.line, "invalid value:", getLocalSizeString(i),
2018 errorMessage.c_str());
2019 recover();
2020 return;
2021 }
2022 }
2023 }
2024
2025 mComputeShaderLocalSizeDeclared = true;
2026 }
2027 else
Jamie Madill1566ef72013-06-20 11:55:54 -04002028 {
Martin Radev802abe02016-08-04 17:48:32 +03002029
2030 if (layoutWorkGroupSizeErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
2031 {
2032 recover();
2033 return;
2034 }
2035
2036 if (typeQualifier.qualifier != EvqUniform)
2037 {
2038 error(typeQualifier.line, "invalid qualifier:",
2039 getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
2040 recover();
2041 return;
2042 }
2043
2044 if (mShaderVersion < 300)
2045 {
2046 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
2047 "layout");
2048 recover();
2049 return;
2050 }
2051
2052 if (layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
2053 {
2054 recover();
2055 return;
2056 }
2057
2058 if (layoutQualifier.matrixPacking != EmpUnspecified)
2059 {
2060 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
2061 }
2062
2063 if (layoutQualifier.blockStorage != EbsUnspecified)
2064 {
2065 mDefaultBlockStorage = layoutQualifier.blockStorage;
2066 }
Jamie Madill1566ef72013-06-20 11:55:54 -04002067 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002068}
2069
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002070TIntermAggregate *TParseContext::addFunctionPrototypeDeclaration(const TFunction &function,
2071 const TSourceLoc &location)
2072{
Olli Etuaho5d653182016-01-04 14:43:28 +02002073 // Note: symbolTableFunction could be the same as function if this is the first declaration.
2074 // Either way the instance in the symbol table is used to track whether the function is declared
2075 // multiple times.
2076 TFunction *symbolTableFunction =
2077 static_cast<TFunction *>(symbolTable.find(function.getMangledName(), getShaderVersion()));
2078 if (symbolTableFunction->hasPrototypeDeclaration() && mShaderVersion == 100)
2079 {
2080 // ESSL 1.00.17 section 4.2.7.
2081 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
2082 error(location, "duplicate function prototype declarations are not allowed", "function");
2083 recover();
2084 }
2085 symbolTableFunction->setHasPrototypeDeclaration();
2086
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002087 TIntermAggregate *prototype = new TIntermAggregate;
2088 prototype->setType(function.getReturnType());
2089 prototype->setName(function.getMangledName());
2090 prototype->setFunctionId(function.getUniqueId());
2091
2092 for (size_t i = 0; i < function.getParamCount(); i++)
2093 {
2094 const TConstParameter &param = function.getParam(i);
2095 if (param.name != 0)
2096 {
2097 TVariable variable(param.name, *param.type);
2098
2099 TIntermSymbol *paramSymbol = intermediate.addSymbol(
2100 variable.getUniqueId(), variable.getName(), variable.getType(), location);
2101 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
2102 }
2103 else
2104 {
2105 TIntermSymbol *paramSymbol = intermediate.addSymbol(0, "", *param.type, location);
2106 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
2107 }
2108 }
2109
2110 prototype->setOp(EOpPrototype);
2111
2112 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002113
2114 if (!symbolTable.atGlobalLevel())
2115 {
2116 // ESSL 3.00.4 section 4.2.4.
2117 error(location, "local function prototype declarations are not allowed", "function");
2118 recover();
2119 }
2120
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002121 return prototype;
2122}
2123
2124TIntermAggregate *TParseContext::addFunctionDefinition(const TFunction &function,
2125 TIntermAggregate *functionPrototype,
2126 TIntermAggregate *functionBody,
2127 const TSourceLoc &location)
2128{
2129 //?? Check that all paths return a value if return type != void ?
2130 // May be best done as post process phase on intermediate code
2131 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
2132 {
2133 error(location, "function does not return a value:", "", function.getName().c_str());
2134 recover();
2135 }
2136
2137 TIntermAggregate *aggregate =
2138 intermediate.growAggregate(functionPrototype, functionBody, location);
2139 intermediate.setAggregateOperator(aggregate, EOpFunction, location);
2140 aggregate->setName(function.getMangledName().c_str());
2141 aggregate->setType(function.getReturnType());
2142 aggregate->setFunctionId(function.getUniqueId());
2143
2144 symbolTable.pop();
2145 return aggregate;
2146}
2147
Jamie Madill185fb402015-06-12 15:48:48 -04002148void TParseContext::parseFunctionPrototype(const TSourceLoc &location,
2149 TFunction *function,
2150 TIntermAggregate **aggregateOut)
2151{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002152 const TSymbol *builtIn =
2153 symbolTable.findBuiltIn(function->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002154
2155 if (builtIn)
2156 {
2157 error(location, "built-in functions cannot be redefined", function->getName().c_str());
2158 recover();
2159 }
2160
Jamie Madillb98c3a82015-07-23 14:26:04 -04002161 TFunction *prevDec =
2162 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Jamie Madill185fb402015-06-12 15:48:48 -04002163 //
2164 // Note: 'prevDec' could be 'function' if this is the first time we've seen function
2165 // as it would have just been put in the symbol table. Otherwise, we're looking up
2166 // an earlier occurance.
2167 //
2168 if (prevDec->isDefined())
2169 {
2170 // Then this function already has a body.
2171 error(location, "function already has a body", function->getName().c_str());
2172 recover();
2173 }
2174 prevDec->setDefined();
2175 //
2176 // Overload the unique ID of the definition to be the same unique ID as the declaration.
2177 // Eventually we will probably want to have only a single definition and just swap the
2178 // arguments to be the definition's arguments.
2179 //
2180 function->setUniqueId(prevDec->getUniqueId());
2181
2182 // Raise error message if main function takes any parameters or return anything other than void
2183 if (function->getName() == "main")
2184 {
2185 if (function->getParamCount() > 0)
2186 {
2187 error(location, "function cannot take any parameter(s)", function->getName().c_str());
2188 recover();
2189 }
2190 if (function->getReturnType().getBasicType() != EbtVoid)
2191 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002192 error(location, "", function->getReturnType().getBasicString(),
2193 "main function cannot return a value");
Jamie Madill185fb402015-06-12 15:48:48 -04002194 recover();
2195 }
2196 }
2197
2198 //
2199 // Remember the return type for later checking for RETURN statements.
2200 //
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002201 mCurrentFunctionType = &(prevDec->getReturnType());
2202 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04002203
2204 //
2205 // Insert parameters into the symbol table.
2206 // If the parameter has no name, it's not an error, just don't insert it
2207 // (could be used for unused args).
2208 //
2209 // Also, accumulate the list of parameters into the HIL, so lower level code
2210 // knows where to find parameters.
2211 //
2212 TIntermAggregate *paramNodes = new TIntermAggregate;
2213 for (size_t i = 0; i < function->getParamCount(); i++)
2214 {
2215 const TConstParameter &param = function->getParam(i);
2216 if (param.name != 0)
2217 {
2218 TVariable *variable = new TVariable(param.name, *param.type);
2219 //
2220 // Insert the parameters with name in the symbol table.
2221 //
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002222 if (!symbolTable.declare(variable))
2223 {
Jamie Madill185fb402015-06-12 15:48:48 -04002224 error(location, "redefinition", variable->getName().c_str());
2225 recover();
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002226 paramNodes = intermediate.growAggregate(
2227 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
2228 continue;
Jamie Madill185fb402015-06-12 15:48:48 -04002229 }
2230
2231 //
2232 // Add the parameter to the HIL
2233 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002234 TIntermSymbol *symbol = intermediate.addSymbol(
2235 variable->getUniqueId(), variable->getName(), variable->getType(), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002236
2237 paramNodes = intermediate.growAggregate(paramNodes, symbol, location);
2238 }
2239 else
2240 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002241 paramNodes = intermediate.growAggregate(
2242 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002243 }
2244 }
2245 intermediate.setAggregateOperator(paramNodes, EOpParameters, location);
2246 *aggregateOut = paramNodes;
2247 setLoopNestingLevel(0);
2248}
2249
Jamie Madillb98c3a82015-07-23 14:26:04 -04002250TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002251{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002252 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002253 // We don't know at this point whether this is a function definition or a prototype.
2254 // The definition production code will check for redefinitions.
2255 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002256 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002257 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
2258 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002259 //
2260 TFunction *prevDec =
2261 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302262
2263 if (getShaderVersion() >= 300 && symbolTable.hasUnmangledBuiltIn(function->getName().c_str()))
2264 {
2265 // With ESSL 3.00, names of built-in functions cannot be redeclared as functions.
2266 // Therefore overloading or redefining builtin functions is an error.
2267 error(location, "Name of a built-in function cannot be redeclared as function",
2268 function->getName().c_str());
2269 recover();
2270 }
2271 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002272 {
2273 if (prevDec->getReturnType() != function->getReturnType())
2274 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002275 error(location, "overloaded functions must have the same return type",
Jamie Madill185fb402015-06-12 15:48:48 -04002276 function->getReturnType().getBasicString());
2277 recover();
2278 }
2279 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2280 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002281 if (prevDec->getParam(i).type->getQualifier() !=
2282 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002283 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002284 error(location, "overloaded functions must have the same parameter qualifiers",
Jamie Madill185fb402015-06-12 15:48:48 -04002285 function->getParam(i).type->getQualifierString());
2286 recover();
2287 }
2288 }
2289 }
2290
2291 //
2292 // Check for previously declared variables using the same name.
2293 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002294 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002295 if (prevSym)
2296 {
2297 if (!prevSym->isFunction())
2298 {
2299 error(location, "redefinition", function->getName().c_str(), "function");
2300 recover();
2301 }
2302 }
2303 else
2304 {
2305 // Insert the unmangled name to detect potential future redefinition as a variable.
Jamie Madillb98c3a82015-07-23 14:26:04 -04002306 TFunction *newFunction =
2307 new TFunction(NewPoolTString(function->getName().c_str()), &function->getReturnType());
Jamie Madill185fb402015-06-12 15:48:48 -04002308 symbolTable.getOuterLevel()->insertUnmangled(newFunction);
2309 }
2310
2311 // We're at the inner scope level of the function's arguments and body statement.
2312 // Add the function prototype to the surrounding scope instead.
2313 symbolTable.getOuterLevel()->insert(function);
2314
2315 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002316 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2317 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002318 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2319 //
2320 return function;
2321}
2322
Olli Etuaho9de84a52016-06-14 17:36:01 +03002323TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
2324 const TString *name,
2325 const TSourceLoc &location)
2326{
2327 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
2328 {
2329 error(location, "no qualifiers allowed for function return",
2330 getQualifierString(type.qualifier));
2331 recover();
2332 }
2333 if (!type.layoutQualifier.isEmpty())
2334 {
2335 error(location, "no qualifiers allowed for function return", "layout");
2336 recover();
2337 }
2338 // make sure a sampler is not involved as well...
2339 if (samplerErrorCheck(location, type, "samplers can't be function return values"))
2340 {
2341 recover();
2342 }
Olli Etuahoe29324f2016-06-15 10:58:03 +03002343 if (mShaderVersion < 300)
2344 {
2345 // Array return values are forbidden, but there's also no valid syntax for declaring array
2346 // return values in ESSL 1.00.
2347 ASSERT(type.arraySize == 0 || mDiagnostics.numErrors() > 0);
2348
2349 if (type.isStructureContainingArrays())
2350 {
2351 // ESSL 1.00.17 section 6.1 Function Definitions
2352 error(location, "structures containing arrays can't be function return values",
2353 TType(type).getCompleteString().c_str());
2354 recover();
2355 }
2356 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03002357
2358 // Add the function as a prototype after parsing it (we do not support recursion)
2359 return new TFunction(name, new TType(type));
2360}
2361
Jamie Madill06145232015-05-13 13:10:01 -04002362TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002363{
Jamie Madill06145232015-05-13 13:10:01 -04002364 TPublicType publicType = publicTypeIn;
Olli Etuahobd163f62015-11-13 12:15:38 +02002365 if (publicType.isStructSpecifier)
2366 {
2367 error(publicType.line, "constructor can't be a structure definition",
2368 getBasicString(publicType.type));
2369 recover();
2370 }
2371
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002372 TOperator op = EOpNull;
2373 if (publicType.userDef)
2374 {
2375 op = EOpConstructStruct;
2376 }
2377 else
2378 {
Geoff Lang156d7192016-07-21 16:11:00 -04002379 op = sh::TypeToConstructorOperator(TType(publicType));
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002380 if (op == EOpNull)
2381 {
2382 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
2383 recover();
2384 publicType.type = EbtFloat;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002385 op = EOpConstructFloat;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002386 }
2387 }
2388
2389 TString tempString;
Dmitry Skiba7f17a502015-06-22 15:08:39 -07002390 const TType *type = new TType(publicType);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002391 return new TFunction(&tempString, type, op);
2392}
2393
Jamie Madillb98c3a82015-07-23 14:26:04 -04002394// This function is used to test for the correctness of the parameters passed to various constructor
2395// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002396//
2397// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
2398//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002399TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments,
2400 TType *type,
2401 TOperator op,
2402 TFunction *fnCall,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302403 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002404{
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002405 TIntermAggregate *constructor = arguments->getAsAggregate();
2406 ASSERT(constructor != nullptr);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002407
Olli Etuahof40319e2015-03-10 14:33:00 +02002408 if (type->isArray())
2409 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002410 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
2411 // the array.
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002412 TIntermSequence *args = constructor->getSequence();
Olli Etuahof40319e2015-03-10 14:33:00 +02002413 for (size_t i = 0; i < args->size(); i++)
2414 {
2415 const TType &argType = (*args)[i]->getAsTyped()->getType();
2416 // It has already been checked that the argument is not an array.
2417 ASSERT(!argType.isArray());
2418 if (!argType.sameElementType(*type))
2419 {
2420 error(line, "Array constructor argument has an incorrect type", "Error");
2421 recover();
2422 return nullptr;
2423 }
2424 }
2425 }
2426 else if (op == EOpConstructStruct)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002427 {
2428 const TFieldList &fields = type->getStruct()->fields();
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002429 TIntermSequence *args = constructor->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002430
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002431 for (size_t i = 0; i < fields.size(); i++)
2432 {
Nicolas Capensffd73872014-08-21 13:49:16 -04002433 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002434 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002435 error(line, "Structure constructor arguments do not match structure fields",
2436 "Error");
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002437 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002438
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002439 return 0;
2440 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002441 }
2442 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002443
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002444 // Turn the argument list itself into a constructor
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002445 constructor->setOp(op);
2446 constructor->setLine(line);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002447 ASSERT(constructor->isConstructor());
2448
2449 // Need to set type before setPrecisionFromChildren() because bool doesn't have precision.
2450 constructor->setType(*type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002451
Olli Etuaho21203702014-11-13 16:16:21 +02002452 // Structs should not be precision qualified, the individual members may be.
2453 // Built-in types on the other hand should be precision qualified.
2454 if (op != EOpConstructStruct)
2455 {
2456 constructor->setPrecisionFromChildren();
2457 type->setPrecision(constructor->getPrecision());
2458 }
2459
Olli Etuaho1d122782015-11-06 15:35:17 +02002460 TIntermTyped *constConstructor = intermediate.foldAggregateBuiltIn(constructor);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002461 if (constConstructor)
2462 {
2463 return constConstructor;
2464 }
2465
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002466 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002467}
2468
Olli Etuaho90892fb2016-07-14 14:44:51 +03002469// This function returns vector field(s) being accessed from a constant vector.
2470TIntermConstantUnion *TParseContext::foldVectorSwizzle(TVectorFields &fields,
2471 TIntermConstantUnion *baseNode,
2472 const TSourceLoc &location)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002473{
Olli Etuaho90892fb2016-07-14 14:44:51 +03002474 const TConstantUnion *unionArray = baseNode->getUnionArrayPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002475 ASSERT(unionArray);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002476
Arun Patole7e7e68d2015-05-22 12:02:25 +05302477 TConstantUnion *constArray = new TConstantUnion[fields.num];
Olli Etuaho90892fb2016-07-14 14:44:51 +03002478 const auto &type = baseNode->getType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002479
Arun Patole7e7e68d2015-05-22 12:02:25 +05302480 for (int i = 0; i < fields.num; i++)
2481 {
Olli Etuaho90892fb2016-07-14 14:44:51 +03002482 // Out-of-range indices should already be checked.
2483 ASSERT(fields.offsets[i] < type.getNominalSize());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002484 constArray[i] = unionArray[fields.offsets[i]];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302485 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03002486 return intermediate.addConstantUnion(constArray, type, location);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002487}
2488
Olli Etuaho90892fb2016-07-14 14:44:51 +03002489// This function returns the column vector being accessed from a constant matrix.
2490TIntermConstantUnion *TParseContext::foldMatrixSubscript(int index,
2491 TIntermConstantUnion *baseNode,
2492 const TSourceLoc &location)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002493{
Olli Etuaho90892fb2016-07-14 14:44:51 +03002494 ASSERT(index < baseNode->getType().getCols());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002495
Olli Etuaho90892fb2016-07-14 14:44:51 +03002496 const TConstantUnion *unionArray = baseNode->getUnionArrayPointer();
2497 int size = baseNode->getType().getRows();
2498 return intermediate.addConstantUnion(&unionArray[size * index], baseNode->getType(), location);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002499}
2500
Olli Etuaho90892fb2016-07-14 14:44:51 +03002501// This function returns an element of an array accessed from a constant array.
2502TIntermConstantUnion *TParseContext::foldArraySubscript(int index,
2503 TIntermConstantUnion *baseNode,
2504 const TSourceLoc &location)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002505{
Olli Etuaho90892fb2016-07-14 14:44:51 +03002506 ASSERT(index < baseNode->getArraySize());
2507
2508 TType arrayElementType = baseNode->getType();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002509 arrayElementType.clearArrayness();
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002510 size_t arrayElementSize = arrayElementType.getObjectSize();
Olli Etuaho90892fb2016-07-14 14:44:51 +03002511 const TConstantUnion *unionArray = baseNode->getUnionArrayPointer();
2512 return intermediate.addConstantUnion(&unionArray[arrayElementSize * index], baseNode->getType(),
2513 location);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002514}
2515
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002516//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002517// This function returns the value of a particular field inside a constant structure from the symbol
2518// table.
2519// If there is an embedded/nested struct, it appropriately calls addConstStructNested or
2520// addConstStructFromAggr function and returns the parse-tree with the values of the embedded/nested
2521// struct.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002522//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002523TIntermTyped *TParseContext::addConstStruct(const TString &identifier,
2524 TIntermTyped *node,
2525 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002526{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302527 const TFieldList &fields = node->getType().getStruct()->fields();
Jamie Madillb98c3a82015-07-23 14:26:04 -04002528 size_t instanceSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002529
Arun Patole7e7e68d2015-05-22 12:02:25 +05302530 for (size_t index = 0; index < fields.size(); ++index)
2531 {
2532 if (fields[index]->name() == identifier)
2533 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002534 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302535 }
2536 else
2537 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002538 instanceSize += fields[index]->type()->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002539 }
2540 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002541
Jamie Madill94bf7f22013-07-08 13:31:15 -04002542 TIntermTyped *typedNode;
2543 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
Arun Patole7e7e68d2015-05-22 12:02:25 +05302544 if (tempConstantNode)
2545 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002546 const TConstantUnion *constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002547
Jamie Madillb98c3a82015-07-23 14:26:04 -04002548 // type will be changed in the calling function
2549 typedNode = intermediate.addConstantUnion(constArray + instanceSize,
2550 tempConstantNode->getType(), line);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302551 }
2552 else
2553 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002554 error(line, "Cannot offset into the structure", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002555 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002556
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002557 return 0;
2558 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002559
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002560 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002561}
2562
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002563//
2564// Interface/uniform blocks
2565//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002566TIntermAggregate *TParseContext::addInterfaceBlock(const TPublicType &typeQualifier,
2567 const TSourceLoc &nameLine,
2568 const TString &blockName,
2569 TFieldList *fieldList,
2570 const TString *instanceName,
2571 const TSourceLoc &instanceLine,
2572 TIntermTyped *arrayIndex,
2573 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002574{
2575 if (reservedErrorCheck(nameLine, blockName))
2576 recover();
2577
2578 if (typeQualifier.qualifier != EvqUniform)
2579 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302580 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
2581 "interface blocks must be uniform");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002582 recover();
2583 }
2584
Jamie Madill099c0f32013-06-20 11:55:52 -04002585 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
2586 if (layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002587 {
2588 recover();
2589 }
2590
Jamie Madill099c0f32013-06-20 11:55:52 -04002591 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2592 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002593 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002594 }
2595
Jamie Madill1566ef72013-06-20 11:55:54 -04002596 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2597 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002598 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002599 }
2600
Martin Radev802abe02016-08-04 17:48:32 +03002601 if (layoutWorkGroupSizeErrorCheck(nameLine, blockLayoutQualifier))
2602 {
2603 recover();
2604 }
2605
Arun Patole7e7e68d2015-05-22 12:02:25 +05302606 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2607 if (!symbolTable.declare(blockNameSymbol))
2608 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002609 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
2610 recover();
2611 }
2612
Jamie Madill98493dd2013-07-08 14:39:03 -04002613 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302614 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2615 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002616 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302617 TType *fieldType = field->type();
2618 if (IsSampler(fieldType->getBasicType()))
2619 {
2620 error(field->line(), "unsupported type", fieldType->getBasicString(),
2621 "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002622 recover();
2623 }
2624
Jamie Madill98493dd2013-07-08 14:39:03 -04002625 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002626 switch (qualifier)
2627 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002628 case EvqGlobal:
2629 case EvqUniform:
2630 break;
2631 default:
2632 error(field->line(), "invalid qualifier on interface block member",
2633 getQualifierString(qualifier));
2634 recover();
2635 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002636 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002637
2638 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002639 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
2640 if (layoutLocationErrorCheck(field->line(), fieldLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002641 {
2642 recover();
2643 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002644
Jamie Madill98493dd2013-07-08 14:39:03 -04002645 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002646 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002647 error(field->line(), "invalid layout qualifier:",
2648 getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04002649 recover();
2650 }
2651
Jamie Madill98493dd2013-07-08 14:39:03 -04002652 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002653 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002654 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002655 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002656 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002657 {
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002658 warning(field->line(), "extraneous layout qualifier:",
Jamie Madillb98c3a82015-07-23 14:26:04 -04002659 getMatrixPackingString(fieldLayoutQualifier.matrixPacking),
2660 "only has an effect on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04002661 }
2662
Jamie Madill98493dd2013-07-08 14:39:03 -04002663 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002664 }
2665
Jamie Madill98493dd2013-07-08 14:39:03 -04002666 // add array index
2667 int arraySize = 0;
2668 if (arrayIndex != NULL)
2669 {
2670 if (arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize))
2671 recover();
2672 }
2673
Jamie Madillb98c3a82015-07-23 14:26:04 -04002674 TInterfaceBlock *interfaceBlock =
2675 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2676 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
2677 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002678
2679 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04002680 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002681
Jamie Madill98493dd2013-07-08 14:39:03 -04002682 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002683 {
2684 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002685 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2686 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002687 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302688 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002689
2690 // set parent pointer of the field variable
2691 fieldType->setInterfaceBlock(interfaceBlock);
2692
Arun Patole7e7e68d2015-05-22 12:02:25 +05302693 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002694 fieldVariable->setQualifier(typeQualifier.qualifier);
2695
Arun Patole7e7e68d2015-05-22 12:02:25 +05302696 if (!symbolTable.declare(fieldVariable))
2697 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002698 error(field->line(), "redefinition", field->name().c_str(),
2699 "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002700 recover();
2701 }
2702 }
2703 }
2704 else
2705 {
Olli Etuahoe0f623a2015-07-10 11:58:30 +03002706 if (reservedErrorCheck(instanceLine, *instanceName))
2707 recover();
2708
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002709 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302710 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002711 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002712
Arun Patole7e7e68d2015-05-22 12:02:25 +05302713 if (!symbolTable.declare(instanceTypeDef))
2714 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002715 error(instanceLine, "redefinition", instanceName->c_str(),
2716 "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002717 recover();
2718 }
2719
Jamie Madillb98c3a82015-07-23 14:26:04 -04002720 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002721 symbolName = instanceTypeDef->getName();
2722 }
2723
Jamie Madillb98c3a82015-07-23 14:26:04 -04002724 TIntermAggregate *aggregate = intermediate.makeAggregate(
2725 intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line),
2726 nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002727 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04002728
2729 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002730 return aggregate;
2731}
2732
Arun Patole7e7e68d2015-05-22 12:02:25 +05302733bool TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002734{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002735 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002736
2737 // Embedded structure definitions are not supported per GLSL ES spec.
2738 // They aren't allowed in GLSL either, but we need to detect this here
2739 // so we don't rely on the GLSL compiler to catch it.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302740 if (mStructNestingLevel > 1)
2741 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002742 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002743 return true;
2744 }
2745
2746 return false;
2747}
2748
2749void TParseContext::exitStructDeclaration()
2750{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002751 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002752}
2753
Jamie Madillb98c3a82015-07-23 14:26:04 -04002754namespace
2755{
kbr@chromium.org476541f2011-10-27 21:14:51 +00002756const int kWebGLMaxStructNesting = 4;
2757
2758} // namespace
2759
Arun Patole7e7e68d2015-05-22 12:02:25 +05302760bool TParseContext::structNestingErrorCheck(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002761{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302762 if (!IsWebGLBasedSpec(mShaderSpec))
2763 {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002764 return false;
2765 }
2766
Arun Patole7e7e68d2015-05-22 12:02:25 +05302767 if (field.type()->getBasicType() != EbtStruct)
2768 {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002769 return false;
2770 }
2771
2772 // We're already inside a structure definition at this point, so add
2773 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302774 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2775 {
Jamie Madill41a49272014-03-18 16:10:13 -04002776 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002777 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
2778 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04002779 std::string reason = reasonStream.str();
2780 error(line, reason.c_str(), field.name().c_str(), "");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002781 return true;
2782 }
2783
2784 return false;
2785}
2786
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002787//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002788// Parse an array index expression
2789//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002790TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
2791 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302792 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002793{
2794 TIntermTyped *indexedExpression = NULL;
2795
2796 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2797 {
2798 if (baseExpression->getAsSymbolNode())
2799 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302800 error(location, " left of '[' is not of type array, matrix, or vector ",
2801 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002802 }
2803 else
2804 {
2805 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2806 }
2807 recover();
2808 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002809
Jamie Madill21c1e452014-12-29 11:33:41 -05002810 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2811
Olli Etuaho36b05142015-11-12 13:10:42 +02002812 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
2813 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
2814 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
2815 // index is a constant expression.
2816 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
2817 {
2818 if (baseExpression->isInterfaceBlock())
2819 {
2820 error(
2821 location, "", "[",
2822 "array indexes for interface blocks arrays must be constant integral expressions");
2823 recover();
2824 }
2825 else if (baseExpression->getQualifier() == EvqFragmentOut)
2826 {
2827 error(location, "", "[",
2828 "array indexes for fragment outputs must be constant integral expressions");
2829 recover();
2830 }
Olli Etuaho3e960462015-11-12 15:58:39 +02002831 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
2832 {
2833 error(location, "", "[", "array index for gl_FragData must be constant zero");
2834 recover();
2835 }
Olli Etuaho36b05142015-11-12 13:10:42 +02002836 }
2837
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002838 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002839 {
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002840 // If the index is not qualified as constant, the behavior in the spec is undefined. This
2841 // applies even if ANGLE has been able to constant fold it (ANGLE may constant fold
2842 // expressions that are not constant expressions). The most compatible way to handle this
2843 // case is to report a warning instead of an error and force the index to be in the
2844 // correct range.
2845 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Jamie Madill21c1e452014-12-29 11:33:41 -05002846 int index = indexConstantUnion->getIConst(0);
Olli Etuaho90892fb2016-07-14 14:44:51 +03002847 if (!baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04002848 {
Olli Etuaho90892fb2016-07-14 14:44:51 +03002849 // Array checks are done later because a different error message might be generated
2850 // based on the index in some cases.
2851 if (baseExpression->isVector())
2852 {
2853 index = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
2854 baseExpression->getType().getNominalSize(),
2855 "vector field selection out of range", "[]");
2856 }
2857 else if (baseExpression->isMatrix())
2858 {
2859 index = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
2860 baseExpression->getType().getCols(),
2861 "matrix field selection out of range", "[]");
2862 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002863 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03002864
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002865 TIntermConstantUnion *baseConstantUnion = baseExpression->getAsConstantUnion();
2866 if (baseConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002867 {
2868 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002869 {
Olli Etuaho90892fb2016-07-14 14:44:51 +03002870 index = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
2871 baseExpression->getArraySize(),
2872 "array index out of range", "[]");
2873 // Constant folding for array indexing.
2874 indexedExpression = foldArraySubscript(index, baseConstantUnion, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002875 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002876 else if (baseExpression->isVector())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002877 {
Olli Etuaho90892fb2016-07-14 14:44:51 +03002878 // Constant folding for vector indexing - reusing vector swizzle folding.
Jamie Madill7164cf42013-07-08 13:30:59 -04002879 TVectorFields fields;
2880 fields.num = 1;
Olli Etuaho90892fb2016-07-14 14:44:51 +03002881 fields.offsets[0] = index;
2882 indexedExpression = foldVectorSwizzle(fields, baseConstantUnion, location);
Jamie Madill7164cf42013-07-08 13:30:59 -04002883 }
2884 else if (baseExpression->isMatrix())
2885 {
Olli Etuaho90892fb2016-07-14 14:44:51 +03002886 // Constant folding for matrix indexing.
2887 indexedExpression = foldMatrixSubscript(index, baseConstantUnion, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002888 }
2889 }
2890 else
2891 {
Jamie Madillb11e2482015-05-04 14:21:22 -04002892 int safeIndex = -1;
2893
Jamie Madill7164cf42013-07-08 13:30:59 -04002894 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002895 {
Olli Etuaho3e960462015-11-12 15:58:39 +02002896 if (baseExpression->getQualifier() == EvqFragData && index > 0)
2897 {
2898 if (mShaderSpec == SH_WEBGL2_SPEC)
2899 {
2900 // Error has been already generated if index is not const.
2901 if (indexExpression->getQualifier() == EvqConst)
2902 {
2903 error(location, "", "[",
2904 "array index for gl_FragData must be constant zero");
2905 recover();
2906 }
2907 safeIndex = 0;
2908 }
2909 else if (!isExtensionEnabled("GL_EXT_draw_buffers"))
2910 {
2911 outOfRangeError(outOfRangeIndexIsError, location, "", "[",
2912 "array index for gl_FragData must be zero when "
2913 "GL_EXT_draw_buffers is disabled");
2914 safeIndex = 0;
2915 }
2916 }
2917 // Only do generic out-of-range check if similar error hasn't already been reported.
Olli Etuaho90892fb2016-07-14 14:44:51 +03002918 if (safeIndex < 0)
Jamie Madill7164cf42013-07-08 13:30:59 -04002919 {
Olli Etuaho90892fb2016-07-14 14:44:51 +03002920 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
2921 baseExpression->getArraySize(),
2922 "array index out of range", "[]");
Jamie Madill7164cf42013-07-08 13:30:59 -04002923 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002924 }
2925
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002926 // Data of constant unions can't be changed, because it may be shared with other
2927 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
2928 // sanitized object.
Jamie Madillb11e2482015-05-04 14:21:22 -04002929 if (safeIndex != -1)
2930 {
2931 TConstantUnion *safeConstantUnion = new TConstantUnion();
2932 safeConstantUnion->setIConst(safeIndex);
2933 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
2934 }
2935
Jamie Madillb98c3a82015-07-23 14:26:04 -04002936 indexedExpression =
2937 intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002938 }
2939 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002940 else
2941 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002942 indexedExpression =
2943 intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
Jamie Madill7164cf42013-07-08 13:30:59 -04002944 }
2945
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002946 if (indexedExpression == 0)
2947 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002948 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002949 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002950 indexedExpression =
2951 intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002952 }
2953 else if (baseExpression->isArray())
2954 {
Olli Etuahob3fbd862015-09-30 17:55:02 +03002955 TType indexedType = baseExpression->getType();
2956 indexedType.clearArrayness();
2957 indexedExpression->setType(indexedType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002958 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002959 else if (baseExpression->isMatrix())
2960 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002961 indexedExpression->setType(TType(baseExpression->getBasicType(),
Olli Etuahob3fbd862015-09-30 17:55:02 +03002962 baseExpression->getPrecision(), EvqTemporary,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002963 static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002964 }
2965 else if (baseExpression->isVector())
2966 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002967 indexedExpression->setType(
Olli Etuahob3fbd862015-09-30 17:55:02 +03002968 TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002969 }
2970 else
2971 {
2972 indexedExpression->setType(baseExpression->getType());
2973 }
2974
Olli Etuahob3fbd862015-09-30 17:55:02 +03002975 if (baseExpression->getType().getQualifier() == EvqConst &&
2976 indexExpression->getType().getQualifier() == EvqConst)
2977 {
2978 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2979 }
2980 else
2981 {
2982 indexedExpression->getTypePointer()->setQualifier(EvqTemporary);
2983 }
2984
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002985 return indexedExpression;
2986}
2987
Olli Etuaho90892fb2016-07-14 14:44:51 +03002988int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
2989 const TSourceLoc &location,
2990 int index,
2991 int arraySize,
2992 const char *reason,
2993 const char *token)
2994{
2995 if (index >= arraySize || index < 0)
2996 {
2997 std::stringstream extraInfoStream;
2998 extraInfoStream << "'" << index << "'";
2999 std::string extraInfo = extraInfoStream.str();
3000 outOfRangeError(outOfRangeIndexIsError, location, reason, token, extraInfo.c_str());
3001 if (index < 0)
3002 {
3003 return 0;
3004 }
3005 else
3006 {
3007 return arraySize - 1;
3008 }
3009 }
3010 return index;
3011}
3012
Jamie Madillb98c3a82015-07-23 14:26:04 -04003013TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3014 const TSourceLoc &dotLocation,
3015 const TString &fieldString,
3016 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003017{
3018 TIntermTyped *indexedExpression = NULL;
3019
3020 if (baseExpression->isArray())
3021 {
3022 error(fieldLocation, "cannot apply dot operator to an array", ".");
3023 recover();
3024 }
3025
3026 if (baseExpression->isVector())
3027 {
3028 TVectorFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003029 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields,
3030 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003031 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003032 fields.num = 1;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003033 fields.offsets[0] = 0;
3034 recover();
3035 }
3036
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003037 if (baseExpression->getAsConstantUnion())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003038 {
3039 // constant folding for vector fields
Olli Etuaho90892fb2016-07-14 14:44:51 +03003040 indexedExpression =
3041 foldVectorSwizzle(fields, baseExpression->getAsConstantUnion(), fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003042 }
3043 else
3044 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303045 TIntermTyped *index = intermediate.addSwizzle(fields, fieldLocation);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003046 indexedExpression =
3047 intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003048 }
3049 if (indexedExpression == nullptr)
3050 {
3051 recover();
3052 indexedExpression = baseExpression;
3053 }
3054 else
3055 {
3056 // Note that the qualifier set here will be corrected later.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003057 indexedExpression->setType(TType(baseExpression->getBasicType(),
3058 baseExpression->getPrecision(), EvqTemporary,
Jamie Madillc2128ff2016-07-04 10:26:17 -04003059 static_cast<unsigned char>(fields.num)));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003060 }
3061 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003062 else if (baseExpression->getBasicType() == EbtStruct)
3063 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003064 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05303065 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003066 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003067 {
3068 error(dotLocation, "structure has no fields", "Internal Error");
3069 recover();
3070 indexedExpression = baseExpression;
3071 }
3072 else
3073 {
3074 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003075 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003076 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003077 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003078 {
3079 fieldFound = true;
3080 break;
3081 }
3082 }
3083 if (fieldFound)
3084 {
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003085 if (baseExpression->getAsConstantUnion())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003086 {
3087 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
3088 if (indexedExpression == 0)
3089 {
3090 recover();
3091 indexedExpression = baseExpression;
3092 }
3093 else
3094 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003095 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003096 }
3097 }
3098 else
3099 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003100 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003101 unionArray->setIConst(i);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003102 TIntermTyped *index = intermediate.addConstantUnion(
3103 unionArray, *fields[i]->type(), fieldLocation);
3104 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression,
3105 index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04003106 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003107 }
3108 }
3109 else
3110 {
3111 error(dotLocation, " no such field in structure", fieldString.c_str());
3112 recover();
3113 indexedExpression = baseExpression;
3114 }
3115 }
3116 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003117 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003118 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003119 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05303120 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003121 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003122 {
3123 error(dotLocation, "interface block has no fields", "Internal Error");
3124 recover();
3125 indexedExpression = baseExpression;
3126 }
3127 else
3128 {
3129 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003130 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003131 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003132 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003133 {
3134 fieldFound = true;
3135 break;
3136 }
3137 }
3138 if (fieldFound)
3139 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003140 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003141 unionArray->setIConst(i);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003142 TIntermTyped *index =
3143 intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
3144 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock,
3145 baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04003146 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003147 }
3148 else
3149 {
3150 error(dotLocation, " no such field in interface block", fieldString.c_str());
3151 recover();
3152 indexedExpression = baseExpression;
3153 }
3154 }
3155 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003156 else
3157 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003158 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003159 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03003160 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303161 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003162 }
3163 else
3164 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303165 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003166 " field selection requires structure, vector, or interface block on left hand "
3167 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303168 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003169 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003170 recover();
3171 indexedExpression = baseExpression;
3172 }
3173
Olli Etuahob1edc4f2015-11-02 17:20:03 +02003174 if (baseExpression->getQualifier() == EvqConst)
3175 {
3176 indexedExpression->getTypePointer()->setQualifier(EvqConst);
3177 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003178 else
3179 {
3180 indexedExpression->getTypePointer()->setQualifier(EvqTemporary);
3181 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02003182
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003183 return indexedExpression;
3184}
3185
Jamie Madillb98c3a82015-07-23 14:26:04 -04003186TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3187 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003188{
Martin Radev802abe02016-08-04 17:48:32 +03003189 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003190
3191 if (qualifierType == "shared")
3192 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003193 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003194 }
3195 else if (qualifierType == "packed")
3196 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003197 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003198 }
3199 else if (qualifierType == "std140")
3200 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003201 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003202 }
3203 else if (qualifierType == "row_major")
3204 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003205 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003206 }
3207 else if (qualifierType == "column_major")
3208 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003209 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003210 }
3211 else if (qualifierType == "location")
3212 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003213 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
3214 "location requires an argument");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003215 recover();
3216 }
3217 else
3218 {
3219 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
3220 recover();
3221 }
3222
Jamie Madilla5efff92013-06-06 11:56:47 -04003223 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003224}
3225
Martin Radev802abe02016-08-04 17:48:32 +03003226void TParseContext::parseLocalSize(const TString &qualifierType,
3227 const TSourceLoc &qualifierTypeLine,
3228 int intValue,
3229 const TSourceLoc &intValueLine,
3230 const std::string &intValueString,
3231 size_t index,
3232 TLocalSize *localSize)
3233{
3234 layoutSupportedErrorCheck(qualifierTypeLine, qualifierType, 310);
3235 if (intValue < 1)
3236 {
3237 std::string errorMessage = std::string(getLocalSizeString(index)) + " must be positive";
3238 error(intValueLine, "out of range:", intValueString.c_str(), errorMessage.c_str());
3239 recover();
3240 }
3241 (*localSize)[index] = intValue;
3242}
3243
Jamie Madillb98c3a82015-07-23 14:26:04 -04003244TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3245 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003246 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303247 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003248{
Martin Radev802abe02016-08-04 17:48:32 +03003249 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003250
Martin Radev802abe02016-08-04 17:48:32 +03003251 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003252
Martin Radev802abe02016-08-04 17:48:32 +03003253 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003254 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003255 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003256 if (intValue < 0)
3257 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003258 error(intValueLine, "out of range:", intValueString.c_str(),
3259 "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003260 recover();
3261 }
3262 else
3263 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003264 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003265 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003266 }
Martin Radev802abe02016-08-04 17:48:32 +03003267 else if (qualifierType == "local_size_x")
3268 {
3269 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
3270 &qualifier.localSize);
3271 }
3272 else if (qualifierType == "local_size_y")
3273 {
3274 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
3275 &qualifier.localSize);
3276 }
3277 else if (qualifierType == "local_size_z")
3278 {
3279 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
3280 &qualifier.localSize);
3281 }
3282 else
3283 {
3284 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
3285 recover();
3286 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003287
Jamie Madilla5efff92013-06-06 11:56:47 -04003288 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003289}
3290
Jamie Madillb98c3a82015-07-23 14:26:04 -04003291TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03003292 TLayoutQualifier rightQualifier,
3293 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003294{
Jamie Madilla5efff92013-06-06 11:56:47 -04003295 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003296
Jamie Madilla5efff92013-06-06 11:56:47 -04003297 if (rightQualifier.location != -1)
3298 {
3299 joinedQualifier.location = rightQualifier.location;
3300 }
3301 if (rightQualifier.matrixPacking != EmpUnspecified)
3302 {
3303 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
3304 }
3305 if (rightQualifier.blockStorage != EbsUnspecified)
3306 {
3307 joinedQualifier.blockStorage = rightQualifier.blockStorage;
3308 }
3309
Martin Radev802abe02016-08-04 17:48:32 +03003310 for (size_t i = 0u; i < rightQualifier.localSize.size(); ++i)
3311 {
3312 if (rightQualifier.localSize[i] != -1)
3313 {
3314 if (joinedQualifier.localSize[i] != -1 &&
3315 joinedQualifier.localSize[i] != rightQualifier.localSize[i])
3316 {
3317 error(rightQualifierLocation,
3318 "Cannot have multiple different work group size specifiers",
3319 getLocalSizeString(i));
3320 recover();
3321 }
3322 joinedQualifier.localSize[i] = rightQualifier.localSize[i];
3323 }
3324 }
3325
Jamie Madilla5efff92013-06-06 11:56:47 -04003326 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003327}
3328
Arun Patole7e7e68d2015-05-22 12:02:25 +05303329TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc,
3330 TQualifier interpolationQualifier,
3331 const TSourceLoc &storageLoc,
3332 TQualifier storageQualifier)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003333{
3334 TQualifier mergedQualifier = EvqSmoothIn;
3335
Arun Patole7e7e68d2015-05-22 12:02:25 +05303336 if (storageQualifier == EvqFragmentIn)
3337 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003338 if (interpolationQualifier == EvqSmooth)
3339 mergedQualifier = EvqSmoothIn;
3340 else if (interpolationQualifier == EvqFlat)
3341 mergedQualifier = EvqFlatIn;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003342 else
3343 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003344 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303345 else if (storageQualifier == EvqCentroidIn)
3346 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003347 if (interpolationQualifier == EvqSmooth)
3348 mergedQualifier = EvqCentroidIn;
3349 else if (interpolationQualifier == EvqFlat)
3350 mergedQualifier = EvqFlatIn;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003351 else
3352 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003353 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303354 else if (storageQualifier == EvqVertexOut)
3355 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003356 if (interpolationQualifier == EvqSmooth)
3357 mergedQualifier = EvqSmoothOut;
3358 else if (interpolationQualifier == EvqFlat)
3359 mergedQualifier = EvqFlatOut;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003360 else
3361 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003362 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303363 else if (storageQualifier == EvqCentroidOut)
3364 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003365 if (interpolationQualifier == EvqSmooth)
3366 mergedQualifier = EvqCentroidOut;
3367 else if (interpolationQualifier == EvqFlat)
3368 mergedQualifier = EvqFlatOut;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003369 else
3370 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003371 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303372 else
3373 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003374 error(interpolationLoc,
3375 "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303376 getInterpolationString(interpolationQualifier));
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003377 recover();
3378
3379 mergedQualifier = storageQualifier;
3380 }
3381
3382 TPublicType type;
3383 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
3384 return type;
3385}
3386
Jamie Madillb98c3a82015-07-23 14:26:04 -04003387TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3388 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003389{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03003390 if (voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier.type))
3391 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003392 recover();
3393 }
3394
Martin Radev802abe02016-08-04 17:48:32 +03003395 if (layoutWorkGroupSizeErrorCheck(typeSpecifier.line, typeSpecifier.layoutQualifier))
3396 {
3397 recover();
3398 }
3399
Arun Patole7e7e68d2015-05-22 12:02:25 +05303400 for (unsigned int i = 0; i < fieldList->size(); ++i)
3401 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003402 //
3403 // Careful not to replace already known aspects of type, like array-ness
3404 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303405 TType *type = (*fieldList)[i]->type();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003406 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003407 type->setPrimarySize(typeSpecifier.primarySize);
3408 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003409 type->setPrecision(typeSpecifier.precision);
3410 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003411 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003412
3413 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303414 if (type->isArray())
3415 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003416 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
3417 recover();
3418 }
3419 if (typeSpecifier.array)
3420 type->setArraySize(typeSpecifier.arraySize);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303421 if (typeSpecifier.userDef)
3422 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003423 type->setStruct(typeSpecifier.userDef->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003424 }
3425
Arun Patole7e7e68d2015-05-22 12:02:25 +05303426 if (structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i]))
3427 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003428 recover();
3429 }
3430 }
3431
Jamie Madill98493dd2013-07-08 14:39:03 -04003432 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003433}
3434
Jamie Madillb98c3a82015-07-23 14:26:04 -04003435TPublicType TParseContext::addStructure(const TSourceLoc &structLine,
3436 const TSourceLoc &nameLine,
3437 const TString *structName,
3438 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003439{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303440 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003441 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003442
Jamie Madill9b820842015-02-12 10:40:10 -05003443 // Store a bool in the struct if we're at global scope, to allow us to
3444 // skip the local struct scoping workaround in HLSL.
Jamie Madillb960cc42015-02-12 15:33:20 +00003445 structure->setUniqueId(TSymbolTable::nextUniqueId());
Jamie Madill9b820842015-02-12 10:40:10 -05003446 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003447
Jamie Madill98493dd2013-07-08 14:39:03 -04003448 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003449 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003450 if (reservedErrorCheck(nameLine, *structName))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003451 {
3452 recover();
3453 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303454 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
3455 if (!symbolTable.declare(userTypeDef))
3456 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003457 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003458 recover();
3459 }
3460 }
3461
3462 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04003463 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003464 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003465 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04003466 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003467 switch (qualifier)
3468 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003469 case EvqGlobal:
3470 case EvqTemporary:
3471 break;
3472 default:
3473 error(field.line(), "invalid qualifier on struct member",
3474 getQualifierString(qualifier));
3475 recover();
3476 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003477 }
3478 }
3479
3480 TPublicType publicType;
3481 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003482 publicType.userDef = structureType;
Olli Etuahobd163f62015-11-13 12:15:38 +02003483 publicType.isStructSpecifier = true;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003484 exitStructDeclaration();
3485
3486 return publicType;
3487}
3488
Jamie Madillb98c3a82015-07-23 14:26:04 -04003489TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
3490 TIntermAggregate *statementList,
3491 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02003492{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003493 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003494 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02003495 init->isVector())
3496 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003497 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
3498 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003499 recover();
3500 return nullptr;
3501 }
3502
Olli Etuahoac5274d2015-02-20 10:19:08 +02003503 if (statementList)
3504 {
3505 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
3506 {
3507 recover();
3508 return nullptr;
3509 }
3510 }
3511
Olli Etuahoa3a36662015-02-17 13:46:51 +02003512 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3513 if (node == nullptr)
3514 {
3515 error(loc, "erroneous switch statement", "switch");
3516 recover();
3517 return nullptr;
3518 }
3519 return node;
3520}
3521
3522TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3523{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003524 if (mSwitchNestingLevel == 0)
3525 {
3526 error(loc, "case labels need to be inside switch statements", "case");
3527 recover();
3528 return nullptr;
3529 }
3530 if (condition == nullptr)
3531 {
3532 error(loc, "case label must have a condition", "case");
3533 recover();
3534 return nullptr;
3535 }
3536 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04003537 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02003538 {
3539 error(condition->getLine(), "case label must be a scalar integer", "case");
3540 recover();
3541 }
3542 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003543 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
3544 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
3545 // fold in case labels.
3546 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02003547 {
3548 error(condition->getLine(), "case label must be constant", "case");
3549 recover();
3550 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003551 TIntermCase *node = intermediate.addCase(condition, loc);
3552 if (node == nullptr)
3553 {
3554 error(loc, "erroneous case statement", "case");
3555 recover();
3556 return nullptr;
3557 }
3558 return node;
3559}
3560
3561TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3562{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003563 if (mSwitchNestingLevel == 0)
3564 {
3565 error(loc, "default labels need to be inside switch statements", "default");
3566 recover();
3567 return nullptr;
3568 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003569 TIntermCase *node = intermediate.addCase(nullptr, loc);
3570 if (node == nullptr)
3571 {
3572 error(loc, "erroneous default statement", "default");
3573 recover();
3574 return nullptr;
3575 }
3576 return node;
3577}
3578
Jamie Madillb98c3a82015-07-23 14:26:04 -04003579TIntermTyped *TParseContext::createUnaryMath(TOperator op,
3580 TIntermTyped *child,
3581 const TSourceLoc &loc,
3582 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003583{
3584 if (child == nullptr)
3585 {
3586 return nullptr;
3587 }
3588
3589 switch (op)
3590 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003591 case EOpLogicalNot:
3592 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
3593 child->isVector())
3594 {
3595 return nullptr;
3596 }
3597 break;
3598 case EOpBitwiseNot:
3599 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3600 child->isMatrix() || child->isArray())
3601 {
3602 return nullptr;
3603 }
3604 break;
3605 case EOpPostIncrement:
3606 case EOpPreIncrement:
3607 case EOpPostDecrement:
3608 case EOpPreDecrement:
3609 case EOpNegative:
3610 case EOpPositive:
3611 if (child->getBasicType() == EbtStruct || child->getBasicType() == EbtBool ||
3612 child->isArray())
3613 {
3614 return nullptr;
3615 }
3616 // Operators for built-ins are already type checked against their prototype.
3617 default:
3618 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003619 }
3620
Olli Etuahof6c694b2015-03-26 14:50:53 +02003621 return intermediate.addUnaryMath(op, child, loc, funcReturnType);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003622}
3623
Olli Etuaho09b22472015-02-11 11:47:26 +02003624TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3625{
Olli Etuahof6c694b2015-03-26 14:50:53 +02003626 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003627 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003628 {
3629 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
3630 recover();
3631 return child;
3632 }
3633 return node;
3634}
3635
Jamie Madillb98c3a82015-07-23 14:26:04 -04003636TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
3637 TIntermTyped *child,
3638 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003639{
3640 if (lValueErrorCheck(loc, GetOperatorString(op), child))
3641 recover();
3642 return addUnaryMath(op, child, loc);
3643}
3644
Jamie Madillb98c3a82015-07-23 14:26:04 -04003645bool TParseContext::binaryOpCommonCheck(TOperator op,
3646 TIntermTyped *left,
3647 TIntermTyped *right,
3648 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003649{
3650 if (left->isArray() || right->isArray())
3651 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003652 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003653 {
3654 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3655 return false;
3656 }
3657
3658 if (left->isArray() != right->isArray())
3659 {
3660 error(loc, "array / non-array mismatch", GetOperatorString(op));
3661 return false;
3662 }
3663
3664 switch (op)
3665 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003666 case EOpEqual:
3667 case EOpNotEqual:
3668 case EOpAssign:
3669 case EOpInitialize:
3670 break;
3671 default:
3672 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3673 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02003674 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003675 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003676 if (left->getArraySize() != right->getArraySize())
3677 {
3678 error(loc, "array size mismatch", GetOperatorString(op));
3679 return false;
3680 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003681 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003682
3683 // Check ops which require integer / ivec parameters
3684 bool isBitShift = false;
3685 switch (op)
3686 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003687 case EOpBitShiftLeft:
3688 case EOpBitShiftRight:
3689 case EOpBitShiftLeftAssign:
3690 case EOpBitShiftRightAssign:
3691 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3692 // check that the basic type is an integer type.
3693 isBitShift = true;
3694 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3695 {
3696 return false;
3697 }
3698 break;
3699 case EOpBitwiseAnd:
3700 case EOpBitwiseXor:
3701 case EOpBitwiseOr:
3702 case EOpBitwiseAndAssign:
3703 case EOpBitwiseXorAssign:
3704 case EOpBitwiseOrAssign:
3705 // It is enough to check the type of only one operand, since later it
3706 // is checked that the operand types match.
3707 if (!IsInteger(left->getBasicType()))
3708 {
3709 return false;
3710 }
3711 break;
3712 default:
3713 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003714 }
3715
3716 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3717 // So the basic type should usually match.
3718 if (!isBitShift && left->getBasicType() != right->getBasicType())
3719 {
3720 return false;
3721 }
3722
Olli Etuaho9dd217b2015-03-20 14:24:31 +02003723 // Check that type sizes match exactly on ops that require that.
Olli Etuahoff699002015-03-23 14:38:42 +02003724 // Also check restrictions for structs that contain arrays or samplers.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003725 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003726 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003727 case EOpAssign:
3728 case EOpInitialize:
3729 case EOpEqual:
3730 case EOpNotEqual:
3731 // ESSL 1.00 sections 5.7, 5.8, 5.9
3732 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
3733 {
3734 error(loc, "undefined operation for structs containing arrays",
3735 GetOperatorString(op));
3736 return false;
3737 }
3738 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3739 // we interpret the spec so that this extends to structs containing samplers,
3740 // similarly to ESSL 1.00 spec.
3741 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
3742 left->getType().isStructureContainingSamplers())
3743 {
3744 error(loc, "undefined operation for structs containing samplers",
3745 GetOperatorString(op));
3746 return false;
3747 }
3748 case EOpLessThan:
3749 case EOpGreaterThan:
3750 case EOpLessThanEqual:
3751 case EOpGreaterThanEqual:
3752 if ((left->getNominalSize() != right->getNominalSize()) ||
3753 (left->getSecondarySize() != right->getSecondarySize()))
3754 {
3755 return false;
3756 }
3757 default:
3758 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003759 }
3760
Olli Etuahod6b14282015-03-17 14:31:35 +02003761 return true;
3762}
3763
Jamie Madillb98c3a82015-07-23 14:26:04 -04003764TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
3765 TIntermTyped *left,
3766 TIntermTyped *right,
3767 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02003768{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003769 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003770 return nullptr;
3771
Olli Etuahofc1806e2015-03-17 13:03:11 +02003772 switch (op)
3773 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003774 case EOpEqual:
3775 case EOpNotEqual:
3776 break;
3777 case EOpLessThan:
3778 case EOpGreaterThan:
3779 case EOpLessThanEqual:
3780 case EOpGreaterThanEqual:
3781 ASSERT(!left->isArray() && !right->isArray());
3782 if (left->isMatrix() || left->isVector() || left->getBasicType() == EbtStruct)
3783 {
3784 return nullptr;
3785 }
3786 break;
3787 case EOpLogicalOr:
3788 case EOpLogicalXor:
3789 case EOpLogicalAnd:
3790 ASSERT(!left->isArray() && !right->isArray());
3791 if (left->getBasicType() != EbtBool || left->isMatrix() || left->isVector())
3792 {
3793 return nullptr;
3794 }
3795 break;
3796 case EOpAdd:
3797 case EOpSub:
3798 case EOpDiv:
3799 case EOpMul:
3800 ASSERT(!left->isArray() && !right->isArray());
3801 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool)
3802 {
3803 return nullptr;
3804 }
3805 break;
3806 case EOpIMod:
3807 ASSERT(!left->isArray() && !right->isArray());
3808 // Note that this is only for the % operator, not for mod()
3809 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool ||
3810 left->getBasicType() == EbtFloat)
3811 {
3812 return nullptr;
3813 }
3814 break;
3815 // Note that for bitwise ops, type checking is done in promote() to
3816 // share code between ops and compound assignment
3817 default:
3818 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003819 }
3820
Olli Etuahofc1806e2015-03-17 13:03:11 +02003821 return intermediate.addBinaryMath(op, left, right, loc);
3822}
3823
Jamie Madillb98c3a82015-07-23 14:26:04 -04003824TIntermTyped *TParseContext::addBinaryMath(TOperator op,
3825 TIntermTyped *left,
3826 TIntermTyped *right,
3827 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003828{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003829 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003830 if (node == 0)
3831 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003832 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3833 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003834 recover();
3835 return left;
3836 }
3837 return node;
3838}
3839
Jamie Madillb98c3a82015-07-23 14:26:04 -04003840TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
3841 TIntermTyped *left,
3842 TIntermTyped *right,
3843 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003844{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003845 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003846 if (node == 0)
3847 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003848 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3849 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003850 recover();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003851 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02003852 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003853 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
3854 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003855 }
3856 return node;
3857}
3858
Jamie Madillb98c3a82015-07-23 14:26:04 -04003859TIntermTyped *TParseContext::createAssign(TOperator op,
3860 TIntermTyped *left,
3861 TIntermTyped *right,
3862 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003863{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003864 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003865 {
3866 return intermediate.addAssign(op, left, right, loc);
3867 }
3868 return nullptr;
3869}
3870
Jamie Madillb98c3a82015-07-23 14:26:04 -04003871TIntermTyped *TParseContext::addAssign(TOperator op,
3872 TIntermTyped *left,
3873 TIntermTyped *right,
3874 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003875{
3876 TIntermTyped *node = createAssign(op, left, right, loc);
3877 if (node == nullptr)
3878 {
3879 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
3880 recover();
3881 return left;
3882 }
3883 return node;
3884}
3885
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003886TIntermTyped *TParseContext::addComma(TIntermTyped *left,
3887 TIntermTyped *right,
3888 const TSourceLoc &loc)
3889{
Corentin Wallez0d959252016-07-12 17:26:32 -04003890 // WebGL2 section 5.26, the following results in an error:
3891 // "Sequence operator applied to void, arrays, or structs containing arrays"
3892 if (mShaderSpec == SH_WEBGL2_SPEC && (left->isArray() || left->getBasicType() == EbtVoid ||
3893 left->getType().isStructureContainingArrays() ||
3894 right->isArray() || right->getBasicType() == EbtVoid ||
3895 right->getType().isStructureContainingArrays()))
3896 {
3897 error(loc,
3898 "sequence operator is not allowed for void, arrays, or structs containing arrays",
3899 ",");
3900 recover();
3901 }
3902
Olli Etuaho15200042015-11-04 16:56:31 +02003903 return intermediate.addComma(left, right, loc, mShaderVersion);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003904}
3905
Olli Etuaho49300862015-02-20 14:54:49 +02003906TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3907{
3908 switch (op)
3909 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003910 case EOpContinue:
3911 if (mLoopNestingLevel <= 0)
3912 {
3913 error(loc, "continue statement only allowed in loops", "");
3914 recover();
3915 }
3916 break;
3917 case EOpBreak:
3918 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
3919 {
3920 error(loc, "break statement only allowed in loops and switch statements", "");
3921 recover();
3922 }
3923 break;
3924 case EOpReturn:
3925 if (mCurrentFunctionType->getBasicType() != EbtVoid)
3926 {
3927 error(loc, "non-void function must return a value", "return");
3928 recover();
3929 }
3930 break;
3931 default:
3932 // No checks for discard
3933 break;
Olli Etuaho49300862015-02-20 14:54:49 +02003934 }
3935 return intermediate.addBranch(op, loc);
3936}
3937
Jamie Madillb98c3a82015-07-23 14:26:04 -04003938TIntermBranch *TParseContext::addBranch(TOperator op,
3939 TIntermTyped *returnValue,
3940 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02003941{
3942 ASSERT(op == EOpReturn);
3943 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003944 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02003945 {
3946 error(loc, "void function cannot return a value", "return");
3947 recover();
3948 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003949 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02003950 {
3951 error(loc, "function return is not matching type:", "return");
3952 recover();
3953 }
3954 return intermediate.addBranch(op, returnValue, loc);
3955}
3956
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003957void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
3958{
3959 ASSERT(!functionCall->isUserDefined());
3960 const TString &name = functionCall->getName();
3961 TIntermNode *offset = nullptr;
3962 TIntermSequence *arguments = functionCall->getSequence();
3963 if (name.compare(0, 16, "texelFetchOffset") == 0 ||
3964 name.compare(0, 16, "textureLodOffset") == 0 ||
3965 name.compare(0, 20, "textureProjLodOffset") == 0 ||
3966 name.compare(0, 17, "textureGradOffset") == 0 ||
3967 name.compare(0, 21, "textureProjGradOffset") == 0)
3968 {
3969 offset = arguments->back();
3970 }
3971 else if (name.compare(0, 13, "textureOffset") == 0 ||
3972 name.compare(0, 17, "textureProjOffset") == 0)
3973 {
3974 // A bias parameter might follow the offset parameter.
3975 ASSERT(arguments->size() >= 3);
3976 offset = (*arguments)[2];
3977 }
3978 if (offset != nullptr)
3979 {
3980 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
3981 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
3982 {
3983 TString unmangledName = TFunction::unmangleName(name);
3984 error(functionCall->getLine(), "Texture offset must be a constant expression",
3985 unmangledName.c_str());
3986 recover();
3987 }
3988 else
3989 {
3990 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
3991 size_t size = offsetConstantUnion->getType().getObjectSize();
3992 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
3993 for (size_t i = 0u; i < size; ++i)
3994 {
3995 int offsetValue = values[i].getIConst();
3996 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
3997 {
3998 std::stringstream tokenStream;
3999 tokenStream << offsetValue;
4000 std::string token = tokenStream.str();
4001 error(offset->getLine(), "Texture offset value out of valid range",
4002 token.c_str());
4003 recover();
4004 }
4005 }
4006 }
4007 }
4008}
4009
Jamie Madillb98c3a82015-07-23 14:26:04 -04004010TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
4011 TIntermNode *paramNode,
4012 TIntermNode *thisNode,
4013 const TSourceLoc &loc,
4014 bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004015{
Jamie Madillb98c3a82015-07-23 14:26:04 -04004016 *fatalError = false;
4017 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004018 TIntermTyped *callNode = nullptr;
4019
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004020 if (thisNode != nullptr)
4021 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004022 TConstantUnion *unionArray = new TConstantUnion[1];
Jamie Madillb98c3a82015-07-23 14:26:04 -04004023 int arraySize = 0;
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004024 TIntermTyped *typedThis = thisNode->getAsTyped();
4025 if (fnCall->getName() != "length")
4026 {
4027 error(loc, "invalid method", fnCall->getName().c_str());
4028 recover();
4029 }
4030 else if (paramNode != nullptr)
4031 {
4032 error(loc, "method takes no parameters", "length");
4033 recover();
4034 }
4035 else if (typedThis == nullptr || !typedThis->isArray())
4036 {
4037 error(loc, "length can only be called on arrays", "length");
4038 recover();
4039 }
4040 else
4041 {
Olli Etuaho96e67382015-04-23 14:27:02 +03004042 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03004043 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004044 {
Olli Etuaho39282e12015-04-23 15:41:48 +03004045 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004046 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03004047 // (func()).length()
4048 // (int[3](0, 1, 2)).length()
Jamie Madillb98c3a82015-07-23 14:26:04 -04004049 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
4050 // expression.
4051 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
4052 // spec section 5.9 which allows "An array, vector or matrix expression with the
4053 // length method applied".
4054 error(loc, "length can only be called on array names, not on array expressions",
4055 "length");
Olli Etuaho39282e12015-04-23 15:41:48 +03004056 recover();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004057 }
4058 }
Olli Etuaho96e67382015-04-23 14:27:02 +03004059 unionArray->setIConst(arraySize);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004060 callNode =
4061 intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004062 }
4063 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004064 {
4065 //
4066 // Then this should be a constructor.
4067 // Don't go through the symbol table for constructors.
4068 // Their parameters will be verified algorithmically.
4069 //
4070 TType type(EbtVoid, EbpUndefined); // use this to get the type back
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004071 if (!constructorErrorCheck(loc, paramNode, *fnCall, op, &type))
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004072 {
4073 //
4074 // It's a constructor, of type 'type'.
4075 //
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004076 callNode = addConstructor(paramNode, &type, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004077 }
Olli Etuaho72ba85b2015-03-04 14:23:26 +02004078
4079 if (callNode == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004080 {
4081 recover();
4082 callNode = intermediate.setAggregateOperator(nullptr, op, loc);
4083 }
4084 callNode->setType(type);
4085 }
4086 else
4087 {
4088 //
4089 // Not a constructor. Find it in the symbol table.
4090 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05304091 const TFunction *fnCandidate;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004092 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004093 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004094 if (fnCandidate)
4095 {
4096 //
4097 // A declared function.
4098 //
4099 if (builtIn && !fnCandidate->getExtension().empty() &&
4100 extensionErrorCheck(loc, fnCandidate->getExtension()))
4101 {
4102 recover();
4103 }
4104 op = fnCandidate->getBuiltInOp();
4105 if (builtIn && op != EOpNull)
4106 {
4107 //
4108 // A function call mapped to a built-in operation.
4109 //
4110 if (fnCandidate->getParamCount() == 1)
4111 {
4112 //
4113 // Treat it like a built-in unary operator.
4114 //
Olli Etuaho15c2ac32015-11-09 15:51:43 +02004115 TIntermAggregate *paramAgg = paramNode->getAsAggregate();
4116 paramNode = paramAgg->getSequence()->front();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004117 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc,
4118 &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004119 if (callNode == nullptr)
4120 {
4121 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004122 extraInfoStream
4123 << "built in unary operator function. Type: "
4124 << static_cast<TIntermTyped *>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004125 std::string extraInfo = extraInfoStream.str();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004126 error(paramNode->getLine(), " wrong operand type", "Internal Error",
4127 extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004128 *fatalError = true;
4129 return nullptr;
4130 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004131 }
4132 else
4133 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004134 TIntermAggregate *aggregate =
4135 intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004136 aggregate->setType(fnCandidate->getReturnType());
4137 aggregate->setPrecisionFromChildren();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02004138 if (aggregate->areChildrenConstQualified())
4139 {
4140 aggregate->getTypePointer()->setQualifier(EvqConst);
4141 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004142
4143 // Some built-in functions have out parameters too.
4144 functionCallLValueErrorCheck(fnCandidate, aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05304145
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004146 // See if we can constant fold a built-in. Note that this may be possible even
4147 // if it is not const-qualified.
Olli Etuahob43846e2015-06-02 18:18:57 +03004148 TIntermTyped *foldedNode = intermediate.foldAggregateBuiltIn(aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05304149 if (foldedNode)
4150 {
Arun Patole274f0702015-05-05 13:33:30 +05304151 callNode = foldedNode;
4152 }
Olli Etuahob43846e2015-06-02 18:18:57 +03004153 else
4154 {
4155 callNode = aggregate;
4156 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004157 }
4158 }
4159 else
4160 {
4161 // This is a real function call
Jamie Madillb98c3a82015-07-23 14:26:04 -04004162 TIntermAggregate *aggregate =
4163 intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004164 aggregate->setType(fnCandidate->getReturnType());
4165
Jamie Madillb98c3a82015-07-23 14:26:04 -04004166 // this is how we know whether the given function is a builtIn function or a user
4167 // defined function
4168 // if builtIn == false, it's a userDefined -> could be an overloaded
4169 // builtIn function also
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004170 // if builtIn == true, it's definitely a builtIn function with EOpNull
4171 if (!builtIn)
4172 aggregate->setUserDefined();
4173 aggregate->setName(fnCandidate->getMangledName());
Corentin Wallez71d147f2015-02-11 11:15:24 -08004174 aggregate->setFunctionId(fnCandidate->getUniqueId());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004175
4176 // This needs to happen after the name is set
4177 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004178 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004179 aggregate->setBuiltInFunctionPrecision();
4180
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004181 checkTextureOffsetConst(aggregate);
4182 }
4183
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004184 callNode = aggregate;
4185
4186 functionCallLValueErrorCheck(fnCandidate, aggregate);
4187 }
4188 }
4189 else
4190 {
4191 // error message was put out by findFunction()
4192 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004193 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004194 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004195 callNode = intermediate.addConstantUnion(unionArray,
4196 TType(EbtFloat, EbpUndefined, EvqConst), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004197 recover();
4198 }
4199 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004200 return callNode;
4201}
4202
Jamie Madillb98c3a82015-07-23 14:26:04 -04004203TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
4204 TIntermTyped *trueBlock,
4205 TIntermTyped *falseBlock,
Olli Etuaho52901742015-04-15 13:42:45 +03004206 const TSourceLoc &loc)
4207{
4208 if (boolErrorCheck(loc, cond))
4209 recover();
4210
4211 if (trueBlock->getType() != falseBlock->getType())
4212 {
4213 binaryOpError(loc, ":", trueBlock->getCompleteString(), falseBlock->getCompleteString());
4214 recover();
4215 return falseBlock;
4216 }
Olli Etuahoa2d53032015-04-15 14:14:44 +03004217 // ESSL1 sections 5.2 and 5.7:
4218 // ESSL3 section 5.7:
4219 // Ternary operator is not among the operators allowed for structures/arrays.
4220 if (trueBlock->isArray() || trueBlock->getBasicType() == EbtStruct)
4221 {
4222 error(loc, "ternary operator is not allowed for structures or arrays", ":");
4223 recover();
4224 return falseBlock;
4225 }
Corentin Wallez0d959252016-07-12 17:26:32 -04004226 // WebGL2 section 5.26, the following results in an error:
4227 // "Ternary operator applied to void, arrays, or structs containing arrays"
4228 if (mShaderSpec == SH_WEBGL2_SPEC && trueBlock->getBasicType() == EbtVoid)
4229 {
4230 error(loc, "ternary operator is not allowed for void", ":");
4231 recover();
4232 return falseBlock;
4233 }
4234
Olli Etuaho52901742015-04-15 13:42:45 +03004235 return intermediate.addSelection(cond, trueBlock, falseBlock, loc);
4236}
Olli Etuaho49300862015-02-20 14:54:49 +02004237
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004238//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004239// Parse an array of strings using yyparse.
4240//
4241// Returns 0 for success.
4242//
Jamie Madillb98c3a82015-07-23 14:26:04 -04004243int PaParseStrings(size_t count,
4244 const char *const string[],
4245 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05304246 TParseContext *context)
4247{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004248 if ((count == 0) || (string == NULL))
4249 return 1;
4250
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004251 if (glslang_initialize(context))
4252 return 1;
4253
alokp@chromium.org408c45e2012-04-05 15:54:43 +00004254 int error = glslang_scan(count, string, length, context);
4255 if (!error)
4256 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004257
alokp@chromium.org73bc2982012-06-19 18:48:05 +00004258 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00004259
alokp@chromium.org6b495712012-06-29 00:06:58 +00004260 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004261}