blob: b0d291e64b92fd44d0cddbbdee08dac9fc883102 [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 {
1487 type.setArraySize(initializer->getArraySize());
1488 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001489 if (!declareVariable(line, identifier, type, &variable))
1490 {
1491 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001492 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001493
Olli Etuahob0c645e2015-05-12 14:25:36 +03001494 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001495 if (symbolTable.atGlobalLevel() &&
1496 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001497 {
1498 // Error message does not completely match behavior with ESSL 1.00, but
1499 // we want to steer developers towards only using constant expressions.
1500 error(line, "global variable initializers must be constant expressions", "=");
1501 return true;
1502 }
1503 if (globalInitWarning)
1504 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001505 warning(
1506 line,
1507 "global variable initializers should be constant expressions "
1508 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1509 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001510 }
1511
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001512 //
1513 // identifier must be of type constant, a global, or a temporary
1514 //
1515 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301516 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1517 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001518 error(line, " cannot initialize this type of qualifier ",
1519 variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001520 return true;
1521 }
1522 //
1523 // test for and propagate constant
1524 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001525
Arun Patole7e7e68d2015-05-22 12:02:25 +05301526 if (qualifier == EvqConst)
1527 {
1528 if (qualifier != initializer->getType().getQualifier())
1529 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001530 std::stringstream extraInfoStream;
1531 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1532 std::string extraInfo = extraInfoStream.str();
1533 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001534 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001535 return true;
1536 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301537 if (type != initializer->getType())
1538 {
1539 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001540 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001541 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001542 return true;
1543 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001544
1545 // Save the constant folded value to the variable if possible. For example array
1546 // initializers are not folded, since that way copying the array literal to multiple places
1547 // in the shader is avoided.
1548 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1549 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301550 if (initializer->getAsConstantUnion())
1551 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001552 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho13389b62016-10-16 11:48:18 +01001553 *initNode = nullptr;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001554 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301555 }
1556 else if (initializer->getAsSymbolNode())
1557 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001558 const TSymbol *symbol =
1559 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1560 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001561
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001562 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001563 if (constArray)
1564 {
1565 variable->shareConstPointer(constArray);
Olli Etuaho13389b62016-10-16 11:48:18 +01001566 *initNode = nullptr;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001567 return false;
1568 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001569 }
1570 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001571
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001572 TIntermSymbol *intermSymbol = intermediate.addSymbol(
1573 variable->getUniqueId(), variable->getName(), variable->getType(), line);
Olli Etuaho13389b62016-10-16 11:48:18 +01001574 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1575 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001576 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001577 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1578 return true;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001579 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001580
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001581 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001582}
1583
Olli Etuaho0e3aee32016-10-27 12:56:38 +01001584void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
1585{
1586 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
1587 typeSpecifier->getBasicType());
1588
1589 if (mShaderVersion < 300 && typeSpecifier->array)
1590 {
1591 error(typeSpecifier->getLine(), "not supported", "first-class array");
1592 typeSpecifier->clearArrayness();
1593 }
1594}
1595
Martin Radev70866b82016-07-22 15:27:42 +03001596TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301597 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001598{
Olli Etuaho613b9592016-09-05 12:05:53 +03001599 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001600
Martin Radev70866b82016-07-22 15:27:42 +03001601 TPublicType returnType = typeSpecifier;
1602 returnType.qualifier = typeQualifier.qualifier;
1603 returnType.invariant = typeQualifier.invariant;
1604 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03001605 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03001606 returnType.precision = typeSpecifier.precision;
1607
1608 if (typeQualifier.precision != EbpUndefined)
1609 {
1610 returnType.precision = typeQualifier.precision;
1611 }
1612
Martin Radev4a9cd802016-09-01 16:51:51 +03001613 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
1614 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03001615
Martin Radev4a9cd802016-09-01 16:51:51 +03001616 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
1617 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03001618
Martin Radev4a9cd802016-09-01 16:51:51 +03001619 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03001620
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001621 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001622 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001623 if (typeSpecifier.array)
1624 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001625 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001626 returnType.clearArrayness();
1627 }
1628
Martin Radev70866b82016-07-22 15:27:42 +03001629 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001630 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001631 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001632 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001633 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001634 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001635
Martin Radev70866b82016-07-22 15:27:42 +03001636 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001637 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001638 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001639 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001640 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001641 }
1642 }
1643 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001644 {
Martin Radev70866b82016-07-22 15:27:42 +03001645 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03001646 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001647 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03001648 }
Martin Radev70866b82016-07-22 15:27:42 +03001649 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
1650 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001651 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001652 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
1653 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001654 }
Martin Radev70866b82016-07-22 15:27:42 +03001655 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03001656 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001657 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03001658 "in");
Martin Radev802abe02016-08-04 17:48:32 +03001659 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001660 }
1661
1662 return returnType;
1663}
1664
Olli Etuaho856c4972016-08-08 11:38:39 +03001665void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
1666 const TPublicType &type,
1667 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03001668{
1669 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03001670 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03001671 {
1672 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001673 }
1674
1675 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
1676 switch (qualifier)
1677 {
1678 case EvqVertexIn:
1679 // ESSL 3.00 section 4.3.4
1680 if (type.array)
1681 {
1682 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001683 }
1684 // Vertex inputs with a struct type are disallowed in singleDeclarationErrorCheck
1685 return;
1686 case EvqFragmentOut:
1687 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03001688 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03001689 {
1690 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001691 }
1692 // Fragment outputs with a struct type are disallowed in singleDeclarationErrorCheck
1693 return;
1694 default:
1695 break;
1696 }
1697
1698 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
1699 // restrictions.
1700 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03001701 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
1702 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03001703 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
1704 {
1705 error(qualifierLocation, "must use 'flat' interpolation here",
1706 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001707 }
1708
Martin Radev4a9cd802016-09-01 16:51:51 +03001709 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03001710 {
1711 // ESSL 3.00 sections 4.3.4 and 4.3.6.
1712 // These restrictions are only implied by the ESSL 3.00 spec, but
1713 // the ESSL 3.10 spec lists these restrictions explicitly.
1714 if (type.array)
1715 {
1716 error(qualifierLocation, "cannot be an array of structures",
1717 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001718 }
1719 if (type.isStructureContainingArrays())
1720 {
1721 error(qualifierLocation, "cannot be a structure containing an array",
1722 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001723 }
1724 if (type.isStructureContainingType(EbtStruct))
1725 {
1726 error(qualifierLocation, "cannot be a structure containing a structure",
1727 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001728 }
1729 if (type.isStructureContainingType(EbtBool))
1730 {
1731 error(qualifierLocation, "cannot be a structure containing a bool",
1732 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001733 }
1734 }
1735}
1736
Martin Radev2cc85b32016-08-05 16:22:53 +03001737void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
1738{
1739 if (qualifier.getType() == QtStorage)
1740 {
1741 const TStorageQualifierWrapper &storageQualifier =
1742 static_cast<const TStorageQualifierWrapper &>(qualifier);
1743 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
1744 !symbolTable.atGlobalLevel())
1745 {
1746 error(storageQualifier.getLine(),
1747 "Local variables can only use the const storage qualifier.",
1748 storageQualifier.getQualifierString().c_str());
1749 }
1750 }
1751}
1752
1753bool TParseContext::checkIsMemoryQualifierNotSpecified(const TMemoryQualifier &memoryQualifier,
1754 const TSourceLoc &location)
1755{
1756 if (memoryQualifier.readonly)
1757 {
1758 error(location, "Only allowed with images.", "readonly");
1759 return false;
1760 }
1761 if (memoryQualifier.writeonly)
1762 {
1763 error(location, "Only allowed with images.", "writeonly");
1764 return false;
1765 }
1766 return true;
1767}
1768
Olli Etuaho13389b62016-10-16 11:48:18 +01001769TIntermDeclaration *TParseContext::parseSingleDeclaration(
1770 TPublicType &publicType,
1771 const TSourceLoc &identifierOrTypeLocation,
1772 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001773{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001774 TType type(publicType);
1775 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
1776 mDirectiveHandler.pragma().stdgl.invariantAll)
1777 {
1778 TQualifier qualifier = type.getQualifier();
1779
1780 // The directive handler has already taken care of rejecting invalid uses of this pragma
1781 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
1782 // affected variable declarations:
1783 //
1784 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
1785 // elsewhere, in TranslatorGLSL.)
1786 //
1787 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
1788 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
1789 // the way this is currently implemented we have to enable this compiler option before
1790 // parsing the shader and determining the shading language version it uses. If this were
1791 // implemented as a post-pass, the workaround could be more targeted.
1792 //
1793 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
1794 // the specification, but there are desktop OpenGL drivers that expect that this is the
1795 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
1796 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
1797 {
1798 type.setInvariant(true);
1799 }
1800 }
1801
1802 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, type, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001803
Olli Etuahobab4c082015-04-24 16:38:49 +03001804 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03001805
Olli Etuahobab4c082015-04-24 16:38:49 +03001806 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1807
Olli Etuaho13389b62016-10-16 11:48:18 +01001808 TIntermDeclaration *declaration = new TIntermDeclaration();
1809 declaration->setLine(identifierOrTypeLocation);
1810
Olli Etuahobab4c082015-04-24 16:38:49 +03001811 if (emptyDeclaration)
1812 {
1813 if (publicType.isUnsizedArray())
1814 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001815 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1816 // error. It is assumed that this applies to empty declarations as well.
1817 error(identifierOrTypeLocation, "empty array declaration needs to specify a size",
1818 identifier.c_str());
Olli Etuahobab4c082015-04-24 16:38:49 +03001819 }
1820 }
1821 else
Jamie Madill60ed9812013-06-06 11:56:46 -04001822 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001823 singleDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001824
Olli Etuaho856c4972016-08-08 11:38:39 +03001825 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001826
Olli Etuaho2935c582015-04-08 14:32:06 +03001827 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001828 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04001829
1830 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01001831 {
Jamie Madill60ed9812013-06-06 11:56:46 -04001832 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01001833 }
Jamie Madill60ed9812013-06-06 11:56:46 -04001834 }
1835
Olli Etuaho13389b62016-10-16 11:48:18 +01001836 // We append the symbol even if the declaration is empty, mainly because of struct declarations
1837 // that may just declare a type.
1838 declaration->appendDeclarator(symbol);
1839
1840 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04001841}
1842
Olli Etuaho13389b62016-10-16 11:48:18 +01001843TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1844 const TSourceLoc &identifierLocation,
1845 const TString &identifier,
1846 const TSourceLoc &indexLocation,
1847 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001848{
Olli Etuahofa33d582015-04-09 14:33:12 +03001849 mDeferredSingleDeclarationErrorCheck = false;
1850
Olli Etuaho383b7912016-08-05 11:22:59 +03001851 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001852
Olli Etuaho856c4972016-08-08 11:38:39 +03001853 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001854
Olli Etuaho8a176262016-08-16 14:23:01 +03001855 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001856
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001857 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001858
Olli Etuaho856c4972016-08-08 11:38:39 +03001859 unsigned int size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001860 // Make the type an array even if size check failed.
1861 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1862 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001863
Olli Etuaho2935c582015-04-08 14:32:06 +03001864 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03001865 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04001866
Olli Etuaho13389b62016-10-16 11:48:18 +01001867 TIntermDeclaration *declaration = new TIntermDeclaration();
1868 declaration->setLine(identifierLocation);
1869
Olli Etuahoe7847b02015-03-16 11:56:12 +02001870 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001871 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01001872 {
Jamie Madill60ed9812013-06-06 11:56:46 -04001873 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01001874 declaration->appendDeclarator(symbol);
1875 }
Jamie Madill60ed9812013-06-06 11:56:46 -04001876
Olli Etuaho13389b62016-10-16 11:48:18 +01001877 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04001878}
1879
Olli Etuaho13389b62016-10-16 11:48:18 +01001880TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
1881 const TSourceLoc &identifierLocation,
1882 const TString &identifier,
1883 const TSourceLoc &initLocation,
1884 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001885{
Olli Etuahofa33d582015-04-09 14:33:12 +03001886 mDeferredSingleDeclarationErrorCheck = false;
1887
Olli Etuaho383b7912016-08-05 11:22:59 +03001888 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001889
Olli Etuaho13389b62016-10-16 11:48:18 +01001890 TIntermDeclaration *declaration = new TIntermDeclaration();
1891 declaration->setLine(identifierLocation);
1892
1893 TIntermBinary *initNode = nullptr;
1894 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04001895 {
Olli Etuaho13389b62016-10-16 11:48:18 +01001896 if (initNode)
1897 {
1898 declaration->appendDeclarator(initNode);
1899 }
Jamie Madill60ed9812013-06-06 11:56:46 -04001900 }
Olli Etuaho13389b62016-10-16 11:48:18 +01001901 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04001902}
1903
Olli Etuaho13389b62016-10-16 11:48:18 +01001904TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Jamie Madillb98c3a82015-07-23 14:26:04 -04001905 TPublicType &publicType,
1906 const TSourceLoc &identifierLocation,
1907 const TString &identifier,
1908 const TSourceLoc &indexLocation,
1909 TIntermTyped *indexExpression,
1910 const TSourceLoc &initLocation,
1911 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001912{
1913 mDeferredSingleDeclarationErrorCheck = false;
1914
Olli Etuaho383b7912016-08-05 11:22:59 +03001915 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001916
Olli Etuaho8a176262016-08-16 14:23:01 +03001917 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001918
1919 TPublicType arrayType(publicType);
1920
Olli Etuaho856c4972016-08-08 11:38:39 +03001921 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001922 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
1923 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03001924 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001925 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001926 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001927 }
1928 // Make the type an array even if size check failed.
1929 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1930 arrayType.setArraySize(size);
1931
Olli Etuaho13389b62016-10-16 11:48:18 +01001932 TIntermDeclaration *declaration = new TIntermDeclaration();
1933 declaration->setLine(identifierLocation);
1934
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001935 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01001936 TIntermBinary *initNode = nullptr;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001937 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1938 {
Olli Etuaho13389b62016-10-16 11:48:18 +01001939 if (initNode)
1940 {
1941 declaration->appendDeclarator(initNode);
1942 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001943 }
Olli Etuaho13389b62016-10-16 11:48:18 +01001944
1945 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001946}
1947
Martin Radev70866b82016-07-22 15:27:42 +03001948TIntermAggregate *TParseContext::parseInvariantDeclaration(
1949 const TTypeQualifierBuilder &typeQualifierBuilder,
1950 const TSourceLoc &identifierLoc,
1951 const TString *identifier,
1952 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04001953{
Olli Etuaho613b9592016-09-05 12:05:53 +03001954 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04001955
Martin Radev70866b82016-07-22 15:27:42 +03001956 if (!typeQualifier.invariant)
1957 {
1958 error(identifierLoc, "Expected invariant", identifier->c_str());
1959 return nullptr;
1960 }
1961 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
1962 {
1963 return nullptr;
1964 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04001965 if (!symbol)
1966 {
1967 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001968 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001969 }
Martin Radev70866b82016-07-22 15:27:42 +03001970 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04001971 {
Martin Radev70866b82016-07-22 15:27:42 +03001972 error(identifierLoc, "invariant declaration specifies qualifier",
1973 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04001974 }
Martin Radev70866b82016-07-22 15:27:42 +03001975 if (typeQualifier.precision != EbpUndefined)
1976 {
1977 error(identifierLoc, "invariant declaration specifies precision",
1978 getPrecisionString(typeQualifier.precision));
1979 }
1980 if (!typeQualifier.layoutQualifier.isEmpty())
1981 {
1982 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
1983 }
1984
1985 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1986 ASSERT(variable);
1987 const TType &type = variable->getType();
1988
1989 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
1990 typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001991 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03001992
1993 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
1994
1995 TIntermSymbol *intermSymbol =
1996 intermediate.addSymbol(variable->getUniqueId(), *identifier, type, identifierLoc);
1997
Olli Etuaho32db19b2016-10-04 14:43:16 +01001998 TIntermAggregate *aggregate = TIntermediate::MakeAggregate(intermSymbol, identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03001999 aggregate->setOp(EOpInvariantDeclaration);
2000 return aggregate;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002001}
2002
Olli Etuaho13389b62016-10-16 11:48:18 +01002003void TParseContext::parseDeclarator(TPublicType &publicType,
2004 const TSourceLoc &identifierLocation,
2005 const TString &identifier,
2006 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002007{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002008 // If the declaration starting this declarator list was empty (example: int,), some checks were
2009 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002010 if (mDeferredSingleDeclarationErrorCheck)
2011 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002012 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002013 mDeferredSingleDeclarationErrorCheck = false;
2014 }
2015
Olli Etuaho856c4972016-08-08 11:38:39 +03002016 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002017
Olli Etuaho856c4972016-08-08 11:38:39 +03002018 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002019
Olli Etuaho2935c582015-04-08 14:32:06 +03002020 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002021 declareVariable(identifierLocation, identifier, TType(publicType), &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002022
Jamie Madillb98c3a82015-07-23 14:26:04 -04002023 TIntermSymbol *symbol =
2024 intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002025 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01002026 {
Jamie Madill502d66f2013-06-20 11:55:52 -04002027 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01002028 declarationOut->appendDeclarator(symbol);
2029 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002030}
2031
Olli Etuaho13389b62016-10-16 11:48:18 +01002032void TParseContext::parseArrayDeclarator(TPublicType &publicType,
2033 const TSourceLoc &identifierLocation,
2034 const TString &identifier,
2035 const TSourceLoc &arrayLocation,
2036 TIntermTyped *indexExpression,
2037 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002038{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002039 // If the declaration starting this declarator list was empty (example: int,), some checks were
2040 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002041 if (mDeferredSingleDeclarationErrorCheck)
2042 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002043 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002044 mDeferredSingleDeclarationErrorCheck = false;
2045 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002046
Olli Etuaho856c4972016-08-08 11:38:39 +03002047 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002048
Olli Etuaho856c4972016-08-08 11:38:39 +03002049 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002050
Olli Etuaho8a176262016-08-16 14:23:01 +03002051 if (checkIsValidTypeAndQualifierForArray(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002052 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02002053 TType arrayType = TType(publicType);
Olli Etuaho856c4972016-08-08 11:38:39 +03002054 unsigned int size = checkIsValidArraySize(arrayLocation, indexExpression);
Olli Etuaho693c9aa2015-04-07 17:50:36 +03002055 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002056
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002057 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002058 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002059
Jamie Madillb98c3a82015-07-23 14:26:04 -04002060 TIntermSymbol *symbol =
2061 intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002062 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002063 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002064
Olli Etuaho13389b62016-10-16 11:48:18 +01002065 declarationOut->appendDeclarator(symbol);
Jamie Madill502d66f2013-06-20 11:55:52 -04002066 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002067}
2068
Olli Etuaho13389b62016-10-16 11:48:18 +01002069void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2070 const TSourceLoc &identifierLocation,
2071 const TString &identifier,
2072 const TSourceLoc &initLocation,
2073 TIntermTyped *initializer,
2074 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002075{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002076 // If the declaration starting this declarator list was empty (example: int,), some checks were
2077 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002078 if (mDeferredSingleDeclarationErrorCheck)
2079 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002080 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002081 mDeferredSingleDeclarationErrorCheck = false;
2082 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002083
Olli Etuaho856c4972016-08-08 11:38:39 +03002084 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002085
Olli Etuaho13389b62016-10-16 11:48:18 +01002086 TIntermBinary *initNode = nullptr;
2087 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002088 {
2089 //
2090 // build the intermediate representation
2091 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002092 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002093 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002094 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002095 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002096 }
2097}
2098
Olli Etuaho13389b62016-10-16 11:48:18 +01002099void TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
2100 const TSourceLoc &identifierLocation,
2101 const TString &identifier,
2102 const TSourceLoc &indexLocation,
2103 TIntermTyped *indexExpression,
2104 const TSourceLoc &initLocation,
2105 TIntermTyped *initializer,
2106 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002107{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002108 // If the declaration starting this declarator list was empty (example: int,), some checks were
2109 // not performed.
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002110 if (mDeferredSingleDeclarationErrorCheck)
2111 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002112 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002113 mDeferredSingleDeclarationErrorCheck = false;
2114 }
2115
Olli Etuaho856c4972016-08-08 11:38:39 +03002116 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002117
Olli Etuaho8a176262016-08-16 14:23:01 +03002118 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002119
2120 TPublicType arrayType(publicType);
2121
Olli Etuaho856c4972016-08-08 11:38:39 +03002122 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002123 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2124 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002125 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002126 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002127 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002128 }
2129 // Make the type an array even if size check failed.
2130 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2131 arrayType.setArraySize(size);
2132
2133 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002134 TIntermBinary *initNode = nullptr;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002135 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
2136 {
2137 if (initNode)
2138 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002139 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002140 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002141 }
2142}
2143
Martin Radev70866b82016-07-22 15:27:42 +03002144void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002145{
Olli Etuaho613b9592016-09-05 12:05:53 +03002146 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002147 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002148
Martin Radev70866b82016-07-22 15:27:42 +03002149 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2150 typeQualifier.line);
2151
Jamie Madillc2128ff2016-07-04 10:26:17 -04002152 // It should never be the case, but some strange parser errors can send us here.
2153 if (layoutQualifier.isEmpty())
2154 {
2155 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002156 return;
2157 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002158
Martin Radev802abe02016-08-04 17:48:32 +03002159 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002160 {
Martin Radev802abe02016-08-04 17:48:32 +03002161 error(typeQualifier.line, "invalid combination:", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002162 return;
2163 }
2164
Martin Radev2cc85b32016-08-05 16:22:53 +03002165 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
2166
2167 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2168
Martin Radev802abe02016-08-04 17:48:32 +03002169 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002170 {
Martin Radev802abe02016-08-04 17:48:32 +03002171 if (mComputeShaderLocalSizeDeclared &&
2172 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2173 {
2174 error(typeQualifier.line, "Work group size does not match the previous declaration",
2175 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002176 return;
2177 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002178
Martin Radev802abe02016-08-04 17:48:32 +03002179 if (mShaderVersion < 310)
2180 {
2181 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002182 return;
2183 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002184
Martin Radev4c4c8e72016-08-04 12:25:34 +03002185 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03002186 {
2187 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002188 return;
2189 }
2190
2191 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
2192 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
2193
2194 const TConstantUnion *maxComputeWorkGroupSizeData =
2195 maxComputeWorkGroupSize->getConstPointer();
2196
2197 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
2198 {
2199 if (layoutQualifier.localSize[i] != -1)
2200 {
2201 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
2202 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
2203 if (mComputeShaderLocalSize[i] < 1 ||
2204 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
2205 {
2206 std::stringstream errorMessageStream;
2207 errorMessageStream << "Value must be at least 1 and no greater than "
2208 << maxComputeWorkGroupSizeValue;
2209 const std::string &errorMessage = errorMessageStream.str();
2210
Martin Radev4c4c8e72016-08-04 12:25:34 +03002211 error(typeQualifier.line, "invalid value:", getWorkGroupSizeString(i),
Martin Radev802abe02016-08-04 17:48:32 +03002212 errorMessage.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03002213 return;
2214 }
2215 }
2216 }
2217
2218 mComputeShaderLocalSizeDeclared = true;
2219 }
2220 else
Jamie Madill1566ef72013-06-20 11:55:54 -04002221 {
Martin Radev802abe02016-08-04 17:48:32 +03002222
Olli Etuaho8a176262016-08-16 14:23:01 +03002223 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03002224 {
Martin Radev802abe02016-08-04 17:48:32 +03002225 return;
2226 }
2227
2228 if (typeQualifier.qualifier != EvqUniform)
2229 {
2230 error(typeQualifier.line, "invalid qualifier:",
2231 getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
Martin Radev802abe02016-08-04 17:48:32 +03002232 return;
2233 }
2234
2235 if (mShaderVersion < 300)
2236 {
2237 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
2238 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002239 return;
2240 }
2241
Olli Etuaho856c4972016-08-08 11:38:39 +03002242 checkLocationIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002243
2244 if (layoutQualifier.matrixPacking != EmpUnspecified)
2245 {
2246 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
2247 }
2248
2249 if (layoutQualifier.blockStorage != EbsUnspecified)
2250 {
2251 mDefaultBlockStorage = layoutQualifier.blockStorage;
2252 }
Jamie Madill1566ef72013-06-20 11:55:54 -04002253 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002254}
2255
Olli Etuaho476197f2016-10-11 13:59:08 +01002256TIntermAggregate *TParseContext::addFunctionPrototypeDeclaration(const TFunction &parsedFunction,
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002257 const TSourceLoc &location)
2258{
Olli Etuaho476197f2016-10-11 13:59:08 +01002259 // Note: function found from the symbol table could be the same as parsedFunction if this is the
2260 // first declaration. Either way the instance in the symbol table is used to track whether the
2261 // function is declared multiple times.
2262 TFunction *function = static_cast<TFunction *>(
2263 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
2264 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02002265 {
2266 // ESSL 1.00.17 section 4.2.7.
2267 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
2268 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02002269 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002270 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02002271
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002272 TIntermAggregate *prototype = new TIntermAggregate;
Olli Etuaho476197f2016-10-11 13:59:08 +01002273 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
2274 // point to the data that already exists in the symbol table.
2275 prototype->setType(function->getReturnType());
Olli Etuahobd674552016-10-06 13:28:42 +01002276 prototype->getFunctionSymbolInfo()->setFromFunction(*function);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002277
Olli Etuaho476197f2016-10-11 13:59:08 +01002278 for (size_t i = 0; i < function->getParamCount(); i++)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002279 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002280 const TConstParameter &param = function->getParam(i);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002281 if (param.name != 0)
2282 {
2283 TVariable variable(param.name, *param.type);
2284
2285 TIntermSymbol *paramSymbol = intermediate.addSymbol(
2286 variable.getUniqueId(), variable.getName(), variable.getType(), location);
2287 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
2288 }
2289 else
2290 {
2291 TIntermSymbol *paramSymbol = intermediate.addSymbol(0, "", *param.type, location);
2292 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
2293 }
2294 }
2295
2296 prototype->setOp(EOpPrototype);
2297
2298 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002299
2300 if (!symbolTable.atGlobalLevel())
2301 {
2302 // ESSL 3.00.4 section 4.2.4.
2303 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002304 }
2305
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002306 return prototype;
2307}
2308
Olli Etuaho336b1472016-10-05 16:37:55 +01002309TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
2310 const TFunction &function,
2311 TIntermAggregate *functionParameters,
2312 TIntermBlock *functionBody,
2313 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002314{
Olli Etuahof51fdd22016-10-03 10:03:40 +01002315 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002316 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
2317 {
2318 error(location, "function does not return a value:", "", function.getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002319 }
2320
Olli Etuahof51fdd22016-10-03 10:03:40 +01002321 if (functionBody == nullptr)
2322 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01002323 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002324 functionBody->setLine(location);
2325 }
Olli Etuaho336b1472016-10-05 16:37:55 +01002326 TIntermFunctionDefinition *functionNode =
2327 new TIntermFunctionDefinition(function.getReturnType(), functionParameters, functionBody);
2328 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01002329
Olli Etuahobd674552016-10-06 13:28:42 +01002330 functionNode->getFunctionSymbolInfo()->setFromFunction(function);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002331
2332 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002333 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002334}
2335
Olli Etuaho476197f2016-10-11 13:59:08 +01002336void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
2337 TFunction **function,
2338 TIntermAggregate **aggregateOut)
Jamie Madill185fb402015-06-12 15:48:48 -04002339{
Olli Etuaho476197f2016-10-11 13:59:08 +01002340 ASSERT(function);
2341 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002342 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01002343 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002344
2345 if (builtIn)
2346 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002347 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002348 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002349 else
Jamie Madill185fb402015-06-12 15:48:48 -04002350 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002351 TFunction *prevDec = static_cast<TFunction *>(
2352 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
2353
2354 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
2355 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
2356 // occurance.
2357 if (*function != prevDec)
2358 {
2359 // Swap the parameters of the previous declaration to the parameters of the function
2360 // definition (parameter names may differ).
2361 prevDec->swapParameters(**function);
2362
2363 // The function definition will share the same symbol as any previous declaration.
2364 *function = prevDec;
2365 }
2366
2367 if ((*function)->isDefined())
2368 {
2369 error(location, "function already has a body", (*function)->getName().c_str());
2370 }
2371
2372 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04002373 }
Jamie Madill185fb402015-06-12 15:48:48 -04002374
2375 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuaho476197f2016-10-11 13:59:08 +01002376 if ((*function)->getName() == "main")
Jamie Madill185fb402015-06-12 15:48:48 -04002377 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002378 if ((*function)->getParamCount() > 0)
Jamie Madill185fb402015-06-12 15:48:48 -04002379 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002380 error(location, "function cannot take any parameter(s)",
2381 (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002382 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002383 if ((*function)->getReturnType().getBasicType() != EbtVoid)
Jamie Madill185fb402015-06-12 15:48:48 -04002384 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002385 error(location, "", (*function)->getReturnType().getBasicString(),
Jamie Madillb98c3a82015-07-23 14:26:04 -04002386 "main function cannot return a value");
Jamie Madill185fb402015-06-12 15:48:48 -04002387 }
2388 }
2389
2390 //
2391 // Remember the return type for later checking for RETURN statements.
2392 //
Olli Etuaho476197f2016-10-11 13:59:08 +01002393 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002394 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04002395
2396 //
2397 // Insert parameters into the symbol table.
2398 // If the parameter has no name, it's not an error, just don't insert it
2399 // (could be used for unused args).
2400 //
2401 // Also, accumulate the list of parameters into the HIL, so lower level code
2402 // knows where to find parameters.
2403 //
2404 TIntermAggregate *paramNodes = new TIntermAggregate;
Olli Etuaho476197f2016-10-11 13:59:08 +01002405 for (size_t i = 0; i < (*function)->getParamCount(); i++)
Jamie Madill185fb402015-06-12 15:48:48 -04002406 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002407 const TConstParameter &param = (*function)->getParam(i);
Jamie Madill185fb402015-06-12 15:48:48 -04002408 if (param.name != 0)
2409 {
2410 TVariable *variable = new TVariable(param.name, *param.type);
2411 //
2412 // Insert the parameters with name in the symbol table.
2413 //
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002414 if (!symbolTable.declare(variable))
2415 {
Jamie Madill185fb402015-06-12 15:48:48 -04002416 error(location, "redefinition", variable->getName().c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002417 paramNodes = intermediate.growAggregate(
2418 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
2419 continue;
Jamie Madill185fb402015-06-12 15:48:48 -04002420 }
2421
2422 //
2423 // Add the parameter to the HIL
2424 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002425 TIntermSymbol *symbol = intermediate.addSymbol(
2426 variable->getUniqueId(), variable->getName(), variable->getType(), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002427
2428 paramNodes = intermediate.growAggregate(paramNodes, symbol, location);
2429 }
2430 else
2431 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002432 paramNodes = intermediate.growAggregate(
2433 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002434 }
2435 }
2436 intermediate.setAggregateOperator(paramNodes, EOpParameters, location);
2437 *aggregateOut = paramNodes;
2438 setLoopNestingLevel(0);
2439}
2440
Jamie Madillb98c3a82015-07-23 14:26:04 -04002441TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002442{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002443 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002444 // We don't know at this point whether this is a function definition or a prototype.
2445 // The definition production code will check for redefinitions.
2446 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002447 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002448 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
2449 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002450 //
2451 TFunction *prevDec =
2452 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302453
2454 if (getShaderVersion() >= 300 && symbolTable.hasUnmangledBuiltIn(function->getName().c_str()))
2455 {
2456 // With ESSL 3.00, names of built-in functions cannot be redeclared as functions.
2457 // Therefore overloading or redefining builtin functions is an error.
2458 error(location, "Name of a built-in function cannot be redeclared as function",
2459 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302460 }
2461 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002462 {
2463 if (prevDec->getReturnType() != function->getReturnType())
2464 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002465 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002466 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04002467 }
2468 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2469 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002470 if (prevDec->getParam(i).type->getQualifier() !=
2471 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002472 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002473 error(location,
2474 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002475 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04002476 }
2477 }
2478 }
2479
2480 //
2481 // Check for previously declared variables using the same name.
2482 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002483 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002484 if (prevSym)
2485 {
2486 if (!prevSym->isFunction())
2487 {
2488 error(location, "redefinition", function->getName().c_str(), "function");
Jamie Madill185fb402015-06-12 15:48:48 -04002489 }
2490 }
2491 else
2492 {
2493 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01002494 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04002495 }
2496
2497 // We're at the inner scope level of the function's arguments and body statement.
2498 // Add the function prototype to the surrounding scope instead.
2499 symbolTable.getOuterLevel()->insert(function);
2500
2501 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002502 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2503 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002504 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2505 //
2506 return function;
2507}
2508
Olli Etuaho9de84a52016-06-14 17:36:01 +03002509TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
2510 const TString *name,
2511 const TSourceLoc &location)
2512{
2513 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
2514 {
2515 error(location, "no qualifiers allowed for function return",
2516 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03002517 }
2518 if (!type.layoutQualifier.isEmpty())
2519 {
2520 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03002521 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002522 // make sure a sampler or an image is not involved as well...
Martin Radev4a9cd802016-09-01 16:51:51 +03002523 checkIsNotSampler(location, type.typeSpecifierNonArray,
2524 "samplers can't be function return values");
Martin Radev2cc85b32016-08-05 16:22:53 +03002525 checkIsNotImage(location, type.typeSpecifierNonArray, "images can't be function return values");
Olli Etuahoe29324f2016-06-15 10:58:03 +03002526 if (mShaderVersion < 300)
2527 {
2528 // Array return values are forbidden, but there's also no valid syntax for declaring array
2529 // return values in ESSL 1.00.
2530 ASSERT(type.arraySize == 0 || mDiagnostics.numErrors() > 0);
2531
2532 if (type.isStructureContainingArrays())
2533 {
2534 // ESSL 1.00.17 section 6.1 Function Definitions
2535 error(location, "structures containing arrays can't be function return values",
2536 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03002537 }
2538 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03002539
2540 // Add the function as a prototype after parsing it (we do not support recursion)
2541 return new TFunction(name, new TType(type));
2542}
2543
Jamie Madill06145232015-05-13 13:10:01 -04002544TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002545{
Jamie Madill06145232015-05-13 13:10:01 -04002546 TPublicType publicType = publicTypeIn;
Martin Radev4a9cd802016-09-01 16:51:51 +03002547 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02002548 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002549 error(publicType.getLine(), "constructor can't be a structure definition",
2550 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02002551 }
2552
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002553 TOperator op = EOpNull;
Martin Radev4a9cd802016-09-01 16:51:51 +03002554 if (publicType.getUserDef())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002555 {
2556 op = EOpConstructStruct;
2557 }
2558 else
2559 {
Geoff Lang156d7192016-07-21 16:11:00 -04002560 op = sh::TypeToConstructorOperator(TType(publicType));
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002561 if (op == EOpNull)
2562 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002563 error(publicType.getLine(), "cannot construct this type",
2564 getBasicString(publicType.getBasicType()));
2565 publicType.setBasicType(EbtFloat);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002566 op = EOpConstructFloat;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002567 }
2568 }
2569
2570 TString tempString;
Dmitry Skiba7f17a502015-06-22 15:08:39 -07002571 const TType *type = new TType(publicType);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002572 return new TFunction(&tempString, type, op);
2573}
2574
Jamie Madillb98c3a82015-07-23 14:26:04 -04002575// This function is used to test for the correctness of the parameters passed to various constructor
2576// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002577//
Olli Etuaho856c4972016-08-08 11:38:39 +03002578// 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 +00002579//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002580TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002581 TOperator op,
2582 TFunction *fnCall,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302583 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002584{
Olli Etuaho856c4972016-08-08 11:38:39 +03002585 TType type = fnCall->getReturnType();
2586 if (type.isUnsizedArray())
2587 {
2588 type.setArraySize(static_cast<unsigned int>(fnCall->getParamCount()));
2589 }
2590 bool constType = true;
2591 for (size_t i = 0; i < fnCall->getParamCount(); ++i)
2592 {
2593 const TConstParameter &param = fnCall->getParam(i);
2594 if (param.type->getQualifier() != EvqConst)
2595 constType = false;
2596 }
2597 if (constType)
2598 type.setQualifier(EvqConst);
2599
Olli Etuaho8a176262016-08-16 14:23:01 +03002600 if (!checkConstructorArguments(line, arguments, *fnCall, op, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03002601 {
2602 TIntermTyped *dummyNode = intermediate.setAggregateOperator(nullptr, op, line);
2603 dummyNode->setType(type);
2604 return dummyNode;
2605 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002606 TIntermAggregate *constructor = arguments->getAsAggregate();
2607 ASSERT(constructor != nullptr);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002608
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002609 // Turn the argument list itself into a constructor
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002610 constructor->setOp(op);
2611 constructor->setLine(line);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002612 ASSERT(constructor->isConstructor());
2613
2614 // Need to set type before setPrecisionFromChildren() because bool doesn't have precision.
Olli Etuaho856c4972016-08-08 11:38:39 +03002615 constructor->setType(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002616
Olli Etuaho21203702014-11-13 16:16:21 +02002617 // Structs should not be precision qualified, the individual members may be.
2618 // Built-in types on the other hand should be precision qualified.
2619 if (op != EOpConstructStruct)
2620 {
2621 constructor->setPrecisionFromChildren();
Olli Etuaho856c4972016-08-08 11:38:39 +03002622 type.setPrecision(constructor->getPrecision());
Olli Etuaho21203702014-11-13 16:16:21 +02002623 }
2624
Olli Etuaho856c4972016-08-08 11:38:39 +03002625 constructor->setType(type);
2626
Olli Etuahof119a262016-08-19 15:54:22 +03002627 TIntermTyped *constConstructor = intermediate.foldAggregateBuiltIn(constructor, &mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002628 if (constConstructor)
2629 {
2630 return constConstructor;
2631 }
2632
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002633 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002634}
2635
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002636//
2637// Interface/uniform blocks
2638//
Olli Etuaho13389b62016-10-16 11:48:18 +01002639TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03002640 const TTypeQualifierBuilder &typeQualifierBuilder,
2641 const TSourceLoc &nameLine,
2642 const TString &blockName,
2643 TFieldList *fieldList,
2644 const TString *instanceName,
2645 const TSourceLoc &instanceLine,
2646 TIntermTyped *arrayIndex,
2647 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002648{
Olli Etuaho856c4972016-08-08 11:38:39 +03002649 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002650
Olli Etuaho613b9592016-09-05 12:05:53 +03002651 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03002652
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002653 if (typeQualifier.qualifier != EvqUniform)
2654 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302655 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
2656 "interface blocks must be uniform");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002657 }
2658
Martin Radev70866b82016-07-22 15:27:42 +03002659 if (typeQualifier.invariant)
2660 {
2661 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
2662 }
2663
Martin Radev2cc85b32016-08-05 16:22:53 +03002664 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
2665
Jamie Madill099c0f32013-06-20 11:55:52 -04002666 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03002667 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002668
Jamie Madill099c0f32013-06-20 11:55:52 -04002669 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2670 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002671 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002672 }
2673
Jamie Madill1566ef72013-06-20 11:55:54 -04002674 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2675 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002676 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002677 }
2678
Olli Etuaho856c4972016-08-08 11:38:39 +03002679 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002680
Martin Radev2cc85b32016-08-05 16:22:53 +03002681 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
2682
Arun Patole7e7e68d2015-05-22 12:02:25 +05302683 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2684 if (!symbolTable.declare(blockNameSymbol))
2685 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002686 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002687 }
2688
Jamie Madill98493dd2013-07-08 14:39:03 -04002689 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302690 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2691 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002692 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302693 TType *fieldType = field->type();
2694 if (IsSampler(fieldType->getBasicType()))
2695 {
2696 error(field->line(), "unsupported type", fieldType->getBasicString(),
2697 "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002698 }
2699
Martin Radev2cc85b32016-08-05 16:22:53 +03002700 if (IsImage(fieldType->getBasicType()))
2701 {
2702 error(field->line(), "unsupported type", fieldType->getBasicString(),
2703 "image types are not allowed in interface blocks");
2704 }
2705
Jamie Madill98493dd2013-07-08 14:39:03 -04002706 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002707 switch (qualifier)
2708 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002709 case EvqGlobal:
2710 case EvqUniform:
2711 break;
2712 default:
2713 error(field->line(), "invalid qualifier on interface block member",
2714 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04002715 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002716 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002717
Martin Radev70866b82016-07-22 15:27:42 +03002718 if (fieldType->isInvariant())
2719 {
2720 error(field->line(), "invalid qualifier on interface block member", "invariant");
2721 }
2722
Jamie Madilla5efff92013-06-06 11:56:47 -04002723 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002724 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03002725 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
Jamie Madill099c0f32013-06-20 11:55:52 -04002726
Jamie Madill98493dd2013-07-08 14:39:03 -04002727 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002728 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002729 error(field->line(), "invalid layout qualifier:",
2730 getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04002731 }
2732
Jamie Madill98493dd2013-07-08 14:39:03 -04002733 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002734 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002735 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002736 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002737 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002738 {
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002739 warning(field->line(), "extraneous layout qualifier:",
Jamie Madillb98c3a82015-07-23 14:26:04 -04002740 getMatrixPackingString(fieldLayoutQualifier.matrixPacking),
2741 "only has an effect on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04002742 }
2743
Jamie Madill98493dd2013-07-08 14:39:03 -04002744 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002745 }
2746
Jamie Madill98493dd2013-07-08 14:39:03 -04002747 // add array index
Olli Etuaho856c4972016-08-08 11:38:39 +03002748 unsigned int arraySize = 0;
2749 if (arrayIndex != nullptr)
Jamie Madill98493dd2013-07-08 14:39:03 -04002750 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002751 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04002752 }
2753
Jamie Madillb98c3a82015-07-23 14:26:04 -04002754 TInterfaceBlock *interfaceBlock =
2755 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2756 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
2757 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002758
2759 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04002760 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002761
Jamie Madill98493dd2013-07-08 14:39:03 -04002762 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002763 {
2764 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002765 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2766 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002767 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302768 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002769
2770 // set parent pointer of the field variable
2771 fieldType->setInterfaceBlock(interfaceBlock);
2772
Arun Patole7e7e68d2015-05-22 12:02:25 +05302773 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002774 fieldVariable->setQualifier(typeQualifier.qualifier);
2775
Arun Patole7e7e68d2015-05-22 12:02:25 +05302776 if (!symbolTable.declare(fieldVariable))
2777 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002778 error(field->line(), "redefinition", field->name().c_str(),
2779 "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002780 }
2781 }
2782 }
2783 else
2784 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002785 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03002786
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002787 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302788 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002789 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002790
Arun Patole7e7e68d2015-05-22 12:02:25 +05302791 if (!symbolTable.declare(instanceTypeDef))
2792 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002793 error(instanceLine, "redefinition", instanceName->c_str(),
2794 "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002795 }
2796
Jamie Madillb98c3a82015-07-23 14:26:04 -04002797 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002798 symbolName = instanceTypeDef->getName();
2799 }
2800
Olli Etuaho13389b62016-10-16 11:48:18 +01002801 TIntermSymbol *blockSymbol =
2802 intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line);
2803 TIntermDeclaration *declaration = new TIntermDeclaration();
2804 declaration->appendDeclarator(blockSymbol);
2805 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04002806
2807 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01002808 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002809}
2810
Olli Etuaho383b7912016-08-05 11:22:59 +03002811void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002812{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002813 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002814
2815 // Embedded structure definitions are not supported per GLSL ES spec.
2816 // They aren't allowed in GLSL either, but we need to detect this here
2817 // so we don't rely on the GLSL compiler to catch it.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302818 if (mStructNestingLevel > 1)
2819 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002820 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002821 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002822}
2823
2824void TParseContext::exitStructDeclaration()
2825{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002826 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002827}
2828
Olli Etuaho8a176262016-08-16 14:23:01 +03002829void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002830{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302831 if (!IsWebGLBasedSpec(mShaderSpec))
2832 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002833 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002834 }
2835
Arun Patole7e7e68d2015-05-22 12:02:25 +05302836 if (field.type()->getBasicType() != EbtStruct)
2837 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002838 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002839 }
2840
2841 // We're already inside a structure definition at this point, so add
2842 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302843 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2844 {
Jamie Madill41a49272014-03-18 16:10:13 -04002845 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002846 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
2847 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04002848 std::string reason = reasonStream.str();
2849 error(line, reason.c_str(), field.name().c_str(), "");
Olli Etuaho8a176262016-08-16 14:23:01 +03002850 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002851 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002852}
2853
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002854//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002855// Parse an array index expression
2856//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002857TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
2858 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302859 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002860{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002861 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2862 {
2863 if (baseExpression->getAsSymbolNode())
2864 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302865 error(location, " left of '[' is not of type array, matrix, or vector ",
2866 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002867 }
2868 else
2869 {
2870 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2871 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002872
2873 TConstantUnion *unionArray = new TConstantUnion[1];
2874 unionArray->setFConst(0.0f);
2875 return intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst),
2876 location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002877 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002878
Jamie Madill21c1e452014-12-29 11:33:41 -05002879 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2880
Olli Etuaho36b05142015-11-12 13:10:42 +02002881 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
2882 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
2883 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
2884 // index is a constant expression.
2885 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
2886 {
2887 if (baseExpression->isInterfaceBlock())
2888 {
2889 error(
2890 location, "", "[",
2891 "array indexes for interface blocks arrays must be constant integral expressions");
Olli Etuaho36b05142015-11-12 13:10:42 +02002892 }
2893 else if (baseExpression->getQualifier() == EvqFragmentOut)
2894 {
2895 error(location, "", "[",
2896 "array indexes for fragment outputs must be constant integral expressions");
Olli Etuaho36b05142015-11-12 13:10:42 +02002897 }
Olli Etuaho3e960462015-11-12 15:58:39 +02002898 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
2899 {
2900 error(location, "", "[", "array index for gl_FragData must be constant zero");
Olli Etuaho3e960462015-11-12 15:58:39 +02002901 }
Olli Etuaho36b05142015-11-12 13:10:42 +02002902 }
2903
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002904 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002905 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002906 // If an out-of-range index is not qualified as constant, the behavior in the spec is
2907 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
2908 // constant fold expressions that are not constant expressions). The most compatible way to
2909 // handle this case is to report a warning instead of an error and force the index to be in
2910 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002911 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Jamie Madill21c1e452014-12-29 11:33:41 -05002912 int index = indexConstantUnion->getIConst(0);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002913
2914 int safeIndex = -1;
2915
2916 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04002917 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002918 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03002919 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002920 if (mShaderSpec == SH_WEBGL2_SPEC)
2921 {
2922 // Error has been already generated if index is not const.
2923 if (indexExpression->getQualifier() == EvqConst)
2924 {
2925 error(location, "", "[",
2926 "array index for gl_FragData must be constant zero");
2927 }
2928 safeIndex = 0;
2929 }
2930 else if (!isExtensionEnabled("GL_EXT_draw_buffers"))
2931 {
2932 outOfRangeError(outOfRangeIndexIsError, location, "", "[",
2933 "array index for gl_FragData must be zero when "
2934 "GL_EXT_draw_buffers is disabled");
2935 safeIndex = 0;
2936 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03002937 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002938 // Only do generic out-of-range check if similar error hasn't already been reported.
2939 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03002940 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002941 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
2942 baseExpression->getArraySize(),
2943 "array index out of range", "[]");
2944 }
2945 }
2946 else if (baseExpression->isMatrix())
2947 {
2948 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03002949 baseExpression->getType().getCols(),
2950 "matrix field selection out of range", "[]");
Jamie Madill7164cf42013-07-08 13:30:59 -04002951 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002952 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04002953 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002954 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
2955 baseExpression->getType().getNominalSize(),
2956 "vector field selection out of range", "[]");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002957 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002958
2959 ASSERT(safeIndex >= 0);
2960 // Data of constant unions can't be changed, because it may be shared with other
2961 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
2962 // sanitized object.
2963 if (safeIndex != index)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002964 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002965 TConstantUnion *safeConstantUnion = new TConstantUnion();
2966 safeConstantUnion->setIConst(safeIndex);
2967 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002968 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002969
2970 return intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location,
2971 &mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002972 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002973 else
2974 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002975 return intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location,
2976 &mDiagnostics);
Jamie Madill7164cf42013-07-08 13:30:59 -04002977 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002978}
2979
Olli Etuaho90892fb2016-07-14 14:44:51 +03002980int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
2981 const TSourceLoc &location,
2982 int index,
2983 int arraySize,
2984 const char *reason,
2985 const char *token)
2986{
2987 if (index >= arraySize || index < 0)
2988 {
2989 std::stringstream extraInfoStream;
2990 extraInfoStream << "'" << index << "'";
2991 std::string extraInfo = extraInfoStream.str();
2992 outOfRangeError(outOfRangeIndexIsError, location, reason, token, extraInfo.c_str());
2993 if (index < 0)
2994 {
2995 return 0;
2996 }
2997 else
2998 {
2999 return arraySize - 1;
3000 }
3001 }
3002 return index;
3003}
3004
Jamie Madillb98c3a82015-07-23 14:26:04 -04003005TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3006 const TSourceLoc &dotLocation,
3007 const TString &fieldString,
3008 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003009{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003010 if (baseExpression->isArray())
3011 {
3012 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003013 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003014 }
3015
3016 if (baseExpression->isVector())
3017 {
3018 TVectorFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003019 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields,
3020 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003021 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003022 fields.num = 1;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003023 fields.offsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003024 }
3025
Olli Etuahob6fa0432016-09-28 16:28:05 +01003026 return TIntermediate::AddSwizzle(baseExpression, fields, dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003027 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003028 else if (baseExpression->getBasicType() == EbtStruct)
3029 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303030 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003031 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003032 {
3033 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003034 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003035 }
3036 else
3037 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003038 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003039 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003040 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003041 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003042 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003043 {
3044 fieldFound = true;
3045 break;
3046 }
3047 }
3048 if (fieldFound)
3049 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003050 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3051 index->setLine(fieldLocation);
3052 return intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index,
3053 dotLocation, &mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003054 }
3055 else
3056 {
3057 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003058 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003059 }
3060 }
3061 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003062 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003063 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303064 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003065 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003066 {
3067 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003068 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003069 }
3070 else
3071 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003072 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003073 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003074 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003075 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003076 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003077 {
3078 fieldFound = true;
3079 break;
3080 }
3081 }
3082 if (fieldFound)
3083 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003084 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3085 index->setLine(fieldLocation);
3086 return intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index,
3087 dotLocation, &mDiagnostics);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003088 }
3089 else
3090 {
3091 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003092 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003093 }
3094 }
3095 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003096 else
3097 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003098 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003099 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03003100 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303101 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003102 }
3103 else
3104 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303105 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003106 " field selection requires structure, vector, or interface block on left hand "
3107 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303108 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003109 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003110 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003111 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003112}
3113
Jamie Madillb98c3a82015-07-23 14:26:04 -04003114TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3115 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003116{
Martin Radev802abe02016-08-04 17:48:32 +03003117 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003118
3119 if (qualifierType == "shared")
3120 {
Olli Etuahof0173152016-10-17 09:05:03 -07003121 if (IsWebGLBasedSpec(mShaderSpec))
3122 {
3123 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
3124 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003125 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003126 }
3127 else if (qualifierType == "packed")
3128 {
Olli Etuahof0173152016-10-17 09:05:03 -07003129 if (IsWebGLBasedSpec(mShaderSpec))
3130 {
3131 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
3132 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003133 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003134 }
3135 else if (qualifierType == "std140")
3136 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003137 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003138 }
3139 else if (qualifierType == "row_major")
3140 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003141 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003142 }
3143 else if (qualifierType == "column_major")
3144 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003145 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003146 }
3147 else if (qualifierType == "location")
3148 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003149 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
3150 "location requires an argument");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003151 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003152 else if (qualifierType == "rgba32f")
3153 {
3154 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3155 qualifier.imageInternalFormat = EiifRGBA32F;
3156 }
3157 else if (qualifierType == "rgba16f")
3158 {
3159 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3160 qualifier.imageInternalFormat = EiifRGBA16F;
3161 }
3162 else if (qualifierType == "r32f")
3163 {
3164 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3165 qualifier.imageInternalFormat = EiifR32F;
3166 }
3167 else if (qualifierType == "rgba8")
3168 {
3169 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3170 qualifier.imageInternalFormat = EiifRGBA8;
3171 }
3172 else if (qualifierType == "rgba8_snorm")
3173 {
3174 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3175 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
3176 }
3177 else if (qualifierType == "rgba32i")
3178 {
3179 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3180 qualifier.imageInternalFormat = EiifRGBA32I;
3181 }
3182 else if (qualifierType == "rgba16i")
3183 {
3184 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3185 qualifier.imageInternalFormat = EiifRGBA16I;
3186 }
3187 else if (qualifierType == "rgba8i")
3188 {
3189 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3190 qualifier.imageInternalFormat = EiifRGBA8I;
3191 }
3192 else if (qualifierType == "r32i")
3193 {
3194 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3195 qualifier.imageInternalFormat = EiifR32I;
3196 }
3197 else if (qualifierType == "rgba32ui")
3198 {
3199 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3200 qualifier.imageInternalFormat = EiifRGBA32UI;
3201 }
3202 else if (qualifierType == "rgba16ui")
3203 {
3204 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3205 qualifier.imageInternalFormat = EiifRGBA16UI;
3206 }
3207 else if (qualifierType == "rgba8ui")
3208 {
3209 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3210 qualifier.imageInternalFormat = EiifRGBA8UI;
3211 }
3212 else if (qualifierType == "r32ui")
3213 {
3214 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3215 qualifier.imageInternalFormat = EiifR32UI;
3216 }
3217
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003218 else
3219 {
3220 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003221 }
3222
Jamie Madilla5efff92013-06-06 11:56:47 -04003223 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003224}
3225
Martin Radev802abe02016-08-04 17:48:32 +03003226void TParseContext::parseLocalSize(const TString &qualifierType,
3227 const TSourceLoc &qualifierTypeLine,
3228 int intValue,
3229 const TSourceLoc &intValueLine,
3230 const std::string &intValueString,
3231 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03003232 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03003233{
Olli Etuaho856c4972016-08-08 11:38:39 +03003234 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03003235 if (intValue < 1)
3236 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03003237 std::string errorMessage = std::string(getWorkGroupSizeString(index)) + " must be positive";
Martin Radev802abe02016-08-04 17:48:32 +03003238 error(intValueLine, "out of range:", intValueString.c_str(), errorMessage.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003239 }
3240 (*localSize)[index] = intValue;
3241}
3242
Jamie Madillb98c3a82015-07-23 14:26:04 -04003243TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3244 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003245 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303246 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003247{
Martin Radev802abe02016-08-04 17:48:32 +03003248 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003249
Martin Radev802abe02016-08-04 17:48:32 +03003250 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003251
Martin Radev802abe02016-08-04 17:48:32 +03003252 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003253 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003254 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003255 if (intValue < 0)
3256 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003257 error(intValueLine, "out of range:", intValueString.c_str(),
3258 "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003259 }
3260 else
3261 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003262 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03003263 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003264 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003265 }
Martin Radev802abe02016-08-04 17:48:32 +03003266 else if (qualifierType == "local_size_x")
3267 {
3268 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
3269 &qualifier.localSize);
3270 }
3271 else if (qualifierType == "local_size_y")
3272 {
3273 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
3274 &qualifier.localSize);
3275 }
3276 else if (qualifierType == "local_size_z")
3277 {
3278 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
3279 &qualifier.localSize);
3280 }
3281 else
3282 {
3283 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003284 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003285
Jamie Madilla5efff92013-06-06 11:56:47 -04003286 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003287}
3288
Olli Etuaho613b9592016-09-05 12:05:53 +03003289TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
3290{
3291 return new TTypeQualifierBuilder(
3292 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
3293 mShaderVersion);
3294}
3295
Jamie Madillb98c3a82015-07-23 14:26:04 -04003296TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03003297 TLayoutQualifier rightQualifier,
3298 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003299{
Martin Radevc28888b2016-07-22 15:27:42 +03003300 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
3301 &mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003302}
3303
Martin Radev70866b82016-07-22 15:27:42 +03003304TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
3305 const TTypeQualifierBuilder &typeQualifierBuilder,
3306 TPublicType *typeSpecifier,
3307 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003308{
Olli Etuaho613b9592016-09-05 12:05:53 +03003309 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003310
Martin Radev70866b82016-07-22 15:27:42 +03003311 typeSpecifier->qualifier = typeQualifier.qualifier;
3312 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03003313 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03003314 typeSpecifier->invariant = typeQualifier.invariant;
3315 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05303316 {
Martin Radev70866b82016-07-22 15:27:42 +03003317 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003318 }
Martin Radev70866b82016-07-22 15:27:42 +03003319 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003320}
3321
Jamie Madillb98c3a82015-07-23 14:26:04 -04003322TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3323 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003324{
Martin Radev4a9cd802016-09-01 16:51:51 +03003325 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
3326 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03003327
Martin Radev4a9cd802016-09-01 16:51:51 +03003328 checkIsNonVoid(typeSpecifier.getLine(), (*fieldList)[0]->name(), typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003329
Martin Radev4a9cd802016-09-01 16:51:51 +03003330 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003331
Arun Patole7e7e68d2015-05-22 12:02:25 +05303332 for (unsigned int i = 0; i < fieldList->size(); ++i)
3333 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003334 //
3335 // Careful not to replace already known aspects of type, like array-ness
3336 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303337 TType *type = (*fieldList)[i]->type();
Martin Radev4a9cd802016-09-01 16:51:51 +03003338 type->setBasicType(typeSpecifier.getBasicType());
3339 type->setPrimarySize(typeSpecifier.getPrimarySize());
3340 type->setSecondarySize(typeSpecifier.getSecondarySize());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003341 type->setPrecision(typeSpecifier.precision);
3342 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003343 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
Martin Radev2cc85b32016-08-05 16:22:53 +03003344 type->setMemoryQualifier(typeSpecifier.memoryQualifier);
Martin Radev70866b82016-07-22 15:27:42 +03003345 type->setInvariant(typeSpecifier.invariant);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003346
3347 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303348 if (type->isArray())
3349 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003350 checkIsValidTypeForArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003351 }
3352 if (typeSpecifier.array)
Olli Etuaho856c4972016-08-08 11:38:39 +03003353 type->setArraySize(static_cast<unsigned int>(typeSpecifier.arraySize));
Martin Radev4a9cd802016-09-01 16:51:51 +03003354 if (typeSpecifier.getUserDef())
Arun Patole7e7e68d2015-05-22 12:02:25 +05303355 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003356 type->setStruct(typeSpecifier.getUserDef()->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003357 }
3358
Martin Radev4a9cd802016-09-01 16:51:51 +03003359 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*fieldList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003360 }
3361
Jamie Madill98493dd2013-07-08 14:39:03 -04003362 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003363}
3364
Martin Radev4a9cd802016-09-01 16:51:51 +03003365TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
3366 const TSourceLoc &nameLine,
3367 const TString *structName,
3368 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003369{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303370 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003371 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003372
Jamie Madill9b820842015-02-12 10:40:10 -05003373 // Store a bool in the struct if we're at global scope, to allow us to
3374 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05003375 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003376
Jamie Madill98493dd2013-07-08 14:39:03 -04003377 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003378 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003379 checkIsNotReserved(nameLine, *structName);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303380 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
3381 if (!symbolTable.declare(userTypeDef))
3382 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003383 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003384 }
3385 }
3386
3387 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04003388 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003389 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003390 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04003391 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003392 switch (qualifier)
3393 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003394 case EvqGlobal:
3395 case EvqTemporary:
3396 break;
3397 default:
3398 error(field.line(), "invalid qualifier on struct member",
3399 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003400 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003401 }
Martin Radev70866b82016-07-22 15:27:42 +03003402 if (field.type()->isInvariant())
3403 {
3404 error(field.line(), "invalid qualifier on struct member", "invariant");
3405 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003406 if (IsImage(field.type()->getBasicType()))
3407 {
3408 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
3409 }
3410
3411 checkIsMemoryQualifierNotSpecified(field.type()->getMemoryQualifier(), field.line());
Martin Radev70866b82016-07-22 15:27:42 +03003412
3413 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003414 }
3415
Martin Radev4a9cd802016-09-01 16:51:51 +03003416 TTypeSpecifierNonArray typeSpecifierNonArray;
3417 typeSpecifierNonArray.initialize(EbtStruct, structLine);
3418 typeSpecifierNonArray.userDef = structureType;
3419 typeSpecifierNonArray.isStructSpecifier = true;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003420 exitStructDeclaration();
3421
Martin Radev4a9cd802016-09-01 16:51:51 +03003422 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003423}
3424
Jamie Madillb98c3a82015-07-23 14:26:04 -04003425TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003426 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003427 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02003428{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003429 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003430 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02003431 init->isVector())
3432 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003433 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
3434 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003435 return nullptr;
3436 }
3437
Olli Etuahoac5274d2015-02-20 10:19:08 +02003438 if (statementList)
3439 {
3440 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
3441 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02003442 return nullptr;
3443 }
3444 }
3445
Olli Etuahoa3a36662015-02-17 13:46:51 +02003446 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3447 if (node == nullptr)
3448 {
3449 error(loc, "erroneous switch statement", "switch");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003450 return nullptr;
3451 }
3452 return node;
3453}
3454
3455TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3456{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003457 if (mSwitchNestingLevel == 0)
3458 {
3459 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003460 return nullptr;
3461 }
3462 if (condition == nullptr)
3463 {
3464 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003465 return nullptr;
3466 }
3467 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04003468 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02003469 {
3470 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003471 }
3472 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003473 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
3474 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
3475 // fold in case labels.
3476 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02003477 {
3478 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003479 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003480 TIntermCase *node = intermediate.addCase(condition, loc);
3481 if (node == nullptr)
3482 {
3483 error(loc, "erroneous case statement", "case");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003484 return nullptr;
3485 }
3486 return node;
3487}
3488
3489TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3490{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003491 if (mSwitchNestingLevel == 0)
3492 {
3493 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003494 return nullptr;
3495 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003496 TIntermCase *node = intermediate.addCase(nullptr, loc);
3497 if (node == nullptr)
3498 {
3499 error(loc, "erroneous default statement", "default");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003500 return nullptr;
3501 }
3502 return node;
3503}
3504
Jamie Madillb98c3a82015-07-23 14:26:04 -04003505TIntermTyped *TParseContext::createUnaryMath(TOperator op,
3506 TIntermTyped *child,
3507 const TSourceLoc &loc,
3508 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003509{
3510 if (child == nullptr)
3511 {
3512 return nullptr;
3513 }
3514
3515 switch (op)
3516 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003517 case EOpLogicalNot:
3518 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
3519 child->isVector())
3520 {
3521 return nullptr;
3522 }
3523 break;
3524 case EOpBitwiseNot:
3525 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3526 child->isMatrix() || child->isArray())
3527 {
3528 return nullptr;
3529 }
3530 break;
3531 case EOpPostIncrement:
3532 case EOpPreIncrement:
3533 case EOpPostDecrement:
3534 case EOpPreDecrement:
3535 case EOpNegative:
3536 case EOpPositive:
3537 if (child->getBasicType() == EbtStruct || child->getBasicType() == EbtBool ||
Martin Radev2cc85b32016-08-05 16:22:53 +03003538 child->isArray() || IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04003539 {
3540 return nullptr;
3541 }
3542 // Operators for built-ins are already type checked against their prototype.
3543 default:
3544 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003545 }
3546
Olli Etuahof119a262016-08-19 15:54:22 +03003547 TIntermUnary *node = new TIntermUnary(op, child);
3548 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03003549
3550 TIntermTyped *foldedNode = node->fold(&mDiagnostics);
3551 if (foldedNode)
3552 return foldedNode;
3553
3554 return node;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003555}
3556
Olli Etuaho09b22472015-02-11 11:47:26 +02003557TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3558{
Olli Etuahof6c694b2015-03-26 14:50:53 +02003559 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003560 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003561 {
3562 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003563 return child;
3564 }
3565 return node;
3566}
3567
Jamie Madillb98c3a82015-07-23 14:26:04 -04003568TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
3569 TIntermTyped *child,
3570 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003571{
Olli Etuaho856c4972016-08-08 11:38:39 +03003572 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02003573 return addUnaryMath(op, child, loc);
3574}
3575
Jamie Madillb98c3a82015-07-23 14:26:04 -04003576bool TParseContext::binaryOpCommonCheck(TOperator op,
3577 TIntermTyped *left,
3578 TIntermTyped *right,
3579 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003580{
Olli Etuaho244be012016-08-18 15:26:02 +03003581 if (left->getType().getStruct() || right->getType().getStruct())
3582 {
3583 switch (op)
3584 {
3585 case EOpIndexDirectStruct:
3586 ASSERT(left->getType().getStruct());
3587 break;
3588 case EOpEqual:
3589 case EOpNotEqual:
3590 case EOpAssign:
3591 case EOpInitialize:
3592 if (left->getType() != right->getType())
3593 {
3594 return false;
3595 }
3596 break;
3597 default:
3598 error(loc, "Invalid operation for structs", GetOperatorString(op));
3599 return false;
3600 }
3601 }
3602
Olli Etuahod6b14282015-03-17 14:31:35 +02003603 if (left->isArray() || right->isArray())
3604 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003605 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003606 {
3607 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3608 return false;
3609 }
3610
3611 if (left->isArray() != right->isArray())
3612 {
3613 error(loc, "array / non-array mismatch", GetOperatorString(op));
3614 return false;
3615 }
3616
3617 switch (op)
3618 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003619 case EOpEqual:
3620 case EOpNotEqual:
3621 case EOpAssign:
3622 case EOpInitialize:
3623 break;
3624 default:
3625 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3626 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02003627 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003628 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003629 if (left->getArraySize() != right->getArraySize())
3630 {
3631 error(loc, "array size mismatch", GetOperatorString(op));
3632 return false;
3633 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003634 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003635
3636 // Check ops which require integer / ivec parameters
3637 bool isBitShift = false;
3638 switch (op)
3639 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003640 case EOpBitShiftLeft:
3641 case EOpBitShiftRight:
3642 case EOpBitShiftLeftAssign:
3643 case EOpBitShiftRightAssign:
3644 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3645 // check that the basic type is an integer type.
3646 isBitShift = true;
3647 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3648 {
3649 return false;
3650 }
3651 break;
3652 case EOpBitwiseAnd:
3653 case EOpBitwiseXor:
3654 case EOpBitwiseOr:
3655 case EOpBitwiseAndAssign:
3656 case EOpBitwiseXorAssign:
3657 case EOpBitwiseOrAssign:
3658 // It is enough to check the type of only one operand, since later it
3659 // is checked that the operand types match.
3660 if (!IsInteger(left->getBasicType()))
3661 {
3662 return false;
3663 }
3664 break;
3665 default:
3666 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003667 }
3668
3669 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3670 // So the basic type should usually match.
3671 if (!isBitShift && left->getBasicType() != right->getBasicType())
3672 {
3673 return false;
3674 }
3675
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003676 // Check that:
3677 // 1. Type sizes match exactly on ops that require that.
3678 // 2. Restrictions for structs that contain arrays or samplers are respected.
3679 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003680 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003681 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003682 case EOpAssign:
3683 case EOpInitialize:
3684 case EOpEqual:
3685 case EOpNotEqual:
3686 // ESSL 1.00 sections 5.7, 5.8, 5.9
3687 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
3688 {
3689 error(loc, "undefined operation for structs containing arrays",
3690 GetOperatorString(op));
3691 return false;
3692 }
3693 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3694 // we interpret the spec so that this extends to structs containing samplers,
3695 // similarly to ESSL 1.00 spec.
3696 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
3697 left->getType().isStructureContainingSamplers())
3698 {
3699 error(loc, "undefined operation for structs containing samplers",
3700 GetOperatorString(op));
3701 return false;
3702 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003703
3704 if ((op == EOpAssign || op == EOpInitialize) &&
3705 left->getType().isStructureContainingImages())
3706 {
3707 error(loc, "undefined operation for structs containing images",
3708 GetOperatorString(op));
3709 return false;
3710 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003711 case EOpLessThan:
3712 case EOpGreaterThan:
3713 case EOpLessThanEqual:
3714 case EOpGreaterThanEqual:
3715 if ((left->getNominalSize() != right->getNominalSize()) ||
3716 (left->getSecondarySize() != right->getSecondarySize()))
3717 {
3718 return false;
3719 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003720 break;
3721 case EOpAdd:
3722 case EOpSub:
3723 case EOpDiv:
3724 case EOpIMod:
3725 case EOpBitShiftLeft:
3726 case EOpBitShiftRight:
3727 case EOpBitwiseAnd:
3728 case EOpBitwiseXor:
3729 case EOpBitwiseOr:
3730 case EOpAddAssign:
3731 case EOpSubAssign:
3732 case EOpDivAssign:
3733 case EOpIModAssign:
3734 case EOpBitShiftLeftAssign:
3735 case EOpBitShiftRightAssign:
3736 case EOpBitwiseAndAssign:
3737 case EOpBitwiseXorAssign:
3738 case EOpBitwiseOrAssign:
3739 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
3740 {
3741 return false;
3742 }
3743
3744 // Are the sizes compatible?
3745 if (left->getNominalSize() != right->getNominalSize() ||
3746 left->getSecondarySize() != right->getSecondarySize())
3747 {
3748 // If the nominal sizes of operands do not match:
3749 // One of them must be a scalar.
3750 if (!left->isScalar() && !right->isScalar())
3751 return false;
3752
3753 // In the case of compound assignment other than multiply-assign,
3754 // the right side needs to be a scalar. Otherwise a vector/matrix
3755 // would be assigned to a scalar. A scalar can't be shifted by a
3756 // vector either.
3757 if (!right->isScalar() &&
3758 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
3759 return false;
3760 }
3761 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003762 default:
3763 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003764 }
3765
Olli Etuahod6b14282015-03-17 14:31:35 +02003766 return true;
3767}
3768
Olli Etuaho1dded802016-08-18 18:13:13 +03003769bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
3770 const TType &left,
3771 const TType &right)
3772{
3773 switch (op)
3774 {
3775 case EOpMul:
3776 case EOpMulAssign:
3777 return left.getNominalSize() == right.getNominalSize() &&
3778 left.getSecondarySize() == right.getSecondarySize();
3779 case EOpVectorTimesScalar:
3780 return true;
3781 case EOpVectorTimesScalarAssign:
3782 ASSERT(!left.isMatrix() && !right.isMatrix());
3783 return left.isVector() && !right.isVector();
3784 case EOpVectorTimesMatrix:
3785 return left.getNominalSize() == right.getRows();
3786 case EOpVectorTimesMatrixAssign:
3787 ASSERT(!left.isMatrix() && right.isMatrix());
3788 return left.isVector() && left.getNominalSize() == right.getRows() &&
3789 left.getNominalSize() == right.getCols();
3790 case EOpMatrixTimesVector:
3791 return left.getCols() == right.getNominalSize();
3792 case EOpMatrixTimesScalar:
3793 return true;
3794 case EOpMatrixTimesScalarAssign:
3795 ASSERT(left.isMatrix() && !right.isMatrix());
3796 return !right.isVector();
3797 case EOpMatrixTimesMatrix:
3798 return left.getCols() == right.getRows();
3799 case EOpMatrixTimesMatrixAssign:
3800 ASSERT(left.isMatrix() && right.isMatrix());
3801 // We need to check two things:
3802 // 1. The matrix multiplication step is valid.
3803 // 2. The result will have the same number of columns as the lvalue.
3804 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
3805
3806 default:
3807 UNREACHABLE();
3808 return false;
3809 }
3810}
3811
Jamie Madillb98c3a82015-07-23 14:26:04 -04003812TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
3813 TIntermTyped *left,
3814 TIntermTyped *right,
3815 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02003816{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003817 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003818 return nullptr;
3819
Olli Etuahofc1806e2015-03-17 13:03:11 +02003820 switch (op)
3821 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003822 case EOpEqual:
3823 case EOpNotEqual:
3824 break;
3825 case EOpLessThan:
3826 case EOpGreaterThan:
3827 case EOpLessThanEqual:
3828 case EOpGreaterThanEqual:
Olli Etuaho244be012016-08-18 15:26:02 +03003829 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3830 !right->getType().getStruct());
3831 if (left->isMatrix() || left->isVector())
Jamie Madillb98c3a82015-07-23 14:26:04 -04003832 {
3833 return nullptr;
3834 }
3835 break;
3836 case EOpLogicalOr:
3837 case EOpLogicalXor:
3838 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03003839 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3840 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003841 if (left->getBasicType() != EbtBool || left->isMatrix() || left->isVector())
3842 {
3843 return nullptr;
3844 }
3845 break;
3846 case EOpAdd:
3847 case EOpSub:
3848 case EOpDiv:
3849 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03003850 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3851 !right->getType().getStruct());
3852 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04003853 {
3854 return nullptr;
3855 }
3856 break;
3857 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03003858 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3859 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003860 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03003861 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04003862 {
3863 return nullptr;
3864 }
3865 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003866 default:
3867 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003868 }
3869
Olli Etuaho1dded802016-08-18 18:13:13 +03003870 if (op == EOpMul)
3871 {
3872 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
3873 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
3874 {
3875 return nullptr;
3876 }
3877 }
3878
Olli Etuaho3fdec912016-08-18 15:08:06 +03003879 TIntermBinary *node = new TIntermBinary(op, left, right);
3880 node->setLine(loc);
3881
Olli Etuaho3fdec912016-08-18 15:08:06 +03003882 // See if we can fold constants.
3883 TIntermTyped *foldedNode = node->fold(&mDiagnostics);
3884 if (foldedNode)
3885 return foldedNode;
3886
3887 return node;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003888}
3889
Jamie Madillb98c3a82015-07-23 14:26:04 -04003890TIntermTyped *TParseContext::addBinaryMath(TOperator op,
3891 TIntermTyped *left,
3892 TIntermTyped *right,
3893 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003894{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003895 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003896 if (node == 0)
3897 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003898 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3899 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003900 return left;
3901 }
3902 return node;
3903}
3904
Jamie Madillb98c3a82015-07-23 14:26:04 -04003905TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
3906 TIntermTyped *left,
3907 TIntermTyped *right,
3908 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003909{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003910 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003911 if (node == 0)
3912 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003913 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3914 right->getCompleteString());
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003915 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02003916 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003917 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
3918 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003919 }
3920 return node;
3921}
3922
Olli Etuaho13389b62016-10-16 11:48:18 +01003923TIntermBinary *TParseContext::createAssign(TOperator op,
3924 TIntermTyped *left,
3925 TIntermTyped *right,
3926 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003927{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003928 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003929 {
Olli Etuaho1dded802016-08-18 18:13:13 +03003930 if (op == EOpMulAssign)
3931 {
3932 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
3933 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
3934 {
3935 return nullptr;
3936 }
3937 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03003938 TIntermBinary *node = new TIntermBinary(op, left, right);
3939 node->setLine(loc);
3940
Olli Etuaho3fdec912016-08-18 15:08:06 +03003941 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02003942 }
3943 return nullptr;
3944}
3945
Jamie Madillb98c3a82015-07-23 14:26:04 -04003946TIntermTyped *TParseContext::addAssign(TOperator op,
3947 TIntermTyped *left,
3948 TIntermTyped *right,
3949 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003950{
3951 TIntermTyped *node = createAssign(op, left, right, loc);
3952 if (node == nullptr)
3953 {
3954 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02003955 return left;
3956 }
3957 return node;
3958}
3959
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003960TIntermTyped *TParseContext::addComma(TIntermTyped *left,
3961 TIntermTyped *right,
3962 const TSourceLoc &loc)
3963{
Corentin Wallez0d959252016-07-12 17:26:32 -04003964 // WebGL2 section 5.26, the following results in an error:
3965 // "Sequence operator applied to void, arrays, or structs containing arrays"
3966 if (mShaderSpec == SH_WEBGL2_SPEC && (left->isArray() || left->getBasicType() == EbtVoid ||
3967 left->getType().isStructureContainingArrays() ||
3968 right->isArray() || right->getBasicType() == EbtVoid ||
3969 right->getType().isStructureContainingArrays()))
3970 {
3971 error(loc,
3972 "sequence operator is not allowed for void, arrays, or structs containing arrays",
3973 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04003974 }
3975
Olli Etuaho4db7ded2016-10-13 12:23:11 +01003976 return TIntermediate::AddComma(left, right, loc, mShaderVersion);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003977}
3978
Olli Etuaho49300862015-02-20 14:54:49 +02003979TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3980{
3981 switch (op)
3982 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003983 case EOpContinue:
3984 if (mLoopNestingLevel <= 0)
3985 {
3986 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04003987 }
3988 break;
3989 case EOpBreak:
3990 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
3991 {
3992 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04003993 }
3994 break;
3995 case EOpReturn:
3996 if (mCurrentFunctionType->getBasicType() != EbtVoid)
3997 {
3998 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04003999 }
4000 break;
4001 default:
4002 // No checks for discard
4003 break;
Olli Etuaho49300862015-02-20 14:54:49 +02004004 }
4005 return intermediate.addBranch(op, loc);
4006}
4007
Jamie Madillb98c3a82015-07-23 14:26:04 -04004008TIntermBranch *TParseContext::addBranch(TOperator op,
4009 TIntermTyped *returnValue,
4010 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02004011{
4012 ASSERT(op == EOpReturn);
4013 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004014 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02004015 {
4016 error(loc, "void function cannot return a value", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02004017 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004018 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02004019 {
4020 error(loc, "function return is not matching type:", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02004021 }
4022 return intermediate.addBranch(op, returnValue, loc);
4023}
4024
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004025void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
4026{
4027 ASSERT(!functionCall->isUserDefined());
Olli Etuahobd674552016-10-06 13:28:42 +01004028 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004029 TIntermNode *offset = nullptr;
4030 TIntermSequence *arguments = functionCall->getSequence();
4031 if (name.compare(0, 16, "texelFetchOffset") == 0 ||
4032 name.compare(0, 16, "textureLodOffset") == 0 ||
4033 name.compare(0, 20, "textureProjLodOffset") == 0 ||
4034 name.compare(0, 17, "textureGradOffset") == 0 ||
4035 name.compare(0, 21, "textureProjGradOffset") == 0)
4036 {
4037 offset = arguments->back();
4038 }
4039 else if (name.compare(0, 13, "textureOffset") == 0 ||
4040 name.compare(0, 17, "textureProjOffset") == 0)
4041 {
4042 // A bias parameter might follow the offset parameter.
4043 ASSERT(arguments->size() >= 3);
4044 offset = (*arguments)[2];
4045 }
4046 if (offset != nullptr)
4047 {
4048 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
4049 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
4050 {
4051 TString unmangledName = TFunction::unmangleName(name);
4052 error(functionCall->getLine(), "Texture offset must be a constant expression",
4053 unmangledName.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004054 }
4055 else
4056 {
4057 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
4058 size_t size = offsetConstantUnion->getType().getObjectSize();
4059 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
4060 for (size_t i = 0u; i < size; ++i)
4061 {
4062 int offsetValue = values[i].getIConst();
4063 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
4064 {
4065 std::stringstream tokenStream;
4066 tokenStream << offsetValue;
4067 std::string token = tokenStream.str();
4068 error(offset->getLine(), "Texture offset value out of valid range",
4069 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004070 }
4071 }
4072 }
4073 }
4074}
4075
Martin Radev2cc85b32016-08-05 16:22:53 +03004076// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
4077void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
4078{
4079 ASSERT(!functionCall->isUserDefined());
4080 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
4081
4082 if (name.compare(0, 5, "image") == 0)
4083 {
4084 TIntermSequence *arguments = functionCall->getSequence();
4085 TIntermNode *imageNode = (*arguments)[0];
4086 TIntermSymbol *imageSymbol = imageNode->getAsSymbolNode();
4087
4088 const TMemoryQualifier &memoryQualifier = imageSymbol->getMemoryQualifier();
4089
4090 if (name.compare(5, 5, "Store") == 0)
4091 {
4092 if (memoryQualifier.readonly)
4093 {
4094 error(imageNode->getLine(),
4095 "'imageStore' cannot be used with images qualified as 'readonly'",
4096 imageSymbol->getSymbol().c_str());
4097 }
4098 }
4099 else if (name.compare(5, 4, "Load") == 0)
4100 {
4101 if (memoryQualifier.writeonly)
4102 {
4103 error(imageNode->getLine(),
4104 "'imageLoad' cannot be used with images qualified as 'writeonly'",
4105 imageSymbol->getSymbol().c_str());
4106 }
4107 }
4108 }
4109}
4110
4111// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
4112void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
4113 const TFunction *functionDefinition,
4114 const TIntermAggregate *functionCall)
4115{
4116 ASSERT(functionCall->isUserDefined());
4117
4118 const TIntermSequence &arguments = *functionCall->getSequence();
4119
4120 ASSERT(functionDefinition->getParamCount() == arguments.size());
4121
4122 for (size_t i = 0; i < arguments.size(); ++i)
4123 {
4124 const TType &functionArgumentType = arguments[i]->getAsTyped()->getType();
4125 const TType &functionParameterType = *functionDefinition->getParam(i).type;
4126 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
4127
4128 if (IsImage(functionArgumentType.getBasicType()))
4129 {
4130 const TMemoryQualifier &functionArgumentMemoryQualifier =
4131 functionArgumentType.getMemoryQualifier();
4132 const TMemoryQualifier &functionParameterMemoryQualifier =
4133 functionParameterType.getMemoryQualifier();
4134 if (functionArgumentMemoryQualifier.readonly &&
4135 !functionParameterMemoryQualifier.readonly)
4136 {
4137 error(functionCall->getLine(),
4138 "Function call discards the 'readonly' qualifier from image",
4139 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4140 }
4141
4142 if (functionArgumentMemoryQualifier.writeonly &&
4143 !functionParameterMemoryQualifier.writeonly)
4144 {
4145 error(functionCall->getLine(),
4146 "Function call discards the 'writeonly' qualifier from image",
4147 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4148 }
4149 }
4150 }
4151}
4152
Jamie Madillb98c3a82015-07-23 14:26:04 -04004153TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
4154 TIntermNode *paramNode,
4155 TIntermNode *thisNode,
4156 const TSourceLoc &loc,
4157 bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004158{
Jamie Madillb98c3a82015-07-23 14:26:04 -04004159 *fatalError = false;
4160 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004161 TIntermTyped *callNode = nullptr;
4162
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004163 if (thisNode != nullptr)
4164 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004165 TConstantUnion *unionArray = new TConstantUnion[1];
Jamie Madillb98c3a82015-07-23 14:26:04 -04004166 int arraySize = 0;
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004167 TIntermTyped *typedThis = thisNode->getAsTyped();
4168 if (fnCall->getName() != "length")
4169 {
4170 error(loc, "invalid method", fnCall->getName().c_str());
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004171 }
4172 else if (paramNode != nullptr)
4173 {
4174 error(loc, "method takes no parameters", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004175 }
4176 else if (typedThis == nullptr || !typedThis->isArray())
4177 {
4178 error(loc, "length can only be called on arrays", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004179 }
4180 else
4181 {
Olli Etuaho96e67382015-04-23 14:27:02 +03004182 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03004183 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004184 {
Olli Etuaho39282e12015-04-23 15:41:48 +03004185 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004186 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03004187 // (func()).length()
4188 // (int[3](0, 1, 2)).length()
Jamie Madillb98c3a82015-07-23 14:26:04 -04004189 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
4190 // expression.
4191 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
4192 // spec section 5.9 which allows "An array, vector or matrix expression with the
4193 // length method applied".
4194 error(loc, "length can only be called on array names, not on array expressions",
4195 "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004196 }
4197 }
Olli Etuaho96e67382015-04-23 14:27:02 +03004198 unionArray->setIConst(arraySize);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004199 callNode =
4200 intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004201 }
4202 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004203 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004204 // Then this should be a constructor.
Olli Etuaho856c4972016-08-08 11:38:39 +03004205 callNode = addConstructor(paramNode, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004206 }
4207 else
4208 {
4209 //
4210 // Not a constructor. Find it in the symbol table.
4211 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05304212 const TFunction *fnCandidate;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004213 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004214 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004215 if (fnCandidate)
4216 {
4217 //
4218 // A declared function.
4219 //
Olli Etuaho383b7912016-08-05 11:22:59 +03004220 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004221 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004222 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004223 }
4224 op = fnCandidate->getBuiltInOp();
4225 if (builtIn && op != EOpNull)
4226 {
4227 //
4228 // A function call mapped to a built-in operation.
4229 //
4230 if (fnCandidate->getParamCount() == 1)
4231 {
4232 //
4233 // Treat it like a built-in unary operator.
4234 //
Olli Etuaho15c2ac32015-11-09 15:51:43 +02004235 TIntermAggregate *paramAgg = paramNode->getAsAggregate();
4236 paramNode = paramAgg->getSequence()->front();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004237 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc,
4238 &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004239 if (callNode == nullptr)
4240 {
4241 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004242 extraInfoStream
4243 << "built in unary operator function. Type: "
4244 << static_cast<TIntermTyped *>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004245 std::string extraInfo = extraInfoStream.str();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004246 error(paramNode->getLine(), " wrong operand type", "Internal Error",
4247 extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004248 *fatalError = true;
4249 return nullptr;
4250 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004251 }
4252 else
4253 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004254 TIntermAggregate *aggregate =
4255 intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004256 aggregate->setType(fnCandidate->getReturnType());
4257 aggregate->setPrecisionFromChildren();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02004258 if (aggregate->areChildrenConstQualified())
4259 {
4260 aggregate->getTypePointer()->setQualifier(EvqConst);
4261 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004262
4263 // Some built-in functions have out parameters too.
4264 functionCallLValueErrorCheck(fnCandidate, aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05304265
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004266 // See if we can constant fold a built-in. Note that this may be possible even
4267 // if it is not const-qualified.
Olli Etuahof119a262016-08-19 15:54:22 +03004268 TIntermTyped *foldedNode =
4269 intermediate.foldAggregateBuiltIn(aggregate, &mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05304270 if (foldedNode)
4271 {
Arun Patole274f0702015-05-05 13:33:30 +05304272 callNode = foldedNode;
4273 }
Olli Etuahob43846e2015-06-02 18:18:57 +03004274 else
4275 {
4276 callNode = aggregate;
4277 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004278 }
4279 }
4280 else
4281 {
4282 // This is a real function call
Jamie Madillb98c3a82015-07-23 14:26:04 -04004283 TIntermAggregate *aggregate =
4284 intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004285 aggregate->setType(fnCandidate->getReturnType());
4286
Jamie Madillb98c3a82015-07-23 14:26:04 -04004287 // this is how we know whether the given function is a builtIn function or a user
4288 // defined function
4289 // if builtIn == false, it's a userDefined -> could be an overloaded
4290 // builtIn function also
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004291 // if builtIn == true, it's definitely a builtIn function with EOpNull
4292 if (!builtIn)
4293 aggregate->setUserDefined();
Olli Etuahobd674552016-10-06 13:28:42 +01004294 aggregate->getFunctionSymbolInfo()->setFromFunction(*fnCandidate);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004295
Olli Etuahobd674552016-10-06 13:28:42 +01004296 // This needs to happen after the function info including name is set
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004297 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004298 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004299 aggregate->setBuiltInFunctionPrecision();
4300
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004301 checkTextureOffsetConst(aggregate);
Martin Radev2cc85b32016-08-05 16:22:53 +03004302
4303 checkImageMemoryAccessForBuiltinFunctions(aggregate);
4304 }
4305 else
4306 {
4307 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, aggregate);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004308 }
4309
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004310 callNode = aggregate;
4311
4312 functionCallLValueErrorCheck(fnCandidate, aggregate);
4313 }
4314 }
4315 else
4316 {
4317 // error message was put out by findFunction()
4318 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004319 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004320 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004321 callNode = intermediate.addConstantUnion(unionArray,
4322 TType(EbtFloat, EbpUndefined, EvqConst), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004323 }
4324 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004325 return callNode;
4326}
4327
Jamie Madillb98c3a82015-07-23 14:26:04 -04004328TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004329 TIntermTyped *trueExpression,
4330 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03004331 const TSourceLoc &loc)
4332{
Olli Etuaho856c4972016-08-08 11:38:39 +03004333 checkIsScalarBool(loc, cond);
Olli Etuaho52901742015-04-15 13:42:45 +03004334
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004335 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03004336 {
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004337 binaryOpError(loc, ":", trueExpression->getCompleteString(),
4338 falseExpression->getCompleteString());
4339 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03004340 }
Olli Etuahode318b22016-10-25 16:18:25 +01004341 if (IsOpaqueType(trueExpression->getBasicType()))
4342 {
4343 // ESSL 1.00 section 4.1.7
4344 // ESSL 3.00 section 4.1.7
4345 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
4346 // Note that structs containing opaque types don't need to be checked as structs are
4347 // forbidden below.
4348 error(loc, "ternary operator is not allowed for opaque types", ":");
4349 return falseExpression;
4350 }
4351
Olli Etuahoa2d53032015-04-15 14:14:44 +03004352 // ESSL1 sections 5.2 and 5.7:
4353 // ESSL3 section 5.7:
4354 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004355 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03004356 {
4357 error(loc, "ternary operator is not allowed for structures or arrays", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004358 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03004359 }
Corentin Wallez0d959252016-07-12 17:26:32 -04004360 // WebGL2 section 5.26, the following results in an error:
4361 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004362 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04004363 {
4364 error(loc, "ternary operator is not allowed for void", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004365 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04004366 }
4367
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004368 return TIntermediate::AddTernarySelection(cond, trueExpression, falseExpression, loc);
Olli Etuaho52901742015-04-15 13:42:45 +03004369}
Olli Etuaho49300862015-02-20 14:54:49 +02004370
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004371//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004372// Parse an array of strings using yyparse.
4373//
4374// Returns 0 for success.
4375//
Jamie Madillb98c3a82015-07-23 14:26:04 -04004376int PaParseStrings(size_t count,
4377 const char *const string[],
4378 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05304379 TParseContext *context)
4380{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004381 if ((count == 0) || (string == NULL))
4382 return 1;
4383
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004384 if (glslang_initialize(context))
4385 return 1;
4386
alokp@chromium.org408c45e2012-04-05 15:54:43 +00004387 int error = glslang_scan(count, string, length, context);
4388 if (!error)
4389 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004390
alokp@chromium.org73bc2982012-06-19 18:48:05 +00004391 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00004392
alokp@chromium.org6b495712012-06-29 00:06:58 +00004393 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004394}