blob: 70c0c27cf360457def67e5a96753896e136d2635 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Jamie Madill6b9cb252013-10-17 10:45:47 -04007#include "compiler/translator/ParseContext.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +00008
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00009#include <stdarg.h>
apatrick@chromium.org8187fa82010-06-15 22:09:28 +000010#include <stdio.h>
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000011
daniel@transgaming.comb401a922012-10-26 18:58:24 +000012#include "compiler/preprocessor/SourceLocation.h"
Dmitry Skiba01971112015-07-10 14:54:00 -040013#include "compiler/translator/Cache.h"
Olli Etuahoac5274d2015-02-20 10:19:08 +020014#include "compiler/translator/glslang.h"
15#include "compiler/translator/ValidateSwitch.h"
Olli Etuahob0c645e2015-05-12 14:25:36 +030016#include "compiler/translator/ValidateGlobalInitializer.h"
Olli Etuaho37ad4742015-04-27 13:18:50 +030017#include "compiler/translator/util.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000018
alokp@chromium.org8b851c62012-06-15 16:25:11 +000019///////////////////////////////////////////////////////////////////////
20//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000021// Sub- vector and matrix fields
22//
23////////////////////////////////////////////////////////////////////////
24
25//
26// Look at a '.' field selector string and change it into offsets
27// for a vector.
28//
Jamie Madillb98c3a82015-07-23 14:26:04 -040029bool TParseContext::parseVectorFields(const TString &compString,
30 int vecSize,
31 TVectorFields &fields,
Arun Patole7e7e68d2015-05-22 12:02:25 +053032 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000033{
Jamie Madillb98c3a82015-07-23 14:26:04 -040034 fields.num = (int)compString.size();
Arun Patole7e7e68d2015-05-22 12:02:25 +053035 if (fields.num > 4)
36 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000037 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000038 return false;
39 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000040
Jamie Madillb98c3a82015-07-23 14:26:04 -040041 enum
42 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000043 exyzw,
44 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +000045 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000046 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000047
Arun Patole7e7e68d2015-05-22 12:02:25 +053048 for (int i = 0; i < fields.num; ++i)
49 {
50 switch (compString[i])
51 {
Jamie Madillb98c3a82015-07-23 14:26:04 -040052 case 'x':
53 fields.offsets[i] = 0;
54 fieldSet[i] = exyzw;
55 break;
56 case 'r':
57 fields.offsets[i] = 0;
58 fieldSet[i] = ergba;
59 break;
60 case 's':
61 fields.offsets[i] = 0;
62 fieldSet[i] = estpq;
63 break;
64 case 'y':
65 fields.offsets[i] = 1;
66 fieldSet[i] = exyzw;
67 break;
68 case 'g':
69 fields.offsets[i] = 1;
70 fieldSet[i] = ergba;
71 break;
72 case 't':
73 fields.offsets[i] = 1;
74 fieldSet[i] = estpq;
75 break;
76 case 'z':
77 fields.offsets[i] = 2;
78 fieldSet[i] = exyzw;
79 break;
80 case 'b':
81 fields.offsets[i] = 2;
82 fieldSet[i] = ergba;
83 break;
84 case 'p':
85 fields.offsets[i] = 2;
86 fieldSet[i] = estpq;
87 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053088
Jamie Madillb98c3a82015-07-23 14:26:04 -040089 case 'w':
90 fields.offsets[i] = 3;
91 fieldSet[i] = exyzw;
92 break;
93 case 'a':
94 fields.offsets[i] = 3;
95 fieldSet[i] = ergba;
96 break;
97 case 'q':
98 fields.offsets[i] = 3;
99 fieldSet[i] = estpq;
100 break;
101 default:
102 error(line, "illegal vector field selection", compString.c_str());
103 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000104 }
105 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000106
Arun Patole7e7e68d2015-05-22 12:02:25 +0530107 for (int i = 0; i < fields.num; ++i)
108 {
109 if (fields.offsets[i] >= vecSize)
110 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400111 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000112 return false;
113 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000114
Arun Patole7e7e68d2015-05-22 12:02:25 +0530115 if (i > 0)
116 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400117 if (fieldSet[i] != fieldSet[i - 1])
Arun Patole7e7e68d2015-05-22 12:02:25 +0530118 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400119 error(line, "illegal - vector component fields not from the same set",
120 compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000121 return false;
122 }
123 }
124 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000125
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000126 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000127}
128
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000129///////////////////////////////////////////////////////////////////////
130//
131// Errors
132//
133////////////////////////////////////////////////////////////////////////
134
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000135
136//
137// Used by flex/bison to output all syntax and parsing errors.
138//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530139void TParseContext::error(const TSourceLoc &loc,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400140 const char *reason,
141 const char *token,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530142 const char *extraInfo)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000143{
Olli Etuaho1cc598f2016-08-18 13:50:30 +0300144 mDiagnostics.error(loc, reason, token, extraInfo);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000145}
146
Arun Patole7e7e68d2015-05-22 12:02:25 +0530147void TParseContext::warning(const TSourceLoc &loc,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400148 const char *reason,
149 const char *token,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530150 const char *extraInfo)
151{
Olli Etuaho1cc598f2016-08-18 13:50:30 +0300152 mDiagnostics.warning(loc, reason, token, extraInfo);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000153}
154
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200155void TParseContext::outOfRangeError(bool isError,
156 const TSourceLoc &loc,
157 const char *reason,
158 const char *token,
159 const char *extraInfo)
160{
161 if (isError)
162 {
163 error(loc, reason, token, extraInfo);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200164 }
165 else
166 {
167 warning(loc, reason, token, extraInfo);
168 }
169}
170
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000171//
172// Same error message for all places assignments don't work.
173//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530174void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000175{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000176 std::stringstream extraInfoStream;
177 extraInfoStream << "cannot convert from '" << right << "' to '" << left << "'";
178 std::string extraInfo = extraInfoStream.str();
179 error(line, "", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000180}
181
182//
183// Same error message for all places unary operations don't work.
184//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530185void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000186{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000187 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400188 extraInfoStream << "no operation '" << op << "' exists that takes an operand of type "
189 << operand << " (or there is no acceptable conversion)";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000190 std::string extraInfo = extraInfoStream.str();
191 error(line, " wrong operand type", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000192}
193
194//
195// Same error message for all binary operations don't work.
196//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400197void TParseContext::binaryOpError(const TSourceLoc &line,
198 const char *op,
199 TString left,
200 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000201{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000202 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400203 extraInfoStream << "no operation '" << op << "' exists that takes a left-hand operand of type '"
204 << left << "' and a right operand of type '" << right
205 << "' (or there is no acceptable conversion)";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000206 std::string extraInfo = extraInfoStream.str();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530207 error(line, " wrong operand types ", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000208}
209
Olli Etuaho856c4972016-08-08 11:38:39 +0300210void TParseContext::checkPrecisionSpecified(const TSourceLoc &line,
211 TPrecision precision,
212 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530213{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400214 if (!mChecksPrecisionErrors)
Olli Etuaho383b7912016-08-05 11:22:59 +0300215 return;
Martin Radev70866b82016-07-22 15:27:42 +0300216
217 if (precision != EbpUndefined && !SupportsPrecision(type))
218 {
219 error(line, "illegal type for precision qualifier", getBasicString(type));
220 }
221
Olli Etuaho183d7e22015-11-20 15:59:09 +0200222 if (precision == EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530223 {
Olli Etuaho183d7e22015-11-20 15:59:09 +0200224 switch (type)
225 {
226 case EbtFloat:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400227 error(line, "No precision specified for (float)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300228 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200229 case EbtInt:
230 case EbtUInt:
231 UNREACHABLE(); // there's always a predeclared qualifier
Jamie Madillb98c3a82015-07-23 14:26:04 -0400232 error(line, "No precision specified (int)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300233 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200234 default:
235 if (IsSampler(type))
236 {
237 error(line, "No precision specified (sampler)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300238 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200239 }
240 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000241 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000242}
243
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000244// Both test and if necessary, spit out an error, to see if the node is really
245// an l-value that can be operated on this way.
Olli Etuaho856c4972016-08-08 11:38:39 +0300246bool TParseContext::checkCanBeLValue(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000247{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400248 TIntermSymbol *symNode = node->getAsSymbolNode();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530249 TIntermBinary *binaryNode = node->getAsBinaryNode();
Olli Etuahob6fa0432016-09-28 16:28:05 +0100250 TIntermSwizzle *swizzleNode = node->getAsSwizzleNode();
251
252 if (swizzleNode)
253 {
254 bool ok = checkCanBeLValue(line, op, swizzleNode->getOperand());
255 if (ok && swizzleNode->hasDuplicateOffsets())
256 {
257 error(line, " l-value of swizzle cannot have duplicate components", op);
258 return false;
259 }
260 return ok;
261 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000262
Arun Patole7e7e68d2015-05-22 12:02:25 +0530263 if (binaryNode)
264 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400265 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530266 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400267 case EOpIndexDirect:
268 case EOpIndexIndirect:
269 case EOpIndexDirectStruct:
270 case EOpIndexDirectInterfaceBlock:
Olli Etuaho856c4972016-08-08 11:38:39 +0300271 return checkCanBeLValue(line, op, binaryNode->getLeft());
Jamie Madillb98c3a82015-07-23 14:26:04 -0400272 default:
273 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000274 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000275 error(line, " l-value required", op);
Olli Etuaho8a176262016-08-16 14:23:01 +0300276 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000277 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000278
Arun Patole7e7e68d2015-05-22 12:02:25 +0530279 const char *symbol = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000280 if (symNode != 0)
281 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000282
Arun Patole7e7e68d2015-05-22 12:02:25 +0530283 const char *message = 0;
284 switch (node->getQualifier())
285 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400286 case EvqConst:
287 message = "can't modify a const";
288 break;
289 case EvqConstReadOnly:
290 message = "can't modify a const";
291 break;
292 case EvqAttribute:
293 message = "can't modify an attribute";
294 break;
295 case EvqFragmentIn:
296 message = "can't modify an input";
297 break;
298 case EvqVertexIn:
299 message = "can't modify an input";
300 break;
301 case EvqUniform:
302 message = "can't modify a uniform";
303 break;
304 case EvqVaryingIn:
305 message = "can't modify a varying";
306 break;
307 case EvqFragCoord:
308 message = "can't modify gl_FragCoord";
309 break;
310 case EvqFrontFacing:
311 message = "can't modify gl_FrontFacing";
312 break;
313 case EvqPointCoord:
314 message = "can't modify gl_PointCoord";
315 break;
Martin Radevb0883602016-08-04 17:48:58 +0300316 case EvqNumWorkGroups:
317 message = "can't modify gl_NumWorkGroups";
318 break;
319 case EvqWorkGroupSize:
320 message = "can't modify gl_WorkGroupSize";
321 break;
322 case EvqWorkGroupID:
323 message = "can't modify gl_WorkGroupID";
324 break;
325 case EvqLocalInvocationID:
326 message = "can't modify gl_LocalInvocationID";
327 break;
328 case EvqGlobalInvocationID:
329 message = "can't modify gl_GlobalInvocationID";
330 break;
331 case EvqLocalInvocationIndex:
332 message = "can't modify gl_LocalInvocationIndex";
333 break;
Martin Radev802abe02016-08-04 17:48:32 +0300334 case EvqComputeIn:
335 message = "can't modify work group size variable";
336 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400337 default:
338 //
339 // Type that can't be written to?
340 //
341 if (node->getBasicType() == EbtVoid)
342 {
343 message = "can't modify void";
344 }
345 if (IsSampler(node->getBasicType()))
346 {
347 message = "can't modify a sampler";
348 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000349 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000350
Arun Patole7e7e68d2015-05-22 12:02:25 +0530351 if (message == 0 && binaryNode == 0 && symNode == 0)
352 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000353 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000354
Olli Etuaho8a176262016-08-16 14:23:01 +0300355 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000356 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000357
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000358 //
359 // Everything else is okay, no error.
360 //
361 if (message == 0)
Olli Etuaho8a176262016-08-16 14:23:01 +0300362 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000363
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000364 //
365 // If we get here, we have an error and a message.
366 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530367 if (symNode)
368 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000369 std::stringstream extraInfoStream;
370 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
371 std::string extraInfo = extraInfoStream.str();
372 error(line, " l-value required", op, extraInfo.c_str());
373 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530374 else
375 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000376 std::stringstream extraInfoStream;
377 extraInfoStream << "(" << message << ")";
378 std::string extraInfo = extraInfoStream.str();
379 error(line, " l-value required", op, extraInfo.c_str());
380 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000381
Olli Etuaho8a176262016-08-16 14:23:01 +0300382 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000383}
384
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000385// Both test, and if necessary spit out an error, to see if the node is really
386// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300387void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000388{
Olli Etuaho383b7912016-08-05 11:22:59 +0300389 if (node->getQualifier() != EvqConst)
390 {
391 error(node->getLine(), "constant expression required", "");
392 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000393}
394
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000395// Both test, and if necessary spit out an error, to see if the node is really
396// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300397void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000398{
Olli Etuaho383b7912016-08-05 11:22:59 +0300399 if (!node->isScalarInt())
400 {
401 error(node->getLine(), "integer expression required", token);
402 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000403}
404
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000405// Both test, and if necessary spit out an error, to see if we are currently
406// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800407bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000408{
Olli Etuaho856c4972016-08-08 11:38:39 +0300409 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300410 {
411 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800412 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300413 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800414 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000415}
416
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000417// For now, keep it simple: if it starts "gl_", it's reserved, independent
418// of scope. Except, if the symbol table is at the built-in push-level,
419// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000420// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
421// webgl shader.
Olli Etuaho856c4972016-08-08 11:38:39 +0300422bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000423{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530424 static const char *reservedErrMsg = "reserved built-in name";
425 if (!symbolTable.atBuiltInLevel())
426 {
427 if (identifier.compare(0, 3, "gl_") == 0)
428 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000429 error(line, reservedErrMsg, "gl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300430 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000431 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530432 if (IsWebGLBasedSpec(mShaderSpec))
433 {
434 if (identifier.compare(0, 6, "webgl_") == 0)
435 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000436 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300437 return false;
alokp@chromium.org613ef312010-07-21 18:54:22 +0000438 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530439 if (identifier.compare(0, 7, "_webgl_") == 0)
440 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000441 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300442 return false;
alokp@chromium.org613ef312010-07-21 18:54:22 +0000443 }
444 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530445 if (identifier.find("__") != TString::npos)
446 {
447 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400448 "identifiers containing two consecutive underscores (__) are reserved as "
449 "possible future keywords",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530450 identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300451 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000452 }
453 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000454
Olli Etuaho8a176262016-08-16 14:23:01 +0300455 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000456}
457
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000458// Make sure there is enough data provided to the constructor to build
459// something of the type of the constructor. Also returns the type of
460// the constructor.
Olli Etuaho856c4972016-08-08 11:38:39 +0300461bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
462 TIntermNode *argumentsNode,
463 const TFunction &function,
464 TOperator op,
465 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000466{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000467 bool constructingMatrix = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400468 switch (op)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530469 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400470 case EOpConstructMat2:
471 case EOpConstructMat2x3:
472 case EOpConstructMat2x4:
473 case EOpConstructMat3x2:
474 case EOpConstructMat3:
475 case EOpConstructMat3x4:
476 case EOpConstructMat4x2:
477 case EOpConstructMat4x3:
478 case EOpConstructMat4:
479 constructingMatrix = true;
480 break;
481 default:
482 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000483 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000484
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000485 //
486 // Note: It's okay to have too many components available, but not okay to have unused
487 // arguments. 'full' will go to true when enough args have been seen. If we loop
488 // again, there is an extra argument, so 'overfull' will become true.
489 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000490
Jamie Madillb98c3a82015-07-23 14:26:04 -0400491 size_t size = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400492 bool full = false;
493 bool overFull = false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000494 bool matrixInMatrix = false;
495 bool arrayArg = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530496 for (size_t i = 0; i < function.getParamCount(); ++i)
497 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700498 const TConstParameter &param = function.getParam(i);
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000499 size += param.type->getObjectSize();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530500
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000501 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000502 matrixInMatrix = true;
503 if (full)
504 overFull = true;
Olli Etuaho856c4972016-08-08 11:38:39 +0300505 if (op != EOpConstructStruct && !type.isArray() && size >= type.getObjectSize())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000506 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000507 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000508 arrayArg = true;
509 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530510
Olli Etuaho856c4972016-08-08 11:38:39 +0300511 if (type.isArray())
Olli Etuaho376f1b52015-04-13 13:23:41 +0300512 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300513 // The size of an unsized constructor should already have been determined.
514 ASSERT(!type.isUnsizedArray());
515 if (static_cast<size_t>(type.getArraySize()) != function.getParamCount())
Olli Etuaho376f1b52015-04-13 13:23:41 +0300516 {
517 error(line, "array constructor needs one argument per array element", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300518 return false;
Olli Etuaho376f1b52015-04-13 13:23:41 +0300519 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000520 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000521
Arun Patole7e7e68d2015-05-22 12:02:25 +0530522 if (arrayArg && op != EOpConstructStruct)
523 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000524 error(line, "constructing from a non-dereferenced array", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300525 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000526 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000527
Olli Etuaho856c4972016-08-08 11:38:39 +0300528 if (matrixInMatrix && !type.isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530529 {
530 if (function.getParamCount() != 1)
531 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400532 error(line, "constructing matrix from matrix can only take one argument",
533 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300534 return false;
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000535 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000536 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000537
Arun Patole7e7e68d2015-05-22 12:02:25 +0530538 if (overFull)
539 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000540 error(line, "too many arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300541 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000542 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530543
Olli Etuaho856c4972016-08-08 11:38:39 +0300544 if (op == EOpConstructStruct && !type.isArray() &&
545 type.getStruct()->fields().size() != function.getParamCount())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530546 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400547 error(line,
548 "Number of constructor parameters does not match the number of structure fields",
549 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300550 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000551 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000552
Olli Etuaho856c4972016-08-08 11:38:39 +0300553 if (!type.isMatrix() || !matrixInMatrix)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530554 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300555 if ((op != EOpConstructStruct && size != 1 && size < type.getObjectSize()) ||
556 (op == EOpConstructStruct && size < type.getObjectSize()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530557 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000558 error(line, "not enough data provided for construction", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300559 return false;
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000560 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000561 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000562
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200563 if (argumentsNode == nullptr)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530564 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200565 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300566 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000567 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200568
569 TIntermAggregate *argumentsAgg = argumentsNode->getAsAggregate();
570 for (TIntermNode *&argNode : *argumentsAgg->getSequence())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530571 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200572 TIntermTyped *argTyped = argNode->getAsTyped();
573 ASSERT(argTyped != nullptr);
574 if (op != EOpConstructStruct && IsSampler(argTyped->getBasicType()))
575 {
576 error(line, "cannot convert a sampler", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300577 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200578 }
579 if (argTyped->getBasicType() == EbtVoid)
580 {
581 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300582 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200583 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000584 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000585
Olli Etuaho856c4972016-08-08 11:38:39 +0300586 if (type.isArray())
587 {
588 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
589 // the array.
590 for (TIntermNode *&argNode : *argumentsAgg->getSequence())
591 {
592 const TType &argType = argNode->getAsTyped()->getType();
593 // It has already been checked that the argument is not an array.
594 ASSERT(!argType.isArray());
595 if (!argType.sameElementType(type))
596 {
597 error(line, "Array constructor argument has an incorrect type", "Error");
Olli Etuaho8a176262016-08-16 14:23:01 +0300598 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300599 }
600 }
601 }
602 else if (op == EOpConstructStruct)
603 {
604 const TFieldList &fields = type.getStruct()->fields();
605 TIntermSequence *args = argumentsAgg->getSequence();
606
607 for (size_t i = 0; i < fields.size(); i++)
608 {
609 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
610 {
611 error(line, "Structure constructor arguments do not match structure fields",
612 "Error");
Olli Etuaho8a176262016-08-16 14:23:01 +0300613 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300614 }
615 }
616 }
617
Olli Etuaho8a176262016-08-16 14:23:01 +0300618 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000619}
620
Jamie Madillb98c3a82015-07-23 14:26:04 -0400621// This function checks to see if a void variable has been declared and raise an error message for
622// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000623//
624// returns true in case of an error
625//
Olli Etuaho856c4972016-08-08 11:38:39 +0300626bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400627 const TString &identifier,
628 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000629{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300630 if (type == EbtVoid)
631 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000632 error(line, "illegal use of type 'void'", identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300633 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300634 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000635
Olli Etuaho8a176262016-08-16 14:23:01 +0300636 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000637}
638
Jamie Madillb98c3a82015-07-23 14:26:04 -0400639// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300640// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300641void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000642{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530643 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector())
644 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000645 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530646 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000647}
648
Jamie Madillb98c3a82015-07-23 14:26:04 -0400649// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300650// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300651void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000652{
Martin Radev4a9cd802016-09-01 16:51:51 +0300653 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530654 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000655 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530656 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000657}
658
Olli Etuaho856c4972016-08-08 11:38:39 +0300659bool TParseContext::checkIsNotSampler(const TSourceLoc &line,
Martin Radev4a9cd802016-09-01 16:51:51 +0300660 const TTypeSpecifierNonArray &pType,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400661 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000662{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530663 if (pType.type == EbtStruct)
664 {
665 if (containsSampler(*pType.userDef))
666 {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000667 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
Olli Etuaho8a176262016-08-16 14:23:01 +0300668 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000669 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530670
Olli Etuaho8a176262016-08-16 14:23:01 +0300671 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530672 }
673 else if (IsSampler(pType.type))
674 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000675 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300676 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000677 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000678
Olli Etuaho8a176262016-08-16 14:23:01 +0300679 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000680}
681
Olli Etuaho856c4972016-08-08 11:38:39 +0300682void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
683 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400684{
685 if (pType.layoutQualifier.location != -1)
686 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400687 error(line, "location must only be specified for a single input or output variable",
688 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400689 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400690}
691
Olli Etuaho856c4972016-08-08 11:38:39 +0300692void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
693 const TLayoutQualifier &layoutQualifier)
694{
695 if (layoutQualifier.location != -1)
696 {
697 error(location, "invalid layout qualifier:", "location",
698 "only valid on program inputs and outputs");
699 }
700}
701
702void TParseContext::checkOutParameterIsNotSampler(const TSourceLoc &line,
703 TQualifier qualifier,
704 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000705{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400706 if ((qualifier == EvqOut || qualifier == EvqInOut) && type.getBasicType() != EbtStruct &&
707 IsSampler(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530708 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000709 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000710 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000711}
712
Arun Patole7e7e68d2015-05-22 12:02:25 +0530713bool TParseContext::containsSampler(const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000714{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000715 if (IsSampler(type.getBasicType()))
716 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000717
Arun Patole7e7e68d2015-05-22 12:02:25 +0530718 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock())
719 {
720 const TFieldList &fields = type.getStruct()->fields();
721 for (unsigned int i = 0; i < fields.size(); ++i)
722 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400723 if (containsSampler(*fields[i]->type()))
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000724 return true;
725 }
726 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000727
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000728 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000729}
730
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000731// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300732unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000733{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530734 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000735
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200736 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
737 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
738 // fold as array size.
739 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000740 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000741 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300742 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000743 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000744
Olli Etuaho856c4972016-08-08 11:38:39 +0300745 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400746
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000747 if (constant->getBasicType() == EbtUInt)
748 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300749 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000750 }
751 else
752 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300753 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000754
Olli Etuaho856c4972016-08-08 11:38:39 +0300755 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000756 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400757 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300758 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000759 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400760
Olli Etuaho856c4972016-08-08 11:38:39 +0300761 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400762 }
763
Olli Etuaho856c4972016-08-08 11:38:39 +0300764 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400765 {
766 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300767 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400768 }
769
770 // The size of arrays is restricted here to prevent issues further down the
771 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
772 // 4096 registers so this should be reasonable even for aggressively optimizable code.
773 const unsigned int sizeLimit = 65536;
774
Olli Etuaho856c4972016-08-08 11:38:39 +0300775 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -0400776 {
777 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300778 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000779 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300780
781 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000782}
783
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000784// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +0300785bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
786 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000787{
Olli Etuaho8a176262016-08-16 14:23:01 +0300788 if ((elementQualifier.qualifier == EvqAttribute) ||
789 (elementQualifier.qualifier == EvqVertexIn) ||
790 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300791 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400792 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300793 TType(elementQualifier).getQualifierString());
794 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000795 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000796
Olli Etuaho8a176262016-08-16 14:23:01 +0300797 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000798}
799
Olli Etuaho8a176262016-08-16 14:23:01 +0300800// See if this element type can be formed into an array.
801bool TParseContext::checkIsValidTypeForArray(const TSourceLoc &line, const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000802{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000803 //
804 // Can the type be an array?
805 //
Olli Etuaho8a176262016-08-16 14:23:01 +0300806 if (elementType.array)
Jamie Madill06145232015-05-13 13:10:01 -0400807 {
Olli Etuaho8a176262016-08-16 14:23:01 +0300808 error(line, "cannot declare arrays of arrays",
809 TType(elementType).getCompleteString().c_str());
810 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000811 }
Olli Etuahocc36b982015-07-10 14:14:18 +0300812 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
813 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
814 // 4.3.4).
Martin Radev4a9cd802016-09-01 16:51:51 +0300815 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Olli Etuaho8a176262016-08-16 14:23:01 +0300816 sh::IsVarying(elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +0300817 {
818 error(line, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300819 TType(elementType).getCompleteString().c_str());
820 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +0300821 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000822
Olli Etuaho8a176262016-08-16 14:23:01 +0300823 return true;
824}
825
826// Check if this qualified element type can be formed into an array.
827bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
828 const TPublicType &elementType)
829{
830 if (checkIsValidTypeForArray(indexLocation, elementType))
831 {
832 return checkIsValidQualifierForArray(indexLocation, elementType);
833 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000834 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000835}
836
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000837// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +0300838void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
839 const TString &identifier,
840 TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000841{
Olli Etuaho3739d232015-04-08 12:23:44 +0300842 ASSERT(type != nullptr);
843 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000844 {
845 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300846 type->qualifier = EvqTemporary;
847
848 // Generate informative error messages for ESSL1.
849 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400850 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000851 {
Arun Patole7e7e68d2015-05-22 12:02:25 +0530852 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400853 "structures containing arrays may not be declared constant since they cannot be "
854 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530855 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000856 }
857 else
858 {
859 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
860 }
Olli Etuaho383b7912016-08-05 11:22:59 +0300861 return;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000862 }
Olli Etuaho376f1b52015-04-13 13:23:41 +0300863 if (type->isUnsizedArray())
864 {
865 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
Olli Etuaho376f1b52015-04-13 13:23:41 +0300866 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000867}
868
Olli Etuaho2935c582015-04-08 14:32:06 +0300869// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000870// and update the symbol table.
871//
Olli Etuaho2935c582015-04-08 14:32:06 +0300872// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000873//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400874bool TParseContext::declareVariable(const TSourceLoc &line,
875 const TString &identifier,
876 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +0300877 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000878{
Olli Etuaho2935c582015-04-08 14:32:06 +0300879 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000880
Olli Etuaho856c4972016-08-08 11:38:39 +0300881 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000882
Olli Etuaho2935c582015-04-08 14:32:06 +0300883 // gl_LastFragData may be redeclared with a new precision qualifier
884 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
885 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400886 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
887 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho856c4972016-08-08 11:38:39 +0300888 if (static_cast<int>(type.getArraySize()) == maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +0300889 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400890 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +0300891 {
Olli Etuaho8a176262016-08-16 14:23:01 +0300892 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->getExtension());
Olli Etuaho2935c582015-04-08 14:32:06 +0300893 }
894 }
895 else
896 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400897 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
898 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +0300899 return false;
900 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000901 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000902
Olli Etuaho8a176262016-08-16 14:23:01 +0300903 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +0300904 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000905
Olli Etuaho2935c582015-04-08 14:32:06 +0300906 (*variable) = new TVariable(&identifier, type);
907 if (!symbolTable.declare(*variable))
908 {
909 error(line, "redefinition", identifier.c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -0400910 *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +0300911 return false;
912 }
913
Olli Etuaho8a176262016-08-16 14:23:01 +0300914 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +0300915 return false;
916
917 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000918}
919
Martin Radev70866b82016-07-22 15:27:42 +0300920void TParseContext::checkIsParameterQualifierValid(
921 const TSourceLoc &line,
922 const TTypeQualifierBuilder &typeQualifierBuilder,
923 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530924{
Olli Etuaho613b9592016-09-05 12:05:53 +0300925 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(&mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +0300926
927 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530928 {
Martin Radev70866b82016-07-22 15:27:42 +0300929 checkOutParameterIsNotSampler(line, typeQualifier.qualifier, *type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000930 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000931
Martin Radev70866b82016-07-22 15:27:42 +0300932 type->setQualifier(typeQualifier.qualifier);
933
934 if (typeQualifier.precision != EbpUndefined)
935 {
936 type->setPrecision(typeQualifier.precision);
937 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000938}
939
Olli Etuaho856c4972016-08-08 11:38:39 +0300940bool TParseContext::checkCanUseExtension(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000941{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400942 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000943 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +0530944 if (iter == extBehavior.end())
945 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000946 error(line, "extension", extension.c_str(), "is not supported");
Olli Etuaho8a176262016-08-16 14:23:01 +0300947 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000948 }
zmo@google.comf5450912011-09-09 01:37:19 +0000949 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +0530950 if (iter->second == EBhDisable || iter->second == EBhUndefined)
951 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000952 error(line, "extension", extension.c_str(), "is disabled");
Olli Etuaho8a176262016-08-16 14:23:01 +0300953 return false;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000954 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530955 if (iter->second == EBhWarn)
956 {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000957 warning(line, "extension", extension.c_str(), "is being used");
Olli Etuaho8a176262016-08-16 14:23:01 +0300958 return true;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000959 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000960
Olli Etuaho8a176262016-08-16 14:23:01 +0300961 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000962}
963
Jamie Madillb98c3a82015-07-23 14:26:04 -0400964// These checks are common for all declarations starting a declarator list, and declarators that
965// follow an empty declaration.
Olli Etuaho383b7912016-08-05 11:22:59 +0300966void TParseContext::singleDeclarationErrorCheck(const TPublicType &publicType,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400967 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -0400968{
Olli Etuahofa33d582015-04-09 14:33:12 +0300969 switch (publicType.qualifier)
970 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400971 case EvqVaryingIn:
972 case EvqVaryingOut:
973 case EvqAttribute:
974 case EvqVertexIn:
975 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +0300976 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +0300977 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -0400978 {
979 error(identifierLocation, "cannot be used with a structure",
980 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +0300981 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400982 }
Olli Etuahofa33d582015-04-09 14:33:12 +0300983
Jamie Madillb98c3a82015-07-23 14:26:04 -0400984 default:
985 break;
Olli Etuahofa33d582015-04-09 14:33:12 +0300986 }
987
Jamie Madillb98c3a82015-07-23 14:26:04 -0400988 if (publicType.qualifier != EvqUniform &&
Martin Radev4a9cd802016-09-01 16:51:51 +0300989 !checkIsNotSampler(identifierLocation, publicType.typeSpecifierNonArray,
990 "samplers must be uniform"))
Olli Etuahofa33d582015-04-09 14:33:12 +0300991 {
Olli Etuaho383b7912016-08-05 11:22:59 +0300992 return;
Olli Etuahofa33d582015-04-09 14:33:12 +0300993 }
Jamie Madilla5efff92013-06-06 11:56:47 -0400994
995 // check for layout qualifier issues
996 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
997
998 if (layoutQualifier.matrixPacking != EmpUnspecified)
999 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001000 error(identifierLocation, "layout qualifier",
1001 getMatrixPackingString(layoutQualifier.matrixPacking),
Olli Etuahofa33d582015-04-09 14:33:12 +03001002 "only valid for interface blocks");
Olli Etuaho383b7912016-08-05 11:22:59 +03001003 return;
Jamie Madilla5efff92013-06-06 11:56:47 -04001004 }
1005
1006 if (layoutQualifier.blockStorage != EbsUnspecified)
1007 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001008 error(identifierLocation, "layout qualifier",
1009 getBlockStorageString(layoutQualifier.blockStorage),
Olli Etuahofa33d582015-04-09 14:33:12 +03001010 "only valid for interface blocks");
Olli Etuaho383b7912016-08-05 11:22:59 +03001011 return;
Jamie Madilla5efff92013-06-06 11:56:47 -04001012 }
1013
Olli Etuaho383b7912016-08-05 11:22:59 +03001014 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut)
Jamie Madilla5efff92013-06-06 11:56:47 -04001015 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001016 checkLocationIsNotSpecified(identifierLocation, publicType.layoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04001017 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001018}
1019
Olli Etuaho856c4972016-08-08 11:38:39 +03001020void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1021 const TString &layoutQualifierName,
1022 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001023{
1024
1025 if (mShaderVersion < versionRequired)
1026 {
1027 error(location, "invalid layout qualifier:", layoutQualifierName.c_str(), "not supported");
Martin Radev802abe02016-08-04 17:48:32 +03001028 }
1029}
1030
Olli Etuaho856c4972016-08-08 11:38:39 +03001031bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1032 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001033{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001034 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001035 for (size_t i = 0u; i < localSize.size(); ++i)
1036 {
1037 if (localSize[i] != -1)
1038 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03001039 error(location, "invalid layout qualifier:", getWorkGroupSizeString(i),
Martin Radev802abe02016-08-04 17:48:32 +03001040 "only valid when used with 'in' in a compute shader global layout declaration");
Olli Etuaho8a176262016-08-16 14:23:01 +03001041 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001042 }
1043 }
1044
Olli Etuaho8a176262016-08-16 14:23:01 +03001045 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001046}
1047
Olli Etuaho383b7912016-08-05 11:22:59 +03001048void TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate,
Olli Etuaho856c4972016-08-08 11:38:39 +03001049 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001050{
1051 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1052 {
1053 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1054 if (qual == EvqOut || qual == EvqInOut)
1055 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001056 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
Olli Etuaho8a176262016-08-16 14:23:01 +03001057 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001058 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001059 error(argument->getLine(),
Jamie Madillb98c3a82015-07-23 14:26:04 -04001060 "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error");
Olli Etuaho383b7912016-08-05 11:22:59 +03001061 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001062 }
1063 }
1064 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001065}
1066
Martin Radev70866b82016-07-22 15:27:42 +03001067void TParseContext::checkInvariantVariableQualifier(bool invariant,
1068 const TQualifier qualifier,
1069 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001070{
Martin Radev70866b82016-07-22 15:27:42 +03001071 if (!invariant)
1072 return;
1073
1074 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001075 {
Martin Radev70866b82016-07-22 15:27:42 +03001076 // input variables in the fragment shader can be also qualified as invariant
1077 if (!sh::CanBeInvariantESSL1(qualifier))
1078 {
1079 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1080 }
1081 }
1082 else
1083 {
1084 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1085 {
1086 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1087 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001088 }
1089}
1090
Arun Patole7e7e68d2015-05-22 12:02:25 +05301091bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001092{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001093 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001094 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1095 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001096}
1097
Arun Patole7e7e68d2015-05-22 12:02:25 +05301098bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001099{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001100 return ::IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001101}
1102
Jamie Madillb98c3a82015-07-23 14:26:04 -04001103void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1104 const char *extName,
1105 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001106{
1107 pp::SourceLocation srcLoc;
1108 srcLoc.file = loc.first_file;
1109 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001110 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001111}
1112
Jamie Madillb98c3a82015-07-23 14:26:04 -04001113void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1114 const char *name,
1115 const char *value,
1116 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001117{
1118 pp::SourceLocation srcLoc;
1119 srcLoc.file = loc.first_file;
1120 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001121 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001122}
1123
Martin Radev4c4c8e72016-08-04 12:25:34 +03001124sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001125{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001126 sh::WorkGroupSize result;
Martin Radev802abe02016-08-04 17:48:32 +03001127 for (size_t i = 0u; i < result.size(); ++i)
1128 {
1129 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1130 {
1131 result[i] = 1;
1132 }
1133 else
1134 {
1135 result[i] = mComputeShaderLocalSize[i];
1136 }
1137 }
1138 return result;
1139}
1140
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001141/////////////////////////////////////////////////////////////////////////////////
1142//
1143// Non-Errors.
1144//
1145/////////////////////////////////////////////////////////////////////////////////
1146
Jamie Madill5c097022014-08-20 16:38:32 -04001147const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1148 const TString *name,
1149 const TSymbol *symbol)
1150{
1151 const TVariable *variable = NULL;
1152
1153 if (!symbol)
1154 {
1155 error(location, "undeclared identifier", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001156 }
1157 else if (!symbol->isVariable())
1158 {
1159 error(location, "variable expected", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001160 }
1161 else
1162 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001163 variable = static_cast<const TVariable *>(symbol);
Jamie Madill5c097022014-08-20 16:38:32 -04001164
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001165 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
Olli Etuaho383b7912016-08-05 11:22:59 +03001166 !variable->getExtension().empty())
Jamie Madill5c097022014-08-20 16:38:32 -04001167 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001168 checkCanUseExtension(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001169 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001170
1171 // Reject shaders using both gl_FragData and gl_FragColor
1172 TQualifier qualifier = variable->getType().getQualifier();
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001173 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001174 {
1175 mUsesFragData = true;
1176 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001177 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001178 {
1179 mUsesFragColor = true;
1180 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001181 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1182 {
1183 mUsesSecondaryOutputs = true;
1184 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001185
1186 // This validation is not quite correct - it's only an error to write to
1187 // both FragData and FragColor. For simplicity, and because users shouldn't
1188 // be rewarded for reading from undefined varaibles, return an error
1189 // if they are both referenced, rather than assigned.
1190 if (mUsesFragData && mUsesFragColor)
1191 {
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001192 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1193 if (mUsesSecondaryOutputs)
1194 {
1195 errorMessage =
1196 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1197 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1198 }
1199 error(location, errorMessage, name->c_str());
Jamie Madill14e95b32015-05-07 10:10:41 -04001200 }
Martin Radevb0883602016-08-04 17:48:58 +03001201
1202 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1203 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1204 qualifier == EvqWorkGroupSize)
1205 {
1206 error(location,
1207 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1208 "gl_WorkGroupSize");
1209 }
Jamie Madill5c097022014-08-20 16:38:32 -04001210 }
1211
1212 if (!variable)
1213 {
1214 TType type(EbtFloat, EbpUndefined);
1215 TVariable *fakeVariable = new TVariable(name, type);
1216 symbolTable.declare(fakeVariable);
1217 variable = fakeVariable;
1218 }
1219
1220 return variable;
1221}
1222
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001223TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1224 const TString *name,
1225 const TSymbol *symbol)
1226{
1227 const TVariable *variable = getNamedVariable(location, name, symbol);
1228
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001229 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001230 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001231 const TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001232 return intermediate.addConstantUnion(constArray, variable->getType(), location);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001233 }
1234 else
1235 {
1236 return intermediate.addSymbol(variable->getUniqueId(), variable->getName(),
1237 variable->getType(), location);
1238 }
1239}
1240
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001241//
1242// Look up a function name in the symbol table, and make sure it is a function.
1243//
1244// Return the function symbol if found, otherwise 0.
1245//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001246const TFunction *TParseContext::findFunction(const TSourceLoc &line,
1247 TFunction *call,
1248 int inputShaderVersion,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301249 bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001250{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001251 // First find by unmangled name to check whether the function name has been
1252 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001253 // If a function is found, check for one with a matching argument list.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301254 const TSymbol *symbol = symbolTable.find(call->getName(), inputShaderVersion, builtIn);
1255 if (symbol == 0 || symbol->isFunction())
1256 {
Austin Kinross3ae64652015-01-26 15:51:39 -08001257 symbol = symbolTable.find(call->getMangledName(), inputShaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001258 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001259
Arun Patole7e7e68d2015-05-22 12:02:25 +05301260 if (symbol == 0)
1261 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001262 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001263 return 0;
1264 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001265
Arun Patole7e7e68d2015-05-22 12:02:25 +05301266 if (!symbol->isFunction())
1267 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001268 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001269 return 0;
1270 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001271
Jamie Madillb98c3a82015-07-23 14:26:04 -04001272 return static_cast<const TFunction *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001273}
1274
1275//
1276// Initializers show up in several places in the grammar. Have one set of
1277// code to handle them here.
1278//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001279// Returns true on error, false if no error
1280//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001281bool TParseContext::executeInitializer(const TSourceLoc &line,
1282 const TString &identifier,
1283 const TPublicType &pType,
1284 TIntermTyped *initializer,
1285 TIntermNode **intermNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001286{
Olli Etuahoe7847b02015-03-16 11:56:12 +02001287 ASSERT(intermNode != nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001288 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001289
Olli Etuaho2935c582015-04-08 14:32:06 +03001290 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001291 if (type.isUnsizedArray())
1292 {
1293 type.setArraySize(initializer->getArraySize());
1294 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001295 if (!declareVariable(line, identifier, type, &variable))
1296 {
1297 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001298 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001299
Olli Etuahob0c645e2015-05-12 14:25:36 +03001300 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001301 if (symbolTable.atGlobalLevel() &&
1302 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001303 {
1304 // Error message does not completely match behavior with ESSL 1.00, but
1305 // we want to steer developers towards only using constant expressions.
1306 error(line, "global variable initializers must be constant expressions", "=");
1307 return true;
1308 }
1309 if (globalInitWarning)
1310 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001311 warning(
1312 line,
1313 "global variable initializers should be constant expressions "
1314 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1315 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001316 }
1317
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001318 //
1319 // identifier must be of type constant, a global, or a temporary
1320 //
1321 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301322 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1323 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001324 error(line, " cannot initialize this type of qualifier ",
1325 variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001326 return true;
1327 }
1328 //
1329 // test for and propagate constant
1330 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001331
Arun Patole7e7e68d2015-05-22 12:02:25 +05301332 if (qualifier == EvqConst)
1333 {
1334 if (qualifier != initializer->getType().getQualifier())
1335 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001336 std::stringstream extraInfoStream;
1337 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1338 std::string extraInfo = extraInfoStream.str();
1339 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001340 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001341 return true;
1342 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301343 if (type != initializer->getType())
1344 {
1345 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001346 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001347 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001348 return true;
1349 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001350
1351 // Save the constant folded value to the variable if possible. For example array
1352 // initializers are not folded, since that way copying the array literal to multiple places
1353 // in the shader is avoided.
1354 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1355 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301356 if (initializer->getAsConstantUnion())
1357 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001358 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001359 *intermNode = nullptr;
1360 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301361 }
1362 else if (initializer->getAsSymbolNode())
1363 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001364 const TSymbol *symbol =
1365 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1366 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001367
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001368 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001369 if (constArray)
1370 {
1371 variable->shareConstPointer(constArray);
1372 *intermNode = nullptr;
1373 return false;
1374 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001375 }
1376 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001377
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001378 TIntermSymbol *intermSymbol = intermediate.addSymbol(
1379 variable->getUniqueId(), variable->getName(), variable->getType(), line);
1380 *intermNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1381 if (*intermNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001382 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001383 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1384 return true;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001385 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001386
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001387 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001388}
1389
Martin Radev70866b82016-07-22 15:27:42 +03001390TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301391 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001392{
Olli Etuaho613b9592016-09-05 12:05:53 +03001393 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001394
Martin Radev70866b82016-07-22 15:27:42 +03001395 TPublicType returnType = typeSpecifier;
1396 returnType.qualifier = typeQualifier.qualifier;
1397 returnType.invariant = typeQualifier.invariant;
1398 returnType.layoutQualifier = typeQualifier.layoutQualifier;
1399 returnType.precision = typeSpecifier.precision;
1400
1401 if (typeQualifier.precision != EbpUndefined)
1402 {
1403 returnType.precision = typeQualifier.precision;
1404 }
1405
Martin Radev4a9cd802016-09-01 16:51:51 +03001406 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
1407 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03001408
Martin Radev4a9cd802016-09-01 16:51:51 +03001409 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
1410 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03001411
Martin Radev4a9cd802016-09-01 16:51:51 +03001412 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03001413
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001414 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001415 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001416 if (typeSpecifier.array)
1417 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001418 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001419 returnType.clearArrayness();
1420 }
1421
Martin Radev70866b82016-07-22 15:27:42 +03001422 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001423 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001424 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001425 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001426 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001427 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001428
Martin Radev70866b82016-07-22 15:27:42 +03001429 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001430 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001431 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001432 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001433 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001434 }
1435 }
1436 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001437 {
Martin Radev70866b82016-07-22 15:27:42 +03001438 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03001439 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001440 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03001441 }
Martin Radev70866b82016-07-22 15:27:42 +03001442 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
1443 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001444 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001445 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
1446 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001447 }
Martin Radev70866b82016-07-22 15:27:42 +03001448 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03001449 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001450 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03001451 "in");
Martin Radev802abe02016-08-04 17:48:32 +03001452 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001453 }
1454
1455 return returnType;
1456}
1457
Olli Etuaho856c4972016-08-08 11:38:39 +03001458void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
1459 const TPublicType &type,
1460 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03001461{
1462 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03001463 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03001464 {
1465 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001466 }
1467
1468 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
1469 switch (qualifier)
1470 {
1471 case EvqVertexIn:
1472 // ESSL 3.00 section 4.3.4
1473 if (type.array)
1474 {
1475 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001476 }
1477 // Vertex inputs with a struct type are disallowed in singleDeclarationErrorCheck
1478 return;
1479 case EvqFragmentOut:
1480 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03001481 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03001482 {
1483 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001484 }
1485 // Fragment outputs with a struct type are disallowed in singleDeclarationErrorCheck
1486 return;
1487 default:
1488 break;
1489 }
1490
1491 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
1492 // restrictions.
1493 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03001494 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
1495 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03001496 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
1497 {
1498 error(qualifierLocation, "must use 'flat' interpolation here",
1499 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001500 }
1501
Martin Radev4a9cd802016-09-01 16:51:51 +03001502 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03001503 {
1504 // ESSL 3.00 sections 4.3.4 and 4.3.6.
1505 // These restrictions are only implied by the ESSL 3.00 spec, but
1506 // the ESSL 3.10 spec lists these restrictions explicitly.
1507 if (type.array)
1508 {
1509 error(qualifierLocation, "cannot be an array of structures",
1510 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001511 }
1512 if (type.isStructureContainingArrays())
1513 {
1514 error(qualifierLocation, "cannot be a structure containing an array",
1515 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001516 }
1517 if (type.isStructureContainingType(EbtStruct))
1518 {
1519 error(qualifierLocation, "cannot be a structure containing a structure",
1520 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001521 }
1522 if (type.isStructureContainingType(EbtBool))
1523 {
1524 error(qualifierLocation, "cannot be a structure containing a bool",
1525 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001526 }
1527 }
1528}
1529
Olli Etuahofa33d582015-04-09 14:33:12 +03001530TIntermAggregate *TParseContext::parseSingleDeclaration(TPublicType &publicType,
1531 const TSourceLoc &identifierOrTypeLocation,
1532 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001533{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001534 TType type(publicType);
1535 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
1536 mDirectiveHandler.pragma().stdgl.invariantAll)
1537 {
1538 TQualifier qualifier = type.getQualifier();
1539
1540 // The directive handler has already taken care of rejecting invalid uses of this pragma
1541 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
1542 // affected variable declarations:
1543 //
1544 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
1545 // elsewhere, in TranslatorGLSL.)
1546 //
1547 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
1548 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
1549 // the way this is currently implemented we have to enable this compiler option before
1550 // parsing the shader and determining the shading language version it uses. If this were
1551 // implemented as a post-pass, the workaround could be more targeted.
1552 //
1553 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
1554 // the specification, but there are desktop OpenGL drivers that expect that this is the
1555 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
1556 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
1557 {
1558 type.setInvariant(true);
1559 }
1560 }
1561
1562 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, type, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001563
Olli Etuahobab4c082015-04-24 16:38:49 +03001564 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03001565
Olli Etuahobab4c082015-04-24 16:38:49 +03001566 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1567
1568 if (emptyDeclaration)
1569 {
1570 if (publicType.isUnsizedArray())
1571 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001572 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1573 // error. It is assumed that this applies to empty declarations as well.
1574 error(identifierOrTypeLocation, "empty array declaration needs to specify a size",
1575 identifier.c_str());
Olli Etuahobab4c082015-04-24 16:38:49 +03001576 }
1577 }
1578 else
Jamie Madill60ed9812013-06-06 11:56:46 -04001579 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001580 singleDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001581
Olli Etuaho856c4972016-08-08 11:38:39 +03001582 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001583
Olli Etuaho2935c582015-04-08 14:32:06 +03001584 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001585 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04001586
1587 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001588 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001589 }
1590
Olli Etuaho32db19b2016-10-04 14:43:16 +01001591 return TIntermediate::MakeAggregate(symbol, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001592}
1593
Olli Etuahoe7847b02015-03-16 11:56:12 +02001594TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1595 const TSourceLoc &identifierLocation,
1596 const TString &identifier,
1597 const TSourceLoc &indexLocation,
1598 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001599{
Olli Etuahofa33d582015-04-09 14:33:12 +03001600 mDeferredSingleDeclarationErrorCheck = false;
1601
Olli Etuaho383b7912016-08-05 11:22:59 +03001602 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001603
Olli Etuaho856c4972016-08-08 11:38:39 +03001604 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001605
Olli Etuaho8a176262016-08-16 14:23:01 +03001606 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001607
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001608 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001609
Olli Etuaho856c4972016-08-08 11:38:39 +03001610 unsigned int size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001611 // Make the type an array even if size check failed.
1612 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1613 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001614
Olli Etuaho2935c582015-04-08 14:32:06 +03001615 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03001616 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04001617
Olli Etuahoe7847b02015-03-16 11:56:12 +02001618 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001619 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001620 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001621
Olli Etuaho32db19b2016-10-04 14:43:16 +01001622 return TIntermediate::MakeAggregate(symbol, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001623}
1624
Jamie Madill06145232015-05-13 13:10:01 -04001625TIntermAggregate *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001626 const TSourceLoc &identifierLocation,
1627 const TString &identifier,
1628 const TSourceLoc &initLocation,
1629 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001630{
Olli Etuahofa33d582015-04-09 14:33:12 +03001631 mDeferredSingleDeclarationErrorCheck = false;
1632
Olli Etuaho383b7912016-08-05 11:22:59 +03001633 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001634
Olli Etuahoe7847b02015-03-16 11:56:12 +02001635 TIntermNode *intermNode = nullptr;
1636 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04001637 {
1638 //
1639 // Build intermediate representation
1640 //
Olli Etuaho32db19b2016-10-04 14:43:16 +01001641 return intermNode ? TIntermediate::MakeAggregate(intermNode, initLocation) : nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001642 }
1643 else
1644 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001645 return nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001646 }
1647}
1648
Jamie Madillb98c3a82015-07-23 14:26:04 -04001649TIntermAggregate *TParseContext::parseSingleArrayInitDeclaration(
1650 TPublicType &publicType,
1651 const TSourceLoc &identifierLocation,
1652 const TString &identifier,
1653 const TSourceLoc &indexLocation,
1654 TIntermTyped *indexExpression,
1655 const TSourceLoc &initLocation,
1656 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001657{
1658 mDeferredSingleDeclarationErrorCheck = false;
1659
Olli Etuaho383b7912016-08-05 11:22:59 +03001660 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001661
Olli Etuaho8a176262016-08-16 14:23:01 +03001662 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001663
1664 TPublicType arrayType(publicType);
1665
Olli Etuaho856c4972016-08-08 11:38:39 +03001666 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001667 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
1668 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03001669 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001670 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001671 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001672 }
1673 // Make the type an array even if size check failed.
1674 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1675 arrayType.setArraySize(size);
1676
1677 // initNode will correspond to the whole of "type b[n] = initializer".
1678 TIntermNode *initNode = nullptr;
1679 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1680 {
Olli Etuaho32db19b2016-10-04 14:43:16 +01001681 return initNode ? TIntermediate::MakeAggregate(initNode, initLocation) : nullptr;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001682 }
1683 else
1684 {
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001685 return nullptr;
1686 }
1687}
1688
Martin Radev70866b82016-07-22 15:27:42 +03001689TIntermAggregate *TParseContext::parseInvariantDeclaration(
1690 const TTypeQualifierBuilder &typeQualifierBuilder,
1691 const TSourceLoc &identifierLoc,
1692 const TString *identifier,
1693 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04001694{
Olli Etuaho613b9592016-09-05 12:05:53 +03001695 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04001696
Martin Radev70866b82016-07-22 15:27:42 +03001697 if (!typeQualifier.invariant)
1698 {
1699 error(identifierLoc, "Expected invariant", identifier->c_str());
1700 return nullptr;
1701 }
1702 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
1703 {
1704 return nullptr;
1705 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04001706 if (!symbol)
1707 {
1708 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001709 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001710 }
Martin Radev70866b82016-07-22 15:27:42 +03001711 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04001712 {
Martin Radev70866b82016-07-22 15:27:42 +03001713 error(identifierLoc, "invariant declaration specifies qualifier",
1714 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04001715 }
Martin Radev70866b82016-07-22 15:27:42 +03001716 if (typeQualifier.precision != EbpUndefined)
1717 {
1718 error(identifierLoc, "invariant declaration specifies precision",
1719 getPrecisionString(typeQualifier.precision));
1720 }
1721 if (!typeQualifier.layoutQualifier.isEmpty())
1722 {
1723 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
1724 }
1725
1726 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1727 ASSERT(variable);
1728 const TType &type = variable->getType();
1729
1730 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
1731 typeQualifier.line);
1732
1733 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
1734
1735 TIntermSymbol *intermSymbol =
1736 intermediate.addSymbol(variable->getUniqueId(), *identifier, type, identifierLoc);
1737
Olli Etuaho32db19b2016-10-04 14:43:16 +01001738 TIntermAggregate *aggregate = TIntermediate::MakeAggregate(intermSymbol, identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03001739 aggregate->setOp(EOpInvariantDeclaration);
1740 return aggregate;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001741}
1742
Jamie Madillb98c3a82015-07-23 14:26:04 -04001743TIntermAggregate *TParseContext::parseDeclarator(TPublicType &publicType,
1744 TIntermAggregate *aggregateDeclaration,
1745 const TSourceLoc &identifierLocation,
1746 const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001747{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001748 // If the declaration starting this declarator list was empty (example: int,), some checks were
1749 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001750 if (mDeferredSingleDeclarationErrorCheck)
1751 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001752 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03001753 mDeferredSingleDeclarationErrorCheck = false;
1754 }
1755
Olli Etuaho856c4972016-08-08 11:38:39 +03001756 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04001757
Olli Etuaho856c4972016-08-08 11:38:39 +03001758 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04001759
Olli Etuaho2935c582015-04-08 14:32:06 +03001760 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03001761 declareVariable(identifierLocation, identifier, TType(publicType), &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001762
Jamie Madillb98c3a82015-07-23 14:26:04 -04001763 TIntermSymbol *symbol =
1764 intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001765 if (variable && symbol)
Jamie Madill502d66f2013-06-20 11:55:52 -04001766 symbol->setId(variable->getUniqueId());
1767
Olli Etuahoe7847b02015-03-16 11:56:12 +02001768 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001769}
1770
Jamie Madillb98c3a82015-07-23 14:26:04 -04001771TIntermAggregate *TParseContext::parseArrayDeclarator(TPublicType &publicType,
1772 TIntermAggregate *aggregateDeclaration,
1773 const TSourceLoc &identifierLocation,
1774 const TString &identifier,
1775 const TSourceLoc &arrayLocation,
1776 TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001777{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001778 // If the declaration starting this declarator list was empty (example: int,), some checks were
1779 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001780 if (mDeferredSingleDeclarationErrorCheck)
1781 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001782 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03001783 mDeferredSingleDeclarationErrorCheck = false;
1784 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001785
Olli Etuaho856c4972016-08-08 11:38:39 +03001786 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04001787
Olli Etuaho856c4972016-08-08 11:38:39 +03001788 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04001789
Olli Etuaho8a176262016-08-16 14:23:01 +03001790 if (checkIsValidTypeAndQualifierForArray(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001791 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001792 TType arrayType = TType(publicType);
Olli Etuaho856c4972016-08-08 11:38:39 +03001793 unsigned int size = checkIsValidArraySize(arrayLocation, indexExpression);
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001794 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001795
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001796 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03001797 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04001798
Jamie Madillb98c3a82015-07-23 14:26:04 -04001799 TIntermSymbol *symbol =
1800 intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001801 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001802 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001803
1804 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001805 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001806
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001807 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001808}
1809
Jamie Madillb98c3a82015-07-23 14:26:04 -04001810TIntermAggregate *TParseContext::parseInitDeclarator(const TPublicType &publicType,
1811 TIntermAggregate *aggregateDeclaration,
1812 const TSourceLoc &identifierLocation,
1813 const TString &identifier,
1814 const TSourceLoc &initLocation,
1815 TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001816{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001817 // If the declaration starting this declarator list was empty (example: int,), some checks were
1818 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001819 if (mDeferredSingleDeclarationErrorCheck)
1820 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001821 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03001822 mDeferredSingleDeclarationErrorCheck = false;
1823 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001824
Olli Etuaho856c4972016-08-08 11:38:39 +03001825 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04001826
Olli Etuahoe7847b02015-03-16 11:56:12 +02001827 TIntermNode *intermNode = nullptr;
1828 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04001829 {
1830 //
1831 // build the intermediate representation
1832 //
1833 if (intermNode)
1834 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001835 return intermediate.growAggregate(aggregateDeclaration, intermNode, initLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001836 }
1837 else
1838 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001839 return aggregateDeclaration;
Jamie Madill502d66f2013-06-20 11:55:52 -04001840 }
1841 }
1842 else
1843 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001844 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001845 }
1846}
1847
Jamie Madill06145232015-05-13 13:10:01 -04001848TIntermAggregate *TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001849 TIntermAggregate *aggregateDeclaration,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301850 const TSourceLoc &identifierLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001851 const TString &identifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301852 const TSourceLoc &indexLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001853 TIntermTyped *indexExpression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001854 const TSourceLoc &initLocation,
1855 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001856{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001857 // If the declaration starting this declarator list was empty (example: int,), some checks were
1858 // not performed.
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001859 if (mDeferredSingleDeclarationErrorCheck)
1860 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001861 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001862 mDeferredSingleDeclarationErrorCheck = false;
1863 }
1864
Olli Etuaho856c4972016-08-08 11:38:39 +03001865 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001866
Olli Etuaho8a176262016-08-16 14:23:01 +03001867 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001868
1869 TPublicType arrayType(publicType);
1870
Olli Etuaho856c4972016-08-08 11:38:39 +03001871 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001872 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
1873 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03001874 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001875 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001876 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001877 }
1878 // Make the type an array even if size check failed.
1879 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1880 arrayType.setArraySize(size);
1881
1882 // initNode will correspond to the whole of "b[n] = initializer".
1883 TIntermNode *initNode = nullptr;
1884 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1885 {
1886 if (initNode)
1887 {
1888 return intermediate.growAggregate(aggregateDeclaration, initNode, initLocation);
1889 }
1890 else
1891 {
1892 return aggregateDeclaration;
1893 }
1894 }
1895 else
1896 {
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001897 return nullptr;
1898 }
1899}
1900
Martin Radev70866b82016-07-22 15:27:42 +03001901void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04001902{
Olli Etuaho613b9592016-09-05 12:05:53 +03001903 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04001904 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04001905
Martin Radev70866b82016-07-22 15:27:42 +03001906 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
1907 typeQualifier.line);
1908
Jamie Madillc2128ff2016-07-04 10:26:17 -04001909 // It should never be the case, but some strange parser errors can send us here.
1910 if (layoutQualifier.isEmpty())
1911 {
1912 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04001913 return;
1914 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001915
Martin Radev802abe02016-08-04 17:48:32 +03001916 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04001917 {
Martin Radev802abe02016-08-04 17:48:32 +03001918 error(typeQualifier.line, "invalid combination:", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04001919 return;
1920 }
1921
Martin Radev802abe02016-08-04 17:48:32 +03001922 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04001923 {
Martin Radev802abe02016-08-04 17:48:32 +03001924 if (mComputeShaderLocalSizeDeclared &&
1925 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
1926 {
1927 error(typeQualifier.line, "Work group size does not match the previous declaration",
1928 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03001929 return;
1930 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001931
Martin Radev802abe02016-08-04 17:48:32 +03001932 if (mShaderVersion < 310)
1933 {
1934 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03001935 return;
1936 }
Jamie Madill099c0f32013-06-20 11:55:52 -04001937
Martin Radev4c4c8e72016-08-04 12:25:34 +03001938 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03001939 {
1940 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03001941 return;
1942 }
1943
1944 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
1945 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
1946
1947 const TConstantUnion *maxComputeWorkGroupSizeData =
1948 maxComputeWorkGroupSize->getConstPointer();
1949
1950 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
1951 {
1952 if (layoutQualifier.localSize[i] != -1)
1953 {
1954 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
1955 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
1956 if (mComputeShaderLocalSize[i] < 1 ||
1957 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
1958 {
1959 std::stringstream errorMessageStream;
1960 errorMessageStream << "Value must be at least 1 and no greater than "
1961 << maxComputeWorkGroupSizeValue;
1962 const std::string &errorMessage = errorMessageStream.str();
1963
Martin Radev4c4c8e72016-08-04 12:25:34 +03001964 error(typeQualifier.line, "invalid value:", getWorkGroupSizeString(i),
Martin Radev802abe02016-08-04 17:48:32 +03001965 errorMessage.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001966 return;
1967 }
1968 }
1969 }
1970
1971 mComputeShaderLocalSizeDeclared = true;
1972 }
1973 else
Jamie Madill1566ef72013-06-20 11:55:54 -04001974 {
Martin Radev802abe02016-08-04 17:48:32 +03001975
Olli Etuaho8a176262016-08-16 14:23:01 +03001976 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03001977 {
Martin Radev802abe02016-08-04 17:48:32 +03001978 return;
1979 }
1980
1981 if (typeQualifier.qualifier != EvqUniform)
1982 {
1983 error(typeQualifier.line, "invalid qualifier:",
1984 getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
Martin Radev802abe02016-08-04 17:48:32 +03001985 return;
1986 }
1987
1988 if (mShaderVersion < 300)
1989 {
1990 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
1991 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03001992 return;
1993 }
1994
Olli Etuaho856c4972016-08-08 11:38:39 +03001995 checkLocationIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03001996
1997 if (layoutQualifier.matrixPacking != EmpUnspecified)
1998 {
1999 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
2000 }
2001
2002 if (layoutQualifier.blockStorage != EbsUnspecified)
2003 {
2004 mDefaultBlockStorage = layoutQualifier.blockStorage;
2005 }
Jamie Madill1566ef72013-06-20 11:55:54 -04002006 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002007}
2008
Olli Etuaho476197f2016-10-11 13:59:08 +01002009TIntermAggregate *TParseContext::addFunctionPrototypeDeclaration(const TFunction &parsedFunction,
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002010 const TSourceLoc &location)
2011{
Olli Etuaho476197f2016-10-11 13:59:08 +01002012 // Note: function found from the symbol table could be the same as parsedFunction if this is the
2013 // first declaration. Either way the instance in the symbol table is used to track whether the
2014 // function is declared multiple times.
2015 TFunction *function = static_cast<TFunction *>(
2016 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
2017 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02002018 {
2019 // ESSL 1.00.17 section 4.2.7.
2020 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
2021 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02002022 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002023 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02002024
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002025 TIntermAggregate *prototype = new TIntermAggregate;
Olli Etuaho476197f2016-10-11 13:59:08 +01002026 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
2027 // point to the data that already exists in the symbol table.
2028 prototype->setType(function->getReturnType());
Olli Etuahobd674552016-10-06 13:28:42 +01002029 prototype->getFunctionSymbolInfo()->setFromFunction(*function);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002030
Olli Etuaho476197f2016-10-11 13:59:08 +01002031 for (size_t i = 0; i < function->getParamCount(); i++)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002032 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002033 const TConstParameter &param = function->getParam(i);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002034 if (param.name != 0)
2035 {
2036 TVariable variable(param.name, *param.type);
2037
2038 TIntermSymbol *paramSymbol = intermediate.addSymbol(
2039 variable.getUniqueId(), variable.getName(), variable.getType(), location);
2040 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
2041 }
2042 else
2043 {
2044 TIntermSymbol *paramSymbol = intermediate.addSymbol(0, "", *param.type, location);
2045 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
2046 }
2047 }
2048
2049 prototype->setOp(EOpPrototype);
2050
2051 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002052
2053 if (!symbolTable.atGlobalLevel())
2054 {
2055 // ESSL 3.00.4 section 4.2.4.
2056 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002057 }
2058
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002059 return prototype;
2060}
2061
Olli Etuaho336b1472016-10-05 16:37:55 +01002062TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
2063 const TFunction &function,
2064 TIntermAggregate *functionParameters,
2065 TIntermBlock *functionBody,
2066 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002067{
Olli Etuahof51fdd22016-10-03 10:03:40 +01002068 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002069 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
2070 {
2071 error(location, "function does not return a value:", "", function.getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002072 }
2073
Olli Etuahof51fdd22016-10-03 10:03:40 +01002074 if (functionBody == nullptr)
2075 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01002076 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002077 functionBody->setLine(location);
2078 }
Olli Etuaho336b1472016-10-05 16:37:55 +01002079 TIntermFunctionDefinition *functionNode =
2080 new TIntermFunctionDefinition(function.getReturnType(), functionParameters, functionBody);
2081 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01002082
Olli Etuahobd674552016-10-06 13:28:42 +01002083 functionNode->getFunctionSymbolInfo()->setFromFunction(function);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002084
2085 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002086 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002087}
2088
Olli Etuaho476197f2016-10-11 13:59:08 +01002089void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
2090 TFunction **function,
2091 TIntermAggregate **aggregateOut)
Jamie Madill185fb402015-06-12 15:48:48 -04002092{
Olli Etuaho476197f2016-10-11 13:59:08 +01002093 ASSERT(function);
2094 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002095 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01002096 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002097
2098 if (builtIn)
2099 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002100 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002101 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002102 else
Jamie Madill185fb402015-06-12 15:48:48 -04002103 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002104 TFunction *prevDec = static_cast<TFunction *>(
2105 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
2106
2107 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
2108 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
2109 // occurance.
2110 if (*function != prevDec)
2111 {
2112 // Swap the parameters of the previous declaration to the parameters of the function
2113 // definition (parameter names may differ).
2114 prevDec->swapParameters(**function);
2115
2116 // The function definition will share the same symbol as any previous declaration.
2117 *function = prevDec;
2118 }
2119
2120 if ((*function)->isDefined())
2121 {
2122 error(location, "function already has a body", (*function)->getName().c_str());
2123 }
2124
2125 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04002126 }
Jamie Madill185fb402015-06-12 15:48:48 -04002127
2128 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuaho476197f2016-10-11 13:59:08 +01002129 if ((*function)->getName() == "main")
Jamie Madill185fb402015-06-12 15:48:48 -04002130 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002131 if ((*function)->getParamCount() > 0)
Jamie Madill185fb402015-06-12 15:48:48 -04002132 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002133 error(location, "function cannot take any parameter(s)",
2134 (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002135 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002136 if ((*function)->getReturnType().getBasicType() != EbtVoid)
Jamie Madill185fb402015-06-12 15:48:48 -04002137 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002138 error(location, "", (*function)->getReturnType().getBasicString(),
Jamie Madillb98c3a82015-07-23 14:26:04 -04002139 "main function cannot return a value");
Jamie Madill185fb402015-06-12 15:48:48 -04002140 }
2141 }
2142
2143 //
2144 // Remember the return type for later checking for RETURN statements.
2145 //
Olli Etuaho476197f2016-10-11 13:59:08 +01002146 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002147 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04002148
2149 //
2150 // Insert parameters into the symbol table.
2151 // If the parameter has no name, it's not an error, just don't insert it
2152 // (could be used for unused args).
2153 //
2154 // Also, accumulate the list of parameters into the HIL, so lower level code
2155 // knows where to find parameters.
2156 //
2157 TIntermAggregate *paramNodes = new TIntermAggregate;
Olli Etuaho476197f2016-10-11 13:59:08 +01002158 for (size_t i = 0; i < (*function)->getParamCount(); i++)
Jamie Madill185fb402015-06-12 15:48:48 -04002159 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002160 const TConstParameter &param = (*function)->getParam(i);
Jamie Madill185fb402015-06-12 15:48:48 -04002161 if (param.name != 0)
2162 {
2163 TVariable *variable = new TVariable(param.name, *param.type);
2164 //
2165 // Insert the parameters with name in the symbol table.
2166 //
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002167 if (!symbolTable.declare(variable))
2168 {
Jamie Madill185fb402015-06-12 15:48:48 -04002169 error(location, "redefinition", variable->getName().c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002170 paramNodes = intermediate.growAggregate(
2171 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
2172 continue;
Jamie Madill185fb402015-06-12 15:48:48 -04002173 }
2174
2175 //
2176 // Add the parameter to the HIL
2177 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002178 TIntermSymbol *symbol = intermediate.addSymbol(
2179 variable->getUniqueId(), variable->getName(), variable->getType(), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002180
2181 paramNodes = intermediate.growAggregate(paramNodes, symbol, location);
2182 }
2183 else
2184 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002185 paramNodes = intermediate.growAggregate(
2186 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002187 }
2188 }
2189 intermediate.setAggregateOperator(paramNodes, EOpParameters, location);
2190 *aggregateOut = paramNodes;
2191 setLoopNestingLevel(0);
2192}
2193
Jamie Madillb98c3a82015-07-23 14:26:04 -04002194TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002195{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002196 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002197 // We don't know at this point whether this is a function definition or a prototype.
2198 // The definition production code will check for redefinitions.
2199 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002200 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002201 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
2202 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002203 //
2204 TFunction *prevDec =
2205 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302206
2207 if (getShaderVersion() >= 300 && symbolTable.hasUnmangledBuiltIn(function->getName().c_str()))
2208 {
2209 // With ESSL 3.00, names of built-in functions cannot be redeclared as functions.
2210 // Therefore overloading or redefining builtin functions is an error.
2211 error(location, "Name of a built-in function cannot be redeclared as function",
2212 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302213 }
2214 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002215 {
2216 if (prevDec->getReturnType() != function->getReturnType())
2217 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002218 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002219 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04002220 }
2221 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2222 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002223 if (prevDec->getParam(i).type->getQualifier() !=
2224 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002225 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002226 error(location,
2227 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002228 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04002229 }
2230 }
2231 }
2232
2233 //
2234 // Check for previously declared variables using the same name.
2235 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002236 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002237 if (prevSym)
2238 {
2239 if (!prevSym->isFunction())
2240 {
2241 error(location, "redefinition", function->getName().c_str(), "function");
Jamie Madill185fb402015-06-12 15:48:48 -04002242 }
2243 }
2244 else
2245 {
2246 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01002247 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04002248 }
2249
2250 // We're at the inner scope level of the function's arguments and body statement.
2251 // Add the function prototype to the surrounding scope instead.
2252 symbolTable.getOuterLevel()->insert(function);
2253
2254 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002255 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2256 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002257 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2258 //
2259 return function;
2260}
2261
Olli Etuaho9de84a52016-06-14 17:36:01 +03002262TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
2263 const TString *name,
2264 const TSourceLoc &location)
2265{
2266 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
2267 {
2268 error(location, "no qualifiers allowed for function return",
2269 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03002270 }
2271 if (!type.layoutQualifier.isEmpty())
2272 {
2273 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03002274 }
2275 // make sure a sampler is not involved as well...
Martin Radev4a9cd802016-09-01 16:51:51 +03002276 checkIsNotSampler(location, type.typeSpecifierNonArray,
2277 "samplers can't be function return values");
Olli Etuahoe29324f2016-06-15 10:58:03 +03002278 if (mShaderVersion < 300)
2279 {
2280 // Array return values are forbidden, but there's also no valid syntax for declaring array
2281 // return values in ESSL 1.00.
2282 ASSERT(type.arraySize == 0 || mDiagnostics.numErrors() > 0);
2283
2284 if (type.isStructureContainingArrays())
2285 {
2286 // ESSL 1.00.17 section 6.1 Function Definitions
2287 error(location, "structures containing arrays can't be function return values",
2288 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03002289 }
2290 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03002291
2292 // Add the function as a prototype after parsing it (we do not support recursion)
2293 return new TFunction(name, new TType(type));
2294}
2295
Jamie Madill06145232015-05-13 13:10:01 -04002296TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002297{
Jamie Madill06145232015-05-13 13:10:01 -04002298 TPublicType publicType = publicTypeIn;
Martin Radev4a9cd802016-09-01 16:51:51 +03002299 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02002300 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002301 error(publicType.getLine(), "constructor can't be a structure definition",
2302 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02002303 }
2304
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002305 TOperator op = EOpNull;
Martin Radev4a9cd802016-09-01 16:51:51 +03002306 if (publicType.getUserDef())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002307 {
2308 op = EOpConstructStruct;
2309 }
2310 else
2311 {
Geoff Lang156d7192016-07-21 16:11:00 -04002312 op = sh::TypeToConstructorOperator(TType(publicType));
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002313 if (op == EOpNull)
2314 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002315 error(publicType.getLine(), "cannot construct this type",
2316 getBasicString(publicType.getBasicType()));
2317 publicType.setBasicType(EbtFloat);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002318 op = EOpConstructFloat;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002319 }
2320 }
2321
2322 TString tempString;
Dmitry Skiba7f17a502015-06-22 15:08:39 -07002323 const TType *type = new TType(publicType);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002324 return new TFunction(&tempString, type, op);
2325}
2326
Jamie Madillb98c3a82015-07-23 14:26:04 -04002327// This function is used to test for the correctness of the parameters passed to various constructor
2328// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002329//
Olli Etuaho856c4972016-08-08 11:38:39 +03002330// 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 +00002331//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002332TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002333 TOperator op,
2334 TFunction *fnCall,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302335 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002336{
Olli Etuaho856c4972016-08-08 11:38:39 +03002337 TType type = fnCall->getReturnType();
2338 if (type.isUnsizedArray())
2339 {
2340 type.setArraySize(static_cast<unsigned int>(fnCall->getParamCount()));
2341 }
2342 bool constType = true;
2343 for (size_t i = 0; i < fnCall->getParamCount(); ++i)
2344 {
2345 const TConstParameter &param = fnCall->getParam(i);
2346 if (param.type->getQualifier() != EvqConst)
2347 constType = false;
2348 }
2349 if (constType)
2350 type.setQualifier(EvqConst);
2351
Olli Etuaho8a176262016-08-16 14:23:01 +03002352 if (!checkConstructorArguments(line, arguments, *fnCall, op, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03002353 {
2354 TIntermTyped *dummyNode = intermediate.setAggregateOperator(nullptr, op, line);
2355 dummyNode->setType(type);
2356 return dummyNode;
2357 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002358 TIntermAggregate *constructor = arguments->getAsAggregate();
2359 ASSERT(constructor != nullptr);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002360
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002361 // Turn the argument list itself into a constructor
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002362 constructor->setOp(op);
2363 constructor->setLine(line);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002364 ASSERT(constructor->isConstructor());
2365
2366 // Need to set type before setPrecisionFromChildren() because bool doesn't have precision.
Olli Etuaho856c4972016-08-08 11:38:39 +03002367 constructor->setType(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002368
Olli Etuaho21203702014-11-13 16:16:21 +02002369 // Structs should not be precision qualified, the individual members may be.
2370 // Built-in types on the other hand should be precision qualified.
2371 if (op != EOpConstructStruct)
2372 {
2373 constructor->setPrecisionFromChildren();
Olli Etuaho856c4972016-08-08 11:38:39 +03002374 type.setPrecision(constructor->getPrecision());
Olli Etuaho21203702014-11-13 16:16:21 +02002375 }
2376
Olli Etuaho856c4972016-08-08 11:38:39 +03002377 constructor->setType(type);
2378
Olli Etuahof119a262016-08-19 15:54:22 +03002379 TIntermTyped *constConstructor = intermediate.foldAggregateBuiltIn(constructor, &mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002380 if (constConstructor)
2381 {
2382 return constConstructor;
2383 }
2384
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002385 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002386}
2387
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002388//
2389// Interface/uniform blocks
2390//
Martin Radev70866b82016-07-22 15:27:42 +03002391TIntermAggregate *TParseContext::addInterfaceBlock(
2392 const TTypeQualifierBuilder &typeQualifierBuilder,
2393 const TSourceLoc &nameLine,
2394 const TString &blockName,
2395 TFieldList *fieldList,
2396 const TString *instanceName,
2397 const TSourceLoc &instanceLine,
2398 TIntermTyped *arrayIndex,
2399 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002400{
Olli Etuaho856c4972016-08-08 11:38:39 +03002401 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002402
Olli Etuaho613b9592016-09-05 12:05:53 +03002403 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03002404
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002405 if (typeQualifier.qualifier != EvqUniform)
2406 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302407 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
2408 "interface blocks must be uniform");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002409 }
2410
Martin Radev70866b82016-07-22 15:27:42 +03002411 if (typeQualifier.invariant)
2412 {
2413 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
2414 }
2415
Jamie Madill099c0f32013-06-20 11:55:52 -04002416 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03002417 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002418
Jamie Madill099c0f32013-06-20 11:55:52 -04002419 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2420 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002421 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002422 }
2423
Jamie Madill1566ef72013-06-20 11:55:54 -04002424 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2425 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002426 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002427 }
2428
Olli Etuaho856c4972016-08-08 11:38:39 +03002429 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002430
Arun Patole7e7e68d2015-05-22 12:02:25 +05302431 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2432 if (!symbolTable.declare(blockNameSymbol))
2433 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002434 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002435 }
2436
Jamie Madill98493dd2013-07-08 14:39:03 -04002437 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302438 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2439 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002440 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302441 TType *fieldType = field->type();
2442 if (IsSampler(fieldType->getBasicType()))
2443 {
2444 error(field->line(), "unsupported type", fieldType->getBasicString(),
2445 "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002446 }
2447
Jamie Madill98493dd2013-07-08 14:39:03 -04002448 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002449 switch (qualifier)
2450 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002451 case EvqGlobal:
2452 case EvqUniform:
2453 break;
2454 default:
2455 error(field->line(), "invalid qualifier on interface block member",
2456 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04002457 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002458 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002459
Martin Radev70866b82016-07-22 15:27:42 +03002460 if (fieldType->isInvariant())
2461 {
2462 error(field->line(), "invalid qualifier on interface block member", "invariant");
2463 }
2464
Jamie Madilla5efff92013-06-06 11:56:47 -04002465 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002466 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03002467 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
Jamie Madill099c0f32013-06-20 11:55:52 -04002468
Jamie Madill98493dd2013-07-08 14:39:03 -04002469 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002470 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002471 error(field->line(), "invalid layout qualifier:",
2472 getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04002473 }
2474
Jamie Madill98493dd2013-07-08 14:39:03 -04002475 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002476 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002477 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002478 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002479 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002480 {
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002481 warning(field->line(), "extraneous layout qualifier:",
Jamie Madillb98c3a82015-07-23 14:26:04 -04002482 getMatrixPackingString(fieldLayoutQualifier.matrixPacking),
2483 "only has an effect on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04002484 }
2485
Jamie Madill98493dd2013-07-08 14:39:03 -04002486 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002487 }
2488
Jamie Madill98493dd2013-07-08 14:39:03 -04002489 // add array index
Olli Etuaho856c4972016-08-08 11:38:39 +03002490 unsigned int arraySize = 0;
2491 if (arrayIndex != nullptr)
Jamie Madill98493dd2013-07-08 14:39:03 -04002492 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002493 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04002494 }
2495
Jamie Madillb98c3a82015-07-23 14:26:04 -04002496 TInterfaceBlock *interfaceBlock =
2497 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2498 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
2499 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002500
2501 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04002502 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002503
Jamie Madill98493dd2013-07-08 14:39:03 -04002504 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002505 {
2506 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002507 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2508 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002509 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302510 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002511
2512 // set parent pointer of the field variable
2513 fieldType->setInterfaceBlock(interfaceBlock);
2514
Arun Patole7e7e68d2015-05-22 12:02:25 +05302515 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002516 fieldVariable->setQualifier(typeQualifier.qualifier);
2517
Arun Patole7e7e68d2015-05-22 12:02:25 +05302518 if (!symbolTable.declare(fieldVariable))
2519 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002520 error(field->line(), "redefinition", field->name().c_str(),
2521 "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002522 }
2523 }
2524 }
2525 else
2526 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002527 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03002528
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002529 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302530 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002531 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002532
Arun Patole7e7e68d2015-05-22 12:02:25 +05302533 if (!symbolTable.declare(instanceTypeDef))
2534 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002535 error(instanceLine, "redefinition", instanceName->c_str(),
2536 "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002537 }
2538
Jamie Madillb98c3a82015-07-23 14:26:04 -04002539 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002540 symbolName = instanceTypeDef->getName();
2541 }
2542
Olli Etuaho32db19b2016-10-04 14:43:16 +01002543 TIntermAggregate *aggregate = TIntermediate::MakeAggregate(
Jamie Madillb98c3a82015-07-23 14:26:04 -04002544 intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line),
2545 nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002546 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04002547
2548 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002549 return aggregate;
2550}
2551
Olli Etuaho383b7912016-08-05 11:22:59 +03002552void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002553{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002554 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002555
2556 // Embedded structure definitions are not supported per GLSL ES spec.
2557 // They aren't allowed in GLSL either, but we need to detect this here
2558 // so we don't rely on the GLSL compiler to catch it.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302559 if (mStructNestingLevel > 1)
2560 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002561 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002562 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002563}
2564
2565void TParseContext::exitStructDeclaration()
2566{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002567 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002568}
2569
Jamie Madillb98c3a82015-07-23 14:26:04 -04002570namespace
2571{
kbr@chromium.org476541f2011-10-27 21:14:51 +00002572const int kWebGLMaxStructNesting = 4;
2573
2574} // namespace
2575
Olli Etuaho8a176262016-08-16 14:23:01 +03002576void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002577{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302578 if (!IsWebGLBasedSpec(mShaderSpec))
2579 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002580 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002581 }
2582
Arun Patole7e7e68d2015-05-22 12:02:25 +05302583 if (field.type()->getBasicType() != EbtStruct)
2584 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002585 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002586 }
2587
2588 // We're already inside a structure definition at this point, so add
2589 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302590 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2591 {
Jamie Madill41a49272014-03-18 16:10:13 -04002592 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002593 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
2594 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04002595 std::string reason = reasonStream.str();
2596 error(line, reason.c_str(), field.name().c_str(), "");
Olli Etuaho8a176262016-08-16 14:23:01 +03002597 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002598 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002599}
2600
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002601//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002602// Parse an array index expression
2603//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002604TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
2605 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302606 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002607{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002608 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2609 {
2610 if (baseExpression->getAsSymbolNode())
2611 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302612 error(location, " left of '[' is not of type array, matrix, or vector ",
2613 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002614 }
2615 else
2616 {
2617 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2618 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002619
2620 TConstantUnion *unionArray = new TConstantUnion[1];
2621 unionArray->setFConst(0.0f);
2622 return intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst),
2623 location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002624 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002625
Jamie Madill21c1e452014-12-29 11:33:41 -05002626 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2627
Olli Etuaho36b05142015-11-12 13:10:42 +02002628 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
2629 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
2630 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
2631 // index is a constant expression.
2632 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
2633 {
2634 if (baseExpression->isInterfaceBlock())
2635 {
2636 error(
2637 location, "", "[",
2638 "array indexes for interface blocks arrays must be constant integral expressions");
Olli Etuaho36b05142015-11-12 13:10:42 +02002639 }
2640 else if (baseExpression->getQualifier() == EvqFragmentOut)
2641 {
2642 error(location, "", "[",
2643 "array indexes for fragment outputs must be constant integral expressions");
Olli Etuaho36b05142015-11-12 13:10:42 +02002644 }
Olli Etuaho3e960462015-11-12 15:58:39 +02002645 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
2646 {
2647 error(location, "", "[", "array index for gl_FragData must be constant zero");
Olli Etuaho3e960462015-11-12 15:58:39 +02002648 }
Olli Etuaho36b05142015-11-12 13:10:42 +02002649 }
2650
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002651 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002652 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002653 // If an out-of-range index is not qualified as constant, the behavior in the spec is
2654 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
2655 // constant fold expressions that are not constant expressions). The most compatible way to
2656 // handle this case is to report a warning instead of an error and force the index to be in
2657 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002658 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Jamie Madill21c1e452014-12-29 11:33:41 -05002659 int index = indexConstantUnion->getIConst(0);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002660
2661 int safeIndex = -1;
2662
2663 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04002664 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002665 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03002666 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002667 if (mShaderSpec == SH_WEBGL2_SPEC)
2668 {
2669 // Error has been already generated if index is not const.
2670 if (indexExpression->getQualifier() == EvqConst)
2671 {
2672 error(location, "", "[",
2673 "array index for gl_FragData must be constant zero");
2674 }
2675 safeIndex = 0;
2676 }
2677 else if (!isExtensionEnabled("GL_EXT_draw_buffers"))
2678 {
2679 outOfRangeError(outOfRangeIndexIsError, location, "", "[",
2680 "array index for gl_FragData must be zero when "
2681 "GL_EXT_draw_buffers is disabled");
2682 safeIndex = 0;
2683 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03002684 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002685 // Only do generic out-of-range check if similar error hasn't already been reported.
2686 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03002687 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002688 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
2689 baseExpression->getArraySize(),
2690 "array index out of range", "[]");
2691 }
2692 }
2693 else if (baseExpression->isMatrix())
2694 {
2695 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03002696 baseExpression->getType().getCols(),
2697 "matrix field selection out of range", "[]");
Jamie Madill7164cf42013-07-08 13:30:59 -04002698 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002699 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04002700 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002701 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
2702 baseExpression->getType().getNominalSize(),
2703 "vector field selection out of range", "[]");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002704 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002705
2706 ASSERT(safeIndex >= 0);
2707 // Data of constant unions can't be changed, because it may be shared with other
2708 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
2709 // sanitized object.
2710 if (safeIndex != index)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002711 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002712 TConstantUnion *safeConstantUnion = new TConstantUnion();
2713 safeConstantUnion->setIConst(safeIndex);
2714 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002715 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002716
2717 return intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location,
2718 &mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002719 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002720 else
2721 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002722 return intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location,
2723 &mDiagnostics);
Jamie Madill7164cf42013-07-08 13:30:59 -04002724 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002725}
2726
Olli Etuaho90892fb2016-07-14 14:44:51 +03002727int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
2728 const TSourceLoc &location,
2729 int index,
2730 int arraySize,
2731 const char *reason,
2732 const char *token)
2733{
2734 if (index >= arraySize || index < 0)
2735 {
2736 std::stringstream extraInfoStream;
2737 extraInfoStream << "'" << index << "'";
2738 std::string extraInfo = extraInfoStream.str();
2739 outOfRangeError(outOfRangeIndexIsError, location, reason, token, extraInfo.c_str());
2740 if (index < 0)
2741 {
2742 return 0;
2743 }
2744 else
2745 {
2746 return arraySize - 1;
2747 }
2748 }
2749 return index;
2750}
2751
Jamie Madillb98c3a82015-07-23 14:26:04 -04002752TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
2753 const TSourceLoc &dotLocation,
2754 const TString &fieldString,
2755 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002756{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002757 if (baseExpression->isArray())
2758 {
2759 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002760 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002761 }
2762
2763 if (baseExpression->isVector())
2764 {
2765 TVectorFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002766 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields,
2767 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002768 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002769 fields.num = 1;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002770 fields.offsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002771 }
2772
Olli Etuahob6fa0432016-09-28 16:28:05 +01002773 return TIntermediate::AddSwizzle(baseExpression, fields, dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002774 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002775 else if (baseExpression->getBasicType() == EbtStruct)
2776 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302777 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04002778 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002779 {
2780 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002781 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002782 }
2783 else
2784 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002785 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002786 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002787 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002788 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002789 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002790 {
2791 fieldFound = true;
2792 break;
2793 }
2794 }
2795 if (fieldFound)
2796 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002797 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
2798 index->setLine(fieldLocation);
2799 return intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index,
2800 dotLocation, &mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002801 }
2802 else
2803 {
2804 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002805 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002806 }
2807 }
2808 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002809 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002810 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302811 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04002812 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002813 {
2814 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002815 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002816 }
2817 else
2818 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002819 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002820 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002821 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002822 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002823 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002824 {
2825 fieldFound = true;
2826 break;
2827 }
2828 }
2829 if (fieldFound)
2830 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002831 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
2832 index->setLine(fieldLocation);
2833 return intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index,
2834 dotLocation, &mDiagnostics);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002835 }
2836 else
2837 {
2838 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002839 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002840 }
2841 }
2842 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002843 else
2844 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002845 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002846 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03002847 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05302848 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002849 }
2850 else
2851 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302852 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03002853 " field selection requires structure, vector, or interface block on left hand "
2854 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05302855 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002856 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002857 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002858 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002859}
2860
Jamie Madillb98c3a82015-07-23 14:26:04 -04002861TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
2862 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002863{
Martin Radev802abe02016-08-04 17:48:32 +03002864 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002865
2866 if (qualifierType == "shared")
2867 {
Olli Etuahof0173152016-10-17 09:05:03 -07002868 if (IsWebGLBasedSpec(mShaderSpec))
2869 {
2870 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
2871 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002872 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002873 }
2874 else if (qualifierType == "packed")
2875 {
Olli Etuahof0173152016-10-17 09:05:03 -07002876 if (IsWebGLBasedSpec(mShaderSpec))
2877 {
2878 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
2879 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002880 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002881 }
2882 else if (qualifierType == "std140")
2883 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002884 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002885 }
2886 else if (qualifierType == "row_major")
2887 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002888 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002889 }
2890 else if (qualifierType == "column_major")
2891 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002892 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002893 }
2894 else if (qualifierType == "location")
2895 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002896 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
2897 "location requires an argument");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002898 }
2899 else
2900 {
2901 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002902 }
2903
Jamie Madilla5efff92013-06-06 11:56:47 -04002904 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002905}
2906
Martin Radev802abe02016-08-04 17:48:32 +03002907void TParseContext::parseLocalSize(const TString &qualifierType,
2908 const TSourceLoc &qualifierTypeLine,
2909 int intValue,
2910 const TSourceLoc &intValueLine,
2911 const std::string &intValueString,
2912 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002913 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03002914{
Olli Etuaho856c4972016-08-08 11:38:39 +03002915 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03002916 if (intValue < 1)
2917 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002918 std::string errorMessage = std::string(getWorkGroupSizeString(index)) + " must be positive";
Martin Radev802abe02016-08-04 17:48:32 +03002919 error(intValueLine, "out of range:", intValueString.c_str(), errorMessage.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03002920 }
2921 (*localSize)[index] = intValue;
2922}
2923
Jamie Madillb98c3a82015-07-23 14:26:04 -04002924TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
2925 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002926 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302927 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002928{
Martin Radev802abe02016-08-04 17:48:32 +03002929 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002930
Martin Radev802abe02016-08-04 17:48:32 +03002931 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002932
Martin Radev802abe02016-08-04 17:48:32 +03002933 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002934 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002935 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002936 if (intValue < 0)
2937 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002938 error(intValueLine, "out of range:", intValueString.c_str(),
2939 "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002940 }
2941 else
2942 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002943 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03002944 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002945 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002946 }
Martin Radev802abe02016-08-04 17:48:32 +03002947 else if (qualifierType == "local_size_x")
2948 {
2949 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
2950 &qualifier.localSize);
2951 }
2952 else if (qualifierType == "local_size_y")
2953 {
2954 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
2955 &qualifier.localSize);
2956 }
2957 else if (qualifierType == "local_size_z")
2958 {
2959 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
2960 &qualifier.localSize);
2961 }
2962 else
2963 {
2964 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03002965 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002966
Jamie Madilla5efff92013-06-06 11:56:47 -04002967 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002968}
2969
Olli Etuaho613b9592016-09-05 12:05:53 +03002970TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
2971{
2972 return new TTypeQualifierBuilder(
2973 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
2974 mShaderVersion);
2975}
2976
Jamie Madillb98c3a82015-07-23 14:26:04 -04002977TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03002978 TLayoutQualifier rightQualifier,
2979 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002980{
Martin Radevc28888b2016-07-22 15:27:42 +03002981 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
2982 &mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002983}
2984
Martin Radev70866b82016-07-22 15:27:42 +03002985TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
2986 const TTypeQualifierBuilder &typeQualifierBuilder,
2987 TPublicType *typeSpecifier,
2988 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002989{
Olli Etuaho613b9592016-09-05 12:05:53 +03002990 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002991
Martin Radev70866b82016-07-22 15:27:42 +03002992 typeSpecifier->qualifier = typeQualifier.qualifier;
2993 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
2994 typeSpecifier->invariant = typeQualifier.invariant;
2995 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05302996 {
Martin Radev70866b82016-07-22 15:27:42 +03002997 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002998 }
Martin Radev70866b82016-07-22 15:27:42 +03002999 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003000}
3001
Jamie Madillb98c3a82015-07-23 14:26:04 -04003002TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3003 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003004{
Martin Radev4a9cd802016-09-01 16:51:51 +03003005 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
3006 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03003007
Martin Radev4a9cd802016-09-01 16:51:51 +03003008 checkIsNonVoid(typeSpecifier.getLine(), (*fieldList)[0]->name(), typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003009
Martin Radev4a9cd802016-09-01 16:51:51 +03003010 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003011
Arun Patole7e7e68d2015-05-22 12:02:25 +05303012 for (unsigned int i = 0; i < fieldList->size(); ++i)
3013 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003014 //
3015 // Careful not to replace already known aspects of type, like array-ness
3016 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303017 TType *type = (*fieldList)[i]->type();
Martin Radev4a9cd802016-09-01 16:51:51 +03003018 type->setBasicType(typeSpecifier.getBasicType());
3019 type->setPrimarySize(typeSpecifier.getPrimarySize());
3020 type->setSecondarySize(typeSpecifier.getSecondarySize());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003021 type->setPrecision(typeSpecifier.precision);
3022 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003023 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
Martin Radev70866b82016-07-22 15:27:42 +03003024 type->setInvariant(typeSpecifier.invariant);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003025
3026 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303027 if (type->isArray())
3028 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003029 checkIsValidTypeForArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003030 }
3031 if (typeSpecifier.array)
Olli Etuaho856c4972016-08-08 11:38:39 +03003032 type->setArraySize(static_cast<unsigned int>(typeSpecifier.arraySize));
Martin Radev4a9cd802016-09-01 16:51:51 +03003033 if (typeSpecifier.getUserDef())
Arun Patole7e7e68d2015-05-22 12:02:25 +05303034 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003035 type->setStruct(typeSpecifier.getUserDef()->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003036 }
3037
Martin Radev4a9cd802016-09-01 16:51:51 +03003038 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*fieldList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003039 }
3040
Jamie Madill98493dd2013-07-08 14:39:03 -04003041 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003042}
3043
Martin Radev4a9cd802016-09-01 16:51:51 +03003044TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
3045 const TSourceLoc &nameLine,
3046 const TString *structName,
3047 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003048{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303049 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003050 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003051
Jamie Madill9b820842015-02-12 10:40:10 -05003052 // Store a bool in the struct if we're at global scope, to allow us to
3053 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05003054 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003055
Jamie Madill98493dd2013-07-08 14:39:03 -04003056 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003057 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003058 checkIsNotReserved(nameLine, *structName);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303059 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
3060 if (!symbolTable.declare(userTypeDef))
3061 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003062 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003063 }
3064 }
3065
3066 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04003067 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003068 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003069 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04003070 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003071 switch (qualifier)
3072 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003073 case EvqGlobal:
3074 case EvqTemporary:
3075 break;
3076 default:
3077 error(field.line(), "invalid qualifier on struct member",
3078 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003079 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003080 }
Martin Radev70866b82016-07-22 15:27:42 +03003081 if (field.type()->isInvariant())
3082 {
3083 error(field.line(), "invalid qualifier on struct member", "invariant");
3084 }
3085
3086 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003087 }
3088
Martin Radev4a9cd802016-09-01 16:51:51 +03003089 TTypeSpecifierNonArray typeSpecifierNonArray;
3090 typeSpecifierNonArray.initialize(EbtStruct, structLine);
3091 typeSpecifierNonArray.userDef = structureType;
3092 typeSpecifierNonArray.isStructSpecifier = true;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003093 exitStructDeclaration();
3094
Martin Radev4a9cd802016-09-01 16:51:51 +03003095 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003096}
3097
Jamie Madillb98c3a82015-07-23 14:26:04 -04003098TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003099 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003100 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02003101{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003102 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003103 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02003104 init->isVector())
3105 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003106 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
3107 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003108 return nullptr;
3109 }
3110
Olli Etuahoac5274d2015-02-20 10:19:08 +02003111 if (statementList)
3112 {
3113 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
3114 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02003115 return nullptr;
3116 }
3117 }
3118
Olli Etuahoa3a36662015-02-17 13:46:51 +02003119 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3120 if (node == nullptr)
3121 {
3122 error(loc, "erroneous switch statement", "switch");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003123 return nullptr;
3124 }
3125 return node;
3126}
3127
3128TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3129{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003130 if (mSwitchNestingLevel == 0)
3131 {
3132 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003133 return nullptr;
3134 }
3135 if (condition == nullptr)
3136 {
3137 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003138 return nullptr;
3139 }
3140 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04003141 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02003142 {
3143 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003144 }
3145 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003146 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
3147 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
3148 // fold in case labels.
3149 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02003150 {
3151 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003152 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003153 TIntermCase *node = intermediate.addCase(condition, loc);
3154 if (node == nullptr)
3155 {
3156 error(loc, "erroneous case statement", "case");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003157 return nullptr;
3158 }
3159 return node;
3160}
3161
3162TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3163{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003164 if (mSwitchNestingLevel == 0)
3165 {
3166 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003167 return nullptr;
3168 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003169 TIntermCase *node = intermediate.addCase(nullptr, loc);
3170 if (node == nullptr)
3171 {
3172 error(loc, "erroneous default statement", "default");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003173 return nullptr;
3174 }
3175 return node;
3176}
3177
Jamie Madillb98c3a82015-07-23 14:26:04 -04003178TIntermTyped *TParseContext::createUnaryMath(TOperator op,
3179 TIntermTyped *child,
3180 const TSourceLoc &loc,
3181 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003182{
3183 if (child == nullptr)
3184 {
3185 return nullptr;
3186 }
3187
3188 switch (op)
3189 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003190 case EOpLogicalNot:
3191 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
3192 child->isVector())
3193 {
3194 return nullptr;
3195 }
3196 break;
3197 case EOpBitwiseNot:
3198 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3199 child->isMatrix() || child->isArray())
3200 {
3201 return nullptr;
3202 }
3203 break;
3204 case EOpPostIncrement:
3205 case EOpPreIncrement:
3206 case EOpPostDecrement:
3207 case EOpPreDecrement:
3208 case EOpNegative:
3209 case EOpPositive:
3210 if (child->getBasicType() == EbtStruct || child->getBasicType() == EbtBool ||
Olli Etuaho558b0382016-08-26 17:54:34 +03003211 child->isArray() || IsSampler(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04003212 {
3213 return nullptr;
3214 }
3215 // Operators for built-ins are already type checked against their prototype.
3216 default:
3217 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003218 }
3219
Olli Etuahof119a262016-08-19 15:54:22 +03003220 TIntermUnary *node = new TIntermUnary(op, child);
3221 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03003222
3223 TIntermTyped *foldedNode = node->fold(&mDiagnostics);
3224 if (foldedNode)
3225 return foldedNode;
3226
3227 return node;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003228}
3229
Olli Etuaho09b22472015-02-11 11:47:26 +02003230TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3231{
Olli Etuahof6c694b2015-03-26 14:50:53 +02003232 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003233 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003234 {
3235 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003236 return child;
3237 }
3238 return node;
3239}
3240
Jamie Madillb98c3a82015-07-23 14:26:04 -04003241TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
3242 TIntermTyped *child,
3243 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003244{
Olli Etuaho856c4972016-08-08 11:38:39 +03003245 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02003246 return addUnaryMath(op, child, loc);
3247}
3248
Jamie Madillb98c3a82015-07-23 14:26:04 -04003249bool TParseContext::binaryOpCommonCheck(TOperator op,
3250 TIntermTyped *left,
3251 TIntermTyped *right,
3252 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003253{
Olli Etuaho244be012016-08-18 15:26:02 +03003254 if (left->getType().getStruct() || right->getType().getStruct())
3255 {
3256 switch (op)
3257 {
3258 case EOpIndexDirectStruct:
3259 ASSERT(left->getType().getStruct());
3260 break;
3261 case EOpEqual:
3262 case EOpNotEqual:
3263 case EOpAssign:
3264 case EOpInitialize:
3265 if (left->getType() != right->getType())
3266 {
3267 return false;
3268 }
3269 break;
3270 default:
3271 error(loc, "Invalid operation for structs", GetOperatorString(op));
3272 return false;
3273 }
3274 }
3275
Olli Etuahod6b14282015-03-17 14:31:35 +02003276 if (left->isArray() || right->isArray())
3277 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003278 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003279 {
3280 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3281 return false;
3282 }
3283
3284 if (left->isArray() != right->isArray())
3285 {
3286 error(loc, "array / non-array mismatch", GetOperatorString(op));
3287 return false;
3288 }
3289
3290 switch (op)
3291 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003292 case EOpEqual:
3293 case EOpNotEqual:
3294 case EOpAssign:
3295 case EOpInitialize:
3296 break;
3297 default:
3298 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3299 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02003300 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003301 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003302 if (left->getArraySize() != right->getArraySize())
3303 {
3304 error(loc, "array size mismatch", GetOperatorString(op));
3305 return false;
3306 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003307 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003308
3309 // Check ops which require integer / ivec parameters
3310 bool isBitShift = false;
3311 switch (op)
3312 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003313 case EOpBitShiftLeft:
3314 case EOpBitShiftRight:
3315 case EOpBitShiftLeftAssign:
3316 case EOpBitShiftRightAssign:
3317 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3318 // check that the basic type is an integer type.
3319 isBitShift = true;
3320 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3321 {
3322 return false;
3323 }
3324 break;
3325 case EOpBitwiseAnd:
3326 case EOpBitwiseXor:
3327 case EOpBitwiseOr:
3328 case EOpBitwiseAndAssign:
3329 case EOpBitwiseXorAssign:
3330 case EOpBitwiseOrAssign:
3331 // It is enough to check the type of only one operand, since later it
3332 // is checked that the operand types match.
3333 if (!IsInteger(left->getBasicType()))
3334 {
3335 return false;
3336 }
3337 break;
3338 default:
3339 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003340 }
3341
3342 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3343 // So the basic type should usually match.
3344 if (!isBitShift && left->getBasicType() != right->getBasicType())
3345 {
3346 return false;
3347 }
3348
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003349 // Check that:
3350 // 1. Type sizes match exactly on ops that require that.
3351 // 2. Restrictions for structs that contain arrays or samplers are respected.
3352 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003353 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003354 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003355 case EOpAssign:
3356 case EOpInitialize:
3357 case EOpEqual:
3358 case EOpNotEqual:
3359 // ESSL 1.00 sections 5.7, 5.8, 5.9
3360 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
3361 {
3362 error(loc, "undefined operation for structs containing arrays",
3363 GetOperatorString(op));
3364 return false;
3365 }
3366 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3367 // we interpret the spec so that this extends to structs containing samplers,
3368 // similarly to ESSL 1.00 spec.
3369 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
3370 left->getType().isStructureContainingSamplers())
3371 {
3372 error(loc, "undefined operation for structs containing samplers",
3373 GetOperatorString(op));
3374 return false;
3375 }
3376 case EOpLessThan:
3377 case EOpGreaterThan:
3378 case EOpLessThanEqual:
3379 case EOpGreaterThanEqual:
3380 if ((left->getNominalSize() != right->getNominalSize()) ||
3381 (left->getSecondarySize() != right->getSecondarySize()))
3382 {
3383 return false;
3384 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003385 break;
3386 case EOpAdd:
3387 case EOpSub:
3388 case EOpDiv:
3389 case EOpIMod:
3390 case EOpBitShiftLeft:
3391 case EOpBitShiftRight:
3392 case EOpBitwiseAnd:
3393 case EOpBitwiseXor:
3394 case EOpBitwiseOr:
3395 case EOpAddAssign:
3396 case EOpSubAssign:
3397 case EOpDivAssign:
3398 case EOpIModAssign:
3399 case EOpBitShiftLeftAssign:
3400 case EOpBitShiftRightAssign:
3401 case EOpBitwiseAndAssign:
3402 case EOpBitwiseXorAssign:
3403 case EOpBitwiseOrAssign:
3404 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
3405 {
3406 return false;
3407 }
3408
3409 // Are the sizes compatible?
3410 if (left->getNominalSize() != right->getNominalSize() ||
3411 left->getSecondarySize() != right->getSecondarySize())
3412 {
3413 // If the nominal sizes of operands do not match:
3414 // One of them must be a scalar.
3415 if (!left->isScalar() && !right->isScalar())
3416 return false;
3417
3418 // In the case of compound assignment other than multiply-assign,
3419 // the right side needs to be a scalar. Otherwise a vector/matrix
3420 // would be assigned to a scalar. A scalar can't be shifted by a
3421 // vector either.
3422 if (!right->isScalar() &&
3423 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
3424 return false;
3425 }
3426 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003427 default:
3428 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003429 }
3430
Olli Etuahod6b14282015-03-17 14:31:35 +02003431 return true;
3432}
3433
Olli Etuaho1dded802016-08-18 18:13:13 +03003434bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
3435 const TType &left,
3436 const TType &right)
3437{
3438 switch (op)
3439 {
3440 case EOpMul:
3441 case EOpMulAssign:
3442 return left.getNominalSize() == right.getNominalSize() &&
3443 left.getSecondarySize() == right.getSecondarySize();
3444 case EOpVectorTimesScalar:
3445 return true;
3446 case EOpVectorTimesScalarAssign:
3447 ASSERT(!left.isMatrix() && !right.isMatrix());
3448 return left.isVector() && !right.isVector();
3449 case EOpVectorTimesMatrix:
3450 return left.getNominalSize() == right.getRows();
3451 case EOpVectorTimesMatrixAssign:
3452 ASSERT(!left.isMatrix() && right.isMatrix());
3453 return left.isVector() && left.getNominalSize() == right.getRows() &&
3454 left.getNominalSize() == right.getCols();
3455 case EOpMatrixTimesVector:
3456 return left.getCols() == right.getNominalSize();
3457 case EOpMatrixTimesScalar:
3458 return true;
3459 case EOpMatrixTimesScalarAssign:
3460 ASSERT(left.isMatrix() && !right.isMatrix());
3461 return !right.isVector();
3462 case EOpMatrixTimesMatrix:
3463 return left.getCols() == right.getRows();
3464 case EOpMatrixTimesMatrixAssign:
3465 ASSERT(left.isMatrix() && right.isMatrix());
3466 // We need to check two things:
3467 // 1. The matrix multiplication step is valid.
3468 // 2. The result will have the same number of columns as the lvalue.
3469 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
3470
3471 default:
3472 UNREACHABLE();
3473 return false;
3474 }
3475}
3476
Jamie Madillb98c3a82015-07-23 14:26:04 -04003477TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
3478 TIntermTyped *left,
3479 TIntermTyped *right,
3480 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02003481{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003482 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003483 return nullptr;
3484
Olli Etuahofc1806e2015-03-17 13:03:11 +02003485 switch (op)
3486 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003487 case EOpEqual:
3488 case EOpNotEqual:
3489 break;
3490 case EOpLessThan:
3491 case EOpGreaterThan:
3492 case EOpLessThanEqual:
3493 case EOpGreaterThanEqual:
Olli Etuaho244be012016-08-18 15:26:02 +03003494 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3495 !right->getType().getStruct());
3496 if (left->isMatrix() || left->isVector())
Jamie Madillb98c3a82015-07-23 14:26:04 -04003497 {
3498 return nullptr;
3499 }
3500 break;
3501 case EOpLogicalOr:
3502 case EOpLogicalXor:
3503 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03003504 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3505 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003506 if (left->getBasicType() != EbtBool || left->isMatrix() || left->isVector())
3507 {
3508 return nullptr;
3509 }
3510 break;
3511 case EOpAdd:
3512 case EOpSub:
3513 case EOpDiv:
3514 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03003515 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3516 !right->getType().getStruct());
3517 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04003518 {
3519 return nullptr;
3520 }
3521 break;
3522 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03003523 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3524 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003525 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03003526 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04003527 {
3528 return nullptr;
3529 }
3530 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003531 default:
3532 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003533 }
3534
Olli Etuaho1dded802016-08-18 18:13:13 +03003535 if (op == EOpMul)
3536 {
3537 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
3538 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
3539 {
3540 return nullptr;
3541 }
3542 }
3543
Olli Etuaho3fdec912016-08-18 15:08:06 +03003544 TIntermBinary *node = new TIntermBinary(op, left, right);
3545 node->setLine(loc);
3546
Olli Etuaho3fdec912016-08-18 15:08:06 +03003547 // See if we can fold constants.
3548 TIntermTyped *foldedNode = node->fold(&mDiagnostics);
3549 if (foldedNode)
3550 return foldedNode;
3551
3552 return node;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003553}
3554
Jamie Madillb98c3a82015-07-23 14:26:04 -04003555TIntermTyped *TParseContext::addBinaryMath(TOperator op,
3556 TIntermTyped *left,
3557 TIntermTyped *right,
3558 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003559{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003560 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003561 if (node == 0)
3562 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003563 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3564 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003565 return left;
3566 }
3567 return node;
3568}
3569
Jamie Madillb98c3a82015-07-23 14:26:04 -04003570TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
3571 TIntermTyped *left,
3572 TIntermTyped *right,
3573 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003574{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003575 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003576 if (node == 0)
3577 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003578 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3579 right->getCompleteString());
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003580 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02003581 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003582 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
3583 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003584 }
3585 return node;
3586}
3587
Jamie Madillb98c3a82015-07-23 14:26:04 -04003588TIntermTyped *TParseContext::createAssign(TOperator op,
3589 TIntermTyped *left,
3590 TIntermTyped *right,
3591 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003592{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003593 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003594 {
Olli Etuaho1dded802016-08-18 18:13:13 +03003595 if (op == EOpMulAssign)
3596 {
3597 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
3598 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
3599 {
3600 return nullptr;
3601 }
3602 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03003603 TIntermBinary *node = new TIntermBinary(op, left, right);
3604 node->setLine(loc);
3605
Olli Etuaho3fdec912016-08-18 15:08:06 +03003606 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02003607 }
3608 return nullptr;
3609}
3610
Jamie Madillb98c3a82015-07-23 14:26:04 -04003611TIntermTyped *TParseContext::addAssign(TOperator op,
3612 TIntermTyped *left,
3613 TIntermTyped *right,
3614 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003615{
3616 TIntermTyped *node = createAssign(op, left, right, loc);
3617 if (node == nullptr)
3618 {
3619 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02003620 return left;
3621 }
3622 return node;
3623}
3624
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003625TIntermTyped *TParseContext::addComma(TIntermTyped *left,
3626 TIntermTyped *right,
3627 const TSourceLoc &loc)
3628{
Corentin Wallez0d959252016-07-12 17:26:32 -04003629 // WebGL2 section 5.26, the following results in an error:
3630 // "Sequence operator applied to void, arrays, or structs containing arrays"
3631 if (mShaderSpec == SH_WEBGL2_SPEC && (left->isArray() || left->getBasicType() == EbtVoid ||
3632 left->getType().isStructureContainingArrays() ||
3633 right->isArray() || right->getBasicType() == EbtVoid ||
3634 right->getType().isStructureContainingArrays()))
3635 {
3636 error(loc,
3637 "sequence operator is not allowed for void, arrays, or structs containing arrays",
3638 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04003639 }
3640
Olli Etuaho4db7ded2016-10-13 12:23:11 +01003641 return TIntermediate::AddComma(left, right, loc, mShaderVersion);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003642}
3643
Olli Etuaho49300862015-02-20 14:54:49 +02003644TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3645{
3646 switch (op)
3647 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003648 case EOpContinue:
3649 if (mLoopNestingLevel <= 0)
3650 {
3651 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04003652 }
3653 break;
3654 case EOpBreak:
3655 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
3656 {
3657 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04003658 }
3659 break;
3660 case EOpReturn:
3661 if (mCurrentFunctionType->getBasicType() != EbtVoid)
3662 {
3663 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04003664 }
3665 break;
3666 default:
3667 // No checks for discard
3668 break;
Olli Etuaho49300862015-02-20 14:54:49 +02003669 }
3670 return intermediate.addBranch(op, loc);
3671}
3672
Jamie Madillb98c3a82015-07-23 14:26:04 -04003673TIntermBranch *TParseContext::addBranch(TOperator op,
3674 TIntermTyped *returnValue,
3675 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02003676{
3677 ASSERT(op == EOpReturn);
3678 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003679 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02003680 {
3681 error(loc, "void function cannot return a value", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02003682 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003683 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02003684 {
3685 error(loc, "function return is not matching type:", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02003686 }
3687 return intermediate.addBranch(op, returnValue, loc);
3688}
3689
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003690void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
3691{
3692 ASSERT(!functionCall->isUserDefined());
Olli Etuahobd674552016-10-06 13:28:42 +01003693 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003694 TIntermNode *offset = nullptr;
3695 TIntermSequence *arguments = functionCall->getSequence();
3696 if (name.compare(0, 16, "texelFetchOffset") == 0 ||
3697 name.compare(0, 16, "textureLodOffset") == 0 ||
3698 name.compare(0, 20, "textureProjLodOffset") == 0 ||
3699 name.compare(0, 17, "textureGradOffset") == 0 ||
3700 name.compare(0, 21, "textureProjGradOffset") == 0)
3701 {
3702 offset = arguments->back();
3703 }
3704 else if (name.compare(0, 13, "textureOffset") == 0 ||
3705 name.compare(0, 17, "textureProjOffset") == 0)
3706 {
3707 // A bias parameter might follow the offset parameter.
3708 ASSERT(arguments->size() >= 3);
3709 offset = (*arguments)[2];
3710 }
3711 if (offset != nullptr)
3712 {
3713 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
3714 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
3715 {
3716 TString unmangledName = TFunction::unmangleName(name);
3717 error(functionCall->getLine(), "Texture offset must be a constant expression",
3718 unmangledName.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003719 }
3720 else
3721 {
3722 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
3723 size_t size = offsetConstantUnion->getType().getObjectSize();
3724 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
3725 for (size_t i = 0u; i < size; ++i)
3726 {
3727 int offsetValue = values[i].getIConst();
3728 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
3729 {
3730 std::stringstream tokenStream;
3731 tokenStream << offsetValue;
3732 std::string token = tokenStream.str();
3733 error(offset->getLine(), "Texture offset value out of valid range",
3734 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003735 }
3736 }
3737 }
3738 }
3739}
3740
Jamie Madillb98c3a82015-07-23 14:26:04 -04003741TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
3742 TIntermNode *paramNode,
3743 TIntermNode *thisNode,
3744 const TSourceLoc &loc,
3745 bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003746{
Jamie Madillb98c3a82015-07-23 14:26:04 -04003747 *fatalError = false;
3748 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003749 TIntermTyped *callNode = nullptr;
3750
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003751 if (thisNode != nullptr)
3752 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003753 TConstantUnion *unionArray = new TConstantUnion[1];
Jamie Madillb98c3a82015-07-23 14:26:04 -04003754 int arraySize = 0;
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003755 TIntermTyped *typedThis = thisNode->getAsTyped();
3756 if (fnCall->getName() != "length")
3757 {
3758 error(loc, "invalid method", fnCall->getName().c_str());
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003759 }
3760 else if (paramNode != nullptr)
3761 {
3762 error(loc, "method takes no parameters", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003763 }
3764 else if (typedThis == nullptr || !typedThis->isArray())
3765 {
3766 error(loc, "length can only be called on arrays", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003767 }
3768 else
3769 {
Olli Etuaho96e67382015-04-23 14:27:02 +03003770 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03003771 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003772 {
Olli Etuaho39282e12015-04-23 15:41:48 +03003773 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003774 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03003775 // (func()).length()
3776 // (int[3](0, 1, 2)).length()
Jamie Madillb98c3a82015-07-23 14:26:04 -04003777 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
3778 // expression.
3779 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
3780 // spec section 5.9 which allows "An array, vector or matrix expression with the
3781 // length method applied".
3782 error(loc, "length can only be called on array names, not on array expressions",
3783 "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003784 }
3785 }
Olli Etuaho96e67382015-04-23 14:27:02 +03003786 unionArray->setIConst(arraySize);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003787 callNode =
3788 intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003789 }
3790 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003791 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003792 // Then this should be a constructor.
Olli Etuaho856c4972016-08-08 11:38:39 +03003793 callNode = addConstructor(paramNode, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003794 }
3795 else
3796 {
3797 //
3798 // Not a constructor. Find it in the symbol table.
3799 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303800 const TFunction *fnCandidate;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003801 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003802 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003803 if (fnCandidate)
3804 {
3805 //
3806 // A declared function.
3807 //
Olli Etuaho383b7912016-08-05 11:22:59 +03003808 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003809 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003810 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003811 }
3812 op = fnCandidate->getBuiltInOp();
3813 if (builtIn && op != EOpNull)
3814 {
3815 //
3816 // A function call mapped to a built-in operation.
3817 //
3818 if (fnCandidate->getParamCount() == 1)
3819 {
3820 //
3821 // Treat it like a built-in unary operator.
3822 //
Olli Etuaho15c2ac32015-11-09 15:51:43 +02003823 TIntermAggregate *paramAgg = paramNode->getAsAggregate();
3824 paramNode = paramAgg->getSequence()->front();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003825 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc,
3826 &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003827 if (callNode == nullptr)
3828 {
3829 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003830 extraInfoStream
3831 << "built in unary operator function. Type: "
3832 << static_cast<TIntermTyped *>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003833 std::string extraInfo = extraInfoStream.str();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003834 error(paramNode->getLine(), " wrong operand type", "Internal Error",
3835 extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003836 *fatalError = true;
3837 return nullptr;
3838 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003839 }
3840 else
3841 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003842 TIntermAggregate *aggregate =
3843 intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003844 aggregate->setType(fnCandidate->getReturnType());
3845 aggregate->setPrecisionFromChildren();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02003846 if (aggregate->areChildrenConstQualified())
3847 {
3848 aggregate->getTypePointer()->setQualifier(EvqConst);
3849 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003850
3851 // Some built-in functions have out parameters too.
3852 functionCallLValueErrorCheck(fnCandidate, aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05303853
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003854 // See if we can constant fold a built-in. Note that this may be possible even
3855 // if it is not const-qualified.
Olli Etuahof119a262016-08-19 15:54:22 +03003856 TIntermTyped *foldedNode =
3857 intermediate.foldAggregateBuiltIn(aggregate, &mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05303858 if (foldedNode)
3859 {
Arun Patole274f0702015-05-05 13:33:30 +05303860 callNode = foldedNode;
3861 }
Olli Etuahob43846e2015-06-02 18:18:57 +03003862 else
3863 {
3864 callNode = aggregate;
3865 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003866 }
3867 }
3868 else
3869 {
3870 // This is a real function call
Jamie Madillb98c3a82015-07-23 14:26:04 -04003871 TIntermAggregate *aggregate =
3872 intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003873 aggregate->setType(fnCandidate->getReturnType());
3874
Jamie Madillb98c3a82015-07-23 14:26:04 -04003875 // this is how we know whether the given function is a builtIn function or a user
3876 // defined function
3877 // if builtIn == false, it's a userDefined -> could be an overloaded
3878 // builtIn function also
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003879 // if builtIn == true, it's definitely a builtIn function with EOpNull
3880 if (!builtIn)
3881 aggregate->setUserDefined();
Olli Etuahobd674552016-10-06 13:28:42 +01003882 aggregate->getFunctionSymbolInfo()->setFromFunction(*fnCandidate);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003883
Olli Etuahobd674552016-10-06 13:28:42 +01003884 // This needs to happen after the function info including name is set
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003885 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003886 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003887 aggregate->setBuiltInFunctionPrecision();
3888
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003889 checkTextureOffsetConst(aggregate);
3890 }
3891
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003892 callNode = aggregate;
3893
3894 functionCallLValueErrorCheck(fnCandidate, aggregate);
3895 }
3896 }
3897 else
3898 {
3899 // error message was put out by findFunction()
3900 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003901 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003902 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003903 callNode = intermediate.addConstantUnion(unionArray,
3904 TType(EbtFloat, EbpUndefined, EvqConst), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003905 }
3906 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003907 return callNode;
3908}
3909
Jamie Madillb98c3a82015-07-23 14:26:04 -04003910TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03003911 TIntermTyped *trueExpression,
3912 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03003913 const TSourceLoc &loc)
3914{
Olli Etuaho856c4972016-08-08 11:38:39 +03003915 checkIsScalarBool(loc, cond);
Olli Etuaho52901742015-04-15 13:42:45 +03003916
Olli Etuahod0bad2c2016-09-09 18:01:16 +03003917 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03003918 {
Olli Etuahod0bad2c2016-09-09 18:01:16 +03003919 binaryOpError(loc, ":", trueExpression->getCompleteString(),
3920 falseExpression->getCompleteString());
3921 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03003922 }
Olli Etuahoa2d53032015-04-15 14:14:44 +03003923 // ESSL1 sections 5.2 and 5.7:
3924 // ESSL3 section 5.7:
3925 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03003926 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03003927 {
3928 error(loc, "ternary operator is not allowed for structures or arrays", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03003929 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03003930 }
Corentin Wallez0d959252016-07-12 17:26:32 -04003931 // WebGL2 section 5.26, the following results in an error:
3932 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03003933 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04003934 {
3935 error(loc, "ternary operator is not allowed for void", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03003936 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04003937 }
3938
Olli Etuahod0bad2c2016-09-09 18:01:16 +03003939 return TIntermediate::AddTernarySelection(cond, trueExpression, falseExpression, loc);
Olli Etuaho52901742015-04-15 13:42:45 +03003940}
Olli Etuaho49300862015-02-20 14:54:49 +02003941
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003942//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003943// Parse an array of strings using yyparse.
3944//
3945// Returns 0 for success.
3946//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003947int PaParseStrings(size_t count,
3948 const char *const string[],
3949 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05303950 TParseContext *context)
3951{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003952 if ((count == 0) || (string == NULL))
3953 return 1;
3954
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003955 if (glslang_initialize(context))
3956 return 1;
3957
alokp@chromium.org408c45e2012-04-05 15:54:43 +00003958 int error = glslang_scan(count, string, length, context);
3959 if (!error)
3960 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003961
alokp@chromium.org73bc2982012-06-19 18:48:05 +00003962 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00003963
alokp@chromium.org6b495712012-06-29 00:06:58 +00003964 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003965}