blob: dab26e39ae6aaf8e0fc0edfd573f6c5859d60645 [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
Martin Radevc28888b2016-07-22 15:27:42 +030019namespace
20{
21// GLSL ES 3.10 does not impose a strict order on type qualifiers and allows multiple layout
22// declarations
23// GLSL ES 3.10 Revision 4, 4.10 Order of Qualification
24bool AreTypeQualifierChecksRelaxed(int shaderVersion)
25{
26 return shaderVersion >= 310;
27}
28} // namespace
29
alokp@chromium.org8b851c62012-06-15 16:25:11 +000030///////////////////////////////////////////////////////////////////////
31//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000032// Sub- vector and matrix fields
33//
34////////////////////////////////////////////////////////////////////////
35
36//
37// Look at a '.' field selector string and change it into offsets
38// for a vector.
39//
Jamie Madillb98c3a82015-07-23 14:26:04 -040040bool TParseContext::parseVectorFields(const TString &compString,
41 int vecSize,
42 TVectorFields &fields,
Arun Patole7e7e68d2015-05-22 12:02:25 +053043 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000044{
Jamie Madillb98c3a82015-07-23 14:26:04 -040045 fields.num = (int)compString.size();
Arun Patole7e7e68d2015-05-22 12:02:25 +053046 if (fields.num > 4)
47 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000048 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000049 return false;
50 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000051
Jamie Madillb98c3a82015-07-23 14:26:04 -040052 enum
53 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000054 exyzw,
55 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +000056 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000057 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000058
Arun Patole7e7e68d2015-05-22 12:02:25 +053059 for (int i = 0; i < fields.num; ++i)
60 {
61 switch (compString[i])
62 {
Jamie Madillb98c3a82015-07-23 14:26:04 -040063 case 'x':
64 fields.offsets[i] = 0;
65 fieldSet[i] = exyzw;
66 break;
67 case 'r':
68 fields.offsets[i] = 0;
69 fieldSet[i] = ergba;
70 break;
71 case 's':
72 fields.offsets[i] = 0;
73 fieldSet[i] = estpq;
74 break;
75 case 'y':
76 fields.offsets[i] = 1;
77 fieldSet[i] = exyzw;
78 break;
79 case 'g':
80 fields.offsets[i] = 1;
81 fieldSet[i] = ergba;
82 break;
83 case 't':
84 fields.offsets[i] = 1;
85 fieldSet[i] = estpq;
86 break;
87 case 'z':
88 fields.offsets[i] = 2;
89 fieldSet[i] = exyzw;
90 break;
91 case 'b':
92 fields.offsets[i] = 2;
93 fieldSet[i] = ergba;
94 break;
95 case 'p':
96 fields.offsets[i] = 2;
97 fieldSet[i] = estpq;
98 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053099
Jamie Madillb98c3a82015-07-23 14:26:04 -0400100 case 'w':
101 fields.offsets[i] = 3;
102 fieldSet[i] = exyzw;
103 break;
104 case 'a':
105 fields.offsets[i] = 3;
106 fieldSet[i] = ergba;
107 break;
108 case 'q':
109 fields.offsets[i] = 3;
110 fieldSet[i] = estpq;
111 break;
112 default:
113 error(line, "illegal vector field selection", compString.c_str());
114 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000115 }
116 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000117
Arun Patole7e7e68d2015-05-22 12:02:25 +0530118 for (int i = 0; i < fields.num; ++i)
119 {
120 if (fields.offsets[i] >= vecSize)
121 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400122 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000123 return false;
124 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000125
Arun Patole7e7e68d2015-05-22 12:02:25 +0530126 if (i > 0)
127 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400128 if (fieldSet[i] != fieldSet[i - 1])
Arun Patole7e7e68d2015-05-22 12:02:25 +0530129 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400130 error(line, "illegal - vector component fields not from the same set",
131 compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000132 return false;
133 }
134 }
135 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000136
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000137 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000138}
139
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000140///////////////////////////////////////////////////////////////////////
141//
142// Errors
143//
144////////////////////////////////////////////////////////////////////////
145
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000146
147//
148// Used by flex/bison to output all syntax and parsing errors.
149//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530150void TParseContext::error(const TSourceLoc &loc,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400151 const char *reason,
152 const char *token,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530153 const char *extraInfo)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000154{
Olli Etuaho1cc598f2016-08-18 13:50:30 +0300155 mDiagnostics.error(loc, reason, token, extraInfo);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000156}
157
Arun Patole7e7e68d2015-05-22 12:02:25 +0530158void TParseContext::warning(const TSourceLoc &loc,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400159 const char *reason,
160 const char *token,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530161 const char *extraInfo)
162{
Olli Etuaho1cc598f2016-08-18 13:50:30 +0300163 mDiagnostics.warning(loc, reason, token, extraInfo);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000164}
165
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200166void TParseContext::outOfRangeError(bool isError,
167 const TSourceLoc &loc,
168 const char *reason,
169 const char *token,
170 const char *extraInfo)
171{
172 if (isError)
173 {
174 error(loc, reason, token, extraInfo);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200175 }
176 else
177 {
178 warning(loc, reason, token, extraInfo);
179 }
180}
181
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000182//
183// Same error message for all places assignments don't work.
184//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530185void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000186{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000187 std::stringstream extraInfoStream;
188 extraInfoStream << "cannot convert from '" << right << "' to '" << left << "'";
189 std::string extraInfo = extraInfoStream.str();
190 error(line, "", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000191}
192
193//
194// Same error message for all places unary operations don't work.
195//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530196void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000197{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000198 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400199 extraInfoStream << "no operation '" << op << "' exists that takes an operand of type "
200 << operand << " (or there is no acceptable conversion)";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000201 std::string extraInfo = extraInfoStream.str();
202 error(line, " wrong operand type", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000203}
204
205//
206// Same error message for all binary operations don't work.
207//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400208void TParseContext::binaryOpError(const TSourceLoc &line,
209 const char *op,
210 TString left,
211 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000212{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000213 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400214 extraInfoStream << "no operation '" << op << "' exists that takes a left-hand operand of type '"
215 << left << "' and a right operand of type '" << right
216 << "' (or there is no acceptable conversion)";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000217 std::string extraInfo = extraInfoStream.str();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530218 error(line, " wrong operand types ", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000219}
220
Olli Etuaho856c4972016-08-08 11:38:39 +0300221void TParseContext::checkPrecisionSpecified(const TSourceLoc &line,
222 TPrecision precision,
223 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530224{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400225 if (!mChecksPrecisionErrors)
Olli Etuaho383b7912016-08-05 11:22:59 +0300226 return;
Martin Radev70866b82016-07-22 15:27:42 +0300227
228 if (precision != EbpUndefined && !SupportsPrecision(type))
229 {
230 error(line, "illegal type for precision qualifier", getBasicString(type));
231 }
232
Olli Etuaho183d7e22015-11-20 15:59:09 +0200233 if (precision == EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530234 {
Olli Etuaho183d7e22015-11-20 15:59:09 +0200235 switch (type)
236 {
237 case EbtFloat:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400238 error(line, "No precision specified for (float)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300239 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200240 case EbtInt:
241 case EbtUInt:
242 UNREACHABLE(); // there's always a predeclared qualifier
Jamie Madillb98c3a82015-07-23 14:26:04 -0400243 error(line, "No precision specified (int)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300244 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200245 default:
246 if (IsSampler(type))
247 {
248 error(line, "No precision specified (sampler)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300249 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200250 }
251 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000252 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000253}
254
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000255// Both test and if necessary, spit out an error, to see if the node is really
256// an l-value that can be operated on this way.
Olli Etuaho856c4972016-08-08 11:38:39 +0300257bool TParseContext::checkCanBeLValue(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000258{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400259 TIntermSymbol *symNode = node->getAsSymbolNode();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530260 TIntermBinary *binaryNode = node->getAsBinaryNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000261
Arun Patole7e7e68d2015-05-22 12:02:25 +0530262 if (binaryNode)
263 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400264 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530265 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400266 case EOpIndexDirect:
267 case EOpIndexIndirect:
268 case EOpIndexDirectStruct:
269 case EOpIndexDirectInterfaceBlock:
Olli Etuaho856c4972016-08-08 11:38:39 +0300270 return checkCanBeLValue(line, op, binaryNode->getLeft());
Jamie Madillb98c3a82015-07-23 14:26:04 -0400271 case EOpVectorSwizzle:
Olli Etuaho8a176262016-08-16 14:23:01 +0300272 {
273 bool ok = checkCanBeLValue(line, op, binaryNode->getLeft());
274 if (ok)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530275 {
Olli Etuaho8a176262016-08-16 14:23:01 +0300276 int offsetCount[4] = {0, 0, 0, 0};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000277
Olli Etuaho8a176262016-08-16 14:23:01 +0300278 TIntermAggregate *swizzleOffsets = binaryNode->getRight()->getAsAggregate();
Jamie Madillb98c3a82015-07-23 14:26:04 -0400279
Olli Etuaho8a176262016-08-16 14:23:01 +0300280 for (const auto &offset : *swizzleOffsets->getSequence())
Jamie Madillb98c3a82015-07-23 14:26:04 -0400281 {
Olli Etuaho8a176262016-08-16 14:23:01 +0300282 int value = offset->getAsTyped()->getAsConstantUnion()->getIConst(0);
283 offsetCount[value]++;
284 if (offsetCount[value] > 1)
Jamie Madillb98c3a82015-07-23 14:26:04 -0400285 {
286 error(line, " l-value of swizzle cannot have duplicate components", op);
Olli Etuaho8a176262016-08-16 14:23:01 +0300287 return false;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400288 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000289 }
290 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000291
Olli Etuaho8a176262016-08-16 14:23:01 +0300292 return ok;
293 }
Jamie Madillb98c3a82015-07-23 14:26:04 -0400294 default:
295 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000296 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000297 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000298
Olli Etuaho8a176262016-08-16 14:23:01 +0300299 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000300 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000301
Arun Patole7e7e68d2015-05-22 12:02:25 +0530302 const char *symbol = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000303 if (symNode != 0)
304 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000305
Arun Patole7e7e68d2015-05-22 12:02:25 +0530306 const char *message = 0;
307 switch (node->getQualifier())
308 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400309 case EvqConst:
310 message = "can't modify a const";
311 break;
312 case EvqConstReadOnly:
313 message = "can't modify a const";
314 break;
315 case EvqAttribute:
316 message = "can't modify an attribute";
317 break;
318 case EvqFragmentIn:
319 message = "can't modify an input";
320 break;
321 case EvqVertexIn:
322 message = "can't modify an input";
323 break;
324 case EvqUniform:
325 message = "can't modify a uniform";
326 break;
327 case EvqVaryingIn:
328 message = "can't modify a varying";
329 break;
330 case EvqFragCoord:
331 message = "can't modify gl_FragCoord";
332 break;
333 case EvqFrontFacing:
334 message = "can't modify gl_FrontFacing";
335 break;
336 case EvqPointCoord:
337 message = "can't modify gl_PointCoord";
338 break;
Martin Radevb0883602016-08-04 17:48:58 +0300339 case EvqNumWorkGroups:
340 message = "can't modify gl_NumWorkGroups";
341 break;
342 case EvqWorkGroupSize:
343 message = "can't modify gl_WorkGroupSize";
344 break;
345 case EvqWorkGroupID:
346 message = "can't modify gl_WorkGroupID";
347 break;
348 case EvqLocalInvocationID:
349 message = "can't modify gl_LocalInvocationID";
350 break;
351 case EvqGlobalInvocationID:
352 message = "can't modify gl_GlobalInvocationID";
353 break;
354 case EvqLocalInvocationIndex:
355 message = "can't modify gl_LocalInvocationIndex";
356 break;
Martin Radev802abe02016-08-04 17:48:32 +0300357 case EvqComputeIn:
358 message = "can't modify work group size variable";
359 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400360 default:
361 //
362 // Type that can't be written to?
363 //
364 if (node->getBasicType() == EbtVoid)
365 {
366 message = "can't modify void";
367 }
368 if (IsSampler(node->getBasicType()))
369 {
370 message = "can't modify a sampler";
371 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000372 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000373
Arun Patole7e7e68d2015-05-22 12:02:25 +0530374 if (message == 0 && binaryNode == 0 && symNode == 0)
375 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000376 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000377
Olli Etuaho8a176262016-08-16 14:23:01 +0300378 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000379 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000380
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000381 //
382 // Everything else is okay, no error.
383 //
384 if (message == 0)
Olli Etuaho8a176262016-08-16 14:23:01 +0300385 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000386
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000387 //
388 // If we get here, we have an error and a message.
389 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530390 if (symNode)
391 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000392 std::stringstream extraInfoStream;
393 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
394 std::string extraInfo = extraInfoStream.str();
395 error(line, " l-value required", op, extraInfo.c_str());
396 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530397 else
398 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000399 std::stringstream extraInfoStream;
400 extraInfoStream << "(" << message << ")";
401 std::string extraInfo = extraInfoStream.str();
402 error(line, " l-value required", op, extraInfo.c_str());
403 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000404
Olli Etuaho8a176262016-08-16 14:23:01 +0300405 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000406}
407
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000408// Both test, and if necessary spit out an error, to see if the node is really
409// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300410void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000411{
Olli Etuaho383b7912016-08-05 11:22:59 +0300412 if (node->getQualifier() != EvqConst)
413 {
414 error(node->getLine(), "constant expression required", "");
415 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000416}
417
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000418// Both test, and if necessary spit out an error, to see if the node is really
419// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300420void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000421{
Olli Etuaho383b7912016-08-05 11:22:59 +0300422 if (!node->isScalarInt())
423 {
424 error(node->getLine(), "integer expression required", token);
425 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000426}
427
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000428// Both test, and if necessary spit out an error, to see if we are currently
429// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800430bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000431{
Olli Etuaho856c4972016-08-08 11:38:39 +0300432 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300433 {
434 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800435 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300436 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800437 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000438}
439
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000440// For now, keep it simple: if it starts "gl_", it's reserved, independent
441// of scope. Except, if the symbol table is at the built-in push-level,
442// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000443// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
444// webgl shader.
Olli Etuaho856c4972016-08-08 11:38:39 +0300445bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000446{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530447 static const char *reservedErrMsg = "reserved built-in name";
448 if (!symbolTable.atBuiltInLevel())
449 {
450 if (identifier.compare(0, 3, "gl_") == 0)
451 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000452 error(line, reservedErrMsg, "gl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300453 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000454 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530455 if (IsWebGLBasedSpec(mShaderSpec))
456 {
457 if (identifier.compare(0, 6, "webgl_") == 0)
458 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000459 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300460 return false;
alokp@chromium.org613ef312010-07-21 18:54:22 +0000461 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530462 if (identifier.compare(0, 7, "_webgl_") == 0)
463 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000464 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300465 return false;
alokp@chromium.org613ef312010-07-21 18:54:22 +0000466 }
467 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530468 if (identifier.find("__") != TString::npos)
469 {
470 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400471 "identifiers containing two consecutive underscores (__) are reserved as "
472 "possible future keywords",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530473 identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300474 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000475 }
476 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000477
Olli Etuaho8a176262016-08-16 14:23:01 +0300478 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000479}
480
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000481// Make sure there is enough data provided to the constructor to build
482// something of the type of the constructor. Also returns the type of
483// the constructor.
Olli Etuaho856c4972016-08-08 11:38:39 +0300484bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
485 TIntermNode *argumentsNode,
486 const TFunction &function,
487 TOperator op,
488 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000489{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000490 bool constructingMatrix = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400491 switch (op)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530492 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400493 case EOpConstructMat2:
494 case EOpConstructMat2x3:
495 case EOpConstructMat2x4:
496 case EOpConstructMat3x2:
497 case EOpConstructMat3:
498 case EOpConstructMat3x4:
499 case EOpConstructMat4x2:
500 case EOpConstructMat4x3:
501 case EOpConstructMat4:
502 constructingMatrix = true;
503 break;
504 default:
505 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000506 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000507
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000508 //
509 // Note: It's okay to have too many components available, but not okay to have unused
510 // arguments. 'full' will go to true when enough args have been seen. If we loop
511 // again, there is an extra argument, so 'overfull' will become true.
512 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000513
Jamie Madillb98c3a82015-07-23 14:26:04 -0400514 size_t size = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400515 bool full = false;
516 bool overFull = false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000517 bool matrixInMatrix = false;
518 bool arrayArg = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530519 for (size_t i = 0; i < function.getParamCount(); ++i)
520 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700521 const TConstParameter &param = function.getParam(i);
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000522 size += param.type->getObjectSize();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530523
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000524 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000525 matrixInMatrix = true;
526 if (full)
527 overFull = true;
Olli Etuaho856c4972016-08-08 11:38:39 +0300528 if (op != EOpConstructStruct && !type.isArray() && size >= type.getObjectSize())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000529 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000530 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000531 arrayArg = true;
532 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530533
Olli Etuaho856c4972016-08-08 11:38:39 +0300534 if (type.isArray())
Olli Etuaho376f1b52015-04-13 13:23:41 +0300535 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300536 // The size of an unsized constructor should already have been determined.
537 ASSERT(!type.isUnsizedArray());
538 if (static_cast<size_t>(type.getArraySize()) != function.getParamCount())
Olli Etuaho376f1b52015-04-13 13:23:41 +0300539 {
540 error(line, "array constructor needs one argument per array element", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300541 return false;
Olli Etuaho376f1b52015-04-13 13:23:41 +0300542 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000543 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000544
Arun Patole7e7e68d2015-05-22 12:02:25 +0530545 if (arrayArg && op != EOpConstructStruct)
546 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000547 error(line, "constructing from a non-dereferenced array", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300548 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000549 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000550
Olli Etuaho856c4972016-08-08 11:38:39 +0300551 if (matrixInMatrix && !type.isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530552 {
553 if (function.getParamCount() != 1)
554 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400555 error(line, "constructing matrix from matrix can only take one argument",
556 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300557 return false;
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000558 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000559 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000560
Arun Patole7e7e68d2015-05-22 12:02:25 +0530561 if (overFull)
562 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000563 error(line, "too many arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300564 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000565 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530566
Olli Etuaho856c4972016-08-08 11:38:39 +0300567 if (op == EOpConstructStruct && !type.isArray() &&
568 type.getStruct()->fields().size() != function.getParamCount())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530569 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400570 error(line,
571 "Number of constructor parameters does not match the number of structure fields",
572 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300573 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000574 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000575
Olli Etuaho856c4972016-08-08 11:38:39 +0300576 if (!type.isMatrix() || !matrixInMatrix)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530577 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300578 if ((op != EOpConstructStruct && size != 1 && size < type.getObjectSize()) ||
579 (op == EOpConstructStruct && size < type.getObjectSize()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530580 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000581 error(line, "not enough data provided for construction", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300582 return false;
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000583 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000584 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000585
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200586 if (argumentsNode == nullptr)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530587 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200588 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300589 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000590 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200591
592 TIntermAggregate *argumentsAgg = argumentsNode->getAsAggregate();
593 for (TIntermNode *&argNode : *argumentsAgg->getSequence())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530594 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200595 TIntermTyped *argTyped = argNode->getAsTyped();
596 ASSERT(argTyped != nullptr);
597 if (op != EOpConstructStruct && IsSampler(argTyped->getBasicType()))
598 {
599 error(line, "cannot convert a sampler", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300600 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200601 }
602 if (argTyped->getBasicType() == EbtVoid)
603 {
604 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300605 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200606 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000607 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000608
Olli Etuaho856c4972016-08-08 11:38:39 +0300609 if (type.isArray())
610 {
611 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
612 // the array.
613 for (TIntermNode *&argNode : *argumentsAgg->getSequence())
614 {
615 const TType &argType = argNode->getAsTyped()->getType();
616 // It has already been checked that the argument is not an array.
617 ASSERT(!argType.isArray());
618 if (!argType.sameElementType(type))
619 {
620 error(line, "Array constructor argument has an incorrect type", "Error");
Olli Etuaho8a176262016-08-16 14:23:01 +0300621 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300622 }
623 }
624 }
625 else if (op == EOpConstructStruct)
626 {
627 const TFieldList &fields = type.getStruct()->fields();
628 TIntermSequence *args = argumentsAgg->getSequence();
629
630 for (size_t i = 0; i < fields.size(); i++)
631 {
632 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
633 {
634 error(line, "Structure constructor arguments do not match structure fields",
635 "Error");
Olli Etuaho8a176262016-08-16 14:23:01 +0300636 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300637 }
638 }
639 }
640
Olli Etuaho8a176262016-08-16 14:23:01 +0300641 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000642}
643
Jamie Madillb98c3a82015-07-23 14:26:04 -0400644// This function checks to see if a void variable has been declared and raise an error message for
645// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000646//
647// returns true in case of an error
648//
Olli Etuaho856c4972016-08-08 11:38:39 +0300649bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400650 const TString &identifier,
651 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000652{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300653 if (type == EbtVoid)
654 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000655 error(line, "illegal use of type 'void'", identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300656 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300657 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000658
Olli Etuaho8a176262016-08-16 14:23:01 +0300659 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000660}
661
Jamie Madillb98c3a82015-07-23 14:26:04 -0400662// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300663// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300664void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000665{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530666 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector())
667 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000668 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530669 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000670}
671
Jamie Madillb98c3a82015-07-23 14:26:04 -0400672// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300673// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300674void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000675{
Martin Radev4a9cd802016-09-01 16:51:51 +0300676 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530677 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000678 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530679 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000680}
681
Olli Etuaho856c4972016-08-08 11:38:39 +0300682bool TParseContext::checkIsNotSampler(const TSourceLoc &line,
Martin Radev4a9cd802016-09-01 16:51:51 +0300683 const TTypeSpecifierNonArray &pType,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400684 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000685{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530686 if (pType.type == EbtStruct)
687 {
688 if (containsSampler(*pType.userDef))
689 {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000690 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
Olli Etuaho8a176262016-08-16 14:23:01 +0300691 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000692 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530693
Olli Etuaho8a176262016-08-16 14:23:01 +0300694 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530695 }
696 else if (IsSampler(pType.type))
697 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000698 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300699 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000700 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000701
Olli Etuaho8a176262016-08-16 14:23:01 +0300702 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000703}
704
Olli Etuaho856c4972016-08-08 11:38:39 +0300705void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
706 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400707{
708 if (pType.layoutQualifier.location != -1)
709 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400710 error(line, "location must only be specified for a single input or output variable",
711 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400712 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400713}
714
Olli Etuaho856c4972016-08-08 11:38:39 +0300715void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
716 const TLayoutQualifier &layoutQualifier)
717{
718 if (layoutQualifier.location != -1)
719 {
720 error(location, "invalid layout qualifier:", "location",
721 "only valid on program inputs and outputs");
722 }
723}
724
725void TParseContext::checkOutParameterIsNotSampler(const TSourceLoc &line,
726 TQualifier qualifier,
727 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000728{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400729 if ((qualifier == EvqOut || qualifier == EvqInOut) && type.getBasicType() != EbtStruct &&
730 IsSampler(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530731 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000732 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000733 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000734}
735
Arun Patole7e7e68d2015-05-22 12:02:25 +0530736bool TParseContext::containsSampler(const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000737{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000738 if (IsSampler(type.getBasicType()))
739 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000740
Arun Patole7e7e68d2015-05-22 12:02:25 +0530741 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock())
742 {
743 const TFieldList &fields = type.getStruct()->fields();
744 for (unsigned int i = 0; i < fields.size(); ++i)
745 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400746 if (containsSampler(*fields[i]->type()))
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000747 return true;
748 }
749 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000750
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000751 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000752}
753
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000754// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300755unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000756{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530757 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000758
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200759 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
760 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
761 // fold as array size.
762 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000763 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000764 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300765 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000766 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000767
Olli Etuaho856c4972016-08-08 11:38:39 +0300768 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400769
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000770 if (constant->getBasicType() == EbtUInt)
771 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300772 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000773 }
774 else
775 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300776 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000777
Olli Etuaho856c4972016-08-08 11:38:39 +0300778 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000779 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400780 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300781 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000782 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400783
Olli Etuaho856c4972016-08-08 11:38:39 +0300784 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400785 }
786
Olli Etuaho856c4972016-08-08 11:38:39 +0300787 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400788 {
789 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300790 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400791 }
792
793 // The size of arrays is restricted here to prevent issues further down the
794 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
795 // 4096 registers so this should be reasonable even for aggressively optimizable code.
796 const unsigned int sizeLimit = 65536;
797
Olli Etuaho856c4972016-08-08 11:38:39 +0300798 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -0400799 {
800 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300801 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000802 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300803
804 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000805}
806
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000807// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +0300808bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
809 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000810{
Olli Etuaho8a176262016-08-16 14:23:01 +0300811 if ((elementQualifier.qualifier == EvqAttribute) ||
812 (elementQualifier.qualifier == EvqVertexIn) ||
813 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300814 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400815 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300816 TType(elementQualifier).getQualifierString());
817 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000818 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000819
Olli Etuaho8a176262016-08-16 14:23:01 +0300820 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000821}
822
Olli Etuaho8a176262016-08-16 14:23:01 +0300823// See if this element type can be formed into an array.
824bool TParseContext::checkIsValidTypeForArray(const TSourceLoc &line, const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000825{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000826 //
827 // Can the type be an array?
828 //
Olli Etuaho8a176262016-08-16 14:23:01 +0300829 if (elementType.array)
Jamie Madill06145232015-05-13 13:10:01 -0400830 {
Olli Etuaho8a176262016-08-16 14:23:01 +0300831 error(line, "cannot declare arrays of arrays",
832 TType(elementType).getCompleteString().c_str());
833 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000834 }
Olli Etuahocc36b982015-07-10 14:14:18 +0300835 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
836 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
837 // 4.3.4).
Martin Radev4a9cd802016-09-01 16:51:51 +0300838 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Olli Etuaho8a176262016-08-16 14:23:01 +0300839 sh::IsVarying(elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +0300840 {
841 error(line, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300842 TType(elementType).getCompleteString().c_str());
843 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +0300844 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000845
Olli Etuaho8a176262016-08-16 14:23:01 +0300846 return true;
847}
848
849// Check if this qualified element type can be formed into an array.
850bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
851 const TPublicType &elementType)
852{
853 if (checkIsValidTypeForArray(indexLocation, elementType))
854 {
855 return checkIsValidQualifierForArray(indexLocation, elementType);
856 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000857 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000858}
859
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000860// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +0300861void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
862 const TString &identifier,
863 TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000864{
Olli Etuaho3739d232015-04-08 12:23:44 +0300865 ASSERT(type != nullptr);
866 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000867 {
868 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300869 type->qualifier = EvqTemporary;
870
871 // Generate informative error messages for ESSL1.
872 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400873 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000874 {
Arun Patole7e7e68d2015-05-22 12:02:25 +0530875 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400876 "structures containing arrays may not be declared constant since they cannot be "
877 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530878 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000879 }
880 else
881 {
882 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
883 }
Olli Etuaho383b7912016-08-05 11:22:59 +0300884 return;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000885 }
Olli Etuaho376f1b52015-04-13 13:23:41 +0300886 if (type->isUnsizedArray())
887 {
888 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
Olli Etuaho376f1b52015-04-13 13:23:41 +0300889 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000890}
891
Olli Etuaho2935c582015-04-08 14:32:06 +0300892// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000893// and update the symbol table.
894//
Olli Etuaho2935c582015-04-08 14:32:06 +0300895// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000896//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400897bool TParseContext::declareVariable(const TSourceLoc &line,
898 const TString &identifier,
899 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +0300900 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000901{
Olli Etuaho2935c582015-04-08 14:32:06 +0300902 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000903
Olli Etuaho856c4972016-08-08 11:38:39 +0300904 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000905
Olli Etuaho2935c582015-04-08 14:32:06 +0300906 // gl_LastFragData may be redeclared with a new precision qualifier
907 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
908 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400909 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
910 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho856c4972016-08-08 11:38:39 +0300911 if (static_cast<int>(type.getArraySize()) == maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +0300912 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400913 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +0300914 {
Olli Etuaho8a176262016-08-16 14:23:01 +0300915 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->getExtension());
Olli Etuaho2935c582015-04-08 14:32:06 +0300916 }
917 }
918 else
919 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400920 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
921 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +0300922 return false;
923 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000924 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000925
Olli Etuaho8a176262016-08-16 14:23:01 +0300926 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +0300927 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000928
Olli Etuaho2935c582015-04-08 14:32:06 +0300929 (*variable) = new TVariable(&identifier, type);
930 if (!symbolTable.declare(*variable))
931 {
932 error(line, "redefinition", identifier.c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -0400933 *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +0300934 return false;
935 }
936
Olli Etuaho8a176262016-08-16 14:23:01 +0300937 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +0300938 return false;
939
940 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000941}
942
Martin Radev70866b82016-07-22 15:27:42 +0300943void TParseContext::checkIsParameterQualifierValid(
944 const TSourceLoc &line,
945 const TTypeQualifierBuilder &typeQualifierBuilder,
946 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530947{
Martin Radevc28888b2016-07-22 15:27:42 +0300948 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(
949 &mDiagnostics, AreTypeQualifierChecksRelaxed(mShaderVersion));
Martin Radev70866b82016-07-22 15:27:42 +0300950
951 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530952 {
Martin Radev70866b82016-07-22 15:27:42 +0300953 checkOutParameterIsNotSampler(line, typeQualifier.qualifier, *type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000954 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000955
Martin Radev70866b82016-07-22 15:27:42 +0300956 type->setQualifier(typeQualifier.qualifier);
957
958 if (typeQualifier.precision != EbpUndefined)
959 {
960 type->setPrecision(typeQualifier.precision);
961 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000962}
963
Olli Etuaho856c4972016-08-08 11:38:39 +0300964bool TParseContext::checkCanUseExtension(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000965{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400966 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000967 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +0530968 if (iter == extBehavior.end())
969 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000970 error(line, "extension", extension.c_str(), "is not supported");
Olli Etuaho8a176262016-08-16 14:23:01 +0300971 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000972 }
zmo@google.comf5450912011-09-09 01:37:19 +0000973 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +0530974 if (iter->second == EBhDisable || iter->second == EBhUndefined)
975 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000976 error(line, "extension", extension.c_str(), "is disabled");
Olli Etuaho8a176262016-08-16 14:23:01 +0300977 return false;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000978 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530979 if (iter->second == EBhWarn)
980 {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000981 warning(line, "extension", extension.c_str(), "is being used");
Olli Etuaho8a176262016-08-16 14:23:01 +0300982 return true;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000983 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000984
Olli Etuaho8a176262016-08-16 14:23:01 +0300985 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000986}
987
Jamie Madillb98c3a82015-07-23 14:26:04 -0400988// These checks are common for all declarations starting a declarator list, and declarators that
989// follow an empty declaration.
Olli Etuaho383b7912016-08-05 11:22:59 +0300990void TParseContext::singleDeclarationErrorCheck(const TPublicType &publicType,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400991 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -0400992{
Olli Etuahofa33d582015-04-09 14:33:12 +0300993 switch (publicType.qualifier)
994 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400995 case EvqVaryingIn:
996 case EvqVaryingOut:
997 case EvqAttribute:
998 case EvqVertexIn:
999 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001000 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001001 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001002 {
1003 error(identifierLocation, "cannot be used with a structure",
1004 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001005 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001006 }
Olli Etuahofa33d582015-04-09 14:33:12 +03001007
Jamie Madillb98c3a82015-07-23 14:26:04 -04001008 default:
1009 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001010 }
1011
Jamie Madillb98c3a82015-07-23 14:26:04 -04001012 if (publicType.qualifier != EvqUniform &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001013 !checkIsNotSampler(identifierLocation, publicType.typeSpecifierNonArray,
1014 "samplers must be uniform"))
Olli Etuahofa33d582015-04-09 14:33:12 +03001015 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001016 return;
Olli Etuahofa33d582015-04-09 14:33:12 +03001017 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001018
1019 // check for layout qualifier issues
1020 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
1021
1022 if (layoutQualifier.matrixPacking != EmpUnspecified)
1023 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001024 error(identifierLocation, "layout qualifier",
1025 getMatrixPackingString(layoutQualifier.matrixPacking),
Olli Etuahofa33d582015-04-09 14:33:12 +03001026 "only valid for interface blocks");
Olli Etuaho383b7912016-08-05 11:22:59 +03001027 return;
Jamie Madilla5efff92013-06-06 11:56:47 -04001028 }
1029
1030 if (layoutQualifier.blockStorage != EbsUnspecified)
1031 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001032 error(identifierLocation, "layout qualifier",
1033 getBlockStorageString(layoutQualifier.blockStorage),
Olli Etuahofa33d582015-04-09 14:33:12 +03001034 "only valid for interface blocks");
Olli Etuaho383b7912016-08-05 11:22:59 +03001035 return;
Jamie Madilla5efff92013-06-06 11:56:47 -04001036 }
1037
Olli Etuaho383b7912016-08-05 11:22:59 +03001038 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut)
Jamie Madilla5efff92013-06-06 11:56:47 -04001039 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001040 checkLocationIsNotSpecified(identifierLocation, publicType.layoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04001041 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001042}
1043
Olli Etuaho856c4972016-08-08 11:38:39 +03001044void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1045 const TString &layoutQualifierName,
1046 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001047{
1048
1049 if (mShaderVersion < versionRequired)
1050 {
1051 error(location, "invalid layout qualifier:", layoutQualifierName.c_str(), "not supported");
Martin Radev802abe02016-08-04 17:48:32 +03001052 }
1053}
1054
Olli Etuaho856c4972016-08-08 11:38:39 +03001055bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1056 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001057{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001058 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001059 for (size_t i = 0u; i < localSize.size(); ++i)
1060 {
1061 if (localSize[i] != -1)
1062 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03001063 error(location, "invalid layout qualifier:", getWorkGroupSizeString(i),
Martin Radev802abe02016-08-04 17:48:32 +03001064 "only valid when used with 'in' in a compute shader global layout declaration");
Olli Etuaho8a176262016-08-16 14:23:01 +03001065 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001066 }
1067 }
1068
Olli Etuaho8a176262016-08-16 14:23:01 +03001069 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001070}
1071
Olli Etuaho383b7912016-08-05 11:22:59 +03001072void TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate,
Olli Etuaho856c4972016-08-08 11:38:39 +03001073 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001074{
1075 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1076 {
1077 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1078 if (qual == EvqOut || qual == EvqInOut)
1079 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001080 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
Olli Etuaho8a176262016-08-16 14:23:01 +03001081 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001082 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001083 error(argument->getLine(),
Jamie Madillb98c3a82015-07-23 14:26:04 -04001084 "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error");
Olli Etuaho383b7912016-08-05 11:22:59 +03001085 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001086 }
1087 }
1088 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001089}
1090
Martin Radev70866b82016-07-22 15:27:42 +03001091void TParseContext::checkInvariantVariableQualifier(bool invariant,
1092 const TQualifier qualifier,
1093 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001094{
Martin Radev70866b82016-07-22 15:27:42 +03001095 if (!invariant)
1096 return;
1097
1098 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001099 {
Martin Radev70866b82016-07-22 15:27:42 +03001100 // input variables in the fragment shader can be also qualified as invariant
1101 if (!sh::CanBeInvariantESSL1(qualifier))
1102 {
1103 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1104 }
1105 }
1106 else
1107 {
1108 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1109 {
1110 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1111 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001112 }
1113}
1114
Arun Patole7e7e68d2015-05-22 12:02:25 +05301115bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001116{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001117 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001118 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1119 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001120}
1121
Arun Patole7e7e68d2015-05-22 12:02:25 +05301122bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001123{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001124 return ::IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001125}
1126
Jamie Madillb98c3a82015-07-23 14:26:04 -04001127void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1128 const char *extName,
1129 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001130{
1131 pp::SourceLocation srcLoc;
1132 srcLoc.file = loc.first_file;
1133 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001134 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001135}
1136
Jamie Madillb98c3a82015-07-23 14:26:04 -04001137void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1138 const char *name,
1139 const char *value,
1140 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001141{
1142 pp::SourceLocation srcLoc;
1143 srcLoc.file = loc.first_file;
1144 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001145 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001146}
1147
Martin Radev4c4c8e72016-08-04 12:25:34 +03001148sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001149{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001150 sh::WorkGroupSize result;
Martin Radev802abe02016-08-04 17:48:32 +03001151 for (size_t i = 0u; i < result.size(); ++i)
1152 {
1153 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1154 {
1155 result[i] = 1;
1156 }
1157 else
1158 {
1159 result[i] = mComputeShaderLocalSize[i];
1160 }
1161 }
1162 return result;
1163}
1164
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001165/////////////////////////////////////////////////////////////////////////////////
1166//
1167// Non-Errors.
1168//
1169/////////////////////////////////////////////////////////////////////////////////
1170
Jamie Madill5c097022014-08-20 16:38:32 -04001171const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1172 const TString *name,
1173 const TSymbol *symbol)
1174{
1175 const TVariable *variable = NULL;
1176
1177 if (!symbol)
1178 {
1179 error(location, "undeclared identifier", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001180 }
1181 else if (!symbol->isVariable())
1182 {
1183 error(location, "variable expected", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001184 }
1185 else
1186 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001187 variable = static_cast<const TVariable *>(symbol);
Jamie Madill5c097022014-08-20 16:38:32 -04001188
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001189 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
Olli Etuaho383b7912016-08-05 11:22:59 +03001190 !variable->getExtension().empty())
Jamie Madill5c097022014-08-20 16:38:32 -04001191 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001192 checkCanUseExtension(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001193 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001194
1195 // Reject shaders using both gl_FragData and gl_FragColor
1196 TQualifier qualifier = variable->getType().getQualifier();
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001197 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001198 {
1199 mUsesFragData = true;
1200 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001201 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001202 {
1203 mUsesFragColor = true;
1204 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001205 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1206 {
1207 mUsesSecondaryOutputs = true;
1208 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001209
1210 // This validation is not quite correct - it's only an error to write to
1211 // both FragData and FragColor. For simplicity, and because users shouldn't
1212 // be rewarded for reading from undefined varaibles, return an error
1213 // if they are both referenced, rather than assigned.
1214 if (mUsesFragData && mUsesFragColor)
1215 {
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001216 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1217 if (mUsesSecondaryOutputs)
1218 {
1219 errorMessage =
1220 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1221 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1222 }
1223 error(location, errorMessage, name->c_str());
Jamie Madill14e95b32015-05-07 10:10:41 -04001224 }
Martin Radevb0883602016-08-04 17:48:58 +03001225
1226 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1227 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1228 qualifier == EvqWorkGroupSize)
1229 {
1230 error(location,
1231 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1232 "gl_WorkGroupSize");
1233 }
Jamie Madill5c097022014-08-20 16:38:32 -04001234 }
1235
1236 if (!variable)
1237 {
1238 TType type(EbtFloat, EbpUndefined);
1239 TVariable *fakeVariable = new TVariable(name, type);
1240 symbolTable.declare(fakeVariable);
1241 variable = fakeVariable;
1242 }
1243
1244 return variable;
1245}
1246
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001247TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1248 const TString *name,
1249 const TSymbol *symbol)
1250{
1251 const TVariable *variable = getNamedVariable(location, name, symbol);
1252
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001253 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001254 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001255 const TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001256 return intermediate.addConstantUnion(constArray, variable->getType(), location);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001257 }
1258 else
1259 {
1260 return intermediate.addSymbol(variable->getUniqueId(), variable->getName(),
1261 variable->getType(), location);
1262 }
1263}
1264
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001265//
1266// Look up a function name in the symbol table, and make sure it is a function.
1267//
1268// Return the function symbol if found, otherwise 0.
1269//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001270const TFunction *TParseContext::findFunction(const TSourceLoc &line,
1271 TFunction *call,
1272 int inputShaderVersion,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301273 bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001274{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001275 // First find by unmangled name to check whether the function name has been
1276 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001277 // If a function is found, check for one with a matching argument list.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301278 const TSymbol *symbol = symbolTable.find(call->getName(), inputShaderVersion, builtIn);
1279 if (symbol == 0 || symbol->isFunction())
1280 {
Austin Kinross3ae64652015-01-26 15:51:39 -08001281 symbol = symbolTable.find(call->getMangledName(), inputShaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001282 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001283
Arun Patole7e7e68d2015-05-22 12:02:25 +05301284 if (symbol == 0)
1285 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001286 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001287 return 0;
1288 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001289
Arun Patole7e7e68d2015-05-22 12:02:25 +05301290 if (!symbol->isFunction())
1291 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001292 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001293 return 0;
1294 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001295
Jamie Madillb98c3a82015-07-23 14:26:04 -04001296 return static_cast<const TFunction *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001297}
1298
1299//
1300// Initializers show up in several places in the grammar. Have one set of
1301// code to handle them here.
1302//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001303// Returns true on error, false if no error
1304//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001305bool TParseContext::executeInitializer(const TSourceLoc &line,
1306 const TString &identifier,
1307 const TPublicType &pType,
1308 TIntermTyped *initializer,
1309 TIntermNode **intermNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001310{
Olli Etuahoe7847b02015-03-16 11:56:12 +02001311 ASSERT(intermNode != nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001312 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001313
Olli Etuaho2935c582015-04-08 14:32:06 +03001314 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001315 if (type.isUnsizedArray())
1316 {
1317 type.setArraySize(initializer->getArraySize());
1318 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001319 if (!declareVariable(line, identifier, type, &variable))
1320 {
1321 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001322 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001323
Olli Etuahob0c645e2015-05-12 14:25:36 +03001324 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001325 if (symbolTable.atGlobalLevel() &&
1326 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001327 {
1328 // Error message does not completely match behavior with ESSL 1.00, but
1329 // we want to steer developers towards only using constant expressions.
1330 error(line, "global variable initializers must be constant expressions", "=");
1331 return true;
1332 }
1333 if (globalInitWarning)
1334 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001335 warning(
1336 line,
1337 "global variable initializers should be constant expressions "
1338 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1339 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001340 }
1341
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001342 //
1343 // identifier must be of type constant, a global, or a temporary
1344 //
1345 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301346 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1347 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001348 error(line, " cannot initialize this type of qualifier ",
1349 variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001350 return true;
1351 }
1352 //
1353 // test for and propagate constant
1354 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001355
Arun Patole7e7e68d2015-05-22 12:02:25 +05301356 if (qualifier == EvqConst)
1357 {
1358 if (qualifier != initializer->getType().getQualifier())
1359 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001360 std::stringstream extraInfoStream;
1361 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1362 std::string extraInfo = extraInfoStream.str();
1363 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001364 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001365 return true;
1366 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301367 if (type != initializer->getType())
1368 {
1369 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001370 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001371 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001372 return true;
1373 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001374
1375 // Save the constant folded value to the variable if possible. For example array
1376 // initializers are not folded, since that way copying the array literal to multiple places
1377 // in the shader is avoided.
1378 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1379 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301380 if (initializer->getAsConstantUnion())
1381 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001382 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001383 *intermNode = nullptr;
1384 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301385 }
1386 else if (initializer->getAsSymbolNode())
1387 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001388 const TSymbol *symbol =
1389 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1390 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001391
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001392 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001393 if (constArray)
1394 {
1395 variable->shareConstPointer(constArray);
1396 *intermNode = nullptr;
1397 return false;
1398 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001399 }
1400 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001401
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001402 TIntermSymbol *intermSymbol = intermediate.addSymbol(
1403 variable->getUniqueId(), variable->getName(), variable->getType(), line);
1404 *intermNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1405 if (*intermNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001406 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001407 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1408 return true;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001409 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001410
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001411 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001412}
1413
Martin Radev70866b82016-07-22 15:27:42 +03001414TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301415 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001416{
Martin Radevc28888b2016-07-22 15:27:42 +03001417 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(
1418 &mDiagnostics, AreTypeQualifierChecksRelaxed(mShaderVersion));
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001419
Martin Radev70866b82016-07-22 15:27:42 +03001420 TPublicType returnType = typeSpecifier;
1421 returnType.qualifier = typeQualifier.qualifier;
1422 returnType.invariant = typeQualifier.invariant;
1423 returnType.layoutQualifier = typeQualifier.layoutQualifier;
1424 returnType.precision = typeSpecifier.precision;
1425
1426 if (typeQualifier.precision != EbpUndefined)
1427 {
1428 returnType.precision = typeQualifier.precision;
1429 }
1430
Martin Radev4a9cd802016-09-01 16:51:51 +03001431 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
1432 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03001433
Martin Radev4a9cd802016-09-01 16:51:51 +03001434 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
1435 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03001436
Martin Radev4a9cd802016-09-01 16:51:51 +03001437 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03001438
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001439 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001440 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001441 if (typeSpecifier.array)
1442 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001443 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001444 returnType.clearArrayness();
1445 }
1446
Martin Radev70866b82016-07-22 15:27:42 +03001447 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001448 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001449 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001450 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001451 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001452 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001453
Martin Radev70866b82016-07-22 15:27:42 +03001454 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001455 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001456 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001457 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001458 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001459 }
1460 }
1461 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001462 {
Martin Radev70866b82016-07-22 15:27:42 +03001463 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03001464 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001465 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03001466 }
Martin Radev70866b82016-07-22 15:27:42 +03001467 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
1468 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001469 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001470 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
1471 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001472 }
Martin Radev70866b82016-07-22 15:27:42 +03001473 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03001474 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001475 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03001476 "in");
Martin Radev802abe02016-08-04 17:48:32 +03001477 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001478 }
1479
1480 return returnType;
1481}
1482
Olli Etuaho856c4972016-08-08 11:38:39 +03001483void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
1484 const TPublicType &type,
1485 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03001486{
1487 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03001488 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03001489 {
1490 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001491 }
1492
1493 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
1494 switch (qualifier)
1495 {
1496 case EvqVertexIn:
1497 // ESSL 3.00 section 4.3.4
1498 if (type.array)
1499 {
1500 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001501 }
1502 // Vertex inputs with a struct type are disallowed in singleDeclarationErrorCheck
1503 return;
1504 case EvqFragmentOut:
1505 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03001506 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03001507 {
1508 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001509 }
1510 // Fragment outputs with a struct type are disallowed in singleDeclarationErrorCheck
1511 return;
1512 default:
1513 break;
1514 }
1515
1516 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
1517 // restrictions.
1518 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03001519 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
1520 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03001521 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
1522 {
1523 error(qualifierLocation, "must use 'flat' interpolation here",
1524 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001525 }
1526
Martin Radev4a9cd802016-09-01 16:51:51 +03001527 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03001528 {
1529 // ESSL 3.00 sections 4.3.4 and 4.3.6.
1530 // These restrictions are only implied by the ESSL 3.00 spec, but
1531 // the ESSL 3.10 spec lists these restrictions explicitly.
1532 if (type.array)
1533 {
1534 error(qualifierLocation, "cannot be an array of structures",
1535 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001536 }
1537 if (type.isStructureContainingArrays())
1538 {
1539 error(qualifierLocation, "cannot be a structure containing an array",
1540 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001541 }
1542 if (type.isStructureContainingType(EbtStruct))
1543 {
1544 error(qualifierLocation, "cannot be a structure containing a structure",
1545 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001546 }
1547 if (type.isStructureContainingType(EbtBool))
1548 {
1549 error(qualifierLocation, "cannot be a structure containing a bool",
1550 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001551 }
1552 }
1553}
1554
Olli Etuahofa33d582015-04-09 14:33:12 +03001555TIntermAggregate *TParseContext::parseSingleDeclaration(TPublicType &publicType,
1556 const TSourceLoc &identifierOrTypeLocation,
1557 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001558{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001559 TType type(publicType);
1560 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
1561 mDirectiveHandler.pragma().stdgl.invariantAll)
1562 {
1563 TQualifier qualifier = type.getQualifier();
1564
1565 // The directive handler has already taken care of rejecting invalid uses of this pragma
1566 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
1567 // affected variable declarations:
1568 //
1569 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
1570 // elsewhere, in TranslatorGLSL.)
1571 //
1572 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
1573 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
1574 // the way this is currently implemented we have to enable this compiler option before
1575 // parsing the shader and determining the shading language version it uses. If this were
1576 // implemented as a post-pass, the workaround could be more targeted.
1577 //
1578 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
1579 // the specification, but there are desktop OpenGL drivers that expect that this is the
1580 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
1581 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
1582 {
1583 type.setInvariant(true);
1584 }
1585 }
1586
1587 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, type, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001588
Olli Etuahobab4c082015-04-24 16:38:49 +03001589 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03001590
Olli Etuahobab4c082015-04-24 16:38:49 +03001591 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1592
1593 if (emptyDeclaration)
1594 {
1595 if (publicType.isUnsizedArray())
1596 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001597 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1598 // error. It is assumed that this applies to empty declarations as well.
1599 error(identifierOrTypeLocation, "empty array declaration needs to specify a size",
1600 identifier.c_str());
Olli Etuahobab4c082015-04-24 16:38:49 +03001601 }
1602 }
1603 else
Jamie Madill60ed9812013-06-06 11:56:46 -04001604 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001605 singleDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001606
Olli Etuaho856c4972016-08-08 11:38:39 +03001607 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001608
Olli Etuaho2935c582015-04-08 14:32:06 +03001609 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001610 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04001611
1612 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001613 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001614 }
1615
Olli Etuahoe7847b02015-03-16 11:56:12 +02001616 return intermediate.makeAggregate(symbol, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001617}
1618
Olli Etuahoe7847b02015-03-16 11:56:12 +02001619TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1620 const TSourceLoc &identifierLocation,
1621 const TString &identifier,
1622 const TSourceLoc &indexLocation,
1623 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001624{
Olli Etuahofa33d582015-04-09 14:33:12 +03001625 mDeferredSingleDeclarationErrorCheck = false;
1626
Olli Etuaho383b7912016-08-05 11:22:59 +03001627 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001628
Olli Etuaho856c4972016-08-08 11:38:39 +03001629 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001630
Olli Etuaho8a176262016-08-16 14:23:01 +03001631 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001632
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001633 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001634
Olli Etuaho856c4972016-08-08 11:38:39 +03001635 unsigned int size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001636 // Make the type an array even if size check failed.
1637 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1638 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001639
Olli Etuaho2935c582015-04-08 14:32:06 +03001640 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03001641 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04001642
Olli Etuahoe7847b02015-03-16 11:56:12 +02001643 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001644 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001645 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001646
Olli Etuahoe7847b02015-03-16 11:56:12 +02001647 return intermediate.makeAggregate(symbol, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001648}
1649
Jamie Madill06145232015-05-13 13:10:01 -04001650TIntermAggregate *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001651 const TSourceLoc &identifierLocation,
1652 const TString &identifier,
1653 const TSourceLoc &initLocation,
1654 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001655{
Olli Etuahofa33d582015-04-09 14:33:12 +03001656 mDeferredSingleDeclarationErrorCheck = false;
1657
Olli Etuaho383b7912016-08-05 11:22:59 +03001658 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001659
Olli Etuahoe7847b02015-03-16 11:56:12 +02001660 TIntermNode *intermNode = nullptr;
1661 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04001662 {
1663 //
1664 // Build intermediate representation
1665 //
Olli Etuahoe7847b02015-03-16 11:56:12 +02001666 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001667 }
1668 else
1669 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001670 return nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001671 }
1672}
1673
Jamie Madillb98c3a82015-07-23 14:26:04 -04001674TIntermAggregate *TParseContext::parseSingleArrayInitDeclaration(
1675 TPublicType &publicType,
1676 const TSourceLoc &identifierLocation,
1677 const TString &identifier,
1678 const TSourceLoc &indexLocation,
1679 TIntermTyped *indexExpression,
1680 const TSourceLoc &initLocation,
1681 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001682{
1683 mDeferredSingleDeclarationErrorCheck = false;
1684
Olli Etuaho383b7912016-08-05 11:22:59 +03001685 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001686
Olli Etuaho8a176262016-08-16 14:23:01 +03001687 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001688
1689 TPublicType arrayType(publicType);
1690
Olli Etuaho856c4972016-08-08 11:38:39 +03001691 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001692 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
1693 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03001694 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001695 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001696 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001697 }
1698 // Make the type an array even if size check failed.
1699 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1700 arrayType.setArraySize(size);
1701
1702 // initNode will correspond to the whole of "type b[n] = initializer".
1703 TIntermNode *initNode = nullptr;
1704 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1705 {
1706 return initNode ? intermediate.makeAggregate(initNode, initLocation) : nullptr;
1707 }
1708 else
1709 {
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001710 return nullptr;
1711 }
1712}
1713
Martin Radev70866b82016-07-22 15:27:42 +03001714TIntermAggregate *TParseContext::parseInvariantDeclaration(
1715 const TTypeQualifierBuilder &typeQualifierBuilder,
1716 const TSourceLoc &identifierLoc,
1717 const TString *identifier,
1718 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04001719{
Martin Radevc28888b2016-07-22 15:27:42 +03001720 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(
1721 &mDiagnostics, AreTypeQualifierChecksRelaxed(mShaderVersion));
Jamie Madill47e3ec02014-08-20 16:38:33 -04001722
Martin Radev70866b82016-07-22 15:27:42 +03001723 if (!typeQualifier.invariant)
1724 {
1725 error(identifierLoc, "Expected invariant", identifier->c_str());
1726 return nullptr;
1727 }
1728 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
1729 {
1730 return nullptr;
1731 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04001732 if (!symbol)
1733 {
1734 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001735 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001736 }
Martin Radev70866b82016-07-22 15:27:42 +03001737 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04001738 {
Martin Radev70866b82016-07-22 15:27:42 +03001739 error(identifierLoc, "invariant declaration specifies qualifier",
1740 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04001741 }
Martin Radev70866b82016-07-22 15:27:42 +03001742 if (typeQualifier.precision != EbpUndefined)
1743 {
1744 error(identifierLoc, "invariant declaration specifies precision",
1745 getPrecisionString(typeQualifier.precision));
1746 }
1747 if (!typeQualifier.layoutQualifier.isEmpty())
1748 {
1749 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
1750 }
1751
1752 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1753 ASSERT(variable);
1754 const TType &type = variable->getType();
1755
1756 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
1757 typeQualifier.line);
1758
1759 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
1760
1761 TIntermSymbol *intermSymbol =
1762 intermediate.addSymbol(variable->getUniqueId(), *identifier, type, identifierLoc);
1763
1764 TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc);
1765 aggregate->setOp(EOpInvariantDeclaration);
1766 return aggregate;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001767}
1768
Jamie Madillb98c3a82015-07-23 14:26:04 -04001769TIntermAggregate *TParseContext::parseDeclarator(TPublicType &publicType,
1770 TIntermAggregate *aggregateDeclaration,
1771 const TSourceLoc &identifierLocation,
1772 const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001773{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001774 // If the declaration starting this declarator list was empty (example: int,), some checks were
1775 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001776 if (mDeferredSingleDeclarationErrorCheck)
1777 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001778 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03001779 mDeferredSingleDeclarationErrorCheck = false;
1780 }
1781
Olli Etuaho856c4972016-08-08 11:38:39 +03001782 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04001783
Olli Etuaho856c4972016-08-08 11:38:39 +03001784 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04001785
Olli Etuaho2935c582015-04-08 14:32:06 +03001786 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03001787 declareVariable(identifierLocation, identifier, TType(publicType), &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001788
Jamie Madillb98c3a82015-07-23 14:26:04 -04001789 TIntermSymbol *symbol =
1790 intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001791 if (variable && symbol)
Jamie Madill502d66f2013-06-20 11:55:52 -04001792 symbol->setId(variable->getUniqueId());
1793
Olli Etuahoe7847b02015-03-16 11:56:12 +02001794 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001795}
1796
Jamie Madillb98c3a82015-07-23 14:26:04 -04001797TIntermAggregate *TParseContext::parseArrayDeclarator(TPublicType &publicType,
1798 TIntermAggregate *aggregateDeclaration,
1799 const TSourceLoc &identifierLocation,
1800 const TString &identifier,
1801 const TSourceLoc &arrayLocation,
1802 TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001803{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001804 // If the declaration starting this declarator list was empty (example: int,), some checks were
1805 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001806 if (mDeferredSingleDeclarationErrorCheck)
1807 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001808 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03001809 mDeferredSingleDeclarationErrorCheck = false;
1810 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001811
Olli Etuaho856c4972016-08-08 11:38:39 +03001812 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04001813
Olli Etuaho856c4972016-08-08 11:38:39 +03001814 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04001815
Olli Etuaho8a176262016-08-16 14:23:01 +03001816 if (checkIsValidTypeAndQualifierForArray(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001817 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001818 TType arrayType = TType(publicType);
Olli Etuaho856c4972016-08-08 11:38:39 +03001819 unsigned int size = checkIsValidArraySize(arrayLocation, indexExpression);
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001820 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001821
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001822 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03001823 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04001824
Jamie Madillb98c3a82015-07-23 14:26:04 -04001825 TIntermSymbol *symbol =
1826 intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001827 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001828 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001829
1830 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001831 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001832
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001833 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001834}
1835
Jamie Madillb98c3a82015-07-23 14:26:04 -04001836TIntermAggregate *TParseContext::parseInitDeclarator(const TPublicType &publicType,
1837 TIntermAggregate *aggregateDeclaration,
1838 const TSourceLoc &identifierLocation,
1839 const TString &identifier,
1840 const TSourceLoc &initLocation,
1841 TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001842{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001843 // If the declaration starting this declarator list was empty (example: int,), some checks were
1844 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001845 if (mDeferredSingleDeclarationErrorCheck)
1846 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001847 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03001848 mDeferredSingleDeclarationErrorCheck = false;
1849 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001850
Olli Etuaho856c4972016-08-08 11:38:39 +03001851 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04001852
Olli Etuahoe7847b02015-03-16 11:56:12 +02001853 TIntermNode *intermNode = nullptr;
1854 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04001855 {
1856 //
1857 // build the intermediate representation
1858 //
1859 if (intermNode)
1860 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001861 return intermediate.growAggregate(aggregateDeclaration, intermNode, initLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001862 }
1863 else
1864 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001865 return aggregateDeclaration;
Jamie Madill502d66f2013-06-20 11:55:52 -04001866 }
1867 }
1868 else
1869 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001870 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001871 }
1872}
1873
Jamie Madill06145232015-05-13 13:10:01 -04001874TIntermAggregate *TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001875 TIntermAggregate *aggregateDeclaration,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301876 const TSourceLoc &identifierLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001877 const TString &identifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301878 const TSourceLoc &indexLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001879 TIntermTyped *indexExpression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001880 const TSourceLoc &initLocation,
1881 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001882{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001883 // If the declaration starting this declarator list was empty (example: int,), some checks were
1884 // not performed.
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001885 if (mDeferredSingleDeclarationErrorCheck)
1886 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001887 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001888 mDeferredSingleDeclarationErrorCheck = false;
1889 }
1890
Olli Etuaho856c4972016-08-08 11:38:39 +03001891 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001892
Olli Etuaho8a176262016-08-16 14:23:01 +03001893 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001894
1895 TPublicType arrayType(publicType);
1896
Olli Etuaho856c4972016-08-08 11:38:39 +03001897 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001898 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
1899 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03001900 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001901 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001902 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001903 }
1904 // Make the type an array even if size check failed.
1905 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1906 arrayType.setArraySize(size);
1907
1908 // initNode will correspond to the whole of "b[n] = initializer".
1909 TIntermNode *initNode = nullptr;
1910 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1911 {
1912 if (initNode)
1913 {
1914 return intermediate.growAggregate(aggregateDeclaration, initNode, initLocation);
1915 }
1916 else
1917 {
1918 return aggregateDeclaration;
1919 }
1920 }
1921 else
1922 {
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001923 return nullptr;
1924 }
1925}
1926
Martin Radev70866b82016-07-22 15:27:42 +03001927void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04001928{
Martin Radevc28888b2016-07-22 15:27:42 +03001929 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(
1930 &mDiagnostics, AreTypeQualifierChecksRelaxed(mShaderVersion));
Jamie Madilla295edf2013-06-06 11:56:48 -04001931 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04001932
Martin Radev70866b82016-07-22 15:27:42 +03001933 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
1934 typeQualifier.line);
1935
Jamie Madillc2128ff2016-07-04 10:26:17 -04001936 // It should never be the case, but some strange parser errors can send us here.
1937 if (layoutQualifier.isEmpty())
1938 {
1939 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04001940 return;
1941 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001942
Martin Radev802abe02016-08-04 17:48:32 +03001943 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04001944 {
Martin Radev802abe02016-08-04 17:48:32 +03001945 error(typeQualifier.line, "invalid combination:", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04001946 return;
1947 }
1948
Martin Radev802abe02016-08-04 17:48:32 +03001949 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04001950 {
Martin Radev802abe02016-08-04 17:48:32 +03001951 if (mComputeShaderLocalSizeDeclared &&
1952 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
1953 {
1954 error(typeQualifier.line, "Work group size does not match the previous declaration",
1955 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03001956 return;
1957 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001958
Martin Radev802abe02016-08-04 17:48:32 +03001959 if (mShaderVersion < 310)
1960 {
1961 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03001962 return;
1963 }
Jamie Madill099c0f32013-06-20 11:55:52 -04001964
Martin Radev4c4c8e72016-08-04 12:25:34 +03001965 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03001966 {
1967 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03001968 return;
1969 }
1970
1971 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
1972 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
1973
1974 const TConstantUnion *maxComputeWorkGroupSizeData =
1975 maxComputeWorkGroupSize->getConstPointer();
1976
1977 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
1978 {
1979 if (layoutQualifier.localSize[i] != -1)
1980 {
1981 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
1982 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
1983 if (mComputeShaderLocalSize[i] < 1 ||
1984 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
1985 {
1986 std::stringstream errorMessageStream;
1987 errorMessageStream << "Value must be at least 1 and no greater than "
1988 << maxComputeWorkGroupSizeValue;
1989 const std::string &errorMessage = errorMessageStream.str();
1990
Martin Radev4c4c8e72016-08-04 12:25:34 +03001991 error(typeQualifier.line, "invalid value:", getWorkGroupSizeString(i),
Martin Radev802abe02016-08-04 17:48:32 +03001992 errorMessage.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001993 return;
1994 }
1995 }
1996 }
1997
1998 mComputeShaderLocalSizeDeclared = true;
1999 }
2000 else
Jamie Madill1566ef72013-06-20 11:55:54 -04002001 {
Martin Radev802abe02016-08-04 17:48:32 +03002002
Olli Etuaho8a176262016-08-16 14:23:01 +03002003 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03002004 {
Martin Radev802abe02016-08-04 17:48:32 +03002005 return;
2006 }
2007
2008 if (typeQualifier.qualifier != EvqUniform)
2009 {
2010 error(typeQualifier.line, "invalid qualifier:",
2011 getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
Martin Radev802abe02016-08-04 17:48:32 +03002012 return;
2013 }
2014
2015 if (mShaderVersion < 300)
2016 {
2017 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
2018 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002019 return;
2020 }
2021
Olli Etuaho856c4972016-08-08 11:38:39 +03002022 checkLocationIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002023
2024 if (layoutQualifier.matrixPacking != EmpUnspecified)
2025 {
2026 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
2027 }
2028
2029 if (layoutQualifier.blockStorage != EbsUnspecified)
2030 {
2031 mDefaultBlockStorage = layoutQualifier.blockStorage;
2032 }
Jamie Madill1566ef72013-06-20 11:55:54 -04002033 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002034}
2035
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002036TIntermAggregate *TParseContext::addFunctionPrototypeDeclaration(const TFunction &function,
2037 const TSourceLoc &location)
2038{
Olli Etuaho5d653182016-01-04 14:43:28 +02002039 // Note: symbolTableFunction could be the same as function if this is the first declaration.
2040 // Either way the instance in the symbol table is used to track whether the function is declared
2041 // multiple times.
2042 TFunction *symbolTableFunction =
2043 static_cast<TFunction *>(symbolTable.find(function.getMangledName(), getShaderVersion()));
2044 if (symbolTableFunction->hasPrototypeDeclaration() && mShaderVersion == 100)
2045 {
2046 // ESSL 1.00.17 section 4.2.7.
2047 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
2048 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02002049 }
2050 symbolTableFunction->setHasPrototypeDeclaration();
2051
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002052 TIntermAggregate *prototype = new TIntermAggregate;
2053 prototype->setType(function.getReturnType());
2054 prototype->setName(function.getMangledName());
2055 prototype->setFunctionId(function.getUniqueId());
2056
2057 for (size_t i = 0; i < function.getParamCount(); i++)
2058 {
2059 const TConstParameter &param = function.getParam(i);
2060 if (param.name != 0)
2061 {
2062 TVariable variable(param.name, *param.type);
2063
2064 TIntermSymbol *paramSymbol = intermediate.addSymbol(
2065 variable.getUniqueId(), variable.getName(), variable.getType(), location);
2066 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
2067 }
2068 else
2069 {
2070 TIntermSymbol *paramSymbol = intermediate.addSymbol(0, "", *param.type, location);
2071 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
2072 }
2073 }
2074
2075 prototype->setOp(EOpPrototype);
2076
2077 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002078
2079 if (!symbolTable.atGlobalLevel())
2080 {
2081 // ESSL 3.00.4 section 4.2.4.
2082 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002083 }
2084
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002085 return prototype;
2086}
2087
2088TIntermAggregate *TParseContext::addFunctionDefinition(const TFunction &function,
2089 TIntermAggregate *functionPrototype,
2090 TIntermAggregate *functionBody,
2091 const TSourceLoc &location)
2092{
2093 //?? Check that all paths return a value if return type != void ?
2094 // May be best done as post process phase on intermediate code
2095 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
2096 {
2097 error(location, "function does not return a value:", "", function.getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002098 }
2099
2100 TIntermAggregate *aggregate =
2101 intermediate.growAggregate(functionPrototype, functionBody, location);
2102 intermediate.setAggregateOperator(aggregate, EOpFunction, location);
2103 aggregate->setName(function.getMangledName().c_str());
2104 aggregate->setType(function.getReturnType());
2105 aggregate->setFunctionId(function.getUniqueId());
2106
2107 symbolTable.pop();
2108 return aggregate;
2109}
2110
Jamie Madill185fb402015-06-12 15:48:48 -04002111void TParseContext::parseFunctionPrototype(const TSourceLoc &location,
2112 TFunction *function,
2113 TIntermAggregate **aggregateOut)
2114{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002115 const TSymbol *builtIn =
2116 symbolTable.findBuiltIn(function->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002117
2118 if (builtIn)
2119 {
2120 error(location, "built-in functions cannot be redefined", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002121 }
2122
Jamie Madillb98c3a82015-07-23 14:26:04 -04002123 TFunction *prevDec =
2124 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Jamie Madill185fb402015-06-12 15:48:48 -04002125 //
2126 // Note: 'prevDec' could be 'function' if this is the first time we've seen function
2127 // as it would have just been put in the symbol table. Otherwise, we're looking up
2128 // an earlier occurance.
2129 //
2130 if (prevDec->isDefined())
2131 {
2132 // Then this function already has a body.
2133 error(location, "function already has a body", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002134 }
2135 prevDec->setDefined();
2136 //
2137 // Overload the unique ID of the definition to be the same unique ID as the declaration.
2138 // Eventually we will probably want to have only a single definition and just swap the
2139 // arguments to be the definition's arguments.
2140 //
2141 function->setUniqueId(prevDec->getUniqueId());
2142
2143 // Raise error message if main function takes any parameters or return anything other than void
2144 if (function->getName() == "main")
2145 {
2146 if (function->getParamCount() > 0)
2147 {
2148 error(location, "function cannot take any parameter(s)", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002149 }
2150 if (function->getReturnType().getBasicType() != EbtVoid)
2151 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002152 error(location, "", function->getReturnType().getBasicString(),
2153 "main function cannot return a value");
Jamie Madill185fb402015-06-12 15:48:48 -04002154 }
2155 }
2156
2157 //
2158 // Remember the return type for later checking for RETURN statements.
2159 //
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002160 mCurrentFunctionType = &(prevDec->getReturnType());
2161 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04002162
2163 //
2164 // Insert parameters into the symbol table.
2165 // If the parameter has no name, it's not an error, just don't insert it
2166 // (could be used for unused args).
2167 //
2168 // Also, accumulate the list of parameters into the HIL, so lower level code
2169 // knows where to find parameters.
2170 //
2171 TIntermAggregate *paramNodes = new TIntermAggregate;
2172 for (size_t i = 0; i < function->getParamCount(); i++)
2173 {
2174 const TConstParameter &param = function->getParam(i);
2175 if (param.name != 0)
2176 {
2177 TVariable *variable = new TVariable(param.name, *param.type);
2178 //
2179 // Insert the parameters with name in the symbol table.
2180 //
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002181 if (!symbolTable.declare(variable))
2182 {
Jamie Madill185fb402015-06-12 15:48:48 -04002183 error(location, "redefinition", variable->getName().c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002184 paramNodes = intermediate.growAggregate(
2185 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
2186 continue;
Jamie Madill185fb402015-06-12 15:48:48 -04002187 }
2188
2189 //
2190 // Add the parameter to the HIL
2191 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002192 TIntermSymbol *symbol = intermediate.addSymbol(
2193 variable->getUniqueId(), variable->getName(), variable->getType(), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002194
2195 paramNodes = intermediate.growAggregate(paramNodes, symbol, location);
2196 }
2197 else
2198 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002199 paramNodes = intermediate.growAggregate(
2200 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002201 }
2202 }
2203 intermediate.setAggregateOperator(paramNodes, EOpParameters, location);
2204 *aggregateOut = paramNodes;
2205 setLoopNestingLevel(0);
2206}
2207
Jamie Madillb98c3a82015-07-23 14:26:04 -04002208TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002209{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002210 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002211 // We don't know at this point whether this is a function definition or a prototype.
2212 // The definition production code will check for redefinitions.
2213 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002214 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002215 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
2216 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002217 //
2218 TFunction *prevDec =
2219 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302220
2221 if (getShaderVersion() >= 300 && symbolTable.hasUnmangledBuiltIn(function->getName().c_str()))
2222 {
2223 // With ESSL 3.00, names of built-in functions cannot be redeclared as functions.
2224 // Therefore overloading or redefining builtin functions is an error.
2225 error(location, "Name of a built-in function cannot be redeclared as function",
2226 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302227 }
2228 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002229 {
2230 if (prevDec->getReturnType() != function->getReturnType())
2231 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002232 error(location, "overloaded functions must have the same return type",
Jamie Madill185fb402015-06-12 15:48:48 -04002233 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04002234 }
2235 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2236 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002237 if (prevDec->getParam(i).type->getQualifier() !=
2238 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002239 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002240 error(location, "overloaded functions must have the same parameter qualifiers",
Jamie Madill185fb402015-06-12 15:48:48 -04002241 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04002242 }
2243 }
2244 }
2245
2246 //
2247 // Check for previously declared variables using the same name.
2248 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002249 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002250 if (prevSym)
2251 {
2252 if (!prevSym->isFunction())
2253 {
2254 error(location, "redefinition", function->getName().c_str(), "function");
Jamie Madill185fb402015-06-12 15:48:48 -04002255 }
2256 }
2257 else
2258 {
2259 // Insert the unmangled name to detect potential future redefinition as a variable.
Jamie Madillb98c3a82015-07-23 14:26:04 -04002260 TFunction *newFunction =
2261 new TFunction(NewPoolTString(function->getName().c_str()), &function->getReturnType());
Jamie Madill185fb402015-06-12 15:48:48 -04002262 symbolTable.getOuterLevel()->insertUnmangled(newFunction);
2263 }
2264
2265 // We're at the inner scope level of the function's arguments and body statement.
2266 // Add the function prototype to the surrounding scope instead.
2267 symbolTable.getOuterLevel()->insert(function);
2268
2269 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002270 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2271 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002272 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2273 //
2274 return function;
2275}
2276
Olli Etuaho9de84a52016-06-14 17:36:01 +03002277TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
2278 const TString *name,
2279 const TSourceLoc &location)
2280{
2281 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
2282 {
2283 error(location, "no qualifiers allowed for function return",
2284 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03002285 }
2286 if (!type.layoutQualifier.isEmpty())
2287 {
2288 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03002289 }
2290 // make sure a sampler is not involved as well...
Martin Radev4a9cd802016-09-01 16:51:51 +03002291 checkIsNotSampler(location, type.typeSpecifierNonArray,
2292 "samplers can't be function return values");
Olli Etuahoe29324f2016-06-15 10:58:03 +03002293 if (mShaderVersion < 300)
2294 {
2295 // Array return values are forbidden, but there's also no valid syntax for declaring array
2296 // return values in ESSL 1.00.
2297 ASSERT(type.arraySize == 0 || mDiagnostics.numErrors() > 0);
2298
2299 if (type.isStructureContainingArrays())
2300 {
2301 // ESSL 1.00.17 section 6.1 Function Definitions
2302 error(location, "structures containing arrays can't be function return values",
2303 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03002304 }
2305 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03002306
2307 // Add the function as a prototype after parsing it (we do not support recursion)
2308 return new TFunction(name, new TType(type));
2309}
2310
Jamie Madill06145232015-05-13 13:10:01 -04002311TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002312{
Jamie Madill06145232015-05-13 13:10:01 -04002313 TPublicType publicType = publicTypeIn;
Martin Radev4a9cd802016-09-01 16:51:51 +03002314 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02002315 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002316 error(publicType.getLine(), "constructor can't be a structure definition",
2317 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02002318 }
2319
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002320 TOperator op = EOpNull;
Martin Radev4a9cd802016-09-01 16:51:51 +03002321 if (publicType.getUserDef())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002322 {
2323 op = EOpConstructStruct;
2324 }
2325 else
2326 {
Geoff Lang156d7192016-07-21 16:11:00 -04002327 op = sh::TypeToConstructorOperator(TType(publicType));
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002328 if (op == EOpNull)
2329 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002330 error(publicType.getLine(), "cannot construct this type",
2331 getBasicString(publicType.getBasicType()));
2332 publicType.setBasicType(EbtFloat);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002333 op = EOpConstructFloat;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002334 }
2335 }
2336
2337 TString tempString;
Dmitry Skiba7f17a502015-06-22 15:08:39 -07002338 const TType *type = new TType(publicType);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002339 return new TFunction(&tempString, type, op);
2340}
2341
Jamie Madillb98c3a82015-07-23 14:26:04 -04002342// This function is used to test for the correctness of the parameters passed to various constructor
2343// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002344//
Olli Etuaho856c4972016-08-08 11:38:39 +03002345// 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 +00002346//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002347TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002348 TOperator op,
2349 TFunction *fnCall,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302350 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002351{
Olli Etuaho856c4972016-08-08 11:38:39 +03002352 TType type = fnCall->getReturnType();
2353 if (type.isUnsizedArray())
2354 {
2355 type.setArraySize(static_cast<unsigned int>(fnCall->getParamCount()));
2356 }
2357 bool constType = true;
2358 for (size_t i = 0; i < fnCall->getParamCount(); ++i)
2359 {
2360 const TConstParameter &param = fnCall->getParam(i);
2361 if (param.type->getQualifier() != EvqConst)
2362 constType = false;
2363 }
2364 if (constType)
2365 type.setQualifier(EvqConst);
2366
Olli Etuaho8a176262016-08-16 14:23:01 +03002367 if (!checkConstructorArguments(line, arguments, *fnCall, op, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03002368 {
2369 TIntermTyped *dummyNode = intermediate.setAggregateOperator(nullptr, op, line);
2370 dummyNode->setType(type);
2371 return dummyNode;
2372 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002373 TIntermAggregate *constructor = arguments->getAsAggregate();
2374 ASSERT(constructor != nullptr);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002375
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002376 // Turn the argument list itself into a constructor
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002377 constructor->setOp(op);
2378 constructor->setLine(line);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002379 ASSERT(constructor->isConstructor());
2380
2381 // Need to set type before setPrecisionFromChildren() because bool doesn't have precision.
Olli Etuaho856c4972016-08-08 11:38:39 +03002382 constructor->setType(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002383
Olli Etuaho21203702014-11-13 16:16:21 +02002384 // Structs should not be precision qualified, the individual members may be.
2385 // Built-in types on the other hand should be precision qualified.
2386 if (op != EOpConstructStruct)
2387 {
2388 constructor->setPrecisionFromChildren();
Olli Etuaho856c4972016-08-08 11:38:39 +03002389 type.setPrecision(constructor->getPrecision());
Olli Etuaho21203702014-11-13 16:16:21 +02002390 }
2391
Olli Etuaho856c4972016-08-08 11:38:39 +03002392 constructor->setType(type);
2393
Olli Etuaho1d122782015-11-06 15:35:17 +02002394 TIntermTyped *constConstructor = intermediate.foldAggregateBuiltIn(constructor);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002395 if (constConstructor)
2396 {
2397 return constConstructor;
2398 }
2399
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002400 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002401}
2402
Olli Etuaho90892fb2016-07-14 14:44:51 +03002403// This function returns vector field(s) being accessed from a constant vector.
2404TIntermConstantUnion *TParseContext::foldVectorSwizzle(TVectorFields &fields,
2405 TIntermConstantUnion *baseNode,
2406 const TSourceLoc &location)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002407{
Olli Etuaho90892fb2016-07-14 14:44:51 +03002408 const TConstantUnion *unionArray = baseNode->getUnionArrayPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002409 ASSERT(unionArray);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002410
Arun Patole7e7e68d2015-05-22 12:02:25 +05302411 TConstantUnion *constArray = new TConstantUnion[fields.num];
Olli Etuaho90892fb2016-07-14 14:44:51 +03002412 const auto &type = baseNode->getType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002413
Arun Patole7e7e68d2015-05-22 12:02:25 +05302414 for (int i = 0; i < fields.num; i++)
2415 {
Olli Etuaho90892fb2016-07-14 14:44:51 +03002416 // Out-of-range indices should already be checked.
2417 ASSERT(fields.offsets[i] < type.getNominalSize());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002418 constArray[i] = unionArray[fields.offsets[i]];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302419 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03002420 return intermediate.addConstantUnion(constArray, type, location);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002421}
2422
Olli Etuaho90892fb2016-07-14 14:44:51 +03002423// This function returns the column vector being accessed from a constant matrix.
2424TIntermConstantUnion *TParseContext::foldMatrixSubscript(int index,
2425 TIntermConstantUnion *baseNode,
2426 const TSourceLoc &location)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002427{
Olli Etuaho90892fb2016-07-14 14:44:51 +03002428 ASSERT(index < baseNode->getType().getCols());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002429
Olli Etuaho90892fb2016-07-14 14:44:51 +03002430 const TConstantUnion *unionArray = baseNode->getUnionArrayPointer();
2431 int size = baseNode->getType().getRows();
2432 return intermediate.addConstantUnion(&unionArray[size * index], baseNode->getType(), location);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002433}
2434
Olli Etuaho90892fb2016-07-14 14:44:51 +03002435// This function returns an element of an array accessed from a constant array.
2436TIntermConstantUnion *TParseContext::foldArraySubscript(int index,
2437 TIntermConstantUnion *baseNode,
2438 const TSourceLoc &location)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002439{
Olli Etuaho856c4972016-08-08 11:38:39 +03002440 ASSERT(index < static_cast<int>(baseNode->getArraySize()));
Olli Etuaho90892fb2016-07-14 14:44:51 +03002441
2442 TType arrayElementType = baseNode->getType();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002443 arrayElementType.clearArrayness();
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002444 size_t arrayElementSize = arrayElementType.getObjectSize();
Olli Etuaho90892fb2016-07-14 14:44:51 +03002445 const TConstantUnion *unionArray = baseNode->getUnionArrayPointer();
2446 return intermediate.addConstantUnion(&unionArray[arrayElementSize * index], baseNode->getType(),
2447 location);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002448}
2449
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002450//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002451// This function returns the value of a particular field inside a constant structure from the symbol
2452// table.
2453// If there is an embedded/nested struct, it appropriately calls addConstStructNested or
2454// addConstStructFromAggr function and returns the parse-tree with the values of the embedded/nested
2455// struct.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002456//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002457TIntermTyped *TParseContext::addConstStruct(const TString &identifier,
2458 TIntermTyped *node,
2459 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002460{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302461 const TFieldList &fields = node->getType().getStruct()->fields();
Jamie Madillb98c3a82015-07-23 14:26:04 -04002462 size_t instanceSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002463
Arun Patole7e7e68d2015-05-22 12:02:25 +05302464 for (size_t index = 0; index < fields.size(); ++index)
2465 {
2466 if (fields[index]->name() == identifier)
2467 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002468 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302469 }
2470 else
2471 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002472 instanceSize += fields[index]->type()->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002473 }
2474 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002475
Jamie Madill94bf7f22013-07-08 13:31:15 -04002476 TIntermTyped *typedNode;
2477 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
Arun Patole7e7e68d2015-05-22 12:02:25 +05302478 if (tempConstantNode)
2479 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002480 const TConstantUnion *constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002481
Jamie Madillb98c3a82015-07-23 14:26:04 -04002482 // type will be changed in the calling function
2483 typedNode = intermediate.addConstantUnion(constArray + instanceSize,
2484 tempConstantNode->getType(), line);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302485 }
2486 else
2487 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002488 error(line, "Cannot offset into the structure", "Error");
Olli Etuaho383b7912016-08-05 11:22:59 +03002489 return nullptr;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002490 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002491
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002492 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002493}
2494
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002495//
2496// Interface/uniform blocks
2497//
Martin Radev70866b82016-07-22 15:27:42 +03002498TIntermAggregate *TParseContext::addInterfaceBlock(
2499 const TTypeQualifierBuilder &typeQualifierBuilder,
2500 const TSourceLoc &nameLine,
2501 const TString &blockName,
2502 TFieldList *fieldList,
2503 const TString *instanceName,
2504 const TSourceLoc &instanceLine,
2505 TIntermTyped *arrayIndex,
2506 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002507{
Olli Etuaho856c4972016-08-08 11:38:39 +03002508 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002509
Martin Radevc28888b2016-07-22 15:27:42 +03002510 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(
2511 &mDiagnostics, AreTypeQualifierChecksRelaxed(mShaderVersion));
Martin Radev70866b82016-07-22 15:27:42 +03002512
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002513 if (typeQualifier.qualifier != EvqUniform)
2514 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302515 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
2516 "interface blocks must be uniform");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002517 }
2518
Martin Radev70866b82016-07-22 15:27:42 +03002519 if (typeQualifier.invariant)
2520 {
2521 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
2522 }
2523
Jamie Madill099c0f32013-06-20 11:55:52 -04002524 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03002525 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002526
Jamie Madill099c0f32013-06-20 11:55:52 -04002527 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2528 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002529 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002530 }
2531
Jamie Madill1566ef72013-06-20 11:55:54 -04002532 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2533 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002534 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002535 }
2536
Olli Etuaho856c4972016-08-08 11:38:39 +03002537 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002538
Arun Patole7e7e68d2015-05-22 12:02:25 +05302539 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2540 if (!symbolTable.declare(blockNameSymbol))
2541 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002542 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002543 }
2544
Jamie Madill98493dd2013-07-08 14:39:03 -04002545 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302546 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2547 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002548 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302549 TType *fieldType = field->type();
2550 if (IsSampler(fieldType->getBasicType()))
2551 {
2552 error(field->line(), "unsupported type", fieldType->getBasicString(),
2553 "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002554 }
2555
Jamie Madill98493dd2013-07-08 14:39:03 -04002556 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002557 switch (qualifier)
2558 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002559 case EvqGlobal:
2560 case EvqUniform:
2561 break;
2562 default:
2563 error(field->line(), "invalid qualifier on interface block member",
2564 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04002565 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002566 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002567
Martin Radev70866b82016-07-22 15:27:42 +03002568 if (fieldType->isInvariant())
2569 {
2570 error(field->line(), "invalid qualifier on interface block member", "invariant");
2571 }
2572
Jamie Madilla5efff92013-06-06 11:56:47 -04002573 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002574 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03002575 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
Jamie Madill099c0f32013-06-20 11:55:52 -04002576
Jamie Madill98493dd2013-07-08 14:39:03 -04002577 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002578 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002579 error(field->line(), "invalid layout qualifier:",
2580 getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04002581 }
2582
Jamie Madill98493dd2013-07-08 14:39:03 -04002583 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002584 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002585 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002586 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002587 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002588 {
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002589 warning(field->line(), "extraneous layout qualifier:",
Jamie Madillb98c3a82015-07-23 14:26:04 -04002590 getMatrixPackingString(fieldLayoutQualifier.matrixPacking),
2591 "only has an effect on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04002592 }
2593
Jamie Madill98493dd2013-07-08 14:39:03 -04002594 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002595 }
2596
Jamie Madill98493dd2013-07-08 14:39:03 -04002597 // add array index
Olli Etuaho856c4972016-08-08 11:38:39 +03002598 unsigned int arraySize = 0;
2599 if (arrayIndex != nullptr)
Jamie Madill98493dd2013-07-08 14:39:03 -04002600 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002601 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04002602 }
2603
Jamie Madillb98c3a82015-07-23 14:26:04 -04002604 TInterfaceBlock *interfaceBlock =
2605 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2606 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
2607 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002608
2609 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04002610 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002611
Jamie Madill98493dd2013-07-08 14:39:03 -04002612 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002613 {
2614 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002615 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2616 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002617 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302618 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002619
2620 // set parent pointer of the field variable
2621 fieldType->setInterfaceBlock(interfaceBlock);
2622
Arun Patole7e7e68d2015-05-22 12:02:25 +05302623 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002624 fieldVariable->setQualifier(typeQualifier.qualifier);
2625
Arun Patole7e7e68d2015-05-22 12:02:25 +05302626 if (!symbolTable.declare(fieldVariable))
2627 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002628 error(field->line(), "redefinition", field->name().c_str(),
2629 "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002630 }
2631 }
2632 }
2633 else
2634 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002635 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03002636
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002637 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302638 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002639 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002640
Arun Patole7e7e68d2015-05-22 12:02:25 +05302641 if (!symbolTable.declare(instanceTypeDef))
2642 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002643 error(instanceLine, "redefinition", instanceName->c_str(),
2644 "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002645 }
2646
Jamie Madillb98c3a82015-07-23 14:26:04 -04002647 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002648 symbolName = instanceTypeDef->getName();
2649 }
2650
Jamie Madillb98c3a82015-07-23 14:26:04 -04002651 TIntermAggregate *aggregate = intermediate.makeAggregate(
2652 intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line),
2653 nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002654 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04002655
2656 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002657 return aggregate;
2658}
2659
Olli Etuaho383b7912016-08-05 11:22:59 +03002660void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002661{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002662 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002663
2664 // Embedded structure definitions are not supported per GLSL ES spec.
2665 // They aren't allowed in GLSL either, but we need to detect this here
2666 // so we don't rely on the GLSL compiler to catch it.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302667 if (mStructNestingLevel > 1)
2668 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002669 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002670 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002671}
2672
2673void TParseContext::exitStructDeclaration()
2674{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002675 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002676}
2677
Jamie Madillb98c3a82015-07-23 14:26:04 -04002678namespace
2679{
kbr@chromium.org476541f2011-10-27 21:14:51 +00002680const int kWebGLMaxStructNesting = 4;
2681
2682} // namespace
2683
Olli Etuaho8a176262016-08-16 14:23:01 +03002684void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002685{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302686 if (!IsWebGLBasedSpec(mShaderSpec))
2687 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002688 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002689 }
2690
Arun Patole7e7e68d2015-05-22 12:02:25 +05302691 if (field.type()->getBasicType() != EbtStruct)
2692 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002693 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002694 }
2695
2696 // We're already inside a structure definition at this point, so add
2697 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302698 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2699 {
Jamie Madill41a49272014-03-18 16:10:13 -04002700 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002701 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
2702 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04002703 std::string reason = reasonStream.str();
2704 error(line, reason.c_str(), field.name().c_str(), "");
Olli Etuaho8a176262016-08-16 14:23:01 +03002705 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002706 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002707}
2708
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002709//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002710// Parse an array index expression
2711//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002712TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
2713 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302714 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002715{
2716 TIntermTyped *indexedExpression = NULL;
2717
2718 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2719 {
2720 if (baseExpression->getAsSymbolNode())
2721 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302722 error(location, " left of '[' is not of type array, matrix, or vector ",
2723 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002724 }
2725 else
2726 {
2727 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2728 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002729 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002730
Jamie Madill21c1e452014-12-29 11:33:41 -05002731 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2732
Olli Etuaho36b05142015-11-12 13:10:42 +02002733 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
2734 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
2735 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
2736 // index is a constant expression.
2737 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
2738 {
2739 if (baseExpression->isInterfaceBlock())
2740 {
2741 error(
2742 location, "", "[",
2743 "array indexes for interface blocks arrays must be constant integral expressions");
Olli Etuaho36b05142015-11-12 13:10:42 +02002744 }
2745 else if (baseExpression->getQualifier() == EvqFragmentOut)
2746 {
2747 error(location, "", "[",
2748 "array indexes for fragment outputs must be constant integral expressions");
Olli Etuaho36b05142015-11-12 13:10:42 +02002749 }
Olli Etuaho3e960462015-11-12 15:58:39 +02002750 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
2751 {
2752 error(location, "", "[", "array index for gl_FragData must be constant zero");
Olli Etuaho3e960462015-11-12 15:58:39 +02002753 }
Olli Etuaho36b05142015-11-12 13:10:42 +02002754 }
2755
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002756 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002757 {
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002758 // If the index is not qualified as constant, the behavior in the spec is undefined. This
2759 // applies even if ANGLE has been able to constant fold it (ANGLE may constant fold
2760 // expressions that are not constant expressions). The most compatible way to handle this
2761 // case is to report a warning instead of an error and force the index to be in the
2762 // correct range.
2763 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Jamie Madill21c1e452014-12-29 11:33:41 -05002764 int index = indexConstantUnion->getIConst(0);
Olli Etuaho90892fb2016-07-14 14:44:51 +03002765 if (!baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04002766 {
Olli Etuaho90892fb2016-07-14 14:44:51 +03002767 // Array checks are done later because a different error message might be generated
2768 // based on the index in some cases.
2769 if (baseExpression->isVector())
2770 {
2771 index = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
2772 baseExpression->getType().getNominalSize(),
2773 "vector field selection out of range", "[]");
2774 }
2775 else if (baseExpression->isMatrix())
2776 {
2777 index = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
2778 baseExpression->getType().getCols(),
2779 "matrix field selection out of range", "[]");
2780 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002781 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03002782
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002783 TIntermConstantUnion *baseConstantUnion = baseExpression->getAsConstantUnion();
2784 if (baseConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002785 {
2786 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002787 {
Olli Etuaho90892fb2016-07-14 14:44:51 +03002788 index = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
2789 baseExpression->getArraySize(),
2790 "array index out of range", "[]");
2791 // Constant folding for array indexing.
2792 indexedExpression = foldArraySubscript(index, baseConstantUnion, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002793 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002794 else if (baseExpression->isVector())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002795 {
Olli Etuaho90892fb2016-07-14 14:44:51 +03002796 // Constant folding for vector indexing - reusing vector swizzle folding.
Jamie Madill7164cf42013-07-08 13:30:59 -04002797 TVectorFields fields;
2798 fields.num = 1;
Olli Etuaho90892fb2016-07-14 14:44:51 +03002799 fields.offsets[0] = index;
2800 indexedExpression = foldVectorSwizzle(fields, baseConstantUnion, location);
Jamie Madill7164cf42013-07-08 13:30:59 -04002801 }
2802 else if (baseExpression->isMatrix())
2803 {
Olli Etuaho90892fb2016-07-14 14:44:51 +03002804 // Constant folding for matrix indexing.
2805 indexedExpression = foldMatrixSubscript(index, baseConstantUnion, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002806 }
2807 }
2808 else
2809 {
Jamie Madillb11e2482015-05-04 14:21:22 -04002810 int safeIndex = -1;
2811
Jamie Madill7164cf42013-07-08 13:30:59 -04002812 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002813 {
Olli Etuaho3e960462015-11-12 15:58:39 +02002814 if (baseExpression->getQualifier() == EvqFragData && index > 0)
2815 {
2816 if (mShaderSpec == SH_WEBGL2_SPEC)
2817 {
2818 // Error has been already generated if index is not const.
2819 if (indexExpression->getQualifier() == EvqConst)
2820 {
2821 error(location, "", "[",
2822 "array index for gl_FragData must be constant zero");
Olli Etuaho3e960462015-11-12 15:58:39 +02002823 }
2824 safeIndex = 0;
2825 }
2826 else if (!isExtensionEnabled("GL_EXT_draw_buffers"))
2827 {
2828 outOfRangeError(outOfRangeIndexIsError, location, "", "[",
2829 "array index for gl_FragData must be zero when "
2830 "GL_EXT_draw_buffers is disabled");
2831 safeIndex = 0;
2832 }
2833 }
2834 // Only do generic out-of-range check if similar error hasn't already been reported.
Olli Etuaho90892fb2016-07-14 14:44:51 +03002835 if (safeIndex < 0)
Jamie Madill7164cf42013-07-08 13:30:59 -04002836 {
Olli Etuaho90892fb2016-07-14 14:44:51 +03002837 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
2838 baseExpression->getArraySize(),
2839 "array index out of range", "[]");
Jamie Madill7164cf42013-07-08 13:30:59 -04002840 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002841 }
2842
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002843 // Data of constant unions can't be changed, because it may be shared with other
2844 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
2845 // sanitized object.
Jamie Madillb11e2482015-05-04 14:21:22 -04002846 if (safeIndex != -1)
2847 {
2848 TConstantUnion *safeConstantUnion = new TConstantUnion();
2849 safeConstantUnion->setIConst(safeIndex);
2850 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
2851 }
2852
Jamie Madillb98c3a82015-07-23 14:26:04 -04002853 indexedExpression =
2854 intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002855 }
2856 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002857 else
2858 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002859 indexedExpression =
2860 intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
Jamie Madill7164cf42013-07-08 13:30:59 -04002861 }
2862
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002863 if (indexedExpression == 0)
2864 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002865 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002866 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002867 indexedExpression =
2868 intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002869 }
2870 else if (baseExpression->isArray())
2871 {
Olli Etuahob3fbd862015-09-30 17:55:02 +03002872 TType indexedType = baseExpression->getType();
2873 indexedType.clearArrayness();
2874 indexedExpression->setType(indexedType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002875 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002876 else if (baseExpression->isMatrix())
2877 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002878 indexedExpression->setType(TType(baseExpression->getBasicType(),
Olli Etuahob3fbd862015-09-30 17:55:02 +03002879 baseExpression->getPrecision(), EvqTemporary,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002880 static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002881 }
2882 else if (baseExpression->isVector())
2883 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002884 indexedExpression->setType(
Olli Etuahob3fbd862015-09-30 17:55:02 +03002885 TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002886 }
2887 else
2888 {
2889 indexedExpression->setType(baseExpression->getType());
2890 }
2891
Olli Etuahob3fbd862015-09-30 17:55:02 +03002892 if (baseExpression->getType().getQualifier() == EvqConst &&
2893 indexExpression->getType().getQualifier() == EvqConst)
2894 {
2895 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2896 }
2897 else
2898 {
2899 indexedExpression->getTypePointer()->setQualifier(EvqTemporary);
2900 }
2901
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002902 return indexedExpression;
2903}
2904
Olli Etuaho90892fb2016-07-14 14:44:51 +03002905int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
2906 const TSourceLoc &location,
2907 int index,
2908 int arraySize,
2909 const char *reason,
2910 const char *token)
2911{
2912 if (index >= arraySize || index < 0)
2913 {
2914 std::stringstream extraInfoStream;
2915 extraInfoStream << "'" << index << "'";
2916 std::string extraInfo = extraInfoStream.str();
2917 outOfRangeError(outOfRangeIndexIsError, location, reason, token, extraInfo.c_str());
2918 if (index < 0)
2919 {
2920 return 0;
2921 }
2922 else
2923 {
2924 return arraySize - 1;
2925 }
2926 }
2927 return index;
2928}
2929
Jamie Madillb98c3a82015-07-23 14:26:04 -04002930TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
2931 const TSourceLoc &dotLocation,
2932 const TString &fieldString,
2933 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002934{
2935 TIntermTyped *indexedExpression = NULL;
2936
2937 if (baseExpression->isArray())
2938 {
2939 error(fieldLocation, "cannot apply dot operator to an array", ".");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002940 }
2941
2942 if (baseExpression->isVector())
2943 {
2944 TVectorFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002945 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields,
2946 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002947 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002948 fields.num = 1;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002949 fields.offsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002950 }
2951
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002952 if (baseExpression->getAsConstantUnion())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002953 {
2954 // constant folding for vector fields
Olli Etuaho90892fb2016-07-14 14:44:51 +03002955 indexedExpression =
2956 foldVectorSwizzle(fields, baseExpression->getAsConstantUnion(), fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002957 }
2958 else
2959 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302960 TIntermTyped *index = intermediate.addSwizzle(fields, fieldLocation);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002961 indexedExpression =
2962 intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002963 }
2964 if (indexedExpression == nullptr)
2965 {
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002966 indexedExpression = baseExpression;
2967 }
2968 else
2969 {
2970 // Note that the qualifier set here will be corrected later.
Jamie Madillb98c3a82015-07-23 14:26:04 -04002971 indexedExpression->setType(TType(baseExpression->getBasicType(),
2972 baseExpression->getPrecision(), EvqTemporary,
Jamie Madillc2128ff2016-07-04 10:26:17 -04002973 static_cast<unsigned char>(fields.num)));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002974 }
2975 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002976 else if (baseExpression->getBasicType() == EbtStruct)
2977 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002978 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302979 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04002980 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002981 {
2982 error(dotLocation, "structure has no fields", "Internal Error");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002983 indexedExpression = baseExpression;
2984 }
2985 else
2986 {
2987 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002988 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002989 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002990 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002991 {
2992 fieldFound = true;
2993 break;
2994 }
2995 }
2996 if (fieldFound)
2997 {
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002998 if (baseExpression->getAsConstantUnion())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002999 {
3000 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
3001 if (indexedExpression == 0)
3002 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003003 indexedExpression = baseExpression;
3004 }
3005 else
3006 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003007 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003008 }
3009 }
3010 else
3011 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003012 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003013 unionArray->setIConst(i);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003014 TIntermTyped *index = intermediate.addConstantUnion(
3015 unionArray, *fields[i]->type(), fieldLocation);
3016 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression,
3017 index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04003018 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003019 }
3020 }
3021 else
3022 {
3023 error(dotLocation, " no such field in structure", fieldString.c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003024 indexedExpression = baseExpression;
3025 }
3026 }
3027 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003028 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003029 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003030 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05303031 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003032 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003033 {
3034 error(dotLocation, "interface block has no fields", "Internal Error");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003035 indexedExpression = baseExpression;
3036 }
3037 else
3038 {
3039 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003040 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003041 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003042 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003043 {
3044 fieldFound = true;
3045 break;
3046 }
3047 }
3048 if (fieldFound)
3049 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003050 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003051 unionArray->setIConst(i);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003052 TIntermTyped *index =
3053 intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
3054 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock,
3055 baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04003056 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003057 }
3058 else
3059 {
3060 error(dotLocation, " no such field in interface block", fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003061 indexedExpression = baseExpression;
3062 }
3063 }
3064 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003065 else
3066 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003067 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003068 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03003069 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303070 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003071 }
3072 else
3073 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303074 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003075 " field selection requires structure, vector, or interface block on left hand "
3076 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303077 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003078 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003079 indexedExpression = baseExpression;
3080 }
3081
Olli Etuahob1edc4f2015-11-02 17:20:03 +02003082 if (baseExpression->getQualifier() == EvqConst)
3083 {
3084 indexedExpression->getTypePointer()->setQualifier(EvqConst);
3085 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003086 else
3087 {
3088 indexedExpression->getTypePointer()->setQualifier(EvqTemporary);
3089 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02003090
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003091 return indexedExpression;
3092}
3093
Jamie Madillb98c3a82015-07-23 14:26:04 -04003094TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3095 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003096{
Martin Radev802abe02016-08-04 17:48:32 +03003097 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003098
3099 if (qualifierType == "shared")
3100 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003101 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003102 }
3103 else if (qualifierType == "packed")
3104 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003105 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003106 }
3107 else if (qualifierType == "std140")
3108 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003109 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003110 }
3111 else if (qualifierType == "row_major")
3112 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003113 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003114 }
3115 else if (qualifierType == "column_major")
3116 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003117 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003118 }
3119 else if (qualifierType == "location")
3120 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003121 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
3122 "location requires an argument");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003123 }
3124 else
3125 {
3126 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003127 }
3128
Jamie Madilla5efff92013-06-06 11:56:47 -04003129 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003130}
3131
Martin Radev802abe02016-08-04 17:48:32 +03003132void TParseContext::parseLocalSize(const TString &qualifierType,
3133 const TSourceLoc &qualifierTypeLine,
3134 int intValue,
3135 const TSourceLoc &intValueLine,
3136 const std::string &intValueString,
3137 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03003138 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03003139{
Olli Etuaho856c4972016-08-08 11:38:39 +03003140 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03003141 if (intValue < 1)
3142 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03003143 std::string errorMessage = std::string(getWorkGroupSizeString(index)) + " must be positive";
Martin Radev802abe02016-08-04 17:48:32 +03003144 error(intValueLine, "out of range:", intValueString.c_str(), errorMessage.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003145 }
3146 (*localSize)[index] = intValue;
3147}
3148
Jamie Madillb98c3a82015-07-23 14:26:04 -04003149TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3150 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003151 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303152 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003153{
Martin Radev802abe02016-08-04 17:48:32 +03003154 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003155
Martin Radev802abe02016-08-04 17:48:32 +03003156 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003157
Martin Radev802abe02016-08-04 17:48:32 +03003158 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003159 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003160 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003161 if (intValue < 0)
3162 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003163 error(intValueLine, "out of range:", intValueString.c_str(),
3164 "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003165 }
3166 else
3167 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003168 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003169 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003170 }
Martin Radev802abe02016-08-04 17:48:32 +03003171 else if (qualifierType == "local_size_x")
3172 {
3173 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
3174 &qualifier.localSize);
3175 }
3176 else if (qualifierType == "local_size_y")
3177 {
3178 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
3179 &qualifier.localSize);
3180 }
3181 else if (qualifierType == "local_size_z")
3182 {
3183 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
3184 &qualifier.localSize);
3185 }
3186 else
3187 {
3188 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003189 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003190
Jamie Madilla5efff92013-06-06 11:56:47 -04003191 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003192}
3193
Jamie Madillb98c3a82015-07-23 14:26:04 -04003194TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03003195 TLayoutQualifier rightQualifier,
3196 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003197{
Martin Radevc28888b2016-07-22 15:27:42 +03003198 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
3199 &mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003200}
3201
Martin Radev70866b82016-07-22 15:27:42 +03003202TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
3203 const TTypeQualifierBuilder &typeQualifierBuilder,
3204 TPublicType *typeSpecifier,
3205 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003206{
Martin Radevc28888b2016-07-22 15:27:42 +03003207 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(
3208 &mDiagnostics, AreTypeQualifierChecksRelaxed(mShaderVersion));
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003209
Martin Radev70866b82016-07-22 15:27:42 +03003210 typeSpecifier->qualifier = typeQualifier.qualifier;
3211 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
3212 typeSpecifier->invariant = typeQualifier.invariant;
3213 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05303214 {
Martin Radev70866b82016-07-22 15:27:42 +03003215 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003216 }
Martin Radev70866b82016-07-22 15:27:42 +03003217 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003218}
3219
Jamie Madillb98c3a82015-07-23 14:26:04 -04003220TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3221 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003222{
Martin Radev4a9cd802016-09-01 16:51:51 +03003223 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
3224 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03003225
Martin Radev4a9cd802016-09-01 16:51:51 +03003226 checkIsNonVoid(typeSpecifier.getLine(), (*fieldList)[0]->name(), typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003227
Martin Radev4a9cd802016-09-01 16:51:51 +03003228 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003229
Arun Patole7e7e68d2015-05-22 12:02:25 +05303230 for (unsigned int i = 0; i < fieldList->size(); ++i)
3231 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003232 //
3233 // Careful not to replace already known aspects of type, like array-ness
3234 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303235 TType *type = (*fieldList)[i]->type();
Martin Radev4a9cd802016-09-01 16:51:51 +03003236 type->setBasicType(typeSpecifier.getBasicType());
3237 type->setPrimarySize(typeSpecifier.getPrimarySize());
3238 type->setSecondarySize(typeSpecifier.getSecondarySize());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003239 type->setPrecision(typeSpecifier.precision);
3240 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003241 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
Martin Radev70866b82016-07-22 15:27:42 +03003242 type->setInvariant(typeSpecifier.invariant);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003243
3244 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303245 if (type->isArray())
3246 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003247 checkIsValidTypeForArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003248 }
3249 if (typeSpecifier.array)
Olli Etuaho856c4972016-08-08 11:38:39 +03003250 type->setArraySize(static_cast<unsigned int>(typeSpecifier.arraySize));
Martin Radev4a9cd802016-09-01 16:51:51 +03003251 if (typeSpecifier.getUserDef())
Arun Patole7e7e68d2015-05-22 12:02:25 +05303252 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003253 type->setStruct(typeSpecifier.getUserDef()->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003254 }
3255
Martin Radev4a9cd802016-09-01 16:51:51 +03003256 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*fieldList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003257 }
3258
Jamie Madill98493dd2013-07-08 14:39:03 -04003259 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003260}
3261
Martin Radev4a9cd802016-09-01 16:51:51 +03003262TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
3263 const TSourceLoc &nameLine,
3264 const TString *structName,
3265 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003266{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303267 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003268 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003269
Jamie Madill9b820842015-02-12 10:40:10 -05003270 // Store a bool in the struct if we're at global scope, to allow us to
3271 // skip the local struct scoping workaround in HLSL.
Jamie Madillb960cc42015-02-12 15:33:20 +00003272 structure->setUniqueId(TSymbolTable::nextUniqueId());
Jamie Madill9b820842015-02-12 10:40:10 -05003273 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003274
Jamie Madill98493dd2013-07-08 14:39:03 -04003275 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003276 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003277 checkIsNotReserved(nameLine, *structName);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303278 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
3279 if (!symbolTable.declare(userTypeDef))
3280 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003281 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003282 }
3283 }
3284
3285 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04003286 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003287 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003288 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04003289 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003290 switch (qualifier)
3291 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003292 case EvqGlobal:
3293 case EvqTemporary:
3294 break;
3295 default:
3296 error(field.line(), "invalid qualifier on struct member",
3297 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003298 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003299 }
Martin Radev70866b82016-07-22 15:27:42 +03003300 if (field.type()->isInvariant())
3301 {
3302 error(field.line(), "invalid qualifier on struct member", "invariant");
3303 }
3304
3305 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003306 }
3307
Martin Radev4a9cd802016-09-01 16:51:51 +03003308 TTypeSpecifierNonArray typeSpecifierNonArray;
3309 typeSpecifierNonArray.initialize(EbtStruct, structLine);
3310 typeSpecifierNonArray.userDef = structureType;
3311 typeSpecifierNonArray.isStructSpecifier = true;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003312 exitStructDeclaration();
3313
Martin Radev4a9cd802016-09-01 16:51:51 +03003314 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003315}
3316
Jamie Madillb98c3a82015-07-23 14:26:04 -04003317TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
3318 TIntermAggregate *statementList,
3319 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02003320{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003321 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003322 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02003323 init->isVector())
3324 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003325 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
3326 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003327 return nullptr;
3328 }
3329
Olli Etuahoac5274d2015-02-20 10:19:08 +02003330 if (statementList)
3331 {
3332 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
3333 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02003334 return nullptr;
3335 }
3336 }
3337
Olli Etuahoa3a36662015-02-17 13:46:51 +02003338 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3339 if (node == nullptr)
3340 {
3341 error(loc, "erroneous switch statement", "switch");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003342 return nullptr;
3343 }
3344 return node;
3345}
3346
3347TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3348{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003349 if (mSwitchNestingLevel == 0)
3350 {
3351 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003352 return nullptr;
3353 }
3354 if (condition == nullptr)
3355 {
3356 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003357 return nullptr;
3358 }
3359 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04003360 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02003361 {
3362 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003363 }
3364 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003365 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
3366 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
3367 // fold in case labels.
3368 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02003369 {
3370 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003371 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003372 TIntermCase *node = intermediate.addCase(condition, loc);
3373 if (node == nullptr)
3374 {
3375 error(loc, "erroneous case statement", "case");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003376 return nullptr;
3377 }
3378 return node;
3379}
3380
3381TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3382{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003383 if (mSwitchNestingLevel == 0)
3384 {
3385 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003386 return nullptr;
3387 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003388 TIntermCase *node = intermediate.addCase(nullptr, loc);
3389 if (node == nullptr)
3390 {
3391 error(loc, "erroneous default statement", "default");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003392 return nullptr;
3393 }
3394 return node;
3395}
3396
Jamie Madillb98c3a82015-07-23 14:26:04 -04003397TIntermTyped *TParseContext::createUnaryMath(TOperator op,
3398 TIntermTyped *child,
3399 const TSourceLoc &loc,
3400 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003401{
3402 if (child == nullptr)
3403 {
3404 return nullptr;
3405 }
3406
3407 switch (op)
3408 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003409 case EOpLogicalNot:
3410 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
3411 child->isVector())
3412 {
3413 return nullptr;
3414 }
3415 break;
3416 case EOpBitwiseNot:
3417 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3418 child->isMatrix() || child->isArray())
3419 {
3420 return nullptr;
3421 }
3422 break;
3423 case EOpPostIncrement:
3424 case EOpPreIncrement:
3425 case EOpPostDecrement:
3426 case EOpPreDecrement:
3427 case EOpNegative:
3428 case EOpPositive:
3429 if (child->getBasicType() == EbtStruct || child->getBasicType() == EbtBool ||
Olli Etuaho558b0382016-08-26 17:54:34 +03003430 child->isArray() || IsSampler(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04003431 {
3432 return nullptr;
3433 }
3434 // Operators for built-ins are already type checked against their prototype.
3435 default:
3436 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003437 }
3438
Olli Etuahof6c694b2015-03-26 14:50:53 +02003439 return intermediate.addUnaryMath(op, child, loc, funcReturnType);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003440}
3441
Olli Etuaho09b22472015-02-11 11:47:26 +02003442TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3443{
Olli Etuahof6c694b2015-03-26 14:50:53 +02003444 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003445 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003446 {
3447 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003448 return child;
3449 }
3450 return node;
3451}
3452
Jamie Madillb98c3a82015-07-23 14:26:04 -04003453TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
3454 TIntermTyped *child,
3455 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003456{
Olli Etuaho856c4972016-08-08 11:38:39 +03003457 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02003458 return addUnaryMath(op, child, loc);
3459}
3460
Jamie Madillb98c3a82015-07-23 14:26:04 -04003461bool TParseContext::binaryOpCommonCheck(TOperator op,
3462 TIntermTyped *left,
3463 TIntermTyped *right,
3464 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003465{
Olli Etuaho244be012016-08-18 15:26:02 +03003466 if (left->getType().getStruct() || right->getType().getStruct())
3467 {
3468 switch (op)
3469 {
3470 case EOpIndexDirectStruct:
3471 ASSERT(left->getType().getStruct());
3472 break;
3473 case EOpEqual:
3474 case EOpNotEqual:
3475 case EOpAssign:
3476 case EOpInitialize:
3477 if (left->getType() != right->getType())
3478 {
3479 return false;
3480 }
3481 break;
3482 default:
3483 error(loc, "Invalid operation for structs", GetOperatorString(op));
3484 return false;
3485 }
3486 }
3487
Olli Etuahod6b14282015-03-17 14:31:35 +02003488 if (left->isArray() || right->isArray())
3489 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003490 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003491 {
3492 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3493 return false;
3494 }
3495
3496 if (left->isArray() != right->isArray())
3497 {
3498 error(loc, "array / non-array mismatch", GetOperatorString(op));
3499 return false;
3500 }
3501
3502 switch (op)
3503 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003504 case EOpEqual:
3505 case EOpNotEqual:
3506 case EOpAssign:
3507 case EOpInitialize:
3508 break;
3509 default:
3510 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3511 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02003512 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003513 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003514 if (left->getArraySize() != right->getArraySize())
3515 {
3516 error(loc, "array size mismatch", GetOperatorString(op));
3517 return false;
3518 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003519 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003520
3521 // Check ops which require integer / ivec parameters
3522 bool isBitShift = false;
3523 switch (op)
3524 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003525 case EOpBitShiftLeft:
3526 case EOpBitShiftRight:
3527 case EOpBitShiftLeftAssign:
3528 case EOpBitShiftRightAssign:
3529 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3530 // check that the basic type is an integer type.
3531 isBitShift = true;
3532 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3533 {
3534 return false;
3535 }
3536 break;
3537 case EOpBitwiseAnd:
3538 case EOpBitwiseXor:
3539 case EOpBitwiseOr:
3540 case EOpBitwiseAndAssign:
3541 case EOpBitwiseXorAssign:
3542 case EOpBitwiseOrAssign:
3543 // It is enough to check the type of only one operand, since later it
3544 // is checked that the operand types match.
3545 if (!IsInteger(left->getBasicType()))
3546 {
3547 return false;
3548 }
3549 break;
3550 default:
3551 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003552 }
3553
3554 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3555 // So the basic type should usually match.
3556 if (!isBitShift && left->getBasicType() != right->getBasicType())
3557 {
3558 return false;
3559 }
3560
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003561 // Check that:
3562 // 1. Type sizes match exactly on ops that require that.
3563 // 2. Restrictions for structs that contain arrays or samplers are respected.
3564 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003565 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003566 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003567 case EOpAssign:
3568 case EOpInitialize:
3569 case EOpEqual:
3570 case EOpNotEqual:
3571 // ESSL 1.00 sections 5.7, 5.8, 5.9
3572 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
3573 {
3574 error(loc, "undefined operation for structs containing arrays",
3575 GetOperatorString(op));
3576 return false;
3577 }
3578 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3579 // we interpret the spec so that this extends to structs containing samplers,
3580 // similarly to ESSL 1.00 spec.
3581 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
3582 left->getType().isStructureContainingSamplers())
3583 {
3584 error(loc, "undefined operation for structs containing samplers",
3585 GetOperatorString(op));
3586 return false;
3587 }
3588 case EOpLessThan:
3589 case EOpGreaterThan:
3590 case EOpLessThanEqual:
3591 case EOpGreaterThanEqual:
3592 if ((left->getNominalSize() != right->getNominalSize()) ||
3593 (left->getSecondarySize() != right->getSecondarySize()))
3594 {
3595 return false;
3596 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003597 break;
3598 case EOpAdd:
3599 case EOpSub:
3600 case EOpDiv:
3601 case EOpIMod:
3602 case EOpBitShiftLeft:
3603 case EOpBitShiftRight:
3604 case EOpBitwiseAnd:
3605 case EOpBitwiseXor:
3606 case EOpBitwiseOr:
3607 case EOpAddAssign:
3608 case EOpSubAssign:
3609 case EOpDivAssign:
3610 case EOpIModAssign:
3611 case EOpBitShiftLeftAssign:
3612 case EOpBitShiftRightAssign:
3613 case EOpBitwiseAndAssign:
3614 case EOpBitwiseXorAssign:
3615 case EOpBitwiseOrAssign:
3616 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
3617 {
3618 return false;
3619 }
3620
3621 // Are the sizes compatible?
3622 if (left->getNominalSize() != right->getNominalSize() ||
3623 left->getSecondarySize() != right->getSecondarySize())
3624 {
3625 // If the nominal sizes of operands do not match:
3626 // One of them must be a scalar.
3627 if (!left->isScalar() && !right->isScalar())
3628 return false;
3629
3630 // In the case of compound assignment other than multiply-assign,
3631 // the right side needs to be a scalar. Otherwise a vector/matrix
3632 // would be assigned to a scalar. A scalar can't be shifted by a
3633 // vector either.
3634 if (!right->isScalar() &&
3635 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
3636 return false;
3637 }
3638 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003639 default:
3640 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003641 }
3642
Olli Etuahod6b14282015-03-17 14:31:35 +02003643 return true;
3644}
3645
Olli Etuaho1dded802016-08-18 18:13:13 +03003646bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
3647 const TType &left,
3648 const TType &right)
3649{
3650 switch (op)
3651 {
3652 case EOpMul:
3653 case EOpMulAssign:
3654 return left.getNominalSize() == right.getNominalSize() &&
3655 left.getSecondarySize() == right.getSecondarySize();
3656 case EOpVectorTimesScalar:
3657 return true;
3658 case EOpVectorTimesScalarAssign:
3659 ASSERT(!left.isMatrix() && !right.isMatrix());
3660 return left.isVector() && !right.isVector();
3661 case EOpVectorTimesMatrix:
3662 return left.getNominalSize() == right.getRows();
3663 case EOpVectorTimesMatrixAssign:
3664 ASSERT(!left.isMatrix() && right.isMatrix());
3665 return left.isVector() && left.getNominalSize() == right.getRows() &&
3666 left.getNominalSize() == right.getCols();
3667 case EOpMatrixTimesVector:
3668 return left.getCols() == right.getNominalSize();
3669 case EOpMatrixTimesScalar:
3670 return true;
3671 case EOpMatrixTimesScalarAssign:
3672 ASSERT(left.isMatrix() && !right.isMatrix());
3673 return !right.isVector();
3674 case EOpMatrixTimesMatrix:
3675 return left.getCols() == right.getRows();
3676 case EOpMatrixTimesMatrixAssign:
3677 ASSERT(left.isMatrix() && right.isMatrix());
3678 // We need to check two things:
3679 // 1. The matrix multiplication step is valid.
3680 // 2. The result will have the same number of columns as the lvalue.
3681 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
3682
3683 default:
3684 UNREACHABLE();
3685 return false;
3686 }
3687}
3688
Jamie Madillb98c3a82015-07-23 14:26:04 -04003689TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
3690 TIntermTyped *left,
3691 TIntermTyped *right,
3692 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02003693{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003694 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003695 return nullptr;
3696
Olli Etuahofc1806e2015-03-17 13:03:11 +02003697 switch (op)
3698 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003699 case EOpEqual:
3700 case EOpNotEqual:
3701 break;
3702 case EOpLessThan:
3703 case EOpGreaterThan:
3704 case EOpLessThanEqual:
3705 case EOpGreaterThanEqual:
Olli Etuaho244be012016-08-18 15:26:02 +03003706 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3707 !right->getType().getStruct());
3708 if (left->isMatrix() || left->isVector())
Jamie Madillb98c3a82015-07-23 14:26:04 -04003709 {
3710 return nullptr;
3711 }
3712 break;
3713 case EOpLogicalOr:
3714 case EOpLogicalXor:
3715 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03003716 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3717 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003718 if (left->getBasicType() != EbtBool || left->isMatrix() || left->isVector())
3719 {
3720 return nullptr;
3721 }
3722 break;
3723 case EOpAdd:
3724 case EOpSub:
3725 case EOpDiv:
3726 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03003727 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3728 !right->getType().getStruct());
3729 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04003730 {
3731 return nullptr;
3732 }
3733 break;
3734 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03003735 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3736 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003737 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03003738 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04003739 {
3740 return nullptr;
3741 }
3742 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003743 default:
3744 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003745 }
3746
Olli Etuaho1dded802016-08-18 18:13:13 +03003747 if (op == EOpMul)
3748 {
3749 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
3750 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
3751 {
3752 return nullptr;
3753 }
3754 }
3755
Olli Etuaho3fdec912016-08-18 15:08:06 +03003756 TIntermBinary *node = new TIntermBinary(op, left, right);
3757 node->setLine(loc);
3758
Olli Etuaho3fdec912016-08-18 15:08:06 +03003759 // See if we can fold constants.
3760 TIntermTyped *foldedNode = node->fold(&mDiagnostics);
3761 if (foldedNode)
3762 return foldedNode;
3763
3764 return node;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003765}
3766
Jamie Madillb98c3a82015-07-23 14:26:04 -04003767TIntermTyped *TParseContext::addBinaryMath(TOperator op,
3768 TIntermTyped *left,
3769 TIntermTyped *right,
3770 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003771{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003772 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003773 if (node == 0)
3774 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003775 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3776 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003777 return left;
3778 }
3779 return node;
3780}
3781
Jamie Madillb98c3a82015-07-23 14:26:04 -04003782TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
3783 TIntermTyped *left,
3784 TIntermTyped *right,
3785 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003786{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003787 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003788 if (node == 0)
3789 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003790 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3791 right->getCompleteString());
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003792 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02003793 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003794 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
3795 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003796 }
3797 return node;
3798}
3799
Jamie Madillb98c3a82015-07-23 14:26:04 -04003800TIntermTyped *TParseContext::createAssign(TOperator op,
3801 TIntermTyped *left,
3802 TIntermTyped *right,
3803 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003804{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003805 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003806 {
Olli Etuaho1dded802016-08-18 18:13:13 +03003807 if (op == EOpMulAssign)
3808 {
3809 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
3810 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
3811 {
3812 return nullptr;
3813 }
3814 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03003815 TIntermBinary *node = new TIntermBinary(op, left, right);
3816 node->setLine(loc);
3817
Olli Etuaho3fdec912016-08-18 15:08:06 +03003818 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02003819 }
3820 return nullptr;
3821}
3822
Jamie Madillb98c3a82015-07-23 14:26:04 -04003823TIntermTyped *TParseContext::addAssign(TOperator op,
3824 TIntermTyped *left,
3825 TIntermTyped *right,
3826 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003827{
3828 TIntermTyped *node = createAssign(op, left, right, loc);
3829 if (node == nullptr)
3830 {
3831 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02003832 return left;
3833 }
3834 return node;
3835}
3836
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003837TIntermTyped *TParseContext::addComma(TIntermTyped *left,
3838 TIntermTyped *right,
3839 const TSourceLoc &loc)
3840{
Corentin Wallez0d959252016-07-12 17:26:32 -04003841 // WebGL2 section 5.26, the following results in an error:
3842 // "Sequence operator applied to void, arrays, or structs containing arrays"
3843 if (mShaderSpec == SH_WEBGL2_SPEC && (left->isArray() || left->getBasicType() == EbtVoid ||
3844 left->getType().isStructureContainingArrays() ||
3845 right->isArray() || right->getBasicType() == EbtVoid ||
3846 right->getType().isStructureContainingArrays()))
3847 {
3848 error(loc,
3849 "sequence operator is not allowed for void, arrays, or structs containing arrays",
3850 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04003851 }
3852
Olli Etuaho15200042015-11-04 16:56:31 +02003853 return intermediate.addComma(left, right, loc, mShaderVersion);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003854}
3855
Olli Etuaho49300862015-02-20 14:54:49 +02003856TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3857{
3858 switch (op)
3859 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003860 case EOpContinue:
3861 if (mLoopNestingLevel <= 0)
3862 {
3863 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04003864 }
3865 break;
3866 case EOpBreak:
3867 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
3868 {
3869 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04003870 }
3871 break;
3872 case EOpReturn:
3873 if (mCurrentFunctionType->getBasicType() != EbtVoid)
3874 {
3875 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04003876 }
3877 break;
3878 default:
3879 // No checks for discard
3880 break;
Olli Etuaho49300862015-02-20 14:54:49 +02003881 }
3882 return intermediate.addBranch(op, loc);
3883}
3884
Jamie Madillb98c3a82015-07-23 14:26:04 -04003885TIntermBranch *TParseContext::addBranch(TOperator op,
3886 TIntermTyped *returnValue,
3887 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02003888{
3889 ASSERT(op == EOpReturn);
3890 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003891 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02003892 {
3893 error(loc, "void function cannot return a value", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02003894 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003895 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02003896 {
3897 error(loc, "function return is not matching type:", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02003898 }
3899 return intermediate.addBranch(op, returnValue, loc);
3900}
3901
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003902void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
3903{
3904 ASSERT(!functionCall->isUserDefined());
3905 const TString &name = functionCall->getName();
3906 TIntermNode *offset = nullptr;
3907 TIntermSequence *arguments = functionCall->getSequence();
3908 if (name.compare(0, 16, "texelFetchOffset") == 0 ||
3909 name.compare(0, 16, "textureLodOffset") == 0 ||
3910 name.compare(0, 20, "textureProjLodOffset") == 0 ||
3911 name.compare(0, 17, "textureGradOffset") == 0 ||
3912 name.compare(0, 21, "textureProjGradOffset") == 0)
3913 {
3914 offset = arguments->back();
3915 }
3916 else if (name.compare(0, 13, "textureOffset") == 0 ||
3917 name.compare(0, 17, "textureProjOffset") == 0)
3918 {
3919 // A bias parameter might follow the offset parameter.
3920 ASSERT(arguments->size() >= 3);
3921 offset = (*arguments)[2];
3922 }
3923 if (offset != nullptr)
3924 {
3925 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
3926 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
3927 {
3928 TString unmangledName = TFunction::unmangleName(name);
3929 error(functionCall->getLine(), "Texture offset must be a constant expression",
3930 unmangledName.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003931 }
3932 else
3933 {
3934 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
3935 size_t size = offsetConstantUnion->getType().getObjectSize();
3936 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
3937 for (size_t i = 0u; i < size; ++i)
3938 {
3939 int offsetValue = values[i].getIConst();
3940 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
3941 {
3942 std::stringstream tokenStream;
3943 tokenStream << offsetValue;
3944 std::string token = tokenStream.str();
3945 error(offset->getLine(), "Texture offset value out of valid range",
3946 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003947 }
3948 }
3949 }
3950 }
3951}
3952
Jamie Madillb98c3a82015-07-23 14:26:04 -04003953TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
3954 TIntermNode *paramNode,
3955 TIntermNode *thisNode,
3956 const TSourceLoc &loc,
3957 bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003958{
Jamie Madillb98c3a82015-07-23 14:26:04 -04003959 *fatalError = false;
3960 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003961 TIntermTyped *callNode = nullptr;
3962
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003963 if (thisNode != nullptr)
3964 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003965 TConstantUnion *unionArray = new TConstantUnion[1];
Jamie Madillb98c3a82015-07-23 14:26:04 -04003966 int arraySize = 0;
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003967 TIntermTyped *typedThis = thisNode->getAsTyped();
3968 if (fnCall->getName() != "length")
3969 {
3970 error(loc, "invalid method", fnCall->getName().c_str());
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003971 }
3972 else if (paramNode != nullptr)
3973 {
3974 error(loc, "method takes no parameters", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003975 }
3976 else if (typedThis == nullptr || !typedThis->isArray())
3977 {
3978 error(loc, "length can only be called on arrays", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003979 }
3980 else
3981 {
Olli Etuaho96e67382015-04-23 14:27:02 +03003982 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03003983 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003984 {
Olli Etuaho39282e12015-04-23 15:41:48 +03003985 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003986 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03003987 // (func()).length()
3988 // (int[3](0, 1, 2)).length()
Jamie Madillb98c3a82015-07-23 14:26:04 -04003989 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
3990 // expression.
3991 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
3992 // spec section 5.9 which allows "An array, vector or matrix expression with the
3993 // length method applied".
3994 error(loc, "length can only be called on array names, not on array expressions",
3995 "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003996 }
3997 }
Olli Etuaho96e67382015-04-23 14:27:02 +03003998 unionArray->setIConst(arraySize);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003999 callNode =
4000 intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004001 }
4002 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004003 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004004 // Then this should be a constructor.
Olli Etuaho856c4972016-08-08 11:38:39 +03004005 callNode = addConstructor(paramNode, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004006 }
4007 else
4008 {
4009 //
4010 // Not a constructor. Find it in the symbol table.
4011 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05304012 const TFunction *fnCandidate;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004013 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004014 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004015 if (fnCandidate)
4016 {
4017 //
4018 // A declared function.
4019 //
Olli Etuaho383b7912016-08-05 11:22:59 +03004020 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004021 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004022 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004023 }
4024 op = fnCandidate->getBuiltInOp();
4025 if (builtIn && op != EOpNull)
4026 {
4027 //
4028 // A function call mapped to a built-in operation.
4029 //
4030 if (fnCandidate->getParamCount() == 1)
4031 {
4032 //
4033 // Treat it like a built-in unary operator.
4034 //
Olli Etuaho15c2ac32015-11-09 15:51:43 +02004035 TIntermAggregate *paramAgg = paramNode->getAsAggregate();
4036 paramNode = paramAgg->getSequence()->front();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004037 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc,
4038 &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004039 if (callNode == nullptr)
4040 {
4041 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004042 extraInfoStream
4043 << "built in unary operator function. Type: "
4044 << static_cast<TIntermTyped *>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004045 std::string extraInfo = extraInfoStream.str();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004046 error(paramNode->getLine(), " wrong operand type", "Internal Error",
4047 extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004048 *fatalError = true;
4049 return nullptr;
4050 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004051 }
4052 else
4053 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004054 TIntermAggregate *aggregate =
4055 intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004056 aggregate->setType(fnCandidate->getReturnType());
4057 aggregate->setPrecisionFromChildren();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02004058 if (aggregate->areChildrenConstQualified())
4059 {
4060 aggregate->getTypePointer()->setQualifier(EvqConst);
4061 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004062
4063 // Some built-in functions have out parameters too.
4064 functionCallLValueErrorCheck(fnCandidate, aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05304065
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004066 // See if we can constant fold a built-in. Note that this may be possible even
4067 // if it is not const-qualified.
Olli Etuahob43846e2015-06-02 18:18:57 +03004068 TIntermTyped *foldedNode = intermediate.foldAggregateBuiltIn(aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05304069 if (foldedNode)
4070 {
Arun Patole274f0702015-05-05 13:33:30 +05304071 callNode = foldedNode;
4072 }
Olli Etuahob43846e2015-06-02 18:18:57 +03004073 else
4074 {
4075 callNode = aggregate;
4076 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004077 }
4078 }
4079 else
4080 {
4081 // This is a real function call
Jamie Madillb98c3a82015-07-23 14:26:04 -04004082 TIntermAggregate *aggregate =
4083 intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004084 aggregate->setType(fnCandidate->getReturnType());
4085
Jamie Madillb98c3a82015-07-23 14:26:04 -04004086 // this is how we know whether the given function is a builtIn function or a user
4087 // defined function
4088 // if builtIn == false, it's a userDefined -> could be an overloaded
4089 // builtIn function also
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004090 // if builtIn == true, it's definitely a builtIn function with EOpNull
4091 if (!builtIn)
4092 aggregate->setUserDefined();
4093 aggregate->setName(fnCandidate->getMangledName());
Corentin Wallez71d147f2015-02-11 11:15:24 -08004094 aggregate->setFunctionId(fnCandidate->getUniqueId());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004095
4096 // This needs to happen after the name is set
4097 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004098 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004099 aggregate->setBuiltInFunctionPrecision();
4100
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004101 checkTextureOffsetConst(aggregate);
4102 }
4103
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004104 callNode = aggregate;
4105
4106 functionCallLValueErrorCheck(fnCandidate, aggregate);
4107 }
4108 }
4109 else
4110 {
4111 // error message was put out by findFunction()
4112 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004113 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004114 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004115 callNode = intermediate.addConstantUnion(unionArray,
4116 TType(EbtFloat, EbpUndefined, EvqConst), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004117 }
4118 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004119 return callNode;
4120}
4121
Jamie Madillb98c3a82015-07-23 14:26:04 -04004122TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
4123 TIntermTyped *trueBlock,
4124 TIntermTyped *falseBlock,
Olli Etuaho52901742015-04-15 13:42:45 +03004125 const TSourceLoc &loc)
4126{
Olli Etuaho856c4972016-08-08 11:38:39 +03004127 checkIsScalarBool(loc, cond);
Olli Etuaho52901742015-04-15 13:42:45 +03004128
4129 if (trueBlock->getType() != falseBlock->getType())
4130 {
4131 binaryOpError(loc, ":", trueBlock->getCompleteString(), falseBlock->getCompleteString());
Olli Etuaho52901742015-04-15 13:42:45 +03004132 return falseBlock;
4133 }
Olli Etuahoa2d53032015-04-15 14:14:44 +03004134 // ESSL1 sections 5.2 and 5.7:
4135 // ESSL3 section 5.7:
4136 // Ternary operator is not among the operators allowed for structures/arrays.
4137 if (trueBlock->isArray() || trueBlock->getBasicType() == EbtStruct)
4138 {
4139 error(loc, "ternary operator is not allowed for structures or arrays", ":");
Olli Etuahoa2d53032015-04-15 14:14:44 +03004140 return falseBlock;
4141 }
Corentin Wallez0d959252016-07-12 17:26:32 -04004142 // WebGL2 section 5.26, the following results in an error:
4143 // "Ternary operator applied to void, arrays, or structs containing arrays"
4144 if (mShaderSpec == SH_WEBGL2_SPEC && trueBlock->getBasicType() == EbtVoid)
4145 {
4146 error(loc, "ternary operator is not allowed for void", ":");
Corentin Wallez0d959252016-07-12 17:26:32 -04004147 return falseBlock;
4148 }
4149
Olli Etuaho52901742015-04-15 13:42:45 +03004150 return intermediate.addSelection(cond, trueBlock, falseBlock, loc);
4151}
Olli Etuaho49300862015-02-20 14:54:49 +02004152
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004153//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004154// Parse an array of strings using yyparse.
4155//
4156// Returns 0 for success.
4157//
Jamie Madillb98c3a82015-07-23 14:26:04 -04004158int PaParseStrings(size_t count,
4159 const char *const string[],
4160 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05304161 TParseContext *context)
4162{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004163 if ((count == 0) || (string == NULL))
4164 return 1;
4165
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004166 if (glslang_initialize(context))
4167 return 1;
4168
alokp@chromium.org408c45e2012-04-05 15:54:43 +00004169 int error = glslang_scan(count, string, length, context);
4170 if (!error)
4171 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004172
alokp@chromium.org73bc2982012-06-19 18:48:05 +00004173 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00004174
alokp@chromium.org6b495712012-06-29 00:06:58 +00004175 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004176}