blob: 2fcadba86c837d9ccc8ffcac06d83ce7abba22f1 [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
2062TIntermAggregate *TParseContext::addFunctionDefinition(const TFunction &function,
2063 TIntermAggregate *functionPrototype,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01002064 TIntermBlock *functionBody,
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002065 const TSourceLoc &location)
2066{
Olli Etuahof51fdd22016-10-03 10:03:40 +01002067 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002068 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
2069 {
2070 error(location, "function does not return a value:", "", function.getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002071 }
2072
Olli Etuahof51fdd22016-10-03 10:03:40 +01002073 TIntermAggregate *functionNode = new TIntermAggregate(EOpFunction);
2074 functionNode->setLine(location);
2075
2076 ASSERT(functionPrototype != nullptr);
2077 functionNode->getSequence()->push_back(functionPrototype);
2078
2079 if (functionBody == nullptr)
2080 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01002081 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002082 functionBody->setLine(location);
2083 }
2084 functionNode->getSequence()->push_back(functionBody);
2085
Olli Etuahobd674552016-10-06 13:28:42 +01002086 functionNode->getFunctionSymbolInfo()->setFromFunction(function);
Olli Etuahof51fdd22016-10-03 10:03:40 +01002087 functionNode->setType(function.getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002088
2089 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002090 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002091}
2092
Olli Etuaho476197f2016-10-11 13:59:08 +01002093void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
2094 TFunction **function,
2095 TIntermAggregate **aggregateOut)
Jamie Madill185fb402015-06-12 15:48:48 -04002096{
Olli Etuaho476197f2016-10-11 13:59:08 +01002097 ASSERT(function);
2098 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002099 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01002100 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002101
2102 if (builtIn)
2103 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002104 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002105 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002106 else
Jamie Madill185fb402015-06-12 15:48:48 -04002107 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002108 TFunction *prevDec = static_cast<TFunction *>(
2109 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
2110
2111 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
2112 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
2113 // occurance.
2114 if (*function != prevDec)
2115 {
2116 // Swap the parameters of the previous declaration to the parameters of the function
2117 // definition (parameter names may differ).
2118 prevDec->swapParameters(**function);
2119
2120 // The function definition will share the same symbol as any previous declaration.
2121 *function = prevDec;
2122 }
2123
2124 if ((*function)->isDefined())
2125 {
2126 error(location, "function already has a body", (*function)->getName().c_str());
2127 }
2128
2129 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04002130 }
Jamie Madill185fb402015-06-12 15:48:48 -04002131
2132 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuaho476197f2016-10-11 13:59:08 +01002133 if ((*function)->getName() == "main")
Jamie Madill185fb402015-06-12 15:48:48 -04002134 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002135 if ((*function)->getParamCount() > 0)
Jamie Madill185fb402015-06-12 15:48:48 -04002136 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002137 error(location, "function cannot take any parameter(s)",
2138 (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002139 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002140 if ((*function)->getReturnType().getBasicType() != EbtVoid)
Jamie Madill185fb402015-06-12 15:48:48 -04002141 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002142 error(location, "", (*function)->getReturnType().getBasicString(),
Jamie Madillb98c3a82015-07-23 14:26:04 -04002143 "main function cannot return a value");
Jamie Madill185fb402015-06-12 15:48:48 -04002144 }
2145 }
2146
2147 //
2148 // Remember the return type for later checking for RETURN statements.
2149 //
Olli Etuaho476197f2016-10-11 13:59:08 +01002150 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002151 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04002152
2153 //
2154 // Insert parameters into the symbol table.
2155 // If the parameter has no name, it's not an error, just don't insert it
2156 // (could be used for unused args).
2157 //
2158 // Also, accumulate the list of parameters into the HIL, so lower level code
2159 // knows where to find parameters.
2160 //
2161 TIntermAggregate *paramNodes = new TIntermAggregate;
Olli Etuaho476197f2016-10-11 13:59:08 +01002162 for (size_t i = 0; i < (*function)->getParamCount(); i++)
Jamie Madill185fb402015-06-12 15:48:48 -04002163 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002164 const TConstParameter &param = (*function)->getParam(i);
Jamie Madill185fb402015-06-12 15:48:48 -04002165 if (param.name != 0)
2166 {
2167 TVariable *variable = new TVariable(param.name, *param.type);
2168 //
2169 // Insert the parameters with name in the symbol table.
2170 //
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002171 if (!symbolTable.declare(variable))
2172 {
Jamie Madill185fb402015-06-12 15:48:48 -04002173 error(location, "redefinition", variable->getName().c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002174 paramNodes = intermediate.growAggregate(
2175 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
2176 continue;
Jamie Madill185fb402015-06-12 15:48:48 -04002177 }
2178
2179 //
2180 // Add the parameter to the HIL
2181 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002182 TIntermSymbol *symbol = intermediate.addSymbol(
2183 variable->getUniqueId(), variable->getName(), variable->getType(), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002184
2185 paramNodes = intermediate.growAggregate(paramNodes, symbol, location);
2186 }
2187 else
2188 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002189 paramNodes = intermediate.growAggregate(
2190 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002191 }
2192 }
2193 intermediate.setAggregateOperator(paramNodes, EOpParameters, location);
2194 *aggregateOut = paramNodes;
2195 setLoopNestingLevel(0);
2196}
2197
Jamie Madillb98c3a82015-07-23 14:26:04 -04002198TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002199{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002200 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002201 // We don't know at this point whether this is a function definition or a prototype.
2202 // The definition production code will check for redefinitions.
2203 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002204 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002205 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
2206 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002207 //
2208 TFunction *prevDec =
2209 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302210
2211 if (getShaderVersion() >= 300 && symbolTable.hasUnmangledBuiltIn(function->getName().c_str()))
2212 {
2213 // With ESSL 3.00, names of built-in functions cannot be redeclared as functions.
2214 // Therefore overloading or redefining builtin functions is an error.
2215 error(location, "Name of a built-in function cannot be redeclared as function",
2216 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302217 }
2218 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002219 {
2220 if (prevDec->getReturnType() != function->getReturnType())
2221 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002222 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002223 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04002224 }
2225 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2226 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002227 if (prevDec->getParam(i).type->getQualifier() !=
2228 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002229 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002230 error(location,
2231 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002232 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04002233 }
2234 }
2235 }
2236
2237 //
2238 // Check for previously declared variables using the same name.
2239 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002240 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002241 if (prevSym)
2242 {
2243 if (!prevSym->isFunction())
2244 {
2245 error(location, "redefinition", function->getName().c_str(), "function");
Jamie Madill185fb402015-06-12 15:48:48 -04002246 }
2247 }
2248 else
2249 {
2250 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01002251 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04002252 }
2253
2254 // We're at the inner scope level of the function's arguments and body statement.
2255 // Add the function prototype to the surrounding scope instead.
2256 symbolTable.getOuterLevel()->insert(function);
2257
2258 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002259 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2260 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002261 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2262 //
2263 return function;
2264}
2265
Olli Etuaho9de84a52016-06-14 17:36:01 +03002266TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
2267 const TString *name,
2268 const TSourceLoc &location)
2269{
2270 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
2271 {
2272 error(location, "no qualifiers allowed for function return",
2273 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03002274 }
2275 if (!type.layoutQualifier.isEmpty())
2276 {
2277 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03002278 }
2279 // make sure a sampler is not involved as well...
Martin Radev4a9cd802016-09-01 16:51:51 +03002280 checkIsNotSampler(location, type.typeSpecifierNonArray,
2281 "samplers can't be function return values");
Olli Etuahoe29324f2016-06-15 10:58:03 +03002282 if (mShaderVersion < 300)
2283 {
2284 // Array return values are forbidden, but there's also no valid syntax for declaring array
2285 // return values in ESSL 1.00.
2286 ASSERT(type.arraySize == 0 || mDiagnostics.numErrors() > 0);
2287
2288 if (type.isStructureContainingArrays())
2289 {
2290 // ESSL 1.00.17 section 6.1 Function Definitions
2291 error(location, "structures containing arrays can't be function return values",
2292 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03002293 }
2294 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03002295
2296 // Add the function as a prototype after parsing it (we do not support recursion)
2297 return new TFunction(name, new TType(type));
2298}
2299
Jamie Madill06145232015-05-13 13:10:01 -04002300TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002301{
Jamie Madill06145232015-05-13 13:10:01 -04002302 TPublicType publicType = publicTypeIn;
Martin Radev4a9cd802016-09-01 16:51:51 +03002303 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02002304 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002305 error(publicType.getLine(), "constructor can't be a structure definition",
2306 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02002307 }
2308
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002309 TOperator op = EOpNull;
Martin Radev4a9cd802016-09-01 16:51:51 +03002310 if (publicType.getUserDef())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002311 {
2312 op = EOpConstructStruct;
2313 }
2314 else
2315 {
Geoff Lang156d7192016-07-21 16:11:00 -04002316 op = sh::TypeToConstructorOperator(TType(publicType));
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002317 if (op == EOpNull)
2318 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002319 error(publicType.getLine(), "cannot construct this type",
2320 getBasicString(publicType.getBasicType()));
2321 publicType.setBasicType(EbtFloat);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002322 op = EOpConstructFloat;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002323 }
2324 }
2325
2326 TString tempString;
Dmitry Skiba7f17a502015-06-22 15:08:39 -07002327 const TType *type = new TType(publicType);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002328 return new TFunction(&tempString, type, op);
2329}
2330
Jamie Madillb98c3a82015-07-23 14:26:04 -04002331// This function is used to test for the correctness of the parameters passed to various constructor
2332// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002333//
Olli Etuaho856c4972016-08-08 11:38:39 +03002334// 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 +00002335//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002336TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002337 TOperator op,
2338 TFunction *fnCall,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302339 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002340{
Olli Etuaho856c4972016-08-08 11:38:39 +03002341 TType type = fnCall->getReturnType();
2342 if (type.isUnsizedArray())
2343 {
2344 type.setArraySize(static_cast<unsigned int>(fnCall->getParamCount()));
2345 }
2346 bool constType = true;
2347 for (size_t i = 0; i < fnCall->getParamCount(); ++i)
2348 {
2349 const TConstParameter &param = fnCall->getParam(i);
2350 if (param.type->getQualifier() != EvqConst)
2351 constType = false;
2352 }
2353 if (constType)
2354 type.setQualifier(EvqConst);
2355
Olli Etuaho8a176262016-08-16 14:23:01 +03002356 if (!checkConstructorArguments(line, arguments, *fnCall, op, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03002357 {
2358 TIntermTyped *dummyNode = intermediate.setAggregateOperator(nullptr, op, line);
2359 dummyNode->setType(type);
2360 return dummyNode;
2361 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002362 TIntermAggregate *constructor = arguments->getAsAggregate();
2363 ASSERT(constructor != nullptr);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002364
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002365 // Turn the argument list itself into a constructor
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002366 constructor->setOp(op);
2367 constructor->setLine(line);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002368 ASSERT(constructor->isConstructor());
2369
2370 // Need to set type before setPrecisionFromChildren() because bool doesn't have precision.
Olli Etuaho856c4972016-08-08 11:38:39 +03002371 constructor->setType(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002372
Olli Etuaho21203702014-11-13 16:16:21 +02002373 // Structs should not be precision qualified, the individual members may be.
2374 // Built-in types on the other hand should be precision qualified.
2375 if (op != EOpConstructStruct)
2376 {
2377 constructor->setPrecisionFromChildren();
Olli Etuaho856c4972016-08-08 11:38:39 +03002378 type.setPrecision(constructor->getPrecision());
Olli Etuaho21203702014-11-13 16:16:21 +02002379 }
2380
Olli Etuaho856c4972016-08-08 11:38:39 +03002381 constructor->setType(type);
2382
Olli Etuahof119a262016-08-19 15:54:22 +03002383 TIntermTyped *constConstructor = intermediate.foldAggregateBuiltIn(constructor, &mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002384 if (constConstructor)
2385 {
2386 return constConstructor;
2387 }
2388
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002389 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002390}
2391
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002392//
2393// Interface/uniform blocks
2394//
Martin Radev70866b82016-07-22 15:27:42 +03002395TIntermAggregate *TParseContext::addInterfaceBlock(
2396 const TTypeQualifierBuilder &typeQualifierBuilder,
2397 const TSourceLoc &nameLine,
2398 const TString &blockName,
2399 TFieldList *fieldList,
2400 const TString *instanceName,
2401 const TSourceLoc &instanceLine,
2402 TIntermTyped *arrayIndex,
2403 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002404{
Olli Etuaho856c4972016-08-08 11:38:39 +03002405 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002406
Olli Etuaho613b9592016-09-05 12:05:53 +03002407 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03002408
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002409 if (typeQualifier.qualifier != EvqUniform)
2410 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302411 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
2412 "interface blocks must be uniform");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002413 }
2414
Martin Radev70866b82016-07-22 15:27:42 +03002415 if (typeQualifier.invariant)
2416 {
2417 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
2418 }
2419
Jamie Madill099c0f32013-06-20 11:55:52 -04002420 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03002421 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002422
Jamie Madill099c0f32013-06-20 11:55:52 -04002423 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2424 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002425 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002426 }
2427
Jamie Madill1566ef72013-06-20 11:55:54 -04002428 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2429 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002430 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002431 }
2432
Olli Etuaho856c4972016-08-08 11:38:39 +03002433 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002434
Arun Patole7e7e68d2015-05-22 12:02:25 +05302435 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2436 if (!symbolTable.declare(blockNameSymbol))
2437 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002438 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002439 }
2440
Jamie Madill98493dd2013-07-08 14:39:03 -04002441 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302442 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2443 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002444 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302445 TType *fieldType = field->type();
2446 if (IsSampler(fieldType->getBasicType()))
2447 {
2448 error(field->line(), "unsupported type", fieldType->getBasicString(),
2449 "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002450 }
2451
Jamie Madill98493dd2013-07-08 14:39:03 -04002452 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002453 switch (qualifier)
2454 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002455 case EvqGlobal:
2456 case EvqUniform:
2457 break;
2458 default:
2459 error(field->line(), "invalid qualifier on interface block member",
2460 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04002461 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002462 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002463
Martin Radev70866b82016-07-22 15:27:42 +03002464 if (fieldType->isInvariant())
2465 {
2466 error(field->line(), "invalid qualifier on interface block member", "invariant");
2467 }
2468
Jamie Madilla5efff92013-06-06 11:56:47 -04002469 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002470 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03002471 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
Jamie Madill099c0f32013-06-20 11:55:52 -04002472
Jamie Madill98493dd2013-07-08 14:39:03 -04002473 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002474 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002475 error(field->line(), "invalid layout qualifier:",
2476 getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04002477 }
2478
Jamie Madill98493dd2013-07-08 14:39:03 -04002479 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002480 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002481 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002482 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002483 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002484 {
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002485 warning(field->line(), "extraneous layout qualifier:",
Jamie Madillb98c3a82015-07-23 14:26:04 -04002486 getMatrixPackingString(fieldLayoutQualifier.matrixPacking),
2487 "only has an effect on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04002488 }
2489
Jamie Madill98493dd2013-07-08 14:39:03 -04002490 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002491 }
2492
Jamie Madill98493dd2013-07-08 14:39:03 -04002493 // add array index
Olli Etuaho856c4972016-08-08 11:38:39 +03002494 unsigned int arraySize = 0;
2495 if (arrayIndex != nullptr)
Jamie Madill98493dd2013-07-08 14:39:03 -04002496 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002497 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04002498 }
2499
Jamie Madillb98c3a82015-07-23 14:26:04 -04002500 TInterfaceBlock *interfaceBlock =
2501 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2502 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
2503 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002504
2505 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04002506 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002507
Jamie Madill98493dd2013-07-08 14:39:03 -04002508 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002509 {
2510 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002511 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2512 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002513 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302514 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002515
2516 // set parent pointer of the field variable
2517 fieldType->setInterfaceBlock(interfaceBlock);
2518
Arun Patole7e7e68d2015-05-22 12:02:25 +05302519 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002520 fieldVariable->setQualifier(typeQualifier.qualifier);
2521
Arun Patole7e7e68d2015-05-22 12:02:25 +05302522 if (!symbolTable.declare(fieldVariable))
2523 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002524 error(field->line(), "redefinition", field->name().c_str(),
2525 "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002526 }
2527 }
2528 }
2529 else
2530 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002531 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03002532
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002533 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302534 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002535 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002536
Arun Patole7e7e68d2015-05-22 12:02:25 +05302537 if (!symbolTable.declare(instanceTypeDef))
2538 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002539 error(instanceLine, "redefinition", instanceName->c_str(),
2540 "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002541 }
2542
Jamie Madillb98c3a82015-07-23 14:26:04 -04002543 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002544 symbolName = instanceTypeDef->getName();
2545 }
2546
Olli Etuaho32db19b2016-10-04 14:43:16 +01002547 TIntermAggregate *aggregate = TIntermediate::MakeAggregate(
Jamie Madillb98c3a82015-07-23 14:26:04 -04002548 intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line),
2549 nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002550 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04002551
2552 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002553 return aggregate;
2554}
2555
Olli Etuaho383b7912016-08-05 11:22:59 +03002556void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002557{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002558 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002559
2560 // Embedded structure definitions are not supported per GLSL ES spec.
2561 // They aren't allowed in GLSL either, but we need to detect this here
2562 // so we don't rely on the GLSL compiler to catch it.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302563 if (mStructNestingLevel > 1)
2564 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002565 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002566 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002567}
2568
2569void TParseContext::exitStructDeclaration()
2570{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002571 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002572}
2573
Jamie Madillb98c3a82015-07-23 14:26:04 -04002574namespace
2575{
kbr@chromium.org476541f2011-10-27 21:14:51 +00002576const int kWebGLMaxStructNesting = 4;
2577
2578} // namespace
2579
Olli Etuaho8a176262016-08-16 14:23:01 +03002580void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002581{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302582 if (!IsWebGLBasedSpec(mShaderSpec))
2583 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002584 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002585 }
2586
Arun Patole7e7e68d2015-05-22 12:02:25 +05302587 if (field.type()->getBasicType() != EbtStruct)
2588 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002589 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002590 }
2591
2592 // We're already inside a structure definition at this point, so add
2593 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302594 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2595 {
Jamie Madill41a49272014-03-18 16:10:13 -04002596 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002597 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
2598 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04002599 std::string reason = reasonStream.str();
2600 error(line, reason.c_str(), field.name().c_str(), "");
Olli Etuaho8a176262016-08-16 14:23:01 +03002601 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002602 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002603}
2604
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002605//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002606// Parse an array index expression
2607//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002608TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
2609 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302610 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002611{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002612 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2613 {
2614 if (baseExpression->getAsSymbolNode())
2615 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302616 error(location, " left of '[' is not of type array, matrix, or vector ",
2617 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002618 }
2619 else
2620 {
2621 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2622 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002623
2624 TConstantUnion *unionArray = new TConstantUnion[1];
2625 unionArray->setFConst(0.0f);
2626 return intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst),
2627 location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002628 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002629
Jamie Madill21c1e452014-12-29 11:33:41 -05002630 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2631
Olli Etuaho36b05142015-11-12 13:10:42 +02002632 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
2633 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
2634 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
2635 // index is a constant expression.
2636 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
2637 {
2638 if (baseExpression->isInterfaceBlock())
2639 {
2640 error(
2641 location, "", "[",
2642 "array indexes for interface blocks arrays must be constant integral expressions");
Olli Etuaho36b05142015-11-12 13:10:42 +02002643 }
2644 else if (baseExpression->getQualifier() == EvqFragmentOut)
2645 {
2646 error(location, "", "[",
2647 "array indexes for fragment outputs must be constant integral expressions");
Olli Etuaho36b05142015-11-12 13:10:42 +02002648 }
Olli Etuaho3e960462015-11-12 15:58:39 +02002649 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
2650 {
2651 error(location, "", "[", "array index for gl_FragData must be constant zero");
Olli Etuaho3e960462015-11-12 15:58:39 +02002652 }
Olli Etuaho36b05142015-11-12 13:10:42 +02002653 }
2654
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002655 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002656 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002657 // If an out-of-range index is not qualified as constant, the behavior in the spec is
2658 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
2659 // constant fold expressions that are not constant expressions). The most compatible way to
2660 // handle this case is to report a warning instead of an error and force the index to be in
2661 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002662 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Jamie Madill21c1e452014-12-29 11:33:41 -05002663 int index = indexConstantUnion->getIConst(0);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002664
2665 int safeIndex = -1;
2666
2667 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04002668 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002669 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03002670 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002671 if (mShaderSpec == SH_WEBGL2_SPEC)
2672 {
2673 // Error has been already generated if index is not const.
2674 if (indexExpression->getQualifier() == EvqConst)
2675 {
2676 error(location, "", "[",
2677 "array index for gl_FragData must be constant zero");
2678 }
2679 safeIndex = 0;
2680 }
2681 else if (!isExtensionEnabled("GL_EXT_draw_buffers"))
2682 {
2683 outOfRangeError(outOfRangeIndexIsError, location, "", "[",
2684 "array index for gl_FragData must be zero when "
2685 "GL_EXT_draw_buffers is disabled");
2686 safeIndex = 0;
2687 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03002688 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002689 // Only do generic out-of-range check if similar error hasn't already been reported.
2690 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03002691 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002692 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
2693 baseExpression->getArraySize(),
2694 "array index out of range", "[]");
2695 }
2696 }
2697 else if (baseExpression->isMatrix())
2698 {
2699 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03002700 baseExpression->getType().getCols(),
2701 "matrix field selection out of range", "[]");
Jamie Madill7164cf42013-07-08 13:30:59 -04002702 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002703 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04002704 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002705 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
2706 baseExpression->getType().getNominalSize(),
2707 "vector field selection out of range", "[]");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002708 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002709
2710 ASSERT(safeIndex >= 0);
2711 // Data of constant unions can't be changed, because it may be shared with other
2712 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
2713 // sanitized object.
2714 if (safeIndex != index)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002715 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002716 TConstantUnion *safeConstantUnion = new TConstantUnion();
2717 safeConstantUnion->setIConst(safeIndex);
2718 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002719 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002720
2721 return intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location,
2722 &mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002723 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002724 else
2725 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002726 return intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location,
2727 &mDiagnostics);
Jamie Madill7164cf42013-07-08 13:30:59 -04002728 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002729}
2730
Olli Etuaho90892fb2016-07-14 14:44:51 +03002731int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
2732 const TSourceLoc &location,
2733 int index,
2734 int arraySize,
2735 const char *reason,
2736 const char *token)
2737{
2738 if (index >= arraySize || index < 0)
2739 {
2740 std::stringstream extraInfoStream;
2741 extraInfoStream << "'" << index << "'";
2742 std::string extraInfo = extraInfoStream.str();
2743 outOfRangeError(outOfRangeIndexIsError, location, reason, token, extraInfo.c_str());
2744 if (index < 0)
2745 {
2746 return 0;
2747 }
2748 else
2749 {
2750 return arraySize - 1;
2751 }
2752 }
2753 return index;
2754}
2755
Jamie Madillb98c3a82015-07-23 14:26:04 -04002756TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
2757 const TSourceLoc &dotLocation,
2758 const TString &fieldString,
2759 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002760{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002761 if (baseExpression->isArray())
2762 {
2763 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002764 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002765 }
2766
2767 if (baseExpression->isVector())
2768 {
2769 TVectorFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002770 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields,
2771 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002772 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002773 fields.num = 1;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002774 fields.offsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002775 }
2776
Olli Etuahob6fa0432016-09-28 16:28:05 +01002777 return TIntermediate::AddSwizzle(baseExpression, fields, dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002778 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002779 else if (baseExpression->getBasicType() == EbtStruct)
2780 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302781 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04002782 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002783 {
2784 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002785 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002786 }
2787 else
2788 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002789 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002790 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002791 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002792 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002793 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002794 {
2795 fieldFound = true;
2796 break;
2797 }
2798 }
2799 if (fieldFound)
2800 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002801 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
2802 index->setLine(fieldLocation);
2803 return intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index,
2804 dotLocation, &mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002805 }
2806 else
2807 {
2808 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002809 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002810 }
2811 }
2812 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002813 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002814 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302815 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04002816 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002817 {
2818 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002819 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002820 }
2821 else
2822 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002823 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002824 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002825 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002826 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002827 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002828 {
2829 fieldFound = true;
2830 break;
2831 }
2832 }
2833 if (fieldFound)
2834 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002835 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
2836 index->setLine(fieldLocation);
2837 return intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index,
2838 dotLocation, &mDiagnostics);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002839 }
2840 else
2841 {
2842 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002843 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002844 }
2845 }
2846 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002847 else
2848 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002849 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002850 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03002851 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05302852 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002853 }
2854 else
2855 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302856 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03002857 " field selection requires structure, vector, or interface block on left hand "
2858 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05302859 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002860 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002861 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002862 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002863}
2864
Jamie Madillb98c3a82015-07-23 14:26:04 -04002865TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
2866 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002867{
Martin Radev802abe02016-08-04 17:48:32 +03002868 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002869
2870 if (qualifierType == "shared")
2871 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002872 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002873 }
2874 else if (qualifierType == "packed")
2875 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002876 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002877 }
2878 else if (qualifierType == "std140")
2879 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002880 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002881 }
2882 else if (qualifierType == "row_major")
2883 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002884 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002885 }
2886 else if (qualifierType == "column_major")
2887 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002888 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002889 }
2890 else if (qualifierType == "location")
2891 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002892 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
2893 "location requires an argument");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002894 }
2895 else
2896 {
2897 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002898 }
2899
Jamie Madilla5efff92013-06-06 11:56:47 -04002900 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002901}
2902
Martin Radev802abe02016-08-04 17:48:32 +03002903void TParseContext::parseLocalSize(const TString &qualifierType,
2904 const TSourceLoc &qualifierTypeLine,
2905 int intValue,
2906 const TSourceLoc &intValueLine,
2907 const std::string &intValueString,
2908 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002909 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03002910{
Olli Etuaho856c4972016-08-08 11:38:39 +03002911 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03002912 if (intValue < 1)
2913 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002914 std::string errorMessage = std::string(getWorkGroupSizeString(index)) + " must be positive";
Martin Radev802abe02016-08-04 17:48:32 +03002915 error(intValueLine, "out of range:", intValueString.c_str(), errorMessage.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03002916 }
2917 (*localSize)[index] = intValue;
2918}
2919
Jamie Madillb98c3a82015-07-23 14:26:04 -04002920TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
2921 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002922 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302923 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002924{
Martin Radev802abe02016-08-04 17:48:32 +03002925 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002926
Martin Radev802abe02016-08-04 17:48:32 +03002927 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002928
Martin Radev802abe02016-08-04 17:48:32 +03002929 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002930 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002931 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002932 if (intValue < 0)
2933 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002934 error(intValueLine, "out of range:", intValueString.c_str(),
2935 "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002936 }
2937 else
2938 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002939 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03002940 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002941 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002942 }
Martin Radev802abe02016-08-04 17:48:32 +03002943 else if (qualifierType == "local_size_x")
2944 {
2945 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
2946 &qualifier.localSize);
2947 }
2948 else if (qualifierType == "local_size_y")
2949 {
2950 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
2951 &qualifier.localSize);
2952 }
2953 else if (qualifierType == "local_size_z")
2954 {
2955 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
2956 &qualifier.localSize);
2957 }
2958 else
2959 {
2960 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03002961 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002962
Jamie Madilla5efff92013-06-06 11:56:47 -04002963 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002964}
2965
Olli Etuaho613b9592016-09-05 12:05:53 +03002966TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
2967{
2968 return new TTypeQualifierBuilder(
2969 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
2970 mShaderVersion);
2971}
2972
Jamie Madillb98c3a82015-07-23 14:26:04 -04002973TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03002974 TLayoutQualifier rightQualifier,
2975 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002976{
Martin Radevc28888b2016-07-22 15:27:42 +03002977 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
2978 &mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002979}
2980
Martin Radev70866b82016-07-22 15:27:42 +03002981TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
2982 const TTypeQualifierBuilder &typeQualifierBuilder,
2983 TPublicType *typeSpecifier,
2984 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002985{
Olli Etuaho613b9592016-09-05 12:05:53 +03002986 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002987
Martin Radev70866b82016-07-22 15:27:42 +03002988 typeSpecifier->qualifier = typeQualifier.qualifier;
2989 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
2990 typeSpecifier->invariant = typeQualifier.invariant;
2991 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05302992 {
Martin Radev70866b82016-07-22 15:27:42 +03002993 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002994 }
Martin Radev70866b82016-07-22 15:27:42 +03002995 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002996}
2997
Jamie Madillb98c3a82015-07-23 14:26:04 -04002998TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
2999 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003000{
Martin Radev4a9cd802016-09-01 16:51:51 +03003001 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
3002 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03003003
Martin Radev4a9cd802016-09-01 16:51:51 +03003004 checkIsNonVoid(typeSpecifier.getLine(), (*fieldList)[0]->name(), typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003005
Martin Radev4a9cd802016-09-01 16:51:51 +03003006 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003007
Arun Patole7e7e68d2015-05-22 12:02:25 +05303008 for (unsigned int i = 0; i < fieldList->size(); ++i)
3009 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003010 //
3011 // Careful not to replace already known aspects of type, like array-ness
3012 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303013 TType *type = (*fieldList)[i]->type();
Martin Radev4a9cd802016-09-01 16:51:51 +03003014 type->setBasicType(typeSpecifier.getBasicType());
3015 type->setPrimarySize(typeSpecifier.getPrimarySize());
3016 type->setSecondarySize(typeSpecifier.getSecondarySize());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003017 type->setPrecision(typeSpecifier.precision);
3018 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003019 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
Martin Radev70866b82016-07-22 15:27:42 +03003020 type->setInvariant(typeSpecifier.invariant);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003021
3022 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303023 if (type->isArray())
3024 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003025 checkIsValidTypeForArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003026 }
3027 if (typeSpecifier.array)
Olli Etuaho856c4972016-08-08 11:38:39 +03003028 type->setArraySize(static_cast<unsigned int>(typeSpecifier.arraySize));
Martin Radev4a9cd802016-09-01 16:51:51 +03003029 if (typeSpecifier.getUserDef())
Arun Patole7e7e68d2015-05-22 12:02:25 +05303030 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003031 type->setStruct(typeSpecifier.getUserDef()->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003032 }
3033
Martin Radev4a9cd802016-09-01 16:51:51 +03003034 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*fieldList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003035 }
3036
Jamie Madill98493dd2013-07-08 14:39:03 -04003037 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003038}
3039
Martin Radev4a9cd802016-09-01 16:51:51 +03003040TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
3041 const TSourceLoc &nameLine,
3042 const TString *structName,
3043 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003044{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303045 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003046 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003047
Jamie Madill9b820842015-02-12 10:40:10 -05003048 // Store a bool in the struct if we're at global scope, to allow us to
3049 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05003050 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003051
Jamie Madill98493dd2013-07-08 14:39:03 -04003052 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003053 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003054 checkIsNotReserved(nameLine, *structName);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303055 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
3056 if (!symbolTable.declare(userTypeDef))
3057 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003058 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003059 }
3060 }
3061
3062 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04003063 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003064 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003065 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04003066 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003067 switch (qualifier)
3068 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003069 case EvqGlobal:
3070 case EvqTemporary:
3071 break;
3072 default:
3073 error(field.line(), "invalid qualifier on struct member",
3074 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003075 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003076 }
Martin Radev70866b82016-07-22 15:27:42 +03003077 if (field.type()->isInvariant())
3078 {
3079 error(field.line(), "invalid qualifier on struct member", "invariant");
3080 }
3081
3082 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003083 }
3084
Martin Radev4a9cd802016-09-01 16:51:51 +03003085 TTypeSpecifierNonArray typeSpecifierNonArray;
3086 typeSpecifierNonArray.initialize(EbtStruct, structLine);
3087 typeSpecifierNonArray.userDef = structureType;
3088 typeSpecifierNonArray.isStructSpecifier = true;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003089 exitStructDeclaration();
3090
Martin Radev4a9cd802016-09-01 16:51:51 +03003091 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003092}
3093
Jamie Madillb98c3a82015-07-23 14:26:04 -04003094TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003095 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003096 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02003097{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003098 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003099 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02003100 init->isVector())
3101 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003102 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
3103 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003104 return nullptr;
3105 }
3106
Olli Etuahoac5274d2015-02-20 10:19:08 +02003107 if (statementList)
3108 {
3109 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
3110 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02003111 return nullptr;
3112 }
3113 }
3114
Olli Etuahoa3a36662015-02-17 13:46:51 +02003115 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3116 if (node == nullptr)
3117 {
3118 error(loc, "erroneous switch statement", "switch");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003119 return nullptr;
3120 }
3121 return node;
3122}
3123
3124TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3125{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003126 if (mSwitchNestingLevel == 0)
3127 {
3128 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003129 return nullptr;
3130 }
3131 if (condition == nullptr)
3132 {
3133 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003134 return nullptr;
3135 }
3136 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04003137 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02003138 {
3139 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003140 }
3141 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003142 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
3143 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
3144 // fold in case labels.
3145 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02003146 {
3147 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003148 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003149 TIntermCase *node = intermediate.addCase(condition, loc);
3150 if (node == nullptr)
3151 {
3152 error(loc, "erroneous case statement", "case");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003153 return nullptr;
3154 }
3155 return node;
3156}
3157
3158TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3159{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003160 if (mSwitchNestingLevel == 0)
3161 {
3162 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003163 return nullptr;
3164 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003165 TIntermCase *node = intermediate.addCase(nullptr, loc);
3166 if (node == nullptr)
3167 {
3168 error(loc, "erroneous default statement", "default");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003169 return nullptr;
3170 }
3171 return node;
3172}
3173
Jamie Madillb98c3a82015-07-23 14:26:04 -04003174TIntermTyped *TParseContext::createUnaryMath(TOperator op,
3175 TIntermTyped *child,
3176 const TSourceLoc &loc,
3177 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003178{
3179 if (child == nullptr)
3180 {
3181 return nullptr;
3182 }
3183
3184 switch (op)
3185 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003186 case EOpLogicalNot:
3187 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
3188 child->isVector())
3189 {
3190 return nullptr;
3191 }
3192 break;
3193 case EOpBitwiseNot:
3194 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3195 child->isMatrix() || child->isArray())
3196 {
3197 return nullptr;
3198 }
3199 break;
3200 case EOpPostIncrement:
3201 case EOpPreIncrement:
3202 case EOpPostDecrement:
3203 case EOpPreDecrement:
3204 case EOpNegative:
3205 case EOpPositive:
3206 if (child->getBasicType() == EbtStruct || child->getBasicType() == EbtBool ||
Olli Etuaho558b0382016-08-26 17:54:34 +03003207 child->isArray() || IsSampler(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04003208 {
3209 return nullptr;
3210 }
3211 // Operators for built-ins are already type checked against their prototype.
3212 default:
3213 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003214 }
3215
Olli Etuahof119a262016-08-19 15:54:22 +03003216 TIntermUnary *node = new TIntermUnary(op, child);
3217 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03003218
3219 TIntermTyped *foldedNode = node->fold(&mDiagnostics);
3220 if (foldedNode)
3221 return foldedNode;
3222
3223 return node;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003224}
3225
Olli Etuaho09b22472015-02-11 11:47:26 +02003226TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3227{
Olli Etuahof6c694b2015-03-26 14:50:53 +02003228 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003229 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003230 {
3231 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003232 return child;
3233 }
3234 return node;
3235}
3236
Jamie Madillb98c3a82015-07-23 14:26:04 -04003237TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
3238 TIntermTyped *child,
3239 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003240{
Olli Etuaho856c4972016-08-08 11:38:39 +03003241 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02003242 return addUnaryMath(op, child, loc);
3243}
3244
Jamie Madillb98c3a82015-07-23 14:26:04 -04003245bool TParseContext::binaryOpCommonCheck(TOperator op,
3246 TIntermTyped *left,
3247 TIntermTyped *right,
3248 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003249{
Olli Etuaho244be012016-08-18 15:26:02 +03003250 if (left->getType().getStruct() || right->getType().getStruct())
3251 {
3252 switch (op)
3253 {
3254 case EOpIndexDirectStruct:
3255 ASSERT(left->getType().getStruct());
3256 break;
3257 case EOpEqual:
3258 case EOpNotEqual:
3259 case EOpAssign:
3260 case EOpInitialize:
3261 if (left->getType() != right->getType())
3262 {
3263 return false;
3264 }
3265 break;
3266 default:
3267 error(loc, "Invalid operation for structs", GetOperatorString(op));
3268 return false;
3269 }
3270 }
3271
Olli Etuahod6b14282015-03-17 14:31:35 +02003272 if (left->isArray() || right->isArray())
3273 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003274 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003275 {
3276 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3277 return false;
3278 }
3279
3280 if (left->isArray() != right->isArray())
3281 {
3282 error(loc, "array / non-array mismatch", GetOperatorString(op));
3283 return false;
3284 }
3285
3286 switch (op)
3287 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003288 case EOpEqual:
3289 case EOpNotEqual:
3290 case EOpAssign:
3291 case EOpInitialize:
3292 break;
3293 default:
3294 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3295 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02003296 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003297 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003298 if (left->getArraySize() != right->getArraySize())
3299 {
3300 error(loc, "array size mismatch", GetOperatorString(op));
3301 return false;
3302 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003303 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003304
3305 // Check ops which require integer / ivec parameters
3306 bool isBitShift = false;
3307 switch (op)
3308 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003309 case EOpBitShiftLeft:
3310 case EOpBitShiftRight:
3311 case EOpBitShiftLeftAssign:
3312 case EOpBitShiftRightAssign:
3313 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3314 // check that the basic type is an integer type.
3315 isBitShift = true;
3316 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3317 {
3318 return false;
3319 }
3320 break;
3321 case EOpBitwiseAnd:
3322 case EOpBitwiseXor:
3323 case EOpBitwiseOr:
3324 case EOpBitwiseAndAssign:
3325 case EOpBitwiseXorAssign:
3326 case EOpBitwiseOrAssign:
3327 // It is enough to check the type of only one operand, since later it
3328 // is checked that the operand types match.
3329 if (!IsInteger(left->getBasicType()))
3330 {
3331 return false;
3332 }
3333 break;
3334 default:
3335 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003336 }
3337
3338 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3339 // So the basic type should usually match.
3340 if (!isBitShift && left->getBasicType() != right->getBasicType())
3341 {
3342 return false;
3343 }
3344
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003345 // Check that:
3346 // 1. Type sizes match exactly on ops that require that.
3347 // 2. Restrictions for structs that contain arrays or samplers are respected.
3348 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003349 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003350 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003351 case EOpAssign:
3352 case EOpInitialize:
3353 case EOpEqual:
3354 case EOpNotEqual:
3355 // ESSL 1.00 sections 5.7, 5.8, 5.9
3356 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
3357 {
3358 error(loc, "undefined operation for structs containing arrays",
3359 GetOperatorString(op));
3360 return false;
3361 }
3362 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3363 // we interpret the spec so that this extends to structs containing samplers,
3364 // similarly to ESSL 1.00 spec.
3365 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
3366 left->getType().isStructureContainingSamplers())
3367 {
3368 error(loc, "undefined operation for structs containing samplers",
3369 GetOperatorString(op));
3370 return false;
3371 }
3372 case EOpLessThan:
3373 case EOpGreaterThan:
3374 case EOpLessThanEqual:
3375 case EOpGreaterThanEqual:
3376 if ((left->getNominalSize() != right->getNominalSize()) ||
3377 (left->getSecondarySize() != right->getSecondarySize()))
3378 {
3379 return false;
3380 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003381 break;
3382 case EOpAdd:
3383 case EOpSub:
3384 case EOpDiv:
3385 case EOpIMod:
3386 case EOpBitShiftLeft:
3387 case EOpBitShiftRight:
3388 case EOpBitwiseAnd:
3389 case EOpBitwiseXor:
3390 case EOpBitwiseOr:
3391 case EOpAddAssign:
3392 case EOpSubAssign:
3393 case EOpDivAssign:
3394 case EOpIModAssign:
3395 case EOpBitShiftLeftAssign:
3396 case EOpBitShiftRightAssign:
3397 case EOpBitwiseAndAssign:
3398 case EOpBitwiseXorAssign:
3399 case EOpBitwiseOrAssign:
3400 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
3401 {
3402 return false;
3403 }
3404
3405 // Are the sizes compatible?
3406 if (left->getNominalSize() != right->getNominalSize() ||
3407 left->getSecondarySize() != right->getSecondarySize())
3408 {
3409 // If the nominal sizes of operands do not match:
3410 // One of them must be a scalar.
3411 if (!left->isScalar() && !right->isScalar())
3412 return false;
3413
3414 // In the case of compound assignment other than multiply-assign,
3415 // the right side needs to be a scalar. Otherwise a vector/matrix
3416 // would be assigned to a scalar. A scalar can't be shifted by a
3417 // vector either.
3418 if (!right->isScalar() &&
3419 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
3420 return false;
3421 }
3422 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003423 default:
3424 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003425 }
3426
Olli Etuahod6b14282015-03-17 14:31:35 +02003427 return true;
3428}
3429
Olli Etuaho1dded802016-08-18 18:13:13 +03003430bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
3431 const TType &left,
3432 const TType &right)
3433{
3434 switch (op)
3435 {
3436 case EOpMul:
3437 case EOpMulAssign:
3438 return left.getNominalSize() == right.getNominalSize() &&
3439 left.getSecondarySize() == right.getSecondarySize();
3440 case EOpVectorTimesScalar:
3441 return true;
3442 case EOpVectorTimesScalarAssign:
3443 ASSERT(!left.isMatrix() && !right.isMatrix());
3444 return left.isVector() && !right.isVector();
3445 case EOpVectorTimesMatrix:
3446 return left.getNominalSize() == right.getRows();
3447 case EOpVectorTimesMatrixAssign:
3448 ASSERT(!left.isMatrix() && right.isMatrix());
3449 return left.isVector() && left.getNominalSize() == right.getRows() &&
3450 left.getNominalSize() == right.getCols();
3451 case EOpMatrixTimesVector:
3452 return left.getCols() == right.getNominalSize();
3453 case EOpMatrixTimesScalar:
3454 return true;
3455 case EOpMatrixTimesScalarAssign:
3456 ASSERT(left.isMatrix() && !right.isMatrix());
3457 return !right.isVector();
3458 case EOpMatrixTimesMatrix:
3459 return left.getCols() == right.getRows();
3460 case EOpMatrixTimesMatrixAssign:
3461 ASSERT(left.isMatrix() && right.isMatrix());
3462 // We need to check two things:
3463 // 1. The matrix multiplication step is valid.
3464 // 2. The result will have the same number of columns as the lvalue.
3465 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
3466
3467 default:
3468 UNREACHABLE();
3469 return false;
3470 }
3471}
3472
Jamie Madillb98c3a82015-07-23 14:26:04 -04003473TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
3474 TIntermTyped *left,
3475 TIntermTyped *right,
3476 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02003477{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003478 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003479 return nullptr;
3480
Olli Etuahofc1806e2015-03-17 13:03:11 +02003481 switch (op)
3482 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003483 case EOpEqual:
3484 case EOpNotEqual:
3485 break;
3486 case EOpLessThan:
3487 case EOpGreaterThan:
3488 case EOpLessThanEqual:
3489 case EOpGreaterThanEqual:
Olli Etuaho244be012016-08-18 15:26:02 +03003490 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3491 !right->getType().getStruct());
3492 if (left->isMatrix() || left->isVector())
Jamie Madillb98c3a82015-07-23 14:26:04 -04003493 {
3494 return nullptr;
3495 }
3496 break;
3497 case EOpLogicalOr:
3498 case EOpLogicalXor:
3499 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03003500 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3501 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003502 if (left->getBasicType() != EbtBool || left->isMatrix() || left->isVector())
3503 {
3504 return nullptr;
3505 }
3506 break;
3507 case EOpAdd:
3508 case EOpSub:
3509 case EOpDiv:
3510 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03003511 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3512 !right->getType().getStruct());
3513 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04003514 {
3515 return nullptr;
3516 }
3517 break;
3518 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03003519 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3520 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003521 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03003522 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04003523 {
3524 return nullptr;
3525 }
3526 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003527 default:
3528 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003529 }
3530
Olli Etuaho1dded802016-08-18 18:13:13 +03003531 if (op == EOpMul)
3532 {
3533 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
3534 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
3535 {
3536 return nullptr;
3537 }
3538 }
3539
Olli Etuaho3fdec912016-08-18 15:08:06 +03003540 TIntermBinary *node = new TIntermBinary(op, left, right);
3541 node->setLine(loc);
3542
Olli Etuaho3fdec912016-08-18 15:08:06 +03003543 // See if we can fold constants.
3544 TIntermTyped *foldedNode = node->fold(&mDiagnostics);
3545 if (foldedNode)
3546 return foldedNode;
3547
3548 return node;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003549}
3550
Jamie Madillb98c3a82015-07-23 14:26:04 -04003551TIntermTyped *TParseContext::addBinaryMath(TOperator op,
3552 TIntermTyped *left,
3553 TIntermTyped *right,
3554 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003555{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003556 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003557 if (node == 0)
3558 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003559 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3560 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003561 return left;
3562 }
3563 return node;
3564}
3565
Jamie Madillb98c3a82015-07-23 14:26:04 -04003566TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
3567 TIntermTyped *left,
3568 TIntermTyped *right,
3569 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003570{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003571 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003572 if (node == 0)
3573 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003574 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3575 right->getCompleteString());
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003576 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02003577 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003578 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
3579 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003580 }
3581 return node;
3582}
3583
Jamie Madillb98c3a82015-07-23 14:26:04 -04003584TIntermTyped *TParseContext::createAssign(TOperator op,
3585 TIntermTyped *left,
3586 TIntermTyped *right,
3587 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003588{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003589 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003590 {
Olli Etuaho1dded802016-08-18 18:13:13 +03003591 if (op == EOpMulAssign)
3592 {
3593 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
3594 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
3595 {
3596 return nullptr;
3597 }
3598 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03003599 TIntermBinary *node = new TIntermBinary(op, left, right);
3600 node->setLine(loc);
3601
Olli Etuaho3fdec912016-08-18 15:08:06 +03003602 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02003603 }
3604 return nullptr;
3605}
3606
Jamie Madillb98c3a82015-07-23 14:26:04 -04003607TIntermTyped *TParseContext::addAssign(TOperator op,
3608 TIntermTyped *left,
3609 TIntermTyped *right,
3610 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003611{
3612 TIntermTyped *node = createAssign(op, left, right, loc);
3613 if (node == nullptr)
3614 {
3615 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02003616 return left;
3617 }
3618 return node;
3619}
3620
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003621TIntermTyped *TParseContext::addComma(TIntermTyped *left,
3622 TIntermTyped *right,
3623 const TSourceLoc &loc)
3624{
Corentin Wallez0d959252016-07-12 17:26:32 -04003625 // WebGL2 section 5.26, the following results in an error:
3626 // "Sequence operator applied to void, arrays, or structs containing arrays"
3627 if (mShaderSpec == SH_WEBGL2_SPEC && (left->isArray() || left->getBasicType() == EbtVoid ||
3628 left->getType().isStructureContainingArrays() ||
3629 right->isArray() || right->getBasicType() == EbtVoid ||
3630 right->getType().isStructureContainingArrays()))
3631 {
3632 error(loc,
3633 "sequence operator is not allowed for void, arrays, or structs containing arrays",
3634 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04003635 }
3636
Olli Etuaho15200042015-11-04 16:56:31 +02003637 return intermediate.addComma(left, right, loc, mShaderVersion);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003638}
3639
Olli Etuaho49300862015-02-20 14:54:49 +02003640TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3641{
3642 switch (op)
3643 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003644 case EOpContinue:
3645 if (mLoopNestingLevel <= 0)
3646 {
3647 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04003648 }
3649 break;
3650 case EOpBreak:
3651 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
3652 {
3653 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04003654 }
3655 break;
3656 case EOpReturn:
3657 if (mCurrentFunctionType->getBasicType() != EbtVoid)
3658 {
3659 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04003660 }
3661 break;
3662 default:
3663 // No checks for discard
3664 break;
Olli Etuaho49300862015-02-20 14:54:49 +02003665 }
3666 return intermediate.addBranch(op, loc);
3667}
3668
Jamie Madillb98c3a82015-07-23 14:26:04 -04003669TIntermBranch *TParseContext::addBranch(TOperator op,
3670 TIntermTyped *returnValue,
3671 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02003672{
3673 ASSERT(op == EOpReturn);
3674 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003675 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02003676 {
3677 error(loc, "void function cannot return a value", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02003678 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003679 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02003680 {
3681 error(loc, "function return is not matching type:", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02003682 }
3683 return intermediate.addBranch(op, returnValue, loc);
3684}
3685
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003686void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
3687{
3688 ASSERT(!functionCall->isUserDefined());
Olli Etuahobd674552016-10-06 13:28:42 +01003689 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003690 TIntermNode *offset = nullptr;
3691 TIntermSequence *arguments = functionCall->getSequence();
3692 if (name.compare(0, 16, "texelFetchOffset") == 0 ||
3693 name.compare(0, 16, "textureLodOffset") == 0 ||
3694 name.compare(0, 20, "textureProjLodOffset") == 0 ||
3695 name.compare(0, 17, "textureGradOffset") == 0 ||
3696 name.compare(0, 21, "textureProjGradOffset") == 0)
3697 {
3698 offset = arguments->back();
3699 }
3700 else if (name.compare(0, 13, "textureOffset") == 0 ||
3701 name.compare(0, 17, "textureProjOffset") == 0)
3702 {
3703 // A bias parameter might follow the offset parameter.
3704 ASSERT(arguments->size() >= 3);
3705 offset = (*arguments)[2];
3706 }
3707 if (offset != nullptr)
3708 {
3709 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
3710 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
3711 {
3712 TString unmangledName = TFunction::unmangleName(name);
3713 error(functionCall->getLine(), "Texture offset must be a constant expression",
3714 unmangledName.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003715 }
3716 else
3717 {
3718 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
3719 size_t size = offsetConstantUnion->getType().getObjectSize();
3720 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
3721 for (size_t i = 0u; i < size; ++i)
3722 {
3723 int offsetValue = values[i].getIConst();
3724 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
3725 {
3726 std::stringstream tokenStream;
3727 tokenStream << offsetValue;
3728 std::string token = tokenStream.str();
3729 error(offset->getLine(), "Texture offset value out of valid range",
3730 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003731 }
3732 }
3733 }
3734 }
3735}
3736
Jamie Madillb98c3a82015-07-23 14:26:04 -04003737TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
3738 TIntermNode *paramNode,
3739 TIntermNode *thisNode,
3740 const TSourceLoc &loc,
3741 bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003742{
Jamie Madillb98c3a82015-07-23 14:26:04 -04003743 *fatalError = false;
3744 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003745 TIntermTyped *callNode = nullptr;
3746
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003747 if (thisNode != nullptr)
3748 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003749 TConstantUnion *unionArray = new TConstantUnion[1];
Jamie Madillb98c3a82015-07-23 14:26:04 -04003750 int arraySize = 0;
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003751 TIntermTyped *typedThis = thisNode->getAsTyped();
3752 if (fnCall->getName() != "length")
3753 {
3754 error(loc, "invalid method", fnCall->getName().c_str());
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003755 }
3756 else if (paramNode != nullptr)
3757 {
3758 error(loc, "method takes no parameters", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003759 }
3760 else if (typedThis == nullptr || !typedThis->isArray())
3761 {
3762 error(loc, "length can only be called on arrays", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003763 }
3764 else
3765 {
Olli Etuaho96e67382015-04-23 14:27:02 +03003766 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03003767 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003768 {
Olli Etuaho39282e12015-04-23 15:41:48 +03003769 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003770 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03003771 // (func()).length()
3772 // (int[3](0, 1, 2)).length()
Jamie Madillb98c3a82015-07-23 14:26:04 -04003773 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
3774 // expression.
3775 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
3776 // spec section 5.9 which allows "An array, vector or matrix expression with the
3777 // length method applied".
3778 error(loc, "length can only be called on array names, not on array expressions",
3779 "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003780 }
3781 }
Olli Etuaho96e67382015-04-23 14:27:02 +03003782 unionArray->setIConst(arraySize);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003783 callNode =
3784 intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003785 }
3786 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003787 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003788 // Then this should be a constructor.
Olli Etuaho856c4972016-08-08 11:38:39 +03003789 callNode = addConstructor(paramNode, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003790 }
3791 else
3792 {
3793 //
3794 // Not a constructor. Find it in the symbol table.
3795 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303796 const TFunction *fnCandidate;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003797 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003798 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003799 if (fnCandidate)
3800 {
3801 //
3802 // A declared function.
3803 //
Olli Etuaho383b7912016-08-05 11:22:59 +03003804 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003805 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003806 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003807 }
3808 op = fnCandidate->getBuiltInOp();
3809 if (builtIn && op != EOpNull)
3810 {
3811 //
3812 // A function call mapped to a built-in operation.
3813 //
3814 if (fnCandidate->getParamCount() == 1)
3815 {
3816 //
3817 // Treat it like a built-in unary operator.
3818 //
Olli Etuaho15c2ac32015-11-09 15:51:43 +02003819 TIntermAggregate *paramAgg = paramNode->getAsAggregate();
3820 paramNode = paramAgg->getSequence()->front();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003821 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc,
3822 &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003823 if (callNode == nullptr)
3824 {
3825 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003826 extraInfoStream
3827 << "built in unary operator function. Type: "
3828 << static_cast<TIntermTyped *>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003829 std::string extraInfo = extraInfoStream.str();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003830 error(paramNode->getLine(), " wrong operand type", "Internal Error",
3831 extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003832 *fatalError = true;
3833 return nullptr;
3834 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003835 }
3836 else
3837 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003838 TIntermAggregate *aggregate =
3839 intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003840 aggregate->setType(fnCandidate->getReturnType());
3841 aggregate->setPrecisionFromChildren();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02003842 if (aggregate->areChildrenConstQualified())
3843 {
3844 aggregate->getTypePointer()->setQualifier(EvqConst);
3845 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003846
3847 // Some built-in functions have out parameters too.
3848 functionCallLValueErrorCheck(fnCandidate, aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05303849
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003850 // See if we can constant fold a built-in. Note that this may be possible even
3851 // if it is not const-qualified.
Olli Etuahof119a262016-08-19 15:54:22 +03003852 TIntermTyped *foldedNode =
3853 intermediate.foldAggregateBuiltIn(aggregate, &mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05303854 if (foldedNode)
3855 {
Arun Patole274f0702015-05-05 13:33:30 +05303856 callNode = foldedNode;
3857 }
Olli Etuahob43846e2015-06-02 18:18:57 +03003858 else
3859 {
3860 callNode = aggregate;
3861 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003862 }
3863 }
3864 else
3865 {
3866 // This is a real function call
Jamie Madillb98c3a82015-07-23 14:26:04 -04003867 TIntermAggregate *aggregate =
3868 intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003869 aggregate->setType(fnCandidate->getReturnType());
3870
Jamie Madillb98c3a82015-07-23 14:26:04 -04003871 // this is how we know whether the given function is a builtIn function or a user
3872 // defined function
3873 // if builtIn == false, it's a userDefined -> could be an overloaded
3874 // builtIn function also
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003875 // if builtIn == true, it's definitely a builtIn function with EOpNull
3876 if (!builtIn)
3877 aggregate->setUserDefined();
Olli Etuahobd674552016-10-06 13:28:42 +01003878 aggregate->getFunctionSymbolInfo()->setFromFunction(*fnCandidate);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003879
Olli Etuahobd674552016-10-06 13:28:42 +01003880 // This needs to happen after the function info including name is set
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003881 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003882 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003883 aggregate->setBuiltInFunctionPrecision();
3884
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003885 checkTextureOffsetConst(aggregate);
3886 }
3887
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003888 callNode = aggregate;
3889
3890 functionCallLValueErrorCheck(fnCandidate, aggregate);
3891 }
3892 }
3893 else
3894 {
3895 // error message was put out by findFunction()
3896 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003897 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003898 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003899 callNode = intermediate.addConstantUnion(unionArray,
3900 TType(EbtFloat, EbpUndefined, EvqConst), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003901 }
3902 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003903 return callNode;
3904}
3905
Jamie Madillb98c3a82015-07-23 14:26:04 -04003906TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03003907 TIntermTyped *trueExpression,
3908 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03003909 const TSourceLoc &loc)
3910{
Olli Etuaho856c4972016-08-08 11:38:39 +03003911 checkIsScalarBool(loc, cond);
Olli Etuaho52901742015-04-15 13:42:45 +03003912
Olli Etuahod0bad2c2016-09-09 18:01:16 +03003913 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03003914 {
Olli Etuahod0bad2c2016-09-09 18:01:16 +03003915 binaryOpError(loc, ":", trueExpression->getCompleteString(),
3916 falseExpression->getCompleteString());
3917 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03003918 }
Olli Etuahoa2d53032015-04-15 14:14:44 +03003919 // ESSL1 sections 5.2 and 5.7:
3920 // ESSL3 section 5.7:
3921 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03003922 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03003923 {
3924 error(loc, "ternary operator is not allowed for structures or arrays", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03003925 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03003926 }
Corentin Wallez0d959252016-07-12 17:26:32 -04003927 // WebGL2 section 5.26, the following results in an error:
3928 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03003929 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04003930 {
3931 error(loc, "ternary operator is not allowed for void", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03003932 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04003933 }
3934
Olli Etuahod0bad2c2016-09-09 18:01:16 +03003935 return TIntermediate::AddTernarySelection(cond, trueExpression, falseExpression, loc);
Olli Etuaho52901742015-04-15 13:42:45 +03003936}
Olli Etuaho49300862015-02-20 14:54:49 +02003937
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003938//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003939// Parse an array of strings using yyparse.
3940//
3941// Returns 0 for success.
3942//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003943int PaParseStrings(size_t count,
3944 const char *const string[],
3945 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05303946 TParseContext *context)
3947{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003948 if ((count == 0) || (string == NULL))
3949 return 1;
3950
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003951 if (glslang_initialize(context))
3952 return 1;
3953
alokp@chromium.org408c45e2012-04-05 15:54:43 +00003954 int error = glslang_scan(count, string, length, context);
3955 if (!error)
3956 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003957
alokp@chromium.org73bc2982012-06-19 18:48:05 +00003958 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00003959
alokp@chromium.org6b495712012-06-29 00:06:58 +00003960 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003961}