blob: b23ee526f41482b1c5343fe01d640f2ae6b3b526 [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
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000135
136//
137// Used by flex/bison to output all syntax and parsing errors.
138//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530139void TParseContext::error(const TSourceLoc &loc,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400140 const char *reason,
141 const char *token,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530142 const char *extraInfo)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000143{
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000144 pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -0400145 srcLoc.file = loc.first_file;
146 srcLoc.line = loc.first_line;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400147 mDiagnostics.writeInfo(pp::Diagnostics::PP_ERROR, srcLoc, reason, token, extraInfo);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000148}
149
Arun Patole7e7e68d2015-05-22 12:02:25 +0530150void TParseContext::warning(const TSourceLoc &loc,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400151 const char *reason,
152 const char *token,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530153 const char *extraInfo)
154{
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000155 pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -0400156 srcLoc.file = loc.first_file;
157 srcLoc.line = loc.first_line;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400158 mDiagnostics.writeInfo(pp::Diagnostics::PP_WARNING, srcLoc, reason, token, extraInfo);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000159}
160
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200161void TParseContext::outOfRangeError(bool isError,
162 const TSourceLoc &loc,
163 const char *reason,
164 const char *token,
165 const char *extraInfo)
166{
167 if (isError)
168 {
169 error(loc, reason, token, extraInfo);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200170 }
171 else
172 {
173 warning(loc, reason, token, extraInfo);
174 }
175}
176
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000177//
178// Same error message for all places assignments don't work.
179//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530180void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000181{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000182 std::stringstream extraInfoStream;
183 extraInfoStream << "cannot convert from '" << right << "' to '" << left << "'";
184 std::string extraInfo = extraInfoStream.str();
185 error(line, "", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000186}
187
188//
189// Same error message for all places unary operations don't work.
190//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530191void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000192{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000193 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400194 extraInfoStream << "no operation '" << op << "' exists that takes an operand of type "
195 << operand << " (or there is no acceptable conversion)";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000196 std::string extraInfo = extraInfoStream.str();
197 error(line, " wrong operand type", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000198}
199
200//
201// Same error message for all binary operations don't work.
202//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400203void TParseContext::binaryOpError(const TSourceLoc &line,
204 const char *op,
205 TString left,
206 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000207{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000208 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400209 extraInfoStream << "no operation '" << op << "' exists that takes a left-hand operand of type '"
210 << left << "' and a right operand of type '" << right
211 << "' (or there is no acceptable conversion)";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000212 std::string extraInfo = extraInfoStream.str();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530213 error(line, " wrong operand types ", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000214}
215
Olli Etuaho383b7912016-08-05 11:22:59 +0300216void TParseContext::precisionErrorCheck(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400217 TPrecision precision,
218 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530219{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400220 if (!mChecksPrecisionErrors)
Olli Etuaho383b7912016-08-05 11:22:59 +0300221 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200222 if (precision == EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530223 {
Olli Etuaho183d7e22015-11-20 15:59:09 +0200224 switch (type)
225 {
226 case EbtFloat:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400227 error(line, "No precision specified for (float)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300228 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200229 case EbtInt:
230 case EbtUInt:
231 UNREACHABLE(); // there's always a predeclared qualifier
Jamie Madillb98c3a82015-07-23 14:26:04 -0400232 error(line, "No precision specified (int)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300233 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200234 default:
235 if (IsSampler(type))
236 {
237 error(line, "No precision specified (sampler)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300238 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200239 }
240 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000241 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000242}
243
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000244//
245// Both test and if necessary, spit out an error, to see if the node is really
246// an l-value that can be operated on this way.
247//
248// Returns true if the was an error.
249//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530250bool TParseContext::lValueErrorCheck(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000251{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400252 TIntermSymbol *symNode = node->getAsSymbolNode();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530253 TIntermBinary *binaryNode = node->getAsBinaryNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000254
Arun Patole7e7e68d2015-05-22 12:02:25 +0530255 if (binaryNode)
256 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000257 bool errorReturn;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000258
Jamie Madillb98c3a82015-07-23 14:26:04 -0400259 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530260 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400261 case EOpIndexDirect:
262 case EOpIndexIndirect:
263 case EOpIndexDirectStruct:
264 case EOpIndexDirectInterfaceBlock:
265 return lValueErrorCheck(line, op, binaryNode->getLeft());
266 case EOpVectorSwizzle:
267 errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft());
268 if (!errorReturn)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530269 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400270 int offset[4] = {0, 0, 0, 0};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000271
Jamie Madillb98c3a82015-07-23 14:26:04 -0400272 TIntermTyped *rightNode = binaryNode->getRight();
273 TIntermAggregate *aggrNode = rightNode->getAsAggregate();
274
275 for (TIntermSequence::iterator p = aggrNode->getSequence()->begin();
276 p != aggrNode->getSequence()->end(); p++)
277 {
278 int value = (*p)->getAsTyped()->getAsConstantUnion()->getIConst(0);
279 offset[value]++;
280 if (offset[value] > 1)
281 {
282 error(line, " l-value of swizzle cannot have duplicate components", op);
283
284 return true;
285 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000286 }
287 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000288
Jamie Madillb98c3a82015-07-23 14:26:04 -0400289 return errorReturn;
290 default:
291 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000292 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000293 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000294
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000295 return true;
296 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000297
Arun Patole7e7e68d2015-05-22 12:02:25 +0530298 const char *symbol = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000299 if (symNode != 0)
300 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000301
Arun Patole7e7e68d2015-05-22 12:02:25 +0530302 const char *message = 0;
303 switch (node->getQualifier())
304 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400305 case EvqConst:
306 message = "can't modify a const";
307 break;
308 case EvqConstReadOnly:
309 message = "can't modify a const";
310 break;
311 case EvqAttribute:
312 message = "can't modify an attribute";
313 break;
314 case EvqFragmentIn:
315 message = "can't modify an input";
316 break;
317 case EvqVertexIn:
318 message = "can't modify an input";
319 break;
320 case EvqUniform:
321 message = "can't modify a uniform";
322 break;
323 case EvqVaryingIn:
324 message = "can't modify a varying";
325 break;
326 case EvqFragCoord:
327 message = "can't modify gl_FragCoord";
328 break;
329 case EvqFrontFacing:
330 message = "can't modify gl_FrontFacing";
331 break;
332 case EvqPointCoord:
333 message = "can't modify gl_PointCoord";
334 break;
Martin Radevb0883602016-08-04 17:48:58 +0300335 case EvqNumWorkGroups:
336 message = "can't modify gl_NumWorkGroups";
337 break;
338 case EvqWorkGroupSize:
339 message = "can't modify gl_WorkGroupSize";
340 break;
341 case EvqWorkGroupID:
342 message = "can't modify gl_WorkGroupID";
343 break;
344 case EvqLocalInvocationID:
345 message = "can't modify gl_LocalInvocationID";
346 break;
347 case EvqGlobalInvocationID:
348 message = "can't modify gl_GlobalInvocationID";
349 break;
350 case EvqLocalInvocationIndex:
351 message = "can't modify gl_LocalInvocationIndex";
352 break;
Martin Radev802abe02016-08-04 17:48:32 +0300353 case EvqComputeIn:
354 message = "can't modify work group size variable";
355 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400356 default:
357 //
358 // Type that can't be written to?
359 //
360 if (node->getBasicType() == EbtVoid)
361 {
362 message = "can't modify void";
363 }
364 if (IsSampler(node->getBasicType()))
365 {
366 message = "can't modify a sampler";
367 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000368 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000369
Arun Patole7e7e68d2015-05-22 12:02:25 +0530370 if (message == 0 && binaryNode == 0 && symNode == 0)
371 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000372 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000373
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000374 return true;
375 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000376
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000377 //
378 // Everything else is okay, no error.
379 //
380 if (message == 0)
381 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000382
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000383 //
384 // If we get here, we have an error and a message.
385 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530386 if (symNode)
387 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000388 std::stringstream extraInfoStream;
389 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
390 std::string extraInfo = extraInfoStream.str();
391 error(line, " l-value required", op, extraInfo.c_str());
392 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530393 else
394 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000395 std::stringstream extraInfoStream;
396 extraInfoStream << "(" << message << ")";
397 std::string extraInfo = extraInfoStream.str();
398 error(line, " l-value required", op, extraInfo.c_str());
399 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000400
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000401 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000402}
403
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000404// Both test, and if necessary spit out an error, to see if the node is really
405// a constant.
Olli Etuaho383b7912016-08-05 11:22:59 +0300406void TParseContext::constErrorCheck(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000407{
Olli Etuaho383b7912016-08-05 11:22:59 +0300408 if (node->getQualifier() != EvqConst)
409 {
410 error(node->getLine(), "constant expression required", "");
411 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000412}
413
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000414// Both test, and if necessary spit out an error, to see if the node is really
415// an integer.
Olli Etuaho383b7912016-08-05 11:22:59 +0300416void TParseContext::integerErrorCheck(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000417{
Olli Etuaho383b7912016-08-05 11:22:59 +0300418 if (!node->isScalarInt())
419 {
420 error(node->getLine(), "integer expression required", token);
421 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000422}
423
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000424// Both test, and if necessary spit out an error, to see if we are currently
425// globally scoped.
Olli Etuaho383b7912016-08-05 11:22:59 +0300426void TParseContext::globalErrorCheck(const TSourceLoc &line, bool global, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000427{
Olli Etuaho383b7912016-08-05 11:22:59 +0300428 if (!global)
429 {
430 error(line, "only allowed at global scope", token);
431 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000432}
433
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000434// For now, keep it simple: if it starts "gl_", it's reserved, independent
435// of scope. Except, if the symbol table is at the built-in push-level,
436// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000437// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
438// webgl shader.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000439//
440// Returns true if there was an error.
441//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530442bool TParseContext::reservedErrorCheck(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000443{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530444 static const char *reservedErrMsg = "reserved built-in name";
445 if (!symbolTable.atBuiltInLevel())
446 {
447 if (identifier.compare(0, 3, "gl_") == 0)
448 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000449 error(line, reservedErrMsg, "gl_");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000450 return true;
451 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530452 if (IsWebGLBasedSpec(mShaderSpec))
453 {
454 if (identifier.compare(0, 6, "webgl_") == 0)
455 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000456 error(line, reservedErrMsg, "webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000457 return true;
458 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530459 if (identifier.compare(0, 7, "_webgl_") == 0)
460 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000461 error(line, reservedErrMsg, "_webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000462 return true;
463 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530464 if (mShaderSpec == SH_CSS_SHADERS_SPEC && identifier.compare(0, 4, "css_") == 0)
465 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000466 error(line, reservedErrMsg, "css_");
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000467 return true;
468 }
alokp@chromium.org613ef312010-07-21 18:54:22 +0000469 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530470 if (identifier.find("__") != TString::npos)
471 {
472 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400473 "identifiers containing two consecutive underscores (__) are reserved as "
474 "possible future keywords",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530475 identifier.c_str());
daniel@transgaming.combeadd5d2012-04-12 02:35:31 +0000476 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000477 }
478 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000479
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000480 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000481}
482
483//
484// Make sure there is enough data provided to the constructor to build
485// something of the type of the constructor. Also returns the type of
486// the constructor.
487//
488// Returns true if there was an error in construction.
489//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400490bool TParseContext::constructorErrorCheck(const TSourceLoc &line,
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200491 TIntermNode *argumentsNode,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400492 TFunction &function,
493 TOperator op,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530494 TType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000495{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000496 *type = function.getReturnType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000497
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000498 bool constructingMatrix = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400499 switch (op)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530500 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400501 case EOpConstructMat2:
502 case EOpConstructMat2x3:
503 case EOpConstructMat2x4:
504 case EOpConstructMat3x2:
505 case EOpConstructMat3:
506 case EOpConstructMat3x4:
507 case EOpConstructMat4x2:
508 case EOpConstructMat4x3:
509 case EOpConstructMat4:
510 constructingMatrix = true;
511 break;
512 default:
513 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000514 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000515
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000516 //
517 // Note: It's okay to have too many components available, but not okay to have unused
518 // arguments. 'full' will go to true when enough args have been seen. If we loop
519 // again, there is an extra argument, so 'overfull' will become true.
520 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000521
Jamie Madillb98c3a82015-07-23 14:26:04 -0400522 size_t size = 0;
523 bool constType = true;
524 bool full = false;
525 bool overFull = false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000526 bool matrixInMatrix = false;
527 bool arrayArg = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530528 for (size_t i = 0; i < function.getParamCount(); ++i)
529 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700530 const TConstParameter &param = function.getParam(i);
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000531 size += param.type->getObjectSize();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530532
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000533 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000534 matrixInMatrix = true;
535 if (full)
536 overFull = true;
537 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
538 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000539 if (param.type->getQualifier() != EvqConst)
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000540 constType = false;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000541 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000542 arrayArg = true;
543 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530544
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000545 if (constType)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000546 type->setQualifier(EvqConst);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000547
Olli Etuaho376f1b52015-04-13 13:23:41 +0300548 if (type->isArray())
549 {
550 if (type->isUnsizedArray())
551 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700552 type->setArraySize(static_cast<int>(function.getParamCount()));
Olli Etuaho376f1b52015-04-13 13:23:41 +0300553 }
554 else if (static_cast<size_t>(type->getArraySize()) != function.getParamCount())
555 {
556 error(line, "array constructor needs one argument per array element", "constructor");
557 return true;
558 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000559 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000560
Arun Patole7e7e68d2015-05-22 12:02:25 +0530561 if (arrayArg && op != EOpConstructStruct)
562 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000563 error(line, "constructing from a non-dereferenced array", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000564 return true;
565 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000566
Arun Patole7e7e68d2015-05-22 12:02:25 +0530567 if (matrixInMatrix && !type->isArray())
568 {
569 if (function.getParamCount() != 1)
570 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400571 error(line, "constructing matrix from matrix can only take one argument",
572 "constructor");
573 return true;
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000574 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000575 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000576
Arun Patole7e7e68d2015-05-22 12:02:25 +0530577 if (overFull)
578 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000579 error(line, "too many arguments", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000580 return true;
581 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530582
Jamie Madillb98c3a82015-07-23 14:26:04 -0400583 if (op == EOpConstructStruct && !type->isArray() &&
584 type->getStruct()->fields().size() != function.getParamCount())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530585 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400586 error(line,
587 "Number of constructor parameters does not match the number of structure fields",
588 "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000589 return true;
590 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000591
Arun Patole7e7e68d2015-05-22 12:02:25 +0530592 if (!type->isMatrix() || !matrixInMatrix)
593 {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000594 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
Arun Patole7e7e68d2015-05-22 12:02:25 +0530595 (op == EOpConstructStruct && size < type->getObjectSize()))
596 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000597 error(line, "not enough data provided for construction", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000598 return true;
599 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000600 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000601
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200602 if (argumentsNode == nullptr)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530603 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200604 error(line, "constructor does not have any arguments", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000605 return true;
606 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200607
608 TIntermAggregate *argumentsAgg = argumentsNode->getAsAggregate();
609 for (TIntermNode *&argNode : *argumentsAgg->getSequence())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530610 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200611 TIntermTyped *argTyped = argNode->getAsTyped();
612 ASSERT(argTyped != nullptr);
613 if (op != EOpConstructStruct && IsSampler(argTyped->getBasicType()))
614 {
615 error(line, "cannot convert a sampler", "constructor");
616 return true;
617 }
618 if (argTyped->getBasicType() == EbtVoid)
619 {
620 error(line, "cannot convert a void", "constructor");
621 return true;
622 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000623 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000624
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000625 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000626}
627
Jamie Madillb98c3a82015-07-23 14:26:04 -0400628// This function checks to see if a void variable has been declared and raise an error message for
629// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000630//
631// returns true in case of an error
632//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400633bool TParseContext::voidErrorCheck(const TSourceLoc &line,
634 const TString &identifier,
635 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000636{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300637 if (type == EbtVoid)
638 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000639 error(line, "illegal use of type 'void'", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000640 return true;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300641 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000642
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000643 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000644}
645
Jamie Madillb98c3a82015-07-23 14:26:04 -0400646// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300647// or not.
648void TParseContext::boolErrorCheck(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000649{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530650 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector())
651 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000652 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530653 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000654}
655
Jamie Madillb98c3a82015-07-23 14:26:04 -0400656// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300657// or not.
658void TParseContext::boolErrorCheck(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000659{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530660 if (pType.type != EbtBool || pType.isAggregate())
661 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000662 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530663 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000664}
665
Jamie Madillb98c3a82015-07-23 14:26:04 -0400666bool TParseContext::samplerErrorCheck(const TSourceLoc &line,
667 const TPublicType &pType,
668 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000669{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530670 if (pType.type == EbtStruct)
671 {
672 if (containsSampler(*pType.userDef))
673 {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000674 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530675
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000676 return true;
677 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530678
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000679 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530680 }
681 else if (IsSampler(pType.type))
682 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000683 error(line, reason, getBasicString(pType.type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000684
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000685 return true;
686 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000687
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000688 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000689}
690
Olli Etuaho383b7912016-08-05 11:22:59 +0300691void TParseContext::locationDeclaratorListCheck(const TSourceLoc &line, const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400692{
693 if (pType.layoutQualifier.location != -1)
694 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400695 error(line, "location must only be specified for a single input or output variable",
696 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400697 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400698}
699
Olli Etuaho383b7912016-08-05 11:22:59 +0300700void TParseContext::parameterSamplerErrorCheck(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400701 TQualifier qualifier,
702 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000703{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400704 if ((qualifier == EvqOut || qualifier == EvqInOut) && type.getBasicType() != EbtStruct &&
705 IsSampler(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530706 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000707 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000708 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000709}
710
Arun Patole7e7e68d2015-05-22 12:02:25 +0530711bool TParseContext::containsSampler(const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000712{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000713 if (IsSampler(type.getBasicType()))
714 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000715
Arun Patole7e7e68d2015-05-22 12:02:25 +0530716 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock())
717 {
718 const TFieldList &fields = type.getStruct()->fields();
719 for (unsigned int i = 0; i < fields.size(); ++i)
720 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400721 if (containsSampler(*fields[i]->type()))
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000722 return true;
723 }
724 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000725
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000726 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000727}
728
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000729// Do size checking for an array type's size.
Olli Etuaho383b7912016-08-05 11:22:59 +0300730void TParseContext::arraySizeErrorCheck(const TSourceLoc &line, TIntermTyped *expr, int &size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000731{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530732 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000733
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200734 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
735 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
736 // fold as array size.
737 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000738 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000739 error(line, "array size must be a constant integer expression", "");
Olli Etuahoe7847b02015-03-16 11:56:12 +0200740 size = 1;
Olli Etuaho383b7912016-08-05 11:22:59 +0300741 return;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000742 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000743
Nicolas Capens906744a2014-06-06 15:18:07 -0400744 unsigned int unsignedSize = 0;
745
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000746 if (constant->getBasicType() == EbtUInt)
747 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400748 unsignedSize = constant->getUConst(0);
Jamie Madillb98c3a82015-07-23 14:26:04 -0400749 size = static_cast<int>(unsignedSize);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000750 }
751 else
752 {
753 size = constant->getIConst(0);
754
Nicolas Capens906744a2014-06-06 15:18:07 -0400755 if (size < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000756 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400757 error(line, "array size must be non-negative", "");
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000758 size = 1;
Olli Etuaho383b7912016-08-05 11:22:59 +0300759 return;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000760 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400761
762 unsignedSize = static_cast<unsigned int>(size);
763 }
764
765 if (size == 0)
766 {
767 error(line, "array size must be greater than zero", "");
768 size = 1;
Olli Etuaho383b7912016-08-05 11:22:59 +0300769 return;
Nicolas Capens906744a2014-06-06 15:18:07 -0400770 }
771
772 // The size of arrays is restricted here to prevent issues further down the
773 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
774 // 4096 registers so this should be reasonable even for aggressively optimizable code.
775 const unsigned int sizeLimit = 65536;
776
777 if (unsignedSize > sizeLimit)
778 {
779 error(line, "array size too large", "");
780 size = 1;
Olli Etuaho383b7912016-08-05 11:22:59 +0300781 return;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000782 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000783}
784
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000785// See if this qualifier can be an array.
786//
787// Returns true if there is an error.
788//
Olli Etuaho3739d232015-04-08 12:23:44 +0300789bool TParseContext::arrayQualifierErrorCheck(const TSourceLoc &line, const TPublicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000790{
Olli Etuaho3739d232015-04-08 12:23:44 +0300791 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqVertexIn) ||
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400792 (type.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300793 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400794 error(line, "cannot declare arrays of this qualifier",
795 TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000796 return true;
797 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000798
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000799 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000800}
801
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000802// See if this type can be an array.
803//
804// Returns true if there is an error.
805//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530806bool TParseContext::arrayTypeErrorCheck(const TSourceLoc &line, const TPublicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000807{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000808 //
809 // Can the type be an array?
810 //
Jamie Madill06145232015-05-13 13:10:01 -0400811 if (type.array)
812 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000813 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000814 return true;
815 }
Olli Etuahocc36b982015-07-10 14:14:18 +0300816 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
817 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
818 // 4.3.4).
819 if (mShaderVersion >= 300 && type.type == EbtStruct && sh::IsVarying(type.qualifier))
820 {
821 error(line, "cannot declare arrays of structs of this qualifier",
822 TType(type).getCompleteString().c_str());
823 return true;
824 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000825
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000826 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000827}
828
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000829// Enforce non-initializer type/qualifier rules.
Olli Etuaho383b7912016-08-05 11:22:59 +0300830void TParseContext::nonInitErrorCheck(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400831 const TString &identifier,
832 TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000833{
Olli Etuaho3739d232015-04-08 12:23:44 +0300834 ASSERT(type != nullptr);
835 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000836 {
837 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300838 type->qualifier = EvqTemporary;
839
840 // Generate informative error messages for ESSL1.
841 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400842 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000843 {
Arun Patole7e7e68d2015-05-22 12:02:25 +0530844 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400845 "structures containing arrays may not be declared constant since they cannot be "
846 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530847 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000848 }
849 else
850 {
851 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
852 }
Olli Etuaho383b7912016-08-05 11:22:59 +0300853 return;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000854 }
Olli Etuaho376f1b52015-04-13 13:23:41 +0300855 if (type->isUnsizedArray())
856 {
857 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
Olli Etuaho376f1b52015-04-13 13:23:41 +0300858 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000859}
860
Olli Etuaho2935c582015-04-08 14:32:06 +0300861// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000862// and update the symbol table.
863//
Olli Etuaho2935c582015-04-08 14:32:06 +0300864// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000865//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400866bool TParseContext::declareVariable(const TSourceLoc &line,
867 const TString &identifier,
868 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +0300869 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000870{
Olli Etuaho2935c582015-04-08 14:32:06 +0300871 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000872
Olli Etuaho2935c582015-04-08 14:32:06 +0300873 bool needsReservedErrorCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000874
Olli Etuaho2935c582015-04-08 14:32:06 +0300875 // gl_LastFragData may be redeclared with a new precision qualifier
876 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
877 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400878 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
879 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho2935c582015-04-08 14:32:06 +0300880 if (type.getArraySize() == maxDrawBuffers->getConstPointer()->getIConst())
881 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400882 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +0300883 {
884 needsReservedErrorCheck = extensionErrorCheck(line, builtInSymbol->getExtension());
885 }
886 }
887 else
888 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400889 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
890 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +0300891 return false;
892 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000893 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000894
Olli Etuaho2935c582015-04-08 14:32:06 +0300895 if (needsReservedErrorCheck && reservedErrorCheck(line, identifier))
896 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000897
Olli Etuaho2935c582015-04-08 14:32:06 +0300898 (*variable) = new TVariable(&identifier, type);
899 if (!symbolTable.declare(*variable))
900 {
901 error(line, "redefinition", identifier.c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -0400902 *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +0300903 return false;
904 }
905
906 if (voidErrorCheck(line, identifier, type.getBasicType()))
907 return false;
908
909 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000910}
911
Olli Etuaho383b7912016-08-05 11:22:59 +0300912void TParseContext::paramErrorCheck(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400913 TQualifier qualifier,
914 TQualifier paramQualifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530915 TType *type)
916{
917 if (qualifier != EvqConst && qualifier != EvqTemporary)
918 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000919 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +0300920 return;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000921 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530922 if (qualifier == EvqConst && paramQualifier != EvqIn)
923 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400924 error(line, "qualifier not allowed with ", getQualifierString(qualifier),
925 getQualifierString(paramQualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +0300926 return;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000927 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000928
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000929 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000930 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000931 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000932 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000933}
934
Arun Patole7e7e68d2015-05-22 12:02:25 +0530935bool TParseContext::extensionErrorCheck(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000936{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400937 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000938 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +0530939 if (iter == extBehavior.end())
940 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000941 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000942 return true;
943 }
zmo@google.comf5450912011-09-09 01:37:19 +0000944 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +0530945 if (iter->second == EBhDisable || iter->second == EBhUndefined)
946 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000947 error(line, "extension", extension.c_str(), "is disabled");
948 return true;
949 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530950 if (iter->second == EBhWarn)
951 {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000952 warning(line, "extension", extension.c_str(), "is being used");
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000953 return false;
954 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000955
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000956 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000957}
958
Jamie Madillb98c3a82015-07-23 14:26:04 -0400959// These checks are common for all declarations starting a declarator list, and declarators that
960// follow an empty declaration.
Olli Etuaho383b7912016-08-05 11:22:59 +0300961void TParseContext::singleDeclarationErrorCheck(const TPublicType &publicType,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400962 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -0400963{
Olli Etuahofa33d582015-04-09 14:33:12 +0300964 switch (publicType.qualifier)
965 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400966 case EvqVaryingIn:
967 case EvqVaryingOut:
968 case EvqAttribute:
969 case EvqVertexIn:
970 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +0300971 case EvqComputeIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400972 if (publicType.type == EbtStruct)
973 {
974 error(identifierLocation, "cannot be used with a structure",
975 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +0300976 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400977 }
Olli Etuahofa33d582015-04-09 14:33:12 +0300978
Jamie Madillb98c3a82015-07-23 14:26:04 -0400979 default:
980 break;
Olli Etuahofa33d582015-04-09 14:33:12 +0300981 }
982
Jamie Madillb98c3a82015-07-23 14:26:04 -0400983 if (publicType.qualifier != EvqUniform &&
984 samplerErrorCheck(identifierLocation, publicType, "samplers must be uniform"))
Olli Etuahofa33d582015-04-09 14:33:12 +0300985 {
Olli Etuaho383b7912016-08-05 11:22:59 +0300986 return;
Olli Etuahofa33d582015-04-09 14:33:12 +0300987 }
Jamie Madilla5efff92013-06-06 11:56:47 -0400988
989 // check for layout qualifier issues
990 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
991
992 if (layoutQualifier.matrixPacking != EmpUnspecified)
993 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400994 error(identifierLocation, "layout qualifier",
995 getMatrixPackingString(layoutQualifier.matrixPacking),
Olli Etuahofa33d582015-04-09 14:33:12 +0300996 "only valid for interface blocks");
Olli Etuaho383b7912016-08-05 11:22:59 +0300997 return;
Jamie Madilla5efff92013-06-06 11:56:47 -0400998 }
999
1000 if (layoutQualifier.blockStorage != EbsUnspecified)
1001 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001002 error(identifierLocation, "layout qualifier",
1003 getBlockStorageString(layoutQualifier.blockStorage),
Olli Etuahofa33d582015-04-09 14:33:12 +03001004 "only valid for interface blocks");
Olli Etuaho383b7912016-08-05 11:22:59 +03001005 return;
Jamie Madilla5efff92013-06-06 11:56:47 -04001006 }
1007
Olli Etuaho383b7912016-08-05 11:22:59 +03001008 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut)
Jamie Madilla5efff92013-06-06 11:56:47 -04001009 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001010 layoutLocationErrorCheck(identifierLocation, publicType.layoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04001011 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001012}
1013
Olli Etuaho383b7912016-08-05 11:22:59 +03001014void TParseContext::layoutLocationErrorCheck(const TSourceLoc &location,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001015 const TLayoutQualifier &layoutQualifier)
Jamie Madilla5efff92013-06-06 11:56:47 -04001016{
1017 if (layoutQualifier.location != -1)
1018 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001019 error(location, "invalid layout qualifier:", "location",
1020 "only valid on program inputs and outputs");
Jamie Madilla5efff92013-06-06 11:56:47 -04001021 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001022}
1023
Martin Radev802abe02016-08-04 17:48:32 +03001024void TParseContext::layoutSupportedErrorCheck(const TSourceLoc &location,
1025 const TString &layoutQualifierName,
1026 int versionRequired)
1027{
1028
1029 if (mShaderVersion < versionRequired)
1030 {
1031 error(location, "invalid layout qualifier:", layoutQualifierName.c_str(), "not supported");
Martin Radev802abe02016-08-04 17:48:32 +03001032 }
1033}
1034
1035bool TParseContext::layoutWorkGroupSizeErrorCheck(const TSourceLoc &location,
1036 const TLayoutQualifier &layoutQualifier)
1037{
1038 const TLocalSize &localSize = layoutQualifier.localSize;
1039 for (size_t i = 0u; i < localSize.size(); ++i)
1040 {
1041 if (localSize[i] != -1)
1042 {
1043 error(location, "invalid layout qualifier:", getLocalSizeString(i),
1044 "only valid when used with 'in' in a compute shader global layout declaration");
1045 return true;
1046 }
1047 }
1048
1049 return false;
1050}
1051
Olli Etuaho383b7912016-08-05 11:22:59 +03001052void TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001053 TIntermAggregate *aggregate)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001054{
1055 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1056 {
1057 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1058 if (qual == EvqOut || qual == EvqInOut)
1059 {
1060 TIntermTyped *node = (*(aggregate->getSequence()))[i]->getAsTyped();
1061 if (lValueErrorCheck(node->getLine(), "assign", node))
1062 {
1063 error(node->getLine(),
Jamie Madillb98c3a82015-07-23 14:26:04 -04001064 "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error");
Olli Etuaho383b7912016-08-05 11:22:59 +03001065 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001066 }
1067 }
1068 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001069}
1070
Jamie Madillb98c3a82015-07-23 14:26:04 -04001071void TParseContext::es3InvariantErrorCheck(const TQualifier qualifier,
1072 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001073{
1074 if (!sh::IsVaryingOut(qualifier) && qualifier != EvqFragmentOut)
1075 {
1076 error(invariantLocation, "Only out variables can be invariant.", "invariant");
Olli Etuaho37ad4742015-04-27 13:18:50 +03001077 }
1078}
1079
Arun Patole7e7e68d2015-05-22 12:02:25 +05301080bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001081{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001082 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001083 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1084 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001085}
1086
Arun Patole7e7e68d2015-05-22 12:02:25 +05301087bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001088{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001089 return ::IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001090}
1091
Jamie Madillb98c3a82015-07-23 14:26:04 -04001092void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1093 const char *extName,
1094 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001095{
1096 pp::SourceLocation srcLoc;
1097 srcLoc.file = loc.first_file;
1098 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001099 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001100}
1101
Jamie Madillb98c3a82015-07-23 14:26:04 -04001102void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1103 const char *name,
1104 const char *value,
1105 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001106{
1107 pp::SourceLocation srcLoc;
1108 srcLoc.file = loc.first_file;
1109 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001110 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001111}
1112
Martin Radev802abe02016-08-04 17:48:32 +03001113TLocalSize TParseContext::getComputeShaderLocalSize() const
1114{
1115 TLocalSize result;
1116 for (size_t i = 0u; i < result.size(); ++i)
1117 {
1118 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1119 {
1120 result[i] = 1;
1121 }
1122 else
1123 {
1124 result[i] = mComputeShaderLocalSize[i];
1125 }
1126 }
1127 return result;
1128}
1129
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001130/////////////////////////////////////////////////////////////////////////////////
1131//
1132// Non-Errors.
1133//
1134/////////////////////////////////////////////////////////////////////////////////
1135
Jamie Madill5c097022014-08-20 16:38:32 -04001136const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1137 const TString *name,
1138 const TSymbol *symbol)
1139{
1140 const TVariable *variable = NULL;
1141
1142 if (!symbol)
1143 {
1144 error(location, "undeclared identifier", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001145 }
1146 else if (!symbol->isVariable())
1147 {
1148 error(location, "variable expected", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001149 }
1150 else
1151 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001152 variable = static_cast<const TVariable *>(symbol);
Jamie Madill5c097022014-08-20 16:38:32 -04001153
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001154 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
Olli Etuaho383b7912016-08-05 11:22:59 +03001155 !variable->getExtension().empty())
Jamie Madill5c097022014-08-20 16:38:32 -04001156 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001157 extensionErrorCheck(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001158 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001159
1160 // Reject shaders using both gl_FragData and gl_FragColor
1161 TQualifier qualifier = variable->getType().getQualifier();
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001162 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001163 {
1164 mUsesFragData = true;
1165 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001166 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001167 {
1168 mUsesFragColor = true;
1169 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001170 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1171 {
1172 mUsesSecondaryOutputs = true;
1173 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001174
1175 // This validation is not quite correct - it's only an error to write to
1176 // both FragData and FragColor. For simplicity, and because users shouldn't
1177 // be rewarded for reading from undefined varaibles, return an error
1178 // if they are both referenced, rather than assigned.
1179 if (mUsesFragData && mUsesFragColor)
1180 {
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001181 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1182 if (mUsesSecondaryOutputs)
1183 {
1184 errorMessage =
1185 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1186 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1187 }
1188 error(location, errorMessage, name->c_str());
Jamie Madill14e95b32015-05-07 10:10:41 -04001189 }
Martin Radevb0883602016-08-04 17:48:58 +03001190
1191 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1192 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1193 qualifier == EvqWorkGroupSize)
1194 {
1195 error(location,
1196 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1197 "gl_WorkGroupSize");
1198 }
Jamie Madill5c097022014-08-20 16:38:32 -04001199 }
1200
1201 if (!variable)
1202 {
1203 TType type(EbtFloat, EbpUndefined);
1204 TVariable *fakeVariable = new TVariable(name, type);
1205 symbolTable.declare(fakeVariable);
1206 variable = fakeVariable;
1207 }
1208
1209 return variable;
1210}
1211
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001212TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1213 const TString *name,
1214 const TSymbol *symbol)
1215{
1216 const TVariable *variable = getNamedVariable(location, name, symbol);
1217
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001218 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001219 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001220 const TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001221 return intermediate.addConstantUnion(constArray, variable->getType(), location);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001222 }
1223 else
1224 {
1225 return intermediate.addSymbol(variable->getUniqueId(), variable->getName(),
1226 variable->getType(), location);
1227 }
1228}
1229
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001230//
1231// Look up a function name in the symbol table, and make sure it is a function.
1232//
1233// Return the function symbol if found, otherwise 0.
1234//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001235const TFunction *TParseContext::findFunction(const TSourceLoc &line,
1236 TFunction *call,
1237 int inputShaderVersion,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301238 bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001239{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001240 // First find by unmangled name to check whether the function name has been
1241 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001242 // If a function is found, check for one with a matching argument list.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301243 const TSymbol *symbol = symbolTable.find(call->getName(), inputShaderVersion, builtIn);
1244 if (symbol == 0 || symbol->isFunction())
1245 {
Austin Kinross3ae64652015-01-26 15:51:39 -08001246 symbol = symbolTable.find(call->getMangledName(), inputShaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001247 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001248
Arun Patole7e7e68d2015-05-22 12:02:25 +05301249 if (symbol == 0)
1250 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001251 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001252 return 0;
1253 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001254
Arun Patole7e7e68d2015-05-22 12:02:25 +05301255 if (!symbol->isFunction())
1256 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001257 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001258 return 0;
1259 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001260
Jamie Madillb98c3a82015-07-23 14:26:04 -04001261 return static_cast<const TFunction *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001262}
1263
1264//
1265// Initializers show up in several places in the grammar. Have one set of
1266// code to handle them here.
1267//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001268// Returns true on error, false if no error
1269//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001270bool TParseContext::executeInitializer(const TSourceLoc &line,
1271 const TString &identifier,
1272 const TPublicType &pType,
1273 TIntermTyped *initializer,
1274 TIntermNode **intermNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001275{
Olli Etuahoe7847b02015-03-16 11:56:12 +02001276 ASSERT(intermNode != nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001277 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001278
Olli Etuaho2935c582015-04-08 14:32:06 +03001279 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001280 if (type.isUnsizedArray())
1281 {
1282 type.setArraySize(initializer->getArraySize());
1283 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001284 if (!declareVariable(line, identifier, type, &variable))
1285 {
1286 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001287 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001288
Olli Etuahob0c645e2015-05-12 14:25:36 +03001289 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001290 if (symbolTable.atGlobalLevel() &&
1291 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001292 {
1293 // Error message does not completely match behavior with ESSL 1.00, but
1294 // we want to steer developers towards only using constant expressions.
1295 error(line, "global variable initializers must be constant expressions", "=");
1296 return true;
1297 }
1298 if (globalInitWarning)
1299 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001300 warning(
1301 line,
1302 "global variable initializers should be constant expressions "
1303 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1304 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001305 }
1306
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001307 //
1308 // identifier must be of type constant, a global, or a temporary
1309 //
1310 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301311 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1312 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001313 error(line, " cannot initialize this type of qualifier ",
1314 variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001315 return true;
1316 }
1317 //
1318 // test for and propagate constant
1319 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001320
Arun Patole7e7e68d2015-05-22 12:02:25 +05301321 if (qualifier == EvqConst)
1322 {
1323 if (qualifier != initializer->getType().getQualifier())
1324 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001325 std::stringstream extraInfoStream;
1326 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1327 std::string extraInfo = extraInfoStream.str();
1328 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001329 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001330 return true;
1331 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301332 if (type != initializer->getType())
1333 {
1334 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001335 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001336 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001337 return true;
1338 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001339
1340 // Save the constant folded value to the variable if possible. For example array
1341 // initializers are not folded, since that way copying the array literal to multiple places
1342 // in the shader is avoided.
1343 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1344 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301345 if (initializer->getAsConstantUnion())
1346 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001347 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001348 *intermNode = nullptr;
1349 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301350 }
1351 else if (initializer->getAsSymbolNode())
1352 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001353 const TSymbol *symbol =
1354 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1355 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001356
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001357 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001358 if (constArray)
1359 {
1360 variable->shareConstPointer(constArray);
1361 *intermNode = nullptr;
1362 return false;
1363 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001364 }
1365 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001366
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001367 TIntermSymbol *intermSymbol = intermediate.addSymbol(
1368 variable->getUniqueId(), variable->getName(), variable->getType(), line);
1369 *intermNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1370 if (*intermNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001371 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001372 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1373 return true;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001374 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001375
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001376 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001377}
1378
Jamie Madillb98c3a82015-07-23 14:26:04 -04001379TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier,
1380 bool invariant,
1381 TLayoutQualifier layoutQualifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301382 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001383{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001384 TPublicType returnType = typeSpecifier;
1385 returnType.qualifier = qualifier;
1386 returnType.invariant = invariant;
Jamie Madilla5efff92013-06-06 11:56:47 -04001387 returnType.layoutQualifier = layoutQualifier;
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001388
Olli Etuaho383b7912016-08-05 11:22:59 +03001389 layoutWorkGroupSizeErrorCheck(typeSpecifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03001390
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001391 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001392 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001393 if (typeSpecifier.array)
1394 {
1395 error(typeSpecifier.line, "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001396 returnType.clearArrayness();
1397 }
1398
Jamie Madillb98c3a82015-07-23 14:26:04 -04001399 if (qualifier == EvqAttribute &&
1400 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001401 {
1402 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001403 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001404
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001405 if ((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1406 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1407 {
1408 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001409 }
1410 }
1411 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001412 {
Olli Etuahoabb0c382015-07-13 12:01:12 +03001413 if (!layoutQualifier.isEmpty())
1414 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001415 globalErrorCheck(typeSpecifier.line, symbolTable.atGlobalLevel(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03001416 }
Olli Etuahocc36b982015-07-10 14:14:18 +03001417 if (sh::IsVarying(qualifier) || qualifier == EvqVertexIn || qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001418 {
Olli Etuahocc36b982015-07-10 14:14:18 +03001419 es3InputOutputTypeCheck(qualifier, typeSpecifier, typeSpecifier.line);
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001420 }
Martin Radev802abe02016-08-04 17:48:32 +03001421 if (qualifier == EvqComputeIn)
1422 {
1423 error(typeSpecifier.line, "'in' can be only used to specify the local group size",
1424 "in");
Martin Radev802abe02016-08-04 17:48:32 +03001425 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001426 }
1427
1428 return returnType;
1429}
1430
Olli Etuahocc36b982015-07-10 14:14:18 +03001431void TParseContext::es3InputOutputTypeCheck(const TQualifier qualifier,
1432 const TPublicType &type,
1433 const TSourceLoc &qualifierLocation)
1434{
1435 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
1436 if (type.type == EbtBool)
1437 {
1438 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001439 }
1440
1441 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
1442 switch (qualifier)
1443 {
1444 case EvqVertexIn:
1445 // ESSL 3.00 section 4.3.4
1446 if (type.array)
1447 {
1448 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001449 }
1450 // Vertex inputs with a struct type are disallowed in singleDeclarationErrorCheck
1451 return;
1452 case EvqFragmentOut:
1453 // ESSL 3.00 section 4.3.6
1454 if (type.isMatrix())
1455 {
1456 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001457 }
1458 // Fragment outputs with a struct type are disallowed in singleDeclarationErrorCheck
1459 return;
1460 default:
1461 break;
1462 }
1463
1464 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
1465 // restrictions.
1466 bool typeContainsIntegers =
1467 (type.type == EbtInt || type.type == EbtUInt || type.isStructureContainingType(EbtInt) ||
1468 type.isStructureContainingType(EbtUInt));
1469 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
1470 {
1471 error(qualifierLocation, "must use 'flat' interpolation here",
1472 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001473 }
1474
1475 if (type.type == EbtStruct)
1476 {
1477 // ESSL 3.00 sections 4.3.4 and 4.3.6.
1478 // These restrictions are only implied by the ESSL 3.00 spec, but
1479 // the ESSL 3.10 spec lists these restrictions explicitly.
1480 if (type.array)
1481 {
1482 error(qualifierLocation, "cannot be an array of structures",
1483 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001484 }
1485 if (type.isStructureContainingArrays())
1486 {
1487 error(qualifierLocation, "cannot be a structure containing an array",
1488 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001489 }
1490 if (type.isStructureContainingType(EbtStruct))
1491 {
1492 error(qualifierLocation, "cannot be a structure containing a structure",
1493 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001494 }
1495 if (type.isStructureContainingType(EbtBool))
1496 {
1497 error(qualifierLocation, "cannot be a structure containing a bool",
1498 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001499 }
1500 }
1501}
1502
Olli Etuahofa33d582015-04-09 14:33:12 +03001503TIntermAggregate *TParseContext::parseSingleDeclaration(TPublicType &publicType,
1504 const TSourceLoc &identifierOrTypeLocation,
1505 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001506{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001507 TIntermSymbol *symbol =
1508 intermediate.addSymbol(0, identifier, TType(publicType), identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001509
Olli Etuahobab4c082015-04-24 16:38:49 +03001510 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03001511
Olli Etuahobab4c082015-04-24 16:38:49 +03001512 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1513
1514 if (emptyDeclaration)
1515 {
1516 if (publicType.isUnsizedArray())
1517 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001518 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1519 // error. It is assumed that this applies to empty declarations as well.
1520 error(identifierOrTypeLocation, "empty array declaration needs to specify a size",
1521 identifier.c_str());
Olli Etuahobab4c082015-04-24 16:38:49 +03001522 }
1523 }
1524 else
Jamie Madill60ed9812013-06-06 11:56:46 -04001525 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001526 singleDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001527
Olli Etuaho383b7912016-08-05 11:22:59 +03001528 nonInitErrorCheck(identifierOrTypeLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001529
Olli Etuaho2935c582015-04-08 14:32:06 +03001530 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03001531 declareVariable(identifierOrTypeLocation, identifier, TType(publicType), &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04001532
1533 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001534 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001535 }
1536
Olli Etuahoe7847b02015-03-16 11:56:12 +02001537 return intermediate.makeAggregate(symbol, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001538}
1539
Olli Etuahoe7847b02015-03-16 11:56:12 +02001540TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1541 const TSourceLoc &identifierLocation,
1542 const TString &identifier,
1543 const TSourceLoc &indexLocation,
1544 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001545{
Olli Etuahofa33d582015-04-09 14:33:12 +03001546 mDeferredSingleDeclarationErrorCheck = false;
1547
Olli Etuaho383b7912016-08-05 11:22:59 +03001548 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001549
Olli Etuaho383b7912016-08-05 11:22:59 +03001550 nonInitErrorCheck(identifierLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001551
Olli Etuaho383b7912016-08-05 11:22:59 +03001552 if (!arrayTypeErrorCheck(indexLocation, publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001553 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001554 arrayQualifierErrorCheck(indexLocation, publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001555 }
1556
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001557 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001558
1559 int size;
Olli Etuaho383b7912016-08-05 11:22:59 +03001560 arraySizeErrorCheck(identifierLocation, indexExpression, size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001561 // Make the type an array even if size check failed.
1562 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1563 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001564
Olli Etuaho2935c582015-04-08 14:32:06 +03001565 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03001566 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04001567
Olli Etuahoe7847b02015-03-16 11:56:12 +02001568 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001569 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001570 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001571
Olli Etuahoe7847b02015-03-16 11:56:12 +02001572 return intermediate.makeAggregate(symbol, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001573}
1574
Jamie Madill06145232015-05-13 13:10:01 -04001575TIntermAggregate *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001576 const TSourceLoc &identifierLocation,
1577 const TString &identifier,
1578 const TSourceLoc &initLocation,
1579 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001580{
Olli Etuahofa33d582015-04-09 14:33:12 +03001581 mDeferredSingleDeclarationErrorCheck = false;
1582
Olli Etuaho383b7912016-08-05 11:22:59 +03001583 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001584
Olli Etuahoe7847b02015-03-16 11:56:12 +02001585 TIntermNode *intermNode = nullptr;
1586 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04001587 {
1588 //
1589 // Build intermediate representation
1590 //
Olli Etuahoe7847b02015-03-16 11:56:12 +02001591 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001592 }
1593 else
1594 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001595 return nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001596 }
1597}
1598
Jamie Madillb98c3a82015-07-23 14:26:04 -04001599TIntermAggregate *TParseContext::parseSingleArrayInitDeclaration(
1600 TPublicType &publicType,
1601 const TSourceLoc &identifierLocation,
1602 const TString &identifier,
1603 const TSourceLoc &indexLocation,
1604 TIntermTyped *indexExpression,
1605 const TSourceLoc &initLocation,
1606 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001607{
1608 mDeferredSingleDeclarationErrorCheck = false;
1609
Olli Etuaho383b7912016-08-05 11:22:59 +03001610 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001611
Olli Etuaho383b7912016-08-05 11:22:59 +03001612 if (!arrayTypeErrorCheck(indexLocation, publicType))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001613 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001614 arrayQualifierErrorCheck(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001615 }
1616
1617 TPublicType arrayType(publicType);
1618
Olli Etuaho376f1b52015-04-13 13:23:41 +03001619 int size = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001620 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
1621 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03001622 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001623 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001624 arraySizeErrorCheck(identifierLocation, indexExpression, size);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001625 }
1626 // Make the type an array even if size check failed.
1627 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1628 arrayType.setArraySize(size);
1629
1630 // initNode will correspond to the whole of "type b[n] = initializer".
1631 TIntermNode *initNode = nullptr;
1632 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1633 {
1634 return initNode ? intermediate.makeAggregate(initNode, initLocation) : nullptr;
1635 }
1636 else
1637 {
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001638 return nullptr;
1639 }
1640}
1641
Olli Etuahoe7847b02015-03-16 11:56:12 +02001642TIntermAggregate *TParseContext::parseInvariantDeclaration(const TSourceLoc &invariantLoc,
Jamie Madill47e3ec02014-08-20 16:38:33 -04001643 const TSourceLoc &identifierLoc,
1644 const TString *identifier,
1645 const TSymbol *symbol)
1646{
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001647 // invariant declaration
Olli Etuaho383b7912016-08-05 11:22:59 +03001648 globalErrorCheck(invariantLoc, symbolTable.atGlobalLevel(), "invariant varying");
Jamie Madill47e3ec02014-08-20 16:38:33 -04001649
1650 if (!symbol)
1651 {
1652 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001653 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001654 }
1655 else
1656 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001657 const TString kGlFrontFacing("gl_FrontFacing");
1658 if (*identifier == kGlFrontFacing)
1659 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001660 error(identifierLoc, "identifier should not be declared as invariant",
1661 identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001662 return nullptr;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001663 }
Jamie Madill2c433252014-12-03 12:36:54 -05001664 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001665 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1666 ASSERT(variable);
1667 const TType &type = variable->getType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04001668 TIntermSymbol *intermSymbol =
1669 intermediate.addSymbol(variable->getUniqueId(), *identifier, type, identifierLoc);
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001670
1671 TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc);
1672 aggregate->setOp(EOpInvariantDeclaration);
1673 return aggregate;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001674 }
1675}
1676
Jamie Madillb98c3a82015-07-23 14:26:04 -04001677TIntermAggregate *TParseContext::parseDeclarator(TPublicType &publicType,
1678 TIntermAggregate *aggregateDeclaration,
1679 const TSourceLoc &identifierLocation,
1680 const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001681{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001682 // If the declaration starting this declarator list was empty (example: int,), some checks were
1683 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001684 if (mDeferredSingleDeclarationErrorCheck)
1685 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001686 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03001687 mDeferredSingleDeclarationErrorCheck = false;
1688 }
1689
Olli Etuaho383b7912016-08-05 11:22:59 +03001690 locationDeclaratorListCheck(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04001691
Olli Etuaho383b7912016-08-05 11:22:59 +03001692 nonInitErrorCheck(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04001693
Olli Etuaho2935c582015-04-08 14:32:06 +03001694 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03001695 declareVariable(identifierLocation, identifier, TType(publicType), &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001696
Jamie Madillb98c3a82015-07-23 14:26:04 -04001697 TIntermSymbol *symbol =
1698 intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001699 if (variable && symbol)
Jamie Madill502d66f2013-06-20 11:55:52 -04001700 symbol->setId(variable->getUniqueId());
1701
Olli Etuahoe7847b02015-03-16 11:56:12 +02001702 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001703}
1704
Jamie Madillb98c3a82015-07-23 14:26:04 -04001705TIntermAggregate *TParseContext::parseArrayDeclarator(TPublicType &publicType,
1706 TIntermAggregate *aggregateDeclaration,
1707 const TSourceLoc &identifierLocation,
1708 const TString &identifier,
1709 const TSourceLoc &arrayLocation,
1710 TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001711{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001712 // If the declaration starting this declarator list was empty (example: int,), some checks were
1713 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001714 if (mDeferredSingleDeclarationErrorCheck)
1715 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001716 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03001717 mDeferredSingleDeclarationErrorCheck = false;
1718 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001719
Olli Etuaho383b7912016-08-05 11:22:59 +03001720 locationDeclaratorListCheck(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04001721
Olli Etuaho383b7912016-08-05 11:22:59 +03001722 nonInitErrorCheck(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04001723
Olli Etuaho383b7912016-08-05 11:22:59 +03001724 if (!arrayTypeErrorCheck(arrayLocation, publicType) &&
1725 !arrayQualifierErrorCheck(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001726 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001727 TType arrayType = TType(publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04001728 int size;
Olli Etuaho383b7912016-08-05 11:22:59 +03001729 arraySizeErrorCheck(arrayLocation, indexExpression, size);
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001730 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001731
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001732 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03001733 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04001734
Jamie Madillb98c3a82015-07-23 14:26:04 -04001735 TIntermSymbol *symbol =
1736 intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001737 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001738 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001739
1740 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001741 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001742
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001743 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001744}
1745
Jamie Madillb98c3a82015-07-23 14:26:04 -04001746TIntermAggregate *TParseContext::parseInitDeclarator(const TPublicType &publicType,
1747 TIntermAggregate *aggregateDeclaration,
1748 const TSourceLoc &identifierLocation,
1749 const TString &identifier,
1750 const TSourceLoc &initLocation,
1751 TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001752{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001753 // If the declaration starting this declarator list was empty (example: int,), some checks were
1754 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001755 if (mDeferredSingleDeclarationErrorCheck)
1756 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001757 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03001758 mDeferredSingleDeclarationErrorCheck = false;
1759 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001760
Olli Etuaho383b7912016-08-05 11:22:59 +03001761 locationDeclaratorListCheck(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04001762
Olli Etuahoe7847b02015-03-16 11:56:12 +02001763 TIntermNode *intermNode = nullptr;
1764 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04001765 {
1766 //
1767 // build the intermediate representation
1768 //
1769 if (intermNode)
1770 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001771 return intermediate.growAggregate(aggregateDeclaration, intermNode, initLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001772 }
1773 else
1774 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001775 return aggregateDeclaration;
Jamie Madill502d66f2013-06-20 11:55:52 -04001776 }
1777 }
1778 else
1779 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001780 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001781 }
1782}
1783
Jamie Madill06145232015-05-13 13:10:01 -04001784TIntermAggregate *TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001785 TIntermAggregate *aggregateDeclaration,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301786 const TSourceLoc &identifierLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001787 const TString &identifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301788 const TSourceLoc &indexLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001789 TIntermTyped *indexExpression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001790 const TSourceLoc &initLocation,
1791 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001792{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001793 // If the declaration starting this declarator list was empty (example: int,), some checks were
1794 // not performed.
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001795 if (mDeferredSingleDeclarationErrorCheck)
1796 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001797 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001798 mDeferredSingleDeclarationErrorCheck = false;
1799 }
1800
Olli Etuaho383b7912016-08-05 11:22:59 +03001801 locationDeclaratorListCheck(identifierLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001802
Olli Etuaho383b7912016-08-05 11:22:59 +03001803 if (!arrayTypeErrorCheck(indexLocation, publicType))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001804 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001805 arrayQualifierErrorCheck(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001806 }
1807
1808 TPublicType arrayType(publicType);
1809
Olli Etuaho376f1b52015-04-13 13:23:41 +03001810 int size = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001811 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
1812 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03001813 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001814 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001815 arraySizeErrorCheck(identifierLocation, indexExpression, size);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001816 }
1817 // Make the type an array even if size check failed.
1818 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1819 arrayType.setArraySize(size);
1820
1821 // initNode will correspond to the whole of "b[n] = initializer".
1822 TIntermNode *initNode = nullptr;
1823 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1824 {
1825 if (initNode)
1826 {
1827 return intermediate.growAggregate(aggregateDeclaration, initNode, initLocation);
1828 }
1829 else
1830 {
1831 return aggregateDeclaration;
1832 }
1833 }
1834 else
1835 {
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001836 return nullptr;
1837 }
1838}
1839
Jamie Madilla295edf2013-06-06 11:56:48 -04001840void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1841{
Jamie Madilla295edf2013-06-06 11:56:48 -04001842 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04001843
1844 // It should never be the case, but some strange parser errors can send us here.
1845 if (layoutQualifier.isEmpty())
1846 {
1847 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04001848 return;
1849 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001850
Martin Radev802abe02016-08-04 17:48:32 +03001851 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04001852 {
Martin Radev802abe02016-08-04 17:48:32 +03001853 error(typeQualifier.line, "invalid combination:", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04001854 return;
1855 }
1856
Martin Radev802abe02016-08-04 17:48:32 +03001857 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04001858 {
Martin Radev802abe02016-08-04 17:48:32 +03001859 if (mComputeShaderLocalSizeDeclared &&
1860 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
1861 {
1862 error(typeQualifier.line, "Work group size does not match the previous declaration",
1863 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03001864 return;
1865 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001866
Martin Radev802abe02016-08-04 17:48:32 +03001867 if (mShaderVersion < 310)
1868 {
1869 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03001870 return;
1871 }
Jamie Madill099c0f32013-06-20 11:55:52 -04001872
Martin Radev802abe02016-08-04 17:48:32 +03001873 if (!layoutQualifier.isGroupSizeSpecified())
1874 {
1875 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03001876 return;
1877 }
1878
1879 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
1880 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
1881
1882 const TConstantUnion *maxComputeWorkGroupSizeData =
1883 maxComputeWorkGroupSize->getConstPointer();
1884
1885 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
1886 {
1887 if (layoutQualifier.localSize[i] != -1)
1888 {
1889 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
1890 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
1891 if (mComputeShaderLocalSize[i] < 1 ||
1892 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
1893 {
1894 std::stringstream errorMessageStream;
1895 errorMessageStream << "Value must be at least 1 and no greater than "
1896 << maxComputeWorkGroupSizeValue;
1897 const std::string &errorMessage = errorMessageStream.str();
1898
1899 error(typeQualifier.line, "invalid value:", getLocalSizeString(i),
1900 errorMessage.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001901 return;
1902 }
1903 }
1904 }
1905
1906 mComputeShaderLocalSizeDeclared = true;
1907 }
1908 else
Jamie Madill1566ef72013-06-20 11:55:54 -04001909 {
Martin Radev802abe02016-08-04 17:48:32 +03001910
1911 if (layoutWorkGroupSizeErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1912 {
Martin Radev802abe02016-08-04 17:48:32 +03001913 return;
1914 }
1915
1916 if (typeQualifier.qualifier != EvqUniform)
1917 {
1918 error(typeQualifier.line, "invalid qualifier:",
1919 getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
Martin Radev802abe02016-08-04 17:48:32 +03001920 return;
1921 }
1922
1923 if (mShaderVersion < 300)
1924 {
1925 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
1926 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03001927 return;
1928 }
1929
Olli Etuaho383b7912016-08-05 11:22:59 +03001930 layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03001931
1932 if (layoutQualifier.matrixPacking != EmpUnspecified)
1933 {
1934 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
1935 }
1936
1937 if (layoutQualifier.blockStorage != EbsUnspecified)
1938 {
1939 mDefaultBlockStorage = layoutQualifier.blockStorage;
1940 }
Jamie Madill1566ef72013-06-20 11:55:54 -04001941 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001942}
1943
Olli Etuahoee63f5d2016-01-04 11:34:54 +02001944TIntermAggregate *TParseContext::addFunctionPrototypeDeclaration(const TFunction &function,
1945 const TSourceLoc &location)
1946{
Olli Etuaho5d653182016-01-04 14:43:28 +02001947 // Note: symbolTableFunction could be the same as function if this is the first declaration.
1948 // Either way the instance in the symbol table is used to track whether the function is declared
1949 // multiple times.
1950 TFunction *symbolTableFunction =
1951 static_cast<TFunction *>(symbolTable.find(function.getMangledName(), getShaderVersion()));
1952 if (symbolTableFunction->hasPrototypeDeclaration() && mShaderVersion == 100)
1953 {
1954 // ESSL 1.00.17 section 4.2.7.
1955 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
1956 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02001957 }
1958 symbolTableFunction->setHasPrototypeDeclaration();
1959
Olli Etuahoee63f5d2016-01-04 11:34:54 +02001960 TIntermAggregate *prototype = new TIntermAggregate;
1961 prototype->setType(function.getReturnType());
1962 prototype->setName(function.getMangledName());
1963 prototype->setFunctionId(function.getUniqueId());
1964
1965 for (size_t i = 0; i < function.getParamCount(); i++)
1966 {
1967 const TConstParameter &param = function.getParam(i);
1968 if (param.name != 0)
1969 {
1970 TVariable variable(param.name, *param.type);
1971
1972 TIntermSymbol *paramSymbol = intermediate.addSymbol(
1973 variable.getUniqueId(), variable.getName(), variable.getType(), location);
1974 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
1975 }
1976 else
1977 {
1978 TIntermSymbol *paramSymbol = intermediate.addSymbol(0, "", *param.type, location);
1979 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
1980 }
1981 }
1982
1983 prototype->setOp(EOpPrototype);
1984
1985 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02001986
1987 if (!symbolTable.atGlobalLevel())
1988 {
1989 // ESSL 3.00.4 section 4.2.4.
1990 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02001991 }
1992
Olli Etuahoee63f5d2016-01-04 11:34:54 +02001993 return prototype;
1994}
1995
1996TIntermAggregate *TParseContext::addFunctionDefinition(const TFunction &function,
1997 TIntermAggregate *functionPrototype,
1998 TIntermAggregate *functionBody,
1999 const TSourceLoc &location)
2000{
2001 //?? Check that all paths return a value if return type != void ?
2002 // May be best done as post process phase on intermediate code
2003 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
2004 {
2005 error(location, "function does not return a value:", "", function.getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002006 }
2007
2008 TIntermAggregate *aggregate =
2009 intermediate.growAggregate(functionPrototype, functionBody, location);
2010 intermediate.setAggregateOperator(aggregate, EOpFunction, location);
2011 aggregate->setName(function.getMangledName().c_str());
2012 aggregate->setType(function.getReturnType());
2013 aggregate->setFunctionId(function.getUniqueId());
2014
2015 symbolTable.pop();
2016 return aggregate;
2017}
2018
Jamie Madill185fb402015-06-12 15:48:48 -04002019void TParseContext::parseFunctionPrototype(const TSourceLoc &location,
2020 TFunction *function,
2021 TIntermAggregate **aggregateOut)
2022{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002023 const TSymbol *builtIn =
2024 symbolTable.findBuiltIn(function->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002025
2026 if (builtIn)
2027 {
2028 error(location, "built-in functions cannot be redefined", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002029 }
2030
Jamie Madillb98c3a82015-07-23 14:26:04 -04002031 TFunction *prevDec =
2032 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Jamie Madill185fb402015-06-12 15:48:48 -04002033 //
2034 // Note: 'prevDec' could be 'function' if this is the first time we've seen function
2035 // as it would have just been put in the symbol table. Otherwise, we're looking up
2036 // an earlier occurance.
2037 //
2038 if (prevDec->isDefined())
2039 {
2040 // Then this function already has a body.
2041 error(location, "function already has a body", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002042 }
2043 prevDec->setDefined();
2044 //
2045 // Overload the unique ID of the definition to be the same unique ID as the declaration.
2046 // Eventually we will probably want to have only a single definition and just swap the
2047 // arguments to be the definition's arguments.
2048 //
2049 function->setUniqueId(prevDec->getUniqueId());
2050
2051 // Raise error message if main function takes any parameters or return anything other than void
2052 if (function->getName() == "main")
2053 {
2054 if (function->getParamCount() > 0)
2055 {
2056 error(location, "function cannot take any parameter(s)", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002057 }
2058 if (function->getReturnType().getBasicType() != EbtVoid)
2059 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002060 error(location, "", function->getReturnType().getBasicString(),
2061 "main function cannot return a value");
Jamie Madill185fb402015-06-12 15:48:48 -04002062 }
2063 }
2064
2065 //
2066 // Remember the return type for later checking for RETURN statements.
2067 //
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002068 mCurrentFunctionType = &(prevDec->getReturnType());
2069 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04002070
2071 //
2072 // Insert parameters into the symbol table.
2073 // If the parameter has no name, it's not an error, just don't insert it
2074 // (could be used for unused args).
2075 //
2076 // Also, accumulate the list of parameters into the HIL, so lower level code
2077 // knows where to find parameters.
2078 //
2079 TIntermAggregate *paramNodes = new TIntermAggregate;
2080 for (size_t i = 0; i < function->getParamCount(); i++)
2081 {
2082 const TConstParameter &param = function->getParam(i);
2083 if (param.name != 0)
2084 {
2085 TVariable *variable = new TVariable(param.name, *param.type);
2086 //
2087 // Insert the parameters with name in the symbol table.
2088 //
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002089 if (!symbolTable.declare(variable))
2090 {
Jamie Madill185fb402015-06-12 15:48:48 -04002091 error(location, "redefinition", variable->getName().c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002092 paramNodes = intermediate.growAggregate(
2093 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
2094 continue;
Jamie Madill185fb402015-06-12 15:48:48 -04002095 }
2096
2097 //
2098 // Add the parameter to the HIL
2099 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002100 TIntermSymbol *symbol = intermediate.addSymbol(
2101 variable->getUniqueId(), variable->getName(), variable->getType(), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002102
2103 paramNodes = intermediate.growAggregate(paramNodes, symbol, location);
2104 }
2105 else
2106 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002107 paramNodes = intermediate.growAggregate(
2108 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002109 }
2110 }
2111 intermediate.setAggregateOperator(paramNodes, EOpParameters, location);
2112 *aggregateOut = paramNodes;
2113 setLoopNestingLevel(0);
2114}
2115
Jamie Madillb98c3a82015-07-23 14:26:04 -04002116TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002117{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002118 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002119 // We don't know at this point whether this is a function definition or a prototype.
2120 // The definition production code will check for redefinitions.
2121 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002122 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002123 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
2124 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002125 //
2126 TFunction *prevDec =
2127 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302128
2129 if (getShaderVersion() >= 300 && symbolTable.hasUnmangledBuiltIn(function->getName().c_str()))
2130 {
2131 // With ESSL 3.00, names of built-in functions cannot be redeclared as functions.
2132 // Therefore overloading or redefining builtin functions is an error.
2133 error(location, "Name of a built-in function cannot be redeclared as function",
2134 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302135 }
2136 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002137 {
2138 if (prevDec->getReturnType() != function->getReturnType())
2139 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002140 error(location, "overloaded functions must have the same return type",
Jamie Madill185fb402015-06-12 15:48:48 -04002141 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04002142 }
2143 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2144 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002145 if (prevDec->getParam(i).type->getQualifier() !=
2146 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002147 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002148 error(location, "overloaded functions must have the same parameter qualifiers",
Jamie Madill185fb402015-06-12 15:48:48 -04002149 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04002150 }
2151 }
2152 }
2153
2154 //
2155 // Check for previously declared variables using the same name.
2156 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002157 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002158 if (prevSym)
2159 {
2160 if (!prevSym->isFunction())
2161 {
2162 error(location, "redefinition", function->getName().c_str(), "function");
Jamie Madill185fb402015-06-12 15:48:48 -04002163 }
2164 }
2165 else
2166 {
2167 // Insert the unmangled name to detect potential future redefinition as a variable.
Jamie Madillb98c3a82015-07-23 14:26:04 -04002168 TFunction *newFunction =
2169 new TFunction(NewPoolTString(function->getName().c_str()), &function->getReturnType());
Jamie Madill185fb402015-06-12 15:48:48 -04002170 symbolTable.getOuterLevel()->insertUnmangled(newFunction);
2171 }
2172
2173 // We're at the inner scope level of the function's arguments and body statement.
2174 // Add the function prototype to the surrounding scope instead.
2175 symbolTable.getOuterLevel()->insert(function);
2176
2177 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002178 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2179 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002180 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2181 //
2182 return function;
2183}
2184
Olli Etuaho9de84a52016-06-14 17:36:01 +03002185TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
2186 const TString *name,
2187 const TSourceLoc &location)
2188{
2189 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
2190 {
2191 error(location, "no qualifiers allowed for function return",
2192 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03002193 }
2194 if (!type.layoutQualifier.isEmpty())
2195 {
2196 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03002197 }
2198 // make sure a sampler is not involved as well...
Olli Etuaho383b7912016-08-05 11:22:59 +03002199 samplerErrorCheck(location, type, "samplers can't be function return values");
Olli Etuahoe29324f2016-06-15 10:58:03 +03002200 if (mShaderVersion < 300)
2201 {
2202 // Array return values are forbidden, but there's also no valid syntax for declaring array
2203 // return values in ESSL 1.00.
2204 ASSERT(type.arraySize == 0 || mDiagnostics.numErrors() > 0);
2205
2206 if (type.isStructureContainingArrays())
2207 {
2208 // ESSL 1.00.17 section 6.1 Function Definitions
2209 error(location, "structures containing arrays can't be function return values",
2210 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03002211 }
2212 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03002213
2214 // Add the function as a prototype after parsing it (we do not support recursion)
2215 return new TFunction(name, new TType(type));
2216}
2217
Jamie Madill06145232015-05-13 13:10:01 -04002218TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002219{
Jamie Madill06145232015-05-13 13:10:01 -04002220 TPublicType publicType = publicTypeIn;
Olli Etuahobd163f62015-11-13 12:15:38 +02002221 if (publicType.isStructSpecifier)
2222 {
2223 error(publicType.line, "constructor can't be a structure definition",
2224 getBasicString(publicType.type));
Olli Etuahobd163f62015-11-13 12:15:38 +02002225 }
2226
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002227 TOperator op = EOpNull;
2228 if (publicType.userDef)
2229 {
2230 op = EOpConstructStruct;
2231 }
2232 else
2233 {
Geoff Lang156d7192016-07-21 16:11:00 -04002234 op = sh::TypeToConstructorOperator(TType(publicType));
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002235 if (op == EOpNull)
2236 {
2237 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002238 publicType.type = EbtFloat;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002239 op = EOpConstructFloat;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002240 }
2241 }
2242
2243 TString tempString;
Dmitry Skiba7f17a502015-06-22 15:08:39 -07002244 const TType *type = new TType(publicType);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002245 return new TFunction(&tempString, type, op);
2246}
2247
Jamie Madillb98c3a82015-07-23 14:26:04 -04002248// This function is used to test for the correctness of the parameters passed to various constructor
2249// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002250//
2251// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
2252//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002253TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments,
2254 TType *type,
2255 TOperator op,
2256 TFunction *fnCall,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302257 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002258{
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002259 TIntermAggregate *constructor = arguments->getAsAggregate();
2260 ASSERT(constructor != nullptr);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002261
Olli Etuahof40319e2015-03-10 14:33:00 +02002262 if (type->isArray())
2263 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002264 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
2265 // the array.
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002266 TIntermSequence *args = constructor->getSequence();
Olli Etuahof40319e2015-03-10 14:33:00 +02002267 for (size_t i = 0; i < args->size(); i++)
2268 {
2269 const TType &argType = (*args)[i]->getAsTyped()->getType();
2270 // It has already been checked that the argument is not an array.
2271 ASSERT(!argType.isArray());
2272 if (!argType.sameElementType(*type))
2273 {
2274 error(line, "Array constructor argument has an incorrect type", "Error");
Olli Etuahof40319e2015-03-10 14:33:00 +02002275 return nullptr;
2276 }
2277 }
2278 }
2279 else if (op == EOpConstructStruct)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002280 {
2281 const TFieldList &fields = type->getStruct()->fields();
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002282 TIntermSequence *args = constructor->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002283
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002284 for (size_t i = 0; i < fields.size(); i++)
2285 {
Nicolas Capensffd73872014-08-21 13:49:16 -04002286 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002287 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002288 error(line, "Structure constructor arguments do not match structure fields",
2289 "Error");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002290
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002291 return 0;
2292 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002293 }
2294 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002295
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002296 // Turn the argument list itself into a constructor
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002297 constructor->setOp(op);
2298 constructor->setLine(line);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002299 ASSERT(constructor->isConstructor());
2300
2301 // Need to set type before setPrecisionFromChildren() because bool doesn't have precision.
2302 constructor->setType(*type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002303
Olli Etuaho21203702014-11-13 16:16:21 +02002304 // Structs should not be precision qualified, the individual members may be.
2305 // Built-in types on the other hand should be precision qualified.
2306 if (op != EOpConstructStruct)
2307 {
2308 constructor->setPrecisionFromChildren();
2309 type->setPrecision(constructor->getPrecision());
2310 }
2311
Olli Etuaho1d122782015-11-06 15:35:17 +02002312 TIntermTyped *constConstructor = intermediate.foldAggregateBuiltIn(constructor);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002313 if (constConstructor)
2314 {
2315 return constConstructor;
2316 }
2317
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002318 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002319}
2320
Olli Etuaho90892fb2016-07-14 14:44:51 +03002321// This function returns vector field(s) being accessed from a constant vector.
2322TIntermConstantUnion *TParseContext::foldVectorSwizzle(TVectorFields &fields,
2323 TIntermConstantUnion *baseNode,
2324 const TSourceLoc &location)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002325{
Olli Etuaho90892fb2016-07-14 14:44:51 +03002326 const TConstantUnion *unionArray = baseNode->getUnionArrayPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002327 ASSERT(unionArray);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002328
Arun Patole7e7e68d2015-05-22 12:02:25 +05302329 TConstantUnion *constArray = new TConstantUnion[fields.num];
Olli Etuaho90892fb2016-07-14 14:44:51 +03002330 const auto &type = baseNode->getType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002331
Arun Patole7e7e68d2015-05-22 12:02:25 +05302332 for (int i = 0; i < fields.num; i++)
2333 {
Olli Etuaho90892fb2016-07-14 14:44:51 +03002334 // Out-of-range indices should already be checked.
2335 ASSERT(fields.offsets[i] < type.getNominalSize());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002336 constArray[i] = unionArray[fields.offsets[i]];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302337 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03002338 return intermediate.addConstantUnion(constArray, type, location);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002339}
2340
Olli Etuaho90892fb2016-07-14 14:44:51 +03002341// This function returns the column vector being accessed from a constant matrix.
2342TIntermConstantUnion *TParseContext::foldMatrixSubscript(int index,
2343 TIntermConstantUnion *baseNode,
2344 const TSourceLoc &location)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002345{
Olli Etuaho90892fb2016-07-14 14:44:51 +03002346 ASSERT(index < baseNode->getType().getCols());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002347
Olli Etuaho90892fb2016-07-14 14:44:51 +03002348 const TConstantUnion *unionArray = baseNode->getUnionArrayPointer();
2349 int size = baseNode->getType().getRows();
2350 return intermediate.addConstantUnion(&unionArray[size * index], baseNode->getType(), location);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002351}
2352
Olli Etuaho90892fb2016-07-14 14:44:51 +03002353// This function returns an element of an array accessed from a constant array.
2354TIntermConstantUnion *TParseContext::foldArraySubscript(int index,
2355 TIntermConstantUnion *baseNode,
2356 const TSourceLoc &location)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002357{
Olli Etuaho90892fb2016-07-14 14:44:51 +03002358 ASSERT(index < baseNode->getArraySize());
2359
2360 TType arrayElementType = baseNode->getType();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002361 arrayElementType.clearArrayness();
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002362 size_t arrayElementSize = arrayElementType.getObjectSize();
Olli Etuaho90892fb2016-07-14 14:44:51 +03002363 const TConstantUnion *unionArray = baseNode->getUnionArrayPointer();
2364 return intermediate.addConstantUnion(&unionArray[arrayElementSize * index], baseNode->getType(),
2365 location);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002366}
2367
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002368//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002369// This function returns the value of a particular field inside a constant structure from the symbol
2370// table.
2371// If there is an embedded/nested struct, it appropriately calls addConstStructNested or
2372// addConstStructFromAggr function and returns the parse-tree with the values of the embedded/nested
2373// struct.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002374//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002375TIntermTyped *TParseContext::addConstStruct(const TString &identifier,
2376 TIntermTyped *node,
2377 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002378{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302379 const TFieldList &fields = node->getType().getStruct()->fields();
Jamie Madillb98c3a82015-07-23 14:26:04 -04002380 size_t instanceSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002381
Arun Patole7e7e68d2015-05-22 12:02:25 +05302382 for (size_t index = 0; index < fields.size(); ++index)
2383 {
2384 if (fields[index]->name() == identifier)
2385 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002386 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302387 }
2388 else
2389 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002390 instanceSize += fields[index]->type()->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002391 }
2392 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002393
Jamie Madill94bf7f22013-07-08 13:31:15 -04002394 TIntermTyped *typedNode;
2395 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
Arun Patole7e7e68d2015-05-22 12:02:25 +05302396 if (tempConstantNode)
2397 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002398 const TConstantUnion *constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002399
Jamie Madillb98c3a82015-07-23 14:26:04 -04002400 // type will be changed in the calling function
2401 typedNode = intermediate.addConstantUnion(constArray + instanceSize,
2402 tempConstantNode->getType(), line);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302403 }
2404 else
2405 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002406 error(line, "Cannot offset into the structure", "Error");
Olli Etuaho383b7912016-08-05 11:22:59 +03002407 return nullptr;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002408 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002409
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002410 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002411}
2412
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002413//
2414// Interface/uniform blocks
2415//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002416TIntermAggregate *TParseContext::addInterfaceBlock(const TPublicType &typeQualifier,
2417 const TSourceLoc &nameLine,
2418 const TString &blockName,
2419 TFieldList *fieldList,
2420 const TString *instanceName,
2421 const TSourceLoc &instanceLine,
2422 TIntermTyped *arrayIndex,
2423 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002424{
Olli Etuaho383b7912016-08-05 11:22:59 +03002425 reservedErrorCheck(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002426
2427 if (typeQualifier.qualifier != EvqUniform)
2428 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302429 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
2430 "interface blocks must be uniform");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002431 }
2432
Jamie Madill099c0f32013-06-20 11:55:52 -04002433 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho383b7912016-08-05 11:22:59 +03002434 layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002435
Jamie Madill099c0f32013-06-20 11:55:52 -04002436 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2437 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002438 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002439 }
2440
Jamie Madill1566ef72013-06-20 11:55:54 -04002441 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2442 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002443 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002444 }
2445
Olli Etuaho383b7912016-08-05 11:22:59 +03002446 layoutWorkGroupSizeErrorCheck(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002447
Arun Patole7e7e68d2015-05-22 12:02:25 +05302448 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2449 if (!symbolTable.declare(blockNameSymbol))
2450 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002451 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002452 }
2453
Jamie Madill98493dd2013-07-08 14:39:03 -04002454 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302455 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2456 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002457 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302458 TType *fieldType = field->type();
2459 if (IsSampler(fieldType->getBasicType()))
2460 {
2461 error(field->line(), "unsupported type", fieldType->getBasicString(),
2462 "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002463 }
2464
Jamie Madill98493dd2013-07-08 14:39:03 -04002465 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002466 switch (qualifier)
2467 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002468 case EvqGlobal:
2469 case EvqUniform:
2470 break;
2471 default:
2472 error(field->line(), "invalid qualifier on interface block member",
2473 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04002474 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002475 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002476
2477 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002478 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho383b7912016-08-05 11:22:59 +03002479 layoutLocationErrorCheck(field->line(), fieldLayoutQualifier);
Jamie Madill099c0f32013-06-20 11:55:52 -04002480
Jamie Madill98493dd2013-07-08 14:39:03 -04002481 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002482 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002483 error(field->line(), "invalid layout qualifier:",
2484 getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04002485 }
2486
Jamie Madill98493dd2013-07-08 14:39:03 -04002487 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002488 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002489 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002490 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002491 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002492 {
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002493 warning(field->line(), "extraneous layout qualifier:",
Jamie Madillb98c3a82015-07-23 14:26:04 -04002494 getMatrixPackingString(fieldLayoutQualifier.matrixPacking),
2495 "only has an effect on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04002496 }
2497
Jamie Madill98493dd2013-07-08 14:39:03 -04002498 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002499 }
2500
Jamie Madill98493dd2013-07-08 14:39:03 -04002501 // add array index
2502 int arraySize = 0;
2503 if (arrayIndex != NULL)
2504 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002505 arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize);
Jamie Madill98493dd2013-07-08 14:39:03 -04002506 }
2507
Jamie Madillb98c3a82015-07-23 14:26:04 -04002508 TInterfaceBlock *interfaceBlock =
2509 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2510 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
2511 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002512
2513 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04002514 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002515
Jamie Madill98493dd2013-07-08 14:39:03 -04002516 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002517 {
2518 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002519 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2520 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002521 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302522 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002523
2524 // set parent pointer of the field variable
2525 fieldType->setInterfaceBlock(interfaceBlock);
2526
Arun Patole7e7e68d2015-05-22 12:02:25 +05302527 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002528 fieldVariable->setQualifier(typeQualifier.qualifier);
2529
Arun Patole7e7e68d2015-05-22 12:02:25 +05302530 if (!symbolTable.declare(fieldVariable))
2531 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002532 error(field->line(), "redefinition", field->name().c_str(),
2533 "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002534 }
2535 }
2536 }
2537 else
2538 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002539 reservedErrorCheck(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03002540
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002541 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302542 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002543 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002544
Arun Patole7e7e68d2015-05-22 12:02:25 +05302545 if (!symbolTable.declare(instanceTypeDef))
2546 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002547 error(instanceLine, "redefinition", instanceName->c_str(),
2548 "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002549 }
2550
Jamie Madillb98c3a82015-07-23 14:26:04 -04002551 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002552 symbolName = instanceTypeDef->getName();
2553 }
2554
Jamie Madillb98c3a82015-07-23 14:26:04 -04002555 TIntermAggregate *aggregate = intermediate.makeAggregate(
2556 intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line),
2557 nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002558 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04002559
2560 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002561 return aggregate;
2562}
2563
Olli Etuaho383b7912016-08-05 11:22:59 +03002564void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002565{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002566 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002567
2568 // Embedded structure definitions are not supported per GLSL ES spec.
2569 // They aren't allowed in GLSL either, but we need to detect this here
2570 // so we don't rely on the GLSL compiler to catch it.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302571 if (mStructNestingLevel > 1)
2572 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002573 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002574 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002575}
2576
2577void TParseContext::exitStructDeclaration()
2578{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002579 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002580}
2581
Jamie Madillb98c3a82015-07-23 14:26:04 -04002582namespace
2583{
kbr@chromium.org476541f2011-10-27 21:14:51 +00002584const int kWebGLMaxStructNesting = 4;
2585
2586} // namespace
2587
Arun Patole7e7e68d2015-05-22 12:02:25 +05302588bool TParseContext::structNestingErrorCheck(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002589{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302590 if (!IsWebGLBasedSpec(mShaderSpec))
2591 {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002592 return false;
2593 }
2594
Arun Patole7e7e68d2015-05-22 12:02:25 +05302595 if (field.type()->getBasicType() != EbtStruct)
2596 {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002597 return false;
2598 }
2599
2600 // We're already inside a structure definition at this point, so add
2601 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302602 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2603 {
Jamie Madill41a49272014-03-18 16:10:13 -04002604 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002605 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
2606 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04002607 std::string reason = reasonStream.str();
2608 error(line, reason.c_str(), field.name().c_str(), "");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002609 return true;
2610 }
2611
2612 return false;
2613}
2614
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002615//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002616// Parse an array index expression
2617//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002618TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
2619 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302620 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002621{
2622 TIntermTyped *indexedExpression = NULL;
2623
2624 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2625 {
2626 if (baseExpression->getAsSymbolNode())
2627 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302628 error(location, " left of '[' is not of type array, matrix, or vector ",
2629 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002630 }
2631 else
2632 {
2633 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2634 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002635 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002636
Jamie Madill21c1e452014-12-29 11:33:41 -05002637 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2638
Olli Etuaho36b05142015-11-12 13:10:42 +02002639 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
2640 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
2641 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
2642 // index is a constant expression.
2643 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
2644 {
2645 if (baseExpression->isInterfaceBlock())
2646 {
2647 error(
2648 location, "", "[",
2649 "array indexes for interface blocks arrays must be constant integral expressions");
Olli Etuaho36b05142015-11-12 13:10:42 +02002650 }
2651 else if (baseExpression->getQualifier() == EvqFragmentOut)
2652 {
2653 error(location, "", "[",
2654 "array indexes for fragment outputs must be constant integral expressions");
Olli Etuaho36b05142015-11-12 13:10:42 +02002655 }
Olli Etuaho3e960462015-11-12 15:58:39 +02002656 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
2657 {
2658 error(location, "", "[", "array index for gl_FragData must be constant zero");
Olli Etuaho3e960462015-11-12 15:58:39 +02002659 }
Olli Etuaho36b05142015-11-12 13:10:42 +02002660 }
2661
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002662 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002663 {
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002664 // If the index is not qualified as constant, the behavior in the spec is undefined. This
2665 // applies even if ANGLE has been able to constant fold it (ANGLE may constant fold
2666 // expressions that are not constant expressions). The most compatible way to handle this
2667 // case is to report a warning instead of an error and force the index to be in the
2668 // correct range.
2669 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Jamie Madill21c1e452014-12-29 11:33:41 -05002670 int index = indexConstantUnion->getIConst(0);
Olli Etuaho90892fb2016-07-14 14:44:51 +03002671 if (!baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04002672 {
Olli Etuaho90892fb2016-07-14 14:44:51 +03002673 // Array checks are done later because a different error message might be generated
2674 // based on the index in some cases.
2675 if (baseExpression->isVector())
2676 {
2677 index = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
2678 baseExpression->getType().getNominalSize(),
2679 "vector field selection out of range", "[]");
2680 }
2681 else if (baseExpression->isMatrix())
2682 {
2683 index = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
2684 baseExpression->getType().getCols(),
2685 "matrix field selection out of range", "[]");
2686 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002687 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03002688
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002689 TIntermConstantUnion *baseConstantUnion = baseExpression->getAsConstantUnion();
2690 if (baseConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002691 {
2692 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002693 {
Olli Etuaho90892fb2016-07-14 14:44:51 +03002694 index = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
2695 baseExpression->getArraySize(),
2696 "array index out of range", "[]");
2697 // Constant folding for array indexing.
2698 indexedExpression = foldArraySubscript(index, baseConstantUnion, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002699 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002700 else if (baseExpression->isVector())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002701 {
Olli Etuaho90892fb2016-07-14 14:44:51 +03002702 // Constant folding for vector indexing - reusing vector swizzle folding.
Jamie Madill7164cf42013-07-08 13:30:59 -04002703 TVectorFields fields;
2704 fields.num = 1;
Olli Etuaho90892fb2016-07-14 14:44:51 +03002705 fields.offsets[0] = index;
2706 indexedExpression = foldVectorSwizzle(fields, baseConstantUnion, location);
Jamie Madill7164cf42013-07-08 13:30:59 -04002707 }
2708 else if (baseExpression->isMatrix())
2709 {
Olli Etuaho90892fb2016-07-14 14:44:51 +03002710 // Constant folding for matrix indexing.
2711 indexedExpression = foldMatrixSubscript(index, baseConstantUnion, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002712 }
2713 }
2714 else
2715 {
Jamie Madillb11e2482015-05-04 14:21:22 -04002716 int safeIndex = -1;
2717
Jamie Madill7164cf42013-07-08 13:30:59 -04002718 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002719 {
Olli Etuaho3e960462015-11-12 15:58:39 +02002720 if (baseExpression->getQualifier() == EvqFragData && index > 0)
2721 {
2722 if (mShaderSpec == SH_WEBGL2_SPEC)
2723 {
2724 // Error has been already generated if index is not const.
2725 if (indexExpression->getQualifier() == EvqConst)
2726 {
2727 error(location, "", "[",
2728 "array index for gl_FragData must be constant zero");
Olli Etuaho3e960462015-11-12 15:58:39 +02002729 }
2730 safeIndex = 0;
2731 }
2732 else if (!isExtensionEnabled("GL_EXT_draw_buffers"))
2733 {
2734 outOfRangeError(outOfRangeIndexIsError, location, "", "[",
2735 "array index for gl_FragData must be zero when "
2736 "GL_EXT_draw_buffers is disabled");
2737 safeIndex = 0;
2738 }
2739 }
2740 // Only do generic out-of-range check if similar error hasn't already been reported.
Olli Etuaho90892fb2016-07-14 14:44:51 +03002741 if (safeIndex < 0)
Jamie Madill7164cf42013-07-08 13:30:59 -04002742 {
Olli Etuaho90892fb2016-07-14 14:44:51 +03002743 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
2744 baseExpression->getArraySize(),
2745 "array index out of range", "[]");
Jamie Madill7164cf42013-07-08 13:30:59 -04002746 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002747 }
2748
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002749 // Data of constant unions can't be changed, because it may be shared with other
2750 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
2751 // sanitized object.
Jamie Madillb11e2482015-05-04 14:21:22 -04002752 if (safeIndex != -1)
2753 {
2754 TConstantUnion *safeConstantUnion = new TConstantUnion();
2755 safeConstantUnion->setIConst(safeIndex);
2756 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
2757 }
2758
Jamie Madillb98c3a82015-07-23 14:26:04 -04002759 indexedExpression =
2760 intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002761 }
2762 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002763 else
2764 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002765 indexedExpression =
2766 intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
Jamie Madill7164cf42013-07-08 13:30:59 -04002767 }
2768
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002769 if (indexedExpression == 0)
2770 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002771 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002772 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002773 indexedExpression =
2774 intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002775 }
2776 else if (baseExpression->isArray())
2777 {
Olli Etuahob3fbd862015-09-30 17:55:02 +03002778 TType indexedType = baseExpression->getType();
2779 indexedType.clearArrayness();
2780 indexedExpression->setType(indexedType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002781 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002782 else if (baseExpression->isMatrix())
2783 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002784 indexedExpression->setType(TType(baseExpression->getBasicType(),
Olli Etuahob3fbd862015-09-30 17:55:02 +03002785 baseExpression->getPrecision(), EvqTemporary,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002786 static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002787 }
2788 else if (baseExpression->isVector())
2789 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002790 indexedExpression->setType(
Olli Etuahob3fbd862015-09-30 17:55:02 +03002791 TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002792 }
2793 else
2794 {
2795 indexedExpression->setType(baseExpression->getType());
2796 }
2797
Olli Etuahob3fbd862015-09-30 17:55:02 +03002798 if (baseExpression->getType().getQualifier() == EvqConst &&
2799 indexExpression->getType().getQualifier() == EvqConst)
2800 {
2801 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2802 }
2803 else
2804 {
2805 indexedExpression->getTypePointer()->setQualifier(EvqTemporary);
2806 }
2807
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002808 return indexedExpression;
2809}
2810
Olli Etuaho90892fb2016-07-14 14:44:51 +03002811int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
2812 const TSourceLoc &location,
2813 int index,
2814 int arraySize,
2815 const char *reason,
2816 const char *token)
2817{
2818 if (index >= arraySize || index < 0)
2819 {
2820 std::stringstream extraInfoStream;
2821 extraInfoStream << "'" << index << "'";
2822 std::string extraInfo = extraInfoStream.str();
2823 outOfRangeError(outOfRangeIndexIsError, location, reason, token, extraInfo.c_str());
2824 if (index < 0)
2825 {
2826 return 0;
2827 }
2828 else
2829 {
2830 return arraySize - 1;
2831 }
2832 }
2833 return index;
2834}
2835
Jamie Madillb98c3a82015-07-23 14:26:04 -04002836TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
2837 const TSourceLoc &dotLocation,
2838 const TString &fieldString,
2839 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002840{
2841 TIntermTyped *indexedExpression = NULL;
2842
2843 if (baseExpression->isArray())
2844 {
2845 error(fieldLocation, "cannot apply dot operator to an array", ".");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002846 }
2847
2848 if (baseExpression->isVector())
2849 {
2850 TVectorFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002851 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields,
2852 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002853 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002854 fields.num = 1;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002855 fields.offsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002856 }
2857
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002858 if (baseExpression->getAsConstantUnion())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002859 {
2860 // constant folding for vector fields
Olli Etuaho90892fb2016-07-14 14:44:51 +03002861 indexedExpression =
2862 foldVectorSwizzle(fields, baseExpression->getAsConstantUnion(), fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002863 }
2864 else
2865 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302866 TIntermTyped *index = intermediate.addSwizzle(fields, fieldLocation);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002867 indexedExpression =
2868 intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002869 }
2870 if (indexedExpression == nullptr)
2871 {
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002872 indexedExpression = baseExpression;
2873 }
2874 else
2875 {
2876 // Note that the qualifier set here will be corrected later.
Jamie Madillb98c3a82015-07-23 14:26:04 -04002877 indexedExpression->setType(TType(baseExpression->getBasicType(),
2878 baseExpression->getPrecision(), EvqTemporary,
Jamie Madillc2128ff2016-07-04 10:26:17 -04002879 static_cast<unsigned char>(fields.num)));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002880 }
2881 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002882 else if (baseExpression->getBasicType() == EbtStruct)
2883 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002884 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302885 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04002886 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002887 {
2888 error(dotLocation, "structure has no fields", "Internal Error");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002889 indexedExpression = baseExpression;
2890 }
2891 else
2892 {
2893 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002894 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002895 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002896 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002897 {
2898 fieldFound = true;
2899 break;
2900 }
2901 }
2902 if (fieldFound)
2903 {
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002904 if (baseExpression->getAsConstantUnion())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002905 {
2906 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2907 if (indexedExpression == 0)
2908 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002909 indexedExpression = baseExpression;
2910 }
2911 else
2912 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002913 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002914 }
2915 }
2916 else
2917 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002918 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002919 unionArray->setIConst(i);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002920 TIntermTyped *index = intermediate.addConstantUnion(
2921 unionArray, *fields[i]->type(), fieldLocation);
2922 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression,
2923 index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002924 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002925 }
2926 }
2927 else
2928 {
2929 error(dotLocation, " no such field in structure", fieldString.c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002930 indexedExpression = baseExpression;
2931 }
2932 }
2933 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002934 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002935 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002936 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302937 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04002938 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002939 {
2940 error(dotLocation, "interface block has no fields", "Internal Error");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002941 indexedExpression = baseExpression;
2942 }
2943 else
2944 {
2945 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002946 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002947 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002948 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002949 {
2950 fieldFound = true;
2951 break;
2952 }
2953 }
2954 if (fieldFound)
2955 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002956 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002957 unionArray->setIConst(i);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002958 TIntermTyped *index =
2959 intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
2960 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock,
2961 baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002962 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002963 }
2964 else
2965 {
2966 error(dotLocation, " no such field in interface block", fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002967 indexedExpression = baseExpression;
2968 }
2969 }
2970 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002971 else
2972 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002973 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002974 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03002975 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05302976 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002977 }
2978 else
2979 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302980 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03002981 " field selection requires structure, vector, or interface block on left hand "
2982 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05302983 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002984 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002985 indexedExpression = baseExpression;
2986 }
2987
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002988 if (baseExpression->getQualifier() == EvqConst)
2989 {
2990 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2991 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002992 else
2993 {
2994 indexedExpression->getTypePointer()->setQualifier(EvqTemporary);
2995 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002996
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002997 return indexedExpression;
2998}
2999
Jamie Madillb98c3a82015-07-23 14:26:04 -04003000TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3001 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003002{
Martin Radev802abe02016-08-04 17:48:32 +03003003 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003004
3005 if (qualifierType == "shared")
3006 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003007 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003008 }
3009 else if (qualifierType == "packed")
3010 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003011 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003012 }
3013 else if (qualifierType == "std140")
3014 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003015 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003016 }
3017 else if (qualifierType == "row_major")
3018 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003019 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003020 }
3021 else if (qualifierType == "column_major")
3022 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003023 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003024 }
3025 else if (qualifierType == "location")
3026 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003027 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
3028 "location requires an argument");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003029 }
3030 else
3031 {
3032 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003033 }
3034
Jamie Madilla5efff92013-06-06 11:56:47 -04003035 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003036}
3037
Martin Radev802abe02016-08-04 17:48:32 +03003038void TParseContext::parseLocalSize(const TString &qualifierType,
3039 const TSourceLoc &qualifierTypeLine,
3040 int intValue,
3041 const TSourceLoc &intValueLine,
3042 const std::string &intValueString,
3043 size_t index,
3044 TLocalSize *localSize)
3045{
3046 layoutSupportedErrorCheck(qualifierTypeLine, qualifierType, 310);
3047 if (intValue < 1)
3048 {
3049 std::string errorMessage = std::string(getLocalSizeString(index)) + " must be positive";
3050 error(intValueLine, "out of range:", intValueString.c_str(), errorMessage.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003051 }
3052 (*localSize)[index] = intValue;
3053}
3054
Jamie Madillb98c3a82015-07-23 14:26:04 -04003055TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3056 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003057 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303058 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003059{
Martin Radev802abe02016-08-04 17:48:32 +03003060 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003061
Martin Radev802abe02016-08-04 17:48:32 +03003062 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003063
Martin Radev802abe02016-08-04 17:48:32 +03003064 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003065 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003066 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003067 if (intValue < 0)
3068 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003069 error(intValueLine, "out of range:", intValueString.c_str(),
3070 "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003071 }
3072 else
3073 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003074 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003075 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003076 }
Martin Radev802abe02016-08-04 17:48:32 +03003077 else if (qualifierType == "local_size_x")
3078 {
3079 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
3080 &qualifier.localSize);
3081 }
3082 else if (qualifierType == "local_size_y")
3083 {
3084 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
3085 &qualifier.localSize);
3086 }
3087 else if (qualifierType == "local_size_z")
3088 {
3089 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
3090 &qualifier.localSize);
3091 }
3092 else
3093 {
3094 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003095 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003096
Jamie Madilla5efff92013-06-06 11:56:47 -04003097 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003098}
3099
Jamie Madillb98c3a82015-07-23 14:26:04 -04003100TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03003101 TLayoutQualifier rightQualifier,
3102 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003103{
Jamie Madilla5efff92013-06-06 11:56:47 -04003104 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003105
Jamie Madilla5efff92013-06-06 11:56:47 -04003106 if (rightQualifier.location != -1)
3107 {
3108 joinedQualifier.location = rightQualifier.location;
3109 }
3110 if (rightQualifier.matrixPacking != EmpUnspecified)
3111 {
3112 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
3113 }
3114 if (rightQualifier.blockStorage != EbsUnspecified)
3115 {
3116 joinedQualifier.blockStorage = rightQualifier.blockStorage;
3117 }
3118
Martin Radev802abe02016-08-04 17:48:32 +03003119 for (size_t i = 0u; i < rightQualifier.localSize.size(); ++i)
3120 {
3121 if (rightQualifier.localSize[i] != -1)
3122 {
3123 if (joinedQualifier.localSize[i] != -1 &&
3124 joinedQualifier.localSize[i] != rightQualifier.localSize[i])
3125 {
3126 error(rightQualifierLocation,
3127 "Cannot have multiple different work group size specifiers",
3128 getLocalSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03003129 }
3130 joinedQualifier.localSize[i] = rightQualifier.localSize[i];
3131 }
3132 }
3133
Jamie Madilla5efff92013-06-06 11:56:47 -04003134 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003135}
3136
Arun Patole7e7e68d2015-05-22 12:02:25 +05303137TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc,
3138 TQualifier interpolationQualifier,
3139 const TSourceLoc &storageLoc,
3140 TQualifier storageQualifier)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003141{
3142 TQualifier mergedQualifier = EvqSmoothIn;
3143
Arun Patole7e7e68d2015-05-22 12:02:25 +05303144 if (storageQualifier == EvqFragmentIn)
3145 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003146 if (interpolationQualifier == EvqSmooth)
3147 mergedQualifier = EvqSmoothIn;
3148 else if (interpolationQualifier == EvqFlat)
3149 mergedQualifier = EvqFlatIn;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003150 else
3151 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003152 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303153 else if (storageQualifier == EvqCentroidIn)
3154 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003155 if (interpolationQualifier == EvqSmooth)
3156 mergedQualifier = EvqCentroidIn;
3157 else if (interpolationQualifier == EvqFlat)
3158 mergedQualifier = EvqFlatIn;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003159 else
3160 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003161 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303162 else if (storageQualifier == EvqVertexOut)
3163 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003164 if (interpolationQualifier == EvqSmooth)
3165 mergedQualifier = EvqSmoothOut;
3166 else if (interpolationQualifier == EvqFlat)
3167 mergedQualifier = EvqFlatOut;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003168 else
3169 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003170 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303171 else if (storageQualifier == EvqCentroidOut)
3172 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003173 if (interpolationQualifier == EvqSmooth)
3174 mergedQualifier = EvqCentroidOut;
3175 else if (interpolationQualifier == EvqFlat)
3176 mergedQualifier = EvqFlatOut;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003177 else
3178 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003179 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303180 else
3181 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003182 error(interpolationLoc,
3183 "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303184 getInterpolationString(interpolationQualifier));
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003185
3186 mergedQualifier = storageQualifier;
3187 }
3188
3189 TPublicType type;
3190 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
3191 return type;
3192}
3193
Jamie Madillb98c3a82015-07-23 14:26:04 -04003194TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3195 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003196{
Olli Etuaho383b7912016-08-05 11:22:59 +03003197 voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier.type);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003198
Olli Etuaho383b7912016-08-05 11:22:59 +03003199 layoutWorkGroupSizeErrorCheck(typeSpecifier.line, typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003200
Arun Patole7e7e68d2015-05-22 12:02:25 +05303201 for (unsigned int i = 0; i < fieldList->size(); ++i)
3202 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003203 //
3204 // Careful not to replace already known aspects of type, like array-ness
3205 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303206 TType *type = (*fieldList)[i]->type();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003207 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003208 type->setPrimarySize(typeSpecifier.primarySize);
3209 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003210 type->setPrecision(typeSpecifier.precision);
3211 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003212 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003213
3214 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303215 if (type->isArray())
3216 {
Olli Etuaho383b7912016-08-05 11:22:59 +03003217 arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003218 }
3219 if (typeSpecifier.array)
3220 type->setArraySize(typeSpecifier.arraySize);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303221 if (typeSpecifier.userDef)
3222 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003223 type->setStruct(typeSpecifier.userDef->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003224 }
3225
Olli Etuaho383b7912016-08-05 11:22:59 +03003226 structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003227 }
3228
Jamie Madill98493dd2013-07-08 14:39:03 -04003229 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003230}
3231
Jamie Madillb98c3a82015-07-23 14:26:04 -04003232TPublicType TParseContext::addStructure(const TSourceLoc &structLine,
3233 const TSourceLoc &nameLine,
3234 const TString *structName,
3235 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003236{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303237 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003238 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003239
Jamie Madill9b820842015-02-12 10:40:10 -05003240 // Store a bool in the struct if we're at global scope, to allow us to
3241 // skip the local struct scoping workaround in HLSL.
Jamie Madillb960cc42015-02-12 15:33:20 +00003242 structure->setUniqueId(TSymbolTable::nextUniqueId());
Jamie Madill9b820842015-02-12 10:40:10 -05003243 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003244
Jamie Madill98493dd2013-07-08 14:39:03 -04003245 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003246 {
Olli Etuaho383b7912016-08-05 11:22:59 +03003247 reservedErrorCheck(nameLine, *structName);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303248 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
3249 if (!symbolTable.declare(userTypeDef))
3250 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003251 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003252 }
3253 }
3254
3255 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04003256 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003257 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003258 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04003259 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003260 switch (qualifier)
3261 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003262 case EvqGlobal:
3263 case EvqTemporary:
3264 break;
3265 default:
3266 error(field.line(), "invalid qualifier on struct member",
3267 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003268 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003269 }
3270 }
3271
3272 TPublicType publicType;
3273 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003274 publicType.userDef = structureType;
Olli Etuahobd163f62015-11-13 12:15:38 +02003275 publicType.isStructSpecifier = true;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003276 exitStructDeclaration();
3277
3278 return publicType;
3279}
3280
Jamie Madillb98c3a82015-07-23 14:26:04 -04003281TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
3282 TIntermAggregate *statementList,
3283 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02003284{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003285 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003286 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02003287 init->isVector())
3288 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003289 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
3290 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003291 return nullptr;
3292 }
3293
Olli Etuahoac5274d2015-02-20 10:19:08 +02003294 if (statementList)
3295 {
3296 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
3297 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02003298 return nullptr;
3299 }
3300 }
3301
Olli Etuahoa3a36662015-02-17 13:46:51 +02003302 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3303 if (node == nullptr)
3304 {
3305 error(loc, "erroneous switch statement", "switch");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003306 return nullptr;
3307 }
3308 return node;
3309}
3310
3311TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3312{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003313 if (mSwitchNestingLevel == 0)
3314 {
3315 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003316 return nullptr;
3317 }
3318 if (condition == nullptr)
3319 {
3320 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003321 return nullptr;
3322 }
3323 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04003324 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02003325 {
3326 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003327 }
3328 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003329 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
3330 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
3331 // fold in case labels.
3332 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02003333 {
3334 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003335 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003336 TIntermCase *node = intermediate.addCase(condition, loc);
3337 if (node == nullptr)
3338 {
3339 error(loc, "erroneous case statement", "case");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003340 return nullptr;
3341 }
3342 return node;
3343}
3344
3345TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3346{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003347 if (mSwitchNestingLevel == 0)
3348 {
3349 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003350 return nullptr;
3351 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003352 TIntermCase *node = intermediate.addCase(nullptr, loc);
3353 if (node == nullptr)
3354 {
3355 error(loc, "erroneous default statement", "default");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003356 return nullptr;
3357 }
3358 return node;
3359}
3360
Jamie Madillb98c3a82015-07-23 14:26:04 -04003361TIntermTyped *TParseContext::createUnaryMath(TOperator op,
3362 TIntermTyped *child,
3363 const TSourceLoc &loc,
3364 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003365{
3366 if (child == nullptr)
3367 {
3368 return nullptr;
3369 }
3370
3371 switch (op)
3372 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003373 case EOpLogicalNot:
3374 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
3375 child->isVector())
3376 {
3377 return nullptr;
3378 }
3379 break;
3380 case EOpBitwiseNot:
3381 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3382 child->isMatrix() || child->isArray())
3383 {
3384 return nullptr;
3385 }
3386 break;
3387 case EOpPostIncrement:
3388 case EOpPreIncrement:
3389 case EOpPostDecrement:
3390 case EOpPreDecrement:
3391 case EOpNegative:
3392 case EOpPositive:
3393 if (child->getBasicType() == EbtStruct || child->getBasicType() == EbtBool ||
3394 child->isArray())
3395 {
3396 return nullptr;
3397 }
3398 // Operators for built-ins are already type checked against their prototype.
3399 default:
3400 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003401 }
3402
Olli Etuahof6c694b2015-03-26 14:50:53 +02003403 return intermediate.addUnaryMath(op, child, loc, funcReturnType);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003404}
3405
Olli Etuaho09b22472015-02-11 11:47:26 +02003406TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3407{
Olli Etuahof6c694b2015-03-26 14:50:53 +02003408 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003409 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003410 {
3411 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003412 return child;
3413 }
3414 return node;
3415}
3416
Jamie Madillb98c3a82015-07-23 14:26:04 -04003417TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
3418 TIntermTyped *child,
3419 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003420{
Olli Etuaho383b7912016-08-05 11:22:59 +03003421 lValueErrorCheck(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02003422 return addUnaryMath(op, child, loc);
3423}
3424
Jamie Madillb98c3a82015-07-23 14:26:04 -04003425bool TParseContext::binaryOpCommonCheck(TOperator op,
3426 TIntermTyped *left,
3427 TIntermTyped *right,
3428 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003429{
3430 if (left->isArray() || right->isArray())
3431 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003432 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003433 {
3434 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3435 return false;
3436 }
3437
3438 if (left->isArray() != right->isArray())
3439 {
3440 error(loc, "array / non-array mismatch", GetOperatorString(op));
3441 return false;
3442 }
3443
3444 switch (op)
3445 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003446 case EOpEqual:
3447 case EOpNotEqual:
3448 case EOpAssign:
3449 case EOpInitialize:
3450 break;
3451 default:
3452 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3453 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02003454 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003455 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003456 if (left->getArraySize() != right->getArraySize())
3457 {
3458 error(loc, "array size mismatch", GetOperatorString(op));
3459 return false;
3460 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003461 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003462
3463 // Check ops which require integer / ivec parameters
3464 bool isBitShift = false;
3465 switch (op)
3466 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003467 case EOpBitShiftLeft:
3468 case EOpBitShiftRight:
3469 case EOpBitShiftLeftAssign:
3470 case EOpBitShiftRightAssign:
3471 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3472 // check that the basic type is an integer type.
3473 isBitShift = true;
3474 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3475 {
3476 return false;
3477 }
3478 break;
3479 case EOpBitwiseAnd:
3480 case EOpBitwiseXor:
3481 case EOpBitwiseOr:
3482 case EOpBitwiseAndAssign:
3483 case EOpBitwiseXorAssign:
3484 case EOpBitwiseOrAssign:
3485 // It is enough to check the type of only one operand, since later it
3486 // is checked that the operand types match.
3487 if (!IsInteger(left->getBasicType()))
3488 {
3489 return false;
3490 }
3491 break;
3492 default:
3493 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003494 }
3495
3496 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3497 // So the basic type should usually match.
3498 if (!isBitShift && left->getBasicType() != right->getBasicType())
3499 {
3500 return false;
3501 }
3502
Olli Etuaho9dd217b2015-03-20 14:24:31 +02003503 // Check that type sizes match exactly on ops that require that.
Olli Etuahoff699002015-03-23 14:38:42 +02003504 // Also check restrictions for structs that contain arrays or samplers.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003505 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003506 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003507 case EOpAssign:
3508 case EOpInitialize:
3509 case EOpEqual:
3510 case EOpNotEqual:
3511 // ESSL 1.00 sections 5.7, 5.8, 5.9
3512 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
3513 {
3514 error(loc, "undefined operation for structs containing arrays",
3515 GetOperatorString(op));
3516 return false;
3517 }
3518 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3519 // we interpret the spec so that this extends to structs containing samplers,
3520 // similarly to ESSL 1.00 spec.
3521 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
3522 left->getType().isStructureContainingSamplers())
3523 {
3524 error(loc, "undefined operation for structs containing samplers",
3525 GetOperatorString(op));
3526 return false;
3527 }
3528 case EOpLessThan:
3529 case EOpGreaterThan:
3530 case EOpLessThanEqual:
3531 case EOpGreaterThanEqual:
3532 if ((left->getNominalSize() != right->getNominalSize()) ||
3533 (left->getSecondarySize() != right->getSecondarySize()))
3534 {
3535 return false;
3536 }
3537 default:
3538 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003539 }
3540
Olli Etuahod6b14282015-03-17 14:31:35 +02003541 return true;
3542}
3543
Jamie Madillb98c3a82015-07-23 14:26:04 -04003544TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
3545 TIntermTyped *left,
3546 TIntermTyped *right,
3547 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02003548{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003549 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003550 return nullptr;
3551
Olli Etuahofc1806e2015-03-17 13:03:11 +02003552 switch (op)
3553 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003554 case EOpEqual:
3555 case EOpNotEqual:
3556 break;
3557 case EOpLessThan:
3558 case EOpGreaterThan:
3559 case EOpLessThanEqual:
3560 case EOpGreaterThanEqual:
3561 ASSERT(!left->isArray() && !right->isArray());
3562 if (left->isMatrix() || left->isVector() || left->getBasicType() == EbtStruct)
3563 {
3564 return nullptr;
3565 }
3566 break;
3567 case EOpLogicalOr:
3568 case EOpLogicalXor:
3569 case EOpLogicalAnd:
3570 ASSERT(!left->isArray() && !right->isArray());
3571 if (left->getBasicType() != EbtBool || left->isMatrix() || left->isVector())
3572 {
3573 return nullptr;
3574 }
3575 break;
3576 case EOpAdd:
3577 case EOpSub:
3578 case EOpDiv:
3579 case EOpMul:
3580 ASSERT(!left->isArray() && !right->isArray());
3581 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool)
3582 {
3583 return nullptr;
3584 }
3585 break;
3586 case EOpIMod:
3587 ASSERT(!left->isArray() && !right->isArray());
3588 // Note that this is only for the % operator, not for mod()
3589 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool ||
3590 left->getBasicType() == EbtFloat)
3591 {
3592 return nullptr;
3593 }
3594 break;
3595 // Note that for bitwise ops, type checking is done in promote() to
3596 // share code between ops and compound assignment
3597 default:
3598 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003599 }
3600
Olli Etuahofc1806e2015-03-17 13:03:11 +02003601 return intermediate.addBinaryMath(op, left, right, loc);
3602}
3603
Jamie Madillb98c3a82015-07-23 14:26:04 -04003604TIntermTyped *TParseContext::addBinaryMath(TOperator op,
3605 TIntermTyped *left,
3606 TIntermTyped *right,
3607 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003608{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003609 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003610 if (node == 0)
3611 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003612 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3613 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003614 return left;
3615 }
3616 return node;
3617}
3618
Jamie Madillb98c3a82015-07-23 14:26:04 -04003619TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
3620 TIntermTyped *left,
3621 TIntermTyped *right,
3622 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003623{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003624 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003625 if (node == 0)
3626 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003627 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3628 right->getCompleteString());
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003629 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02003630 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003631 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
3632 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003633 }
3634 return node;
3635}
3636
Jamie Madillb98c3a82015-07-23 14:26:04 -04003637TIntermTyped *TParseContext::createAssign(TOperator op,
3638 TIntermTyped *left,
3639 TIntermTyped *right,
3640 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003641{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003642 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003643 {
3644 return intermediate.addAssign(op, left, right, loc);
3645 }
3646 return nullptr;
3647}
3648
Jamie Madillb98c3a82015-07-23 14:26:04 -04003649TIntermTyped *TParseContext::addAssign(TOperator op,
3650 TIntermTyped *left,
3651 TIntermTyped *right,
3652 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003653{
3654 TIntermTyped *node = createAssign(op, left, right, loc);
3655 if (node == nullptr)
3656 {
3657 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02003658 return left;
3659 }
3660 return node;
3661}
3662
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003663TIntermTyped *TParseContext::addComma(TIntermTyped *left,
3664 TIntermTyped *right,
3665 const TSourceLoc &loc)
3666{
Corentin Wallez0d959252016-07-12 17:26:32 -04003667 // WebGL2 section 5.26, the following results in an error:
3668 // "Sequence operator applied to void, arrays, or structs containing arrays"
3669 if (mShaderSpec == SH_WEBGL2_SPEC && (left->isArray() || left->getBasicType() == EbtVoid ||
3670 left->getType().isStructureContainingArrays() ||
3671 right->isArray() || right->getBasicType() == EbtVoid ||
3672 right->getType().isStructureContainingArrays()))
3673 {
3674 error(loc,
3675 "sequence operator is not allowed for void, arrays, or structs containing arrays",
3676 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04003677 }
3678
Olli Etuaho15200042015-11-04 16:56:31 +02003679 return intermediate.addComma(left, right, loc, mShaderVersion);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003680}
3681
Olli Etuaho49300862015-02-20 14:54:49 +02003682TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3683{
3684 switch (op)
3685 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003686 case EOpContinue:
3687 if (mLoopNestingLevel <= 0)
3688 {
3689 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04003690 }
3691 break;
3692 case EOpBreak:
3693 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
3694 {
3695 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04003696 }
3697 break;
3698 case EOpReturn:
3699 if (mCurrentFunctionType->getBasicType() != EbtVoid)
3700 {
3701 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04003702 }
3703 break;
3704 default:
3705 // No checks for discard
3706 break;
Olli Etuaho49300862015-02-20 14:54:49 +02003707 }
3708 return intermediate.addBranch(op, loc);
3709}
3710
Jamie Madillb98c3a82015-07-23 14:26:04 -04003711TIntermBranch *TParseContext::addBranch(TOperator op,
3712 TIntermTyped *returnValue,
3713 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02003714{
3715 ASSERT(op == EOpReturn);
3716 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003717 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02003718 {
3719 error(loc, "void function cannot return a value", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02003720 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003721 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02003722 {
3723 error(loc, "function return is not matching type:", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02003724 }
3725 return intermediate.addBranch(op, returnValue, loc);
3726}
3727
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003728void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
3729{
3730 ASSERT(!functionCall->isUserDefined());
3731 const TString &name = functionCall->getName();
3732 TIntermNode *offset = nullptr;
3733 TIntermSequence *arguments = functionCall->getSequence();
3734 if (name.compare(0, 16, "texelFetchOffset") == 0 ||
3735 name.compare(0, 16, "textureLodOffset") == 0 ||
3736 name.compare(0, 20, "textureProjLodOffset") == 0 ||
3737 name.compare(0, 17, "textureGradOffset") == 0 ||
3738 name.compare(0, 21, "textureProjGradOffset") == 0)
3739 {
3740 offset = arguments->back();
3741 }
3742 else if (name.compare(0, 13, "textureOffset") == 0 ||
3743 name.compare(0, 17, "textureProjOffset") == 0)
3744 {
3745 // A bias parameter might follow the offset parameter.
3746 ASSERT(arguments->size() >= 3);
3747 offset = (*arguments)[2];
3748 }
3749 if (offset != nullptr)
3750 {
3751 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
3752 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
3753 {
3754 TString unmangledName = TFunction::unmangleName(name);
3755 error(functionCall->getLine(), "Texture offset must be a constant expression",
3756 unmangledName.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003757 }
3758 else
3759 {
3760 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
3761 size_t size = offsetConstantUnion->getType().getObjectSize();
3762 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
3763 for (size_t i = 0u; i < size; ++i)
3764 {
3765 int offsetValue = values[i].getIConst();
3766 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
3767 {
3768 std::stringstream tokenStream;
3769 tokenStream << offsetValue;
3770 std::string token = tokenStream.str();
3771 error(offset->getLine(), "Texture offset value out of valid range",
3772 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003773 }
3774 }
3775 }
3776 }
3777}
3778
Jamie Madillb98c3a82015-07-23 14:26:04 -04003779TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
3780 TIntermNode *paramNode,
3781 TIntermNode *thisNode,
3782 const TSourceLoc &loc,
3783 bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003784{
Jamie Madillb98c3a82015-07-23 14:26:04 -04003785 *fatalError = false;
3786 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003787 TIntermTyped *callNode = nullptr;
3788
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003789 if (thisNode != nullptr)
3790 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003791 TConstantUnion *unionArray = new TConstantUnion[1];
Jamie Madillb98c3a82015-07-23 14:26:04 -04003792 int arraySize = 0;
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003793 TIntermTyped *typedThis = thisNode->getAsTyped();
3794 if (fnCall->getName() != "length")
3795 {
3796 error(loc, "invalid method", fnCall->getName().c_str());
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003797 }
3798 else if (paramNode != nullptr)
3799 {
3800 error(loc, "method takes no parameters", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003801 }
3802 else if (typedThis == nullptr || !typedThis->isArray())
3803 {
3804 error(loc, "length can only be called on arrays", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003805 }
3806 else
3807 {
Olli Etuaho96e67382015-04-23 14:27:02 +03003808 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03003809 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003810 {
Olli Etuaho39282e12015-04-23 15:41:48 +03003811 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003812 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03003813 // (func()).length()
3814 // (int[3](0, 1, 2)).length()
Jamie Madillb98c3a82015-07-23 14:26:04 -04003815 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
3816 // expression.
3817 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
3818 // spec section 5.9 which allows "An array, vector or matrix expression with the
3819 // length method applied".
3820 error(loc, "length can only be called on array names, not on array expressions",
3821 "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003822 }
3823 }
Olli Etuaho96e67382015-04-23 14:27:02 +03003824 unionArray->setIConst(arraySize);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003825 callNode =
3826 intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003827 }
3828 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003829 {
3830 //
3831 // Then this should be a constructor.
3832 // Don't go through the symbol table for constructors.
3833 // Their parameters will be verified algorithmically.
3834 //
3835 TType type(EbtVoid, EbpUndefined); // use this to get the type back
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003836 if (!constructorErrorCheck(loc, paramNode, *fnCall, op, &type))
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003837 {
3838 //
3839 // It's a constructor, of type 'type'.
3840 //
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003841 callNode = addConstructor(paramNode, &type, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003842 }
Olli Etuaho72ba85b2015-03-04 14:23:26 +02003843
3844 if (callNode == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003845 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003846 callNode = intermediate.setAggregateOperator(nullptr, op, loc);
3847 }
3848 callNode->setType(type);
3849 }
3850 else
3851 {
3852 //
3853 // Not a constructor. Find it in the symbol table.
3854 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303855 const TFunction *fnCandidate;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003856 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003857 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003858 if (fnCandidate)
3859 {
3860 //
3861 // A declared function.
3862 //
Olli Etuaho383b7912016-08-05 11:22:59 +03003863 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003864 {
Olli Etuaho383b7912016-08-05 11:22:59 +03003865 extensionErrorCheck(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003866 }
3867 op = fnCandidate->getBuiltInOp();
3868 if (builtIn && op != EOpNull)
3869 {
3870 //
3871 // A function call mapped to a built-in operation.
3872 //
3873 if (fnCandidate->getParamCount() == 1)
3874 {
3875 //
3876 // Treat it like a built-in unary operator.
3877 //
Olli Etuaho15c2ac32015-11-09 15:51:43 +02003878 TIntermAggregate *paramAgg = paramNode->getAsAggregate();
3879 paramNode = paramAgg->getSequence()->front();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003880 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc,
3881 &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003882 if (callNode == nullptr)
3883 {
3884 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003885 extraInfoStream
3886 << "built in unary operator function. Type: "
3887 << static_cast<TIntermTyped *>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003888 std::string extraInfo = extraInfoStream.str();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003889 error(paramNode->getLine(), " wrong operand type", "Internal Error",
3890 extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003891 *fatalError = true;
3892 return nullptr;
3893 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003894 }
3895 else
3896 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003897 TIntermAggregate *aggregate =
3898 intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003899 aggregate->setType(fnCandidate->getReturnType());
3900 aggregate->setPrecisionFromChildren();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02003901 if (aggregate->areChildrenConstQualified())
3902 {
3903 aggregate->getTypePointer()->setQualifier(EvqConst);
3904 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003905
3906 // Some built-in functions have out parameters too.
3907 functionCallLValueErrorCheck(fnCandidate, aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05303908
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003909 // See if we can constant fold a built-in. Note that this may be possible even
3910 // if it is not const-qualified.
Olli Etuahob43846e2015-06-02 18:18:57 +03003911 TIntermTyped *foldedNode = intermediate.foldAggregateBuiltIn(aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05303912 if (foldedNode)
3913 {
Arun Patole274f0702015-05-05 13:33:30 +05303914 callNode = foldedNode;
3915 }
Olli Etuahob43846e2015-06-02 18:18:57 +03003916 else
3917 {
3918 callNode = aggregate;
3919 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003920 }
3921 }
3922 else
3923 {
3924 // This is a real function call
Jamie Madillb98c3a82015-07-23 14:26:04 -04003925 TIntermAggregate *aggregate =
3926 intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003927 aggregate->setType(fnCandidate->getReturnType());
3928
Jamie Madillb98c3a82015-07-23 14:26:04 -04003929 // this is how we know whether the given function is a builtIn function or a user
3930 // defined function
3931 // if builtIn == false, it's a userDefined -> could be an overloaded
3932 // builtIn function also
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003933 // if builtIn == true, it's definitely a builtIn function with EOpNull
3934 if (!builtIn)
3935 aggregate->setUserDefined();
3936 aggregate->setName(fnCandidate->getMangledName());
Corentin Wallez71d147f2015-02-11 11:15:24 -08003937 aggregate->setFunctionId(fnCandidate->getUniqueId());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003938
3939 // This needs to happen after the name is set
3940 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003941 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003942 aggregate->setBuiltInFunctionPrecision();
3943
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003944 checkTextureOffsetConst(aggregate);
3945 }
3946
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003947 callNode = aggregate;
3948
3949 functionCallLValueErrorCheck(fnCandidate, aggregate);
3950 }
3951 }
3952 else
3953 {
3954 // error message was put out by findFunction()
3955 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003956 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003957 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003958 callNode = intermediate.addConstantUnion(unionArray,
3959 TType(EbtFloat, EbpUndefined, EvqConst), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003960 }
3961 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003962 return callNode;
3963}
3964
Jamie Madillb98c3a82015-07-23 14:26:04 -04003965TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
3966 TIntermTyped *trueBlock,
3967 TIntermTyped *falseBlock,
Olli Etuaho52901742015-04-15 13:42:45 +03003968 const TSourceLoc &loc)
3969{
Olli Etuaho383b7912016-08-05 11:22:59 +03003970 boolErrorCheck(loc, cond);
Olli Etuaho52901742015-04-15 13:42:45 +03003971
3972 if (trueBlock->getType() != falseBlock->getType())
3973 {
3974 binaryOpError(loc, ":", trueBlock->getCompleteString(), falseBlock->getCompleteString());
Olli Etuaho52901742015-04-15 13:42:45 +03003975 return falseBlock;
3976 }
Olli Etuahoa2d53032015-04-15 14:14:44 +03003977 // ESSL1 sections 5.2 and 5.7:
3978 // ESSL3 section 5.7:
3979 // Ternary operator is not among the operators allowed for structures/arrays.
3980 if (trueBlock->isArray() || trueBlock->getBasicType() == EbtStruct)
3981 {
3982 error(loc, "ternary operator is not allowed for structures or arrays", ":");
Olli Etuahoa2d53032015-04-15 14:14:44 +03003983 return falseBlock;
3984 }
Corentin Wallez0d959252016-07-12 17:26:32 -04003985 // WebGL2 section 5.26, the following results in an error:
3986 // "Ternary operator applied to void, arrays, or structs containing arrays"
3987 if (mShaderSpec == SH_WEBGL2_SPEC && trueBlock->getBasicType() == EbtVoid)
3988 {
3989 error(loc, "ternary operator is not allowed for void", ":");
Corentin Wallez0d959252016-07-12 17:26:32 -04003990 return falseBlock;
3991 }
3992
Olli Etuaho52901742015-04-15 13:42:45 +03003993 return intermediate.addSelection(cond, trueBlock, falseBlock, loc);
3994}
Olli Etuaho49300862015-02-20 14:54:49 +02003995
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003996//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003997// Parse an array of strings using yyparse.
3998//
3999// Returns 0 for success.
4000//
Jamie Madillb98c3a82015-07-23 14:26:04 -04004001int PaParseStrings(size_t count,
4002 const char *const string[],
4003 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05304004 TParseContext *context)
4005{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004006 if ((count == 0) || (string == NULL))
4007 return 1;
4008
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004009 if (glslang_initialize(context))
4010 return 1;
4011
alokp@chromium.org408c45e2012-04-05 15:54:43 +00004012 int error = glslang_scan(count, string, length, context);
4013 if (!error)
4014 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004015
alokp@chromium.org73bc2982012-06-19 18:48:05 +00004016 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00004017
alokp@chromium.org6b495712012-06-29 00:06:58 +00004018 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004019}