blob: 79b2b06e3e5100d46e77d2a41cf54839a12288f0 [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 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002868 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002869 }
2870 else if (qualifierType == "packed")
2871 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002872 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002873 }
2874 else if (qualifierType == "std140")
2875 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002876 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002877 }
2878 else if (qualifierType == "row_major")
2879 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002880 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002881 }
2882 else if (qualifierType == "column_major")
2883 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002884 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002885 }
2886 else if (qualifierType == "location")
2887 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002888 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
2889 "location requires an argument");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002890 }
2891 else
2892 {
2893 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002894 }
2895
Jamie Madilla5efff92013-06-06 11:56:47 -04002896 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002897}
2898
Martin Radev802abe02016-08-04 17:48:32 +03002899void TParseContext::parseLocalSize(const TString &qualifierType,
2900 const TSourceLoc &qualifierTypeLine,
2901 int intValue,
2902 const TSourceLoc &intValueLine,
2903 const std::string &intValueString,
2904 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002905 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03002906{
Olli Etuaho856c4972016-08-08 11:38:39 +03002907 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03002908 if (intValue < 1)
2909 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002910 std::string errorMessage = std::string(getWorkGroupSizeString(index)) + " must be positive";
Martin Radev802abe02016-08-04 17:48:32 +03002911 error(intValueLine, "out of range:", intValueString.c_str(), errorMessage.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03002912 }
2913 (*localSize)[index] = intValue;
2914}
2915
Jamie Madillb98c3a82015-07-23 14:26:04 -04002916TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
2917 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002918 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302919 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002920{
Martin Radev802abe02016-08-04 17:48:32 +03002921 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002922
Martin Radev802abe02016-08-04 17:48:32 +03002923 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002924
Martin Radev802abe02016-08-04 17:48:32 +03002925 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002926 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002927 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002928 if (intValue < 0)
2929 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002930 error(intValueLine, "out of range:", intValueString.c_str(),
2931 "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002932 }
2933 else
2934 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002935 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03002936 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002937 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002938 }
Martin Radev802abe02016-08-04 17:48:32 +03002939 else if (qualifierType == "local_size_x")
2940 {
2941 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
2942 &qualifier.localSize);
2943 }
2944 else if (qualifierType == "local_size_y")
2945 {
2946 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
2947 &qualifier.localSize);
2948 }
2949 else if (qualifierType == "local_size_z")
2950 {
2951 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
2952 &qualifier.localSize);
2953 }
2954 else
2955 {
2956 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03002957 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002958
Jamie Madilla5efff92013-06-06 11:56:47 -04002959 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002960}
2961
Olli Etuaho613b9592016-09-05 12:05:53 +03002962TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
2963{
2964 return new TTypeQualifierBuilder(
2965 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
2966 mShaderVersion);
2967}
2968
Jamie Madillb98c3a82015-07-23 14:26:04 -04002969TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03002970 TLayoutQualifier rightQualifier,
2971 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002972{
Martin Radevc28888b2016-07-22 15:27:42 +03002973 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
2974 &mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002975}
2976
Martin Radev70866b82016-07-22 15:27:42 +03002977TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
2978 const TTypeQualifierBuilder &typeQualifierBuilder,
2979 TPublicType *typeSpecifier,
2980 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002981{
Olli Etuaho613b9592016-09-05 12:05:53 +03002982 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002983
Martin Radev70866b82016-07-22 15:27:42 +03002984 typeSpecifier->qualifier = typeQualifier.qualifier;
2985 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
2986 typeSpecifier->invariant = typeQualifier.invariant;
2987 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05302988 {
Martin Radev70866b82016-07-22 15:27:42 +03002989 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002990 }
Martin Radev70866b82016-07-22 15:27:42 +03002991 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002992}
2993
Jamie Madillb98c3a82015-07-23 14:26:04 -04002994TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
2995 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002996{
Martin Radev4a9cd802016-09-01 16:51:51 +03002997 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
2998 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002999
Martin Radev4a9cd802016-09-01 16:51:51 +03003000 checkIsNonVoid(typeSpecifier.getLine(), (*fieldList)[0]->name(), typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003001
Martin Radev4a9cd802016-09-01 16:51:51 +03003002 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003003
Arun Patole7e7e68d2015-05-22 12:02:25 +05303004 for (unsigned int i = 0; i < fieldList->size(); ++i)
3005 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003006 //
3007 // Careful not to replace already known aspects of type, like array-ness
3008 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303009 TType *type = (*fieldList)[i]->type();
Martin Radev4a9cd802016-09-01 16:51:51 +03003010 type->setBasicType(typeSpecifier.getBasicType());
3011 type->setPrimarySize(typeSpecifier.getPrimarySize());
3012 type->setSecondarySize(typeSpecifier.getSecondarySize());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003013 type->setPrecision(typeSpecifier.precision);
3014 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003015 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
Martin Radev70866b82016-07-22 15:27:42 +03003016 type->setInvariant(typeSpecifier.invariant);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003017
3018 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303019 if (type->isArray())
3020 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003021 checkIsValidTypeForArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003022 }
3023 if (typeSpecifier.array)
Olli Etuaho856c4972016-08-08 11:38:39 +03003024 type->setArraySize(static_cast<unsigned int>(typeSpecifier.arraySize));
Martin Radev4a9cd802016-09-01 16:51:51 +03003025 if (typeSpecifier.getUserDef())
Arun Patole7e7e68d2015-05-22 12:02:25 +05303026 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003027 type->setStruct(typeSpecifier.getUserDef()->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003028 }
3029
Martin Radev4a9cd802016-09-01 16:51:51 +03003030 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*fieldList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003031 }
3032
Jamie Madill98493dd2013-07-08 14:39:03 -04003033 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003034}
3035
Martin Radev4a9cd802016-09-01 16:51:51 +03003036TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
3037 const TSourceLoc &nameLine,
3038 const TString *structName,
3039 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003040{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303041 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003042 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003043
Jamie Madill9b820842015-02-12 10:40:10 -05003044 // Store a bool in the struct if we're at global scope, to allow us to
3045 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05003046 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003047
Jamie Madill98493dd2013-07-08 14:39:03 -04003048 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003049 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003050 checkIsNotReserved(nameLine, *structName);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303051 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
3052 if (!symbolTable.declare(userTypeDef))
3053 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003054 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003055 }
3056 }
3057
3058 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04003059 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003060 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003061 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04003062 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003063 switch (qualifier)
3064 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003065 case EvqGlobal:
3066 case EvqTemporary:
3067 break;
3068 default:
3069 error(field.line(), "invalid qualifier on struct member",
3070 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003071 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003072 }
Martin Radev70866b82016-07-22 15:27:42 +03003073 if (field.type()->isInvariant())
3074 {
3075 error(field.line(), "invalid qualifier on struct member", "invariant");
3076 }
3077
3078 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003079 }
3080
Martin Radev4a9cd802016-09-01 16:51:51 +03003081 TTypeSpecifierNonArray typeSpecifierNonArray;
3082 typeSpecifierNonArray.initialize(EbtStruct, structLine);
3083 typeSpecifierNonArray.userDef = structureType;
3084 typeSpecifierNonArray.isStructSpecifier = true;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003085 exitStructDeclaration();
3086
Martin Radev4a9cd802016-09-01 16:51:51 +03003087 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003088}
3089
Jamie Madillb98c3a82015-07-23 14:26:04 -04003090TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003091 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003092 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02003093{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003094 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003095 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02003096 init->isVector())
3097 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003098 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
3099 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003100 return nullptr;
3101 }
3102
Olli Etuahoac5274d2015-02-20 10:19:08 +02003103 if (statementList)
3104 {
3105 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
3106 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02003107 return nullptr;
3108 }
3109 }
3110
Olli Etuahoa3a36662015-02-17 13:46:51 +02003111 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3112 if (node == nullptr)
3113 {
3114 error(loc, "erroneous switch statement", "switch");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003115 return nullptr;
3116 }
3117 return node;
3118}
3119
3120TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3121{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003122 if (mSwitchNestingLevel == 0)
3123 {
3124 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003125 return nullptr;
3126 }
3127 if (condition == nullptr)
3128 {
3129 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003130 return nullptr;
3131 }
3132 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04003133 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02003134 {
3135 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003136 }
3137 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003138 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
3139 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
3140 // fold in case labels.
3141 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02003142 {
3143 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003144 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003145 TIntermCase *node = intermediate.addCase(condition, loc);
3146 if (node == nullptr)
3147 {
3148 error(loc, "erroneous case statement", "case");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003149 return nullptr;
3150 }
3151 return node;
3152}
3153
3154TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3155{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003156 if (mSwitchNestingLevel == 0)
3157 {
3158 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003159 return nullptr;
3160 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003161 TIntermCase *node = intermediate.addCase(nullptr, loc);
3162 if (node == nullptr)
3163 {
3164 error(loc, "erroneous default statement", "default");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003165 return nullptr;
3166 }
3167 return node;
3168}
3169
Jamie Madillb98c3a82015-07-23 14:26:04 -04003170TIntermTyped *TParseContext::createUnaryMath(TOperator op,
3171 TIntermTyped *child,
3172 const TSourceLoc &loc,
3173 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003174{
3175 if (child == nullptr)
3176 {
3177 return nullptr;
3178 }
3179
3180 switch (op)
3181 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003182 case EOpLogicalNot:
3183 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
3184 child->isVector())
3185 {
3186 return nullptr;
3187 }
3188 break;
3189 case EOpBitwiseNot:
3190 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3191 child->isMatrix() || child->isArray())
3192 {
3193 return nullptr;
3194 }
3195 break;
3196 case EOpPostIncrement:
3197 case EOpPreIncrement:
3198 case EOpPostDecrement:
3199 case EOpPreDecrement:
3200 case EOpNegative:
3201 case EOpPositive:
3202 if (child->getBasicType() == EbtStruct || child->getBasicType() == EbtBool ||
Olli Etuaho558b0382016-08-26 17:54:34 +03003203 child->isArray() || IsSampler(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04003204 {
3205 return nullptr;
3206 }
3207 // Operators for built-ins are already type checked against their prototype.
3208 default:
3209 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003210 }
3211
Olli Etuahof119a262016-08-19 15:54:22 +03003212 TIntermUnary *node = new TIntermUnary(op, child);
3213 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03003214
3215 TIntermTyped *foldedNode = node->fold(&mDiagnostics);
3216 if (foldedNode)
3217 return foldedNode;
3218
3219 return node;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003220}
3221
Olli Etuaho09b22472015-02-11 11:47:26 +02003222TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3223{
Olli Etuahof6c694b2015-03-26 14:50:53 +02003224 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003225 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003226 {
3227 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003228 return child;
3229 }
3230 return node;
3231}
3232
Jamie Madillb98c3a82015-07-23 14:26:04 -04003233TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
3234 TIntermTyped *child,
3235 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003236{
Olli Etuaho856c4972016-08-08 11:38:39 +03003237 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02003238 return addUnaryMath(op, child, loc);
3239}
3240
Jamie Madillb98c3a82015-07-23 14:26:04 -04003241bool TParseContext::binaryOpCommonCheck(TOperator op,
3242 TIntermTyped *left,
3243 TIntermTyped *right,
3244 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003245{
Olli Etuaho244be012016-08-18 15:26:02 +03003246 if (left->getType().getStruct() || right->getType().getStruct())
3247 {
3248 switch (op)
3249 {
3250 case EOpIndexDirectStruct:
3251 ASSERT(left->getType().getStruct());
3252 break;
3253 case EOpEqual:
3254 case EOpNotEqual:
3255 case EOpAssign:
3256 case EOpInitialize:
3257 if (left->getType() != right->getType())
3258 {
3259 return false;
3260 }
3261 break;
3262 default:
3263 error(loc, "Invalid operation for structs", GetOperatorString(op));
3264 return false;
3265 }
3266 }
3267
Olli Etuahod6b14282015-03-17 14:31:35 +02003268 if (left->isArray() || right->isArray())
3269 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003270 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003271 {
3272 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3273 return false;
3274 }
3275
3276 if (left->isArray() != right->isArray())
3277 {
3278 error(loc, "array / non-array mismatch", GetOperatorString(op));
3279 return false;
3280 }
3281
3282 switch (op)
3283 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003284 case EOpEqual:
3285 case EOpNotEqual:
3286 case EOpAssign:
3287 case EOpInitialize:
3288 break;
3289 default:
3290 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3291 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02003292 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003293 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003294 if (left->getArraySize() != right->getArraySize())
3295 {
3296 error(loc, "array size mismatch", GetOperatorString(op));
3297 return false;
3298 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003299 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003300
3301 // Check ops which require integer / ivec parameters
3302 bool isBitShift = false;
3303 switch (op)
3304 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003305 case EOpBitShiftLeft:
3306 case EOpBitShiftRight:
3307 case EOpBitShiftLeftAssign:
3308 case EOpBitShiftRightAssign:
3309 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3310 // check that the basic type is an integer type.
3311 isBitShift = true;
3312 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3313 {
3314 return false;
3315 }
3316 break;
3317 case EOpBitwiseAnd:
3318 case EOpBitwiseXor:
3319 case EOpBitwiseOr:
3320 case EOpBitwiseAndAssign:
3321 case EOpBitwiseXorAssign:
3322 case EOpBitwiseOrAssign:
3323 // It is enough to check the type of only one operand, since later it
3324 // is checked that the operand types match.
3325 if (!IsInteger(left->getBasicType()))
3326 {
3327 return false;
3328 }
3329 break;
3330 default:
3331 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003332 }
3333
3334 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3335 // So the basic type should usually match.
3336 if (!isBitShift && left->getBasicType() != right->getBasicType())
3337 {
3338 return false;
3339 }
3340
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003341 // Check that:
3342 // 1. Type sizes match exactly on ops that require that.
3343 // 2. Restrictions for structs that contain arrays or samplers are respected.
3344 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003345 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003346 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003347 case EOpAssign:
3348 case EOpInitialize:
3349 case EOpEqual:
3350 case EOpNotEqual:
3351 // ESSL 1.00 sections 5.7, 5.8, 5.9
3352 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
3353 {
3354 error(loc, "undefined operation for structs containing arrays",
3355 GetOperatorString(op));
3356 return false;
3357 }
3358 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3359 // we interpret the spec so that this extends to structs containing samplers,
3360 // similarly to ESSL 1.00 spec.
3361 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
3362 left->getType().isStructureContainingSamplers())
3363 {
3364 error(loc, "undefined operation for structs containing samplers",
3365 GetOperatorString(op));
3366 return false;
3367 }
3368 case EOpLessThan:
3369 case EOpGreaterThan:
3370 case EOpLessThanEqual:
3371 case EOpGreaterThanEqual:
3372 if ((left->getNominalSize() != right->getNominalSize()) ||
3373 (left->getSecondarySize() != right->getSecondarySize()))
3374 {
3375 return false;
3376 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003377 break;
3378 case EOpAdd:
3379 case EOpSub:
3380 case EOpDiv:
3381 case EOpIMod:
3382 case EOpBitShiftLeft:
3383 case EOpBitShiftRight:
3384 case EOpBitwiseAnd:
3385 case EOpBitwiseXor:
3386 case EOpBitwiseOr:
3387 case EOpAddAssign:
3388 case EOpSubAssign:
3389 case EOpDivAssign:
3390 case EOpIModAssign:
3391 case EOpBitShiftLeftAssign:
3392 case EOpBitShiftRightAssign:
3393 case EOpBitwiseAndAssign:
3394 case EOpBitwiseXorAssign:
3395 case EOpBitwiseOrAssign:
3396 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
3397 {
3398 return false;
3399 }
3400
3401 // Are the sizes compatible?
3402 if (left->getNominalSize() != right->getNominalSize() ||
3403 left->getSecondarySize() != right->getSecondarySize())
3404 {
3405 // If the nominal sizes of operands do not match:
3406 // One of them must be a scalar.
3407 if (!left->isScalar() && !right->isScalar())
3408 return false;
3409
3410 // In the case of compound assignment other than multiply-assign,
3411 // the right side needs to be a scalar. Otherwise a vector/matrix
3412 // would be assigned to a scalar. A scalar can't be shifted by a
3413 // vector either.
3414 if (!right->isScalar() &&
3415 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
3416 return false;
3417 }
3418 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003419 default:
3420 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003421 }
3422
Olli Etuahod6b14282015-03-17 14:31:35 +02003423 return true;
3424}
3425
Olli Etuaho1dded802016-08-18 18:13:13 +03003426bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
3427 const TType &left,
3428 const TType &right)
3429{
3430 switch (op)
3431 {
3432 case EOpMul:
3433 case EOpMulAssign:
3434 return left.getNominalSize() == right.getNominalSize() &&
3435 left.getSecondarySize() == right.getSecondarySize();
3436 case EOpVectorTimesScalar:
3437 return true;
3438 case EOpVectorTimesScalarAssign:
3439 ASSERT(!left.isMatrix() && !right.isMatrix());
3440 return left.isVector() && !right.isVector();
3441 case EOpVectorTimesMatrix:
3442 return left.getNominalSize() == right.getRows();
3443 case EOpVectorTimesMatrixAssign:
3444 ASSERT(!left.isMatrix() && right.isMatrix());
3445 return left.isVector() && left.getNominalSize() == right.getRows() &&
3446 left.getNominalSize() == right.getCols();
3447 case EOpMatrixTimesVector:
3448 return left.getCols() == right.getNominalSize();
3449 case EOpMatrixTimesScalar:
3450 return true;
3451 case EOpMatrixTimesScalarAssign:
3452 ASSERT(left.isMatrix() && !right.isMatrix());
3453 return !right.isVector();
3454 case EOpMatrixTimesMatrix:
3455 return left.getCols() == right.getRows();
3456 case EOpMatrixTimesMatrixAssign:
3457 ASSERT(left.isMatrix() && right.isMatrix());
3458 // We need to check two things:
3459 // 1. The matrix multiplication step is valid.
3460 // 2. The result will have the same number of columns as the lvalue.
3461 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
3462
3463 default:
3464 UNREACHABLE();
3465 return false;
3466 }
3467}
3468
Jamie Madillb98c3a82015-07-23 14:26:04 -04003469TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
3470 TIntermTyped *left,
3471 TIntermTyped *right,
3472 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02003473{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003474 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003475 return nullptr;
3476
Olli Etuahofc1806e2015-03-17 13:03:11 +02003477 switch (op)
3478 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003479 case EOpEqual:
3480 case EOpNotEqual:
3481 break;
3482 case EOpLessThan:
3483 case EOpGreaterThan:
3484 case EOpLessThanEqual:
3485 case EOpGreaterThanEqual:
Olli Etuaho244be012016-08-18 15:26:02 +03003486 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3487 !right->getType().getStruct());
3488 if (left->isMatrix() || left->isVector())
Jamie Madillb98c3a82015-07-23 14:26:04 -04003489 {
3490 return nullptr;
3491 }
3492 break;
3493 case EOpLogicalOr:
3494 case EOpLogicalXor:
3495 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03003496 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3497 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003498 if (left->getBasicType() != EbtBool || left->isMatrix() || left->isVector())
3499 {
3500 return nullptr;
3501 }
3502 break;
3503 case EOpAdd:
3504 case EOpSub:
3505 case EOpDiv:
3506 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03003507 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3508 !right->getType().getStruct());
3509 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04003510 {
3511 return nullptr;
3512 }
3513 break;
3514 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03003515 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3516 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003517 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03003518 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04003519 {
3520 return nullptr;
3521 }
3522 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003523 default:
3524 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003525 }
3526
Olli Etuaho1dded802016-08-18 18:13:13 +03003527 if (op == EOpMul)
3528 {
3529 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
3530 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
3531 {
3532 return nullptr;
3533 }
3534 }
3535
Olli Etuaho3fdec912016-08-18 15:08:06 +03003536 TIntermBinary *node = new TIntermBinary(op, left, right);
3537 node->setLine(loc);
3538
Olli Etuaho3fdec912016-08-18 15:08:06 +03003539 // See if we can fold constants.
3540 TIntermTyped *foldedNode = node->fold(&mDiagnostics);
3541 if (foldedNode)
3542 return foldedNode;
3543
3544 return node;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003545}
3546
Jamie Madillb98c3a82015-07-23 14:26:04 -04003547TIntermTyped *TParseContext::addBinaryMath(TOperator op,
3548 TIntermTyped *left,
3549 TIntermTyped *right,
3550 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003551{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003552 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003553 if (node == 0)
3554 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003555 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3556 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003557 return left;
3558 }
3559 return node;
3560}
3561
Jamie Madillb98c3a82015-07-23 14:26:04 -04003562TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
3563 TIntermTyped *left,
3564 TIntermTyped *right,
3565 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003566{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003567 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003568 if (node == 0)
3569 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003570 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3571 right->getCompleteString());
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003572 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02003573 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003574 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
3575 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003576 }
3577 return node;
3578}
3579
Jamie Madillb98c3a82015-07-23 14:26:04 -04003580TIntermTyped *TParseContext::createAssign(TOperator op,
3581 TIntermTyped *left,
3582 TIntermTyped *right,
3583 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003584{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003585 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003586 {
Olli Etuaho1dded802016-08-18 18:13:13 +03003587 if (op == EOpMulAssign)
3588 {
3589 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
3590 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
3591 {
3592 return nullptr;
3593 }
3594 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03003595 TIntermBinary *node = new TIntermBinary(op, left, right);
3596 node->setLine(loc);
3597
Olli Etuaho3fdec912016-08-18 15:08:06 +03003598 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02003599 }
3600 return nullptr;
3601}
3602
Jamie Madillb98c3a82015-07-23 14:26:04 -04003603TIntermTyped *TParseContext::addAssign(TOperator op,
3604 TIntermTyped *left,
3605 TIntermTyped *right,
3606 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003607{
3608 TIntermTyped *node = createAssign(op, left, right, loc);
3609 if (node == nullptr)
3610 {
3611 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02003612 return left;
3613 }
3614 return node;
3615}
3616
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003617TIntermTyped *TParseContext::addComma(TIntermTyped *left,
3618 TIntermTyped *right,
3619 const TSourceLoc &loc)
3620{
Corentin Wallez0d959252016-07-12 17:26:32 -04003621 // WebGL2 section 5.26, the following results in an error:
3622 // "Sequence operator applied to void, arrays, or structs containing arrays"
3623 if (mShaderSpec == SH_WEBGL2_SPEC && (left->isArray() || left->getBasicType() == EbtVoid ||
3624 left->getType().isStructureContainingArrays() ||
3625 right->isArray() || right->getBasicType() == EbtVoid ||
3626 right->getType().isStructureContainingArrays()))
3627 {
3628 error(loc,
3629 "sequence operator is not allowed for void, arrays, or structs containing arrays",
3630 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04003631 }
3632
Olli Etuaho4db7ded2016-10-13 12:23:11 +01003633 return TIntermediate::AddComma(left, right, loc, mShaderVersion);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003634}
3635
Olli Etuaho49300862015-02-20 14:54:49 +02003636TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3637{
3638 switch (op)
3639 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003640 case EOpContinue:
3641 if (mLoopNestingLevel <= 0)
3642 {
3643 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04003644 }
3645 break;
3646 case EOpBreak:
3647 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
3648 {
3649 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04003650 }
3651 break;
3652 case EOpReturn:
3653 if (mCurrentFunctionType->getBasicType() != EbtVoid)
3654 {
3655 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04003656 }
3657 break;
3658 default:
3659 // No checks for discard
3660 break;
Olli Etuaho49300862015-02-20 14:54:49 +02003661 }
3662 return intermediate.addBranch(op, loc);
3663}
3664
Jamie Madillb98c3a82015-07-23 14:26:04 -04003665TIntermBranch *TParseContext::addBranch(TOperator op,
3666 TIntermTyped *returnValue,
3667 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02003668{
3669 ASSERT(op == EOpReturn);
3670 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003671 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02003672 {
3673 error(loc, "void function cannot return a value", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02003674 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003675 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02003676 {
3677 error(loc, "function return is not matching type:", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02003678 }
3679 return intermediate.addBranch(op, returnValue, loc);
3680}
3681
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003682void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
3683{
3684 ASSERT(!functionCall->isUserDefined());
Olli Etuahobd674552016-10-06 13:28:42 +01003685 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003686 TIntermNode *offset = nullptr;
3687 TIntermSequence *arguments = functionCall->getSequence();
3688 if (name.compare(0, 16, "texelFetchOffset") == 0 ||
3689 name.compare(0, 16, "textureLodOffset") == 0 ||
3690 name.compare(0, 20, "textureProjLodOffset") == 0 ||
3691 name.compare(0, 17, "textureGradOffset") == 0 ||
3692 name.compare(0, 21, "textureProjGradOffset") == 0)
3693 {
3694 offset = arguments->back();
3695 }
3696 else if (name.compare(0, 13, "textureOffset") == 0 ||
3697 name.compare(0, 17, "textureProjOffset") == 0)
3698 {
3699 // A bias parameter might follow the offset parameter.
3700 ASSERT(arguments->size() >= 3);
3701 offset = (*arguments)[2];
3702 }
3703 if (offset != nullptr)
3704 {
3705 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
3706 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
3707 {
3708 TString unmangledName = TFunction::unmangleName(name);
3709 error(functionCall->getLine(), "Texture offset must be a constant expression",
3710 unmangledName.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003711 }
3712 else
3713 {
3714 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
3715 size_t size = offsetConstantUnion->getType().getObjectSize();
3716 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
3717 for (size_t i = 0u; i < size; ++i)
3718 {
3719 int offsetValue = values[i].getIConst();
3720 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
3721 {
3722 std::stringstream tokenStream;
3723 tokenStream << offsetValue;
3724 std::string token = tokenStream.str();
3725 error(offset->getLine(), "Texture offset value out of valid range",
3726 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003727 }
3728 }
3729 }
3730 }
3731}
3732
Jamie Madillb98c3a82015-07-23 14:26:04 -04003733TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
3734 TIntermNode *paramNode,
3735 TIntermNode *thisNode,
3736 const TSourceLoc &loc,
3737 bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003738{
Jamie Madillb98c3a82015-07-23 14:26:04 -04003739 *fatalError = false;
3740 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003741 TIntermTyped *callNode = nullptr;
3742
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003743 if (thisNode != nullptr)
3744 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003745 TConstantUnion *unionArray = new TConstantUnion[1];
Jamie Madillb98c3a82015-07-23 14:26:04 -04003746 int arraySize = 0;
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003747 TIntermTyped *typedThis = thisNode->getAsTyped();
3748 if (fnCall->getName() != "length")
3749 {
3750 error(loc, "invalid method", fnCall->getName().c_str());
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003751 }
3752 else if (paramNode != nullptr)
3753 {
3754 error(loc, "method takes no parameters", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003755 }
3756 else if (typedThis == nullptr || !typedThis->isArray())
3757 {
3758 error(loc, "length can only be called on arrays", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003759 }
3760 else
3761 {
Olli Etuaho96e67382015-04-23 14:27:02 +03003762 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03003763 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003764 {
Olli Etuaho39282e12015-04-23 15:41:48 +03003765 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003766 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03003767 // (func()).length()
3768 // (int[3](0, 1, 2)).length()
Jamie Madillb98c3a82015-07-23 14:26:04 -04003769 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
3770 // expression.
3771 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
3772 // spec section 5.9 which allows "An array, vector or matrix expression with the
3773 // length method applied".
3774 error(loc, "length can only be called on array names, not on array expressions",
3775 "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003776 }
3777 }
Olli Etuaho96e67382015-04-23 14:27:02 +03003778 unionArray->setIConst(arraySize);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003779 callNode =
3780 intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003781 }
3782 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003783 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003784 // Then this should be a constructor.
Olli Etuaho856c4972016-08-08 11:38:39 +03003785 callNode = addConstructor(paramNode, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003786 }
3787 else
3788 {
3789 //
3790 // Not a constructor. Find it in the symbol table.
3791 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303792 const TFunction *fnCandidate;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003793 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003794 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003795 if (fnCandidate)
3796 {
3797 //
3798 // A declared function.
3799 //
Olli Etuaho383b7912016-08-05 11:22:59 +03003800 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003801 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003802 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003803 }
3804 op = fnCandidate->getBuiltInOp();
3805 if (builtIn && op != EOpNull)
3806 {
3807 //
3808 // A function call mapped to a built-in operation.
3809 //
3810 if (fnCandidate->getParamCount() == 1)
3811 {
3812 //
3813 // Treat it like a built-in unary operator.
3814 //
Olli Etuaho15c2ac32015-11-09 15:51:43 +02003815 TIntermAggregate *paramAgg = paramNode->getAsAggregate();
3816 paramNode = paramAgg->getSequence()->front();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003817 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc,
3818 &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003819 if (callNode == nullptr)
3820 {
3821 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003822 extraInfoStream
3823 << "built in unary operator function. Type: "
3824 << static_cast<TIntermTyped *>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003825 std::string extraInfo = extraInfoStream.str();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003826 error(paramNode->getLine(), " wrong operand type", "Internal Error",
3827 extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003828 *fatalError = true;
3829 return nullptr;
3830 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003831 }
3832 else
3833 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003834 TIntermAggregate *aggregate =
3835 intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003836 aggregate->setType(fnCandidate->getReturnType());
3837 aggregate->setPrecisionFromChildren();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02003838 if (aggregate->areChildrenConstQualified())
3839 {
3840 aggregate->getTypePointer()->setQualifier(EvqConst);
3841 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003842
3843 // Some built-in functions have out parameters too.
3844 functionCallLValueErrorCheck(fnCandidate, aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05303845
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003846 // See if we can constant fold a built-in. Note that this may be possible even
3847 // if it is not const-qualified.
Olli Etuahof119a262016-08-19 15:54:22 +03003848 TIntermTyped *foldedNode =
3849 intermediate.foldAggregateBuiltIn(aggregate, &mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05303850 if (foldedNode)
3851 {
Arun Patole274f0702015-05-05 13:33:30 +05303852 callNode = foldedNode;
3853 }
Olli Etuahob43846e2015-06-02 18:18:57 +03003854 else
3855 {
3856 callNode = aggregate;
3857 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003858 }
3859 }
3860 else
3861 {
3862 // This is a real function call
Jamie Madillb98c3a82015-07-23 14:26:04 -04003863 TIntermAggregate *aggregate =
3864 intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003865 aggregate->setType(fnCandidate->getReturnType());
3866
Jamie Madillb98c3a82015-07-23 14:26:04 -04003867 // this is how we know whether the given function is a builtIn function or a user
3868 // defined function
3869 // if builtIn == false, it's a userDefined -> could be an overloaded
3870 // builtIn function also
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003871 // if builtIn == true, it's definitely a builtIn function with EOpNull
3872 if (!builtIn)
3873 aggregate->setUserDefined();
Olli Etuahobd674552016-10-06 13:28:42 +01003874 aggregate->getFunctionSymbolInfo()->setFromFunction(*fnCandidate);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003875
Olli Etuahobd674552016-10-06 13:28:42 +01003876 // This needs to happen after the function info including name is set
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003877 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003878 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003879 aggregate->setBuiltInFunctionPrecision();
3880
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003881 checkTextureOffsetConst(aggregate);
3882 }
3883
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003884 callNode = aggregate;
3885
3886 functionCallLValueErrorCheck(fnCandidate, aggregate);
3887 }
3888 }
3889 else
3890 {
3891 // error message was put out by findFunction()
3892 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003893 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003894 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003895 callNode = intermediate.addConstantUnion(unionArray,
3896 TType(EbtFloat, EbpUndefined, EvqConst), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003897 }
3898 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003899 return callNode;
3900}
3901
Jamie Madillb98c3a82015-07-23 14:26:04 -04003902TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03003903 TIntermTyped *trueExpression,
3904 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03003905 const TSourceLoc &loc)
3906{
Olli Etuaho856c4972016-08-08 11:38:39 +03003907 checkIsScalarBool(loc, cond);
Olli Etuaho52901742015-04-15 13:42:45 +03003908
Olli Etuahod0bad2c2016-09-09 18:01:16 +03003909 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03003910 {
Olli Etuahod0bad2c2016-09-09 18:01:16 +03003911 binaryOpError(loc, ":", trueExpression->getCompleteString(),
3912 falseExpression->getCompleteString());
3913 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03003914 }
Olli Etuahoa2d53032015-04-15 14:14:44 +03003915 // ESSL1 sections 5.2 and 5.7:
3916 // ESSL3 section 5.7:
3917 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03003918 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03003919 {
3920 error(loc, "ternary operator is not allowed for structures or arrays", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03003921 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03003922 }
Corentin Wallez0d959252016-07-12 17:26:32 -04003923 // WebGL2 section 5.26, the following results in an error:
3924 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03003925 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04003926 {
3927 error(loc, "ternary operator is not allowed for void", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03003928 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04003929 }
3930
Olli Etuahod0bad2c2016-09-09 18:01:16 +03003931 return TIntermediate::AddTernarySelection(cond, trueExpression, falseExpression, loc);
Olli Etuaho52901742015-04-15 13:42:45 +03003932}
Olli Etuaho49300862015-02-20 14:54:49 +02003933
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003934//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003935// Parse an array of strings using yyparse.
3936//
3937// Returns 0 for success.
3938//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003939int PaParseStrings(size_t count,
3940 const char *const string[],
3941 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05303942 TParseContext *context)
3943{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003944 if ((count == 0) || (string == NULL))
3945 return 1;
3946
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003947 if (glslang_initialize(context))
3948 return 1;
3949
alokp@chromium.org408c45e2012-04-05 15:54:43 +00003950 int error = glslang_scan(count, string, length, context);
3951 if (!error)
3952 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003953
alokp@chromium.org73bc2982012-06-19 18:48:05 +00003954 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00003955
alokp@chromium.org6b495712012-06-29 00:06:58 +00003956 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003957}