blob: 36a2d8426907ba3f14b404587ef3661d6c1db264 [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 Etuahoee63f5d2016-01-04 11:34:54 +02002009TIntermAggregate *TParseContext::addFunctionPrototypeDeclaration(const TFunction &function,
2010 const TSourceLoc &location)
2011{
Olli Etuaho5d653182016-01-04 14:43:28 +02002012 // Note: symbolTableFunction could be the same as function if this is the first declaration.
2013 // Either way the instance in the symbol table is used to track whether the function is declared
2014 // multiple times.
2015 TFunction *symbolTableFunction =
2016 static_cast<TFunction *>(symbolTable.find(function.getMangledName(), getShaderVersion()));
2017 if (symbolTableFunction->hasPrototypeDeclaration() && mShaderVersion == 100)
2018 {
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 }
2023 symbolTableFunction->setHasPrototypeDeclaration();
2024
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002025 TIntermAggregate *prototype = new TIntermAggregate;
2026 prototype->setType(function.getReturnType());
2027 prototype->setName(function.getMangledName());
2028 prototype->setFunctionId(function.getUniqueId());
2029
2030 for (size_t i = 0; i < function.getParamCount(); i++)
2031 {
2032 const TConstParameter &param = function.getParam(i);
2033 if (param.name != 0)
2034 {
2035 TVariable variable(param.name, *param.type);
2036
2037 TIntermSymbol *paramSymbol = intermediate.addSymbol(
2038 variable.getUniqueId(), variable.getName(), variable.getType(), location);
2039 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
2040 }
2041 else
2042 {
2043 TIntermSymbol *paramSymbol = intermediate.addSymbol(0, "", *param.type, location);
2044 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
2045 }
2046 }
2047
2048 prototype->setOp(EOpPrototype);
2049
2050 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002051
2052 if (!symbolTable.atGlobalLevel())
2053 {
2054 // ESSL 3.00.4 section 4.2.4.
2055 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002056 }
2057
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002058 return prototype;
2059}
2060
2061TIntermAggregate *TParseContext::addFunctionDefinition(const TFunction &function,
2062 TIntermAggregate *functionPrototype,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01002063 TIntermBlock *functionBody,
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002064 const TSourceLoc &location)
2065{
Olli Etuahof51fdd22016-10-03 10:03:40 +01002066 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002067 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
2068 {
2069 error(location, "function does not return a value:", "", function.getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002070 }
2071
Olli Etuahof51fdd22016-10-03 10:03:40 +01002072 TIntermAggregate *functionNode = new TIntermAggregate(EOpFunction);
2073 functionNode->setLine(location);
2074
2075 ASSERT(functionPrototype != nullptr);
2076 functionNode->getSequence()->push_back(functionPrototype);
2077
2078 if (functionBody == nullptr)
2079 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01002080 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002081 functionBody->setLine(location);
2082 }
2083 functionNode->getSequence()->push_back(functionBody);
2084
2085 functionNode->setName(function.getMangledName().c_str());
2086 functionNode->setType(function.getReturnType());
2087 functionNode->setFunctionId(function.getUniqueId());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002088
2089 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002090 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002091}
2092
Jamie Madill185fb402015-06-12 15:48:48 -04002093void TParseContext::parseFunctionPrototype(const TSourceLoc &location,
2094 TFunction *function,
2095 TIntermAggregate **aggregateOut)
2096{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002097 const TSymbol *builtIn =
2098 symbolTable.findBuiltIn(function->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002099
2100 if (builtIn)
2101 {
2102 error(location, "built-in functions cannot be redefined", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002103 }
2104
Jamie Madillb98c3a82015-07-23 14:26:04 -04002105 TFunction *prevDec =
2106 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Jamie Madill185fb402015-06-12 15:48:48 -04002107 //
2108 // Note: 'prevDec' could be 'function' if this is the first time we've seen function
2109 // as it would have just been put in the symbol table. Otherwise, we're looking up
2110 // an earlier occurance.
2111 //
2112 if (prevDec->isDefined())
2113 {
2114 // Then this function already has a body.
2115 error(location, "function already has a body", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002116 }
2117 prevDec->setDefined();
2118 //
2119 // Overload the unique ID of the definition to be the same unique ID as the declaration.
2120 // Eventually we will probably want to have only a single definition and just swap the
2121 // arguments to be the definition's arguments.
2122 //
2123 function->setUniqueId(prevDec->getUniqueId());
2124
2125 // Raise error message if main function takes any parameters or return anything other than void
2126 if (function->getName() == "main")
2127 {
2128 if (function->getParamCount() > 0)
2129 {
2130 error(location, "function cannot take any parameter(s)", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002131 }
2132 if (function->getReturnType().getBasicType() != EbtVoid)
2133 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002134 error(location, "", function->getReturnType().getBasicString(),
2135 "main function cannot return a value");
Jamie Madill185fb402015-06-12 15:48:48 -04002136 }
2137 }
2138
2139 //
2140 // Remember the return type for later checking for RETURN statements.
2141 //
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002142 mCurrentFunctionType = &(prevDec->getReturnType());
2143 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04002144
2145 //
2146 // Insert parameters into the symbol table.
2147 // If the parameter has no name, it's not an error, just don't insert it
2148 // (could be used for unused args).
2149 //
2150 // Also, accumulate the list of parameters into the HIL, so lower level code
2151 // knows where to find parameters.
2152 //
2153 TIntermAggregate *paramNodes = new TIntermAggregate;
2154 for (size_t i = 0; i < function->getParamCount(); i++)
2155 {
2156 const TConstParameter &param = function->getParam(i);
2157 if (param.name != 0)
2158 {
2159 TVariable *variable = new TVariable(param.name, *param.type);
2160 //
2161 // Insert the parameters with name in the symbol table.
2162 //
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002163 if (!symbolTable.declare(variable))
2164 {
Jamie Madill185fb402015-06-12 15:48:48 -04002165 error(location, "redefinition", variable->getName().c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002166 paramNodes = intermediate.growAggregate(
2167 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
2168 continue;
Jamie Madill185fb402015-06-12 15:48:48 -04002169 }
2170
2171 //
2172 // Add the parameter to the HIL
2173 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002174 TIntermSymbol *symbol = intermediate.addSymbol(
2175 variable->getUniqueId(), variable->getName(), variable->getType(), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002176
2177 paramNodes = intermediate.growAggregate(paramNodes, symbol, location);
2178 }
2179 else
2180 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002181 paramNodes = intermediate.growAggregate(
2182 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002183 }
2184 }
2185 intermediate.setAggregateOperator(paramNodes, EOpParameters, location);
2186 *aggregateOut = paramNodes;
2187 setLoopNestingLevel(0);
2188}
2189
Jamie Madillb98c3a82015-07-23 14:26:04 -04002190TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002191{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002192 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002193 // We don't know at this point whether this is a function definition or a prototype.
2194 // The definition production code will check for redefinitions.
2195 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002196 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002197 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
2198 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002199 //
2200 TFunction *prevDec =
2201 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302202
2203 if (getShaderVersion() >= 300 && symbolTable.hasUnmangledBuiltIn(function->getName().c_str()))
2204 {
2205 // With ESSL 3.00, names of built-in functions cannot be redeclared as functions.
2206 // Therefore overloading or redefining builtin functions is an error.
2207 error(location, "Name of a built-in function cannot be redeclared as function",
2208 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302209 }
2210 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002211 {
2212 if (prevDec->getReturnType() != function->getReturnType())
2213 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002214 error(location, "overloaded functions must have the same return type",
Jamie Madill185fb402015-06-12 15:48:48 -04002215 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04002216 }
2217 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2218 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002219 if (prevDec->getParam(i).type->getQualifier() !=
2220 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002221 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002222 error(location, "overloaded functions must have the same parameter qualifiers",
Jamie Madill185fb402015-06-12 15:48:48 -04002223 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04002224 }
2225 }
2226 }
2227
2228 //
2229 // Check for previously declared variables using the same name.
2230 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002231 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002232 if (prevSym)
2233 {
2234 if (!prevSym->isFunction())
2235 {
2236 error(location, "redefinition", function->getName().c_str(), "function");
Jamie Madill185fb402015-06-12 15:48:48 -04002237 }
2238 }
2239 else
2240 {
2241 // Insert the unmangled name to detect potential future redefinition as a variable.
Jamie Madillb98c3a82015-07-23 14:26:04 -04002242 TFunction *newFunction =
2243 new TFunction(NewPoolTString(function->getName().c_str()), &function->getReturnType());
Jamie Madill185fb402015-06-12 15:48:48 -04002244 symbolTable.getOuterLevel()->insertUnmangled(newFunction);
2245 }
2246
2247 // We're at the inner scope level of the function's arguments and body statement.
2248 // Add the function prototype to the surrounding scope instead.
2249 symbolTable.getOuterLevel()->insert(function);
2250
2251 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002252 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2253 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002254 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2255 //
2256 return function;
2257}
2258
Olli Etuaho9de84a52016-06-14 17:36:01 +03002259TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
2260 const TString *name,
2261 const TSourceLoc &location)
2262{
2263 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
2264 {
2265 error(location, "no qualifiers allowed for function return",
2266 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03002267 }
2268 if (!type.layoutQualifier.isEmpty())
2269 {
2270 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03002271 }
2272 // make sure a sampler is not involved as well...
Martin Radev4a9cd802016-09-01 16:51:51 +03002273 checkIsNotSampler(location, type.typeSpecifierNonArray,
2274 "samplers can't be function return values");
Olli Etuahoe29324f2016-06-15 10:58:03 +03002275 if (mShaderVersion < 300)
2276 {
2277 // Array return values are forbidden, but there's also no valid syntax for declaring array
2278 // return values in ESSL 1.00.
2279 ASSERT(type.arraySize == 0 || mDiagnostics.numErrors() > 0);
2280
2281 if (type.isStructureContainingArrays())
2282 {
2283 // ESSL 1.00.17 section 6.1 Function Definitions
2284 error(location, "structures containing arrays can't be function return values",
2285 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03002286 }
2287 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03002288
2289 // Add the function as a prototype after parsing it (we do not support recursion)
2290 return new TFunction(name, new TType(type));
2291}
2292
Jamie Madill06145232015-05-13 13:10:01 -04002293TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002294{
Jamie Madill06145232015-05-13 13:10:01 -04002295 TPublicType publicType = publicTypeIn;
Martin Radev4a9cd802016-09-01 16:51:51 +03002296 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02002297 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002298 error(publicType.getLine(), "constructor can't be a structure definition",
2299 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02002300 }
2301
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002302 TOperator op = EOpNull;
Martin Radev4a9cd802016-09-01 16:51:51 +03002303 if (publicType.getUserDef())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002304 {
2305 op = EOpConstructStruct;
2306 }
2307 else
2308 {
Geoff Lang156d7192016-07-21 16:11:00 -04002309 op = sh::TypeToConstructorOperator(TType(publicType));
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002310 if (op == EOpNull)
2311 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002312 error(publicType.getLine(), "cannot construct this type",
2313 getBasicString(publicType.getBasicType()));
2314 publicType.setBasicType(EbtFloat);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002315 op = EOpConstructFloat;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002316 }
2317 }
2318
2319 TString tempString;
Dmitry Skiba7f17a502015-06-22 15:08:39 -07002320 const TType *type = new TType(publicType);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002321 return new TFunction(&tempString, type, op);
2322}
2323
Jamie Madillb98c3a82015-07-23 14:26:04 -04002324// This function is used to test for the correctness of the parameters passed to various constructor
2325// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002326//
Olli Etuaho856c4972016-08-08 11:38:39 +03002327// 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 +00002328//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002329TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002330 TOperator op,
2331 TFunction *fnCall,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302332 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002333{
Olli Etuaho856c4972016-08-08 11:38:39 +03002334 TType type = fnCall->getReturnType();
2335 if (type.isUnsizedArray())
2336 {
2337 type.setArraySize(static_cast<unsigned int>(fnCall->getParamCount()));
2338 }
2339 bool constType = true;
2340 for (size_t i = 0; i < fnCall->getParamCount(); ++i)
2341 {
2342 const TConstParameter &param = fnCall->getParam(i);
2343 if (param.type->getQualifier() != EvqConst)
2344 constType = false;
2345 }
2346 if (constType)
2347 type.setQualifier(EvqConst);
2348
Olli Etuaho8a176262016-08-16 14:23:01 +03002349 if (!checkConstructorArguments(line, arguments, *fnCall, op, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03002350 {
2351 TIntermTyped *dummyNode = intermediate.setAggregateOperator(nullptr, op, line);
2352 dummyNode->setType(type);
2353 return dummyNode;
2354 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002355 TIntermAggregate *constructor = arguments->getAsAggregate();
2356 ASSERT(constructor != nullptr);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002357
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002358 // Turn the argument list itself into a constructor
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002359 constructor->setOp(op);
2360 constructor->setLine(line);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002361 ASSERT(constructor->isConstructor());
2362
2363 // Need to set type before setPrecisionFromChildren() because bool doesn't have precision.
Olli Etuaho856c4972016-08-08 11:38:39 +03002364 constructor->setType(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002365
Olli Etuaho21203702014-11-13 16:16:21 +02002366 // Structs should not be precision qualified, the individual members may be.
2367 // Built-in types on the other hand should be precision qualified.
2368 if (op != EOpConstructStruct)
2369 {
2370 constructor->setPrecisionFromChildren();
Olli Etuaho856c4972016-08-08 11:38:39 +03002371 type.setPrecision(constructor->getPrecision());
Olli Etuaho21203702014-11-13 16:16:21 +02002372 }
2373
Olli Etuaho856c4972016-08-08 11:38:39 +03002374 constructor->setType(type);
2375
Olli Etuahof119a262016-08-19 15:54:22 +03002376 TIntermTyped *constConstructor = intermediate.foldAggregateBuiltIn(constructor, &mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002377 if (constConstructor)
2378 {
2379 return constConstructor;
2380 }
2381
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002382 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002383}
2384
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002385//
2386// Interface/uniform blocks
2387//
Martin Radev70866b82016-07-22 15:27:42 +03002388TIntermAggregate *TParseContext::addInterfaceBlock(
2389 const TTypeQualifierBuilder &typeQualifierBuilder,
2390 const TSourceLoc &nameLine,
2391 const TString &blockName,
2392 TFieldList *fieldList,
2393 const TString *instanceName,
2394 const TSourceLoc &instanceLine,
2395 TIntermTyped *arrayIndex,
2396 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002397{
Olli Etuaho856c4972016-08-08 11:38:39 +03002398 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002399
Olli Etuaho613b9592016-09-05 12:05:53 +03002400 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03002401
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002402 if (typeQualifier.qualifier != EvqUniform)
2403 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302404 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
2405 "interface blocks must be uniform");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002406 }
2407
Martin Radev70866b82016-07-22 15:27:42 +03002408 if (typeQualifier.invariant)
2409 {
2410 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
2411 }
2412
Jamie Madill099c0f32013-06-20 11:55:52 -04002413 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03002414 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002415
Jamie Madill099c0f32013-06-20 11:55:52 -04002416 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2417 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002418 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002419 }
2420
Jamie Madill1566ef72013-06-20 11:55:54 -04002421 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2422 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002423 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002424 }
2425
Olli Etuaho856c4972016-08-08 11:38:39 +03002426 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002427
Arun Patole7e7e68d2015-05-22 12:02:25 +05302428 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2429 if (!symbolTable.declare(blockNameSymbol))
2430 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002431 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002432 }
2433
Jamie Madill98493dd2013-07-08 14:39:03 -04002434 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302435 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2436 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002437 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302438 TType *fieldType = field->type();
2439 if (IsSampler(fieldType->getBasicType()))
2440 {
2441 error(field->line(), "unsupported type", fieldType->getBasicString(),
2442 "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002443 }
2444
Jamie Madill98493dd2013-07-08 14:39:03 -04002445 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002446 switch (qualifier)
2447 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002448 case EvqGlobal:
2449 case EvqUniform:
2450 break;
2451 default:
2452 error(field->line(), "invalid qualifier on interface block member",
2453 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04002454 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002455 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002456
Martin Radev70866b82016-07-22 15:27:42 +03002457 if (fieldType->isInvariant())
2458 {
2459 error(field->line(), "invalid qualifier on interface block member", "invariant");
2460 }
2461
Jamie Madilla5efff92013-06-06 11:56:47 -04002462 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002463 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03002464 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
Jamie Madill099c0f32013-06-20 11:55:52 -04002465
Jamie Madill98493dd2013-07-08 14:39:03 -04002466 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002467 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002468 error(field->line(), "invalid layout qualifier:",
2469 getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04002470 }
2471
Jamie Madill98493dd2013-07-08 14:39:03 -04002472 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002473 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002474 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002475 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002476 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002477 {
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002478 warning(field->line(), "extraneous layout qualifier:",
Jamie Madillb98c3a82015-07-23 14:26:04 -04002479 getMatrixPackingString(fieldLayoutQualifier.matrixPacking),
2480 "only has an effect on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04002481 }
2482
Jamie Madill98493dd2013-07-08 14:39:03 -04002483 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002484 }
2485
Jamie Madill98493dd2013-07-08 14:39:03 -04002486 // add array index
Olli Etuaho856c4972016-08-08 11:38:39 +03002487 unsigned int arraySize = 0;
2488 if (arrayIndex != nullptr)
Jamie Madill98493dd2013-07-08 14:39:03 -04002489 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002490 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04002491 }
2492
Jamie Madillb98c3a82015-07-23 14:26:04 -04002493 TInterfaceBlock *interfaceBlock =
2494 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2495 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
2496 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002497
2498 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04002499 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002500
Jamie Madill98493dd2013-07-08 14:39:03 -04002501 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002502 {
2503 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002504 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2505 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002506 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302507 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002508
2509 // set parent pointer of the field variable
2510 fieldType->setInterfaceBlock(interfaceBlock);
2511
Arun Patole7e7e68d2015-05-22 12:02:25 +05302512 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002513 fieldVariable->setQualifier(typeQualifier.qualifier);
2514
Arun Patole7e7e68d2015-05-22 12:02:25 +05302515 if (!symbolTable.declare(fieldVariable))
2516 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002517 error(field->line(), "redefinition", field->name().c_str(),
2518 "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002519 }
2520 }
2521 }
2522 else
2523 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002524 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03002525
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002526 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302527 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002528 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002529
Arun Patole7e7e68d2015-05-22 12:02:25 +05302530 if (!symbolTable.declare(instanceTypeDef))
2531 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002532 error(instanceLine, "redefinition", instanceName->c_str(),
2533 "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002534 }
2535
Jamie Madillb98c3a82015-07-23 14:26:04 -04002536 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002537 symbolName = instanceTypeDef->getName();
2538 }
2539
Olli Etuaho32db19b2016-10-04 14:43:16 +01002540 TIntermAggregate *aggregate = TIntermediate::MakeAggregate(
Jamie Madillb98c3a82015-07-23 14:26:04 -04002541 intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line),
2542 nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002543 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04002544
2545 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002546 return aggregate;
2547}
2548
Olli Etuaho383b7912016-08-05 11:22:59 +03002549void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002550{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002551 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002552
2553 // Embedded structure definitions are not supported per GLSL ES spec.
2554 // They aren't allowed in GLSL either, but we need to detect this here
2555 // so we don't rely on the GLSL compiler to catch it.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302556 if (mStructNestingLevel > 1)
2557 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002558 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002559 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002560}
2561
2562void TParseContext::exitStructDeclaration()
2563{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002564 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002565}
2566
Jamie Madillb98c3a82015-07-23 14:26:04 -04002567namespace
2568{
kbr@chromium.org476541f2011-10-27 21:14:51 +00002569const int kWebGLMaxStructNesting = 4;
2570
2571} // namespace
2572
Olli Etuaho8a176262016-08-16 14:23:01 +03002573void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002574{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302575 if (!IsWebGLBasedSpec(mShaderSpec))
2576 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002577 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002578 }
2579
Arun Patole7e7e68d2015-05-22 12:02:25 +05302580 if (field.type()->getBasicType() != EbtStruct)
2581 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002582 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002583 }
2584
2585 // We're already inside a structure definition at this point, so add
2586 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302587 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2588 {
Jamie Madill41a49272014-03-18 16:10:13 -04002589 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002590 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
2591 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04002592 std::string reason = reasonStream.str();
2593 error(line, reason.c_str(), field.name().c_str(), "");
Olli Etuaho8a176262016-08-16 14:23:01 +03002594 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002595 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002596}
2597
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002598//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002599// Parse an array index expression
2600//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002601TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
2602 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302603 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002604{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002605 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2606 {
2607 if (baseExpression->getAsSymbolNode())
2608 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302609 error(location, " left of '[' is not of type array, matrix, or vector ",
2610 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002611 }
2612 else
2613 {
2614 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2615 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002616
2617 TConstantUnion *unionArray = new TConstantUnion[1];
2618 unionArray->setFConst(0.0f);
2619 return intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst),
2620 location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002621 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002622
Jamie Madill21c1e452014-12-29 11:33:41 -05002623 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2624
Olli Etuaho36b05142015-11-12 13:10:42 +02002625 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
2626 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
2627 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
2628 // index is a constant expression.
2629 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
2630 {
2631 if (baseExpression->isInterfaceBlock())
2632 {
2633 error(
2634 location, "", "[",
2635 "array indexes for interface blocks arrays must be constant integral expressions");
Olli Etuaho36b05142015-11-12 13:10:42 +02002636 }
2637 else if (baseExpression->getQualifier() == EvqFragmentOut)
2638 {
2639 error(location, "", "[",
2640 "array indexes for fragment outputs must be constant integral expressions");
Olli Etuaho36b05142015-11-12 13:10:42 +02002641 }
Olli Etuaho3e960462015-11-12 15:58:39 +02002642 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
2643 {
2644 error(location, "", "[", "array index for gl_FragData must be constant zero");
Olli Etuaho3e960462015-11-12 15:58:39 +02002645 }
Olli Etuaho36b05142015-11-12 13:10:42 +02002646 }
2647
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002648 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002649 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002650 // If an out-of-range index is not qualified as constant, the behavior in the spec is
2651 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
2652 // constant fold expressions that are not constant expressions). The most compatible way to
2653 // handle this case is to report a warning instead of an error and force the index to be in
2654 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002655 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Jamie Madill21c1e452014-12-29 11:33:41 -05002656 int index = indexConstantUnion->getIConst(0);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002657
2658 int safeIndex = -1;
2659
2660 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04002661 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002662 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03002663 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002664 if (mShaderSpec == SH_WEBGL2_SPEC)
2665 {
2666 // Error has been already generated if index is not const.
2667 if (indexExpression->getQualifier() == EvqConst)
2668 {
2669 error(location, "", "[",
2670 "array index for gl_FragData must be constant zero");
2671 }
2672 safeIndex = 0;
2673 }
2674 else if (!isExtensionEnabled("GL_EXT_draw_buffers"))
2675 {
2676 outOfRangeError(outOfRangeIndexIsError, location, "", "[",
2677 "array index for gl_FragData must be zero when "
2678 "GL_EXT_draw_buffers is disabled");
2679 safeIndex = 0;
2680 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03002681 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002682 // Only do generic out-of-range check if similar error hasn't already been reported.
2683 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03002684 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002685 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
2686 baseExpression->getArraySize(),
2687 "array index out of range", "[]");
2688 }
2689 }
2690 else if (baseExpression->isMatrix())
2691 {
2692 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03002693 baseExpression->getType().getCols(),
2694 "matrix field selection out of range", "[]");
Jamie Madill7164cf42013-07-08 13:30:59 -04002695 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002696 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04002697 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002698 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
2699 baseExpression->getType().getNominalSize(),
2700 "vector field selection out of range", "[]");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002701 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002702
2703 ASSERT(safeIndex >= 0);
2704 // Data of constant unions can't be changed, because it may be shared with other
2705 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
2706 // sanitized object.
2707 if (safeIndex != index)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002708 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002709 TConstantUnion *safeConstantUnion = new TConstantUnion();
2710 safeConstantUnion->setIConst(safeIndex);
2711 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002712 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002713
2714 return intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location,
2715 &mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002716 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002717 else
2718 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002719 return intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location,
2720 &mDiagnostics);
Jamie Madill7164cf42013-07-08 13:30:59 -04002721 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002722}
2723
Olli Etuaho90892fb2016-07-14 14:44:51 +03002724int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
2725 const TSourceLoc &location,
2726 int index,
2727 int arraySize,
2728 const char *reason,
2729 const char *token)
2730{
2731 if (index >= arraySize || index < 0)
2732 {
2733 std::stringstream extraInfoStream;
2734 extraInfoStream << "'" << index << "'";
2735 std::string extraInfo = extraInfoStream.str();
2736 outOfRangeError(outOfRangeIndexIsError, location, reason, token, extraInfo.c_str());
2737 if (index < 0)
2738 {
2739 return 0;
2740 }
2741 else
2742 {
2743 return arraySize - 1;
2744 }
2745 }
2746 return index;
2747}
2748
Jamie Madillb98c3a82015-07-23 14:26:04 -04002749TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
2750 const TSourceLoc &dotLocation,
2751 const TString &fieldString,
2752 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002753{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002754 if (baseExpression->isArray())
2755 {
2756 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002757 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002758 }
2759
2760 if (baseExpression->isVector())
2761 {
2762 TVectorFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002763 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields,
2764 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002765 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002766 fields.num = 1;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002767 fields.offsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002768 }
2769
Olli Etuahob6fa0432016-09-28 16:28:05 +01002770 return TIntermediate::AddSwizzle(baseExpression, fields, dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002771 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002772 else if (baseExpression->getBasicType() == EbtStruct)
2773 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302774 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04002775 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002776 {
2777 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002778 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002779 }
2780 else
2781 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002782 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002783 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002784 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002785 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002786 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002787 {
2788 fieldFound = true;
2789 break;
2790 }
2791 }
2792 if (fieldFound)
2793 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002794 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
2795 index->setLine(fieldLocation);
2796 return intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index,
2797 dotLocation, &mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002798 }
2799 else
2800 {
2801 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002802 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002803 }
2804 }
2805 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002806 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002807 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302808 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04002809 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002810 {
2811 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002812 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002813 }
2814 else
2815 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002816 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002817 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002818 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002819 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002820 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002821 {
2822 fieldFound = true;
2823 break;
2824 }
2825 }
2826 if (fieldFound)
2827 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002828 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
2829 index->setLine(fieldLocation);
2830 return intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index,
2831 dotLocation, &mDiagnostics);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002832 }
2833 else
2834 {
2835 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002836 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002837 }
2838 }
2839 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002840 else
2841 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002842 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002843 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03002844 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05302845 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002846 }
2847 else
2848 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302849 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03002850 " field selection requires structure, vector, or interface block on left hand "
2851 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05302852 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002853 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002854 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002855 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002856}
2857
Jamie Madillb98c3a82015-07-23 14:26:04 -04002858TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
2859 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002860{
Martin Radev802abe02016-08-04 17:48:32 +03002861 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002862
2863 if (qualifierType == "shared")
2864 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002865 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002866 }
2867 else if (qualifierType == "packed")
2868 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002869 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002870 }
2871 else if (qualifierType == "std140")
2872 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002873 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002874 }
2875 else if (qualifierType == "row_major")
2876 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002877 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002878 }
2879 else if (qualifierType == "column_major")
2880 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002881 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002882 }
2883 else if (qualifierType == "location")
2884 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002885 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
2886 "location requires an argument");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002887 }
2888 else
2889 {
2890 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002891 }
2892
Jamie Madilla5efff92013-06-06 11:56:47 -04002893 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002894}
2895
Martin Radev802abe02016-08-04 17:48:32 +03002896void TParseContext::parseLocalSize(const TString &qualifierType,
2897 const TSourceLoc &qualifierTypeLine,
2898 int intValue,
2899 const TSourceLoc &intValueLine,
2900 const std::string &intValueString,
2901 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03002902 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03002903{
Olli Etuaho856c4972016-08-08 11:38:39 +03002904 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03002905 if (intValue < 1)
2906 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03002907 std::string errorMessage = std::string(getWorkGroupSizeString(index)) + " must be positive";
Martin Radev802abe02016-08-04 17:48:32 +03002908 error(intValueLine, "out of range:", intValueString.c_str(), errorMessage.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03002909 }
2910 (*localSize)[index] = intValue;
2911}
2912
Jamie Madillb98c3a82015-07-23 14:26:04 -04002913TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
2914 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002915 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302916 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002917{
Martin Radev802abe02016-08-04 17:48:32 +03002918 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002919
Martin Radev802abe02016-08-04 17:48:32 +03002920 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002921
Martin Radev802abe02016-08-04 17:48:32 +03002922 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002923 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002924 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002925 if (intValue < 0)
2926 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002927 error(intValueLine, "out of range:", intValueString.c_str(),
2928 "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002929 }
2930 else
2931 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002932 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03002933 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002934 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002935 }
Martin Radev802abe02016-08-04 17:48:32 +03002936 else if (qualifierType == "local_size_x")
2937 {
2938 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
2939 &qualifier.localSize);
2940 }
2941 else if (qualifierType == "local_size_y")
2942 {
2943 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
2944 &qualifier.localSize);
2945 }
2946 else if (qualifierType == "local_size_z")
2947 {
2948 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
2949 &qualifier.localSize);
2950 }
2951 else
2952 {
2953 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03002954 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002955
Jamie Madilla5efff92013-06-06 11:56:47 -04002956 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002957}
2958
Olli Etuaho613b9592016-09-05 12:05:53 +03002959TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
2960{
2961 return new TTypeQualifierBuilder(
2962 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
2963 mShaderVersion);
2964}
2965
Jamie Madillb98c3a82015-07-23 14:26:04 -04002966TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03002967 TLayoutQualifier rightQualifier,
2968 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002969{
Martin Radevc28888b2016-07-22 15:27:42 +03002970 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
2971 &mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002972}
2973
Martin Radev70866b82016-07-22 15:27:42 +03002974TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
2975 const TTypeQualifierBuilder &typeQualifierBuilder,
2976 TPublicType *typeSpecifier,
2977 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002978{
Olli Etuaho613b9592016-09-05 12:05:53 +03002979 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002980
Martin Radev70866b82016-07-22 15:27:42 +03002981 typeSpecifier->qualifier = typeQualifier.qualifier;
2982 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
2983 typeSpecifier->invariant = typeQualifier.invariant;
2984 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05302985 {
Martin Radev70866b82016-07-22 15:27:42 +03002986 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002987 }
Martin Radev70866b82016-07-22 15:27:42 +03002988 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002989}
2990
Jamie Madillb98c3a82015-07-23 14:26:04 -04002991TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
2992 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002993{
Martin Radev4a9cd802016-09-01 16:51:51 +03002994 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
2995 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002996
Martin Radev4a9cd802016-09-01 16:51:51 +03002997 checkIsNonVoid(typeSpecifier.getLine(), (*fieldList)[0]->name(), typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002998
Martin Radev4a9cd802016-09-01 16:51:51 +03002999 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003000
Arun Patole7e7e68d2015-05-22 12:02:25 +05303001 for (unsigned int i = 0; i < fieldList->size(); ++i)
3002 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003003 //
3004 // Careful not to replace already known aspects of type, like array-ness
3005 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303006 TType *type = (*fieldList)[i]->type();
Martin Radev4a9cd802016-09-01 16:51:51 +03003007 type->setBasicType(typeSpecifier.getBasicType());
3008 type->setPrimarySize(typeSpecifier.getPrimarySize());
3009 type->setSecondarySize(typeSpecifier.getSecondarySize());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003010 type->setPrecision(typeSpecifier.precision);
3011 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003012 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
Martin Radev70866b82016-07-22 15:27:42 +03003013 type->setInvariant(typeSpecifier.invariant);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003014
3015 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303016 if (type->isArray())
3017 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003018 checkIsValidTypeForArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003019 }
3020 if (typeSpecifier.array)
Olli Etuaho856c4972016-08-08 11:38:39 +03003021 type->setArraySize(static_cast<unsigned int>(typeSpecifier.arraySize));
Martin Radev4a9cd802016-09-01 16:51:51 +03003022 if (typeSpecifier.getUserDef())
Arun Patole7e7e68d2015-05-22 12:02:25 +05303023 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003024 type->setStruct(typeSpecifier.getUserDef()->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003025 }
3026
Martin Radev4a9cd802016-09-01 16:51:51 +03003027 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*fieldList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003028 }
3029
Jamie Madill98493dd2013-07-08 14:39:03 -04003030 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003031}
3032
Martin Radev4a9cd802016-09-01 16:51:51 +03003033TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
3034 const TSourceLoc &nameLine,
3035 const TString *structName,
3036 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003037{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303038 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003039 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003040
Jamie Madill9b820842015-02-12 10:40:10 -05003041 // Store a bool in the struct if we're at global scope, to allow us to
3042 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05003043 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003044
Jamie Madill98493dd2013-07-08 14:39:03 -04003045 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003046 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003047 checkIsNotReserved(nameLine, *structName);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303048 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
3049 if (!symbolTable.declare(userTypeDef))
3050 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003051 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003052 }
3053 }
3054
3055 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04003056 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003057 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003058 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04003059 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003060 switch (qualifier)
3061 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003062 case EvqGlobal:
3063 case EvqTemporary:
3064 break;
3065 default:
3066 error(field.line(), "invalid qualifier on struct member",
3067 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003068 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003069 }
Martin Radev70866b82016-07-22 15:27:42 +03003070 if (field.type()->isInvariant())
3071 {
3072 error(field.line(), "invalid qualifier on struct member", "invariant");
3073 }
3074
3075 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003076 }
3077
Martin Radev4a9cd802016-09-01 16:51:51 +03003078 TTypeSpecifierNonArray typeSpecifierNonArray;
3079 typeSpecifierNonArray.initialize(EbtStruct, structLine);
3080 typeSpecifierNonArray.userDef = structureType;
3081 typeSpecifierNonArray.isStructSpecifier = true;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003082 exitStructDeclaration();
3083
Martin Radev4a9cd802016-09-01 16:51:51 +03003084 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003085}
3086
Jamie Madillb98c3a82015-07-23 14:26:04 -04003087TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003088 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003089 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02003090{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003091 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003092 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02003093 init->isVector())
3094 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003095 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
3096 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003097 return nullptr;
3098 }
3099
Olli Etuahoac5274d2015-02-20 10:19:08 +02003100 if (statementList)
3101 {
3102 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
3103 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02003104 return nullptr;
3105 }
3106 }
3107
Olli Etuahoa3a36662015-02-17 13:46:51 +02003108 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3109 if (node == nullptr)
3110 {
3111 error(loc, "erroneous switch statement", "switch");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003112 return nullptr;
3113 }
3114 return node;
3115}
3116
3117TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3118{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003119 if (mSwitchNestingLevel == 0)
3120 {
3121 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003122 return nullptr;
3123 }
3124 if (condition == nullptr)
3125 {
3126 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003127 return nullptr;
3128 }
3129 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04003130 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02003131 {
3132 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003133 }
3134 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003135 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
3136 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
3137 // fold in case labels.
3138 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02003139 {
3140 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003141 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003142 TIntermCase *node = intermediate.addCase(condition, loc);
3143 if (node == nullptr)
3144 {
3145 error(loc, "erroneous case statement", "case");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003146 return nullptr;
3147 }
3148 return node;
3149}
3150
3151TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3152{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003153 if (mSwitchNestingLevel == 0)
3154 {
3155 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003156 return nullptr;
3157 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003158 TIntermCase *node = intermediate.addCase(nullptr, loc);
3159 if (node == nullptr)
3160 {
3161 error(loc, "erroneous default statement", "default");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003162 return nullptr;
3163 }
3164 return node;
3165}
3166
Jamie Madillb98c3a82015-07-23 14:26:04 -04003167TIntermTyped *TParseContext::createUnaryMath(TOperator op,
3168 TIntermTyped *child,
3169 const TSourceLoc &loc,
3170 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003171{
3172 if (child == nullptr)
3173 {
3174 return nullptr;
3175 }
3176
3177 switch (op)
3178 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003179 case EOpLogicalNot:
3180 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
3181 child->isVector())
3182 {
3183 return nullptr;
3184 }
3185 break;
3186 case EOpBitwiseNot:
3187 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3188 child->isMatrix() || child->isArray())
3189 {
3190 return nullptr;
3191 }
3192 break;
3193 case EOpPostIncrement:
3194 case EOpPreIncrement:
3195 case EOpPostDecrement:
3196 case EOpPreDecrement:
3197 case EOpNegative:
3198 case EOpPositive:
3199 if (child->getBasicType() == EbtStruct || child->getBasicType() == EbtBool ||
Olli Etuaho558b0382016-08-26 17:54:34 +03003200 child->isArray() || IsSampler(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04003201 {
3202 return nullptr;
3203 }
3204 // Operators for built-ins are already type checked against their prototype.
3205 default:
3206 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003207 }
3208
Olli Etuahof119a262016-08-19 15:54:22 +03003209 TIntermUnary *node = new TIntermUnary(op, child);
3210 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03003211
3212 TIntermTyped *foldedNode = node->fold(&mDiagnostics);
3213 if (foldedNode)
3214 return foldedNode;
3215
3216 return node;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003217}
3218
Olli Etuaho09b22472015-02-11 11:47:26 +02003219TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3220{
Olli Etuahof6c694b2015-03-26 14:50:53 +02003221 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003222 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003223 {
3224 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003225 return child;
3226 }
3227 return node;
3228}
3229
Jamie Madillb98c3a82015-07-23 14:26:04 -04003230TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
3231 TIntermTyped *child,
3232 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003233{
Olli Etuaho856c4972016-08-08 11:38:39 +03003234 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02003235 return addUnaryMath(op, child, loc);
3236}
3237
Jamie Madillb98c3a82015-07-23 14:26:04 -04003238bool TParseContext::binaryOpCommonCheck(TOperator op,
3239 TIntermTyped *left,
3240 TIntermTyped *right,
3241 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003242{
Olli Etuaho244be012016-08-18 15:26:02 +03003243 if (left->getType().getStruct() || right->getType().getStruct())
3244 {
3245 switch (op)
3246 {
3247 case EOpIndexDirectStruct:
3248 ASSERT(left->getType().getStruct());
3249 break;
3250 case EOpEqual:
3251 case EOpNotEqual:
3252 case EOpAssign:
3253 case EOpInitialize:
3254 if (left->getType() != right->getType())
3255 {
3256 return false;
3257 }
3258 break;
3259 default:
3260 error(loc, "Invalid operation for structs", GetOperatorString(op));
3261 return false;
3262 }
3263 }
3264
Olli Etuahod6b14282015-03-17 14:31:35 +02003265 if (left->isArray() || right->isArray())
3266 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003267 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003268 {
3269 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3270 return false;
3271 }
3272
3273 if (left->isArray() != right->isArray())
3274 {
3275 error(loc, "array / non-array mismatch", GetOperatorString(op));
3276 return false;
3277 }
3278
3279 switch (op)
3280 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003281 case EOpEqual:
3282 case EOpNotEqual:
3283 case EOpAssign:
3284 case EOpInitialize:
3285 break;
3286 default:
3287 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3288 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02003289 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003290 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003291 if (left->getArraySize() != right->getArraySize())
3292 {
3293 error(loc, "array size mismatch", GetOperatorString(op));
3294 return false;
3295 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003296 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003297
3298 // Check ops which require integer / ivec parameters
3299 bool isBitShift = false;
3300 switch (op)
3301 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003302 case EOpBitShiftLeft:
3303 case EOpBitShiftRight:
3304 case EOpBitShiftLeftAssign:
3305 case EOpBitShiftRightAssign:
3306 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3307 // check that the basic type is an integer type.
3308 isBitShift = true;
3309 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3310 {
3311 return false;
3312 }
3313 break;
3314 case EOpBitwiseAnd:
3315 case EOpBitwiseXor:
3316 case EOpBitwiseOr:
3317 case EOpBitwiseAndAssign:
3318 case EOpBitwiseXorAssign:
3319 case EOpBitwiseOrAssign:
3320 // It is enough to check the type of only one operand, since later it
3321 // is checked that the operand types match.
3322 if (!IsInteger(left->getBasicType()))
3323 {
3324 return false;
3325 }
3326 break;
3327 default:
3328 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003329 }
3330
3331 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3332 // So the basic type should usually match.
3333 if (!isBitShift && left->getBasicType() != right->getBasicType())
3334 {
3335 return false;
3336 }
3337
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003338 // Check that:
3339 // 1. Type sizes match exactly on ops that require that.
3340 // 2. Restrictions for structs that contain arrays or samplers are respected.
3341 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003342 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003343 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003344 case EOpAssign:
3345 case EOpInitialize:
3346 case EOpEqual:
3347 case EOpNotEqual:
3348 // ESSL 1.00 sections 5.7, 5.8, 5.9
3349 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
3350 {
3351 error(loc, "undefined operation for structs containing arrays",
3352 GetOperatorString(op));
3353 return false;
3354 }
3355 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3356 // we interpret the spec so that this extends to structs containing samplers,
3357 // similarly to ESSL 1.00 spec.
3358 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
3359 left->getType().isStructureContainingSamplers())
3360 {
3361 error(loc, "undefined operation for structs containing samplers",
3362 GetOperatorString(op));
3363 return false;
3364 }
3365 case EOpLessThan:
3366 case EOpGreaterThan:
3367 case EOpLessThanEqual:
3368 case EOpGreaterThanEqual:
3369 if ((left->getNominalSize() != right->getNominalSize()) ||
3370 (left->getSecondarySize() != right->getSecondarySize()))
3371 {
3372 return false;
3373 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003374 break;
3375 case EOpAdd:
3376 case EOpSub:
3377 case EOpDiv:
3378 case EOpIMod:
3379 case EOpBitShiftLeft:
3380 case EOpBitShiftRight:
3381 case EOpBitwiseAnd:
3382 case EOpBitwiseXor:
3383 case EOpBitwiseOr:
3384 case EOpAddAssign:
3385 case EOpSubAssign:
3386 case EOpDivAssign:
3387 case EOpIModAssign:
3388 case EOpBitShiftLeftAssign:
3389 case EOpBitShiftRightAssign:
3390 case EOpBitwiseAndAssign:
3391 case EOpBitwiseXorAssign:
3392 case EOpBitwiseOrAssign:
3393 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
3394 {
3395 return false;
3396 }
3397
3398 // Are the sizes compatible?
3399 if (left->getNominalSize() != right->getNominalSize() ||
3400 left->getSecondarySize() != right->getSecondarySize())
3401 {
3402 // If the nominal sizes of operands do not match:
3403 // One of them must be a scalar.
3404 if (!left->isScalar() && !right->isScalar())
3405 return false;
3406
3407 // In the case of compound assignment other than multiply-assign,
3408 // the right side needs to be a scalar. Otherwise a vector/matrix
3409 // would be assigned to a scalar. A scalar can't be shifted by a
3410 // vector either.
3411 if (!right->isScalar() &&
3412 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
3413 return false;
3414 }
3415 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003416 default:
3417 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003418 }
3419
Olli Etuahod6b14282015-03-17 14:31:35 +02003420 return true;
3421}
3422
Olli Etuaho1dded802016-08-18 18:13:13 +03003423bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
3424 const TType &left,
3425 const TType &right)
3426{
3427 switch (op)
3428 {
3429 case EOpMul:
3430 case EOpMulAssign:
3431 return left.getNominalSize() == right.getNominalSize() &&
3432 left.getSecondarySize() == right.getSecondarySize();
3433 case EOpVectorTimesScalar:
3434 return true;
3435 case EOpVectorTimesScalarAssign:
3436 ASSERT(!left.isMatrix() && !right.isMatrix());
3437 return left.isVector() && !right.isVector();
3438 case EOpVectorTimesMatrix:
3439 return left.getNominalSize() == right.getRows();
3440 case EOpVectorTimesMatrixAssign:
3441 ASSERT(!left.isMatrix() && right.isMatrix());
3442 return left.isVector() && left.getNominalSize() == right.getRows() &&
3443 left.getNominalSize() == right.getCols();
3444 case EOpMatrixTimesVector:
3445 return left.getCols() == right.getNominalSize();
3446 case EOpMatrixTimesScalar:
3447 return true;
3448 case EOpMatrixTimesScalarAssign:
3449 ASSERT(left.isMatrix() && !right.isMatrix());
3450 return !right.isVector();
3451 case EOpMatrixTimesMatrix:
3452 return left.getCols() == right.getRows();
3453 case EOpMatrixTimesMatrixAssign:
3454 ASSERT(left.isMatrix() && right.isMatrix());
3455 // We need to check two things:
3456 // 1. The matrix multiplication step is valid.
3457 // 2. The result will have the same number of columns as the lvalue.
3458 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
3459
3460 default:
3461 UNREACHABLE();
3462 return false;
3463 }
3464}
3465
Jamie Madillb98c3a82015-07-23 14:26:04 -04003466TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
3467 TIntermTyped *left,
3468 TIntermTyped *right,
3469 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02003470{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003471 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003472 return nullptr;
3473
Olli Etuahofc1806e2015-03-17 13:03:11 +02003474 switch (op)
3475 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003476 case EOpEqual:
3477 case EOpNotEqual:
3478 break;
3479 case EOpLessThan:
3480 case EOpGreaterThan:
3481 case EOpLessThanEqual:
3482 case EOpGreaterThanEqual:
Olli Etuaho244be012016-08-18 15:26:02 +03003483 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3484 !right->getType().getStruct());
3485 if (left->isMatrix() || left->isVector())
Jamie Madillb98c3a82015-07-23 14:26:04 -04003486 {
3487 return nullptr;
3488 }
3489 break;
3490 case EOpLogicalOr:
3491 case EOpLogicalXor:
3492 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03003493 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3494 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003495 if (left->getBasicType() != EbtBool || left->isMatrix() || left->isVector())
3496 {
3497 return nullptr;
3498 }
3499 break;
3500 case EOpAdd:
3501 case EOpSub:
3502 case EOpDiv:
3503 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03003504 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3505 !right->getType().getStruct());
3506 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04003507 {
3508 return nullptr;
3509 }
3510 break;
3511 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03003512 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3513 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003514 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03003515 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04003516 {
3517 return nullptr;
3518 }
3519 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003520 default:
3521 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003522 }
3523
Olli Etuaho1dded802016-08-18 18:13:13 +03003524 if (op == EOpMul)
3525 {
3526 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
3527 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
3528 {
3529 return nullptr;
3530 }
3531 }
3532
Olli Etuaho3fdec912016-08-18 15:08:06 +03003533 TIntermBinary *node = new TIntermBinary(op, left, right);
3534 node->setLine(loc);
3535
Olli Etuaho3fdec912016-08-18 15:08:06 +03003536 // See if we can fold constants.
3537 TIntermTyped *foldedNode = node->fold(&mDiagnostics);
3538 if (foldedNode)
3539 return foldedNode;
3540
3541 return node;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003542}
3543
Jamie Madillb98c3a82015-07-23 14:26:04 -04003544TIntermTyped *TParseContext::addBinaryMath(TOperator op,
3545 TIntermTyped *left,
3546 TIntermTyped *right,
3547 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003548{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003549 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003550 if (node == 0)
3551 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003552 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3553 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003554 return left;
3555 }
3556 return node;
3557}
3558
Jamie Madillb98c3a82015-07-23 14:26:04 -04003559TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
3560 TIntermTyped *left,
3561 TIntermTyped *right,
3562 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003563{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003564 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003565 if (node == 0)
3566 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003567 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3568 right->getCompleteString());
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003569 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02003570 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003571 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
3572 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003573 }
3574 return node;
3575}
3576
Jamie Madillb98c3a82015-07-23 14:26:04 -04003577TIntermTyped *TParseContext::createAssign(TOperator op,
3578 TIntermTyped *left,
3579 TIntermTyped *right,
3580 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003581{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003582 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003583 {
Olli Etuaho1dded802016-08-18 18:13:13 +03003584 if (op == EOpMulAssign)
3585 {
3586 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
3587 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
3588 {
3589 return nullptr;
3590 }
3591 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03003592 TIntermBinary *node = new TIntermBinary(op, left, right);
3593 node->setLine(loc);
3594
Olli Etuaho3fdec912016-08-18 15:08:06 +03003595 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02003596 }
3597 return nullptr;
3598}
3599
Jamie Madillb98c3a82015-07-23 14:26:04 -04003600TIntermTyped *TParseContext::addAssign(TOperator op,
3601 TIntermTyped *left,
3602 TIntermTyped *right,
3603 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003604{
3605 TIntermTyped *node = createAssign(op, left, right, loc);
3606 if (node == nullptr)
3607 {
3608 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02003609 return left;
3610 }
3611 return node;
3612}
3613
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003614TIntermTyped *TParseContext::addComma(TIntermTyped *left,
3615 TIntermTyped *right,
3616 const TSourceLoc &loc)
3617{
Corentin Wallez0d959252016-07-12 17:26:32 -04003618 // WebGL2 section 5.26, the following results in an error:
3619 // "Sequence operator applied to void, arrays, or structs containing arrays"
3620 if (mShaderSpec == SH_WEBGL2_SPEC && (left->isArray() || left->getBasicType() == EbtVoid ||
3621 left->getType().isStructureContainingArrays() ||
3622 right->isArray() || right->getBasicType() == EbtVoid ||
3623 right->getType().isStructureContainingArrays()))
3624 {
3625 error(loc,
3626 "sequence operator is not allowed for void, arrays, or structs containing arrays",
3627 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04003628 }
3629
Olli Etuaho15200042015-11-04 16:56:31 +02003630 return intermediate.addComma(left, right, loc, mShaderVersion);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003631}
3632
Olli Etuaho49300862015-02-20 14:54:49 +02003633TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3634{
3635 switch (op)
3636 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003637 case EOpContinue:
3638 if (mLoopNestingLevel <= 0)
3639 {
3640 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04003641 }
3642 break;
3643 case EOpBreak:
3644 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
3645 {
3646 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04003647 }
3648 break;
3649 case EOpReturn:
3650 if (mCurrentFunctionType->getBasicType() != EbtVoid)
3651 {
3652 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04003653 }
3654 break;
3655 default:
3656 // No checks for discard
3657 break;
Olli Etuaho49300862015-02-20 14:54:49 +02003658 }
3659 return intermediate.addBranch(op, loc);
3660}
3661
Jamie Madillb98c3a82015-07-23 14:26:04 -04003662TIntermBranch *TParseContext::addBranch(TOperator op,
3663 TIntermTyped *returnValue,
3664 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02003665{
3666 ASSERT(op == EOpReturn);
3667 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003668 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02003669 {
3670 error(loc, "void function cannot return a value", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02003671 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003672 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02003673 {
3674 error(loc, "function return is not matching type:", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02003675 }
3676 return intermediate.addBranch(op, returnValue, loc);
3677}
3678
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003679void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
3680{
3681 ASSERT(!functionCall->isUserDefined());
3682 const TString &name = functionCall->getName();
3683 TIntermNode *offset = nullptr;
3684 TIntermSequence *arguments = functionCall->getSequence();
3685 if (name.compare(0, 16, "texelFetchOffset") == 0 ||
3686 name.compare(0, 16, "textureLodOffset") == 0 ||
3687 name.compare(0, 20, "textureProjLodOffset") == 0 ||
3688 name.compare(0, 17, "textureGradOffset") == 0 ||
3689 name.compare(0, 21, "textureProjGradOffset") == 0)
3690 {
3691 offset = arguments->back();
3692 }
3693 else if (name.compare(0, 13, "textureOffset") == 0 ||
3694 name.compare(0, 17, "textureProjOffset") == 0)
3695 {
3696 // A bias parameter might follow the offset parameter.
3697 ASSERT(arguments->size() >= 3);
3698 offset = (*arguments)[2];
3699 }
3700 if (offset != nullptr)
3701 {
3702 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
3703 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
3704 {
3705 TString unmangledName = TFunction::unmangleName(name);
3706 error(functionCall->getLine(), "Texture offset must be a constant expression",
3707 unmangledName.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003708 }
3709 else
3710 {
3711 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
3712 size_t size = offsetConstantUnion->getType().getObjectSize();
3713 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
3714 for (size_t i = 0u; i < size; ++i)
3715 {
3716 int offsetValue = values[i].getIConst();
3717 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
3718 {
3719 std::stringstream tokenStream;
3720 tokenStream << offsetValue;
3721 std::string token = tokenStream.str();
3722 error(offset->getLine(), "Texture offset value out of valid range",
3723 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003724 }
3725 }
3726 }
3727 }
3728}
3729
Jamie Madillb98c3a82015-07-23 14:26:04 -04003730TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
3731 TIntermNode *paramNode,
3732 TIntermNode *thisNode,
3733 const TSourceLoc &loc,
3734 bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003735{
Jamie Madillb98c3a82015-07-23 14:26:04 -04003736 *fatalError = false;
3737 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003738 TIntermTyped *callNode = nullptr;
3739
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003740 if (thisNode != nullptr)
3741 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003742 TConstantUnion *unionArray = new TConstantUnion[1];
Jamie Madillb98c3a82015-07-23 14:26:04 -04003743 int arraySize = 0;
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003744 TIntermTyped *typedThis = thisNode->getAsTyped();
3745 if (fnCall->getName() != "length")
3746 {
3747 error(loc, "invalid method", fnCall->getName().c_str());
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003748 }
3749 else if (paramNode != nullptr)
3750 {
3751 error(loc, "method takes no parameters", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003752 }
3753 else if (typedThis == nullptr || !typedThis->isArray())
3754 {
3755 error(loc, "length can only be called on arrays", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003756 }
3757 else
3758 {
Olli Etuaho96e67382015-04-23 14:27:02 +03003759 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03003760 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003761 {
Olli Etuaho39282e12015-04-23 15:41:48 +03003762 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003763 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03003764 // (func()).length()
3765 // (int[3](0, 1, 2)).length()
Jamie Madillb98c3a82015-07-23 14:26:04 -04003766 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
3767 // expression.
3768 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
3769 // spec section 5.9 which allows "An array, vector or matrix expression with the
3770 // length method applied".
3771 error(loc, "length can only be called on array names, not on array expressions",
3772 "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003773 }
3774 }
Olli Etuaho96e67382015-04-23 14:27:02 +03003775 unionArray->setIConst(arraySize);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003776 callNode =
3777 intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003778 }
3779 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003780 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003781 // Then this should be a constructor.
Olli Etuaho856c4972016-08-08 11:38:39 +03003782 callNode = addConstructor(paramNode, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003783 }
3784 else
3785 {
3786 //
3787 // Not a constructor. Find it in the symbol table.
3788 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303789 const TFunction *fnCandidate;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003790 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003791 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003792 if (fnCandidate)
3793 {
3794 //
3795 // A declared function.
3796 //
Olli Etuaho383b7912016-08-05 11:22:59 +03003797 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003798 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003799 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003800 }
3801 op = fnCandidate->getBuiltInOp();
3802 if (builtIn && op != EOpNull)
3803 {
3804 //
3805 // A function call mapped to a built-in operation.
3806 //
3807 if (fnCandidate->getParamCount() == 1)
3808 {
3809 //
3810 // Treat it like a built-in unary operator.
3811 //
Olli Etuaho15c2ac32015-11-09 15:51:43 +02003812 TIntermAggregate *paramAgg = paramNode->getAsAggregate();
3813 paramNode = paramAgg->getSequence()->front();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003814 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc,
3815 &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003816 if (callNode == nullptr)
3817 {
3818 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003819 extraInfoStream
3820 << "built in unary operator function. Type: "
3821 << static_cast<TIntermTyped *>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003822 std::string extraInfo = extraInfoStream.str();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003823 error(paramNode->getLine(), " wrong operand type", "Internal Error",
3824 extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003825 *fatalError = true;
3826 return nullptr;
3827 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003828 }
3829 else
3830 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003831 TIntermAggregate *aggregate =
3832 intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003833 aggregate->setType(fnCandidate->getReturnType());
3834 aggregate->setPrecisionFromChildren();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02003835 if (aggregate->areChildrenConstQualified())
3836 {
3837 aggregate->getTypePointer()->setQualifier(EvqConst);
3838 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003839
3840 // Some built-in functions have out parameters too.
3841 functionCallLValueErrorCheck(fnCandidate, aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05303842
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003843 // See if we can constant fold a built-in. Note that this may be possible even
3844 // if it is not const-qualified.
Olli Etuahof119a262016-08-19 15:54:22 +03003845 TIntermTyped *foldedNode =
3846 intermediate.foldAggregateBuiltIn(aggregate, &mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05303847 if (foldedNode)
3848 {
Arun Patole274f0702015-05-05 13:33:30 +05303849 callNode = foldedNode;
3850 }
Olli Etuahob43846e2015-06-02 18:18:57 +03003851 else
3852 {
3853 callNode = aggregate;
3854 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003855 }
3856 }
3857 else
3858 {
3859 // This is a real function call
Jamie Madillb98c3a82015-07-23 14:26:04 -04003860 TIntermAggregate *aggregate =
3861 intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003862 aggregate->setType(fnCandidate->getReturnType());
3863
Jamie Madillb98c3a82015-07-23 14:26:04 -04003864 // this is how we know whether the given function is a builtIn function or a user
3865 // defined function
3866 // if builtIn == false, it's a userDefined -> could be an overloaded
3867 // builtIn function also
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003868 // if builtIn == true, it's definitely a builtIn function with EOpNull
3869 if (!builtIn)
3870 aggregate->setUserDefined();
3871 aggregate->setName(fnCandidate->getMangledName());
Corentin Wallez71d147f2015-02-11 11:15:24 -08003872 aggregate->setFunctionId(fnCandidate->getUniqueId());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003873
3874 // This needs to happen after the name is set
3875 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003876 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003877 aggregate->setBuiltInFunctionPrecision();
3878
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003879 checkTextureOffsetConst(aggregate);
3880 }
3881
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003882 callNode = aggregate;
3883
3884 functionCallLValueErrorCheck(fnCandidate, aggregate);
3885 }
3886 }
3887 else
3888 {
3889 // error message was put out by findFunction()
3890 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003891 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003892 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003893 callNode = intermediate.addConstantUnion(unionArray,
3894 TType(EbtFloat, EbpUndefined, EvqConst), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003895 }
3896 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003897 return callNode;
3898}
3899
Jamie Madillb98c3a82015-07-23 14:26:04 -04003900TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03003901 TIntermTyped *trueExpression,
3902 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03003903 const TSourceLoc &loc)
3904{
Olli Etuaho856c4972016-08-08 11:38:39 +03003905 checkIsScalarBool(loc, cond);
Olli Etuaho52901742015-04-15 13:42:45 +03003906
Olli Etuahod0bad2c2016-09-09 18:01:16 +03003907 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03003908 {
Olli Etuahod0bad2c2016-09-09 18:01:16 +03003909 binaryOpError(loc, ":", trueExpression->getCompleteString(),
3910 falseExpression->getCompleteString());
3911 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03003912 }
Olli Etuahoa2d53032015-04-15 14:14:44 +03003913 // ESSL1 sections 5.2 and 5.7:
3914 // ESSL3 section 5.7:
3915 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03003916 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03003917 {
3918 error(loc, "ternary operator is not allowed for structures or arrays", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03003919 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03003920 }
Corentin Wallez0d959252016-07-12 17:26:32 -04003921 // WebGL2 section 5.26, the following results in an error:
3922 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03003923 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04003924 {
3925 error(loc, "ternary operator is not allowed for void", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03003926 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04003927 }
3928
Olli Etuahod0bad2c2016-09-09 18:01:16 +03003929 return TIntermediate::AddTernarySelection(cond, trueExpression, falseExpression, loc);
Olli Etuaho52901742015-04-15 13:42:45 +03003930}
Olli Etuaho49300862015-02-20 14:54:49 +02003931
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003932//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003933// Parse an array of strings using yyparse.
3934//
3935// Returns 0 for success.
3936//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003937int PaParseStrings(size_t count,
3938 const char *const string[],
3939 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05303940 TParseContext *context)
3941{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003942 if ((count == 0) || (string == NULL))
3943 return 1;
3944
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003945 if (glslang_initialize(context))
3946 return 1;
3947
alokp@chromium.org408c45e2012-04-05 15:54:43 +00003948 int error = glslang_scan(count, string, length, context);
3949 if (!error)
3950 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003951
alokp@chromium.org73bc2982012-06-19 18:48:05 +00003952 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00003953
alokp@chromium.org6b495712012-06-29 00:06:58 +00003954 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003955}