blob: 30bc0790207af69e0d2fbbbed1077abca84bcfd8 [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
135//
136// Track whether errors have occurred.
137//
138void TParseContext::recover()
139{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000140}
141
142//
143// Used by flex/bison to output all syntax and parsing errors.
144//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530145void TParseContext::error(const TSourceLoc &loc,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400146 const char *reason,
147 const char *token,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530148 const char *extraInfo)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000149{
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000150 pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -0400151 srcLoc.file = loc.first_file;
152 srcLoc.line = loc.first_line;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400153 mDiagnostics.writeInfo(pp::Diagnostics::PP_ERROR, srcLoc, reason, token, extraInfo);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000154}
155
Arun Patole7e7e68d2015-05-22 12:02:25 +0530156void TParseContext::warning(const TSourceLoc &loc,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400157 const char *reason,
158 const char *token,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530159 const char *extraInfo)
160{
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000161 pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -0400162 srcLoc.file = loc.first_file;
163 srcLoc.line = loc.first_line;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400164 mDiagnostics.writeInfo(pp::Diagnostics::PP_WARNING, srcLoc, reason, token, extraInfo);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000165}
166
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200167void TParseContext::outOfRangeError(bool isError,
168 const TSourceLoc &loc,
169 const char *reason,
170 const char *token,
171 const char *extraInfo)
172{
173 if (isError)
174 {
175 error(loc, reason, token, extraInfo);
176 recover();
177 }
178 else
179 {
180 warning(loc, reason, token, extraInfo);
181 }
182}
183
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000184//
185// Same error message for all places assignments don't work.
186//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530187void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000188{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000189 std::stringstream extraInfoStream;
190 extraInfoStream << "cannot convert from '" << right << "' to '" << left << "'";
191 std::string extraInfo = extraInfoStream.str();
192 error(line, "", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000193}
194
195//
196// Same error message for all places unary operations don't work.
197//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530198void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000199{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000200 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400201 extraInfoStream << "no operation '" << op << "' exists that takes an operand of type "
202 << operand << " (or there is no acceptable conversion)";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000203 std::string extraInfo = extraInfoStream.str();
204 error(line, " wrong operand type", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000205}
206
207//
208// Same error message for all binary operations don't work.
209//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400210void TParseContext::binaryOpError(const TSourceLoc &line,
211 const char *op,
212 TString left,
213 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000214{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000215 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400216 extraInfoStream << "no operation '" << op << "' exists that takes a left-hand operand of type '"
217 << left << "' and a right operand of type '" << right
218 << "' (or there is no acceptable conversion)";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000219 std::string extraInfo = extraInfoStream.str();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530220 error(line, " wrong operand types ", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000221}
222
Jamie Madillb98c3a82015-07-23 14:26:04 -0400223bool TParseContext::precisionErrorCheck(const TSourceLoc &line,
224 TPrecision precision,
225 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530226{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400227 if (!mChecksPrecisionErrors)
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000228 return false;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200229 if (precision == EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530230 {
Olli Etuaho183d7e22015-11-20 15:59:09 +0200231 switch (type)
232 {
233 case EbtFloat:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400234 error(line, "No precision specified for (float)", "");
235 return true;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200236 case EbtInt:
237 case EbtUInt:
238 UNREACHABLE(); // there's always a predeclared qualifier
Jamie Madillb98c3a82015-07-23 14:26:04 -0400239 error(line, "No precision specified (int)", "");
240 return true;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200241 default:
242 if (IsSampler(type))
243 {
244 error(line, "No precision specified (sampler)", "");
245 return true;
246 }
247 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000248 }
249 return false;
250}
251
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000252//
253// Both test and if necessary, spit out an error, to see if the node is really
254// an l-value that can be operated on this way.
255//
256// Returns true if the was an error.
257//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530258bool TParseContext::lValueErrorCheck(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000259{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400260 TIntermSymbol *symNode = node->getAsSymbolNode();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530261 TIntermBinary *binaryNode = node->getAsBinaryNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000262
Arun Patole7e7e68d2015-05-22 12:02:25 +0530263 if (binaryNode)
264 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000265 bool errorReturn;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000266
Jamie Madillb98c3a82015-07-23 14:26:04 -0400267 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530268 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400269 case EOpIndexDirect:
270 case EOpIndexIndirect:
271 case EOpIndexDirectStruct:
272 case EOpIndexDirectInterfaceBlock:
273 return lValueErrorCheck(line, op, binaryNode->getLeft());
274 case EOpVectorSwizzle:
275 errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft());
276 if (!errorReturn)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530277 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400278 int offset[4] = {0, 0, 0, 0};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000279
Jamie Madillb98c3a82015-07-23 14:26:04 -0400280 TIntermTyped *rightNode = binaryNode->getRight();
281 TIntermAggregate *aggrNode = rightNode->getAsAggregate();
282
283 for (TIntermSequence::iterator p = aggrNode->getSequence()->begin();
284 p != aggrNode->getSequence()->end(); p++)
285 {
286 int value = (*p)->getAsTyped()->getAsConstantUnion()->getIConst(0);
287 offset[value]++;
288 if (offset[value] > 1)
289 {
290 error(line, " l-value of swizzle cannot have duplicate components", op);
291
292 return true;
293 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000294 }
295 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000296
Jamie Madillb98c3a82015-07-23 14:26:04 -0400297 return errorReturn;
298 default:
299 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000300 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000301 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000302
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000303 return true;
304 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000305
Arun Patole7e7e68d2015-05-22 12:02:25 +0530306 const char *symbol = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000307 if (symNode != 0)
308 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000309
Arun Patole7e7e68d2015-05-22 12:02:25 +0530310 const char *message = 0;
311 switch (node->getQualifier())
312 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400313 case EvqConst:
314 message = "can't modify a const";
315 break;
316 case EvqConstReadOnly:
317 message = "can't modify a const";
318 break;
319 case EvqAttribute:
320 message = "can't modify an attribute";
321 break;
322 case EvqFragmentIn:
323 message = "can't modify an input";
324 break;
325 case EvqVertexIn:
326 message = "can't modify an input";
327 break;
328 case EvqUniform:
329 message = "can't modify a uniform";
330 break;
331 case EvqVaryingIn:
332 message = "can't modify a varying";
333 break;
334 case EvqFragCoord:
335 message = "can't modify gl_FragCoord";
336 break;
337 case EvqFrontFacing:
338 message = "can't modify gl_FrontFacing";
339 break;
340 case EvqPointCoord:
341 message = "can't modify gl_PointCoord";
342 break;
343 default:
344 //
345 // Type that can't be written to?
346 //
347 if (node->getBasicType() == EbtVoid)
348 {
349 message = "can't modify void";
350 }
351 if (IsSampler(node->getBasicType()))
352 {
353 message = "can't modify a sampler";
354 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000355 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000356
Arun Patole7e7e68d2015-05-22 12:02:25 +0530357 if (message == 0 && binaryNode == 0 && symNode == 0)
358 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000359 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000360
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000361 return true;
362 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000363
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000364 //
365 // Everything else is okay, no error.
366 //
367 if (message == 0)
368 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000369
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000370 //
371 // If we get here, we have an error and a message.
372 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530373 if (symNode)
374 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000375 std::stringstream extraInfoStream;
376 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
377 std::string extraInfo = extraInfoStream.str();
378 error(line, " l-value required", op, extraInfo.c_str());
379 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530380 else
381 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000382 std::stringstream extraInfoStream;
383 extraInfoStream << "(" << message << ")";
384 std::string extraInfo = extraInfoStream.str();
385 error(line, " l-value required", op, extraInfo.c_str());
386 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000387
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000388 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000389}
390
391//
392// Both test, and if necessary spit out an error, to see if the node is really
393// a constant.
394//
395// Returns true if the was an error.
396//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530397bool TParseContext::constErrorCheck(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000398{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000399 if (node->getQualifier() == EvqConst)
400 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000401
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000402 error(node->getLine(), "constant expression required", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000403
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000404 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000405}
406
407//
408// Both test, and if necessary spit out an error, to see if the node is really
409// an integer.
410//
411// Returns true if the was an error.
412//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530413bool TParseContext::integerErrorCheck(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000414{
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000415 if (node->isScalarInt())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000416 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000417
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000418 error(node->getLine(), "integer expression required", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000419
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000420 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000421}
422
423//
424// Both test, and if necessary spit out an error, to see if we are currently
425// globally scoped.
426//
427// Returns true if the was an error.
428//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530429bool TParseContext::globalErrorCheck(const TSourceLoc &line, bool global, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000430{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000431 if (global)
432 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000433
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000434 error(line, "only allowed at global scope", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000435
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000436 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000437}
438
439//
440// For now, keep it simple: if it starts "gl_", it's reserved, independent
441// of scope. Except, if the symbol table is at the built-in push-level,
442// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000443// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
444// webgl shader.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000445//
446// Returns true if there was an error.
447//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530448bool TParseContext::reservedErrorCheck(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000449{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530450 static const char *reservedErrMsg = "reserved built-in name";
451 if (!symbolTable.atBuiltInLevel())
452 {
453 if (identifier.compare(0, 3, "gl_") == 0)
454 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000455 error(line, reservedErrMsg, "gl_");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000456 return true;
457 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530458 if (IsWebGLBasedSpec(mShaderSpec))
459 {
460 if (identifier.compare(0, 6, "webgl_") == 0)
461 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000462 error(line, reservedErrMsg, "webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000463 return true;
464 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530465 if (identifier.compare(0, 7, "_webgl_") == 0)
466 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000467 error(line, reservedErrMsg, "_webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000468 return true;
469 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530470 if (mShaderSpec == SH_CSS_SHADERS_SPEC && identifier.compare(0, 4, "css_") == 0)
471 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000472 error(line, reservedErrMsg, "css_");
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000473 return true;
474 }
alokp@chromium.org613ef312010-07-21 18:54:22 +0000475 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530476 if (identifier.find("__") != TString::npos)
477 {
478 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400479 "identifiers containing two consecutive underscores (__) are reserved as "
480 "possible future keywords",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530481 identifier.c_str());
daniel@transgaming.combeadd5d2012-04-12 02:35:31 +0000482 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000483 }
484 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000485
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000486 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000487}
488
489//
490// Make sure there is enough data provided to the constructor to build
491// something of the type of the constructor. Also returns the type of
492// the constructor.
493//
494// Returns true if there was an error in construction.
495//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400496bool TParseContext::constructorErrorCheck(const TSourceLoc &line,
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200497 TIntermNode *argumentsNode,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400498 TFunction &function,
499 TOperator op,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530500 TType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000501{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000502 *type = function.getReturnType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000503
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000504 bool constructingMatrix = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400505 switch (op)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530506 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400507 case EOpConstructMat2:
508 case EOpConstructMat2x3:
509 case EOpConstructMat2x4:
510 case EOpConstructMat3x2:
511 case EOpConstructMat3:
512 case EOpConstructMat3x4:
513 case EOpConstructMat4x2:
514 case EOpConstructMat4x3:
515 case EOpConstructMat4:
516 constructingMatrix = true;
517 break;
518 default:
519 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000520 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000521
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000522 //
523 // Note: It's okay to have too many components available, but not okay to have unused
524 // arguments. 'full' will go to true when enough args have been seen. If we loop
525 // again, there is an extra argument, so 'overfull' will become true.
526 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000527
Jamie Madillb98c3a82015-07-23 14:26:04 -0400528 size_t size = 0;
529 bool constType = true;
530 bool full = false;
531 bool overFull = false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000532 bool matrixInMatrix = false;
533 bool arrayArg = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530534 for (size_t i = 0; i < function.getParamCount(); ++i)
535 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700536 const TConstParameter &param = function.getParam(i);
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000537 size += param.type->getObjectSize();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530538
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000539 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000540 matrixInMatrix = true;
541 if (full)
542 overFull = true;
543 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
544 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000545 if (param.type->getQualifier() != EvqConst)
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000546 constType = false;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000547 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000548 arrayArg = true;
549 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530550
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000551 if (constType)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000552 type->setQualifier(EvqConst);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000553
Olli Etuaho376f1b52015-04-13 13:23:41 +0300554 if (type->isArray())
555 {
556 if (type->isUnsizedArray())
557 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700558 type->setArraySize(static_cast<int>(function.getParamCount()));
Olli Etuaho376f1b52015-04-13 13:23:41 +0300559 }
560 else if (static_cast<size_t>(type->getArraySize()) != function.getParamCount())
561 {
562 error(line, "array constructor needs one argument per array element", "constructor");
563 return true;
564 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000565 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000566
Arun Patole7e7e68d2015-05-22 12:02:25 +0530567 if (arrayArg && op != EOpConstructStruct)
568 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000569 error(line, "constructing from a non-dereferenced array", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000570 return true;
571 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000572
Arun Patole7e7e68d2015-05-22 12:02:25 +0530573 if (matrixInMatrix && !type->isArray())
574 {
575 if (function.getParamCount() != 1)
576 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400577 error(line, "constructing matrix from matrix can only take one argument",
578 "constructor");
579 return true;
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000580 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000581 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000582
Arun Patole7e7e68d2015-05-22 12:02:25 +0530583 if (overFull)
584 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000585 error(line, "too many arguments", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000586 return true;
587 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530588
Jamie Madillb98c3a82015-07-23 14:26:04 -0400589 if (op == EOpConstructStruct && !type->isArray() &&
590 type->getStruct()->fields().size() != function.getParamCount())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530591 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400592 error(line,
593 "Number of constructor parameters does not match the number of structure fields",
594 "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000595 return true;
596 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000597
Arun Patole7e7e68d2015-05-22 12:02:25 +0530598 if (!type->isMatrix() || !matrixInMatrix)
599 {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000600 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
Arun Patole7e7e68d2015-05-22 12:02:25 +0530601 (op == EOpConstructStruct && size < type->getObjectSize()))
602 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000603 error(line, "not enough data provided for construction", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000604 return true;
605 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000606 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000607
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200608 if (argumentsNode == nullptr)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530609 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200610 error(line, "constructor does not have any arguments", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000611 return true;
612 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200613
614 TIntermAggregate *argumentsAgg = argumentsNode->getAsAggregate();
615 for (TIntermNode *&argNode : *argumentsAgg->getSequence())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530616 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200617 TIntermTyped *argTyped = argNode->getAsTyped();
618 ASSERT(argTyped != nullptr);
619 if (op != EOpConstructStruct && IsSampler(argTyped->getBasicType()))
620 {
621 error(line, "cannot convert a sampler", "constructor");
622 return true;
623 }
624 if (argTyped->getBasicType() == EbtVoid)
625 {
626 error(line, "cannot convert a void", "constructor");
627 return true;
628 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000629 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000630
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000631 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000632}
633
Jamie Madillb98c3a82015-07-23 14:26:04 -0400634// This function checks to see if a void variable has been declared and raise an error message for
635// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000636//
637// returns true in case of an error
638//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400639bool TParseContext::voidErrorCheck(const TSourceLoc &line,
640 const TString &identifier,
641 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000642{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300643 if (type == EbtVoid)
644 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000645 error(line, "illegal use of type 'void'", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000646 return true;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300647 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000648
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000649 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000650}
651
Jamie Madillb98c3a82015-07-23 14:26:04 -0400652// This function checks to see if the node (for the expression) contains a scalar boolean expression
653// or not
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000654//
655// returns true in case of an error
656//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530657bool TParseContext::boolErrorCheck(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000658{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530659 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector())
660 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000661 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000662 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530663 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000664
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000665 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000666}
667
Jamie Madillb98c3a82015-07-23 14:26:04 -0400668// This function checks to see if the node (for the expression) contains a scalar boolean expression
669// or not
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000670//
671// returns true in case of an error
672//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530673bool TParseContext::boolErrorCheck(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000674{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530675 if (pType.type != EbtBool || pType.isAggregate())
676 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000677 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000678 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530679 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000680
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000681 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000682}
683
Jamie Madillb98c3a82015-07-23 14:26:04 -0400684bool TParseContext::samplerErrorCheck(const TSourceLoc &line,
685 const TPublicType &pType,
686 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000687{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530688 if (pType.type == EbtStruct)
689 {
690 if (containsSampler(*pType.userDef))
691 {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000692 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530693
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000694 return true;
695 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530696
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000697 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530698 }
699 else if (IsSampler(pType.type))
700 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000701 error(line, reason, getBasicString(pType.type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000702
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000703 return true;
704 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000705
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000706 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000707}
708
Arun Patole7e7e68d2015-05-22 12:02:25 +0530709bool TParseContext::locationDeclaratorListCheck(const TSourceLoc &line, const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400710{
711 if (pType.layoutQualifier.location != -1)
712 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400713 error(line, "location must only be specified for a single input or output variable",
714 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400715 return true;
716 }
717
718 return false;
719}
720
Jamie Madillb98c3a82015-07-23 14:26:04 -0400721bool TParseContext::parameterSamplerErrorCheck(const TSourceLoc &line,
722 TQualifier qualifier,
723 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000724{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400725 if ((qualifier == EvqOut || qualifier == EvqInOut) && type.getBasicType() != EbtStruct &&
726 IsSampler(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530727 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000728 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000729 return true;
730 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000731
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000732 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000733}
734
Arun Patole7e7e68d2015-05-22 12:02:25 +0530735bool TParseContext::containsSampler(const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000736{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000737 if (IsSampler(type.getBasicType()))
738 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000739
Arun Patole7e7e68d2015-05-22 12:02:25 +0530740 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock())
741 {
742 const TFieldList &fields = type.getStruct()->fields();
743 for (unsigned int i = 0; i < fields.size(); ++i)
744 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400745 if (containsSampler(*fields[i]->type()))
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000746 return true;
747 }
748 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000749
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000750 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000751}
752
753//
754// Do size checking for an array type's size.
755//
756// Returns true if there was an error.
757//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530758bool TParseContext::arraySizeErrorCheck(const TSourceLoc &line, TIntermTyped *expr, int &size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000759{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530760 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000761
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200762 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
763 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
764 // fold as array size.
765 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000766 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000767 error(line, "array size must be a constant integer expression", "");
Olli Etuahoe7847b02015-03-16 11:56:12 +0200768 size = 1;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000769 return true;
770 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000771
Nicolas Capens906744a2014-06-06 15:18:07 -0400772 unsigned int unsignedSize = 0;
773
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000774 if (constant->getBasicType() == EbtUInt)
775 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400776 unsignedSize = constant->getUConst(0);
Jamie Madillb98c3a82015-07-23 14:26:04 -0400777 size = static_cast<int>(unsignedSize);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000778 }
779 else
780 {
781 size = constant->getIConst(0);
782
Nicolas Capens906744a2014-06-06 15:18:07 -0400783 if (size < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000784 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400785 error(line, "array size must be non-negative", "");
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000786 size = 1;
787 return true;
788 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400789
790 unsignedSize = static_cast<unsigned int>(size);
791 }
792
793 if (size == 0)
794 {
795 error(line, "array size must be greater than zero", "");
796 size = 1;
797 return true;
798 }
799
800 // The size of arrays is restricted here to prevent issues further down the
801 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
802 // 4096 registers so this should be reasonable even for aggressively optimizable code.
803 const unsigned int sizeLimit = 65536;
804
805 if (unsignedSize > sizeLimit)
806 {
807 error(line, "array size too large", "");
808 size = 1;
809 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000810 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000811
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000812 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000813}
814
815//
816// See if this qualifier can be an array.
817//
818// Returns true if there is an error.
819//
Olli Etuaho3739d232015-04-08 12:23:44 +0300820bool TParseContext::arrayQualifierErrorCheck(const TSourceLoc &line, const TPublicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000821{
Olli Etuaho3739d232015-04-08 12:23:44 +0300822 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqVertexIn) ||
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400823 (type.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300824 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400825 error(line, "cannot declare arrays of this qualifier",
826 TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000827 return true;
828 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000829
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000830 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000831}
832
833//
834// See if this type can be an array.
835//
836// Returns true if there is an error.
837//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530838bool TParseContext::arrayTypeErrorCheck(const TSourceLoc &line, const TPublicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000839{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000840 //
841 // Can the type be an array?
842 //
Jamie Madill06145232015-05-13 13:10:01 -0400843 if (type.array)
844 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000845 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000846 return true;
847 }
Olli Etuahocc36b982015-07-10 14:14:18 +0300848 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
849 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
850 // 4.3.4).
851 if (mShaderVersion >= 300 && type.type == EbtStruct && sh::IsVarying(type.qualifier))
852 {
853 error(line, "cannot declare arrays of structs of this qualifier",
854 TType(type).getCompleteString().c_str());
855 return true;
856 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000857
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000858 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000859}
860
861//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000862// Enforce non-initializer type/qualifier rules.
863//
864// Returns true if there was an error.
865//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400866bool TParseContext::nonInitErrorCheck(const TSourceLoc &line,
867 const TString &identifier,
868 TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000869{
Olli Etuaho3739d232015-04-08 12:23:44 +0300870 ASSERT(type != nullptr);
871 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000872 {
873 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300874 type->qualifier = EvqTemporary;
875
876 // Generate informative error messages for ESSL1.
877 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400878 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000879 {
Arun Patole7e7e68d2015-05-22 12:02:25 +0530880 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400881 "structures containing arrays may not be declared constant since they cannot be "
882 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530883 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000884 }
885 else
886 {
887 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
888 }
889
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000890 return true;
891 }
Olli Etuaho376f1b52015-04-13 13:23:41 +0300892 if (type->isUnsizedArray())
893 {
894 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
895 return true;
896 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000897 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000898}
899
Olli Etuaho2935c582015-04-08 14:32:06 +0300900// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000901// and update the symbol table.
902//
Olli Etuaho2935c582015-04-08 14:32:06 +0300903// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000904//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400905bool TParseContext::declareVariable(const TSourceLoc &line,
906 const TString &identifier,
907 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +0300908 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000909{
Olli Etuaho2935c582015-04-08 14:32:06 +0300910 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000911
Olli Etuaho2935c582015-04-08 14:32:06 +0300912 bool needsReservedErrorCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000913
Olli Etuaho2935c582015-04-08 14:32:06 +0300914 // gl_LastFragData may be redeclared with a new precision qualifier
915 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
916 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400917 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
918 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho2935c582015-04-08 14:32:06 +0300919 if (type.getArraySize() == maxDrawBuffers->getConstPointer()->getIConst())
920 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400921 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +0300922 {
923 needsReservedErrorCheck = extensionErrorCheck(line, builtInSymbol->getExtension());
924 }
925 }
926 else
927 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400928 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
929 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +0300930 return false;
931 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000932 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000933
Olli Etuaho2935c582015-04-08 14:32:06 +0300934 if (needsReservedErrorCheck && reservedErrorCheck(line, identifier))
935 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000936
Olli Etuaho2935c582015-04-08 14:32:06 +0300937 (*variable) = new TVariable(&identifier, type);
938 if (!symbolTable.declare(*variable))
939 {
940 error(line, "redefinition", identifier.c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -0400941 *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +0300942 return false;
943 }
944
945 if (voidErrorCheck(line, identifier, type.getBasicType()))
946 return false;
947
948 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000949}
950
Jamie Madillb98c3a82015-07-23 14:26:04 -0400951bool TParseContext::paramErrorCheck(const TSourceLoc &line,
952 TQualifier qualifier,
953 TQualifier paramQualifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530954 TType *type)
955{
956 if (qualifier != EvqConst && qualifier != EvqTemporary)
957 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000958 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000959 return true;
960 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530961 if (qualifier == EvqConst && paramQualifier != EvqIn)
962 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400963 error(line, "qualifier not allowed with ", getQualifierString(qualifier),
964 getQualifierString(paramQualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000965 return true;
966 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000967
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000968 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000969 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000970 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000971 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000972
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000973 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000974}
975
Arun Patole7e7e68d2015-05-22 12:02:25 +0530976bool TParseContext::extensionErrorCheck(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000977{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400978 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000979 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +0530980 if (iter == extBehavior.end())
981 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000982 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000983 return true;
984 }
zmo@google.comf5450912011-09-09 01:37:19 +0000985 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +0530986 if (iter->second == EBhDisable || iter->second == EBhUndefined)
987 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000988 error(line, "extension", extension.c_str(), "is disabled");
989 return true;
990 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530991 if (iter->second == EBhWarn)
992 {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000993 warning(line, "extension", extension.c_str(), "is being used");
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000994 return false;
995 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000996
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000997 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000998}
999
Jamie Madillb98c3a82015-07-23 14:26:04 -04001000// These checks are common for all declarations starting a declarator list, and declarators that
1001// follow an empty declaration.
Olli Etuahofa33d582015-04-09 14:33:12 +03001002//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001003bool TParseContext::singleDeclarationErrorCheck(const TPublicType &publicType,
1004 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001005{
Olli Etuahofa33d582015-04-09 14:33:12 +03001006 switch (publicType.qualifier)
1007 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001008 case EvqVaryingIn:
1009 case EvqVaryingOut:
1010 case EvqAttribute:
1011 case EvqVertexIn:
1012 case EvqFragmentOut:
1013 if (publicType.type == EbtStruct)
1014 {
1015 error(identifierLocation, "cannot be used with a structure",
1016 getQualifierString(publicType.qualifier));
1017 return true;
1018 }
Olli Etuahofa33d582015-04-09 14:33:12 +03001019
Jamie Madillb98c3a82015-07-23 14:26:04 -04001020 default:
1021 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001022 }
1023
Jamie Madillb98c3a82015-07-23 14:26:04 -04001024 if (publicType.qualifier != EvqUniform &&
1025 samplerErrorCheck(identifierLocation, publicType, "samplers must be uniform"))
Olli Etuahofa33d582015-04-09 14:33:12 +03001026 {
Jamie Madilla5efff92013-06-06 11:56:47 -04001027 return true;
Olli Etuahofa33d582015-04-09 14:33:12 +03001028 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001029
1030 // check for layout qualifier issues
1031 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
1032
1033 if (layoutQualifier.matrixPacking != EmpUnspecified)
1034 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001035 error(identifierLocation, "layout qualifier",
1036 getMatrixPackingString(layoutQualifier.matrixPacking),
Olli Etuahofa33d582015-04-09 14:33:12 +03001037 "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -04001038 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001039 }
1040
1041 if (layoutQualifier.blockStorage != EbsUnspecified)
1042 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001043 error(identifierLocation, "layout qualifier",
1044 getBlockStorageString(layoutQualifier.blockStorage),
Olli Etuahofa33d582015-04-09 14:33:12 +03001045 "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -04001046 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001047 }
1048
Olli Etuahofa33d582015-04-09 14:33:12 +03001049 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut &&
1050 layoutLocationErrorCheck(identifierLocation, publicType.layoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001051 {
Jamie Madill51a53c72013-06-19 09:24:43 -04001052 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001053 }
1054
1055 return false;
1056}
1057
Jamie Madillb98c3a82015-07-23 14:26:04 -04001058bool TParseContext::layoutLocationErrorCheck(const TSourceLoc &location,
1059 const TLayoutQualifier &layoutQualifier)
Jamie Madilla5efff92013-06-06 11:56:47 -04001060{
1061 if (layoutQualifier.location != -1)
1062 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001063 error(location, "invalid layout qualifier:", "location",
1064 "only valid on program inputs and outputs");
Jamie Madilla5efff92013-06-06 11:56:47 -04001065 return true;
1066 }
1067
1068 return false;
1069}
1070
Jamie Madillb98c3a82015-07-23 14:26:04 -04001071bool TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate,
1072 TIntermAggregate *aggregate)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001073{
1074 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1075 {
1076 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1077 if (qual == EvqOut || qual == EvqInOut)
1078 {
1079 TIntermTyped *node = (*(aggregate->getSequence()))[i]->getAsTyped();
1080 if (lValueErrorCheck(node->getLine(), "assign", node))
1081 {
1082 error(node->getLine(),
Jamie Madillb98c3a82015-07-23 14:26:04 -04001083 "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error");
Olli Etuahob6e07a62015-02-16 12:22:10 +02001084 recover();
1085 return true;
1086 }
1087 }
1088 }
1089 return false;
1090}
1091
Jamie Madillb98c3a82015-07-23 14:26:04 -04001092void TParseContext::es3InvariantErrorCheck(const TQualifier qualifier,
1093 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001094{
1095 if (!sh::IsVaryingOut(qualifier) && qualifier != EvqFragmentOut)
1096 {
1097 error(invariantLocation, "Only out variables can be invariant.", "invariant");
1098 recover();
1099 }
1100}
1101
Arun Patole7e7e68d2015-05-22 12:02:25 +05301102bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001103{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001104 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001105 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1106 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001107}
1108
Arun Patole7e7e68d2015-05-22 12:02:25 +05301109bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001110{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001111 return ::IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001112}
1113
Jamie Madillb98c3a82015-07-23 14:26:04 -04001114void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1115 const char *extName,
1116 const char *behavior)
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.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001122}
1123
Jamie Madillb98c3a82015-07-23 14:26:04 -04001124void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1125 const char *name,
1126 const char *value,
1127 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001128{
1129 pp::SourceLocation srcLoc;
1130 srcLoc.file = loc.first_file;
1131 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001132 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001133}
1134
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001135/////////////////////////////////////////////////////////////////////////////////
1136//
1137// Non-Errors.
1138//
1139/////////////////////////////////////////////////////////////////////////////////
1140
Jamie Madill5c097022014-08-20 16:38:32 -04001141const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1142 const TString *name,
1143 const TSymbol *symbol)
1144{
1145 const TVariable *variable = NULL;
1146
1147 if (!symbol)
1148 {
1149 error(location, "undeclared identifier", name->c_str());
1150 recover();
1151 }
1152 else if (!symbol->isVariable())
1153 {
1154 error(location, "variable expected", name->c_str());
1155 recover();
1156 }
1157 else
1158 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001159 variable = static_cast<const TVariable *>(symbol);
Jamie Madill5c097022014-08-20 16:38:32 -04001160
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001161 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
Jamie Madill5c097022014-08-20 16:38:32 -04001162 !variable->getExtension().empty() &&
1163 extensionErrorCheck(location, variable->getExtension()))
1164 {
1165 recover();
1166 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001167
1168 // Reject shaders using both gl_FragData and gl_FragColor
1169 TQualifier qualifier = variable->getType().getQualifier();
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001170 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001171 {
1172 mUsesFragData = true;
1173 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001174 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001175 {
1176 mUsesFragColor = true;
1177 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001178 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1179 {
1180 mUsesSecondaryOutputs = true;
1181 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001182
1183 // This validation is not quite correct - it's only an error to write to
1184 // both FragData and FragColor. For simplicity, and because users shouldn't
1185 // be rewarded for reading from undefined varaibles, return an error
1186 // if they are both referenced, rather than assigned.
1187 if (mUsesFragData && mUsesFragColor)
1188 {
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001189 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1190 if (mUsesSecondaryOutputs)
1191 {
1192 errorMessage =
1193 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1194 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1195 }
1196 error(location, errorMessage, name->c_str());
Jamie Madill14e95b32015-05-07 10:10:41 -04001197 recover();
1198 }
Jamie Madill5c097022014-08-20 16:38:32 -04001199 }
1200
1201 if (!variable)
1202 {
1203 TType type(EbtFloat, EbpUndefined);
1204 TVariable *fakeVariable = new TVariable(name, type);
1205 symbolTable.declare(fakeVariable);
1206 variable = fakeVariable;
1207 }
1208
1209 return variable;
1210}
1211
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001212TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1213 const TString *name,
1214 const TSymbol *symbol)
1215{
1216 const TVariable *variable = getNamedVariable(location, name, symbol);
1217
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001218 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001219 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001220 const TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001221 return intermediate.addConstantUnion(constArray, variable->getType(), location);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001222 }
1223 else
1224 {
1225 return intermediate.addSymbol(variable->getUniqueId(), variable->getName(),
1226 variable->getType(), location);
1227 }
1228}
1229
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001230//
1231// Look up a function name in the symbol table, and make sure it is a function.
1232//
1233// Return the function symbol if found, otherwise 0.
1234//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001235const TFunction *TParseContext::findFunction(const TSourceLoc &line,
1236 TFunction *call,
1237 int inputShaderVersion,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301238 bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001239{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001240 // First find by unmangled name to check whether the function name has been
1241 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001242 // If a function is found, check for one with a matching argument list.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301243 const TSymbol *symbol = symbolTable.find(call->getName(), inputShaderVersion, builtIn);
1244 if (symbol == 0 || symbol->isFunction())
1245 {
Austin Kinross3ae64652015-01-26 15:51:39 -08001246 symbol = symbolTable.find(call->getMangledName(), inputShaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001247 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001248
Arun Patole7e7e68d2015-05-22 12:02:25 +05301249 if (symbol == 0)
1250 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001251 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001252 return 0;
1253 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001254
Arun Patole7e7e68d2015-05-22 12:02:25 +05301255 if (!symbol->isFunction())
1256 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001257 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001258 return 0;
1259 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001260
Jamie Madillb98c3a82015-07-23 14:26:04 -04001261 return static_cast<const TFunction *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001262}
1263
1264//
1265// Initializers show up in several places in the grammar. Have one set of
1266// code to handle them here.
1267//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001268// Returns true on error, false if no error
1269//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001270bool TParseContext::executeInitializer(const TSourceLoc &line,
1271 const TString &identifier,
1272 const TPublicType &pType,
1273 TIntermTyped *initializer,
1274 TIntermNode **intermNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001275{
Olli Etuahoe7847b02015-03-16 11:56:12 +02001276 ASSERT(intermNode != nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001277 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001278
Olli Etuaho2935c582015-04-08 14:32:06 +03001279 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001280 if (type.isUnsizedArray())
1281 {
1282 type.setArraySize(initializer->getArraySize());
1283 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001284 if (!declareVariable(line, identifier, type, &variable))
1285 {
1286 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001287 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001288
Olli Etuahob0c645e2015-05-12 14:25:36 +03001289 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001290 if (symbolTable.atGlobalLevel() &&
1291 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001292 {
1293 // Error message does not completely match behavior with ESSL 1.00, but
1294 // we want to steer developers towards only using constant expressions.
1295 error(line, "global variable initializers must be constant expressions", "=");
1296 return true;
1297 }
1298 if (globalInitWarning)
1299 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001300 warning(
1301 line,
1302 "global variable initializers should be constant expressions "
1303 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1304 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001305 }
1306
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001307 //
1308 // identifier must be of type constant, a global, or a temporary
1309 //
1310 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301311 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1312 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001313 error(line, " cannot initialize this type of qualifier ",
1314 variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001315 return true;
1316 }
1317 //
1318 // test for and propagate constant
1319 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001320
Arun Patole7e7e68d2015-05-22 12:02:25 +05301321 if (qualifier == EvqConst)
1322 {
1323 if (qualifier != initializer->getType().getQualifier())
1324 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001325 std::stringstream extraInfoStream;
1326 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1327 std::string extraInfo = extraInfoStream.str();
1328 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001329 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001330 return true;
1331 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301332 if (type != initializer->getType())
1333 {
1334 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001335 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001336 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001337 return true;
1338 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001339
1340 // Save the constant folded value to the variable if possible. For example array
1341 // initializers are not folded, since that way copying the array literal to multiple places
1342 // in the shader is avoided.
1343 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1344 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301345 if (initializer->getAsConstantUnion())
1346 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001347 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001348 *intermNode = nullptr;
1349 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301350 }
1351 else if (initializer->getAsSymbolNode())
1352 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001353 const TSymbol *symbol =
1354 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1355 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001356
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001357 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001358 if (constArray)
1359 {
1360 variable->shareConstPointer(constArray);
1361 *intermNode = nullptr;
1362 return false;
1363 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001364 }
1365 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001366
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001367 TIntermSymbol *intermSymbol = intermediate.addSymbol(
1368 variable->getUniqueId(), variable->getName(), variable->getType(), line);
1369 *intermNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1370 if (*intermNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001371 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001372 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1373 return true;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001374 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001375
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001376 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001377}
1378
Jamie Madillb98c3a82015-07-23 14:26:04 -04001379TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier,
1380 bool invariant,
1381 TLayoutQualifier layoutQualifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301382 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001383{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001384 TPublicType returnType = typeSpecifier;
1385 returnType.qualifier = qualifier;
1386 returnType.invariant = invariant;
Jamie Madilla5efff92013-06-06 11:56:47 -04001387 returnType.layoutQualifier = layoutQualifier;
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001388
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001389 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001390 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001391 if (typeSpecifier.array)
1392 {
1393 error(typeSpecifier.line, "not supported", "first-class array");
1394 recover();
1395 returnType.clearArrayness();
1396 }
1397
Jamie Madillb98c3a82015-07-23 14:26:04 -04001398 if (qualifier == EvqAttribute &&
1399 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001400 {
1401 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1402 recover();
1403 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001404
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001405 if ((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1406 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1407 {
1408 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1409 recover();
1410 }
1411 }
1412 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001413 {
Olli Etuahoabb0c382015-07-13 12:01:12 +03001414 if (!layoutQualifier.isEmpty())
1415 {
1416 if (globalErrorCheck(typeSpecifier.line, symbolTable.atGlobalLevel(), "layout"))
1417 {
1418 recover();
1419 }
1420 }
Olli Etuahocc36b982015-07-10 14:14:18 +03001421 if (sh::IsVarying(qualifier) || qualifier == EvqVertexIn || qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001422 {
Olli Etuahocc36b982015-07-10 14:14:18 +03001423 es3InputOutputTypeCheck(qualifier, typeSpecifier, typeSpecifier.line);
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001424 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001425 }
1426
1427 return returnType;
1428}
1429
Olli Etuahocc36b982015-07-10 14:14:18 +03001430void TParseContext::es3InputOutputTypeCheck(const TQualifier qualifier,
1431 const TPublicType &type,
1432 const TSourceLoc &qualifierLocation)
1433{
1434 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
1435 if (type.type == EbtBool)
1436 {
1437 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
1438 recover();
1439 }
1440
1441 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
1442 switch (qualifier)
1443 {
1444 case EvqVertexIn:
1445 // ESSL 3.00 section 4.3.4
1446 if (type.array)
1447 {
1448 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
1449 recover();
1450 }
1451 // Vertex inputs with a struct type are disallowed in singleDeclarationErrorCheck
1452 return;
1453 case EvqFragmentOut:
1454 // ESSL 3.00 section 4.3.6
1455 if (type.isMatrix())
1456 {
1457 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
1458 recover();
1459 }
1460 // Fragment outputs with a struct type are disallowed in singleDeclarationErrorCheck
1461 return;
1462 default:
1463 break;
1464 }
1465
1466 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
1467 // restrictions.
1468 bool typeContainsIntegers =
1469 (type.type == EbtInt || type.type == EbtUInt || type.isStructureContainingType(EbtInt) ||
1470 type.isStructureContainingType(EbtUInt));
1471 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
1472 {
1473 error(qualifierLocation, "must use 'flat' interpolation here",
1474 getQualifierString(qualifier));
1475 recover();
1476 }
1477
1478 if (type.type == EbtStruct)
1479 {
1480 // ESSL 3.00 sections 4.3.4 and 4.3.6.
1481 // These restrictions are only implied by the ESSL 3.00 spec, but
1482 // the ESSL 3.10 spec lists these restrictions explicitly.
1483 if (type.array)
1484 {
1485 error(qualifierLocation, "cannot be an array of structures",
1486 getQualifierString(qualifier));
1487 recover();
1488 }
1489 if (type.isStructureContainingArrays())
1490 {
1491 error(qualifierLocation, "cannot be a structure containing an array",
1492 getQualifierString(qualifier));
1493 recover();
1494 }
1495 if (type.isStructureContainingType(EbtStruct))
1496 {
1497 error(qualifierLocation, "cannot be a structure containing a structure",
1498 getQualifierString(qualifier));
1499 recover();
1500 }
1501 if (type.isStructureContainingType(EbtBool))
1502 {
1503 error(qualifierLocation, "cannot be a structure containing a bool",
1504 getQualifierString(qualifier));
1505 recover();
1506 }
1507 }
1508}
1509
Olli Etuahofa33d582015-04-09 14:33:12 +03001510TIntermAggregate *TParseContext::parseSingleDeclaration(TPublicType &publicType,
1511 const TSourceLoc &identifierOrTypeLocation,
1512 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001513{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001514 TIntermSymbol *symbol =
1515 intermediate.addSymbol(0, identifier, TType(publicType), identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001516
Olli Etuahobab4c082015-04-24 16:38:49 +03001517 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03001518
Olli Etuahobab4c082015-04-24 16:38:49 +03001519 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1520
1521 if (emptyDeclaration)
1522 {
1523 if (publicType.isUnsizedArray())
1524 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001525 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1526 // error. It is assumed that this applies to empty declarations as well.
1527 error(identifierOrTypeLocation, "empty array declaration needs to specify a size",
1528 identifier.c_str());
Olli Etuahobab4c082015-04-24 16:38:49 +03001529 }
1530 }
1531 else
Jamie Madill60ed9812013-06-06 11:56:46 -04001532 {
Olli Etuahofa33d582015-04-09 14:33:12 +03001533 if (singleDeclarationErrorCheck(publicType, identifierOrTypeLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001534 recover();
1535
Olli Etuaho376f1b52015-04-13 13:23:41 +03001536 if (nonInitErrorCheck(identifierOrTypeLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001537 recover();
1538
Olli Etuaho2935c582015-04-08 14:32:06 +03001539 TVariable *variable = nullptr;
Olli Etuahofa33d582015-04-09 14:33:12 +03001540 if (!declareVariable(identifierOrTypeLocation, identifier, TType(publicType), &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001541 recover();
1542
1543 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001544 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001545 }
1546
Olli Etuahoe7847b02015-03-16 11:56:12 +02001547 return intermediate.makeAggregate(symbol, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001548}
1549
Olli Etuahoe7847b02015-03-16 11:56:12 +02001550TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1551 const TSourceLoc &identifierLocation,
1552 const TString &identifier,
1553 const TSourceLoc &indexLocation,
1554 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001555{
Olli Etuahofa33d582015-04-09 14:33:12 +03001556 mDeferredSingleDeclarationErrorCheck = false;
1557
1558 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001559 recover();
1560
Olli Etuaho376f1b52015-04-13 13:23:41 +03001561 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001562 recover();
1563
Jamie Madillb98c3a82015-07-23 14:26:04 -04001564 if (arrayTypeErrorCheck(indexLocation, publicType) ||
1565 arrayQualifierErrorCheck(indexLocation, publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001566 {
1567 recover();
1568 }
1569
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001570 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001571
1572 int size;
1573 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1574 {
1575 recover();
1576 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001577 // Make the type an array even if size check failed.
1578 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1579 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001580
Olli Etuaho2935c582015-04-08 14:32:06 +03001581 TVariable *variable = nullptr;
1582 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001583 recover();
1584
Olli Etuahoe7847b02015-03-16 11:56:12 +02001585 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001586 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001587 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001588
Olli Etuahoe7847b02015-03-16 11:56:12 +02001589 return intermediate.makeAggregate(symbol, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001590}
1591
Jamie Madill06145232015-05-13 13:10:01 -04001592TIntermAggregate *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001593 const TSourceLoc &identifierLocation,
1594 const TString &identifier,
1595 const TSourceLoc &initLocation,
1596 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001597{
Olli Etuahofa33d582015-04-09 14:33:12 +03001598 mDeferredSingleDeclarationErrorCheck = false;
1599
1600 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001601 recover();
1602
Olli Etuahoe7847b02015-03-16 11:56:12 +02001603 TIntermNode *intermNode = nullptr;
1604 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04001605 {
1606 //
1607 // Build intermediate representation
1608 //
Olli Etuahoe7847b02015-03-16 11:56:12 +02001609 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001610 }
1611 else
1612 {
1613 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001614 return nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001615 }
1616}
1617
Jamie Madillb98c3a82015-07-23 14:26:04 -04001618TIntermAggregate *TParseContext::parseSingleArrayInitDeclaration(
1619 TPublicType &publicType,
1620 const TSourceLoc &identifierLocation,
1621 const TString &identifier,
1622 const TSourceLoc &indexLocation,
1623 TIntermTyped *indexExpression,
1624 const TSourceLoc &initLocation,
1625 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001626{
1627 mDeferredSingleDeclarationErrorCheck = false;
1628
1629 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1630 recover();
1631
Jamie Madillb98c3a82015-07-23 14:26:04 -04001632 if (arrayTypeErrorCheck(indexLocation, publicType) ||
1633 arrayQualifierErrorCheck(indexLocation, publicType))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001634 {
1635 recover();
1636 }
1637
1638 TPublicType arrayType(publicType);
1639
Olli Etuaho376f1b52015-04-13 13:23:41 +03001640 int size = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001641 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
1642 // the initializer.
1643 if (indexExpression != nullptr &&
1644 arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001645 {
1646 recover();
1647 }
1648 // Make the type an array even if size check failed.
1649 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1650 arrayType.setArraySize(size);
1651
1652 // initNode will correspond to the whole of "type b[n] = initializer".
1653 TIntermNode *initNode = nullptr;
1654 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1655 {
1656 return initNode ? intermediate.makeAggregate(initNode, initLocation) : nullptr;
1657 }
1658 else
1659 {
1660 recover();
1661 return nullptr;
1662 }
1663}
1664
Olli Etuahoe7847b02015-03-16 11:56:12 +02001665TIntermAggregate *TParseContext::parseInvariantDeclaration(const TSourceLoc &invariantLoc,
Jamie Madill47e3ec02014-08-20 16:38:33 -04001666 const TSourceLoc &identifierLoc,
1667 const TString *identifier,
1668 const TSymbol *symbol)
1669{
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001670 // invariant declaration
Jamie Madill47e3ec02014-08-20 16:38:33 -04001671 if (globalErrorCheck(invariantLoc, symbolTable.atGlobalLevel(), "invariant varying"))
1672 {
1673 recover();
1674 }
1675
1676 if (!symbol)
1677 {
1678 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
1679 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001680 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001681 }
1682 else
1683 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001684 const TString kGlFrontFacing("gl_FrontFacing");
1685 if (*identifier == kGlFrontFacing)
1686 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001687 error(identifierLoc, "identifier should not be declared as invariant",
1688 identifier->c_str());
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001689 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001690 return nullptr;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001691 }
Jamie Madill2c433252014-12-03 12:36:54 -05001692 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001693 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1694 ASSERT(variable);
1695 const TType &type = variable->getType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04001696 TIntermSymbol *intermSymbol =
1697 intermediate.addSymbol(variable->getUniqueId(), *identifier, type, identifierLoc);
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001698
1699 TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc);
1700 aggregate->setOp(EOpInvariantDeclaration);
1701 return aggregate;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001702 }
1703}
1704
Jamie Madillb98c3a82015-07-23 14:26:04 -04001705TIntermAggregate *TParseContext::parseDeclarator(TPublicType &publicType,
1706 TIntermAggregate *aggregateDeclaration,
1707 const TSourceLoc &identifierLocation,
1708 const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001709{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001710 // If the declaration starting this declarator list was empty (example: int,), some checks were
1711 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001712 if (mDeferredSingleDeclarationErrorCheck)
1713 {
1714 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1715 recover();
1716 mDeferredSingleDeclarationErrorCheck = false;
1717 }
1718
Jamie Madill0bd18df2013-06-20 11:55:52 -04001719 if (locationDeclaratorListCheck(identifierLocation, publicType))
1720 recover();
1721
Olli Etuaho376f1b52015-04-13 13:23:41 +03001722 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001723 recover();
1724
Olli Etuaho2935c582015-04-08 14:32:06 +03001725 TVariable *variable = nullptr;
1726 if (!declareVariable(identifierLocation, identifier, TType(publicType), &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001727 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001728
Jamie Madillb98c3a82015-07-23 14:26:04 -04001729 TIntermSymbol *symbol =
1730 intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001731 if (variable && symbol)
Jamie Madill502d66f2013-06-20 11:55:52 -04001732 symbol->setId(variable->getUniqueId());
1733
Olli Etuahoe7847b02015-03-16 11:56:12 +02001734 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001735}
1736
Jamie Madillb98c3a82015-07-23 14:26:04 -04001737TIntermAggregate *TParseContext::parseArrayDeclarator(TPublicType &publicType,
1738 TIntermAggregate *aggregateDeclaration,
1739 const TSourceLoc &identifierLocation,
1740 const TString &identifier,
1741 const TSourceLoc &arrayLocation,
1742 TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001743{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001744 // If the declaration starting this declarator list was empty (example: int,), some checks were
1745 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001746 if (mDeferredSingleDeclarationErrorCheck)
1747 {
1748 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1749 recover();
1750 mDeferredSingleDeclarationErrorCheck = false;
1751 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001752
Jamie Madill0bd18df2013-06-20 11:55:52 -04001753 if (locationDeclaratorListCheck(identifierLocation, publicType))
1754 recover();
1755
Olli Etuaho376f1b52015-04-13 13:23:41 +03001756 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001757 recover();
1758
Jamie Madillb98c3a82015-07-23 14:26:04 -04001759 if (arrayTypeErrorCheck(arrayLocation, publicType) ||
1760 arrayQualifierErrorCheck(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001761 {
1762 recover();
1763 }
Olli Etuaho93a90fd2015-04-07 18:14:07 +03001764 else
Jamie Madill502d66f2013-06-20 11:55:52 -04001765 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001766 TType arrayType = TType(publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04001767 int size;
1768 if (arraySizeErrorCheck(arrayLocation, indexExpression, size))
Olli Etuahoe7847b02015-03-16 11:56:12 +02001769 {
Jamie Madill502d66f2013-06-20 11:55:52 -04001770 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001771 }
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001772 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001773
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001774 TVariable *variable = nullptr;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001775 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001776 recover();
Jamie Madill502d66f2013-06-20 11:55:52 -04001777
Jamie Madillb98c3a82015-07-23 14:26:04 -04001778 TIntermSymbol *symbol =
1779 intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001780 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001781 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001782
1783 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001784 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001785
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001786 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001787}
1788
Jamie Madillb98c3a82015-07-23 14:26:04 -04001789TIntermAggregate *TParseContext::parseInitDeclarator(const TPublicType &publicType,
1790 TIntermAggregate *aggregateDeclaration,
1791 const TSourceLoc &identifierLocation,
1792 const TString &identifier,
1793 const TSourceLoc &initLocation,
1794 TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001795{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001796 // If the declaration starting this declarator list was empty (example: int,), some checks were
1797 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001798 if (mDeferredSingleDeclarationErrorCheck)
1799 {
1800 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1801 recover();
1802 mDeferredSingleDeclarationErrorCheck = false;
1803 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001804
Jamie Madill0bd18df2013-06-20 11:55:52 -04001805 if (locationDeclaratorListCheck(identifierLocation, publicType))
1806 recover();
1807
Olli Etuahoe7847b02015-03-16 11:56:12 +02001808 TIntermNode *intermNode = nullptr;
1809 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04001810 {
1811 //
1812 // build the intermediate representation
1813 //
1814 if (intermNode)
1815 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001816 return intermediate.growAggregate(aggregateDeclaration, intermNode, initLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001817 }
1818 else
1819 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001820 return aggregateDeclaration;
Jamie Madill502d66f2013-06-20 11:55:52 -04001821 }
1822 }
1823 else
1824 {
1825 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001826 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001827 }
1828}
1829
Jamie Madill06145232015-05-13 13:10:01 -04001830TIntermAggregate *TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001831 TIntermAggregate *aggregateDeclaration,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301832 const TSourceLoc &identifierLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001833 const TString &identifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301834 const TSourceLoc &indexLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001835 TIntermTyped *indexExpression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001836 const TSourceLoc &initLocation,
1837 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001838{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001839 // If the declaration starting this declarator list was empty (example: int,), some checks were
1840 // not performed.
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001841 if (mDeferredSingleDeclarationErrorCheck)
1842 {
1843 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1844 recover();
1845 mDeferredSingleDeclarationErrorCheck = false;
1846 }
1847
1848 if (locationDeclaratorListCheck(identifierLocation, publicType))
1849 recover();
1850
Jamie Madillb98c3a82015-07-23 14:26:04 -04001851 if (arrayTypeErrorCheck(indexLocation, publicType) ||
1852 arrayQualifierErrorCheck(indexLocation, publicType))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001853 {
1854 recover();
1855 }
1856
1857 TPublicType arrayType(publicType);
1858
Olli Etuaho376f1b52015-04-13 13:23:41 +03001859 int size = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001860 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
1861 // the initializer.
1862 if (indexExpression != nullptr &&
1863 arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001864 {
1865 recover();
1866 }
1867 // Make the type an array even if size check failed.
1868 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1869 arrayType.setArraySize(size);
1870
1871 // initNode will correspond to the whole of "b[n] = initializer".
1872 TIntermNode *initNode = nullptr;
1873 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1874 {
1875 if (initNode)
1876 {
1877 return intermediate.growAggregate(aggregateDeclaration, initNode, initLocation);
1878 }
1879 else
1880 {
1881 return aggregateDeclaration;
1882 }
1883 }
1884 else
1885 {
1886 recover();
1887 return nullptr;
1888 }
1889}
1890
Jamie Madilla295edf2013-06-06 11:56:48 -04001891void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1892{
1893 if (typeQualifier.qualifier != EvqUniform)
1894 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001895 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
1896 "global layout must be uniform");
Jamie Madilla295edf2013-06-06 11:56:48 -04001897 recover();
1898 return;
1899 }
1900
1901 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04001902
1903 // It should never be the case, but some strange parser errors can send us here.
1904 if (layoutQualifier.isEmpty())
1905 {
1906 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
1907 recover();
1908 return;
1909 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001910
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001911 if (mShaderVersion < 300)
Jamie Madilla295edf2013-06-06 11:56:48 -04001912 {
1913 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1914 recover();
1915 return;
1916 }
1917
1918 if (layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1919 {
1920 recover();
1921 return;
1922 }
1923
Jamie Madill099c0f32013-06-20 11:55:52 -04001924 if (layoutQualifier.matrixPacking != EmpUnspecified)
1925 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001926 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04001927 }
1928
Geoff Langc6856732014-02-11 09:38:55 -05001929 if (layoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04001930 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001931 mDefaultBlockStorage = layoutQualifier.blockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04001932 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001933}
1934
Olli Etuahoee63f5d2016-01-04 11:34:54 +02001935TIntermAggregate *TParseContext::addFunctionPrototypeDeclaration(const TFunction &function,
1936 const TSourceLoc &location)
1937{
Olli Etuaho5d653182016-01-04 14:43:28 +02001938 // Note: symbolTableFunction could be the same as function if this is the first declaration.
1939 // Either way the instance in the symbol table is used to track whether the function is declared
1940 // multiple times.
1941 TFunction *symbolTableFunction =
1942 static_cast<TFunction *>(symbolTable.find(function.getMangledName(), getShaderVersion()));
1943 if (symbolTableFunction->hasPrototypeDeclaration() && mShaderVersion == 100)
1944 {
1945 // ESSL 1.00.17 section 4.2.7.
1946 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
1947 error(location, "duplicate function prototype declarations are not allowed", "function");
1948 recover();
1949 }
1950 symbolTableFunction->setHasPrototypeDeclaration();
1951
Olli Etuahoee63f5d2016-01-04 11:34:54 +02001952 TIntermAggregate *prototype = new TIntermAggregate;
1953 prototype->setType(function.getReturnType());
1954 prototype->setName(function.getMangledName());
1955 prototype->setFunctionId(function.getUniqueId());
1956
1957 for (size_t i = 0; i < function.getParamCount(); i++)
1958 {
1959 const TConstParameter &param = function.getParam(i);
1960 if (param.name != 0)
1961 {
1962 TVariable variable(param.name, *param.type);
1963
1964 TIntermSymbol *paramSymbol = intermediate.addSymbol(
1965 variable.getUniqueId(), variable.getName(), variable.getType(), location);
1966 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
1967 }
1968 else
1969 {
1970 TIntermSymbol *paramSymbol = intermediate.addSymbol(0, "", *param.type, location);
1971 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
1972 }
1973 }
1974
1975 prototype->setOp(EOpPrototype);
1976
1977 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02001978
1979 if (!symbolTable.atGlobalLevel())
1980 {
1981 // ESSL 3.00.4 section 4.2.4.
1982 error(location, "local function prototype declarations are not allowed", "function");
1983 recover();
1984 }
1985
Olli Etuahoee63f5d2016-01-04 11:34:54 +02001986 return prototype;
1987}
1988
1989TIntermAggregate *TParseContext::addFunctionDefinition(const TFunction &function,
1990 TIntermAggregate *functionPrototype,
1991 TIntermAggregate *functionBody,
1992 const TSourceLoc &location)
1993{
1994 //?? Check that all paths return a value if return type != void ?
1995 // May be best done as post process phase on intermediate code
1996 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
1997 {
1998 error(location, "function does not return a value:", "", function.getName().c_str());
1999 recover();
2000 }
2001
2002 TIntermAggregate *aggregate =
2003 intermediate.growAggregate(functionPrototype, functionBody, location);
2004 intermediate.setAggregateOperator(aggregate, EOpFunction, location);
2005 aggregate->setName(function.getMangledName().c_str());
2006 aggregate->setType(function.getReturnType());
2007 aggregate->setFunctionId(function.getUniqueId());
2008
2009 symbolTable.pop();
2010 return aggregate;
2011}
2012
Jamie Madill185fb402015-06-12 15:48:48 -04002013void TParseContext::parseFunctionPrototype(const TSourceLoc &location,
2014 TFunction *function,
2015 TIntermAggregate **aggregateOut)
2016{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002017 const TSymbol *builtIn =
2018 symbolTable.findBuiltIn(function->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002019
2020 if (builtIn)
2021 {
2022 error(location, "built-in functions cannot be redefined", function->getName().c_str());
2023 recover();
2024 }
2025
Jamie Madillb98c3a82015-07-23 14:26:04 -04002026 TFunction *prevDec =
2027 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Jamie Madill185fb402015-06-12 15:48:48 -04002028 //
2029 // Note: 'prevDec' could be 'function' if this is the first time we've seen function
2030 // as it would have just been put in the symbol table. Otherwise, we're looking up
2031 // an earlier occurance.
2032 //
2033 if (prevDec->isDefined())
2034 {
2035 // Then this function already has a body.
2036 error(location, "function already has a body", function->getName().c_str());
2037 recover();
2038 }
2039 prevDec->setDefined();
2040 //
2041 // Overload the unique ID of the definition to be the same unique ID as the declaration.
2042 // Eventually we will probably want to have only a single definition and just swap the
2043 // arguments to be the definition's arguments.
2044 //
2045 function->setUniqueId(prevDec->getUniqueId());
2046
2047 // Raise error message if main function takes any parameters or return anything other than void
2048 if (function->getName() == "main")
2049 {
2050 if (function->getParamCount() > 0)
2051 {
2052 error(location, "function cannot take any parameter(s)", function->getName().c_str());
2053 recover();
2054 }
2055 if (function->getReturnType().getBasicType() != EbtVoid)
2056 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002057 error(location, "", function->getReturnType().getBasicString(),
2058 "main function cannot return a value");
Jamie Madill185fb402015-06-12 15:48:48 -04002059 recover();
2060 }
2061 }
2062
2063 //
2064 // Remember the return type for later checking for RETURN statements.
2065 //
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002066 mCurrentFunctionType = &(prevDec->getReturnType());
2067 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04002068
2069 //
2070 // Insert parameters into the symbol table.
2071 // If the parameter has no name, it's not an error, just don't insert it
2072 // (could be used for unused args).
2073 //
2074 // Also, accumulate the list of parameters into the HIL, so lower level code
2075 // knows where to find parameters.
2076 //
2077 TIntermAggregate *paramNodes = new TIntermAggregate;
2078 for (size_t i = 0; i < function->getParamCount(); i++)
2079 {
2080 const TConstParameter &param = function->getParam(i);
2081 if (param.name != 0)
2082 {
2083 TVariable *variable = new TVariable(param.name, *param.type);
2084 //
2085 // Insert the parameters with name in the symbol table.
2086 //
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002087 if (!symbolTable.declare(variable))
2088 {
Jamie Madill185fb402015-06-12 15:48:48 -04002089 error(location, "redefinition", variable->getName().c_str());
2090 recover();
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002091 paramNodes = intermediate.growAggregate(
2092 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
2093 continue;
Jamie Madill185fb402015-06-12 15:48:48 -04002094 }
2095
2096 //
2097 // Add the parameter to the HIL
2098 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002099 TIntermSymbol *symbol = intermediate.addSymbol(
2100 variable->getUniqueId(), variable->getName(), variable->getType(), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002101
2102 paramNodes = intermediate.growAggregate(paramNodes, symbol, location);
2103 }
2104 else
2105 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002106 paramNodes = intermediate.growAggregate(
2107 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002108 }
2109 }
2110 intermediate.setAggregateOperator(paramNodes, EOpParameters, location);
2111 *aggregateOut = paramNodes;
2112 setLoopNestingLevel(0);
2113}
2114
Jamie Madillb98c3a82015-07-23 14:26:04 -04002115TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002116{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002117 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002118 // We don't know at this point whether this is a function definition or a prototype.
2119 // The definition production code will check for redefinitions.
2120 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002121 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002122 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
2123 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002124 //
2125 TFunction *prevDec =
2126 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302127
2128 if (getShaderVersion() >= 300 && symbolTable.hasUnmangledBuiltIn(function->getName().c_str()))
2129 {
2130 // With ESSL 3.00, names of built-in functions cannot be redeclared as functions.
2131 // Therefore overloading or redefining builtin functions is an error.
2132 error(location, "Name of a built-in function cannot be redeclared as function",
2133 function->getName().c_str());
2134 recover();
2135 }
2136 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002137 {
2138 if (prevDec->getReturnType() != function->getReturnType())
2139 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002140 error(location, "overloaded functions must have the same return type",
Jamie Madill185fb402015-06-12 15:48:48 -04002141 function->getReturnType().getBasicString());
2142 recover();
2143 }
2144 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2145 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002146 if (prevDec->getParam(i).type->getQualifier() !=
2147 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002148 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002149 error(location, "overloaded functions must have the same parameter qualifiers",
Jamie Madill185fb402015-06-12 15:48:48 -04002150 function->getParam(i).type->getQualifierString());
2151 recover();
2152 }
2153 }
2154 }
2155
2156 //
2157 // Check for previously declared variables using the same name.
2158 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002159 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002160 if (prevSym)
2161 {
2162 if (!prevSym->isFunction())
2163 {
2164 error(location, "redefinition", function->getName().c_str(), "function");
2165 recover();
2166 }
2167 }
2168 else
2169 {
2170 // Insert the unmangled name to detect potential future redefinition as a variable.
Jamie Madillb98c3a82015-07-23 14:26:04 -04002171 TFunction *newFunction =
2172 new TFunction(NewPoolTString(function->getName().c_str()), &function->getReturnType());
Jamie Madill185fb402015-06-12 15:48:48 -04002173 symbolTable.getOuterLevel()->insertUnmangled(newFunction);
2174 }
2175
2176 // We're at the inner scope level of the function's arguments and body statement.
2177 // Add the function prototype to the surrounding scope instead.
2178 symbolTable.getOuterLevel()->insert(function);
2179
2180 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002181 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2182 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002183 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2184 //
2185 return function;
2186}
2187
Olli Etuaho9de84a52016-06-14 17:36:01 +03002188TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
2189 const TString *name,
2190 const TSourceLoc &location)
2191{
2192 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
2193 {
2194 error(location, "no qualifiers allowed for function return",
2195 getQualifierString(type.qualifier));
2196 recover();
2197 }
2198 if (!type.layoutQualifier.isEmpty())
2199 {
2200 error(location, "no qualifiers allowed for function return", "layout");
2201 recover();
2202 }
2203 // make sure a sampler is not involved as well...
2204 if (samplerErrorCheck(location, type, "samplers can't be function return values"))
2205 {
2206 recover();
2207 }
Olli Etuahoe29324f2016-06-15 10:58:03 +03002208 if (mShaderVersion < 300)
2209 {
2210 // Array return values are forbidden, but there's also no valid syntax for declaring array
2211 // return values in ESSL 1.00.
2212 ASSERT(type.arraySize == 0 || mDiagnostics.numErrors() > 0);
2213
2214 if (type.isStructureContainingArrays())
2215 {
2216 // ESSL 1.00.17 section 6.1 Function Definitions
2217 error(location, "structures containing arrays can't be function return values",
2218 TType(type).getCompleteString().c_str());
2219 recover();
2220 }
2221 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03002222
2223 // Add the function as a prototype after parsing it (we do not support recursion)
2224 return new TFunction(name, new TType(type));
2225}
2226
Jamie Madill06145232015-05-13 13:10:01 -04002227TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002228{
Jamie Madill06145232015-05-13 13:10:01 -04002229 TPublicType publicType = publicTypeIn;
Olli Etuahobd163f62015-11-13 12:15:38 +02002230 if (publicType.isStructSpecifier)
2231 {
2232 error(publicType.line, "constructor can't be a structure definition",
2233 getBasicString(publicType.type));
2234 recover();
2235 }
2236
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002237 TOperator op = EOpNull;
2238 if (publicType.userDef)
2239 {
2240 op = EOpConstructStruct;
2241 }
2242 else
2243 {
2244 switch (publicType.type)
2245 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002246 case EbtFloat:
2247 if (publicType.isMatrix())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002248 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002249 switch (publicType.getCols())
Alexis Hetu07e57df2015-06-16 16:55:52 -04002250 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002251 case 2:
2252 switch (publicType.getRows())
2253 {
2254 case 2:
2255 op = EOpConstructMat2;
2256 break;
2257 case 3:
2258 op = EOpConstructMat2x3;
2259 break;
2260 case 4:
2261 op = EOpConstructMat2x4;
2262 break;
2263 }
2264 break;
2265 case 3:
2266 switch (publicType.getRows())
2267 {
2268 case 2:
2269 op = EOpConstructMat3x2;
2270 break;
2271 case 3:
2272 op = EOpConstructMat3;
2273 break;
2274 case 4:
2275 op = EOpConstructMat3x4;
2276 break;
2277 }
2278 break;
2279 case 4:
2280 switch (publicType.getRows())
2281 {
2282 case 2:
2283 op = EOpConstructMat4x2;
2284 break;
2285 case 3:
2286 op = EOpConstructMat4x3;
2287 break;
2288 case 4:
2289 op = EOpConstructMat4;
2290 break;
2291 }
2292 break;
Alexis Hetu07e57df2015-06-16 16:55:52 -04002293 }
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002294 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04002295 else
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002296 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002297 switch (publicType.getNominalSize())
2298 {
2299 case 1:
2300 op = EOpConstructFloat;
2301 break;
2302 case 2:
2303 op = EOpConstructVec2;
2304 break;
2305 case 3:
2306 op = EOpConstructVec3;
2307 break;
2308 case 4:
2309 op = EOpConstructVec4;
2310 break;
2311 }
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002312 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04002313 break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002314
Jamie Madillb98c3a82015-07-23 14:26:04 -04002315 case EbtInt:
2316 switch (publicType.getNominalSize())
2317 {
2318 case 1:
2319 op = EOpConstructInt;
2320 break;
2321 case 2:
2322 op = EOpConstructIVec2;
2323 break;
2324 case 3:
2325 op = EOpConstructIVec3;
2326 break;
2327 case 4:
2328 op = EOpConstructIVec4;
2329 break;
2330 }
2331 break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002332
Jamie Madillb98c3a82015-07-23 14:26:04 -04002333 case EbtUInt:
2334 switch (publicType.getNominalSize())
2335 {
2336 case 1:
2337 op = EOpConstructUInt;
2338 break;
2339 case 2:
2340 op = EOpConstructUVec2;
2341 break;
2342 case 3:
2343 op = EOpConstructUVec3;
2344 break;
2345 case 4:
2346 op = EOpConstructUVec4;
2347 break;
2348 }
2349 break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002350
Jamie Madillb98c3a82015-07-23 14:26:04 -04002351 case EbtBool:
2352 switch (publicType.getNominalSize())
2353 {
2354 case 1:
2355 op = EOpConstructBool;
2356 break;
2357 case 2:
2358 op = EOpConstructBVec2;
2359 break;
2360 case 3:
2361 op = EOpConstructBVec3;
2362 break;
2363 case 4:
2364 op = EOpConstructBVec4;
2365 break;
2366 }
2367 break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002368
Jamie Madillb98c3a82015-07-23 14:26:04 -04002369 default:
2370 break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002371 }
2372
2373 if (op == EOpNull)
2374 {
2375 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
2376 recover();
2377 publicType.type = EbtFloat;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002378 op = EOpConstructFloat;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002379 }
2380 }
2381
2382 TString tempString;
Dmitry Skiba7f17a502015-06-22 15:08:39 -07002383 const TType *type = new TType(publicType);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002384 return new TFunction(&tempString, type, op);
2385}
2386
Jamie Madillb98c3a82015-07-23 14:26:04 -04002387// This function is used to test for the correctness of the parameters passed to various constructor
2388// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002389//
2390// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
2391//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002392TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments,
2393 TType *type,
2394 TOperator op,
2395 TFunction *fnCall,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302396 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002397{
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002398 TIntermAggregate *constructor = arguments->getAsAggregate();
2399 ASSERT(constructor != nullptr);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002400
Olli Etuahof40319e2015-03-10 14:33:00 +02002401 if (type->isArray())
2402 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002403 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
2404 // the array.
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002405 TIntermSequence *args = constructor->getSequence();
Olli Etuahof40319e2015-03-10 14:33:00 +02002406 for (size_t i = 0; i < args->size(); i++)
2407 {
2408 const TType &argType = (*args)[i]->getAsTyped()->getType();
2409 // It has already been checked that the argument is not an array.
2410 ASSERT(!argType.isArray());
2411 if (!argType.sameElementType(*type))
2412 {
2413 error(line, "Array constructor argument has an incorrect type", "Error");
2414 recover();
2415 return nullptr;
2416 }
2417 }
2418 }
2419 else if (op == EOpConstructStruct)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002420 {
2421 const TFieldList &fields = type->getStruct()->fields();
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002422 TIntermSequence *args = constructor->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002423
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002424 for (size_t i = 0; i < fields.size(); i++)
2425 {
Nicolas Capensffd73872014-08-21 13:49:16 -04002426 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002427 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002428 error(line, "Structure constructor arguments do not match structure fields",
2429 "Error");
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002430 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002431
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002432 return 0;
2433 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002434 }
2435 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002436
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002437 // Turn the argument list itself into a constructor
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002438 constructor->setOp(op);
2439 constructor->setLine(line);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002440 ASSERT(constructor->isConstructor());
2441
2442 // Need to set type before setPrecisionFromChildren() because bool doesn't have precision.
2443 constructor->setType(*type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002444
Olli Etuaho21203702014-11-13 16:16:21 +02002445 // Structs should not be precision qualified, the individual members may be.
2446 // Built-in types on the other hand should be precision qualified.
2447 if (op != EOpConstructStruct)
2448 {
2449 constructor->setPrecisionFromChildren();
2450 type->setPrecision(constructor->getPrecision());
2451 }
2452
Olli Etuaho1d122782015-11-06 15:35:17 +02002453 TIntermTyped *constConstructor = intermediate.foldAggregateBuiltIn(constructor);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002454 if (constConstructor)
2455 {
2456 return constConstructor;
2457 }
2458
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002459 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002460}
2461
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002462//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002463// This function returns the tree representation for the vector field(s) being accessed from contant
2464// vector.
2465// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a
2466// contant node is returned, else an aggregate node is returned (for v.xy). The input to this
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002467// function could either be the symbol node or it could be the intermediate tree representation of
2468// accessing fields in a constant structure or column of a constant matrix.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002469//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002470TIntermTyped *TParseContext::addConstVectorNode(TVectorFields &fields,
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002471 TIntermConstantUnion *node,
2472 const TSourceLoc &line,
2473 bool outOfRangeIndexIsError)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002474{
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002475 const TConstantUnion *unionArray = node->getUnionArrayPointer();
2476 ASSERT(unionArray);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002477
Arun Patole7e7e68d2015-05-22 12:02:25 +05302478 TConstantUnion *constArray = new TConstantUnion[fields.num];
Jamie Madillc2128ff2016-07-04 10:26:17 -04002479 const auto &type = node->getType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002480
Arun Patole7e7e68d2015-05-22 12:02:25 +05302481 for (int i = 0; i < fields.num; i++)
2482 {
Jamie Madillc2128ff2016-07-04 10:26:17 -04002483 if (fields.offsets[i] >= type.getNominalSize())
Arun Patole7e7e68d2015-05-22 12:02:25 +05302484 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002485 std::stringstream extraInfoStream;
2486 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
2487 std::string extraInfo = extraInfoStream.str();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002488 outOfRangeError(outOfRangeIndexIsError, line, "", "[", extraInfo.c_str());
Jamie Madillc2128ff2016-07-04 10:26:17 -04002489 fields.offsets[i] = type.getNominalSize() - 1;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002490 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302491
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002492 constArray[i] = unionArray[fields.offsets[i]];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302493 }
Jamie Madillc2128ff2016-07-04 10:26:17 -04002494 return intermediate.addConstantUnion(constArray, type, line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002495}
2496
2497//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002498// This function returns the column being accessed from a constant matrix. The values are retrieved
2499// from the symbol table and parse-tree is built for a vector (each column of a matrix is a vector).
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002500// The input to the function could either be a symbol node (m[0] where m is a constant matrix)that
2501// represents a constant matrix or it could be the tree representation of the constant matrix
2502// (s.m1[0] where s is a constant structure)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002503//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002504TIntermTyped *TParseContext::addConstMatrixNode(int index,
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002505 TIntermConstantUnion *node,
2506 const TSourceLoc &line,
2507 bool outOfRangeIndexIsError)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002508{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302509 if (index >= node->getType().getCols())
2510 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002511 std::stringstream extraInfoStream;
2512 extraInfoStream << "matrix field selection out of range '" << index << "'";
2513 std::string extraInfo = extraInfoStream.str();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002514 outOfRangeError(outOfRangeIndexIsError, line, "", "[", extraInfo.c_str());
2515 index = node->getType().getCols() - 1;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002516 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002517
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002518 const TConstantUnion *unionArray = node->getUnionArrayPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002519 int size = node->getType().getCols();
2520 return intermediate.addConstantUnion(&unionArray[size * index], node->getType(), line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002521}
2522
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002523//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002524// This function returns an element of an array accessed from a constant array. The values are
2525// retrieved from the symbol table and parse-tree is built for the type of the element. The input
Arun Patole7e7e68d2015-05-22 12:02:25 +05302526// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
Jamie Madillb98c3a82015-07-23 14:26:04 -04002527// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a
2528// constant structure)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002529//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002530TIntermTyped *TParseContext::addConstArrayNode(int index,
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002531 TIntermConstantUnion *node,
2532 const TSourceLoc &line,
2533 bool outOfRangeIndexIsError)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002534{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002535 TType arrayElementType = node->getType();
2536 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002537
Arun Patole7e7e68d2015-05-22 12:02:25 +05302538 if (index >= node->getType().getArraySize())
2539 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002540 std::stringstream extraInfoStream;
2541 extraInfoStream << "array field selection out of range '" << index << "'";
2542 std::string extraInfo = extraInfoStream.str();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002543 outOfRangeError(outOfRangeIndexIsError, line, "", "[", extraInfo.c_str());
2544 index = node->getType().getArraySize() - 1;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002545 }
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002546 size_t arrayElementSize = arrayElementType.getObjectSize();
2547 const TConstantUnion *unionArray = node->getUnionArrayPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002548 return intermediate.addConstantUnion(&unionArray[arrayElementSize * index], node->getType(),
2549 line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002550}
2551
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002552//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002553// This function returns the value of a particular field inside a constant structure from the symbol
2554// table.
2555// If there is an embedded/nested struct, it appropriately calls addConstStructNested or
2556// addConstStructFromAggr function and returns the parse-tree with the values of the embedded/nested
2557// struct.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002558//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002559TIntermTyped *TParseContext::addConstStruct(const TString &identifier,
2560 TIntermTyped *node,
2561 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002562{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302563 const TFieldList &fields = node->getType().getStruct()->fields();
Jamie Madillb98c3a82015-07-23 14:26:04 -04002564 size_t instanceSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002565
Arun Patole7e7e68d2015-05-22 12:02:25 +05302566 for (size_t index = 0; index < fields.size(); ++index)
2567 {
2568 if (fields[index]->name() == identifier)
2569 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002570 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302571 }
2572 else
2573 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002574 instanceSize += fields[index]->type()->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002575 }
2576 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002577
Jamie Madill94bf7f22013-07-08 13:31:15 -04002578 TIntermTyped *typedNode;
2579 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
Arun Patole7e7e68d2015-05-22 12:02:25 +05302580 if (tempConstantNode)
2581 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002582 const TConstantUnion *constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002583
Jamie Madillb98c3a82015-07-23 14:26:04 -04002584 // type will be changed in the calling function
2585 typedNode = intermediate.addConstantUnion(constArray + instanceSize,
2586 tempConstantNode->getType(), line);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302587 }
2588 else
2589 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002590 error(line, "Cannot offset into the structure", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002591 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002592
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002593 return 0;
2594 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002595
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002596 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002597}
2598
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002599//
2600// Interface/uniform blocks
2601//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002602TIntermAggregate *TParseContext::addInterfaceBlock(const TPublicType &typeQualifier,
2603 const TSourceLoc &nameLine,
2604 const TString &blockName,
2605 TFieldList *fieldList,
2606 const TString *instanceName,
2607 const TSourceLoc &instanceLine,
2608 TIntermTyped *arrayIndex,
2609 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002610{
2611 if (reservedErrorCheck(nameLine, blockName))
2612 recover();
2613
2614 if (typeQualifier.qualifier != EvqUniform)
2615 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302616 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
2617 "interface blocks must be uniform");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002618 recover();
2619 }
2620
Jamie Madill099c0f32013-06-20 11:55:52 -04002621 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
2622 if (layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002623 {
2624 recover();
2625 }
2626
Jamie Madill099c0f32013-06-20 11:55:52 -04002627 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2628 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002629 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002630 }
2631
Jamie Madill1566ef72013-06-20 11:55:54 -04002632 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2633 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002634 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002635 }
2636
Arun Patole7e7e68d2015-05-22 12:02:25 +05302637 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2638 if (!symbolTable.declare(blockNameSymbol))
2639 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002640 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
2641 recover();
2642 }
2643
Jamie Madill98493dd2013-07-08 14:39:03 -04002644 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302645 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2646 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002647 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302648 TType *fieldType = field->type();
2649 if (IsSampler(fieldType->getBasicType()))
2650 {
2651 error(field->line(), "unsupported type", fieldType->getBasicString(),
2652 "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002653 recover();
2654 }
2655
Jamie Madill98493dd2013-07-08 14:39:03 -04002656 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002657 switch (qualifier)
2658 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002659 case EvqGlobal:
2660 case EvqUniform:
2661 break;
2662 default:
2663 error(field->line(), "invalid qualifier on interface block member",
2664 getQualifierString(qualifier));
2665 recover();
2666 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002667 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002668
2669 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002670 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
2671 if (layoutLocationErrorCheck(field->line(), fieldLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002672 {
2673 recover();
2674 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002675
Jamie Madill98493dd2013-07-08 14:39:03 -04002676 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002677 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002678 error(field->line(), "invalid layout qualifier:",
2679 getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04002680 recover();
2681 }
2682
Jamie Madill98493dd2013-07-08 14:39:03 -04002683 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002684 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002685 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002686 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002687 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002688 {
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002689 warning(field->line(), "extraneous layout qualifier:",
Jamie Madillb98c3a82015-07-23 14:26:04 -04002690 getMatrixPackingString(fieldLayoutQualifier.matrixPacking),
2691 "only has an effect on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04002692 }
2693
Jamie Madill98493dd2013-07-08 14:39:03 -04002694 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002695 }
2696
Jamie Madill98493dd2013-07-08 14:39:03 -04002697 // add array index
2698 int arraySize = 0;
2699 if (arrayIndex != NULL)
2700 {
2701 if (arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize))
2702 recover();
2703 }
2704
Jamie Madillb98c3a82015-07-23 14:26:04 -04002705 TInterfaceBlock *interfaceBlock =
2706 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2707 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
2708 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002709
2710 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04002711 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002712
Jamie Madill98493dd2013-07-08 14:39:03 -04002713 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002714 {
2715 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002716 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2717 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002718 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302719 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002720
2721 // set parent pointer of the field variable
2722 fieldType->setInterfaceBlock(interfaceBlock);
2723
Arun Patole7e7e68d2015-05-22 12:02:25 +05302724 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002725 fieldVariable->setQualifier(typeQualifier.qualifier);
2726
Arun Patole7e7e68d2015-05-22 12:02:25 +05302727 if (!symbolTable.declare(fieldVariable))
2728 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002729 error(field->line(), "redefinition", field->name().c_str(),
2730 "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002731 recover();
2732 }
2733 }
2734 }
2735 else
2736 {
Olli Etuahoe0f623a2015-07-10 11:58:30 +03002737 if (reservedErrorCheck(instanceLine, *instanceName))
2738 recover();
2739
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002740 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302741 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002742 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002743
Arun Patole7e7e68d2015-05-22 12:02:25 +05302744 if (!symbolTable.declare(instanceTypeDef))
2745 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002746 error(instanceLine, "redefinition", instanceName->c_str(),
2747 "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002748 recover();
2749 }
2750
Jamie Madillb98c3a82015-07-23 14:26:04 -04002751 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002752 symbolName = instanceTypeDef->getName();
2753 }
2754
Jamie Madillb98c3a82015-07-23 14:26:04 -04002755 TIntermAggregate *aggregate = intermediate.makeAggregate(
2756 intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line),
2757 nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002758 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04002759
2760 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002761 return aggregate;
2762}
2763
Arun Patole7e7e68d2015-05-22 12:02:25 +05302764bool TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002765{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002766 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002767
2768 // Embedded structure definitions are not supported per GLSL ES spec.
2769 // They aren't allowed in GLSL either, but we need to detect this here
2770 // so we don't rely on the GLSL compiler to catch it.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302771 if (mStructNestingLevel > 1)
2772 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002773 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002774 return true;
2775 }
2776
2777 return false;
2778}
2779
2780void TParseContext::exitStructDeclaration()
2781{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002782 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002783}
2784
Jamie Madillb98c3a82015-07-23 14:26:04 -04002785namespace
2786{
kbr@chromium.org476541f2011-10-27 21:14:51 +00002787const int kWebGLMaxStructNesting = 4;
2788
2789} // namespace
2790
Arun Patole7e7e68d2015-05-22 12:02:25 +05302791bool TParseContext::structNestingErrorCheck(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002792{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302793 if (!IsWebGLBasedSpec(mShaderSpec))
2794 {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002795 return false;
2796 }
2797
Arun Patole7e7e68d2015-05-22 12:02:25 +05302798 if (field.type()->getBasicType() != EbtStruct)
2799 {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002800 return false;
2801 }
2802
2803 // We're already inside a structure definition at this point, so add
2804 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302805 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2806 {
Jamie Madill41a49272014-03-18 16:10:13 -04002807 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002808 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
2809 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04002810 std::string reason = reasonStream.str();
2811 error(line, reason.c_str(), field.name().c_str(), "");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002812 return true;
2813 }
2814
2815 return false;
2816}
2817
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002818//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002819// Parse an array index expression
2820//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002821TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
2822 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302823 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002824{
2825 TIntermTyped *indexedExpression = NULL;
2826
2827 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2828 {
2829 if (baseExpression->getAsSymbolNode())
2830 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302831 error(location, " left of '[' is not of type array, matrix, or vector ",
2832 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002833 }
2834 else
2835 {
2836 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2837 }
2838 recover();
2839 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002840
Jamie Madill21c1e452014-12-29 11:33:41 -05002841 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2842
Olli Etuaho36b05142015-11-12 13:10:42 +02002843 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
2844 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
2845 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
2846 // index is a constant expression.
2847 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
2848 {
2849 if (baseExpression->isInterfaceBlock())
2850 {
2851 error(
2852 location, "", "[",
2853 "array indexes for interface blocks arrays must be constant integral expressions");
2854 recover();
2855 }
2856 else if (baseExpression->getQualifier() == EvqFragmentOut)
2857 {
2858 error(location, "", "[",
2859 "array indexes for fragment outputs must be constant integral expressions");
2860 recover();
2861 }
Olli Etuaho3e960462015-11-12 15:58:39 +02002862 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
2863 {
2864 error(location, "", "[", "array index for gl_FragData must be constant zero");
2865 recover();
2866 }
Olli Etuaho36b05142015-11-12 13:10:42 +02002867 }
2868
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002869 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002870 {
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002871 // If the index is not qualified as constant, the behavior in the spec is undefined. This
2872 // applies even if ANGLE has been able to constant fold it (ANGLE may constant fold
2873 // expressions that are not constant expressions). The most compatible way to handle this
2874 // case is to report a warning instead of an error and force the index to be in the
2875 // correct range.
2876 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Jamie Madill21c1e452014-12-29 11:33:41 -05002877 int index = indexConstantUnion->getIConst(0);
Jamie Madill7164cf42013-07-08 13:30:59 -04002878 if (index < 0)
2879 {
2880 std::stringstream infoStream;
2881 infoStream << index;
2882 std::string info = infoStream.str();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002883 outOfRangeError(outOfRangeIndexIsError, location, "negative index", info.c_str());
Jamie Madill7164cf42013-07-08 13:30:59 -04002884 index = 0;
2885 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002886 TIntermConstantUnion *baseConstantUnion = baseExpression->getAsConstantUnion();
2887 if (baseConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002888 {
2889 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002890 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002891 // constant folding for array indexing
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002892 indexedExpression =
2893 addConstArrayNode(index, baseConstantUnion, location, outOfRangeIndexIsError);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002894 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002895 else if (baseExpression->isVector())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002896 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002897 // constant folding for vector indexing
Jamie Madill7164cf42013-07-08 13:30:59 -04002898 TVectorFields fields;
2899 fields.num = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002900 fields.offsets[0] =
2901 index; // need to do it this way because v.xy sends fields integer array
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002902 indexedExpression =
2903 addConstVectorNode(fields, baseConstantUnion, location, outOfRangeIndexIsError);
Jamie Madill7164cf42013-07-08 13:30:59 -04002904 }
2905 else if (baseExpression->isMatrix())
2906 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002907 // constant folding for matrix indexing
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002908 indexedExpression =
2909 addConstMatrixNode(index, baseConstantUnion, location, outOfRangeIndexIsError);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002910 }
2911 }
2912 else
2913 {
Jamie Madillb11e2482015-05-04 14:21:22 -04002914 int safeIndex = -1;
2915
Jamie Madill7164cf42013-07-08 13:30:59 -04002916 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002917 {
Olli Etuaho3e960462015-11-12 15:58:39 +02002918 if (baseExpression->getQualifier() == EvqFragData && index > 0)
2919 {
2920 if (mShaderSpec == SH_WEBGL2_SPEC)
2921 {
2922 // Error has been already generated if index is not const.
2923 if (indexExpression->getQualifier() == EvqConst)
2924 {
2925 error(location, "", "[",
2926 "array index for gl_FragData must be constant zero");
2927 recover();
2928 }
2929 safeIndex = 0;
2930 }
2931 else if (!isExtensionEnabled("GL_EXT_draw_buffers"))
2932 {
2933 outOfRangeError(outOfRangeIndexIsError, location, "", "[",
2934 "array index for gl_FragData must be zero when "
2935 "GL_EXT_draw_buffers is disabled");
2936 safeIndex = 0;
2937 }
2938 }
2939 // Only do generic out-of-range check if similar error hasn't already been reported.
2940 if (safeIndex < 0 && index >= baseExpression->getType().getArraySize())
Jamie Madill7164cf42013-07-08 13:30:59 -04002941 {
2942 std::stringstream extraInfoStream;
2943 extraInfoStream << "array index out of range '" << index << "'";
2944 std::string extraInfo = extraInfoStream.str();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002945 outOfRangeError(outOfRangeIndexIsError, location, "", "[", extraInfo.c_str());
Jamie Madillb11e2482015-05-04 14:21:22 -04002946 safeIndex = baseExpression->getType().getArraySize() - 1;
Jamie Madill7164cf42013-07-08 13:30:59 -04002947 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002948 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302949 else if ((baseExpression->isVector() || baseExpression->isMatrix()) &&
Jamie Madillb98c3a82015-07-23 14:26:04 -04002950 baseExpression->getType().getNominalSize() <= index)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002951 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002952 std::stringstream extraInfoStream;
2953 extraInfoStream << "field selection out of range '" << index << "'";
2954 std::string extraInfo = extraInfoStream.str();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002955 outOfRangeError(outOfRangeIndexIsError, location, "", "[", extraInfo.c_str());
Jamie Madillb11e2482015-05-04 14:21:22 -04002956 safeIndex = baseExpression->getType().getNominalSize() - 1;
Jamie Madill46131a32013-06-20 11:55:50 -04002957 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002958
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002959 // Data of constant unions can't be changed, because it may be shared with other
2960 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
2961 // sanitized object.
Jamie Madillb11e2482015-05-04 14:21:22 -04002962 if (safeIndex != -1)
2963 {
2964 TConstantUnion *safeConstantUnion = new TConstantUnion();
2965 safeConstantUnion->setIConst(safeIndex);
2966 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
2967 }
2968
Jamie Madillb98c3a82015-07-23 14:26:04 -04002969 indexedExpression =
2970 intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002971 }
2972 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002973 else
2974 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002975 indexedExpression =
2976 intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
Jamie Madill7164cf42013-07-08 13:30:59 -04002977 }
2978
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002979 if (indexedExpression == 0)
2980 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002981 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002982 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002983 indexedExpression =
2984 intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002985 }
2986 else if (baseExpression->isArray())
2987 {
Olli Etuahob3fbd862015-09-30 17:55:02 +03002988 TType indexedType = baseExpression->getType();
2989 indexedType.clearArrayness();
2990 indexedExpression->setType(indexedType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002991 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002992 else if (baseExpression->isMatrix())
2993 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002994 indexedExpression->setType(TType(baseExpression->getBasicType(),
Olli Etuahob3fbd862015-09-30 17:55:02 +03002995 baseExpression->getPrecision(), EvqTemporary,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002996 static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002997 }
2998 else if (baseExpression->isVector())
2999 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003000 indexedExpression->setType(
Olli Etuahob3fbd862015-09-30 17:55:02 +03003001 TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003002 }
3003 else
3004 {
3005 indexedExpression->setType(baseExpression->getType());
3006 }
3007
Olli Etuahob3fbd862015-09-30 17:55:02 +03003008 if (baseExpression->getType().getQualifier() == EvqConst &&
3009 indexExpression->getType().getQualifier() == EvqConst)
3010 {
3011 indexedExpression->getTypePointer()->setQualifier(EvqConst);
3012 }
3013 else
3014 {
3015 indexedExpression->getTypePointer()->setQualifier(EvqTemporary);
3016 }
3017
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003018 return indexedExpression;
3019}
3020
Jamie Madillb98c3a82015-07-23 14:26:04 -04003021TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3022 const TSourceLoc &dotLocation,
3023 const TString &fieldString,
3024 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003025{
3026 TIntermTyped *indexedExpression = NULL;
3027
3028 if (baseExpression->isArray())
3029 {
3030 error(fieldLocation, "cannot apply dot operator to an array", ".");
3031 recover();
3032 }
3033
3034 if (baseExpression->isVector())
3035 {
3036 TVectorFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003037 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields,
3038 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003039 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003040 fields.num = 1;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003041 fields.offsets[0] = 0;
3042 recover();
3043 }
3044
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003045 if (baseExpression->getAsConstantUnion())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003046 {
3047 // constant folding for vector fields
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003048 indexedExpression = addConstVectorNode(fields, baseExpression->getAsConstantUnion(),
3049 fieldLocation, true);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003050 }
3051 else
3052 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303053 TIntermTyped *index = intermediate.addSwizzle(fields, fieldLocation);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003054 indexedExpression =
3055 intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003056 }
3057 if (indexedExpression == nullptr)
3058 {
3059 recover();
3060 indexedExpression = baseExpression;
3061 }
3062 else
3063 {
3064 // Note that the qualifier set here will be corrected later.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003065 indexedExpression->setType(TType(baseExpression->getBasicType(),
3066 baseExpression->getPrecision(), EvqTemporary,
Jamie Madillc2128ff2016-07-04 10:26:17 -04003067 static_cast<unsigned char>(fields.num)));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003068 }
3069 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003070 else if (baseExpression->getBasicType() == EbtStruct)
3071 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003072 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05303073 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003074 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003075 {
3076 error(dotLocation, "structure has no fields", "Internal Error");
3077 recover();
3078 indexedExpression = baseExpression;
3079 }
3080 else
3081 {
3082 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003083 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003084 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003085 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003086 {
3087 fieldFound = true;
3088 break;
3089 }
3090 }
3091 if (fieldFound)
3092 {
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003093 if (baseExpression->getAsConstantUnion())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003094 {
3095 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
3096 if (indexedExpression == 0)
3097 {
3098 recover();
3099 indexedExpression = baseExpression;
3100 }
3101 else
3102 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003103 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003104 }
3105 }
3106 else
3107 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003108 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003109 unionArray->setIConst(i);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003110 TIntermTyped *index = intermediate.addConstantUnion(
3111 unionArray, *fields[i]->type(), fieldLocation);
3112 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression,
3113 index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04003114 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003115 }
3116 }
3117 else
3118 {
3119 error(dotLocation, " no such field in structure", fieldString.c_str());
3120 recover();
3121 indexedExpression = baseExpression;
3122 }
3123 }
3124 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003125 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003126 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003127 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05303128 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003129 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003130 {
3131 error(dotLocation, "interface block has no fields", "Internal Error");
3132 recover();
3133 indexedExpression = baseExpression;
3134 }
3135 else
3136 {
3137 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003138 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003139 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003140 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003141 {
3142 fieldFound = true;
3143 break;
3144 }
3145 }
3146 if (fieldFound)
3147 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003148 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003149 unionArray->setIConst(i);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003150 TIntermTyped *index =
3151 intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
3152 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock,
3153 baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04003154 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003155 }
3156 else
3157 {
3158 error(dotLocation, " no such field in interface block", fieldString.c_str());
3159 recover();
3160 indexedExpression = baseExpression;
3161 }
3162 }
3163 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003164 else
3165 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003166 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003167 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03003168 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303169 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003170 }
3171 else
3172 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303173 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003174 " field selection requires structure, vector, or interface block on left hand "
3175 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303176 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003177 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003178 recover();
3179 indexedExpression = baseExpression;
3180 }
3181
Olli Etuahob1edc4f2015-11-02 17:20:03 +02003182 if (baseExpression->getQualifier() == EvqConst)
3183 {
3184 indexedExpression->getTypePointer()->setQualifier(EvqConst);
3185 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003186 else
3187 {
3188 indexedExpression->getTypePointer()->setQualifier(EvqTemporary);
3189 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02003190
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003191 return indexedExpression;
3192}
3193
Jamie Madillb98c3a82015-07-23 14:26:04 -04003194TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3195 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003196{
Jamie Madilla5efff92013-06-06 11:56:47 -04003197 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003198
Jamie Madillb98c3a82015-07-23 14:26:04 -04003199 qualifier.location = -1;
Jamie Madilla5efff92013-06-06 11:56:47 -04003200 qualifier.matrixPacking = EmpUnspecified;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003201 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003202
3203 if (qualifierType == "shared")
3204 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003205 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003206 }
3207 else if (qualifierType == "packed")
3208 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003209 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003210 }
3211 else if (qualifierType == "std140")
3212 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003213 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003214 }
3215 else if (qualifierType == "row_major")
3216 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003217 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003218 }
3219 else if (qualifierType == "column_major")
3220 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003221 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003222 }
3223 else if (qualifierType == "location")
3224 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003225 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
3226 "location requires an argument");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003227 recover();
3228 }
3229 else
3230 {
3231 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
3232 recover();
3233 }
3234
Jamie Madilla5efff92013-06-06 11:56:47 -04003235 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003236}
3237
Jamie Madillb98c3a82015-07-23 14:26:04 -04003238TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3239 const TSourceLoc &qualifierTypeLine,
3240 const TString &intValueString,
3241 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303242 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003243{
Jamie Madilla5efff92013-06-06 11:56:47 -04003244 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003245
Jamie Madillb98c3a82015-07-23 14:26:04 -04003246 qualifier.location = -1;
Jamie Madilla5efff92013-06-06 11:56:47 -04003247 qualifier.matrixPacking = EmpUnspecified;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003248 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003249
3250 if (qualifierType != "location")
3251 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303252 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
3253 "only location may have arguments");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003254 recover();
3255 }
3256 else
3257 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003258 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003259 if (intValue < 0)
3260 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003261 error(intValueLine, "out of range:", intValueString.c_str(),
3262 "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003263 recover();
3264 }
3265 else
3266 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003267 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003268 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003269 }
3270
Jamie Madilla5efff92013-06-06 11:56:47 -04003271 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003272}
3273
Jamie Madillb98c3a82015-07-23 14:26:04 -04003274TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
3275 TLayoutQualifier rightQualifier)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003276{
Jamie Madilla5efff92013-06-06 11:56:47 -04003277 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003278
Jamie Madilla5efff92013-06-06 11:56:47 -04003279 if (rightQualifier.location != -1)
3280 {
3281 joinedQualifier.location = rightQualifier.location;
3282 }
3283 if (rightQualifier.matrixPacking != EmpUnspecified)
3284 {
3285 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
3286 }
3287 if (rightQualifier.blockStorage != EbsUnspecified)
3288 {
3289 joinedQualifier.blockStorage = rightQualifier.blockStorage;
3290 }
3291
3292 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003293}
3294
Arun Patole7e7e68d2015-05-22 12:02:25 +05303295TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc,
3296 TQualifier interpolationQualifier,
3297 const TSourceLoc &storageLoc,
3298 TQualifier storageQualifier)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003299{
3300 TQualifier mergedQualifier = EvqSmoothIn;
3301
Arun Patole7e7e68d2015-05-22 12:02:25 +05303302 if (storageQualifier == EvqFragmentIn)
3303 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003304 if (interpolationQualifier == EvqSmooth)
3305 mergedQualifier = EvqSmoothIn;
3306 else if (interpolationQualifier == EvqFlat)
3307 mergedQualifier = EvqFlatIn;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003308 else
3309 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003310 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303311 else if (storageQualifier == EvqCentroidIn)
3312 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003313 if (interpolationQualifier == EvqSmooth)
3314 mergedQualifier = EvqCentroidIn;
3315 else if (interpolationQualifier == EvqFlat)
3316 mergedQualifier = EvqFlatIn;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003317 else
3318 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003319 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303320 else if (storageQualifier == EvqVertexOut)
3321 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003322 if (interpolationQualifier == EvqSmooth)
3323 mergedQualifier = EvqSmoothOut;
3324 else if (interpolationQualifier == EvqFlat)
3325 mergedQualifier = EvqFlatOut;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003326 else
3327 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003328 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303329 else if (storageQualifier == EvqCentroidOut)
3330 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003331 if (interpolationQualifier == EvqSmooth)
3332 mergedQualifier = EvqCentroidOut;
3333 else if (interpolationQualifier == EvqFlat)
3334 mergedQualifier = EvqFlatOut;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003335 else
3336 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003337 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303338 else
3339 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003340 error(interpolationLoc,
3341 "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303342 getInterpolationString(interpolationQualifier));
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003343 recover();
3344
3345 mergedQualifier = storageQualifier;
3346 }
3347
3348 TPublicType type;
3349 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
3350 return type;
3351}
3352
Jamie Madillb98c3a82015-07-23 14:26:04 -04003353TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3354 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003355{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03003356 if (voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier.type))
3357 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003358 recover();
3359 }
3360
Arun Patole7e7e68d2015-05-22 12:02:25 +05303361 for (unsigned int i = 0; i < fieldList->size(); ++i)
3362 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003363 //
3364 // Careful not to replace already known aspects of type, like array-ness
3365 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303366 TType *type = (*fieldList)[i]->type();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003367 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003368 type->setPrimarySize(typeSpecifier.primarySize);
3369 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003370 type->setPrecision(typeSpecifier.precision);
3371 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003372 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003373
3374 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303375 if (type->isArray())
3376 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003377 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
3378 recover();
3379 }
3380 if (typeSpecifier.array)
3381 type->setArraySize(typeSpecifier.arraySize);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303382 if (typeSpecifier.userDef)
3383 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003384 type->setStruct(typeSpecifier.userDef->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003385 }
3386
Arun Patole7e7e68d2015-05-22 12:02:25 +05303387 if (structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i]))
3388 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003389 recover();
3390 }
3391 }
3392
Jamie Madill98493dd2013-07-08 14:39:03 -04003393 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003394}
3395
Jamie Madillb98c3a82015-07-23 14:26:04 -04003396TPublicType TParseContext::addStructure(const TSourceLoc &structLine,
3397 const TSourceLoc &nameLine,
3398 const TString *structName,
3399 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003400{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303401 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003402 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003403
Jamie Madill9b820842015-02-12 10:40:10 -05003404 // Store a bool in the struct if we're at global scope, to allow us to
3405 // skip the local struct scoping workaround in HLSL.
Jamie Madillb960cc42015-02-12 15:33:20 +00003406 structure->setUniqueId(TSymbolTable::nextUniqueId());
Jamie Madill9b820842015-02-12 10:40:10 -05003407 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003408
Jamie Madill98493dd2013-07-08 14:39:03 -04003409 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003410 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003411 if (reservedErrorCheck(nameLine, *structName))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003412 {
3413 recover();
3414 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303415 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
3416 if (!symbolTable.declare(userTypeDef))
3417 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003418 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003419 recover();
3420 }
3421 }
3422
3423 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04003424 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003425 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003426 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04003427 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003428 switch (qualifier)
3429 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003430 case EvqGlobal:
3431 case EvqTemporary:
3432 break;
3433 default:
3434 error(field.line(), "invalid qualifier on struct member",
3435 getQualifierString(qualifier));
3436 recover();
3437 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003438 }
3439 }
3440
3441 TPublicType publicType;
3442 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003443 publicType.userDef = structureType;
Olli Etuahobd163f62015-11-13 12:15:38 +02003444 publicType.isStructSpecifier = true;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003445 exitStructDeclaration();
3446
3447 return publicType;
3448}
3449
Jamie Madillb98c3a82015-07-23 14:26:04 -04003450TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
3451 TIntermAggregate *statementList,
3452 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02003453{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003454 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003455 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02003456 init->isVector())
3457 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003458 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
3459 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003460 recover();
3461 return nullptr;
3462 }
3463
Olli Etuahoac5274d2015-02-20 10:19:08 +02003464 if (statementList)
3465 {
3466 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
3467 {
3468 recover();
3469 return nullptr;
3470 }
3471 }
3472
Olli Etuahoa3a36662015-02-17 13:46:51 +02003473 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3474 if (node == nullptr)
3475 {
3476 error(loc, "erroneous switch statement", "switch");
3477 recover();
3478 return nullptr;
3479 }
3480 return node;
3481}
3482
3483TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3484{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003485 if (mSwitchNestingLevel == 0)
3486 {
3487 error(loc, "case labels need to be inside switch statements", "case");
3488 recover();
3489 return nullptr;
3490 }
3491 if (condition == nullptr)
3492 {
3493 error(loc, "case label must have a condition", "case");
3494 recover();
3495 return nullptr;
3496 }
3497 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04003498 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02003499 {
3500 error(condition->getLine(), "case label must be a scalar integer", "case");
3501 recover();
3502 }
3503 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003504 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
3505 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
3506 // fold in case labels.
3507 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02003508 {
3509 error(condition->getLine(), "case label must be constant", "case");
3510 recover();
3511 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003512 TIntermCase *node = intermediate.addCase(condition, loc);
3513 if (node == nullptr)
3514 {
3515 error(loc, "erroneous case statement", "case");
3516 recover();
3517 return nullptr;
3518 }
3519 return node;
3520}
3521
3522TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3523{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003524 if (mSwitchNestingLevel == 0)
3525 {
3526 error(loc, "default labels need to be inside switch statements", "default");
3527 recover();
3528 return nullptr;
3529 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003530 TIntermCase *node = intermediate.addCase(nullptr, loc);
3531 if (node == nullptr)
3532 {
3533 error(loc, "erroneous default statement", "default");
3534 recover();
3535 return nullptr;
3536 }
3537 return node;
3538}
3539
Jamie Madillb98c3a82015-07-23 14:26:04 -04003540TIntermTyped *TParseContext::createUnaryMath(TOperator op,
3541 TIntermTyped *child,
3542 const TSourceLoc &loc,
3543 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003544{
3545 if (child == nullptr)
3546 {
3547 return nullptr;
3548 }
3549
3550 switch (op)
3551 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003552 case EOpLogicalNot:
3553 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
3554 child->isVector())
3555 {
3556 return nullptr;
3557 }
3558 break;
3559 case EOpBitwiseNot:
3560 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3561 child->isMatrix() || child->isArray())
3562 {
3563 return nullptr;
3564 }
3565 break;
3566 case EOpPostIncrement:
3567 case EOpPreIncrement:
3568 case EOpPostDecrement:
3569 case EOpPreDecrement:
3570 case EOpNegative:
3571 case EOpPositive:
3572 if (child->getBasicType() == EbtStruct || child->getBasicType() == EbtBool ||
3573 child->isArray())
3574 {
3575 return nullptr;
3576 }
3577 // Operators for built-ins are already type checked against their prototype.
3578 default:
3579 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003580 }
3581
Olli Etuahof6c694b2015-03-26 14:50:53 +02003582 return intermediate.addUnaryMath(op, child, loc, funcReturnType);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003583}
3584
Olli Etuaho09b22472015-02-11 11:47:26 +02003585TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3586{
Olli Etuahof6c694b2015-03-26 14:50:53 +02003587 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003588 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003589 {
3590 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
3591 recover();
3592 return child;
3593 }
3594 return node;
3595}
3596
Jamie Madillb98c3a82015-07-23 14:26:04 -04003597TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
3598 TIntermTyped *child,
3599 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003600{
3601 if (lValueErrorCheck(loc, GetOperatorString(op), child))
3602 recover();
3603 return addUnaryMath(op, child, loc);
3604}
3605
Jamie Madillb98c3a82015-07-23 14:26:04 -04003606bool TParseContext::binaryOpCommonCheck(TOperator op,
3607 TIntermTyped *left,
3608 TIntermTyped *right,
3609 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003610{
3611 if (left->isArray() || right->isArray())
3612 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003613 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003614 {
3615 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3616 return false;
3617 }
3618
3619 if (left->isArray() != right->isArray())
3620 {
3621 error(loc, "array / non-array mismatch", GetOperatorString(op));
3622 return false;
3623 }
3624
3625 switch (op)
3626 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003627 case EOpEqual:
3628 case EOpNotEqual:
3629 case EOpAssign:
3630 case EOpInitialize:
3631 break;
3632 default:
3633 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3634 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02003635 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003636 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003637 if (left->getArraySize() != right->getArraySize())
3638 {
3639 error(loc, "array size mismatch", GetOperatorString(op));
3640 return false;
3641 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003642 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003643
3644 // Check ops which require integer / ivec parameters
3645 bool isBitShift = false;
3646 switch (op)
3647 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003648 case EOpBitShiftLeft:
3649 case EOpBitShiftRight:
3650 case EOpBitShiftLeftAssign:
3651 case EOpBitShiftRightAssign:
3652 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3653 // check that the basic type is an integer type.
3654 isBitShift = true;
3655 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3656 {
3657 return false;
3658 }
3659 break;
3660 case EOpBitwiseAnd:
3661 case EOpBitwiseXor:
3662 case EOpBitwiseOr:
3663 case EOpBitwiseAndAssign:
3664 case EOpBitwiseXorAssign:
3665 case EOpBitwiseOrAssign:
3666 // It is enough to check the type of only one operand, since later it
3667 // is checked that the operand types match.
3668 if (!IsInteger(left->getBasicType()))
3669 {
3670 return false;
3671 }
3672 break;
3673 default:
3674 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003675 }
3676
3677 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3678 // So the basic type should usually match.
3679 if (!isBitShift && left->getBasicType() != right->getBasicType())
3680 {
3681 return false;
3682 }
3683
Olli Etuaho9dd217b2015-03-20 14:24:31 +02003684 // Check that type sizes match exactly on ops that require that.
Olli Etuahoff699002015-03-23 14:38:42 +02003685 // Also check restrictions for structs that contain arrays or samplers.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003686 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003687 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003688 case EOpAssign:
3689 case EOpInitialize:
3690 case EOpEqual:
3691 case EOpNotEqual:
3692 // ESSL 1.00 sections 5.7, 5.8, 5.9
3693 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
3694 {
3695 error(loc, "undefined operation for structs containing arrays",
3696 GetOperatorString(op));
3697 return false;
3698 }
3699 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3700 // we interpret the spec so that this extends to structs containing samplers,
3701 // similarly to ESSL 1.00 spec.
3702 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
3703 left->getType().isStructureContainingSamplers())
3704 {
3705 error(loc, "undefined operation for structs containing samplers",
3706 GetOperatorString(op));
3707 return false;
3708 }
3709 case EOpLessThan:
3710 case EOpGreaterThan:
3711 case EOpLessThanEqual:
3712 case EOpGreaterThanEqual:
3713 if ((left->getNominalSize() != right->getNominalSize()) ||
3714 (left->getSecondarySize() != right->getSecondarySize()))
3715 {
3716 return false;
3717 }
3718 default:
3719 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003720 }
3721
Olli Etuahod6b14282015-03-17 14:31:35 +02003722 return true;
3723}
3724
Jamie Madillb98c3a82015-07-23 14:26:04 -04003725TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
3726 TIntermTyped *left,
3727 TIntermTyped *right,
3728 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02003729{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003730 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003731 return nullptr;
3732
Olli Etuahofc1806e2015-03-17 13:03:11 +02003733 switch (op)
3734 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003735 case EOpEqual:
3736 case EOpNotEqual:
3737 break;
3738 case EOpLessThan:
3739 case EOpGreaterThan:
3740 case EOpLessThanEqual:
3741 case EOpGreaterThanEqual:
3742 ASSERT(!left->isArray() && !right->isArray());
3743 if (left->isMatrix() || left->isVector() || left->getBasicType() == EbtStruct)
3744 {
3745 return nullptr;
3746 }
3747 break;
3748 case EOpLogicalOr:
3749 case EOpLogicalXor:
3750 case EOpLogicalAnd:
3751 ASSERT(!left->isArray() && !right->isArray());
3752 if (left->getBasicType() != EbtBool || left->isMatrix() || left->isVector())
3753 {
3754 return nullptr;
3755 }
3756 break;
3757 case EOpAdd:
3758 case EOpSub:
3759 case EOpDiv:
3760 case EOpMul:
3761 ASSERT(!left->isArray() && !right->isArray());
3762 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool)
3763 {
3764 return nullptr;
3765 }
3766 break;
3767 case EOpIMod:
3768 ASSERT(!left->isArray() && !right->isArray());
3769 // Note that this is only for the % operator, not for mod()
3770 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool ||
3771 left->getBasicType() == EbtFloat)
3772 {
3773 return nullptr;
3774 }
3775 break;
3776 // Note that for bitwise ops, type checking is done in promote() to
3777 // share code between ops and compound assignment
3778 default:
3779 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003780 }
3781
Olli Etuahofc1806e2015-03-17 13:03:11 +02003782 return intermediate.addBinaryMath(op, left, right, loc);
3783}
3784
Jamie Madillb98c3a82015-07-23 14:26:04 -04003785TIntermTyped *TParseContext::addBinaryMath(TOperator op,
3786 TIntermTyped *left,
3787 TIntermTyped *right,
3788 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003789{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003790 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003791 if (node == 0)
3792 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003793 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3794 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003795 recover();
3796 return left;
3797 }
3798 return node;
3799}
3800
Jamie Madillb98c3a82015-07-23 14:26:04 -04003801TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
3802 TIntermTyped *left,
3803 TIntermTyped *right,
3804 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003805{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003806 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003807 if (node == 0)
3808 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003809 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3810 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003811 recover();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003812 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02003813 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003814 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
3815 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003816 }
3817 return node;
3818}
3819
Jamie Madillb98c3a82015-07-23 14:26:04 -04003820TIntermTyped *TParseContext::createAssign(TOperator op,
3821 TIntermTyped *left,
3822 TIntermTyped *right,
3823 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003824{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003825 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003826 {
3827 return intermediate.addAssign(op, left, right, loc);
3828 }
3829 return nullptr;
3830}
3831
Jamie Madillb98c3a82015-07-23 14:26:04 -04003832TIntermTyped *TParseContext::addAssign(TOperator op,
3833 TIntermTyped *left,
3834 TIntermTyped *right,
3835 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003836{
3837 TIntermTyped *node = createAssign(op, left, right, loc);
3838 if (node == nullptr)
3839 {
3840 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
3841 recover();
3842 return left;
3843 }
3844 return node;
3845}
3846
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003847TIntermTyped *TParseContext::addComma(TIntermTyped *left,
3848 TIntermTyped *right,
3849 const TSourceLoc &loc)
3850{
Olli Etuaho15200042015-11-04 16:56:31 +02003851 return intermediate.addComma(left, right, loc, mShaderVersion);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003852}
3853
Olli Etuaho49300862015-02-20 14:54:49 +02003854TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3855{
3856 switch (op)
3857 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003858 case EOpContinue:
3859 if (mLoopNestingLevel <= 0)
3860 {
3861 error(loc, "continue statement only allowed in loops", "");
3862 recover();
3863 }
3864 break;
3865 case EOpBreak:
3866 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
3867 {
3868 error(loc, "break statement only allowed in loops and switch statements", "");
3869 recover();
3870 }
3871 break;
3872 case EOpReturn:
3873 if (mCurrentFunctionType->getBasicType() != EbtVoid)
3874 {
3875 error(loc, "non-void function must return a value", "return");
3876 recover();
3877 }
3878 break;
3879 default:
3880 // No checks for discard
3881 break;
Olli Etuaho49300862015-02-20 14:54:49 +02003882 }
3883 return intermediate.addBranch(op, loc);
3884}
3885
Jamie Madillb98c3a82015-07-23 14:26:04 -04003886TIntermBranch *TParseContext::addBranch(TOperator op,
3887 TIntermTyped *returnValue,
3888 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02003889{
3890 ASSERT(op == EOpReturn);
3891 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003892 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02003893 {
3894 error(loc, "void function cannot return a value", "return");
3895 recover();
3896 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003897 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02003898 {
3899 error(loc, "function return is not matching type:", "return");
3900 recover();
3901 }
3902 return intermediate.addBranch(op, returnValue, loc);
3903}
3904
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003905void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
3906{
3907 ASSERT(!functionCall->isUserDefined());
3908 const TString &name = functionCall->getName();
3909 TIntermNode *offset = nullptr;
3910 TIntermSequence *arguments = functionCall->getSequence();
3911 if (name.compare(0, 16, "texelFetchOffset") == 0 ||
3912 name.compare(0, 16, "textureLodOffset") == 0 ||
3913 name.compare(0, 20, "textureProjLodOffset") == 0 ||
3914 name.compare(0, 17, "textureGradOffset") == 0 ||
3915 name.compare(0, 21, "textureProjGradOffset") == 0)
3916 {
3917 offset = arguments->back();
3918 }
3919 else if (name.compare(0, 13, "textureOffset") == 0 ||
3920 name.compare(0, 17, "textureProjOffset") == 0)
3921 {
3922 // A bias parameter might follow the offset parameter.
3923 ASSERT(arguments->size() >= 3);
3924 offset = (*arguments)[2];
3925 }
3926 if (offset != nullptr)
3927 {
3928 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
3929 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
3930 {
3931 TString unmangledName = TFunction::unmangleName(name);
3932 error(functionCall->getLine(), "Texture offset must be a constant expression",
3933 unmangledName.c_str());
3934 recover();
3935 }
3936 else
3937 {
3938 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
3939 size_t size = offsetConstantUnion->getType().getObjectSize();
3940 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
3941 for (size_t i = 0u; i < size; ++i)
3942 {
3943 int offsetValue = values[i].getIConst();
3944 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
3945 {
3946 std::stringstream tokenStream;
3947 tokenStream << offsetValue;
3948 std::string token = tokenStream.str();
3949 error(offset->getLine(), "Texture offset value out of valid range",
3950 token.c_str());
3951 recover();
3952 }
3953 }
3954 }
3955 }
3956}
3957
Jamie Madillb98c3a82015-07-23 14:26:04 -04003958TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
3959 TIntermNode *paramNode,
3960 TIntermNode *thisNode,
3961 const TSourceLoc &loc,
3962 bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003963{
Jamie Madillb98c3a82015-07-23 14:26:04 -04003964 *fatalError = false;
3965 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003966 TIntermTyped *callNode = nullptr;
3967
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003968 if (thisNode != nullptr)
3969 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003970 TConstantUnion *unionArray = new TConstantUnion[1];
Jamie Madillb98c3a82015-07-23 14:26:04 -04003971 int arraySize = 0;
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003972 TIntermTyped *typedThis = thisNode->getAsTyped();
3973 if (fnCall->getName() != "length")
3974 {
3975 error(loc, "invalid method", fnCall->getName().c_str());
3976 recover();
3977 }
3978 else if (paramNode != nullptr)
3979 {
3980 error(loc, "method takes no parameters", "length");
3981 recover();
3982 }
3983 else if (typedThis == nullptr || !typedThis->isArray())
3984 {
3985 error(loc, "length can only be called on arrays", "length");
3986 recover();
3987 }
3988 else
3989 {
Olli Etuaho96e67382015-04-23 14:27:02 +03003990 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03003991 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003992 {
Olli Etuaho39282e12015-04-23 15:41:48 +03003993 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003994 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03003995 // (func()).length()
3996 // (int[3](0, 1, 2)).length()
Jamie Madillb98c3a82015-07-23 14:26:04 -04003997 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
3998 // expression.
3999 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
4000 // spec section 5.9 which allows "An array, vector or matrix expression with the
4001 // length method applied".
4002 error(loc, "length can only be called on array names, not on array expressions",
4003 "length");
Olli Etuaho39282e12015-04-23 15:41:48 +03004004 recover();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004005 }
4006 }
Olli Etuaho96e67382015-04-23 14:27:02 +03004007 unionArray->setIConst(arraySize);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004008 callNode =
4009 intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004010 }
4011 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004012 {
4013 //
4014 // Then this should be a constructor.
4015 // Don't go through the symbol table for constructors.
4016 // Their parameters will be verified algorithmically.
4017 //
4018 TType type(EbtVoid, EbpUndefined); // use this to get the type back
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004019 if (!constructorErrorCheck(loc, paramNode, *fnCall, op, &type))
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004020 {
4021 //
4022 // It's a constructor, of type 'type'.
4023 //
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004024 callNode = addConstructor(paramNode, &type, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004025 }
Olli Etuaho72ba85b2015-03-04 14:23:26 +02004026
4027 if (callNode == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004028 {
4029 recover();
4030 callNode = intermediate.setAggregateOperator(nullptr, op, loc);
4031 }
4032 callNode->setType(type);
4033 }
4034 else
4035 {
4036 //
4037 // Not a constructor. Find it in the symbol table.
4038 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05304039 const TFunction *fnCandidate;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004040 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004041 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004042 if (fnCandidate)
4043 {
4044 //
4045 // A declared function.
4046 //
4047 if (builtIn && !fnCandidate->getExtension().empty() &&
4048 extensionErrorCheck(loc, fnCandidate->getExtension()))
4049 {
4050 recover();
4051 }
4052 op = fnCandidate->getBuiltInOp();
4053 if (builtIn && op != EOpNull)
4054 {
4055 //
4056 // A function call mapped to a built-in operation.
4057 //
4058 if (fnCandidate->getParamCount() == 1)
4059 {
4060 //
4061 // Treat it like a built-in unary operator.
4062 //
Olli Etuaho15c2ac32015-11-09 15:51:43 +02004063 TIntermAggregate *paramAgg = paramNode->getAsAggregate();
4064 paramNode = paramAgg->getSequence()->front();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004065 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc,
4066 &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004067 if (callNode == nullptr)
4068 {
4069 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004070 extraInfoStream
4071 << "built in unary operator function. Type: "
4072 << static_cast<TIntermTyped *>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004073 std::string extraInfo = extraInfoStream.str();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004074 error(paramNode->getLine(), " wrong operand type", "Internal Error",
4075 extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004076 *fatalError = true;
4077 return nullptr;
4078 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004079 }
4080 else
4081 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004082 TIntermAggregate *aggregate =
4083 intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004084 aggregate->setType(fnCandidate->getReturnType());
4085 aggregate->setPrecisionFromChildren();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02004086 if (aggregate->areChildrenConstQualified())
4087 {
4088 aggregate->getTypePointer()->setQualifier(EvqConst);
4089 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004090
4091 // Some built-in functions have out parameters too.
4092 functionCallLValueErrorCheck(fnCandidate, aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05304093
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004094 // See if we can constant fold a built-in. Note that this may be possible even
4095 // if it is not const-qualified.
Olli Etuahob43846e2015-06-02 18:18:57 +03004096 TIntermTyped *foldedNode = intermediate.foldAggregateBuiltIn(aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05304097 if (foldedNode)
4098 {
Arun Patole274f0702015-05-05 13:33:30 +05304099 callNode = foldedNode;
4100 }
Olli Etuahob43846e2015-06-02 18:18:57 +03004101 else
4102 {
4103 callNode = aggregate;
4104 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004105 }
4106 }
4107 else
4108 {
4109 // This is a real function call
Jamie Madillb98c3a82015-07-23 14:26:04 -04004110 TIntermAggregate *aggregate =
4111 intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004112 aggregate->setType(fnCandidate->getReturnType());
4113
Jamie Madillb98c3a82015-07-23 14:26:04 -04004114 // this is how we know whether the given function is a builtIn function or a user
4115 // defined function
4116 // if builtIn == false, it's a userDefined -> could be an overloaded
4117 // builtIn function also
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004118 // if builtIn == true, it's definitely a builtIn function with EOpNull
4119 if (!builtIn)
4120 aggregate->setUserDefined();
4121 aggregate->setName(fnCandidate->getMangledName());
Corentin Wallez71d147f2015-02-11 11:15:24 -08004122 aggregate->setFunctionId(fnCandidate->getUniqueId());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004123
4124 // This needs to happen after the name is set
4125 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004126 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004127 aggregate->setBuiltInFunctionPrecision();
4128
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004129 checkTextureOffsetConst(aggregate);
4130 }
4131
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004132 callNode = aggregate;
4133
4134 functionCallLValueErrorCheck(fnCandidate, aggregate);
4135 }
4136 }
4137 else
4138 {
4139 // error message was put out by findFunction()
4140 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004141 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004142 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004143 callNode = intermediate.addConstantUnion(unionArray,
4144 TType(EbtFloat, EbpUndefined, EvqConst), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004145 recover();
4146 }
4147 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004148 return callNode;
4149}
4150
Jamie Madillb98c3a82015-07-23 14:26:04 -04004151TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
4152 TIntermTyped *trueBlock,
4153 TIntermTyped *falseBlock,
Olli Etuaho52901742015-04-15 13:42:45 +03004154 const TSourceLoc &loc)
4155{
4156 if (boolErrorCheck(loc, cond))
4157 recover();
4158
4159 if (trueBlock->getType() != falseBlock->getType())
4160 {
4161 binaryOpError(loc, ":", trueBlock->getCompleteString(), falseBlock->getCompleteString());
4162 recover();
4163 return falseBlock;
4164 }
Olli Etuahoa2d53032015-04-15 14:14:44 +03004165 // ESSL1 sections 5.2 and 5.7:
4166 // ESSL3 section 5.7:
4167 // Ternary operator is not among the operators allowed for structures/arrays.
4168 if (trueBlock->isArray() || trueBlock->getBasicType() == EbtStruct)
4169 {
4170 error(loc, "ternary operator is not allowed for structures or arrays", ":");
4171 recover();
4172 return falseBlock;
4173 }
Olli Etuaho52901742015-04-15 13:42:45 +03004174 return intermediate.addSelection(cond, trueBlock, falseBlock, loc);
4175}
Olli Etuaho49300862015-02-20 14:54:49 +02004176
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004177//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004178// Parse an array of strings using yyparse.
4179//
4180// Returns 0 for success.
4181//
Jamie Madillb98c3a82015-07-23 14:26:04 -04004182int PaParseStrings(size_t count,
4183 const char *const string[],
4184 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05304185 TParseContext *context)
4186{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004187 if ((count == 0) || (string == NULL))
4188 return 1;
4189
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004190 if (glslang_initialize(context))
4191 return 1;
4192
alokp@chromium.org408c45e2012-04-05 15:54:43 +00004193 int error = glslang_scan(count, string, length, context);
4194 if (!error)
4195 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004196
alokp@chromium.org73bc2982012-06-19 18:48:05 +00004197 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00004198
alokp@chromium.org6b495712012-06-29 00:06:58 +00004199 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004200}