blob: c405ceaf22b6dbd59ef9eab4562fba0c4e618e2d [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Jamie Madill6b9cb252013-10-17 10:45:47 -04007#include "compiler/translator/ParseContext.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +00008
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00009#include <stdarg.h>
apatrick@chromium.org8187fa82010-06-15 22:09:28 +000010#include <stdio.h>
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000011
daniel@transgaming.comb401a922012-10-26 18:58:24 +000012#include "compiler/preprocessor/SourceLocation.h"
Dmitry Skiba01971112015-07-10 14:54:00 -040013#include "compiler/translator/Cache.h"
Olli Etuahoac5274d2015-02-20 10:19:08 +020014#include "compiler/translator/glslang.h"
15#include "compiler/translator/ValidateSwitch.h"
Olli Etuahob0c645e2015-05-12 14:25:36 +030016#include "compiler/translator/ValidateGlobalInitializer.h"
Olli Etuaho37ad4742015-04-27 13:18:50 +030017#include "compiler/translator/util.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000018
alokp@chromium.org8b851c62012-06-15 16:25:11 +000019///////////////////////////////////////////////////////////////////////
20//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000021// Sub- vector and matrix fields
22//
23////////////////////////////////////////////////////////////////////////
24
25//
26// Look at a '.' field selector string and change it into offsets
27// for a vector.
28//
Jamie Madillb98c3a82015-07-23 14:26:04 -040029bool TParseContext::parseVectorFields(const TString &compString,
30 int vecSize,
31 TVectorFields &fields,
Arun Patole7e7e68d2015-05-22 12:02:25 +053032 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000033{
Jamie Madillb98c3a82015-07-23 14:26:04 -040034 fields.num = (int)compString.size();
Arun Patole7e7e68d2015-05-22 12:02:25 +053035 if (fields.num > 4)
36 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000037 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000038 return false;
39 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000040
Jamie Madillb98c3a82015-07-23 14:26:04 -040041 enum
42 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000043 exyzw,
44 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +000045 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000046 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000047
Arun Patole7e7e68d2015-05-22 12:02:25 +053048 for (int i = 0; i < fields.num; ++i)
49 {
50 switch (compString[i])
51 {
Jamie Madillb98c3a82015-07-23 14:26:04 -040052 case 'x':
53 fields.offsets[i] = 0;
54 fieldSet[i] = exyzw;
55 break;
56 case 'r':
57 fields.offsets[i] = 0;
58 fieldSet[i] = ergba;
59 break;
60 case 's':
61 fields.offsets[i] = 0;
62 fieldSet[i] = estpq;
63 break;
64 case 'y':
65 fields.offsets[i] = 1;
66 fieldSet[i] = exyzw;
67 break;
68 case 'g':
69 fields.offsets[i] = 1;
70 fieldSet[i] = ergba;
71 break;
72 case 't':
73 fields.offsets[i] = 1;
74 fieldSet[i] = estpq;
75 break;
76 case 'z':
77 fields.offsets[i] = 2;
78 fieldSet[i] = exyzw;
79 break;
80 case 'b':
81 fields.offsets[i] = 2;
82 fieldSet[i] = ergba;
83 break;
84 case 'p':
85 fields.offsets[i] = 2;
86 fieldSet[i] = estpq;
87 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053088
Jamie Madillb98c3a82015-07-23 14:26:04 -040089 case 'w':
90 fields.offsets[i] = 3;
91 fieldSet[i] = exyzw;
92 break;
93 case 'a':
94 fields.offsets[i] = 3;
95 fieldSet[i] = ergba;
96 break;
97 case 'q':
98 fields.offsets[i] = 3;
99 fieldSet[i] = estpq;
100 break;
101 default:
102 error(line, "illegal vector field selection", compString.c_str());
103 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000104 }
105 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000106
Arun Patole7e7e68d2015-05-22 12:02:25 +0530107 for (int i = 0; i < fields.num; ++i)
108 {
109 if (fields.offsets[i] >= vecSize)
110 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400111 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000112 return false;
113 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000114
Arun Patole7e7e68d2015-05-22 12:02:25 +0530115 if (i > 0)
116 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400117 if (fieldSet[i] != fieldSet[i - 1])
Arun Patole7e7e68d2015-05-22 12:02:25 +0530118 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400119 error(line, "illegal - vector component fields not from the same set",
120 compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000121 return false;
122 }
123 }
124 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000125
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000126 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000127}
128
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000129///////////////////////////////////////////////////////////////////////
130//
131// Errors
132//
133////////////////////////////////////////////////////////////////////////
134
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000135
136//
137// Used by flex/bison to output all syntax and parsing errors.
138//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530139void TParseContext::error(const TSourceLoc &loc,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400140 const char *reason,
141 const char *token,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530142 const char *extraInfo)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000143{
Olli Etuaho1cc598f2016-08-18 13:50:30 +0300144 mDiagnostics.error(loc, reason, token, extraInfo);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000145}
146
Arun Patole7e7e68d2015-05-22 12:02:25 +0530147void TParseContext::warning(const TSourceLoc &loc,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400148 const char *reason,
149 const char *token,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530150 const char *extraInfo)
151{
Olli Etuaho1cc598f2016-08-18 13:50:30 +0300152 mDiagnostics.warning(loc, reason, token, extraInfo);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000153}
154
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200155void TParseContext::outOfRangeError(bool isError,
156 const TSourceLoc &loc,
157 const char *reason,
158 const char *token,
159 const char *extraInfo)
160{
161 if (isError)
162 {
163 error(loc, reason, token, extraInfo);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200164 }
165 else
166 {
167 warning(loc, reason, token, extraInfo);
168 }
169}
170
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000171//
172// Same error message for all places assignments don't work.
173//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530174void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000175{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000176 std::stringstream extraInfoStream;
177 extraInfoStream << "cannot convert from '" << right << "' to '" << left << "'";
178 std::string extraInfo = extraInfoStream.str();
179 error(line, "", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000180}
181
182//
183// Same error message for all places unary operations don't work.
184//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530185void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000186{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000187 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400188 extraInfoStream << "no operation '" << op << "' exists that takes an operand of type "
189 << operand << " (or there is no acceptable conversion)";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000190 std::string extraInfo = extraInfoStream.str();
191 error(line, " wrong operand type", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000192}
193
194//
195// Same error message for all binary operations don't work.
196//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400197void TParseContext::binaryOpError(const TSourceLoc &line,
198 const char *op,
199 TString left,
200 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000201{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000202 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400203 extraInfoStream << "no operation '" << op << "' exists that takes a left-hand operand of type '"
204 << left << "' and a right operand of type '" << right
205 << "' (or there is no acceptable conversion)";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000206 std::string extraInfo = extraInfoStream.str();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530207 error(line, " wrong operand types ", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000208}
209
Olli Etuaho856c4972016-08-08 11:38:39 +0300210void TParseContext::checkPrecisionSpecified(const TSourceLoc &line,
211 TPrecision precision,
212 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530213{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400214 if (!mChecksPrecisionErrors)
Olli Etuaho383b7912016-08-05 11:22:59 +0300215 return;
Martin Radev70866b82016-07-22 15:27:42 +0300216
217 if (precision != EbpUndefined && !SupportsPrecision(type))
218 {
219 error(line, "illegal type for precision qualifier", getBasicString(type));
220 }
221
Olli Etuaho183d7e22015-11-20 15:59:09 +0200222 if (precision == EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530223 {
Olli Etuaho183d7e22015-11-20 15:59:09 +0200224 switch (type)
225 {
226 case EbtFloat:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400227 error(line, "No precision specified for (float)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300228 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200229 case EbtInt:
230 case EbtUInt:
231 UNREACHABLE(); // there's always a predeclared qualifier
Jamie Madillb98c3a82015-07-23 14:26:04 -0400232 error(line, "No precision specified (int)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300233 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200234 default:
235 if (IsSampler(type))
236 {
237 error(line, "No precision specified (sampler)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300238 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200239 }
240 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000241 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000242}
243
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000244// Both test and if necessary, spit out an error, to see if the node is really
245// an l-value that can be operated on this way.
Olli Etuaho856c4972016-08-08 11:38:39 +0300246bool TParseContext::checkCanBeLValue(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000247{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400248 TIntermSymbol *symNode = node->getAsSymbolNode();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530249 TIntermBinary *binaryNode = node->getAsBinaryNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000250
Arun Patole7e7e68d2015-05-22 12:02:25 +0530251 if (binaryNode)
252 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400253 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530254 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400255 case EOpIndexDirect:
256 case EOpIndexIndirect:
257 case EOpIndexDirectStruct:
258 case EOpIndexDirectInterfaceBlock:
Olli Etuaho856c4972016-08-08 11:38:39 +0300259 return checkCanBeLValue(line, op, binaryNode->getLeft());
Jamie Madillb98c3a82015-07-23 14:26:04 -0400260 case EOpVectorSwizzle:
Olli Etuaho8a176262016-08-16 14:23:01 +0300261 {
262 bool ok = checkCanBeLValue(line, op, binaryNode->getLeft());
263 if (ok)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530264 {
Olli Etuaho8a176262016-08-16 14:23:01 +0300265 int offsetCount[4] = {0, 0, 0, 0};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000266
Olli Etuaho8a176262016-08-16 14:23:01 +0300267 TIntermAggregate *swizzleOffsets = binaryNode->getRight()->getAsAggregate();
Jamie Madillb98c3a82015-07-23 14:26:04 -0400268
Olli Etuaho8a176262016-08-16 14:23:01 +0300269 for (const auto &offset : *swizzleOffsets->getSequence())
Jamie Madillb98c3a82015-07-23 14:26:04 -0400270 {
Olli Etuaho8a176262016-08-16 14:23:01 +0300271 int value = offset->getAsTyped()->getAsConstantUnion()->getIConst(0);
272 offsetCount[value]++;
273 if (offsetCount[value] > 1)
Jamie Madillb98c3a82015-07-23 14:26:04 -0400274 {
275 error(line, " l-value of swizzle cannot have duplicate components", op);
Olli Etuaho8a176262016-08-16 14:23:01 +0300276 return false;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400277 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000278 }
279 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000280
Olli Etuaho8a176262016-08-16 14:23:01 +0300281 return ok;
282 }
Jamie Madillb98c3a82015-07-23 14:26:04 -0400283 default:
284 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000285 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000286 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000287
Olli Etuaho8a176262016-08-16 14:23:01 +0300288 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000289 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000290
Arun Patole7e7e68d2015-05-22 12:02:25 +0530291 const char *symbol = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000292 if (symNode != 0)
293 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000294
Arun Patole7e7e68d2015-05-22 12:02:25 +0530295 const char *message = 0;
296 switch (node->getQualifier())
297 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400298 case EvqConst:
299 message = "can't modify a const";
300 break;
301 case EvqConstReadOnly:
302 message = "can't modify a const";
303 break;
304 case EvqAttribute:
305 message = "can't modify an attribute";
306 break;
307 case EvqFragmentIn:
308 message = "can't modify an input";
309 break;
310 case EvqVertexIn:
311 message = "can't modify an input";
312 break;
313 case EvqUniform:
314 message = "can't modify a uniform";
315 break;
316 case EvqVaryingIn:
317 message = "can't modify a varying";
318 break;
319 case EvqFragCoord:
320 message = "can't modify gl_FragCoord";
321 break;
322 case EvqFrontFacing:
323 message = "can't modify gl_FrontFacing";
324 break;
325 case EvqPointCoord:
326 message = "can't modify gl_PointCoord";
327 break;
Martin Radevb0883602016-08-04 17:48:58 +0300328 case EvqNumWorkGroups:
329 message = "can't modify gl_NumWorkGroups";
330 break;
331 case EvqWorkGroupSize:
332 message = "can't modify gl_WorkGroupSize";
333 break;
334 case EvqWorkGroupID:
335 message = "can't modify gl_WorkGroupID";
336 break;
337 case EvqLocalInvocationID:
338 message = "can't modify gl_LocalInvocationID";
339 break;
340 case EvqGlobalInvocationID:
341 message = "can't modify gl_GlobalInvocationID";
342 break;
343 case EvqLocalInvocationIndex:
344 message = "can't modify gl_LocalInvocationIndex";
345 break;
Martin Radev802abe02016-08-04 17:48:32 +0300346 case EvqComputeIn:
347 message = "can't modify work group size variable";
348 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400349 default:
350 //
351 // Type that can't be written to?
352 //
353 if (node->getBasicType() == EbtVoid)
354 {
355 message = "can't modify void";
356 }
357 if (IsSampler(node->getBasicType()))
358 {
359 message = "can't modify a sampler";
360 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000361 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000362
Arun Patole7e7e68d2015-05-22 12:02:25 +0530363 if (message == 0 && binaryNode == 0 && symNode == 0)
364 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000365 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000366
Olli Etuaho8a176262016-08-16 14:23:01 +0300367 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000368 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000369
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000370 //
371 // Everything else is okay, no error.
372 //
373 if (message == 0)
Olli Etuaho8a176262016-08-16 14:23:01 +0300374 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000375
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000376 //
377 // If we get here, we have an error and a message.
378 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530379 if (symNode)
380 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000381 std::stringstream extraInfoStream;
382 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
383 std::string extraInfo = extraInfoStream.str();
384 error(line, " l-value required", op, extraInfo.c_str());
385 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530386 else
387 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000388 std::stringstream extraInfoStream;
389 extraInfoStream << "(" << message << ")";
390 std::string extraInfo = extraInfoStream.str();
391 error(line, " l-value required", op, extraInfo.c_str());
392 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000393
Olli Etuaho8a176262016-08-16 14:23:01 +0300394 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000395}
396
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000397// Both test, and if necessary spit out an error, to see if the node is really
398// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300399void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000400{
Olli Etuaho383b7912016-08-05 11:22:59 +0300401 if (node->getQualifier() != EvqConst)
402 {
403 error(node->getLine(), "constant expression required", "");
404 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000405}
406
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000407// Both test, and if necessary spit out an error, to see if the node is really
408// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300409void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000410{
Olli Etuaho383b7912016-08-05 11:22:59 +0300411 if (!node->isScalarInt())
412 {
413 error(node->getLine(), "integer expression required", token);
414 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000415}
416
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000417// Both test, and if necessary spit out an error, to see if we are currently
418// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800419bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000420{
Olli Etuaho856c4972016-08-08 11:38:39 +0300421 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300422 {
423 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800424 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300425 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800426 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000427}
428
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000429// For now, keep it simple: if it starts "gl_", it's reserved, independent
430// of scope. Except, if the symbol table is at the built-in push-level,
431// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000432// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
433// webgl shader.
Olli Etuaho856c4972016-08-08 11:38:39 +0300434bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000435{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530436 static const char *reservedErrMsg = "reserved built-in name";
437 if (!symbolTable.atBuiltInLevel())
438 {
439 if (identifier.compare(0, 3, "gl_") == 0)
440 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000441 error(line, reservedErrMsg, "gl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300442 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000443 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530444 if (IsWebGLBasedSpec(mShaderSpec))
445 {
446 if (identifier.compare(0, 6, "webgl_") == 0)
447 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000448 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300449 return false;
alokp@chromium.org613ef312010-07-21 18:54:22 +0000450 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530451 if (identifier.compare(0, 7, "_webgl_") == 0)
452 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000453 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300454 return false;
alokp@chromium.org613ef312010-07-21 18:54:22 +0000455 }
456 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530457 if (identifier.find("__") != TString::npos)
458 {
459 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400460 "identifiers containing two consecutive underscores (__) are reserved as "
461 "possible future keywords",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530462 identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300463 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000464 }
465 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000466
Olli Etuaho8a176262016-08-16 14:23:01 +0300467 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000468}
469
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000470// Make sure there is enough data provided to the constructor to build
471// something of the type of the constructor. Also returns the type of
472// the constructor.
Olli Etuaho856c4972016-08-08 11:38:39 +0300473bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
474 TIntermNode *argumentsNode,
475 const TFunction &function,
476 TOperator op,
477 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000478{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000479 bool constructingMatrix = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400480 switch (op)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530481 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400482 case EOpConstructMat2:
483 case EOpConstructMat2x3:
484 case EOpConstructMat2x4:
485 case EOpConstructMat3x2:
486 case EOpConstructMat3:
487 case EOpConstructMat3x4:
488 case EOpConstructMat4x2:
489 case EOpConstructMat4x3:
490 case EOpConstructMat4:
491 constructingMatrix = true;
492 break;
493 default:
494 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000495 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000496
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000497 //
498 // Note: It's okay to have too many components available, but not okay to have unused
499 // arguments. 'full' will go to true when enough args have been seen. If we loop
500 // again, there is an extra argument, so 'overfull' will become true.
501 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000502
Jamie Madillb98c3a82015-07-23 14:26:04 -0400503 size_t size = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400504 bool full = false;
505 bool overFull = false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000506 bool matrixInMatrix = false;
507 bool arrayArg = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530508 for (size_t i = 0; i < function.getParamCount(); ++i)
509 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700510 const TConstParameter &param = function.getParam(i);
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000511 size += param.type->getObjectSize();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530512
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000513 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000514 matrixInMatrix = true;
515 if (full)
516 overFull = true;
Olli Etuaho856c4972016-08-08 11:38:39 +0300517 if (op != EOpConstructStruct && !type.isArray() && size >= type.getObjectSize())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000518 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000519 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000520 arrayArg = true;
521 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530522
Olli Etuaho856c4972016-08-08 11:38:39 +0300523 if (type.isArray())
Olli Etuaho376f1b52015-04-13 13:23:41 +0300524 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300525 // The size of an unsized constructor should already have been determined.
526 ASSERT(!type.isUnsizedArray());
527 if (static_cast<size_t>(type.getArraySize()) != function.getParamCount())
Olli Etuaho376f1b52015-04-13 13:23:41 +0300528 {
529 error(line, "array constructor needs one argument per array element", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300530 return false;
Olli Etuaho376f1b52015-04-13 13:23:41 +0300531 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000532 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000533
Arun Patole7e7e68d2015-05-22 12:02:25 +0530534 if (arrayArg && op != EOpConstructStruct)
535 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000536 error(line, "constructing from a non-dereferenced array", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300537 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000538 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000539
Olli Etuaho856c4972016-08-08 11:38:39 +0300540 if (matrixInMatrix && !type.isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530541 {
542 if (function.getParamCount() != 1)
543 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400544 error(line, "constructing matrix from matrix can only take one argument",
545 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300546 return false;
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000547 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000548 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000549
Arun Patole7e7e68d2015-05-22 12:02:25 +0530550 if (overFull)
551 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000552 error(line, "too many arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300553 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000554 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530555
Olli Etuaho856c4972016-08-08 11:38:39 +0300556 if (op == EOpConstructStruct && !type.isArray() &&
557 type.getStruct()->fields().size() != function.getParamCount())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530558 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400559 error(line,
560 "Number of constructor parameters does not match the number of structure fields",
561 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300562 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000563 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000564
Olli Etuaho856c4972016-08-08 11:38:39 +0300565 if (!type.isMatrix() || !matrixInMatrix)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530566 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300567 if ((op != EOpConstructStruct && size != 1 && size < type.getObjectSize()) ||
568 (op == EOpConstructStruct && size < type.getObjectSize()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530569 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000570 error(line, "not enough data provided for construction", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300571 return false;
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000572 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000573 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000574
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200575 if (argumentsNode == nullptr)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530576 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200577 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300578 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000579 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200580
581 TIntermAggregate *argumentsAgg = argumentsNode->getAsAggregate();
582 for (TIntermNode *&argNode : *argumentsAgg->getSequence())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530583 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200584 TIntermTyped *argTyped = argNode->getAsTyped();
585 ASSERT(argTyped != nullptr);
586 if (op != EOpConstructStruct && IsSampler(argTyped->getBasicType()))
587 {
588 error(line, "cannot convert a sampler", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300589 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200590 }
591 if (argTyped->getBasicType() == EbtVoid)
592 {
593 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300594 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200595 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000596 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000597
Olli Etuaho856c4972016-08-08 11:38:39 +0300598 if (type.isArray())
599 {
600 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
601 // the array.
602 for (TIntermNode *&argNode : *argumentsAgg->getSequence())
603 {
604 const TType &argType = argNode->getAsTyped()->getType();
605 // It has already been checked that the argument is not an array.
606 ASSERT(!argType.isArray());
607 if (!argType.sameElementType(type))
608 {
609 error(line, "Array constructor argument has an incorrect type", "Error");
Olli Etuaho8a176262016-08-16 14:23:01 +0300610 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300611 }
612 }
613 }
614 else if (op == EOpConstructStruct)
615 {
616 const TFieldList &fields = type.getStruct()->fields();
617 TIntermSequence *args = argumentsAgg->getSequence();
618
619 for (size_t i = 0; i < fields.size(); i++)
620 {
621 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
622 {
623 error(line, "Structure constructor arguments do not match structure fields",
624 "Error");
Olli Etuaho8a176262016-08-16 14:23:01 +0300625 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300626 }
627 }
628 }
629
Olli Etuaho8a176262016-08-16 14:23:01 +0300630 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000631}
632
Jamie Madillb98c3a82015-07-23 14:26:04 -0400633// This function checks to see if a void variable has been declared and raise an error message for
634// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000635//
636// returns true in case of an error
637//
Olli Etuaho856c4972016-08-08 11:38:39 +0300638bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400639 const TString &identifier,
640 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000641{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300642 if (type == EbtVoid)
643 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000644 error(line, "illegal use of type 'void'", identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300645 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300646 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000647
Olli Etuaho8a176262016-08-16 14:23:01 +0300648 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000649}
650
Jamie Madillb98c3a82015-07-23 14:26:04 -0400651// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300652// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300653void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000654{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530655 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector())
656 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000657 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530658 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000659}
660
Jamie Madillb98c3a82015-07-23 14:26:04 -0400661// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300662// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300663void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000664{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530665 if (pType.type != EbtBool || pType.isAggregate())
666 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000667 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530668 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000669}
670
Olli Etuaho856c4972016-08-08 11:38:39 +0300671bool TParseContext::checkIsNotSampler(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400672 const TPublicType &pType,
673 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000674{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530675 if (pType.type == EbtStruct)
676 {
677 if (containsSampler(*pType.userDef))
678 {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000679 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
Olli Etuaho8a176262016-08-16 14:23:01 +0300680 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000681 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530682
Olli Etuaho8a176262016-08-16 14:23:01 +0300683 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530684 }
685 else if (IsSampler(pType.type))
686 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000687 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300688 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000689 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000690
Olli Etuaho8a176262016-08-16 14:23:01 +0300691 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000692}
693
Olli Etuaho856c4972016-08-08 11:38:39 +0300694void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
695 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400696{
697 if (pType.layoutQualifier.location != -1)
698 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400699 error(line, "location must only be specified for a single input or output variable",
700 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400701 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400702}
703
Olli Etuaho856c4972016-08-08 11:38:39 +0300704void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
705 const TLayoutQualifier &layoutQualifier)
706{
707 if (layoutQualifier.location != -1)
708 {
709 error(location, "invalid layout qualifier:", "location",
710 "only valid on program inputs and outputs");
711 }
712}
713
714void TParseContext::checkOutParameterIsNotSampler(const TSourceLoc &line,
715 TQualifier qualifier,
716 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000717{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400718 if ((qualifier == EvqOut || qualifier == EvqInOut) && type.getBasicType() != EbtStruct &&
719 IsSampler(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530720 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000721 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000722 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000723}
724
Arun Patole7e7e68d2015-05-22 12:02:25 +0530725bool TParseContext::containsSampler(const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000726{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000727 if (IsSampler(type.getBasicType()))
728 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000729
Arun Patole7e7e68d2015-05-22 12:02:25 +0530730 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock())
731 {
732 const TFieldList &fields = type.getStruct()->fields();
733 for (unsigned int i = 0; i < fields.size(); ++i)
734 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400735 if (containsSampler(*fields[i]->type()))
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000736 return true;
737 }
738 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000739
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000740 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000741}
742
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000743// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300744unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000745{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530746 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000747
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200748 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
749 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
750 // fold as array size.
751 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000752 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000753 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300754 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000755 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000756
Olli Etuaho856c4972016-08-08 11:38:39 +0300757 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400758
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000759 if (constant->getBasicType() == EbtUInt)
760 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300761 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000762 }
763 else
764 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300765 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000766
Olli Etuaho856c4972016-08-08 11:38:39 +0300767 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000768 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400769 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300770 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000771 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400772
Olli Etuaho856c4972016-08-08 11:38:39 +0300773 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400774 }
775
Olli Etuaho856c4972016-08-08 11:38:39 +0300776 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400777 {
778 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300779 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400780 }
781
782 // The size of arrays is restricted here to prevent issues further down the
783 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
784 // 4096 registers so this should be reasonable even for aggressively optimizable code.
785 const unsigned int sizeLimit = 65536;
786
Olli Etuaho856c4972016-08-08 11:38:39 +0300787 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -0400788 {
789 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300790 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000791 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300792
793 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000794}
795
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000796// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +0300797bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
798 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000799{
Olli Etuaho8a176262016-08-16 14:23:01 +0300800 if ((elementQualifier.qualifier == EvqAttribute) ||
801 (elementQualifier.qualifier == EvqVertexIn) ||
802 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300803 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400804 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300805 TType(elementQualifier).getQualifierString());
806 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000807 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000808
Olli Etuaho8a176262016-08-16 14:23:01 +0300809 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000810}
811
Olli Etuaho8a176262016-08-16 14:23:01 +0300812// See if this element type can be formed into an array.
813bool TParseContext::checkIsValidTypeForArray(const TSourceLoc &line, const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000814{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000815 //
816 // Can the type be an array?
817 //
Olli Etuaho8a176262016-08-16 14:23:01 +0300818 if (elementType.array)
Jamie Madill06145232015-05-13 13:10:01 -0400819 {
Olli Etuaho8a176262016-08-16 14:23:01 +0300820 error(line, "cannot declare arrays of arrays",
821 TType(elementType).getCompleteString().c_str());
822 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000823 }
Olli Etuahocc36b982015-07-10 14:14:18 +0300824 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
825 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
826 // 4.3.4).
Olli Etuaho8a176262016-08-16 14:23:01 +0300827 if (mShaderVersion >= 300 && elementType.type == EbtStruct &&
828 sh::IsVarying(elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +0300829 {
830 error(line, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300831 TType(elementType).getCompleteString().c_str());
832 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +0300833 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000834
Olli Etuaho8a176262016-08-16 14:23:01 +0300835 return true;
836}
837
838// Check if this qualified element type can be formed into an array.
839bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
840 const TPublicType &elementType)
841{
842 if (checkIsValidTypeForArray(indexLocation, elementType))
843 {
844 return checkIsValidQualifierForArray(indexLocation, elementType);
845 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000846 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000847}
848
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000849// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +0300850void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
851 const TString &identifier,
852 TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000853{
Olli Etuaho3739d232015-04-08 12:23:44 +0300854 ASSERT(type != nullptr);
855 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000856 {
857 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300858 type->qualifier = EvqTemporary;
859
860 // Generate informative error messages for ESSL1.
861 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400862 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000863 {
Arun Patole7e7e68d2015-05-22 12:02:25 +0530864 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400865 "structures containing arrays may not be declared constant since they cannot be "
866 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530867 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000868 }
869 else
870 {
871 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
872 }
Olli Etuaho383b7912016-08-05 11:22:59 +0300873 return;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000874 }
Olli Etuaho376f1b52015-04-13 13:23:41 +0300875 if (type->isUnsizedArray())
876 {
877 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
Olli Etuaho376f1b52015-04-13 13:23:41 +0300878 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000879}
880
Olli Etuaho2935c582015-04-08 14:32:06 +0300881// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000882// and update the symbol table.
883//
Olli Etuaho2935c582015-04-08 14:32:06 +0300884// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000885//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400886bool TParseContext::declareVariable(const TSourceLoc &line,
887 const TString &identifier,
888 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +0300889 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000890{
Olli Etuaho2935c582015-04-08 14:32:06 +0300891 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000892
Olli Etuaho856c4972016-08-08 11:38:39 +0300893 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000894
Olli Etuaho2935c582015-04-08 14:32:06 +0300895 // gl_LastFragData may be redeclared with a new precision qualifier
896 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
897 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400898 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
899 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho856c4972016-08-08 11:38:39 +0300900 if (static_cast<int>(type.getArraySize()) == maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +0300901 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400902 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +0300903 {
Olli Etuaho8a176262016-08-16 14:23:01 +0300904 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->getExtension());
Olli Etuaho2935c582015-04-08 14:32:06 +0300905 }
906 }
907 else
908 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400909 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
910 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +0300911 return false;
912 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000913 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000914
Olli Etuaho8a176262016-08-16 14:23:01 +0300915 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +0300916 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000917
Olli Etuaho2935c582015-04-08 14:32:06 +0300918 (*variable) = new TVariable(&identifier, type);
919 if (!symbolTable.declare(*variable))
920 {
921 error(line, "redefinition", identifier.c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -0400922 *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +0300923 return false;
924 }
925
Olli Etuaho8a176262016-08-16 14:23:01 +0300926 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +0300927 return false;
928
929 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000930}
931
Martin Radev70866b82016-07-22 15:27:42 +0300932void TParseContext::checkIsParameterQualifierValid(
933 const TSourceLoc &line,
934 const TTypeQualifierBuilder &typeQualifierBuilder,
935 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530936{
Martin Radev70866b82016-07-22 15:27:42 +0300937 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(&mDiagnostics);
938
939 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530940 {
Martin Radev70866b82016-07-22 15:27:42 +0300941 checkOutParameterIsNotSampler(line, typeQualifier.qualifier, *type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000942 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000943
Martin Radev70866b82016-07-22 15:27:42 +0300944 type->setQualifier(typeQualifier.qualifier);
945
946 if (typeQualifier.precision != EbpUndefined)
947 {
948 type->setPrecision(typeQualifier.precision);
949 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000950}
951
Olli Etuaho856c4972016-08-08 11:38:39 +0300952bool TParseContext::checkCanUseExtension(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000953{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400954 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000955 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +0530956 if (iter == extBehavior.end())
957 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000958 error(line, "extension", extension.c_str(), "is not supported");
Olli Etuaho8a176262016-08-16 14:23:01 +0300959 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000960 }
zmo@google.comf5450912011-09-09 01:37:19 +0000961 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +0530962 if (iter->second == EBhDisable || iter->second == EBhUndefined)
963 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000964 error(line, "extension", extension.c_str(), "is disabled");
Olli Etuaho8a176262016-08-16 14:23:01 +0300965 return false;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000966 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530967 if (iter->second == EBhWarn)
968 {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000969 warning(line, "extension", extension.c_str(), "is being used");
Olli Etuaho8a176262016-08-16 14:23:01 +0300970 return true;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000971 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000972
Olli Etuaho8a176262016-08-16 14:23:01 +0300973 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000974}
975
Jamie Madillb98c3a82015-07-23 14:26:04 -0400976// These checks are common for all declarations starting a declarator list, and declarators that
977// follow an empty declaration.
Olli Etuaho383b7912016-08-05 11:22:59 +0300978void TParseContext::singleDeclarationErrorCheck(const TPublicType &publicType,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400979 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -0400980{
Olli Etuahofa33d582015-04-09 14:33:12 +0300981 switch (publicType.qualifier)
982 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400983 case EvqVaryingIn:
984 case EvqVaryingOut:
985 case EvqAttribute:
986 case EvqVertexIn:
987 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +0300988 case EvqComputeIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400989 if (publicType.type == EbtStruct)
990 {
991 error(identifierLocation, "cannot be used with a structure",
992 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +0300993 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400994 }
Olli Etuahofa33d582015-04-09 14:33:12 +0300995
Jamie Madillb98c3a82015-07-23 14:26:04 -0400996 default:
997 break;
Olli Etuahofa33d582015-04-09 14:33:12 +0300998 }
999
Jamie Madillb98c3a82015-07-23 14:26:04 -04001000 if (publicType.qualifier != EvqUniform &&
Olli Etuaho8a176262016-08-16 14:23:01 +03001001 !checkIsNotSampler(identifierLocation, publicType, "samplers must be uniform"))
Olli Etuahofa33d582015-04-09 14:33:12 +03001002 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001003 return;
Olli Etuahofa33d582015-04-09 14:33:12 +03001004 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001005
1006 // check for layout qualifier issues
1007 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
1008
1009 if (layoutQualifier.matrixPacking != EmpUnspecified)
1010 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001011 error(identifierLocation, "layout qualifier",
1012 getMatrixPackingString(layoutQualifier.matrixPacking),
Olli Etuahofa33d582015-04-09 14:33:12 +03001013 "only valid for interface blocks");
Olli Etuaho383b7912016-08-05 11:22:59 +03001014 return;
Jamie Madilla5efff92013-06-06 11:56:47 -04001015 }
1016
1017 if (layoutQualifier.blockStorage != EbsUnspecified)
1018 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001019 error(identifierLocation, "layout qualifier",
1020 getBlockStorageString(layoutQualifier.blockStorage),
Olli Etuahofa33d582015-04-09 14:33:12 +03001021 "only valid for interface blocks");
Olli Etuaho383b7912016-08-05 11:22:59 +03001022 return;
Jamie Madilla5efff92013-06-06 11:56:47 -04001023 }
1024
Olli Etuaho383b7912016-08-05 11:22:59 +03001025 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut)
Jamie Madilla5efff92013-06-06 11:56:47 -04001026 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001027 checkLocationIsNotSpecified(identifierLocation, publicType.layoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04001028 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001029}
1030
Olli Etuaho856c4972016-08-08 11:38:39 +03001031void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1032 const TString &layoutQualifierName,
1033 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001034{
1035
1036 if (mShaderVersion < versionRequired)
1037 {
1038 error(location, "invalid layout qualifier:", layoutQualifierName.c_str(), "not supported");
Martin Radev802abe02016-08-04 17:48:32 +03001039 }
1040}
1041
Olli Etuaho856c4972016-08-08 11:38:39 +03001042bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1043 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001044{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001045 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001046 for (size_t i = 0u; i < localSize.size(); ++i)
1047 {
1048 if (localSize[i] != -1)
1049 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03001050 error(location, "invalid layout qualifier:", getWorkGroupSizeString(i),
Martin Radev802abe02016-08-04 17:48:32 +03001051 "only valid when used with 'in' in a compute shader global layout declaration");
Olli Etuaho8a176262016-08-16 14:23:01 +03001052 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001053 }
1054 }
1055
Olli Etuaho8a176262016-08-16 14:23:01 +03001056 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001057}
1058
Olli Etuaho383b7912016-08-05 11:22:59 +03001059void TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate,
Olli Etuaho856c4972016-08-08 11:38:39 +03001060 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001061{
1062 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1063 {
1064 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1065 if (qual == EvqOut || qual == EvqInOut)
1066 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001067 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
Olli Etuaho8a176262016-08-16 14:23:01 +03001068 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001069 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001070 error(argument->getLine(),
Jamie Madillb98c3a82015-07-23 14:26:04 -04001071 "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error");
Olli Etuaho383b7912016-08-05 11:22:59 +03001072 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001073 }
1074 }
1075 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001076}
1077
Martin Radev70866b82016-07-22 15:27:42 +03001078void TParseContext::checkInvariantVariableQualifier(bool invariant,
1079 const TQualifier qualifier,
1080 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001081{
Martin Radev70866b82016-07-22 15:27:42 +03001082 if (!invariant)
1083 return;
1084
1085 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001086 {
Martin Radev70866b82016-07-22 15:27:42 +03001087 // input variables in the fragment shader can be also qualified as invariant
1088 if (!sh::CanBeInvariantESSL1(qualifier))
1089 {
1090 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1091 }
1092 }
1093 else
1094 {
1095 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1096 {
1097 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1098 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001099 }
1100}
1101
Arun Patole7e7e68d2015-05-22 12:02:25 +05301102bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001103{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001104 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001105 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1106 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001107}
1108
Arun Patole7e7e68d2015-05-22 12:02:25 +05301109bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001110{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001111 return ::IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001112}
1113
Jamie Madillb98c3a82015-07-23 14:26:04 -04001114void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1115 const char *extName,
1116 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001117{
1118 pp::SourceLocation srcLoc;
1119 srcLoc.file = loc.first_file;
1120 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001121 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001122}
1123
Jamie Madillb98c3a82015-07-23 14:26:04 -04001124void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1125 const char *name,
1126 const char *value,
1127 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001128{
1129 pp::SourceLocation srcLoc;
1130 srcLoc.file = loc.first_file;
1131 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001132 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001133}
1134
Martin Radev4c4c8e72016-08-04 12:25:34 +03001135sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001136{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001137 sh::WorkGroupSize result;
Martin Radev802abe02016-08-04 17:48:32 +03001138 for (size_t i = 0u; i < result.size(); ++i)
1139 {
1140 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1141 {
1142 result[i] = 1;
1143 }
1144 else
1145 {
1146 result[i] = mComputeShaderLocalSize[i];
1147 }
1148 }
1149 return result;
1150}
1151
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001152/////////////////////////////////////////////////////////////////////////////////
1153//
1154// Non-Errors.
1155//
1156/////////////////////////////////////////////////////////////////////////////////
1157
Jamie Madill5c097022014-08-20 16:38:32 -04001158const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1159 const TString *name,
1160 const TSymbol *symbol)
1161{
1162 const TVariable *variable = NULL;
1163
1164 if (!symbol)
1165 {
1166 error(location, "undeclared identifier", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001167 }
1168 else if (!symbol->isVariable())
1169 {
1170 error(location, "variable expected", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001171 }
1172 else
1173 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001174 variable = static_cast<const TVariable *>(symbol);
Jamie Madill5c097022014-08-20 16:38:32 -04001175
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001176 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
Olli Etuaho383b7912016-08-05 11:22:59 +03001177 !variable->getExtension().empty())
Jamie Madill5c097022014-08-20 16:38:32 -04001178 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001179 checkCanUseExtension(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001180 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001181
1182 // Reject shaders using both gl_FragData and gl_FragColor
1183 TQualifier qualifier = variable->getType().getQualifier();
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001184 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001185 {
1186 mUsesFragData = true;
1187 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001188 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001189 {
1190 mUsesFragColor = true;
1191 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001192 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1193 {
1194 mUsesSecondaryOutputs = true;
1195 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001196
1197 // This validation is not quite correct - it's only an error to write to
1198 // both FragData and FragColor. For simplicity, and because users shouldn't
1199 // be rewarded for reading from undefined varaibles, return an error
1200 // if they are both referenced, rather than assigned.
1201 if (mUsesFragData && mUsesFragColor)
1202 {
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001203 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1204 if (mUsesSecondaryOutputs)
1205 {
1206 errorMessage =
1207 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1208 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1209 }
1210 error(location, errorMessage, name->c_str());
Jamie Madill14e95b32015-05-07 10:10:41 -04001211 }
Martin Radevb0883602016-08-04 17:48:58 +03001212
1213 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1214 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1215 qualifier == EvqWorkGroupSize)
1216 {
1217 error(location,
1218 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1219 "gl_WorkGroupSize");
1220 }
Jamie Madill5c097022014-08-20 16:38:32 -04001221 }
1222
1223 if (!variable)
1224 {
1225 TType type(EbtFloat, EbpUndefined);
1226 TVariable *fakeVariable = new TVariable(name, type);
1227 symbolTable.declare(fakeVariable);
1228 variable = fakeVariable;
1229 }
1230
1231 return variable;
1232}
1233
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001234TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1235 const TString *name,
1236 const TSymbol *symbol)
1237{
1238 const TVariable *variable = getNamedVariable(location, name, symbol);
1239
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001240 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001241 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001242 const TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001243 return intermediate.addConstantUnion(constArray, variable->getType(), location);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001244 }
1245 else
1246 {
1247 return intermediate.addSymbol(variable->getUniqueId(), variable->getName(),
1248 variable->getType(), location);
1249 }
1250}
1251
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001252//
1253// Look up a function name in the symbol table, and make sure it is a function.
1254//
1255// Return the function symbol if found, otherwise 0.
1256//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001257const TFunction *TParseContext::findFunction(const TSourceLoc &line,
1258 TFunction *call,
1259 int inputShaderVersion,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301260 bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001261{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001262 // First find by unmangled name to check whether the function name has been
1263 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001264 // If a function is found, check for one with a matching argument list.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301265 const TSymbol *symbol = symbolTable.find(call->getName(), inputShaderVersion, builtIn);
1266 if (symbol == 0 || symbol->isFunction())
1267 {
Austin Kinross3ae64652015-01-26 15:51:39 -08001268 symbol = symbolTable.find(call->getMangledName(), inputShaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001269 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001270
Arun Patole7e7e68d2015-05-22 12:02:25 +05301271 if (symbol == 0)
1272 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001273 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001274 return 0;
1275 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001276
Arun Patole7e7e68d2015-05-22 12:02:25 +05301277 if (!symbol->isFunction())
1278 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001279 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001280 return 0;
1281 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001282
Jamie Madillb98c3a82015-07-23 14:26:04 -04001283 return static_cast<const TFunction *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001284}
1285
1286//
1287// Initializers show up in several places in the grammar. Have one set of
1288// code to handle them here.
1289//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001290// Returns true on error, false if no error
1291//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001292bool TParseContext::executeInitializer(const TSourceLoc &line,
1293 const TString &identifier,
1294 const TPublicType &pType,
1295 TIntermTyped *initializer,
1296 TIntermNode **intermNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001297{
Olli Etuahoe7847b02015-03-16 11:56:12 +02001298 ASSERT(intermNode != nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001299 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001300
Olli Etuaho2935c582015-04-08 14:32:06 +03001301 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001302 if (type.isUnsizedArray())
1303 {
1304 type.setArraySize(initializer->getArraySize());
1305 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001306 if (!declareVariable(line, identifier, type, &variable))
1307 {
1308 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001309 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001310
Olli Etuahob0c645e2015-05-12 14:25:36 +03001311 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001312 if (symbolTable.atGlobalLevel() &&
1313 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001314 {
1315 // Error message does not completely match behavior with ESSL 1.00, but
1316 // we want to steer developers towards only using constant expressions.
1317 error(line, "global variable initializers must be constant expressions", "=");
1318 return true;
1319 }
1320 if (globalInitWarning)
1321 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001322 warning(
1323 line,
1324 "global variable initializers should be constant expressions "
1325 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1326 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001327 }
1328
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001329 //
1330 // identifier must be of type constant, a global, or a temporary
1331 //
1332 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301333 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1334 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001335 error(line, " cannot initialize this type of qualifier ",
1336 variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001337 return true;
1338 }
1339 //
1340 // test for and propagate constant
1341 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001342
Arun Patole7e7e68d2015-05-22 12:02:25 +05301343 if (qualifier == EvqConst)
1344 {
1345 if (qualifier != initializer->getType().getQualifier())
1346 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001347 std::stringstream extraInfoStream;
1348 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1349 std::string extraInfo = extraInfoStream.str();
1350 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001351 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001352 return true;
1353 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301354 if (type != initializer->getType())
1355 {
1356 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001357 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001358 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001359 return true;
1360 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001361
1362 // Save the constant folded value to the variable if possible. For example array
1363 // initializers are not folded, since that way copying the array literal to multiple places
1364 // in the shader is avoided.
1365 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1366 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301367 if (initializer->getAsConstantUnion())
1368 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001369 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001370 *intermNode = nullptr;
1371 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301372 }
1373 else if (initializer->getAsSymbolNode())
1374 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001375 const TSymbol *symbol =
1376 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1377 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001378
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001379 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001380 if (constArray)
1381 {
1382 variable->shareConstPointer(constArray);
1383 *intermNode = nullptr;
1384 return false;
1385 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001386 }
1387 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001388
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001389 TIntermSymbol *intermSymbol = intermediate.addSymbol(
1390 variable->getUniqueId(), variable->getName(), variable->getType(), line);
1391 *intermNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1392 if (*intermNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001393 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001394 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1395 return true;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001396 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001397
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001398 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001399}
1400
Martin Radev70866b82016-07-22 15:27:42 +03001401TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301402 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001403{
Martin Radev70866b82016-07-22 15:27:42 +03001404 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001405
Martin Radev70866b82016-07-22 15:27:42 +03001406 TPublicType returnType = typeSpecifier;
1407 returnType.qualifier = typeQualifier.qualifier;
1408 returnType.invariant = typeQualifier.invariant;
1409 returnType.layoutQualifier = typeQualifier.layoutQualifier;
1410 returnType.precision = typeSpecifier.precision;
1411
1412 if (typeQualifier.precision != EbpUndefined)
1413 {
1414 returnType.precision = typeQualifier.precision;
1415 }
1416
1417 checkPrecisionSpecified(typeSpecifier.line, returnType.precision, typeSpecifier.type);
1418
1419 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier, typeSpecifier.line);
1420
1421 checkWorkGroupSizeIsNotSpecified(typeSpecifier.line, returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03001422
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001423 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001424 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001425 if (typeSpecifier.array)
1426 {
1427 error(typeSpecifier.line, "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001428 returnType.clearArrayness();
1429 }
1430
Martin Radev70866b82016-07-22 15:27:42 +03001431 if (returnType.qualifier == EvqAttribute &&
Jamie Madillb98c3a82015-07-23 14:26:04 -04001432 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001433 {
Martin Radev70866b82016-07-22 15:27:42 +03001434 error(typeSpecifier.line, "cannot be bool or int",
1435 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001436 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001437
Martin Radev70866b82016-07-22 15:27:42 +03001438 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001439 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1440 {
Martin Radev70866b82016-07-22 15:27:42 +03001441 error(typeSpecifier.line, "cannot be bool or int",
1442 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001443 }
1444 }
1445 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001446 {
Martin Radev70866b82016-07-22 15:27:42 +03001447 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03001448 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001449 checkIsAtGlobalLevel(typeSpecifier.line, "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03001450 }
Martin Radev70866b82016-07-22 15:27:42 +03001451 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
1452 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001453 {
Martin Radev70866b82016-07-22 15:27:42 +03001454 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier, typeSpecifier.line);
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001455 }
Martin Radev70866b82016-07-22 15:27:42 +03001456 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03001457 {
1458 error(typeSpecifier.line, "'in' can be only used to specify the local group size",
1459 "in");
Martin Radev802abe02016-08-04 17:48:32 +03001460 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001461 }
1462
1463 return returnType;
1464}
1465
Olli Etuaho856c4972016-08-08 11:38:39 +03001466void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
1467 const TPublicType &type,
1468 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03001469{
1470 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
1471 if (type.type == EbtBool)
1472 {
1473 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001474 }
1475
1476 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
1477 switch (qualifier)
1478 {
1479 case EvqVertexIn:
1480 // ESSL 3.00 section 4.3.4
1481 if (type.array)
1482 {
1483 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001484 }
1485 // Vertex inputs with a struct type are disallowed in singleDeclarationErrorCheck
1486 return;
1487 case EvqFragmentOut:
1488 // ESSL 3.00 section 4.3.6
1489 if (type.isMatrix())
1490 {
1491 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001492 }
1493 // Fragment outputs with a struct type are disallowed in singleDeclarationErrorCheck
1494 return;
1495 default:
1496 break;
1497 }
1498
1499 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
1500 // restrictions.
1501 bool typeContainsIntegers =
1502 (type.type == EbtInt || type.type == EbtUInt || type.isStructureContainingType(EbtInt) ||
1503 type.isStructureContainingType(EbtUInt));
1504 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
1505 {
1506 error(qualifierLocation, "must use 'flat' interpolation here",
1507 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001508 }
1509
1510 if (type.type == EbtStruct)
1511 {
1512 // ESSL 3.00 sections 4.3.4 and 4.3.6.
1513 // These restrictions are only implied by the ESSL 3.00 spec, but
1514 // the ESSL 3.10 spec lists these restrictions explicitly.
1515 if (type.array)
1516 {
1517 error(qualifierLocation, "cannot be an array of structures",
1518 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001519 }
1520 if (type.isStructureContainingArrays())
1521 {
1522 error(qualifierLocation, "cannot be a structure containing an array",
1523 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001524 }
1525 if (type.isStructureContainingType(EbtStruct))
1526 {
1527 error(qualifierLocation, "cannot be a structure containing a structure",
1528 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001529 }
1530 if (type.isStructureContainingType(EbtBool))
1531 {
1532 error(qualifierLocation, "cannot be a structure containing a bool",
1533 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001534 }
1535 }
1536}
1537
Olli Etuahofa33d582015-04-09 14:33:12 +03001538TIntermAggregate *TParseContext::parseSingleDeclaration(TPublicType &publicType,
1539 const TSourceLoc &identifierOrTypeLocation,
1540 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001541{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001542 TType type(publicType);
1543 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
1544 mDirectiveHandler.pragma().stdgl.invariantAll)
1545 {
1546 TQualifier qualifier = type.getQualifier();
1547
1548 // The directive handler has already taken care of rejecting invalid uses of this pragma
1549 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
1550 // affected variable declarations:
1551 //
1552 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
1553 // elsewhere, in TranslatorGLSL.)
1554 //
1555 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
1556 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
1557 // the way this is currently implemented we have to enable this compiler option before
1558 // parsing the shader and determining the shading language version it uses. If this were
1559 // implemented as a post-pass, the workaround could be more targeted.
1560 //
1561 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
1562 // the specification, but there are desktop OpenGL drivers that expect that this is the
1563 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
1564 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
1565 {
1566 type.setInvariant(true);
1567 }
1568 }
1569
1570 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, type, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001571
Olli Etuahobab4c082015-04-24 16:38:49 +03001572 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03001573
Olli Etuahobab4c082015-04-24 16:38:49 +03001574 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1575
1576 if (emptyDeclaration)
1577 {
1578 if (publicType.isUnsizedArray())
1579 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001580 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1581 // error. It is assumed that this applies to empty declarations as well.
1582 error(identifierOrTypeLocation, "empty array declaration needs to specify a size",
1583 identifier.c_str());
Olli Etuahobab4c082015-04-24 16:38:49 +03001584 }
1585 }
1586 else
Jamie Madill60ed9812013-06-06 11:56:46 -04001587 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001588 singleDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001589
Olli Etuaho856c4972016-08-08 11:38:39 +03001590 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001591
Olli Etuaho2935c582015-04-08 14:32:06 +03001592 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001593 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04001594
1595 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001596 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001597 }
1598
Olli Etuahoe7847b02015-03-16 11:56:12 +02001599 return intermediate.makeAggregate(symbol, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001600}
1601
Olli Etuahoe7847b02015-03-16 11:56:12 +02001602TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1603 const TSourceLoc &identifierLocation,
1604 const TString &identifier,
1605 const TSourceLoc &indexLocation,
1606 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001607{
Olli Etuahofa33d582015-04-09 14:33:12 +03001608 mDeferredSingleDeclarationErrorCheck = false;
1609
Olli Etuaho383b7912016-08-05 11:22:59 +03001610 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001611
Olli Etuaho856c4972016-08-08 11:38:39 +03001612 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001613
Olli Etuaho8a176262016-08-16 14:23:01 +03001614 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001615
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001616 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001617
Olli Etuaho856c4972016-08-08 11:38:39 +03001618 unsigned int size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001619 // Make the type an array even if size check failed.
1620 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1621 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001622
Olli Etuaho2935c582015-04-08 14:32:06 +03001623 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03001624 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04001625
Olli Etuahoe7847b02015-03-16 11:56:12 +02001626 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001627 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001628 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001629
Olli Etuahoe7847b02015-03-16 11:56:12 +02001630 return intermediate.makeAggregate(symbol, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001631}
1632
Jamie Madill06145232015-05-13 13:10:01 -04001633TIntermAggregate *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001634 const TSourceLoc &identifierLocation,
1635 const TString &identifier,
1636 const TSourceLoc &initLocation,
1637 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001638{
Olli Etuahofa33d582015-04-09 14:33:12 +03001639 mDeferredSingleDeclarationErrorCheck = false;
1640
Olli Etuaho383b7912016-08-05 11:22:59 +03001641 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001642
Olli Etuahoe7847b02015-03-16 11:56:12 +02001643 TIntermNode *intermNode = nullptr;
1644 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04001645 {
1646 //
1647 // Build intermediate representation
1648 //
Olli Etuahoe7847b02015-03-16 11:56:12 +02001649 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001650 }
1651 else
1652 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001653 return nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001654 }
1655}
1656
Jamie Madillb98c3a82015-07-23 14:26:04 -04001657TIntermAggregate *TParseContext::parseSingleArrayInitDeclaration(
1658 TPublicType &publicType,
1659 const TSourceLoc &identifierLocation,
1660 const TString &identifier,
1661 const TSourceLoc &indexLocation,
1662 TIntermTyped *indexExpression,
1663 const TSourceLoc &initLocation,
1664 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001665{
1666 mDeferredSingleDeclarationErrorCheck = false;
1667
Olli Etuaho383b7912016-08-05 11:22:59 +03001668 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001669
Olli Etuaho8a176262016-08-16 14:23:01 +03001670 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001671
1672 TPublicType arrayType(publicType);
1673
Olli Etuaho856c4972016-08-08 11:38:39 +03001674 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001675 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
1676 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03001677 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001678 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001679 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001680 }
1681 // Make the type an array even if size check failed.
1682 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1683 arrayType.setArraySize(size);
1684
1685 // initNode will correspond to the whole of "type b[n] = initializer".
1686 TIntermNode *initNode = nullptr;
1687 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1688 {
1689 return initNode ? intermediate.makeAggregate(initNode, initLocation) : nullptr;
1690 }
1691 else
1692 {
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001693 return nullptr;
1694 }
1695}
1696
Martin Radev70866b82016-07-22 15:27:42 +03001697TIntermAggregate *TParseContext::parseInvariantDeclaration(
1698 const TTypeQualifierBuilder &typeQualifierBuilder,
1699 const TSourceLoc &identifierLoc,
1700 const TString *identifier,
1701 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04001702{
Martin Radev70866b82016-07-22 15:27:42 +03001703 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04001704
Martin Radev70866b82016-07-22 15:27:42 +03001705 if (!typeQualifier.invariant)
1706 {
1707 error(identifierLoc, "Expected invariant", identifier->c_str());
1708 return nullptr;
1709 }
1710 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
1711 {
1712 return nullptr;
1713 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04001714 if (!symbol)
1715 {
1716 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001717 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001718 }
Martin Radev70866b82016-07-22 15:27:42 +03001719 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04001720 {
Martin Radev70866b82016-07-22 15:27:42 +03001721 error(identifierLoc, "invariant declaration specifies qualifier",
1722 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04001723 }
Martin Radev70866b82016-07-22 15:27:42 +03001724 if (typeQualifier.precision != EbpUndefined)
1725 {
1726 error(identifierLoc, "invariant declaration specifies precision",
1727 getPrecisionString(typeQualifier.precision));
1728 }
1729 if (!typeQualifier.layoutQualifier.isEmpty())
1730 {
1731 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
1732 }
1733
1734 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1735 ASSERT(variable);
1736 const TType &type = variable->getType();
1737
1738 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
1739 typeQualifier.line);
1740
1741 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
1742
1743 TIntermSymbol *intermSymbol =
1744 intermediate.addSymbol(variable->getUniqueId(), *identifier, type, identifierLoc);
1745
1746 TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc);
1747 aggregate->setOp(EOpInvariantDeclaration);
1748 return aggregate;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001749}
1750
Jamie Madillb98c3a82015-07-23 14:26:04 -04001751TIntermAggregate *TParseContext::parseDeclarator(TPublicType &publicType,
1752 TIntermAggregate *aggregateDeclaration,
1753 const TSourceLoc &identifierLocation,
1754 const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001755{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001756 // If the declaration starting this declarator list was empty (example: int,), some checks were
1757 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001758 if (mDeferredSingleDeclarationErrorCheck)
1759 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001760 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03001761 mDeferredSingleDeclarationErrorCheck = false;
1762 }
1763
Olli Etuaho856c4972016-08-08 11:38:39 +03001764 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04001765
Olli Etuaho856c4972016-08-08 11:38:39 +03001766 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04001767
Olli Etuaho2935c582015-04-08 14:32:06 +03001768 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03001769 declareVariable(identifierLocation, identifier, TType(publicType), &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001770
Jamie Madillb98c3a82015-07-23 14:26:04 -04001771 TIntermSymbol *symbol =
1772 intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001773 if (variable && symbol)
Jamie Madill502d66f2013-06-20 11:55:52 -04001774 symbol->setId(variable->getUniqueId());
1775
Olli Etuahoe7847b02015-03-16 11:56:12 +02001776 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001777}
1778
Jamie Madillb98c3a82015-07-23 14:26:04 -04001779TIntermAggregate *TParseContext::parseArrayDeclarator(TPublicType &publicType,
1780 TIntermAggregate *aggregateDeclaration,
1781 const TSourceLoc &identifierLocation,
1782 const TString &identifier,
1783 const TSourceLoc &arrayLocation,
1784 TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001785{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001786 // If the declaration starting this declarator list was empty (example: int,), some checks were
1787 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001788 if (mDeferredSingleDeclarationErrorCheck)
1789 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001790 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03001791 mDeferredSingleDeclarationErrorCheck = false;
1792 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001793
Olli Etuaho856c4972016-08-08 11:38:39 +03001794 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04001795
Olli Etuaho856c4972016-08-08 11:38:39 +03001796 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04001797
Olli Etuaho8a176262016-08-16 14:23:01 +03001798 if (checkIsValidTypeAndQualifierForArray(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001799 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001800 TType arrayType = TType(publicType);
Olli Etuaho856c4972016-08-08 11:38:39 +03001801 unsigned int size = checkIsValidArraySize(arrayLocation, indexExpression);
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001802 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001803
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001804 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03001805 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04001806
Jamie Madillb98c3a82015-07-23 14:26:04 -04001807 TIntermSymbol *symbol =
1808 intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001809 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001810 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001811
1812 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001813 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001814
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001815 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001816}
1817
Jamie Madillb98c3a82015-07-23 14:26:04 -04001818TIntermAggregate *TParseContext::parseInitDeclarator(const TPublicType &publicType,
1819 TIntermAggregate *aggregateDeclaration,
1820 const TSourceLoc &identifierLocation,
1821 const TString &identifier,
1822 const TSourceLoc &initLocation,
1823 TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001824{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001825 // If the declaration starting this declarator list was empty (example: int,), some checks were
1826 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001827 if (mDeferredSingleDeclarationErrorCheck)
1828 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001829 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03001830 mDeferredSingleDeclarationErrorCheck = false;
1831 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001832
Olli Etuaho856c4972016-08-08 11:38:39 +03001833 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04001834
Olli Etuahoe7847b02015-03-16 11:56:12 +02001835 TIntermNode *intermNode = nullptr;
1836 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04001837 {
1838 //
1839 // build the intermediate representation
1840 //
1841 if (intermNode)
1842 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001843 return intermediate.growAggregate(aggregateDeclaration, intermNode, initLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001844 }
1845 else
1846 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001847 return aggregateDeclaration;
Jamie Madill502d66f2013-06-20 11:55:52 -04001848 }
1849 }
1850 else
1851 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001852 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001853 }
1854}
1855
Jamie Madill06145232015-05-13 13:10:01 -04001856TIntermAggregate *TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001857 TIntermAggregate *aggregateDeclaration,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301858 const TSourceLoc &identifierLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001859 const TString &identifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301860 const TSourceLoc &indexLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001861 TIntermTyped *indexExpression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001862 const TSourceLoc &initLocation,
1863 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001864{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001865 // If the declaration starting this declarator list was empty (example: int,), some checks were
1866 // not performed.
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001867 if (mDeferredSingleDeclarationErrorCheck)
1868 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001869 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001870 mDeferredSingleDeclarationErrorCheck = false;
1871 }
1872
Olli Etuaho856c4972016-08-08 11:38:39 +03001873 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001874
Olli Etuaho8a176262016-08-16 14:23:01 +03001875 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001876
1877 TPublicType arrayType(publicType);
1878
Olli Etuaho856c4972016-08-08 11:38:39 +03001879 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001880 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
1881 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03001882 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001883 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001884 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001885 }
1886 // Make the type an array even if size check failed.
1887 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1888 arrayType.setArraySize(size);
1889
1890 // initNode will correspond to the whole of "b[n] = initializer".
1891 TIntermNode *initNode = nullptr;
1892 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1893 {
1894 if (initNode)
1895 {
1896 return intermediate.growAggregate(aggregateDeclaration, initNode, initLocation);
1897 }
1898 else
1899 {
1900 return aggregateDeclaration;
1901 }
1902 }
1903 else
1904 {
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001905 return nullptr;
1906 }
1907}
1908
Martin Radev70866b82016-07-22 15:27:42 +03001909void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04001910{
Martin Radev70866b82016-07-22 15:27:42 +03001911 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04001912 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04001913
Martin Radev70866b82016-07-22 15:27:42 +03001914 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
1915 typeQualifier.line);
1916
Jamie Madillc2128ff2016-07-04 10:26:17 -04001917 // It should never be the case, but some strange parser errors can send us here.
1918 if (layoutQualifier.isEmpty())
1919 {
1920 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04001921 return;
1922 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001923
Martin Radev802abe02016-08-04 17:48:32 +03001924 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04001925 {
Martin Radev802abe02016-08-04 17:48:32 +03001926 error(typeQualifier.line, "invalid combination:", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04001927 return;
1928 }
1929
Martin Radev802abe02016-08-04 17:48:32 +03001930 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04001931 {
Martin Radev802abe02016-08-04 17:48:32 +03001932 if (mComputeShaderLocalSizeDeclared &&
1933 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
1934 {
1935 error(typeQualifier.line, "Work group size does not match the previous declaration",
1936 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03001937 return;
1938 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001939
Martin Radev802abe02016-08-04 17:48:32 +03001940 if (mShaderVersion < 310)
1941 {
1942 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03001943 return;
1944 }
Jamie Madill099c0f32013-06-20 11:55:52 -04001945
Martin Radev4c4c8e72016-08-04 12:25:34 +03001946 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03001947 {
1948 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03001949 return;
1950 }
1951
1952 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
1953 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
1954
1955 const TConstantUnion *maxComputeWorkGroupSizeData =
1956 maxComputeWorkGroupSize->getConstPointer();
1957
1958 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
1959 {
1960 if (layoutQualifier.localSize[i] != -1)
1961 {
1962 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
1963 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
1964 if (mComputeShaderLocalSize[i] < 1 ||
1965 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
1966 {
1967 std::stringstream errorMessageStream;
1968 errorMessageStream << "Value must be at least 1 and no greater than "
1969 << maxComputeWorkGroupSizeValue;
1970 const std::string &errorMessage = errorMessageStream.str();
1971
Martin Radev4c4c8e72016-08-04 12:25:34 +03001972 error(typeQualifier.line, "invalid value:", getWorkGroupSizeString(i),
Martin Radev802abe02016-08-04 17:48:32 +03001973 errorMessage.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001974 return;
1975 }
1976 }
1977 }
1978
1979 mComputeShaderLocalSizeDeclared = true;
1980 }
1981 else
Jamie Madill1566ef72013-06-20 11:55:54 -04001982 {
Martin Radev802abe02016-08-04 17:48:32 +03001983
Olli Etuaho8a176262016-08-16 14:23:01 +03001984 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03001985 {
Martin Radev802abe02016-08-04 17:48:32 +03001986 return;
1987 }
1988
1989 if (typeQualifier.qualifier != EvqUniform)
1990 {
1991 error(typeQualifier.line, "invalid qualifier:",
1992 getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
Martin Radev802abe02016-08-04 17:48:32 +03001993 return;
1994 }
1995
1996 if (mShaderVersion < 300)
1997 {
1998 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
1999 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002000 return;
2001 }
2002
Olli Etuaho856c4972016-08-08 11:38:39 +03002003 checkLocationIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002004
2005 if (layoutQualifier.matrixPacking != EmpUnspecified)
2006 {
2007 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
2008 }
2009
2010 if (layoutQualifier.blockStorage != EbsUnspecified)
2011 {
2012 mDefaultBlockStorage = layoutQualifier.blockStorage;
2013 }
Jamie Madill1566ef72013-06-20 11:55:54 -04002014 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002015}
2016
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002017TIntermAggregate *TParseContext::addFunctionPrototypeDeclaration(const TFunction &function,
2018 const TSourceLoc &location)
2019{
Olli Etuaho5d653182016-01-04 14:43:28 +02002020 // Note: symbolTableFunction could be the same as function if this is the first declaration.
2021 // Either way the instance in the symbol table is used to track whether the function is declared
2022 // multiple times.
2023 TFunction *symbolTableFunction =
2024 static_cast<TFunction *>(symbolTable.find(function.getMangledName(), getShaderVersion()));
2025 if (symbolTableFunction->hasPrototypeDeclaration() && mShaderVersion == 100)
2026 {
2027 // ESSL 1.00.17 section 4.2.7.
2028 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
2029 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02002030 }
2031 symbolTableFunction->setHasPrototypeDeclaration();
2032
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002033 TIntermAggregate *prototype = new TIntermAggregate;
2034 prototype->setType(function.getReturnType());
2035 prototype->setName(function.getMangledName());
2036 prototype->setFunctionId(function.getUniqueId());
2037
2038 for (size_t i = 0; i < function.getParamCount(); i++)
2039 {
2040 const TConstParameter &param = function.getParam(i);
2041 if (param.name != 0)
2042 {
2043 TVariable variable(param.name, *param.type);
2044
2045 TIntermSymbol *paramSymbol = intermediate.addSymbol(
2046 variable.getUniqueId(), variable.getName(), variable.getType(), location);
2047 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
2048 }
2049 else
2050 {
2051 TIntermSymbol *paramSymbol = intermediate.addSymbol(0, "", *param.type, location);
2052 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
2053 }
2054 }
2055
2056 prototype->setOp(EOpPrototype);
2057
2058 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002059
2060 if (!symbolTable.atGlobalLevel())
2061 {
2062 // ESSL 3.00.4 section 4.2.4.
2063 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002064 }
2065
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002066 return prototype;
2067}
2068
2069TIntermAggregate *TParseContext::addFunctionDefinition(const TFunction &function,
2070 TIntermAggregate *functionPrototype,
2071 TIntermAggregate *functionBody,
2072 const TSourceLoc &location)
2073{
2074 //?? Check that all paths return a value if return type != void ?
2075 // May be best done as post process phase on intermediate code
2076 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
2077 {
2078 error(location, "function does not return a value:", "", function.getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002079 }
2080
2081 TIntermAggregate *aggregate =
2082 intermediate.growAggregate(functionPrototype, functionBody, location);
2083 intermediate.setAggregateOperator(aggregate, EOpFunction, location);
2084 aggregate->setName(function.getMangledName().c_str());
2085 aggregate->setType(function.getReturnType());
2086 aggregate->setFunctionId(function.getUniqueId());
2087
2088 symbolTable.pop();
2089 return aggregate;
2090}
2091
Jamie Madill185fb402015-06-12 15:48:48 -04002092void TParseContext::parseFunctionPrototype(const TSourceLoc &location,
2093 TFunction *function,
2094 TIntermAggregate **aggregateOut)
2095{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002096 const TSymbol *builtIn =
2097 symbolTable.findBuiltIn(function->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002098
2099 if (builtIn)
2100 {
2101 error(location, "built-in functions cannot be redefined", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002102 }
2103
Jamie Madillb98c3a82015-07-23 14:26:04 -04002104 TFunction *prevDec =
2105 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Jamie Madill185fb402015-06-12 15:48:48 -04002106 //
2107 // Note: 'prevDec' could be 'function' if this is the first time we've seen function
2108 // as it would have just been put in the symbol table. Otherwise, we're looking up
2109 // an earlier occurance.
2110 //
2111 if (prevDec->isDefined())
2112 {
2113 // Then this function already has a body.
2114 error(location, "function already has a body", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002115 }
2116 prevDec->setDefined();
2117 //
2118 // Overload the unique ID of the definition to be the same unique ID as the declaration.
2119 // Eventually we will probably want to have only a single definition and just swap the
2120 // arguments to be the definition's arguments.
2121 //
2122 function->setUniqueId(prevDec->getUniqueId());
2123
2124 // Raise error message if main function takes any parameters or return anything other than void
2125 if (function->getName() == "main")
2126 {
2127 if (function->getParamCount() > 0)
2128 {
2129 error(location, "function cannot take any parameter(s)", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002130 }
2131 if (function->getReturnType().getBasicType() != EbtVoid)
2132 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002133 error(location, "", function->getReturnType().getBasicString(),
2134 "main function cannot return a value");
Jamie Madill185fb402015-06-12 15:48:48 -04002135 }
2136 }
2137
2138 //
2139 // Remember the return type for later checking for RETURN statements.
2140 //
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002141 mCurrentFunctionType = &(prevDec->getReturnType());
2142 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04002143
2144 //
2145 // Insert parameters into the symbol table.
2146 // If the parameter has no name, it's not an error, just don't insert it
2147 // (could be used for unused args).
2148 //
2149 // Also, accumulate the list of parameters into the HIL, so lower level code
2150 // knows where to find parameters.
2151 //
2152 TIntermAggregate *paramNodes = new TIntermAggregate;
2153 for (size_t i = 0; i < function->getParamCount(); i++)
2154 {
2155 const TConstParameter &param = function->getParam(i);
2156 if (param.name != 0)
2157 {
2158 TVariable *variable = new TVariable(param.name, *param.type);
2159 //
2160 // Insert the parameters with name in the symbol table.
2161 //
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002162 if (!symbolTable.declare(variable))
2163 {
Jamie Madill185fb402015-06-12 15:48:48 -04002164 error(location, "redefinition", variable->getName().c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002165 paramNodes = intermediate.growAggregate(
2166 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
2167 continue;
Jamie Madill185fb402015-06-12 15:48:48 -04002168 }
2169
2170 //
2171 // Add the parameter to the HIL
2172 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002173 TIntermSymbol *symbol = intermediate.addSymbol(
2174 variable->getUniqueId(), variable->getName(), variable->getType(), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002175
2176 paramNodes = intermediate.growAggregate(paramNodes, symbol, location);
2177 }
2178 else
2179 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002180 paramNodes = intermediate.growAggregate(
2181 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002182 }
2183 }
2184 intermediate.setAggregateOperator(paramNodes, EOpParameters, location);
2185 *aggregateOut = paramNodes;
2186 setLoopNestingLevel(0);
2187}
2188
Jamie Madillb98c3a82015-07-23 14:26:04 -04002189TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002190{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002191 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002192 // We don't know at this point whether this is a function definition or a prototype.
2193 // The definition production code will check for redefinitions.
2194 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002195 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002196 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
2197 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002198 //
2199 TFunction *prevDec =
2200 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302201
2202 if (getShaderVersion() >= 300 && symbolTable.hasUnmangledBuiltIn(function->getName().c_str()))
2203 {
2204 // With ESSL 3.00, names of built-in functions cannot be redeclared as functions.
2205 // Therefore overloading or redefining builtin functions is an error.
2206 error(location, "Name of a built-in function cannot be redeclared as function",
2207 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302208 }
2209 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002210 {
2211 if (prevDec->getReturnType() != function->getReturnType())
2212 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002213 error(location, "overloaded functions must have the same return type",
Jamie Madill185fb402015-06-12 15:48:48 -04002214 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04002215 }
2216 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2217 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002218 if (prevDec->getParam(i).type->getQualifier() !=
2219 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002220 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002221 error(location, "overloaded functions must have the same parameter qualifiers",
Jamie Madill185fb402015-06-12 15:48:48 -04002222 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04002223 }
2224 }
2225 }
2226
2227 //
2228 // Check for previously declared variables using the same name.
2229 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002230 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002231 if (prevSym)
2232 {
2233 if (!prevSym->isFunction())
2234 {
2235 error(location, "redefinition", function->getName().c_str(), "function");
Jamie Madill185fb402015-06-12 15:48:48 -04002236 }
2237 }
2238 else
2239 {
2240 // Insert the unmangled name to detect potential future redefinition as a variable.
Jamie Madillb98c3a82015-07-23 14:26:04 -04002241 TFunction *newFunction =
2242 new TFunction(NewPoolTString(function->getName().c_str()), &function->getReturnType());
Jamie Madill185fb402015-06-12 15:48:48 -04002243 symbolTable.getOuterLevel()->insertUnmangled(newFunction);
2244 }
2245
2246 // We're at the inner scope level of the function's arguments and body statement.
2247 // Add the function prototype to the surrounding scope instead.
2248 symbolTable.getOuterLevel()->insert(function);
2249
2250 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002251 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2252 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002253 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2254 //
2255 return function;
2256}
2257
Olli Etuaho9de84a52016-06-14 17:36:01 +03002258TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
2259 const TString *name,
2260 const TSourceLoc &location)
2261{
2262 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
2263 {
2264 error(location, "no qualifiers allowed for function return",
2265 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03002266 }
2267 if (!type.layoutQualifier.isEmpty())
2268 {
2269 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03002270 }
2271 // make sure a sampler is not involved as well...
Olli Etuaho856c4972016-08-08 11:38:39 +03002272 checkIsNotSampler(location, type, "samplers can't be function return values");
Olli Etuahoe29324f2016-06-15 10:58:03 +03002273 if (mShaderVersion < 300)
2274 {
2275 // Array return values are forbidden, but there's also no valid syntax for declaring array
2276 // return values in ESSL 1.00.
2277 ASSERT(type.arraySize == 0 || mDiagnostics.numErrors() > 0);
2278
2279 if (type.isStructureContainingArrays())
2280 {
2281 // ESSL 1.00.17 section 6.1 Function Definitions
2282 error(location, "structures containing arrays can't be function return values",
2283 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03002284 }
2285 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03002286
2287 // Add the function as a prototype after parsing it (we do not support recursion)
2288 return new TFunction(name, new TType(type));
2289}
2290
Jamie Madill06145232015-05-13 13:10:01 -04002291TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002292{
Jamie Madill06145232015-05-13 13:10:01 -04002293 TPublicType publicType = publicTypeIn;
Olli Etuahobd163f62015-11-13 12:15:38 +02002294 if (publicType.isStructSpecifier)
2295 {
2296 error(publicType.line, "constructor can't be a structure definition",
2297 getBasicString(publicType.type));
Olli Etuahobd163f62015-11-13 12:15:38 +02002298 }
2299
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002300 TOperator op = EOpNull;
2301 if (publicType.userDef)
2302 {
2303 op = EOpConstructStruct;
2304 }
2305 else
2306 {
Geoff Lang156d7192016-07-21 16:11:00 -04002307 op = sh::TypeToConstructorOperator(TType(publicType));
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002308 if (op == EOpNull)
2309 {
2310 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002311 publicType.type = EbtFloat;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002312 op = EOpConstructFloat;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002313 }
2314 }
2315
2316 TString tempString;
Dmitry Skiba7f17a502015-06-22 15:08:39 -07002317 const TType *type = new TType(publicType);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002318 return new TFunction(&tempString, type, op);
2319}
2320
Jamie Madillb98c3a82015-07-23 14:26:04 -04002321// This function is used to test for the correctness of the parameters passed to various constructor
2322// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002323//
Olli Etuaho856c4972016-08-08 11:38:39 +03002324// 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 +00002325//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002326TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002327 TOperator op,
2328 TFunction *fnCall,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302329 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002330{
Olli Etuaho856c4972016-08-08 11:38:39 +03002331 TType type = fnCall->getReturnType();
2332 if (type.isUnsizedArray())
2333 {
2334 type.setArraySize(static_cast<unsigned int>(fnCall->getParamCount()));
2335 }
2336 bool constType = true;
2337 for (size_t i = 0; i < fnCall->getParamCount(); ++i)
2338 {
2339 const TConstParameter &param = fnCall->getParam(i);
2340 if (param.type->getQualifier() != EvqConst)
2341 constType = false;
2342 }
2343 if (constType)
2344 type.setQualifier(EvqConst);
2345
Olli Etuaho8a176262016-08-16 14:23:01 +03002346 if (!checkConstructorArguments(line, arguments, *fnCall, op, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03002347 {
2348 TIntermTyped *dummyNode = intermediate.setAggregateOperator(nullptr, op, line);
2349 dummyNode->setType(type);
2350 return dummyNode;
2351 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002352 TIntermAggregate *constructor = arguments->getAsAggregate();
2353 ASSERT(constructor != nullptr);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002354
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002355 // Turn the argument list itself into a constructor
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002356 constructor->setOp(op);
2357 constructor->setLine(line);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002358 ASSERT(constructor->isConstructor());
2359
2360 // Need to set type before setPrecisionFromChildren() because bool doesn't have precision.
Olli Etuaho856c4972016-08-08 11:38:39 +03002361 constructor->setType(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002362
Olli Etuaho21203702014-11-13 16:16:21 +02002363 // Structs should not be precision qualified, the individual members may be.
2364 // Built-in types on the other hand should be precision qualified.
2365 if (op != EOpConstructStruct)
2366 {
2367 constructor->setPrecisionFromChildren();
Olli Etuaho856c4972016-08-08 11:38:39 +03002368 type.setPrecision(constructor->getPrecision());
Olli Etuaho21203702014-11-13 16:16:21 +02002369 }
2370
Olli Etuaho856c4972016-08-08 11:38:39 +03002371 constructor->setType(type);
2372
Olli Etuaho1d122782015-11-06 15:35:17 +02002373 TIntermTyped *constConstructor = intermediate.foldAggregateBuiltIn(constructor);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002374 if (constConstructor)
2375 {
2376 return constConstructor;
2377 }
2378
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002379 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002380}
2381
Olli Etuaho90892fb2016-07-14 14:44:51 +03002382// This function returns vector field(s) being accessed from a constant vector.
2383TIntermConstantUnion *TParseContext::foldVectorSwizzle(TVectorFields &fields,
2384 TIntermConstantUnion *baseNode,
2385 const TSourceLoc &location)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002386{
Olli Etuaho90892fb2016-07-14 14:44:51 +03002387 const TConstantUnion *unionArray = baseNode->getUnionArrayPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002388 ASSERT(unionArray);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002389
Arun Patole7e7e68d2015-05-22 12:02:25 +05302390 TConstantUnion *constArray = new TConstantUnion[fields.num];
Olli Etuaho90892fb2016-07-14 14:44:51 +03002391 const auto &type = baseNode->getType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002392
Arun Patole7e7e68d2015-05-22 12:02:25 +05302393 for (int i = 0; i < fields.num; i++)
2394 {
Olli Etuaho90892fb2016-07-14 14:44:51 +03002395 // Out-of-range indices should already be checked.
2396 ASSERT(fields.offsets[i] < type.getNominalSize());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002397 constArray[i] = unionArray[fields.offsets[i]];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302398 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03002399 return intermediate.addConstantUnion(constArray, type, location);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002400}
2401
Olli Etuaho90892fb2016-07-14 14:44:51 +03002402// This function returns the column vector being accessed from a constant matrix.
2403TIntermConstantUnion *TParseContext::foldMatrixSubscript(int index,
2404 TIntermConstantUnion *baseNode,
2405 const TSourceLoc &location)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002406{
Olli Etuaho90892fb2016-07-14 14:44:51 +03002407 ASSERT(index < baseNode->getType().getCols());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002408
Olli Etuaho90892fb2016-07-14 14:44:51 +03002409 const TConstantUnion *unionArray = baseNode->getUnionArrayPointer();
2410 int size = baseNode->getType().getRows();
2411 return intermediate.addConstantUnion(&unionArray[size * index], baseNode->getType(), location);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002412}
2413
Olli Etuaho90892fb2016-07-14 14:44:51 +03002414// This function returns an element of an array accessed from a constant array.
2415TIntermConstantUnion *TParseContext::foldArraySubscript(int index,
2416 TIntermConstantUnion *baseNode,
2417 const TSourceLoc &location)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002418{
Olli Etuaho856c4972016-08-08 11:38:39 +03002419 ASSERT(index < static_cast<int>(baseNode->getArraySize()));
Olli Etuaho90892fb2016-07-14 14:44:51 +03002420
2421 TType arrayElementType = baseNode->getType();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002422 arrayElementType.clearArrayness();
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002423 size_t arrayElementSize = arrayElementType.getObjectSize();
Olli Etuaho90892fb2016-07-14 14:44:51 +03002424 const TConstantUnion *unionArray = baseNode->getUnionArrayPointer();
2425 return intermediate.addConstantUnion(&unionArray[arrayElementSize * index], baseNode->getType(),
2426 location);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002427}
2428
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002429//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002430// This function returns the value of a particular field inside a constant structure from the symbol
2431// table.
2432// If there is an embedded/nested struct, it appropriately calls addConstStructNested or
2433// addConstStructFromAggr function and returns the parse-tree with the values of the embedded/nested
2434// struct.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002435//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002436TIntermTyped *TParseContext::addConstStruct(const TString &identifier,
2437 TIntermTyped *node,
2438 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002439{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302440 const TFieldList &fields = node->getType().getStruct()->fields();
Jamie Madillb98c3a82015-07-23 14:26:04 -04002441 size_t instanceSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002442
Arun Patole7e7e68d2015-05-22 12:02:25 +05302443 for (size_t index = 0; index < fields.size(); ++index)
2444 {
2445 if (fields[index]->name() == identifier)
2446 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002447 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302448 }
2449 else
2450 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002451 instanceSize += fields[index]->type()->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002452 }
2453 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002454
Jamie Madill94bf7f22013-07-08 13:31:15 -04002455 TIntermTyped *typedNode;
2456 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
Arun Patole7e7e68d2015-05-22 12:02:25 +05302457 if (tempConstantNode)
2458 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002459 const TConstantUnion *constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002460
Jamie Madillb98c3a82015-07-23 14:26:04 -04002461 // type will be changed in the calling function
2462 typedNode = intermediate.addConstantUnion(constArray + instanceSize,
2463 tempConstantNode->getType(), line);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302464 }
2465 else
2466 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002467 error(line, "Cannot offset into the structure", "Error");
Olli Etuaho383b7912016-08-05 11:22:59 +03002468 return nullptr;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002469 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002470
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002471 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002472}
2473
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002474//
2475// Interface/uniform blocks
2476//
Martin Radev70866b82016-07-22 15:27:42 +03002477TIntermAggregate *TParseContext::addInterfaceBlock(
2478 const TTypeQualifierBuilder &typeQualifierBuilder,
2479 const TSourceLoc &nameLine,
2480 const TString &blockName,
2481 TFieldList *fieldList,
2482 const TString *instanceName,
2483 const TSourceLoc &instanceLine,
2484 TIntermTyped *arrayIndex,
2485 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002486{
Olli Etuaho856c4972016-08-08 11:38:39 +03002487 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002488
Martin Radev70866b82016-07-22 15:27:42 +03002489 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
2490
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002491 if (typeQualifier.qualifier != EvqUniform)
2492 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302493 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
2494 "interface blocks must be uniform");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002495 }
2496
Martin Radev70866b82016-07-22 15:27:42 +03002497 if (typeQualifier.invariant)
2498 {
2499 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
2500 }
2501
Jamie Madill099c0f32013-06-20 11:55:52 -04002502 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03002503 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002504
Jamie Madill099c0f32013-06-20 11:55:52 -04002505 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2506 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002507 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002508 }
2509
Jamie Madill1566ef72013-06-20 11:55:54 -04002510 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2511 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002512 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002513 }
2514
Olli Etuaho856c4972016-08-08 11:38:39 +03002515 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002516
Arun Patole7e7e68d2015-05-22 12:02:25 +05302517 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2518 if (!symbolTable.declare(blockNameSymbol))
2519 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002520 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002521 }
2522
Jamie Madill98493dd2013-07-08 14:39:03 -04002523 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302524 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2525 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002526 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302527 TType *fieldType = field->type();
2528 if (IsSampler(fieldType->getBasicType()))
2529 {
2530 error(field->line(), "unsupported type", fieldType->getBasicString(),
2531 "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002532 }
2533
Jamie Madill98493dd2013-07-08 14:39:03 -04002534 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002535 switch (qualifier)
2536 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002537 case EvqGlobal:
2538 case EvqUniform:
2539 break;
2540 default:
2541 error(field->line(), "invalid qualifier on interface block member",
2542 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04002543 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002544 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002545
Martin Radev70866b82016-07-22 15:27:42 +03002546 if (fieldType->isInvariant())
2547 {
2548 error(field->line(), "invalid qualifier on interface block member", "invariant");
2549 }
2550
Jamie Madilla5efff92013-06-06 11:56:47 -04002551 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002552 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03002553 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
Jamie Madill099c0f32013-06-20 11:55:52 -04002554
Jamie Madill98493dd2013-07-08 14:39:03 -04002555 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002556 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002557 error(field->line(), "invalid layout qualifier:",
2558 getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04002559 }
2560
Jamie Madill98493dd2013-07-08 14:39:03 -04002561 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002562 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002563 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002564 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002565 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002566 {
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002567 warning(field->line(), "extraneous layout qualifier:",
Jamie Madillb98c3a82015-07-23 14:26:04 -04002568 getMatrixPackingString(fieldLayoutQualifier.matrixPacking),
2569 "only has an effect on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04002570 }
2571
Jamie Madill98493dd2013-07-08 14:39:03 -04002572 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002573 }
2574
Jamie Madill98493dd2013-07-08 14:39:03 -04002575 // add array index
Olli Etuaho856c4972016-08-08 11:38:39 +03002576 unsigned int arraySize = 0;
2577 if (arrayIndex != nullptr)
Jamie Madill98493dd2013-07-08 14:39:03 -04002578 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002579 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04002580 }
2581
Jamie Madillb98c3a82015-07-23 14:26:04 -04002582 TInterfaceBlock *interfaceBlock =
2583 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2584 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
2585 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002586
2587 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04002588 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002589
Jamie Madill98493dd2013-07-08 14:39:03 -04002590 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002591 {
2592 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002593 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2594 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002595 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302596 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002597
2598 // set parent pointer of the field variable
2599 fieldType->setInterfaceBlock(interfaceBlock);
2600
Arun Patole7e7e68d2015-05-22 12:02:25 +05302601 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002602 fieldVariable->setQualifier(typeQualifier.qualifier);
2603
Arun Patole7e7e68d2015-05-22 12:02:25 +05302604 if (!symbolTable.declare(fieldVariable))
2605 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002606 error(field->line(), "redefinition", field->name().c_str(),
2607 "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002608 }
2609 }
2610 }
2611 else
2612 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002613 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03002614
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002615 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302616 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002617 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002618
Arun Patole7e7e68d2015-05-22 12:02:25 +05302619 if (!symbolTable.declare(instanceTypeDef))
2620 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002621 error(instanceLine, "redefinition", instanceName->c_str(),
2622 "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002623 }
2624
Jamie Madillb98c3a82015-07-23 14:26:04 -04002625 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002626 symbolName = instanceTypeDef->getName();
2627 }
2628
Jamie Madillb98c3a82015-07-23 14:26:04 -04002629 TIntermAggregate *aggregate = intermediate.makeAggregate(
2630 intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line),
2631 nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002632 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04002633
2634 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002635 return aggregate;
2636}
2637
Olli Etuaho383b7912016-08-05 11:22:59 +03002638void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002639{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002640 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002641
2642 // Embedded structure definitions are not supported per GLSL ES spec.
2643 // They aren't allowed in GLSL either, but we need to detect this here
2644 // so we don't rely on the GLSL compiler to catch it.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302645 if (mStructNestingLevel > 1)
2646 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002647 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002648 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002649}
2650
2651void TParseContext::exitStructDeclaration()
2652{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002653 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002654}
2655
Jamie Madillb98c3a82015-07-23 14:26:04 -04002656namespace
2657{
kbr@chromium.org476541f2011-10-27 21:14:51 +00002658const int kWebGLMaxStructNesting = 4;
2659
2660} // namespace
2661
Olli Etuaho8a176262016-08-16 14:23:01 +03002662void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002663{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302664 if (!IsWebGLBasedSpec(mShaderSpec))
2665 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002666 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002667 }
2668
Arun Patole7e7e68d2015-05-22 12:02:25 +05302669 if (field.type()->getBasicType() != EbtStruct)
2670 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002671 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002672 }
2673
2674 // We're already inside a structure definition at this point, so add
2675 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302676 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2677 {
Jamie Madill41a49272014-03-18 16:10:13 -04002678 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002679 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
2680 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04002681 std::string reason = reasonStream.str();
2682 error(line, reason.c_str(), field.name().c_str(), "");
Olli Etuaho8a176262016-08-16 14:23:01 +03002683 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002684 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002685}
2686
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002687//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002688// Parse an array index expression
2689//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002690TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
2691 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302692 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002693{
2694 TIntermTyped *indexedExpression = NULL;
2695
2696 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2697 {
2698 if (baseExpression->getAsSymbolNode())
2699 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302700 error(location, " left of '[' is not of type array, matrix, or vector ",
2701 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002702 }
2703 else
2704 {
2705 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2706 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002707 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002708
Jamie Madill21c1e452014-12-29 11:33:41 -05002709 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2710
Olli Etuaho36b05142015-11-12 13:10:42 +02002711 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
2712 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
2713 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
2714 // index is a constant expression.
2715 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
2716 {
2717 if (baseExpression->isInterfaceBlock())
2718 {
2719 error(
2720 location, "", "[",
2721 "array indexes for interface blocks arrays must be constant integral expressions");
Olli Etuaho36b05142015-11-12 13:10:42 +02002722 }
2723 else if (baseExpression->getQualifier() == EvqFragmentOut)
2724 {
2725 error(location, "", "[",
2726 "array indexes for fragment outputs must be constant integral expressions");
Olli Etuaho36b05142015-11-12 13:10:42 +02002727 }
Olli Etuaho3e960462015-11-12 15:58:39 +02002728 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
2729 {
2730 error(location, "", "[", "array index for gl_FragData must be constant zero");
Olli Etuaho3e960462015-11-12 15:58:39 +02002731 }
Olli Etuaho36b05142015-11-12 13:10:42 +02002732 }
2733
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002734 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002735 {
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002736 // If the index is not qualified as constant, the behavior in the spec is undefined. This
2737 // applies even if ANGLE has been able to constant fold it (ANGLE may constant fold
2738 // expressions that are not constant expressions). The most compatible way to handle this
2739 // case is to report a warning instead of an error and force the index to be in the
2740 // correct range.
2741 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Jamie Madill21c1e452014-12-29 11:33:41 -05002742 int index = indexConstantUnion->getIConst(0);
Olli Etuaho90892fb2016-07-14 14:44:51 +03002743 if (!baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04002744 {
Olli Etuaho90892fb2016-07-14 14:44:51 +03002745 // Array checks are done later because a different error message might be generated
2746 // based on the index in some cases.
2747 if (baseExpression->isVector())
2748 {
2749 index = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
2750 baseExpression->getType().getNominalSize(),
2751 "vector field selection out of range", "[]");
2752 }
2753 else if (baseExpression->isMatrix())
2754 {
2755 index = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
2756 baseExpression->getType().getCols(),
2757 "matrix field selection out of range", "[]");
2758 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002759 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03002760
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002761 TIntermConstantUnion *baseConstantUnion = baseExpression->getAsConstantUnion();
2762 if (baseConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002763 {
2764 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002765 {
Olli Etuaho90892fb2016-07-14 14:44:51 +03002766 index = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
2767 baseExpression->getArraySize(),
2768 "array index out of range", "[]");
2769 // Constant folding for array indexing.
2770 indexedExpression = foldArraySubscript(index, baseConstantUnion, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002771 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002772 else if (baseExpression->isVector())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002773 {
Olli Etuaho90892fb2016-07-14 14:44:51 +03002774 // Constant folding for vector indexing - reusing vector swizzle folding.
Jamie Madill7164cf42013-07-08 13:30:59 -04002775 TVectorFields fields;
2776 fields.num = 1;
Olli Etuaho90892fb2016-07-14 14:44:51 +03002777 fields.offsets[0] = index;
2778 indexedExpression = foldVectorSwizzle(fields, baseConstantUnion, location);
Jamie Madill7164cf42013-07-08 13:30:59 -04002779 }
2780 else if (baseExpression->isMatrix())
2781 {
Olli Etuaho90892fb2016-07-14 14:44:51 +03002782 // Constant folding for matrix indexing.
2783 indexedExpression = foldMatrixSubscript(index, baseConstantUnion, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002784 }
2785 }
2786 else
2787 {
Jamie Madillb11e2482015-05-04 14:21:22 -04002788 int safeIndex = -1;
2789
Jamie Madill7164cf42013-07-08 13:30:59 -04002790 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002791 {
Olli Etuaho3e960462015-11-12 15:58:39 +02002792 if (baseExpression->getQualifier() == EvqFragData && index > 0)
2793 {
2794 if (mShaderSpec == SH_WEBGL2_SPEC)
2795 {
2796 // Error has been already generated if index is not const.
2797 if (indexExpression->getQualifier() == EvqConst)
2798 {
2799 error(location, "", "[",
2800 "array index for gl_FragData must be constant zero");
Olli Etuaho3e960462015-11-12 15:58:39 +02002801 }
2802 safeIndex = 0;
2803 }
2804 else if (!isExtensionEnabled("GL_EXT_draw_buffers"))
2805 {
2806 outOfRangeError(outOfRangeIndexIsError, location, "", "[",
2807 "array index for gl_FragData must be zero when "
2808 "GL_EXT_draw_buffers is disabled");
2809 safeIndex = 0;
2810 }
2811 }
2812 // Only do generic out-of-range check if similar error hasn't already been reported.
Olli Etuaho90892fb2016-07-14 14:44:51 +03002813 if (safeIndex < 0)
Jamie Madill7164cf42013-07-08 13:30:59 -04002814 {
Olli Etuaho90892fb2016-07-14 14:44:51 +03002815 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
2816 baseExpression->getArraySize(),
2817 "array index out of range", "[]");
Jamie Madill7164cf42013-07-08 13:30:59 -04002818 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002819 }
2820
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002821 // Data of constant unions can't be changed, because it may be shared with other
2822 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
2823 // sanitized object.
Jamie Madillb11e2482015-05-04 14:21:22 -04002824 if (safeIndex != -1)
2825 {
2826 TConstantUnion *safeConstantUnion = new TConstantUnion();
2827 safeConstantUnion->setIConst(safeIndex);
2828 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
2829 }
2830
Jamie Madillb98c3a82015-07-23 14:26:04 -04002831 indexedExpression =
2832 intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002833 }
2834 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002835 else
2836 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002837 indexedExpression =
2838 intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
Jamie Madill7164cf42013-07-08 13:30:59 -04002839 }
2840
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002841 if (indexedExpression == 0)
2842 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002843 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002844 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002845 indexedExpression =
2846 intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002847 }
2848 else if (baseExpression->isArray())
2849 {
Olli Etuahob3fbd862015-09-30 17:55:02 +03002850 TType indexedType = baseExpression->getType();
2851 indexedType.clearArrayness();
2852 indexedExpression->setType(indexedType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002853 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002854 else if (baseExpression->isMatrix())
2855 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002856 indexedExpression->setType(TType(baseExpression->getBasicType(),
Olli Etuahob3fbd862015-09-30 17:55:02 +03002857 baseExpression->getPrecision(), EvqTemporary,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002858 static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002859 }
2860 else if (baseExpression->isVector())
2861 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002862 indexedExpression->setType(
Olli Etuahob3fbd862015-09-30 17:55:02 +03002863 TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002864 }
2865 else
2866 {
2867 indexedExpression->setType(baseExpression->getType());
2868 }
2869
Olli Etuahob3fbd862015-09-30 17:55:02 +03002870 if (baseExpression->getType().getQualifier() == EvqConst &&
2871 indexExpression->getType().getQualifier() == EvqConst)
2872 {
2873 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2874 }
2875 else
2876 {
2877 indexedExpression->getTypePointer()->setQualifier(EvqTemporary);
2878 }
2879
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002880 return indexedExpression;
2881}
2882
Olli Etuaho90892fb2016-07-14 14:44:51 +03002883int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
2884 const TSourceLoc &location,
2885 int index,
2886 int arraySize,
2887 const char *reason,
2888 const char *token)
2889{
2890 if (index >= arraySize || index < 0)
2891 {
2892 std::stringstream extraInfoStream;
2893 extraInfoStream << "'" << index << "'";
2894 std::string extraInfo = extraInfoStream.str();
2895 outOfRangeError(outOfRangeIndexIsError, location, reason, token, extraInfo.c_str());
2896 if (index < 0)
2897 {
2898 return 0;
2899 }
2900 else
2901 {
2902 return arraySize - 1;
2903 }
2904 }
2905 return index;
2906}
2907
Jamie Madillb98c3a82015-07-23 14:26:04 -04002908TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
2909 const TSourceLoc &dotLocation,
2910 const TString &fieldString,
2911 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002912{
2913 TIntermTyped *indexedExpression = NULL;
2914
2915 if (baseExpression->isArray())
2916 {
2917 error(fieldLocation, "cannot apply dot operator to an array", ".");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002918 }
2919
2920 if (baseExpression->isVector())
2921 {
2922 TVectorFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002923 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields,
2924 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002925 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002926 fields.num = 1;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002927 fields.offsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002928 }
2929
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002930 if (baseExpression->getAsConstantUnion())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002931 {
2932 // constant folding for vector fields
Olli Etuaho90892fb2016-07-14 14:44:51 +03002933 indexedExpression =
2934 foldVectorSwizzle(fields, baseExpression->getAsConstantUnion(), fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002935 }
2936 else
2937 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302938 TIntermTyped *index = intermediate.addSwizzle(fields, fieldLocation);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002939 indexedExpression =
2940 intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002941 }
2942 if (indexedExpression == nullptr)
2943 {
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002944 indexedExpression = baseExpression;
2945 }
2946 else
2947 {
2948 // Note that the qualifier set here will be corrected later.
Jamie Madillb98c3a82015-07-23 14:26:04 -04002949 indexedExpression->setType(TType(baseExpression->getBasicType(),
2950 baseExpression->getPrecision(), EvqTemporary,
Jamie Madillc2128ff2016-07-04 10:26:17 -04002951 static_cast<unsigned char>(fields.num)));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002952 }
2953 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002954 else if (baseExpression->getBasicType() == EbtStruct)
2955 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002956 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302957 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04002958 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002959 {
2960 error(dotLocation, "structure has no fields", "Internal Error");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002961 indexedExpression = baseExpression;
2962 }
2963 else
2964 {
2965 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002966 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002967 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002968 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002969 {
2970 fieldFound = true;
2971 break;
2972 }
2973 }
2974 if (fieldFound)
2975 {
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002976 if (baseExpression->getAsConstantUnion())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002977 {
2978 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2979 if (indexedExpression == 0)
2980 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002981 indexedExpression = baseExpression;
2982 }
2983 else
2984 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002985 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002986 }
2987 }
2988 else
2989 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002990 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002991 unionArray->setIConst(i);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002992 TIntermTyped *index = intermediate.addConstantUnion(
2993 unionArray, *fields[i]->type(), fieldLocation);
2994 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression,
2995 index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002996 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002997 }
2998 }
2999 else
3000 {
3001 error(dotLocation, " no such field in structure", fieldString.c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003002 indexedExpression = baseExpression;
3003 }
3004 }
3005 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003006 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003007 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003008 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05303009 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003010 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003011 {
3012 error(dotLocation, "interface block has no fields", "Internal Error");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003013 indexedExpression = baseExpression;
3014 }
3015 else
3016 {
3017 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003018 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003019 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003020 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003021 {
3022 fieldFound = true;
3023 break;
3024 }
3025 }
3026 if (fieldFound)
3027 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003028 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003029 unionArray->setIConst(i);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003030 TIntermTyped *index =
3031 intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
3032 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock,
3033 baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04003034 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003035 }
3036 else
3037 {
3038 error(dotLocation, " no such field in interface block", fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003039 indexedExpression = baseExpression;
3040 }
3041 }
3042 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003043 else
3044 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003045 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003046 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03003047 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303048 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003049 }
3050 else
3051 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303052 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003053 " field selection requires structure, vector, or interface block on left hand "
3054 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303055 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003056 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003057 indexedExpression = baseExpression;
3058 }
3059
Olli Etuahob1edc4f2015-11-02 17:20:03 +02003060 if (baseExpression->getQualifier() == EvqConst)
3061 {
3062 indexedExpression->getTypePointer()->setQualifier(EvqConst);
3063 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003064 else
3065 {
3066 indexedExpression->getTypePointer()->setQualifier(EvqTemporary);
3067 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02003068
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003069 return indexedExpression;
3070}
3071
Jamie Madillb98c3a82015-07-23 14:26:04 -04003072TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3073 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003074{
Martin Radev802abe02016-08-04 17:48:32 +03003075 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003076
3077 if (qualifierType == "shared")
3078 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003079 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003080 }
3081 else if (qualifierType == "packed")
3082 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003083 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003084 }
3085 else if (qualifierType == "std140")
3086 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003087 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003088 }
3089 else if (qualifierType == "row_major")
3090 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003091 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003092 }
3093 else if (qualifierType == "column_major")
3094 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003095 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003096 }
3097 else if (qualifierType == "location")
3098 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003099 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
3100 "location requires an argument");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003101 }
3102 else
3103 {
3104 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003105 }
3106
Jamie Madilla5efff92013-06-06 11:56:47 -04003107 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003108}
3109
Martin Radev802abe02016-08-04 17:48:32 +03003110void TParseContext::parseLocalSize(const TString &qualifierType,
3111 const TSourceLoc &qualifierTypeLine,
3112 int intValue,
3113 const TSourceLoc &intValueLine,
3114 const std::string &intValueString,
3115 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03003116 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03003117{
Olli Etuaho856c4972016-08-08 11:38:39 +03003118 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03003119 if (intValue < 1)
3120 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03003121 std::string errorMessage = std::string(getWorkGroupSizeString(index)) + " must be positive";
Martin Radev802abe02016-08-04 17:48:32 +03003122 error(intValueLine, "out of range:", intValueString.c_str(), errorMessage.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003123 }
3124 (*localSize)[index] = intValue;
3125}
3126
Jamie Madillb98c3a82015-07-23 14:26:04 -04003127TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3128 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003129 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303130 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003131{
Martin Radev802abe02016-08-04 17:48:32 +03003132 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003133
Martin Radev802abe02016-08-04 17:48:32 +03003134 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003135
Martin Radev802abe02016-08-04 17:48:32 +03003136 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003137 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003138 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003139 if (intValue < 0)
3140 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003141 error(intValueLine, "out of range:", intValueString.c_str(),
3142 "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003143 }
3144 else
3145 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003146 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003147 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003148 }
Martin Radev802abe02016-08-04 17:48:32 +03003149 else if (qualifierType == "local_size_x")
3150 {
3151 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
3152 &qualifier.localSize);
3153 }
3154 else if (qualifierType == "local_size_y")
3155 {
3156 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
3157 &qualifier.localSize);
3158 }
3159 else if (qualifierType == "local_size_z")
3160 {
3161 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
3162 &qualifier.localSize);
3163 }
3164 else
3165 {
3166 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003167 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003168
Jamie Madilla5efff92013-06-06 11:56:47 -04003169 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003170}
3171
Jamie Madillb98c3a82015-07-23 14:26:04 -04003172TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03003173 TLayoutQualifier rightQualifier,
3174 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003175{
Jamie Madilla5efff92013-06-06 11:56:47 -04003176 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003177
Jamie Madilla5efff92013-06-06 11:56:47 -04003178 if (rightQualifier.location != -1)
3179 {
3180 joinedQualifier.location = rightQualifier.location;
3181 }
3182 if (rightQualifier.matrixPacking != EmpUnspecified)
3183 {
3184 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
3185 }
3186 if (rightQualifier.blockStorage != EbsUnspecified)
3187 {
3188 joinedQualifier.blockStorage = rightQualifier.blockStorage;
3189 }
3190
Martin Radev802abe02016-08-04 17:48:32 +03003191 for (size_t i = 0u; i < rightQualifier.localSize.size(); ++i)
3192 {
3193 if (rightQualifier.localSize[i] != -1)
3194 {
3195 if (joinedQualifier.localSize[i] != -1 &&
3196 joinedQualifier.localSize[i] != rightQualifier.localSize[i])
3197 {
3198 error(rightQualifierLocation,
3199 "Cannot have multiple different work group size specifiers",
Martin Radev4c4c8e72016-08-04 12:25:34 +03003200 getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03003201 }
3202 joinedQualifier.localSize[i] = rightQualifier.localSize[i];
3203 }
3204 }
3205
Jamie Madilla5efff92013-06-06 11:56:47 -04003206 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003207}
3208
Martin Radev70866b82016-07-22 15:27:42 +03003209TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
3210 const TTypeQualifierBuilder &typeQualifierBuilder,
3211 TPublicType *typeSpecifier,
3212 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003213{
Martin Radev70866b82016-07-22 15:27:42 +03003214 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003215
Martin Radev70866b82016-07-22 15:27:42 +03003216 typeSpecifier->qualifier = typeQualifier.qualifier;
3217 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
3218 typeSpecifier->invariant = typeQualifier.invariant;
3219 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05303220 {
Martin Radev70866b82016-07-22 15:27:42 +03003221 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003222 }
Martin Radev70866b82016-07-22 15:27:42 +03003223 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003224}
3225
Jamie Madillb98c3a82015-07-23 14:26:04 -04003226TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3227 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003228{
Martin Radev70866b82016-07-22 15:27:42 +03003229 checkPrecisionSpecified(typeSpecifier.line, typeSpecifier.precision, typeSpecifier.type);
3230
Olli Etuaho856c4972016-08-08 11:38:39 +03003231 checkIsNonVoid(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier.type);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003232
Olli Etuaho856c4972016-08-08 11:38:39 +03003233 checkWorkGroupSizeIsNotSpecified(typeSpecifier.line, typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003234
Arun Patole7e7e68d2015-05-22 12:02:25 +05303235 for (unsigned int i = 0; i < fieldList->size(); ++i)
3236 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003237 //
3238 // Careful not to replace already known aspects of type, like array-ness
3239 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303240 TType *type = (*fieldList)[i]->type();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003241 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003242 type->setPrimarySize(typeSpecifier.primarySize);
3243 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003244 type->setPrecision(typeSpecifier.precision);
3245 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003246 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
Martin Radev70866b82016-07-22 15:27:42 +03003247 type->setInvariant(typeSpecifier.invariant);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003248
3249 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303250 if (type->isArray())
3251 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003252 checkIsValidTypeForArray(typeSpecifier.line, typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003253 }
3254 if (typeSpecifier.array)
Olli Etuaho856c4972016-08-08 11:38:39 +03003255 type->setArraySize(static_cast<unsigned int>(typeSpecifier.arraySize));
Arun Patole7e7e68d2015-05-22 12:02:25 +05303256 if (typeSpecifier.userDef)
3257 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003258 type->setStruct(typeSpecifier.userDef->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003259 }
3260
Olli Etuaho8a176262016-08-16 14:23:01 +03003261 checkIsBelowStructNestingLimit(typeSpecifier.line, *(*fieldList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003262 }
3263
Jamie Madill98493dd2013-07-08 14:39:03 -04003264 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003265}
3266
Jamie Madillb98c3a82015-07-23 14:26:04 -04003267TPublicType TParseContext::addStructure(const TSourceLoc &structLine,
3268 const TSourceLoc &nameLine,
3269 const TString *structName,
3270 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003271{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303272 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003273 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003274
Jamie Madill9b820842015-02-12 10:40:10 -05003275 // Store a bool in the struct if we're at global scope, to allow us to
3276 // skip the local struct scoping workaround in HLSL.
Jamie Madillb960cc42015-02-12 15:33:20 +00003277 structure->setUniqueId(TSymbolTable::nextUniqueId());
Jamie Madill9b820842015-02-12 10:40:10 -05003278 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003279
Jamie Madill98493dd2013-07-08 14:39:03 -04003280 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003281 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003282 checkIsNotReserved(nameLine, *structName);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303283 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
3284 if (!symbolTable.declare(userTypeDef))
3285 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003286 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003287 }
3288 }
3289
3290 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04003291 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003292 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003293 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04003294 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003295 switch (qualifier)
3296 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003297 case EvqGlobal:
3298 case EvqTemporary:
3299 break;
3300 default:
3301 error(field.line(), "invalid qualifier on struct member",
3302 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003303 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003304 }
Martin Radev70866b82016-07-22 15:27:42 +03003305 if (field.type()->isInvariant())
3306 {
3307 error(field.line(), "invalid qualifier on struct member", "invariant");
3308 }
3309
3310 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003311 }
3312
3313 TPublicType publicType;
3314 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003315 publicType.userDef = structureType;
Olli Etuahobd163f62015-11-13 12:15:38 +02003316 publicType.isStructSpecifier = true;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003317 exitStructDeclaration();
3318
3319 return publicType;
3320}
3321
Jamie Madillb98c3a82015-07-23 14:26:04 -04003322TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
3323 TIntermAggregate *statementList,
3324 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02003325{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003326 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003327 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02003328 init->isVector())
3329 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003330 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
3331 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003332 return nullptr;
3333 }
3334
Olli Etuahoac5274d2015-02-20 10:19:08 +02003335 if (statementList)
3336 {
3337 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
3338 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02003339 return nullptr;
3340 }
3341 }
3342
Olli Etuahoa3a36662015-02-17 13:46:51 +02003343 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3344 if (node == nullptr)
3345 {
3346 error(loc, "erroneous switch statement", "switch");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003347 return nullptr;
3348 }
3349 return node;
3350}
3351
3352TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3353{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003354 if (mSwitchNestingLevel == 0)
3355 {
3356 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003357 return nullptr;
3358 }
3359 if (condition == nullptr)
3360 {
3361 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003362 return nullptr;
3363 }
3364 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04003365 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02003366 {
3367 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003368 }
3369 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003370 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
3371 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
3372 // fold in case labels.
3373 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02003374 {
3375 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003376 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003377 TIntermCase *node = intermediate.addCase(condition, loc);
3378 if (node == nullptr)
3379 {
3380 error(loc, "erroneous case statement", "case");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003381 return nullptr;
3382 }
3383 return node;
3384}
3385
3386TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3387{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003388 if (mSwitchNestingLevel == 0)
3389 {
3390 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003391 return nullptr;
3392 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003393 TIntermCase *node = intermediate.addCase(nullptr, loc);
3394 if (node == nullptr)
3395 {
3396 error(loc, "erroneous default statement", "default");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003397 return nullptr;
3398 }
3399 return node;
3400}
3401
Jamie Madillb98c3a82015-07-23 14:26:04 -04003402TIntermTyped *TParseContext::createUnaryMath(TOperator op,
3403 TIntermTyped *child,
3404 const TSourceLoc &loc,
3405 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003406{
3407 if (child == nullptr)
3408 {
3409 return nullptr;
3410 }
3411
3412 switch (op)
3413 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003414 case EOpLogicalNot:
3415 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
3416 child->isVector())
3417 {
3418 return nullptr;
3419 }
3420 break;
3421 case EOpBitwiseNot:
3422 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3423 child->isMatrix() || child->isArray())
3424 {
3425 return nullptr;
3426 }
3427 break;
3428 case EOpPostIncrement:
3429 case EOpPreIncrement:
3430 case EOpPostDecrement:
3431 case EOpPreDecrement:
3432 case EOpNegative:
3433 case EOpPositive:
3434 if (child->getBasicType() == EbtStruct || child->getBasicType() == EbtBool ||
Olli Etuaho558b0382016-08-26 17:54:34 +03003435 child->isArray() || IsSampler(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04003436 {
3437 return nullptr;
3438 }
3439 // Operators for built-ins are already type checked against their prototype.
3440 default:
3441 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003442 }
3443
Olli Etuahof6c694b2015-03-26 14:50:53 +02003444 return intermediate.addUnaryMath(op, child, loc, funcReturnType);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003445}
3446
Olli Etuaho09b22472015-02-11 11:47:26 +02003447TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3448{
Olli Etuahof6c694b2015-03-26 14:50:53 +02003449 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003450 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003451 {
3452 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003453 return child;
3454 }
3455 return node;
3456}
3457
Jamie Madillb98c3a82015-07-23 14:26:04 -04003458TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
3459 TIntermTyped *child,
3460 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003461{
Olli Etuaho856c4972016-08-08 11:38:39 +03003462 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02003463 return addUnaryMath(op, child, loc);
3464}
3465
Jamie Madillb98c3a82015-07-23 14:26:04 -04003466bool TParseContext::binaryOpCommonCheck(TOperator op,
3467 TIntermTyped *left,
3468 TIntermTyped *right,
3469 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003470{
Olli Etuaho244be012016-08-18 15:26:02 +03003471 if (left->getType().getStruct() || right->getType().getStruct())
3472 {
3473 switch (op)
3474 {
3475 case EOpIndexDirectStruct:
3476 ASSERT(left->getType().getStruct());
3477 break;
3478 case EOpEqual:
3479 case EOpNotEqual:
3480 case EOpAssign:
3481 case EOpInitialize:
3482 if (left->getType() != right->getType())
3483 {
3484 return false;
3485 }
3486 break;
3487 default:
3488 error(loc, "Invalid operation for structs", GetOperatorString(op));
3489 return false;
3490 }
3491 }
3492
Olli Etuahod6b14282015-03-17 14:31:35 +02003493 if (left->isArray() || right->isArray())
3494 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003495 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003496 {
3497 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3498 return false;
3499 }
3500
3501 if (left->isArray() != right->isArray())
3502 {
3503 error(loc, "array / non-array mismatch", GetOperatorString(op));
3504 return false;
3505 }
3506
3507 switch (op)
3508 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003509 case EOpEqual:
3510 case EOpNotEqual:
3511 case EOpAssign:
3512 case EOpInitialize:
3513 break;
3514 default:
3515 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3516 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02003517 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003518 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003519 if (left->getArraySize() != right->getArraySize())
3520 {
3521 error(loc, "array size mismatch", GetOperatorString(op));
3522 return false;
3523 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003524 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003525
3526 // Check ops which require integer / ivec parameters
3527 bool isBitShift = false;
3528 switch (op)
3529 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003530 case EOpBitShiftLeft:
3531 case EOpBitShiftRight:
3532 case EOpBitShiftLeftAssign:
3533 case EOpBitShiftRightAssign:
3534 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3535 // check that the basic type is an integer type.
3536 isBitShift = true;
3537 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3538 {
3539 return false;
3540 }
3541 break;
3542 case EOpBitwiseAnd:
3543 case EOpBitwiseXor:
3544 case EOpBitwiseOr:
3545 case EOpBitwiseAndAssign:
3546 case EOpBitwiseXorAssign:
3547 case EOpBitwiseOrAssign:
3548 // It is enough to check the type of only one operand, since later it
3549 // is checked that the operand types match.
3550 if (!IsInteger(left->getBasicType()))
3551 {
3552 return false;
3553 }
3554 break;
3555 default:
3556 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003557 }
3558
3559 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3560 // So the basic type should usually match.
3561 if (!isBitShift && left->getBasicType() != right->getBasicType())
3562 {
3563 return false;
3564 }
3565
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003566 // Check that:
3567 // 1. Type sizes match exactly on ops that require that.
3568 // 2. Restrictions for structs that contain arrays or samplers are respected.
3569 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003570 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003571 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003572 case EOpAssign:
3573 case EOpInitialize:
3574 case EOpEqual:
3575 case EOpNotEqual:
3576 // ESSL 1.00 sections 5.7, 5.8, 5.9
3577 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
3578 {
3579 error(loc, "undefined operation for structs containing arrays",
3580 GetOperatorString(op));
3581 return false;
3582 }
3583 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3584 // we interpret the spec so that this extends to structs containing samplers,
3585 // similarly to ESSL 1.00 spec.
3586 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
3587 left->getType().isStructureContainingSamplers())
3588 {
3589 error(loc, "undefined operation for structs containing samplers",
3590 GetOperatorString(op));
3591 return false;
3592 }
3593 case EOpLessThan:
3594 case EOpGreaterThan:
3595 case EOpLessThanEqual:
3596 case EOpGreaterThanEqual:
3597 if ((left->getNominalSize() != right->getNominalSize()) ||
3598 (left->getSecondarySize() != right->getSecondarySize()))
3599 {
3600 return false;
3601 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003602 break;
3603 case EOpAdd:
3604 case EOpSub:
3605 case EOpDiv:
3606 case EOpIMod:
3607 case EOpBitShiftLeft:
3608 case EOpBitShiftRight:
3609 case EOpBitwiseAnd:
3610 case EOpBitwiseXor:
3611 case EOpBitwiseOr:
3612 case EOpAddAssign:
3613 case EOpSubAssign:
3614 case EOpDivAssign:
3615 case EOpIModAssign:
3616 case EOpBitShiftLeftAssign:
3617 case EOpBitShiftRightAssign:
3618 case EOpBitwiseAndAssign:
3619 case EOpBitwiseXorAssign:
3620 case EOpBitwiseOrAssign:
3621 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
3622 {
3623 return false;
3624 }
3625
3626 // Are the sizes compatible?
3627 if (left->getNominalSize() != right->getNominalSize() ||
3628 left->getSecondarySize() != right->getSecondarySize())
3629 {
3630 // If the nominal sizes of operands do not match:
3631 // One of them must be a scalar.
3632 if (!left->isScalar() && !right->isScalar())
3633 return false;
3634
3635 // In the case of compound assignment other than multiply-assign,
3636 // the right side needs to be a scalar. Otherwise a vector/matrix
3637 // would be assigned to a scalar. A scalar can't be shifted by a
3638 // vector either.
3639 if (!right->isScalar() &&
3640 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
3641 return false;
3642 }
3643 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003644 default:
3645 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003646 }
3647
Olli Etuahod6b14282015-03-17 14:31:35 +02003648 return true;
3649}
3650
Olli Etuaho1dded802016-08-18 18:13:13 +03003651bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
3652 const TType &left,
3653 const TType &right)
3654{
3655 switch (op)
3656 {
3657 case EOpMul:
3658 case EOpMulAssign:
3659 return left.getNominalSize() == right.getNominalSize() &&
3660 left.getSecondarySize() == right.getSecondarySize();
3661 case EOpVectorTimesScalar:
3662 return true;
3663 case EOpVectorTimesScalarAssign:
3664 ASSERT(!left.isMatrix() && !right.isMatrix());
3665 return left.isVector() && !right.isVector();
3666 case EOpVectorTimesMatrix:
3667 return left.getNominalSize() == right.getRows();
3668 case EOpVectorTimesMatrixAssign:
3669 ASSERT(!left.isMatrix() && right.isMatrix());
3670 return left.isVector() && left.getNominalSize() == right.getRows() &&
3671 left.getNominalSize() == right.getCols();
3672 case EOpMatrixTimesVector:
3673 return left.getCols() == right.getNominalSize();
3674 case EOpMatrixTimesScalar:
3675 return true;
3676 case EOpMatrixTimesScalarAssign:
3677 ASSERT(left.isMatrix() && !right.isMatrix());
3678 return !right.isVector();
3679 case EOpMatrixTimesMatrix:
3680 return left.getCols() == right.getRows();
3681 case EOpMatrixTimesMatrixAssign:
3682 ASSERT(left.isMatrix() && right.isMatrix());
3683 // We need to check two things:
3684 // 1. The matrix multiplication step is valid.
3685 // 2. The result will have the same number of columns as the lvalue.
3686 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
3687
3688 default:
3689 UNREACHABLE();
3690 return false;
3691 }
3692}
3693
Jamie Madillb98c3a82015-07-23 14:26:04 -04003694TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
3695 TIntermTyped *left,
3696 TIntermTyped *right,
3697 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02003698{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003699 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003700 return nullptr;
3701
Olli Etuahofc1806e2015-03-17 13:03:11 +02003702 switch (op)
3703 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003704 case EOpEqual:
3705 case EOpNotEqual:
3706 break;
3707 case EOpLessThan:
3708 case EOpGreaterThan:
3709 case EOpLessThanEqual:
3710 case EOpGreaterThanEqual:
Olli Etuaho244be012016-08-18 15:26:02 +03003711 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3712 !right->getType().getStruct());
3713 if (left->isMatrix() || left->isVector())
Jamie Madillb98c3a82015-07-23 14:26:04 -04003714 {
3715 return nullptr;
3716 }
3717 break;
3718 case EOpLogicalOr:
3719 case EOpLogicalXor:
3720 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03003721 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3722 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003723 if (left->getBasicType() != EbtBool || left->isMatrix() || left->isVector())
3724 {
3725 return nullptr;
3726 }
3727 break;
3728 case EOpAdd:
3729 case EOpSub:
3730 case EOpDiv:
3731 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03003732 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3733 !right->getType().getStruct());
3734 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04003735 {
3736 return nullptr;
3737 }
3738 break;
3739 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03003740 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3741 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003742 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03003743 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04003744 {
3745 return nullptr;
3746 }
3747 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003748 default:
3749 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003750 }
3751
Olli Etuaho1dded802016-08-18 18:13:13 +03003752 if (op == EOpMul)
3753 {
3754 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
3755 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
3756 {
3757 return nullptr;
3758 }
3759 }
3760
Olli Etuaho3fdec912016-08-18 15:08:06 +03003761 TIntermBinary *node = new TIntermBinary(op, left, right);
3762 node->setLine(loc);
3763
Olli Etuaho3fdec912016-08-18 15:08:06 +03003764 // See if we can fold constants.
3765 TIntermTyped *foldedNode = node->fold(&mDiagnostics);
3766 if (foldedNode)
3767 return foldedNode;
3768
3769 return node;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003770}
3771
Jamie Madillb98c3a82015-07-23 14:26:04 -04003772TIntermTyped *TParseContext::addBinaryMath(TOperator op,
3773 TIntermTyped *left,
3774 TIntermTyped *right,
3775 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003776{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003777 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003778 if (node == 0)
3779 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003780 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3781 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003782 return left;
3783 }
3784 return node;
3785}
3786
Jamie Madillb98c3a82015-07-23 14:26:04 -04003787TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
3788 TIntermTyped *left,
3789 TIntermTyped *right,
3790 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003791{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003792 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003793 if (node == 0)
3794 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003795 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3796 right->getCompleteString());
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003797 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02003798 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003799 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
3800 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003801 }
3802 return node;
3803}
3804
Jamie Madillb98c3a82015-07-23 14:26:04 -04003805TIntermTyped *TParseContext::createAssign(TOperator op,
3806 TIntermTyped *left,
3807 TIntermTyped *right,
3808 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003809{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003810 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003811 {
Olli Etuaho1dded802016-08-18 18:13:13 +03003812 if (op == EOpMulAssign)
3813 {
3814 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
3815 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
3816 {
3817 return nullptr;
3818 }
3819 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03003820 TIntermBinary *node = new TIntermBinary(op, left, right);
3821 node->setLine(loc);
3822
Olli Etuaho3fdec912016-08-18 15:08:06 +03003823 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02003824 }
3825 return nullptr;
3826}
3827
Jamie Madillb98c3a82015-07-23 14:26:04 -04003828TIntermTyped *TParseContext::addAssign(TOperator op,
3829 TIntermTyped *left,
3830 TIntermTyped *right,
3831 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003832{
3833 TIntermTyped *node = createAssign(op, left, right, loc);
3834 if (node == nullptr)
3835 {
3836 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02003837 return left;
3838 }
3839 return node;
3840}
3841
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003842TIntermTyped *TParseContext::addComma(TIntermTyped *left,
3843 TIntermTyped *right,
3844 const TSourceLoc &loc)
3845{
Corentin Wallez0d959252016-07-12 17:26:32 -04003846 // WebGL2 section 5.26, the following results in an error:
3847 // "Sequence operator applied to void, arrays, or structs containing arrays"
3848 if (mShaderSpec == SH_WEBGL2_SPEC && (left->isArray() || left->getBasicType() == EbtVoid ||
3849 left->getType().isStructureContainingArrays() ||
3850 right->isArray() || right->getBasicType() == EbtVoid ||
3851 right->getType().isStructureContainingArrays()))
3852 {
3853 error(loc,
3854 "sequence operator is not allowed for void, arrays, or structs containing arrays",
3855 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04003856 }
3857
Olli Etuaho15200042015-11-04 16:56:31 +02003858 return intermediate.addComma(left, right, loc, mShaderVersion);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003859}
3860
Olli Etuaho49300862015-02-20 14:54:49 +02003861TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3862{
3863 switch (op)
3864 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003865 case EOpContinue:
3866 if (mLoopNestingLevel <= 0)
3867 {
3868 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04003869 }
3870 break;
3871 case EOpBreak:
3872 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
3873 {
3874 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04003875 }
3876 break;
3877 case EOpReturn:
3878 if (mCurrentFunctionType->getBasicType() != EbtVoid)
3879 {
3880 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04003881 }
3882 break;
3883 default:
3884 // No checks for discard
3885 break;
Olli Etuaho49300862015-02-20 14:54:49 +02003886 }
3887 return intermediate.addBranch(op, loc);
3888}
3889
Jamie Madillb98c3a82015-07-23 14:26:04 -04003890TIntermBranch *TParseContext::addBranch(TOperator op,
3891 TIntermTyped *returnValue,
3892 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02003893{
3894 ASSERT(op == EOpReturn);
3895 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003896 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02003897 {
3898 error(loc, "void function cannot return a value", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02003899 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003900 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02003901 {
3902 error(loc, "function return is not matching type:", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02003903 }
3904 return intermediate.addBranch(op, returnValue, loc);
3905}
3906
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003907void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
3908{
3909 ASSERT(!functionCall->isUserDefined());
3910 const TString &name = functionCall->getName();
3911 TIntermNode *offset = nullptr;
3912 TIntermSequence *arguments = functionCall->getSequence();
3913 if (name.compare(0, 16, "texelFetchOffset") == 0 ||
3914 name.compare(0, 16, "textureLodOffset") == 0 ||
3915 name.compare(0, 20, "textureProjLodOffset") == 0 ||
3916 name.compare(0, 17, "textureGradOffset") == 0 ||
3917 name.compare(0, 21, "textureProjGradOffset") == 0)
3918 {
3919 offset = arguments->back();
3920 }
3921 else if (name.compare(0, 13, "textureOffset") == 0 ||
3922 name.compare(0, 17, "textureProjOffset") == 0)
3923 {
3924 // A bias parameter might follow the offset parameter.
3925 ASSERT(arguments->size() >= 3);
3926 offset = (*arguments)[2];
3927 }
3928 if (offset != nullptr)
3929 {
3930 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
3931 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
3932 {
3933 TString unmangledName = TFunction::unmangleName(name);
3934 error(functionCall->getLine(), "Texture offset must be a constant expression",
3935 unmangledName.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003936 }
3937 else
3938 {
3939 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
3940 size_t size = offsetConstantUnion->getType().getObjectSize();
3941 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
3942 for (size_t i = 0u; i < size; ++i)
3943 {
3944 int offsetValue = values[i].getIConst();
3945 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
3946 {
3947 std::stringstream tokenStream;
3948 tokenStream << offsetValue;
3949 std::string token = tokenStream.str();
3950 error(offset->getLine(), "Texture offset value out of valid range",
3951 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003952 }
3953 }
3954 }
3955 }
3956}
3957
Jamie Madillb98c3a82015-07-23 14:26:04 -04003958TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
3959 TIntermNode *paramNode,
3960 TIntermNode *thisNode,
3961 const TSourceLoc &loc,
3962 bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003963{
Jamie Madillb98c3a82015-07-23 14:26:04 -04003964 *fatalError = false;
3965 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003966 TIntermTyped *callNode = nullptr;
3967
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003968 if (thisNode != nullptr)
3969 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003970 TConstantUnion *unionArray = new TConstantUnion[1];
Jamie Madillb98c3a82015-07-23 14:26:04 -04003971 int arraySize = 0;
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003972 TIntermTyped *typedThis = thisNode->getAsTyped();
3973 if (fnCall->getName() != "length")
3974 {
3975 error(loc, "invalid method", fnCall->getName().c_str());
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003976 }
3977 else if (paramNode != nullptr)
3978 {
3979 error(loc, "method takes no parameters", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003980 }
3981 else if (typedThis == nullptr || !typedThis->isArray())
3982 {
3983 error(loc, "length can only be called on arrays", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003984 }
3985 else
3986 {
Olli Etuaho96e67382015-04-23 14:27:02 +03003987 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03003988 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003989 {
Olli Etuaho39282e12015-04-23 15:41:48 +03003990 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003991 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03003992 // (func()).length()
3993 // (int[3](0, 1, 2)).length()
Jamie Madillb98c3a82015-07-23 14:26:04 -04003994 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
3995 // expression.
3996 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
3997 // spec section 5.9 which allows "An array, vector or matrix expression with the
3998 // length method applied".
3999 error(loc, "length can only be called on array names, not on array expressions",
4000 "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004001 }
4002 }
Olli Etuaho96e67382015-04-23 14:27:02 +03004003 unionArray->setIConst(arraySize);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004004 callNode =
4005 intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004006 }
4007 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004008 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004009 // Then this should be a constructor.
Olli Etuaho856c4972016-08-08 11:38:39 +03004010 callNode = addConstructor(paramNode, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004011 }
4012 else
4013 {
4014 //
4015 // Not a constructor. Find it in the symbol table.
4016 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05304017 const TFunction *fnCandidate;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004018 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004019 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004020 if (fnCandidate)
4021 {
4022 //
4023 // A declared function.
4024 //
Olli Etuaho383b7912016-08-05 11:22:59 +03004025 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004026 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004027 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004028 }
4029 op = fnCandidate->getBuiltInOp();
4030 if (builtIn && op != EOpNull)
4031 {
4032 //
4033 // A function call mapped to a built-in operation.
4034 //
4035 if (fnCandidate->getParamCount() == 1)
4036 {
4037 //
4038 // Treat it like a built-in unary operator.
4039 //
Olli Etuaho15c2ac32015-11-09 15:51:43 +02004040 TIntermAggregate *paramAgg = paramNode->getAsAggregate();
4041 paramNode = paramAgg->getSequence()->front();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004042 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc,
4043 &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004044 if (callNode == nullptr)
4045 {
4046 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004047 extraInfoStream
4048 << "built in unary operator function. Type: "
4049 << static_cast<TIntermTyped *>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004050 std::string extraInfo = extraInfoStream.str();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004051 error(paramNode->getLine(), " wrong operand type", "Internal Error",
4052 extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004053 *fatalError = true;
4054 return nullptr;
4055 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004056 }
4057 else
4058 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004059 TIntermAggregate *aggregate =
4060 intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004061 aggregate->setType(fnCandidate->getReturnType());
4062 aggregate->setPrecisionFromChildren();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02004063 if (aggregate->areChildrenConstQualified())
4064 {
4065 aggregate->getTypePointer()->setQualifier(EvqConst);
4066 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004067
4068 // Some built-in functions have out parameters too.
4069 functionCallLValueErrorCheck(fnCandidate, aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05304070
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004071 // See if we can constant fold a built-in. Note that this may be possible even
4072 // if it is not const-qualified.
Olli Etuahob43846e2015-06-02 18:18:57 +03004073 TIntermTyped *foldedNode = intermediate.foldAggregateBuiltIn(aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05304074 if (foldedNode)
4075 {
Arun Patole274f0702015-05-05 13:33:30 +05304076 callNode = foldedNode;
4077 }
Olli Etuahob43846e2015-06-02 18:18:57 +03004078 else
4079 {
4080 callNode = aggregate;
4081 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004082 }
4083 }
4084 else
4085 {
4086 // This is a real function call
Jamie Madillb98c3a82015-07-23 14:26:04 -04004087 TIntermAggregate *aggregate =
4088 intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004089 aggregate->setType(fnCandidate->getReturnType());
4090
Jamie Madillb98c3a82015-07-23 14:26:04 -04004091 // this is how we know whether the given function is a builtIn function or a user
4092 // defined function
4093 // if builtIn == false, it's a userDefined -> could be an overloaded
4094 // builtIn function also
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004095 // if builtIn == true, it's definitely a builtIn function with EOpNull
4096 if (!builtIn)
4097 aggregate->setUserDefined();
4098 aggregate->setName(fnCandidate->getMangledName());
Corentin Wallez71d147f2015-02-11 11:15:24 -08004099 aggregate->setFunctionId(fnCandidate->getUniqueId());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004100
4101 // This needs to happen after the name is set
4102 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004103 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004104 aggregate->setBuiltInFunctionPrecision();
4105
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004106 checkTextureOffsetConst(aggregate);
4107 }
4108
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004109 callNode = aggregate;
4110
4111 functionCallLValueErrorCheck(fnCandidate, aggregate);
4112 }
4113 }
4114 else
4115 {
4116 // error message was put out by findFunction()
4117 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004118 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004119 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004120 callNode = intermediate.addConstantUnion(unionArray,
4121 TType(EbtFloat, EbpUndefined, EvqConst), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004122 }
4123 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004124 return callNode;
4125}
4126
Jamie Madillb98c3a82015-07-23 14:26:04 -04004127TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
4128 TIntermTyped *trueBlock,
4129 TIntermTyped *falseBlock,
Olli Etuaho52901742015-04-15 13:42:45 +03004130 const TSourceLoc &loc)
4131{
Olli Etuaho856c4972016-08-08 11:38:39 +03004132 checkIsScalarBool(loc, cond);
Olli Etuaho52901742015-04-15 13:42:45 +03004133
4134 if (trueBlock->getType() != falseBlock->getType())
4135 {
4136 binaryOpError(loc, ":", trueBlock->getCompleteString(), falseBlock->getCompleteString());
Olli Etuaho52901742015-04-15 13:42:45 +03004137 return falseBlock;
4138 }
Olli Etuahoa2d53032015-04-15 14:14:44 +03004139 // ESSL1 sections 5.2 and 5.7:
4140 // ESSL3 section 5.7:
4141 // Ternary operator is not among the operators allowed for structures/arrays.
4142 if (trueBlock->isArray() || trueBlock->getBasicType() == EbtStruct)
4143 {
4144 error(loc, "ternary operator is not allowed for structures or arrays", ":");
Olli Etuahoa2d53032015-04-15 14:14:44 +03004145 return falseBlock;
4146 }
Corentin Wallez0d959252016-07-12 17:26:32 -04004147 // WebGL2 section 5.26, the following results in an error:
4148 // "Ternary operator applied to void, arrays, or structs containing arrays"
4149 if (mShaderSpec == SH_WEBGL2_SPEC && trueBlock->getBasicType() == EbtVoid)
4150 {
4151 error(loc, "ternary operator is not allowed for void", ":");
Corentin Wallez0d959252016-07-12 17:26:32 -04004152 return falseBlock;
4153 }
4154
Olli Etuaho52901742015-04-15 13:42:45 +03004155 return intermediate.addSelection(cond, trueBlock, falseBlock, loc);
4156}
Olli Etuaho49300862015-02-20 14:54:49 +02004157
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004158//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004159// Parse an array of strings using yyparse.
4160//
4161// Returns 0 for success.
4162//
Jamie Madillb98c3a82015-07-23 14:26:04 -04004163int PaParseStrings(size_t count,
4164 const char *const string[],
4165 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05304166 TParseContext *context)
4167{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004168 if ((count == 0) || (string == NULL))
4169 return 1;
4170
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004171 if (glslang_initialize(context))
4172 return 1;
4173
alokp@chromium.org408c45e2012-04-05 15:54:43 +00004174 int error = glslang_scan(count, string, length, context);
4175 if (!error)
4176 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004177
alokp@chromium.org73bc2982012-06-19 18:48:05 +00004178 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00004179
alokp@chromium.org6b495712012-06-29 00:06:58 +00004180 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004181}