blob: 8e4d487662ee037149e17b05bd50f4532f85a27e [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;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400229 switch (type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530230 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400231 case EbtFloat:
232 if (precision == EbpUndefined)
233 {
234 error(line, "No precision specified for (float)", "");
235 return true;
236 }
237 break;
238 case EbtInt:
239 if (precision == EbpUndefined)
240 {
241 error(line, "No precision specified (int)", "");
242 return true;
243 }
244 break;
245 default:
246 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000247 }
248 return false;
249}
250
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000251//
252// Both test and if necessary, spit out an error, to see if the node is really
253// an l-value that can be operated on this way.
254//
255// Returns true if the was an error.
256//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530257bool TParseContext::lValueErrorCheck(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000258{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400259 TIntermSymbol *symNode = node->getAsSymbolNode();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530260 TIntermBinary *binaryNode = node->getAsBinaryNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000261
Arun Patole7e7e68d2015-05-22 12:02:25 +0530262 if (binaryNode)
263 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000264 bool errorReturn;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000265
Jamie Madillb98c3a82015-07-23 14:26:04 -0400266 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530267 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400268 case EOpIndexDirect:
269 case EOpIndexIndirect:
270 case EOpIndexDirectStruct:
271 case EOpIndexDirectInterfaceBlock:
272 return lValueErrorCheck(line, op, binaryNode->getLeft());
273 case EOpVectorSwizzle:
274 errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft());
275 if (!errorReturn)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530276 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400277 int offset[4] = {0, 0, 0, 0};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000278
Jamie Madillb98c3a82015-07-23 14:26:04 -0400279 TIntermTyped *rightNode = binaryNode->getRight();
280 TIntermAggregate *aggrNode = rightNode->getAsAggregate();
281
282 for (TIntermSequence::iterator p = aggrNode->getSequence()->begin();
283 p != aggrNode->getSequence()->end(); p++)
284 {
285 int value = (*p)->getAsTyped()->getAsConstantUnion()->getIConst(0);
286 offset[value]++;
287 if (offset[value] > 1)
288 {
289 error(line, " l-value of swizzle cannot have duplicate components", op);
290
291 return true;
292 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000293 }
294 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000295
Jamie Madillb98c3a82015-07-23 14:26:04 -0400296 return errorReturn;
297 default:
298 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000299 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000300 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000301
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000302 return true;
303 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000304
Arun Patole7e7e68d2015-05-22 12:02:25 +0530305 const char *symbol = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000306 if (symNode != 0)
307 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000308
Arun Patole7e7e68d2015-05-22 12:02:25 +0530309 const char *message = 0;
310 switch (node->getQualifier())
311 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400312 case EvqConst:
313 message = "can't modify a const";
314 break;
315 case EvqConstReadOnly:
316 message = "can't modify a const";
317 break;
318 case EvqAttribute:
319 message = "can't modify an attribute";
320 break;
321 case EvqFragmentIn:
322 message = "can't modify an input";
323 break;
324 case EvqVertexIn:
325 message = "can't modify an input";
326 break;
327 case EvqUniform:
328 message = "can't modify a uniform";
329 break;
330 case EvqVaryingIn:
331 message = "can't modify a varying";
332 break;
333 case EvqFragCoord:
334 message = "can't modify gl_FragCoord";
335 break;
336 case EvqFrontFacing:
337 message = "can't modify gl_FrontFacing";
338 break;
339 case EvqPointCoord:
340 message = "can't modify gl_PointCoord";
341 break;
342 default:
343 //
344 // Type that can't be written to?
345 //
346 if (node->getBasicType() == EbtVoid)
347 {
348 message = "can't modify void";
349 }
350 if (IsSampler(node->getBasicType()))
351 {
352 message = "can't modify a sampler";
353 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000354 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000355
Arun Patole7e7e68d2015-05-22 12:02:25 +0530356 if (message == 0 && binaryNode == 0 && symNode == 0)
357 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000358 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000359
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000360 return true;
361 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000362
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000363 //
364 // Everything else is okay, no error.
365 //
366 if (message == 0)
367 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000368
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000369 //
370 // If we get here, we have an error and a message.
371 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530372 if (symNode)
373 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000374 std::stringstream extraInfoStream;
375 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
376 std::string extraInfo = extraInfoStream.str();
377 error(line, " l-value required", op, extraInfo.c_str());
378 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530379 else
380 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000381 std::stringstream extraInfoStream;
382 extraInfoStream << "(" << message << ")";
383 std::string extraInfo = extraInfoStream.str();
384 error(line, " l-value required", op, extraInfo.c_str());
385 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000386
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000387 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000388}
389
390//
391// Both test, and if necessary spit out an error, to see if the node is really
392// a constant.
393//
394// Returns true if the was an error.
395//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530396bool TParseContext::constErrorCheck(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000397{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000398 if (node->getQualifier() == EvqConst)
399 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000400
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000401 error(node->getLine(), "constant expression required", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000402
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000403 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000404}
405
406//
407// Both test, and if necessary spit out an error, to see if the node is really
408// an integer.
409//
410// Returns true if the was an error.
411//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530412bool TParseContext::integerErrorCheck(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000413{
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000414 if (node->isScalarInt())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000415 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000416
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000417 error(node->getLine(), "integer expression required", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000418
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000419 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000420}
421
422//
423// Both test, and if necessary spit out an error, to see if we are currently
424// globally scoped.
425//
426// Returns true if the was an error.
427//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530428bool TParseContext::globalErrorCheck(const TSourceLoc &line, bool global, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000429{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000430 if (global)
431 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000432
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000433 error(line, "only allowed at global scope", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000434
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000435 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000436}
437
438//
439// For now, keep it simple: if it starts "gl_", it's reserved, independent
440// of scope. Except, if the symbol table is at the built-in push-level,
441// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000442// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
443// webgl shader.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000444//
445// Returns true if there was an error.
446//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530447bool TParseContext::reservedErrorCheck(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000448{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530449 static const char *reservedErrMsg = "reserved built-in name";
450 if (!symbolTable.atBuiltInLevel())
451 {
452 if (identifier.compare(0, 3, "gl_") == 0)
453 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000454 error(line, reservedErrMsg, "gl_");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000455 return true;
456 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530457 if (IsWebGLBasedSpec(mShaderSpec))
458 {
459 if (identifier.compare(0, 6, "webgl_") == 0)
460 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000461 error(line, reservedErrMsg, "webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000462 return true;
463 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530464 if (identifier.compare(0, 7, "_webgl_") == 0)
465 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000466 error(line, reservedErrMsg, "_webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000467 return true;
468 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530469 if (mShaderSpec == SH_CSS_SHADERS_SPEC && identifier.compare(0, 4, "css_") == 0)
470 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000471 error(line, reservedErrMsg, "css_");
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000472 return true;
473 }
alokp@chromium.org613ef312010-07-21 18:54:22 +0000474 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530475 if (identifier.find("__") != TString::npos)
476 {
477 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400478 "identifiers containing two consecutive underscores (__) are reserved as "
479 "possible future keywords",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530480 identifier.c_str());
daniel@transgaming.combeadd5d2012-04-12 02:35:31 +0000481 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000482 }
483 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000484
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000485 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000486}
487
488//
489// Make sure there is enough data provided to the constructor to build
490// something of the type of the constructor. Also returns the type of
491// the constructor.
492//
493// Returns true if there was an error in construction.
494//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400495bool TParseContext::constructorErrorCheck(const TSourceLoc &line,
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200496 TIntermNode *argumentsNode,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400497 TFunction &function,
498 TOperator op,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530499 TType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000500{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000501 *type = function.getReturnType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000502
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000503 bool constructingMatrix = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400504 switch (op)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530505 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400506 case EOpConstructMat2:
507 case EOpConstructMat2x3:
508 case EOpConstructMat2x4:
509 case EOpConstructMat3x2:
510 case EOpConstructMat3:
511 case EOpConstructMat3x4:
512 case EOpConstructMat4x2:
513 case EOpConstructMat4x3:
514 case EOpConstructMat4:
515 constructingMatrix = true;
516 break;
517 default:
518 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000519 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000520
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000521 //
522 // Note: It's okay to have too many components available, but not okay to have unused
523 // arguments. 'full' will go to true when enough args have been seen. If we loop
524 // again, there is an extra argument, so 'overfull' will become true.
525 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000526
Jamie Madillb98c3a82015-07-23 14:26:04 -0400527 size_t size = 0;
528 bool constType = true;
529 bool full = false;
530 bool overFull = false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000531 bool matrixInMatrix = false;
532 bool arrayArg = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530533 for (size_t i = 0; i < function.getParamCount(); ++i)
534 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700535 const TConstParameter &param = function.getParam(i);
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000536 size += param.type->getObjectSize();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530537
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000538 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000539 matrixInMatrix = true;
540 if (full)
541 overFull = true;
542 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
543 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000544 if (param.type->getQualifier() != EvqConst)
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000545 constType = false;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000546 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000547 arrayArg = true;
548 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530549
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000550 if (constType)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000551 type->setQualifier(EvqConst);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000552
Olli Etuaho376f1b52015-04-13 13:23:41 +0300553 if (type->isArray())
554 {
555 if (type->isUnsizedArray())
556 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700557 type->setArraySize(static_cast<int>(function.getParamCount()));
Olli Etuaho376f1b52015-04-13 13:23:41 +0300558 }
559 else if (static_cast<size_t>(type->getArraySize()) != function.getParamCount())
560 {
561 error(line, "array constructor needs one argument per array element", "constructor");
562 return true;
563 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000564 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000565
Arun Patole7e7e68d2015-05-22 12:02:25 +0530566 if (arrayArg && op != EOpConstructStruct)
567 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000568 error(line, "constructing from a non-dereferenced array", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000569 return true;
570 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000571
Arun Patole7e7e68d2015-05-22 12:02:25 +0530572 if (matrixInMatrix && !type->isArray())
573 {
574 if (function.getParamCount() != 1)
575 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400576 error(line, "constructing matrix from matrix can only take one argument",
577 "constructor");
578 return true;
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000579 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000580 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000581
Arun Patole7e7e68d2015-05-22 12:02:25 +0530582 if (overFull)
583 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000584 error(line, "too many arguments", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000585 return true;
586 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530587
Jamie Madillb98c3a82015-07-23 14:26:04 -0400588 if (op == EOpConstructStruct && !type->isArray() &&
589 type->getStruct()->fields().size() != function.getParamCount())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530590 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400591 error(line,
592 "Number of constructor parameters does not match the number of structure fields",
593 "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000594 return true;
595 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000596
Arun Patole7e7e68d2015-05-22 12:02:25 +0530597 if (!type->isMatrix() || !matrixInMatrix)
598 {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000599 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
Arun Patole7e7e68d2015-05-22 12:02:25 +0530600 (op == EOpConstructStruct && size < type->getObjectSize()))
601 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000602 error(line, "not enough data provided for construction", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000603 return true;
604 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000605 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000606
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200607 if (argumentsNode == nullptr)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530608 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200609 error(line, "constructor does not have any arguments", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000610 return true;
611 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200612
613 TIntermAggregate *argumentsAgg = argumentsNode->getAsAggregate();
614 for (TIntermNode *&argNode : *argumentsAgg->getSequence())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530615 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200616 TIntermTyped *argTyped = argNode->getAsTyped();
617 ASSERT(argTyped != nullptr);
618 if (op != EOpConstructStruct && IsSampler(argTyped->getBasicType()))
619 {
620 error(line, "cannot convert a sampler", "constructor");
621 return true;
622 }
623 if (argTyped->getBasicType() == EbtVoid)
624 {
625 error(line, "cannot convert a void", "constructor");
626 return true;
627 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000628 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000629
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000630 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000631}
632
Jamie Madillb98c3a82015-07-23 14:26:04 -0400633// This function checks to see if a void variable has been declared and raise an error message for
634// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000635//
636// returns true in case of an error
637//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400638bool TParseContext::voidErrorCheck(const TSourceLoc &line,
639 const TString &identifier,
640 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000641{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300642 if (type == EbtVoid)
643 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000644 error(line, "illegal use of type 'void'", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000645 return true;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300646 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000647
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000648 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000649}
650
Jamie Madillb98c3a82015-07-23 14:26:04 -0400651// This function checks to see if the node (for the expression) contains a scalar boolean expression
652// or not
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000653//
654// returns true in case of an error
655//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530656bool TParseContext::boolErrorCheck(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000657{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530658 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector())
659 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000660 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000661 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530662 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000663
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000664 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000665}
666
Jamie Madillb98c3a82015-07-23 14:26:04 -0400667// This function checks to see if the node (for the expression) contains a scalar boolean expression
668// or not
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000669//
670// returns true in case of an error
671//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530672bool TParseContext::boolErrorCheck(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000673{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530674 if (pType.type != EbtBool || pType.isAggregate())
675 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000676 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000677 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530678 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000679
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000680 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000681}
682
Jamie Madillb98c3a82015-07-23 14:26:04 -0400683bool TParseContext::samplerErrorCheck(const TSourceLoc &line,
684 const TPublicType &pType,
685 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000686{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530687 if (pType.type == EbtStruct)
688 {
689 if (containsSampler(*pType.userDef))
690 {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000691 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530692
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000693 return true;
694 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530695
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000696 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530697 }
698 else if (IsSampler(pType.type))
699 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000700 error(line, reason, getBasicString(pType.type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000701
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000702 return true;
703 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000704
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000705 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000706}
707
Arun Patole7e7e68d2015-05-22 12:02:25 +0530708bool TParseContext::locationDeclaratorListCheck(const TSourceLoc &line, const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400709{
710 if (pType.layoutQualifier.location != -1)
711 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400712 error(line, "location must only be specified for a single input or output variable",
713 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400714 return true;
715 }
716
717 return false;
718}
719
Jamie Madillb98c3a82015-07-23 14:26:04 -0400720bool TParseContext::parameterSamplerErrorCheck(const TSourceLoc &line,
721 TQualifier qualifier,
722 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000723{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400724 if ((qualifier == EvqOut || qualifier == EvqInOut) && type.getBasicType() != EbtStruct &&
725 IsSampler(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530726 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000727 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000728 return true;
729 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000730
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000731 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000732}
733
Arun Patole7e7e68d2015-05-22 12:02:25 +0530734bool TParseContext::containsSampler(const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000735{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000736 if (IsSampler(type.getBasicType()))
737 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000738
Arun Patole7e7e68d2015-05-22 12:02:25 +0530739 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock())
740 {
741 const TFieldList &fields = type.getStruct()->fields();
742 for (unsigned int i = 0; i < fields.size(); ++i)
743 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400744 if (containsSampler(*fields[i]->type()))
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000745 return true;
746 }
747 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000748
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000749 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000750}
751
752//
753// Do size checking for an array type's size.
754//
755// Returns true if there was an error.
756//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530757bool TParseContext::arraySizeErrorCheck(const TSourceLoc &line, TIntermTyped *expr, int &size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000758{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530759 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000760
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200761 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
762 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
763 // fold as array size.
764 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000765 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000766 error(line, "array size must be a constant integer expression", "");
Olli Etuahoe7847b02015-03-16 11:56:12 +0200767 size = 1;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000768 return true;
769 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000770
Nicolas Capens906744a2014-06-06 15:18:07 -0400771 unsigned int unsignedSize = 0;
772
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000773 if (constant->getBasicType() == EbtUInt)
774 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400775 unsignedSize = constant->getUConst(0);
Jamie Madillb98c3a82015-07-23 14:26:04 -0400776 size = static_cast<int>(unsignedSize);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000777 }
778 else
779 {
780 size = constant->getIConst(0);
781
Nicolas Capens906744a2014-06-06 15:18:07 -0400782 if (size < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000783 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400784 error(line, "array size must be non-negative", "");
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000785 size = 1;
786 return true;
787 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400788
789 unsignedSize = static_cast<unsigned int>(size);
790 }
791
792 if (size == 0)
793 {
794 error(line, "array size must be greater than zero", "");
795 size = 1;
796 return true;
797 }
798
799 // The size of arrays is restricted here to prevent issues further down the
800 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
801 // 4096 registers so this should be reasonable even for aggressively optimizable code.
802 const unsigned int sizeLimit = 65536;
803
804 if (unsignedSize > sizeLimit)
805 {
806 error(line, "array size too large", "");
807 size = 1;
808 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000809 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000810
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000811 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000812}
813
814//
815// See if this qualifier can be an array.
816//
817// Returns true if there is an error.
818//
Olli Etuaho3739d232015-04-08 12:23:44 +0300819bool TParseContext::arrayQualifierErrorCheck(const TSourceLoc &line, const TPublicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000820{
Olli Etuaho3739d232015-04-08 12:23:44 +0300821 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqVertexIn) ||
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400822 (type.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300823 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400824 error(line, "cannot declare arrays of this qualifier",
825 TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000826 return true;
827 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000828
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000829 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000830}
831
832//
833// See if this type can be an array.
834//
835// Returns true if there is an error.
836//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530837bool TParseContext::arrayTypeErrorCheck(const TSourceLoc &line, const TPublicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000838{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000839 //
840 // Can the type be an array?
841 //
Jamie Madill06145232015-05-13 13:10:01 -0400842 if (type.array)
843 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000844 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000845 return true;
846 }
Olli Etuahocc36b982015-07-10 14:14:18 +0300847 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
848 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
849 // 4.3.4).
850 if (mShaderVersion >= 300 && type.type == EbtStruct && sh::IsVarying(type.qualifier))
851 {
852 error(line, "cannot declare arrays of structs of this qualifier",
853 TType(type).getCompleteString().c_str());
854 return true;
855 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000856
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000857 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000858}
859
860//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000861// Enforce non-initializer type/qualifier rules.
862//
863// Returns true if there was an error.
864//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400865bool TParseContext::nonInitErrorCheck(const TSourceLoc &line,
866 const TString &identifier,
867 TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000868{
Olli Etuaho3739d232015-04-08 12:23:44 +0300869 ASSERT(type != nullptr);
870 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000871 {
872 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300873 type->qualifier = EvqTemporary;
874
875 // Generate informative error messages for ESSL1.
876 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400877 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000878 {
Arun Patole7e7e68d2015-05-22 12:02:25 +0530879 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400880 "structures containing arrays may not be declared constant since they cannot be "
881 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530882 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000883 }
884 else
885 {
886 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
887 }
888
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000889 return true;
890 }
Olli Etuaho376f1b52015-04-13 13:23:41 +0300891 if (type->isUnsizedArray())
892 {
893 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
894 return true;
895 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000896 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000897}
898
Olli Etuaho2935c582015-04-08 14:32:06 +0300899// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000900// and update the symbol table.
901//
Olli Etuaho2935c582015-04-08 14:32:06 +0300902// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000903//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400904bool TParseContext::declareVariable(const TSourceLoc &line,
905 const TString &identifier,
906 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +0300907 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000908{
Olli Etuaho2935c582015-04-08 14:32:06 +0300909 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000910
Olli Etuaho2935c582015-04-08 14:32:06 +0300911 bool needsReservedErrorCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000912
Olli Etuaho2935c582015-04-08 14:32:06 +0300913 // gl_LastFragData may be redeclared with a new precision qualifier
914 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
915 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400916 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
917 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho2935c582015-04-08 14:32:06 +0300918 if (type.getArraySize() == maxDrawBuffers->getConstPointer()->getIConst())
919 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400920 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +0300921 {
922 needsReservedErrorCheck = extensionErrorCheck(line, builtInSymbol->getExtension());
923 }
924 }
925 else
926 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400927 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
928 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +0300929 return false;
930 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000931 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000932
Olli Etuaho2935c582015-04-08 14:32:06 +0300933 if (needsReservedErrorCheck && reservedErrorCheck(line, identifier))
934 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000935
Olli Etuaho2935c582015-04-08 14:32:06 +0300936 (*variable) = new TVariable(&identifier, type);
937 if (!symbolTable.declare(*variable))
938 {
939 error(line, "redefinition", identifier.c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -0400940 *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +0300941 return false;
942 }
943
944 if (voidErrorCheck(line, identifier, type.getBasicType()))
945 return false;
946
947 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000948}
949
Jamie Madillb98c3a82015-07-23 14:26:04 -0400950bool TParseContext::paramErrorCheck(const TSourceLoc &line,
951 TQualifier qualifier,
952 TQualifier paramQualifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530953 TType *type)
954{
955 if (qualifier != EvqConst && qualifier != EvqTemporary)
956 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000957 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000958 return true;
959 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530960 if (qualifier == EvqConst && paramQualifier != EvqIn)
961 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400962 error(line, "qualifier not allowed with ", getQualifierString(qualifier),
963 getQualifierString(paramQualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000964 return true;
965 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000966
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000967 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000968 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000969 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000970 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000971
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000972 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000973}
974
Arun Patole7e7e68d2015-05-22 12:02:25 +0530975bool TParseContext::extensionErrorCheck(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000976{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400977 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000978 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +0530979 if (iter == extBehavior.end())
980 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000981 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000982 return true;
983 }
zmo@google.comf5450912011-09-09 01:37:19 +0000984 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +0530985 if (iter->second == EBhDisable || iter->second == EBhUndefined)
986 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000987 error(line, "extension", extension.c_str(), "is disabled");
988 return true;
989 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530990 if (iter->second == EBhWarn)
991 {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000992 warning(line, "extension", extension.c_str(), "is being used");
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000993 return false;
994 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000995
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000996 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000997}
998
Jamie Madillb98c3a82015-07-23 14:26:04 -0400999// These checks are common for all declarations starting a declarator list, and declarators that
1000// follow an empty declaration.
Olli Etuahofa33d582015-04-09 14:33:12 +03001001//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001002bool TParseContext::singleDeclarationErrorCheck(const TPublicType &publicType,
1003 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001004{
Olli Etuahofa33d582015-04-09 14:33:12 +03001005 switch (publicType.qualifier)
1006 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001007 case EvqVaryingIn:
1008 case EvqVaryingOut:
1009 case EvqAttribute:
1010 case EvqVertexIn:
1011 case EvqFragmentOut:
1012 if (publicType.type == EbtStruct)
1013 {
1014 error(identifierLocation, "cannot be used with a structure",
1015 getQualifierString(publicType.qualifier));
1016 return true;
1017 }
Olli Etuahofa33d582015-04-09 14:33:12 +03001018
Jamie Madillb98c3a82015-07-23 14:26:04 -04001019 default:
1020 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001021 }
1022
Jamie Madillb98c3a82015-07-23 14:26:04 -04001023 if (publicType.qualifier != EvqUniform &&
1024 samplerErrorCheck(identifierLocation, publicType, "samplers must be uniform"))
Olli Etuahofa33d582015-04-09 14:33:12 +03001025 {
Jamie Madilla5efff92013-06-06 11:56:47 -04001026 return true;
Olli Etuahofa33d582015-04-09 14:33:12 +03001027 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001028
1029 // check for layout qualifier issues
1030 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
1031
1032 if (layoutQualifier.matrixPacking != EmpUnspecified)
1033 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001034 error(identifierLocation, "layout qualifier",
1035 getMatrixPackingString(layoutQualifier.matrixPacking),
Olli Etuahofa33d582015-04-09 14:33:12 +03001036 "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -04001037 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001038 }
1039
1040 if (layoutQualifier.blockStorage != EbsUnspecified)
1041 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001042 error(identifierLocation, "layout qualifier",
1043 getBlockStorageString(layoutQualifier.blockStorage),
Olli Etuahofa33d582015-04-09 14:33:12 +03001044 "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -04001045 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001046 }
1047
Olli Etuahofa33d582015-04-09 14:33:12 +03001048 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut &&
1049 layoutLocationErrorCheck(identifierLocation, publicType.layoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001050 {
Jamie Madill51a53c72013-06-19 09:24:43 -04001051 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001052 }
1053
1054 return false;
1055}
1056
Jamie Madillb98c3a82015-07-23 14:26:04 -04001057bool TParseContext::layoutLocationErrorCheck(const TSourceLoc &location,
1058 const TLayoutQualifier &layoutQualifier)
Jamie Madilla5efff92013-06-06 11:56:47 -04001059{
1060 if (layoutQualifier.location != -1)
1061 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001062 error(location, "invalid layout qualifier:", "location",
1063 "only valid on program inputs and outputs");
Jamie Madilla5efff92013-06-06 11:56:47 -04001064 return true;
1065 }
1066
1067 return false;
1068}
1069
Jamie Madillb98c3a82015-07-23 14:26:04 -04001070bool TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate,
1071 TIntermAggregate *aggregate)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001072{
1073 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1074 {
1075 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1076 if (qual == EvqOut || qual == EvqInOut)
1077 {
1078 TIntermTyped *node = (*(aggregate->getSequence()))[i]->getAsTyped();
1079 if (lValueErrorCheck(node->getLine(), "assign", node))
1080 {
1081 error(node->getLine(),
Jamie Madillb98c3a82015-07-23 14:26:04 -04001082 "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error");
Olli Etuahob6e07a62015-02-16 12:22:10 +02001083 recover();
1084 return true;
1085 }
1086 }
1087 }
1088 return false;
1089}
1090
Jamie Madillb98c3a82015-07-23 14:26:04 -04001091void TParseContext::es3InvariantErrorCheck(const TQualifier qualifier,
1092 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001093{
1094 if (!sh::IsVaryingOut(qualifier) && qualifier != EvqFragmentOut)
1095 {
1096 error(invariantLocation, "Only out variables can be invariant.", "invariant");
1097 recover();
1098 }
1099}
1100
Arun Patole7e7e68d2015-05-22 12:02:25 +05301101bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001102{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001103 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001104 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1105 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001106}
1107
Arun Patole7e7e68d2015-05-22 12:02:25 +05301108bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001109{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001110 return ::IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001111}
1112
Jamie Madillb98c3a82015-07-23 14:26:04 -04001113void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1114 const char *extName,
1115 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001116{
1117 pp::SourceLocation srcLoc;
1118 srcLoc.file = loc.first_file;
1119 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001120 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001121}
1122
Jamie Madillb98c3a82015-07-23 14:26:04 -04001123void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1124 const char *name,
1125 const char *value,
1126 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001127{
1128 pp::SourceLocation srcLoc;
1129 srcLoc.file = loc.first_file;
1130 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001131 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001132}
1133
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001134/////////////////////////////////////////////////////////////////////////////////
1135//
1136// Non-Errors.
1137//
1138/////////////////////////////////////////////////////////////////////////////////
1139
Jamie Madill5c097022014-08-20 16:38:32 -04001140const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1141 const TString *name,
1142 const TSymbol *symbol)
1143{
1144 const TVariable *variable = NULL;
1145
1146 if (!symbol)
1147 {
1148 error(location, "undeclared identifier", name->c_str());
1149 recover();
1150 }
1151 else if (!symbol->isVariable())
1152 {
1153 error(location, "variable expected", name->c_str());
1154 recover();
1155 }
1156 else
1157 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001158 variable = static_cast<const TVariable *>(symbol);
Jamie Madill5c097022014-08-20 16:38:32 -04001159
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001160 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
Jamie Madill5c097022014-08-20 16:38:32 -04001161 !variable->getExtension().empty() &&
1162 extensionErrorCheck(location, variable->getExtension()))
1163 {
1164 recover();
1165 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001166
1167 // Reject shaders using both gl_FragData and gl_FragColor
1168 TQualifier qualifier = variable->getType().getQualifier();
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001169 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001170 {
1171 mUsesFragData = true;
1172 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001173 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001174 {
1175 mUsesFragColor = true;
1176 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001177 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1178 {
1179 mUsesSecondaryOutputs = true;
1180 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001181
1182 // This validation is not quite correct - it's only an error to write to
1183 // both FragData and FragColor. For simplicity, and because users shouldn't
1184 // be rewarded for reading from undefined varaibles, return an error
1185 // if they are both referenced, rather than assigned.
1186 if (mUsesFragData && mUsesFragColor)
1187 {
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001188 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1189 if (mUsesSecondaryOutputs)
1190 {
1191 errorMessage =
1192 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1193 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1194 }
1195 error(location, errorMessage, name->c_str());
Jamie Madill14e95b32015-05-07 10:10:41 -04001196 recover();
1197 }
Jamie Madill5c097022014-08-20 16:38:32 -04001198 }
1199
1200 if (!variable)
1201 {
1202 TType type(EbtFloat, EbpUndefined);
1203 TVariable *fakeVariable = new TVariable(name, type);
1204 symbolTable.declare(fakeVariable);
1205 variable = fakeVariable;
1206 }
1207
1208 return variable;
1209}
1210
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001211TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1212 const TString *name,
1213 const TSymbol *symbol)
1214{
1215 const TVariable *variable = getNamedVariable(location, name, symbol);
1216
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001217 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001218 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001219 const TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001220 return intermediate.addConstantUnion(constArray, variable->getType(), location);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001221 }
1222 else
1223 {
1224 return intermediate.addSymbol(variable->getUniqueId(), variable->getName(),
1225 variable->getType(), location);
1226 }
1227}
1228
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001229//
1230// Look up a function name in the symbol table, and make sure it is a function.
1231//
1232// Return the function symbol if found, otherwise 0.
1233//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001234const TFunction *TParseContext::findFunction(const TSourceLoc &line,
1235 TFunction *call,
1236 int inputShaderVersion,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301237 bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001238{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001239 // First find by unmangled name to check whether the function name has been
1240 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001241 // If a function is found, check for one with a matching argument list.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301242 const TSymbol *symbol = symbolTable.find(call->getName(), inputShaderVersion, builtIn);
1243 if (symbol == 0 || symbol->isFunction())
1244 {
Austin Kinross3ae64652015-01-26 15:51:39 -08001245 symbol = symbolTable.find(call->getMangledName(), inputShaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001246 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001247
Arun Patole7e7e68d2015-05-22 12:02:25 +05301248 if (symbol == 0)
1249 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001250 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001251 return 0;
1252 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001253
Arun Patole7e7e68d2015-05-22 12:02:25 +05301254 if (!symbol->isFunction())
1255 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001256 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001257 return 0;
1258 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001259
Jamie Madillb98c3a82015-07-23 14:26:04 -04001260 return static_cast<const TFunction *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001261}
1262
1263//
1264// Initializers show up in several places in the grammar. Have one set of
1265// code to handle them here.
1266//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001267// Returns true on error, false if no error
1268//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001269bool TParseContext::executeInitializer(const TSourceLoc &line,
1270 const TString &identifier,
1271 const TPublicType &pType,
1272 TIntermTyped *initializer,
1273 TIntermNode **intermNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001274{
Olli Etuahoe7847b02015-03-16 11:56:12 +02001275 ASSERT(intermNode != nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001276 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001277
Olli Etuaho2935c582015-04-08 14:32:06 +03001278 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001279 if (type.isUnsizedArray())
1280 {
1281 type.setArraySize(initializer->getArraySize());
1282 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001283 if (!declareVariable(line, identifier, type, &variable))
1284 {
1285 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001286 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001287
Olli Etuahob0c645e2015-05-12 14:25:36 +03001288 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001289 if (symbolTable.atGlobalLevel() &&
1290 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001291 {
1292 // Error message does not completely match behavior with ESSL 1.00, but
1293 // we want to steer developers towards only using constant expressions.
1294 error(line, "global variable initializers must be constant expressions", "=");
1295 return true;
1296 }
1297 if (globalInitWarning)
1298 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001299 warning(
1300 line,
1301 "global variable initializers should be constant expressions "
1302 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1303 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001304 }
1305
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001306 //
1307 // identifier must be of type constant, a global, or a temporary
1308 //
1309 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301310 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1311 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001312 error(line, " cannot initialize this type of qualifier ",
1313 variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001314 return true;
1315 }
1316 //
1317 // test for and propagate constant
1318 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001319
Arun Patole7e7e68d2015-05-22 12:02:25 +05301320 if (qualifier == EvqConst)
1321 {
1322 if (qualifier != initializer->getType().getQualifier())
1323 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001324 std::stringstream extraInfoStream;
1325 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1326 std::string extraInfo = extraInfoStream.str();
1327 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001328 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001329 return true;
1330 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301331 if (type != initializer->getType())
1332 {
1333 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001334 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001335 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001336 return true;
1337 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001338
1339 // Save the constant folded value to the variable if possible. For example array
1340 // initializers are not folded, since that way copying the array literal to multiple places
1341 // in the shader is avoided.
1342 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1343 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301344 if (initializer->getAsConstantUnion())
1345 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001346 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001347 *intermNode = nullptr;
1348 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301349 }
1350 else if (initializer->getAsSymbolNode())
1351 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001352 const TSymbol *symbol =
1353 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1354 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001355
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001356 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001357 if (constArray)
1358 {
1359 variable->shareConstPointer(constArray);
1360 *intermNode = nullptr;
1361 return false;
1362 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001363 }
1364 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001365
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001366 TIntermSymbol *intermSymbol = intermediate.addSymbol(
1367 variable->getUniqueId(), variable->getName(), variable->getType(), line);
1368 *intermNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1369 if (*intermNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001370 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001371 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1372 return true;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001373 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001374
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001375 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001376}
1377
Jamie Madillb98c3a82015-07-23 14:26:04 -04001378TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier,
1379 bool invariant,
1380 TLayoutQualifier layoutQualifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301381 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001382{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001383 TPublicType returnType = typeSpecifier;
1384 returnType.qualifier = qualifier;
1385 returnType.invariant = invariant;
Jamie Madilla5efff92013-06-06 11:56:47 -04001386 returnType.layoutQualifier = layoutQualifier;
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001387
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001388 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001389 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001390 if (typeSpecifier.array)
1391 {
1392 error(typeSpecifier.line, "not supported", "first-class array");
1393 recover();
1394 returnType.clearArrayness();
1395 }
1396
Jamie Madillb98c3a82015-07-23 14:26:04 -04001397 if (qualifier == EvqAttribute &&
1398 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001399 {
1400 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1401 recover();
1402 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001403
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001404 if ((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1405 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1406 {
1407 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1408 recover();
1409 }
1410 }
1411 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001412 {
Olli Etuahoabb0c382015-07-13 12:01:12 +03001413 if (!layoutQualifier.isEmpty())
1414 {
1415 if (globalErrorCheck(typeSpecifier.line, symbolTable.atGlobalLevel(), "layout"))
1416 {
1417 recover();
1418 }
1419 }
Olli Etuahocc36b982015-07-10 14:14:18 +03001420 if (sh::IsVarying(qualifier) || qualifier == EvqVertexIn || qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001421 {
Olli Etuahocc36b982015-07-10 14:14:18 +03001422 es3InputOutputTypeCheck(qualifier, typeSpecifier, typeSpecifier.line);
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001423 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001424 }
1425
1426 return returnType;
1427}
1428
Olli Etuahocc36b982015-07-10 14:14:18 +03001429void TParseContext::es3InputOutputTypeCheck(const TQualifier qualifier,
1430 const TPublicType &type,
1431 const TSourceLoc &qualifierLocation)
1432{
1433 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
1434 if (type.type == EbtBool)
1435 {
1436 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
1437 recover();
1438 }
1439
1440 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
1441 switch (qualifier)
1442 {
1443 case EvqVertexIn:
1444 // ESSL 3.00 section 4.3.4
1445 if (type.array)
1446 {
1447 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
1448 recover();
1449 }
1450 // Vertex inputs with a struct type are disallowed in singleDeclarationErrorCheck
1451 return;
1452 case EvqFragmentOut:
1453 // ESSL 3.00 section 4.3.6
1454 if (type.isMatrix())
1455 {
1456 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
1457 recover();
1458 }
1459 // Fragment outputs with a struct type are disallowed in singleDeclarationErrorCheck
1460 return;
1461 default:
1462 break;
1463 }
1464
1465 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
1466 // restrictions.
1467 bool typeContainsIntegers =
1468 (type.type == EbtInt || type.type == EbtUInt || type.isStructureContainingType(EbtInt) ||
1469 type.isStructureContainingType(EbtUInt));
1470 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
1471 {
1472 error(qualifierLocation, "must use 'flat' interpolation here",
1473 getQualifierString(qualifier));
1474 recover();
1475 }
1476
1477 if (type.type == EbtStruct)
1478 {
1479 // ESSL 3.00 sections 4.3.4 and 4.3.6.
1480 // These restrictions are only implied by the ESSL 3.00 spec, but
1481 // the ESSL 3.10 spec lists these restrictions explicitly.
1482 if (type.array)
1483 {
1484 error(qualifierLocation, "cannot be an array of structures",
1485 getQualifierString(qualifier));
1486 recover();
1487 }
1488 if (type.isStructureContainingArrays())
1489 {
1490 error(qualifierLocation, "cannot be a structure containing an array",
1491 getQualifierString(qualifier));
1492 recover();
1493 }
1494 if (type.isStructureContainingType(EbtStruct))
1495 {
1496 error(qualifierLocation, "cannot be a structure containing a structure",
1497 getQualifierString(qualifier));
1498 recover();
1499 }
1500 if (type.isStructureContainingType(EbtBool))
1501 {
1502 error(qualifierLocation, "cannot be a structure containing a bool",
1503 getQualifierString(qualifier));
1504 recover();
1505 }
1506 }
1507}
1508
Olli Etuahofa33d582015-04-09 14:33:12 +03001509TIntermAggregate *TParseContext::parseSingleDeclaration(TPublicType &publicType,
1510 const TSourceLoc &identifierOrTypeLocation,
1511 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001512{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001513 TIntermSymbol *symbol =
1514 intermediate.addSymbol(0, identifier, TType(publicType), identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001515
Olli Etuahobab4c082015-04-24 16:38:49 +03001516 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03001517
Olli Etuahobab4c082015-04-24 16:38:49 +03001518 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1519
1520 if (emptyDeclaration)
1521 {
1522 if (publicType.isUnsizedArray())
1523 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001524 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1525 // error. It is assumed that this applies to empty declarations as well.
1526 error(identifierOrTypeLocation, "empty array declaration needs to specify a size",
1527 identifier.c_str());
Olli Etuahobab4c082015-04-24 16:38:49 +03001528 }
1529 }
1530 else
Jamie Madill60ed9812013-06-06 11:56:46 -04001531 {
Olli Etuahofa33d582015-04-09 14:33:12 +03001532 if (singleDeclarationErrorCheck(publicType, identifierOrTypeLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001533 recover();
1534
Olli Etuaho376f1b52015-04-13 13:23:41 +03001535 if (nonInitErrorCheck(identifierOrTypeLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001536 recover();
1537
Olli Etuaho2935c582015-04-08 14:32:06 +03001538 TVariable *variable = nullptr;
Olli Etuahofa33d582015-04-09 14:33:12 +03001539 if (!declareVariable(identifierOrTypeLocation, identifier, TType(publicType), &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001540 recover();
1541
1542 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001543 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001544 }
1545
Olli Etuahoe7847b02015-03-16 11:56:12 +02001546 return intermediate.makeAggregate(symbol, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001547}
1548
Olli Etuahoe7847b02015-03-16 11:56:12 +02001549TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1550 const TSourceLoc &identifierLocation,
1551 const TString &identifier,
1552 const TSourceLoc &indexLocation,
1553 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001554{
Olli Etuahofa33d582015-04-09 14:33:12 +03001555 mDeferredSingleDeclarationErrorCheck = false;
1556
1557 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001558 recover();
1559
Olli Etuaho376f1b52015-04-13 13:23:41 +03001560 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001561 recover();
1562
Jamie Madillb98c3a82015-07-23 14:26:04 -04001563 if (arrayTypeErrorCheck(indexLocation, publicType) ||
1564 arrayQualifierErrorCheck(indexLocation, publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001565 {
1566 recover();
1567 }
1568
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001569 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001570
1571 int size;
1572 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1573 {
1574 recover();
1575 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001576 // Make the type an array even if size check failed.
1577 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1578 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001579
Olli Etuaho2935c582015-04-08 14:32:06 +03001580 TVariable *variable = nullptr;
1581 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001582 recover();
1583
Olli Etuahoe7847b02015-03-16 11:56:12 +02001584 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001585 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001586 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001587
Olli Etuahoe7847b02015-03-16 11:56:12 +02001588 return intermediate.makeAggregate(symbol, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001589}
1590
Jamie Madill06145232015-05-13 13:10:01 -04001591TIntermAggregate *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001592 const TSourceLoc &identifierLocation,
1593 const TString &identifier,
1594 const TSourceLoc &initLocation,
1595 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001596{
Olli Etuahofa33d582015-04-09 14:33:12 +03001597 mDeferredSingleDeclarationErrorCheck = false;
1598
1599 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001600 recover();
1601
Olli Etuahoe7847b02015-03-16 11:56:12 +02001602 TIntermNode *intermNode = nullptr;
1603 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04001604 {
1605 //
1606 // Build intermediate representation
1607 //
Olli Etuahoe7847b02015-03-16 11:56:12 +02001608 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001609 }
1610 else
1611 {
1612 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001613 return nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001614 }
1615}
1616
Jamie Madillb98c3a82015-07-23 14:26:04 -04001617TIntermAggregate *TParseContext::parseSingleArrayInitDeclaration(
1618 TPublicType &publicType,
1619 const TSourceLoc &identifierLocation,
1620 const TString &identifier,
1621 const TSourceLoc &indexLocation,
1622 TIntermTyped *indexExpression,
1623 const TSourceLoc &initLocation,
1624 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001625{
1626 mDeferredSingleDeclarationErrorCheck = false;
1627
1628 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1629 recover();
1630
Jamie Madillb98c3a82015-07-23 14:26:04 -04001631 if (arrayTypeErrorCheck(indexLocation, publicType) ||
1632 arrayQualifierErrorCheck(indexLocation, publicType))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001633 {
1634 recover();
1635 }
1636
1637 TPublicType arrayType(publicType);
1638
Olli Etuaho376f1b52015-04-13 13:23:41 +03001639 int size = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001640 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
1641 // the initializer.
1642 if (indexExpression != nullptr &&
1643 arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001644 {
1645 recover();
1646 }
1647 // Make the type an array even if size check failed.
1648 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1649 arrayType.setArraySize(size);
1650
1651 // initNode will correspond to the whole of "type b[n] = initializer".
1652 TIntermNode *initNode = nullptr;
1653 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1654 {
1655 return initNode ? intermediate.makeAggregate(initNode, initLocation) : nullptr;
1656 }
1657 else
1658 {
1659 recover();
1660 return nullptr;
1661 }
1662}
1663
Olli Etuahoe7847b02015-03-16 11:56:12 +02001664TIntermAggregate *TParseContext::parseInvariantDeclaration(const TSourceLoc &invariantLoc,
Jamie Madill47e3ec02014-08-20 16:38:33 -04001665 const TSourceLoc &identifierLoc,
1666 const TString *identifier,
1667 const TSymbol *symbol)
1668{
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001669 // invariant declaration
Jamie Madill47e3ec02014-08-20 16:38:33 -04001670 if (globalErrorCheck(invariantLoc, symbolTable.atGlobalLevel(), "invariant varying"))
1671 {
1672 recover();
1673 }
1674
1675 if (!symbol)
1676 {
1677 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
1678 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001679 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001680 }
1681 else
1682 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001683 const TString kGlFrontFacing("gl_FrontFacing");
1684 if (*identifier == kGlFrontFacing)
1685 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001686 error(identifierLoc, "identifier should not be declared as invariant",
1687 identifier->c_str());
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001688 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001689 return nullptr;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001690 }
Jamie Madill2c433252014-12-03 12:36:54 -05001691 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001692 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1693 ASSERT(variable);
1694 const TType &type = variable->getType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04001695 TIntermSymbol *intermSymbol =
1696 intermediate.addSymbol(variable->getUniqueId(), *identifier, type, identifierLoc);
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001697
1698 TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc);
1699 aggregate->setOp(EOpInvariantDeclaration);
1700 return aggregate;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001701 }
1702}
1703
Jamie Madillb98c3a82015-07-23 14:26:04 -04001704TIntermAggregate *TParseContext::parseDeclarator(TPublicType &publicType,
1705 TIntermAggregate *aggregateDeclaration,
1706 const TSourceLoc &identifierLocation,
1707 const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001708{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001709 // If the declaration starting this declarator list was empty (example: int,), some checks were
1710 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001711 if (mDeferredSingleDeclarationErrorCheck)
1712 {
1713 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1714 recover();
1715 mDeferredSingleDeclarationErrorCheck = false;
1716 }
1717
Jamie Madill0bd18df2013-06-20 11:55:52 -04001718 if (locationDeclaratorListCheck(identifierLocation, publicType))
1719 recover();
1720
Olli Etuaho376f1b52015-04-13 13:23:41 +03001721 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001722 recover();
1723
Olli Etuaho2935c582015-04-08 14:32:06 +03001724 TVariable *variable = nullptr;
1725 if (!declareVariable(identifierLocation, identifier, TType(publicType), &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001726 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001727
Jamie Madillb98c3a82015-07-23 14:26:04 -04001728 TIntermSymbol *symbol =
1729 intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001730 if (variable && symbol)
Jamie Madill502d66f2013-06-20 11:55:52 -04001731 symbol->setId(variable->getUniqueId());
1732
Olli Etuahoe7847b02015-03-16 11:56:12 +02001733 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001734}
1735
Jamie Madillb98c3a82015-07-23 14:26:04 -04001736TIntermAggregate *TParseContext::parseArrayDeclarator(TPublicType &publicType,
1737 TIntermAggregate *aggregateDeclaration,
1738 const TSourceLoc &identifierLocation,
1739 const TString &identifier,
1740 const TSourceLoc &arrayLocation,
1741 TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001742{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001743 // If the declaration starting this declarator list was empty (example: int,), some checks were
1744 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001745 if (mDeferredSingleDeclarationErrorCheck)
1746 {
1747 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1748 recover();
1749 mDeferredSingleDeclarationErrorCheck = false;
1750 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001751
Jamie Madill0bd18df2013-06-20 11:55:52 -04001752 if (locationDeclaratorListCheck(identifierLocation, publicType))
1753 recover();
1754
Olli Etuaho376f1b52015-04-13 13:23:41 +03001755 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001756 recover();
1757
Jamie Madillb98c3a82015-07-23 14:26:04 -04001758 if (arrayTypeErrorCheck(arrayLocation, publicType) ||
1759 arrayQualifierErrorCheck(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001760 {
1761 recover();
1762 }
Olli Etuaho93a90fd2015-04-07 18:14:07 +03001763 else
Jamie Madill502d66f2013-06-20 11:55:52 -04001764 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001765 TType arrayType = TType(publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04001766 int size;
1767 if (arraySizeErrorCheck(arrayLocation, indexExpression, size))
Olli Etuahoe7847b02015-03-16 11:56:12 +02001768 {
Jamie Madill502d66f2013-06-20 11:55:52 -04001769 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001770 }
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001771 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001772
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001773 TVariable *variable = nullptr;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001774 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001775 recover();
Jamie Madill502d66f2013-06-20 11:55:52 -04001776
Jamie Madillb98c3a82015-07-23 14:26:04 -04001777 TIntermSymbol *symbol =
1778 intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001779 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001780 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001781
1782 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001783 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001784
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001785 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001786}
1787
Jamie Madillb98c3a82015-07-23 14:26:04 -04001788TIntermAggregate *TParseContext::parseInitDeclarator(const TPublicType &publicType,
1789 TIntermAggregate *aggregateDeclaration,
1790 const TSourceLoc &identifierLocation,
1791 const TString &identifier,
1792 const TSourceLoc &initLocation,
1793 TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001794{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001795 // If the declaration starting this declarator list was empty (example: int,), some checks were
1796 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001797 if (mDeferredSingleDeclarationErrorCheck)
1798 {
1799 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1800 recover();
1801 mDeferredSingleDeclarationErrorCheck = false;
1802 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001803
Jamie Madill0bd18df2013-06-20 11:55:52 -04001804 if (locationDeclaratorListCheck(identifierLocation, publicType))
1805 recover();
1806
Olli Etuahoe7847b02015-03-16 11:56:12 +02001807 TIntermNode *intermNode = nullptr;
1808 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04001809 {
1810 //
1811 // build the intermediate representation
1812 //
1813 if (intermNode)
1814 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001815 return intermediate.growAggregate(aggregateDeclaration, intermNode, initLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001816 }
1817 else
1818 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001819 return aggregateDeclaration;
Jamie Madill502d66f2013-06-20 11:55:52 -04001820 }
1821 }
1822 else
1823 {
1824 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001825 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001826 }
1827}
1828
Jamie Madill06145232015-05-13 13:10:01 -04001829TIntermAggregate *TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001830 TIntermAggregate *aggregateDeclaration,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301831 const TSourceLoc &identifierLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001832 const TString &identifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301833 const TSourceLoc &indexLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001834 TIntermTyped *indexExpression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001835 const TSourceLoc &initLocation,
1836 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001837{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001838 // If the declaration starting this declarator list was empty (example: int,), some checks were
1839 // not performed.
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001840 if (mDeferredSingleDeclarationErrorCheck)
1841 {
1842 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1843 recover();
1844 mDeferredSingleDeclarationErrorCheck = false;
1845 }
1846
1847 if (locationDeclaratorListCheck(identifierLocation, publicType))
1848 recover();
1849
Jamie Madillb98c3a82015-07-23 14:26:04 -04001850 if (arrayTypeErrorCheck(indexLocation, publicType) ||
1851 arrayQualifierErrorCheck(indexLocation, publicType))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001852 {
1853 recover();
1854 }
1855
1856 TPublicType arrayType(publicType);
1857
Olli Etuaho376f1b52015-04-13 13:23:41 +03001858 int size = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001859 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
1860 // the initializer.
1861 if (indexExpression != nullptr &&
1862 arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001863 {
1864 recover();
1865 }
1866 // Make the type an array even if size check failed.
1867 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1868 arrayType.setArraySize(size);
1869
1870 // initNode will correspond to the whole of "b[n] = initializer".
1871 TIntermNode *initNode = nullptr;
1872 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1873 {
1874 if (initNode)
1875 {
1876 return intermediate.growAggregate(aggregateDeclaration, initNode, initLocation);
1877 }
1878 else
1879 {
1880 return aggregateDeclaration;
1881 }
1882 }
1883 else
1884 {
1885 recover();
1886 return nullptr;
1887 }
1888}
1889
Jamie Madilla295edf2013-06-06 11:56:48 -04001890void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1891{
1892 if (typeQualifier.qualifier != EvqUniform)
1893 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001894 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
1895 "global layout must be uniform");
Jamie Madilla295edf2013-06-06 11:56:48 -04001896 recover();
1897 return;
1898 }
1899
1900 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1901 ASSERT(!layoutQualifier.isEmpty());
1902
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001903 if (mShaderVersion < 300)
Jamie Madilla295edf2013-06-06 11:56:48 -04001904 {
1905 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1906 recover();
1907 return;
1908 }
1909
1910 if (layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1911 {
1912 recover();
1913 return;
1914 }
1915
Jamie Madill099c0f32013-06-20 11:55:52 -04001916 if (layoutQualifier.matrixPacking != EmpUnspecified)
1917 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001918 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04001919 }
1920
Geoff Langc6856732014-02-11 09:38:55 -05001921 if (layoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04001922 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001923 mDefaultBlockStorage = layoutQualifier.blockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04001924 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001925}
1926
Jamie Madill185fb402015-06-12 15:48:48 -04001927void TParseContext::parseFunctionPrototype(const TSourceLoc &location,
1928 TFunction *function,
1929 TIntermAggregate **aggregateOut)
1930{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001931 const TSymbol *builtIn =
1932 symbolTable.findBuiltIn(function->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04001933
1934 if (builtIn)
1935 {
1936 error(location, "built-in functions cannot be redefined", function->getName().c_str());
1937 recover();
1938 }
1939
Jamie Madillb98c3a82015-07-23 14:26:04 -04001940 TFunction *prevDec =
1941 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Jamie Madill185fb402015-06-12 15:48:48 -04001942 //
1943 // Note: 'prevDec' could be 'function' if this is the first time we've seen function
1944 // as it would have just been put in the symbol table. Otherwise, we're looking up
1945 // an earlier occurance.
1946 //
1947 if (prevDec->isDefined())
1948 {
1949 // Then this function already has a body.
1950 error(location, "function already has a body", function->getName().c_str());
1951 recover();
1952 }
1953 prevDec->setDefined();
1954 //
1955 // Overload the unique ID of the definition to be the same unique ID as the declaration.
1956 // Eventually we will probably want to have only a single definition and just swap the
1957 // arguments to be the definition's arguments.
1958 //
1959 function->setUniqueId(prevDec->getUniqueId());
1960
1961 // Raise error message if main function takes any parameters or return anything other than void
1962 if (function->getName() == "main")
1963 {
1964 if (function->getParamCount() > 0)
1965 {
1966 error(location, "function cannot take any parameter(s)", function->getName().c_str());
1967 recover();
1968 }
1969 if (function->getReturnType().getBasicType() != EbtVoid)
1970 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001971 error(location, "", function->getReturnType().getBasicString(),
1972 "main function cannot return a value");
Jamie Madill185fb402015-06-12 15:48:48 -04001973 recover();
1974 }
1975 }
1976
1977 //
1978 // Remember the return type for later checking for RETURN statements.
1979 //
1980 setCurrentFunctionType(&(prevDec->getReturnType()));
1981 setFunctionReturnsValue(false);
1982
1983 //
1984 // Insert parameters into the symbol table.
1985 // If the parameter has no name, it's not an error, just don't insert it
1986 // (could be used for unused args).
1987 //
1988 // Also, accumulate the list of parameters into the HIL, so lower level code
1989 // knows where to find parameters.
1990 //
1991 TIntermAggregate *paramNodes = new TIntermAggregate;
1992 for (size_t i = 0; i < function->getParamCount(); i++)
1993 {
1994 const TConstParameter &param = function->getParam(i);
1995 if (param.name != 0)
1996 {
1997 TVariable *variable = new TVariable(param.name, *param.type);
1998 //
1999 // Insert the parameters with name in the symbol table.
2000 //
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002001 if (!symbolTable.declare(variable))
2002 {
Jamie Madill185fb402015-06-12 15:48:48 -04002003 error(location, "redefinition", variable->getName().c_str());
2004 recover();
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002005 paramNodes = intermediate.growAggregate(
2006 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
2007 continue;
Jamie Madill185fb402015-06-12 15:48:48 -04002008 }
2009
2010 //
2011 // Add the parameter to the HIL
2012 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002013 TIntermSymbol *symbol = intermediate.addSymbol(
2014 variable->getUniqueId(), variable->getName(), variable->getType(), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002015
2016 paramNodes = intermediate.growAggregate(paramNodes, symbol, location);
2017 }
2018 else
2019 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002020 paramNodes = intermediate.growAggregate(
2021 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002022 }
2023 }
2024 intermediate.setAggregateOperator(paramNodes, EOpParameters, location);
2025 *aggregateOut = paramNodes;
2026 setLoopNestingLevel(0);
2027}
2028
Jamie Madillb98c3a82015-07-23 14:26:04 -04002029TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002030{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002031 //
2032 // Multiple declarations of the same function are allowed.
2033 //
2034 // If this is a definition, the definition production code will check for redefinitions
2035 // (we don't know at this point if it's a definition or not).
2036 //
2037 // Redeclarations are allowed. But, return types and parameter qualifiers must match.
2038 //
2039 TFunction *prevDec =
2040 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
2041 if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002042 {
2043 if (prevDec->getReturnType() != function->getReturnType())
2044 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002045 error(location, "overloaded functions must have the same return type",
Jamie Madill185fb402015-06-12 15:48:48 -04002046 function->getReturnType().getBasicString());
2047 recover();
2048 }
2049 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2050 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002051 if (prevDec->getParam(i).type->getQualifier() !=
2052 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002053 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002054 error(location, "overloaded functions must have the same parameter qualifiers",
Jamie Madill185fb402015-06-12 15:48:48 -04002055 function->getParam(i).type->getQualifierString());
2056 recover();
2057 }
2058 }
2059 }
2060
2061 //
2062 // Check for previously declared variables using the same name.
2063 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002064 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002065 if (prevSym)
2066 {
2067 if (!prevSym->isFunction())
2068 {
2069 error(location, "redefinition", function->getName().c_str(), "function");
2070 recover();
2071 }
2072 }
2073 else
2074 {
2075 // Insert the unmangled name to detect potential future redefinition as a variable.
Jamie Madillb98c3a82015-07-23 14:26:04 -04002076 TFunction *newFunction =
2077 new TFunction(NewPoolTString(function->getName().c_str()), &function->getReturnType());
Jamie Madill185fb402015-06-12 15:48:48 -04002078 symbolTable.getOuterLevel()->insertUnmangled(newFunction);
2079 }
2080
2081 // We're at the inner scope level of the function's arguments and body statement.
2082 // Add the function prototype to the surrounding scope instead.
2083 symbolTable.getOuterLevel()->insert(function);
2084
2085 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002086 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2087 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002088 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2089 //
2090 return function;
2091}
2092
Jamie Madill06145232015-05-13 13:10:01 -04002093TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002094{
Jamie Madill06145232015-05-13 13:10:01 -04002095 TPublicType publicType = publicTypeIn;
Olli Etuahobd163f62015-11-13 12:15:38 +02002096 if (publicType.isStructSpecifier)
2097 {
2098 error(publicType.line, "constructor can't be a structure definition",
2099 getBasicString(publicType.type));
2100 recover();
2101 }
2102
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002103 TOperator op = EOpNull;
2104 if (publicType.userDef)
2105 {
2106 op = EOpConstructStruct;
2107 }
2108 else
2109 {
2110 switch (publicType.type)
2111 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002112 case EbtFloat:
2113 if (publicType.isMatrix())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002114 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002115 switch (publicType.getCols())
Alexis Hetu07e57df2015-06-16 16:55:52 -04002116 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002117 case 2:
2118 switch (publicType.getRows())
2119 {
2120 case 2:
2121 op = EOpConstructMat2;
2122 break;
2123 case 3:
2124 op = EOpConstructMat2x3;
2125 break;
2126 case 4:
2127 op = EOpConstructMat2x4;
2128 break;
2129 }
2130 break;
2131 case 3:
2132 switch (publicType.getRows())
2133 {
2134 case 2:
2135 op = EOpConstructMat3x2;
2136 break;
2137 case 3:
2138 op = EOpConstructMat3;
2139 break;
2140 case 4:
2141 op = EOpConstructMat3x4;
2142 break;
2143 }
2144 break;
2145 case 4:
2146 switch (publicType.getRows())
2147 {
2148 case 2:
2149 op = EOpConstructMat4x2;
2150 break;
2151 case 3:
2152 op = EOpConstructMat4x3;
2153 break;
2154 case 4:
2155 op = EOpConstructMat4;
2156 break;
2157 }
2158 break;
Alexis Hetu07e57df2015-06-16 16:55:52 -04002159 }
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002160 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04002161 else
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002162 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002163 switch (publicType.getNominalSize())
2164 {
2165 case 1:
2166 op = EOpConstructFloat;
2167 break;
2168 case 2:
2169 op = EOpConstructVec2;
2170 break;
2171 case 3:
2172 op = EOpConstructVec3;
2173 break;
2174 case 4:
2175 op = EOpConstructVec4;
2176 break;
2177 }
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002178 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04002179 break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002180
Jamie Madillb98c3a82015-07-23 14:26:04 -04002181 case EbtInt:
2182 switch (publicType.getNominalSize())
2183 {
2184 case 1:
2185 op = EOpConstructInt;
2186 break;
2187 case 2:
2188 op = EOpConstructIVec2;
2189 break;
2190 case 3:
2191 op = EOpConstructIVec3;
2192 break;
2193 case 4:
2194 op = EOpConstructIVec4;
2195 break;
2196 }
2197 break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002198
Jamie Madillb98c3a82015-07-23 14:26:04 -04002199 case EbtUInt:
2200 switch (publicType.getNominalSize())
2201 {
2202 case 1:
2203 op = EOpConstructUInt;
2204 break;
2205 case 2:
2206 op = EOpConstructUVec2;
2207 break;
2208 case 3:
2209 op = EOpConstructUVec3;
2210 break;
2211 case 4:
2212 op = EOpConstructUVec4;
2213 break;
2214 }
2215 break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002216
Jamie Madillb98c3a82015-07-23 14:26:04 -04002217 case EbtBool:
2218 switch (publicType.getNominalSize())
2219 {
2220 case 1:
2221 op = EOpConstructBool;
2222 break;
2223 case 2:
2224 op = EOpConstructBVec2;
2225 break;
2226 case 3:
2227 op = EOpConstructBVec3;
2228 break;
2229 case 4:
2230 op = EOpConstructBVec4;
2231 break;
2232 }
2233 break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002234
Jamie Madillb98c3a82015-07-23 14:26:04 -04002235 default:
2236 break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002237 }
2238
2239 if (op == EOpNull)
2240 {
2241 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
2242 recover();
2243 publicType.type = EbtFloat;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002244 op = EOpConstructFloat;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002245 }
2246 }
2247
2248 TString tempString;
Dmitry Skiba7f17a502015-06-22 15:08:39 -07002249 const TType *type = new TType(publicType);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002250 return new TFunction(&tempString, type, op);
2251}
2252
Jamie Madillb98c3a82015-07-23 14:26:04 -04002253// This function is used to test for the correctness of the parameters passed to various constructor
2254// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002255//
2256// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
2257//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002258TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments,
2259 TType *type,
2260 TOperator op,
2261 TFunction *fnCall,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302262 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002263{
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002264 TIntermAggregate *constructor = arguments->getAsAggregate();
2265 ASSERT(constructor != nullptr);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002266
Olli Etuahof40319e2015-03-10 14:33:00 +02002267 if (type->isArray())
2268 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002269 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
2270 // the array.
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002271 TIntermSequence *args = constructor->getSequence();
Olli Etuahof40319e2015-03-10 14:33:00 +02002272 for (size_t i = 0; i < args->size(); i++)
2273 {
2274 const TType &argType = (*args)[i]->getAsTyped()->getType();
2275 // It has already been checked that the argument is not an array.
2276 ASSERT(!argType.isArray());
2277 if (!argType.sameElementType(*type))
2278 {
2279 error(line, "Array constructor argument has an incorrect type", "Error");
2280 recover();
2281 return nullptr;
2282 }
2283 }
2284 }
2285 else if (op == EOpConstructStruct)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002286 {
2287 const TFieldList &fields = type->getStruct()->fields();
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002288 TIntermSequence *args = constructor->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002289
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002290 for (size_t i = 0; i < fields.size(); i++)
2291 {
Nicolas Capensffd73872014-08-21 13:49:16 -04002292 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002293 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002294 error(line, "Structure constructor arguments do not match structure fields",
2295 "Error");
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002296 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002297
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002298 return 0;
2299 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002300 }
2301 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002302
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002303 // Turn the argument list itself into a constructor
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002304 constructor->setOp(op);
2305 constructor->setLine(line);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002306 ASSERT(constructor->isConstructor());
2307
2308 // Need to set type before setPrecisionFromChildren() because bool doesn't have precision.
2309 constructor->setType(*type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002310
Olli Etuaho21203702014-11-13 16:16:21 +02002311 // Structs should not be precision qualified, the individual members may be.
2312 // Built-in types on the other hand should be precision qualified.
2313 if (op != EOpConstructStruct)
2314 {
2315 constructor->setPrecisionFromChildren();
2316 type->setPrecision(constructor->getPrecision());
2317 }
2318
Olli Etuaho1d122782015-11-06 15:35:17 +02002319 TIntermTyped *constConstructor = intermediate.foldAggregateBuiltIn(constructor);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002320 if (constConstructor)
2321 {
2322 return constConstructor;
2323 }
2324
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002325 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002326}
2327
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002328//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002329// This function returns the tree representation for the vector field(s) being accessed from contant
2330// vector.
2331// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a
2332// contant node is returned, else an aggregate node is returned (for v.xy). The input to this
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002333// function could either be the symbol node or it could be the intermediate tree representation of
2334// accessing fields in a constant structure or column of a constant matrix.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002335//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002336TIntermTyped *TParseContext::addConstVectorNode(TVectorFields &fields,
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002337 TIntermConstantUnion *node,
2338 const TSourceLoc &line,
2339 bool outOfRangeIndexIsError)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002340{
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002341 const TConstantUnion *unionArray = node->getUnionArrayPointer();
2342 ASSERT(unionArray);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002343
Arun Patole7e7e68d2015-05-22 12:02:25 +05302344 TConstantUnion *constArray = new TConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002345
Arun Patole7e7e68d2015-05-22 12:02:25 +05302346 for (int i = 0; i < fields.num; i++)
2347 {
2348 if (fields.offsets[i] >= node->getType().getNominalSize())
2349 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002350 std::stringstream extraInfoStream;
2351 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
2352 std::string extraInfo = extraInfoStream.str();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002353 outOfRangeError(outOfRangeIndexIsError, line, "", "[", extraInfo.c_str());
2354 fields.offsets[i] = node->getType().getNominalSize() - 1;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002355 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302356
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002357 constArray[i] = unionArray[fields.offsets[i]];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302358 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002359 return intermediate.addConstantUnion(constArray, node->getType(), line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002360}
2361
2362//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002363// This function returns the column being accessed from a constant matrix. The values are retrieved
2364// 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 +02002365// The input to the function could either be a symbol node (m[0] where m is a constant matrix)that
2366// represents a constant matrix or it could be the tree representation of the constant matrix
2367// (s.m1[0] where s is a constant structure)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002368//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002369TIntermTyped *TParseContext::addConstMatrixNode(int index,
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002370 TIntermConstantUnion *node,
2371 const TSourceLoc &line,
2372 bool outOfRangeIndexIsError)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002373{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302374 if (index >= node->getType().getCols())
2375 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002376 std::stringstream extraInfoStream;
2377 extraInfoStream << "matrix field selection out of range '" << index << "'";
2378 std::string extraInfo = extraInfoStream.str();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002379 outOfRangeError(outOfRangeIndexIsError, line, "", "[", extraInfo.c_str());
2380 index = node->getType().getCols() - 1;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002381 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002382
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002383 const TConstantUnion *unionArray = node->getUnionArrayPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002384 int size = node->getType().getCols();
2385 return intermediate.addConstantUnion(&unionArray[size * index], node->getType(), line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002386}
2387
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002388//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002389// This function returns an element of an array accessed from a constant array. The values are
2390// 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 +05302391// 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 -04002392// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a
2393// constant structure)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002394//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002395TIntermTyped *TParseContext::addConstArrayNode(int index,
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002396 TIntermConstantUnion *node,
2397 const TSourceLoc &line,
2398 bool outOfRangeIndexIsError)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002399{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002400 TType arrayElementType = node->getType();
2401 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002402
Arun Patole7e7e68d2015-05-22 12:02:25 +05302403 if (index >= node->getType().getArraySize())
2404 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002405 std::stringstream extraInfoStream;
2406 extraInfoStream << "array field selection out of range '" << index << "'";
2407 std::string extraInfo = extraInfoStream.str();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002408 outOfRangeError(outOfRangeIndexIsError, line, "", "[", extraInfo.c_str());
2409 index = node->getType().getArraySize() - 1;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002410 }
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002411 size_t arrayElementSize = arrayElementType.getObjectSize();
2412 const TConstantUnion *unionArray = node->getUnionArrayPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002413 return intermediate.addConstantUnion(&unionArray[arrayElementSize * index], node->getType(),
2414 line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002415}
2416
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002417//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002418// This function returns the value of a particular field inside a constant structure from the symbol
2419// table.
2420// If there is an embedded/nested struct, it appropriately calls addConstStructNested or
2421// addConstStructFromAggr function and returns the parse-tree with the values of the embedded/nested
2422// struct.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002423//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002424TIntermTyped *TParseContext::addConstStruct(const TString &identifier,
2425 TIntermTyped *node,
2426 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002427{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302428 const TFieldList &fields = node->getType().getStruct()->fields();
Jamie Madillb98c3a82015-07-23 14:26:04 -04002429 size_t instanceSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002430
Arun Patole7e7e68d2015-05-22 12:02:25 +05302431 for (size_t index = 0; index < fields.size(); ++index)
2432 {
2433 if (fields[index]->name() == identifier)
2434 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002435 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302436 }
2437 else
2438 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002439 instanceSize += fields[index]->type()->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002440 }
2441 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002442
Jamie Madill94bf7f22013-07-08 13:31:15 -04002443 TIntermTyped *typedNode;
2444 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
Arun Patole7e7e68d2015-05-22 12:02:25 +05302445 if (tempConstantNode)
2446 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002447 const TConstantUnion *constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002448
Jamie Madillb98c3a82015-07-23 14:26:04 -04002449 // type will be changed in the calling function
2450 typedNode = intermediate.addConstantUnion(constArray + instanceSize,
2451 tempConstantNode->getType(), line);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302452 }
2453 else
2454 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002455 error(line, "Cannot offset into the structure", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002456 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002457
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002458 return 0;
2459 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002460
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002461 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002462}
2463
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002464//
2465// Interface/uniform blocks
2466//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002467TIntermAggregate *TParseContext::addInterfaceBlock(const TPublicType &typeQualifier,
2468 const TSourceLoc &nameLine,
2469 const TString &blockName,
2470 TFieldList *fieldList,
2471 const TString *instanceName,
2472 const TSourceLoc &instanceLine,
2473 TIntermTyped *arrayIndex,
2474 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002475{
2476 if (reservedErrorCheck(nameLine, blockName))
2477 recover();
2478
2479 if (typeQualifier.qualifier != EvqUniform)
2480 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302481 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
2482 "interface blocks must be uniform");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002483 recover();
2484 }
2485
Jamie Madill099c0f32013-06-20 11:55:52 -04002486 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
2487 if (layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002488 {
2489 recover();
2490 }
2491
Jamie Madill099c0f32013-06-20 11:55:52 -04002492 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2493 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002494 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002495 }
2496
Jamie Madill1566ef72013-06-20 11:55:54 -04002497 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2498 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002499 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002500 }
2501
Arun Patole7e7e68d2015-05-22 12:02:25 +05302502 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2503 if (!symbolTable.declare(blockNameSymbol))
2504 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002505 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
2506 recover();
2507 }
2508
Jamie Madill98493dd2013-07-08 14:39:03 -04002509 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302510 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2511 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002512 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302513 TType *fieldType = field->type();
2514 if (IsSampler(fieldType->getBasicType()))
2515 {
2516 error(field->line(), "unsupported type", fieldType->getBasicString(),
2517 "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002518 recover();
2519 }
2520
Jamie Madill98493dd2013-07-08 14:39:03 -04002521 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002522 switch (qualifier)
2523 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002524 case EvqGlobal:
2525 case EvqUniform:
2526 break;
2527 default:
2528 error(field->line(), "invalid qualifier on interface block member",
2529 getQualifierString(qualifier));
2530 recover();
2531 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002532 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002533
2534 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002535 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
2536 if (layoutLocationErrorCheck(field->line(), fieldLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002537 {
2538 recover();
2539 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002540
Jamie Madill98493dd2013-07-08 14:39:03 -04002541 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002542 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002543 error(field->line(), "invalid layout qualifier:",
2544 getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04002545 recover();
2546 }
2547
Jamie Madill98493dd2013-07-08 14:39:03 -04002548 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002549 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002550 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002551 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002552 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002553 {
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002554 warning(field->line(), "extraneous layout qualifier:",
Jamie Madillb98c3a82015-07-23 14:26:04 -04002555 getMatrixPackingString(fieldLayoutQualifier.matrixPacking),
2556 "only has an effect on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04002557 }
2558
Jamie Madill98493dd2013-07-08 14:39:03 -04002559 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002560 }
2561
Jamie Madill98493dd2013-07-08 14:39:03 -04002562 // add array index
2563 int arraySize = 0;
2564 if (arrayIndex != NULL)
2565 {
2566 if (arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize))
2567 recover();
2568 }
2569
Jamie Madillb98c3a82015-07-23 14:26:04 -04002570 TInterfaceBlock *interfaceBlock =
2571 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2572 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
2573 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002574
2575 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04002576 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002577
Jamie Madill98493dd2013-07-08 14:39:03 -04002578 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002579 {
2580 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002581 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2582 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002583 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302584 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002585
2586 // set parent pointer of the field variable
2587 fieldType->setInterfaceBlock(interfaceBlock);
2588
Arun Patole7e7e68d2015-05-22 12:02:25 +05302589 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002590 fieldVariable->setQualifier(typeQualifier.qualifier);
2591
Arun Patole7e7e68d2015-05-22 12:02:25 +05302592 if (!symbolTable.declare(fieldVariable))
2593 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002594 error(field->line(), "redefinition", field->name().c_str(),
2595 "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002596 recover();
2597 }
2598 }
2599 }
2600 else
2601 {
Olli Etuahoe0f623a2015-07-10 11:58:30 +03002602 if (reservedErrorCheck(instanceLine, *instanceName))
2603 recover();
2604
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002605 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302606 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002607 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002608
Arun Patole7e7e68d2015-05-22 12:02:25 +05302609 if (!symbolTable.declare(instanceTypeDef))
2610 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002611 error(instanceLine, "redefinition", instanceName->c_str(),
2612 "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002613 recover();
2614 }
2615
Jamie Madillb98c3a82015-07-23 14:26:04 -04002616 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002617 symbolName = instanceTypeDef->getName();
2618 }
2619
Jamie Madillb98c3a82015-07-23 14:26:04 -04002620 TIntermAggregate *aggregate = intermediate.makeAggregate(
2621 intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line),
2622 nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002623 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04002624
2625 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002626 return aggregate;
2627}
2628
Arun Patole7e7e68d2015-05-22 12:02:25 +05302629bool TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002630{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002631 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002632
2633 // Embedded structure definitions are not supported per GLSL ES spec.
2634 // They aren't allowed in GLSL either, but we need to detect this here
2635 // so we don't rely on the GLSL compiler to catch it.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302636 if (mStructNestingLevel > 1)
2637 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002638 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002639 return true;
2640 }
2641
2642 return false;
2643}
2644
2645void TParseContext::exitStructDeclaration()
2646{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002647 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002648}
2649
Jamie Madillb98c3a82015-07-23 14:26:04 -04002650namespace
2651{
kbr@chromium.org476541f2011-10-27 21:14:51 +00002652const int kWebGLMaxStructNesting = 4;
2653
2654} // namespace
2655
Arun Patole7e7e68d2015-05-22 12:02:25 +05302656bool TParseContext::structNestingErrorCheck(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002657{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302658 if (!IsWebGLBasedSpec(mShaderSpec))
2659 {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002660 return false;
2661 }
2662
Arun Patole7e7e68d2015-05-22 12:02:25 +05302663 if (field.type()->getBasicType() != EbtStruct)
2664 {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002665 return false;
2666 }
2667
2668 // We're already inside a structure definition at this point, so add
2669 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302670 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2671 {
Jamie Madill41a49272014-03-18 16:10:13 -04002672 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002673 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
2674 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04002675 std::string reason = reasonStream.str();
2676 error(line, reason.c_str(), field.name().c_str(), "");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002677 return true;
2678 }
2679
2680 return false;
2681}
2682
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002683//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002684// Parse an array index expression
2685//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002686TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
2687 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302688 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002689{
2690 TIntermTyped *indexedExpression = NULL;
2691
2692 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2693 {
2694 if (baseExpression->getAsSymbolNode())
2695 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302696 error(location, " left of '[' is not of type array, matrix, or vector ",
2697 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002698 }
2699 else
2700 {
2701 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2702 }
2703 recover();
2704 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002705
Jamie Madill21c1e452014-12-29 11:33:41 -05002706 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2707
Olli Etuaho36b05142015-11-12 13:10:42 +02002708 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
2709 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
2710 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
2711 // index is a constant expression.
2712 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
2713 {
2714 if (baseExpression->isInterfaceBlock())
2715 {
2716 error(
2717 location, "", "[",
2718 "array indexes for interface blocks arrays must be constant integral expressions");
2719 recover();
2720 }
2721 else if (baseExpression->getQualifier() == EvqFragmentOut)
2722 {
2723 error(location, "", "[",
2724 "array indexes for fragment outputs must be constant integral expressions");
2725 recover();
2726 }
Olli Etuaho3e960462015-11-12 15:58:39 +02002727 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
2728 {
2729 error(location, "", "[", "array index for gl_FragData must be constant zero");
2730 recover();
2731 }
Olli Etuaho36b05142015-11-12 13:10:42 +02002732 }
2733
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002734 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002735 {
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002736 // If the index is not qualified as constant, the behavior in the spec is undefined. This
2737 // applies even if ANGLE has been able to constant fold it (ANGLE may constant fold
2738 // expressions that are not constant expressions). The most compatible way to handle this
2739 // case is to report a warning instead of an error and force the index to be in the
2740 // correct range.
2741 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Jamie Madill21c1e452014-12-29 11:33:41 -05002742 int index = indexConstantUnion->getIConst(0);
Jamie Madill7164cf42013-07-08 13:30:59 -04002743 if (index < 0)
2744 {
2745 std::stringstream infoStream;
2746 infoStream << index;
2747 std::string info = infoStream.str();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002748 outOfRangeError(outOfRangeIndexIsError, location, "negative index", info.c_str());
Jamie Madill7164cf42013-07-08 13:30:59 -04002749 index = 0;
2750 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002751 TIntermConstantUnion *baseConstantUnion = baseExpression->getAsConstantUnion();
2752 if (baseConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002753 {
2754 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002755 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002756 // constant folding for array indexing
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002757 indexedExpression =
2758 addConstArrayNode(index, baseConstantUnion, location, outOfRangeIndexIsError);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002759 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002760 else if (baseExpression->isVector())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002761 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002762 // constant folding for vector indexing
Jamie Madill7164cf42013-07-08 13:30:59 -04002763 TVectorFields fields;
2764 fields.num = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002765 fields.offsets[0] =
2766 index; // need to do it this way because v.xy sends fields integer array
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002767 indexedExpression =
2768 addConstVectorNode(fields, baseConstantUnion, location, outOfRangeIndexIsError);
Jamie Madill7164cf42013-07-08 13:30:59 -04002769 }
2770 else if (baseExpression->isMatrix())
2771 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002772 // constant folding for matrix indexing
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002773 indexedExpression =
2774 addConstMatrixNode(index, baseConstantUnion, location, outOfRangeIndexIsError);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002775 }
2776 }
2777 else
2778 {
Jamie Madillb11e2482015-05-04 14:21:22 -04002779 int safeIndex = -1;
2780
Jamie Madill7164cf42013-07-08 13:30:59 -04002781 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002782 {
Olli Etuaho3e960462015-11-12 15:58:39 +02002783 if (baseExpression->getQualifier() == EvqFragData && index > 0)
2784 {
2785 if (mShaderSpec == SH_WEBGL2_SPEC)
2786 {
2787 // Error has been already generated if index is not const.
2788 if (indexExpression->getQualifier() == EvqConst)
2789 {
2790 error(location, "", "[",
2791 "array index for gl_FragData must be constant zero");
2792 recover();
2793 }
2794 safeIndex = 0;
2795 }
2796 else if (!isExtensionEnabled("GL_EXT_draw_buffers"))
2797 {
2798 outOfRangeError(outOfRangeIndexIsError, location, "", "[",
2799 "array index for gl_FragData must be zero when "
2800 "GL_EXT_draw_buffers is disabled");
2801 safeIndex = 0;
2802 }
2803 }
2804 // Only do generic out-of-range check if similar error hasn't already been reported.
2805 if (safeIndex < 0 && index >= baseExpression->getType().getArraySize())
Jamie Madill7164cf42013-07-08 13:30:59 -04002806 {
2807 std::stringstream extraInfoStream;
2808 extraInfoStream << "array index out of range '" << index << "'";
2809 std::string extraInfo = extraInfoStream.str();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002810 outOfRangeError(outOfRangeIndexIsError, location, "", "[", extraInfo.c_str());
Jamie Madillb11e2482015-05-04 14:21:22 -04002811 safeIndex = baseExpression->getType().getArraySize() - 1;
Jamie Madill7164cf42013-07-08 13:30:59 -04002812 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002813 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302814 else if ((baseExpression->isVector() || baseExpression->isMatrix()) &&
Jamie Madillb98c3a82015-07-23 14:26:04 -04002815 baseExpression->getType().getNominalSize() <= index)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002816 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002817 std::stringstream extraInfoStream;
2818 extraInfoStream << "field selection out of range '" << index << "'";
2819 std::string extraInfo = extraInfoStream.str();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002820 outOfRangeError(outOfRangeIndexIsError, location, "", "[", extraInfo.c_str());
Jamie Madillb11e2482015-05-04 14:21:22 -04002821 safeIndex = baseExpression->getType().getNominalSize() - 1;
Jamie Madill46131a32013-06-20 11:55:50 -04002822 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002823
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002824 // Data of constant unions can't be changed, because it may be shared with other
2825 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
2826 // sanitized object.
Jamie Madillb11e2482015-05-04 14:21:22 -04002827 if (safeIndex != -1)
2828 {
2829 TConstantUnion *safeConstantUnion = new TConstantUnion();
2830 safeConstantUnion->setIConst(safeIndex);
2831 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
2832 }
2833
Jamie Madillb98c3a82015-07-23 14:26:04 -04002834 indexedExpression =
2835 intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002836 }
2837 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002838 else
2839 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002840 indexedExpression =
2841 intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
Jamie Madill7164cf42013-07-08 13:30:59 -04002842 }
2843
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002844 if (indexedExpression == 0)
2845 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002846 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002847 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002848 indexedExpression =
2849 intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002850 }
2851 else if (baseExpression->isArray())
2852 {
Olli Etuahob3fbd862015-09-30 17:55:02 +03002853 TType indexedType = baseExpression->getType();
2854 indexedType.clearArrayness();
2855 indexedExpression->setType(indexedType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002856 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002857 else if (baseExpression->isMatrix())
2858 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002859 indexedExpression->setType(TType(baseExpression->getBasicType(),
Olli Etuahob3fbd862015-09-30 17:55:02 +03002860 baseExpression->getPrecision(), EvqTemporary,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002861 static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002862 }
2863 else if (baseExpression->isVector())
2864 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002865 indexedExpression->setType(
Olli Etuahob3fbd862015-09-30 17:55:02 +03002866 TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002867 }
2868 else
2869 {
2870 indexedExpression->setType(baseExpression->getType());
2871 }
2872
Olli Etuahob3fbd862015-09-30 17:55:02 +03002873 if (baseExpression->getType().getQualifier() == EvqConst &&
2874 indexExpression->getType().getQualifier() == EvqConst)
2875 {
2876 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2877 }
2878 else
2879 {
2880 indexedExpression->getTypePointer()->setQualifier(EvqTemporary);
2881 }
2882
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002883 return indexedExpression;
2884}
2885
Jamie Madillb98c3a82015-07-23 14:26:04 -04002886TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
2887 const TSourceLoc &dotLocation,
2888 const TString &fieldString,
2889 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002890{
2891 TIntermTyped *indexedExpression = NULL;
2892
2893 if (baseExpression->isArray())
2894 {
2895 error(fieldLocation, "cannot apply dot operator to an array", ".");
2896 recover();
2897 }
2898
2899 if (baseExpression->isVector())
2900 {
2901 TVectorFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002902 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields,
2903 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002904 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002905 fields.num = 1;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002906 fields.offsets[0] = 0;
2907 recover();
2908 }
2909
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002910 if (baseExpression->getAsConstantUnion())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002911 {
2912 // constant folding for vector fields
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002913 indexedExpression = addConstVectorNode(fields, baseExpression->getAsConstantUnion(),
2914 fieldLocation, true);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002915 }
2916 else
2917 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302918 TIntermTyped *index = intermediate.addSwizzle(fields, fieldLocation);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002919 indexedExpression =
2920 intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002921 }
2922 if (indexedExpression == nullptr)
2923 {
2924 recover();
2925 indexedExpression = baseExpression;
2926 }
2927 else
2928 {
2929 // Note that the qualifier set here will be corrected later.
Jamie Madillb98c3a82015-07-23 14:26:04 -04002930 indexedExpression->setType(TType(baseExpression->getBasicType(),
2931 baseExpression->getPrecision(), EvqTemporary,
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002932 (unsigned char)(fieldString).size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002933 }
2934 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002935 else if (baseExpression->getBasicType() == EbtStruct)
2936 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002937 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302938 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04002939 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002940 {
2941 error(dotLocation, "structure has no fields", "Internal Error");
2942 recover();
2943 indexedExpression = baseExpression;
2944 }
2945 else
2946 {
2947 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002948 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002949 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002950 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002951 {
2952 fieldFound = true;
2953 break;
2954 }
2955 }
2956 if (fieldFound)
2957 {
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002958 if (baseExpression->getAsConstantUnion())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002959 {
2960 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2961 if (indexedExpression == 0)
2962 {
2963 recover();
2964 indexedExpression = baseExpression;
2965 }
2966 else
2967 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002968 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002969 }
2970 }
2971 else
2972 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002973 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002974 unionArray->setIConst(i);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002975 TIntermTyped *index = intermediate.addConstantUnion(
2976 unionArray, *fields[i]->type(), fieldLocation);
2977 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression,
2978 index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002979 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002980 }
2981 }
2982 else
2983 {
2984 error(dotLocation, " no such field in structure", fieldString.c_str());
2985 recover();
2986 indexedExpression = baseExpression;
2987 }
2988 }
2989 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002990 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002991 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002992 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302993 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04002994 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002995 {
2996 error(dotLocation, "interface block has no fields", "Internal Error");
2997 recover();
2998 indexedExpression = baseExpression;
2999 }
3000 else
3001 {
3002 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003003 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003004 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003005 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003006 {
3007 fieldFound = true;
3008 break;
3009 }
3010 }
3011 if (fieldFound)
3012 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003013 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003014 unionArray->setIConst(i);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003015 TIntermTyped *index =
3016 intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
3017 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock,
3018 baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04003019 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003020 }
3021 else
3022 {
3023 error(dotLocation, " no such field in interface block", fieldString.c_str());
3024 recover();
3025 indexedExpression = baseExpression;
3026 }
3027 }
3028 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003029 else
3030 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003031 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003032 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03003033 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303034 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003035 }
3036 else
3037 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303038 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003039 " field selection requires structure, vector, or interface block on left hand "
3040 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303041 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003042 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003043 recover();
3044 indexedExpression = baseExpression;
3045 }
3046
Olli Etuahob1edc4f2015-11-02 17:20:03 +02003047 if (baseExpression->getQualifier() == EvqConst)
3048 {
3049 indexedExpression->getTypePointer()->setQualifier(EvqConst);
3050 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003051 else
3052 {
3053 indexedExpression->getTypePointer()->setQualifier(EvqTemporary);
3054 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02003055
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003056 return indexedExpression;
3057}
3058
Jamie Madillb98c3a82015-07-23 14:26:04 -04003059TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3060 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003061{
Jamie Madilla5efff92013-06-06 11:56:47 -04003062 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003063
Jamie Madillb98c3a82015-07-23 14:26:04 -04003064 qualifier.location = -1;
Jamie Madilla5efff92013-06-06 11:56:47 -04003065 qualifier.matrixPacking = EmpUnspecified;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003066 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003067
3068 if (qualifierType == "shared")
3069 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003070 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003071 }
3072 else if (qualifierType == "packed")
3073 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003074 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003075 }
3076 else if (qualifierType == "std140")
3077 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003078 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003079 }
3080 else if (qualifierType == "row_major")
3081 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003082 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003083 }
3084 else if (qualifierType == "column_major")
3085 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003086 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003087 }
3088 else if (qualifierType == "location")
3089 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003090 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
3091 "location requires an argument");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003092 recover();
3093 }
3094 else
3095 {
3096 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
3097 recover();
3098 }
3099
Jamie Madilla5efff92013-06-06 11:56:47 -04003100 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003101}
3102
Jamie Madillb98c3a82015-07-23 14:26:04 -04003103TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3104 const TSourceLoc &qualifierTypeLine,
3105 const TString &intValueString,
3106 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303107 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003108{
Jamie Madilla5efff92013-06-06 11:56:47 -04003109 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003110
Jamie Madillb98c3a82015-07-23 14:26:04 -04003111 qualifier.location = -1;
Jamie Madilla5efff92013-06-06 11:56:47 -04003112 qualifier.matrixPacking = EmpUnspecified;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003113 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003114
3115 if (qualifierType != "location")
3116 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303117 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
3118 "only location may have arguments");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003119 recover();
3120 }
3121 else
3122 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003123 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003124 if (intValue < 0)
3125 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003126 error(intValueLine, "out of range:", intValueString.c_str(),
3127 "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003128 recover();
3129 }
3130 else
3131 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003132 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003133 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003134 }
3135
Jamie Madilla5efff92013-06-06 11:56:47 -04003136 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003137}
3138
Jamie Madillb98c3a82015-07-23 14:26:04 -04003139TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
3140 TLayoutQualifier rightQualifier)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003141{
Jamie Madilla5efff92013-06-06 11:56:47 -04003142 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003143
Jamie Madilla5efff92013-06-06 11:56:47 -04003144 if (rightQualifier.location != -1)
3145 {
3146 joinedQualifier.location = rightQualifier.location;
3147 }
3148 if (rightQualifier.matrixPacking != EmpUnspecified)
3149 {
3150 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
3151 }
3152 if (rightQualifier.blockStorage != EbsUnspecified)
3153 {
3154 joinedQualifier.blockStorage = rightQualifier.blockStorage;
3155 }
3156
3157 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003158}
3159
Arun Patole7e7e68d2015-05-22 12:02:25 +05303160TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc,
3161 TQualifier interpolationQualifier,
3162 const TSourceLoc &storageLoc,
3163 TQualifier storageQualifier)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003164{
3165 TQualifier mergedQualifier = EvqSmoothIn;
3166
Arun Patole7e7e68d2015-05-22 12:02:25 +05303167 if (storageQualifier == EvqFragmentIn)
3168 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003169 if (interpolationQualifier == EvqSmooth)
3170 mergedQualifier = EvqSmoothIn;
3171 else if (interpolationQualifier == EvqFlat)
3172 mergedQualifier = EvqFlatIn;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003173 else
3174 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003175 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303176 else if (storageQualifier == EvqCentroidIn)
3177 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003178 if (interpolationQualifier == EvqSmooth)
3179 mergedQualifier = EvqCentroidIn;
3180 else if (interpolationQualifier == EvqFlat)
3181 mergedQualifier = EvqFlatIn;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003182 else
3183 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003184 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303185 else if (storageQualifier == EvqVertexOut)
3186 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003187 if (interpolationQualifier == EvqSmooth)
3188 mergedQualifier = EvqSmoothOut;
3189 else if (interpolationQualifier == EvqFlat)
3190 mergedQualifier = EvqFlatOut;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003191 else
3192 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003193 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303194 else if (storageQualifier == EvqCentroidOut)
3195 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003196 if (interpolationQualifier == EvqSmooth)
3197 mergedQualifier = EvqCentroidOut;
3198 else if (interpolationQualifier == EvqFlat)
3199 mergedQualifier = EvqFlatOut;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003200 else
3201 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003202 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303203 else
3204 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003205 error(interpolationLoc,
3206 "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303207 getInterpolationString(interpolationQualifier));
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003208 recover();
3209
3210 mergedQualifier = storageQualifier;
3211 }
3212
3213 TPublicType type;
3214 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
3215 return type;
3216}
3217
Jamie Madillb98c3a82015-07-23 14:26:04 -04003218TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3219 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003220{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03003221 if (voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier.type))
3222 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003223 recover();
3224 }
3225
Arun Patole7e7e68d2015-05-22 12:02:25 +05303226 for (unsigned int i = 0; i < fieldList->size(); ++i)
3227 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003228 //
3229 // Careful not to replace already known aspects of type, like array-ness
3230 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303231 TType *type = (*fieldList)[i]->type();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003232 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003233 type->setPrimarySize(typeSpecifier.primarySize);
3234 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003235 type->setPrecision(typeSpecifier.precision);
3236 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003237 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003238
3239 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303240 if (type->isArray())
3241 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003242 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
3243 recover();
3244 }
3245 if (typeSpecifier.array)
3246 type->setArraySize(typeSpecifier.arraySize);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303247 if (typeSpecifier.userDef)
3248 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003249 type->setStruct(typeSpecifier.userDef->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003250 }
3251
Arun Patole7e7e68d2015-05-22 12:02:25 +05303252 if (structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i]))
3253 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003254 recover();
3255 }
3256 }
3257
Jamie Madill98493dd2013-07-08 14:39:03 -04003258 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003259}
3260
Jamie Madillb98c3a82015-07-23 14:26:04 -04003261TPublicType TParseContext::addStructure(const TSourceLoc &structLine,
3262 const TSourceLoc &nameLine,
3263 const TString *structName,
3264 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003265{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303266 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003267 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003268
Jamie Madill9b820842015-02-12 10:40:10 -05003269 // Store a bool in the struct if we're at global scope, to allow us to
3270 // skip the local struct scoping workaround in HLSL.
Jamie Madillb960cc42015-02-12 15:33:20 +00003271 structure->setUniqueId(TSymbolTable::nextUniqueId());
Jamie Madill9b820842015-02-12 10:40:10 -05003272 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003273
Jamie Madill98493dd2013-07-08 14:39:03 -04003274 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003275 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003276 if (reservedErrorCheck(nameLine, *structName))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003277 {
3278 recover();
3279 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303280 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
3281 if (!symbolTable.declare(userTypeDef))
3282 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003283 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003284 recover();
3285 }
3286 }
3287
3288 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04003289 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003290 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003291 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04003292 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003293 switch (qualifier)
3294 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003295 case EvqGlobal:
3296 case EvqTemporary:
3297 break;
3298 default:
3299 error(field.line(), "invalid qualifier on struct member",
3300 getQualifierString(qualifier));
3301 recover();
3302 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003303 }
3304 }
3305
3306 TPublicType publicType;
3307 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003308 publicType.userDef = structureType;
Olli Etuahobd163f62015-11-13 12:15:38 +02003309 publicType.isStructSpecifier = true;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003310 exitStructDeclaration();
3311
3312 return publicType;
3313}
3314
Jamie Madillb98c3a82015-07-23 14:26:04 -04003315TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
3316 TIntermAggregate *statementList,
3317 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02003318{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003319 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003320 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02003321 init->isVector())
3322 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003323 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
3324 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003325 recover();
3326 return nullptr;
3327 }
3328
Olli Etuahoac5274d2015-02-20 10:19:08 +02003329 if (statementList)
3330 {
3331 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
3332 {
3333 recover();
3334 return nullptr;
3335 }
3336 }
3337
Olli Etuahoa3a36662015-02-17 13:46:51 +02003338 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3339 if (node == nullptr)
3340 {
3341 error(loc, "erroneous switch statement", "switch");
3342 recover();
3343 return nullptr;
3344 }
3345 return node;
3346}
3347
3348TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3349{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003350 if (mSwitchNestingLevel == 0)
3351 {
3352 error(loc, "case labels need to be inside switch statements", "case");
3353 recover();
3354 return nullptr;
3355 }
3356 if (condition == nullptr)
3357 {
3358 error(loc, "case label must have a condition", "case");
3359 recover();
3360 return nullptr;
3361 }
3362 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04003363 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02003364 {
3365 error(condition->getLine(), "case label must be a scalar integer", "case");
3366 recover();
3367 }
3368 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003369 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
3370 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
3371 // fold in case labels.
3372 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02003373 {
3374 error(condition->getLine(), "case label must be constant", "case");
3375 recover();
3376 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003377 TIntermCase *node = intermediate.addCase(condition, loc);
3378 if (node == nullptr)
3379 {
3380 error(loc, "erroneous case statement", "case");
3381 recover();
3382 return nullptr;
3383 }
3384 return node;
3385}
3386
3387TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3388{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003389 if (mSwitchNestingLevel == 0)
3390 {
3391 error(loc, "default labels need to be inside switch statements", "default");
3392 recover();
3393 return nullptr;
3394 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003395 TIntermCase *node = intermediate.addCase(nullptr, loc);
3396 if (node == nullptr)
3397 {
3398 error(loc, "erroneous default statement", "default");
3399 recover();
3400 return nullptr;
3401 }
3402 return node;
3403}
3404
Jamie Madillb98c3a82015-07-23 14:26:04 -04003405TIntermTyped *TParseContext::createUnaryMath(TOperator op,
3406 TIntermTyped *child,
3407 const TSourceLoc &loc,
3408 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003409{
3410 if (child == nullptr)
3411 {
3412 return nullptr;
3413 }
3414
3415 switch (op)
3416 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003417 case EOpLogicalNot:
3418 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
3419 child->isVector())
3420 {
3421 return nullptr;
3422 }
3423 break;
3424 case EOpBitwiseNot:
3425 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3426 child->isMatrix() || child->isArray())
3427 {
3428 return nullptr;
3429 }
3430 break;
3431 case EOpPostIncrement:
3432 case EOpPreIncrement:
3433 case EOpPostDecrement:
3434 case EOpPreDecrement:
3435 case EOpNegative:
3436 case EOpPositive:
3437 if (child->getBasicType() == EbtStruct || child->getBasicType() == EbtBool ||
3438 child->isArray())
3439 {
3440 return nullptr;
3441 }
3442 // Operators for built-ins are already type checked against their prototype.
3443 default:
3444 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003445 }
3446
Olli Etuahof6c694b2015-03-26 14:50:53 +02003447 return intermediate.addUnaryMath(op, child, loc, funcReturnType);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003448}
3449
Olli Etuaho09b22472015-02-11 11:47:26 +02003450TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3451{
Olli Etuahof6c694b2015-03-26 14:50:53 +02003452 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003453 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003454 {
3455 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
3456 recover();
3457 return child;
3458 }
3459 return node;
3460}
3461
Jamie Madillb98c3a82015-07-23 14:26:04 -04003462TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
3463 TIntermTyped *child,
3464 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003465{
3466 if (lValueErrorCheck(loc, GetOperatorString(op), child))
3467 recover();
3468 return addUnaryMath(op, child, loc);
3469}
3470
Jamie Madillb98c3a82015-07-23 14:26:04 -04003471bool TParseContext::binaryOpCommonCheck(TOperator op,
3472 TIntermTyped *left,
3473 TIntermTyped *right,
3474 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003475{
3476 if (left->isArray() || right->isArray())
3477 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003478 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003479 {
3480 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3481 return false;
3482 }
3483
3484 if (left->isArray() != right->isArray())
3485 {
3486 error(loc, "array / non-array mismatch", GetOperatorString(op));
3487 return false;
3488 }
3489
3490 switch (op)
3491 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003492 case EOpEqual:
3493 case EOpNotEqual:
3494 case EOpAssign:
3495 case EOpInitialize:
3496 break;
3497 default:
3498 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3499 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02003500 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003501 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003502 if (left->getArraySize() != right->getArraySize())
3503 {
3504 error(loc, "array size mismatch", GetOperatorString(op));
3505 return false;
3506 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003507 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003508
3509 // Check ops which require integer / ivec parameters
3510 bool isBitShift = false;
3511 switch (op)
3512 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003513 case EOpBitShiftLeft:
3514 case EOpBitShiftRight:
3515 case EOpBitShiftLeftAssign:
3516 case EOpBitShiftRightAssign:
3517 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3518 // check that the basic type is an integer type.
3519 isBitShift = true;
3520 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3521 {
3522 return false;
3523 }
3524 break;
3525 case EOpBitwiseAnd:
3526 case EOpBitwiseXor:
3527 case EOpBitwiseOr:
3528 case EOpBitwiseAndAssign:
3529 case EOpBitwiseXorAssign:
3530 case EOpBitwiseOrAssign:
3531 // It is enough to check the type of only one operand, since later it
3532 // is checked that the operand types match.
3533 if (!IsInteger(left->getBasicType()))
3534 {
3535 return false;
3536 }
3537 break;
3538 default:
3539 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003540 }
3541
3542 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3543 // So the basic type should usually match.
3544 if (!isBitShift && left->getBasicType() != right->getBasicType())
3545 {
3546 return false;
3547 }
3548
Olli Etuaho9dd217b2015-03-20 14:24:31 +02003549 // Check that type sizes match exactly on ops that require that.
Olli Etuahoff699002015-03-23 14:38:42 +02003550 // Also check restrictions for structs that contain arrays or samplers.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003551 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003552 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003553 case EOpAssign:
3554 case EOpInitialize:
3555 case EOpEqual:
3556 case EOpNotEqual:
3557 // ESSL 1.00 sections 5.7, 5.8, 5.9
3558 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
3559 {
3560 error(loc, "undefined operation for structs containing arrays",
3561 GetOperatorString(op));
3562 return false;
3563 }
3564 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3565 // we interpret the spec so that this extends to structs containing samplers,
3566 // similarly to ESSL 1.00 spec.
3567 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
3568 left->getType().isStructureContainingSamplers())
3569 {
3570 error(loc, "undefined operation for structs containing samplers",
3571 GetOperatorString(op));
3572 return false;
3573 }
3574 case EOpLessThan:
3575 case EOpGreaterThan:
3576 case EOpLessThanEqual:
3577 case EOpGreaterThanEqual:
3578 if ((left->getNominalSize() != right->getNominalSize()) ||
3579 (left->getSecondarySize() != right->getSecondarySize()))
3580 {
3581 return false;
3582 }
3583 default:
3584 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003585 }
3586
Olli Etuahod6b14282015-03-17 14:31:35 +02003587 return true;
3588}
3589
Jamie Madillb98c3a82015-07-23 14:26:04 -04003590TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
3591 TIntermTyped *left,
3592 TIntermTyped *right,
3593 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02003594{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003595 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003596 return nullptr;
3597
Olli Etuahofc1806e2015-03-17 13:03:11 +02003598 switch (op)
3599 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003600 case EOpEqual:
3601 case EOpNotEqual:
3602 break;
3603 case EOpLessThan:
3604 case EOpGreaterThan:
3605 case EOpLessThanEqual:
3606 case EOpGreaterThanEqual:
3607 ASSERT(!left->isArray() && !right->isArray());
3608 if (left->isMatrix() || left->isVector() || left->getBasicType() == EbtStruct)
3609 {
3610 return nullptr;
3611 }
3612 break;
3613 case EOpLogicalOr:
3614 case EOpLogicalXor:
3615 case EOpLogicalAnd:
3616 ASSERT(!left->isArray() && !right->isArray());
3617 if (left->getBasicType() != EbtBool || left->isMatrix() || left->isVector())
3618 {
3619 return nullptr;
3620 }
3621 break;
3622 case EOpAdd:
3623 case EOpSub:
3624 case EOpDiv:
3625 case EOpMul:
3626 ASSERT(!left->isArray() && !right->isArray());
3627 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool)
3628 {
3629 return nullptr;
3630 }
3631 break;
3632 case EOpIMod:
3633 ASSERT(!left->isArray() && !right->isArray());
3634 // Note that this is only for the % operator, not for mod()
3635 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool ||
3636 left->getBasicType() == EbtFloat)
3637 {
3638 return nullptr;
3639 }
3640 break;
3641 // Note that for bitwise ops, type checking is done in promote() to
3642 // share code between ops and compound assignment
3643 default:
3644 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003645 }
3646
Olli Etuahofc1806e2015-03-17 13:03:11 +02003647 return intermediate.addBinaryMath(op, left, right, loc);
3648}
3649
Jamie Madillb98c3a82015-07-23 14:26:04 -04003650TIntermTyped *TParseContext::addBinaryMath(TOperator op,
3651 TIntermTyped *left,
3652 TIntermTyped *right,
3653 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003654{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003655 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003656 if (node == 0)
3657 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003658 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3659 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003660 recover();
3661 return left;
3662 }
3663 return node;
3664}
3665
Jamie Madillb98c3a82015-07-23 14:26:04 -04003666TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
3667 TIntermTyped *left,
3668 TIntermTyped *right,
3669 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003670{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003671 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003672 if (node == 0)
3673 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003674 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3675 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003676 recover();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003677 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02003678 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003679 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
3680 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003681 }
3682 return node;
3683}
3684
Jamie Madillb98c3a82015-07-23 14:26:04 -04003685TIntermTyped *TParseContext::createAssign(TOperator op,
3686 TIntermTyped *left,
3687 TIntermTyped *right,
3688 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003689{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003690 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003691 {
3692 return intermediate.addAssign(op, left, right, loc);
3693 }
3694 return nullptr;
3695}
3696
Jamie Madillb98c3a82015-07-23 14:26:04 -04003697TIntermTyped *TParseContext::addAssign(TOperator op,
3698 TIntermTyped *left,
3699 TIntermTyped *right,
3700 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003701{
3702 TIntermTyped *node = createAssign(op, left, right, loc);
3703 if (node == nullptr)
3704 {
3705 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
3706 recover();
3707 return left;
3708 }
3709 return node;
3710}
3711
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003712TIntermTyped *TParseContext::addComma(TIntermTyped *left,
3713 TIntermTyped *right,
3714 const TSourceLoc &loc)
3715{
Olli Etuaho15200042015-11-04 16:56:31 +02003716 return intermediate.addComma(left, right, loc, mShaderVersion);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003717}
3718
Olli Etuaho49300862015-02-20 14:54:49 +02003719TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3720{
3721 switch (op)
3722 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003723 case EOpContinue:
3724 if (mLoopNestingLevel <= 0)
3725 {
3726 error(loc, "continue statement only allowed in loops", "");
3727 recover();
3728 }
3729 break;
3730 case EOpBreak:
3731 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
3732 {
3733 error(loc, "break statement only allowed in loops and switch statements", "");
3734 recover();
3735 }
3736 break;
3737 case EOpReturn:
3738 if (mCurrentFunctionType->getBasicType() != EbtVoid)
3739 {
3740 error(loc, "non-void function must return a value", "return");
3741 recover();
3742 }
3743 break;
3744 default:
3745 // No checks for discard
3746 break;
Olli Etuaho49300862015-02-20 14:54:49 +02003747 }
3748 return intermediate.addBranch(op, loc);
3749}
3750
Jamie Madillb98c3a82015-07-23 14:26:04 -04003751TIntermBranch *TParseContext::addBranch(TOperator op,
3752 TIntermTyped *returnValue,
3753 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02003754{
3755 ASSERT(op == EOpReturn);
3756 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003757 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02003758 {
3759 error(loc, "void function cannot return a value", "return");
3760 recover();
3761 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003762 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02003763 {
3764 error(loc, "function return is not matching type:", "return");
3765 recover();
3766 }
3767 return intermediate.addBranch(op, returnValue, loc);
3768}
3769
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003770void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
3771{
3772 ASSERT(!functionCall->isUserDefined());
3773 const TString &name = functionCall->getName();
3774 TIntermNode *offset = nullptr;
3775 TIntermSequence *arguments = functionCall->getSequence();
3776 if (name.compare(0, 16, "texelFetchOffset") == 0 ||
3777 name.compare(0, 16, "textureLodOffset") == 0 ||
3778 name.compare(0, 20, "textureProjLodOffset") == 0 ||
3779 name.compare(0, 17, "textureGradOffset") == 0 ||
3780 name.compare(0, 21, "textureProjGradOffset") == 0)
3781 {
3782 offset = arguments->back();
3783 }
3784 else if (name.compare(0, 13, "textureOffset") == 0 ||
3785 name.compare(0, 17, "textureProjOffset") == 0)
3786 {
3787 // A bias parameter might follow the offset parameter.
3788 ASSERT(arguments->size() >= 3);
3789 offset = (*arguments)[2];
3790 }
3791 if (offset != nullptr)
3792 {
3793 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
3794 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
3795 {
3796 TString unmangledName = TFunction::unmangleName(name);
3797 error(functionCall->getLine(), "Texture offset must be a constant expression",
3798 unmangledName.c_str());
3799 recover();
3800 }
3801 else
3802 {
3803 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
3804 size_t size = offsetConstantUnion->getType().getObjectSize();
3805 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
3806 for (size_t i = 0u; i < size; ++i)
3807 {
3808 int offsetValue = values[i].getIConst();
3809 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
3810 {
3811 std::stringstream tokenStream;
3812 tokenStream << offsetValue;
3813 std::string token = tokenStream.str();
3814 error(offset->getLine(), "Texture offset value out of valid range",
3815 token.c_str());
3816 recover();
3817 }
3818 }
3819 }
3820 }
3821}
3822
Jamie Madillb98c3a82015-07-23 14:26:04 -04003823TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
3824 TIntermNode *paramNode,
3825 TIntermNode *thisNode,
3826 const TSourceLoc &loc,
3827 bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003828{
Jamie Madillb98c3a82015-07-23 14:26:04 -04003829 *fatalError = false;
3830 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003831 TIntermTyped *callNode = nullptr;
3832
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003833 if (thisNode != nullptr)
3834 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003835 TConstantUnion *unionArray = new TConstantUnion[1];
Jamie Madillb98c3a82015-07-23 14:26:04 -04003836 int arraySize = 0;
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003837 TIntermTyped *typedThis = thisNode->getAsTyped();
3838 if (fnCall->getName() != "length")
3839 {
3840 error(loc, "invalid method", fnCall->getName().c_str());
3841 recover();
3842 }
3843 else if (paramNode != nullptr)
3844 {
3845 error(loc, "method takes no parameters", "length");
3846 recover();
3847 }
3848 else if (typedThis == nullptr || !typedThis->isArray())
3849 {
3850 error(loc, "length can only be called on arrays", "length");
3851 recover();
3852 }
3853 else
3854 {
Olli Etuaho96e67382015-04-23 14:27:02 +03003855 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03003856 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003857 {
Olli Etuaho39282e12015-04-23 15:41:48 +03003858 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003859 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03003860 // (func()).length()
3861 // (int[3](0, 1, 2)).length()
Jamie Madillb98c3a82015-07-23 14:26:04 -04003862 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
3863 // expression.
3864 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
3865 // spec section 5.9 which allows "An array, vector or matrix expression with the
3866 // length method applied".
3867 error(loc, "length can only be called on array names, not on array expressions",
3868 "length");
Olli Etuaho39282e12015-04-23 15:41:48 +03003869 recover();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003870 }
3871 }
Olli Etuaho96e67382015-04-23 14:27:02 +03003872 unionArray->setIConst(arraySize);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003873 callNode =
3874 intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003875 }
3876 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003877 {
3878 //
3879 // Then this should be a constructor.
3880 // Don't go through the symbol table for constructors.
3881 // Their parameters will be verified algorithmically.
3882 //
3883 TType type(EbtVoid, EbpUndefined); // use this to get the type back
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003884 if (!constructorErrorCheck(loc, paramNode, *fnCall, op, &type))
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003885 {
3886 //
3887 // It's a constructor, of type 'type'.
3888 //
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003889 callNode = addConstructor(paramNode, &type, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003890 }
Olli Etuaho72ba85b2015-03-04 14:23:26 +02003891
3892 if (callNode == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003893 {
3894 recover();
3895 callNode = intermediate.setAggregateOperator(nullptr, op, loc);
3896 }
3897 callNode->setType(type);
3898 }
3899 else
3900 {
3901 //
3902 // Not a constructor. Find it in the symbol table.
3903 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303904 const TFunction *fnCandidate;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003905 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003906 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003907 if (fnCandidate)
3908 {
3909 //
3910 // A declared function.
3911 //
3912 if (builtIn && !fnCandidate->getExtension().empty() &&
3913 extensionErrorCheck(loc, fnCandidate->getExtension()))
3914 {
3915 recover();
3916 }
3917 op = fnCandidate->getBuiltInOp();
3918 if (builtIn && op != EOpNull)
3919 {
3920 //
3921 // A function call mapped to a built-in operation.
3922 //
3923 if (fnCandidate->getParamCount() == 1)
3924 {
3925 //
3926 // Treat it like a built-in unary operator.
3927 //
Olli Etuaho15c2ac32015-11-09 15:51:43 +02003928 TIntermAggregate *paramAgg = paramNode->getAsAggregate();
3929 paramNode = paramAgg->getSequence()->front();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003930 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc,
3931 &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003932 if (callNode == nullptr)
3933 {
3934 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003935 extraInfoStream
3936 << "built in unary operator function. Type: "
3937 << static_cast<TIntermTyped *>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003938 std::string extraInfo = extraInfoStream.str();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003939 error(paramNode->getLine(), " wrong operand type", "Internal Error",
3940 extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003941 *fatalError = true;
3942 return nullptr;
3943 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003944 }
3945 else
3946 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003947 TIntermAggregate *aggregate =
3948 intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003949 aggregate->setType(fnCandidate->getReturnType());
3950 aggregate->setPrecisionFromChildren();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02003951 if (aggregate->areChildrenConstQualified())
3952 {
3953 aggregate->getTypePointer()->setQualifier(EvqConst);
3954 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003955
3956 // Some built-in functions have out parameters too.
3957 functionCallLValueErrorCheck(fnCandidate, aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05303958
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003959 // See if we can constant fold a built-in. Note that this may be possible even
3960 // if it is not const-qualified.
Olli Etuahob43846e2015-06-02 18:18:57 +03003961 TIntermTyped *foldedNode = intermediate.foldAggregateBuiltIn(aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05303962 if (foldedNode)
3963 {
Arun Patole274f0702015-05-05 13:33:30 +05303964 callNode = foldedNode;
3965 }
Olli Etuahob43846e2015-06-02 18:18:57 +03003966 else
3967 {
3968 callNode = aggregate;
3969 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003970 }
3971 }
3972 else
3973 {
3974 // This is a real function call
Jamie Madillb98c3a82015-07-23 14:26:04 -04003975 TIntermAggregate *aggregate =
3976 intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003977 aggregate->setType(fnCandidate->getReturnType());
3978
Jamie Madillb98c3a82015-07-23 14:26:04 -04003979 // this is how we know whether the given function is a builtIn function or a user
3980 // defined function
3981 // if builtIn == false, it's a userDefined -> could be an overloaded
3982 // builtIn function also
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003983 // if builtIn == true, it's definitely a builtIn function with EOpNull
3984 if (!builtIn)
3985 aggregate->setUserDefined();
3986 aggregate->setName(fnCandidate->getMangledName());
Corentin Wallez71d147f2015-02-11 11:15:24 -08003987 aggregate->setFunctionId(fnCandidate->getUniqueId());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003988
3989 // This needs to happen after the name is set
3990 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003991 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003992 aggregate->setBuiltInFunctionPrecision();
3993
Olli Etuahoe1a94c62015-11-16 17:35:25 +02003994 checkTextureOffsetConst(aggregate);
3995 }
3996
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003997 callNode = aggregate;
3998
3999 functionCallLValueErrorCheck(fnCandidate, aggregate);
4000 }
4001 }
4002 else
4003 {
4004 // error message was put out by findFunction()
4005 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004006 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004007 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004008 callNode = intermediate.addConstantUnion(unionArray,
4009 TType(EbtFloat, EbpUndefined, EvqConst), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004010 recover();
4011 }
4012 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004013 return callNode;
4014}
4015
Jamie Madillb98c3a82015-07-23 14:26:04 -04004016TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
4017 TIntermTyped *trueBlock,
4018 TIntermTyped *falseBlock,
Olli Etuaho52901742015-04-15 13:42:45 +03004019 const TSourceLoc &loc)
4020{
4021 if (boolErrorCheck(loc, cond))
4022 recover();
4023
4024 if (trueBlock->getType() != falseBlock->getType())
4025 {
4026 binaryOpError(loc, ":", trueBlock->getCompleteString(), falseBlock->getCompleteString());
4027 recover();
4028 return falseBlock;
4029 }
Olli Etuahoa2d53032015-04-15 14:14:44 +03004030 // ESSL1 sections 5.2 and 5.7:
4031 // ESSL3 section 5.7:
4032 // Ternary operator is not among the operators allowed for structures/arrays.
4033 if (trueBlock->isArray() || trueBlock->getBasicType() == EbtStruct)
4034 {
4035 error(loc, "ternary operator is not allowed for structures or arrays", ":");
4036 recover();
4037 return falseBlock;
4038 }
Olli Etuaho52901742015-04-15 13:42:45 +03004039 return intermediate.addSelection(cond, trueBlock, falseBlock, loc);
4040}
Olli Etuaho49300862015-02-20 14:54:49 +02004041
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004042//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004043// Parse an array of strings using yyparse.
4044//
4045// Returns 0 for success.
4046//
Jamie Madillb98c3a82015-07-23 14:26:04 -04004047int PaParseStrings(size_t count,
4048 const char *const string[],
4049 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05304050 TParseContext *context)
4051{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004052 if ((count == 0) || (string == NULL))
4053 return 1;
4054
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004055 if (glslang_initialize(context))
4056 return 1;
4057
alokp@chromium.org408c45e2012-04-05 15:54:43 +00004058 int error = glslang_scan(count, string, length, context);
4059 if (!error)
4060 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004061
alokp@chromium.org73bc2982012-06-19 18:48:05 +00004062 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00004063
alokp@chromium.org6b495712012-06-29 00:06:58 +00004064 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004065}