blob: 344c92fd71c5c0b93ed4fb0327a3a74ca786a977 [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
Martin Radev2cc85b32016-08-05 16:22:53 +030025namespace
26{
27
28const int kWebGLMaxStructNesting = 4;
29
30bool ContainsSampler(const TType &type)
31{
32 if (IsSampler(type.getBasicType()))
33 return true;
34
35 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock())
36 {
37 const TFieldList &fields = type.getStruct()->fields();
38 for (unsigned int i = 0; i < fields.size(); ++i)
39 {
40 if (ContainsSampler(*fields[i]->type()))
41 return true;
42 }
43 }
44
45 return false;
46}
47
48bool ContainsImage(const TType &type)
49{
50 if (IsImage(type.getBasicType()))
51 return true;
52
53 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock())
54 {
55 const TFieldList &fields = type.getStruct()->fields();
56 for (unsigned int i = 0; i < fields.size(); ++i)
57 {
58 if (ContainsImage(*fields[i]->type()))
59 return true;
60 }
61 }
62
63 return false;
64}
65
66} // namespace
67
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000068//
69// Look at a '.' field selector string and change it into offsets
70// for a vector.
71//
Jamie Madillb98c3a82015-07-23 14:26:04 -040072bool TParseContext::parseVectorFields(const TString &compString,
73 int vecSize,
74 TVectorFields &fields,
Arun Patole7e7e68d2015-05-22 12:02:25 +053075 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000076{
Jamie Madillb98c3a82015-07-23 14:26:04 -040077 fields.num = (int)compString.size();
Arun Patole7e7e68d2015-05-22 12:02:25 +053078 if (fields.num > 4)
79 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000080 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000081 return false;
82 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000083
Jamie Madillb98c3a82015-07-23 14:26:04 -040084 enum
85 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000086 exyzw,
87 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +000088 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000089 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000090
Arun Patole7e7e68d2015-05-22 12:02:25 +053091 for (int i = 0; i < fields.num; ++i)
92 {
93 switch (compString[i])
94 {
Jamie Madillb98c3a82015-07-23 14:26:04 -040095 case 'x':
96 fields.offsets[i] = 0;
97 fieldSet[i] = exyzw;
98 break;
99 case 'r':
100 fields.offsets[i] = 0;
101 fieldSet[i] = ergba;
102 break;
103 case 's':
104 fields.offsets[i] = 0;
105 fieldSet[i] = estpq;
106 break;
107 case 'y':
108 fields.offsets[i] = 1;
109 fieldSet[i] = exyzw;
110 break;
111 case 'g':
112 fields.offsets[i] = 1;
113 fieldSet[i] = ergba;
114 break;
115 case 't':
116 fields.offsets[i] = 1;
117 fieldSet[i] = estpq;
118 break;
119 case 'z':
120 fields.offsets[i] = 2;
121 fieldSet[i] = exyzw;
122 break;
123 case 'b':
124 fields.offsets[i] = 2;
125 fieldSet[i] = ergba;
126 break;
127 case 'p':
128 fields.offsets[i] = 2;
129 fieldSet[i] = estpq;
130 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530131
Jamie Madillb98c3a82015-07-23 14:26:04 -0400132 case 'w':
133 fields.offsets[i] = 3;
134 fieldSet[i] = exyzw;
135 break;
136 case 'a':
137 fields.offsets[i] = 3;
138 fieldSet[i] = ergba;
139 break;
140 case 'q':
141 fields.offsets[i] = 3;
142 fieldSet[i] = estpq;
143 break;
144 default:
145 error(line, "illegal vector field selection", compString.c_str());
146 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000147 }
148 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000149
Arun Patole7e7e68d2015-05-22 12:02:25 +0530150 for (int i = 0; i < fields.num; ++i)
151 {
152 if (fields.offsets[i] >= vecSize)
153 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400154 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000155 return false;
156 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000157
Arun Patole7e7e68d2015-05-22 12:02:25 +0530158 if (i > 0)
159 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400160 if (fieldSet[i] != fieldSet[i - 1])
Arun Patole7e7e68d2015-05-22 12:02:25 +0530161 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400162 error(line, "illegal - vector component fields not from the same set",
163 compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000164 return false;
165 }
166 }
167 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000168
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000169 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000170}
171
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000172///////////////////////////////////////////////////////////////////////
173//
174// Errors
175//
176////////////////////////////////////////////////////////////////////////
177
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000178
179//
180// Used by flex/bison to output all syntax and parsing errors.
181//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530182void TParseContext::error(const TSourceLoc &loc,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400183 const char *reason,
184 const char *token,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530185 const char *extraInfo)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000186{
Olli Etuaho1cc598f2016-08-18 13:50:30 +0300187 mDiagnostics.error(loc, reason, token, extraInfo);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000188}
189
Arun Patole7e7e68d2015-05-22 12:02:25 +0530190void TParseContext::warning(const TSourceLoc &loc,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400191 const char *reason,
192 const char *token,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530193 const char *extraInfo)
194{
Olli Etuaho1cc598f2016-08-18 13:50:30 +0300195 mDiagnostics.warning(loc, reason, token, extraInfo);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000196}
197
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200198void TParseContext::outOfRangeError(bool isError,
199 const TSourceLoc &loc,
200 const char *reason,
201 const char *token,
202 const char *extraInfo)
203{
204 if (isError)
205 {
206 error(loc, reason, token, extraInfo);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200207 }
208 else
209 {
210 warning(loc, reason, token, extraInfo);
211 }
212}
213
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000214//
215// Same error message for all places assignments don't work.
216//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530217void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000218{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000219 std::stringstream extraInfoStream;
220 extraInfoStream << "cannot convert from '" << right << "' to '" << left << "'";
221 std::string extraInfo = extraInfoStream.str();
222 error(line, "", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000223}
224
225//
226// Same error message for all places unary operations don't work.
227//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530228void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000229{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000230 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400231 extraInfoStream << "no operation '" << op << "' exists that takes an operand of type "
232 << operand << " (or there is no acceptable conversion)";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000233 std::string extraInfo = extraInfoStream.str();
234 error(line, " wrong operand type", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000235}
236
237//
238// Same error message for all binary operations don't work.
239//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400240void TParseContext::binaryOpError(const TSourceLoc &line,
241 const char *op,
242 TString left,
243 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000244{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000245 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400246 extraInfoStream << "no operation '" << op << "' exists that takes a left-hand operand of type '"
247 << left << "' and a right operand of type '" << right
248 << "' (or there is no acceptable conversion)";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000249 std::string extraInfo = extraInfoStream.str();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530250 error(line, " wrong operand types ", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000251}
252
Olli Etuaho856c4972016-08-08 11:38:39 +0300253void TParseContext::checkPrecisionSpecified(const TSourceLoc &line,
254 TPrecision precision,
255 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530256{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400257 if (!mChecksPrecisionErrors)
Olli Etuaho383b7912016-08-05 11:22:59 +0300258 return;
Martin Radev70866b82016-07-22 15:27:42 +0300259
260 if (precision != EbpUndefined && !SupportsPrecision(type))
261 {
262 error(line, "illegal type for precision qualifier", getBasicString(type));
263 }
264
Olli Etuaho183d7e22015-11-20 15:59:09 +0200265 if (precision == EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530266 {
Olli Etuaho183d7e22015-11-20 15:59:09 +0200267 switch (type)
268 {
269 case EbtFloat:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400270 error(line, "No precision specified for (float)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300271 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200272 case EbtInt:
273 case EbtUInt:
274 UNREACHABLE(); // there's always a predeclared qualifier
Jamie Madillb98c3a82015-07-23 14:26:04 -0400275 error(line, "No precision specified (int)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300276 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200277 default:
278 if (IsSampler(type))
279 {
280 error(line, "No precision specified (sampler)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300281 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200282 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300283 if (IsImage(type))
284 {
285 error(line, "No precision specified (image)", "");
286 return;
287 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200288 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000289 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000290}
291
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000292// Both test and if necessary, spit out an error, to see if the node is really
293// an l-value that can be operated on this way.
Olli Etuaho856c4972016-08-08 11:38:39 +0300294bool TParseContext::checkCanBeLValue(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000295{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400296 TIntermSymbol *symNode = node->getAsSymbolNode();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530297 TIntermBinary *binaryNode = node->getAsBinaryNode();
Olli Etuahob6fa0432016-09-28 16:28:05 +0100298 TIntermSwizzle *swizzleNode = node->getAsSwizzleNode();
299
300 if (swizzleNode)
301 {
302 bool ok = checkCanBeLValue(line, op, swizzleNode->getOperand());
303 if (ok && swizzleNode->hasDuplicateOffsets())
304 {
305 error(line, " l-value of swizzle cannot have duplicate components", op);
306 return false;
307 }
308 return ok;
309 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000310
Arun Patole7e7e68d2015-05-22 12:02:25 +0530311 if (binaryNode)
312 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400313 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530314 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400315 case EOpIndexDirect:
316 case EOpIndexIndirect:
317 case EOpIndexDirectStruct:
318 case EOpIndexDirectInterfaceBlock:
Olli Etuaho856c4972016-08-08 11:38:39 +0300319 return checkCanBeLValue(line, op, binaryNode->getLeft());
Jamie Madillb98c3a82015-07-23 14:26:04 -0400320 default:
321 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000322 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000323 error(line, " l-value required", op);
Olli Etuaho8a176262016-08-16 14:23:01 +0300324 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000325 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000326
Arun Patole7e7e68d2015-05-22 12:02:25 +0530327 const char *symbol = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000328 if (symNode != 0)
329 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000330
Arun Patole7e7e68d2015-05-22 12:02:25 +0530331 const char *message = 0;
332 switch (node->getQualifier())
333 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400334 case EvqConst:
335 message = "can't modify a const";
336 break;
337 case EvqConstReadOnly:
338 message = "can't modify a const";
339 break;
340 case EvqAttribute:
341 message = "can't modify an attribute";
342 break;
343 case EvqFragmentIn:
344 message = "can't modify an input";
345 break;
346 case EvqVertexIn:
347 message = "can't modify an input";
348 break;
349 case EvqUniform:
350 message = "can't modify a uniform";
351 break;
352 case EvqVaryingIn:
353 message = "can't modify a varying";
354 break;
355 case EvqFragCoord:
356 message = "can't modify gl_FragCoord";
357 break;
358 case EvqFrontFacing:
359 message = "can't modify gl_FrontFacing";
360 break;
361 case EvqPointCoord:
362 message = "can't modify gl_PointCoord";
363 break;
Martin Radevb0883602016-08-04 17:48:58 +0300364 case EvqNumWorkGroups:
365 message = "can't modify gl_NumWorkGroups";
366 break;
367 case EvqWorkGroupSize:
368 message = "can't modify gl_WorkGroupSize";
369 break;
370 case EvqWorkGroupID:
371 message = "can't modify gl_WorkGroupID";
372 break;
373 case EvqLocalInvocationID:
374 message = "can't modify gl_LocalInvocationID";
375 break;
376 case EvqGlobalInvocationID:
377 message = "can't modify gl_GlobalInvocationID";
378 break;
379 case EvqLocalInvocationIndex:
380 message = "can't modify gl_LocalInvocationIndex";
381 break;
Martin Radev802abe02016-08-04 17:48:32 +0300382 case EvqComputeIn:
383 message = "can't modify work group size variable";
384 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400385 default:
386 //
387 // Type that can't be written to?
388 //
389 if (node->getBasicType() == EbtVoid)
390 {
391 message = "can't modify void";
392 }
393 if (IsSampler(node->getBasicType()))
394 {
395 message = "can't modify a sampler";
396 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300397 if (IsImage(node->getBasicType()))
398 {
399 message = "can't modify an image";
400 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000401 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000402
Arun Patole7e7e68d2015-05-22 12:02:25 +0530403 if (message == 0 && binaryNode == 0 && symNode == 0)
404 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000405 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000406
Olli Etuaho8a176262016-08-16 14:23:01 +0300407 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000408 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000409
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000410 //
411 // Everything else is okay, no error.
412 //
413 if (message == 0)
Olli Etuaho8a176262016-08-16 14:23:01 +0300414 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000415
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000416 //
417 // If we get here, we have an error and a message.
418 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530419 if (symNode)
420 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000421 std::stringstream extraInfoStream;
422 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
423 std::string extraInfo = extraInfoStream.str();
424 error(line, " l-value required", op, extraInfo.c_str());
425 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530426 else
427 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000428 std::stringstream extraInfoStream;
429 extraInfoStream << "(" << message << ")";
430 std::string extraInfo = extraInfoStream.str();
431 error(line, " l-value required", op, extraInfo.c_str());
432 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000433
Olli Etuaho8a176262016-08-16 14:23:01 +0300434 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000435}
436
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000437// Both test, and if necessary spit out an error, to see if the node is really
438// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300439void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000440{
Olli Etuaho383b7912016-08-05 11:22:59 +0300441 if (node->getQualifier() != EvqConst)
442 {
443 error(node->getLine(), "constant expression required", "");
444 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000445}
446
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000447// Both test, and if necessary spit out an error, to see if the node is really
448// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300449void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000450{
Olli Etuaho383b7912016-08-05 11:22:59 +0300451 if (!node->isScalarInt())
452 {
453 error(node->getLine(), "integer expression required", token);
454 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000455}
456
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000457// Both test, and if necessary spit out an error, to see if we are currently
458// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800459bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000460{
Olli Etuaho856c4972016-08-08 11:38:39 +0300461 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300462 {
463 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800464 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300465 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800466 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000467}
468
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000469// For now, keep it simple: if it starts "gl_", it's reserved, independent
470// of scope. Except, if the symbol table is at the built-in push-level,
471// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000472// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
473// webgl shader.
Olli Etuaho856c4972016-08-08 11:38:39 +0300474bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000475{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530476 static const char *reservedErrMsg = "reserved built-in name";
477 if (!symbolTable.atBuiltInLevel())
478 {
479 if (identifier.compare(0, 3, "gl_") == 0)
480 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000481 error(line, reservedErrMsg, "gl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300482 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000483 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530484 if (IsWebGLBasedSpec(mShaderSpec))
485 {
486 if (identifier.compare(0, 6, "webgl_") == 0)
487 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000488 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300489 return false;
alokp@chromium.org613ef312010-07-21 18:54:22 +0000490 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530491 if (identifier.compare(0, 7, "_webgl_") == 0)
492 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000493 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300494 return false;
alokp@chromium.org613ef312010-07-21 18:54:22 +0000495 }
496 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530497 if (identifier.find("__") != TString::npos)
498 {
499 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400500 "identifiers containing two consecutive underscores (__) are reserved as "
501 "possible future keywords",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530502 identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300503 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000504 }
505 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000506
Olli Etuaho8a176262016-08-16 14:23:01 +0300507 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000508}
509
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000510// Make sure there is enough data provided to the constructor to build
511// something of the type of the constructor. Also returns the type of
512// the constructor.
Olli Etuaho856c4972016-08-08 11:38:39 +0300513bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
514 TIntermNode *argumentsNode,
515 const TFunction &function,
516 TOperator op,
517 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000518{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000519 bool constructingMatrix = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400520 switch (op)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530521 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400522 case EOpConstructMat2:
523 case EOpConstructMat2x3:
524 case EOpConstructMat2x4:
525 case EOpConstructMat3x2:
526 case EOpConstructMat3:
527 case EOpConstructMat3x4:
528 case EOpConstructMat4x2:
529 case EOpConstructMat4x3:
530 case EOpConstructMat4:
531 constructingMatrix = true;
532 break;
533 default:
534 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000535 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000536
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000537 //
538 // Note: It's okay to have too many components available, but not okay to have unused
539 // arguments. 'full' will go to true when enough args have been seen. If we loop
540 // again, there is an extra argument, so 'overfull' will become true.
541 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000542
Jamie Madillb98c3a82015-07-23 14:26:04 -0400543 size_t size = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400544 bool full = false;
545 bool overFull = false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000546 bool matrixInMatrix = false;
547 bool arrayArg = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530548 for (size_t i = 0; i < function.getParamCount(); ++i)
549 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700550 const TConstParameter &param = function.getParam(i);
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000551 size += param.type->getObjectSize();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530552
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000553 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000554 matrixInMatrix = true;
555 if (full)
556 overFull = true;
Olli Etuaho856c4972016-08-08 11:38:39 +0300557 if (op != EOpConstructStruct && !type.isArray() && size >= type.getObjectSize())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000558 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000559 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000560 arrayArg = true;
561 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530562
Olli Etuaho856c4972016-08-08 11:38:39 +0300563 if (type.isArray())
Olli Etuaho376f1b52015-04-13 13:23:41 +0300564 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300565 // The size of an unsized constructor should already have been determined.
566 ASSERT(!type.isUnsizedArray());
567 if (static_cast<size_t>(type.getArraySize()) != function.getParamCount())
Olli Etuaho376f1b52015-04-13 13:23:41 +0300568 {
569 error(line, "array constructor needs one argument per array element", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300570 return false;
Olli Etuaho376f1b52015-04-13 13:23:41 +0300571 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000572 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000573
Arun Patole7e7e68d2015-05-22 12:02:25 +0530574 if (arrayArg && op != EOpConstructStruct)
575 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000576 error(line, "constructing from a non-dereferenced array", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300577 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000578 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000579
Olli Etuaho856c4972016-08-08 11:38:39 +0300580 if (matrixInMatrix && !type.isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530581 {
582 if (function.getParamCount() != 1)
583 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400584 error(line, "constructing matrix from matrix can only take one argument",
585 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300586 return false;
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000587 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000588 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000589
Arun Patole7e7e68d2015-05-22 12:02:25 +0530590 if (overFull)
591 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000592 error(line, "too many arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300593 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000594 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530595
Olli Etuaho856c4972016-08-08 11:38:39 +0300596 if (op == EOpConstructStruct && !type.isArray() &&
597 type.getStruct()->fields().size() != function.getParamCount())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530598 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400599 error(line,
600 "Number of constructor parameters does not match the number of structure fields",
601 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300602 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000603 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000604
Olli Etuaho856c4972016-08-08 11:38:39 +0300605 if (!type.isMatrix() || !matrixInMatrix)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530606 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300607 if ((op != EOpConstructStruct && size != 1 && size < type.getObjectSize()) ||
608 (op == EOpConstructStruct && size < type.getObjectSize()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530609 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000610 error(line, "not enough data provided for construction", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300611 return false;
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000612 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000613 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000614
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200615 if (argumentsNode == nullptr)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530616 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200617 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300618 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000619 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200620
621 TIntermAggregate *argumentsAgg = argumentsNode->getAsAggregate();
622 for (TIntermNode *&argNode : *argumentsAgg->getSequence())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530623 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200624 TIntermTyped *argTyped = argNode->getAsTyped();
625 ASSERT(argTyped != nullptr);
626 if (op != EOpConstructStruct && IsSampler(argTyped->getBasicType()))
627 {
628 error(line, "cannot convert a sampler", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300629 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200630 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300631 if (op != EOpConstructStruct && IsImage(argTyped->getBasicType()))
632 {
633 error(line, "cannot convert an image", "constructor");
634 return false;
635 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200636 if (argTyped->getBasicType() == EbtVoid)
637 {
638 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300639 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200640 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000641 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000642
Olli Etuaho856c4972016-08-08 11:38:39 +0300643 if (type.isArray())
644 {
645 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
646 // the array.
647 for (TIntermNode *&argNode : *argumentsAgg->getSequence())
648 {
649 const TType &argType = argNode->getAsTyped()->getType();
650 // It has already been checked that the argument is not an array.
651 ASSERT(!argType.isArray());
652 if (!argType.sameElementType(type))
653 {
654 error(line, "Array constructor argument has an incorrect type", "Error");
Olli Etuaho8a176262016-08-16 14:23:01 +0300655 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300656 }
657 }
658 }
659 else if (op == EOpConstructStruct)
660 {
661 const TFieldList &fields = type.getStruct()->fields();
662 TIntermSequence *args = argumentsAgg->getSequence();
663
664 for (size_t i = 0; i < fields.size(); i++)
665 {
666 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
667 {
668 error(line, "Structure constructor arguments do not match structure fields",
669 "Error");
Olli Etuaho8a176262016-08-16 14:23:01 +0300670 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300671 }
672 }
673 }
674
Olli Etuaho8a176262016-08-16 14:23:01 +0300675 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000676}
677
Jamie Madillb98c3a82015-07-23 14:26:04 -0400678// This function checks to see if a void variable has been declared and raise an error message for
679// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000680//
681// returns true in case of an error
682//
Olli Etuaho856c4972016-08-08 11:38:39 +0300683bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400684 const TString &identifier,
685 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000686{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300687 if (type == EbtVoid)
688 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000689 error(line, "illegal use of type 'void'", identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300690 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300691 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000692
Olli Etuaho8a176262016-08-16 14:23:01 +0300693 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000694}
695
Jamie Madillb98c3a82015-07-23 14:26:04 -0400696// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300697// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300698void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000699{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530700 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector())
701 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000702 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530703 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000704}
705
Jamie Madillb98c3a82015-07-23 14:26:04 -0400706// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300707// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300708void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000709{
Martin Radev4a9cd802016-09-01 16:51:51 +0300710 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530711 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000712 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530713 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000714}
715
Olli Etuaho856c4972016-08-08 11:38:39 +0300716bool TParseContext::checkIsNotSampler(const TSourceLoc &line,
Martin Radev4a9cd802016-09-01 16:51:51 +0300717 const TTypeSpecifierNonArray &pType,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400718 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000719{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530720 if (pType.type == EbtStruct)
721 {
Martin Radev2cc85b32016-08-05 16:22:53 +0300722 if (ContainsSampler(*pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530723 {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000724 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
Olli Etuaho8a176262016-08-16 14:23:01 +0300725 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000726 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530727
Olli Etuaho8a176262016-08-16 14:23:01 +0300728 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530729 }
730 else if (IsSampler(pType.type))
731 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000732 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300733 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000734 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000735
Olli Etuaho8a176262016-08-16 14:23:01 +0300736 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000737}
738
Martin Radev2cc85b32016-08-05 16:22:53 +0300739bool TParseContext::checkIsNotImage(const TSourceLoc &line,
740 const TTypeSpecifierNonArray &pType,
741 const char *reason)
742{
743 if (pType.type == EbtStruct)
744 {
745 if (ContainsImage(*pType.userDef))
746 {
747 error(line, reason, getBasicString(pType.type), "(structure contains an image)");
748
749 return false;
750 }
751
752 return true;
753 }
754 else if (IsImage(pType.type))
755 {
756 error(line, reason, getBasicString(pType.type));
757
758 return false;
759 }
760
761 return true;
762}
763
Olli Etuaho856c4972016-08-08 11:38:39 +0300764void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
765 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400766{
767 if (pType.layoutQualifier.location != -1)
768 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400769 error(line, "location must only be specified for a single input or output variable",
770 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400771 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400772}
773
Olli Etuaho856c4972016-08-08 11:38:39 +0300774void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
775 const TLayoutQualifier &layoutQualifier)
776{
777 if (layoutQualifier.location != -1)
778 {
779 error(location, "invalid layout qualifier:", "location",
780 "only valid on program inputs and outputs");
781 }
782}
783
Martin Radev2cc85b32016-08-05 16:22:53 +0300784void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
785 TQualifier qualifier,
786 const TType &type)
787{
788 checkOutParameterIsNotSampler(line, qualifier, type);
789 checkOutParameterIsNotImage(line, qualifier, type);
790}
791
Olli Etuaho856c4972016-08-08 11:38:39 +0300792void TParseContext::checkOutParameterIsNotSampler(const TSourceLoc &line,
793 TQualifier qualifier,
794 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000795{
Martin Radev2cc85b32016-08-05 16:22:53 +0300796 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
797 if (IsSampler(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530798 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000799 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000800 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000801}
802
Martin Radev2cc85b32016-08-05 16:22:53 +0300803void TParseContext::checkOutParameterIsNotImage(const TSourceLoc &line,
804 TQualifier qualifier,
805 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000806{
Martin Radev2cc85b32016-08-05 16:22:53 +0300807 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
808 if (IsImage(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530809 {
Martin Radev2cc85b32016-08-05 16:22:53 +0300810 error(line, "images cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000811 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000812}
813
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000814// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300815unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000816{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530817 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000818
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200819 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
820 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
821 // fold as array size.
822 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000823 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000824 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300825 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000826 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000827
Olli Etuaho856c4972016-08-08 11:38:39 +0300828 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400829
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000830 if (constant->getBasicType() == EbtUInt)
831 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300832 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000833 }
834 else
835 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300836 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000837
Olli Etuaho856c4972016-08-08 11:38:39 +0300838 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000839 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400840 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300841 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000842 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400843
Olli Etuaho856c4972016-08-08 11:38:39 +0300844 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400845 }
846
Olli Etuaho856c4972016-08-08 11:38:39 +0300847 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400848 {
849 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300850 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400851 }
852
853 // The size of arrays is restricted here to prevent issues further down the
854 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
855 // 4096 registers so this should be reasonable even for aggressively optimizable code.
856 const unsigned int sizeLimit = 65536;
857
Olli Etuaho856c4972016-08-08 11:38:39 +0300858 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -0400859 {
860 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300861 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000862 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300863
864 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000865}
866
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000867// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +0300868bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
869 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000870{
Olli Etuaho8a176262016-08-16 14:23:01 +0300871 if ((elementQualifier.qualifier == EvqAttribute) ||
872 (elementQualifier.qualifier == EvqVertexIn) ||
873 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300874 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400875 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300876 TType(elementQualifier).getQualifierString());
877 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000878 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000879
Olli Etuaho8a176262016-08-16 14:23:01 +0300880 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000881}
882
Olli Etuaho8a176262016-08-16 14:23:01 +0300883// See if this element type can be formed into an array.
884bool TParseContext::checkIsValidTypeForArray(const TSourceLoc &line, const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000885{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000886 //
887 // Can the type be an array?
888 //
Olli Etuaho8a176262016-08-16 14:23:01 +0300889 if (elementType.array)
Jamie Madill06145232015-05-13 13:10:01 -0400890 {
Olli Etuaho8a176262016-08-16 14:23:01 +0300891 error(line, "cannot declare arrays of arrays",
892 TType(elementType).getCompleteString().c_str());
893 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000894 }
Olli Etuahocc36b982015-07-10 14:14:18 +0300895 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
896 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
897 // 4.3.4).
Martin Radev4a9cd802016-09-01 16:51:51 +0300898 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Olli Etuaho8a176262016-08-16 14:23:01 +0300899 sh::IsVarying(elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +0300900 {
901 error(line, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300902 TType(elementType).getCompleteString().c_str());
903 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +0300904 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000905
Olli Etuaho8a176262016-08-16 14:23:01 +0300906 return true;
907}
908
909// Check if this qualified element type can be formed into an array.
910bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
911 const TPublicType &elementType)
912{
913 if (checkIsValidTypeForArray(indexLocation, elementType))
914 {
915 return checkIsValidQualifierForArray(indexLocation, elementType);
916 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000917 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000918}
919
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000920// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +0300921void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
922 const TString &identifier,
923 TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000924{
Olli Etuaho3739d232015-04-08 12:23:44 +0300925 ASSERT(type != nullptr);
926 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000927 {
928 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300929 type->qualifier = EvqTemporary;
930
931 // Generate informative error messages for ESSL1.
932 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400933 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000934 {
Arun Patole7e7e68d2015-05-22 12:02:25 +0530935 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400936 "structures containing arrays may not be declared constant since they cannot be "
937 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530938 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000939 }
940 else
941 {
942 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
943 }
Olli Etuaho383b7912016-08-05 11:22:59 +0300944 return;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000945 }
Olli Etuaho376f1b52015-04-13 13:23:41 +0300946 if (type->isUnsizedArray())
947 {
948 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
Olli Etuaho376f1b52015-04-13 13:23:41 +0300949 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000950}
951
Olli Etuaho2935c582015-04-08 14:32:06 +0300952// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000953// and update the symbol table.
954//
Olli Etuaho2935c582015-04-08 14:32:06 +0300955// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000956//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400957bool TParseContext::declareVariable(const TSourceLoc &line,
958 const TString &identifier,
959 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +0300960 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000961{
Olli Etuaho2935c582015-04-08 14:32:06 +0300962 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000963
Olli Etuaho856c4972016-08-08 11:38:39 +0300964 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000965
Olli Etuaho2935c582015-04-08 14:32:06 +0300966 // gl_LastFragData may be redeclared with a new precision qualifier
967 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
968 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400969 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
970 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho856c4972016-08-08 11:38:39 +0300971 if (static_cast<int>(type.getArraySize()) == maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +0300972 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400973 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +0300974 {
Olli Etuaho8a176262016-08-16 14:23:01 +0300975 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->getExtension());
Olli Etuaho2935c582015-04-08 14:32:06 +0300976 }
977 }
978 else
979 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400980 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
981 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +0300982 return false;
983 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000984 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000985
Olli Etuaho8a176262016-08-16 14:23:01 +0300986 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +0300987 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000988
Olli Etuaho2935c582015-04-08 14:32:06 +0300989 (*variable) = new TVariable(&identifier, type);
990 if (!symbolTable.declare(*variable))
991 {
992 error(line, "redefinition", identifier.c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -0400993 *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +0300994 return false;
995 }
996
Olli Etuaho8a176262016-08-16 14:23:01 +0300997 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +0300998 return false;
999
1000 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001001}
1002
Martin Radev70866b82016-07-22 15:27:42 +03001003void TParseContext::checkIsParameterQualifierValid(
1004 const TSourceLoc &line,
1005 const TTypeQualifierBuilder &typeQualifierBuilder,
1006 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301007{
Olli Etuaho613b9592016-09-05 12:05:53 +03001008 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(&mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001009
1010 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301011 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001012 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1013 }
1014
1015 if (!IsImage(type->getBasicType()))
1016 {
1017 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, line);
1018 }
1019 else
1020 {
1021 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001022 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001023
Martin Radev70866b82016-07-22 15:27:42 +03001024 type->setQualifier(typeQualifier.qualifier);
1025
1026 if (typeQualifier.precision != EbpUndefined)
1027 {
1028 type->setPrecision(typeQualifier.precision);
1029 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001030}
1031
Olli Etuaho856c4972016-08-08 11:38:39 +03001032bool TParseContext::checkCanUseExtension(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001033{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001034 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001035 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +05301036 if (iter == extBehavior.end())
1037 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001038 error(line, "extension", extension.c_str(), "is not supported");
Olli Etuaho8a176262016-08-16 14:23:01 +03001039 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001040 }
zmo@google.comf5450912011-09-09 01:37:19 +00001041 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +05301042 if (iter->second == EBhDisable || iter->second == EBhUndefined)
1043 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001044 error(line, "extension", extension.c_str(), "is disabled");
Olli Etuaho8a176262016-08-16 14:23:01 +03001045 return false;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001046 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301047 if (iter->second == EBhWarn)
1048 {
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001049 warning(line, "extension", extension.c_str(), "is being used");
Olli Etuaho8a176262016-08-16 14:23:01 +03001050 return true;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001051 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001052
Olli Etuaho8a176262016-08-16 14:23:01 +03001053 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001054}
1055
Jamie Madillb98c3a82015-07-23 14:26:04 -04001056// These checks are common for all declarations starting a declarator list, and declarators that
1057// follow an empty declaration.
Olli Etuaho383b7912016-08-05 11:22:59 +03001058void TParseContext::singleDeclarationErrorCheck(const TPublicType &publicType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001059 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001060{
Olli Etuahofa33d582015-04-09 14:33:12 +03001061 switch (publicType.qualifier)
1062 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001063 case EvqVaryingIn:
1064 case EvqVaryingOut:
1065 case EvqAttribute:
1066 case EvqVertexIn:
1067 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001068 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001069 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001070 {
1071 error(identifierLocation, "cannot be used with a structure",
1072 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001073 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001074 }
Olli Etuahofa33d582015-04-09 14:33:12 +03001075
Jamie Madillb98c3a82015-07-23 14:26:04 -04001076 default:
1077 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001078 }
1079
Jamie Madillb98c3a82015-07-23 14:26:04 -04001080 if (publicType.qualifier != EvqUniform &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001081 !checkIsNotSampler(identifierLocation, publicType.typeSpecifierNonArray,
1082 "samplers must be uniform"))
Olli Etuahofa33d582015-04-09 14:33:12 +03001083 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001084 return;
Olli Etuahofa33d582015-04-09 14:33:12 +03001085 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001086 if (publicType.qualifier != EvqUniform &&
1087 !checkIsNotImage(identifierLocation, publicType.typeSpecifierNonArray,
1088 "images must be uniform"))
1089 {
1090 return;
1091 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001092
1093 // check for layout qualifier issues
1094 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
1095
1096 if (layoutQualifier.matrixPacking != EmpUnspecified)
1097 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001098 error(identifierLocation, "layout qualifier",
1099 getMatrixPackingString(layoutQualifier.matrixPacking),
Olli Etuahofa33d582015-04-09 14:33:12 +03001100 "only valid for interface blocks");
Olli Etuaho383b7912016-08-05 11:22:59 +03001101 return;
Jamie Madilla5efff92013-06-06 11:56:47 -04001102 }
1103
1104 if (layoutQualifier.blockStorage != EbsUnspecified)
1105 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001106 error(identifierLocation, "layout qualifier",
1107 getBlockStorageString(layoutQualifier.blockStorage),
Olli Etuahofa33d582015-04-09 14:33:12 +03001108 "only valid for interface blocks");
Olli Etuaho383b7912016-08-05 11:22:59 +03001109 return;
Jamie Madilla5efff92013-06-06 11:56:47 -04001110 }
1111
Olli Etuaho383b7912016-08-05 11:22:59 +03001112 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut)
Jamie Madilla5efff92013-06-06 11:56:47 -04001113 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001114 checkLocationIsNotSpecified(identifierLocation, publicType.layoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04001115 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001116
1117 if (IsImage(publicType.getBasicType()))
1118 {
1119
1120 switch (layoutQualifier.imageInternalFormat)
1121 {
1122 case EiifRGBA32F:
1123 case EiifRGBA16F:
1124 case EiifR32F:
1125 case EiifRGBA8:
1126 case EiifRGBA8_SNORM:
1127 if (!IsFloatImage(publicType.getBasicType()))
1128 {
1129 error(identifierLocation,
1130 "internal image format requires a floating image type",
1131 getBasicString(publicType.getBasicType()));
1132 return;
1133 }
1134 break;
1135 case EiifRGBA32I:
1136 case EiifRGBA16I:
1137 case EiifRGBA8I:
1138 case EiifR32I:
1139 if (!IsIntegerImage(publicType.getBasicType()))
1140 {
1141 error(identifierLocation,
1142 "internal image format requires an integer image type",
1143 getBasicString(publicType.getBasicType()));
1144 return;
1145 }
1146 break;
1147 case EiifRGBA32UI:
1148 case EiifRGBA16UI:
1149 case EiifRGBA8UI:
1150 case EiifR32UI:
1151 if (!IsUnsignedImage(publicType.getBasicType()))
1152 {
1153 error(identifierLocation,
1154 "internal image format requires an unsigned image type",
1155 getBasicString(publicType.getBasicType()));
1156 return;
1157 }
1158 break;
1159 case EiifUnspecified:
1160 error(identifierLocation, "layout qualifier", "No image internal format specified");
1161 return;
1162 default:
1163 error(identifierLocation, "layout qualifier", "unrecognized token");
1164 return;
1165 }
1166
1167 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1168 switch (layoutQualifier.imageInternalFormat)
1169 {
1170 case EiifR32F:
1171 case EiifR32I:
1172 case EiifR32UI:
1173 break;
1174 default:
1175 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1176 {
1177 error(identifierLocation, "layout qualifier",
1178 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1179 "image variables must be qualified readonly and/or writeonly");
1180 return;
1181 }
1182 break;
1183 }
1184 }
1185 else
1186 {
1187
1188 if (!checkInternalFormatIsNotSpecified(identifierLocation,
1189 layoutQualifier.imageInternalFormat))
1190 {
1191 return;
1192 }
1193
1194 if (!checkIsMemoryQualifierNotSpecified(publicType.memoryQualifier, identifierLocation))
1195 {
1196 return;
1197 }
1198 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001199}
1200
Olli Etuaho856c4972016-08-08 11:38:39 +03001201void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1202 const TString &layoutQualifierName,
1203 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001204{
1205
1206 if (mShaderVersion < versionRequired)
1207 {
1208 error(location, "invalid layout qualifier:", layoutQualifierName.c_str(), "not supported");
Martin Radev802abe02016-08-04 17:48:32 +03001209 }
1210}
1211
Olli Etuaho856c4972016-08-08 11:38:39 +03001212bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1213 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001214{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001215 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001216 for (size_t i = 0u; i < localSize.size(); ++i)
1217 {
1218 if (localSize[i] != -1)
1219 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03001220 error(location, "invalid layout qualifier:", getWorkGroupSizeString(i),
Martin Radev802abe02016-08-04 17:48:32 +03001221 "only valid when used with 'in' in a compute shader global layout declaration");
Olli Etuaho8a176262016-08-16 14:23:01 +03001222 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001223 }
1224 }
1225
Olli Etuaho8a176262016-08-16 14:23:01 +03001226 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001227}
1228
Martin Radev2cc85b32016-08-05 16:22:53 +03001229bool TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
1230 TLayoutImageInternalFormat internalFormat)
1231{
1232 if (internalFormat != EiifUnspecified)
1233 {
1234 error(location, "invalid layout qualifier:", getImageInternalFormatString(internalFormat),
1235 "only valid when used with images");
1236 return false;
1237 }
1238 return true;
1239}
1240
Olli Etuaho383b7912016-08-05 11:22:59 +03001241void TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate,
Olli Etuaho856c4972016-08-08 11:38:39 +03001242 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001243{
1244 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1245 {
1246 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1247 if (qual == EvqOut || qual == EvqInOut)
1248 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001249 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
Olli Etuaho8a176262016-08-16 14:23:01 +03001250 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001251 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001252 error(argument->getLine(),
Jamie Madillb98c3a82015-07-23 14:26:04 -04001253 "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error");
Olli Etuaho383b7912016-08-05 11:22:59 +03001254 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001255 }
1256 }
1257 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001258}
1259
Martin Radev70866b82016-07-22 15:27:42 +03001260void TParseContext::checkInvariantVariableQualifier(bool invariant,
1261 const TQualifier qualifier,
1262 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001263{
Martin Radev70866b82016-07-22 15:27:42 +03001264 if (!invariant)
1265 return;
1266
1267 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001268 {
Martin Radev70866b82016-07-22 15:27:42 +03001269 // input variables in the fragment shader can be also qualified as invariant
1270 if (!sh::CanBeInvariantESSL1(qualifier))
1271 {
1272 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1273 }
1274 }
1275 else
1276 {
1277 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1278 {
1279 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1280 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001281 }
1282}
1283
Arun Patole7e7e68d2015-05-22 12:02:25 +05301284bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001285{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001286 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001287 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1288 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001289}
1290
Arun Patole7e7e68d2015-05-22 12:02:25 +05301291bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001292{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001293 return ::IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001294}
1295
Jamie Madillb98c3a82015-07-23 14:26:04 -04001296void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1297 const char *extName,
1298 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001299{
1300 pp::SourceLocation srcLoc;
1301 srcLoc.file = loc.first_file;
1302 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001303 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001304}
1305
Jamie Madillb98c3a82015-07-23 14:26:04 -04001306void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1307 const char *name,
1308 const char *value,
1309 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001310{
1311 pp::SourceLocation srcLoc;
1312 srcLoc.file = loc.first_file;
1313 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001314 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001315}
1316
Martin Radev4c4c8e72016-08-04 12:25:34 +03001317sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001318{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001319 sh::WorkGroupSize result;
Martin Radev802abe02016-08-04 17:48:32 +03001320 for (size_t i = 0u; i < result.size(); ++i)
1321 {
1322 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1323 {
1324 result[i] = 1;
1325 }
1326 else
1327 {
1328 result[i] = mComputeShaderLocalSize[i];
1329 }
1330 }
1331 return result;
1332}
1333
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001334/////////////////////////////////////////////////////////////////////////////////
1335//
1336// Non-Errors.
1337//
1338/////////////////////////////////////////////////////////////////////////////////
1339
Jamie Madill5c097022014-08-20 16:38:32 -04001340const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1341 const TString *name,
1342 const TSymbol *symbol)
1343{
1344 const TVariable *variable = NULL;
1345
1346 if (!symbol)
1347 {
1348 error(location, "undeclared identifier", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001349 }
1350 else if (!symbol->isVariable())
1351 {
1352 error(location, "variable expected", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001353 }
1354 else
1355 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001356 variable = static_cast<const TVariable *>(symbol);
Jamie Madill5c097022014-08-20 16:38:32 -04001357
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001358 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
Olli Etuaho383b7912016-08-05 11:22:59 +03001359 !variable->getExtension().empty())
Jamie Madill5c097022014-08-20 16:38:32 -04001360 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001361 checkCanUseExtension(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001362 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001363
1364 // Reject shaders using both gl_FragData and gl_FragColor
1365 TQualifier qualifier = variable->getType().getQualifier();
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001366 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001367 {
1368 mUsesFragData = true;
1369 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001370 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001371 {
1372 mUsesFragColor = true;
1373 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001374 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1375 {
1376 mUsesSecondaryOutputs = true;
1377 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001378
1379 // This validation is not quite correct - it's only an error to write to
1380 // both FragData and FragColor. For simplicity, and because users shouldn't
1381 // be rewarded for reading from undefined varaibles, return an error
1382 // if they are both referenced, rather than assigned.
1383 if (mUsesFragData && mUsesFragColor)
1384 {
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001385 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1386 if (mUsesSecondaryOutputs)
1387 {
1388 errorMessage =
1389 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1390 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1391 }
1392 error(location, errorMessage, name->c_str());
Jamie Madill14e95b32015-05-07 10:10:41 -04001393 }
Martin Radevb0883602016-08-04 17:48:58 +03001394
1395 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1396 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1397 qualifier == EvqWorkGroupSize)
1398 {
1399 error(location,
1400 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1401 "gl_WorkGroupSize");
1402 }
Jamie Madill5c097022014-08-20 16:38:32 -04001403 }
1404
1405 if (!variable)
1406 {
1407 TType type(EbtFloat, EbpUndefined);
1408 TVariable *fakeVariable = new TVariable(name, type);
1409 symbolTable.declare(fakeVariable);
1410 variable = fakeVariable;
1411 }
1412
1413 return variable;
1414}
1415
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001416TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1417 const TString *name,
1418 const TSymbol *symbol)
1419{
1420 const TVariable *variable = getNamedVariable(location, name, symbol);
1421
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001422 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001423 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001424 const TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001425 return intermediate.addConstantUnion(constArray, variable->getType(), location);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001426 }
1427 else
1428 {
1429 return intermediate.addSymbol(variable->getUniqueId(), variable->getName(),
1430 variable->getType(), location);
1431 }
1432}
1433
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001434//
1435// Look up a function name in the symbol table, and make sure it is a function.
1436//
1437// Return the function symbol if found, otherwise 0.
1438//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001439const TFunction *TParseContext::findFunction(const TSourceLoc &line,
1440 TFunction *call,
1441 int inputShaderVersion,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301442 bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001443{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001444 // First find by unmangled name to check whether the function name has been
1445 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001446 // If a function is found, check for one with a matching argument list.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301447 const TSymbol *symbol = symbolTable.find(call->getName(), inputShaderVersion, builtIn);
1448 if (symbol == 0 || symbol->isFunction())
1449 {
Austin Kinross3ae64652015-01-26 15:51:39 -08001450 symbol = symbolTable.find(call->getMangledName(), inputShaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001451 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001452
Arun Patole7e7e68d2015-05-22 12:02:25 +05301453 if (symbol == 0)
1454 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001455 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001456 return 0;
1457 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001458
Arun Patole7e7e68d2015-05-22 12:02:25 +05301459 if (!symbol->isFunction())
1460 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001461 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001462 return 0;
1463 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001464
Jamie Madillb98c3a82015-07-23 14:26:04 -04001465 return static_cast<const TFunction *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001466}
1467
1468//
1469// Initializers show up in several places in the grammar. Have one set of
1470// code to handle them here.
1471//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001472// Returns true on error, false if no error
1473//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001474bool TParseContext::executeInitializer(const TSourceLoc &line,
1475 const TString &identifier,
1476 const TPublicType &pType,
1477 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001478 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001479{
Olli Etuaho13389b62016-10-16 11:48:18 +01001480 ASSERT(initNode != nullptr);
1481 ASSERT(*initNode == nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001482 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001483
Olli Etuaho2935c582015-04-08 14:32:06 +03001484 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001485 if (type.isUnsizedArray())
1486 {
Olli Etuaho02bd82c2016-11-03 10:29:43 +00001487 // We have not checked yet whether the initializer actually is an array or not.
1488 if (initializer->isArray())
1489 {
1490 type.setArraySize(initializer->getArraySize());
1491 }
1492 else
1493 {
1494 // Having a non-array initializer for an unsized array will result in an error later,
1495 // so we don't generate an error message here.
1496 type.setArraySize(1u);
1497 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001498 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001499 if (!declareVariable(line, identifier, type, &variable))
1500 {
1501 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001502 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001503
Olli Etuahob0c645e2015-05-12 14:25:36 +03001504 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001505 if (symbolTable.atGlobalLevel() &&
1506 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001507 {
1508 // Error message does not completely match behavior with ESSL 1.00, but
1509 // we want to steer developers towards only using constant expressions.
1510 error(line, "global variable initializers must be constant expressions", "=");
1511 return true;
1512 }
1513 if (globalInitWarning)
1514 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001515 warning(
1516 line,
1517 "global variable initializers should be constant expressions "
1518 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1519 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001520 }
1521
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001522 //
1523 // identifier must be of type constant, a global, or a temporary
1524 //
1525 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301526 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1527 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001528 error(line, " cannot initialize this type of qualifier ",
1529 variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001530 return true;
1531 }
1532 //
1533 // test for and propagate constant
1534 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001535
Arun Patole7e7e68d2015-05-22 12:02:25 +05301536 if (qualifier == EvqConst)
1537 {
1538 if (qualifier != initializer->getType().getQualifier())
1539 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001540 std::stringstream extraInfoStream;
1541 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1542 std::string extraInfo = extraInfoStream.str();
1543 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001544 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001545 return true;
1546 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301547 if (type != initializer->getType())
1548 {
1549 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001550 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001551 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001552 return true;
1553 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001554
1555 // Save the constant folded value to the variable if possible. For example array
1556 // initializers are not folded, since that way copying the array literal to multiple places
1557 // in the shader is avoided.
1558 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1559 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301560 if (initializer->getAsConstantUnion())
1561 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001562 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho13389b62016-10-16 11:48:18 +01001563 *initNode = nullptr;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001564 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301565 }
1566 else if (initializer->getAsSymbolNode())
1567 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001568 const TSymbol *symbol =
1569 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1570 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001571
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001572 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001573 if (constArray)
1574 {
1575 variable->shareConstPointer(constArray);
Olli Etuaho13389b62016-10-16 11:48:18 +01001576 *initNode = nullptr;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001577 return false;
1578 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001579 }
1580 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001581
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001582 TIntermSymbol *intermSymbol = intermediate.addSymbol(
1583 variable->getUniqueId(), variable->getName(), variable->getType(), line);
Olli Etuaho13389b62016-10-16 11:48:18 +01001584 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1585 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001586 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001587 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1588 return true;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001589 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001590
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001591 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001592}
1593
Olli Etuaho0e3aee32016-10-27 12:56:38 +01001594void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
1595{
1596 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
1597 typeSpecifier->getBasicType());
1598
1599 if (mShaderVersion < 300 && typeSpecifier->array)
1600 {
1601 error(typeSpecifier->getLine(), "not supported", "first-class array");
1602 typeSpecifier->clearArrayness();
1603 }
1604}
1605
Martin Radev70866b82016-07-22 15:27:42 +03001606TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301607 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001608{
Olli Etuaho613b9592016-09-05 12:05:53 +03001609 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001610
Martin Radev70866b82016-07-22 15:27:42 +03001611 TPublicType returnType = typeSpecifier;
1612 returnType.qualifier = typeQualifier.qualifier;
1613 returnType.invariant = typeQualifier.invariant;
1614 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03001615 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03001616 returnType.precision = typeSpecifier.precision;
1617
1618 if (typeQualifier.precision != EbpUndefined)
1619 {
1620 returnType.precision = typeQualifier.precision;
1621 }
1622
Martin Radev4a9cd802016-09-01 16:51:51 +03001623 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
1624 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03001625
Martin Radev4a9cd802016-09-01 16:51:51 +03001626 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
1627 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03001628
Martin Radev4a9cd802016-09-01 16:51:51 +03001629 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03001630
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001631 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001632 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001633 if (typeSpecifier.array)
1634 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001635 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001636 returnType.clearArrayness();
1637 }
1638
Martin Radev70866b82016-07-22 15:27:42 +03001639 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001640 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001641 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001642 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001643 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001644 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001645
Martin Radev70866b82016-07-22 15:27:42 +03001646 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001647 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001648 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001649 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001650 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001651 }
1652 }
1653 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001654 {
Martin Radev70866b82016-07-22 15:27:42 +03001655 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03001656 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001657 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03001658 }
Martin Radev70866b82016-07-22 15:27:42 +03001659 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
1660 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001661 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001662 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
1663 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001664 }
Martin Radev70866b82016-07-22 15:27:42 +03001665 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03001666 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001667 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03001668 "in");
Martin Radev802abe02016-08-04 17:48:32 +03001669 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001670 }
1671
1672 return returnType;
1673}
1674
Olli Etuaho856c4972016-08-08 11:38:39 +03001675void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
1676 const TPublicType &type,
1677 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03001678{
1679 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03001680 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03001681 {
1682 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001683 }
1684
1685 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
1686 switch (qualifier)
1687 {
1688 case EvqVertexIn:
1689 // ESSL 3.00 section 4.3.4
1690 if (type.array)
1691 {
1692 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001693 }
1694 // Vertex inputs with a struct type are disallowed in singleDeclarationErrorCheck
1695 return;
1696 case EvqFragmentOut:
1697 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03001698 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03001699 {
1700 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001701 }
1702 // Fragment outputs with a struct type are disallowed in singleDeclarationErrorCheck
1703 return;
1704 default:
1705 break;
1706 }
1707
1708 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
1709 // restrictions.
1710 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03001711 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
1712 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03001713 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
1714 {
1715 error(qualifierLocation, "must use 'flat' interpolation here",
1716 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001717 }
1718
Martin Radev4a9cd802016-09-01 16:51:51 +03001719 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03001720 {
1721 // ESSL 3.00 sections 4.3.4 and 4.3.6.
1722 // These restrictions are only implied by the ESSL 3.00 spec, but
1723 // the ESSL 3.10 spec lists these restrictions explicitly.
1724 if (type.array)
1725 {
1726 error(qualifierLocation, "cannot be an array of structures",
1727 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001728 }
1729 if (type.isStructureContainingArrays())
1730 {
1731 error(qualifierLocation, "cannot be a structure containing an array",
1732 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001733 }
1734 if (type.isStructureContainingType(EbtStruct))
1735 {
1736 error(qualifierLocation, "cannot be a structure containing a structure",
1737 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001738 }
1739 if (type.isStructureContainingType(EbtBool))
1740 {
1741 error(qualifierLocation, "cannot be a structure containing a bool",
1742 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001743 }
1744 }
1745}
1746
Martin Radev2cc85b32016-08-05 16:22:53 +03001747void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
1748{
1749 if (qualifier.getType() == QtStorage)
1750 {
1751 const TStorageQualifierWrapper &storageQualifier =
1752 static_cast<const TStorageQualifierWrapper &>(qualifier);
1753 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
1754 !symbolTable.atGlobalLevel())
1755 {
1756 error(storageQualifier.getLine(),
1757 "Local variables can only use the const storage qualifier.",
1758 storageQualifier.getQualifierString().c_str());
1759 }
1760 }
1761}
1762
1763bool TParseContext::checkIsMemoryQualifierNotSpecified(const TMemoryQualifier &memoryQualifier,
1764 const TSourceLoc &location)
1765{
1766 if (memoryQualifier.readonly)
1767 {
1768 error(location, "Only allowed with images.", "readonly");
1769 return false;
1770 }
1771 if (memoryQualifier.writeonly)
1772 {
1773 error(location, "Only allowed with images.", "writeonly");
1774 return false;
1775 }
1776 return true;
1777}
1778
Olli Etuaho13389b62016-10-16 11:48:18 +01001779TIntermDeclaration *TParseContext::parseSingleDeclaration(
1780 TPublicType &publicType,
1781 const TSourceLoc &identifierOrTypeLocation,
1782 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001783{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001784 TType type(publicType);
1785 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
1786 mDirectiveHandler.pragma().stdgl.invariantAll)
1787 {
1788 TQualifier qualifier = type.getQualifier();
1789
1790 // The directive handler has already taken care of rejecting invalid uses of this pragma
1791 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
1792 // affected variable declarations:
1793 //
1794 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
1795 // elsewhere, in TranslatorGLSL.)
1796 //
1797 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
1798 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
1799 // the way this is currently implemented we have to enable this compiler option before
1800 // parsing the shader and determining the shading language version it uses. If this were
1801 // implemented as a post-pass, the workaround could be more targeted.
1802 //
1803 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
1804 // the specification, but there are desktop OpenGL drivers that expect that this is the
1805 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
1806 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
1807 {
1808 type.setInvariant(true);
1809 }
1810 }
1811
1812 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, type, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001813
Olli Etuahobab4c082015-04-24 16:38:49 +03001814 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03001815
Olli Etuahobab4c082015-04-24 16:38:49 +03001816 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1817
Olli Etuaho13389b62016-10-16 11:48:18 +01001818 TIntermDeclaration *declaration = new TIntermDeclaration();
1819 declaration->setLine(identifierOrTypeLocation);
1820
Olli Etuahobab4c082015-04-24 16:38:49 +03001821 if (emptyDeclaration)
1822 {
1823 if (publicType.isUnsizedArray())
1824 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001825 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1826 // error. It is assumed that this applies to empty declarations as well.
1827 error(identifierOrTypeLocation, "empty array declaration needs to specify a size",
1828 identifier.c_str());
Olli Etuahobab4c082015-04-24 16:38:49 +03001829 }
1830 }
1831 else
Jamie Madill60ed9812013-06-06 11:56:46 -04001832 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001833 singleDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001834
Olli Etuaho856c4972016-08-08 11:38:39 +03001835 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001836
Olli Etuaho2935c582015-04-08 14:32:06 +03001837 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001838 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04001839
1840 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01001841 {
Jamie Madill60ed9812013-06-06 11:56:46 -04001842 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01001843 }
Jamie Madill60ed9812013-06-06 11:56:46 -04001844 }
1845
Olli Etuaho13389b62016-10-16 11:48:18 +01001846 // We append the symbol even if the declaration is empty, mainly because of struct declarations
1847 // that may just declare a type.
1848 declaration->appendDeclarator(symbol);
1849
1850 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04001851}
1852
Olli Etuaho13389b62016-10-16 11:48:18 +01001853TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1854 const TSourceLoc &identifierLocation,
1855 const TString &identifier,
1856 const TSourceLoc &indexLocation,
1857 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001858{
Olli Etuahofa33d582015-04-09 14:33:12 +03001859 mDeferredSingleDeclarationErrorCheck = false;
1860
Olli Etuaho383b7912016-08-05 11:22:59 +03001861 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001862
Olli Etuaho856c4972016-08-08 11:38:39 +03001863 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001864
Olli Etuaho8a176262016-08-16 14:23:01 +03001865 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001866
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001867 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001868
Olli Etuaho856c4972016-08-08 11:38:39 +03001869 unsigned int size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001870 // Make the type an array even if size check failed.
1871 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1872 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001873
Olli Etuaho2935c582015-04-08 14:32:06 +03001874 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03001875 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04001876
Olli Etuaho13389b62016-10-16 11:48:18 +01001877 TIntermDeclaration *declaration = new TIntermDeclaration();
1878 declaration->setLine(identifierLocation);
1879
Olli Etuahoe7847b02015-03-16 11:56:12 +02001880 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001881 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01001882 {
Jamie Madill60ed9812013-06-06 11:56:46 -04001883 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01001884 declaration->appendDeclarator(symbol);
1885 }
Jamie Madill60ed9812013-06-06 11:56:46 -04001886
Olli Etuaho13389b62016-10-16 11:48:18 +01001887 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04001888}
1889
Olli Etuaho13389b62016-10-16 11:48:18 +01001890TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
1891 const TSourceLoc &identifierLocation,
1892 const TString &identifier,
1893 const TSourceLoc &initLocation,
1894 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001895{
Olli Etuahofa33d582015-04-09 14:33:12 +03001896 mDeferredSingleDeclarationErrorCheck = false;
1897
Olli Etuaho383b7912016-08-05 11:22:59 +03001898 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001899
Olli Etuaho13389b62016-10-16 11:48:18 +01001900 TIntermDeclaration *declaration = new TIntermDeclaration();
1901 declaration->setLine(identifierLocation);
1902
1903 TIntermBinary *initNode = nullptr;
1904 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04001905 {
Olli Etuaho13389b62016-10-16 11:48:18 +01001906 if (initNode)
1907 {
1908 declaration->appendDeclarator(initNode);
1909 }
Jamie Madill60ed9812013-06-06 11:56:46 -04001910 }
Olli Etuaho13389b62016-10-16 11:48:18 +01001911 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04001912}
1913
Olli Etuaho13389b62016-10-16 11:48:18 +01001914TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Jamie Madillb98c3a82015-07-23 14:26:04 -04001915 TPublicType &publicType,
1916 const TSourceLoc &identifierLocation,
1917 const TString &identifier,
1918 const TSourceLoc &indexLocation,
1919 TIntermTyped *indexExpression,
1920 const TSourceLoc &initLocation,
1921 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001922{
1923 mDeferredSingleDeclarationErrorCheck = false;
1924
Olli Etuaho383b7912016-08-05 11:22:59 +03001925 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001926
Olli Etuaho8a176262016-08-16 14:23:01 +03001927 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001928
1929 TPublicType arrayType(publicType);
1930
Olli Etuaho856c4972016-08-08 11:38:39 +03001931 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001932 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
1933 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03001934 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001935 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001936 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001937 }
1938 // Make the type an array even if size check failed.
1939 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1940 arrayType.setArraySize(size);
1941
Olli Etuaho13389b62016-10-16 11:48:18 +01001942 TIntermDeclaration *declaration = new TIntermDeclaration();
1943 declaration->setLine(identifierLocation);
1944
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001945 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01001946 TIntermBinary *initNode = nullptr;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001947 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1948 {
Olli Etuaho13389b62016-10-16 11:48:18 +01001949 if (initNode)
1950 {
1951 declaration->appendDeclarator(initNode);
1952 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001953 }
Olli Etuaho13389b62016-10-16 11:48:18 +01001954
1955 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001956}
1957
Martin Radev70866b82016-07-22 15:27:42 +03001958TIntermAggregate *TParseContext::parseInvariantDeclaration(
1959 const TTypeQualifierBuilder &typeQualifierBuilder,
1960 const TSourceLoc &identifierLoc,
1961 const TString *identifier,
1962 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04001963{
Olli Etuaho613b9592016-09-05 12:05:53 +03001964 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04001965
Martin Radev70866b82016-07-22 15:27:42 +03001966 if (!typeQualifier.invariant)
1967 {
1968 error(identifierLoc, "Expected invariant", identifier->c_str());
1969 return nullptr;
1970 }
1971 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
1972 {
1973 return nullptr;
1974 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04001975 if (!symbol)
1976 {
1977 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001978 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001979 }
Martin Radev70866b82016-07-22 15:27:42 +03001980 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04001981 {
Martin Radev70866b82016-07-22 15:27:42 +03001982 error(identifierLoc, "invariant declaration specifies qualifier",
1983 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04001984 }
Martin Radev70866b82016-07-22 15:27:42 +03001985 if (typeQualifier.precision != EbpUndefined)
1986 {
1987 error(identifierLoc, "invariant declaration specifies precision",
1988 getPrecisionString(typeQualifier.precision));
1989 }
1990 if (!typeQualifier.layoutQualifier.isEmpty())
1991 {
1992 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
1993 }
1994
1995 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1996 ASSERT(variable);
1997 const TType &type = variable->getType();
1998
1999 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2000 typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002001 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002002
2003 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2004
2005 TIntermSymbol *intermSymbol =
2006 intermediate.addSymbol(variable->getUniqueId(), *identifier, type, identifierLoc);
2007
Olli Etuaho32db19b2016-10-04 14:43:16 +01002008 TIntermAggregate *aggregate = TIntermediate::MakeAggregate(intermSymbol, identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002009 aggregate->setOp(EOpInvariantDeclaration);
2010 return aggregate;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002011}
2012
Olli Etuaho13389b62016-10-16 11:48:18 +01002013void TParseContext::parseDeclarator(TPublicType &publicType,
2014 const TSourceLoc &identifierLocation,
2015 const TString &identifier,
2016 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002017{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002018 // If the declaration starting this declarator list was empty (example: int,), some checks were
2019 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002020 if (mDeferredSingleDeclarationErrorCheck)
2021 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002022 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002023 mDeferredSingleDeclarationErrorCheck = false;
2024 }
2025
Olli Etuaho856c4972016-08-08 11:38:39 +03002026 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002027
Olli Etuaho856c4972016-08-08 11:38:39 +03002028 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002029
Olli Etuaho2935c582015-04-08 14:32:06 +03002030 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002031 declareVariable(identifierLocation, identifier, TType(publicType), &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002032
Jamie Madillb98c3a82015-07-23 14:26:04 -04002033 TIntermSymbol *symbol =
2034 intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002035 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01002036 {
Jamie Madill502d66f2013-06-20 11:55:52 -04002037 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01002038 declarationOut->appendDeclarator(symbol);
2039 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002040}
2041
Olli Etuaho13389b62016-10-16 11:48:18 +01002042void TParseContext::parseArrayDeclarator(TPublicType &publicType,
2043 const TSourceLoc &identifierLocation,
2044 const TString &identifier,
2045 const TSourceLoc &arrayLocation,
2046 TIntermTyped *indexExpression,
2047 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002048{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002049 // If the declaration starting this declarator list was empty (example: int,), some checks were
2050 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002051 if (mDeferredSingleDeclarationErrorCheck)
2052 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002053 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002054 mDeferredSingleDeclarationErrorCheck = false;
2055 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002056
Olli Etuaho856c4972016-08-08 11:38:39 +03002057 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002058
Olli Etuaho856c4972016-08-08 11:38:39 +03002059 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002060
Olli Etuaho8a176262016-08-16 14:23:01 +03002061 if (checkIsValidTypeAndQualifierForArray(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002062 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02002063 TType arrayType = TType(publicType);
Olli Etuaho856c4972016-08-08 11:38:39 +03002064 unsigned int size = checkIsValidArraySize(arrayLocation, indexExpression);
Olli Etuaho693c9aa2015-04-07 17:50:36 +03002065 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002066
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002067 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002068 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002069
Jamie Madillb98c3a82015-07-23 14:26:04 -04002070 TIntermSymbol *symbol =
2071 intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002072 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002073 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002074
Olli Etuaho13389b62016-10-16 11:48:18 +01002075 declarationOut->appendDeclarator(symbol);
Jamie Madill502d66f2013-06-20 11:55:52 -04002076 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002077}
2078
Olli Etuaho13389b62016-10-16 11:48:18 +01002079void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2080 const TSourceLoc &identifierLocation,
2081 const TString &identifier,
2082 const TSourceLoc &initLocation,
2083 TIntermTyped *initializer,
2084 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002085{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002086 // If the declaration starting this declarator list was empty (example: int,), some checks were
2087 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002088 if (mDeferredSingleDeclarationErrorCheck)
2089 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002090 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002091 mDeferredSingleDeclarationErrorCheck = false;
2092 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002093
Olli Etuaho856c4972016-08-08 11:38:39 +03002094 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002095
Olli Etuaho13389b62016-10-16 11:48:18 +01002096 TIntermBinary *initNode = nullptr;
2097 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002098 {
2099 //
2100 // build the intermediate representation
2101 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002102 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002103 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002104 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002105 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002106 }
2107}
2108
Olli Etuaho13389b62016-10-16 11:48:18 +01002109void TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
2110 const TSourceLoc &identifierLocation,
2111 const TString &identifier,
2112 const TSourceLoc &indexLocation,
2113 TIntermTyped *indexExpression,
2114 const TSourceLoc &initLocation,
2115 TIntermTyped *initializer,
2116 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002117{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002118 // If the declaration starting this declarator list was empty (example: int,), some checks were
2119 // not performed.
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002120 if (mDeferredSingleDeclarationErrorCheck)
2121 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002122 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002123 mDeferredSingleDeclarationErrorCheck = false;
2124 }
2125
Olli Etuaho856c4972016-08-08 11:38:39 +03002126 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002127
Olli Etuaho8a176262016-08-16 14:23:01 +03002128 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002129
2130 TPublicType arrayType(publicType);
2131
Olli Etuaho856c4972016-08-08 11:38:39 +03002132 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002133 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2134 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002135 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002136 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002137 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002138 }
2139 // Make the type an array even if size check failed.
2140 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2141 arrayType.setArraySize(size);
2142
2143 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002144 TIntermBinary *initNode = nullptr;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002145 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
2146 {
2147 if (initNode)
2148 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002149 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002150 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002151 }
2152}
2153
Martin Radev70866b82016-07-22 15:27:42 +03002154void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002155{
Olli Etuaho613b9592016-09-05 12:05:53 +03002156 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002157 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002158
Martin Radev70866b82016-07-22 15:27:42 +03002159 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2160 typeQualifier.line);
2161
Jamie Madillc2128ff2016-07-04 10:26:17 -04002162 // It should never be the case, but some strange parser errors can send us here.
2163 if (layoutQualifier.isEmpty())
2164 {
2165 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002166 return;
2167 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002168
Martin Radev802abe02016-08-04 17:48:32 +03002169 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002170 {
Martin Radev802abe02016-08-04 17:48:32 +03002171 error(typeQualifier.line, "invalid combination:", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002172 return;
2173 }
2174
Martin Radev2cc85b32016-08-05 16:22:53 +03002175 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
2176
2177 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2178
Martin Radev802abe02016-08-04 17:48:32 +03002179 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002180 {
Martin Radev802abe02016-08-04 17:48:32 +03002181 if (mComputeShaderLocalSizeDeclared &&
2182 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2183 {
2184 error(typeQualifier.line, "Work group size does not match the previous declaration",
2185 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002186 return;
2187 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002188
Martin Radev802abe02016-08-04 17:48:32 +03002189 if (mShaderVersion < 310)
2190 {
2191 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002192 return;
2193 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002194
Martin Radev4c4c8e72016-08-04 12:25:34 +03002195 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03002196 {
2197 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002198 return;
2199 }
2200
2201 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
2202 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
2203
2204 const TConstantUnion *maxComputeWorkGroupSizeData =
2205 maxComputeWorkGroupSize->getConstPointer();
2206
2207 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
2208 {
2209 if (layoutQualifier.localSize[i] != -1)
2210 {
2211 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
2212 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
2213 if (mComputeShaderLocalSize[i] < 1 ||
2214 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
2215 {
2216 std::stringstream errorMessageStream;
2217 errorMessageStream << "Value must be at least 1 and no greater than "
2218 << maxComputeWorkGroupSizeValue;
2219 const std::string &errorMessage = errorMessageStream.str();
2220
Martin Radev4c4c8e72016-08-04 12:25:34 +03002221 error(typeQualifier.line, "invalid value:", getWorkGroupSizeString(i),
Martin Radev802abe02016-08-04 17:48:32 +03002222 errorMessage.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03002223 return;
2224 }
2225 }
2226 }
2227
2228 mComputeShaderLocalSizeDeclared = true;
2229 }
2230 else
Jamie Madill1566ef72013-06-20 11:55:54 -04002231 {
Martin Radev802abe02016-08-04 17:48:32 +03002232
Olli Etuaho8a176262016-08-16 14:23:01 +03002233 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03002234 {
Martin Radev802abe02016-08-04 17:48:32 +03002235 return;
2236 }
2237
2238 if (typeQualifier.qualifier != EvqUniform)
2239 {
2240 error(typeQualifier.line, "invalid qualifier:",
2241 getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
Martin Radev802abe02016-08-04 17:48:32 +03002242 return;
2243 }
2244
2245 if (mShaderVersion < 300)
2246 {
2247 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
2248 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002249 return;
2250 }
2251
Olli Etuaho856c4972016-08-08 11:38:39 +03002252 checkLocationIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002253
2254 if (layoutQualifier.matrixPacking != EmpUnspecified)
2255 {
2256 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
2257 }
2258
2259 if (layoutQualifier.blockStorage != EbsUnspecified)
2260 {
2261 mDefaultBlockStorage = layoutQualifier.blockStorage;
2262 }
Jamie Madill1566ef72013-06-20 11:55:54 -04002263 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002264}
2265
Olli Etuaho476197f2016-10-11 13:59:08 +01002266TIntermAggregate *TParseContext::addFunctionPrototypeDeclaration(const TFunction &parsedFunction,
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002267 const TSourceLoc &location)
2268{
Olli Etuaho476197f2016-10-11 13:59:08 +01002269 // Note: function found from the symbol table could be the same as parsedFunction if this is the
2270 // first declaration. Either way the instance in the symbol table is used to track whether the
2271 // function is declared multiple times.
2272 TFunction *function = static_cast<TFunction *>(
2273 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
2274 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02002275 {
2276 // ESSL 1.00.17 section 4.2.7.
2277 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
2278 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02002279 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002280 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02002281
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002282 TIntermAggregate *prototype = new TIntermAggregate;
Olli Etuaho476197f2016-10-11 13:59:08 +01002283 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
2284 // point to the data that already exists in the symbol table.
2285 prototype->setType(function->getReturnType());
Olli Etuahobd674552016-10-06 13:28:42 +01002286 prototype->getFunctionSymbolInfo()->setFromFunction(*function);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002287
Olli Etuaho476197f2016-10-11 13:59:08 +01002288 for (size_t i = 0; i < function->getParamCount(); i++)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002289 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002290 const TConstParameter &param = function->getParam(i);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002291 if (param.name != 0)
2292 {
2293 TVariable variable(param.name, *param.type);
2294
2295 TIntermSymbol *paramSymbol = intermediate.addSymbol(
2296 variable.getUniqueId(), variable.getName(), variable.getType(), location);
2297 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
2298 }
2299 else
2300 {
2301 TIntermSymbol *paramSymbol = intermediate.addSymbol(0, "", *param.type, location);
2302 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
2303 }
2304 }
2305
2306 prototype->setOp(EOpPrototype);
2307
2308 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002309
2310 if (!symbolTable.atGlobalLevel())
2311 {
2312 // ESSL 3.00.4 section 4.2.4.
2313 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002314 }
2315
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002316 return prototype;
2317}
2318
Olli Etuaho336b1472016-10-05 16:37:55 +01002319TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
2320 const TFunction &function,
2321 TIntermAggregate *functionParameters,
2322 TIntermBlock *functionBody,
2323 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002324{
Olli Etuahof51fdd22016-10-03 10:03:40 +01002325 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002326 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
2327 {
2328 error(location, "function does not return a value:", "", function.getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002329 }
2330
Olli Etuahof51fdd22016-10-03 10:03:40 +01002331 if (functionBody == nullptr)
2332 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01002333 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002334 functionBody->setLine(location);
2335 }
Olli Etuaho336b1472016-10-05 16:37:55 +01002336 TIntermFunctionDefinition *functionNode =
2337 new TIntermFunctionDefinition(function.getReturnType(), functionParameters, functionBody);
2338 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01002339
Olli Etuahobd674552016-10-06 13:28:42 +01002340 functionNode->getFunctionSymbolInfo()->setFromFunction(function);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002341
2342 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002343 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002344}
2345
Olli Etuaho476197f2016-10-11 13:59:08 +01002346void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
2347 TFunction **function,
2348 TIntermAggregate **aggregateOut)
Jamie Madill185fb402015-06-12 15:48:48 -04002349{
Olli Etuaho476197f2016-10-11 13:59:08 +01002350 ASSERT(function);
2351 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002352 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01002353 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002354
2355 if (builtIn)
2356 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002357 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002358 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002359 else
Jamie Madill185fb402015-06-12 15:48:48 -04002360 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002361 TFunction *prevDec = static_cast<TFunction *>(
2362 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
2363
2364 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
2365 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
2366 // occurance.
2367 if (*function != prevDec)
2368 {
2369 // Swap the parameters of the previous declaration to the parameters of the function
2370 // definition (parameter names may differ).
2371 prevDec->swapParameters(**function);
2372
2373 // The function definition will share the same symbol as any previous declaration.
2374 *function = prevDec;
2375 }
2376
2377 if ((*function)->isDefined())
2378 {
2379 error(location, "function already has a body", (*function)->getName().c_str());
2380 }
2381
2382 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04002383 }
Jamie Madill185fb402015-06-12 15:48:48 -04002384
2385 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuaho476197f2016-10-11 13:59:08 +01002386 if ((*function)->getName() == "main")
Jamie Madill185fb402015-06-12 15:48:48 -04002387 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002388 if ((*function)->getParamCount() > 0)
Jamie Madill185fb402015-06-12 15:48:48 -04002389 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002390 error(location, "function cannot take any parameter(s)",
2391 (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002392 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002393 if ((*function)->getReturnType().getBasicType() != EbtVoid)
Jamie Madill185fb402015-06-12 15:48:48 -04002394 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002395 error(location, "", (*function)->getReturnType().getBasicString(),
Jamie Madillb98c3a82015-07-23 14:26:04 -04002396 "main function cannot return a value");
Jamie Madill185fb402015-06-12 15:48:48 -04002397 }
2398 }
2399
2400 //
2401 // Remember the return type for later checking for RETURN statements.
2402 //
Olli Etuaho476197f2016-10-11 13:59:08 +01002403 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002404 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04002405
2406 //
2407 // Insert parameters into the symbol table.
2408 // If the parameter has no name, it's not an error, just don't insert it
2409 // (could be used for unused args).
2410 //
2411 // Also, accumulate the list of parameters into the HIL, so lower level code
2412 // knows where to find parameters.
2413 //
2414 TIntermAggregate *paramNodes = new TIntermAggregate;
Olli Etuaho476197f2016-10-11 13:59:08 +01002415 for (size_t i = 0; i < (*function)->getParamCount(); i++)
Jamie Madill185fb402015-06-12 15:48:48 -04002416 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002417 const TConstParameter &param = (*function)->getParam(i);
Jamie Madill185fb402015-06-12 15:48:48 -04002418 if (param.name != 0)
2419 {
2420 TVariable *variable = new TVariable(param.name, *param.type);
2421 //
2422 // Insert the parameters with name in the symbol table.
2423 //
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002424 if (!symbolTable.declare(variable))
2425 {
Jamie Madill185fb402015-06-12 15:48:48 -04002426 error(location, "redefinition", variable->getName().c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002427 paramNodes = intermediate.growAggregate(
2428 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
2429 continue;
Jamie Madill185fb402015-06-12 15:48:48 -04002430 }
2431
2432 //
2433 // Add the parameter to the HIL
2434 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002435 TIntermSymbol *symbol = intermediate.addSymbol(
2436 variable->getUniqueId(), variable->getName(), variable->getType(), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002437
2438 paramNodes = intermediate.growAggregate(paramNodes, symbol, location);
2439 }
2440 else
2441 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002442 paramNodes = intermediate.growAggregate(
2443 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002444 }
2445 }
2446 intermediate.setAggregateOperator(paramNodes, EOpParameters, location);
2447 *aggregateOut = paramNodes;
2448 setLoopNestingLevel(0);
2449}
2450
Jamie Madillb98c3a82015-07-23 14:26:04 -04002451TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002452{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002453 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002454 // We don't know at this point whether this is a function definition or a prototype.
2455 // The definition production code will check for redefinitions.
2456 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002457 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002458 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
2459 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002460 //
2461 TFunction *prevDec =
2462 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302463
2464 if (getShaderVersion() >= 300 && symbolTable.hasUnmangledBuiltIn(function->getName().c_str()))
2465 {
2466 // With ESSL 3.00, names of built-in functions cannot be redeclared as functions.
2467 // Therefore overloading or redefining builtin functions is an error.
2468 error(location, "Name of a built-in function cannot be redeclared as function",
2469 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302470 }
2471 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002472 {
2473 if (prevDec->getReturnType() != function->getReturnType())
2474 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002475 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002476 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04002477 }
2478 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2479 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002480 if (prevDec->getParam(i).type->getQualifier() !=
2481 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002482 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002483 error(location,
2484 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002485 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04002486 }
2487 }
2488 }
2489
2490 //
2491 // Check for previously declared variables using the same name.
2492 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002493 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002494 if (prevSym)
2495 {
2496 if (!prevSym->isFunction())
2497 {
2498 error(location, "redefinition", function->getName().c_str(), "function");
Jamie Madill185fb402015-06-12 15:48:48 -04002499 }
2500 }
2501 else
2502 {
2503 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01002504 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04002505 }
2506
2507 // We're at the inner scope level of the function's arguments and body statement.
2508 // Add the function prototype to the surrounding scope instead.
2509 symbolTable.getOuterLevel()->insert(function);
2510
2511 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002512 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2513 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002514 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2515 //
2516 return function;
2517}
2518
Olli Etuaho9de84a52016-06-14 17:36:01 +03002519TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
2520 const TString *name,
2521 const TSourceLoc &location)
2522{
2523 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
2524 {
2525 error(location, "no qualifiers allowed for function return",
2526 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03002527 }
2528 if (!type.layoutQualifier.isEmpty())
2529 {
2530 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03002531 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002532 // make sure a sampler or an image is not involved as well...
Martin Radev4a9cd802016-09-01 16:51:51 +03002533 checkIsNotSampler(location, type.typeSpecifierNonArray,
2534 "samplers can't be function return values");
Martin Radev2cc85b32016-08-05 16:22:53 +03002535 checkIsNotImage(location, type.typeSpecifierNonArray, "images can't be function return values");
Olli Etuahoe29324f2016-06-15 10:58:03 +03002536 if (mShaderVersion < 300)
2537 {
2538 // Array return values are forbidden, but there's also no valid syntax for declaring array
2539 // return values in ESSL 1.00.
2540 ASSERT(type.arraySize == 0 || mDiagnostics.numErrors() > 0);
2541
2542 if (type.isStructureContainingArrays())
2543 {
2544 // ESSL 1.00.17 section 6.1 Function Definitions
2545 error(location, "structures containing arrays can't be function return values",
2546 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03002547 }
2548 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03002549
2550 // Add the function as a prototype after parsing it (we do not support recursion)
2551 return new TFunction(name, new TType(type));
2552}
2553
Jamie Madill06145232015-05-13 13:10:01 -04002554TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002555{
Jamie Madill06145232015-05-13 13:10:01 -04002556 TPublicType publicType = publicTypeIn;
Martin Radev4a9cd802016-09-01 16:51:51 +03002557 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02002558 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002559 error(publicType.getLine(), "constructor can't be a structure definition",
2560 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02002561 }
2562
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002563 TOperator op = EOpNull;
Martin Radev4a9cd802016-09-01 16:51:51 +03002564 if (publicType.getUserDef())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002565 {
2566 op = EOpConstructStruct;
2567 }
2568 else
2569 {
Geoff Lang156d7192016-07-21 16:11:00 -04002570 op = sh::TypeToConstructorOperator(TType(publicType));
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002571 if (op == EOpNull)
2572 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002573 error(publicType.getLine(), "cannot construct this type",
2574 getBasicString(publicType.getBasicType()));
2575 publicType.setBasicType(EbtFloat);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002576 op = EOpConstructFloat;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002577 }
2578 }
2579
2580 TString tempString;
Dmitry Skiba7f17a502015-06-22 15:08:39 -07002581 const TType *type = new TType(publicType);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002582 return new TFunction(&tempString, type, op);
2583}
2584
Jamie Madillb98c3a82015-07-23 14:26:04 -04002585// This function is used to test for the correctness of the parameters passed to various constructor
2586// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002587//
Olli Etuaho856c4972016-08-08 11:38:39 +03002588// 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 +00002589//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002590TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002591 TOperator op,
2592 TFunction *fnCall,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302593 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002594{
Olli Etuaho856c4972016-08-08 11:38:39 +03002595 TType type = fnCall->getReturnType();
2596 if (type.isUnsizedArray())
2597 {
Olli Etuahobbe9fb52016-11-03 17:16:05 +00002598 if (fnCall->getParamCount() == 0)
2599 {
2600 error(line, "implicitly sized array constructor must have at least one argument", "[]");
2601 type.setArraySize(1u);
2602 return TIntermTyped::CreateZero(type);
2603 }
Olli Etuaho856c4972016-08-08 11:38:39 +03002604 type.setArraySize(static_cast<unsigned int>(fnCall->getParamCount()));
2605 }
2606 bool constType = true;
2607 for (size_t i = 0; i < fnCall->getParamCount(); ++i)
2608 {
2609 const TConstParameter &param = fnCall->getParam(i);
2610 if (param.type->getQualifier() != EvqConst)
2611 constType = false;
2612 }
2613 if (constType)
2614 type.setQualifier(EvqConst);
2615
Olli Etuaho8a176262016-08-16 14:23:01 +03002616 if (!checkConstructorArguments(line, arguments, *fnCall, op, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03002617 {
2618 TIntermTyped *dummyNode = intermediate.setAggregateOperator(nullptr, op, line);
2619 dummyNode->setType(type);
2620 return dummyNode;
2621 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002622 TIntermAggregate *constructor = arguments->getAsAggregate();
2623 ASSERT(constructor != nullptr);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002624
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002625 // Turn the argument list itself into a constructor
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002626 constructor->setOp(op);
2627 constructor->setLine(line);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002628 ASSERT(constructor->isConstructor());
2629
2630 // Need to set type before setPrecisionFromChildren() because bool doesn't have precision.
Olli Etuaho856c4972016-08-08 11:38:39 +03002631 constructor->setType(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002632
Olli Etuaho21203702014-11-13 16:16:21 +02002633 // Structs should not be precision qualified, the individual members may be.
2634 // Built-in types on the other hand should be precision qualified.
2635 if (op != EOpConstructStruct)
2636 {
2637 constructor->setPrecisionFromChildren();
Olli Etuaho856c4972016-08-08 11:38:39 +03002638 type.setPrecision(constructor->getPrecision());
Olli Etuaho21203702014-11-13 16:16:21 +02002639 }
2640
Olli Etuaho856c4972016-08-08 11:38:39 +03002641 constructor->setType(type);
2642
Olli Etuahof119a262016-08-19 15:54:22 +03002643 TIntermTyped *constConstructor = intermediate.foldAggregateBuiltIn(constructor, &mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002644 if (constConstructor)
2645 {
2646 return constConstructor;
2647 }
2648
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002649 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002650}
2651
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002652//
2653// Interface/uniform blocks
2654//
Olli Etuaho13389b62016-10-16 11:48:18 +01002655TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03002656 const TTypeQualifierBuilder &typeQualifierBuilder,
2657 const TSourceLoc &nameLine,
2658 const TString &blockName,
2659 TFieldList *fieldList,
2660 const TString *instanceName,
2661 const TSourceLoc &instanceLine,
2662 TIntermTyped *arrayIndex,
2663 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002664{
Olli Etuaho856c4972016-08-08 11:38:39 +03002665 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002666
Olli Etuaho613b9592016-09-05 12:05:53 +03002667 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03002668
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002669 if (typeQualifier.qualifier != EvqUniform)
2670 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302671 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
2672 "interface blocks must be uniform");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002673 }
2674
Martin Radev70866b82016-07-22 15:27:42 +03002675 if (typeQualifier.invariant)
2676 {
2677 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
2678 }
2679
Martin Radev2cc85b32016-08-05 16:22:53 +03002680 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
2681
Jamie Madill099c0f32013-06-20 11:55:52 -04002682 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03002683 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002684
Jamie Madill099c0f32013-06-20 11:55:52 -04002685 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2686 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002687 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002688 }
2689
Jamie Madill1566ef72013-06-20 11:55:54 -04002690 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2691 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002692 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002693 }
2694
Olli Etuaho856c4972016-08-08 11:38:39 +03002695 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002696
Martin Radev2cc85b32016-08-05 16:22:53 +03002697 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
2698
Arun Patole7e7e68d2015-05-22 12:02:25 +05302699 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2700 if (!symbolTable.declare(blockNameSymbol))
2701 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002702 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002703 }
2704
Jamie Madill98493dd2013-07-08 14:39:03 -04002705 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302706 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2707 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002708 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302709 TType *fieldType = field->type();
2710 if (IsSampler(fieldType->getBasicType()))
2711 {
2712 error(field->line(), "unsupported type", fieldType->getBasicString(),
2713 "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002714 }
2715
Martin Radev2cc85b32016-08-05 16:22:53 +03002716 if (IsImage(fieldType->getBasicType()))
2717 {
2718 error(field->line(), "unsupported type", fieldType->getBasicString(),
2719 "image types are not allowed in interface blocks");
2720 }
2721
Jamie Madill98493dd2013-07-08 14:39:03 -04002722 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002723 switch (qualifier)
2724 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002725 case EvqGlobal:
2726 case EvqUniform:
2727 break;
2728 default:
2729 error(field->line(), "invalid qualifier on interface block member",
2730 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04002731 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002732 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002733
Martin Radev70866b82016-07-22 15:27:42 +03002734 if (fieldType->isInvariant())
2735 {
2736 error(field->line(), "invalid qualifier on interface block member", "invariant");
2737 }
2738
Jamie Madilla5efff92013-06-06 11:56:47 -04002739 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002740 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03002741 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
Jamie Madill099c0f32013-06-20 11:55:52 -04002742
Jamie Madill98493dd2013-07-08 14:39:03 -04002743 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002744 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002745 error(field->line(), "invalid layout qualifier:",
2746 getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04002747 }
2748
Jamie Madill98493dd2013-07-08 14:39:03 -04002749 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002750 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002751 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002752 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002753 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002754 {
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002755 warning(field->line(), "extraneous layout qualifier:",
Jamie Madillb98c3a82015-07-23 14:26:04 -04002756 getMatrixPackingString(fieldLayoutQualifier.matrixPacking),
2757 "only has an effect on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04002758 }
2759
Jamie Madill98493dd2013-07-08 14:39:03 -04002760 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002761 }
2762
Jamie Madill98493dd2013-07-08 14:39:03 -04002763 // add array index
Olli Etuaho856c4972016-08-08 11:38:39 +03002764 unsigned int arraySize = 0;
2765 if (arrayIndex != nullptr)
Jamie Madill98493dd2013-07-08 14:39:03 -04002766 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002767 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04002768 }
2769
Jamie Madillb98c3a82015-07-23 14:26:04 -04002770 TInterfaceBlock *interfaceBlock =
2771 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2772 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
2773 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002774
2775 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04002776 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002777
Jamie Madill98493dd2013-07-08 14:39:03 -04002778 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002779 {
2780 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002781 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2782 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002783 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302784 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002785
2786 // set parent pointer of the field variable
2787 fieldType->setInterfaceBlock(interfaceBlock);
2788
Arun Patole7e7e68d2015-05-22 12:02:25 +05302789 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002790 fieldVariable->setQualifier(typeQualifier.qualifier);
2791
Arun Patole7e7e68d2015-05-22 12:02:25 +05302792 if (!symbolTable.declare(fieldVariable))
2793 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002794 error(field->line(), "redefinition", field->name().c_str(),
2795 "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002796 }
2797 }
2798 }
2799 else
2800 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002801 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03002802
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002803 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302804 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002805 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002806
Arun Patole7e7e68d2015-05-22 12:02:25 +05302807 if (!symbolTable.declare(instanceTypeDef))
2808 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002809 error(instanceLine, "redefinition", instanceName->c_str(),
2810 "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002811 }
2812
Jamie Madillb98c3a82015-07-23 14:26:04 -04002813 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002814 symbolName = instanceTypeDef->getName();
2815 }
2816
Olli Etuaho13389b62016-10-16 11:48:18 +01002817 TIntermSymbol *blockSymbol =
2818 intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line);
2819 TIntermDeclaration *declaration = new TIntermDeclaration();
2820 declaration->appendDeclarator(blockSymbol);
2821 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04002822
2823 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01002824 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002825}
2826
Olli Etuaho383b7912016-08-05 11:22:59 +03002827void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002828{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002829 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002830
2831 // Embedded structure definitions are not supported per GLSL ES spec.
2832 // They aren't allowed in GLSL either, but we need to detect this here
2833 // so we don't rely on the GLSL compiler to catch it.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302834 if (mStructNestingLevel > 1)
2835 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002836 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002837 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002838}
2839
2840void TParseContext::exitStructDeclaration()
2841{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002842 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002843}
2844
Olli Etuaho8a176262016-08-16 14:23:01 +03002845void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002846{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302847 if (!IsWebGLBasedSpec(mShaderSpec))
2848 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002849 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002850 }
2851
Arun Patole7e7e68d2015-05-22 12:02:25 +05302852 if (field.type()->getBasicType() != EbtStruct)
2853 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002854 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002855 }
2856
2857 // We're already inside a structure definition at this point, so add
2858 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302859 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2860 {
Jamie Madill41a49272014-03-18 16:10:13 -04002861 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002862 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
2863 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04002864 std::string reason = reasonStream.str();
2865 error(line, reason.c_str(), field.name().c_str(), "");
Olli Etuaho8a176262016-08-16 14:23:01 +03002866 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002867 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002868}
2869
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002870//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002871// Parse an array index expression
2872//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002873TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
2874 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302875 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002876{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002877 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2878 {
2879 if (baseExpression->getAsSymbolNode())
2880 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302881 error(location, " left of '[' is not of type array, matrix, or vector ",
2882 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002883 }
2884 else
2885 {
2886 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2887 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002888
2889 TConstantUnion *unionArray = new TConstantUnion[1];
2890 unionArray->setFConst(0.0f);
2891 return intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst),
2892 location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002893 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002894
Jamie Madill21c1e452014-12-29 11:33:41 -05002895 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2896
Olli Etuaho36b05142015-11-12 13:10:42 +02002897 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
2898 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
2899 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
2900 // index is a constant expression.
2901 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
2902 {
2903 if (baseExpression->isInterfaceBlock())
2904 {
2905 error(
2906 location, "", "[",
2907 "array indexes for interface blocks arrays must be constant integral expressions");
Olli Etuaho36b05142015-11-12 13:10:42 +02002908 }
2909 else if (baseExpression->getQualifier() == EvqFragmentOut)
2910 {
2911 error(location, "", "[",
2912 "array indexes for fragment outputs must be constant integral expressions");
Olli Etuaho36b05142015-11-12 13:10:42 +02002913 }
Olli Etuaho3e960462015-11-12 15:58:39 +02002914 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
2915 {
2916 error(location, "", "[", "array index for gl_FragData must be constant zero");
Olli Etuaho3e960462015-11-12 15:58:39 +02002917 }
Olli Etuaho36b05142015-11-12 13:10:42 +02002918 }
2919
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002920 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002921 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002922 // If an out-of-range index is not qualified as constant, the behavior in the spec is
2923 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
2924 // constant fold expressions that are not constant expressions). The most compatible way to
2925 // handle this case is to report a warning instead of an error and force the index to be in
2926 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002927 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Jamie Madill21c1e452014-12-29 11:33:41 -05002928 int index = indexConstantUnion->getIConst(0);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002929
2930 int safeIndex = -1;
2931
2932 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04002933 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002934 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03002935 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002936 if (mShaderSpec == SH_WEBGL2_SPEC)
2937 {
2938 // Error has been already generated if index is not const.
2939 if (indexExpression->getQualifier() == EvqConst)
2940 {
2941 error(location, "", "[",
2942 "array index for gl_FragData must be constant zero");
2943 }
2944 safeIndex = 0;
2945 }
2946 else if (!isExtensionEnabled("GL_EXT_draw_buffers"))
2947 {
2948 outOfRangeError(outOfRangeIndexIsError, location, "", "[",
2949 "array index for gl_FragData must be zero when "
2950 "GL_EXT_draw_buffers is disabled");
2951 safeIndex = 0;
2952 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03002953 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002954 // Only do generic out-of-range check if similar error hasn't already been reported.
2955 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03002956 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002957 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
2958 baseExpression->getArraySize(),
2959 "array index out of range", "[]");
2960 }
2961 }
2962 else if (baseExpression->isMatrix())
2963 {
2964 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03002965 baseExpression->getType().getCols(),
2966 "matrix field selection out of range", "[]");
Jamie Madill7164cf42013-07-08 13:30:59 -04002967 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002968 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04002969 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002970 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
2971 baseExpression->getType().getNominalSize(),
2972 "vector field selection out of range", "[]");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002973 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002974
2975 ASSERT(safeIndex >= 0);
2976 // Data of constant unions can't be changed, because it may be shared with other
2977 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
2978 // sanitized object.
2979 if (safeIndex != index)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002980 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002981 TConstantUnion *safeConstantUnion = new TConstantUnion();
2982 safeConstantUnion->setIConst(safeIndex);
2983 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002984 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002985
2986 return intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location,
2987 &mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002988 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002989 else
2990 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002991 return intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location,
2992 &mDiagnostics);
Jamie Madill7164cf42013-07-08 13:30:59 -04002993 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002994}
2995
Olli Etuaho90892fb2016-07-14 14:44:51 +03002996int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
2997 const TSourceLoc &location,
2998 int index,
2999 int arraySize,
3000 const char *reason,
3001 const char *token)
3002{
3003 if (index >= arraySize || index < 0)
3004 {
3005 std::stringstream extraInfoStream;
3006 extraInfoStream << "'" << index << "'";
3007 std::string extraInfo = extraInfoStream.str();
3008 outOfRangeError(outOfRangeIndexIsError, location, reason, token, extraInfo.c_str());
3009 if (index < 0)
3010 {
3011 return 0;
3012 }
3013 else
3014 {
3015 return arraySize - 1;
3016 }
3017 }
3018 return index;
3019}
3020
Jamie Madillb98c3a82015-07-23 14:26:04 -04003021TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3022 const TSourceLoc &dotLocation,
3023 const TString &fieldString,
3024 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003025{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003026 if (baseExpression->isArray())
3027 {
3028 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003029 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003030 }
3031
3032 if (baseExpression->isVector())
3033 {
3034 TVectorFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003035 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields,
3036 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003037 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003038 fields.num = 1;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003039 fields.offsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003040 }
3041
Olli Etuahob6fa0432016-09-28 16:28:05 +01003042 return TIntermediate::AddSwizzle(baseExpression, fields, dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003043 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003044 else if (baseExpression->getBasicType() == EbtStruct)
3045 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303046 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003047 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003048 {
3049 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003050 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003051 }
3052 else
3053 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003054 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003055 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003056 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003057 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003058 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003059 {
3060 fieldFound = true;
3061 break;
3062 }
3063 }
3064 if (fieldFound)
3065 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003066 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3067 index->setLine(fieldLocation);
3068 return intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index,
3069 dotLocation, &mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003070 }
3071 else
3072 {
3073 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003074 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003075 }
3076 }
3077 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003078 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003079 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303080 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003081 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003082 {
3083 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003084 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003085 }
3086 else
3087 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003088 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003089 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003090 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003091 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003092 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003093 {
3094 fieldFound = true;
3095 break;
3096 }
3097 }
3098 if (fieldFound)
3099 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003100 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3101 index->setLine(fieldLocation);
3102 return intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index,
3103 dotLocation, &mDiagnostics);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003104 }
3105 else
3106 {
3107 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003108 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003109 }
3110 }
3111 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003112 else
3113 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003114 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003115 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03003116 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303117 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003118 }
3119 else
3120 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303121 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003122 " field selection requires structure, vector, or interface block on left hand "
3123 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303124 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003125 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003126 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003127 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003128}
3129
Jamie Madillb98c3a82015-07-23 14:26:04 -04003130TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3131 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003132{
Martin Radev802abe02016-08-04 17:48:32 +03003133 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003134
3135 if (qualifierType == "shared")
3136 {
Olli Etuahof0173152016-10-17 09:05:03 -07003137 if (IsWebGLBasedSpec(mShaderSpec))
3138 {
3139 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
3140 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003141 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003142 }
3143 else if (qualifierType == "packed")
3144 {
Olli Etuahof0173152016-10-17 09:05:03 -07003145 if (IsWebGLBasedSpec(mShaderSpec))
3146 {
3147 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
3148 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003149 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003150 }
3151 else if (qualifierType == "std140")
3152 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003153 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003154 }
3155 else if (qualifierType == "row_major")
3156 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003157 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003158 }
3159 else if (qualifierType == "column_major")
3160 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003161 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003162 }
3163 else if (qualifierType == "location")
3164 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003165 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
3166 "location requires an argument");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003167 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003168 else if (qualifierType == "rgba32f")
3169 {
3170 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3171 qualifier.imageInternalFormat = EiifRGBA32F;
3172 }
3173 else if (qualifierType == "rgba16f")
3174 {
3175 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3176 qualifier.imageInternalFormat = EiifRGBA16F;
3177 }
3178 else if (qualifierType == "r32f")
3179 {
3180 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3181 qualifier.imageInternalFormat = EiifR32F;
3182 }
3183 else if (qualifierType == "rgba8")
3184 {
3185 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3186 qualifier.imageInternalFormat = EiifRGBA8;
3187 }
3188 else if (qualifierType == "rgba8_snorm")
3189 {
3190 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3191 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
3192 }
3193 else if (qualifierType == "rgba32i")
3194 {
3195 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3196 qualifier.imageInternalFormat = EiifRGBA32I;
3197 }
3198 else if (qualifierType == "rgba16i")
3199 {
3200 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3201 qualifier.imageInternalFormat = EiifRGBA16I;
3202 }
3203 else if (qualifierType == "rgba8i")
3204 {
3205 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3206 qualifier.imageInternalFormat = EiifRGBA8I;
3207 }
3208 else if (qualifierType == "r32i")
3209 {
3210 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3211 qualifier.imageInternalFormat = EiifR32I;
3212 }
3213 else if (qualifierType == "rgba32ui")
3214 {
3215 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3216 qualifier.imageInternalFormat = EiifRGBA32UI;
3217 }
3218 else if (qualifierType == "rgba16ui")
3219 {
3220 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3221 qualifier.imageInternalFormat = EiifRGBA16UI;
3222 }
3223 else if (qualifierType == "rgba8ui")
3224 {
3225 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3226 qualifier.imageInternalFormat = EiifRGBA8UI;
3227 }
3228 else if (qualifierType == "r32ui")
3229 {
3230 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3231 qualifier.imageInternalFormat = EiifR32UI;
3232 }
3233
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003234 else
3235 {
3236 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003237 }
3238
Jamie Madilla5efff92013-06-06 11:56:47 -04003239 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003240}
3241
Martin Radev802abe02016-08-04 17:48:32 +03003242void TParseContext::parseLocalSize(const TString &qualifierType,
3243 const TSourceLoc &qualifierTypeLine,
3244 int intValue,
3245 const TSourceLoc &intValueLine,
3246 const std::string &intValueString,
3247 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03003248 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03003249{
Olli Etuaho856c4972016-08-08 11:38:39 +03003250 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03003251 if (intValue < 1)
3252 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03003253 std::string errorMessage = std::string(getWorkGroupSizeString(index)) + " must be positive";
Martin Radev802abe02016-08-04 17:48:32 +03003254 error(intValueLine, "out of range:", intValueString.c_str(), errorMessage.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003255 }
3256 (*localSize)[index] = intValue;
3257}
3258
Jamie Madillb98c3a82015-07-23 14:26:04 -04003259TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3260 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003261 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303262 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003263{
Martin Radev802abe02016-08-04 17:48:32 +03003264 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003265
Martin Radev802abe02016-08-04 17:48:32 +03003266 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003267
Martin Radev802abe02016-08-04 17:48:32 +03003268 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003269 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003270 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003271 if (intValue < 0)
3272 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003273 error(intValueLine, "out of range:", intValueString.c_str(),
3274 "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003275 }
3276 else
3277 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003278 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03003279 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003280 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003281 }
Martin Radev802abe02016-08-04 17:48:32 +03003282 else if (qualifierType == "local_size_x")
3283 {
3284 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
3285 &qualifier.localSize);
3286 }
3287 else if (qualifierType == "local_size_y")
3288 {
3289 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
3290 &qualifier.localSize);
3291 }
3292 else if (qualifierType == "local_size_z")
3293 {
3294 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
3295 &qualifier.localSize);
3296 }
3297 else
3298 {
3299 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003300 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003301
Jamie Madilla5efff92013-06-06 11:56:47 -04003302 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003303}
3304
Olli Etuaho613b9592016-09-05 12:05:53 +03003305TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
3306{
3307 return new TTypeQualifierBuilder(
3308 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
3309 mShaderVersion);
3310}
3311
Jamie Madillb98c3a82015-07-23 14:26:04 -04003312TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03003313 TLayoutQualifier rightQualifier,
3314 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003315{
Martin Radevc28888b2016-07-22 15:27:42 +03003316 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
3317 &mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003318}
3319
Martin Radev70866b82016-07-22 15:27:42 +03003320TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
3321 const TTypeQualifierBuilder &typeQualifierBuilder,
3322 TPublicType *typeSpecifier,
3323 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003324{
Olli Etuaho613b9592016-09-05 12:05:53 +03003325 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003326
Martin Radev70866b82016-07-22 15:27:42 +03003327 typeSpecifier->qualifier = typeQualifier.qualifier;
3328 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03003329 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03003330 typeSpecifier->invariant = typeQualifier.invariant;
3331 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05303332 {
Martin Radev70866b82016-07-22 15:27:42 +03003333 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003334 }
Martin Radev70866b82016-07-22 15:27:42 +03003335 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003336}
3337
Jamie Madillb98c3a82015-07-23 14:26:04 -04003338TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3339 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003340{
Martin Radev4a9cd802016-09-01 16:51:51 +03003341 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
3342 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03003343
Martin Radev4a9cd802016-09-01 16:51:51 +03003344 checkIsNonVoid(typeSpecifier.getLine(), (*fieldList)[0]->name(), typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003345
Martin Radev4a9cd802016-09-01 16:51:51 +03003346 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003347
Arun Patole7e7e68d2015-05-22 12:02:25 +05303348 for (unsigned int i = 0; i < fieldList->size(); ++i)
3349 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003350 //
3351 // Careful not to replace already known aspects of type, like array-ness
3352 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303353 TType *type = (*fieldList)[i]->type();
Martin Radev4a9cd802016-09-01 16:51:51 +03003354 type->setBasicType(typeSpecifier.getBasicType());
3355 type->setPrimarySize(typeSpecifier.getPrimarySize());
3356 type->setSecondarySize(typeSpecifier.getSecondarySize());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003357 type->setPrecision(typeSpecifier.precision);
3358 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003359 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
Martin Radev2cc85b32016-08-05 16:22:53 +03003360 type->setMemoryQualifier(typeSpecifier.memoryQualifier);
Martin Radev70866b82016-07-22 15:27:42 +03003361 type->setInvariant(typeSpecifier.invariant);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003362
3363 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303364 if (type->isArray())
3365 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003366 checkIsValidTypeForArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003367 }
3368 if (typeSpecifier.array)
Olli Etuaho856c4972016-08-08 11:38:39 +03003369 type->setArraySize(static_cast<unsigned int>(typeSpecifier.arraySize));
Martin Radev4a9cd802016-09-01 16:51:51 +03003370 if (typeSpecifier.getUserDef())
Arun Patole7e7e68d2015-05-22 12:02:25 +05303371 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003372 type->setStruct(typeSpecifier.getUserDef()->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003373 }
3374
Martin Radev4a9cd802016-09-01 16:51:51 +03003375 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*fieldList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003376 }
3377
Jamie Madill98493dd2013-07-08 14:39:03 -04003378 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003379}
3380
Martin Radev4a9cd802016-09-01 16:51:51 +03003381TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
3382 const TSourceLoc &nameLine,
3383 const TString *structName,
3384 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003385{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303386 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003387 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003388
Jamie Madill9b820842015-02-12 10:40:10 -05003389 // Store a bool in the struct if we're at global scope, to allow us to
3390 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05003391 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003392
Jamie Madill98493dd2013-07-08 14:39:03 -04003393 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003394 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003395 checkIsNotReserved(nameLine, *structName);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303396 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
3397 if (!symbolTable.declare(userTypeDef))
3398 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003399 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003400 }
3401 }
3402
3403 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04003404 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003405 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003406 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04003407 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003408 switch (qualifier)
3409 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003410 case EvqGlobal:
3411 case EvqTemporary:
3412 break;
3413 default:
3414 error(field.line(), "invalid qualifier on struct member",
3415 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003416 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003417 }
Martin Radev70866b82016-07-22 15:27:42 +03003418 if (field.type()->isInvariant())
3419 {
3420 error(field.line(), "invalid qualifier on struct member", "invariant");
3421 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003422 if (IsImage(field.type()->getBasicType()))
3423 {
3424 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
3425 }
3426
3427 checkIsMemoryQualifierNotSpecified(field.type()->getMemoryQualifier(), field.line());
Martin Radev70866b82016-07-22 15:27:42 +03003428
3429 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003430 }
3431
Martin Radev4a9cd802016-09-01 16:51:51 +03003432 TTypeSpecifierNonArray typeSpecifierNonArray;
3433 typeSpecifierNonArray.initialize(EbtStruct, structLine);
3434 typeSpecifierNonArray.userDef = structureType;
3435 typeSpecifierNonArray.isStructSpecifier = true;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003436 exitStructDeclaration();
3437
Martin Radev4a9cd802016-09-01 16:51:51 +03003438 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003439}
3440
Jamie Madillb98c3a82015-07-23 14:26:04 -04003441TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003442 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003443 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02003444{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003445 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003446 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02003447 init->isVector())
3448 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003449 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
3450 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003451 return nullptr;
3452 }
3453
Olli Etuahoac5274d2015-02-20 10:19:08 +02003454 if (statementList)
3455 {
3456 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
3457 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02003458 return nullptr;
3459 }
3460 }
3461
Olli Etuahoa3a36662015-02-17 13:46:51 +02003462 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3463 if (node == nullptr)
3464 {
3465 error(loc, "erroneous switch statement", "switch");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003466 return nullptr;
3467 }
3468 return node;
3469}
3470
3471TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3472{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003473 if (mSwitchNestingLevel == 0)
3474 {
3475 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003476 return nullptr;
3477 }
3478 if (condition == nullptr)
3479 {
3480 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003481 return nullptr;
3482 }
3483 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04003484 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02003485 {
3486 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003487 }
3488 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003489 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
3490 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
3491 // fold in case labels.
3492 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02003493 {
3494 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003495 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003496 TIntermCase *node = intermediate.addCase(condition, loc);
3497 if (node == nullptr)
3498 {
3499 error(loc, "erroneous case statement", "case");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003500 return nullptr;
3501 }
3502 return node;
3503}
3504
3505TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3506{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003507 if (mSwitchNestingLevel == 0)
3508 {
3509 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003510 return nullptr;
3511 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003512 TIntermCase *node = intermediate.addCase(nullptr, loc);
3513 if (node == nullptr)
3514 {
3515 error(loc, "erroneous default statement", "default");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003516 return nullptr;
3517 }
3518 return node;
3519}
3520
Jamie Madillb98c3a82015-07-23 14:26:04 -04003521TIntermTyped *TParseContext::createUnaryMath(TOperator op,
3522 TIntermTyped *child,
3523 const TSourceLoc &loc,
3524 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003525{
3526 if (child == nullptr)
3527 {
3528 return nullptr;
3529 }
3530
3531 switch (op)
3532 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003533 case EOpLogicalNot:
3534 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
3535 child->isVector())
3536 {
3537 return nullptr;
3538 }
3539 break;
3540 case EOpBitwiseNot:
3541 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3542 child->isMatrix() || child->isArray())
3543 {
3544 return nullptr;
3545 }
3546 break;
3547 case EOpPostIncrement:
3548 case EOpPreIncrement:
3549 case EOpPostDecrement:
3550 case EOpPreDecrement:
3551 case EOpNegative:
3552 case EOpPositive:
3553 if (child->getBasicType() == EbtStruct || child->getBasicType() == EbtBool ||
Martin Radev2cc85b32016-08-05 16:22:53 +03003554 child->isArray() || IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04003555 {
3556 return nullptr;
3557 }
3558 // Operators for built-ins are already type checked against their prototype.
3559 default:
3560 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003561 }
3562
Olli Etuahof119a262016-08-19 15:54:22 +03003563 TIntermUnary *node = new TIntermUnary(op, child);
3564 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03003565
3566 TIntermTyped *foldedNode = node->fold(&mDiagnostics);
3567 if (foldedNode)
3568 return foldedNode;
3569
3570 return node;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003571}
3572
Olli Etuaho09b22472015-02-11 11:47:26 +02003573TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3574{
Olli Etuahof6c694b2015-03-26 14:50:53 +02003575 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003576 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003577 {
3578 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003579 return child;
3580 }
3581 return node;
3582}
3583
Jamie Madillb98c3a82015-07-23 14:26:04 -04003584TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
3585 TIntermTyped *child,
3586 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003587{
Olli Etuaho856c4972016-08-08 11:38:39 +03003588 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02003589 return addUnaryMath(op, child, loc);
3590}
3591
Jamie Madillb98c3a82015-07-23 14:26:04 -04003592bool TParseContext::binaryOpCommonCheck(TOperator op,
3593 TIntermTyped *left,
3594 TIntermTyped *right,
3595 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003596{
Olli Etuaho244be012016-08-18 15:26:02 +03003597 if (left->getType().getStruct() || right->getType().getStruct())
3598 {
3599 switch (op)
3600 {
3601 case EOpIndexDirectStruct:
3602 ASSERT(left->getType().getStruct());
3603 break;
3604 case EOpEqual:
3605 case EOpNotEqual:
3606 case EOpAssign:
3607 case EOpInitialize:
3608 if (left->getType() != right->getType())
3609 {
3610 return false;
3611 }
3612 break;
3613 default:
3614 error(loc, "Invalid operation for structs", GetOperatorString(op));
3615 return false;
3616 }
3617 }
3618
Olli Etuahod6b14282015-03-17 14:31:35 +02003619 if (left->isArray() || right->isArray())
3620 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003621 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003622 {
3623 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3624 return false;
3625 }
3626
3627 if (left->isArray() != right->isArray())
3628 {
3629 error(loc, "array / non-array mismatch", GetOperatorString(op));
3630 return false;
3631 }
3632
3633 switch (op)
3634 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003635 case EOpEqual:
3636 case EOpNotEqual:
3637 case EOpAssign:
3638 case EOpInitialize:
3639 break;
3640 default:
3641 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3642 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02003643 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003644 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003645 if (left->getArraySize() != right->getArraySize())
3646 {
3647 error(loc, "array size mismatch", GetOperatorString(op));
3648 return false;
3649 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003650 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003651
3652 // Check ops which require integer / ivec parameters
3653 bool isBitShift = false;
3654 switch (op)
3655 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003656 case EOpBitShiftLeft:
3657 case EOpBitShiftRight:
3658 case EOpBitShiftLeftAssign:
3659 case EOpBitShiftRightAssign:
3660 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3661 // check that the basic type is an integer type.
3662 isBitShift = true;
3663 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3664 {
3665 return false;
3666 }
3667 break;
3668 case EOpBitwiseAnd:
3669 case EOpBitwiseXor:
3670 case EOpBitwiseOr:
3671 case EOpBitwiseAndAssign:
3672 case EOpBitwiseXorAssign:
3673 case EOpBitwiseOrAssign:
3674 // It is enough to check the type of only one operand, since later it
3675 // is checked that the operand types match.
3676 if (!IsInteger(left->getBasicType()))
3677 {
3678 return false;
3679 }
3680 break;
3681 default:
3682 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003683 }
3684
3685 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3686 // So the basic type should usually match.
3687 if (!isBitShift && left->getBasicType() != right->getBasicType())
3688 {
3689 return false;
3690 }
3691
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003692 // Check that:
3693 // 1. Type sizes match exactly on ops that require that.
3694 // 2. Restrictions for structs that contain arrays or samplers are respected.
3695 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003696 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003697 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003698 case EOpAssign:
3699 case EOpInitialize:
3700 case EOpEqual:
3701 case EOpNotEqual:
3702 // ESSL 1.00 sections 5.7, 5.8, 5.9
3703 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
3704 {
3705 error(loc, "undefined operation for structs containing arrays",
3706 GetOperatorString(op));
3707 return false;
3708 }
3709 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3710 // we interpret the spec so that this extends to structs containing samplers,
3711 // similarly to ESSL 1.00 spec.
3712 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
3713 left->getType().isStructureContainingSamplers())
3714 {
3715 error(loc, "undefined operation for structs containing samplers",
3716 GetOperatorString(op));
3717 return false;
3718 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003719
3720 if ((op == EOpAssign || op == EOpInitialize) &&
3721 left->getType().isStructureContainingImages())
3722 {
3723 error(loc, "undefined operation for structs containing images",
3724 GetOperatorString(op));
3725 return false;
3726 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003727 case EOpLessThan:
3728 case EOpGreaterThan:
3729 case EOpLessThanEqual:
3730 case EOpGreaterThanEqual:
3731 if ((left->getNominalSize() != right->getNominalSize()) ||
3732 (left->getSecondarySize() != right->getSecondarySize()))
3733 {
3734 return false;
3735 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003736 break;
3737 case EOpAdd:
3738 case EOpSub:
3739 case EOpDiv:
3740 case EOpIMod:
3741 case EOpBitShiftLeft:
3742 case EOpBitShiftRight:
3743 case EOpBitwiseAnd:
3744 case EOpBitwiseXor:
3745 case EOpBitwiseOr:
3746 case EOpAddAssign:
3747 case EOpSubAssign:
3748 case EOpDivAssign:
3749 case EOpIModAssign:
3750 case EOpBitShiftLeftAssign:
3751 case EOpBitShiftRightAssign:
3752 case EOpBitwiseAndAssign:
3753 case EOpBitwiseXorAssign:
3754 case EOpBitwiseOrAssign:
3755 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
3756 {
3757 return false;
3758 }
3759
3760 // Are the sizes compatible?
3761 if (left->getNominalSize() != right->getNominalSize() ||
3762 left->getSecondarySize() != right->getSecondarySize())
3763 {
3764 // If the nominal sizes of operands do not match:
3765 // One of them must be a scalar.
3766 if (!left->isScalar() && !right->isScalar())
3767 return false;
3768
3769 // In the case of compound assignment other than multiply-assign,
3770 // the right side needs to be a scalar. Otherwise a vector/matrix
3771 // would be assigned to a scalar. A scalar can't be shifted by a
3772 // vector either.
3773 if (!right->isScalar() &&
3774 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
3775 return false;
3776 }
3777 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003778 default:
3779 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003780 }
3781
Olli Etuahod6b14282015-03-17 14:31:35 +02003782 return true;
3783}
3784
Olli Etuaho1dded802016-08-18 18:13:13 +03003785bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
3786 const TType &left,
3787 const TType &right)
3788{
3789 switch (op)
3790 {
3791 case EOpMul:
3792 case EOpMulAssign:
3793 return left.getNominalSize() == right.getNominalSize() &&
3794 left.getSecondarySize() == right.getSecondarySize();
3795 case EOpVectorTimesScalar:
3796 return true;
3797 case EOpVectorTimesScalarAssign:
3798 ASSERT(!left.isMatrix() && !right.isMatrix());
3799 return left.isVector() && !right.isVector();
3800 case EOpVectorTimesMatrix:
3801 return left.getNominalSize() == right.getRows();
3802 case EOpVectorTimesMatrixAssign:
3803 ASSERT(!left.isMatrix() && right.isMatrix());
3804 return left.isVector() && left.getNominalSize() == right.getRows() &&
3805 left.getNominalSize() == right.getCols();
3806 case EOpMatrixTimesVector:
3807 return left.getCols() == right.getNominalSize();
3808 case EOpMatrixTimesScalar:
3809 return true;
3810 case EOpMatrixTimesScalarAssign:
3811 ASSERT(left.isMatrix() && !right.isMatrix());
3812 return !right.isVector();
3813 case EOpMatrixTimesMatrix:
3814 return left.getCols() == right.getRows();
3815 case EOpMatrixTimesMatrixAssign:
3816 ASSERT(left.isMatrix() && right.isMatrix());
3817 // We need to check two things:
3818 // 1. The matrix multiplication step is valid.
3819 // 2. The result will have the same number of columns as the lvalue.
3820 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
3821
3822 default:
3823 UNREACHABLE();
3824 return false;
3825 }
3826}
3827
Jamie Madillb98c3a82015-07-23 14:26:04 -04003828TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
3829 TIntermTyped *left,
3830 TIntermTyped *right,
3831 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02003832{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003833 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003834 return nullptr;
3835
Olli Etuahofc1806e2015-03-17 13:03:11 +02003836 switch (op)
3837 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003838 case EOpEqual:
3839 case EOpNotEqual:
3840 break;
3841 case EOpLessThan:
3842 case EOpGreaterThan:
3843 case EOpLessThanEqual:
3844 case EOpGreaterThanEqual:
Olli Etuaho244be012016-08-18 15:26:02 +03003845 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3846 !right->getType().getStruct());
3847 if (left->isMatrix() || left->isVector())
Jamie Madillb98c3a82015-07-23 14:26:04 -04003848 {
3849 return nullptr;
3850 }
3851 break;
3852 case EOpLogicalOr:
3853 case EOpLogicalXor:
3854 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03003855 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3856 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00003857 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04003858 {
3859 return nullptr;
3860 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00003861 // Basic types matching should have been already checked.
3862 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003863 break;
3864 case EOpAdd:
3865 case EOpSub:
3866 case EOpDiv:
3867 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03003868 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3869 !right->getType().getStruct());
3870 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04003871 {
3872 return nullptr;
3873 }
3874 break;
3875 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03003876 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3877 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003878 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03003879 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04003880 {
3881 return nullptr;
3882 }
3883 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003884 default:
3885 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003886 }
3887
Olli Etuaho1dded802016-08-18 18:13:13 +03003888 if (op == EOpMul)
3889 {
3890 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
3891 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
3892 {
3893 return nullptr;
3894 }
3895 }
3896
Olli Etuaho3fdec912016-08-18 15:08:06 +03003897 TIntermBinary *node = new TIntermBinary(op, left, right);
3898 node->setLine(loc);
3899
Olli Etuaho3fdec912016-08-18 15:08:06 +03003900 // See if we can fold constants.
3901 TIntermTyped *foldedNode = node->fold(&mDiagnostics);
3902 if (foldedNode)
3903 return foldedNode;
3904
3905 return node;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003906}
3907
Jamie Madillb98c3a82015-07-23 14:26:04 -04003908TIntermTyped *TParseContext::addBinaryMath(TOperator op,
3909 TIntermTyped *left,
3910 TIntermTyped *right,
3911 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003912{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003913 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003914 if (node == 0)
3915 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003916 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3917 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003918 return left;
3919 }
3920 return node;
3921}
3922
Jamie Madillb98c3a82015-07-23 14:26:04 -04003923TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
3924 TIntermTyped *left,
3925 TIntermTyped *right,
3926 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003927{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003928 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003929 if (node == 0)
3930 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003931 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3932 right->getCompleteString());
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003933 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02003934 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003935 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
3936 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003937 }
3938 return node;
3939}
3940
Olli Etuaho13389b62016-10-16 11:48:18 +01003941TIntermBinary *TParseContext::createAssign(TOperator op,
3942 TIntermTyped *left,
3943 TIntermTyped *right,
3944 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003945{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003946 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003947 {
Olli Etuaho1dded802016-08-18 18:13:13 +03003948 if (op == EOpMulAssign)
3949 {
3950 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
3951 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
3952 {
3953 return nullptr;
3954 }
3955 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03003956 TIntermBinary *node = new TIntermBinary(op, left, right);
3957 node->setLine(loc);
3958
Olli Etuaho3fdec912016-08-18 15:08:06 +03003959 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02003960 }
3961 return nullptr;
3962}
3963
Jamie Madillb98c3a82015-07-23 14:26:04 -04003964TIntermTyped *TParseContext::addAssign(TOperator op,
3965 TIntermTyped *left,
3966 TIntermTyped *right,
3967 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003968{
3969 TIntermTyped *node = createAssign(op, left, right, loc);
3970 if (node == nullptr)
3971 {
3972 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02003973 return left;
3974 }
3975 return node;
3976}
3977
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003978TIntermTyped *TParseContext::addComma(TIntermTyped *left,
3979 TIntermTyped *right,
3980 const TSourceLoc &loc)
3981{
Corentin Wallez0d959252016-07-12 17:26:32 -04003982 // WebGL2 section 5.26, the following results in an error:
3983 // "Sequence operator applied to void, arrays, or structs containing arrays"
3984 if (mShaderSpec == SH_WEBGL2_SPEC && (left->isArray() || left->getBasicType() == EbtVoid ||
3985 left->getType().isStructureContainingArrays() ||
3986 right->isArray() || right->getBasicType() == EbtVoid ||
3987 right->getType().isStructureContainingArrays()))
3988 {
3989 error(loc,
3990 "sequence operator is not allowed for void, arrays, or structs containing arrays",
3991 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04003992 }
3993
Olli Etuaho4db7ded2016-10-13 12:23:11 +01003994 return TIntermediate::AddComma(left, right, loc, mShaderVersion);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003995}
3996
Olli Etuaho49300862015-02-20 14:54:49 +02003997TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3998{
3999 switch (op)
4000 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004001 case EOpContinue:
4002 if (mLoopNestingLevel <= 0)
4003 {
4004 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004005 }
4006 break;
4007 case EOpBreak:
4008 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
4009 {
4010 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004011 }
4012 break;
4013 case EOpReturn:
4014 if (mCurrentFunctionType->getBasicType() != EbtVoid)
4015 {
4016 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004017 }
4018 break;
4019 default:
4020 // No checks for discard
4021 break;
Olli Etuaho49300862015-02-20 14:54:49 +02004022 }
4023 return intermediate.addBranch(op, loc);
4024}
4025
Jamie Madillb98c3a82015-07-23 14:26:04 -04004026TIntermBranch *TParseContext::addBranch(TOperator op,
4027 TIntermTyped *returnValue,
4028 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02004029{
4030 ASSERT(op == EOpReturn);
4031 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004032 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02004033 {
4034 error(loc, "void function cannot return a value", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02004035 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004036 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02004037 {
4038 error(loc, "function return is not matching type:", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02004039 }
4040 return intermediate.addBranch(op, returnValue, loc);
4041}
4042
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004043void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
4044{
4045 ASSERT(!functionCall->isUserDefined());
Olli Etuahobd674552016-10-06 13:28:42 +01004046 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004047 TIntermNode *offset = nullptr;
4048 TIntermSequence *arguments = functionCall->getSequence();
4049 if (name.compare(0, 16, "texelFetchOffset") == 0 ||
4050 name.compare(0, 16, "textureLodOffset") == 0 ||
4051 name.compare(0, 20, "textureProjLodOffset") == 0 ||
4052 name.compare(0, 17, "textureGradOffset") == 0 ||
4053 name.compare(0, 21, "textureProjGradOffset") == 0)
4054 {
4055 offset = arguments->back();
4056 }
4057 else if (name.compare(0, 13, "textureOffset") == 0 ||
4058 name.compare(0, 17, "textureProjOffset") == 0)
4059 {
4060 // A bias parameter might follow the offset parameter.
4061 ASSERT(arguments->size() >= 3);
4062 offset = (*arguments)[2];
4063 }
4064 if (offset != nullptr)
4065 {
4066 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
4067 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
4068 {
4069 TString unmangledName = TFunction::unmangleName(name);
4070 error(functionCall->getLine(), "Texture offset must be a constant expression",
4071 unmangledName.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004072 }
4073 else
4074 {
4075 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
4076 size_t size = offsetConstantUnion->getType().getObjectSize();
4077 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
4078 for (size_t i = 0u; i < size; ++i)
4079 {
4080 int offsetValue = values[i].getIConst();
4081 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
4082 {
4083 std::stringstream tokenStream;
4084 tokenStream << offsetValue;
4085 std::string token = tokenStream.str();
4086 error(offset->getLine(), "Texture offset value out of valid range",
4087 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004088 }
4089 }
4090 }
4091 }
4092}
4093
Martin Radev2cc85b32016-08-05 16:22:53 +03004094// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
4095void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
4096{
4097 ASSERT(!functionCall->isUserDefined());
4098 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
4099
4100 if (name.compare(0, 5, "image") == 0)
4101 {
4102 TIntermSequence *arguments = functionCall->getSequence();
4103 TIntermNode *imageNode = (*arguments)[0];
4104 TIntermSymbol *imageSymbol = imageNode->getAsSymbolNode();
4105
4106 const TMemoryQualifier &memoryQualifier = imageSymbol->getMemoryQualifier();
4107
4108 if (name.compare(5, 5, "Store") == 0)
4109 {
4110 if (memoryQualifier.readonly)
4111 {
4112 error(imageNode->getLine(),
4113 "'imageStore' cannot be used with images qualified as 'readonly'",
4114 imageSymbol->getSymbol().c_str());
4115 }
4116 }
4117 else if (name.compare(5, 4, "Load") == 0)
4118 {
4119 if (memoryQualifier.writeonly)
4120 {
4121 error(imageNode->getLine(),
4122 "'imageLoad' cannot be used with images qualified as 'writeonly'",
4123 imageSymbol->getSymbol().c_str());
4124 }
4125 }
4126 }
4127}
4128
4129// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
4130void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
4131 const TFunction *functionDefinition,
4132 const TIntermAggregate *functionCall)
4133{
4134 ASSERT(functionCall->isUserDefined());
4135
4136 const TIntermSequence &arguments = *functionCall->getSequence();
4137
4138 ASSERT(functionDefinition->getParamCount() == arguments.size());
4139
4140 for (size_t i = 0; i < arguments.size(); ++i)
4141 {
4142 const TType &functionArgumentType = arguments[i]->getAsTyped()->getType();
4143 const TType &functionParameterType = *functionDefinition->getParam(i).type;
4144 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
4145
4146 if (IsImage(functionArgumentType.getBasicType()))
4147 {
4148 const TMemoryQualifier &functionArgumentMemoryQualifier =
4149 functionArgumentType.getMemoryQualifier();
4150 const TMemoryQualifier &functionParameterMemoryQualifier =
4151 functionParameterType.getMemoryQualifier();
4152 if (functionArgumentMemoryQualifier.readonly &&
4153 !functionParameterMemoryQualifier.readonly)
4154 {
4155 error(functionCall->getLine(),
4156 "Function call discards the 'readonly' qualifier from image",
4157 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4158 }
4159
4160 if (functionArgumentMemoryQualifier.writeonly &&
4161 !functionParameterMemoryQualifier.writeonly)
4162 {
4163 error(functionCall->getLine(),
4164 "Function call discards the 'writeonly' qualifier from image",
4165 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4166 }
4167 }
4168 }
4169}
4170
Jamie Madillb98c3a82015-07-23 14:26:04 -04004171TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
4172 TIntermNode *paramNode,
4173 TIntermNode *thisNode,
4174 const TSourceLoc &loc,
4175 bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004176{
Jamie Madillb98c3a82015-07-23 14:26:04 -04004177 *fatalError = false;
4178 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004179 TIntermTyped *callNode = nullptr;
4180
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004181 if (thisNode != nullptr)
4182 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004183 TConstantUnion *unionArray = new TConstantUnion[1];
Jamie Madillb98c3a82015-07-23 14:26:04 -04004184 int arraySize = 0;
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004185 TIntermTyped *typedThis = thisNode->getAsTyped();
4186 if (fnCall->getName() != "length")
4187 {
4188 error(loc, "invalid method", fnCall->getName().c_str());
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004189 }
4190 else if (paramNode != nullptr)
4191 {
4192 error(loc, "method takes no parameters", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004193 }
4194 else if (typedThis == nullptr || !typedThis->isArray())
4195 {
4196 error(loc, "length can only be called on arrays", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004197 }
4198 else
4199 {
Olli Etuaho96e67382015-04-23 14:27:02 +03004200 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03004201 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004202 {
Olli Etuaho39282e12015-04-23 15:41:48 +03004203 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004204 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03004205 // (func()).length()
4206 // (int[3](0, 1, 2)).length()
Jamie Madillb98c3a82015-07-23 14:26:04 -04004207 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
4208 // expression.
4209 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
4210 // spec section 5.9 which allows "An array, vector or matrix expression with the
4211 // length method applied".
4212 error(loc, "length can only be called on array names, not on array expressions",
4213 "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004214 }
4215 }
Olli Etuaho96e67382015-04-23 14:27:02 +03004216 unionArray->setIConst(arraySize);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004217 callNode =
4218 intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004219 }
4220 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004221 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004222 // Then this should be a constructor.
Olli Etuaho856c4972016-08-08 11:38:39 +03004223 callNode = addConstructor(paramNode, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004224 }
4225 else
4226 {
4227 //
4228 // Not a constructor. Find it in the symbol table.
4229 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05304230 const TFunction *fnCandidate;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004231 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004232 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004233 if (fnCandidate)
4234 {
4235 //
4236 // A declared function.
4237 //
Olli Etuaho383b7912016-08-05 11:22:59 +03004238 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004239 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004240 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004241 }
4242 op = fnCandidate->getBuiltInOp();
4243 if (builtIn && op != EOpNull)
4244 {
4245 //
4246 // A function call mapped to a built-in operation.
4247 //
4248 if (fnCandidate->getParamCount() == 1)
4249 {
4250 //
4251 // Treat it like a built-in unary operator.
4252 //
Olli Etuaho15c2ac32015-11-09 15:51:43 +02004253 TIntermAggregate *paramAgg = paramNode->getAsAggregate();
4254 paramNode = paramAgg->getSequence()->front();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004255 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc,
4256 &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004257 if (callNode == nullptr)
4258 {
4259 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004260 extraInfoStream
4261 << "built in unary operator function. Type: "
4262 << static_cast<TIntermTyped *>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004263 std::string extraInfo = extraInfoStream.str();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004264 error(paramNode->getLine(), " wrong operand type", "Internal Error",
4265 extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004266 *fatalError = true;
4267 return nullptr;
4268 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004269 }
4270 else
4271 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004272 TIntermAggregate *aggregate =
4273 intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004274 aggregate->setType(fnCandidate->getReturnType());
4275 aggregate->setPrecisionFromChildren();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02004276 if (aggregate->areChildrenConstQualified())
4277 {
4278 aggregate->getTypePointer()->setQualifier(EvqConst);
4279 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004280
4281 // Some built-in functions have out parameters too.
4282 functionCallLValueErrorCheck(fnCandidate, aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05304283
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004284 // See if we can constant fold a built-in. Note that this may be possible even
4285 // if it is not const-qualified.
Olli Etuahof119a262016-08-19 15:54:22 +03004286 TIntermTyped *foldedNode =
4287 intermediate.foldAggregateBuiltIn(aggregate, &mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05304288 if (foldedNode)
4289 {
Arun Patole274f0702015-05-05 13:33:30 +05304290 callNode = foldedNode;
4291 }
Olli Etuahob43846e2015-06-02 18:18:57 +03004292 else
4293 {
4294 callNode = aggregate;
4295 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004296 }
4297 }
4298 else
4299 {
4300 // This is a real function call
Jamie Madillb98c3a82015-07-23 14:26:04 -04004301 TIntermAggregate *aggregate =
4302 intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004303 aggregate->setType(fnCandidate->getReturnType());
4304
Jamie Madillb98c3a82015-07-23 14:26:04 -04004305 // this is how we know whether the given function is a builtIn function or a user
4306 // defined function
4307 // if builtIn == false, it's a userDefined -> could be an overloaded
4308 // builtIn function also
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004309 // if builtIn == true, it's definitely a builtIn function with EOpNull
4310 if (!builtIn)
4311 aggregate->setUserDefined();
Olli Etuahobd674552016-10-06 13:28:42 +01004312 aggregate->getFunctionSymbolInfo()->setFromFunction(*fnCandidate);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004313
Olli Etuahobd674552016-10-06 13:28:42 +01004314 // This needs to happen after the function info including name is set
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004315 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004316 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004317 aggregate->setBuiltInFunctionPrecision();
4318
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004319 checkTextureOffsetConst(aggregate);
Martin Radev2cc85b32016-08-05 16:22:53 +03004320
4321 checkImageMemoryAccessForBuiltinFunctions(aggregate);
4322 }
4323 else
4324 {
4325 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, aggregate);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004326 }
4327
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004328 callNode = aggregate;
4329
4330 functionCallLValueErrorCheck(fnCandidate, aggregate);
4331 }
4332 }
4333 else
4334 {
4335 // error message was put out by findFunction()
4336 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004337 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004338 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004339 callNode = intermediate.addConstantUnion(unionArray,
4340 TType(EbtFloat, EbpUndefined, EvqConst), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004341 }
4342 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004343 return callNode;
4344}
4345
Jamie Madillb98c3a82015-07-23 14:26:04 -04004346TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004347 TIntermTyped *trueExpression,
4348 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03004349 const TSourceLoc &loc)
4350{
Olli Etuaho856c4972016-08-08 11:38:39 +03004351 checkIsScalarBool(loc, cond);
Olli Etuaho52901742015-04-15 13:42:45 +03004352
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004353 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03004354 {
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004355 binaryOpError(loc, ":", trueExpression->getCompleteString(),
4356 falseExpression->getCompleteString());
4357 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03004358 }
Olli Etuahode318b22016-10-25 16:18:25 +01004359 if (IsOpaqueType(trueExpression->getBasicType()))
4360 {
4361 // ESSL 1.00 section 4.1.7
4362 // ESSL 3.00 section 4.1.7
4363 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
4364 // Note that structs containing opaque types don't need to be checked as structs are
4365 // forbidden below.
4366 error(loc, "ternary operator is not allowed for opaque types", ":");
4367 return falseExpression;
4368 }
4369
Olli Etuahoa2d53032015-04-15 14:14:44 +03004370 // ESSL1 sections 5.2 and 5.7:
4371 // ESSL3 section 5.7:
4372 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004373 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03004374 {
4375 error(loc, "ternary operator is not allowed for structures or arrays", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004376 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03004377 }
Corentin Wallez0d959252016-07-12 17:26:32 -04004378 // WebGL2 section 5.26, the following results in an error:
4379 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004380 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04004381 {
4382 error(loc, "ternary operator is not allowed for void", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004383 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04004384 }
4385
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004386 return TIntermediate::AddTernarySelection(cond, trueExpression, falseExpression, loc);
Olli Etuaho52901742015-04-15 13:42:45 +03004387}
Olli Etuaho49300862015-02-20 14:54:49 +02004388
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004389//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004390// Parse an array of strings using yyparse.
4391//
4392// Returns 0 for success.
4393//
Jamie Madillb98c3a82015-07-23 14:26:04 -04004394int PaParseStrings(size_t count,
4395 const char *const string[],
4396 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05304397 TParseContext *context)
4398{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004399 if ((count == 0) || (string == NULL))
4400 return 1;
4401
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004402 if (glslang_initialize(context))
4403 return 1;
4404
alokp@chromium.org408c45e2012-04-05 15:54:43 +00004405 int error = glslang_scan(count, string, length, context);
4406 if (!error)
4407 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004408
alokp@chromium.org73bc2982012-06-19 18:48:05 +00004409 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00004410
alokp@chromium.org6b495712012-06-29 00:06:58 +00004411 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004412}