blob: 2e2c27e0cc0776c5e9277235e0b76b3a907f9ec5 [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
Martin Radev70866b82016-07-22 15:27:42 +03001584TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301585 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001586{
Olli Etuaho613b9592016-09-05 12:05:53 +03001587 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001588
Martin Radev70866b82016-07-22 15:27:42 +03001589 TPublicType returnType = typeSpecifier;
1590 returnType.qualifier = typeQualifier.qualifier;
1591 returnType.invariant = typeQualifier.invariant;
1592 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03001593 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03001594 returnType.precision = typeSpecifier.precision;
1595
1596 if (typeQualifier.precision != EbpUndefined)
1597 {
1598 returnType.precision = typeQualifier.precision;
1599 }
1600
Martin Radev4a9cd802016-09-01 16:51:51 +03001601 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
1602 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03001603
Martin Radev4a9cd802016-09-01 16:51:51 +03001604 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
1605 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03001606
Martin Radev4a9cd802016-09-01 16:51:51 +03001607 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03001608
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001609 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001610 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001611 if (typeSpecifier.array)
1612 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001613 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001614 returnType.clearArrayness();
1615 }
1616
Martin Radev70866b82016-07-22 15:27:42 +03001617 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001618 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001619 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001620 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001621 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001622 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001623
Martin Radev70866b82016-07-22 15:27:42 +03001624 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001625 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001626 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001627 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001628 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001629 }
1630 }
1631 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001632 {
Martin Radev70866b82016-07-22 15:27:42 +03001633 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03001634 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001635 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03001636 }
Martin Radev70866b82016-07-22 15:27:42 +03001637 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
1638 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001639 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001640 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
1641 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001642 }
Martin Radev70866b82016-07-22 15:27:42 +03001643 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03001644 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001645 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03001646 "in");
Martin Radev802abe02016-08-04 17:48:32 +03001647 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001648 }
1649
1650 return returnType;
1651}
1652
Olli Etuaho856c4972016-08-08 11:38:39 +03001653void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
1654 const TPublicType &type,
1655 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03001656{
1657 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03001658 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03001659 {
1660 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001661 }
1662
1663 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
1664 switch (qualifier)
1665 {
1666 case EvqVertexIn:
1667 // ESSL 3.00 section 4.3.4
1668 if (type.array)
1669 {
1670 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001671 }
1672 // Vertex inputs with a struct type are disallowed in singleDeclarationErrorCheck
1673 return;
1674 case EvqFragmentOut:
1675 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03001676 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03001677 {
1678 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001679 }
1680 // Fragment outputs with a struct type are disallowed in singleDeclarationErrorCheck
1681 return;
1682 default:
1683 break;
1684 }
1685
1686 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
1687 // restrictions.
1688 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03001689 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
1690 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03001691 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
1692 {
1693 error(qualifierLocation, "must use 'flat' interpolation here",
1694 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001695 }
1696
Martin Radev4a9cd802016-09-01 16:51:51 +03001697 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03001698 {
1699 // ESSL 3.00 sections 4.3.4 and 4.3.6.
1700 // These restrictions are only implied by the ESSL 3.00 spec, but
1701 // the ESSL 3.10 spec lists these restrictions explicitly.
1702 if (type.array)
1703 {
1704 error(qualifierLocation, "cannot be an array of structures",
1705 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001706 }
1707 if (type.isStructureContainingArrays())
1708 {
1709 error(qualifierLocation, "cannot be a structure containing an array",
1710 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001711 }
1712 if (type.isStructureContainingType(EbtStruct))
1713 {
1714 error(qualifierLocation, "cannot be a structure containing a structure",
1715 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001716 }
1717 if (type.isStructureContainingType(EbtBool))
1718 {
1719 error(qualifierLocation, "cannot be a structure containing a bool",
1720 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001721 }
1722 }
1723}
1724
Martin Radev2cc85b32016-08-05 16:22:53 +03001725void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
1726{
1727 if (qualifier.getType() == QtStorage)
1728 {
1729 const TStorageQualifierWrapper &storageQualifier =
1730 static_cast<const TStorageQualifierWrapper &>(qualifier);
1731 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
1732 !symbolTable.atGlobalLevel())
1733 {
1734 error(storageQualifier.getLine(),
1735 "Local variables can only use the const storage qualifier.",
1736 storageQualifier.getQualifierString().c_str());
1737 }
1738 }
1739}
1740
1741bool TParseContext::checkIsMemoryQualifierNotSpecified(const TMemoryQualifier &memoryQualifier,
1742 const TSourceLoc &location)
1743{
1744 if (memoryQualifier.readonly)
1745 {
1746 error(location, "Only allowed with images.", "readonly");
1747 return false;
1748 }
1749 if (memoryQualifier.writeonly)
1750 {
1751 error(location, "Only allowed with images.", "writeonly");
1752 return false;
1753 }
1754 return true;
1755}
1756
Olli Etuaho13389b62016-10-16 11:48:18 +01001757TIntermDeclaration *TParseContext::parseSingleDeclaration(
1758 TPublicType &publicType,
1759 const TSourceLoc &identifierOrTypeLocation,
1760 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001761{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001762 TType type(publicType);
1763 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
1764 mDirectiveHandler.pragma().stdgl.invariantAll)
1765 {
1766 TQualifier qualifier = type.getQualifier();
1767
1768 // The directive handler has already taken care of rejecting invalid uses of this pragma
1769 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
1770 // affected variable declarations:
1771 //
1772 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
1773 // elsewhere, in TranslatorGLSL.)
1774 //
1775 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
1776 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
1777 // the way this is currently implemented we have to enable this compiler option before
1778 // parsing the shader and determining the shading language version it uses. If this were
1779 // implemented as a post-pass, the workaround could be more targeted.
1780 //
1781 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
1782 // the specification, but there are desktop OpenGL drivers that expect that this is the
1783 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
1784 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
1785 {
1786 type.setInvariant(true);
1787 }
1788 }
1789
1790 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, type, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001791
Olli Etuahobab4c082015-04-24 16:38:49 +03001792 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03001793
Olli Etuahobab4c082015-04-24 16:38:49 +03001794 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1795
Olli Etuaho13389b62016-10-16 11:48:18 +01001796 TIntermDeclaration *declaration = new TIntermDeclaration();
1797 declaration->setLine(identifierOrTypeLocation);
1798
Olli Etuahobab4c082015-04-24 16:38:49 +03001799 if (emptyDeclaration)
1800 {
1801 if (publicType.isUnsizedArray())
1802 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001803 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1804 // error. It is assumed that this applies to empty declarations as well.
1805 error(identifierOrTypeLocation, "empty array declaration needs to specify a size",
1806 identifier.c_str());
Olli Etuahobab4c082015-04-24 16:38:49 +03001807 }
1808 }
1809 else
Jamie Madill60ed9812013-06-06 11:56:46 -04001810 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001811 singleDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001812
Olli Etuaho856c4972016-08-08 11:38:39 +03001813 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001814
Olli Etuaho2935c582015-04-08 14:32:06 +03001815 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001816 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04001817
1818 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01001819 {
Jamie Madill60ed9812013-06-06 11:56:46 -04001820 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01001821 }
Jamie Madill60ed9812013-06-06 11:56:46 -04001822 }
1823
Olli Etuaho13389b62016-10-16 11:48:18 +01001824 // We append the symbol even if the declaration is empty, mainly because of struct declarations
1825 // that may just declare a type.
1826 declaration->appendDeclarator(symbol);
1827
1828 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04001829}
1830
Olli Etuaho13389b62016-10-16 11:48:18 +01001831TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1832 const TSourceLoc &identifierLocation,
1833 const TString &identifier,
1834 const TSourceLoc &indexLocation,
1835 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001836{
Olli Etuahofa33d582015-04-09 14:33:12 +03001837 mDeferredSingleDeclarationErrorCheck = false;
1838
Olli Etuaho383b7912016-08-05 11:22:59 +03001839 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001840
Olli Etuaho856c4972016-08-08 11:38:39 +03001841 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001842
Olli Etuaho8a176262016-08-16 14:23:01 +03001843 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001844
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001845 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001846
Olli Etuaho856c4972016-08-08 11:38:39 +03001847 unsigned int size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001848 // Make the type an array even if size check failed.
1849 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1850 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001851
Olli Etuaho2935c582015-04-08 14:32:06 +03001852 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03001853 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04001854
Olli Etuaho13389b62016-10-16 11:48:18 +01001855 TIntermDeclaration *declaration = new TIntermDeclaration();
1856 declaration->setLine(identifierLocation);
1857
Olli Etuahoe7847b02015-03-16 11:56:12 +02001858 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001859 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01001860 {
Jamie Madill60ed9812013-06-06 11:56:46 -04001861 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01001862 declaration->appendDeclarator(symbol);
1863 }
Jamie Madill60ed9812013-06-06 11:56:46 -04001864
Olli Etuaho13389b62016-10-16 11:48:18 +01001865 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04001866}
1867
Olli Etuaho13389b62016-10-16 11:48:18 +01001868TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
1869 const TSourceLoc &identifierLocation,
1870 const TString &identifier,
1871 const TSourceLoc &initLocation,
1872 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001873{
Olli Etuahofa33d582015-04-09 14:33:12 +03001874 mDeferredSingleDeclarationErrorCheck = false;
1875
Olli Etuaho383b7912016-08-05 11:22:59 +03001876 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001877
Olli Etuaho13389b62016-10-16 11:48:18 +01001878 TIntermDeclaration *declaration = new TIntermDeclaration();
1879 declaration->setLine(identifierLocation);
1880
1881 TIntermBinary *initNode = nullptr;
1882 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04001883 {
Olli Etuaho13389b62016-10-16 11:48:18 +01001884 if (initNode)
1885 {
1886 declaration->appendDeclarator(initNode);
1887 }
Jamie Madill60ed9812013-06-06 11:56:46 -04001888 }
Olli Etuaho13389b62016-10-16 11:48:18 +01001889 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04001890}
1891
Olli Etuaho13389b62016-10-16 11:48:18 +01001892TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Jamie Madillb98c3a82015-07-23 14:26:04 -04001893 TPublicType &publicType,
1894 const TSourceLoc &identifierLocation,
1895 const TString &identifier,
1896 const TSourceLoc &indexLocation,
1897 TIntermTyped *indexExpression,
1898 const TSourceLoc &initLocation,
1899 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001900{
1901 mDeferredSingleDeclarationErrorCheck = false;
1902
Olli Etuaho383b7912016-08-05 11:22:59 +03001903 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001904
Olli Etuaho8a176262016-08-16 14:23:01 +03001905 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001906
1907 TPublicType arrayType(publicType);
1908
Olli Etuaho856c4972016-08-08 11:38:39 +03001909 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001910 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
1911 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03001912 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001913 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001914 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001915 }
1916 // Make the type an array even if size check failed.
1917 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1918 arrayType.setArraySize(size);
1919
Olli Etuaho13389b62016-10-16 11:48:18 +01001920 TIntermDeclaration *declaration = new TIntermDeclaration();
1921 declaration->setLine(identifierLocation);
1922
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001923 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01001924 TIntermBinary *initNode = nullptr;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001925 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1926 {
Olli Etuaho13389b62016-10-16 11:48:18 +01001927 if (initNode)
1928 {
1929 declaration->appendDeclarator(initNode);
1930 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001931 }
Olli Etuaho13389b62016-10-16 11:48:18 +01001932
1933 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001934}
1935
Martin Radev70866b82016-07-22 15:27:42 +03001936TIntermAggregate *TParseContext::parseInvariantDeclaration(
1937 const TTypeQualifierBuilder &typeQualifierBuilder,
1938 const TSourceLoc &identifierLoc,
1939 const TString *identifier,
1940 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04001941{
Olli Etuaho613b9592016-09-05 12:05:53 +03001942 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04001943
Martin Radev70866b82016-07-22 15:27:42 +03001944 if (!typeQualifier.invariant)
1945 {
1946 error(identifierLoc, "Expected invariant", identifier->c_str());
1947 return nullptr;
1948 }
1949 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
1950 {
1951 return nullptr;
1952 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04001953 if (!symbol)
1954 {
1955 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001956 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001957 }
Martin Radev70866b82016-07-22 15:27:42 +03001958 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04001959 {
Martin Radev70866b82016-07-22 15:27:42 +03001960 error(identifierLoc, "invariant declaration specifies qualifier",
1961 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04001962 }
Martin Radev70866b82016-07-22 15:27:42 +03001963 if (typeQualifier.precision != EbpUndefined)
1964 {
1965 error(identifierLoc, "invariant declaration specifies precision",
1966 getPrecisionString(typeQualifier.precision));
1967 }
1968 if (!typeQualifier.layoutQualifier.isEmpty())
1969 {
1970 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
1971 }
1972
1973 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1974 ASSERT(variable);
1975 const TType &type = variable->getType();
1976
1977 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
1978 typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001979 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03001980
1981 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
1982
1983 TIntermSymbol *intermSymbol =
1984 intermediate.addSymbol(variable->getUniqueId(), *identifier, type, identifierLoc);
1985
Olli Etuaho32db19b2016-10-04 14:43:16 +01001986 TIntermAggregate *aggregate = TIntermediate::MakeAggregate(intermSymbol, identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03001987 aggregate->setOp(EOpInvariantDeclaration);
1988 return aggregate;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001989}
1990
Olli Etuaho13389b62016-10-16 11:48:18 +01001991void TParseContext::parseDeclarator(TPublicType &publicType,
1992 const TSourceLoc &identifierLocation,
1993 const TString &identifier,
1994 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04001995{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001996 // If the declaration starting this declarator list was empty (example: int,), some checks were
1997 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001998 if (mDeferredSingleDeclarationErrorCheck)
1999 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002000 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002001 mDeferredSingleDeclarationErrorCheck = false;
2002 }
2003
Olli Etuaho856c4972016-08-08 11:38:39 +03002004 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002005
Olli Etuaho856c4972016-08-08 11:38:39 +03002006 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002007
Olli Etuaho2935c582015-04-08 14:32:06 +03002008 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002009 declareVariable(identifierLocation, identifier, TType(publicType), &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002010
Jamie Madillb98c3a82015-07-23 14:26:04 -04002011 TIntermSymbol *symbol =
2012 intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002013 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01002014 {
Jamie Madill502d66f2013-06-20 11:55:52 -04002015 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01002016 declarationOut->appendDeclarator(symbol);
2017 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002018}
2019
Olli Etuaho13389b62016-10-16 11:48:18 +01002020void TParseContext::parseArrayDeclarator(TPublicType &publicType,
2021 const TSourceLoc &identifierLocation,
2022 const TString &identifier,
2023 const TSourceLoc &arrayLocation,
2024 TIntermTyped *indexExpression,
2025 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002026{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002027 // If the declaration starting this declarator list was empty (example: int,), some checks were
2028 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002029 if (mDeferredSingleDeclarationErrorCheck)
2030 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002031 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002032 mDeferredSingleDeclarationErrorCheck = false;
2033 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002034
Olli Etuaho856c4972016-08-08 11:38:39 +03002035 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002036
Olli Etuaho856c4972016-08-08 11:38:39 +03002037 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002038
Olli Etuaho8a176262016-08-16 14:23:01 +03002039 if (checkIsValidTypeAndQualifierForArray(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002040 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02002041 TType arrayType = TType(publicType);
Olli Etuaho856c4972016-08-08 11:38:39 +03002042 unsigned int size = checkIsValidArraySize(arrayLocation, indexExpression);
Olli Etuaho693c9aa2015-04-07 17:50:36 +03002043 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002044
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002045 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002046 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002047
Jamie Madillb98c3a82015-07-23 14:26:04 -04002048 TIntermSymbol *symbol =
2049 intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002050 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002051 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002052
Olli Etuaho13389b62016-10-16 11:48:18 +01002053 declarationOut->appendDeclarator(symbol);
Jamie Madill502d66f2013-06-20 11:55:52 -04002054 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002055}
2056
Olli Etuaho13389b62016-10-16 11:48:18 +01002057void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2058 const TSourceLoc &identifierLocation,
2059 const TString &identifier,
2060 const TSourceLoc &initLocation,
2061 TIntermTyped *initializer,
2062 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002063{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002064 // If the declaration starting this declarator list was empty (example: int,), some checks were
2065 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002066 if (mDeferredSingleDeclarationErrorCheck)
2067 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002068 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002069 mDeferredSingleDeclarationErrorCheck = false;
2070 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002071
Olli Etuaho856c4972016-08-08 11:38:39 +03002072 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002073
Olli Etuaho13389b62016-10-16 11:48:18 +01002074 TIntermBinary *initNode = nullptr;
2075 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002076 {
2077 //
2078 // build the intermediate representation
2079 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002080 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002081 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002082 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002083 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002084 }
2085}
2086
Olli Etuaho13389b62016-10-16 11:48:18 +01002087void TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
2088 const TSourceLoc &identifierLocation,
2089 const TString &identifier,
2090 const TSourceLoc &indexLocation,
2091 TIntermTyped *indexExpression,
2092 const TSourceLoc &initLocation,
2093 TIntermTyped *initializer,
2094 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002095{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002096 // If the declaration starting this declarator list was empty (example: int,), some checks were
2097 // not performed.
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002098 if (mDeferredSingleDeclarationErrorCheck)
2099 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002100 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002101 mDeferredSingleDeclarationErrorCheck = false;
2102 }
2103
Olli Etuaho856c4972016-08-08 11:38:39 +03002104 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002105
Olli Etuaho8a176262016-08-16 14:23:01 +03002106 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002107
2108 TPublicType arrayType(publicType);
2109
Olli Etuaho856c4972016-08-08 11:38:39 +03002110 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002111 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2112 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002113 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002114 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002115 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002116 }
2117 // Make the type an array even if size check failed.
2118 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2119 arrayType.setArraySize(size);
2120
2121 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002122 TIntermBinary *initNode = nullptr;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002123 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
2124 {
2125 if (initNode)
2126 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002127 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002128 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002129 }
2130}
2131
Martin Radev70866b82016-07-22 15:27:42 +03002132void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002133{
Olli Etuaho613b9592016-09-05 12:05:53 +03002134 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002135 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002136
Martin Radev70866b82016-07-22 15:27:42 +03002137 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2138 typeQualifier.line);
2139
Jamie Madillc2128ff2016-07-04 10:26:17 -04002140 // It should never be the case, but some strange parser errors can send us here.
2141 if (layoutQualifier.isEmpty())
2142 {
2143 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002144 return;
2145 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002146
Martin Radev802abe02016-08-04 17:48:32 +03002147 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002148 {
Martin Radev802abe02016-08-04 17:48:32 +03002149 error(typeQualifier.line, "invalid combination:", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002150 return;
2151 }
2152
Martin Radev2cc85b32016-08-05 16:22:53 +03002153 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
2154
2155 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2156
Martin Radev802abe02016-08-04 17:48:32 +03002157 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002158 {
Martin Radev802abe02016-08-04 17:48:32 +03002159 if (mComputeShaderLocalSizeDeclared &&
2160 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2161 {
2162 error(typeQualifier.line, "Work group size does not match the previous declaration",
2163 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002164 return;
2165 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002166
Martin Radev802abe02016-08-04 17:48:32 +03002167 if (mShaderVersion < 310)
2168 {
2169 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002170 return;
2171 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002172
Martin Radev4c4c8e72016-08-04 12:25:34 +03002173 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03002174 {
2175 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002176 return;
2177 }
2178
2179 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
2180 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
2181
2182 const TConstantUnion *maxComputeWorkGroupSizeData =
2183 maxComputeWorkGroupSize->getConstPointer();
2184
2185 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
2186 {
2187 if (layoutQualifier.localSize[i] != -1)
2188 {
2189 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
2190 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
2191 if (mComputeShaderLocalSize[i] < 1 ||
2192 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
2193 {
2194 std::stringstream errorMessageStream;
2195 errorMessageStream << "Value must be at least 1 and no greater than "
2196 << maxComputeWorkGroupSizeValue;
2197 const std::string &errorMessage = errorMessageStream.str();
2198
Martin Radev4c4c8e72016-08-04 12:25:34 +03002199 error(typeQualifier.line, "invalid value:", getWorkGroupSizeString(i),
Martin Radev802abe02016-08-04 17:48:32 +03002200 errorMessage.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03002201 return;
2202 }
2203 }
2204 }
2205
2206 mComputeShaderLocalSizeDeclared = true;
2207 }
2208 else
Jamie Madill1566ef72013-06-20 11:55:54 -04002209 {
Martin Radev802abe02016-08-04 17:48:32 +03002210
Olli Etuaho8a176262016-08-16 14:23:01 +03002211 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03002212 {
Martin Radev802abe02016-08-04 17:48:32 +03002213 return;
2214 }
2215
2216 if (typeQualifier.qualifier != EvqUniform)
2217 {
2218 error(typeQualifier.line, "invalid qualifier:",
2219 getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
Martin Radev802abe02016-08-04 17:48:32 +03002220 return;
2221 }
2222
2223 if (mShaderVersion < 300)
2224 {
2225 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
2226 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002227 return;
2228 }
2229
Olli Etuaho856c4972016-08-08 11:38:39 +03002230 checkLocationIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002231
2232 if (layoutQualifier.matrixPacking != EmpUnspecified)
2233 {
2234 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
2235 }
2236
2237 if (layoutQualifier.blockStorage != EbsUnspecified)
2238 {
2239 mDefaultBlockStorage = layoutQualifier.blockStorage;
2240 }
Jamie Madill1566ef72013-06-20 11:55:54 -04002241 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002242}
2243
Olli Etuaho476197f2016-10-11 13:59:08 +01002244TIntermAggregate *TParseContext::addFunctionPrototypeDeclaration(const TFunction &parsedFunction,
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002245 const TSourceLoc &location)
2246{
Olli Etuaho476197f2016-10-11 13:59:08 +01002247 // Note: function found from the symbol table could be the same as parsedFunction if this is the
2248 // first declaration. Either way the instance in the symbol table is used to track whether the
2249 // function is declared multiple times.
2250 TFunction *function = static_cast<TFunction *>(
2251 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
2252 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02002253 {
2254 // ESSL 1.00.17 section 4.2.7.
2255 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
2256 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02002257 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002258 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02002259
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002260 TIntermAggregate *prototype = new TIntermAggregate;
Olli Etuaho476197f2016-10-11 13:59:08 +01002261 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
2262 // point to the data that already exists in the symbol table.
2263 prototype->setType(function->getReturnType());
Olli Etuahobd674552016-10-06 13:28:42 +01002264 prototype->getFunctionSymbolInfo()->setFromFunction(*function);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002265
Olli Etuaho476197f2016-10-11 13:59:08 +01002266 for (size_t i = 0; i < function->getParamCount(); i++)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002267 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002268 const TConstParameter &param = function->getParam(i);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002269 if (param.name != 0)
2270 {
2271 TVariable variable(param.name, *param.type);
2272
2273 TIntermSymbol *paramSymbol = intermediate.addSymbol(
2274 variable.getUniqueId(), variable.getName(), variable.getType(), location);
2275 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
2276 }
2277 else
2278 {
2279 TIntermSymbol *paramSymbol = intermediate.addSymbol(0, "", *param.type, location);
2280 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
2281 }
2282 }
2283
2284 prototype->setOp(EOpPrototype);
2285
2286 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002287
2288 if (!symbolTable.atGlobalLevel())
2289 {
2290 // ESSL 3.00.4 section 4.2.4.
2291 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002292 }
2293
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002294 return prototype;
2295}
2296
Olli Etuaho336b1472016-10-05 16:37:55 +01002297TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
2298 const TFunction &function,
2299 TIntermAggregate *functionParameters,
2300 TIntermBlock *functionBody,
2301 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002302{
Olli Etuahof51fdd22016-10-03 10:03:40 +01002303 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002304 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
2305 {
2306 error(location, "function does not return a value:", "", function.getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002307 }
2308
Olli Etuahof51fdd22016-10-03 10:03:40 +01002309 if (functionBody == nullptr)
2310 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01002311 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002312 functionBody->setLine(location);
2313 }
Olli Etuaho336b1472016-10-05 16:37:55 +01002314 TIntermFunctionDefinition *functionNode =
2315 new TIntermFunctionDefinition(function.getReturnType(), functionParameters, functionBody);
2316 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01002317
Olli Etuahobd674552016-10-06 13:28:42 +01002318 functionNode->getFunctionSymbolInfo()->setFromFunction(function);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002319
2320 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002321 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002322}
2323
Olli Etuaho476197f2016-10-11 13:59:08 +01002324void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
2325 TFunction **function,
2326 TIntermAggregate **aggregateOut)
Jamie Madill185fb402015-06-12 15:48:48 -04002327{
Olli Etuaho476197f2016-10-11 13:59:08 +01002328 ASSERT(function);
2329 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002330 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01002331 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002332
2333 if (builtIn)
2334 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002335 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002336 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002337 else
Jamie Madill185fb402015-06-12 15:48:48 -04002338 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002339 TFunction *prevDec = static_cast<TFunction *>(
2340 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
2341
2342 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
2343 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
2344 // occurance.
2345 if (*function != prevDec)
2346 {
2347 // Swap the parameters of the previous declaration to the parameters of the function
2348 // definition (parameter names may differ).
2349 prevDec->swapParameters(**function);
2350
2351 // The function definition will share the same symbol as any previous declaration.
2352 *function = prevDec;
2353 }
2354
2355 if ((*function)->isDefined())
2356 {
2357 error(location, "function already has a body", (*function)->getName().c_str());
2358 }
2359
2360 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04002361 }
Jamie Madill185fb402015-06-12 15:48:48 -04002362
2363 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuaho476197f2016-10-11 13:59:08 +01002364 if ((*function)->getName() == "main")
Jamie Madill185fb402015-06-12 15:48:48 -04002365 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002366 if ((*function)->getParamCount() > 0)
Jamie Madill185fb402015-06-12 15:48:48 -04002367 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002368 error(location, "function cannot take any parameter(s)",
2369 (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002370 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002371 if ((*function)->getReturnType().getBasicType() != EbtVoid)
Jamie Madill185fb402015-06-12 15:48:48 -04002372 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002373 error(location, "", (*function)->getReturnType().getBasicString(),
Jamie Madillb98c3a82015-07-23 14:26:04 -04002374 "main function cannot return a value");
Jamie Madill185fb402015-06-12 15:48:48 -04002375 }
2376 }
2377
2378 //
2379 // Remember the return type for later checking for RETURN statements.
2380 //
Olli Etuaho476197f2016-10-11 13:59:08 +01002381 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002382 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04002383
2384 //
2385 // Insert parameters into the symbol table.
2386 // If the parameter has no name, it's not an error, just don't insert it
2387 // (could be used for unused args).
2388 //
2389 // Also, accumulate the list of parameters into the HIL, so lower level code
2390 // knows where to find parameters.
2391 //
2392 TIntermAggregate *paramNodes = new TIntermAggregate;
Olli Etuaho476197f2016-10-11 13:59:08 +01002393 for (size_t i = 0; i < (*function)->getParamCount(); i++)
Jamie Madill185fb402015-06-12 15:48:48 -04002394 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002395 const TConstParameter &param = (*function)->getParam(i);
Jamie Madill185fb402015-06-12 15:48:48 -04002396 if (param.name != 0)
2397 {
2398 TVariable *variable = new TVariable(param.name, *param.type);
2399 //
2400 // Insert the parameters with name in the symbol table.
2401 //
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002402 if (!symbolTable.declare(variable))
2403 {
Jamie Madill185fb402015-06-12 15:48:48 -04002404 error(location, "redefinition", variable->getName().c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002405 paramNodes = intermediate.growAggregate(
2406 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
2407 continue;
Jamie Madill185fb402015-06-12 15:48:48 -04002408 }
2409
2410 //
2411 // Add the parameter to the HIL
2412 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002413 TIntermSymbol *symbol = intermediate.addSymbol(
2414 variable->getUniqueId(), variable->getName(), variable->getType(), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002415
2416 paramNodes = intermediate.growAggregate(paramNodes, symbol, location);
2417 }
2418 else
2419 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002420 paramNodes = intermediate.growAggregate(
2421 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002422 }
2423 }
2424 intermediate.setAggregateOperator(paramNodes, EOpParameters, location);
2425 *aggregateOut = paramNodes;
2426 setLoopNestingLevel(0);
2427}
2428
Jamie Madillb98c3a82015-07-23 14:26:04 -04002429TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002430{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002431 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002432 // We don't know at this point whether this is a function definition or a prototype.
2433 // The definition production code will check for redefinitions.
2434 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002435 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002436 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
2437 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002438 //
2439 TFunction *prevDec =
2440 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302441
2442 if (getShaderVersion() >= 300 && symbolTable.hasUnmangledBuiltIn(function->getName().c_str()))
2443 {
2444 // With ESSL 3.00, names of built-in functions cannot be redeclared as functions.
2445 // Therefore overloading or redefining builtin functions is an error.
2446 error(location, "Name of a built-in function cannot be redeclared as function",
2447 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302448 }
2449 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002450 {
2451 if (prevDec->getReturnType() != function->getReturnType())
2452 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002453 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002454 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04002455 }
2456 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2457 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002458 if (prevDec->getParam(i).type->getQualifier() !=
2459 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002460 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002461 error(location,
2462 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002463 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04002464 }
2465 }
2466 }
2467
2468 //
2469 // Check for previously declared variables using the same name.
2470 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002471 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002472 if (prevSym)
2473 {
2474 if (!prevSym->isFunction())
2475 {
2476 error(location, "redefinition", function->getName().c_str(), "function");
Jamie Madill185fb402015-06-12 15:48:48 -04002477 }
2478 }
2479 else
2480 {
2481 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01002482 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04002483 }
2484
2485 // We're at the inner scope level of the function's arguments and body statement.
2486 // Add the function prototype to the surrounding scope instead.
2487 symbolTable.getOuterLevel()->insert(function);
2488
2489 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002490 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2491 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002492 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2493 //
2494 return function;
2495}
2496
Olli Etuaho9de84a52016-06-14 17:36:01 +03002497TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
2498 const TString *name,
2499 const TSourceLoc &location)
2500{
2501 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
2502 {
2503 error(location, "no qualifiers allowed for function return",
2504 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03002505 }
2506 if (!type.layoutQualifier.isEmpty())
2507 {
2508 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03002509 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002510 // make sure a sampler or an image is not involved as well...
Martin Radev4a9cd802016-09-01 16:51:51 +03002511 checkIsNotSampler(location, type.typeSpecifierNonArray,
2512 "samplers can't be function return values");
Martin Radev2cc85b32016-08-05 16:22:53 +03002513 checkIsNotImage(location, type.typeSpecifierNonArray, "images can't be function return values");
Olli Etuahoe29324f2016-06-15 10:58:03 +03002514 if (mShaderVersion < 300)
2515 {
2516 // Array return values are forbidden, but there's also no valid syntax for declaring array
2517 // return values in ESSL 1.00.
2518 ASSERT(type.arraySize == 0 || mDiagnostics.numErrors() > 0);
2519
2520 if (type.isStructureContainingArrays())
2521 {
2522 // ESSL 1.00.17 section 6.1 Function Definitions
2523 error(location, "structures containing arrays can't be function return values",
2524 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03002525 }
2526 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03002527
2528 // Add the function as a prototype after parsing it (we do not support recursion)
2529 return new TFunction(name, new TType(type));
2530}
2531
Jamie Madill06145232015-05-13 13:10:01 -04002532TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002533{
Jamie Madill06145232015-05-13 13:10:01 -04002534 TPublicType publicType = publicTypeIn;
Martin Radev4a9cd802016-09-01 16:51:51 +03002535 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02002536 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002537 error(publicType.getLine(), "constructor can't be a structure definition",
2538 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02002539 }
2540
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002541 TOperator op = EOpNull;
Martin Radev4a9cd802016-09-01 16:51:51 +03002542 if (publicType.getUserDef())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002543 {
2544 op = EOpConstructStruct;
2545 }
2546 else
2547 {
Geoff Lang156d7192016-07-21 16:11:00 -04002548 op = sh::TypeToConstructorOperator(TType(publicType));
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002549 if (op == EOpNull)
2550 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002551 error(publicType.getLine(), "cannot construct this type",
2552 getBasicString(publicType.getBasicType()));
2553 publicType.setBasicType(EbtFloat);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002554 op = EOpConstructFloat;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002555 }
2556 }
2557
2558 TString tempString;
Dmitry Skiba7f17a502015-06-22 15:08:39 -07002559 const TType *type = new TType(publicType);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002560 return new TFunction(&tempString, type, op);
2561}
2562
Jamie Madillb98c3a82015-07-23 14:26:04 -04002563// This function is used to test for the correctness of the parameters passed to various constructor
2564// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002565//
Olli Etuaho856c4972016-08-08 11:38:39 +03002566// 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 +00002567//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002568TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002569 TOperator op,
2570 TFunction *fnCall,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302571 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002572{
Olli Etuaho856c4972016-08-08 11:38:39 +03002573 TType type = fnCall->getReturnType();
2574 if (type.isUnsizedArray())
2575 {
2576 type.setArraySize(static_cast<unsigned int>(fnCall->getParamCount()));
2577 }
2578 bool constType = true;
2579 for (size_t i = 0; i < fnCall->getParamCount(); ++i)
2580 {
2581 const TConstParameter &param = fnCall->getParam(i);
2582 if (param.type->getQualifier() != EvqConst)
2583 constType = false;
2584 }
2585 if (constType)
2586 type.setQualifier(EvqConst);
2587
Olli Etuaho8a176262016-08-16 14:23:01 +03002588 if (!checkConstructorArguments(line, arguments, *fnCall, op, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03002589 {
2590 TIntermTyped *dummyNode = intermediate.setAggregateOperator(nullptr, op, line);
2591 dummyNode->setType(type);
2592 return dummyNode;
2593 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002594 TIntermAggregate *constructor = arguments->getAsAggregate();
2595 ASSERT(constructor != nullptr);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002596
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002597 // Turn the argument list itself into a constructor
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002598 constructor->setOp(op);
2599 constructor->setLine(line);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002600 ASSERT(constructor->isConstructor());
2601
2602 // Need to set type before setPrecisionFromChildren() because bool doesn't have precision.
Olli Etuaho856c4972016-08-08 11:38:39 +03002603 constructor->setType(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002604
Olli Etuaho21203702014-11-13 16:16:21 +02002605 // Structs should not be precision qualified, the individual members may be.
2606 // Built-in types on the other hand should be precision qualified.
2607 if (op != EOpConstructStruct)
2608 {
2609 constructor->setPrecisionFromChildren();
Olli Etuaho856c4972016-08-08 11:38:39 +03002610 type.setPrecision(constructor->getPrecision());
Olli Etuaho21203702014-11-13 16:16:21 +02002611 }
2612
Olli Etuaho856c4972016-08-08 11:38:39 +03002613 constructor->setType(type);
2614
Olli Etuahof119a262016-08-19 15:54:22 +03002615 TIntermTyped *constConstructor = intermediate.foldAggregateBuiltIn(constructor, &mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002616 if (constConstructor)
2617 {
2618 return constConstructor;
2619 }
2620
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002621 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002622}
2623
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002624//
2625// Interface/uniform blocks
2626//
Olli Etuaho13389b62016-10-16 11:48:18 +01002627TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03002628 const TTypeQualifierBuilder &typeQualifierBuilder,
2629 const TSourceLoc &nameLine,
2630 const TString &blockName,
2631 TFieldList *fieldList,
2632 const TString *instanceName,
2633 const TSourceLoc &instanceLine,
2634 TIntermTyped *arrayIndex,
2635 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002636{
Olli Etuaho856c4972016-08-08 11:38:39 +03002637 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002638
Olli Etuaho613b9592016-09-05 12:05:53 +03002639 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03002640
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002641 if (typeQualifier.qualifier != EvqUniform)
2642 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302643 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
2644 "interface blocks must be uniform");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002645 }
2646
Martin Radev70866b82016-07-22 15:27:42 +03002647 if (typeQualifier.invariant)
2648 {
2649 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
2650 }
2651
Martin Radev2cc85b32016-08-05 16:22:53 +03002652 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
2653
Jamie Madill099c0f32013-06-20 11:55:52 -04002654 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03002655 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002656
Jamie Madill099c0f32013-06-20 11:55:52 -04002657 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2658 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002659 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002660 }
2661
Jamie Madill1566ef72013-06-20 11:55:54 -04002662 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2663 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002664 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002665 }
2666
Olli Etuaho856c4972016-08-08 11:38:39 +03002667 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002668
Martin Radev2cc85b32016-08-05 16:22:53 +03002669 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
2670
Arun Patole7e7e68d2015-05-22 12:02:25 +05302671 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2672 if (!symbolTable.declare(blockNameSymbol))
2673 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002674 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002675 }
2676
Jamie Madill98493dd2013-07-08 14:39:03 -04002677 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302678 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2679 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002680 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302681 TType *fieldType = field->type();
2682 if (IsSampler(fieldType->getBasicType()))
2683 {
2684 error(field->line(), "unsupported type", fieldType->getBasicString(),
2685 "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002686 }
2687
Martin Radev2cc85b32016-08-05 16:22:53 +03002688 if (IsImage(fieldType->getBasicType()))
2689 {
2690 error(field->line(), "unsupported type", fieldType->getBasicString(),
2691 "image types are not allowed in interface blocks");
2692 }
2693
Jamie Madill98493dd2013-07-08 14:39:03 -04002694 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002695 switch (qualifier)
2696 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002697 case EvqGlobal:
2698 case EvqUniform:
2699 break;
2700 default:
2701 error(field->line(), "invalid qualifier on interface block member",
2702 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04002703 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002704 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002705
Martin Radev70866b82016-07-22 15:27:42 +03002706 if (fieldType->isInvariant())
2707 {
2708 error(field->line(), "invalid qualifier on interface block member", "invariant");
2709 }
2710
Jamie Madilla5efff92013-06-06 11:56:47 -04002711 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002712 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03002713 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
Jamie Madill099c0f32013-06-20 11:55:52 -04002714
Jamie Madill98493dd2013-07-08 14:39:03 -04002715 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002716 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002717 error(field->line(), "invalid layout qualifier:",
2718 getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04002719 }
2720
Jamie Madill98493dd2013-07-08 14:39:03 -04002721 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002722 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002723 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002724 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002725 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002726 {
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002727 warning(field->line(), "extraneous layout qualifier:",
Jamie Madillb98c3a82015-07-23 14:26:04 -04002728 getMatrixPackingString(fieldLayoutQualifier.matrixPacking),
2729 "only has an effect on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04002730 }
2731
Jamie Madill98493dd2013-07-08 14:39:03 -04002732 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002733 }
2734
Jamie Madill98493dd2013-07-08 14:39:03 -04002735 // add array index
Olli Etuaho856c4972016-08-08 11:38:39 +03002736 unsigned int arraySize = 0;
2737 if (arrayIndex != nullptr)
Jamie Madill98493dd2013-07-08 14:39:03 -04002738 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002739 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04002740 }
2741
Jamie Madillb98c3a82015-07-23 14:26:04 -04002742 TInterfaceBlock *interfaceBlock =
2743 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2744 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
2745 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002746
2747 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04002748 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002749
Jamie Madill98493dd2013-07-08 14:39:03 -04002750 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002751 {
2752 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002753 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2754 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002755 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302756 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002757
2758 // set parent pointer of the field variable
2759 fieldType->setInterfaceBlock(interfaceBlock);
2760
Arun Patole7e7e68d2015-05-22 12:02:25 +05302761 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002762 fieldVariable->setQualifier(typeQualifier.qualifier);
2763
Arun Patole7e7e68d2015-05-22 12:02:25 +05302764 if (!symbolTable.declare(fieldVariable))
2765 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002766 error(field->line(), "redefinition", field->name().c_str(),
2767 "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002768 }
2769 }
2770 }
2771 else
2772 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002773 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03002774
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002775 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302776 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002777 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002778
Arun Patole7e7e68d2015-05-22 12:02:25 +05302779 if (!symbolTable.declare(instanceTypeDef))
2780 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002781 error(instanceLine, "redefinition", instanceName->c_str(),
2782 "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002783 }
2784
Jamie Madillb98c3a82015-07-23 14:26:04 -04002785 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002786 symbolName = instanceTypeDef->getName();
2787 }
2788
Olli Etuaho13389b62016-10-16 11:48:18 +01002789 TIntermSymbol *blockSymbol =
2790 intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line);
2791 TIntermDeclaration *declaration = new TIntermDeclaration();
2792 declaration->appendDeclarator(blockSymbol);
2793 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04002794
2795 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01002796 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002797}
2798
Olli Etuaho383b7912016-08-05 11:22:59 +03002799void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002800{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002801 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002802
2803 // Embedded structure definitions are not supported per GLSL ES spec.
2804 // They aren't allowed in GLSL either, but we need to detect this here
2805 // so we don't rely on the GLSL compiler to catch it.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302806 if (mStructNestingLevel > 1)
2807 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002808 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002809 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002810}
2811
2812void TParseContext::exitStructDeclaration()
2813{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002814 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002815}
2816
Olli Etuaho8a176262016-08-16 14:23:01 +03002817void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002818{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302819 if (!IsWebGLBasedSpec(mShaderSpec))
2820 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002821 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002822 }
2823
Arun Patole7e7e68d2015-05-22 12:02:25 +05302824 if (field.type()->getBasicType() != EbtStruct)
2825 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002826 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002827 }
2828
2829 // We're already inside a structure definition at this point, so add
2830 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302831 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2832 {
Jamie Madill41a49272014-03-18 16:10:13 -04002833 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002834 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
2835 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04002836 std::string reason = reasonStream.str();
2837 error(line, reason.c_str(), field.name().c_str(), "");
Olli Etuaho8a176262016-08-16 14:23:01 +03002838 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002839 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002840}
2841
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002842//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002843// Parse an array index expression
2844//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002845TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
2846 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302847 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002848{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002849 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2850 {
2851 if (baseExpression->getAsSymbolNode())
2852 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302853 error(location, " left of '[' is not of type array, matrix, or vector ",
2854 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002855 }
2856 else
2857 {
2858 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2859 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002860
2861 TConstantUnion *unionArray = new TConstantUnion[1];
2862 unionArray->setFConst(0.0f);
2863 return intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst),
2864 location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002865 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002866
Jamie Madill21c1e452014-12-29 11:33:41 -05002867 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2868
Olli Etuaho36b05142015-11-12 13:10:42 +02002869 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
2870 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
2871 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
2872 // index is a constant expression.
2873 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
2874 {
2875 if (baseExpression->isInterfaceBlock())
2876 {
2877 error(
2878 location, "", "[",
2879 "array indexes for interface blocks arrays must be constant integral expressions");
Olli Etuaho36b05142015-11-12 13:10:42 +02002880 }
2881 else if (baseExpression->getQualifier() == EvqFragmentOut)
2882 {
2883 error(location, "", "[",
2884 "array indexes for fragment outputs must be constant integral expressions");
Olli Etuaho36b05142015-11-12 13:10:42 +02002885 }
Olli Etuaho3e960462015-11-12 15:58:39 +02002886 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
2887 {
2888 error(location, "", "[", "array index for gl_FragData must be constant zero");
Olli Etuaho3e960462015-11-12 15:58:39 +02002889 }
Olli Etuaho36b05142015-11-12 13:10:42 +02002890 }
2891
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002892 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002893 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002894 // If an out-of-range index is not qualified as constant, the behavior in the spec is
2895 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
2896 // constant fold expressions that are not constant expressions). The most compatible way to
2897 // handle this case is to report a warning instead of an error and force the index to be in
2898 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002899 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Jamie Madill21c1e452014-12-29 11:33:41 -05002900 int index = indexConstantUnion->getIConst(0);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002901
2902 int safeIndex = -1;
2903
2904 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04002905 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002906 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03002907 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002908 if (mShaderSpec == SH_WEBGL2_SPEC)
2909 {
2910 // Error has been already generated if index is not const.
2911 if (indexExpression->getQualifier() == EvqConst)
2912 {
2913 error(location, "", "[",
2914 "array index for gl_FragData must be constant zero");
2915 }
2916 safeIndex = 0;
2917 }
2918 else if (!isExtensionEnabled("GL_EXT_draw_buffers"))
2919 {
2920 outOfRangeError(outOfRangeIndexIsError, location, "", "[",
2921 "array index for gl_FragData must be zero when "
2922 "GL_EXT_draw_buffers is disabled");
2923 safeIndex = 0;
2924 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03002925 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002926 // Only do generic out-of-range check if similar error hasn't already been reported.
2927 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03002928 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002929 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
2930 baseExpression->getArraySize(),
2931 "array index out of range", "[]");
2932 }
2933 }
2934 else if (baseExpression->isMatrix())
2935 {
2936 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03002937 baseExpression->getType().getCols(),
2938 "matrix field selection out of range", "[]");
Jamie Madill7164cf42013-07-08 13:30:59 -04002939 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002940 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04002941 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002942 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
2943 baseExpression->getType().getNominalSize(),
2944 "vector field selection out of range", "[]");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002945 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002946
2947 ASSERT(safeIndex >= 0);
2948 // Data of constant unions can't be changed, because it may be shared with other
2949 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
2950 // sanitized object.
2951 if (safeIndex != index)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002952 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002953 TConstantUnion *safeConstantUnion = new TConstantUnion();
2954 safeConstantUnion->setIConst(safeIndex);
2955 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002956 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002957
2958 return intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location,
2959 &mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002960 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002961 else
2962 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002963 return intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location,
2964 &mDiagnostics);
Jamie Madill7164cf42013-07-08 13:30:59 -04002965 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002966}
2967
Olli Etuaho90892fb2016-07-14 14:44:51 +03002968int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
2969 const TSourceLoc &location,
2970 int index,
2971 int arraySize,
2972 const char *reason,
2973 const char *token)
2974{
2975 if (index >= arraySize || index < 0)
2976 {
2977 std::stringstream extraInfoStream;
2978 extraInfoStream << "'" << index << "'";
2979 std::string extraInfo = extraInfoStream.str();
2980 outOfRangeError(outOfRangeIndexIsError, location, reason, token, extraInfo.c_str());
2981 if (index < 0)
2982 {
2983 return 0;
2984 }
2985 else
2986 {
2987 return arraySize - 1;
2988 }
2989 }
2990 return index;
2991}
2992
Jamie Madillb98c3a82015-07-23 14:26:04 -04002993TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
2994 const TSourceLoc &dotLocation,
2995 const TString &fieldString,
2996 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002997{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002998 if (baseExpression->isArray())
2999 {
3000 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003001 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003002 }
3003
3004 if (baseExpression->isVector())
3005 {
3006 TVectorFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003007 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields,
3008 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003009 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003010 fields.num = 1;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003011 fields.offsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003012 }
3013
Olli Etuahob6fa0432016-09-28 16:28:05 +01003014 return TIntermediate::AddSwizzle(baseExpression, fields, dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003015 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003016 else if (baseExpression->getBasicType() == EbtStruct)
3017 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303018 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003019 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003020 {
3021 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003022 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003023 }
3024 else
3025 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003026 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003027 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003028 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003029 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003030 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003031 {
3032 fieldFound = true;
3033 break;
3034 }
3035 }
3036 if (fieldFound)
3037 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003038 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3039 index->setLine(fieldLocation);
3040 return intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index,
3041 dotLocation, &mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003042 }
3043 else
3044 {
3045 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003046 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003047 }
3048 }
3049 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003050 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003051 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303052 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003053 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003054 {
3055 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003056 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003057 }
3058 else
3059 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003060 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003061 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003062 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003063 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003064 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003065 {
3066 fieldFound = true;
3067 break;
3068 }
3069 }
3070 if (fieldFound)
3071 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003072 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3073 index->setLine(fieldLocation);
3074 return intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index,
3075 dotLocation, &mDiagnostics);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003076 }
3077 else
3078 {
3079 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003080 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003081 }
3082 }
3083 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003084 else
3085 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003086 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003087 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03003088 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303089 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003090 }
3091 else
3092 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303093 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003094 " field selection requires structure, vector, or interface block on left hand "
3095 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303096 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003097 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003098 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003099 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003100}
3101
Jamie Madillb98c3a82015-07-23 14:26:04 -04003102TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3103 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003104{
Martin Radev802abe02016-08-04 17:48:32 +03003105 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003106
3107 if (qualifierType == "shared")
3108 {
Olli Etuahof0173152016-10-17 09:05:03 -07003109 if (IsWebGLBasedSpec(mShaderSpec))
3110 {
3111 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
3112 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003113 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003114 }
3115 else if (qualifierType == "packed")
3116 {
Olli Etuahof0173152016-10-17 09:05:03 -07003117 if (IsWebGLBasedSpec(mShaderSpec))
3118 {
3119 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
3120 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003121 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003122 }
3123 else if (qualifierType == "std140")
3124 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003125 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003126 }
3127 else if (qualifierType == "row_major")
3128 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003129 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003130 }
3131 else if (qualifierType == "column_major")
3132 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003133 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003134 }
3135 else if (qualifierType == "location")
3136 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003137 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
3138 "location requires an argument");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003139 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003140 else if (qualifierType == "rgba32f")
3141 {
3142 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3143 qualifier.imageInternalFormat = EiifRGBA32F;
3144 }
3145 else if (qualifierType == "rgba16f")
3146 {
3147 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3148 qualifier.imageInternalFormat = EiifRGBA16F;
3149 }
3150 else if (qualifierType == "r32f")
3151 {
3152 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3153 qualifier.imageInternalFormat = EiifR32F;
3154 }
3155 else if (qualifierType == "rgba8")
3156 {
3157 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3158 qualifier.imageInternalFormat = EiifRGBA8;
3159 }
3160 else if (qualifierType == "rgba8_snorm")
3161 {
3162 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3163 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
3164 }
3165 else if (qualifierType == "rgba32i")
3166 {
3167 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3168 qualifier.imageInternalFormat = EiifRGBA32I;
3169 }
3170 else if (qualifierType == "rgba16i")
3171 {
3172 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3173 qualifier.imageInternalFormat = EiifRGBA16I;
3174 }
3175 else if (qualifierType == "rgba8i")
3176 {
3177 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3178 qualifier.imageInternalFormat = EiifRGBA8I;
3179 }
3180 else if (qualifierType == "r32i")
3181 {
3182 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3183 qualifier.imageInternalFormat = EiifR32I;
3184 }
3185 else if (qualifierType == "rgba32ui")
3186 {
3187 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3188 qualifier.imageInternalFormat = EiifRGBA32UI;
3189 }
3190 else if (qualifierType == "rgba16ui")
3191 {
3192 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3193 qualifier.imageInternalFormat = EiifRGBA16UI;
3194 }
3195 else if (qualifierType == "rgba8ui")
3196 {
3197 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3198 qualifier.imageInternalFormat = EiifRGBA8UI;
3199 }
3200 else if (qualifierType == "r32ui")
3201 {
3202 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3203 qualifier.imageInternalFormat = EiifR32UI;
3204 }
3205
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003206 else
3207 {
3208 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003209 }
3210
Jamie Madilla5efff92013-06-06 11:56:47 -04003211 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003212}
3213
Martin Radev802abe02016-08-04 17:48:32 +03003214void TParseContext::parseLocalSize(const TString &qualifierType,
3215 const TSourceLoc &qualifierTypeLine,
3216 int intValue,
3217 const TSourceLoc &intValueLine,
3218 const std::string &intValueString,
3219 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03003220 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03003221{
Olli Etuaho856c4972016-08-08 11:38:39 +03003222 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03003223 if (intValue < 1)
3224 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03003225 std::string errorMessage = std::string(getWorkGroupSizeString(index)) + " must be positive";
Martin Radev802abe02016-08-04 17:48:32 +03003226 error(intValueLine, "out of range:", intValueString.c_str(), errorMessage.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003227 }
3228 (*localSize)[index] = intValue;
3229}
3230
Jamie Madillb98c3a82015-07-23 14:26:04 -04003231TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3232 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003233 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303234 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003235{
Martin Radev802abe02016-08-04 17:48:32 +03003236 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003237
Martin Radev802abe02016-08-04 17:48:32 +03003238 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003239
Martin Radev802abe02016-08-04 17:48:32 +03003240 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003241 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003242 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003243 if (intValue < 0)
3244 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003245 error(intValueLine, "out of range:", intValueString.c_str(),
3246 "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003247 }
3248 else
3249 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003250 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03003251 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003252 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003253 }
Martin Radev802abe02016-08-04 17:48:32 +03003254 else if (qualifierType == "local_size_x")
3255 {
3256 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
3257 &qualifier.localSize);
3258 }
3259 else if (qualifierType == "local_size_y")
3260 {
3261 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
3262 &qualifier.localSize);
3263 }
3264 else if (qualifierType == "local_size_z")
3265 {
3266 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
3267 &qualifier.localSize);
3268 }
3269 else
3270 {
3271 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003272 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003273
Jamie Madilla5efff92013-06-06 11:56:47 -04003274 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003275}
3276
Olli Etuaho613b9592016-09-05 12:05:53 +03003277TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
3278{
3279 return new TTypeQualifierBuilder(
3280 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
3281 mShaderVersion);
3282}
3283
Jamie Madillb98c3a82015-07-23 14:26:04 -04003284TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03003285 TLayoutQualifier rightQualifier,
3286 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003287{
Martin Radevc28888b2016-07-22 15:27:42 +03003288 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
3289 &mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003290}
3291
Martin Radev70866b82016-07-22 15:27:42 +03003292TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
3293 const TTypeQualifierBuilder &typeQualifierBuilder,
3294 TPublicType *typeSpecifier,
3295 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003296{
Olli Etuaho613b9592016-09-05 12:05:53 +03003297 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003298
Martin Radev70866b82016-07-22 15:27:42 +03003299 typeSpecifier->qualifier = typeQualifier.qualifier;
3300 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03003301 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03003302 typeSpecifier->invariant = typeQualifier.invariant;
3303 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05303304 {
Martin Radev70866b82016-07-22 15:27:42 +03003305 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003306 }
Martin Radev70866b82016-07-22 15:27:42 +03003307 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003308}
3309
Jamie Madillb98c3a82015-07-23 14:26:04 -04003310TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3311 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003312{
Martin Radev4a9cd802016-09-01 16:51:51 +03003313 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
3314 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03003315
Martin Radev4a9cd802016-09-01 16:51:51 +03003316 checkIsNonVoid(typeSpecifier.getLine(), (*fieldList)[0]->name(), typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003317
Martin Radev4a9cd802016-09-01 16:51:51 +03003318 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003319
Arun Patole7e7e68d2015-05-22 12:02:25 +05303320 for (unsigned int i = 0; i < fieldList->size(); ++i)
3321 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003322 //
3323 // Careful not to replace already known aspects of type, like array-ness
3324 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303325 TType *type = (*fieldList)[i]->type();
Martin Radev4a9cd802016-09-01 16:51:51 +03003326 type->setBasicType(typeSpecifier.getBasicType());
3327 type->setPrimarySize(typeSpecifier.getPrimarySize());
3328 type->setSecondarySize(typeSpecifier.getSecondarySize());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003329 type->setPrecision(typeSpecifier.precision);
3330 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003331 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
Martin Radev2cc85b32016-08-05 16:22:53 +03003332 type->setMemoryQualifier(typeSpecifier.memoryQualifier);
Martin Radev70866b82016-07-22 15:27:42 +03003333 type->setInvariant(typeSpecifier.invariant);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003334
3335 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303336 if (type->isArray())
3337 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003338 checkIsValidTypeForArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003339 }
3340 if (typeSpecifier.array)
Olli Etuaho856c4972016-08-08 11:38:39 +03003341 type->setArraySize(static_cast<unsigned int>(typeSpecifier.arraySize));
Martin Radev4a9cd802016-09-01 16:51:51 +03003342 if (typeSpecifier.getUserDef())
Arun Patole7e7e68d2015-05-22 12:02:25 +05303343 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003344 type->setStruct(typeSpecifier.getUserDef()->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003345 }
3346
Martin Radev4a9cd802016-09-01 16:51:51 +03003347 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*fieldList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003348 }
3349
Jamie Madill98493dd2013-07-08 14:39:03 -04003350 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003351}
3352
Martin Radev4a9cd802016-09-01 16:51:51 +03003353TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
3354 const TSourceLoc &nameLine,
3355 const TString *structName,
3356 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003357{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303358 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003359 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003360
Jamie Madill9b820842015-02-12 10:40:10 -05003361 // Store a bool in the struct if we're at global scope, to allow us to
3362 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05003363 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003364
Jamie Madill98493dd2013-07-08 14:39:03 -04003365 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003366 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003367 checkIsNotReserved(nameLine, *structName);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303368 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
3369 if (!symbolTable.declare(userTypeDef))
3370 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003371 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003372 }
3373 }
3374
3375 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04003376 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003377 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003378 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04003379 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003380 switch (qualifier)
3381 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003382 case EvqGlobal:
3383 case EvqTemporary:
3384 break;
3385 default:
3386 error(field.line(), "invalid qualifier on struct member",
3387 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003388 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003389 }
Martin Radev70866b82016-07-22 15:27:42 +03003390 if (field.type()->isInvariant())
3391 {
3392 error(field.line(), "invalid qualifier on struct member", "invariant");
3393 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003394 if (IsImage(field.type()->getBasicType()))
3395 {
3396 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
3397 }
3398
3399 checkIsMemoryQualifierNotSpecified(field.type()->getMemoryQualifier(), field.line());
Martin Radev70866b82016-07-22 15:27:42 +03003400
3401 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003402 }
3403
Martin Radev4a9cd802016-09-01 16:51:51 +03003404 TTypeSpecifierNonArray typeSpecifierNonArray;
3405 typeSpecifierNonArray.initialize(EbtStruct, structLine);
3406 typeSpecifierNonArray.userDef = structureType;
3407 typeSpecifierNonArray.isStructSpecifier = true;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003408 exitStructDeclaration();
3409
Martin Radev4a9cd802016-09-01 16:51:51 +03003410 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003411}
3412
Jamie Madillb98c3a82015-07-23 14:26:04 -04003413TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003414 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003415 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02003416{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003417 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003418 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02003419 init->isVector())
3420 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003421 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
3422 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003423 return nullptr;
3424 }
3425
Olli Etuahoac5274d2015-02-20 10:19:08 +02003426 if (statementList)
3427 {
3428 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
3429 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02003430 return nullptr;
3431 }
3432 }
3433
Olli Etuahoa3a36662015-02-17 13:46:51 +02003434 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3435 if (node == nullptr)
3436 {
3437 error(loc, "erroneous switch statement", "switch");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003438 return nullptr;
3439 }
3440 return node;
3441}
3442
3443TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3444{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003445 if (mSwitchNestingLevel == 0)
3446 {
3447 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003448 return nullptr;
3449 }
3450 if (condition == nullptr)
3451 {
3452 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003453 return nullptr;
3454 }
3455 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04003456 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02003457 {
3458 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003459 }
3460 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003461 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
3462 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
3463 // fold in case labels.
3464 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02003465 {
3466 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003467 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003468 TIntermCase *node = intermediate.addCase(condition, loc);
3469 if (node == nullptr)
3470 {
3471 error(loc, "erroneous case statement", "case");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003472 return nullptr;
3473 }
3474 return node;
3475}
3476
3477TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3478{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003479 if (mSwitchNestingLevel == 0)
3480 {
3481 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003482 return nullptr;
3483 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003484 TIntermCase *node = intermediate.addCase(nullptr, loc);
3485 if (node == nullptr)
3486 {
3487 error(loc, "erroneous default statement", "default");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003488 return nullptr;
3489 }
3490 return node;
3491}
3492
Jamie Madillb98c3a82015-07-23 14:26:04 -04003493TIntermTyped *TParseContext::createUnaryMath(TOperator op,
3494 TIntermTyped *child,
3495 const TSourceLoc &loc,
3496 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003497{
3498 if (child == nullptr)
3499 {
3500 return nullptr;
3501 }
3502
3503 switch (op)
3504 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003505 case EOpLogicalNot:
3506 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
3507 child->isVector())
3508 {
3509 return nullptr;
3510 }
3511 break;
3512 case EOpBitwiseNot:
3513 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3514 child->isMatrix() || child->isArray())
3515 {
3516 return nullptr;
3517 }
3518 break;
3519 case EOpPostIncrement:
3520 case EOpPreIncrement:
3521 case EOpPostDecrement:
3522 case EOpPreDecrement:
3523 case EOpNegative:
3524 case EOpPositive:
3525 if (child->getBasicType() == EbtStruct || child->getBasicType() == EbtBool ||
Martin Radev2cc85b32016-08-05 16:22:53 +03003526 child->isArray() || IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04003527 {
3528 return nullptr;
3529 }
3530 // Operators for built-ins are already type checked against their prototype.
3531 default:
3532 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003533 }
3534
Olli Etuahof119a262016-08-19 15:54:22 +03003535 TIntermUnary *node = new TIntermUnary(op, child);
3536 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03003537
3538 TIntermTyped *foldedNode = node->fold(&mDiagnostics);
3539 if (foldedNode)
3540 return foldedNode;
3541
3542 return node;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003543}
3544
Olli Etuaho09b22472015-02-11 11:47:26 +02003545TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3546{
Olli Etuahof6c694b2015-03-26 14:50:53 +02003547 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003548 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003549 {
3550 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003551 return child;
3552 }
3553 return node;
3554}
3555
Jamie Madillb98c3a82015-07-23 14:26:04 -04003556TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
3557 TIntermTyped *child,
3558 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003559{
Olli Etuaho856c4972016-08-08 11:38:39 +03003560 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02003561 return addUnaryMath(op, child, loc);
3562}
3563
Jamie Madillb98c3a82015-07-23 14:26:04 -04003564bool TParseContext::binaryOpCommonCheck(TOperator op,
3565 TIntermTyped *left,
3566 TIntermTyped *right,
3567 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003568{
Olli Etuaho244be012016-08-18 15:26:02 +03003569 if (left->getType().getStruct() || right->getType().getStruct())
3570 {
3571 switch (op)
3572 {
3573 case EOpIndexDirectStruct:
3574 ASSERT(left->getType().getStruct());
3575 break;
3576 case EOpEqual:
3577 case EOpNotEqual:
3578 case EOpAssign:
3579 case EOpInitialize:
3580 if (left->getType() != right->getType())
3581 {
3582 return false;
3583 }
3584 break;
3585 default:
3586 error(loc, "Invalid operation for structs", GetOperatorString(op));
3587 return false;
3588 }
3589 }
3590
Olli Etuahod6b14282015-03-17 14:31:35 +02003591 if (left->isArray() || right->isArray())
3592 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003593 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003594 {
3595 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3596 return false;
3597 }
3598
3599 if (left->isArray() != right->isArray())
3600 {
3601 error(loc, "array / non-array mismatch", GetOperatorString(op));
3602 return false;
3603 }
3604
3605 switch (op)
3606 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003607 case EOpEqual:
3608 case EOpNotEqual:
3609 case EOpAssign:
3610 case EOpInitialize:
3611 break;
3612 default:
3613 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3614 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02003615 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003616 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003617 if (left->getArraySize() != right->getArraySize())
3618 {
3619 error(loc, "array size mismatch", GetOperatorString(op));
3620 return false;
3621 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003622 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003623
3624 // Check ops which require integer / ivec parameters
3625 bool isBitShift = false;
3626 switch (op)
3627 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003628 case EOpBitShiftLeft:
3629 case EOpBitShiftRight:
3630 case EOpBitShiftLeftAssign:
3631 case EOpBitShiftRightAssign:
3632 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3633 // check that the basic type is an integer type.
3634 isBitShift = true;
3635 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3636 {
3637 return false;
3638 }
3639 break;
3640 case EOpBitwiseAnd:
3641 case EOpBitwiseXor:
3642 case EOpBitwiseOr:
3643 case EOpBitwiseAndAssign:
3644 case EOpBitwiseXorAssign:
3645 case EOpBitwiseOrAssign:
3646 // It is enough to check the type of only one operand, since later it
3647 // is checked that the operand types match.
3648 if (!IsInteger(left->getBasicType()))
3649 {
3650 return false;
3651 }
3652 break;
3653 default:
3654 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003655 }
3656
3657 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3658 // So the basic type should usually match.
3659 if (!isBitShift && left->getBasicType() != right->getBasicType())
3660 {
3661 return false;
3662 }
3663
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003664 // Check that:
3665 // 1. Type sizes match exactly on ops that require that.
3666 // 2. Restrictions for structs that contain arrays or samplers are respected.
3667 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003668 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003669 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003670 case EOpAssign:
3671 case EOpInitialize:
3672 case EOpEqual:
3673 case EOpNotEqual:
3674 // ESSL 1.00 sections 5.7, 5.8, 5.9
3675 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
3676 {
3677 error(loc, "undefined operation for structs containing arrays",
3678 GetOperatorString(op));
3679 return false;
3680 }
3681 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3682 // we interpret the spec so that this extends to structs containing samplers,
3683 // similarly to ESSL 1.00 spec.
3684 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
3685 left->getType().isStructureContainingSamplers())
3686 {
3687 error(loc, "undefined operation for structs containing samplers",
3688 GetOperatorString(op));
3689 return false;
3690 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003691
3692 if ((op == EOpAssign || op == EOpInitialize) &&
3693 left->getType().isStructureContainingImages())
3694 {
3695 error(loc, "undefined operation for structs containing images",
3696 GetOperatorString(op));
3697 return false;
3698 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003699 case EOpLessThan:
3700 case EOpGreaterThan:
3701 case EOpLessThanEqual:
3702 case EOpGreaterThanEqual:
3703 if ((left->getNominalSize() != right->getNominalSize()) ||
3704 (left->getSecondarySize() != right->getSecondarySize()))
3705 {
3706 return false;
3707 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003708 break;
3709 case EOpAdd:
3710 case EOpSub:
3711 case EOpDiv:
3712 case EOpIMod:
3713 case EOpBitShiftLeft:
3714 case EOpBitShiftRight:
3715 case EOpBitwiseAnd:
3716 case EOpBitwiseXor:
3717 case EOpBitwiseOr:
3718 case EOpAddAssign:
3719 case EOpSubAssign:
3720 case EOpDivAssign:
3721 case EOpIModAssign:
3722 case EOpBitShiftLeftAssign:
3723 case EOpBitShiftRightAssign:
3724 case EOpBitwiseAndAssign:
3725 case EOpBitwiseXorAssign:
3726 case EOpBitwiseOrAssign:
3727 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
3728 {
3729 return false;
3730 }
3731
3732 // Are the sizes compatible?
3733 if (left->getNominalSize() != right->getNominalSize() ||
3734 left->getSecondarySize() != right->getSecondarySize())
3735 {
3736 // If the nominal sizes of operands do not match:
3737 // One of them must be a scalar.
3738 if (!left->isScalar() && !right->isScalar())
3739 return false;
3740
3741 // In the case of compound assignment other than multiply-assign,
3742 // the right side needs to be a scalar. Otherwise a vector/matrix
3743 // would be assigned to a scalar. A scalar can't be shifted by a
3744 // vector either.
3745 if (!right->isScalar() &&
3746 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
3747 return false;
3748 }
3749 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003750 default:
3751 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003752 }
3753
Olli Etuahod6b14282015-03-17 14:31:35 +02003754 return true;
3755}
3756
Olli Etuaho1dded802016-08-18 18:13:13 +03003757bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
3758 const TType &left,
3759 const TType &right)
3760{
3761 switch (op)
3762 {
3763 case EOpMul:
3764 case EOpMulAssign:
3765 return left.getNominalSize() == right.getNominalSize() &&
3766 left.getSecondarySize() == right.getSecondarySize();
3767 case EOpVectorTimesScalar:
3768 return true;
3769 case EOpVectorTimesScalarAssign:
3770 ASSERT(!left.isMatrix() && !right.isMatrix());
3771 return left.isVector() && !right.isVector();
3772 case EOpVectorTimesMatrix:
3773 return left.getNominalSize() == right.getRows();
3774 case EOpVectorTimesMatrixAssign:
3775 ASSERT(!left.isMatrix() && right.isMatrix());
3776 return left.isVector() && left.getNominalSize() == right.getRows() &&
3777 left.getNominalSize() == right.getCols();
3778 case EOpMatrixTimesVector:
3779 return left.getCols() == right.getNominalSize();
3780 case EOpMatrixTimesScalar:
3781 return true;
3782 case EOpMatrixTimesScalarAssign:
3783 ASSERT(left.isMatrix() && !right.isMatrix());
3784 return !right.isVector();
3785 case EOpMatrixTimesMatrix:
3786 return left.getCols() == right.getRows();
3787 case EOpMatrixTimesMatrixAssign:
3788 ASSERT(left.isMatrix() && right.isMatrix());
3789 // We need to check two things:
3790 // 1. The matrix multiplication step is valid.
3791 // 2. The result will have the same number of columns as the lvalue.
3792 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
3793
3794 default:
3795 UNREACHABLE();
3796 return false;
3797 }
3798}
3799
Jamie Madillb98c3a82015-07-23 14:26:04 -04003800TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
3801 TIntermTyped *left,
3802 TIntermTyped *right,
3803 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02003804{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003805 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003806 return nullptr;
3807
Olli Etuahofc1806e2015-03-17 13:03:11 +02003808 switch (op)
3809 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003810 case EOpEqual:
3811 case EOpNotEqual:
3812 break;
3813 case EOpLessThan:
3814 case EOpGreaterThan:
3815 case EOpLessThanEqual:
3816 case EOpGreaterThanEqual:
Olli Etuaho244be012016-08-18 15:26:02 +03003817 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3818 !right->getType().getStruct());
3819 if (left->isMatrix() || left->isVector())
Jamie Madillb98c3a82015-07-23 14:26:04 -04003820 {
3821 return nullptr;
3822 }
3823 break;
3824 case EOpLogicalOr:
3825 case EOpLogicalXor:
3826 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03003827 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3828 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003829 if (left->getBasicType() != EbtBool || left->isMatrix() || left->isVector())
3830 {
3831 return nullptr;
3832 }
3833 break;
3834 case EOpAdd:
3835 case EOpSub:
3836 case EOpDiv:
3837 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03003838 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3839 !right->getType().getStruct());
3840 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04003841 {
3842 return nullptr;
3843 }
3844 break;
3845 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03003846 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3847 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003848 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03003849 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04003850 {
3851 return nullptr;
3852 }
3853 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003854 default:
3855 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003856 }
3857
Olli Etuaho1dded802016-08-18 18:13:13 +03003858 if (op == EOpMul)
3859 {
3860 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
3861 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
3862 {
3863 return nullptr;
3864 }
3865 }
3866
Olli Etuaho3fdec912016-08-18 15:08:06 +03003867 TIntermBinary *node = new TIntermBinary(op, left, right);
3868 node->setLine(loc);
3869
Olli Etuaho3fdec912016-08-18 15:08:06 +03003870 // See if we can fold constants.
3871 TIntermTyped *foldedNode = node->fold(&mDiagnostics);
3872 if (foldedNode)
3873 return foldedNode;
3874
3875 return node;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003876}
3877
Jamie Madillb98c3a82015-07-23 14:26:04 -04003878TIntermTyped *TParseContext::addBinaryMath(TOperator op,
3879 TIntermTyped *left,
3880 TIntermTyped *right,
3881 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003882{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003883 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003884 if (node == 0)
3885 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003886 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3887 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003888 return left;
3889 }
3890 return node;
3891}
3892
Jamie Madillb98c3a82015-07-23 14:26:04 -04003893TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
3894 TIntermTyped *left,
3895 TIntermTyped *right,
3896 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003897{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003898 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003899 if (node == 0)
3900 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003901 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3902 right->getCompleteString());
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003903 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02003904 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003905 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
3906 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003907 }
3908 return node;
3909}
3910
Olli Etuaho13389b62016-10-16 11:48:18 +01003911TIntermBinary *TParseContext::createAssign(TOperator op,
3912 TIntermTyped *left,
3913 TIntermTyped *right,
3914 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003915{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003916 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003917 {
Olli Etuaho1dded802016-08-18 18:13:13 +03003918 if (op == EOpMulAssign)
3919 {
3920 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
3921 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
3922 {
3923 return nullptr;
3924 }
3925 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03003926 TIntermBinary *node = new TIntermBinary(op, left, right);
3927 node->setLine(loc);
3928
Olli Etuaho3fdec912016-08-18 15:08:06 +03003929 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02003930 }
3931 return nullptr;
3932}
3933
Jamie Madillb98c3a82015-07-23 14:26:04 -04003934TIntermTyped *TParseContext::addAssign(TOperator op,
3935 TIntermTyped *left,
3936 TIntermTyped *right,
3937 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003938{
3939 TIntermTyped *node = createAssign(op, left, right, loc);
3940 if (node == nullptr)
3941 {
3942 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02003943 return left;
3944 }
3945 return node;
3946}
3947
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003948TIntermTyped *TParseContext::addComma(TIntermTyped *left,
3949 TIntermTyped *right,
3950 const TSourceLoc &loc)
3951{
Corentin Wallez0d959252016-07-12 17:26:32 -04003952 // WebGL2 section 5.26, the following results in an error:
3953 // "Sequence operator applied to void, arrays, or structs containing arrays"
3954 if (mShaderSpec == SH_WEBGL2_SPEC && (left->isArray() || left->getBasicType() == EbtVoid ||
3955 left->getType().isStructureContainingArrays() ||
3956 right->isArray() || right->getBasicType() == EbtVoid ||
3957 right->getType().isStructureContainingArrays()))
3958 {
3959 error(loc,
3960 "sequence operator is not allowed for void, arrays, or structs containing arrays",
3961 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04003962 }
3963
Olli Etuaho4db7ded2016-10-13 12:23:11 +01003964 return TIntermediate::AddComma(left, right, loc, mShaderVersion);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003965}
3966
Olli Etuaho49300862015-02-20 14:54:49 +02003967TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3968{
3969 switch (op)
3970 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003971 case EOpContinue:
3972 if (mLoopNestingLevel <= 0)
3973 {
3974 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04003975 }
3976 break;
3977 case EOpBreak:
3978 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
3979 {
3980 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04003981 }
3982 break;
3983 case EOpReturn:
3984 if (mCurrentFunctionType->getBasicType() != EbtVoid)
3985 {
3986 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04003987 }
3988 break;
3989 default:
3990 // No checks for discard
3991 break;
Olli Etuaho49300862015-02-20 14:54:49 +02003992 }
3993 return intermediate.addBranch(op, loc);
3994}
3995
Jamie Madillb98c3a82015-07-23 14:26:04 -04003996TIntermBranch *TParseContext::addBranch(TOperator op,
3997 TIntermTyped *returnValue,
3998 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02003999{
4000 ASSERT(op == EOpReturn);
4001 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004002 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02004003 {
4004 error(loc, "void function cannot return a value", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02004005 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004006 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02004007 {
4008 error(loc, "function return is not matching type:", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02004009 }
4010 return intermediate.addBranch(op, returnValue, loc);
4011}
4012
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004013void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
4014{
4015 ASSERT(!functionCall->isUserDefined());
Olli Etuahobd674552016-10-06 13:28:42 +01004016 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004017 TIntermNode *offset = nullptr;
4018 TIntermSequence *arguments = functionCall->getSequence();
4019 if (name.compare(0, 16, "texelFetchOffset") == 0 ||
4020 name.compare(0, 16, "textureLodOffset") == 0 ||
4021 name.compare(0, 20, "textureProjLodOffset") == 0 ||
4022 name.compare(0, 17, "textureGradOffset") == 0 ||
4023 name.compare(0, 21, "textureProjGradOffset") == 0)
4024 {
4025 offset = arguments->back();
4026 }
4027 else if (name.compare(0, 13, "textureOffset") == 0 ||
4028 name.compare(0, 17, "textureProjOffset") == 0)
4029 {
4030 // A bias parameter might follow the offset parameter.
4031 ASSERT(arguments->size() >= 3);
4032 offset = (*arguments)[2];
4033 }
4034 if (offset != nullptr)
4035 {
4036 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
4037 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
4038 {
4039 TString unmangledName = TFunction::unmangleName(name);
4040 error(functionCall->getLine(), "Texture offset must be a constant expression",
4041 unmangledName.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004042 }
4043 else
4044 {
4045 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
4046 size_t size = offsetConstantUnion->getType().getObjectSize();
4047 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
4048 for (size_t i = 0u; i < size; ++i)
4049 {
4050 int offsetValue = values[i].getIConst();
4051 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
4052 {
4053 std::stringstream tokenStream;
4054 tokenStream << offsetValue;
4055 std::string token = tokenStream.str();
4056 error(offset->getLine(), "Texture offset value out of valid range",
4057 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004058 }
4059 }
4060 }
4061 }
4062}
4063
Martin Radev2cc85b32016-08-05 16:22:53 +03004064// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
4065void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
4066{
4067 ASSERT(!functionCall->isUserDefined());
4068 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
4069
4070 if (name.compare(0, 5, "image") == 0)
4071 {
4072 TIntermSequence *arguments = functionCall->getSequence();
4073 TIntermNode *imageNode = (*arguments)[0];
4074 TIntermSymbol *imageSymbol = imageNode->getAsSymbolNode();
4075
4076 const TMemoryQualifier &memoryQualifier = imageSymbol->getMemoryQualifier();
4077
4078 if (name.compare(5, 5, "Store") == 0)
4079 {
4080 if (memoryQualifier.readonly)
4081 {
4082 error(imageNode->getLine(),
4083 "'imageStore' cannot be used with images qualified as 'readonly'",
4084 imageSymbol->getSymbol().c_str());
4085 }
4086 }
4087 else if (name.compare(5, 4, "Load") == 0)
4088 {
4089 if (memoryQualifier.writeonly)
4090 {
4091 error(imageNode->getLine(),
4092 "'imageLoad' cannot be used with images qualified as 'writeonly'",
4093 imageSymbol->getSymbol().c_str());
4094 }
4095 }
4096 }
4097}
4098
4099// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
4100void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
4101 const TFunction *functionDefinition,
4102 const TIntermAggregate *functionCall)
4103{
4104 ASSERT(functionCall->isUserDefined());
4105
4106 const TIntermSequence &arguments = *functionCall->getSequence();
4107
4108 ASSERT(functionDefinition->getParamCount() == arguments.size());
4109
4110 for (size_t i = 0; i < arguments.size(); ++i)
4111 {
4112 const TType &functionArgumentType = arguments[i]->getAsTyped()->getType();
4113 const TType &functionParameterType = *functionDefinition->getParam(i).type;
4114 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
4115
4116 if (IsImage(functionArgumentType.getBasicType()))
4117 {
4118 const TMemoryQualifier &functionArgumentMemoryQualifier =
4119 functionArgumentType.getMemoryQualifier();
4120 const TMemoryQualifier &functionParameterMemoryQualifier =
4121 functionParameterType.getMemoryQualifier();
4122 if (functionArgumentMemoryQualifier.readonly &&
4123 !functionParameterMemoryQualifier.readonly)
4124 {
4125 error(functionCall->getLine(),
4126 "Function call discards the 'readonly' qualifier from image",
4127 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4128 }
4129
4130 if (functionArgumentMemoryQualifier.writeonly &&
4131 !functionParameterMemoryQualifier.writeonly)
4132 {
4133 error(functionCall->getLine(),
4134 "Function call discards the 'writeonly' qualifier from image",
4135 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4136 }
4137 }
4138 }
4139}
4140
Jamie Madillb98c3a82015-07-23 14:26:04 -04004141TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
4142 TIntermNode *paramNode,
4143 TIntermNode *thisNode,
4144 const TSourceLoc &loc,
4145 bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004146{
Jamie Madillb98c3a82015-07-23 14:26:04 -04004147 *fatalError = false;
4148 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004149 TIntermTyped *callNode = nullptr;
4150
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004151 if (thisNode != nullptr)
4152 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004153 TConstantUnion *unionArray = new TConstantUnion[1];
Jamie Madillb98c3a82015-07-23 14:26:04 -04004154 int arraySize = 0;
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004155 TIntermTyped *typedThis = thisNode->getAsTyped();
4156 if (fnCall->getName() != "length")
4157 {
4158 error(loc, "invalid method", fnCall->getName().c_str());
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004159 }
4160 else if (paramNode != nullptr)
4161 {
4162 error(loc, "method takes no parameters", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004163 }
4164 else if (typedThis == nullptr || !typedThis->isArray())
4165 {
4166 error(loc, "length can only be called on arrays", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004167 }
4168 else
4169 {
Olli Etuaho96e67382015-04-23 14:27:02 +03004170 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03004171 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004172 {
Olli Etuaho39282e12015-04-23 15:41:48 +03004173 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004174 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03004175 // (func()).length()
4176 // (int[3](0, 1, 2)).length()
Jamie Madillb98c3a82015-07-23 14:26:04 -04004177 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
4178 // expression.
4179 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
4180 // spec section 5.9 which allows "An array, vector or matrix expression with the
4181 // length method applied".
4182 error(loc, "length can only be called on array names, not on array expressions",
4183 "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004184 }
4185 }
Olli Etuaho96e67382015-04-23 14:27:02 +03004186 unionArray->setIConst(arraySize);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004187 callNode =
4188 intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004189 }
4190 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004191 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004192 // Then this should be a constructor.
Olli Etuaho856c4972016-08-08 11:38:39 +03004193 callNode = addConstructor(paramNode, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004194 }
4195 else
4196 {
4197 //
4198 // Not a constructor. Find it in the symbol table.
4199 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05304200 const TFunction *fnCandidate;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004201 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004202 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004203 if (fnCandidate)
4204 {
4205 //
4206 // A declared function.
4207 //
Olli Etuaho383b7912016-08-05 11:22:59 +03004208 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004209 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004210 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004211 }
4212 op = fnCandidate->getBuiltInOp();
4213 if (builtIn && op != EOpNull)
4214 {
4215 //
4216 // A function call mapped to a built-in operation.
4217 //
4218 if (fnCandidate->getParamCount() == 1)
4219 {
4220 //
4221 // Treat it like a built-in unary operator.
4222 //
Olli Etuaho15c2ac32015-11-09 15:51:43 +02004223 TIntermAggregate *paramAgg = paramNode->getAsAggregate();
4224 paramNode = paramAgg->getSequence()->front();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004225 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc,
4226 &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004227 if (callNode == nullptr)
4228 {
4229 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004230 extraInfoStream
4231 << "built in unary operator function. Type: "
4232 << static_cast<TIntermTyped *>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004233 std::string extraInfo = extraInfoStream.str();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004234 error(paramNode->getLine(), " wrong operand type", "Internal Error",
4235 extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004236 *fatalError = true;
4237 return nullptr;
4238 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004239 }
4240 else
4241 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004242 TIntermAggregate *aggregate =
4243 intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004244 aggregate->setType(fnCandidate->getReturnType());
4245 aggregate->setPrecisionFromChildren();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02004246 if (aggregate->areChildrenConstQualified())
4247 {
4248 aggregate->getTypePointer()->setQualifier(EvqConst);
4249 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004250
4251 // Some built-in functions have out parameters too.
4252 functionCallLValueErrorCheck(fnCandidate, aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05304253
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004254 // See if we can constant fold a built-in. Note that this may be possible even
4255 // if it is not const-qualified.
Olli Etuahof119a262016-08-19 15:54:22 +03004256 TIntermTyped *foldedNode =
4257 intermediate.foldAggregateBuiltIn(aggregate, &mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05304258 if (foldedNode)
4259 {
Arun Patole274f0702015-05-05 13:33:30 +05304260 callNode = foldedNode;
4261 }
Olli Etuahob43846e2015-06-02 18:18:57 +03004262 else
4263 {
4264 callNode = aggregate;
4265 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004266 }
4267 }
4268 else
4269 {
4270 // This is a real function call
Jamie Madillb98c3a82015-07-23 14:26:04 -04004271 TIntermAggregate *aggregate =
4272 intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004273 aggregate->setType(fnCandidate->getReturnType());
4274
Jamie Madillb98c3a82015-07-23 14:26:04 -04004275 // this is how we know whether the given function is a builtIn function or a user
4276 // defined function
4277 // if builtIn == false, it's a userDefined -> could be an overloaded
4278 // builtIn function also
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004279 // if builtIn == true, it's definitely a builtIn function with EOpNull
4280 if (!builtIn)
4281 aggregate->setUserDefined();
Olli Etuahobd674552016-10-06 13:28:42 +01004282 aggregate->getFunctionSymbolInfo()->setFromFunction(*fnCandidate);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004283
Olli Etuahobd674552016-10-06 13:28:42 +01004284 // This needs to happen after the function info including name is set
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004285 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004286 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004287 aggregate->setBuiltInFunctionPrecision();
4288
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004289 checkTextureOffsetConst(aggregate);
Martin Radev2cc85b32016-08-05 16:22:53 +03004290
4291 checkImageMemoryAccessForBuiltinFunctions(aggregate);
4292 }
4293 else
4294 {
4295 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, aggregate);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004296 }
4297
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004298 callNode = aggregate;
4299
4300 functionCallLValueErrorCheck(fnCandidate, aggregate);
4301 }
4302 }
4303 else
4304 {
4305 // error message was put out by findFunction()
4306 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004307 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004308 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004309 callNode = intermediate.addConstantUnion(unionArray,
4310 TType(EbtFloat, EbpUndefined, EvqConst), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004311 }
4312 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004313 return callNode;
4314}
4315
Jamie Madillb98c3a82015-07-23 14:26:04 -04004316TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004317 TIntermTyped *trueExpression,
4318 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03004319 const TSourceLoc &loc)
4320{
Olli Etuaho856c4972016-08-08 11:38:39 +03004321 checkIsScalarBool(loc, cond);
Olli Etuaho52901742015-04-15 13:42:45 +03004322
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004323 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03004324 {
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004325 binaryOpError(loc, ":", trueExpression->getCompleteString(),
4326 falseExpression->getCompleteString());
4327 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03004328 }
Olli Etuahode318b22016-10-25 16:18:25 +01004329 if (IsOpaqueType(trueExpression->getBasicType()))
4330 {
4331 // ESSL 1.00 section 4.1.7
4332 // ESSL 3.00 section 4.1.7
4333 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
4334 // Note that structs containing opaque types don't need to be checked as structs are
4335 // forbidden below.
4336 error(loc, "ternary operator is not allowed for opaque types", ":");
4337 return falseExpression;
4338 }
4339
Olli Etuahoa2d53032015-04-15 14:14:44 +03004340 // ESSL1 sections 5.2 and 5.7:
4341 // ESSL3 section 5.7:
4342 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004343 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03004344 {
4345 error(loc, "ternary operator is not allowed for structures or arrays", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004346 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03004347 }
Corentin Wallez0d959252016-07-12 17:26:32 -04004348 // WebGL2 section 5.26, the following results in an error:
4349 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004350 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04004351 {
4352 error(loc, "ternary operator is not allowed for void", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004353 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04004354 }
4355
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004356 return TIntermediate::AddTernarySelection(cond, trueExpression, falseExpression, loc);
Olli Etuaho52901742015-04-15 13:42:45 +03004357}
Olli Etuaho49300862015-02-20 14:54:49 +02004358
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004359//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004360// Parse an array of strings using yyparse.
4361//
4362// Returns 0 for success.
4363//
Jamie Madillb98c3a82015-07-23 14:26:04 -04004364int PaParseStrings(size_t count,
4365 const char *const string[],
4366 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05304367 TParseContext *context)
4368{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004369 if ((count == 0) || (string == NULL))
4370 return 1;
4371
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004372 if (glslang_initialize(context))
4373 return 1;
4374
alokp@chromium.org408c45e2012-04-05 15:54:43 +00004375 int error = glslang_scan(count, string, length, context);
4376 if (!error)
4377 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004378
alokp@chromium.org73bc2982012-06-19 18:48:05 +00004379 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00004380
alokp@chromium.org6b495712012-06-29 00:06:58 +00004381 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004382}