blob: 75129d7cde956e29161e71bd9ce53c11aaaaca58 [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 Etuaho856c4972016-08-08 11:38:39 +0300216void TParseContext::checkPrecisionSpecified(const TSourceLoc &line,
217 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//
Olli Etuaho856c4972016-08-08 11:38:39 +0300250bool TParseContext::checkCanBeLValue(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:
Olli Etuaho856c4972016-08-08 11:38:39 +0300265 return checkCanBeLValue(line, op, binaryNode->getLeft());
Jamie Madillb98c3a82015-07-23 14:26:04 -0400266 case EOpVectorSwizzle:
Olli Etuaho856c4972016-08-08 11:38:39 +0300267 errorReturn = checkCanBeLValue(line, op, binaryNode->getLeft());
Jamie Madillb98c3a82015-07-23 14:26:04 -0400268 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 Etuaho856c4972016-08-08 11:38:39 +0300406void TParseContext::checkIsConst(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 Etuaho856c4972016-08-08 11:38:39 +0300416void TParseContext::checkIsScalarInteger(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 Etuaho856c4972016-08-08 11:38:39 +0300426void TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000427{
Olli Etuaho856c4972016-08-08 11:38:39 +0300428 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300429 {
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//
Olli Etuaho856c4972016-08-08 11:38:39 +0300442bool TParseContext::checkIsNotReserved(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//
Olli Etuaho856c4972016-08-08 11:38:39 +0300490bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
491 TIntermNode *argumentsNode,
492 const TFunction &function,
493 TOperator op,
494 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000495{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000496 bool constructingMatrix = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400497 switch (op)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530498 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400499 case EOpConstructMat2:
500 case EOpConstructMat2x3:
501 case EOpConstructMat2x4:
502 case EOpConstructMat3x2:
503 case EOpConstructMat3:
504 case EOpConstructMat3x4:
505 case EOpConstructMat4x2:
506 case EOpConstructMat4x3:
507 case EOpConstructMat4:
508 constructingMatrix = true;
509 break;
510 default:
511 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000512 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000513
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000514 //
515 // Note: It's okay to have too many components available, but not okay to have unused
516 // arguments. 'full' will go to true when enough args have been seen. If we loop
517 // again, there is an extra argument, so 'overfull' will become true.
518 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000519
Jamie Madillb98c3a82015-07-23 14:26:04 -0400520 size_t size = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400521 bool full = false;
522 bool overFull = false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000523 bool matrixInMatrix = false;
524 bool arrayArg = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530525 for (size_t i = 0; i < function.getParamCount(); ++i)
526 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700527 const TConstParameter &param = function.getParam(i);
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000528 size += param.type->getObjectSize();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530529
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000530 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000531 matrixInMatrix = true;
532 if (full)
533 overFull = true;
Olli Etuaho856c4972016-08-08 11:38:39 +0300534 if (op != EOpConstructStruct && !type.isArray() && size >= type.getObjectSize())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000535 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000536 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000537 arrayArg = true;
538 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530539
Olli Etuaho856c4972016-08-08 11:38:39 +0300540 if (type.isArray())
Olli Etuaho376f1b52015-04-13 13:23:41 +0300541 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300542 // The size of an unsized constructor should already have been determined.
543 ASSERT(!type.isUnsizedArray());
544 if (static_cast<size_t>(type.getArraySize()) != function.getParamCount())
Olli Etuaho376f1b52015-04-13 13:23:41 +0300545 {
546 error(line, "array constructor needs one argument per array element", "constructor");
547 return true;
548 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000549 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000550
Arun Patole7e7e68d2015-05-22 12:02:25 +0530551 if (arrayArg && op != EOpConstructStruct)
552 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000553 error(line, "constructing from a non-dereferenced array", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000554 return true;
555 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000556
Olli Etuaho856c4972016-08-08 11:38:39 +0300557 if (matrixInMatrix && !type.isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530558 {
559 if (function.getParamCount() != 1)
560 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400561 error(line, "constructing matrix from matrix can only take one argument",
562 "constructor");
563 return true;
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000564 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000565 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000566
Arun Patole7e7e68d2015-05-22 12:02:25 +0530567 if (overFull)
568 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000569 error(line, "too many arguments", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000570 return true;
571 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530572
Olli Etuaho856c4972016-08-08 11:38:39 +0300573 if (op == EOpConstructStruct && !type.isArray() &&
574 type.getStruct()->fields().size() != function.getParamCount())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530575 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400576 error(line,
577 "Number of constructor parameters does not match the number of structure fields",
578 "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000579 return true;
580 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000581
Olli Etuaho856c4972016-08-08 11:38:39 +0300582 if (!type.isMatrix() || !matrixInMatrix)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530583 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300584 if ((op != EOpConstructStruct && size != 1 && size < type.getObjectSize()) ||
585 (op == EOpConstructStruct && size < type.getObjectSize()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530586 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000587 error(line, "not enough data provided for construction", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000588 return true;
589 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000590 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000591
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200592 if (argumentsNode == nullptr)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530593 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200594 error(line, "constructor does not have any arguments", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000595 return true;
596 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200597
598 TIntermAggregate *argumentsAgg = argumentsNode->getAsAggregate();
599 for (TIntermNode *&argNode : *argumentsAgg->getSequence())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530600 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200601 TIntermTyped *argTyped = argNode->getAsTyped();
602 ASSERT(argTyped != nullptr);
603 if (op != EOpConstructStruct && IsSampler(argTyped->getBasicType()))
604 {
605 error(line, "cannot convert a sampler", "constructor");
606 return true;
607 }
608 if (argTyped->getBasicType() == EbtVoid)
609 {
610 error(line, "cannot convert a void", "constructor");
611 return true;
612 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000613 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000614
Olli Etuaho856c4972016-08-08 11:38:39 +0300615 if (type.isArray())
616 {
617 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
618 // the array.
619 for (TIntermNode *&argNode : *argumentsAgg->getSequence())
620 {
621 const TType &argType = argNode->getAsTyped()->getType();
622 // It has already been checked that the argument is not an array.
623 ASSERT(!argType.isArray());
624 if (!argType.sameElementType(type))
625 {
626 error(line, "Array constructor argument has an incorrect type", "Error");
627 return true;
628 }
629 }
630 }
631 else if (op == EOpConstructStruct)
632 {
633 const TFieldList &fields = type.getStruct()->fields();
634 TIntermSequence *args = argumentsAgg->getSequence();
635
636 for (size_t i = 0; i < fields.size(); i++)
637 {
638 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
639 {
640 error(line, "Structure constructor arguments do not match structure fields",
641 "Error");
642 return true;
643 }
644 }
645 }
646
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000647 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000648}
649
Jamie Madillb98c3a82015-07-23 14:26:04 -0400650// This function checks to see if a void variable has been declared and raise an error message for
651// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000652//
653// returns true in case of an error
654//
Olli Etuaho856c4972016-08-08 11:38:39 +0300655bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400656 const TString &identifier,
657 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000658{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300659 if (type == EbtVoid)
660 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000661 error(line, "illegal use of type 'void'", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000662 return true;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300663 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000664
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000665 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000666}
667
Jamie Madillb98c3a82015-07-23 14:26:04 -0400668// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300669// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300670void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000671{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530672 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector())
673 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000674 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530675 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000676}
677
Jamie Madillb98c3a82015-07-23 14:26:04 -0400678// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300679// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300680void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000681{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530682 if (pType.type != EbtBool || pType.isAggregate())
683 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000684 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530685 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000686}
687
Olli Etuaho856c4972016-08-08 11:38:39 +0300688bool TParseContext::checkIsNotSampler(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400689 const TPublicType &pType,
690 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000691{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530692 if (pType.type == EbtStruct)
693 {
694 if (containsSampler(*pType.userDef))
695 {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000696 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530697
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000698 return true;
699 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530700
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000701 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530702 }
703 else if (IsSampler(pType.type))
704 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000705 error(line, reason, getBasicString(pType.type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000706
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000707 return true;
708 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000709
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000710 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000711}
712
Olli Etuaho856c4972016-08-08 11:38:39 +0300713void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
714 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400715{
716 if (pType.layoutQualifier.location != -1)
717 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400718 error(line, "location must only be specified for a single input or output variable",
719 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400720 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400721}
722
Olli Etuaho856c4972016-08-08 11:38:39 +0300723void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
724 const TLayoutQualifier &layoutQualifier)
725{
726 if (layoutQualifier.location != -1)
727 {
728 error(location, "invalid layout qualifier:", "location",
729 "only valid on program inputs and outputs");
730 }
731}
732
733void TParseContext::checkOutParameterIsNotSampler(const TSourceLoc &line,
734 TQualifier qualifier,
735 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000736{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400737 if ((qualifier == EvqOut || qualifier == EvqInOut) && type.getBasicType() != EbtStruct &&
738 IsSampler(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530739 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000740 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000741 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000742}
743
Arun Patole7e7e68d2015-05-22 12:02:25 +0530744bool TParseContext::containsSampler(const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000745{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000746 if (IsSampler(type.getBasicType()))
747 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000748
Arun Patole7e7e68d2015-05-22 12:02:25 +0530749 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock())
750 {
751 const TFieldList &fields = type.getStruct()->fields();
752 for (unsigned int i = 0; i < fields.size(); ++i)
753 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400754 if (containsSampler(*fields[i]->type()))
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000755 return true;
756 }
757 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000758
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000759 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000760}
761
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000762// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300763unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000764{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530765 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000766
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200767 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
768 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
769 // fold as array size.
770 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000771 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000772 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300773 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000774 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000775
Olli Etuaho856c4972016-08-08 11:38:39 +0300776 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400777
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000778 if (constant->getBasicType() == EbtUInt)
779 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300780 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000781 }
782 else
783 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300784 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000785
Olli Etuaho856c4972016-08-08 11:38:39 +0300786 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000787 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400788 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300789 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000790 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400791
Olli Etuaho856c4972016-08-08 11:38:39 +0300792 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400793 }
794
Olli Etuaho856c4972016-08-08 11:38:39 +0300795 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400796 {
797 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300798 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400799 }
800
801 // The size of arrays is restricted here to prevent issues further down the
802 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
803 // 4096 registers so this should be reasonable even for aggressively optimizable code.
804 const unsigned int sizeLimit = 65536;
805
Olli Etuaho856c4972016-08-08 11:38:39 +0300806 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -0400807 {
808 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300809 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000810 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300811
812 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000813}
814
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000815// See if this qualifier can be an array.
816//
817// Returns true if there is an error.
818//
Olli Etuaho856c4972016-08-08 11:38:39 +0300819bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line, const TPublicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000820{
Olli Etuaho3739d232015-04-08 12:23:44 +0300821 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqVertexIn) ||
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400822 (type.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300823 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400824 error(line, "cannot declare arrays of this qualifier",
825 TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000826 return true;
827 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000828
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000829 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000830}
831
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000832// See if this type can be an array.
833//
834// Returns true if there is an error.
835//
Olli Etuaho856c4972016-08-08 11:38:39 +0300836bool TParseContext::checkIsValidTypeForArray(const TSourceLoc &line, const TPublicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000837{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000838 //
839 // Can the type be an array?
840 //
Jamie Madill06145232015-05-13 13:10:01 -0400841 if (type.array)
842 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000843 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000844 return true;
845 }
Olli Etuahocc36b982015-07-10 14:14:18 +0300846 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
847 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
848 // 4.3.4).
849 if (mShaderVersion >= 300 && type.type == EbtStruct && sh::IsVarying(type.qualifier))
850 {
851 error(line, "cannot declare arrays of structs of this qualifier",
852 TType(type).getCompleteString().c_str());
853 return true;
854 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000855
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000856 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000857}
858
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000859// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +0300860void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
861 const TString &identifier,
862 TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000863{
Olli Etuaho3739d232015-04-08 12:23:44 +0300864 ASSERT(type != nullptr);
865 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000866 {
867 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300868 type->qualifier = EvqTemporary;
869
870 // Generate informative error messages for ESSL1.
871 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400872 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000873 {
Arun Patole7e7e68d2015-05-22 12:02:25 +0530874 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400875 "structures containing arrays may not be declared constant since they cannot be "
876 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530877 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000878 }
879 else
880 {
881 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
882 }
Olli Etuaho383b7912016-08-05 11:22:59 +0300883 return;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000884 }
Olli Etuaho376f1b52015-04-13 13:23:41 +0300885 if (type->isUnsizedArray())
886 {
887 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
Olli Etuaho376f1b52015-04-13 13:23:41 +0300888 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000889}
890
Olli Etuaho2935c582015-04-08 14:32:06 +0300891// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000892// and update the symbol table.
893//
Olli Etuaho2935c582015-04-08 14:32:06 +0300894// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000895//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400896bool TParseContext::declareVariable(const TSourceLoc &line,
897 const TString &identifier,
898 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +0300899 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000900{
Olli Etuaho2935c582015-04-08 14:32:06 +0300901 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000902
Olli Etuaho856c4972016-08-08 11:38:39 +0300903 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000904
Olli Etuaho2935c582015-04-08 14:32:06 +0300905 // gl_LastFragData may be redeclared with a new precision qualifier
906 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
907 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400908 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
909 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho856c4972016-08-08 11:38:39 +0300910 if (static_cast<int>(type.getArraySize()) == maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +0300911 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400912 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +0300913 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300914 needsReservedCheck = checkCanUseExtension(line, builtInSymbol->getExtension());
Olli Etuaho2935c582015-04-08 14:32:06 +0300915 }
916 }
917 else
918 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400919 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
920 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +0300921 return false;
922 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000923 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000924
Olli Etuaho856c4972016-08-08 11:38:39 +0300925 if (needsReservedCheck && checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +0300926 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000927
Olli Etuaho2935c582015-04-08 14:32:06 +0300928 (*variable) = new TVariable(&identifier, type);
929 if (!symbolTable.declare(*variable))
930 {
931 error(line, "redefinition", identifier.c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -0400932 *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +0300933 return false;
934 }
935
Olli Etuaho856c4972016-08-08 11:38:39 +0300936 if (checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +0300937 return false;
938
939 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000940}
941
Olli Etuaho856c4972016-08-08 11:38:39 +0300942void TParseContext::checkIsParameterQualifierValid(const TSourceLoc &line,
943 TQualifier qualifier,
944 TQualifier paramQualifier,
945 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530946{
947 if (qualifier != EvqConst && qualifier != EvqTemporary)
948 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000949 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +0300950 return;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000951 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530952 if (qualifier == EvqConst && paramQualifier != EvqIn)
953 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400954 error(line, "qualifier not allowed with ", getQualifierString(qualifier),
955 getQualifierString(paramQualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +0300956 return;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000957 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000958
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000959 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000960 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000961 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000962 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000963}
964
Olli Etuaho856c4972016-08-08 11:38:39 +0300965bool TParseContext::checkCanUseExtension(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000966{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400967 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000968 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +0530969 if (iter == extBehavior.end())
970 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000971 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000972 return true;
973 }
zmo@google.comf5450912011-09-09 01:37:19 +0000974 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +0530975 if (iter->second == EBhDisable || iter->second == EBhUndefined)
976 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000977 error(line, "extension", extension.c_str(), "is disabled");
978 return true;
979 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530980 if (iter->second == EBhWarn)
981 {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000982 warning(line, "extension", extension.c_str(), "is being used");
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000983 return false;
984 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000985
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000986 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000987}
988
Jamie Madillb98c3a82015-07-23 14:26:04 -0400989// These checks are common for all declarations starting a declarator list, and declarators that
990// follow an empty declaration.
Olli Etuaho383b7912016-08-05 11:22:59 +0300991void TParseContext::singleDeclarationErrorCheck(const TPublicType &publicType,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400992 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -0400993{
Olli Etuahofa33d582015-04-09 14:33:12 +0300994 switch (publicType.qualifier)
995 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400996 case EvqVaryingIn:
997 case EvqVaryingOut:
998 case EvqAttribute:
999 case EvqVertexIn:
1000 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001001 case EvqComputeIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -04001002 if (publicType.type == EbtStruct)
1003 {
1004 error(identifierLocation, "cannot be used with a structure",
1005 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001006 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001007 }
Olli Etuahofa33d582015-04-09 14:33:12 +03001008
Jamie Madillb98c3a82015-07-23 14:26:04 -04001009 default:
1010 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001011 }
1012
Jamie Madillb98c3a82015-07-23 14:26:04 -04001013 if (publicType.qualifier != EvqUniform &&
Olli Etuaho856c4972016-08-08 11:38:39 +03001014 checkIsNotSampler(identifierLocation, publicType, "samplers must be uniform"))
Olli Etuahofa33d582015-04-09 14:33:12 +03001015 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001016 return;
Olli Etuahofa33d582015-04-09 14:33:12 +03001017 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001018
1019 // check for layout qualifier issues
1020 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
1021
1022 if (layoutQualifier.matrixPacking != EmpUnspecified)
1023 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001024 error(identifierLocation, "layout qualifier",
1025 getMatrixPackingString(layoutQualifier.matrixPacking),
Olli Etuahofa33d582015-04-09 14:33:12 +03001026 "only valid for interface blocks");
Olli Etuaho383b7912016-08-05 11:22:59 +03001027 return;
Jamie Madilla5efff92013-06-06 11:56:47 -04001028 }
1029
1030 if (layoutQualifier.blockStorage != EbsUnspecified)
1031 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001032 error(identifierLocation, "layout qualifier",
1033 getBlockStorageString(layoutQualifier.blockStorage),
Olli Etuahofa33d582015-04-09 14:33:12 +03001034 "only valid for interface blocks");
Olli Etuaho383b7912016-08-05 11:22:59 +03001035 return;
Jamie Madilla5efff92013-06-06 11:56:47 -04001036 }
1037
Olli Etuaho383b7912016-08-05 11:22:59 +03001038 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut)
Jamie Madilla5efff92013-06-06 11:56:47 -04001039 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001040 checkLocationIsNotSpecified(identifierLocation, publicType.layoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04001041 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001042}
1043
Olli Etuaho856c4972016-08-08 11:38:39 +03001044void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1045 const TString &layoutQualifierName,
1046 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001047{
1048
1049 if (mShaderVersion < versionRequired)
1050 {
1051 error(location, "invalid layout qualifier:", layoutQualifierName.c_str(), "not supported");
Martin Radev802abe02016-08-04 17:48:32 +03001052 }
1053}
1054
Olli Etuaho856c4972016-08-08 11:38:39 +03001055bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1056 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001057{
1058 const TLocalSize &localSize = layoutQualifier.localSize;
1059 for (size_t i = 0u; i < localSize.size(); ++i)
1060 {
1061 if (localSize[i] != -1)
1062 {
1063 error(location, "invalid layout qualifier:", getLocalSizeString(i),
1064 "only valid when used with 'in' in a compute shader global layout declaration");
1065 return true;
1066 }
1067 }
1068
1069 return false;
1070}
1071
Olli Etuaho383b7912016-08-05 11:22:59 +03001072void TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate,
Olli Etuaho856c4972016-08-08 11:38:39 +03001073 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001074{
1075 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1076 {
1077 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1078 if (qual == EvqOut || qual == EvqInOut)
1079 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001080 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
1081 if (checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001082 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001083 error(argument->getLine(),
Jamie Madillb98c3a82015-07-23 14:26:04 -04001084 "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error");
Olli Etuaho383b7912016-08-05 11:22:59 +03001085 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001086 }
1087 }
1088 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001089}
1090
Olli Etuaho856c4972016-08-08 11:38:39 +03001091void TParseContext::checkInvariantIsOutVariableES3(const TQualifier qualifier,
1092 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001093{
1094 if (!sh::IsVaryingOut(qualifier) && qualifier != EvqFragmentOut)
1095 {
1096 error(invariantLocation, "Only out variables can be invariant.", "invariant");
Olli Etuaho37ad4742015-04-27 13:18:50 +03001097 }
1098}
1099
Arun Patole7e7e68d2015-05-22 12:02:25 +05301100bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001101{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001102 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001103 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1104 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001105}
1106
Arun Patole7e7e68d2015-05-22 12:02:25 +05301107bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001108{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001109 return ::IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001110}
1111
Jamie Madillb98c3a82015-07-23 14:26:04 -04001112void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1113 const char *extName,
1114 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001115{
1116 pp::SourceLocation srcLoc;
1117 srcLoc.file = loc.first_file;
1118 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001119 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001120}
1121
Jamie Madillb98c3a82015-07-23 14:26:04 -04001122void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1123 const char *name,
1124 const char *value,
1125 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001126{
1127 pp::SourceLocation srcLoc;
1128 srcLoc.file = loc.first_file;
1129 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001130 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001131}
1132
Martin Radev802abe02016-08-04 17:48:32 +03001133TLocalSize TParseContext::getComputeShaderLocalSize() const
1134{
1135 TLocalSize result;
1136 for (size_t i = 0u; i < result.size(); ++i)
1137 {
1138 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1139 {
1140 result[i] = 1;
1141 }
1142 else
1143 {
1144 result[i] = mComputeShaderLocalSize[i];
1145 }
1146 }
1147 return result;
1148}
1149
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001150/////////////////////////////////////////////////////////////////////////////////
1151//
1152// Non-Errors.
1153//
1154/////////////////////////////////////////////////////////////////////////////////
1155
Jamie Madill5c097022014-08-20 16:38:32 -04001156const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1157 const TString *name,
1158 const TSymbol *symbol)
1159{
1160 const TVariable *variable = NULL;
1161
1162 if (!symbol)
1163 {
1164 error(location, "undeclared identifier", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001165 }
1166 else if (!symbol->isVariable())
1167 {
1168 error(location, "variable expected", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001169 }
1170 else
1171 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001172 variable = static_cast<const TVariable *>(symbol);
Jamie Madill5c097022014-08-20 16:38:32 -04001173
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001174 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
Olli Etuaho383b7912016-08-05 11:22:59 +03001175 !variable->getExtension().empty())
Jamie Madill5c097022014-08-20 16:38:32 -04001176 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001177 checkCanUseExtension(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001178 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001179
1180 // Reject shaders using both gl_FragData and gl_FragColor
1181 TQualifier qualifier = variable->getType().getQualifier();
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001182 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001183 {
1184 mUsesFragData = true;
1185 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001186 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001187 {
1188 mUsesFragColor = true;
1189 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001190 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1191 {
1192 mUsesSecondaryOutputs = true;
1193 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001194
1195 // This validation is not quite correct - it's only an error to write to
1196 // both FragData and FragColor. For simplicity, and because users shouldn't
1197 // be rewarded for reading from undefined varaibles, return an error
1198 // if they are both referenced, rather than assigned.
1199 if (mUsesFragData && mUsesFragColor)
1200 {
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001201 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1202 if (mUsesSecondaryOutputs)
1203 {
1204 errorMessage =
1205 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1206 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1207 }
1208 error(location, errorMessage, name->c_str());
Jamie Madill14e95b32015-05-07 10:10:41 -04001209 }
Martin Radevb0883602016-08-04 17:48:58 +03001210
1211 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1212 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1213 qualifier == EvqWorkGroupSize)
1214 {
1215 error(location,
1216 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1217 "gl_WorkGroupSize");
1218 }
Jamie Madill5c097022014-08-20 16:38:32 -04001219 }
1220
1221 if (!variable)
1222 {
1223 TType type(EbtFloat, EbpUndefined);
1224 TVariable *fakeVariable = new TVariable(name, type);
1225 symbolTable.declare(fakeVariable);
1226 variable = fakeVariable;
1227 }
1228
1229 return variable;
1230}
1231
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001232TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1233 const TString *name,
1234 const TSymbol *symbol)
1235{
1236 const TVariable *variable = getNamedVariable(location, name, symbol);
1237
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001238 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001239 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001240 const TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001241 return intermediate.addConstantUnion(constArray, variable->getType(), location);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001242 }
1243 else
1244 {
1245 return intermediate.addSymbol(variable->getUniqueId(), variable->getName(),
1246 variable->getType(), location);
1247 }
1248}
1249
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001250//
1251// Look up a function name in the symbol table, and make sure it is a function.
1252//
1253// Return the function symbol if found, otherwise 0.
1254//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001255const TFunction *TParseContext::findFunction(const TSourceLoc &line,
1256 TFunction *call,
1257 int inputShaderVersion,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301258 bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001259{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001260 // First find by unmangled name to check whether the function name has been
1261 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001262 // If a function is found, check for one with a matching argument list.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301263 const TSymbol *symbol = symbolTable.find(call->getName(), inputShaderVersion, builtIn);
1264 if (symbol == 0 || symbol->isFunction())
1265 {
Austin Kinross3ae64652015-01-26 15:51:39 -08001266 symbol = symbolTable.find(call->getMangledName(), inputShaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001267 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001268
Arun Patole7e7e68d2015-05-22 12:02:25 +05301269 if (symbol == 0)
1270 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001271 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001272 return 0;
1273 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001274
Arun Patole7e7e68d2015-05-22 12:02:25 +05301275 if (!symbol->isFunction())
1276 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001277 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001278 return 0;
1279 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001280
Jamie Madillb98c3a82015-07-23 14:26:04 -04001281 return static_cast<const TFunction *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001282}
1283
1284//
1285// Initializers show up in several places in the grammar. Have one set of
1286// code to handle them here.
1287//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001288// Returns true on error, false if no error
1289//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001290bool TParseContext::executeInitializer(const TSourceLoc &line,
1291 const TString &identifier,
1292 const TPublicType &pType,
1293 TIntermTyped *initializer,
1294 TIntermNode **intermNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001295{
Olli Etuahoe7847b02015-03-16 11:56:12 +02001296 ASSERT(intermNode != nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001297 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001298
Olli Etuaho2935c582015-04-08 14:32:06 +03001299 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001300 if (type.isUnsizedArray())
1301 {
1302 type.setArraySize(initializer->getArraySize());
1303 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001304 if (!declareVariable(line, identifier, type, &variable))
1305 {
1306 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001307 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001308
Olli Etuahob0c645e2015-05-12 14:25:36 +03001309 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001310 if (symbolTable.atGlobalLevel() &&
1311 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001312 {
1313 // Error message does not completely match behavior with ESSL 1.00, but
1314 // we want to steer developers towards only using constant expressions.
1315 error(line, "global variable initializers must be constant expressions", "=");
1316 return true;
1317 }
1318 if (globalInitWarning)
1319 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001320 warning(
1321 line,
1322 "global variable initializers should be constant expressions "
1323 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1324 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001325 }
1326
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001327 //
1328 // identifier must be of type constant, a global, or a temporary
1329 //
1330 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301331 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1332 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001333 error(line, " cannot initialize this type of qualifier ",
1334 variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001335 return true;
1336 }
1337 //
1338 // test for and propagate constant
1339 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001340
Arun Patole7e7e68d2015-05-22 12:02:25 +05301341 if (qualifier == EvqConst)
1342 {
1343 if (qualifier != initializer->getType().getQualifier())
1344 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001345 std::stringstream extraInfoStream;
1346 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1347 std::string extraInfo = extraInfoStream.str();
1348 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001349 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001350 return true;
1351 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301352 if (type != initializer->getType())
1353 {
1354 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001355 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001356 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001357 return true;
1358 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001359
1360 // Save the constant folded value to the variable if possible. For example array
1361 // initializers are not folded, since that way copying the array literal to multiple places
1362 // in the shader is avoided.
1363 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1364 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301365 if (initializer->getAsConstantUnion())
1366 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001367 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001368 *intermNode = nullptr;
1369 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301370 }
1371 else if (initializer->getAsSymbolNode())
1372 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001373 const TSymbol *symbol =
1374 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1375 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001376
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001377 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001378 if (constArray)
1379 {
1380 variable->shareConstPointer(constArray);
1381 *intermNode = nullptr;
1382 return false;
1383 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001384 }
1385 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001386
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001387 TIntermSymbol *intermSymbol = intermediate.addSymbol(
1388 variable->getUniqueId(), variable->getName(), variable->getType(), line);
1389 *intermNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1390 if (*intermNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001391 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001392 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1393 return true;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001394 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001395
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001396 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001397}
1398
Jamie Madillb98c3a82015-07-23 14:26:04 -04001399TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier,
1400 bool invariant,
1401 TLayoutQualifier layoutQualifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301402 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001403{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001404 TPublicType returnType = typeSpecifier;
1405 returnType.qualifier = qualifier;
1406 returnType.invariant = invariant;
Jamie Madilla5efff92013-06-06 11:56:47 -04001407 returnType.layoutQualifier = layoutQualifier;
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001408
Olli Etuaho856c4972016-08-08 11:38:39 +03001409 checkWorkGroupSizeIsNotSpecified(typeSpecifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03001410
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001411 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001412 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001413 if (typeSpecifier.array)
1414 {
1415 error(typeSpecifier.line, "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001416 returnType.clearArrayness();
1417 }
1418
Jamie Madillb98c3a82015-07-23 14:26:04 -04001419 if (qualifier == EvqAttribute &&
1420 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001421 {
1422 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001423 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001424
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001425 if ((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1426 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1427 {
1428 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001429 }
1430 }
1431 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001432 {
Olli Etuahoabb0c382015-07-13 12:01:12 +03001433 if (!layoutQualifier.isEmpty())
1434 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001435 checkIsAtGlobalLevel(typeSpecifier.line, "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03001436 }
Olli Etuahocc36b982015-07-10 14:14:18 +03001437 if (sh::IsVarying(qualifier) || qualifier == EvqVertexIn || qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001438 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001439 checkInputOutputTypeIsValidES3(qualifier, typeSpecifier, typeSpecifier.line);
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001440 }
Martin Radev802abe02016-08-04 17:48:32 +03001441 if (qualifier == EvqComputeIn)
1442 {
1443 error(typeSpecifier.line, "'in' can be only used to specify the local group size",
1444 "in");
Martin Radev802abe02016-08-04 17:48:32 +03001445 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001446 }
1447
1448 return returnType;
1449}
1450
Olli Etuaho856c4972016-08-08 11:38:39 +03001451void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
1452 const TPublicType &type,
1453 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03001454{
1455 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
1456 if (type.type == EbtBool)
1457 {
1458 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001459 }
1460
1461 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
1462 switch (qualifier)
1463 {
1464 case EvqVertexIn:
1465 // ESSL 3.00 section 4.3.4
1466 if (type.array)
1467 {
1468 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001469 }
1470 // Vertex inputs with a struct type are disallowed in singleDeclarationErrorCheck
1471 return;
1472 case EvqFragmentOut:
1473 // ESSL 3.00 section 4.3.6
1474 if (type.isMatrix())
1475 {
1476 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001477 }
1478 // Fragment outputs with a struct type are disallowed in singleDeclarationErrorCheck
1479 return;
1480 default:
1481 break;
1482 }
1483
1484 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
1485 // restrictions.
1486 bool typeContainsIntegers =
1487 (type.type == EbtInt || type.type == EbtUInt || type.isStructureContainingType(EbtInt) ||
1488 type.isStructureContainingType(EbtUInt));
1489 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
1490 {
1491 error(qualifierLocation, "must use 'flat' interpolation here",
1492 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001493 }
1494
1495 if (type.type == EbtStruct)
1496 {
1497 // ESSL 3.00 sections 4.3.4 and 4.3.6.
1498 // These restrictions are only implied by the ESSL 3.00 spec, but
1499 // the ESSL 3.10 spec lists these restrictions explicitly.
1500 if (type.array)
1501 {
1502 error(qualifierLocation, "cannot be an array of structures",
1503 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001504 }
1505 if (type.isStructureContainingArrays())
1506 {
1507 error(qualifierLocation, "cannot be a structure containing an array",
1508 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001509 }
1510 if (type.isStructureContainingType(EbtStruct))
1511 {
1512 error(qualifierLocation, "cannot be a structure containing a structure",
1513 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001514 }
1515 if (type.isStructureContainingType(EbtBool))
1516 {
1517 error(qualifierLocation, "cannot be a structure containing a bool",
1518 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001519 }
1520 }
1521}
1522
Olli Etuahofa33d582015-04-09 14:33:12 +03001523TIntermAggregate *TParseContext::parseSingleDeclaration(TPublicType &publicType,
1524 const TSourceLoc &identifierOrTypeLocation,
1525 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001526{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001527 TIntermSymbol *symbol =
1528 intermediate.addSymbol(0, identifier, TType(publicType), identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001529
Olli Etuahobab4c082015-04-24 16:38:49 +03001530 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03001531
Olli Etuahobab4c082015-04-24 16:38:49 +03001532 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1533
1534 if (emptyDeclaration)
1535 {
1536 if (publicType.isUnsizedArray())
1537 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001538 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1539 // error. It is assumed that this applies to empty declarations as well.
1540 error(identifierOrTypeLocation, "empty array declaration needs to specify a size",
1541 identifier.c_str());
Olli Etuahobab4c082015-04-24 16:38:49 +03001542 }
1543 }
1544 else
Jamie Madill60ed9812013-06-06 11:56:46 -04001545 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001546 singleDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001547
Olli Etuaho856c4972016-08-08 11:38:39 +03001548 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001549
Olli Etuaho2935c582015-04-08 14:32:06 +03001550 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03001551 declareVariable(identifierOrTypeLocation, identifier, TType(publicType), &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04001552
1553 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001554 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001555 }
1556
Olli Etuahoe7847b02015-03-16 11:56:12 +02001557 return intermediate.makeAggregate(symbol, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001558}
1559
Olli Etuahoe7847b02015-03-16 11:56:12 +02001560TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1561 const TSourceLoc &identifierLocation,
1562 const TString &identifier,
1563 const TSourceLoc &indexLocation,
1564 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001565{
Olli Etuahofa33d582015-04-09 14:33:12 +03001566 mDeferredSingleDeclarationErrorCheck = false;
1567
Olli Etuaho383b7912016-08-05 11:22:59 +03001568 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001569
Olli Etuaho856c4972016-08-08 11:38:39 +03001570 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001571
Olli Etuaho856c4972016-08-08 11:38:39 +03001572 if (!checkIsValidTypeForArray(indexLocation, publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001573 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001574 checkIsValidQualifierForArray(indexLocation, publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001575 }
1576
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001577 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001578
Olli Etuaho856c4972016-08-08 11:38:39 +03001579 unsigned int size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001580 // Make the type an array even if size check failed.
1581 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1582 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001583
Olli Etuaho2935c582015-04-08 14:32:06 +03001584 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03001585 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04001586
Olli Etuahoe7847b02015-03-16 11:56:12 +02001587 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001588 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001589 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001590
Olli Etuahoe7847b02015-03-16 11:56:12 +02001591 return intermediate.makeAggregate(symbol, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001592}
1593
Jamie Madill06145232015-05-13 13:10:01 -04001594TIntermAggregate *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001595 const TSourceLoc &identifierLocation,
1596 const TString &identifier,
1597 const TSourceLoc &initLocation,
1598 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001599{
Olli Etuahofa33d582015-04-09 14:33:12 +03001600 mDeferredSingleDeclarationErrorCheck = false;
1601
Olli Etuaho383b7912016-08-05 11:22:59 +03001602 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001603
Olli Etuahoe7847b02015-03-16 11:56:12 +02001604 TIntermNode *intermNode = nullptr;
1605 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04001606 {
1607 //
1608 // Build intermediate representation
1609 //
Olli Etuahoe7847b02015-03-16 11:56:12 +02001610 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001611 }
1612 else
1613 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001614 return nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001615 }
1616}
1617
Jamie Madillb98c3a82015-07-23 14:26:04 -04001618TIntermAggregate *TParseContext::parseSingleArrayInitDeclaration(
1619 TPublicType &publicType,
1620 const TSourceLoc &identifierLocation,
1621 const TString &identifier,
1622 const TSourceLoc &indexLocation,
1623 TIntermTyped *indexExpression,
1624 const TSourceLoc &initLocation,
1625 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001626{
1627 mDeferredSingleDeclarationErrorCheck = false;
1628
Olli Etuaho383b7912016-08-05 11:22:59 +03001629 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001630
Olli Etuaho856c4972016-08-08 11:38:39 +03001631 if (!checkIsValidTypeForArray(indexLocation, publicType))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001632 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001633 checkIsValidQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001634 }
1635
1636 TPublicType arrayType(publicType);
1637
Olli Etuaho856c4972016-08-08 11:38:39 +03001638 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001639 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
1640 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03001641 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001642 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001643 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001644 }
1645 // Make the type an array even if size check failed.
1646 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1647 arrayType.setArraySize(size);
1648
1649 // initNode will correspond to the whole of "type b[n] = initializer".
1650 TIntermNode *initNode = nullptr;
1651 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1652 {
1653 return initNode ? intermediate.makeAggregate(initNode, initLocation) : nullptr;
1654 }
1655 else
1656 {
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001657 return nullptr;
1658 }
1659}
1660
Olli Etuahoe7847b02015-03-16 11:56:12 +02001661TIntermAggregate *TParseContext::parseInvariantDeclaration(const TSourceLoc &invariantLoc,
Jamie Madill47e3ec02014-08-20 16:38:33 -04001662 const TSourceLoc &identifierLoc,
1663 const TString *identifier,
1664 const TSymbol *symbol)
1665{
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001666 // invariant declaration
Olli Etuaho856c4972016-08-08 11:38:39 +03001667 checkIsAtGlobalLevel(invariantLoc, "invariant varying");
Jamie Madill47e3ec02014-08-20 16:38:33 -04001668
1669 if (!symbol)
1670 {
1671 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001672 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001673 }
1674 else
1675 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001676 const TString kGlFrontFacing("gl_FrontFacing");
1677 if (*identifier == kGlFrontFacing)
1678 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001679 error(identifierLoc, "identifier should not be declared as invariant",
1680 identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001681 return nullptr;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001682 }
Jamie Madill2c433252014-12-03 12:36:54 -05001683 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001684 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1685 ASSERT(variable);
1686 const TType &type = variable->getType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04001687 TIntermSymbol *intermSymbol =
1688 intermediate.addSymbol(variable->getUniqueId(), *identifier, type, identifierLoc);
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001689
1690 TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc);
1691 aggregate->setOp(EOpInvariantDeclaration);
1692 return aggregate;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001693 }
1694}
1695
Jamie Madillb98c3a82015-07-23 14:26:04 -04001696TIntermAggregate *TParseContext::parseDeclarator(TPublicType &publicType,
1697 TIntermAggregate *aggregateDeclaration,
1698 const TSourceLoc &identifierLocation,
1699 const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001700{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001701 // If the declaration starting this declarator list was empty (example: int,), some checks were
1702 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001703 if (mDeferredSingleDeclarationErrorCheck)
1704 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001705 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03001706 mDeferredSingleDeclarationErrorCheck = false;
1707 }
1708
Olli Etuaho856c4972016-08-08 11:38:39 +03001709 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04001710
Olli Etuaho856c4972016-08-08 11:38:39 +03001711 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04001712
Olli Etuaho2935c582015-04-08 14:32:06 +03001713 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03001714 declareVariable(identifierLocation, identifier, TType(publicType), &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001715
Jamie Madillb98c3a82015-07-23 14:26:04 -04001716 TIntermSymbol *symbol =
1717 intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001718 if (variable && symbol)
Jamie Madill502d66f2013-06-20 11:55:52 -04001719 symbol->setId(variable->getUniqueId());
1720
Olli Etuahoe7847b02015-03-16 11:56:12 +02001721 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001722}
1723
Jamie Madillb98c3a82015-07-23 14:26:04 -04001724TIntermAggregate *TParseContext::parseArrayDeclarator(TPublicType &publicType,
1725 TIntermAggregate *aggregateDeclaration,
1726 const TSourceLoc &identifierLocation,
1727 const TString &identifier,
1728 const TSourceLoc &arrayLocation,
1729 TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001730{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001731 // If the declaration starting this declarator list was empty (example: int,), some checks were
1732 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001733 if (mDeferredSingleDeclarationErrorCheck)
1734 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001735 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03001736 mDeferredSingleDeclarationErrorCheck = false;
1737 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001738
Olli Etuaho856c4972016-08-08 11:38:39 +03001739 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04001740
Olli Etuaho856c4972016-08-08 11:38:39 +03001741 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04001742
Olli Etuaho856c4972016-08-08 11:38:39 +03001743 if (!checkIsValidTypeForArray(arrayLocation, publicType) &&
1744 !checkIsValidQualifierForArray(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001745 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001746 TType arrayType = TType(publicType);
Olli Etuaho856c4972016-08-08 11:38:39 +03001747 unsigned int size = checkIsValidArraySize(arrayLocation, indexExpression);
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001748 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001749
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001750 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03001751 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04001752
Jamie Madillb98c3a82015-07-23 14:26:04 -04001753 TIntermSymbol *symbol =
1754 intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001755 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001756 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001757
1758 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001759 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001760
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001761 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001762}
1763
Jamie Madillb98c3a82015-07-23 14:26:04 -04001764TIntermAggregate *TParseContext::parseInitDeclarator(const TPublicType &publicType,
1765 TIntermAggregate *aggregateDeclaration,
1766 const TSourceLoc &identifierLocation,
1767 const TString &identifier,
1768 const TSourceLoc &initLocation,
1769 TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001770{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001771 // If the declaration starting this declarator list was empty (example: int,), some checks were
1772 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001773 if (mDeferredSingleDeclarationErrorCheck)
1774 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001775 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03001776 mDeferredSingleDeclarationErrorCheck = false;
1777 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001778
Olli Etuaho856c4972016-08-08 11:38:39 +03001779 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04001780
Olli Etuahoe7847b02015-03-16 11:56:12 +02001781 TIntermNode *intermNode = nullptr;
1782 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04001783 {
1784 //
1785 // build the intermediate representation
1786 //
1787 if (intermNode)
1788 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001789 return intermediate.growAggregate(aggregateDeclaration, intermNode, initLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001790 }
1791 else
1792 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001793 return aggregateDeclaration;
Jamie Madill502d66f2013-06-20 11:55:52 -04001794 }
1795 }
1796 else
1797 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001798 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001799 }
1800}
1801
Jamie Madill06145232015-05-13 13:10:01 -04001802TIntermAggregate *TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001803 TIntermAggregate *aggregateDeclaration,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301804 const TSourceLoc &identifierLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001805 const TString &identifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301806 const TSourceLoc &indexLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001807 TIntermTyped *indexExpression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001808 const TSourceLoc &initLocation,
1809 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001810{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001811 // If the declaration starting this declarator list was empty (example: int,), some checks were
1812 // not performed.
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001813 if (mDeferredSingleDeclarationErrorCheck)
1814 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001815 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001816 mDeferredSingleDeclarationErrorCheck = false;
1817 }
1818
Olli Etuaho856c4972016-08-08 11:38:39 +03001819 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001820
Olli Etuaho856c4972016-08-08 11:38:39 +03001821 if (!checkIsValidTypeForArray(indexLocation, publicType))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001822 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001823 checkIsValidQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001824 }
1825
1826 TPublicType arrayType(publicType);
1827
Olli Etuaho856c4972016-08-08 11:38:39 +03001828 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001829 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
1830 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03001831 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001832 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001833 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001834 }
1835 // Make the type an array even if size check failed.
1836 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1837 arrayType.setArraySize(size);
1838
1839 // initNode will correspond to the whole of "b[n] = initializer".
1840 TIntermNode *initNode = nullptr;
1841 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1842 {
1843 if (initNode)
1844 {
1845 return intermediate.growAggregate(aggregateDeclaration, initNode, initLocation);
1846 }
1847 else
1848 {
1849 return aggregateDeclaration;
1850 }
1851 }
1852 else
1853 {
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001854 return nullptr;
1855 }
1856}
1857
Jamie Madilla295edf2013-06-06 11:56:48 -04001858void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1859{
Jamie Madilla295edf2013-06-06 11:56:48 -04001860 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04001861
1862 // It should never be the case, but some strange parser errors can send us here.
1863 if (layoutQualifier.isEmpty())
1864 {
1865 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04001866 return;
1867 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001868
Martin Radev802abe02016-08-04 17:48:32 +03001869 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04001870 {
Martin Radev802abe02016-08-04 17:48:32 +03001871 error(typeQualifier.line, "invalid combination:", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04001872 return;
1873 }
1874
Martin Radev802abe02016-08-04 17:48:32 +03001875 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04001876 {
Martin Radev802abe02016-08-04 17:48:32 +03001877 if (mComputeShaderLocalSizeDeclared &&
1878 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
1879 {
1880 error(typeQualifier.line, "Work group size does not match the previous declaration",
1881 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03001882 return;
1883 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001884
Martin Radev802abe02016-08-04 17:48:32 +03001885 if (mShaderVersion < 310)
1886 {
1887 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03001888 return;
1889 }
Jamie Madill099c0f32013-06-20 11:55:52 -04001890
Martin Radev802abe02016-08-04 17:48:32 +03001891 if (!layoutQualifier.isGroupSizeSpecified())
1892 {
1893 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03001894 return;
1895 }
1896
1897 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
1898 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
1899
1900 const TConstantUnion *maxComputeWorkGroupSizeData =
1901 maxComputeWorkGroupSize->getConstPointer();
1902
1903 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
1904 {
1905 if (layoutQualifier.localSize[i] != -1)
1906 {
1907 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
1908 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
1909 if (mComputeShaderLocalSize[i] < 1 ||
1910 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
1911 {
1912 std::stringstream errorMessageStream;
1913 errorMessageStream << "Value must be at least 1 and no greater than "
1914 << maxComputeWorkGroupSizeValue;
1915 const std::string &errorMessage = errorMessageStream.str();
1916
1917 error(typeQualifier.line, "invalid value:", getLocalSizeString(i),
1918 errorMessage.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001919 return;
1920 }
1921 }
1922 }
1923
1924 mComputeShaderLocalSizeDeclared = true;
1925 }
1926 else
Jamie Madill1566ef72013-06-20 11:55:54 -04001927 {
Martin Radev802abe02016-08-04 17:48:32 +03001928
Olli Etuaho856c4972016-08-08 11:38:39 +03001929 if (checkWorkGroupSizeIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03001930 {
Martin Radev802abe02016-08-04 17:48:32 +03001931 return;
1932 }
1933
1934 if (typeQualifier.qualifier != EvqUniform)
1935 {
1936 error(typeQualifier.line, "invalid qualifier:",
1937 getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
Martin Radev802abe02016-08-04 17:48:32 +03001938 return;
1939 }
1940
1941 if (mShaderVersion < 300)
1942 {
1943 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
1944 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03001945 return;
1946 }
1947
Olli Etuaho856c4972016-08-08 11:38:39 +03001948 checkLocationIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03001949
1950 if (layoutQualifier.matrixPacking != EmpUnspecified)
1951 {
1952 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
1953 }
1954
1955 if (layoutQualifier.blockStorage != EbsUnspecified)
1956 {
1957 mDefaultBlockStorage = layoutQualifier.blockStorage;
1958 }
Jamie Madill1566ef72013-06-20 11:55:54 -04001959 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001960}
1961
Olli Etuahoee63f5d2016-01-04 11:34:54 +02001962TIntermAggregate *TParseContext::addFunctionPrototypeDeclaration(const TFunction &function,
1963 const TSourceLoc &location)
1964{
Olli Etuaho5d653182016-01-04 14:43:28 +02001965 // Note: symbolTableFunction could be the same as function if this is the first declaration.
1966 // Either way the instance in the symbol table is used to track whether the function is declared
1967 // multiple times.
1968 TFunction *symbolTableFunction =
1969 static_cast<TFunction *>(symbolTable.find(function.getMangledName(), getShaderVersion()));
1970 if (symbolTableFunction->hasPrototypeDeclaration() && mShaderVersion == 100)
1971 {
1972 // ESSL 1.00.17 section 4.2.7.
1973 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
1974 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02001975 }
1976 symbolTableFunction->setHasPrototypeDeclaration();
1977
Olli Etuahoee63f5d2016-01-04 11:34:54 +02001978 TIntermAggregate *prototype = new TIntermAggregate;
1979 prototype->setType(function.getReturnType());
1980 prototype->setName(function.getMangledName());
1981 prototype->setFunctionId(function.getUniqueId());
1982
1983 for (size_t i = 0; i < function.getParamCount(); i++)
1984 {
1985 const TConstParameter &param = function.getParam(i);
1986 if (param.name != 0)
1987 {
1988 TVariable variable(param.name, *param.type);
1989
1990 TIntermSymbol *paramSymbol = intermediate.addSymbol(
1991 variable.getUniqueId(), variable.getName(), variable.getType(), location);
1992 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
1993 }
1994 else
1995 {
1996 TIntermSymbol *paramSymbol = intermediate.addSymbol(0, "", *param.type, location);
1997 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
1998 }
1999 }
2000
2001 prototype->setOp(EOpPrototype);
2002
2003 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002004
2005 if (!symbolTable.atGlobalLevel())
2006 {
2007 // ESSL 3.00.4 section 4.2.4.
2008 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002009 }
2010
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002011 return prototype;
2012}
2013
2014TIntermAggregate *TParseContext::addFunctionDefinition(const TFunction &function,
2015 TIntermAggregate *functionPrototype,
2016 TIntermAggregate *functionBody,
2017 const TSourceLoc &location)
2018{
2019 //?? Check that all paths return a value if return type != void ?
2020 // May be best done as post process phase on intermediate code
2021 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
2022 {
2023 error(location, "function does not return a value:", "", function.getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002024 }
2025
2026 TIntermAggregate *aggregate =
2027 intermediate.growAggregate(functionPrototype, functionBody, location);
2028 intermediate.setAggregateOperator(aggregate, EOpFunction, location);
2029 aggregate->setName(function.getMangledName().c_str());
2030 aggregate->setType(function.getReturnType());
2031 aggregate->setFunctionId(function.getUniqueId());
2032
2033 symbolTable.pop();
2034 return aggregate;
2035}
2036
Jamie Madill185fb402015-06-12 15:48:48 -04002037void TParseContext::parseFunctionPrototype(const TSourceLoc &location,
2038 TFunction *function,
2039 TIntermAggregate **aggregateOut)
2040{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002041 const TSymbol *builtIn =
2042 symbolTable.findBuiltIn(function->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002043
2044 if (builtIn)
2045 {
2046 error(location, "built-in functions cannot be redefined", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002047 }
2048
Jamie Madillb98c3a82015-07-23 14:26:04 -04002049 TFunction *prevDec =
2050 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Jamie Madill185fb402015-06-12 15:48:48 -04002051 //
2052 // Note: 'prevDec' could be 'function' if this is the first time we've seen function
2053 // as it would have just been put in the symbol table. Otherwise, we're looking up
2054 // an earlier occurance.
2055 //
2056 if (prevDec->isDefined())
2057 {
2058 // Then this function already has a body.
2059 error(location, "function already has a body", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002060 }
2061 prevDec->setDefined();
2062 //
2063 // Overload the unique ID of the definition to be the same unique ID as the declaration.
2064 // Eventually we will probably want to have only a single definition and just swap the
2065 // arguments to be the definition's arguments.
2066 //
2067 function->setUniqueId(prevDec->getUniqueId());
2068
2069 // Raise error message if main function takes any parameters or return anything other than void
2070 if (function->getName() == "main")
2071 {
2072 if (function->getParamCount() > 0)
2073 {
2074 error(location, "function cannot take any parameter(s)", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002075 }
2076 if (function->getReturnType().getBasicType() != EbtVoid)
2077 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002078 error(location, "", function->getReturnType().getBasicString(),
2079 "main function cannot return a value");
Jamie Madill185fb402015-06-12 15:48:48 -04002080 }
2081 }
2082
2083 //
2084 // Remember the return type for later checking for RETURN statements.
2085 //
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002086 mCurrentFunctionType = &(prevDec->getReturnType());
2087 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04002088
2089 //
2090 // Insert parameters into the symbol table.
2091 // If the parameter has no name, it's not an error, just don't insert it
2092 // (could be used for unused args).
2093 //
2094 // Also, accumulate the list of parameters into the HIL, so lower level code
2095 // knows where to find parameters.
2096 //
2097 TIntermAggregate *paramNodes = new TIntermAggregate;
2098 for (size_t i = 0; i < function->getParamCount(); i++)
2099 {
2100 const TConstParameter &param = function->getParam(i);
2101 if (param.name != 0)
2102 {
2103 TVariable *variable = new TVariable(param.name, *param.type);
2104 //
2105 // Insert the parameters with name in the symbol table.
2106 //
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002107 if (!symbolTable.declare(variable))
2108 {
Jamie Madill185fb402015-06-12 15:48:48 -04002109 error(location, "redefinition", variable->getName().c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002110 paramNodes = intermediate.growAggregate(
2111 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
2112 continue;
Jamie Madill185fb402015-06-12 15:48:48 -04002113 }
2114
2115 //
2116 // Add the parameter to the HIL
2117 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002118 TIntermSymbol *symbol = intermediate.addSymbol(
2119 variable->getUniqueId(), variable->getName(), variable->getType(), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002120
2121 paramNodes = intermediate.growAggregate(paramNodes, symbol, location);
2122 }
2123 else
2124 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002125 paramNodes = intermediate.growAggregate(
2126 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002127 }
2128 }
2129 intermediate.setAggregateOperator(paramNodes, EOpParameters, location);
2130 *aggregateOut = paramNodes;
2131 setLoopNestingLevel(0);
2132}
2133
Jamie Madillb98c3a82015-07-23 14:26:04 -04002134TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002135{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002136 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002137 // We don't know at this point whether this is a function definition or a prototype.
2138 // The definition production code will check for redefinitions.
2139 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002140 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002141 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
2142 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002143 //
2144 TFunction *prevDec =
2145 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302146
2147 if (getShaderVersion() >= 300 && symbolTable.hasUnmangledBuiltIn(function->getName().c_str()))
2148 {
2149 // With ESSL 3.00, names of built-in functions cannot be redeclared as functions.
2150 // Therefore overloading or redefining builtin functions is an error.
2151 error(location, "Name of a built-in function cannot be redeclared as function",
2152 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302153 }
2154 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002155 {
2156 if (prevDec->getReturnType() != function->getReturnType())
2157 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002158 error(location, "overloaded functions must have the same return type",
Jamie Madill185fb402015-06-12 15:48:48 -04002159 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04002160 }
2161 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2162 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002163 if (prevDec->getParam(i).type->getQualifier() !=
2164 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002165 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002166 error(location, "overloaded functions must have the same parameter qualifiers",
Jamie Madill185fb402015-06-12 15:48:48 -04002167 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04002168 }
2169 }
2170 }
2171
2172 //
2173 // Check for previously declared variables using the same name.
2174 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002175 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002176 if (prevSym)
2177 {
2178 if (!prevSym->isFunction())
2179 {
2180 error(location, "redefinition", function->getName().c_str(), "function");
Jamie Madill185fb402015-06-12 15:48:48 -04002181 }
2182 }
2183 else
2184 {
2185 // Insert the unmangled name to detect potential future redefinition as a variable.
Jamie Madillb98c3a82015-07-23 14:26:04 -04002186 TFunction *newFunction =
2187 new TFunction(NewPoolTString(function->getName().c_str()), &function->getReturnType());
Jamie Madill185fb402015-06-12 15:48:48 -04002188 symbolTable.getOuterLevel()->insertUnmangled(newFunction);
2189 }
2190
2191 // We're at the inner scope level of the function's arguments and body statement.
2192 // Add the function prototype to the surrounding scope instead.
2193 symbolTable.getOuterLevel()->insert(function);
2194
2195 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002196 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2197 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002198 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2199 //
2200 return function;
2201}
2202
Olli Etuaho9de84a52016-06-14 17:36:01 +03002203TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
2204 const TString *name,
2205 const TSourceLoc &location)
2206{
2207 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
2208 {
2209 error(location, "no qualifiers allowed for function return",
2210 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03002211 }
2212 if (!type.layoutQualifier.isEmpty())
2213 {
2214 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03002215 }
2216 // make sure a sampler is not involved as well...
Olli Etuaho856c4972016-08-08 11:38:39 +03002217 checkIsNotSampler(location, type, "samplers can't be function return values");
Olli Etuahoe29324f2016-06-15 10:58:03 +03002218 if (mShaderVersion < 300)
2219 {
2220 // Array return values are forbidden, but there's also no valid syntax for declaring array
2221 // return values in ESSL 1.00.
2222 ASSERT(type.arraySize == 0 || mDiagnostics.numErrors() > 0);
2223
2224 if (type.isStructureContainingArrays())
2225 {
2226 // ESSL 1.00.17 section 6.1 Function Definitions
2227 error(location, "structures containing arrays can't be function return values",
2228 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03002229 }
2230 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03002231
2232 // Add the function as a prototype after parsing it (we do not support recursion)
2233 return new TFunction(name, new TType(type));
2234}
2235
Jamie Madill06145232015-05-13 13:10:01 -04002236TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002237{
Jamie Madill06145232015-05-13 13:10:01 -04002238 TPublicType publicType = publicTypeIn;
Olli Etuahobd163f62015-11-13 12:15:38 +02002239 if (publicType.isStructSpecifier)
2240 {
2241 error(publicType.line, "constructor can't be a structure definition",
2242 getBasicString(publicType.type));
Olli Etuahobd163f62015-11-13 12:15:38 +02002243 }
2244
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002245 TOperator op = EOpNull;
2246 if (publicType.userDef)
2247 {
2248 op = EOpConstructStruct;
2249 }
2250 else
2251 {
Geoff Lang156d7192016-07-21 16:11:00 -04002252 op = sh::TypeToConstructorOperator(TType(publicType));
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002253 if (op == EOpNull)
2254 {
2255 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002256 publicType.type = EbtFloat;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002257 op = EOpConstructFloat;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002258 }
2259 }
2260
2261 TString tempString;
Dmitry Skiba7f17a502015-06-22 15:08:39 -07002262 const TType *type = new TType(publicType);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002263 return new TFunction(&tempString, type, op);
2264}
2265
Jamie Madillb98c3a82015-07-23 14:26:04 -04002266// This function is used to test for the correctness of the parameters passed to various constructor
2267// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002268//
Olli Etuaho856c4972016-08-08 11:38:39 +03002269// Returns a node to add to the tree regardless of if an error was generated or not.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002270//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002271TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002272 TOperator op,
2273 TFunction *fnCall,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302274 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002275{
Olli Etuaho856c4972016-08-08 11:38:39 +03002276 TType type = fnCall->getReturnType();
2277 if (type.isUnsizedArray())
2278 {
2279 type.setArraySize(static_cast<unsigned int>(fnCall->getParamCount()));
2280 }
2281 bool constType = true;
2282 for (size_t i = 0; i < fnCall->getParamCount(); ++i)
2283 {
2284 const TConstParameter &param = fnCall->getParam(i);
2285 if (param.type->getQualifier() != EvqConst)
2286 constType = false;
2287 }
2288 if (constType)
2289 type.setQualifier(EvqConst);
2290
2291 if (checkConstructorArguments(line, arguments, *fnCall, op, type))
2292 {
2293 TIntermTyped *dummyNode = intermediate.setAggregateOperator(nullptr, op, line);
2294 dummyNode->setType(type);
2295 return dummyNode;
2296 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002297 TIntermAggregate *constructor = arguments->getAsAggregate();
2298 ASSERT(constructor != nullptr);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002299
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002300 // Turn the argument list itself into a constructor
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002301 constructor->setOp(op);
2302 constructor->setLine(line);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002303 ASSERT(constructor->isConstructor());
2304
2305 // Need to set type before setPrecisionFromChildren() because bool doesn't have precision.
Olli Etuaho856c4972016-08-08 11:38:39 +03002306 constructor->setType(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002307
Olli Etuaho21203702014-11-13 16:16:21 +02002308 // Structs should not be precision qualified, the individual members may be.
2309 // Built-in types on the other hand should be precision qualified.
2310 if (op != EOpConstructStruct)
2311 {
2312 constructor->setPrecisionFromChildren();
Olli Etuaho856c4972016-08-08 11:38:39 +03002313 type.setPrecision(constructor->getPrecision());
Olli Etuaho21203702014-11-13 16:16:21 +02002314 }
2315
Olli Etuaho856c4972016-08-08 11:38:39 +03002316 constructor->setType(type);
2317
Olli Etuaho1d122782015-11-06 15:35:17 +02002318 TIntermTyped *constConstructor = intermediate.foldAggregateBuiltIn(constructor);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002319 if (constConstructor)
2320 {
2321 return constConstructor;
2322 }
2323
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002324 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002325}
2326
Olli Etuaho90892fb2016-07-14 14:44:51 +03002327// This function returns vector field(s) being accessed from a constant vector.
2328TIntermConstantUnion *TParseContext::foldVectorSwizzle(TVectorFields &fields,
2329 TIntermConstantUnion *baseNode,
2330 const TSourceLoc &location)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002331{
Olli Etuaho90892fb2016-07-14 14:44:51 +03002332 const TConstantUnion *unionArray = baseNode->getUnionArrayPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002333 ASSERT(unionArray);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002334
Arun Patole7e7e68d2015-05-22 12:02:25 +05302335 TConstantUnion *constArray = new TConstantUnion[fields.num];
Olli Etuaho90892fb2016-07-14 14:44:51 +03002336 const auto &type = baseNode->getType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002337
Arun Patole7e7e68d2015-05-22 12:02:25 +05302338 for (int i = 0; i < fields.num; i++)
2339 {
Olli Etuaho90892fb2016-07-14 14:44:51 +03002340 // Out-of-range indices should already be checked.
2341 ASSERT(fields.offsets[i] < type.getNominalSize());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002342 constArray[i] = unionArray[fields.offsets[i]];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302343 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03002344 return intermediate.addConstantUnion(constArray, type, location);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002345}
2346
Olli Etuaho90892fb2016-07-14 14:44:51 +03002347// This function returns the column vector being accessed from a constant matrix.
2348TIntermConstantUnion *TParseContext::foldMatrixSubscript(int index,
2349 TIntermConstantUnion *baseNode,
2350 const TSourceLoc &location)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002351{
Olli Etuaho90892fb2016-07-14 14:44:51 +03002352 ASSERT(index < baseNode->getType().getCols());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002353
Olli Etuaho90892fb2016-07-14 14:44:51 +03002354 const TConstantUnion *unionArray = baseNode->getUnionArrayPointer();
2355 int size = baseNode->getType().getRows();
2356 return intermediate.addConstantUnion(&unionArray[size * index], baseNode->getType(), location);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002357}
2358
Olli Etuaho90892fb2016-07-14 14:44:51 +03002359// This function returns an element of an array accessed from a constant array.
2360TIntermConstantUnion *TParseContext::foldArraySubscript(int index,
2361 TIntermConstantUnion *baseNode,
2362 const TSourceLoc &location)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002363{
Olli Etuaho856c4972016-08-08 11:38:39 +03002364 ASSERT(index < static_cast<int>(baseNode->getArraySize()));
Olli Etuaho90892fb2016-07-14 14:44:51 +03002365
2366 TType arrayElementType = baseNode->getType();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002367 arrayElementType.clearArrayness();
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002368 size_t arrayElementSize = arrayElementType.getObjectSize();
Olli Etuaho90892fb2016-07-14 14:44:51 +03002369 const TConstantUnion *unionArray = baseNode->getUnionArrayPointer();
2370 return intermediate.addConstantUnion(&unionArray[arrayElementSize * index], baseNode->getType(),
2371 location);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002372}
2373
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002374//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002375// This function returns the value of a particular field inside a constant structure from the symbol
2376// table.
2377// If there is an embedded/nested struct, it appropriately calls addConstStructNested or
2378// addConstStructFromAggr function and returns the parse-tree with the values of the embedded/nested
2379// struct.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002380//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002381TIntermTyped *TParseContext::addConstStruct(const TString &identifier,
2382 TIntermTyped *node,
2383 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002384{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302385 const TFieldList &fields = node->getType().getStruct()->fields();
Jamie Madillb98c3a82015-07-23 14:26:04 -04002386 size_t instanceSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002387
Arun Patole7e7e68d2015-05-22 12:02:25 +05302388 for (size_t index = 0; index < fields.size(); ++index)
2389 {
2390 if (fields[index]->name() == identifier)
2391 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002392 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302393 }
2394 else
2395 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002396 instanceSize += fields[index]->type()->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002397 }
2398 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002399
Jamie Madill94bf7f22013-07-08 13:31:15 -04002400 TIntermTyped *typedNode;
2401 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
Arun Patole7e7e68d2015-05-22 12:02:25 +05302402 if (tempConstantNode)
2403 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002404 const TConstantUnion *constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002405
Jamie Madillb98c3a82015-07-23 14:26:04 -04002406 // type will be changed in the calling function
2407 typedNode = intermediate.addConstantUnion(constArray + instanceSize,
2408 tempConstantNode->getType(), line);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302409 }
2410 else
2411 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002412 error(line, "Cannot offset into the structure", "Error");
Olli Etuaho383b7912016-08-05 11:22:59 +03002413 return nullptr;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002414 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002415
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002416 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002417}
2418
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002419//
2420// Interface/uniform blocks
2421//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002422TIntermAggregate *TParseContext::addInterfaceBlock(const TPublicType &typeQualifier,
2423 const TSourceLoc &nameLine,
2424 const TString &blockName,
2425 TFieldList *fieldList,
2426 const TString *instanceName,
2427 const TSourceLoc &instanceLine,
2428 TIntermTyped *arrayIndex,
2429 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002430{
Olli Etuaho856c4972016-08-08 11:38:39 +03002431 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002432
2433 if (typeQualifier.qualifier != EvqUniform)
2434 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302435 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
2436 "interface blocks must be uniform");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002437 }
2438
Jamie Madill099c0f32013-06-20 11:55:52 -04002439 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03002440 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002441
Jamie Madill099c0f32013-06-20 11:55:52 -04002442 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2443 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002444 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002445 }
2446
Jamie Madill1566ef72013-06-20 11:55:54 -04002447 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2448 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002449 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002450 }
2451
Olli Etuaho856c4972016-08-08 11:38:39 +03002452 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002453
Arun Patole7e7e68d2015-05-22 12:02:25 +05302454 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2455 if (!symbolTable.declare(blockNameSymbol))
2456 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002457 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002458 }
2459
Jamie Madill98493dd2013-07-08 14:39:03 -04002460 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302461 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2462 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002463 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302464 TType *fieldType = field->type();
2465 if (IsSampler(fieldType->getBasicType()))
2466 {
2467 error(field->line(), "unsupported type", fieldType->getBasicString(),
2468 "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002469 }
2470
Jamie Madill98493dd2013-07-08 14:39:03 -04002471 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002472 switch (qualifier)
2473 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002474 case EvqGlobal:
2475 case EvqUniform:
2476 break;
2477 default:
2478 error(field->line(), "invalid qualifier on interface block member",
2479 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04002480 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002481 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002482
2483 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002484 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03002485 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
Jamie Madill099c0f32013-06-20 11:55:52 -04002486
Jamie Madill98493dd2013-07-08 14:39:03 -04002487 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002488 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002489 error(field->line(), "invalid layout qualifier:",
2490 getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04002491 }
2492
Jamie Madill98493dd2013-07-08 14:39:03 -04002493 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002494 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002495 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002496 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002497 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002498 {
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002499 warning(field->line(), "extraneous layout qualifier:",
Jamie Madillb98c3a82015-07-23 14:26:04 -04002500 getMatrixPackingString(fieldLayoutQualifier.matrixPacking),
2501 "only has an effect on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04002502 }
2503
Jamie Madill98493dd2013-07-08 14:39:03 -04002504 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002505 }
2506
Jamie Madill98493dd2013-07-08 14:39:03 -04002507 // add array index
Olli Etuaho856c4972016-08-08 11:38:39 +03002508 unsigned int arraySize = 0;
2509 if (arrayIndex != nullptr)
Jamie Madill98493dd2013-07-08 14:39:03 -04002510 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002511 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04002512 }
2513
Jamie Madillb98c3a82015-07-23 14:26:04 -04002514 TInterfaceBlock *interfaceBlock =
2515 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2516 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
2517 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002518
2519 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04002520 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002521
Jamie Madill98493dd2013-07-08 14:39:03 -04002522 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002523 {
2524 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002525 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2526 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002527 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302528 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002529
2530 // set parent pointer of the field variable
2531 fieldType->setInterfaceBlock(interfaceBlock);
2532
Arun Patole7e7e68d2015-05-22 12:02:25 +05302533 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002534 fieldVariable->setQualifier(typeQualifier.qualifier);
2535
Arun Patole7e7e68d2015-05-22 12:02:25 +05302536 if (!symbolTable.declare(fieldVariable))
2537 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002538 error(field->line(), "redefinition", field->name().c_str(),
2539 "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002540 }
2541 }
2542 }
2543 else
2544 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002545 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03002546
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002547 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302548 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002549 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002550
Arun Patole7e7e68d2015-05-22 12:02:25 +05302551 if (!symbolTable.declare(instanceTypeDef))
2552 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002553 error(instanceLine, "redefinition", instanceName->c_str(),
2554 "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002555 }
2556
Jamie Madillb98c3a82015-07-23 14:26:04 -04002557 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002558 symbolName = instanceTypeDef->getName();
2559 }
2560
Jamie Madillb98c3a82015-07-23 14:26:04 -04002561 TIntermAggregate *aggregate = intermediate.makeAggregate(
2562 intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line),
2563 nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002564 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04002565
2566 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002567 return aggregate;
2568}
2569
Olli Etuaho383b7912016-08-05 11:22:59 +03002570void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002571{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002572 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002573
2574 // Embedded structure definitions are not supported per GLSL ES spec.
2575 // They aren't allowed in GLSL either, but we need to detect this here
2576 // so we don't rely on the GLSL compiler to catch it.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302577 if (mStructNestingLevel > 1)
2578 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002579 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002580 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002581}
2582
2583void TParseContext::exitStructDeclaration()
2584{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002585 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002586}
2587
Jamie Madillb98c3a82015-07-23 14:26:04 -04002588namespace
2589{
kbr@chromium.org476541f2011-10-27 21:14:51 +00002590const int kWebGLMaxStructNesting = 4;
2591
2592} // namespace
2593
Arun Patole7e7e68d2015-05-22 12:02:25 +05302594bool TParseContext::structNestingErrorCheck(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002595{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302596 if (!IsWebGLBasedSpec(mShaderSpec))
2597 {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002598 return false;
2599 }
2600
Arun Patole7e7e68d2015-05-22 12:02:25 +05302601 if (field.type()->getBasicType() != EbtStruct)
2602 {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002603 return false;
2604 }
2605
2606 // We're already inside a structure definition at this point, so add
2607 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302608 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2609 {
Jamie Madill41a49272014-03-18 16:10:13 -04002610 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002611 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
2612 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04002613 std::string reason = reasonStream.str();
2614 error(line, reason.c_str(), field.name().c_str(), "");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002615 return true;
2616 }
2617
2618 return false;
2619}
2620
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002621//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002622// Parse an array index expression
2623//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002624TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
2625 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302626 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002627{
2628 TIntermTyped *indexedExpression = NULL;
2629
2630 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2631 {
2632 if (baseExpression->getAsSymbolNode())
2633 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302634 error(location, " left of '[' is not of type array, matrix, or vector ",
2635 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002636 }
2637 else
2638 {
2639 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2640 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002641 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002642
Jamie Madill21c1e452014-12-29 11:33:41 -05002643 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2644
Olli Etuaho36b05142015-11-12 13:10:42 +02002645 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
2646 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
2647 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
2648 // index is a constant expression.
2649 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
2650 {
2651 if (baseExpression->isInterfaceBlock())
2652 {
2653 error(
2654 location, "", "[",
2655 "array indexes for interface blocks arrays must be constant integral expressions");
Olli Etuaho36b05142015-11-12 13:10:42 +02002656 }
2657 else if (baseExpression->getQualifier() == EvqFragmentOut)
2658 {
2659 error(location, "", "[",
2660 "array indexes for fragment outputs must be constant integral expressions");
Olli Etuaho36b05142015-11-12 13:10:42 +02002661 }
Olli Etuaho3e960462015-11-12 15:58:39 +02002662 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
2663 {
2664 error(location, "", "[", "array index for gl_FragData must be constant zero");
Olli Etuaho3e960462015-11-12 15:58:39 +02002665 }
Olli Etuaho36b05142015-11-12 13:10:42 +02002666 }
2667
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002668 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002669 {
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002670 // If the index is not qualified as constant, the behavior in the spec is undefined. This
2671 // applies even if ANGLE has been able to constant fold it (ANGLE may constant fold
2672 // expressions that are not constant expressions). The most compatible way to handle this
2673 // case is to report a warning instead of an error and force the index to be in the
2674 // correct range.
2675 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Jamie Madill21c1e452014-12-29 11:33:41 -05002676 int index = indexConstantUnion->getIConst(0);
Olli Etuaho90892fb2016-07-14 14:44:51 +03002677 if (!baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04002678 {
Olli Etuaho90892fb2016-07-14 14:44:51 +03002679 // Array checks are done later because a different error message might be generated
2680 // based on the index in some cases.
2681 if (baseExpression->isVector())
2682 {
2683 index = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
2684 baseExpression->getType().getNominalSize(),
2685 "vector field selection out of range", "[]");
2686 }
2687 else if (baseExpression->isMatrix())
2688 {
2689 index = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
2690 baseExpression->getType().getCols(),
2691 "matrix field selection out of range", "[]");
2692 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002693 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03002694
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002695 TIntermConstantUnion *baseConstantUnion = baseExpression->getAsConstantUnion();
2696 if (baseConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002697 {
2698 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002699 {
Olli Etuaho90892fb2016-07-14 14:44:51 +03002700 index = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
2701 baseExpression->getArraySize(),
2702 "array index out of range", "[]");
2703 // Constant folding for array indexing.
2704 indexedExpression = foldArraySubscript(index, baseConstantUnion, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002705 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002706 else if (baseExpression->isVector())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002707 {
Olli Etuaho90892fb2016-07-14 14:44:51 +03002708 // Constant folding for vector indexing - reusing vector swizzle folding.
Jamie Madill7164cf42013-07-08 13:30:59 -04002709 TVectorFields fields;
2710 fields.num = 1;
Olli Etuaho90892fb2016-07-14 14:44:51 +03002711 fields.offsets[0] = index;
2712 indexedExpression = foldVectorSwizzle(fields, baseConstantUnion, location);
Jamie Madill7164cf42013-07-08 13:30:59 -04002713 }
2714 else if (baseExpression->isMatrix())
2715 {
Olli Etuaho90892fb2016-07-14 14:44:51 +03002716 // Constant folding for matrix indexing.
2717 indexedExpression = foldMatrixSubscript(index, baseConstantUnion, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002718 }
2719 }
2720 else
2721 {
Jamie Madillb11e2482015-05-04 14:21:22 -04002722 int safeIndex = -1;
2723
Jamie Madill7164cf42013-07-08 13:30:59 -04002724 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002725 {
Olli Etuaho3e960462015-11-12 15:58:39 +02002726 if (baseExpression->getQualifier() == EvqFragData && index > 0)
2727 {
2728 if (mShaderSpec == SH_WEBGL2_SPEC)
2729 {
2730 // Error has been already generated if index is not const.
2731 if (indexExpression->getQualifier() == EvqConst)
2732 {
2733 error(location, "", "[",
2734 "array index for gl_FragData must be constant zero");
Olli Etuaho3e960462015-11-12 15:58:39 +02002735 }
2736 safeIndex = 0;
2737 }
2738 else if (!isExtensionEnabled("GL_EXT_draw_buffers"))
2739 {
2740 outOfRangeError(outOfRangeIndexIsError, location, "", "[",
2741 "array index for gl_FragData must be zero when "
2742 "GL_EXT_draw_buffers is disabled");
2743 safeIndex = 0;
2744 }
2745 }
2746 // Only do generic out-of-range check if similar error hasn't already been reported.
Olli Etuaho90892fb2016-07-14 14:44:51 +03002747 if (safeIndex < 0)
Jamie Madill7164cf42013-07-08 13:30:59 -04002748 {
Olli Etuaho90892fb2016-07-14 14:44:51 +03002749 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
2750 baseExpression->getArraySize(),
2751 "array index out of range", "[]");
Jamie Madill7164cf42013-07-08 13:30:59 -04002752 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002753 }
2754
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002755 // Data of constant unions can't be changed, because it may be shared with other
2756 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
2757 // sanitized object.
Jamie Madillb11e2482015-05-04 14:21:22 -04002758 if (safeIndex != -1)
2759 {
2760 TConstantUnion *safeConstantUnion = new TConstantUnion();
2761 safeConstantUnion->setIConst(safeIndex);
2762 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
2763 }
2764
Jamie Madillb98c3a82015-07-23 14:26:04 -04002765 indexedExpression =
2766 intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002767 }
2768 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002769 else
2770 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002771 indexedExpression =
2772 intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
Jamie Madill7164cf42013-07-08 13:30:59 -04002773 }
2774
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002775 if (indexedExpression == 0)
2776 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002777 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002778 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002779 indexedExpression =
2780 intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002781 }
2782 else if (baseExpression->isArray())
2783 {
Olli Etuahob3fbd862015-09-30 17:55:02 +03002784 TType indexedType = baseExpression->getType();
2785 indexedType.clearArrayness();
2786 indexedExpression->setType(indexedType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002787 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002788 else if (baseExpression->isMatrix())
2789 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002790 indexedExpression->setType(TType(baseExpression->getBasicType(),
Olli Etuahob3fbd862015-09-30 17:55:02 +03002791 baseExpression->getPrecision(), EvqTemporary,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002792 static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002793 }
2794 else if (baseExpression->isVector())
2795 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002796 indexedExpression->setType(
Olli Etuahob3fbd862015-09-30 17:55:02 +03002797 TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002798 }
2799 else
2800 {
2801 indexedExpression->setType(baseExpression->getType());
2802 }
2803
Olli Etuahob3fbd862015-09-30 17:55:02 +03002804 if (baseExpression->getType().getQualifier() == EvqConst &&
2805 indexExpression->getType().getQualifier() == EvqConst)
2806 {
2807 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2808 }
2809 else
2810 {
2811 indexedExpression->getTypePointer()->setQualifier(EvqTemporary);
2812 }
2813
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002814 return indexedExpression;
2815}
2816
Olli Etuaho90892fb2016-07-14 14:44:51 +03002817int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
2818 const TSourceLoc &location,
2819 int index,
2820 int arraySize,
2821 const char *reason,
2822 const char *token)
2823{
2824 if (index >= arraySize || index < 0)
2825 {
2826 std::stringstream extraInfoStream;
2827 extraInfoStream << "'" << index << "'";
2828 std::string extraInfo = extraInfoStream.str();
2829 outOfRangeError(outOfRangeIndexIsError, location, reason, token, extraInfo.c_str());
2830 if (index < 0)
2831 {
2832 return 0;
2833 }
2834 else
2835 {
2836 return arraySize - 1;
2837 }
2838 }
2839 return index;
2840}
2841
Jamie Madillb98c3a82015-07-23 14:26:04 -04002842TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
2843 const TSourceLoc &dotLocation,
2844 const TString &fieldString,
2845 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002846{
2847 TIntermTyped *indexedExpression = NULL;
2848
2849 if (baseExpression->isArray())
2850 {
2851 error(fieldLocation, "cannot apply dot operator to an array", ".");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002852 }
2853
2854 if (baseExpression->isVector())
2855 {
2856 TVectorFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002857 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields,
2858 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002859 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002860 fields.num = 1;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002861 fields.offsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002862 }
2863
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002864 if (baseExpression->getAsConstantUnion())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002865 {
2866 // constant folding for vector fields
Olli Etuaho90892fb2016-07-14 14:44:51 +03002867 indexedExpression =
2868 foldVectorSwizzle(fields, baseExpression->getAsConstantUnion(), fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002869 }
2870 else
2871 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302872 TIntermTyped *index = intermediate.addSwizzle(fields, fieldLocation);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002873 indexedExpression =
2874 intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002875 }
2876 if (indexedExpression == nullptr)
2877 {
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002878 indexedExpression = baseExpression;
2879 }
2880 else
2881 {
2882 // Note that the qualifier set here will be corrected later.
Jamie Madillb98c3a82015-07-23 14:26:04 -04002883 indexedExpression->setType(TType(baseExpression->getBasicType(),
2884 baseExpression->getPrecision(), EvqTemporary,
Jamie Madillc2128ff2016-07-04 10:26:17 -04002885 static_cast<unsigned char>(fields.num)));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002886 }
2887 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002888 else if (baseExpression->getBasicType() == EbtStruct)
2889 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002890 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302891 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04002892 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002893 {
2894 error(dotLocation, "structure has no fields", "Internal Error");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002895 indexedExpression = baseExpression;
2896 }
2897 else
2898 {
2899 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002900 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002901 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002902 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002903 {
2904 fieldFound = true;
2905 break;
2906 }
2907 }
2908 if (fieldFound)
2909 {
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002910 if (baseExpression->getAsConstantUnion())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002911 {
2912 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2913 if (indexedExpression == 0)
2914 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002915 indexedExpression = baseExpression;
2916 }
2917 else
2918 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002919 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002920 }
2921 }
2922 else
2923 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002924 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002925 unionArray->setIConst(i);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002926 TIntermTyped *index = intermediate.addConstantUnion(
2927 unionArray, *fields[i]->type(), fieldLocation);
2928 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression,
2929 index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002930 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002931 }
2932 }
2933 else
2934 {
2935 error(dotLocation, " no such field in structure", fieldString.c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002936 indexedExpression = baseExpression;
2937 }
2938 }
2939 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002940 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002941 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002942 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302943 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04002944 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002945 {
2946 error(dotLocation, "interface block has no fields", "Internal Error");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002947 indexedExpression = baseExpression;
2948 }
2949 else
2950 {
2951 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002952 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002953 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002954 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002955 {
2956 fieldFound = true;
2957 break;
2958 }
2959 }
2960 if (fieldFound)
2961 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002962 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002963 unionArray->setIConst(i);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002964 TIntermTyped *index =
2965 intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
2966 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock,
2967 baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002968 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002969 }
2970 else
2971 {
2972 error(dotLocation, " no such field in interface block", fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002973 indexedExpression = baseExpression;
2974 }
2975 }
2976 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002977 else
2978 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002979 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002980 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03002981 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05302982 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002983 }
2984 else
2985 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302986 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03002987 " field selection requires structure, vector, or interface block on left hand "
2988 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05302989 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002990 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002991 indexedExpression = baseExpression;
2992 }
2993
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002994 if (baseExpression->getQualifier() == EvqConst)
2995 {
2996 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2997 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002998 else
2999 {
3000 indexedExpression->getTypePointer()->setQualifier(EvqTemporary);
3001 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02003002
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003003 return indexedExpression;
3004}
3005
Jamie Madillb98c3a82015-07-23 14:26:04 -04003006TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3007 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003008{
Martin Radev802abe02016-08-04 17:48:32 +03003009 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003010
3011 if (qualifierType == "shared")
3012 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003013 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003014 }
3015 else if (qualifierType == "packed")
3016 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003017 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003018 }
3019 else if (qualifierType == "std140")
3020 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003021 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003022 }
3023 else if (qualifierType == "row_major")
3024 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003025 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003026 }
3027 else if (qualifierType == "column_major")
3028 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003029 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003030 }
3031 else if (qualifierType == "location")
3032 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003033 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
3034 "location requires an argument");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003035 }
3036 else
3037 {
3038 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003039 }
3040
Jamie Madilla5efff92013-06-06 11:56:47 -04003041 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003042}
3043
Martin Radev802abe02016-08-04 17:48:32 +03003044void TParseContext::parseLocalSize(const TString &qualifierType,
3045 const TSourceLoc &qualifierTypeLine,
3046 int intValue,
3047 const TSourceLoc &intValueLine,
3048 const std::string &intValueString,
3049 size_t index,
3050 TLocalSize *localSize)
3051{
Olli Etuaho856c4972016-08-08 11:38:39 +03003052 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03003053 if (intValue < 1)
3054 {
3055 std::string errorMessage = std::string(getLocalSizeString(index)) + " must be positive";
3056 error(intValueLine, "out of range:", intValueString.c_str(), errorMessage.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003057 }
3058 (*localSize)[index] = intValue;
3059}
3060
Jamie Madillb98c3a82015-07-23 14:26:04 -04003061TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3062 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003063 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303064 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003065{
Martin Radev802abe02016-08-04 17:48:32 +03003066 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003067
Martin Radev802abe02016-08-04 17:48:32 +03003068 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003069
Martin Radev802abe02016-08-04 17:48:32 +03003070 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003071 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003072 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003073 if (intValue < 0)
3074 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003075 error(intValueLine, "out of range:", intValueString.c_str(),
3076 "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003077 }
3078 else
3079 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003080 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003081 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003082 }
Martin Radev802abe02016-08-04 17:48:32 +03003083 else if (qualifierType == "local_size_x")
3084 {
3085 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
3086 &qualifier.localSize);
3087 }
3088 else if (qualifierType == "local_size_y")
3089 {
3090 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
3091 &qualifier.localSize);
3092 }
3093 else if (qualifierType == "local_size_z")
3094 {
3095 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
3096 &qualifier.localSize);
3097 }
3098 else
3099 {
3100 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003101 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003102
Jamie Madilla5efff92013-06-06 11:56:47 -04003103 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003104}
3105
Jamie Madillb98c3a82015-07-23 14:26:04 -04003106TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03003107 TLayoutQualifier rightQualifier,
3108 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003109{
Jamie Madilla5efff92013-06-06 11:56:47 -04003110 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003111
Jamie Madilla5efff92013-06-06 11:56:47 -04003112 if (rightQualifier.location != -1)
3113 {
3114 joinedQualifier.location = rightQualifier.location;
3115 }
3116 if (rightQualifier.matrixPacking != EmpUnspecified)
3117 {
3118 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
3119 }
3120 if (rightQualifier.blockStorage != EbsUnspecified)
3121 {
3122 joinedQualifier.blockStorage = rightQualifier.blockStorage;
3123 }
3124
Martin Radev802abe02016-08-04 17:48:32 +03003125 for (size_t i = 0u; i < rightQualifier.localSize.size(); ++i)
3126 {
3127 if (rightQualifier.localSize[i] != -1)
3128 {
3129 if (joinedQualifier.localSize[i] != -1 &&
3130 joinedQualifier.localSize[i] != rightQualifier.localSize[i])
3131 {
3132 error(rightQualifierLocation,
3133 "Cannot have multiple different work group size specifiers",
3134 getLocalSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03003135 }
3136 joinedQualifier.localSize[i] = rightQualifier.localSize[i];
3137 }
3138 }
3139
Jamie Madilla5efff92013-06-06 11:56:47 -04003140 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003141}
3142
Arun Patole7e7e68d2015-05-22 12:02:25 +05303143TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc,
3144 TQualifier interpolationQualifier,
3145 const TSourceLoc &storageLoc,
3146 TQualifier storageQualifier)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003147{
3148 TQualifier mergedQualifier = EvqSmoothIn;
3149
Arun Patole7e7e68d2015-05-22 12:02:25 +05303150 if (storageQualifier == EvqFragmentIn)
3151 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003152 if (interpolationQualifier == EvqSmooth)
3153 mergedQualifier = EvqSmoothIn;
3154 else if (interpolationQualifier == EvqFlat)
3155 mergedQualifier = EvqFlatIn;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003156 else
3157 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003158 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303159 else if (storageQualifier == EvqCentroidIn)
3160 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003161 if (interpolationQualifier == EvqSmooth)
3162 mergedQualifier = EvqCentroidIn;
3163 else if (interpolationQualifier == EvqFlat)
3164 mergedQualifier = EvqFlatIn;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003165 else
3166 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003167 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303168 else if (storageQualifier == EvqVertexOut)
3169 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003170 if (interpolationQualifier == EvqSmooth)
3171 mergedQualifier = EvqSmoothOut;
3172 else if (interpolationQualifier == EvqFlat)
3173 mergedQualifier = EvqFlatOut;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003174 else
3175 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003176 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303177 else if (storageQualifier == EvqCentroidOut)
3178 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003179 if (interpolationQualifier == EvqSmooth)
3180 mergedQualifier = EvqCentroidOut;
3181 else if (interpolationQualifier == EvqFlat)
3182 mergedQualifier = EvqFlatOut;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003183 else
3184 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003185 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303186 else
3187 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003188 error(interpolationLoc,
3189 "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303190 getInterpolationString(interpolationQualifier));
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003191
3192 mergedQualifier = storageQualifier;
3193 }
3194
3195 TPublicType type;
3196 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
3197 return type;
3198}
3199
Jamie Madillb98c3a82015-07-23 14:26:04 -04003200TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3201 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003202{
Olli Etuaho856c4972016-08-08 11:38:39 +03003203 checkIsNonVoid(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier.type);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003204
Olli Etuaho856c4972016-08-08 11:38:39 +03003205 checkWorkGroupSizeIsNotSpecified(typeSpecifier.line, typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003206
Arun Patole7e7e68d2015-05-22 12:02:25 +05303207 for (unsigned int i = 0; i < fieldList->size(); ++i)
3208 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003209 //
3210 // Careful not to replace already known aspects of type, like array-ness
3211 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303212 TType *type = (*fieldList)[i]->type();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003213 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003214 type->setPrimarySize(typeSpecifier.primarySize);
3215 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003216 type->setPrecision(typeSpecifier.precision);
3217 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003218 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003219
3220 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303221 if (type->isArray())
3222 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003223 checkIsValidTypeForArray(typeSpecifier.line, typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003224 }
3225 if (typeSpecifier.array)
Olli Etuaho856c4972016-08-08 11:38:39 +03003226 type->setArraySize(static_cast<unsigned int>(typeSpecifier.arraySize));
Arun Patole7e7e68d2015-05-22 12:02:25 +05303227 if (typeSpecifier.userDef)
3228 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003229 type->setStruct(typeSpecifier.userDef->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003230 }
3231
Olli Etuaho383b7912016-08-05 11:22:59 +03003232 structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003233 }
3234
Jamie Madill98493dd2013-07-08 14:39:03 -04003235 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003236}
3237
Jamie Madillb98c3a82015-07-23 14:26:04 -04003238TPublicType TParseContext::addStructure(const TSourceLoc &structLine,
3239 const TSourceLoc &nameLine,
3240 const TString *structName,
3241 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003242{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303243 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003244 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003245
Jamie Madill9b820842015-02-12 10:40:10 -05003246 // Store a bool in the struct if we're at global scope, to allow us to
3247 // skip the local struct scoping workaround in HLSL.
Jamie Madillb960cc42015-02-12 15:33:20 +00003248 structure->setUniqueId(TSymbolTable::nextUniqueId());
Jamie Madill9b820842015-02-12 10:40:10 -05003249 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003250
Jamie Madill98493dd2013-07-08 14:39:03 -04003251 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003252 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003253 checkIsNotReserved(nameLine, *structName);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303254 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
3255 if (!symbolTable.declare(userTypeDef))
3256 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003257 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003258 }
3259 }
3260
3261 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04003262 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003263 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003264 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04003265 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003266 switch (qualifier)
3267 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003268 case EvqGlobal:
3269 case EvqTemporary:
3270 break;
3271 default:
3272 error(field.line(), "invalid qualifier on struct member",
3273 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003274 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003275 }
3276 }
3277
3278 TPublicType publicType;
3279 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003280 publicType.userDef = structureType;
Olli Etuahobd163f62015-11-13 12:15:38 +02003281 publicType.isStructSpecifier = true;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003282 exitStructDeclaration();
3283
3284 return publicType;
3285}
3286
Jamie Madillb98c3a82015-07-23 14:26:04 -04003287TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
3288 TIntermAggregate *statementList,
3289 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02003290{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003291 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003292 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02003293 init->isVector())
3294 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003295 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
3296 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003297 return nullptr;
3298 }
3299
Olli Etuahoac5274d2015-02-20 10:19:08 +02003300 if (statementList)
3301 {
3302 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
3303 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02003304 return nullptr;
3305 }
3306 }
3307
Olli Etuahoa3a36662015-02-17 13:46:51 +02003308 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3309 if (node == nullptr)
3310 {
3311 error(loc, "erroneous switch statement", "switch");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003312 return nullptr;
3313 }
3314 return node;
3315}
3316
3317TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3318{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003319 if (mSwitchNestingLevel == 0)
3320 {
3321 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003322 return nullptr;
3323 }
3324 if (condition == nullptr)
3325 {
3326 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003327 return nullptr;
3328 }
3329 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04003330 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02003331 {
3332 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003333 }
3334 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003335 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
3336 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
3337 // fold in case labels.
3338 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02003339 {
3340 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003341 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003342 TIntermCase *node = intermediate.addCase(condition, loc);
3343 if (node == nullptr)
3344 {
3345 error(loc, "erroneous case statement", "case");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003346 return nullptr;
3347 }
3348 return node;
3349}
3350
3351TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3352{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003353 if (mSwitchNestingLevel == 0)
3354 {
3355 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003356 return nullptr;
3357 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003358 TIntermCase *node = intermediate.addCase(nullptr, loc);
3359 if (node == nullptr)
3360 {
3361 error(loc, "erroneous default statement", "default");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003362 return nullptr;
3363 }
3364 return node;
3365}
3366
Jamie Madillb98c3a82015-07-23 14:26:04 -04003367TIntermTyped *TParseContext::createUnaryMath(TOperator op,
3368 TIntermTyped *child,
3369 const TSourceLoc &loc,
3370 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003371{
3372 if (child == nullptr)
3373 {
3374 return nullptr;
3375 }
3376
3377 switch (op)
3378 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003379 case EOpLogicalNot:
3380 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
3381 child->isVector())
3382 {
3383 return nullptr;
3384 }
3385 break;
3386 case EOpBitwiseNot:
3387 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3388 child->isMatrix() || child->isArray())
3389 {
3390 return nullptr;
3391 }
3392 break;
3393 case EOpPostIncrement:
3394 case EOpPreIncrement:
3395 case EOpPostDecrement:
3396 case EOpPreDecrement:
3397 case EOpNegative:
3398 case EOpPositive:
3399 if (child->getBasicType() == EbtStruct || child->getBasicType() == EbtBool ||
3400 child->isArray())
3401 {
3402 return nullptr;
3403 }
3404 // Operators for built-ins are already type checked against their prototype.
3405 default:
3406 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003407 }
3408
Olli Etuahof6c694b2015-03-26 14:50:53 +02003409 return intermediate.addUnaryMath(op, child, loc, funcReturnType);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003410}
3411
Olli Etuaho09b22472015-02-11 11:47:26 +02003412TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3413{
Olli Etuahof6c694b2015-03-26 14:50:53 +02003414 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003415 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003416 {
3417 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003418 return child;
3419 }
3420 return node;
3421}
3422
Jamie Madillb98c3a82015-07-23 14:26:04 -04003423TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
3424 TIntermTyped *child,
3425 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003426{
Olli Etuaho856c4972016-08-08 11:38:39 +03003427 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02003428 return addUnaryMath(op, child, loc);
3429}
3430
Jamie Madillb98c3a82015-07-23 14:26:04 -04003431bool TParseContext::binaryOpCommonCheck(TOperator op,
3432 TIntermTyped *left,
3433 TIntermTyped *right,
3434 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003435{
3436 if (left->isArray() || right->isArray())
3437 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003438 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003439 {
3440 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3441 return false;
3442 }
3443
3444 if (left->isArray() != right->isArray())
3445 {
3446 error(loc, "array / non-array mismatch", GetOperatorString(op));
3447 return false;
3448 }
3449
3450 switch (op)
3451 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003452 case EOpEqual:
3453 case EOpNotEqual:
3454 case EOpAssign:
3455 case EOpInitialize:
3456 break;
3457 default:
3458 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3459 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02003460 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003461 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003462 if (left->getArraySize() != right->getArraySize())
3463 {
3464 error(loc, "array size mismatch", GetOperatorString(op));
3465 return false;
3466 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003467 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003468
3469 // Check ops which require integer / ivec parameters
3470 bool isBitShift = false;
3471 switch (op)
3472 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003473 case EOpBitShiftLeft:
3474 case EOpBitShiftRight:
3475 case EOpBitShiftLeftAssign:
3476 case EOpBitShiftRightAssign:
3477 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3478 // check that the basic type is an integer type.
3479 isBitShift = true;
3480 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3481 {
3482 return false;
3483 }
3484 break;
3485 case EOpBitwiseAnd:
3486 case EOpBitwiseXor:
3487 case EOpBitwiseOr:
3488 case EOpBitwiseAndAssign:
3489 case EOpBitwiseXorAssign:
3490 case EOpBitwiseOrAssign:
3491 // It is enough to check the type of only one operand, since later it
3492 // is checked that the operand types match.
3493 if (!IsInteger(left->getBasicType()))
3494 {
3495 return false;
3496 }
3497 break;
3498 default:
3499 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003500 }
3501
3502 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3503 // So the basic type should usually match.
3504 if (!isBitShift && left->getBasicType() != right->getBasicType())
3505 {
3506 return false;
3507 }
3508
Olli Etuaho9dd217b2015-03-20 14:24:31 +02003509 // Check that type sizes match exactly on ops that require that.
Olli Etuahoff699002015-03-23 14:38:42 +02003510 // Also check restrictions for structs that contain arrays or samplers.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003511 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003512 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003513 case EOpAssign:
3514 case EOpInitialize:
3515 case EOpEqual:
3516 case EOpNotEqual:
3517 // ESSL 1.00 sections 5.7, 5.8, 5.9
3518 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
3519 {
3520 error(loc, "undefined operation for structs containing arrays",
3521 GetOperatorString(op));
3522 return false;
3523 }
3524 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3525 // we interpret the spec so that this extends to structs containing samplers,
3526 // similarly to ESSL 1.00 spec.
3527 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
3528 left->getType().isStructureContainingSamplers())
3529 {
3530 error(loc, "undefined operation for structs containing samplers",
3531 GetOperatorString(op));
3532 return false;
3533 }
3534 case EOpLessThan:
3535 case EOpGreaterThan:
3536 case EOpLessThanEqual:
3537 case EOpGreaterThanEqual:
3538 if ((left->getNominalSize() != right->getNominalSize()) ||
3539 (left->getSecondarySize() != right->getSecondarySize()))
3540 {
3541 return false;
3542 }
3543 default:
3544 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003545 }
3546
Olli Etuahod6b14282015-03-17 14:31:35 +02003547 return true;
3548}
3549
Jamie Madillb98c3a82015-07-23 14:26:04 -04003550TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
3551 TIntermTyped *left,
3552 TIntermTyped *right,
3553 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02003554{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003555 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003556 return nullptr;
3557
Olli Etuahofc1806e2015-03-17 13:03:11 +02003558 switch (op)
3559 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003560 case EOpEqual:
3561 case EOpNotEqual:
3562 break;
3563 case EOpLessThan:
3564 case EOpGreaterThan:
3565 case EOpLessThanEqual:
3566 case EOpGreaterThanEqual:
3567 ASSERT(!left->isArray() && !right->isArray());
3568 if (left->isMatrix() || left->isVector() || left->getBasicType() == EbtStruct)
3569 {
3570 return nullptr;
3571 }
3572 break;
3573 case EOpLogicalOr:
3574 case EOpLogicalXor:
3575 case EOpLogicalAnd:
3576 ASSERT(!left->isArray() && !right->isArray());
3577 if (left->getBasicType() != EbtBool || left->isMatrix() || left->isVector())
3578 {
3579 return nullptr;
3580 }
3581 break;
3582 case EOpAdd:
3583 case EOpSub:
3584 case EOpDiv:
3585 case EOpMul:
3586 ASSERT(!left->isArray() && !right->isArray());
3587 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool)
3588 {
3589 return nullptr;
3590 }
3591 break;
3592 case EOpIMod:
3593 ASSERT(!left->isArray() && !right->isArray());
3594 // Note that this is only for the % operator, not for mod()
3595 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool ||
3596 left->getBasicType() == EbtFloat)
3597 {
3598 return nullptr;
3599 }
3600 break;
3601 // Note that for bitwise ops, type checking is done in promote() to
3602 // share code between ops and compound assignment
3603 default:
3604 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003605 }
3606
Olli Etuahofc1806e2015-03-17 13:03:11 +02003607 return intermediate.addBinaryMath(op, left, right, loc);
3608}
3609
Jamie Madillb98c3a82015-07-23 14:26:04 -04003610TIntermTyped *TParseContext::addBinaryMath(TOperator op,
3611 TIntermTyped *left,
3612 TIntermTyped *right,
3613 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003614{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003615 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003616 if (node == 0)
3617 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003618 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3619 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003620 return left;
3621 }
3622 return node;
3623}
3624
Jamie Madillb98c3a82015-07-23 14:26:04 -04003625TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
3626 TIntermTyped *left,
3627 TIntermTyped *right,
3628 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003629{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003630 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003631 if (node == 0)
3632 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003633 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3634 right->getCompleteString());
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003635 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02003636 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003637 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
3638 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003639 }
3640 return node;
3641}
3642
Jamie Madillb98c3a82015-07-23 14:26:04 -04003643TIntermTyped *TParseContext::createAssign(TOperator op,
3644 TIntermTyped *left,
3645 TIntermTyped *right,
3646 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003647{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003648 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003649 {
3650 return intermediate.addAssign(op, left, right, loc);
3651 }
3652 return nullptr;
3653}
3654
Jamie Madillb98c3a82015-07-23 14:26:04 -04003655TIntermTyped *TParseContext::addAssign(TOperator op,
3656 TIntermTyped *left,
3657 TIntermTyped *right,
3658 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003659{
3660 TIntermTyped *node = createAssign(op, left, right, loc);
3661 if (node == nullptr)
3662 {
3663 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02003664 return left;
3665 }
3666 return node;
3667}
3668
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003669TIntermTyped *TParseContext::addComma(TIntermTyped *left,
3670 TIntermTyped *right,
3671 const TSourceLoc &loc)
3672{
Corentin Wallez0d959252016-07-12 17:26:32 -04003673 // WebGL2 section 5.26, the following results in an error:
3674 // "Sequence operator applied to void, arrays, or structs containing arrays"
3675 if (mShaderSpec == SH_WEBGL2_SPEC && (left->isArray() || left->getBasicType() == EbtVoid ||
3676 left->getType().isStructureContainingArrays() ||
3677 right->isArray() || right->getBasicType() == EbtVoid ||
3678 right->getType().isStructureContainingArrays()))
3679 {
3680 error(loc,
3681 "sequence operator is not allowed for void, arrays, or structs containing arrays",
3682 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04003683 }
3684
Olli Etuaho15200042015-11-04 16:56:31 +02003685 return intermediate.addComma(left, right, loc, mShaderVersion);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003686}
3687
Olli Etuaho49300862015-02-20 14:54:49 +02003688TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3689{
3690 switch (op)
3691 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003692 case EOpContinue:
3693 if (mLoopNestingLevel <= 0)
3694 {
3695 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04003696 }
3697 break;
3698 case EOpBreak:
3699 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
3700 {
3701 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04003702 }
3703 break;
3704 case EOpReturn:
3705 if (mCurrentFunctionType->getBasicType() != EbtVoid)
3706 {
3707 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04003708 }
3709 break;
3710 default:
3711 // No checks for discard
3712 break;
Olli Etuaho49300862015-02-20 14:54:49 +02003713 }
3714 return intermediate.addBranch(op, loc);
3715}
3716
Jamie Madillb98c3a82015-07-23 14:26:04 -04003717TIntermBranch *TParseContext::addBranch(TOperator op,
3718 TIntermTyped *returnValue,
3719 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02003720{
3721 ASSERT(op == EOpReturn);
3722 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003723 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02003724 {
3725 error(loc, "void function cannot return a value", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02003726 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003727 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02003728 {
3729 error(loc, "function return is not matching type:", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02003730 }
3731 return intermediate.addBranch(op, returnValue, loc);
3732}
3733
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003734void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
3735{
3736 ASSERT(!functionCall->isUserDefined());
3737 const TString &name = functionCall->getName();
3738 TIntermNode *offset = nullptr;
3739 TIntermSequence *arguments = functionCall->getSequence();
3740 if (name.compare(0, 16, "texelFetchOffset") == 0 ||
3741 name.compare(0, 16, "textureLodOffset") == 0 ||
3742 name.compare(0, 20, "textureProjLodOffset") == 0 ||
3743 name.compare(0, 17, "textureGradOffset") == 0 ||
3744 name.compare(0, 21, "textureProjGradOffset") == 0)
3745 {
3746 offset = arguments->back();
3747 }
3748 else if (name.compare(0, 13, "textureOffset") == 0 ||
3749 name.compare(0, 17, "textureProjOffset") == 0)
3750 {
3751 // A bias parameter might follow the offset parameter.
3752 ASSERT(arguments->size() >= 3);
3753 offset = (*arguments)[2];
3754 }
3755 if (offset != nullptr)
3756 {
3757 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
3758 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
3759 {
3760 TString unmangledName = TFunction::unmangleName(name);
3761 error(functionCall->getLine(), "Texture offset must be a constant expression",
3762 unmangledName.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003763 }
3764 else
3765 {
3766 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
3767 size_t size = offsetConstantUnion->getType().getObjectSize();
3768 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
3769 for (size_t i = 0u; i < size; ++i)
3770 {
3771 int offsetValue = values[i].getIConst();
3772 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
3773 {
3774 std::stringstream tokenStream;
3775 tokenStream << offsetValue;
3776 std::string token = tokenStream.str();
3777 error(offset->getLine(), "Texture offset value out of valid range",
3778 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003779 }
3780 }
3781 }
3782 }
3783}
3784
Jamie Madillb98c3a82015-07-23 14:26:04 -04003785TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
3786 TIntermNode *paramNode,
3787 TIntermNode *thisNode,
3788 const TSourceLoc &loc,
3789 bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003790{
Jamie Madillb98c3a82015-07-23 14:26:04 -04003791 *fatalError = false;
3792 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003793 TIntermTyped *callNode = nullptr;
3794
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003795 if (thisNode != nullptr)
3796 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003797 TConstantUnion *unionArray = new TConstantUnion[1];
Jamie Madillb98c3a82015-07-23 14:26:04 -04003798 int arraySize = 0;
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003799 TIntermTyped *typedThis = thisNode->getAsTyped();
3800 if (fnCall->getName() != "length")
3801 {
3802 error(loc, "invalid method", fnCall->getName().c_str());
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003803 }
3804 else if (paramNode != nullptr)
3805 {
3806 error(loc, "method takes no parameters", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003807 }
3808 else if (typedThis == nullptr || !typedThis->isArray())
3809 {
3810 error(loc, "length can only be called on arrays", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003811 }
3812 else
3813 {
Olli Etuaho96e67382015-04-23 14:27:02 +03003814 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03003815 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003816 {
Olli Etuaho39282e12015-04-23 15:41:48 +03003817 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003818 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03003819 // (func()).length()
3820 // (int[3](0, 1, 2)).length()
Jamie Madillb98c3a82015-07-23 14:26:04 -04003821 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
3822 // expression.
3823 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
3824 // spec section 5.9 which allows "An array, vector or matrix expression with the
3825 // length method applied".
3826 error(loc, "length can only be called on array names, not on array expressions",
3827 "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003828 }
3829 }
Olli Etuaho96e67382015-04-23 14:27:02 +03003830 unionArray->setIConst(arraySize);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003831 callNode =
3832 intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003833 }
3834 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003835 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003836 // Then this should be a constructor.
Olli Etuaho856c4972016-08-08 11:38:39 +03003837 callNode = addConstructor(paramNode, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003838 }
3839 else
3840 {
3841 //
3842 // Not a constructor. Find it in the symbol table.
3843 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303844 const TFunction *fnCandidate;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003845 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003846 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003847 if (fnCandidate)
3848 {
3849 //
3850 // A declared function.
3851 //
Olli Etuaho383b7912016-08-05 11:22:59 +03003852 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003853 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003854 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003855 }
3856 op = fnCandidate->getBuiltInOp();
3857 if (builtIn && op != EOpNull)
3858 {
3859 //
3860 // A function call mapped to a built-in operation.
3861 //
3862 if (fnCandidate->getParamCount() == 1)
3863 {
3864 //
3865 // Treat it like a built-in unary operator.
3866 //
Olli Etuaho15c2ac32015-11-09 15:51:43 +02003867 TIntermAggregate *paramAgg = paramNode->getAsAggregate();
3868 paramNode = paramAgg->getSequence()->front();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003869 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc,
3870 &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003871 if (callNode == nullptr)
3872 {
3873 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003874 extraInfoStream
3875 << "built in unary operator function. Type: "
3876 << static_cast<TIntermTyped *>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003877 std::string extraInfo = extraInfoStream.str();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003878 error(paramNode->getLine(), " wrong operand type", "Internal Error",
3879 extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003880 *fatalError = true;
3881 return nullptr;
3882 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003883 }
3884 else
3885 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003886 TIntermAggregate *aggregate =
3887 intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003888 aggregate->setType(fnCandidate->getReturnType());
3889 aggregate->setPrecisionFromChildren();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02003890 if (aggregate->areChildrenConstQualified())
3891 {
3892 aggregate->getTypePointer()->setQualifier(EvqConst);
3893 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003894
3895 // Some built-in functions have out parameters too.
3896 functionCallLValueErrorCheck(fnCandidate, aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05303897
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003898 // See if we can constant fold a built-in. Note that this may be possible even
3899 // if it is not const-qualified.
Olli Etuahob43846e2015-06-02 18:18:57 +03003900 TIntermTyped *foldedNode = intermediate.foldAggregateBuiltIn(aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05303901 if (foldedNode)
3902 {
Arun Patole274f0702015-05-05 13:33:30 +05303903 callNode = foldedNode;
3904 }
Olli Etuahob43846e2015-06-02 18:18:57 +03003905 else
3906 {
3907 callNode = aggregate;
3908 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003909 }
3910 }
3911 else
3912 {
3913 // This is a real function call
Jamie Madillb98c3a82015-07-23 14:26:04 -04003914 TIntermAggregate *aggregate =
3915 intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003916 aggregate->setType(fnCandidate->getReturnType());
3917
Jamie Madillb98c3a82015-07-23 14:26:04 -04003918 // this is how we know whether the given function is a builtIn function or a user
3919 // defined function
3920 // if builtIn == false, it's a userDefined -> could be an overloaded
3921 // builtIn function also
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003922 // if builtIn == true, it's definitely a builtIn function with EOpNull
3923 if (!builtIn)
3924 aggregate->setUserDefined();
3925 aggregate->setName(fnCandidate->getMangledName());
Corentin Wallez71d147f2015-02-11 11:15:24 -08003926 aggregate->setFunctionId(fnCandidate->getUniqueId());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003927
3928 // This needs to happen after the name is set
3929 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003930 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003931 aggregate->setBuiltInFunctionPrecision();
3932
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003933 checkTextureOffsetConst(aggregate);
3934 }
3935
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003936 callNode = aggregate;
3937
3938 functionCallLValueErrorCheck(fnCandidate, aggregate);
3939 }
3940 }
3941 else
3942 {
3943 // error message was put out by findFunction()
3944 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003945 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003946 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003947 callNode = intermediate.addConstantUnion(unionArray,
3948 TType(EbtFloat, EbpUndefined, EvqConst), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003949 }
3950 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003951 return callNode;
3952}
3953
Jamie Madillb98c3a82015-07-23 14:26:04 -04003954TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
3955 TIntermTyped *trueBlock,
3956 TIntermTyped *falseBlock,
Olli Etuaho52901742015-04-15 13:42:45 +03003957 const TSourceLoc &loc)
3958{
Olli Etuaho856c4972016-08-08 11:38:39 +03003959 checkIsScalarBool(loc, cond);
Olli Etuaho52901742015-04-15 13:42:45 +03003960
3961 if (trueBlock->getType() != falseBlock->getType())
3962 {
3963 binaryOpError(loc, ":", trueBlock->getCompleteString(), falseBlock->getCompleteString());
Olli Etuaho52901742015-04-15 13:42:45 +03003964 return falseBlock;
3965 }
Olli Etuahoa2d53032015-04-15 14:14:44 +03003966 // ESSL1 sections 5.2 and 5.7:
3967 // ESSL3 section 5.7:
3968 // Ternary operator is not among the operators allowed for structures/arrays.
3969 if (trueBlock->isArray() || trueBlock->getBasicType() == EbtStruct)
3970 {
3971 error(loc, "ternary operator is not allowed for structures or arrays", ":");
Olli Etuahoa2d53032015-04-15 14:14:44 +03003972 return falseBlock;
3973 }
Corentin Wallez0d959252016-07-12 17:26:32 -04003974 // WebGL2 section 5.26, the following results in an error:
3975 // "Ternary operator applied to void, arrays, or structs containing arrays"
3976 if (mShaderSpec == SH_WEBGL2_SPEC && trueBlock->getBasicType() == EbtVoid)
3977 {
3978 error(loc, "ternary operator is not allowed for void", ":");
Corentin Wallez0d959252016-07-12 17:26:32 -04003979 return falseBlock;
3980 }
3981
Olli Etuaho52901742015-04-15 13:42:45 +03003982 return intermediate.addSelection(cond, trueBlock, falseBlock, loc);
3983}
Olli Etuaho49300862015-02-20 14:54:49 +02003984
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003985//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003986// Parse an array of strings using yyparse.
3987//
3988// Returns 0 for success.
3989//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003990int PaParseStrings(size_t count,
3991 const char *const string[],
3992 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05303993 TParseContext *context)
3994{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003995 if ((count == 0) || (string == NULL))
3996 return 1;
3997
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003998 if (glslang_initialize(context))
3999 return 1;
4000
alokp@chromium.org408c45e2012-04-05 15:54:43 +00004001 int error = glslang_scan(count, string, length, context);
4002 if (!error)
4003 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004004
alokp@chromium.org73bc2982012-06-19 18:48:05 +00004005 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00004006
alokp@chromium.org6b495712012-06-29 00:06:58 +00004007 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004008}