blob: fc5ea60bb4aabe4dc3ba51d929c55e9fe32a2681 [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,
1478 TIntermNode **intermNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001479{
Olli Etuahoe7847b02015-03-16 11:56:12 +02001480 ASSERT(intermNode != nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001481 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001482
Olli Etuaho2935c582015-04-08 14:32:06 +03001483 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001484 if (type.isUnsizedArray())
1485 {
1486 type.setArraySize(initializer->getArraySize());
1487 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001488 if (!declareVariable(line, identifier, type, &variable))
1489 {
1490 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001491 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001492
Olli Etuahob0c645e2015-05-12 14:25:36 +03001493 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001494 if (symbolTable.atGlobalLevel() &&
1495 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001496 {
1497 // Error message does not completely match behavior with ESSL 1.00, but
1498 // we want to steer developers towards only using constant expressions.
1499 error(line, "global variable initializers must be constant expressions", "=");
1500 return true;
1501 }
1502 if (globalInitWarning)
1503 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001504 warning(
1505 line,
1506 "global variable initializers should be constant expressions "
1507 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1508 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001509 }
1510
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001511 //
1512 // identifier must be of type constant, a global, or a temporary
1513 //
1514 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301515 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1516 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001517 error(line, " cannot initialize this type of qualifier ",
1518 variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001519 return true;
1520 }
1521 //
1522 // test for and propagate constant
1523 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001524
Arun Patole7e7e68d2015-05-22 12:02:25 +05301525 if (qualifier == EvqConst)
1526 {
1527 if (qualifier != initializer->getType().getQualifier())
1528 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001529 std::stringstream extraInfoStream;
1530 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1531 std::string extraInfo = extraInfoStream.str();
1532 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001533 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001534 return true;
1535 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301536 if (type != initializer->getType())
1537 {
1538 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001539 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001540 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001541 return true;
1542 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001543
1544 // Save the constant folded value to the variable if possible. For example array
1545 // initializers are not folded, since that way copying the array literal to multiple places
1546 // in the shader is avoided.
1547 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1548 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301549 if (initializer->getAsConstantUnion())
1550 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001551 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001552 *intermNode = nullptr;
1553 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301554 }
1555 else if (initializer->getAsSymbolNode())
1556 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001557 const TSymbol *symbol =
1558 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1559 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001560
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001561 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001562 if (constArray)
1563 {
1564 variable->shareConstPointer(constArray);
1565 *intermNode = nullptr;
1566 return false;
1567 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001568 }
1569 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001570
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001571 TIntermSymbol *intermSymbol = intermediate.addSymbol(
1572 variable->getUniqueId(), variable->getName(), variable->getType(), line);
1573 *intermNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1574 if (*intermNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001575 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001576 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1577 return true;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001578 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001579
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001580 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001581}
1582
Martin Radev70866b82016-07-22 15:27:42 +03001583TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301584 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001585{
Olli Etuaho613b9592016-09-05 12:05:53 +03001586 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001587
Martin Radev70866b82016-07-22 15:27:42 +03001588 TPublicType returnType = typeSpecifier;
1589 returnType.qualifier = typeQualifier.qualifier;
1590 returnType.invariant = typeQualifier.invariant;
1591 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03001592 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03001593 returnType.precision = typeSpecifier.precision;
1594
1595 if (typeQualifier.precision != EbpUndefined)
1596 {
1597 returnType.precision = typeQualifier.precision;
1598 }
1599
Martin Radev4a9cd802016-09-01 16:51:51 +03001600 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
1601 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03001602
Martin Radev4a9cd802016-09-01 16:51:51 +03001603 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
1604 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03001605
Martin Radev4a9cd802016-09-01 16:51:51 +03001606 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03001607
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001608 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001609 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001610 if (typeSpecifier.array)
1611 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001612 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001613 returnType.clearArrayness();
1614 }
1615
Martin Radev70866b82016-07-22 15:27:42 +03001616 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001617 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001618 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001619 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001620 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001621 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001622
Martin Radev70866b82016-07-22 15:27:42 +03001623 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001624 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001625 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001626 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001627 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001628 }
1629 }
1630 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001631 {
Martin Radev70866b82016-07-22 15:27:42 +03001632 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03001633 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001634 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03001635 }
Martin Radev70866b82016-07-22 15:27:42 +03001636 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
1637 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001638 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001639 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
1640 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001641 }
Martin Radev70866b82016-07-22 15:27:42 +03001642 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03001643 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001644 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03001645 "in");
Martin Radev802abe02016-08-04 17:48:32 +03001646 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001647 }
1648
1649 return returnType;
1650}
1651
Olli Etuaho856c4972016-08-08 11:38:39 +03001652void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
1653 const TPublicType &type,
1654 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03001655{
1656 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03001657 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03001658 {
1659 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001660 }
1661
1662 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
1663 switch (qualifier)
1664 {
1665 case EvqVertexIn:
1666 // ESSL 3.00 section 4.3.4
1667 if (type.array)
1668 {
1669 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001670 }
1671 // Vertex inputs with a struct type are disallowed in singleDeclarationErrorCheck
1672 return;
1673 case EvqFragmentOut:
1674 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03001675 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03001676 {
1677 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001678 }
1679 // Fragment outputs with a struct type are disallowed in singleDeclarationErrorCheck
1680 return;
1681 default:
1682 break;
1683 }
1684
1685 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
1686 // restrictions.
1687 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03001688 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
1689 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03001690 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
1691 {
1692 error(qualifierLocation, "must use 'flat' interpolation here",
1693 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001694 }
1695
Martin Radev4a9cd802016-09-01 16:51:51 +03001696 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03001697 {
1698 // ESSL 3.00 sections 4.3.4 and 4.3.6.
1699 // These restrictions are only implied by the ESSL 3.00 spec, but
1700 // the ESSL 3.10 spec lists these restrictions explicitly.
1701 if (type.array)
1702 {
1703 error(qualifierLocation, "cannot be an array of structures",
1704 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001705 }
1706 if (type.isStructureContainingArrays())
1707 {
1708 error(qualifierLocation, "cannot be a structure containing an array",
1709 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001710 }
1711 if (type.isStructureContainingType(EbtStruct))
1712 {
1713 error(qualifierLocation, "cannot be a structure containing a structure",
1714 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001715 }
1716 if (type.isStructureContainingType(EbtBool))
1717 {
1718 error(qualifierLocation, "cannot be a structure containing a bool",
1719 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001720 }
1721 }
1722}
1723
Martin Radev2cc85b32016-08-05 16:22:53 +03001724void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
1725{
1726 if (qualifier.getType() == QtStorage)
1727 {
1728 const TStorageQualifierWrapper &storageQualifier =
1729 static_cast<const TStorageQualifierWrapper &>(qualifier);
1730 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
1731 !symbolTable.atGlobalLevel())
1732 {
1733 error(storageQualifier.getLine(),
1734 "Local variables can only use the const storage qualifier.",
1735 storageQualifier.getQualifierString().c_str());
1736 }
1737 }
1738}
1739
1740bool TParseContext::checkIsMemoryQualifierNotSpecified(const TMemoryQualifier &memoryQualifier,
1741 const TSourceLoc &location)
1742{
1743 if (memoryQualifier.readonly)
1744 {
1745 error(location, "Only allowed with images.", "readonly");
1746 return false;
1747 }
1748 if (memoryQualifier.writeonly)
1749 {
1750 error(location, "Only allowed with images.", "writeonly");
1751 return false;
1752 }
1753 return true;
1754}
1755
Olli Etuahofa33d582015-04-09 14:33:12 +03001756TIntermAggregate *TParseContext::parseSingleDeclaration(TPublicType &publicType,
1757 const TSourceLoc &identifierOrTypeLocation,
1758 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001759{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001760 TType type(publicType);
1761 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
1762 mDirectiveHandler.pragma().stdgl.invariantAll)
1763 {
1764 TQualifier qualifier = type.getQualifier();
1765
1766 // The directive handler has already taken care of rejecting invalid uses of this pragma
1767 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
1768 // affected variable declarations:
1769 //
1770 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
1771 // elsewhere, in TranslatorGLSL.)
1772 //
1773 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
1774 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
1775 // the way this is currently implemented we have to enable this compiler option before
1776 // parsing the shader and determining the shading language version it uses. If this were
1777 // implemented as a post-pass, the workaround could be more targeted.
1778 //
1779 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
1780 // the specification, but there are desktop OpenGL drivers that expect that this is the
1781 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
1782 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
1783 {
1784 type.setInvariant(true);
1785 }
1786 }
1787
1788 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, type, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001789
Olli Etuahobab4c082015-04-24 16:38:49 +03001790 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03001791
Olli Etuahobab4c082015-04-24 16:38:49 +03001792 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1793
1794 if (emptyDeclaration)
1795 {
1796 if (publicType.isUnsizedArray())
1797 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001798 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1799 // error. It is assumed that this applies to empty declarations as well.
1800 error(identifierOrTypeLocation, "empty array declaration needs to specify a size",
1801 identifier.c_str());
Olli Etuahobab4c082015-04-24 16:38:49 +03001802 }
1803 }
1804 else
Jamie Madill60ed9812013-06-06 11:56:46 -04001805 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001806 singleDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001807
Olli Etuaho856c4972016-08-08 11:38:39 +03001808 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001809
Olli Etuaho2935c582015-04-08 14:32:06 +03001810 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001811 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04001812
1813 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001814 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001815 }
1816
Olli Etuaho32db19b2016-10-04 14:43:16 +01001817 return TIntermediate::MakeAggregate(symbol, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001818}
1819
Olli Etuahoe7847b02015-03-16 11:56:12 +02001820TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1821 const TSourceLoc &identifierLocation,
1822 const TString &identifier,
1823 const TSourceLoc &indexLocation,
1824 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001825{
Olli Etuahofa33d582015-04-09 14:33:12 +03001826 mDeferredSingleDeclarationErrorCheck = false;
1827
Olli Etuaho383b7912016-08-05 11:22:59 +03001828 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001829
Olli Etuaho856c4972016-08-08 11:38:39 +03001830 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001831
Olli Etuaho8a176262016-08-16 14:23:01 +03001832 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001833
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001834 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001835
Olli Etuaho856c4972016-08-08 11:38:39 +03001836 unsigned int size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001837 // Make the type an array even if size check failed.
1838 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1839 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001840
Olli Etuaho2935c582015-04-08 14:32:06 +03001841 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03001842 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04001843
Olli Etuahoe7847b02015-03-16 11:56:12 +02001844 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001845 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001846 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001847
Olli Etuaho32db19b2016-10-04 14:43:16 +01001848 return TIntermediate::MakeAggregate(symbol, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001849}
1850
Jamie Madill06145232015-05-13 13:10:01 -04001851TIntermAggregate *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001852 const TSourceLoc &identifierLocation,
1853 const TString &identifier,
1854 const TSourceLoc &initLocation,
1855 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001856{
Olli Etuahofa33d582015-04-09 14:33:12 +03001857 mDeferredSingleDeclarationErrorCheck = false;
1858
Olli Etuaho383b7912016-08-05 11:22:59 +03001859 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001860
Olli Etuahoe7847b02015-03-16 11:56:12 +02001861 TIntermNode *intermNode = nullptr;
1862 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04001863 {
1864 //
1865 // Build intermediate representation
1866 //
Olli Etuaho32db19b2016-10-04 14:43:16 +01001867 return intermNode ? TIntermediate::MakeAggregate(intermNode, initLocation) : nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001868 }
1869 else
1870 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001871 return nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001872 }
1873}
1874
Jamie Madillb98c3a82015-07-23 14:26:04 -04001875TIntermAggregate *TParseContext::parseSingleArrayInitDeclaration(
1876 TPublicType &publicType,
1877 const TSourceLoc &identifierLocation,
1878 const TString &identifier,
1879 const TSourceLoc &indexLocation,
1880 TIntermTyped *indexExpression,
1881 const TSourceLoc &initLocation,
1882 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001883{
1884 mDeferredSingleDeclarationErrorCheck = false;
1885
Olli Etuaho383b7912016-08-05 11:22:59 +03001886 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001887
Olli Etuaho8a176262016-08-16 14:23:01 +03001888 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001889
1890 TPublicType arrayType(publicType);
1891
Olli Etuaho856c4972016-08-08 11:38:39 +03001892 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001893 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
1894 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03001895 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001896 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001897 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001898 }
1899 // Make the type an array even if size check failed.
1900 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1901 arrayType.setArraySize(size);
1902
1903 // initNode will correspond to the whole of "type b[n] = initializer".
1904 TIntermNode *initNode = nullptr;
1905 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1906 {
Olli Etuaho32db19b2016-10-04 14:43:16 +01001907 return initNode ? TIntermediate::MakeAggregate(initNode, initLocation) : nullptr;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001908 }
1909 else
1910 {
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001911 return nullptr;
1912 }
1913}
1914
Martin Radev70866b82016-07-22 15:27:42 +03001915TIntermAggregate *TParseContext::parseInvariantDeclaration(
1916 const TTypeQualifierBuilder &typeQualifierBuilder,
1917 const TSourceLoc &identifierLoc,
1918 const TString *identifier,
1919 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04001920{
Olli Etuaho613b9592016-09-05 12:05:53 +03001921 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04001922
Martin Radev70866b82016-07-22 15:27:42 +03001923 if (!typeQualifier.invariant)
1924 {
1925 error(identifierLoc, "Expected invariant", identifier->c_str());
1926 return nullptr;
1927 }
1928 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
1929 {
1930 return nullptr;
1931 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04001932 if (!symbol)
1933 {
1934 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001935 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001936 }
Martin Radev70866b82016-07-22 15:27:42 +03001937 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04001938 {
Martin Radev70866b82016-07-22 15:27:42 +03001939 error(identifierLoc, "invariant declaration specifies qualifier",
1940 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04001941 }
Martin Radev70866b82016-07-22 15:27:42 +03001942 if (typeQualifier.precision != EbpUndefined)
1943 {
1944 error(identifierLoc, "invariant declaration specifies precision",
1945 getPrecisionString(typeQualifier.precision));
1946 }
1947 if (!typeQualifier.layoutQualifier.isEmpty())
1948 {
1949 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
1950 }
1951
1952 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1953 ASSERT(variable);
1954 const TType &type = variable->getType();
1955
1956 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
1957 typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001958 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03001959
1960 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
1961
1962 TIntermSymbol *intermSymbol =
1963 intermediate.addSymbol(variable->getUniqueId(), *identifier, type, identifierLoc);
1964
Olli Etuaho32db19b2016-10-04 14:43:16 +01001965 TIntermAggregate *aggregate = TIntermediate::MakeAggregate(intermSymbol, identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03001966 aggregate->setOp(EOpInvariantDeclaration);
1967 return aggregate;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001968}
1969
Jamie Madillb98c3a82015-07-23 14:26:04 -04001970TIntermAggregate *TParseContext::parseDeclarator(TPublicType &publicType,
1971 TIntermAggregate *aggregateDeclaration,
1972 const TSourceLoc &identifierLocation,
1973 const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001974{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001975 // If the declaration starting this declarator list was empty (example: int,), some checks were
1976 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001977 if (mDeferredSingleDeclarationErrorCheck)
1978 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001979 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03001980 mDeferredSingleDeclarationErrorCheck = false;
1981 }
1982
Olli Etuaho856c4972016-08-08 11:38:39 +03001983 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04001984
Olli Etuaho856c4972016-08-08 11:38:39 +03001985 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04001986
Olli Etuaho2935c582015-04-08 14:32:06 +03001987 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03001988 declareVariable(identifierLocation, identifier, TType(publicType), &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001989
Jamie Madillb98c3a82015-07-23 14:26:04 -04001990 TIntermSymbol *symbol =
1991 intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001992 if (variable && symbol)
Jamie Madill502d66f2013-06-20 11:55:52 -04001993 symbol->setId(variable->getUniqueId());
1994
Olli Etuahoe7847b02015-03-16 11:56:12 +02001995 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001996}
1997
Jamie Madillb98c3a82015-07-23 14:26:04 -04001998TIntermAggregate *TParseContext::parseArrayDeclarator(TPublicType &publicType,
1999 TIntermAggregate *aggregateDeclaration,
2000 const TSourceLoc &identifierLocation,
2001 const TString &identifier,
2002 const TSourceLoc &arrayLocation,
2003 TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04002004{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002005 // If the declaration starting this declarator list was empty (example: int,), some checks were
2006 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002007 if (mDeferredSingleDeclarationErrorCheck)
2008 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002009 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002010 mDeferredSingleDeclarationErrorCheck = false;
2011 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002012
Olli Etuaho856c4972016-08-08 11:38:39 +03002013 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002014
Olli Etuaho856c4972016-08-08 11:38:39 +03002015 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002016
Olli Etuaho8a176262016-08-16 14:23:01 +03002017 if (checkIsValidTypeAndQualifierForArray(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002018 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02002019 TType arrayType = TType(publicType);
Olli Etuaho856c4972016-08-08 11:38:39 +03002020 unsigned int size = checkIsValidArraySize(arrayLocation, indexExpression);
Olli Etuaho693c9aa2015-04-07 17:50:36 +03002021 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002022
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002023 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002024 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002025
Jamie Madillb98c3a82015-07-23 14:26:04 -04002026 TIntermSymbol *symbol =
2027 intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002028 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002029 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002030
2031 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04002032 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002033
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002034 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04002035}
2036
Jamie Madillb98c3a82015-07-23 14:26:04 -04002037TIntermAggregate *TParseContext::parseInitDeclarator(const TPublicType &publicType,
2038 TIntermAggregate *aggregateDeclaration,
2039 const TSourceLoc &identifierLocation,
2040 const TString &identifier,
2041 const TSourceLoc &initLocation,
2042 TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04002043{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002044 // If the declaration starting this declarator list was empty (example: int,), some checks were
2045 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002046 if (mDeferredSingleDeclarationErrorCheck)
2047 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002048 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002049 mDeferredSingleDeclarationErrorCheck = false;
2050 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002051
Olli Etuaho856c4972016-08-08 11:38:39 +03002052 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002053
Olli Etuahoe7847b02015-03-16 11:56:12 +02002054 TIntermNode *intermNode = nullptr;
2055 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002056 {
2057 //
2058 // build the intermediate representation
2059 //
2060 if (intermNode)
2061 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02002062 return intermediate.growAggregate(aggregateDeclaration, intermNode, initLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04002063 }
2064 else
2065 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02002066 return aggregateDeclaration;
Jamie Madill502d66f2013-06-20 11:55:52 -04002067 }
2068 }
2069 else
2070 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02002071 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04002072 }
2073}
2074
Jamie Madill06145232015-05-13 13:10:01 -04002075TIntermAggregate *TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002076 TIntermAggregate *aggregateDeclaration,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302077 const TSourceLoc &identifierLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002078 const TString &identifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302079 const TSourceLoc &indexLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002080 TIntermTyped *indexExpression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002081 const TSourceLoc &initLocation,
2082 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002083{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002084 // If the declaration starting this declarator list was empty (example: int,), some checks were
2085 // not performed.
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002086 if (mDeferredSingleDeclarationErrorCheck)
2087 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002088 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002089 mDeferredSingleDeclarationErrorCheck = false;
2090 }
2091
Olli Etuaho856c4972016-08-08 11:38:39 +03002092 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002093
Olli Etuaho8a176262016-08-16 14:23:01 +03002094 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002095
2096 TPublicType arrayType(publicType);
2097
Olli Etuaho856c4972016-08-08 11:38:39 +03002098 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002099 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2100 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002101 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002102 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002103 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002104 }
2105 // Make the type an array even if size check failed.
2106 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2107 arrayType.setArraySize(size);
2108
2109 // initNode will correspond to the whole of "b[n] = initializer".
2110 TIntermNode *initNode = nullptr;
2111 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
2112 {
2113 if (initNode)
2114 {
2115 return intermediate.growAggregate(aggregateDeclaration, initNode, initLocation);
2116 }
2117 else
2118 {
2119 return aggregateDeclaration;
2120 }
2121 }
2122 else
2123 {
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002124 return nullptr;
2125 }
2126}
2127
Martin Radev70866b82016-07-22 15:27:42 +03002128void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002129{
Olli Etuaho613b9592016-09-05 12:05:53 +03002130 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002131 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002132
Martin Radev70866b82016-07-22 15:27:42 +03002133 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2134 typeQualifier.line);
2135
Jamie Madillc2128ff2016-07-04 10:26:17 -04002136 // It should never be the case, but some strange parser errors can send us here.
2137 if (layoutQualifier.isEmpty())
2138 {
2139 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002140 return;
2141 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002142
Martin Radev802abe02016-08-04 17:48:32 +03002143 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002144 {
Martin Radev802abe02016-08-04 17:48:32 +03002145 error(typeQualifier.line, "invalid combination:", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002146 return;
2147 }
2148
Martin Radev2cc85b32016-08-05 16:22:53 +03002149 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
2150
2151 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2152
Martin Radev802abe02016-08-04 17:48:32 +03002153 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002154 {
Martin Radev802abe02016-08-04 17:48:32 +03002155 if (mComputeShaderLocalSizeDeclared &&
2156 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2157 {
2158 error(typeQualifier.line, "Work group size does not match the previous declaration",
2159 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002160 return;
2161 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002162
Martin Radev802abe02016-08-04 17:48:32 +03002163 if (mShaderVersion < 310)
2164 {
2165 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002166 return;
2167 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002168
Martin Radev4c4c8e72016-08-04 12:25:34 +03002169 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03002170 {
2171 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002172 return;
2173 }
2174
2175 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
2176 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
2177
2178 const TConstantUnion *maxComputeWorkGroupSizeData =
2179 maxComputeWorkGroupSize->getConstPointer();
2180
2181 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
2182 {
2183 if (layoutQualifier.localSize[i] != -1)
2184 {
2185 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
2186 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
2187 if (mComputeShaderLocalSize[i] < 1 ||
2188 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
2189 {
2190 std::stringstream errorMessageStream;
2191 errorMessageStream << "Value must be at least 1 and no greater than "
2192 << maxComputeWorkGroupSizeValue;
2193 const std::string &errorMessage = errorMessageStream.str();
2194
Martin Radev4c4c8e72016-08-04 12:25:34 +03002195 error(typeQualifier.line, "invalid value:", getWorkGroupSizeString(i),
Martin Radev802abe02016-08-04 17:48:32 +03002196 errorMessage.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03002197 return;
2198 }
2199 }
2200 }
2201
2202 mComputeShaderLocalSizeDeclared = true;
2203 }
2204 else
Jamie Madill1566ef72013-06-20 11:55:54 -04002205 {
Martin Radev802abe02016-08-04 17:48:32 +03002206
Olli Etuaho8a176262016-08-16 14:23:01 +03002207 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03002208 {
Martin Radev802abe02016-08-04 17:48:32 +03002209 return;
2210 }
2211
2212 if (typeQualifier.qualifier != EvqUniform)
2213 {
2214 error(typeQualifier.line, "invalid qualifier:",
2215 getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
Martin Radev802abe02016-08-04 17:48:32 +03002216 return;
2217 }
2218
2219 if (mShaderVersion < 300)
2220 {
2221 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
2222 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002223 return;
2224 }
2225
Olli Etuaho856c4972016-08-08 11:38:39 +03002226 checkLocationIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002227
2228 if (layoutQualifier.matrixPacking != EmpUnspecified)
2229 {
2230 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
2231 }
2232
2233 if (layoutQualifier.blockStorage != EbsUnspecified)
2234 {
2235 mDefaultBlockStorage = layoutQualifier.blockStorage;
2236 }
Jamie Madill1566ef72013-06-20 11:55:54 -04002237 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002238}
2239
Olli Etuaho476197f2016-10-11 13:59:08 +01002240TIntermAggregate *TParseContext::addFunctionPrototypeDeclaration(const TFunction &parsedFunction,
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002241 const TSourceLoc &location)
2242{
Olli Etuaho476197f2016-10-11 13:59:08 +01002243 // Note: function found from the symbol table could be the same as parsedFunction if this is the
2244 // first declaration. Either way the instance in the symbol table is used to track whether the
2245 // function is declared multiple times.
2246 TFunction *function = static_cast<TFunction *>(
2247 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
2248 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02002249 {
2250 // ESSL 1.00.17 section 4.2.7.
2251 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
2252 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02002253 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002254 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02002255
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002256 TIntermAggregate *prototype = new TIntermAggregate;
Olli Etuaho476197f2016-10-11 13:59:08 +01002257 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
2258 // point to the data that already exists in the symbol table.
2259 prototype->setType(function->getReturnType());
Olli Etuahobd674552016-10-06 13:28:42 +01002260 prototype->getFunctionSymbolInfo()->setFromFunction(*function);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002261
Olli Etuaho476197f2016-10-11 13:59:08 +01002262 for (size_t i = 0; i < function->getParamCount(); i++)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002263 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002264 const TConstParameter &param = function->getParam(i);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002265 if (param.name != 0)
2266 {
2267 TVariable variable(param.name, *param.type);
2268
2269 TIntermSymbol *paramSymbol = intermediate.addSymbol(
2270 variable.getUniqueId(), variable.getName(), variable.getType(), location);
2271 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
2272 }
2273 else
2274 {
2275 TIntermSymbol *paramSymbol = intermediate.addSymbol(0, "", *param.type, location);
2276 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
2277 }
2278 }
2279
2280 prototype->setOp(EOpPrototype);
2281
2282 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002283
2284 if (!symbolTable.atGlobalLevel())
2285 {
2286 // ESSL 3.00.4 section 4.2.4.
2287 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002288 }
2289
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002290 return prototype;
2291}
2292
Olli Etuaho336b1472016-10-05 16:37:55 +01002293TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
2294 const TFunction &function,
2295 TIntermAggregate *functionParameters,
2296 TIntermBlock *functionBody,
2297 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002298{
Olli Etuahof51fdd22016-10-03 10:03:40 +01002299 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002300 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
2301 {
2302 error(location, "function does not return a value:", "", function.getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002303 }
2304
Olli Etuahof51fdd22016-10-03 10:03:40 +01002305 if (functionBody == nullptr)
2306 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01002307 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002308 functionBody->setLine(location);
2309 }
Olli Etuaho336b1472016-10-05 16:37:55 +01002310 TIntermFunctionDefinition *functionNode =
2311 new TIntermFunctionDefinition(function.getReturnType(), functionParameters, functionBody);
2312 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01002313
Olli Etuahobd674552016-10-06 13:28:42 +01002314 functionNode->getFunctionSymbolInfo()->setFromFunction(function);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002315
2316 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002317 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002318}
2319
Olli Etuaho476197f2016-10-11 13:59:08 +01002320void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
2321 TFunction **function,
2322 TIntermAggregate **aggregateOut)
Jamie Madill185fb402015-06-12 15:48:48 -04002323{
Olli Etuaho476197f2016-10-11 13:59:08 +01002324 ASSERT(function);
2325 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002326 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01002327 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002328
2329 if (builtIn)
2330 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002331 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002332 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002333 else
Jamie Madill185fb402015-06-12 15:48:48 -04002334 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002335 TFunction *prevDec = static_cast<TFunction *>(
2336 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
2337
2338 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
2339 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
2340 // occurance.
2341 if (*function != prevDec)
2342 {
2343 // Swap the parameters of the previous declaration to the parameters of the function
2344 // definition (parameter names may differ).
2345 prevDec->swapParameters(**function);
2346
2347 // The function definition will share the same symbol as any previous declaration.
2348 *function = prevDec;
2349 }
2350
2351 if ((*function)->isDefined())
2352 {
2353 error(location, "function already has a body", (*function)->getName().c_str());
2354 }
2355
2356 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04002357 }
Jamie Madill185fb402015-06-12 15:48:48 -04002358
2359 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuaho476197f2016-10-11 13:59:08 +01002360 if ((*function)->getName() == "main")
Jamie Madill185fb402015-06-12 15:48:48 -04002361 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002362 if ((*function)->getParamCount() > 0)
Jamie Madill185fb402015-06-12 15:48:48 -04002363 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002364 error(location, "function cannot take any parameter(s)",
2365 (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002366 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002367 if ((*function)->getReturnType().getBasicType() != EbtVoid)
Jamie Madill185fb402015-06-12 15:48:48 -04002368 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002369 error(location, "", (*function)->getReturnType().getBasicString(),
Jamie Madillb98c3a82015-07-23 14:26:04 -04002370 "main function cannot return a value");
Jamie Madill185fb402015-06-12 15:48:48 -04002371 }
2372 }
2373
2374 //
2375 // Remember the return type for later checking for RETURN statements.
2376 //
Olli Etuaho476197f2016-10-11 13:59:08 +01002377 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002378 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04002379
2380 //
2381 // Insert parameters into the symbol table.
2382 // If the parameter has no name, it's not an error, just don't insert it
2383 // (could be used for unused args).
2384 //
2385 // Also, accumulate the list of parameters into the HIL, so lower level code
2386 // knows where to find parameters.
2387 //
2388 TIntermAggregate *paramNodes = new TIntermAggregate;
Olli Etuaho476197f2016-10-11 13:59:08 +01002389 for (size_t i = 0; i < (*function)->getParamCount(); i++)
Jamie Madill185fb402015-06-12 15:48:48 -04002390 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002391 const TConstParameter &param = (*function)->getParam(i);
Jamie Madill185fb402015-06-12 15:48:48 -04002392 if (param.name != 0)
2393 {
2394 TVariable *variable = new TVariable(param.name, *param.type);
2395 //
2396 // Insert the parameters with name in the symbol table.
2397 //
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002398 if (!symbolTable.declare(variable))
2399 {
Jamie Madill185fb402015-06-12 15:48:48 -04002400 error(location, "redefinition", variable->getName().c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002401 paramNodes = intermediate.growAggregate(
2402 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
2403 continue;
Jamie Madill185fb402015-06-12 15:48:48 -04002404 }
2405
2406 //
2407 // Add the parameter to the HIL
2408 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002409 TIntermSymbol *symbol = intermediate.addSymbol(
2410 variable->getUniqueId(), variable->getName(), variable->getType(), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002411
2412 paramNodes = intermediate.growAggregate(paramNodes, symbol, location);
2413 }
2414 else
2415 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002416 paramNodes = intermediate.growAggregate(
2417 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002418 }
2419 }
2420 intermediate.setAggregateOperator(paramNodes, EOpParameters, location);
2421 *aggregateOut = paramNodes;
2422 setLoopNestingLevel(0);
2423}
2424
Jamie Madillb98c3a82015-07-23 14:26:04 -04002425TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002426{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002427 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002428 // We don't know at this point whether this is a function definition or a prototype.
2429 // The definition production code will check for redefinitions.
2430 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002431 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002432 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
2433 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002434 //
2435 TFunction *prevDec =
2436 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302437
2438 if (getShaderVersion() >= 300 && symbolTable.hasUnmangledBuiltIn(function->getName().c_str()))
2439 {
2440 // With ESSL 3.00, names of built-in functions cannot be redeclared as functions.
2441 // Therefore overloading or redefining builtin functions is an error.
2442 error(location, "Name of a built-in function cannot be redeclared as function",
2443 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302444 }
2445 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002446 {
2447 if (prevDec->getReturnType() != function->getReturnType())
2448 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002449 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002450 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04002451 }
2452 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2453 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002454 if (prevDec->getParam(i).type->getQualifier() !=
2455 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002456 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002457 error(location,
2458 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002459 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04002460 }
2461 }
2462 }
2463
2464 //
2465 // Check for previously declared variables using the same name.
2466 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002467 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002468 if (prevSym)
2469 {
2470 if (!prevSym->isFunction())
2471 {
2472 error(location, "redefinition", function->getName().c_str(), "function");
Jamie Madill185fb402015-06-12 15:48:48 -04002473 }
2474 }
2475 else
2476 {
2477 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01002478 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04002479 }
2480
2481 // We're at the inner scope level of the function's arguments and body statement.
2482 // Add the function prototype to the surrounding scope instead.
2483 symbolTable.getOuterLevel()->insert(function);
2484
2485 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002486 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2487 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002488 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2489 //
2490 return function;
2491}
2492
Olli Etuaho9de84a52016-06-14 17:36:01 +03002493TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
2494 const TString *name,
2495 const TSourceLoc &location)
2496{
2497 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
2498 {
2499 error(location, "no qualifiers allowed for function return",
2500 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03002501 }
2502 if (!type.layoutQualifier.isEmpty())
2503 {
2504 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03002505 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002506 // make sure a sampler or an image is not involved as well...
Martin Radev4a9cd802016-09-01 16:51:51 +03002507 checkIsNotSampler(location, type.typeSpecifierNonArray,
2508 "samplers can't be function return values");
Martin Radev2cc85b32016-08-05 16:22:53 +03002509 checkIsNotImage(location, type.typeSpecifierNonArray, "images can't be function return values");
Olli Etuahoe29324f2016-06-15 10:58:03 +03002510 if (mShaderVersion < 300)
2511 {
2512 // Array return values are forbidden, but there's also no valid syntax for declaring array
2513 // return values in ESSL 1.00.
2514 ASSERT(type.arraySize == 0 || mDiagnostics.numErrors() > 0);
2515
2516 if (type.isStructureContainingArrays())
2517 {
2518 // ESSL 1.00.17 section 6.1 Function Definitions
2519 error(location, "structures containing arrays can't be function return values",
2520 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03002521 }
2522 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03002523
2524 // Add the function as a prototype after parsing it (we do not support recursion)
2525 return new TFunction(name, new TType(type));
2526}
2527
Jamie Madill06145232015-05-13 13:10:01 -04002528TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002529{
Jamie Madill06145232015-05-13 13:10:01 -04002530 TPublicType publicType = publicTypeIn;
Martin Radev4a9cd802016-09-01 16:51:51 +03002531 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02002532 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002533 error(publicType.getLine(), "constructor can't be a structure definition",
2534 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02002535 }
2536
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002537 TOperator op = EOpNull;
Martin Radev4a9cd802016-09-01 16:51:51 +03002538 if (publicType.getUserDef())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002539 {
2540 op = EOpConstructStruct;
2541 }
2542 else
2543 {
Geoff Lang156d7192016-07-21 16:11:00 -04002544 op = sh::TypeToConstructorOperator(TType(publicType));
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002545 if (op == EOpNull)
2546 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002547 error(publicType.getLine(), "cannot construct this type",
2548 getBasicString(publicType.getBasicType()));
2549 publicType.setBasicType(EbtFloat);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002550 op = EOpConstructFloat;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002551 }
2552 }
2553
2554 TString tempString;
Dmitry Skiba7f17a502015-06-22 15:08:39 -07002555 const TType *type = new TType(publicType);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002556 return new TFunction(&tempString, type, op);
2557}
2558
Jamie Madillb98c3a82015-07-23 14:26:04 -04002559// This function is used to test for the correctness of the parameters passed to various constructor
2560// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002561//
Olli Etuaho856c4972016-08-08 11:38:39 +03002562// 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 +00002563//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002564TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002565 TOperator op,
2566 TFunction *fnCall,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302567 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002568{
Olli Etuaho856c4972016-08-08 11:38:39 +03002569 TType type = fnCall->getReturnType();
2570 if (type.isUnsizedArray())
2571 {
2572 type.setArraySize(static_cast<unsigned int>(fnCall->getParamCount()));
2573 }
2574 bool constType = true;
2575 for (size_t i = 0; i < fnCall->getParamCount(); ++i)
2576 {
2577 const TConstParameter &param = fnCall->getParam(i);
2578 if (param.type->getQualifier() != EvqConst)
2579 constType = false;
2580 }
2581 if (constType)
2582 type.setQualifier(EvqConst);
2583
Olli Etuaho8a176262016-08-16 14:23:01 +03002584 if (!checkConstructorArguments(line, arguments, *fnCall, op, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03002585 {
2586 TIntermTyped *dummyNode = intermediate.setAggregateOperator(nullptr, op, line);
2587 dummyNode->setType(type);
2588 return dummyNode;
2589 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002590 TIntermAggregate *constructor = arguments->getAsAggregate();
2591 ASSERT(constructor != nullptr);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002592
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002593 // Turn the argument list itself into a constructor
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002594 constructor->setOp(op);
2595 constructor->setLine(line);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002596 ASSERT(constructor->isConstructor());
2597
2598 // Need to set type before setPrecisionFromChildren() because bool doesn't have precision.
Olli Etuaho856c4972016-08-08 11:38:39 +03002599 constructor->setType(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002600
Olli Etuaho21203702014-11-13 16:16:21 +02002601 // Structs should not be precision qualified, the individual members may be.
2602 // Built-in types on the other hand should be precision qualified.
2603 if (op != EOpConstructStruct)
2604 {
2605 constructor->setPrecisionFromChildren();
Olli Etuaho856c4972016-08-08 11:38:39 +03002606 type.setPrecision(constructor->getPrecision());
Olli Etuaho21203702014-11-13 16:16:21 +02002607 }
2608
Olli Etuaho856c4972016-08-08 11:38:39 +03002609 constructor->setType(type);
2610
Olli Etuahof119a262016-08-19 15:54:22 +03002611 TIntermTyped *constConstructor = intermediate.foldAggregateBuiltIn(constructor, &mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002612 if (constConstructor)
2613 {
2614 return constConstructor;
2615 }
2616
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002617 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002618}
2619
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002620//
2621// Interface/uniform blocks
2622//
Martin Radev70866b82016-07-22 15:27:42 +03002623TIntermAggregate *TParseContext::addInterfaceBlock(
2624 const TTypeQualifierBuilder &typeQualifierBuilder,
2625 const TSourceLoc &nameLine,
2626 const TString &blockName,
2627 TFieldList *fieldList,
2628 const TString *instanceName,
2629 const TSourceLoc &instanceLine,
2630 TIntermTyped *arrayIndex,
2631 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002632{
Olli Etuaho856c4972016-08-08 11:38:39 +03002633 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002634
Olli Etuaho613b9592016-09-05 12:05:53 +03002635 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03002636
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002637 if (typeQualifier.qualifier != EvqUniform)
2638 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302639 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
2640 "interface blocks must be uniform");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002641 }
2642
Martin Radev70866b82016-07-22 15:27:42 +03002643 if (typeQualifier.invariant)
2644 {
2645 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
2646 }
2647
Martin Radev2cc85b32016-08-05 16:22:53 +03002648 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
2649
Jamie Madill099c0f32013-06-20 11:55:52 -04002650 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03002651 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002652
Jamie Madill099c0f32013-06-20 11:55:52 -04002653 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2654 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002655 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002656 }
2657
Jamie Madill1566ef72013-06-20 11:55:54 -04002658 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2659 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002660 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002661 }
2662
Olli Etuaho856c4972016-08-08 11:38:39 +03002663 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002664
Martin Radev2cc85b32016-08-05 16:22:53 +03002665 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
2666
Arun Patole7e7e68d2015-05-22 12:02:25 +05302667 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2668 if (!symbolTable.declare(blockNameSymbol))
2669 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002670 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002671 }
2672
Jamie Madill98493dd2013-07-08 14:39:03 -04002673 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302674 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2675 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002676 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302677 TType *fieldType = field->type();
2678 if (IsSampler(fieldType->getBasicType()))
2679 {
2680 error(field->line(), "unsupported type", fieldType->getBasicString(),
2681 "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002682 }
2683
Martin Radev2cc85b32016-08-05 16:22:53 +03002684 if (IsImage(fieldType->getBasicType()))
2685 {
2686 error(field->line(), "unsupported type", fieldType->getBasicString(),
2687 "image types are not allowed in interface blocks");
2688 }
2689
Jamie Madill98493dd2013-07-08 14:39:03 -04002690 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002691 switch (qualifier)
2692 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002693 case EvqGlobal:
2694 case EvqUniform:
2695 break;
2696 default:
2697 error(field->line(), "invalid qualifier on interface block member",
2698 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04002699 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002700 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002701
Martin Radev70866b82016-07-22 15:27:42 +03002702 if (fieldType->isInvariant())
2703 {
2704 error(field->line(), "invalid qualifier on interface block member", "invariant");
2705 }
2706
Jamie Madilla5efff92013-06-06 11:56:47 -04002707 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002708 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03002709 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
Jamie Madill099c0f32013-06-20 11:55:52 -04002710
Jamie Madill98493dd2013-07-08 14:39:03 -04002711 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002712 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002713 error(field->line(), "invalid layout qualifier:",
2714 getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04002715 }
2716
Jamie Madill98493dd2013-07-08 14:39:03 -04002717 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002718 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002719 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002720 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002721 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002722 {
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002723 warning(field->line(), "extraneous layout qualifier:",
Jamie Madillb98c3a82015-07-23 14:26:04 -04002724 getMatrixPackingString(fieldLayoutQualifier.matrixPacking),
2725 "only has an effect on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04002726 }
2727
Jamie Madill98493dd2013-07-08 14:39:03 -04002728 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002729 }
2730
Jamie Madill98493dd2013-07-08 14:39:03 -04002731 // add array index
Olli Etuaho856c4972016-08-08 11:38:39 +03002732 unsigned int arraySize = 0;
2733 if (arrayIndex != nullptr)
Jamie Madill98493dd2013-07-08 14:39:03 -04002734 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002735 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04002736 }
2737
Jamie Madillb98c3a82015-07-23 14:26:04 -04002738 TInterfaceBlock *interfaceBlock =
2739 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2740 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
2741 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002742
2743 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04002744 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002745
Jamie Madill98493dd2013-07-08 14:39:03 -04002746 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002747 {
2748 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002749 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2750 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002751 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302752 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002753
2754 // set parent pointer of the field variable
2755 fieldType->setInterfaceBlock(interfaceBlock);
2756
Arun Patole7e7e68d2015-05-22 12:02:25 +05302757 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002758 fieldVariable->setQualifier(typeQualifier.qualifier);
2759
Arun Patole7e7e68d2015-05-22 12:02:25 +05302760 if (!symbolTable.declare(fieldVariable))
2761 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002762 error(field->line(), "redefinition", field->name().c_str(),
2763 "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002764 }
2765 }
2766 }
2767 else
2768 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002769 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03002770
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002771 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302772 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002773 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002774
Arun Patole7e7e68d2015-05-22 12:02:25 +05302775 if (!symbolTable.declare(instanceTypeDef))
2776 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002777 error(instanceLine, "redefinition", instanceName->c_str(),
2778 "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002779 }
2780
Jamie Madillb98c3a82015-07-23 14:26:04 -04002781 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002782 symbolName = instanceTypeDef->getName();
2783 }
2784
Olli Etuaho32db19b2016-10-04 14:43:16 +01002785 TIntermAggregate *aggregate = TIntermediate::MakeAggregate(
Jamie Madillb98c3a82015-07-23 14:26:04 -04002786 intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line),
2787 nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002788 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04002789
2790 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002791 return aggregate;
2792}
2793
Olli Etuaho383b7912016-08-05 11:22:59 +03002794void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002795{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002796 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002797
2798 // Embedded structure definitions are not supported per GLSL ES spec.
2799 // They aren't allowed in GLSL either, but we need to detect this here
2800 // so we don't rely on the GLSL compiler to catch it.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302801 if (mStructNestingLevel > 1)
2802 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002803 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002804 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002805}
2806
2807void TParseContext::exitStructDeclaration()
2808{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002809 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002810}
2811
Olli Etuaho8a176262016-08-16 14:23:01 +03002812void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002813{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302814 if (!IsWebGLBasedSpec(mShaderSpec))
2815 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002816 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002817 }
2818
Arun Patole7e7e68d2015-05-22 12:02:25 +05302819 if (field.type()->getBasicType() != EbtStruct)
2820 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002821 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002822 }
2823
2824 // We're already inside a structure definition at this point, so add
2825 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302826 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2827 {
Jamie Madill41a49272014-03-18 16:10:13 -04002828 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002829 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
2830 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04002831 std::string reason = reasonStream.str();
2832 error(line, reason.c_str(), field.name().c_str(), "");
Olli Etuaho8a176262016-08-16 14:23:01 +03002833 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002834 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002835}
2836
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002837//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002838// Parse an array index expression
2839//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002840TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
2841 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302842 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002843{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002844 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2845 {
2846 if (baseExpression->getAsSymbolNode())
2847 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302848 error(location, " left of '[' is not of type array, matrix, or vector ",
2849 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002850 }
2851 else
2852 {
2853 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2854 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002855
2856 TConstantUnion *unionArray = new TConstantUnion[1];
2857 unionArray->setFConst(0.0f);
2858 return intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst),
2859 location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002860 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002861
Jamie Madill21c1e452014-12-29 11:33:41 -05002862 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2863
Olli Etuaho36b05142015-11-12 13:10:42 +02002864 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
2865 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
2866 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
2867 // index is a constant expression.
2868 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
2869 {
2870 if (baseExpression->isInterfaceBlock())
2871 {
2872 error(
2873 location, "", "[",
2874 "array indexes for interface blocks arrays must be constant integral expressions");
Olli Etuaho36b05142015-11-12 13:10:42 +02002875 }
2876 else if (baseExpression->getQualifier() == EvqFragmentOut)
2877 {
2878 error(location, "", "[",
2879 "array indexes for fragment outputs must be constant integral expressions");
Olli Etuaho36b05142015-11-12 13:10:42 +02002880 }
Olli Etuaho3e960462015-11-12 15:58:39 +02002881 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
2882 {
2883 error(location, "", "[", "array index for gl_FragData must be constant zero");
Olli Etuaho3e960462015-11-12 15:58:39 +02002884 }
Olli Etuaho36b05142015-11-12 13:10:42 +02002885 }
2886
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002887 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002888 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002889 // If an out-of-range index is not qualified as constant, the behavior in the spec is
2890 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
2891 // constant fold expressions that are not constant expressions). The most compatible way to
2892 // handle this case is to report a warning instead of an error and force the index to be in
2893 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002894 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Jamie Madill21c1e452014-12-29 11:33:41 -05002895 int index = indexConstantUnion->getIConst(0);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002896
2897 int safeIndex = -1;
2898
2899 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04002900 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002901 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03002902 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002903 if (mShaderSpec == SH_WEBGL2_SPEC)
2904 {
2905 // Error has been already generated if index is not const.
2906 if (indexExpression->getQualifier() == EvqConst)
2907 {
2908 error(location, "", "[",
2909 "array index for gl_FragData must be constant zero");
2910 }
2911 safeIndex = 0;
2912 }
2913 else if (!isExtensionEnabled("GL_EXT_draw_buffers"))
2914 {
2915 outOfRangeError(outOfRangeIndexIsError, location, "", "[",
2916 "array index for gl_FragData must be zero when "
2917 "GL_EXT_draw_buffers is disabled");
2918 safeIndex = 0;
2919 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03002920 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002921 // Only do generic out-of-range check if similar error hasn't already been reported.
2922 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03002923 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002924 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
2925 baseExpression->getArraySize(),
2926 "array index out of range", "[]");
2927 }
2928 }
2929 else if (baseExpression->isMatrix())
2930 {
2931 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03002932 baseExpression->getType().getCols(),
2933 "matrix field selection out of range", "[]");
Jamie Madill7164cf42013-07-08 13:30:59 -04002934 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002935 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04002936 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002937 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
2938 baseExpression->getType().getNominalSize(),
2939 "vector field selection out of range", "[]");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002940 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002941
2942 ASSERT(safeIndex >= 0);
2943 // Data of constant unions can't be changed, because it may be shared with other
2944 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
2945 // sanitized object.
2946 if (safeIndex != index)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002947 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002948 TConstantUnion *safeConstantUnion = new TConstantUnion();
2949 safeConstantUnion->setIConst(safeIndex);
2950 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002951 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002952
2953 return intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location,
2954 &mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002955 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002956 else
2957 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002958 return intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location,
2959 &mDiagnostics);
Jamie Madill7164cf42013-07-08 13:30:59 -04002960 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002961}
2962
Olli Etuaho90892fb2016-07-14 14:44:51 +03002963int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
2964 const TSourceLoc &location,
2965 int index,
2966 int arraySize,
2967 const char *reason,
2968 const char *token)
2969{
2970 if (index >= arraySize || index < 0)
2971 {
2972 std::stringstream extraInfoStream;
2973 extraInfoStream << "'" << index << "'";
2974 std::string extraInfo = extraInfoStream.str();
2975 outOfRangeError(outOfRangeIndexIsError, location, reason, token, extraInfo.c_str());
2976 if (index < 0)
2977 {
2978 return 0;
2979 }
2980 else
2981 {
2982 return arraySize - 1;
2983 }
2984 }
2985 return index;
2986}
2987
Jamie Madillb98c3a82015-07-23 14:26:04 -04002988TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
2989 const TSourceLoc &dotLocation,
2990 const TString &fieldString,
2991 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002992{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002993 if (baseExpression->isArray())
2994 {
2995 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002996 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002997 }
2998
2999 if (baseExpression->isVector())
3000 {
3001 TVectorFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003002 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields,
3003 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003004 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003005 fields.num = 1;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003006 fields.offsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003007 }
3008
Olli Etuahob6fa0432016-09-28 16:28:05 +01003009 return TIntermediate::AddSwizzle(baseExpression, fields, dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003010 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003011 else if (baseExpression->getBasicType() == EbtStruct)
3012 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303013 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003014 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003015 {
3016 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003017 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003018 }
3019 else
3020 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003021 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003022 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003023 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003024 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003025 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003026 {
3027 fieldFound = true;
3028 break;
3029 }
3030 }
3031 if (fieldFound)
3032 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003033 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3034 index->setLine(fieldLocation);
3035 return intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index,
3036 dotLocation, &mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003037 }
3038 else
3039 {
3040 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003041 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003042 }
3043 }
3044 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003045 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003046 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303047 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003048 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003049 {
3050 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003051 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003052 }
3053 else
3054 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003055 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003056 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003057 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003058 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003059 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003060 {
3061 fieldFound = true;
3062 break;
3063 }
3064 }
3065 if (fieldFound)
3066 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003067 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3068 index->setLine(fieldLocation);
3069 return intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index,
3070 dotLocation, &mDiagnostics);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003071 }
3072 else
3073 {
3074 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003075 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003076 }
3077 }
3078 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003079 else
3080 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003081 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003082 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03003083 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303084 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003085 }
3086 else
3087 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303088 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003089 " field selection requires structure, vector, or interface block on left hand "
3090 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303091 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003092 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003093 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003094 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003095}
3096
Jamie Madillb98c3a82015-07-23 14:26:04 -04003097TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3098 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003099{
Martin Radev802abe02016-08-04 17:48:32 +03003100 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003101
3102 if (qualifierType == "shared")
3103 {
Olli Etuahof0173152016-10-17 09:05:03 -07003104 if (IsWebGLBasedSpec(mShaderSpec))
3105 {
3106 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
3107 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003108 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003109 }
3110 else if (qualifierType == "packed")
3111 {
Olli Etuahof0173152016-10-17 09:05:03 -07003112 if (IsWebGLBasedSpec(mShaderSpec))
3113 {
3114 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
3115 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003116 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003117 }
3118 else if (qualifierType == "std140")
3119 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003120 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003121 }
3122 else if (qualifierType == "row_major")
3123 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003124 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003125 }
3126 else if (qualifierType == "column_major")
3127 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003128 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003129 }
3130 else if (qualifierType == "location")
3131 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003132 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
3133 "location requires an argument");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003134 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003135 else if (qualifierType == "rgba32f")
3136 {
3137 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3138 qualifier.imageInternalFormat = EiifRGBA32F;
3139 }
3140 else if (qualifierType == "rgba16f")
3141 {
3142 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3143 qualifier.imageInternalFormat = EiifRGBA16F;
3144 }
3145 else if (qualifierType == "r32f")
3146 {
3147 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3148 qualifier.imageInternalFormat = EiifR32F;
3149 }
3150 else if (qualifierType == "rgba8")
3151 {
3152 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3153 qualifier.imageInternalFormat = EiifRGBA8;
3154 }
3155 else if (qualifierType == "rgba8_snorm")
3156 {
3157 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3158 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
3159 }
3160 else if (qualifierType == "rgba32i")
3161 {
3162 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3163 qualifier.imageInternalFormat = EiifRGBA32I;
3164 }
3165 else if (qualifierType == "rgba16i")
3166 {
3167 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3168 qualifier.imageInternalFormat = EiifRGBA16I;
3169 }
3170 else if (qualifierType == "rgba8i")
3171 {
3172 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3173 qualifier.imageInternalFormat = EiifRGBA8I;
3174 }
3175 else if (qualifierType == "r32i")
3176 {
3177 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3178 qualifier.imageInternalFormat = EiifR32I;
3179 }
3180 else if (qualifierType == "rgba32ui")
3181 {
3182 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3183 qualifier.imageInternalFormat = EiifRGBA32UI;
3184 }
3185 else if (qualifierType == "rgba16ui")
3186 {
3187 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3188 qualifier.imageInternalFormat = EiifRGBA16UI;
3189 }
3190 else if (qualifierType == "rgba8ui")
3191 {
3192 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3193 qualifier.imageInternalFormat = EiifRGBA8UI;
3194 }
3195 else if (qualifierType == "r32ui")
3196 {
3197 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3198 qualifier.imageInternalFormat = EiifR32UI;
3199 }
3200
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003201 else
3202 {
3203 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003204 }
3205
Jamie Madilla5efff92013-06-06 11:56:47 -04003206 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003207}
3208
Martin Radev802abe02016-08-04 17:48:32 +03003209void TParseContext::parseLocalSize(const TString &qualifierType,
3210 const TSourceLoc &qualifierTypeLine,
3211 int intValue,
3212 const TSourceLoc &intValueLine,
3213 const std::string &intValueString,
3214 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03003215 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03003216{
Olli Etuaho856c4972016-08-08 11:38:39 +03003217 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03003218 if (intValue < 1)
3219 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03003220 std::string errorMessage = std::string(getWorkGroupSizeString(index)) + " must be positive";
Martin Radev802abe02016-08-04 17:48:32 +03003221 error(intValueLine, "out of range:", intValueString.c_str(), errorMessage.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003222 }
3223 (*localSize)[index] = intValue;
3224}
3225
Jamie Madillb98c3a82015-07-23 14:26:04 -04003226TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3227 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003228 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303229 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003230{
Martin Radev802abe02016-08-04 17:48:32 +03003231 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003232
Martin Radev802abe02016-08-04 17:48:32 +03003233 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003234
Martin Radev802abe02016-08-04 17:48:32 +03003235 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003236 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003237 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003238 if (intValue < 0)
3239 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003240 error(intValueLine, "out of range:", intValueString.c_str(),
3241 "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003242 }
3243 else
3244 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003245 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03003246 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003247 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003248 }
Martin Radev802abe02016-08-04 17:48:32 +03003249 else if (qualifierType == "local_size_x")
3250 {
3251 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
3252 &qualifier.localSize);
3253 }
3254 else if (qualifierType == "local_size_y")
3255 {
3256 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
3257 &qualifier.localSize);
3258 }
3259 else if (qualifierType == "local_size_z")
3260 {
3261 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
3262 &qualifier.localSize);
3263 }
3264 else
3265 {
3266 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003267 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003268
Jamie Madilla5efff92013-06-06 11:56:47 -04003269 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003270}
3271
Olli Etuaho613b9592016-09-05 12:05:53 +03003272TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
3273{
3274 return new TTypeQualifierBuilder(
3275 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
3276 mShaderVersion);
3277}
3278
Jamie Madillb98c3a82015-07-23 14:26:04 -04003279TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03003280 TLayoutQualifier rightQualifier,
3281 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003282{
Martin Radevc28888b2016-07-22 15:27:42 +03003283 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
3284 &mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003285}
3286
Martin Radev70866b82016-07-22 15:27:42 +03003287TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
3288 const TTypeQualifierBuilder &typeQualifierBuilder,
3289 TPublicType *typeSpecifier,
3290 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003291{
Olli Etuaho613b9592016-09-05 12:05:53 +03003292 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003293
Martin Radev70866b82016-07-22 15:27:42 +03003294 typeSpecifier->qualifier = typeQualifier.qualifier;
3295 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03003296 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03003297 typeSpecifier->invariant = typeQualifier.invariant;
3298 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05303299 {
Martin Radev70866b82016-07-22 15:27:42 +03003300 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003301 }
Martin Radev70866b82016-07-22 15:27:42 +03003302 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003303}
3304
Jamie Madillb98c3a82015-07-23 14:26:04 -04003305TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3306 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003307{
Martin Radev4a9cd802016-09-01 16:51:51 +03003308 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
3309 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03003310
Martin Radev4a9cd802016-09-01 16:51:51 +03003311 checkIsNonVoid(typeSpecifier.getLine(), (*fieldList)[0]->name(), typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003312
Martin Radev4a9cd802016-09-01 16:51:51 +03003313 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003314
Arun Patole7e7e68d2015-05-22 12:02:25 +05303315 for (unsigned int i = 0; i < fieldList->size(); ++i)
3316 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003317 //
3318 // Careful not to replace already known aspects of type, like array-ness
3319 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303320 TType *type = (*fieldList)[i]->type();
Martin Radev4a9cd802016-09-01 16:51:51 +03003321 type->setBasicType(typeSpecifier.getBasicType());
3322 type->setPrimarySize(typeSpecifier.getPrimarySize());
3323 type->setSecondarySize(typeSpecifier.getSecondarySize());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003324 type->setPrecision(typeSpecifier.precision);
3325 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003326 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
Martin Radev2cc85b32016-08-05 16:22:53 +03003327 type->setMemoryQualifier(typeSpecifier.memoryQualifier);
Martin Radev70866b82016-07-22 15:27:42 +03003328 type->setInvariant(typeSpecifier.invariant);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003329
3330 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303331 if (type->isArray())
3332 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003333 checkIsValidTypeForArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003334 }
3335 if (typeSpecifier.array)
Olli Etuaho856c4972016-08-08 11:38:39 +03003336 type->setArraySize(static_cast<unsigned int>(typeSpecifier.arraySize));
Martin Radev4a9cd802016-09-01 16:51:51 +03003337 if (typeSpecifier.getUserDef())
Arun Patole7e7e68d2015-05-22 12:02:25 +05303338 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003339 type->setStruct(typeSpecifier.getUserDef()->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003340 }
3341
Martin Radev4a9cd802016-09-01 16:51:51 +03003342 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*fieldList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003343 }
3344
Jamie Madill98493dd2013-07-08 14:39:03 -04003345 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003346}
3347
Martin Radev4a9cd802016-09-01 16:51:51 +03003348TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
3349 const TSourceLoc &nameLine,
3350 const TString *structName,
3351 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003352{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303353 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003354 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003355
Jamie Madill9b820842015-02-12 10:40:10 -05003356 // Store a bool in the struct if we're at global scope, to allow us to
3357 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05003358 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003359
Jamie Madill98493dd2013-07-08 14:39:03 -04003360 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003361 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003362 checkIsNotReserved(nameLine, *structName);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303363 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
3364 if (!symbolTable.declare(userTypeDef))
3365 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003366 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003367 }
3368 }
3369
3370 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04003371 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003372 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003373 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04003374 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003375 switch (qualifier)
3376 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003377 case EvqGlobal:
3378 case EvqTemporary:
3379 break;
3380 default:
3381 error(field.line(), "invalid qualifier on struct member",
3382 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003383 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003384 }
Martin Radev70866b82016-07-22 15:27:42 +03003385 if (field.type()->isInvariant())
3386 {
3387 error(field.line(), "invalid qualifier on struct member", "invariant");
3388 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003389 if (IsImage(field.type()->getBasicType()))
3390 {
3391 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
3392 }
3393
3394 checkIsMemoryQualifierNotSpecified(field.type()->getMemoryQualifier(), field.line());
Martin Radev70866b82016-07-22 15:27:42 +03003395
3396 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003397 }
3398
Martin Radev4a9cd802016-09-01 16:51:51 +03003399 TTypeSpecifierNonArray typeSpecifierNonArray;
3400 typeSpecifierNonArray.initialize(EbtStruct, structLine);
3401 typeSpecifierNonArray.userDef = structureType;
3402 typeSpecifierNonArray.isStructSpecifier = true;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003403 exitStructDeclaration();
3404
Martin Radev4a9cd802016-09-01 16:51:51 +03003405 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003406}
3407
Jamie Madillb98c3a82015-07-23 14:26:04 -04003408TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003409 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003410 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02003411{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003412 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003413 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02003414 init->isVector())
3415 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003416 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
3417 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003418 return nullptr;
3419 }
3420
Olli Etuahoac5274d2015-02-20 10:19:08 +02003421 if (statementList)
3422 {
3423 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
3424 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02003425 return nullptr;
3426 }
3427 }
3428
Olli Etuahoa3a36662015-02-17 13:46:51 +02003429 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3430 if (node == nullptr)
3431 {
3432 error(loc, "erroneous switch statement", "switch");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003433 return nullptr;
3434 }
3435 return node;
3436}
3437
3438TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3439{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003440 if (mSwitchNestingLevel == 0)
3441 {
3442 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003443 return nullptr;
3444 }
3445 if (condition == nullptr)
3446 {
3447 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003448 return nullptr;
3449 }
3450 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04003451 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02003452 {
3453 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003454 }
3455 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003456 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
3457 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
3458 // fold in case labels.
3459 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02003460 {
3461 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003462 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003463 TIntermCase *node = intermediate.addCase(condition, loc);
3464 if (node == nullptr)
3465 {
3466 error(loc, "erroneous case statement", "case");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003467 return nullptr;
3468 }
3469 return node;
3470}
3471
3472TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3473{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003474 if (mSwitchNestingLevel == 0)
3475 {
3476 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003477 return nullptr;
3478 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003479 TIntermCase *node = intermediate.addCase(nullptr, loc);
3480 if (node == nullptr)
3481 {
3482 error(loc, "erroneous default statement", "default");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003483 return nullptr;
3484 }
3485 return node;
3486}
3487
Jamie Madillb98c3a82015-07-23 14:26:04 -04003488TIntermTyped *TParseContext::createUnaryMath(TOperator op,
3489 TIntermTyped *child,
3490 const TSourceLoc &loc,
3491 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003492{
3493 if (child == nullptr)
3494 {
3495 return nullptr;
3496 }
3497
3498 switch (op)
3499 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003500 case EOpLogicalNot:
3501 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
3502 child->isVector())
3503 {
3504 return nullptr;
3505 }
3506 break;
3507 case EOpBitwiseNot:
3508 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3509 child->isMatrix() || child->isArray())
3510 {
3511 return nullptr;
3512 }
3513 break;
3514 case EOpPostIncrement:
3515 case EOpPreIncrement:
3516 case EOpPostDecrement:
3517 case EOpPreDecrement:
3518 case EOpNegative:
3519 case EOpPositive:
3520 if (child->getBasicType() == EbtStruct || child->getBasicType() == EbtBool ||
Martin Radev2cc85b32016-08-05 16:22:53 +03003521 child->isArray() || IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04003522 {
3523 return nullptr;
3524 }
3525 // Operators for built-ins are already type checked against their prototype.
3526 default:
3527 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003528 }
3529
Olli Etuahof119a262016-08-19 15:54:22 +03003530 TIntermUnary *node = new TIntermUnary(op, child);
3531 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03003532
3533 TIntermTyped *foldedNode = node->fold(&mDiagnostics);
3534 if (foldedNode)
3535 return foldedNode;
3536
3537 return node;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003538}
3539
Olli Etuaho09b22472015-02-11 11:47:26 +02003540TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3541{
Olli Etuahof6c694b2015-03-26 14:50:53 +02003542 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003543 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003544 {
3545 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003546 return child;
3547 }
3548 return node;
3549}
3550
Jamie Madillb98c3a82015-07-23 14:26:04 -04003551TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
3552 TIntermTyped *child,
3553 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003554{
Olli Etuaho856c4972016-08-08 11:38:39 +03003555 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02003556 return addUnaryMath(op, child, loc);
3557}
3558
Jamie Madillb98c3a82015-07-23 14:26:04 -04003559bool TParseContext::binaryOpCommonCheck(TOperator op,
3560 TIntermTyped *left,
3561 TIntermTyped *right,
3562 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003563{
Olli Etuaho244be012016-08-18 15:26:02 +03003564 if (left->getType().getStruct() || right->getType().getStruct())
3565 {
3566 switch (op)
3567 {
3568 case EOpIndexDirectStruct:
3569 ASSERT(left->getType().getStruct());
3570 break;
3571 case EOpEqual:
3572 case EOpNotEqual:
3573 case EOpAssign:
3574 case EOpInitialize:
3575 if (left->getType() != right->getType())
3576 {
3577 return false;
3578 }
3579 break;
3580 default:
3581 error(loc, "Invalid operation for structs", GetOperatorString(op));
3582 return false;
3583 }
3584 }
3585
Olli Etuahod6b14282015-03-17 14:31:35 +02003586 if (left->isArray() || right->isArray())
3587 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003588 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003589 {
3590 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3591 return false;
3592 }
3593
3594 if (left->isArray() != right->isArray())
3595 {
3596 error(loc, "array / non-array mismatch", GetOperatorString(op));
3597 return false;
3598 }
3599
3600 switch (op)
3601 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003602 case EOpEqual:
3603 case EOpNotEqual:
3604 case EOpAssign:
3605 case EOpInitialize:
3606 break;
3607 default:
3608 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3609 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02003610 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003611 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003612 if (left->getArraySize() != right->getArraySize())
3613 {
3614 error(loc, "array size mismatch", GetOperatorString(op));
3615 return false;
3616 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003617 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003618
3619 // Check ops which require integer / ivec parameters
3620 bool isBitShift = false;
3621 switch (op)
3622 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003623 case EOpBitShiftLeft:
3624 case EOpBitShiftRight:
3625 case EOpBitShiftLeftAssign:
3626 case EOpBitShiftRightAssign:
3627 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3628 // check that the basic type is an integer type.
3629 isBitShift = true;
3630 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3631 {
3632 return false;
3633 }
3634 break;
3635 case EOpBitwiseAnd:
3636 case EOpBitwiseXor:
3637 case EOpBitwiseOr:
3638 case EOpBitwiseAndAssign:
3639 case EOpBitwiseXorAssign:
3640 case EOpBitwiseOrAssign:
3641 // It is enough to check the type of only one operand, since later it
3642 // is checked that the operand types match.
3643 if (!IsInteger(left->getBasicType()))
3644 {
3645 return false;
3646 }
3647 break;
3648 default:
3649 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003650 }
3651
3652 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3653 // So the basic type should usually match.
3654 if (!isBitShift && left->getBasicType() != right->getBasicType())
3655 {
3656 return false;
3657 }
3658
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003659 // Check that:
3660 // 1. Type sizes match exactly on ops that require that.
3661 // 2. Restrictions for structs that contain arrays or samplers are respected.
3662 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003663 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003664 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003665 case EOpAssign:
3666 case EOpInitialize:
3667 case EOpEqual:
3668 case EOpNotEqual:
3669 // ESSL 1.00 sections 5.7, 5.8, 5.9
3670 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
3671 {
3672 error(loc, "undefined operation for structs containing arrays",
3673 GetOperatorString(op));
3674 return false;
3675 }
3676 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3677 // we interpret the spec so that this extends to structs containing samplers,
3678 // similarly to ESSL 1.00 spec.
3679 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
3680 left->getType().isStructureContainingSamplers())
3681 {
3682 error(loc, "undefined operation for structs containing samplers",
3683 GetOperatorString(op));
3684 return false;
3685 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003686
3687 if ((op == EOpAssign || op == EOpInitialize) &&
3688 left->getType().isStructureContainingImages())
3689 {
3690 error(loc, "undefined operation for structs containing images",
3691 GetOperatorString(op));
3692 return false;
3693 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003694 case EOpLessThan:
3695 case EOpGreaterThan:
3696 case EOpLessThanEqual:
3697 case EOpGreaterThanEqual:
3698 if ((left->getNominalSize() != right->getNominalSize()) ||
3699 (left->getSecondarySize() != right->getSecondarySize()))
3700 {
3701 return false;
3702 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003703 break;
3704 case EOpAdd:
3705 case EOpSub:
3706 case EOpDiv:
3707 case EOpIMod:
3708 case EOpBitShiftLeft:
3709 case EOpBitShiftRight:
3710 case EOpBitwiseAnd:
3711 case EOpBitwiseXor:
3712 case EOpBitwiseOr:
3713 case EOpAddAssign:
3714 case EOpSubAssign:
3715 case EOpDivAssign:
3716 case EOpIModAssign:
3717 case EOpBitShiftLeftAssign:
3718 case EOpBitShiftRightAssign:
3719 case EOpBitwiseAndAssign:
3720 case EOpBitwiseXorAssign:
3721 case EOpBitwiseOrAssign:
3722 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
3723 {
3724 return false;
3725 }
3726
3727 // Are the sizes compatible?
3728 if (left->getNominalSize() != right->getNominalSize() ||
3729 left->getSecondarySize() != right->getSecondarySize())
3730 {
3731 // If the nominal sizes of operands do not match:
3732 // One of them must be a scalar.
3733 if (!left->isScalar() && !right->isScalar())
3734 return false;
3735
3736 // In the case of compound assignment other than multiply-assign,
3737 // the right side needs to be a scalar. Otherwise a vector/matrix
3738 // would be assigned to a scalar. A scalar can't be shifted by a
3739 // vector either.
3740 if (!right->isScalar() &&
3741 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
3742 return false;
3743 }
3744 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003745 default:
3746 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003747 }
3748
Olli Etuahod6b14282015-03-17 14:31:35 +02003749 return true;
3750}
3751
Olli Etuaho1dded802016-08-18 18:13:13 +03003752bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
3753 const TType &left,
3754 const TType &right)
3755{
3756 switch (op)
3757 {
3758 case EOpMul:
3759 case EOpMulAssign:
3760 return left.getNominalSize() == right.getNominalSize() &&
3761 left.getSecondarySize() == right.getSecondarySize();
3762 case EOpVectorTimesScalar:
3763 return true;
3764 case EOpVectorTimesScalarAssign:
3765 ASSERT(!left.isMatrix() && !right.isMatrix());
3766 return left.isVector() && !right.isVector();
3767 case EOpVectorTimesMatrix:
3768 return left.getNominalSize() == right.getRows();
3769 case EOpVectorTimesMatrixAssign:
3770 ASSERT(!left.isMatrix() && right.isMatrix());
3771 return left.isVector() && left.getNominalSize() == right.getRows() &&
3772 left.getNominalSize() == right.getCols();
3773 case EOpMatrixTimesVector:
3774 return left.getCols() == right.getNominalSize();
3775 case EOpMatrixTimesScalar:
3776 return true;
3777 case EOpMatrixTimesScalarAssign:
3778 ASSERT(left.isMatrix() && !right.isMatrix());
3779 return !right.isVector();
3780 case EOpMatrixTimesMatrix:
3781 return left.getCols() == right.getRows();
3782 case EOpMatrixTimesMatrixAssign:
3783 ASSERT(left.isMatrix() && right.isMatrix());
3784 // We need to check two things:
3785 // 1. The matrix multiplication step is valid.
3786 // 2. The result will have the same number of columns as the lvalue.
3787 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
3788
3789 default:
3790 UNREACHABLE();
3791 return false;
3792 }
3793}
3794
Jamie Madillb98c3a82015-07-23 14:26:04 -04003795TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
3796 TIntermTyped *left,
3797 TIntermTyped *right,
3798 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02003799{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003800 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003801 return nullptr;
3802
Olli Etuahofc1806e2015-03-17 13:03:11 +02003803 switch (op)
3804 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003805 case EOpEqual:
3806 case EOpNotEqual:
3807 break;
3808 case EOpLessThan:
3809 case EOpGreaterThan:
3810 case EOpLessThanEqual:
3811 case EOpGreaterThanEqual:
Olli Etuaho244be012016-08-18 15:26:02 +03003812 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3813 !right->getType().getStruct());
3814 if (left->isMatrix() || left->isVector())
Jamie Madillb98c3a82015-07-23 14:26:04 -04003815 {
3816 return nullptr;
3817 }
3818 break;
3819 case EOpLogicalOr:
3820 case EOpLogicalXor:
3821 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03003822 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3823 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003824 if (left->getBasicType() != EbtBool || left->isMatrix() || left->isVector())
3825 {
3826 return nullptr;
3827 }
3828 break;
3829 case EOpAdd:
3830 case EOpSub:
3831 case EOpDiv:
3832 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03003833 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3834 !right->getType().getStruct());
3835 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04003836 {
3837 return nullptr;
3838 }
3839 break;
3840 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03003841 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3842 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003843 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03003844 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04003845 {
3846 return nullptr;
3847 }
3848 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003849 default:
3850 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003851 }
3852
Olli Etuaho1dded802016-08-18 18:13:13 +03003853 if (op == EOpMul)
3854 {
3855 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
3856 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
3857 {
3858 return nullptr;
3859 }
3860 }
3861
Olli Etuaho3fdec912016-08-18 15:08:06 +03003862 TIntermBinary *node = new TIntermBinary(op, left, right);
3863 node->setLine(loc);
3864
Olli Etuaho3fdec912016-08-18 15:08:06 +03003865 // See if we can fold constants.
3866 TIntermTyped *foldedNode = node->fold(&mDiagnostics);
3867 if (foldedNode)
3868 return foldedNode;
3869
3870 return node;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003871}
3872
Jamie Madillb98c3a82015-07-23 14:26:04 -04003873TIntermTyped *TParseContext::addBinaryMath(TOperator op,
3874 TIntermTyped *left,
3875 TIntermTyped *right,
3876 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003877{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003878 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003879 if (node == 0)
3880 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003881 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3882 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003883 return left;
3884 }
3885 return node;
3886}
3887
Jamie Madillb98c3a82015-07-23 14:26:04 -04003888TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
3889 TIntermTyped *left,
3890 TIntermTyped *right,
3891 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003892{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003893 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003894 if (node == 0)
3895 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003896 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3897 right->getCompleteString());
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003898 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02003899 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003900 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
3901 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003902 }
3903 return node;
3904}
3905
Jamie Madillb98c3a82015-07-23 14:26:04 -04003906TIntermTyped *TParseContext::createAssign(TOperator op,
3907 TIntermTyped *left,
3908 TIntermTyped *right,
3909 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003910{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003911 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003912 {
Olli Etuaho1dded802016-08-18 18:13:13 +03003913 if (op == EOpMulAssign)
3914 {
3915 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
3916 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
3917 {
3918 return nullptr;
3919 }
3920 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03003921 TIntermBinary *node = new TIntermBinary(op, left, right);
3922 node->setLine(loc);
3923
Olli Etuaho3fdec912016-08-18 15:08:06 +03003924 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02003925 }
3926 return nullptr;
3927}
3928
Jamie Madillb98c3a82015-07-23 14:26:04 -04003929TIntermTyped *TParseContext::addAssign(TOperator op,
3930 TIntermTyped *left,
3931 TIntermTyped *right,
3932 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003933{
3934 TIntermTyped *node = createAssign(op, left, right, loc);
3935 if (node == nullptr)
3936 {
3937 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02003938 return left;
3939 }
3940 return node;
3941}
3942
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003943TIntermTyped *TParseContext::addComma(TIntermTyped *left,
3944 TIntermTyped *right,
3945 const TSourceLoc &loc)
3946{
Corentin Wallez0d959252016-07-12 17:26:32 -04003947 // WebGL2 section 5.26, the following results in an error:
3948 // "Sequence operator applied to void, arrays, or structs containing arrays"
3949 if (mShaderSpec == SH_WEBGL2_SPEC && (left->isArray() || left->getBasicType() == EbtVoid ||
3950 left->getType().isStructureContainingArrays() ||
3951 right->isArray() || right->getBasicType() == EbtVoid ||
3952 right->getType().isStructureContainingArrays()))
3953 {
3954 error(loc,
3955 "sequence operator is not allowed for void, arrays, or structs containing arrays",
3956 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04003957 }
3958
Olli Etuaho4db7ded2016-10-13 12:23:11 +01003959 return TIntermediate::AddComma(left, right, loc, mShaderVersion);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003960}
3961
Olli Etuaho49300862015-02-20 14:54:49 +02003962TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3963{
3964 switch (op)
3965 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003966 case EOpContinue:
3967 if (mLoopNestingLevel <= 0)
3968 {
3969 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04003970 }
3971 break;
3972 case EOpBreak:
3973 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
3974 {
3975 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04003976 }
3977 break;
3978 case EOpReturn:
3979 if (mCurrentFunctionType->getBasicType() != EbtVoid)
3980 {
3981 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04003982 }
3983 break;
3984 default:
3985 // No checks for discard
3986 break;
Olli Etuaho49300862015-02-20 14:54:49 +02003987 }
3988 return intermediate.addBranch(op, loc);
3989}
3990
Jamie Madillb98c3a82015-07-23 14:26:04 -04003991TIntermBranch *TParseContext::addBranch(TOperator op,
3992 TIntermTyped *returnValue,
3993 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02003994{
3995 ASSERT(op == EOpReturn);
3996 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003997 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02003998 {
3999 error(loc, "void function cannot return a value", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02004000 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004001 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02004002 {
4003 error(loc, "function return is not matching type:", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02004004 }
4005 return intermediate.addBranch(op, returnValue, loc);
4006}
4007
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004008void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
4009{
4010 ASSERT(!functionCall->isUserDefined());
Olli Etuahobd674552016-10-06 13:28:42 +01004011 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004012 TIntermNode *offset = nullptr;
4013 TIntermSequence *arguments = functionCall->getSequence();
4014 if (name.compare(0, 16, "texelFetchOffset") == 0 ||
4015 name.compare(0, 16, "textureLodOffset") == 0 ||
4016 name.compare(0, 20, "textureProjLodOffset") == 0 ||
4017 name.compare(0, 17, "textureGradOffset") == 0 ||
4018 name.compare(0, 21, "textureProjGradOffset") == 0)
4019 {
4020 offset = arguments->back();
4021 }
4022 else if (name.compare(0, 13, "textureOffset") == 0 ||
4023 name.compare(0, 17, "textureProjOffset") == 0)
4024 {
4025 // A bias parameter might follow the offset parameter.
4026 ASSERT(arguments->size() >= 3);
4027 offset = (*arguments)[2];
4028 }
4029 if (offset != nullptr)
4030 {
4031 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
4032 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
4033 {
4034 TString unmangledName = TFunction::unmangleName(name);
4035 error(functionCall->getLine(), "Texture offset must be a constant expression",
4036 unmangledName.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004037 }
4038 else
4039 {
4040 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
4041 size_t size = offsetConstantUnion->getType().getObjectSize();
4042 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
4043 for (size_t i = 0u; i < size; ++i)
4044 {
4045 int offsetValue = values[i].getIConst();
4046 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
4047 {
4048 std::stringstream tokenStream;
4049 tokenStream << offsetValue;
4050 std::string token = tokenStream.str();
4051 error(offset->getLine(), "Texture offset value out of valid range",
4052 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004053 }
4054 }
4055 }
4056 }
4057}
4058
Martin Radev2cc85b32016-08-05 16:22:53 +03004059// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
4060void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
4061{
4062 ASSERT(!functionCall->isUserDefined());
4063 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
4064
4065 if (name.compare(0, 5, "image") == 0)
4066 {
4067 TIntermSequence *arguments = functionCall->getSequence();
4068 TIntermNode *imageNode = (*arguments)[0];
4069 TIntermSymbol *imageSymbol = imageNode->getAsSymbolNode();
4070
4071 const TMemoryQualifier &memoryQualifier = imageSymbol->getMemoryQualifier();
4072
4073 if (name.compare(5, 5, "Store") == 0)
4074 {
4075 if (memoryQualifier.readonly)
4076 {
4077 error(imageNode->getLine(),
4078 "'imageStore' cannot be used with images qualified as 'readonly'",
4079 imageSymbol->getSymbol().c_str());
4080 }
4081 }
4082 else if (name.compare(5, 4, "Load") == 0)
4083 {
4084 if (memoryQualifier.writeonly)
4085 {
4086 error(imageNode->getLine(),
4087 "'imageLoad' cannot be used with images qualified as 'writeonly'",
4088 imageSymbol->getSymbol().c_str());
4089 }
4090 }
4091 }
4092}
4093
4094// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
4095void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
4096 const TFunction *functionDefinition,
4097 const TIntermAggregate *functionCall)
4098{
4099 ASSERT(functionCall->isUserDefined());
4100
4101 const TIntermSequence &arguments = *functionCall->getSequence();
4102
4103 ASSERT(functionDefinition->getParamCount() == arguments.size());
4104
4105 for (size_t i = 0; i < arguments.size(); ++i)
4106 {
4107 const TType &functionArgumentType = arguments[i]->getAsTyped()->getType();
4108 const TType &functionParameterType = *functionDefinition->getParam(i).type;
4109 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
4110
4111 if (IsImage(functionArgumentType.getBasicType()))
4112 {
4113 const TMemoryQualifier &functionArgumentMemoryQualifier =
4114 functionArgumentType.getMemoryQualifier();
4115 const TMemoryQualifier &functionParameterMemoryQualifier =
4116 functionParameterType.getMemoryQualifier();
4117 if (functionArgumentMemoryQualifier.readonly &&
4118 !functionParameterMemoryQualifier.readonly)
4119 {
4120 error(functionCall->getLine(),
4121 "Function call discards the 'readonly' qualifier from image",
4122 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4123 }
4124
4125 if (functionArgumentMemoryQualifier.writeonly &&
4126 !functionParameterMemoryQualifier.writeonly)
4127 {
4128 error(functionCall->getLine(),
4129 "Function call discards the 'writeonly' qualifier from image",
4130 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4131 }
4132 }
4133 }
4134}
4135
Jamie Madillb98c3a82015-07-23 14:26:04 -04004136TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
4137 TIntermNode *paramNode,
4138 TIntermNode *thisNode,
4139 const TSourceLoc &loc,
4140 bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004141{
Jamie Madillb98c3a82015-07-23 14:26:04 -04004142 *fatalError = false;
4143 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004144 TIntermTyped *callNode = nullptr;
4145
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004146 if (thisNode != nullptr)
4147 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004148 TConstantUnion *unionArray = new TConstantUnion[1];
Jamie Madillb98c3a82015-07-23 14:26:04 -04004149 int arraySize = 0;
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004150 TIntermTyped *typedThis = thisNode->getAsTyped();
4151 if (fnCall->getName() != "length")
4152 {
4153 error(loc, "invalid method", fnCall->getName().c_str());
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004154 }
4155 else if (paramNode != nullptr)
4156 {
4157 error(loc, "method takes no parameters", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004158 }
4159 else if (typedThis == nullptr || !typedThis->isArray())
4160 {
4161 error(loc, "length can only be called on arrays", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004162 }
4163 else
4164 {
Olli Etuaho96e67382015-04-23 14:27:02 +03004165 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03004166 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004167 {
Olli Etuaho39282e12015-04-23 15:41:48 +03004168 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004169 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03004170 // (func()).length()
4171 // (int[3](0, 1, 2)).length()
Jamie Madillb98c3a82015-07-23 14:26:04 -04004172 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
4173 // expression.
4174 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
4175 // spec section 5.9 which allows "An array, vector or matrix expression with the
4176 // length method applied".
4177 error(loc, "length can only be called on array names, not on array expressions",
4178 "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004179 }
4180 }
Olli Etuaho96e67382015-04-23 14:27:02 +03004181 unionArray->setIConst(arraySize);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004182 callNode =
4183 intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004184 }
4185 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004186 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004187 // Then this should be a constructor.
Olli Etuaho856c4972016-08-08 11:38:39 +03004188 callNode = addConstructor(paramNode, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004189 }
4190 else
4191 {
4192 //
4193 // Not a constructor. Find it in the symbol table.
4194 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05304195 const TFunction *fnCandidate;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004196 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004197 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004198 if (fnCandidate)
4199 {
4200 //
4201 // A declared function.
4202 //
Olli Etuaho383b7912016-08-05 11:22:59 +03004203 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004204 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004205 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004206 }
4207 op = fnCandidate->getBuiltInOp();
4208 if (builtIn && op != EOpNull)
4209 {
4210 //
4211 // A function call mapped to a built-in operation.
4212 //
4213 if (fnCandidate->getParamCount() == 1)
4214 {
4215 //
4216 // Treat it like a built-in unary operator.
4217 //
Olli Etuaho15c2ac32015-11-09 15:51:43 +02004218 TIntermAggregate *paramAgg = paramNode->getAsAggregate();
4219 paramNode = paramAgg->getSequence()->front();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004220 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc,
4221 &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004222 if (callNode == nullptr)
4223 {
4224 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004225 extraInfoStream
4226 << "built in unary operator function. Type: "
4227 << static_cast<TIntermTyped *>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004228 std::string extraInfo = extraInfoStream.str();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004229 error(paramNode->getLine(), " wrong operand type", "Internal Error",
4230 extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004231 *fatalError = true;
4232 return nullptr;
4233 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004234 }
4235 else
4236 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004237 TIntermAggregate *aggregate =
4238 intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004239 aggregate->setType(fnCandidate->getReturnType());
4240 aggregate->setPrecisionFromChildren();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02004241 if (aggregate->areChildrenConstQualified())
4242 {
4243 aggregate->getTypePointer()->setQualifier(EvqConst);
4244 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004245
4246 // Some built-in functions have out parameters too.
4247 functionCallLValueErrorCheck(fnCandidate, aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05304248
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004249 // See if we can constant fold a built-in. Note that this may be possible even
4250 // if it is not const-qualified.
Olli Etuahof119a262016-08-19 15:54:22 +03004251 TIntermTyped *foldedNode =
4252 intermediate.foldAggregateBuiltIn(aggregate, &mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05304253 if (foldedNode)
4254 {
Arun Patole274f0702015-05-05 13:33:30 +05304255 callNode = foldedNode;
4256 }
Olli Etuahob43846e2015-06-02 18:18:57 +03004257 else
4258 {
4259 callNode = aggregate;
4260 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004261 }
4262 }
4263 else
4264 {
4265 // This is a real function call
Jamie Madillb98c3a82015-07-23 14:26:04 -04004266 TIntermAggregate *aggregate =
4267 intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004268 aggregate->setType(fnCandidate->getReturnType());
4269
Jamie Madillb98c3a82015-07-23 14:26:04 -04004270 // this is how we know whether the given function is a builtIn function or a user
4271 // defined function
4272 // if builtIn == false, it's a userDefined -> could be an overloaded
4273 // builtIn function also
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004274 // if builtIn == true, it's definitely a builtIn function with EOpNull
4275 if (!builtIn)
4276 aggregate->setUserDefined();
Olli Etuahobd674552016-10-06 13:28:42 +01004277 aggregate->getFunctionSymbolInfo()->setFromFunction(*fnCandidate);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004278
Olli Etuahobd674552016-10-06 13:28:42 +01004279 // This needs to happen after the function info including name is set
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004280 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004281 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004282 aggregate->setBuiltInFunctionPrecision();
4283
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004284 checkTextureOffsetConst(aggregate);
Martin Radev2cc85b32016-08-05 16:22:53 +03004285
4286 checkImageMemoryAccessForBuiltinFunctions(aggregate);
4287 }
4288 else
4289 {
4290 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, aggregate);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004291 }
4292
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004293 callNode = aggregate;
4294
4295 functionCallLValueErrorCheck(fnCandidate, aggregate);
4296 }
4297 }
4298 else
4299 {
4300 // error message was put out by findFunction()
4301 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004302 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004303 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004304 callNode = intermediate.addConstantUnion(unionArray,
4305 TType(EbtFloat, EbpUndefined, EvqConst), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004306 }
4307 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004308 return callNode;
4309}
4310
Jamie Madillb98c3a82015-07-23 14:26:04 -04004311TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004312 TIntermTyped *trueExpression,
4313 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03004314 const TSourceLoc &loc)
4315{
Olli Etuaho856c4972016-08-08 11:38:39 +03004316 checkIsScalarBool(loc, cond);
Olli Etuaho52901742015-04-15 13:42:45 +03004317
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004318 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03004319 {
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004320 binaryOpError(loc, ":", trueExpression->getCompleteString(),
4321 falseExpression->getCompleteString());
4322 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03004323 }
Olli Etuahode318b22016-10-25 16:18:25 +01004324 if (IsOpaqueType(trueExpression->getBasicType()))
4325 {
4326 // ESSL 1.00 section 4.1.7
4327 // ESSL 3.00 section 4.1.7
4328 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
4329 // Note that structs containing opaque types don't need to be checked as structs are
4330 // forbidden below.
4331 error(loc, "ternary operator is not allowed for opaque types", ":");
4332 return falseExpression;
4333 }
4334
Olli Etuahoa2d53032015-04-15 14:14:44 +03004335 // ESSL1 sections 5.2 and 5.7:
4336 // ESSL3 section 5.7:
4337 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004338 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03004339 {
4340 error(loc, "ternary operator is not allowed for structures or arrays", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004341 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03004342 }
Corentin Wallez0d959252016-07-12 17:26:32 -04004343 // WebGL2 section 5.26, the following results in an error:
4344 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004345 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04004346 {
4347 error(loc, "ternary operator is not allowed for void", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004348 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04004349 }
4350
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004351 return TIntermediate::AddTernarySelection(cond, trueExpression, falseExpression, loc);
Olli Etuaho52901742015-04-15 13:42:45 +03004352}
Olli Etuaho49300862015-02-20 14:54:49 +02004353
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004354//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004355// Parse an array of strings using yyparse.
4356//
4357// Returns 0 for success.
4358//
Jamie Madillb98c3a82015-07-23 14:26:04 -04004359int PaParseStrings(size_t count,
4360 const char *const string[],
4361 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05304362 TParseContext *context)
4363{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004364 if ((count == 0) || (string == NULL))
4365 return 1;
4366
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004367 if (glslang_initialize(context))
4368 return 1;
4369
alokp@chromium.org408c45e2012-04-05 15:54:43 +00004370 int error = glslang_scan(count, string, length, context);
4371 if (!error)
4372 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004373
alokp@chromium.org73bc2982012-06-19 18:48:05 +00004374 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00004375
alokp@chromium.org6b495712012-06-29 00:06:58 +00004376 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004377}