blob: 6b415883f7d2513c0fa227a8bd00cef64b8daf48 [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());
2029 prototype->setName(function->getMangledName());
2030 prototype->setFunctionId(function->getUniqueId());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002031
Olli Etuaho476197f2016-10-11 13:59:08 +01002032 for (size_t i = 0; i < function->getParamCount(); i++)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002033 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002034 const TConstParameter &param = function->getParam(i);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002035 if (param.name != 0)
2036 {
2037 TVariable variable(param.name, *param.type);
2038
2039 TIntermSymbol *paramSymbol = intermediate.addSymbol(
2040 variable.getUniqueId(), variable.getName(), variable.getType(), location);
2041 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
2042 }
2043 else
2044 {
2045 TIntermSymbol *paramSymbol = intermediate.addSymbol(0, "", *param.type, location);
2046 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
2047 }
2048 }
2049
2050 prototype->setOp(EOpPrototype);
2051
2052 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002053
2054 if (!symbolTable.atGlobalLevel())
2055 {
2056 // ESSL 3.00.4 section 4.2.4.
2057 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002058 }
2059
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002060 return prototype;
2061}
2062
2063TIntermAggregate *TParseContext::addFunctionDefinition(const TFunction &function,
2064 TIntermAggregate *functionPrototype,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01002065 TIntermBlock *functionBody,
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002066 const TSourceLoc &location)
2067{
Olli Etuahof51fdd22016-10-03 10:03:40 +01002068 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002069 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
2070 {
2071 error(location, "function does not return a value:", "", function.getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002072 }
2073
Olli Etuahof51fdd22016-10-03 10:03:40 +01002074 TIntermAggregate *functionNode = new TIntermAggregate(EOpFunction);
2075 functionNode->setLine(location);
2076
2077 ASSERT(functionPrototype != nullptr);
2078 functionNode->getSequence()->push_back(functionPrototype);
2079
2080 if (functionBody == nullptr)
2081 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01002082 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002083 functionBody->setLine(location);
2084 }
2085 functionNode->getSequence()->push_back(functionBody);
2086
2087 functionNode->setName(function.getMangledName().c_str());
2088 functionNode->setType(function.getReturnType());
2089 functionNode->setFunctionId(function.getUniqueId());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002090
2091 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002092 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002093}
2094
Olli Etuaho476197f2016-10-11 13:59:08 +01002095void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
2096 TFunction **function,
2097 TIntermAggregate **aggregateOut)
Jamie Madill185fb402015-06-12 15:48:48 -04002098{
Olli Etuaho476197f2016-10-11 13:59:08 +01002099 ASSERT(function);
2100 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002101 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01002102 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002103
2104 if (builtIn)
2105 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002106 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002107 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002108 else
Jamie Madill185fb402015-06-12 15:48:48 -04002109 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002110 TFunction *prevDec = static_cast<TFunction *>(
2111 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
2112
2113 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
2114 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
2115 // occurance.
2116 if (*function != prevDec)
2117 {
2118 // Swap the parameters of the previous declaration to the parameters of the function
2119 // definition (parameter names may differ).
2120 prevDec->swapParameters(**function);
2121
2122 // The function definition will share the same symbol as any previous declaration.
2123 *function = prevDec;
2124 }
2125
2126 if ((*function)->isDefined())
2127 {
2128 error(location, "function already has a body", (*function)->getName().c_str());
2129 }
2130
2131 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04002132 }
Jamie Madill185fb402015-06-12 15:48:48 -04002133
2134 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuaho476197f2016-10-11 13:59:08 +01002135 if ((*function)->getName() == "main")
Jamie Madill185fb402015-06-12 15:48:48 -04002136 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002137 if ((*function)->getParamCount() > 0)
Jamie Madill185fb402015-06-12 15:48:48 -04002138 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002139 error(location, "function cannot take any parameter(s)",
2140 (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002141 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002142 if ((*function)->getReturnType().getBasicType() != EbtVoid)
Jamie Madill185fb402015-06-12 15:48:48 -04002143 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002144 error(location, "", (*function)->getReturnType().getBasicString(),
Jamie Madillb98c3a82015-07-23 14:26:04 -04002145 "main function cannot return a value");
Jamie Madill185fb402015-06-12 15:48:48 -04002146 }
2147 }
2148
2149 //
2150 // Remember the return type for later checking for RETURN statements.
2151 //
Olli Etuaho476197f2016-10-11 13:59:08 +01002152 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002153 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04002154
2155 //
2156 // Insert parameters into the symbol table.
2157 // If the parameter has no name, it's not an error, just don't insert it
2158 // (could be used for unused args).
2159 //
2160 // Also, accumulate the list of parameters into the HIL, so lower level code
2161 // knows where to find parameters.
2162 //
2163 TIntermAggregate *paramNodes = new TIntermAggregate;
Olli Etuaho476197f2016-10-11 13:59:08 +01002164 for (size_t i = 0; i < (*function)->getParamCount(); i++)
Jamie Madill185fb402015-06-12 15:48:48 -04002165 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002166 const TConstParameter &param = (*function)->getParam(i);
Jamie Madill185fb402015-06-12 15:48:48 -04002167 if (param.name != 0)
2168 {
2169 TVariable *variable = new TVariable(param.name, *param.type);
2170 //
2171 // Insert the parameters with name in the symbol table.
2172 //
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002173 if (!symbolTable.declare(variable))
2174 {
Jamie Madill185fb402015-06-12 15:48:48 -04002175 error(location, "redefinition", variable->getName().c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002176 paramNodes = intermediate.growAggregate(
2177 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
2178 continue;
Jamie Madill185fb402015-06-12 15:48:48 -04002179 }
2180
2181 //
2182 // Add the parameter to the HIL
2183 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002184 TIntermSymbol *symbol = intermediate.addSymbol(
2185 variable->getUniqueId(), variable->getName(), variable->getType(), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002186
2187 paramNodes = intermediate.growAggregate(paramNodes, symbol, location);
2188 }
2189 else
2190 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002191 paramNodes = intermediate.growAggregate(
2192 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002193 }
2194 }
2195 intermediate.setAggregateOperator(paramNodes, EOpParameters, location);
2196 *aggregateOut = paramNodes;
2197 setLoopNestingLevel(0);
2198}
2199
Jamie Madillb98c3a82015-07-23 14:26:04 -04002200TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002201{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002202 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002203 // We don't know at this point whether this is a function definition or a prototype.
2204 // The definition production code will check for redefinitions.
2205 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002206 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002207 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
2208 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002209 //
2210 TFunction *prevDec =
2211 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302212
2213 if (getShaderVersion() >= 300 && symbolTable.hasUnmangledBuiltIn(function->getName().c_str()))
2214 {
2215 // With ESSL 3.00, names of built-in functions cannot be redeclared as functions.
2216 // Therefore overloading or redefining builtin functions is an error.
2217 error(location, "Name of a built-in function cannot be redeclared as function",
2218 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302219 }
2220 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002221 {
2222 if (prevDec->getReturnType() != function->getReturnType())
2223 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002224 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002225 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04002226 }
2227 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2228 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002229 if (prevDec->getParam(i).type->getQualifier() !=
2230 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002231 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002232 error(location,
2233 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002234 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04002235 }
2236 }
2237 }
2238
2239 //
2240 // Check for previously declared variables using the same name.
2241 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002242 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002243 if (prevSym)
2244 {
2245 if (!prevSym->isFunction())
2246 {
2247 error(location, "redefinition", function->getName().c_str(), "function");
Jamie Madill185fb402015-06-12 15:48:48 -04002248 }
2249 }
2250 else
2251 {
2252 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01002253 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04002254 }
2255
2256 // We're at the inner scope level of the function's arguments and body statement.
2257 // Add the function prototype to the surrounding scope instead.
2258 symbolTable.getOuterLevel()->insert(function);
2259
2260 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002261 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2262 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002263 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2264 //
2265 return function;
2266}
2267
Olli Etuaho9de84a52016-06-14 17:36:01 +03002268TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
2269 const TString *name,
2270 const TSourceLoc &location)
2271{
2272 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
2273 {
2274 error(location, "no qualifiers allowed for function return",
2275 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03002276 }
2277 if (!type.layoutQualifier.isEmpty())
2278 {
2279 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03002280 }
2281 // make sure a sampler is not involved as well...
Martin Radev4a9cd802016-09-01 16:51:51 +03002282 checkIsNotSampler(location, type.typeSpecifierNonArray,
2283 "samplers can't be function return values");
Olli Etuahoe29324f2016-06-15 10:58:03 +03002284 if (mShaderVersion < 300)
2285 {
2286 // Array return values are forbidden, but there's also no valid syntax for declaring array
2287 // return values in ESSL 1.00.
2288 ASSERT(type.arraySize == 0 || mDiagnostics.numErrors() > 0);
2289
2290 if (type.isStructureContainingArrays())
2291 {
2292 // ESSL 1.00.17 section 6.1 Function Definitions
2293 error(location, "structures containing arrays can't be function return values",
2294 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03002295 }
2296 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03002297
2298 // Add the function as a prototype after parsing it (we do not support recursion)
2299 return new TFunction(name, new TType(type));
2300}
2301
Jamie Madill06145232015-05-13 13:10:01 -04002302TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002303{
Jamie Madill06145232015-05-13 13:10:01 -04002304 TPublicType publicType = publicTypeIn;
Martin Radev4a9cd802016-09-01 16:51:51 +03002305 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02002306 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002307 error(publicType.getLine(), "constructor can't be a structure definition",
2308 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02002309 }
2310
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002311 TOperator op = EOpNull;
Martin Radev4a9cd802016-09-01 16:51:51 +03002312 if (publicType.getUserDef())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002313 {
2314 op = EOpConstructStruct;
2315 }
2316 else
2317 {
Geoff Lang156d7192016-07-21 16:11:00 -04002318 op = sh::TypeToConstructorOperator(TType(publicType));
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002319 if (op == EOpNull)
2320 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002321 error(publicType.getLine(), "cannot construct this type",
2322 getBasicString(publicType.getBasicType()));
2323 publicType.setBasicType(EbtFloat);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002324 op = EOpConstructFloat;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002325 }
2326 }
2327
2328 TString tempString;
Dmitry Skiba7f17a502015-06-22 15:08:39 -07002329 const TType *type = new TType(publicType);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002330 return new TFunction(&tempString, type, op);
2331}
2332
Jamie Madillb98c3a82015-07-23 14:26:04 -04002333// This function is used to test for the correctness of the parameters passed to various constructor
2334// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002335//
Olli Etuaho856c4972016-08-08 11:38:39 +03002336// 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 +00002337//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002338TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002339 TOperator op,
2340 TFunction *fnCall,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302341 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002342{
Olli Etuaho856c4972016-08-08 11:38:39 +03002343 TType type = fnCall->getReturnType();
2344 if (type.isUnsizedArray())
2345 {
2346 type.setArraySize(static_cast<unsigned int>(fnCall->getParamCount()));
2347 }
2348 bool constType = true;
2349 for (size_t i = 0; i < fnCall->getParamCount(); ++i)
2350 {
2351 const TConstParameter &param = fnCall->getParam(i);
2352 if (param.type->getQualifier() != EvqConst)
2353 constType = false;
2354 }
2355 if (constType)
2356 type.setQualifier(EvqConst);
2357
Olli Etuaho8a176262016-08-16 14:23:01 +03002358 if (!checkConstructorArguments(line, arguments, *fnCall, op, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03002359 {
2360 TIntermTyped *dummyNode = intermediate.setAggregateOperator(nullptr, op, line);
2361 dummyNode->setType(type);
2362 return dummyNode;
2363 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002364 TIntermAggregate *constructor = arguments->getAsAggregate();
2365 ASSERT(constructor != nullptr);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002366
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002367 // Turn the argument list itself into a constructor
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002368 constructor->setOp(op);
2369 constructor->setLine(line);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002370 ASSERT(constructor->isConstructor());
2371
2372 // Need to set type before setPrecisionFromChildren() because bool doesn't have precision.
Olli Etuaho856c4972016-08-08 11:38:39 +03002373 constructor->setType(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002374
Olli Etuaho21203702014-11-13 16:16:21 +02002375 // Structs should not be precision qualified, the individual members may be.
2376 // Built-in types on the other hand should be precision qualified.
2377 if (op != EOpConstructStruct)
2378 {
2379 constructor->setPrecisionFromChildren();
Olli Etuaho856c4972016-08-08 11:38:39 +03002380 type.setPrecision(constructor->getPrecision());
Olli Etuaho21203702014-11-13 16:16:21 +02002381 }
2382
Olli Etuaho856c4972016-08-08 11:38:39 +03002383 constructor->setType(type);
2384
Olli Etuahof119a262016-08-19 15:54:22 +03002385 TIntermTyped *constConstructor = intermediate.foldAggregateBuiltIn(constructor, &mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002386 if (constConstructor)
2387 {
2388 return constConstructor;
2389 }
2390
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002391 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002392}
2393
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002394//
2395// Interface/uniform blocks
2396//
Martin Radev70866b82016-07-22 15:27:42 +03002397TIntermAggregate *TParseContext::addInterfaceBlock(
2398 const TTypeQualifierBuilder &typeQualifierBuilder,
2399 const TSourceLoc &nameLine,
2400 const TString &blockName,
2401 TFieldList *fieldList,
2402 const TString *instanceName,
2403 const TSourceLoc &instanceLine,
2404 TIntermTyped *arrayIndex,
2405 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002406{
Olli Etuaho856c4972016-08-08 11:38:39 +03002407 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002408
Olli Etuaho613b9592016-09-05 12:05:53 +03002409 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03002410
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002411 if (typeQualifier.qualifier != EvqUniform)
2412 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302413 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
2414 "interface blocks must be uniform");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002415 }
2416
Martin Radev70866b82016-07-22 15:27:42 +03002417 if (typeQualifier.invariant)
2418 {
2419 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
2420 }
2421
Jamie Madill099c0f32013-06-20 11:55:52 -04002422 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03002423 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002424
Jamie Madill099c0f32013-06-20 11:55:52 -04002425 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2426 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002427 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002428 }
2429
Jamie Madill1566ef72013-06-20 11:55:54 -04002430 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2431 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002432 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002433 }
2434
Olli Etuaho856c4972016-08-08 11:38:39 +03002435 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002436
Arun Patole7e7e68d2015-05-22 12:02:25 +05302437 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2438 if (!symbolTable.declare(blockNameSymbol))
2439 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002440 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002441 }
2442
Jamie Madill98493dd2013-07-08 14:39:03 -04002443 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302444 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2445 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002446 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302447 TType *fieldType = field->type();
2448 if (IsSampler(fieldType->getBasicType()))
2449 {
2450 error(field->line(), "unsupported type", fieldType->getBasicString(),
2451 "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002452 }
2453
Jamie Madill98493dd2013-07-08 14:39:03 -04002454 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002455 switch (qualifier)
2456 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002457 case EvqGlobal:
2458 case EvqUniform:
2459 break;
2460 default:
2461 error(field->line(), "invalid qualifier on interface block member",
2462 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04002463 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002464 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002465
Martin Radev70866b82016-07-22 15:27:42 +03002466 if (fieldType->isInvariant())
2467 {
2468 error(field->line(), "invalid qualifier on interface block member", "invariant");
2469 }
2470
Jamie Madilla5efff92013-06-06 11:56:47 -04002471 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002472 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03002473 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
Jamie Madill099c0f32013-06-20 11:55:52 -04002474
Jamie Madill98493dd2013-07-08 14:39:03 -04002475 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002476 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002477 error(field->line(), "invalid layout qualifier:",
2478 getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04002479 }
2480
Jamie Madill98493dd2013-07-08 14:39:03 -04002481 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002482 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002483 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002484 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002485 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002486 {
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002487 warning(field->line(), "extraneous layout qualifier:",
Jamie Madillb98c3a82015-07-23 14:26:04 -04002488 getMatrixPackingString(fieldLayoutQualifier.matrixPacking),
2489 "only has an effect on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04002490 }
2491
Jamie Madill98493dd2013-07-08 14:39:03 -04002492 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002493 }
2494
Jamie Madill98493dd2013-07-08 14:39:03 -04002495 // add array index
Olli Etuaho856c4972016-08-08 11:38:39 +03002496 unsigned int arraySize = 0;
2497 if (arrayIndex != nullptr)
Jamie Madill98493dd2013-07-08 14:39:03 -04002498 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002499 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04002500 }
2501
Jamie Madillb98c3a82015-07-23 14:26:04 -04002502 TInterfaceBlock *interfaceBlock =
2503 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2504 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
2505 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002506
2507 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04002508 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002509
Jamie Madill98493dd2013-07-08 14:39:03 -04002510 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002511 {
2512 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002513 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2514 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002515 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302516 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002517
2518 // set parent pointer of the field variable
2519 fieldType->setInterfaceBlock(interfaceBlock);
2520
Arun Patole7e7e68d2015-05-22 12:02:25 +05302521 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002522 fieldVariable->setQualifier(typeQualifier.qualifier);
2523
Arun Patole7e7e68d2015-05-22 12:02:25 +05302524 if (!symbolTable.declare(fieldVariable))
2525 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002526 error(field->line(), "redefinition", field->name().c_str(),
2527 "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002528 }
2529 }
2530 }
2531 else
2532 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002533 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03002534
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002535 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302536 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002537 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002538
Arun Patole7e7e68d2015-05-22 12:02:25 +05302539 if (!symbolTable.declare(instanceTypeDef))
2540 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002541 error(instanceLine, "redefinition", instanceName->c_str(),
2542 "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002543 }
2544
Jamie Madillb98c3a82015-07-23 14:26:04 -04002545 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002546 symbolName = instanceTypeDef->getName();
2547 }
2548
Olli Etuaho32db19b2016-10-04 14:43:16 +01002549 TIntermAggregate *aggregate = TIntermediate::MakeAggregate(
Jamie Madillb98c3a82015-07-23 14:26:04 -04002550 intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line),
2551 nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002552 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04002553
2554 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002555 return aggregate;
2556}
2557
Olli Etuaho383b7912016-08-05 11:22:59 +03002558void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002559{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002560 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002561
2562 // Embedded structure definitions are not supported per GLSL ES spec.
2563 // They aren't allowed in GLSL either, but we need to detect this here
2564 // so we don't rely on the GLSL compiler to catch it.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302565 if (mStructNestingLevel > 1)
2566 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002567 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002568 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002569}
2570
2571void TParseContext::exitStructDeclaration()
2572{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002573 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002574}
2575
Jamie Madillb98c3a82015-07-23 14:26:04 -04002576namespace
2577{
kbr@chromium.org476541f2011-10-27 21:14:51 +00002578const int kWebGLMaxStructNesting = 4;
2579
2580} // namespace
2581
Olli Etuaho8a176262016-08-16 14:23:01 +03002582void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002583{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302584 if (!IsWebGLBasedSpec(mShaderSpec))
2585 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002586 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002587 }
2588
Arun Patole7e7e68d2015-05-22 12:02:25 +05302589 if (field.type()->getBasicType() != EbtStruct)
2590 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002591 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002592 }
2593
2594 // We're already inside a structure definition at this point, so add
2595 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302596 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2597 {
Jamie Madill41a49272014-03-18 16:10:13 -04002598 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002599 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
2600 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04002601 std::string reason = reasonStream.str();
2602 error(line, reason.c_str(), field.name().c_str(), "");
Olli Etuaho8a176262016-08-16 14:23:01 +03002603 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002604 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002605}
2606
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002607//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002608// Parse an array index expression
2609//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002610TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
2611 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302612 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002613{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002614 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2615 {
2616 if (baseExpression->getAsSymbolNode())
2617 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302618 error(location, " left of '[' is not of type array, matrix, or vector ",
2619 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002620 }
2621 else
2622 {
2623 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2624 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002625
2626 TConstantUnion *unionArray = new TConstantUnion[1];
2627 unionArray->setFConst(0.0f);
2628 return intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst),
2629 location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002630 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002631
Jamie Madill21c1e452014-12-29 11:33:41 -05002632 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2633
Olli Etuaho36b05142015-11-12 13:10:42 +02002634 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
2635 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
2636 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
2637 // index is a constant expression.
2638 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
2639 {
2640 if (baseExpression->isInterfaceBlock())
2641 {
2642 error(
2643 location, "", "[",
2644 "array indexes for interface blocks arrays must be constant integral expressions");
Olli Etuaho36b05142015-11-12 13:10:42 +02002645 }
2646 else if (baseExpression->getQualifier() == EvqFragmentOut)
2647 {
2648 error(location, "", "[",
2649 "array indexes for fragment outputs must be constant integral expressions");
Olli Etuaho36b05142015-11-12 13:10:42 +02002650 }
Olli Etuaho3e960462015-11-12 15:58:39 +02002651 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
2652 {
2653 error(location, "", "[", "array index for gl_FragData must be constant zero");
Olli Etuaho3e960462015-11-12 15:58:39 +02002654 }
Olli Etuaho36b05142015-11-12 13:10:42 +02002655 }
2656
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002657 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002658 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002659 // If an out-of-range index is not qualified as constant, the behavior in the spec is
2660 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
2661 // constant fold expressions that are not constant expressions). The most compatible way to
2662 // handle this case is to report a warning instead of an error and force the index to be in
2663 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002664 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Jamie Madill21c1e452014-12-29 11:33:41 -05002665 int index = indexConstantUnion->getIConst(0);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002666
2667 int safeIndex = -1;
2668
2669 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04002670 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002671 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03002672 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002673 if (mShaderSpec == SH_WEBGL2_SPEC)
2674 {
2675 // Error has been already generated if index is not const.
2676 if (indexExpression->getQualifier() == EvqConst)
2677 {
2678 error(location, "", "[",
2679 "array index for gl_FragData must be constant zero");
2680 }
2681 safeIndex = 0;
2682 }
2683 else if (!isExtensionEnabled("GL_EXT_draw_buffers"))
2684 {
2685 outOfRangeError(outOfRangeIndexIsError, location, "", "[",
2686 "array index for gl_FragData must be zero when "
2687 "GL_EXT_draw_buffers is disabled");
2688 safeIndex = 0;
2689 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03002690 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002691 // Only do generic out-of-range check if similar error hasn't already been reported.
2692 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03002693 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002694 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
2695 baseExpression->getArraySize(),
2696 "array index out of range", "[]");
2697 }
2698 }
2699 else if (baseExpression->isMatrix())
2700 {
2701 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03002702 baseExpression->getType().getCols(),
2703 "matrix field selection out of range", "[]");
Jamie Madill7164cf42013-07-08 13:30:59 -04002704 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002705 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04002706 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002707 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
2708 baseExpression->getType().getNominalSize(),
2709 "vector field selection out of range", "[]");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002710 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002711
2712 ASSERT(safeIndex >= 0);
2713 // Data of constant unions can't be changed, because it may be shared with other
2714 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
2715 // sanitized object.
2716 if (safeIndex != index)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002717 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002718 TConstantUnion *safeConstantUnion = new TConstantUnion();
2719 safeConstantUnion->setIConst(safeIndex);
2720 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002721 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002722
2723 return intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location,
2724 &mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002725 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002726 else
2727 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002728 return intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location,
2729 &mDiagnostics);
Jamie Madill7164cf42013-07-08 13:30:59 -04002730 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002731}
2732
Olli Etuaho90892fb2016-07-14 14:44:51 +03002733int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
2734 const TSourceLoc &location,
2735 int index,
2736 int arraySize,
2737 const char *reason,
2738 const char *token)
2739{
2740 if (index >= arraySize || index < 0)
2741 {
2742 std::stringstream extraInfoStream;
2743 extraInfoStream << "'" << index << "'";
2744 std::string extraInfo = extraInfoStream.str();
2745 outOfRangeError(outOfRangeIndexIsError, location, reason, token, extraInfo.c_str());
2746 if (index < 0)
2747 {
2748 return 0;
2749 }
2750 else
2751 {
2752 return arraySize - 1;
2753 }
2754 }
2755 return index;
2756}
2757
Jamie Madillb98c3a82015-07-23 14:26:04 -04002758TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
2759 const TSourceLoc &dotLocation,
2760 const TString &fieldString,
2761 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002762{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002763 if (baseExpression->isArray())
2764 {
2765 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002766 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002767 }
2768
2769 if (baseExpression->isVector())
2770 {
2771 TVectorFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002772 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields,
2773 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002774 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002775 fields.num = 1;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002776 fields.offsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002777 }
2778
Olli Etuahob6fa0432016-09-28 16:28:05 +01002779 return TIntermediate::AddSwizzle(baseExpression, fields, dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002780 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002781 else if (baseExpression->getBasicType() == EbtStruct)
2782 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302783 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04002784 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002785 {
2786 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002787 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002788 }
2789 else
2790 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002791 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002792 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002793 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002794 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002795 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002796 {
2797 fieldFound = true;
2798 break;
2799 }
2800 }
2801 if (fieldFound)
2802 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002803 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
2804 index->setLine(fieldLocation);
2805 return intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index,
2806 dotLocation, &mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002807 }
2808 else
2809 {
2810 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002811 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002812 }
2813 }
2814 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002815 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002816 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302817 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04002818 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002819 {
2820 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002821 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002822 }
2823 else
2824 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002825 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002826 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002827 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002828 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002829 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002830 {
2831 fieldFound = true;
2832 break;
2833 }
2834 }
2835 if (fieldFound)
2836 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002837 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
2838 index->setLine(fieldLocation);
2839 return intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index,
2840 dotLocation, &mDiagnostics);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002841 }
2842 else
2843 {
2844 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002845 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002846 }
2847 }
2848 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002849 else
2850 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002851 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002852 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03002853 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05302854 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002855 }
2856 else
2857 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302858 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03002859 " field selection requires structure, vector, or interface block on left hand "
2860 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05302861 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002862 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002863 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002864 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002865}
2866
Jamie Madillb98c3a82015-07-23 14:26:04 -04002867TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
2868 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002869{
Martin Radev802abe02016-08-04 17:48:32 +03002870 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002871
2872 if (qualifierType == "shared")
2873 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002874 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002875 }
2876 else if (qualifierType == "packed")
2877 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002878 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002879 }
2880 else if (qualifierType == "std140")
2881 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002882 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002883 }
2884 else if (qualifierType == "row_major")
2885 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002886 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002887 }
2888 else if (qualifierType == "column_major")
2889 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002890 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002891 }
2892 else if (qualifierType == "location")
2893 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002894 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
2895 "location requires an argument");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002896 }
2897 else
2898 {
2899 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002900 }
2901
Jamie Madilla5efff92013-06-06 11:56:47 -04002902 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002903}
2904
Martin Radev802abe02016-08-04 17:48:32 +03002905void TParseContext::parseLocalSize(const TString &qualifierType,
2906 const TSourceLoc &qualifierTypeLine,
2907 int intValue,
2908 const TSourceLoc &intValueLine,
2909 const std::string &intValueString,
2910 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002911 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03002912{
Olli Etuaho856c4972016-08-08 11:38:39 +03002913 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03002914 if (intValue < 1)
2915 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002916 std::string errorMessage = std::string(getWorkGroupSizeString(index)) + " must be positive";
Martin Radev802abe02016-08-04 17:48:32 +03002917 error(intValueLine, "out of range:", intValueString.c_str(), errorMessage.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03002918 }
2919 (*localSize)[index] = intValue;
2920}
2921
Jamie Madillb98c3a82015-07-23 14:26:04 -04002922TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
2923 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002924 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302925 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002926{
Martin Radev802abe02016-08-04 17:48:32 +03002927 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002928
Martin Radev802abe02016-08-04 17:48:32 +03002929 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002930
Martin Radev802abe02016-08-04 17:48:32 +03002931 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002932 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002933 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002934 if (intValue < 0)
2935 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002936 error(intValueLine, "out of range:", intValueString.c_str(),
2937 "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002938 }
2939 else
2940 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002941 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03002942 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002943 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002944 }
Martin Radev802abe02016-08-04 17:48:32 +03002945 else if (qualifierType == "local_size_x")
2946 {
2947 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
2948 &qualifier.localSize);
2949 }
2950 else if (qualifierType == "local_size_y")
2951 {
2952 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
2953 &qualifier.localSize);
2954 }
2955 else if (qualifierType == "local_size_z")
2956 {
2957 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
2958 &qualifier.localSize);
2959 }
2960 else
2961 {
2962 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03002963 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002964
Jamie Madilla5efff92013-06-06 11:56:47 -04002965 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002966}
2967
Olli Etuaho613b9592016-09-05 12:05:53 +03002968TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
2969{
2970 return new TTypeQualifierBuilder(
2971 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
2972 mShaderVersion);
2973}
2974
Jamie Madillb98c3a82015-07-23 14:26:04 -04002975TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03002976 TLayoutQualifier rightQualifier,
2977 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002978{
Martin Radevc28888b2016-07-22 15:27:42 +03002979 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
2980 &mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002981}
2982
Martin Radev70866b82016-07-22 15:27:42 +03002983TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
2984 const TTypeQualifierBuilder &typeQualifierBuilder,
2985 TPublicType *typeSpecifier,
2986 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002987{
Olli Etuaho613b9592016-09-05 12:05:53 +03002988 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002989
Martin Radev70866b82016-07-22 15:27:42 +03002990 typeSpecifier->qualifier = typeQualifier.qualifier;
2991 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
2992 typeSpecifier->invariant = typeQualifier.invariant;
2993 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05302994 {
Martin Radev70866b82016-07-22 15:27:42 +03002995 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002996 }
Martin Radev70866b82016-07-22 15:27:42 +03002997 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002998}
2999
Jamie Madillb98c3a82015-07-23 14:26:04 -04003000TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3001 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003002{
Martin Radev4a9cd802016-09-01 16:51:51 +03003003 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
3004 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03003005
Martin Radev4a9cd802016-09-01 16:51:51 +03003006 checkIsNonVoid(typeSpecifier.getLine(), (*fieldList)[0]->name(), typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003007
Martin Radev4a9cd802016-09-01 16:51:51 +03003008 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003009
Arun Patole7e7e68d2015-05-22 12:02:25 +05303010 for (unsigned int i = 0; i < fieldList->size(); ++i)
3011 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003012 //
3013 // Careful not to replace already known aspects of type, like array-ness
3014 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303015 TType *type = (*fieldList)[i]->type();
Martin Radev4a9cd802016-09-01 16:51:51 +03003016 type->setBasicType(typeSpecifier.getBasicType());
3017 type->setPrimarySize(typeSpecifier.getPrimarySize());
3018 type->setSecondarySize(typeSpecifier.getSecondarySize());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003019 type->setPrecision(typeSpecifier.precision);
3020 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003021 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
Martin Radev70866b82016-07-22 15:27:42 +03003022 type->setInvariant(typeSpecifier.invariant);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003023
3024 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303025 if (type->isArray())
3026 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003027 checkIsValidTypeForArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003028 }
3029 if (typeSpecifier.array)
Olli Etuaho856c4972016-08-08 11:38:39 +03003030 type->setArraySize(static_cast<unsigned int>(typeSpecifier.arraySize));
Martin Radev4a9cd802016-09-01 16:51:51 +03003031 if (typeSpecifier.getUserDef())
Arun Patole7e7e68d2015-05-22 12:02:25 +05303032 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003033 type->setStruct(typeSpecifier.getUserDef()->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003034 }
3035
Martin Radev4a9cd802016-09-01 16:51:51 +03003036 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*fieldList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003037 }
3038
Jamie Madill98493dd2013-07-08 14:39:03 -04003039 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003040}
3041
Martin Radev4a9cd802016-09-01 16:51:51 +03003042TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
3043 const TSourceLoc &nameLine,
3044 const TString *structName,
3045 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003046{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303047 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003048 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003049
Jamie Madill9b820842015-02-12 10:40:10 -05003050 // Store a bool in the struct if we're at global scope, to allow us to
3051 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05003052 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003053
Jamie Madill98493dd2013-07-08 14:39:03 -04003054 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003055 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003056 checkIsNotReserved(nameLine, *structName);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303057 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
3058 if (!symbolTable.declare(userTypeDef))
3059 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003060 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003061 }
3062 }
3063
3064 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04003065 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003066 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003067 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04003068 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003069 switch (qualifier)
3070 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003071 case EvqGlobal:
3072 case EvqTemporary:
3073 break;
3074 default:
3075 error(field.line(), "invalid qualifier on struct member",
3076 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003077 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003078 }
Martin Radev70866b82016-07-22 15:27:42 +03003079 if (field.type()->isInvariant())
3080 {
3081 error(field.line(), "invalid qualifier on struct member", "invariant");
3082 }
3083
3084 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003085 }
3086
Martin Radev4a9cd802016-09-01 16:51:51 +03003087 TTypeSpecifierNonArray typeSpecifierNonArray;
3088 typeSpecifierNonArray.initialize(EbtStruct, structLine);
3089 typeSpecifierNonArray.userDef = structureType;
3090 typeSpecifierNonArray.isStructSpecifier = true;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003091 exitStructDeclaration();
3092
Martin Radev4a9cd802016-09-01 16:51:51 +03003093 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003094}
3095
Jamie Madillb98c3a82015-07-23 14:26:04 -04003096TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003097 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003098 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02003099{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003100 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003101 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02003102 init->isVector())
3103 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003104 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
3105 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003106 return nullptr;
3107 }
3108
Olli Etuahoac5274d2015-02-20 10:19:08 +02003109 if (statementList)
3110 {
3111 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
3112 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02003113 return nullptr;
3114 }
3115 }
3116
Olli Etuahoa3a36662015-02-17 13:46:51 +02003117 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3118 if (node == nullptr)
3119 {
3120 error(loc, "erroneous switch statement", "switch");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003121 return nullptr;
3122 }
3123 return node;
3124}
3125
3126TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3127{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003128 if (mSwitchNestingLevel == 0)
3129 {
3130 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003131 return nullptr;
3132 }
3133 if (condition == nullptr)
3134 {
3135 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003136 return nullptr;
3137 }
3138 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04003139 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02003140 {
3141 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003142 }
3143 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003144 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
3145 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
3146 // fold in case labels.
3147 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02003148 {
3149 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003150 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003151 TIntermCase *node = intermediate.addCase(condition, loc);
3152 if (node == nullptr)
3153 {
3154 error(loc, "erroneous case statement", "case");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003155 return nullptr;
3156 }
3157 return node;
3158}
3159
3160TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3161{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003162 if (mSwitchNestingLevel == 0)
3163 {
3164 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003165 return nullptr;
3166 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003167 TIntermCase *node = intermediate.addCase(nullptr, loc);
3168 if (node == nullptr)
3169 {
3170 error(loc, "erroneous default statement", "default");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003171 return nullptr;
3172 }
3173 return node;
3174}
3175
Jamie Madillb98c3a82015-07-23 14:26:04 -04003176TIntermTyped *TParseContext::createUnaryMath(TOperator op,
3177 TIntermTyped *child,
3178 const TSourceLoc &loc,
3179 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003180{
3181 if (child == nullptr)
3182 {
3183 return nullptr;
3184 }
3185
3186 switch (op)
3187 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003188 case EOpLogicalNot:
3189 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
3190 child->isVector())
3191 {
3192 return nullptr;
3193 }
3194 break;
3195 case EOpBitwiseNot:
3196 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3197 child->isMatrix() || child->isArray())
3198 {
3199 return nullptr;
3200 }
3201 break;
3202 case EOpPostIncrement:
3203 case EOpPreIncrement:
3204 case EOpPostDecrement:
3205 case EOpPreDecrement:
3206 case EOpNegative:
3207 case EOpPositive:
3208 if (child->getBasicType() == EbtStruct || child->getBasicType() == EbtBool ||
Olli Etuaho558b0382016-08-26 17:54:34 +03003209 child->isArray() || IsSampler(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04003210 {
3211 return nullptr;
3212 }
3213 // Operators for built-ins are already type checked against their prototype.
3214 default:
3215 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003216 }
3217
Olli Etuahof119a262016-08-19 15:54:22 +03003218 TIntermUnary *node = new TIntermUnary(op, child);
3219 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03003220
3221 TIntermTyped *foldedNode = node->fold(&mDiagnostics);
3222 if (foldedNode)
3223 return foldedNode;
3224
3225 return node;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003226}
3227
Olli Etuaho09b22472015-02-11 11:47:26 +02003228TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3229{
Olli Etuahof6c694b2015-03-26 14:50:53 +02003230 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003231 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003232 {
3233 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003234 return child;
3235 }
3236 return node;
3237}
3238
Jamie Madillb98c3a82015-07-23 14:26:04 -04003239TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
3240 TIntermTyped *child,
3241 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003242{
Olli Etuaho856c4972016-08-08 11:38:39 +03003243 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02003244 return addUnaryMath(op, child, loc);
3245}
3246
Jamie Madillb98c3a82015-07-23 14:26:04 -04003247bool TParseContext::binaryOpCommonCheck(TOperator op,
3248 TIntermTyped *left,
3249 TIntermTyped *right,
3250 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003251{
Olli Etuaho244be012016-08-18 15:26:02 +03003252 if (left->getType().getStruct() || right->getType().getStruct())
3253 {
3254 switch (op)
3255 {
3256 case EOpIndexDirectStruct:
3257 ASSERT(left->getType().getStruct());
3258 break;
3259 case EOpEqual:
3260 case EOpNotEqual:
3261 case EOpAssign:
3262 case EOpInitialize:
3263 if (left->getType() != right->getType())
3264 {
3265 return false;
3266 }
3267 break;
3268 default:
3269 error(loc, "Invalid operation for structs", GetOperatorString(op));
3270 return false;
3271 }
3272 }
3273
Olli Etuahod6b14282015-03-17 14:31:35 +02003274 if (left->isArray() || right->isArray())
3275 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003276 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003277 {
3278 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3279 return false;
3280 }
3281
3282 if (left->isArray() != right->isArray())
3283 {
3284 error(loc, "array / non-array mismatch", GetOperatorString(op));
3285 return false;
3286 }
3287
3288 switch (op)
3289 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003290 case EOpEqual:
3291 case EOpNotEqual:
3292 case EOpAssign:
3293 case EOpInitialize:
3294 break;
3295 default:
3296 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3297 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02003298 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003299 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003300 if (left->getArraySize() != right->getArraySize())
3301 {
3302 error(loc, "array size mismatch", GetOperatorString(op));
3303 return false;
3304 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003305 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003306
3307 // Check ops which require integer / ivec parameters
3308 bool isBitShift = false;
3309 switch (op)
3310 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003311 case EOpBitShiftLeft:
3312 case EOpBitShiftRight:
3313 case EOpBitShiftLeftAssign:
3314 case EOpBitShiftRightAssign:
3315 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3316 // check that the basic type is an integer type.
3317 isBitShift = true;
3318 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3319 {
3320 return false;
3321 }
3322 break;
3323 case EOpBitwiseAnd:
3324 case EOpBitwiseXor:
3325 case EOpBitwiseOr:
3326 case EOpBitwiseAndAssign:
3327 case EOpBitwiseXorAssign:
3328 case EOpBitwiseOrAssign:
3329 // It is enough to check the type of only one operand, since later it
3330 // is checked that the operand types match.
3331 if (!IsInteger(left->getBasicType()))
3332 {
3333 return false;
3334 }
3335 break;
3336 default:
3337 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003338 }
3339
3340 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3341 // So the basic type should usually match.
3342 if (!isBitShift && left->getBasicType() != right->getBasicType())
3343 {
3344 return false;
3345 }
3346
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003347 // Check that:
3348 // 1. Type sizes match exactly on ops that require that.
3349 // 2. Restrictions for structs that contain arrays or samplers are respected.
3350 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003351 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003352 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003353 case EOpAssign:
3354 case EOpInitialize:
3355 case EOpEqual:
3356 case EOpNotEqual:
3357 // ESSL 1.00 sections 5.7, 5.8, 5.9
3358 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
3359 {
3360 error(loc, "undefined operation for structs containing arrays",
3361 GetOperatorString(op));
3362 return false;
3363 }
3364 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3365 // we interpret the spec so that this extends to structs containing samplers,
3366 // similarly to ESSL 1.00 spec.
3367 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
3368 left->getType().isStructureContainingSamplers())
3369 {
3370 error(loc, "undefined operation for structs containing samplers",
3371 GetOperatorString(op));
3372 return false;
3373 }
3374 case EOpLessThan:
3375 case EOpGreaterThan:
3376 case EOpLessThanEqual:
3377 case EOpGreaterThanEqual:
3378 if ((left->getNominalSize() != right->getNominalSize()) ||
3379 (left->getSecondarySize() != right->getSecondarySize()))
3380 {
3381 return false;
3382 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003383 break;
3384 case EOpAdd:
3385 case EOpSub:
3386 case EOpDiv:
3387 case EOpIMod:
3388 case EOpBitShiftLeft:
3389 case EOpBitShiftRight:
3390 case EOpBitwiseAnd:
3391 case EOpBitwiseXor:
3392 case EOpBitwiseOr:
3393 case EOpAddAssign:
3394 case EOpSubAssign:
3395 case EOpDivAssign:
3396 case EOpIModAssign:
3397 case EOpBitShiftLeftAssign:
3398 case EOpBitShiftRightAssign:
3399 case EOpBitwiseAndAssign:
3400 case EOpBitwiseXorAssign:
3401 case EOpBitwiseOrAssign:
3402 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
3403 {
3404 return false;
3405 }
3406
3407 // Are the sizes compatible?
3408 if (left->getNominalSize() != right->getNominalSize() ||
3409 left->getSecondarySize() != right->getSecondarySize())
3410 {
3411 // If the nominal sizes of operands do not match:
3412 // One of them must be a scalar.
3413 if (!left->isScalar() && !right->isScalar())
3414 return false;
3415
3416 // In the case of compound assignment other than multiply-assign,
3417 // the right side needs to be a scalar. Otherwise a vector/matrix
3418 // would be assigned to a scalar. A scalar can't be shifted by a
3419 // vector either.
3420 if (!right->isScalar() &&
3421 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
3422 return false;
3423 }
3424 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003425 default:
3426 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003427 }
3428
Olli Etuahod6b14282015-03-17 14:31:35 +02003429 return true;
3430}
3431
Olli Etuaho1dded802016-08-18 18:13:13 +03003432bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
3433 const TType &left,
3434 const TType &right)
3435{
3436 switch (op)
3437 {
3438 case EOpMul:
3439 case EOpMulAssign:
3440 return left.getNominalSize() == right.getNominalSize() &&
3441 left.getSecondarySize() == right.getSecondarySize();
3442 case EOpVectorTimesScalar:
3443 return true;
3444 case EOpVectorTimesScalarAssign:
3445 ASSERT(!left.isMatrix() && !right.isMatrix());
3446 return left.isVector() && !right.isVector();
3447 case EOpVectorTimesMatrix:
3448 return left.getNominalSize() == right.getRows();
3449 case EOpVectorTimesMatrixAssign:
3450 ASSERT(!left.isMatrix() && right.isMatrix());
3451 return left.isVector() && left.getNominalSize() == right.getRows() &&
3452 left.getNominalSize() == right.getCols();
3453 case EOpMatrixTimesVector:
3454 return left.getCols() == right.getNominalSize();
3455 case EOpMatrixTimesScalar:
3456 return true;
3457 case EOpMatrixTimesScalarAssign:
3458 ASSERT(left.isMatrix() && !right.isMatrix());
3459 return !right.isVector();
3460 case EOpMatrixTimesMatrix:
3461 return left.getCols() == right.getRows();
3462 case EOpMatrixTimesMatrixAssign:
3463 ASSERT(left.isMatrix() && right.isMatrix());
3464 // We need to check two things:
3465 // 1. The matrix multiplication step is valid.
3466 // 2. The result will have the same number of columns as the lvalue.
3467 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
3468
3469 default:
3470 UNREACHABLE();
3471 return false;
3472 }
3473}
3474
Jamie Madillb98c3a82015-07-23 14:26:04 -04003475TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
3476 TIntermTyped *left,
3477 TIntermTyped *right,
3478 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02003479{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003480 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003481 return nullptr;
3482
Olli Etuahofc1806e2015-03-17 13:03:11 +02003483 switch (op)
3484 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003485 case EOpEqual:
3486 case EOpNotEqual:
3487 break;
3488 case EOpLessThan:
3489 case EOpGreaterThan:
3490 case EOpLessThanEqual:
3491 case EOpGreaterThanEqual:
Olli Etuaho244be012016-08-18 15:26:02 +03003492 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3493 !right->getType().getStruct());
3494 if (left->isMatrix() || left->isVector())
Jamie Madillb98c3a82015-07-23 14:26:04 -04003495 {
3496 return nullptr;
3497 }
3498 break;
3499 case EOpLogicalOr:
3500 case EOpLogicalXor:
3501 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03003502 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3503 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003504 if (left->getBasicType() != EbtBool || left->isMatrix() || left->isVector())
3505 {
3506 return nullptr;
3507 }
3508 break;
3509 case EOpAdd:
3510 case EOpSub:
3511 case EOpDiv:
3512 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03003513 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3514 !right->getType().getStruct());
3515 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04003516 {
3517 return nullptr;
3518 }
3519 break;
3520 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03003521 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3522 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003523 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03003524 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04003525 {
3526 return nullptr;
3527 }
3528 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003529 default:
3530 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003531 }
3532
Olli Etuaho1dded802016-08-18 18:13:13 +03003533 if (op == EOpMul)
3534 {
3535 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
3536 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
3537 {
3538 return nullptr;
3539 }
3540 }
3541
Olli Etuaho3fdec912016-08-18 15:08:06 +03003542 TIntermBinary *node = new TIntermBinary(op, left, right);
3543 node->setLine(loc);
3544
Olli Etuaho3fdec912016-08-18 15:08:06 +03003545 // See if we can fold constants.
3546 TIntermTyped *foldedNode = node->fold(&mDiagnostics);
3547 if (foldedNode)
3548 return foldedNode;
3549
3550 return node;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003551}
3552
Jamie Madillb98c3a82015-07-23 14:26:04 -04003553TIntermTyped *TParseContext::addBinaryMath(TOperator op,
3554 TIntermTyped *left,
3555 TIntermTyped *right,
3556 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003557{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003558 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003559 if (node == 0)
3560 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003561 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3562 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003563 return left;
3564 }
3565 return node;
3566}
3567
Jamie Madillb98c3a82015-07-23 14:26:04 -04003568TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
3569 TIntermTyped *left,
3570 TIntermTyped *right,
3571 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003572{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003573 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003574 if (node == 0)
3575 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003576 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3577 right->getCompleteString());
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003578 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02003579 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003580 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
3581 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003582 }
3583 return node;
3584}
3585
Jamie Madillb98c3a82015-07-23 14:26:04 -04003586TIntermTyped *TParseContext::createAssign(TOperator op,
3587 TIntermTyped *left,
3588 TIntermTyped *right,
3589 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003590{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003591 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003592 {
Olli Etuaho1dded802016-08-18 18:13:13 +03003593 if (op == EOpMulAssign)
3594 {
3595 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
3596 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
3597 {
3598 return nullptr;
3599 }
3600 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03003601 TIntermBinary *node = new TIntermBinary(op, left, right);
3602 node->setLine(loc);
3603
Olli Etuaho3fdec912016-08-18 15:08:06 +03003604 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02003605 }
3606 return nullptr;
3607}
3608
Jamie Madillb98c3a82015-07-23 14:26:04 -04003609TIntermTyped *TParseContext::addAssign(TOperator op,
3610 TIntermTyped *left,
3611 TIntermTyped *right,
3612 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003613{
3614 TIntermTyped *node = createAssign(op, left, right, loc);
3615 if (node == nullptr)
3616 {
3617 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02003618 return left;
3619 }
3620 return node;
3621}
3622
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003623TIntermTyped *TParseContext::addComma(TIntermTyped *left,
3624 TIntermTyped *right,
3625 const TSourceLoc &loc)
3626{
Corentin Wallez0d959252016-07-12 17:26:32 -04003627 // WebGL2 section 5.26, the following results in an error:
3628 // "Sequence operator applied to void, arrays, or structs containing arrays"
3629 if (mShaderSpec == SH_WEBGL2_SPEC && (left->isArray() || left->getBasicType() == EbtVoid ||
3630 left->getType().isStructureContainingArrays() ||
3631 right->isArray() || right->getBasicType() == EbtVoid ||
3632 right->getType().isStructureContainingArrays()))
3633 {
3634 error(loc,
3635 "sequence operator is not allowed for void, arrays, or structs containing arrays",
3636 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04003637 }
3638
Olli Etuaho15200042015-11-04 16:56:31 +02003639 return intermediate.addComma(left, right, loc, mShaderVersion);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003640}
3641
Olli Etuaho49300862015-02-20 14:54:49 +02003642TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3643{
3644 switch (op)
3645 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003646 case EOpContinue:
3647 if (mLoopNestingLevel <= 0)
3648 {
3649 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04003650 }
3651 break;
3652 case EOpBreak:
3653 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
3654 {
3655 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04003656 }
3657 break;
3658 case EOpReturn:
3659 if (mCurrentFunctionType->getBasicType() != EbtVoid)
3660 {
3661 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04003662 }
3663 break;
3664 default:
3665 // No checks for discard
3666 break;
Olli Etuaho49300862015-02-20 14:54:49 +02003667 }
3668 return intermediate.addBranch(op, loc);
3669}
3670
Jamie Madillb98c3a82015-07-23 14:26:04 -04003671TIntermBranch *TParseContext::addBranch(TOperator op,
3672 TIntermTyped *returnValue,
3673 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02003674{
3675 ASSERT(op == EOpReturn);
3676 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003677 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02003678 {
3679 error(loc, "void function cannot return a value", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02003680 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003681 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02003682 {
3683 error(loc, "function return is not matching type:", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02003684 }
3685 return intermediate.addBranch(op, returnValue, loc);
3686}
3687
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003688void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
3689{
3690 ASSERT(!functionCall->isUserDefined());
3691 const TString &name = functionCall->getName();
3692 TIntermNode *offset = nullptr;
3693 TIntermSequence *arguments = functionCall->getSequence();
3694 if (name.compare(0, 16, "texelFetchOffset") == 0 ||
3695 name.compare(0, 16, "textureLodOffset") == 0 ||
3696 name.compare(0, 20, "textureProjLodOffset") == 0 ||
3697 name.compare(0, 17, "textureGradOffset") == 0 ||
3698 name.compare(0, 21, "textureProjGradOffset") == 0)
3699 {
3700 offset = arguments->back();
3701 }
3702 else if (name.compare(0, 13, "textureOffset") == 0 ||
3703 name.compare(0, 17, "textureProjOffset") == 0)
3704 {
3705 // A bias parameter might follow the offset parameter.
3706 ASSERT(arguments->size() >= 3);
3707 offset = (*arguments)[2];
3708 }
3709 if (offset != nullptr)
3710 {
3711 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
3712 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
3713 {
3714 TString unmangledName = TFunction::unmangleName(name);
3715 error(functionCall->getLine(), "Texture offset must be a constant expression",
3716 unmangledName.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003717 }
3718 else
3719 {
3720 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
3721 size_t size = offsetConstantUnion->getType().getObjectSize();
3722 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
3723 for (size_t i = 0u; i < size; ++i)
3724 {
3725 int offsetValue = values[i].getIConst();
3726 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
3727 {
3728 std::stringstream tokenStream;
3729 tokenStream << offsetValue;
3730 std::string token = tokenStream.str();
3731 error(offset->getLine(), "Texture offset value out of valid range",
3732 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003733 }
3734 }
3735 }
3736 }
3737}
3738
Jamie Madillb98c3a82015-07-23 14:26:04 -04003739TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
3740 TIntermNode *paramNode,
3741 TIntermNode *thisNode,
3742 const TSourceLoc &loc,
3743 bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003744{
Jamie Madillb98c3a82015-07-23 14:26:04 -04003745 *fatalError = false;
3746 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003747 TIntermTyped *callNode = nullptr;
3748
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003749 if (thisNode != nullptr)
3750 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003751 TConstantUnion *unionArray = new TConstantUnion[1];
Jamie Madillb98c3a82015-07-23 14:26:04 -04003752 int arraySize = 0;
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003753 TIntermTyped *typedThis = thisNode->getAsTyped();
3754 if (fnCall->getName() != "length")
3755 {
3756 error(loc, "invalid method", fnCall->getName().c_str());
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003757 }
3758 else if (paramNode != nullptr)
3759 {
3760 error(loc, "method takes no parameters", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003761 }
3762 else if (typedThis == nullptr || !typedThis->isArray())
3763 {
3764 error(loc, "length can only be called on arrays", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003765 }
3766 else
3767 {
Olli Etuaho96e67382015-04-23 14:27:02 +03003768 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03003769 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003770 {
Olli Etuaho39282e12015-04-23 15:41:48 +03003771 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003772 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03003773 // (func()).length()
3774 // (int[3](0, 1, 2)).length()
Jamie Madillb98c3a82015-07-23 14:26:04 -04003775 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
3776 // expression.
3777 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
3778 // spec section 5.9 which allows "An array, vector or matrix expression with the
3779 // length method applied".
3780 error(loc, "length can only be called on array names, not on array expressions",
3781 "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003782 }
3783 }
Olli Etuaho96e67382015-04-23 14:27:02 +03003784 unionArray->setIConst(arraySize);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003785 callNode =
3786 intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003787 }
3788 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003789 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003790 // Then this should be a constructor.
Olli Etuaho856c4972016-08-08 11:38:39 +03003791 callNode = addConstructor(paramNode, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003792 }
3793 else
3794 {
3795 //
3796 // Not a constructor. Find it in the symbol table.
3797 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303798 const TFunction *fnCandidate;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003799 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003800 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003801 if (fnCandidate)
3802 {
3803 //
3804 // A declared function.
3805 //
Olli Etuaho383b7912016-08-05 11:22:59 +03003806 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003807 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003808 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003809 }
3810 op = fnCandidate->getBuiltInOp();
3811 if (builtIn && op != EOpNull)
3812 {
3813 //
3814 // A function call mapped to a built-in operation.
3815 //
3816 if (fnCandidate->getParamCount() == 1)
3817 {
3818 //
3819 // Treat it like a built-in unary operator.
3820 //
Olli Etuaho15c2ac32015-11-09 15:51:43 +02003821 TIntermAggregate *paramAgg = paramNode->getAsAggregate();
3822 paramNode = paramAgg->getSequence()->front();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003823 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc,
3824 &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003825 if (callNode == nullptr)
3826 {
3827 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003828 extraInfoStream
3829 << "built in unary operator function. Type: "
3830 << static_cast<TIntermTyped *>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003831 std::string extraInfo = extraInfoStream.str();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003832 error(paramNode->getLine(), " wrong operand type", "Internal Error",
3833 extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003834 *fatalError = true;
3835 return nullptr;
3836 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003837 }
3838 else
3839 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003840 TIntermAggregate *aggregate =
3841 intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003842 aggregate->setType(fnCandidate->getReturnType());
3843 aggregate->setPrecisionFromChildren();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02003844 if (aggregate->areChildrenConstQualified())
3845 {
3846 aggregate->getTypePointer()->setQualifier(EvqConst);
3847 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003848
3849 // Some built-in functions have out parameters too.
3850 functionCallLValueErrorCheck(fnCandidate, aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05303851
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003852 // See if we can constant fold a built-in. Note that this may be possible even
3853 // if it is not const-qualified.
Olli Etuahof119a262016-08-19 15:54:22 +03003854 TIntermTyped *foldedNode =
3855 intermediate.foldAggregateBuiltIn(aggregate, &mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05303856 if (foldedNode)
3857 {
Arun Patole274f0702015-05-05 13:33:30 +05303858 callNode = foldedNode;
3859 }
Olli Etuahob43846e2015-06-02 18:18:57 +03003860 else
3861 {
3862 callNode = aggregate;
3863 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003864 }
3865 }
3866 else
3867 {
3868 // This is a real function call
Jamie Madillb98c3a82015-07-23 14:26:04 -04003869 TIntermAggregate *aggregate =
3870 intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003871 aggregate->setType(fnCandidate->getReturnType());
3872
Jamie Madillb98c3a82015-07-23 14:26:04 -04003873 // this is how we know whether the given function is a builtIn function or a user
3874 // defined function
3875 // if builtIn == false, it's a userDefined -> could be an overloaded
3876 // builtIn function also
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003877 // if builtIn == true, it's definitely a builtIn function with EOpNull
3878 if (!builtIn)
3879 aggregate->setUserDefined();
3880 aggregate->setName(fnCandidate->getMangledName());
Corentin Wallez71d147f2015-02-11 11:15:24 -08003881 aggregate->setFunctionId(fnCandidate->getUniqueId());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003882
3883 // This needs to happen after the name is set
3884 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003885 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003886 aggregate->setBuiltInFunctionPrecision();
3887
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003888 checkTextureOffsetConst(aggregate);
3889 }
3890
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003891 callNode = aggregate;
3892
3893 functionCallLValueErrorCheck(fnCandidate, aggregate);
3894 }
3895 }
3896 else
3897 {
3898 // error message was put out by findFunction()
3899 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003900 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003901 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003902 callNode = intermediate.addConstantUnion(unionArray,
3903 TType(EbtFloat, EbpUndefined, EvqConst), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003904 }
3905 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003906 return callNode;
3907}
3908
Jamie Madillb98c3a82015-07-23 14:26:04 -04003909TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03003910 TIntermTyped *trueExpression,
3911 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03003912 const TSourceLoc &loc)
3913{
Olli Etuaho856c4972016-08-08 11:38:39 +03003914 checkIsScalarBool(loc, cond);
Olli Etuaho52901742015-04-15 13:42:45 +03003915
Olli Etuahod0bad2c2016-09-09 18:01:16 +03003916 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03003917 {
Olli Etuahod0bad2c2016-09-09 18:01:16 +03003918 binaryOpError(loc, ":", trueExpression->getCompleteString(),
3919 falseExpression->getCompleteString());
3920 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03003921 }
Olli Etuahoa2d53032015-04-15 14:14:44 +03003922 // ESSL1 sections 5.2 and 5.7:
3923 // ESSL3 section 5.7:
3924 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03003925 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03003926 {
3927 error(loc, "ternary operator is not allowed for structures or arrays", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03003928 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03003929 }
Corentin Wallez0d959252016-07-12 17:26:32 -04003930 // WebGL2 section 5.26, the following results in an error:
3931 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03003932 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04003933 {
3934 error(loc, "ternary operator is not allowed for void", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03003935 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04003936 }
3937
Olli Etuahod0bad2c2016-09-09 18:01:16 +03003938 return TIntermediate::AddTernarySelection(cond, trueExpression, falseExpression, loc);
Olli Etuaho52901742015-04-15 13:42:45 +03003939}
Olli Etuaho49300862015-02-20 14:54:49 +02003940
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003941//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003942// Parse an array of strings using yyparse.
3943//
3944// Returns 0 for success.
3945//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003946int PaParseStrings(size_t count,
3947 const char *const string[],
3948 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05303949 TParseContext *context)
3950{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003951 if ((count == 0) || (string == NULL))
3952 return 1;
3953
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003954 if (glslang_initialize(context))
3955 return 1;
3956
alokp@chromium.org408c45e2012-04-05 15:54:43 +00003957 int error = glslang_scan(count, string, length, context);
3958 if (!error)
3959 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003960
alokp@chromium.org73bc2982012-06-19 18:48:05 +00003961 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00003962
alokp@chromium.org6b495712012-06-29 00:06:58 +00003963 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003964}