blob: d537bcb741d6274c5543b95a18c03644405abd38 [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,
496 TIntermNode *node,
497 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
daniel@transgaming.com0b53fc02011-03-09 15:12:12 +0000607 TIntermTyped *typed = node ? node->getAsTyped() : 0;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530608 if (typed == 0)
609 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000610 error(line, "constructor argument does not have a type", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000611 return true;
612 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530613 if (op != EOpConstructStruct && IsSampler(typed->getBasicType()))
614 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000615 error(line, "cannot convert a sampler", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000616 return true;
617 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530618 if (typed->getBasicType() == EbtVoid)
619 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000620 error(line, "cannot convert a void", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000621 return true;
622 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000623
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000624 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000625}
626
Jamie Madillb98c3a82015-07-23 14:26:04 -0400627// This function checks to see if a void variable has been declared and raise an error message for
628// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000629//
630// returns true in case of an error
631//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400632bool TParseContext::voidErrorCheck(const TSourceLoc &line,
633 const TString &identifier,
634 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000635{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300636 if (type == EbtVoid)
637 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000638 error(line, "illegal use of type 'void'", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000639 return true;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300640 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000641
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000642 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000643}
644
Jamie Madillb98c3a82015-07-23 14:26:04 -0400645// This function checks to see if the node (for the expression) contains a scalar boolean expression
646// or not
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000647//
648// returns true in case of an error
649//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530650bool TParseContext::boolErrorCheck(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000651{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530652 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector())
653 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000654 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000655 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530656 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000657
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000658 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000659}
660
Jamie Madillb98c3a82015-07-23 14:26:04 -0400661// This function checks to see if the node (for the expression) contains a scalar boolean expression
662// or not
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000663//
664// returns true in case of an error
665//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530666bool TParseContext::boolErrorCheck(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000667{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530668 if (pType.type != EbtBool || pType.isAggregate())
669 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000670 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000671 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530672 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000673
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000674 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000675}
676
Jamie Madillb98c3a82015-07-23 14:26:04 -0400677bool TParseContext::samplerErrorCheck(const TSourceLoc &line,
678 const TPublicType &pType,
679 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000680{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530681 if (pType.type == EbtStruct)
682 {
683 if (containsSampler(*pType.userDef))
684 {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000685 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530686
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000687 return true;
688 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530689
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000690 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530691 }
692 else if (IsSampler(pType.type))
693 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000694 error(line, reason, getBasicString(pType.type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000695
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000696 return true;
697 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000698
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000699 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000700}
701
Arun Patole7e7e68d2015-05-22 12:02:25 +0530702bool TParseContext::locationDeclaratorListCheck(const TSourceLoc &line, const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400703{
704 if (pType.layoutQualifier.location != -1)
705 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400706 error(line, "location must only be specified for a single input or output variable",
707 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400708 return true;
709 }
710
711 return false;
712}
713
Jamie Madillb98c3a82015-07-23 14:26:04 -0400714bool TParseContext::parameterSamplerErrorCheck(const TSourceLoc &line,
715 TQualifier qualifier,
716 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000717{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400718 if ((qualifier == EvqOut || qualifier == EvqInOut) && type.getBasicType() != EbtStruct &&
719 IsSampler(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530720 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000721 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000722 return true;
723 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000724
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000725 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000726}
727
Arun Patole7e7e68d2015-05-22 12:02:25 +0530728bool TParseContext::containsSampler(const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000729{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000730 if (IsSampler(type.getBasicType()))
731 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000732
Arun Patole7e7e68d2015-05-22 12:02:25 +0530733 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock())
734 {
735 const TFieldList &fields = type.getStruct()->fields();
736 for (unsigned int i = 0; i < fields.size(); ++i)
737 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400738 if (containsSampler(*fields[i]->type()))
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000739 return true;
740 }
741 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000742
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000743 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000744}
745
746//
747// Do size checking for an array type's size.
748//
749// Returns true if there was an error.
750//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530751bool TParseContext::arraySizeErrorCheck(const TSourceLoc &line, TIntermTyped *expr, int &size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000752{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530753 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000754
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200755 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
756 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
757 // fold as array size.
758 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000759 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000760 error(line, "array size must be a constant integer expression", "");
Olli Etuahoe7847b02015-03-16 11:56:12 +0200761 size = 1;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000762 return true;
763 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000764
Nicolas Capens906744a2014-06-06 15:18:07 -0400765 unsigned int unsignedSize = 0;
766
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000767 if (constant->getBasicType() == EbtUInt)
768 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400769 unsignedSize = constant->getUConst(0);
Jamie Madillb98c3a82015-07-23 14:26:04 -0400770 size = static_cast<int>(unsignedSize);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000771 }
772 else
773 {
774 size = constant->getIConst(0);
775
Nicolas Capens906744a2014-06-06 15:18:07 -0400776 if (size < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000777 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400778 error(line, "array size must be non-negative", "");
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000779 size = 1;
780 return true;
781 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400782
783 unsignedSize = static_cast<unsigned int>(size);
784 }
785
786 if (size == 0)
787 {
788 error(line, "array size must be greater than zero", "");
789 size = 1;
790 return true;
791 }
792
793 // The size of arrays is restricted here to prevent issues further down the
794 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
795 // 4096 registers so this should be reasonable even for aggressively optimizable code.
796 const unsigned int sizeLimit = 65536;
797
798 if (unsignedSize > sizeLimit)
799 {
800 error(line, "array size too large", "");
801 size = 1;
802 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000803 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000804
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000805 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000806}
807
808//
809// See if this qualifier can be an array.
810//
811// Returns true if there is an error.
812//
Olli Etuaho3739d232015-04-08 12:23:44 +0300813bool TParseContext::arrayQualifierErrorCheck(const TSourceLoc &line, const TPublicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000814{
Olli Etuaho3739d232015-04-08 12:23:44 +0300815 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqVertexIn) ||
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400816 (type.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300817 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400818 error(line, "cannot declare arrays of this qualifier",
819 TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000820 return true;
821 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000822
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000823 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000824}
825
826//
827// See if this type can be an array.
828//
829// Returns true if there is an error.
830//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530831bool TParseContext::arrayTypeErrorCheck(const TSourceLoc &line, const TPublicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000832{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000833 //
834 // Can the type be an array?
835 //
Jamie Madill06145232015-05-13 13:10:01 -0400836 if (type.array)
837 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000838 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000839 return true;
840 }
Olli Etuahocc36b982015-07-10 14:14:18 +0300841 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
842 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
843 // 4.3.4).
844 if (mShaderVersion >= 300 && type.type == EbtStruct && sh::IsVarying(type.qualifier))
845 {
846 error(line, "cannot declare arrays of structs of this qualifier",
847 TType(type).getCompleteString().c_str());
848 return true;
849 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000850
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000851 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000852}
853
854//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000855// Enforce non-initializer type/qualifier rules.
856//
857// Returns true if there was an error.
858//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400859bool TParseContext::nonInitErrorCheck(const TSourceLoc &line,
860 const TString &identifier,
861 TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000862{
Olli Etuaho3739d232015-04-08 12:23:44 +0300863 ASSERT(type != nullptr);
864 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000865 {
866 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300867 type->qualifier = EvqTemporary;
868
869 // Generate informative error messages for ESSL1.
870 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400871 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000872 {
Arun Patole7e7e68d2015-05-22 12:02:25 +0530873 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400874 "structures containing arrays may not be declared constant since they cannot be "
875 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530876 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000877 }
878 else
879 {
880 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
881 }
882
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000883 return true;
884 }
Olli Etuaho376f1b52015-04-13 13:23:41 +0300885 if (type->isUnsizedArray())
886 {
887 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
888 return true;
889 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000890 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000891}
892
Olli Etuaho2935c582015-04-08 14:32:06 +0300893// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000894// and update the symbol table.
895//
Olli Etuaho2935c582015-04-08 14:32:06 +0300896// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000897//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400898bool TParseContext::declareVariable(const TSourceLoc &line,
899 const TString &identifier,
900 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +0300901 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000902{
Olli Etuaho2935c582015-04-08 14:32:06 +0300903 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000904
Olli Etuaho2935c582015-04-08 14:32:06 +0300905 bool needsReservedErrorCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000906
Olli Etuaho2935c582015-04-08 14:32:06 +0300907 // gl_LastFragData may be redeclared with a new precision qualifier
908 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
909 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400910 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
911 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho2935c582015-04-08 14:32:06 +0300912 if (type.getArraySize() == maxDrawBuffers->getConstPointer()->getIConst())
913 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400914 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +0300915 {
916 needsReservedErrorCheck = extensionErrorCheck(line, builtInSymbol->getExtension());
917 }
918 }
919 else
920 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400921 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
922 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +0300923 return false;
924 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000925 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000926
Olli Etuaho2935c582015-04-08 14:32:06 +0300927 if (needsReservedErrorCheck && reservedErrorCheck(line, identifier))
928 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000929
Olli Etuaho2935c582015-04-08 14:32:06 +0300930 (*variable) = new TVariable(&identifier, type);
931 if (!symbolTable.declare(*variable))
932 {
933 error(line, "redefinition", identifier.c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -0400934 *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +0300935 return false;
936 }
937
938 if (voidErrorCheck(line, identifier, type.getBasicType()))
939 return false;
940
941 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000942}
943
Jamie Madillb98c3a82015-07-23 14:26:04 -0400944bool TParseContext::paramErrorCheck(const TSourceLoc &line,
945 TQualifier qualifier,
946 TQualifier paramQualifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530947 TType *type)
948{
949 if (qualifier != EvqConst && qualifier != EvqTemporary)
950 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000951 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000952 return true;
953 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530954 if (qualifier == EvqConst && paramQualifier != EvqIn)
955 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400956 error(line, "qualifier not allowed with ", getQualifierString(qualifier),
957 getQualifierString(paramQualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000958 return true;
959 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000960
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000961 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000962 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000963 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000964 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000965
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000966 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000967}
968
Arun Patole7e7e68d2015-05-22 12:02:25 +0530969bool TParseContext::extensionErrorCheck(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000970{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400971 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000972 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +0530973 if (iter == extBehavior.end())
974 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000975 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000976 return true;
977 }
zmo@google.comf5450912011-09-09 01:37:19 +0000978 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +0530979 if (iter->second == EBhDisable || iter->second == EBhUndefined)
980 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000981 error(line, "extension", extension.c_str(), "is disabled");
982 return true;
983 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530984 if (iter->second == EBhWarn)
985 {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000986 warning(line, "extension", extension.c_str(), "is being used");
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000987 return false;
988 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000989
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000990 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000991}
992
Jamie Madillb98c3a82015-07-23 14:26:04 -0400993// These checks are common for all declarations starting a declarator list, and declarators that
994// follow an empty declaration.
Olli Etuahofa33d582015-04-09 14:33:12 +0300995//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400996bool TParseContext::singleDeclarationErrorCheck(const TPublicType &publicType,
997 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -0400998{
Olli Etuahofa33d582015-04-09 14:33:12 +0300999 switch (publicType.qualifier)
1000 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001001 case EvqVaryingIn:
1002 case EvqVaryingOut:
1003 case EvqAttribute:
1004 case EvqVertexIn:
1005 case EvqFragmentOut:
1006 if (publicType.type == EbtStruct)
1007 {
1008 error(identifierLocation, "cannot be used with a structure",
1009 getQualifierString(publicType.qualifier));
1010 return true;
1011 }
Olli Etuahofa33d582015-04-09 14:33:12 +03001012
Jamie Madillb98c3a82015-07-23 14:26:04 -04001013 default:
1014 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001015 }
1016
Jamie Madillb98c3a82015-07-23 14:26:04 -04001017 if (publicType.qualifier != EvqUniform &&
1018 samplerErrorCheck(identifierLocation, publicType, "samplers must be uniform"))
Olli Etuahofa33d582015-04-09 14:33:12 +03001019 {
Jamie Madilla5efff92013-06-06 11:56:47 -04001020 return true;
Olli Etuahofa33d582015-04-09 14:33:12 +03001021 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001022
1023 // check for layout qualifier issues
1024 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
1025
1026 if (layoutQualifier.matrixPacking != EmpUnspecified)
1027 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001028 error(identifierLocation, "layout qualifier",
1029 getMatrixPackingString(layoutQualifier.matrixPacking),
Olli Etuahofa33d582015-04-09 14:33:12 +03001030 "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -04001031 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001032 }
1033
1034 if (layoutQualifier.blockStorage != EbsUnspecified)
1035 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001036 error(identifierLocation, "layout qualifier",
1037 getBlockStorageString(layoutQualifier.blockStorage),
Olli Etuahofa33d582015-04-09 14:33:12 +03001038 "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -04001039 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001040 }
1041
Olli Etuahofa33d582015-04-09 14:33:12 +03001042 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut &&
1043 layoutLocationErrorCheck(identifierLocation, publicType.layoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001044 {
Jamie Madill51a53c72013-06-19 09:24:43 -04001045 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001046 }
1047
1048 return false;
1049}
1050
Jamie Madillb98c3a82015-07-23 14:26:04 -04001051bool TParseContext::layoutLocationErrorCheck(const TSourceLoc &location,
1052 const TLayoutQualifier &layoutQualifier)
Jamie Madilla5efff92013-06-06 11:56:47 -04001053{
1054 if (layoutQualifier.location != -1)
1055 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001056 error(location, "invalid layout qualifier:", "location",
1057 "only valid on program inputs and outputs");
Jamie Madilla5efff92013-06-06 11:56:47 -04001058 return true;
1059 }
1060
1061 return false;
1062}
1063
Jamie Madillb98c3a82015-07-23 14:26:04 -04001064bool TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate,
1065 TIntermAggregate *aggregate)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001066{
1067 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1068 {
1069 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1070 if (qual == EvqOut || qual == EvqInOut)
1071 {
1072 TIntermTyped *node = (*(aggregate->getSequence()))[i]->getAsTyped();
1073 if (lValueErrorCheck(node->getLine(), "assign", node))
1074 {
1075 error(node->getLine(),
Jamie Madillb98c3a82015-07-23 14:26:04 -04001076 "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error");
Olli Etuahob6e07a62015-02-16 12:22:10 +02001077 recover();
1078 return true;
1079 }
1080 }
1081 }
1082 return false;
1083}
1084
Jamie Madillb98c3a82015-07-23 14:26:04 -04001085void TParseContext::es3InvariantErrorCheck(const TQualifier qualifier,
1086 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001087{
1088 if (!sh::IsVaryingOut(qualifier) && qualifier != EvqFragmentOut)
1089 {
1090 error(invariantLocation, "Only out variables can be invariant.", "invariant");
1091 recover();
1092 }
1093}
1094
Arun Patole7e7e68d2015-05-22 12:02:25 +05301095bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001096{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001097 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001098 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1099 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001100}
1101
Arun Patole7e7e68d2015-05-22 12:02:25 +05301102bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001103{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001104 return ::IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001105}
1106
Jamie Madillb98c3a82015-07-23 14:26:04 -04001107void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1108 const char *extName,
1109 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001110{
1111 pp::SourceLocation srcLoc;
1112 srcLoc.file = loc.first_file;
1113 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001114 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001115}
1116
Jamie Madillb98c3a82015-07-23 14:26:04 -04001117void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1118 const char *name,
1119 const char *value,
1120 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001121{
1122 pp::SourceLocation srcLoc;
1123 srcLoc.file = loc.first_file;
1124 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001125 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001126}
1127
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001128/////////////////////////////////////////////////////////////////////////////////
1129//
1130// Non-Errors.
1131//
1132/////////////////////////////////////////////////////////////////////////////////
1133
Jamie Madill5c097022014-08-20 16:38:32 -04001134const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1135 const TString *name,
1136 const TSymbol *symbol)
1137{
1138 const TVariable *variable = NULL;
1139
1140 if (!symbol)
1141 {
1142 error(location, "undeclared identifier", name->c_str());
1143 recover();
1144 }
1145 else if (!symbol->isVariable())
1146 {
1147 error(location, "variable expected", name->c_str());
1148 recover();
1149 }
1150 else
1151 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001152 variable = static_cast<const TVariable *>(symbol);
Jamie Madill5c097022014-08-20 16:38:32 -04001153
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001154 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
Jamie Madill5c097022014-08-20 16:38:32 -04001155 !variable->getExtension().empty() &&
1156 extensionErrorCheck(location, variable->getExtension()))
1157 {
1158 recover();
1159 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001160
1161 // Reject shaders using both gl_FragData and gl_FragColor
1162 TQualifier qualifier = variable->getType().getQualifier();
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001163 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001164 {
1165 mUsesFragData = true;
1166 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001167 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001168 {
1169 mUsesFragColor = true;
1170 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001171 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1172 {
1173 mUsesSecondaryOutputs = true;
1174 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001175
1176 // This validation is not quite correct - it's only an error to write to
1177 // both FragData and FragColor. For simplicity, and because users shouldn't
1178 // be rewarded for reading from undefined varaibles, return an error
1179 // if they are both referenced, rather than assigned.
1180 if (mUsesFragData && mUsesFragColor)
1181 {
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001182 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1183 if (mUsesSecondaryOutputs)
1184 {
1185 errorMessage =
1186 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1187 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1188 }
1189 error(location, errorMessage, name->c_str());
Jamie Madill14e95b32015-05-07 10:10:41 -04001190 recover();
1191 }
Jamie Madill5c097022014-08-20 16:38:32 -04001192 }
1193
1194 if (!variable)
1195 {
1196 TType type(EbtFloat, EbpUndefined);
1197 TVariable *fakeVariable = new TVariable(name, type);
1198 symbolTable.declare(fakeVariable);
1199 variable = fakeVariable;
1200 }
1201
1202 return variable;
1203}
1204
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001205TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1206 const TString *name,
1207 const TSymbol *symbol)
1208{
1209 const TVariable *variable = getNamedVariable(location, name, symbol);
1210
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001211 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001212 {
1213 TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001214 return intermediate.addConstantUnion(constArray, variable->getType(), location);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001215 }
1216 else
1217 {
1218 return intermediate.addSymbol(variable->getUniqueId(), variable->getName(),
1219 variable->getType(), location);
1220 }
1221}
1222
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001223//
1224// Look up a function name in the symbol table, and make sure it is a function.
1225//
1226// Return the function symbol if found, otherwise 0.
1227//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001228const TFunction *TParseContext::findFunction(const TSourceLoc &line,
1229 TFunction *call,
1230 int inputShaderVersion,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301231 bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001232{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001233 // First find by unmangled name to check whether the function name has been
1234 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001235 // If a function is found, check for one with a matching argument list.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301236 const TSymbol *symbol = symbolTable.find(call->getName(), inputShaderVersion, builtIn);
1237 if (symbol == 0 || symbol->isFunction())
1238 {
Austin Kinross3ae64652015-01-26 15:51:39 -08001239 symbol = symbolTable.find(call->getMangledName(), inputShaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001240 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001241
Arun Patole7e7e68d2015-05-22 12:02:25 +05301242 if (symbol == 0)
1243 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001244 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001245 return 0;
1246 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001247
Arun Patole7e7e68d2015-05-22 12:02:25 +05301248 if (!symbol->isFunction())
1249 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001250 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001251 return 0;
1252 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001253
Jamie Madillb98c3a82015-07-23 14:26:04 -04001254 return static_cast<const TFunction *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001255}
1256
1257//
1258// Initializers show up in several places in the grammar. Have one set of
1259// code to handle them here.
1260//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001261// Returns true on error, false if no error
1262//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001263bool TParseContext::executeInitializer(const TSourceLoc &line,
1264 const TString &identifier,
1265 const TPublicType &pType,
1266 TIntermTyped *initializer,
1267 TIntermNode **intermNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001268{
Olli Etuahoe7847b02015-03-16 11:56:12 +02001269 ASSERT(intermNode != nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001270 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001271
Olli Etuaho2935c582015-04-08 14:32:06 +03001272 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001273 if (type.isUnsizedArray())
1274 {
1275 type.setArraySize(initializer->getArraySize());
1276 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001277 if (!declareVariable(line, identifier, type, &variable))
1278 {
1279 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001280 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001281
Olli Etuahob0c645e2015-05-12 14:25:36 +03001282 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001283 if (symbolTable.atGlobalLevel() &&
1284 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001285 {
1286 // Error message does not completely match behavior with ESSL 1.00, but
1287 // we want to steer developers towards only using constant expressions.
1288 error(line, "global variable initializers must be constant expressions", "=");
1289 return true;
1290 }
1291 if (globalInitWarning)
1292 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001293 warning(
1294 line,
1295 "global variable initializers should be constant expressions "
1296 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1297 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001298 }
1299
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001300 //
1301 // identifier must be of type constant, a global, or a temporary
1302 //
1303 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301304 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1305 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001306 error(line, " cannot initialize this type of qualifier ",
1307 variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001308 return true;
1309 }
1310 //
1311 // test for and propagate constant
1312 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001313
Arun Patole7e7e68d2015-05-22 12:02:25 +05301314 if (qualifier == EvqConst)
1315 {
1316 if (qualifier != initializer->getType().getQualifier())
1317 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001318 std::stringstream extraInfoStream;
1319 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1320 std::string extraInfo = extraInfoStream.str();
1321 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001322 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001323 return true;
1324 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301325 if (type != initializer->getType())
1326 {
1327 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001328 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001329 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001330 return true;
1331 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001332
1333 // Save the constant folded value to the variable if possible. For example array
1334 // initializers are not folded, since that way copying the array literal to multiple places
1335 // in the shader is avoided.
1336 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1337 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301338 if (initializer->getAsConstantUnion())
1339 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001340 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001341 *intermNode = nullptr;
1342 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301343 }
1344 else if (initializer->getAsSymbolNode())
1345 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001346 const TSymbol *symbol =
1347 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1348 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001349
Arun Patole7e7e68d2015-05-22 12:02:25 +05301350 TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001351 if (constArray)
1352 {
1353 variable->shareConstPointer(constArray);
1354 *intermNode = nullptr;
1355 return false;
1356 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001357 }
1358 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001359
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001360 TIntermSymbol *intermSymbol = intermediate.addSymbol(
1361 variable->getUniqueId(), variable->getName(), variable->getType(), line);
1362 *intermNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1363 if (*intermNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001364 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001365 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1366 return true;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001367 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001368
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001369 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001370}
1371
Jamie Madillb98c3a82015-07-23 14:26:04 -04001372TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier,
1373 bool invariant,
1374 TLayoutQualifier layoutQualifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301375 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001376{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001377 TPublicType returnType = typeSpecifier;
1378 returnType.qualifier = qualifier;
1379 returnType.invariant = invariant;
Jamie Madilla5efff92013-06-06 11:56:47 -04001380 returnType.layoutQualifier = layoutQualifier;
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001381
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001382 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001383 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001384 if (typeSpecifier.array)
1385 {
1386 error(typeSpecifier.line, "not supported", "first-class array");
1387 recover();
1388 returnType.clearArrayness();
1389 }
1390
Jamie Madillb98c3a82015-07-23 14:26:04 -04001391 if (qualifier == EvqAttribute &&
1392 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001393 {
1394 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1395 recover();
1396 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001397
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001398 if ((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1399 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1400 {
1401 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1402 recover();
1403 }
1404 }
1405 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001406 {
Olli Etuahoabb0c382015-07-13 12:01:12 +03001407 if (!layoutQualifier.isEmpty())
1408 {
1409 if (globalErrorCheck(typeSpecifier.line, symbolTable.atGlobalLevel(), "layout"))
1410 {
1411 recover();
1412 }
1413 }
Olli Etuahocc36b982015-07-10 14:14:18 +03001414 if (sh::IsVarying(qualifier) || qualifier == EvqVertexIn || qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001415 {
Olli Etuahocc36b982015-07-10 14:14:18 +03001416 es3InputOutputTypeCheck(qualifier, typeSpecifier, typeSpecifier.line);
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001417 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001418 }
1419
1420 return returnType;
1421}
1422
Olli Etuahocc36b982015-07-10 14:14:18 +03001423void TParseContext::es3InputOutputTypeCheck(const TQualifier qualifier,
1424 const TPublicType &type,
1425 const TSourceLoc &qualifierLocation)
1426{
1427 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
1428 if (type.type == EbtBool)
1429 {
1430 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
1431 recover();
1432 }
1433
1434 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
1435 switch (qualifier)
1436 {
1437 case EvqVertexIn:
1438 // ESSL 3.00 section 4.3.4
1439 if (type.array)
1440 {
1441 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
1442 recover();
1443 }
1444 // Vertex inputs with a struct type are disallowed in singleDeclarationErrorCheck
1445 return;
1446 case EvqFragmentOut:
1447 // ESSL 3.00 section 4.3.6
1448 if (type.isMatrix())
1449 {
1450 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
1451 recover();
1452 }
1453 // Fragment outputs with a struct type are disallowed in singleDeclarationErrorCheck
1454 return;
1455 default:
1456 break;
1457 }
1458
1459 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
1460 // restrictions.
1461 bool typeContainsIntegers =
1462 (type.type == EbtInt || type.type == EbtUInt || type.isStructureContainingType(EbtInt) ||
1463 type.isStructureContainingType(EbtUInt));
1464 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
1465 {
1466 error(qualifierLocation, "must use 'flat' interpolation here",
1467 getQualifierString(qualifier));
1468 recover();
1469 }
1470
1471 if (type.type == EbtStruct)
1472 {
1473 // ESSL 3.00 sections 4.3.4 and 4.3.6.
1474 // These restrictions are only implied by the ESSL 3.00 spec, but
1475 // the ESSL 3.10 spec lists these restrictions explicitly.
1476 if (type.array)
1477 {
1478 error(qualifierLocation, "cannot be an array of structures",
1479 getQualifierString(qualifier));
1480 recover();
1481 }
1482 if (type.isStructureContainingArrays())
1483 {
1484 error(qualifierLocation, "cannot be a structure containing an array",
1485 getQualifierString(qualifier));
1486 recover();
1487 }
1488 if (type.isStructureContainingType(EbtStruct))
1489 {
1490 error(qualifierLocation, "cannot be a structure containing a structure",
1491 getQualifierString(qualifier));
1492 recover();
1493 }
1494 if (type.isStructureContainingType(EbtBool))
1495 {
1496 error(qualifierLocation, "cannot be a structure containing a bool",
1497 getQualifierString(qualifier));
1498 recover();
1499 }
1500 }
1501}
1502
Olli Etuahofa33d582015-04-09 14:33:12 +03001503TIntermAggregate *TParseContext::parseSingleDeclaration(TPublicType &publicType,
1504 const TSourceLoc &identifierOrTypeLocation,
1505 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001506{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001507 TIntermSymbol *symbol =
1508 intermediate.addSymbol(0, identifier, TType(publicType), identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001509
Olli Etuahobab4c082015-04-24 16:38:49 +03001510 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03001511
Olli Etuahobab4c082015-04-24 16:38:49 +03001512 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1513
1514 if (emptyDeclaration)
1515 {
1516 if (publicType.isUnsizedArray())
1517 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001518 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1519 // error. It is assumed that this applies to empty declarations as well.
1520 error(identifierOrTypeLocation, "empty array declaration needs to specify a size",
1521 identifier.c_str());
Olli Etuahobab4c082015-04-24 16:38:49 +03001522 }
1523 }
1524 else
Jamie Madill60ed9812013-06-06 11:56:46 -04001525 {
Olli Etuahofa33d582015-04-09 14:33:12 +03001526 if (singleDeclarationErrorCheck(publicType, identifierOrTypeLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001527 recover();
1528
Olli Etuaho376f1b52015-04-13 13:23:41 +03001529 if (nonInitErrorCheck(identifierOrTypeLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001530 recover();
1531
Olli Etuaho2935c582015-04-08 14:32:06 +03001532 TVariable *variable = nullptr;
Olli Etuahofa33d582015-04-09 14:33:12 +03001533 if (!declareVariable(identifierOrTypeLocation, identifier, TType(publicType), &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001534 recover();
1535
1536 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001537 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001538 }
1539
Olli Etuahoe7847b02015-03-16 11:56:12 +02001540 return intermediate.makeAggregate(symbol, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001541}
1542
Olli Etuahoe7847b02015-03-16 11:56:12 +02001543TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1544 const TSourceLoc &identifierLocation,
1545 const TString &identifier,
1546 const TSourceLoc &indexLocation,
1547 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001548{
Olli Etuahofa33d582015-04-09 14:33:12 +03001549 mDeferredSingleDeclarationErrorCheck = false;
1550
1551 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001552 recover();
1553
Olli Etuaho376f1b52015-04-13 13:23:41 +03001554 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001555 recover();
1556
Jamie Madillb98c3a82015-07-23 14:26:04 -04001557 if (arrayTypeErrorCheck(indexLocation, publicType) ||
1558 arrayQualifierErrorCheck(indexLocation, publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001559 {
1560 recover();
1561 }
1562
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001563 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001564
1565 int size;
1566 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1567 {
1568 recover();
1569 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001570 // Make the type an array even if size check failed.
1571 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1572 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001573
Olli Etuaho2935c582015-04-08 14:32:06 +03001574 TVariable *variable = nullptr;
1575 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001576 recover();
1577
Olli Etuahoe7847b02015-03-16 11:56:12 +02001578 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001579 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001580 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001581
Olli Etuahoe7847b02015-03-16 11:56:12 +02001582 return intermediate.makeAggregate(symbol, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001583}
1584
Jamie Madill06145232015-05-13 13:10:01 -04001585TIntermAggregate *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001586 const TSourceLoc &identifierLocation,
1587 const TString &identifier,
1588 const TSourceLoc &initLocation,
1589 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001590{
Olli Etuahofa33d582015-04-09 14:33:12 +03001591 mDeferredSingleDeclarationErrorCheck = false;
1592
1593 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001594 recover();
1595
Olli Etuahoe7847b02015-03-16 11:56:12 +02001596 TIntermNode *intermNode = nullptr;
1597 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04001598 {
1599 //
1600 // Build intermediate representation
1601 //
Olli Etuahoe7847b02015-03-16 11:56:12 +02001602 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001603 }
1604 else
1605 {
1606 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001607 return nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001608 }
1609}
1610
Jamie Madillb98c3a82015-07-23 14:26:04 -04001611TIntermAggregate *TParseContext::parseSingleArrayInitDeclaration(
1612 TPublicType &publicType,
1613 const TSourceLoc &identifierLocation,
1614 const TString &identifier,
1615 const TSourceLoc &indexLocation,
1616 TIntermTyped *indexExpression,
1617 const TSourceLoc &initLocation,
1618 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001619{
1620 mDeferredSingleDeclarationErrorCheck = false;
1621
1622 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1623 recover();
1624
Jamie Madillb98c3a82015-07-23 14:26:04 -04001625 if (arrayTypeErrorCheck(indexLocation, publicType) ||
1626 arrayQualifierErrorCheck(indexLocation, publicType))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001627 {
1628 recover();
1629 }
1630
1631 TPublicType arrayType(publicType);
1632
Olli Etuaho376f1b52015-04-13 13:23:41 +03001633 int size = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001634 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
1635 // the initializer.
1636 if (indexExpression != nullptr &&
1637 arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001638 {
1639 recover();
1640 }
1641 // Make the type an array even if size check failed.
1642 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1643 arrayType.setArraySize(size);
1644
1645 // initNode will correspond to the whole of "type b[n] = initializer".
1646 TIntermNode *initNode = nullptr;
1647 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1648 {
1649 return initNode ? intermediate.makeAggregate(initNode, initLocation) : nullptr;
1650 }
1651 else
1652 {
1653 recover();
1654 return nullptr;
1655 }
1656}
1657
Olli Etuahoe7847b02015-03-16 11:56:12 +02001658TIntermAggregate *TParseContext::parseInvariantDeclaration(const TSourceLoc &invariantLoc,
Jamie Madill47e3ec02014-08-20 16:38:33 -04001659 const TSourceLoc &identifierLoc,
1660 const TString *identifier,
1661 const TSymbol *symbol)
1662{
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001663 // invariant declaration
Jamie Madill47e3ec02014-08-20 16:38:33 -04001664 if (globalErrorCheck(invariantLoc, symbolTable.atGlobalLevel(), "invariant varying"))
1665 {
1666 recover();
1667 }
1668
1669 if (!symbol)
1670 {
1671 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
1672 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001673 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001674 }
1675 else
1676 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001677 const TString kGlFrontFacing("gl_FrontFacing");
1678 if (*identifier == kGlFrontFacing)
1679 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001680 error(identifierLoc, "identifier should not be declared as invariant",
1681 identifier->c_str());
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001682 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001683 return nullptr;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001684 }
Jamie Madill2c433252014-12-03 12:36:54 -05001685 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001686 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1687 ASSERT(variable);
1688 const TType &type = variable->getType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04001689 TIntermSymbol *intermSymbol =
1690 intermediate.addSymbol(variable->getUniqueId(), *identifier, type, identifierLoc);
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001691
1692 TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc);
1693 aggregate->setOp(EOpInvariantDeclaration);
1694 return aggregate;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001695 }
1696}
1697
Jamie Madillb98c3a82015-07-23 14:26:04 -04001698TIntermAggregate *TParseContext::parseDeclarator(TPublicType &publicType,
1699 TIntermAggregate *aggregateDeclaration,
1700 const TSourceLoc &identifierLocation,
1701 const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001702{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001703 // If the declaration starting this declarator list was empty (example: int,), some checks were
1704 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001705 if (mDeferredSingleDeclarationErrorCheck)
1706 {
1707 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1708 recover();
1709 mDeferredSingleDeclarationErrorCheck = false;
1710 }
1711
Jamie Madill0bd18df2013-06-20 11:55:52 -04001712 if (locationDeclaratorListCheck(identifierLocation, publicType))
1713 recover();
1714
Olli Etuaho376f1b52015-04-13 13:23:41 +03001715 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001716 recover();
1717
Olli Etuaho2935c582015-04-08 14:32:06 +03001718 TVariable *variable = nullptr;
1719 if (!declareVariable(identifierLocation, identifier, TType(publicType), &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001720 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001721
Jamie Madillb98c3a82015-07-23 14:26:04 -04001722 TIntermSymbol *symbol =
1723 intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001724 if (variable && symbol)
Jamie Madill502d66f2013-06-20 11:55:52 -04001725 symbol->setId(variable->getUniqueId());
1726
Olli Etuahoe7847b02015-03-16 11:56:12 +02001727 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001728}
1729
Jamie Madillb98c3a82015-07-23 14:26:04 -04001730TIntermAggregate *TParseContext::parseArrayDeclarator(TPublicType &publicType,
1731 TIntermAggregate *aggregateDeclaration,
1732 const TSourceLoc &identifierLocation,
1733 const TString &identifier,
1734 const TSourceLoc &arrayLocation,
1735 TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001736{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001737 // If the declaration starting this declarator list was empty (example: int,), some checks were
1738 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001739 if (mDeferredSingleDeclarationErrorCheck)
1740 {
1741 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1742 recover();
1743 mDeferredSingleDeclarationErrorCheck = false;
1744 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001745
Jamie Madill0bd18df2013-06-20 11:55:52 -04001746 if (locationDeclaratorListCheck(identifierLocation, publicType))
1747 recover();
1748
Olli Etuaho376f1b52015-04-13 13:23:41 +03001749 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001750 recover();
1751
Jamie Madillb98c3a82015-07-23 14:26:04 -04001752 if (arrayTypeErrorCheck(arrayLocation, publicType) ||
1753 arrayQualifierErrorCheck(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001754 {
1755 recover();
1756 }
Olli Etuaho93a90fd2015-04-07 18:14:07 +03001757 else
Jamie Madill502d66f2013-06-20 11:55:52 -04001758 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001759 TType arrayType = TType(publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04001760 int size;
1761 if (arraySizeErrorCheck(arrayLocation, indexExpression, size))
Olli Etuahoe7847b02015-03-16 11:56:12 +02001762 {
Jamie Madill502d66f2013-06-20 11:55:52 -04001763 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001764 }
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001765 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001766
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001767 TVariable *variable = nullptr;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001768 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001769 recover();
Jamie Madill502d66f2013-06-20 11:55:52 -04001770
Jamie Madillb98c3a82015-07-23 14:26:04 -04001771 TIntermSymbol *symbol =
1772 intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001773 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001774 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001775
1776 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001777 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001778
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001779 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001780}
1781
Jamie Madillb98c3a82015-07-23 14:26:04 -04001782TIntermAggregate *TParseContext::parseInitDeclarator(const TPublicType &publicType,
1783 TIntermAggregate *aggregateDeclaration,
1784 const TSourceLoc &identifierLocation,
1785 const TString &identifier,
1786 const TSourceLoc &initLocation,
1787 TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001788{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001789 // If the declaration starting this declarator list was empty (example: int,), some checks were
1790 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001791 if (mDeferredSingleDeclarationErrorCheck)
1792 {
1793 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1794 recover();
1795 mDeferredSingleDeclarationErrorCheck = false;
1796 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001797
Jamie Madill0bd18df2013-06-20 11:55:52 -04001798 if (locationDeclaratorListCheck(identifierLocation, publicType))
1799 recover();
1800
Olli Etuahoe7847b02015-03-16 11:56:12 +02001801 TIntermNode *intermNode = nullptr;
1802 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04001803 {
1804 //
1805 // build the intermediate representation
1806 //
1807 if (intermNode)
1808 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001809 return intermediate.growAggregate(aggregateDeclaration, intermNode, initLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001810 }
1811 else
1812 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001813 return aggregateDeclaration;
Jamie Madill502d66f2013-06-20 11:55:52 -04001814 }
1815 }
1816 else
1817 {
1818 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001819 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001820 }
1821}
1822
Jamie Madill06145232015-05-13 13:10:01 -04001823TIntermAggregate *TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001824 TIntermAggregate *aggregateDeclaration,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301825 const TSourceLoc &identifierLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001826 const TString &identifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301827 const TSourceLoc &indexLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001828 TIntermTyped *indexExpression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001829 const TSourceLoc &initLocation,
1830 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001831{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001832 // If the declaration starting this declarator list was empty (example: int,), some checks were
1833 // not performed.
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001834 if (mDeferredSingleDeclarationErrorCheck)
1835 {
1836 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1837 recover();
1838 mDeferredSingleDeclarationErrorCheck = false;
1839 }
1840
1841 if (locationDeclaratorListCheck(identifierLocation, publicType))
1842 recover();
1843
Jamie Madillb98c3a82015-07-23 14:26:04 -04001844 if (arrayTypeErrorCheck(indexLocation, publicType) ||
1845 arrayQualifierErrorCheck(indexLocation, publicType))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001846 {
1847 recover();
1848 }
1849
1850 TPublicType arrayType(publicType);
1851
Olli Etuaho376f1b52015-04-13 13:23:41 +03001852 int size = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001853 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
1854 // the initializer.
1855 if (indexExpression != nullptr &&
1856 arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001857 {
1858 recover();
1859 }
1860 // Make the type an array even if size check failed.
1861 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1862 arrayType.setArraySize(size);
1863
1864 // initNode will correspond to the whole of "b[n] = initializer".
1865 TIntermNode *initNode = nullptr;
1866 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1867 {
1868 if (initNode)
1869 {
1870 return intermediate.growAggregate(aggregateDeclaration, initNode, initLocation);
1871 }
1872 else
1873 {
1874 return aggregateDeclaration;
1875 }
1876 }
1877 else
1878 {
1879 recover();
1880 return nullptr;
1881 }
1882}
1883
Jamie Madilla295edf2013-06-06 11:56:48 -04001884void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1885{
1886 if (typeQualifier.qualifier != EvqUniform)
1887 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001888 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
1889 "global layout must be uniform");
Jamie Madilla295edf2013-06-06 11:56:48 -04001890 recover();
1891 return;
1892 }
1893
1894 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1895 ASSERT(!layoutQualifier.isEmpty());
1896
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001897 if (mShaderVersion < 300)
Jamie Madilla295edf2013-06-06 11:56:48 -04001898 {
1899 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1900 recover();
1901 return;
1902 }
1903
1904 if (layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1905 {
1906 recover();
1907 return;
1908 }
1909
Jamie Madill099c0f32013-06-20 11:55:52 -04001910 if (layoutQualifier.matrixPacking != EmpUnspecified)
1911 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001912 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04001913 }
1914
Geoff Langc6856732014-02-11 09:38:55 -05001915 if (layoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04001916 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001917 mDefaultBlockStorage = layoutQualifier.blockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04001918 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001919}
1920
Jamie Madill185fb402015-06-12 15:48:48 -04001921void TParseContext::parseFunctionPrototype(const TSourceLoc &location,
1922 TFunction *function,
1923 TIntermAggregate **aggregateOut)
1924{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001925 const TSymbol *builtIn =
1926 symbolTable.findBuiltIn(function->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04001927
1928 if (builtIn)
1929 {
1930 error(location, "built-in functions cannot be redefined", function->getName().c_str());
1931 recover();
1932 }
1933
Jamie Madillb98c3a82015-07-23 14:26:04 -04001934 TFunction *prevDec =
1935 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Jamie Madill185fb402015-06-12 15:48:48 -04001936 //
1937 // Note: 'prevDec' could be 'function' if this is the first time we've seen function
1938 // as it would have just been put in the symbol table. Otherwise, we're looking up
1939 // an earlier occurance.
1940 //
1941 if (prevDec->isDefined())
1942 {
1943 // Then this function already has a body.
1944 error(location, "function already has a body", function->getName().c_str());
1945 recover();
1946 }
1947 prevDec->setDefined();
1948 //
1949 // Overload the unique ID of the definition to be the same unique ID as the declaration.
1950 // Eventually we will probably want to have only a single definition and just swap the
1951 // arguments to be the definition's arguments.
1952 //
1953 function->setUniqueId(prevDec->getUniqueId());
1954
1955 // Raise error message if main function takes any parameters or return anything other than void
1956 if (function->getName() == "main")
1957 {
1958 if (function->getParamCount() > 0)
1959 {
1960 error(location, "function cannot take any parameter(s)", function->getName().c_str());
1961 recover();
1962 }
1963 if (function->getReturnType().getBasicType() != EbtVoid)
1964 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001965 error(location, "", function->getReturnType().getBasicString(),
1966 "main function cannot return a value");
Jamie Madill185fb402015-06-12 15:48:48 -04001967 recover();
1968 }
1969 }
1970
1971 //
1972 // Remember the return type for later checking for RETURN statements.
1973 //
1974 setCurrentFunctionType(&(prevDec->getReturnType()));
1975 setFunctionReturnsValue(false);
1976
1977 //
1978 // Insert parameters into the symbol table.
1979 // If the parameter has no name, it's not an error, just don't insert it
1980 // (could be used for unused args).
1981 //
1982 // Also, accumulate the list of parameters into the HIL, so lower level code
1983 // knows where to find parameters.
1984 //
1985 TIntermAggregate *paramNodes = new TIntermAggregate;
1986 for (size_t i = 0; i < function->getParamCount(); i++)
1987 {
1988 const TConstParameter &param = function->getParam(i);
1989 if (param.name != 0)
1990 {
1991 TVariable *variable = new TVariable(param.name, *param.type);
1992 //
1993 // Insert the parameters with name in the symbol table.
1994 //
Jamie Madill1a4b1b32015-07-23 18:27:13 -04001995 if (!symbolTable.declare(variable))
1996 {
Jamie Madill185fb402015-06-12 15:48:48 -04001997 error(location, "redefinition", variable->getName().c_str());
1998 recover();
Jamie Madill1a4b1b32015-07-23 18:27:13 -04001999 paramNodes = intermediate.growAggregate(
2000 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
2001 continue;
Jamie Madill185fb402015-06-12 15:48:48 -04002002 }
2003
2004 //
2005 // Add the parameter to the HIL
2006 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002007 TIntermSymbol *symbol = intermediate.addSymbol(
2008 variable->getUniqueId(), variable->getName(), variable->getType(), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002009
2010 paramNodes = intermediate.growAggregate(paramNodes, symbol, location);
2011 }
2012 else
2013 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002014 paramNodes = intermediate.growAggregate(
2015 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002016 }
2017 }
2018 intermediate.setAggregateOperator(paramNodes, EOpParameters, location);
2019 *aggregateOut = paramNodes;
2020 setLoopNestingLevel(0);
2021}
2022
Jamie Madillb98c3a82015-07-23 14:26:04 -04002023TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002024{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002025 //
2026 // Multiple declarations of the same function are allowed.
2027 //
2028 // If this is a definition, the definition production code will check for redefinitions
2029 // (we don't know at this point if it's a definition or not).
2030 //
2031 // Redeclarations are allowed. But, return types and parameter qualifiers must match.
2032 //
2033 TFunction *prevDec =
2034 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
2035 if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002036 {
2037 if (prevDec->getReturnType() != function->getReturnType())
2038 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002039 error(location, "overloaded functions must have the same return type",
Jamie Madill185fb402015-06-12 15:48:48 -04002040 function->getReturnType().getBasicString());
2041 recover();
2042 }
2043 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2044 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002045 if (prevDec->getParam(i).type->getQualifier() !=
2046 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002047 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002048 error(location, "overloaded functions must have the same parameter qualifiers",
Jamie Madill185fb402015-06-12 15:48:48 -04002049 function->getParam(i).type->getQualifierString());
2050 recover();
2051 }
2052 }
2053 }
2054
2055 //
2056 // Check for previously declared variables using the same name.
2057 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002058 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002059 if (prevSym)
2060 {
2061 if (!prevSym->isFunction())
2062 {
2063 error(location, "redefinition", function->getName().c_str(), "function");
2064 recover();
2065 }
2066 }
2067 else
2068 {
2069 // Insert the unmangled name to detect potential future redefinition as a variable.
Jamie Madillb98c3a82015-07-23 14:26:04 -04002070 TFunction *newFunction =
2071 new TFunction(NewPoolTString(function->getName().c_str()), &function->getReturnType());
Jamie Madill185fb402015-06-12 15:48:48 -04002072 symbolTable.getOuterLevel()->insertUnmangled(newFunction);
2073 }
2074
2075 // We're at the inner scope level of the function's arguments and body statement.
2076 // Add the function prototype to the surrounding scope instead.
2077 symbolTable.getOuterLevel()->insert(function);
2078
2079 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002080 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2081 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002082 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2083 //
2084 return function;
2085}
2086
Jamie Madill06145232015-05-13 13:10:01 -04002087TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002088{
Jamie Madill06145232015-05-13 13:10:01 -04002089 TPublicType publicType = publicTypeIn;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002090 TOperator op = EOpNull;
2091 if (publicType.userDef)
2092 {
2093 op = EOpConstructStruct;
2094 }
2095 else
2096 {
2097 switch (publicType.type)
2098 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002099 case EbtFloat:
2100 if (publicType.isMatrix())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002101 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002102 switch (publicType.getCols())
Alexis Hetu07e57df2015-06-16 16:55:52 -04002103 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002104 case 2:
2105 switch (publicType.getRows())
2106 {
2107 case 2:
2108 op = EOpConstructMat2;
2109 break;
2110 case 3:
2111 op = EOpConstructMat2x3;
2112 break;
2113 case 4:
2114 op = EOpConstructMat2x4;
2115 break;
2116 }
2117 break;
2118 case 3:
2119 switch (publicType.getRows())
2120 {
2121 case 2:
2122 op = EOpConstructMat3x2;
2123 break;
2124 case 3:
2125 op = EOpConstructMat3;
2126 break;
2127 case 4:
2128 op = EOpConstructMat3x4;
2129 break;
2130 }
2131 break;
2132 case 4:
2133 switch (publicType.getRows())
2134 {
2135 case 2:
2136 op = EOpConstructMat4x2;
2137 break;
2138 case 3:
2139 op = EOpConstructMat4x3;
2140 break;
2141 case 4:
2142 op = EOpConstructMat4;
2143 break;
2144 }
2145 break;
Alexis Hetu07e57df2015-06-16 16:55:52 -04002146 }
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002147 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04002148 else
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002149 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002150 switch (publicType.getNominalSize())
2151 {
2152 case 1:
2153 op = EOpConstructFloat;
2154 break;
2155 case 2:
2156 op = EOpConstructVec2;
2157 break;
2158 case 3:
2159 op = EOpConstructVec3;
2160 break;
2161 case 4:
2162 op = EOpConstructVec4;
2163 break;
2164 }
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002165 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04002166 break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002167
Jamie Madillb98c3a82015-07-23 14:26:04 -04002168 case EbtInt:
2169 switch (publicType.getNominalSize())
2170 {
2171 case 1:
2172 op = EOpConstructInt;
2173 break;
2174 case 2:
2175 op = EOpConstructIVec2;
2176 break;
2177 case 3:
2178 op = EOpConstructIVec3;
2179 break;
2180 case 4:
2181 op = EOpConstructIVec4;
2182 break;
2183 }
2184 break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002185
Jamie Madillb98c3a82015-07-23 14:26:04 -04002186 case EbtUInt:
2187 switch (publicType.getNominalSize())
2188 {
2189 case 1:
2190 op = EOpConstructUInt;
2191 break;
2192 case 2:
2193 op = EOpConstructUVec2;
2194 break;
2195 case 3:
2196 op = EOpConstructUVec3;
2197 break;
2198 case 4:
2199 op = EOpConstructUVec4;
2200 break;
2201 }
2202 break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002203
Jamie Madillb98c3a82015-07-23 14:26:04 -04002204 case EbtBool:
2205 switch (publicType.getNominalSize())
2206 {
2207 case 1:
2208 op = EOpConstructBool;
2209 break;
2210 case 2:
2211 op = EOpConstructBVec2;
2212 break;
2213 case 3:
2214 op = EOpConstructBVec3;
2215 break;
2216 case 4:
2217 op = EOpConstructBVec4;
2218 break;
2219 }
2220 break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002221
Jamie Madillb98c3a82015-07-23 14:26:04 -04002222 default:
2223 break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002224 }
2225
2226 if (op == EOpNull)
2227 {
2228 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
2229 recover();
2230 publicType.type = EbtFloat;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002231 op = EOpConstructFloat;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002232 }
2233 }
2234
2235 TString tempString;
Dmitry Skiba7f17a502015-06-22 15:08:39 -07002236 const TType *type = new TType(publicType);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002237 return new TFunction(&tempString, type, op);
2238}
2239
Jamie Madillb98c3a82015-07-23 14:26:04 -04002240// This function is used to test for the correctness of the parameters passed to various constructor
2241// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002242//
2243// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
2244//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002245TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments,
2246 TType *type,
2247 TOperator op,
2248 TFunction *fnCall,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302249 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002250{
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002251 TIntermAggregate *aggregateArguments = arguments->getAsAggregate();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002252
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002253 if (!aggregateArguments)
2254 {
2255 aggregateArguments = new TIntermAggregate;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002256 aggregateArguments->getSequence()->push_back(arguments);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002257 }
2258
Olli Etuahof40319e2015-03-10 14:33:00 +02002259 if (type->isArray())
2260 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002261 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
2262 // the array.
Olli Etuahof40319e2015-03-10 14:33:00 +02002263 TIntermSequence *args = aggregateArguments->getSequence();
2264 for (size_t i = 0; i < args->size(); i++)
2265 {
2266 const TType &argType = (*args)[i]->getAsTyped()->getType();
2267 // It has already been checked that the argument is not an array.
2268 ASSERT(!argType.isArray());
2269 if (!argType.sameElementType(*type))
2270 {
2271 error(line, "Array constructor argument has an incorrect type", "Error");
2272 recover();
2273 return nullptr;
2274 }
2275 }
2276 }
2277 else if (op == EOpConstructStruct)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002278 {
2279 const TFieldList &fields = type->getStruct()->fields();
Jamie Madillb98c3a82015-07-23 14:26:04 -04002280 TIntermSequence *args = aggregateArguments->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002281
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002282 for (size_t i = 0; i < fields.size(); i++)
2283 {
Nicolas Capensffd73872014-08-21 13:49:16 -04002284 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002285 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002286 error(line, "Structure constructor arguments do not match structure fields",
2287 "Error");
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002288 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002289
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002290 return 0;
2291 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002292 }
2293 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002294
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002295 // Turn the argument list itself into a constructor
Jamie Madillb98c3a82015-07-23 14:26:04 -04002296 TIntermAggregate *constructor = intermediate.setAggregateOperator(aggregateArguments, op, line);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002297 ASSERT(constructor->isConstructor());
2298
2299 // Need to set type before setPrecisionFromChildren() because bool doesn't have precision.
2300 constructor->setType(*type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002301
Olli Etuaho21203702014-11-13 16:16:21 +02002302 // Structs should not be precision qualified, the individual members may be.
2303 // Built-in types on the other hand should be precision qualified.
2304 if (op != EOpConstructStruct)
2305 {
2306 constructor->setPrecisionFromChildren();
2307 type->setPrecision(constructor->getPrecision());
2308 }
2309
Olli Etuaho1d122782015-11-06 15:35:17 +02002310 TIntermTyped *constConstructor = intermediate.foldAggregateBuiltIn(constructor);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002311 if (constConstructor)
2312 {
2313 return constConstructor;
2314 }
2315
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002316 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002317}
2318
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002319//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002320// This function returns the tree representation for the vector field(s) being accessed from contant
2321// vector.
2322// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a
2323// contant node is returned, else an aggregate node is returned (for v.xy). The input to this
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002324// function could either be the symbol node or it could be the intermediate tree representation of
2325// accessing fields in a constant structure or column of a constant matrix.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002326//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002327TIntermTyped *TParseContext::addConstVectorNode(TVectorFields &fields,
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002328 TIntermConstantUnion *node,
2329 const TSourceLoc &line,
2330 bool outOfRangeIndexIsError)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002331{
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002332 const TConstantUnion *unionArray = node->getUnionArrayPointer();
2333 ASSERT(unionArray);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002334
Arun Patole7e7e68d2015-05-22 12:02:25 +05302335 TConstantUnion *constArray = new TConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002336
Arun Patole7e7e68d2015-05-22 12:02:25 +05302337 for (int i = 0; i < fields.num; i++)
2338 {
2339 if (fields.offsets[i] >= node->getType().getNominalSize())
2340 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002341 std::stringstream extraInfoStream;
2342 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
2343 std::string extraInfo = extraInfoStream.str();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002344 outOfRangeError(outOfRangeIndexIsError, line, "", "[", extraInfo.c_str());
2345 fields.offsets[i] = node->getType().getNominalSize() - 1;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002346 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302347
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002348 constArray[i] = unionArray[fields.offsets[i]];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302349 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002350 return intermediate.addConstantUnion(constArray, node->getType(), line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002351}
2352
2353//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002354// This function returns the column being accessed from a constant matrix. The values are retrieved
2355// 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 +02002356// The input to the function could either be a symbol node (m[0] where m is a constant matrix)that
2357// represents a constant matrix or it could be the tree representation of the constant matrix
2358// (s.m1[0] where s is a constant structure)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002359//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002360TIntermTyped *TParseContext::addConstMatrixNode(int index,
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002361 TIntermConstantUnion *node,
2362 const TSourceLoc &line,
2363 bool outOfRangeIndexIsError)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002364{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302365 if (index >= node->getType().getCols())
2366 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002367 std::stringstream extraInfoStream;
2368 extraInfoStream << "matrix field selection out of range '" << index << "'";
2369 std::string extraInfo = extraInfoStream.str();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002370 outOfRangeError(outOfRangeIndexIsError, line, "", "[", extraInfo.c_str());
2371 index = node->getType().getCols() - 1;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002372 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002373
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002374 TConstantUnion *unionArray = node->getUnionArrayPointer();
2375 int size = node->getType().getCols();
2376 return intermediate.addConstantUnion(&unionArray[size * index], node->getType(), line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002377}
2378
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002379//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002380// This function returns an element of an array accessed from a constant array. The values are
2381// 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 +05302382// 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 -04002383// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a
2384// constant structure)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002385//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002386TIntermTyped *TParseContext::addConstArrayNode(int index,
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002387 TIntermConstantUnion *node,
2388 const TSourceLoc &line,
2389 bool outOfRangeIndexIsError)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002390{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002391 TType arrayElementType = node->getType();
2392 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002393
Arun Patole7e7e68d2015-05-22 12:02:25 +05302394 if (index >= node->getType().getArraySize())
2395 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002396 std::stringstream extraInfoStream;
2397 extraInfoStream << "array field selection out of range '" << index << "'";
2398 std::string extraInfo = extraInfoStream.str();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002399 outOfRangeError(outOfRangeIndexIsError, line, "", "[", extraInfo.c_str());
2400 index = node->getType().getArraySize() - 1;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002401 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002402 size_t arrayElementSize = arrayElementType.getObjectSize();
2403 TConstantUnion *unionArray = node->getUnionArrayPointer();
2404 return intermediate.addConstantUnion(&unionArray[arrayElementSize * index], node->getType(),
2405 line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002406}
2407
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002408//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002409// This function returns the value of a particular field inside a constant structure from the symbol
2410// table.
2411// If there is an embedded/nested struct, it appropriately calls addConstStructNested or
2412// addConstStructFromAggr function and returns the parse-tree with the values of the embedded/nested
2413// struct.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002414//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002415TIntermTyped *TParseContext::addConstStruct(const TString &identifier,
2416 TIntermTyped *node,
2417 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002418{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302419 const TFieldList &fields = node->getType().getStruct()->fields();
Jamie Madillb98c3a82015-07-23 14:26:04 -04002420 size_t instanceSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002421
Arun Patole7e7e68d2015-05-22 12:02:25 +05302422 for (size_t index = 0; index < fields.size(); ++index)
2423 {
2424 if (fields[index]->name() == identifier)
2425 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002426 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302427 }
2428 else
2429 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002430 instanceSize += fields[index]->type()->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002431 }
2432 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002433
Jamie Madill94bf7f22013-07-08 13:31:15 -04002434 TIntermTyped *typedNode;
2435 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
Arun Patole7e7e68d2015-05-22 12:02:25 +05302436 if (tempConstantNode)
2437 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002438 TConstantUnion *constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002439
Jamie Madillb98c3a82015-07-23 14:26:04 -04002440 // type will be changed in the calling function
2441 typedNode = intermediate.addConstantUnion(constArray + instanceSize,
2442 tempConstantNode->getType(), line);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302443 }
2444 else
2445 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002446 error(line, "Cannot offset into the structure", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002447 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002448
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002449 return 0;
2450 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002451
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002452 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002453}
2454
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002455//
2456// Interface/uniform blocks
2457//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002458TIntermAggregate *TParseContext::addInterfaceBlock(const TPublicType &typeQualifier,
2459 const TSourceLoc &nameLine,
2460 const TString &blockName,
2461 TFieldList *fieldList,
2462 const TString *instanceName,
2463 const TSourceLoc &instanceLine,
2464 TIntermTyped *arrayIndex,
2465 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002466{
2467 if (reservedErrorCheck(nameLine, blockName))
2468 recover();
2469
2470 if (typeQualifier.qualifier != EvqUniform)
2471 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302472 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
2473 "interface blocks must be uniform");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002474 recover();
2475 }
2476
Jamie Madill099c0f32013-06-20 11:55:52 -04002477 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
2478 if (layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002479 {
2480 recover();
2481 }
2482
Jamie Madill099c0f32013-06-20 11:55:52 -04002483 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2484 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002485 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002486 }
2487
Jamie Madill1566ef72013-06-20 11:55:54 -04002488 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2489 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002490 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002491 }
2492
Arun Patole7e7e68d2015-05-22 12:02:25 +05302493 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2494 if (!symbolTable.declare(blockNameSymbol))
2495 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002496 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
2497 recover();
2498 }
2499
Jamie Madill98493dd2013-07-08 14:39:03 -04002500 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302501 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2502 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002503 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302504 TType *fieldType = field->type();
2505 if (IsSampler(fieldType->getBasicType()))
2506 {
2507 error(field->line(), "unsupported type", fieldType->getBasicString(),
2508 "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002509 recover();
2510 }
2511
Jamie Madill98493dd2013-07-08 14:39:03 -04002512 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002513 switch (qualifier)
2514 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002515 case EvqGlobal:
2516 case EvqUniform:
2517 break;
2518 default:
2519 error(field->line(), "invalid qualifier on interface block member",
2520 getQualifierString(qualifier));
2521 recover();
2522 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002523 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002524
2525 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002526 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
2527 if (layoutLocationErrorCheck(field->line(), fieldLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002528 {
2529 recover();
2530 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002531
Jamie Madill98493dd2013-07-08 14:39:03 -04002532 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002533 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002534 error(field->line(), "invalid layout qualifier:",
2535 getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04002536 recover();
2537 }
2538
Jamie Madill98493dd2013-07-08 14:39:03 -04002539 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002540 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002541 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002542 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002543 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002544 {
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002545 warning(field->line(), "extraneous layout qualifier:",
Jamie Madillb98c3a82015-07-23 14:26:04 -04002546 getMatrixPackingString(fieldLayoutQualifier.matrixPacking),
2547 "only has an effect on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04002548 }
2549
Jamie Madill98493dd2013-07-08 14:39:03 -04002550 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002551 }
2552
Jamie Madill98493dd2013-07-08 14:39:03 -04002553 // add array index
2554 int arraySize = 0;
2555 if (arrayIndex != NULL)
2556 {
2557 if (arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize))
2558 recover();
2559 }
2560
Jamie Madillb98c3a82015-07-23 14:26:04 -04002561 TInterfaceBlock *interfaceBlock =
2562 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2563 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
2564 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002565
2566 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04002567 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002568
Jamie Madill98493dd2013-07-08 14:39:03 -04002569 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002570 {
2571 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002572 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2573 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002574 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302575 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002576
2577 // set parent pointer of the field variable
2578 fieldType->setInterfaceBlock(interfaceBlock);
2579
Arun Patole7e7e68d2015-05-22 12:02:25 +05302580 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002581 fieldVariable->setQualifier(typeQualifier.qualifier);
2582
Arun Patole7e7e68d2015-05-22 12:02:25 +05302583 if (!symbolTable.declare(fieldVariable))
2584 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002585 error(field->line(), "redefinition", field->name().c_str(),
2586 "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002587 recover();
2588 }
2589 }
2590 }
2591 else
2592 {
Olli Etuahoe0f623a2015-07-10 11:58:30 +03002593 if (reservedErrorCheck(instanceLine, *instanceName))
2594 recover();
2595
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002596 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302597 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002598 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002599
Arun Patole7e7e68d2015-05-22 12:02:25 +05302600 if (!symbolTable.declare(instanceTypeDef))
2601 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002602 error(instanceLine, "redefinition", instanceName->c_str(),
2603 "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002604 recover();
2605 }
2606
Jamie Madillb98c3a82015-07-23 14:26:04 -04002607 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002608 symbolName = instanceTypeDef->getName();
2609 }
2610
Jamie Madillb98c3a82015-07-23 14:26:04 -04002611 TIntermAggregate *aggregate = intermediate.makeAggregate(
2612 intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line),
2613 nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002614 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04002615
2616 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002617 return aggregate;
2618}
2619
Arun Patole7e7e68d2015-05-22 12:02:25 +05302620bool TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002621{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002622 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002623
2624 // Embedded structure definitions are not supported per GLSL ES spec.
2625 // They aren't allowed in GLSL either, but we need to detect this here
2626 // so we don't rely on the GLSL compiler to catch it.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302627 if (mStructNestingLevel > 1)
2628 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002629 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002630 return true;
2631 }
2632
2633 return false;
2634}
2635
2636void TParseContext::exitStructDeclaration()
2637{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002638 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002639}
2640
Jamie Madillb98c3a82015-07-23 14:26:04 -04002641namespace
2642{
kbr@chromium.org476541f2011-10-27 21:14:51 +00002643const int kWebGLMaxStructNesting = 4;
2644
2645} // namespace
2646
Arun Patole7e7e68d2015-05-22 12:02:25 +05302647bool TParseContext::structNestingErrorCheck(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002648{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302649 if (!IsWebGLBasedSpec(mShaderSpec))
2650 {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002651 return false;
2652 }
2653
Arun Patole7e7e68d2015-05-22 12:02:25 +05302654 if (field.type()->getBasicType() != EbtStruct)
2655 {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002656 return false;
2657 }
2658
2659 // We're already inside a structure definition at this point, so add
2660 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302661 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2662 {
Jamie Madill41a49272014-03-18 16:10:13 -04002663 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002664 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
2665 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04002666 std::string reason = reasonStream.str();
2667 error(line, reason.c_str(), field.name().c_str(), "");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002668 return true;
2669 }
2670
2671 return false;
2672}
2673
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002674//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002675// Parse an array index expression
2676//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002677TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
2678 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302679 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002680{
2681 TIntermTyped *indexedExpression = NULL;
2682
2683 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2684 {
2685 if (baseExpression->getAsSymbolNode())
2686 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302687 error(location, " left of '[' is not of type array, matrix, or vector ",
2688 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002689 }
2690 else
2691 {
2692 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2693 }
2694 recover();
2695 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002696
Jamie Madill21c1e452014-12-29 11:33:41 -05002697 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2698
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002699 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002700 {
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002701 // If the index is not qualified as constant, the behavior in the spec is undefined. This
2702 // applies even if ANGLE has been able to constant fold it (ANGLE may constant fold
2703 // expressions that are not constant expressions). The most compatible way to handle this
2704 // case is to report a warning instead of an error and force the index to be in the
2705 // correct range.
2706 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Jamie Madill21c1e452014-12-29 11:33:41 -05002707 int index = indexConstantUnion->getIConst(0);
Jamie Madill7164cf42013-07-08 13:30:59 -04002708 if (index < 0)
2709 {
2710 std::stringstream infoStream;
2711 infoStream << index;
2712 std::string info = infoStream.str();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002713 outOfRangeError(outOfRangeIndexIsError, location, "negative index", info.c_str());
Jamie Madill7164cf42013-07-08 13:30:59 -04002714 index = 0;
2715 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002716 TIntermConstantUnion *baseConstantUnion = baseExpression->getAsConstantUnion();
2717 if (baseConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002718 {
2719 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002720 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002721 // constant folding for array indexing
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002722 indexedExpression =
2723 addConstArrayNode(index, baseConstantUnion, location, outOfRangeIndexIsError);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002724 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002725 else if (baseExpression->isVector())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002726 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002727 // constant folding for vector indexing
Jamie Madill7164cf42013-07-08 13:30:59 -04002728 TVectorFields fields;
2729 fields.num = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002730 fields.offsets[0] =
2731 index; // need to do it this way because v.xy sends fields integer array
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002732 indexedExpression =
2733 addConstVectorNode(fields, baseConstantUnion, location, outOfRangeIndexIsError);
Jamie Madill7164cf42013-07-08 13:30:59 -04002734 }
2735 else if (baseExpression->isMatrix())
2736 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002737 // constant folding for matrix indexing
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002738 indexedExpression =
2739 addConstMatrixNode(index, baseConstantUnion, location, outOfRangeIndexIsError);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002740 }
2741 }
2742 else
2743 {
Jamie Madillb11e2482015-05-04 14:21:22 -04002744 int safeIndex = -1;
2745
Jamie Madill7164cf42013-07-08 13:30:59 -04002746 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002747 {
Jamie Madill18464b52013-07-08 14:01:55 -04002748 if (index >= baseExpression->getType().getArraySize())
Jamie Madill7164cf42013-07-08 13:30:59 -04002749 {
2750 std::stringstream extraInfoStream;
2751 extraInfoStream << "array index out of range '" << index << "'";
2752 std::string extraInfo = extraInfoStream.str();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002753 outOfRangeError(outOfRangeIndexIsError, location, "", "[", extraInfo.c_str());
Jamie Madillb11e2482015-05-04 14:21:22 -04002754 safeIndex = baseExpression->getType().getArraySize() - 1;
Jamie Madill7164cf42013-07-08 13:30:59 -04002755 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302756 else if (baseExpression->getQualifier() == EvqFragData && index > 0 &&
2757 !isExtensionEnabled("GL_EXT_draw_buffers"))
Jamie Madill5d287f52013-07-12 15:38:19 -04002758 {
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002759 outOfRangeError(
2760 outOfRangeIndexIsError, location, "", "[",
2761 "array indexes for gl_FragData must be zero when GL_EXT_draw_buffers is "
2762 "disabled");
Jamie Madillb11e2482015-05-04 14:21:22 -04002763 safeIndex = 0;
Jamie Madill5d287f52013-07-12 15:38:19 -04002764 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002765 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302766 else if ((baseExpression->isVector() || baseExpression->isMatrix()) &&
Jamie Madillb98c3a82015-07-23 14:26:04 -04002767 baseExpression->getType().getNominalSize() <= index)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002768 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002769 std::stringstream extraInfoStream;
2770 extraInfoStream << "field selection out of range '" << index << "'";
2771 std::string extraInfo = extraInfoStream.str();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002772 outOfRangeError(outOfRangeIndexIsError, location, "", "[", extraInfo.c_str());
Jamie Madillb11e2482015-05-04 14:21:22 -04002773 safeIndex = baseExpression->getType().getNominalSize() - 1;
Jamie Madill46131a32013-06-20 11:55:50 -04002774 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002775
Jamie Madillb11e2482015-05-04 14:21:22 -04002776 // Don't modify the data of the previous constant union, because it can point
2777 // to builtins, like gl_MaxDrawBuffers. Instead use a new sanitized object.
2778 if (safeIndex != -1)
2779 {
2780 TConstantUnion *safeConstantUnion = new TConstantUnion();
2781 safeConstantUnion->setIConst(safeIndex);
2782 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
2783 }
2784
Jamie Madillb98c3a82015-07-23 14:26:04 -04002785 indexedExpression =
2786 intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002787 }
2788 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002789 else
2790 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002791 if (baseExpression->isInterfaceBlock())
Jamie Madill7164cf42013-07-08 13:30:59 -04002792 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002793 error(
2794 location, "", "[",
2795 "array indexes for interface blocks arrays must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002796 recover();
2797 }
Jamie Madill19571812013-08-12 15:26:34 -07002798 else if (baseExpression->getQualifier() == EvqFragmentOut)
Jamie Madill7164cf42013-07-08 13:30:59 -04002799 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002800 error(location, "", "[",
2801 "array indexes for fragment outputs must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002802 recover();
2803 }
2804
Jamie Madillb98c3a82015-07-23 14:26:04 -04002805 indexedExpression =
2806 intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
Jamie Madill7164cf42013-07-08 13:30:59 -04002807 }
2808
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002809 if (indexedExpression == 0)
2810 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002811 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002812 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002813 indexedExpression =
2814 intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002815 }
2816 else if (baseExpression->isArray())
2817 {
Olli Etuahob3fbd862015-09-30 17:55:02 +03002818 TType indexedType = baseExpression->getType();
2819 indexedType.clearArrayness();
2820 indexedExpression->setType(indexedType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002821 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002822 else if (baseExpression->isMatrix())
2823 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002824 indexedExpression->setType(TType(baseExpression->getBasicType(),
Olli Etuahob3fbd862015-09-30 17:55:02 +03002825 baseExpression->getPrecision(), EvqTemporary,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002826 static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002827 }
2828 else if (baseExpression->isVector())
2829 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002830 indexedExpression->setType(
Olli Etuahob3fbd862015-09-30 17:55:02 +03002831 TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002832 }
2833 else
2834 {
2835 indexedExpression->setType(baseExpression->getType());
2836 }
2837
Olli Etuahob3fbd862015-09-30 17:55:02 +03002838 if (baseExpression->getType().getQualifier() == EvqConst &&
2839 indexExpression->getType().getQualifier() == EvqConst)
2840 {
2841 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2842 }
2843 else
2844 {
2845 indexedExpression->getTypePointer()->setQualifier(EvqTemporary);
2846 }
2847
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002848 return indexedExpression;
2849}
2850
Jamie Madillb98c3a82015-07-23 14:26:04 -04002851TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
2852 const TSourceLoc &dotLocation,
2853 const TString &fieldString,
2854 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002855{
2856 TIntermTyped *indexedExpression = NULL;
2857
2858 if (baseExpression->isArray())
2859 {
2860 error(fieldLocation, "cannot apply dot operator to an array", ".");
2861 recover();
2862 }
2863
2864 if (baseExpression->isVector())
2865 {
2866 TVectorFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002867 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields,
2868 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002869 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002870 fields.num = 1;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002871 fields.offsets[0] = 0;
2872 recover();
2873 }
2874
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002875 if (baseExpression->getAsConstantUnion())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002876 {
2877 // constant folding for vector fields
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002878 indexedExpression = addConstVectorNode(fields, baseExpression->getAsConstantUnion(),
2879 fieldLocation, true);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002880 }
2881 else
2882 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302883 TIntermTyped *index = intermediate.addSwizzle(fields, fieldLocation);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002884 indexedExpression =
2885 intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002886 }
2887 if (indexedExpression == nullptr)
2888 {
2889 recover();
2890 indexedExpression = baseExpression;
2891 }
2892 else
2893 {
2894 // Note that the qualifier set here will be corrected later.
Jamie Madillb98c3a82015-07-23 14:26:04 -04002895 indexedExpression->setType(TType(baseExpression->getBasicType(),
2896 baseExpression->getPrecision(), EvqTemporary,
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002897 (unsigned char)(fieldString).size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002898 }
2899 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002900 else if (baseExpression->getBasicType() == EbtStruct)
2901 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002902 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302903 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04002904 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002905 {
2906 error(dotLocation, "structure has no fields", "Internal Error");
2907 recover();
2908 indexedExpression = baseExpression;
2909 }
2910 else
2911 {
2912 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002913 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002914 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002915 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002916 {
2917 fieldFound = true;
2918 break;
2919 }
2920 }
2921 if (fieldFound)
2922 {
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002923 if (baseExpression->getAsConstantUnion())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002924 {
2925 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2926 if (indexedExpression == 0)
2927 {
2928 recover();
2929 indexedExpression = baseExpression;
2930 }
2931 else
2932 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002933 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002934 }
2935 }
2936 else
2937 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002938 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002939 unionArray->setIConst(i);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002940 TIntermTyped *index = intermediate.addConstantUnion(
2941 unionArray, *fields[i]->type(), fieldLocation);
2942 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression,
2943 index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002944 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002945 }
2946 }
2947 else
2948 {
2949 error(dotLocation, " no such field in structure", fieldString.c_str());
2950 recover();
2951 indexedExpression = baseExpression;
2952 }
2953 }
2954 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002955 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002956 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002957 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302958 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04002959 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002960 {
2961 error(dotLocation, "interface block has no fields", "Internal Error");
2962 recover();
2963 indexedExpression = baseExpression;
2964 }
2965 else
2966 {
2967 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002968 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002969 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002970 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002971 {
2972 fieldFound = true;
2973 break;
2974 }
2975 }
2976 if (fieldFound)
2977 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002978 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002979 unionArray->setIConst(i);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002980 TIntermTyped *index =
2981 intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
2982 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock,
2983 baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002984 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002985 }
2986 else
2987 {
2988 error(dotLocation, " no such field in interface block", fieldString.c_str());
2989 recover();
2990 indexedExpression = baseExpression;
2991 }
2992 }
2993 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002994 else
2995 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002996 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002997 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03002998 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05302999 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003000 }
3001 else
3002 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303003 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003004 " field selection requires structure, vector, or interface block on left hand "
3005 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303006 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003007 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003008 recover();
3009 indexedExpression = baseExpression;
3010 }
3011
Olli Etuahob1edc4f2015-11-02 17:20:03 +02003012 if (baseExpression->getQualifier() == EvqConst)
3013 {
3014 indexedExpression->getTypePointer()->setQualifier(EvqConst);
3015 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003016 else
3017 {
3018 indexedExpression->getTypePointer()->setQualifier(EvqTemporary);
3019 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02003020
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003021 return indexedExpression;
3022}
3023
Jamie Madillb98c3a82015-07-23 14:26:04 -04003024TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3025 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003026{
Jamie Madilla5efff92013-06-06 11:56:47 -04003027 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003028
Jamie Madillb98c3a82015-07-23 14:26:04 -04003029 qualifier.location = -1;
Jamie Madilla5efff92013-06-06 11:56:47 -04003030 qualifier.matrixPacking = EmpUnspecified;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003031 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003032
3033 if (qualifierType == "shared")
3034 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003035 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003036 }
3037 else if (qualifierType == "packed")
3038 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003039 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003040 }
3041 else if (qualifierType == "std140")
3042 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003043 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003044 }
3045 else if (qualifierType == "row_major")
3046 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003047 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003048 }
3049 else if (qualifierType == "column_major")
3050 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003051 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003052 }
3053 else if (qualifierType == "location")
3054 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003055 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
3056 "location requires an argument");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003057 recover();
3058 }
3059 else
3060 {
3061 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
3062 recover();
3063 }
3064
Jamie Madilla5efff92013-06-06 11:56:47 -04003065 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003066}
3067
Jamie Madillb98c3a82015-07-23 14:26:04 -04003068TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3069 const TSourceLoc &qualifierTypeLine,
3070 const TString &intValueString,
3071 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303072 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003073{
Jamie Madilla5efff92013-06-06 11:56:47 -04003074 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003075
Jamie Madillb98c3a82015-07-23 14:26:04 -04003076 qualifier.location = -1;
Jamie Madilla5efff92013-06-06 11:56:47 -04003077 qualifier.matrixPacking = EmpUnspecified;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003078 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003079
3080 if (qualifierType != "location")
3081 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303082 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
3083 "only location may have arguments");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003084 recover();
3085 }
3086 else
3087 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003088 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003089 if (intValue < 0)
3090 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003091 error(intValueLine, "out of range:", intValueString.c_str(),
3092 "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003093 recover();
3094 }
3095 else
3096 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003097 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003098 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003099 }
3100
Jamie Madilla5efff92013-06-06 11:56:47 -04003101 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003102}
3103
Jamie Madillb98c3a82015-07-23 14:26:04 -04003104TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
3105 TLayoutQualifier rightQualifier)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003106{
Jamie Madilla5efff92013-06-06 11:56:47 -04003107 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003108
Jamie Madilla5efff92013-06-06 11:56:47 -04003109 if (rightQualifier.location != -1)
3110 {
3111 joinedQualifier.location = rightQualifier.location;
3112 }
3113 if (rightQualifier.matrixPacking != EmpUnspecified)
3114 {
3115 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
3116 }
3117 if (rightQualifier.blockStorage != EbsUnspecified)
3118 {
3119 joinedQualifier.blockStorage = rightQualifier.blockStorage;
3120 }
3121
3122 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003123}
3124
Arun Patole7e7e68d2015-05-22 12:02:25 +05303125TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc,
3126 TQualifier interpolationQualifier,
3127 const TSourceLoc &storageLoc,
3128 TQualifier storageQualifier)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003129{
3130 TQualifier mergedQualifier = EvqSmoothIn;
3131
Arun Patole7e7e68d2015-05-22 12:02:25 +05303132 if (storageQualifier == EvqFragmentIn)
3133 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003134 if (interpolationQualifier == EvqSmooth)
3135 mergedQualifier = EvqSmoothIn;
3136 else if (interpolationQualifier == EvqFlat)
3137 mergedQualifier = EvqFlatIn;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003138 else
3139 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003140 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303141 else if (storageQualifier == EvqCentroidIn)
3142 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003143 if (interpolationQualifier == EvqSmooth)
3144 mergedQualifier = EvqCentroidIn;
3145 else if (interpolationQualifier == EvqFlat)
3146 mergedQualifier = EvqFlatIn;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003147 else
3148 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003149 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303150 else if (storageQualifier == EvqVertexOut)
3151 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003152 if (interpolationQualifier == EvqSmooth)
3153 mergedQualifier = EvqSmoothOut;
3154 else if (interpolationQualifier == EvqFlat)
3155 mergedQualifier = EvqFlatOut;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003156 else
3157 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003158 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303159 else if (storageQualifier == EvqCentroidOut)
3160 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003161 if (interpolationQualifier == EvqSmooth)
3162 mergedQualifier = EvqCentroidOut;
3163 else if (interpolationQualifier == EvqFlat)
3164 mergedQualifier = EvqFlatOut;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003165 else
3166 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003167 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303168 else
3169 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003170 error(interpolationLoc,
3171 "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303172 getInterpolationString(interpolationQualifier));
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003173 recover();
3174
3175 mergedQualifier = storageQualifier;
3176 }
3177
3178 TPublicType type;
3179 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
3180 return type;
3181}
3182
Jamie Madillb98c3a82015-07-23 14:26:04 -04003183TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3184 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003185{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03003186 if (voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier.type))
3187 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003188 recover();
3189 }
3190
Arun Patole7e7e68d2015-05-22 12:02:25 +05303191 for (unsigned int i = 0; i < fieldList->size(); ++i)
3192 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003193 //
3194 // Careful not to replace already known aspects of type, like array-ness
3195 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303196 TType *type = (*fieldList)[i]->type();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003197 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003198 type->setPrimarySize(typeSpecifier.primarySize);
3199 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003200 type->setPrecision(typeSpecifier.precision);
3201 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003202 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003203
3204 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303205 if (type->isArray())
3206 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003207 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
3208 recover();
3209 }
3210 if (typeSpecifier.array)
3211 type->setArraySize(typeSpecifier.arraySize);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303212 if (typeSpecifier.userDef)
3213 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003214 type->setStruct(typeSpecifier.userDef->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003215 }
3216
Arun Patole7e7e68d2015-05-22 12:02:25 +05303217 if (structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i]))
3218 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003219 recover();
3220 }
3221 }
3222
Jamie Madill98493dd2013-07-08 14:39:03 -04003223 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003224}
3225
Jamie Madillb98c3a82015-07-23 14:26:04 -04003226TPublicType TParseContext::addStructure(const TSourceLoc &structLine,
3227 const TSourceLoc &nameLine,
3228 const TString *structName,
3229 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003230{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303231 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003232 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003233
Jamie Madill9b820842015-02-12 10:40:10 -05003234 // Store a bool in the struct if we're at global scope, to allow us to
3235 // skip the local struct scoping workaround in HLSL.
Jamie Madillb960cc42015-02-12 15:33:20 +00003236 structure->setUniqueId(TSymbolTable::nextUniqueId());
Jamie Madill9b820842015-02-12 10:40:10 -05003237 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003238
Jamie Madill98493dd2013-07-08 14:39:03 -04003239 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003240 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003241 if (reservedErrorCheck(nameLine, *structName))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003242 {
3243 recover();
3244 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303245 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
3246 if (!symbolTable.declare(userTypeDef))
3247 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003248 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003249 recover();
3250 }
3251 }
3252
3253 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04003254 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003255 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003256 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04003257 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003258 switch (qualifier)
3259 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003260 case EvqGlobal:
3261 case EvqTemporary:
3262 break;
3263 default:
3264 error(field.line(), "invalid qualifier on struct member",
3265 getQualifierString(qualifier));
3266 recover();
3267 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003268 }
3269 }
3270
3271 TPublicType publicType;
3272 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003273 publicType.userDef = structureType;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003274 exitStructDeclaration();
3275
3276 return publicType;
3277}
3278
Jamie Madillb98c3a82015-07-23 14:26:04 -04003279TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
3280 TIntermAggregate *statementList,
3281 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02003282{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003283 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003284 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02003285 init->isVector())
3286 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003287 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
3288 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003289 recover();
3290 return nullptr;
3291 }
3292
Olli Etuahoac5274d2015-02-20 10:19:08 +02003293 if (statementList)
3294 {
3295 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
3296 {
3297 recover();
3298 return nullptr;
3299 }
3300 }
3301
Olli Etuahoa3a36662015-02-17 13:46:51 +02003302 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3303 if (node == nullptr)
3304 {
3305 error(loc, "erroneous switch statement", "switch");
3306 recover();
3307 return nullptr;
3308 }
3309 return node;
3310}
3311
3312TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3313{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003314 if (mSwitchNestingLevel == 0)
3315 {
3316 error(loc, "case labels need to be inside switch statements", "case");
3317 recover();
3318 return nullptr;
3319 }
3320 if (condition == nullptr)
3321 {
3322 error(loc, "case label must have a condition", "case");
3323 recover();
3324 return nullptr;
3325 }
3326 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04003327 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02003328 {
3329 error(condition->getLine(), "case label must be a scalar integer", "case");
3330 recover();
3331 }
3332 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003333 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
3334 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
3335 // fold in case labels.
3336 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02003337 {
3338 error(condition->getLine(), "case label must be constant", "case");
3339 recover();
3340 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003341 TIntermCase *node = intermediate.addCase(condition, loc);
3342 if (node == nullptr)
3343 {
3344 error(loc, "erroneous case statement", "case");
3345 recover();
3346 return nullptr;
3347 }
3348 return node;
3349}
3350
3351TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3352{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003353 if (mSwitchNestingLevel == 0)
3354 {
3355 error(loc, "default labels need to be inside switch statements", "default");
3356 recover();
3357 return nullptr;
3358 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003359 TIntermCase *node = intermediate.addCase(nullptr, loc);
3360 if (node == nullptr)
3361 {
3362 error(loc, "erroneous default statement", "default");
3363 recover();
3364 return nullptr;
3365 }
3366 return node;
3367}
3368
Jamie Madillb98c3a82015-07-23 14:26:04 -04003369TIntermTyped *TParseContext::createUnaryMath(TOperator op,
3370 TIntermTyped *child,
3371 const TSourceLoc &loc,
3372 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003373{
3374 if (child == nullptr)
3375 {
3376 return nullptr;
3377 }
3378
3379 switch (op)
3380 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003381 case EOpLogicalNot:
3382 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
3383 child->isVector())
3384 {
3385 return nullptr;
3386 }
3387 break;
3388 case EOpBitwiseNot:
3389 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3390 child->isMatrix() || child->isArray())
3391 {
3392 return nullptr;
3393 }
3394 break;
3395 case EOpPostIncrement:
3396 case EOpPreIncrement:
3397 case EOpPostDecrement:
3398 case EOpPreDecrement:
3399 case EOpNegative:
3400 case EOpPositive:
3401 if (child->getBasicType() == EbtStruct || child->getBasicType() == EbtBool ||
3402 child->isArray())
3403 {
3404 return nullptr;
3405 }
3406 // Operators for built-ins are already type checked against their prototype.
3407 default:
3408 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003409 }
3410
Olli Etuahof6c694b2015-03-26 14:50:53 +02003411 return intermediate.addUnaryMath(op, child, loc, funcReturnType);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003412}
3413
Olli Etuaho09b22472015-02-11 11:47:26 +02003414TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3415{
Olli Etuahof6c694b2015-03-26 14:50:53 +02003416 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003417 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003418 {
3419 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
3420 recover();
3421 return child;
3422 }
3423 return node;
3424}
3425
Jamie Madillb98c3a82015-07-23 14:26:04 -04003426TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
3427 TIntermTyped *child,
3428 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003429{
3430 if (lValueErrorCheck(loc, GetOperatorString(op), child))
3431 recover();
3432 return addUnaryMath(op, child, loc);
3433}
3434
Jamie Madillb98c3a82015-07-23 14:26:04 -04003435bool TParseContext::binaryOpCommonCheck(TOperator op,
3436 TIntermTyped *left,
3437 TIntermTyped *right,
3438 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003439{
3440 if (left->isArray() || right->isArray())
3441 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003442 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003443 {
3444 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3445 return false;
3446 }
3447
3448 if (left->isArray() != right->isArray())
3449 {
3450 error(loc, "array / non-array mismatch", GetOperatorString(op));
3451 return false;
3452 }
3453
3454 switch (op)
3455 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003456 case EOpEqual:
3457 case EOpNotEqual:
3458 case EOpAssign:
3459 case EOpInitialize:
3460 break;
3461 default:
3462 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3463 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02003464 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003465 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003466 if (left->getArraySize() != right->getArraySize())
3467 {
3468 error(loc, "array size mismatch", GetOperatorString(op));
3469 return false;
3470 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003471 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003472
3473 // Check ops which require integer / ivec parameters
3474 bool isBitShift = false;
3475 switch (op)
3476 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003477 case EOpBitShiftLeft:
3478 case EOpBitShiftRight:
3479 case EOpBitShiftLeftAssign:
3480 case EOpBitShiftRightAssign:
3481 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3482 // check that the basic type is an integer type.
3483 isBitShift = true;
3484 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3485 {
3486 return false;
3487 }
3488 break;
3489 case EOpBitwiseAnd:
3490 case EOpBitwiseXor:
3491 case EOpBitwiseOr:
3492 case EOpBitwiseAndAssign:
3493 case EOpBitwiseXorAssign:
3494 case EOpBitwiseOrAssign:
3495 // It is enough to check the type of only one operand, since later it
3496 // is checked that the operand types match.
3497 if (!IsInteger(left->getBasicType()))
3498 {
3499 return false;
3500 }
3501 break;
3502 default:
3503 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003504 }
3505
3506 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3507 // So the basic type should usually match.
3508 if (!isBitShift && left->getBasicType() != right->getBasicType())
3509 {
3510 return false;
3511 }
3512
Olli Etuaho9dd217b2015-03-20 14:24:31 +02003513 // Check that type sizes match exactly on ops that require that.
Olli Etuahoff699002015-03-23 14:38:42 +02003514 // Also check restrictions for structs that contain arrays or samplers.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003515 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003516 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003517 case EOpAssign:
3518 case EOpInitialize:
3519 case EOpEqual:
3520 case EOpNotEqual:
3521 // ESSL 1.00 sections 5.7, 5.8, 5.9
3522 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
3523 {
3524 error(loc, "undefined operation for structs containing arrays",
3525 GetOperatorString(op));
3526 return false;
3527 }
3528 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3529 // we interpret the spec so that this extends to structs containing samplers,
3530 // similarly to ESSL 1.00 spec.
3531 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
3532 left->getType().isStructureContainingSamplers())
3533 {
3534 error(loc, "undefined operation for structs containing samplers",
3535 GetOperatorString(op));
3536 return false;
3537 }
3538 case EOpLessThan:
3539 case EOpGreaterThan:
3540 case EOpLessThanEqual:
3541 case EOpGreaterThanEqual:
3542 if ((left->getNominalSize() != right->getNominalSize()) ||
3543 (left->getSecondarySize() != right->getSecondarySize()))
3544 {
3545 return false;
3546 }
3547 default:
3548 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003549 }
3550
Olli Etuahod6b14282015-03-17 14:31:35 +02003551 return true;
3552}
3553
Jamie Madillb98c3a82015-07-23 14:26:04 -04003554TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
3555 TIntermTyped *left,
3556 TIntermTyped *right,
3557 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02003558{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003559 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003560 return nullptr;
3561
Olli Etuahofc1806e2015-03-17 13:03:11 +02003562 switch (op)
3563 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003564 case EOpEqual:
3565 case EOpNotEqual:
3566 break;
3567 case EOpLessThan:
3568 case EOpGreaterThan:
3569 case EOpLessThanEqual:
3570 case EOpGreaterThanEqual:
3571 ASSERT(!left->isArray() && !right->isArray());
3572 if (left->isMatrix() || left->isVector() || left->getBasicType() == EbtStruct)
3573 {
3574 return nullptr;
3575 }
3576 break;
3577 case EOpLogicalOr:
3578 case EOpLogicalXor:
3579 case EOpLogicalAnd:
3580 ASSERT(!left->isArray() && !right->isArray());
3581 if (left->getBasicType() != EbtBool || left->isMatrix() || left->isVector())
3582 {
3583 return nullptr;
3584 }
3585 break;
3586 case EOpAdd:
3587 case EOpSub:
3588 case EOpDiv:
3589 case EOpMul:
3590 ASSERT(!left->isArray() && !right->isArray());
3591 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool)
3592 {
3593 return nullptr;
3594 }
3595 break;
3596 case EOpIMod:
3597 ASSERT(!left->isArray() && !right->isArray());
3598 // Note that this is only for the % operator, not for mod()
3599 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool ||
3600 left->getBasicType() == EbtFloat)
3601 {
3602 return nullptr;
3603 }
3604 break;
3605 // Note that for bitwise ops, type checking is done in promote() to
3606 // share code between ops and compound assignment
3607 default:
3608 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003609 }
3610
Olli Etuahofc1806e2015-03-17 13:03:11 +02003611 return intermediate.addBinaryMath(op, left, right, loc);
3612}
3613
Jamie Madillb98c3a82015-07-23 14:26:04 -04003614TIntermTyped *TParseContext::addBinaryMath(TOperator op,
3615 TIntermTyped *left,
3616 TIntermTyped *right,
3617 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003618{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003619 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003620 if (node == 0)
3621 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003622 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3623 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003624 recover();
3625 return left;
3626 }
3627 return node;
3628}
3629
Jamie Madillb98c3a82015-07-23 14:26:04 -04003630TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
3631 TIntermTyped *left,
3632 TIntermTyped *right,
3633 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003634{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003635 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003636 if (node == 0)
3637 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003638 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3639 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003640 recover();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003641 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02003642 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003643 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
3644 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003645 }
3646 return node;
3647}
3648
Jamie Madillb98c3a82015-07-23 14:26:04 -04003649TIntermTyped *TParseContext::createAssign(TOperator op,
3650 TIntermTyped *left,
3651 TIntermTyped *right,
3652 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003653{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003654 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003655 {
3656 return intermediate.addAssign(op, left, right, loc);
3657 }
3658 return nullptr;
3659}
3660
Jamie Madillb98c3a82015-07-23 14:26:04 -04003661TIntermTyped *TParseContext::addAssign(TOperator op,
3662 TIntermTyped *left,
3663 TIntermTyped *right,
3664 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003665{
3666 TIntermTyped *node = createAssign(op, left, right, loc);
3667 if (node == nullptr)
3668 {
3669 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
3670 recover();
3671 return left;
3672 }
3673 return node;
3674}
3675
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003676TIntermTyped *TParseContext::addComma(TIntermTyped *left,
3677 TIntermTyped *right,
3678 const TSourceLoc &loc)
3679{
Olli Etuaho15200042015-11-04 16:56:31 +02003680 return intermediate.addComma(left, right, loc, mShaderVersion);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003681}
3682
Olli Etuaho49300862015-02-20 14:54:49 +02003683TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3684{
3685 switch (op)
3686 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003687 case EOpContinue:
3688 if (mLoopNestingLevel <= 0)
3689 {
3690 error(loc, "continue statement only allowed in loops", "");
3691 recover();
3692 }
3693 break;
3694 case EOpBreak:
3695 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
3696 {
3697 error(loc, "break statement only allowed in loops and switch statements", "");
3698 recover();
3699 }
3700 break;
3701 case EOpReturn:
3702 if (mCurrentFunctionType->getBasicType() != EbtVoid)
3703 {
3704 error(loc, "non-void function must return a value", "return");
3705 recover();
3706 }
3707 break;
3708 default:
3709 // No checks for discard
3710 break;
Olli Etuaho49300862015-02-20 14:54:49 +02003711 }
3712 return intermediate.addBranch(op, loc);
3713}
3714
Jamie Madillb98c3a82015-07-23 14:26:04 -04003715TIntermBranch *TParseContext::addBranch(TOperator op,
3716 TIntermTyped *returnValue,
3717 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02003718{
3719 ASSERT(op == EOpReturn);
3720 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003721 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02003722 {
3723 error(loc, "void function cannot return a value", "return");
3724 recover();
3725 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003726 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02003727 {
3728 error(loc, "function return is not matching type:", "return");
3729 recover();
3730 }
3731 return intermediate.addBranch(op, returnValue, loc);
3732}
3733
Jamie Madillb98c3a82015-07-23 14:26:04 -04003734TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
3735 TIntermNode *paramNode,
3736 TIntermNode *thisNode,
3737 const TSourceLoc &loc,
3738 bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003739{
Jamie Madillb98c3a82015-07-23 14:26:04 -04003740 *fatalError = false;
3741 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003742 TIntermTyped *callNode = nullptr;
3743
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003744 if (thisNode != nullptr)
3745 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003746 TConstantUnion *unionArray = new TConstantUnion[1];
Jamie Madillb98c3a82015-07-23 14:26:04 -04003747 int arraySize = 0;
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003748 TIntermTyped *typedThis = thisNode->getAsTyped();
3749 if (fnCall->getName() != "length")
3750 {
3751 error(loc, "invalid method", fnCall->getName().c_str());
3752 recover();
3753 }
3754 else if (paramNode != nullptr)
3755 {
3756 error(loc, "method takes no parameters", "length");
3757 recover();
3758 }
3759 else if (typedThis == nullptr || !typedThis->isArray())
3760 {
3761 error(loc, "length can only be called on arrays", "length");
3762 recover();
3763 }
3764 else
3765 {
Olli Etuaho96e67382015-04-23 14:27:02 +03003766 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03003767 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003768 {
Olli Etuaho39282e12015-04-23 15:41:48 +03003769 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003770 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03003771 // (func()).length()
3772 // (int[3](0, 1, 2)).length()
Jamie Madillb98c3a82015-07-23 14:26:04 -04003773 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
3774 // expression.
3775 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
3776 // spec section 5.9 which allows "An array, vector or matrix expression with the
3777 // length method applied".
3778 error(loc, "length can only be called on array names, not on array expressions",
3779 "length");
Olli Etuaho39282e12015-04-23 15:41:48 +03003780 recover();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003781 }
3782 }
Olli Etuaho96e67382015-04-23 14:27:02 +03003783 unionArray->setIConst(arraySize);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003784 callNode =
3785 intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003786 }
3787 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003788 {
3789 //
3790 // Then this should be a constructor.
3791 // Don't go through the symbol table for constructors.
3792 // Their parameters will be verified algorithmically.
3793 //
3794 TType type(EbtVoid, EbpUndefined); // use this to get the type back
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003795 if (!constructorErrorCheck(loc, paramNode, *fnCall, op, &type))
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003796 {
3797 //
3798 // It's a constructor, of type 'type'.
3799 //
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003800 callNode = addConstructor(paramNode, &type, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003801 }
Olli Etuaho72ba85b2015-03-04 14:23:26 +02003802
3803 if (callNode == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003804 {
3805 recover();
3806 callNode = intermediate.setAggregateOperator(nullptr, op, loc);
3807 }
3808 callNode->setType(type);
3809 }
3810 else
3811 {
3812 //
3813 // Not a constructor. Find it in the symbol table.
3814 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303815 const TFunction *fnCandidate;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003816 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003817 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003818 if (fnCandidate)
3819 {
3820 //
3821 // A declared function.
3822 //
3823 if (builtIn && !fnCandidate->getExtension().empty() &&
3824 extensionErrorCheck(loc, fnCandidate->getExtension()))
3825 {
3826 recover();
3827 }
3828 op = fnCandidate->getBuiltInOp();
3829 if (builtIn && op != EOpNull)
3830 {
3831 //
3832 // A function call mapped to a built-in operation.
3833 //
3834 if (fnCandidate->getParamCount() == 1)
3835 {
3836 //
3837 // Treat it like a built-in unary operator.
3838 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003839 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc,
3840 &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003841 if (callNode == nullptr)
3842 {
3843 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003844 extraInfoStream
3845 << "built in unary operator function. Type: "
3846 << static_cast<TIntermTyped *>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003847 std::string extraInfo = extraInfoStream.str();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003848 error(paramNode->getLine(), " wrong operand type", "Internal Error",
3849 extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003850 *fatalError = true;
3851 return nullptr;
3852 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003853 }
3854 else
3855 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003856 TIntermAggregate *aggregate =
3857 intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003858 aggregate->setType(fnCandidate->getReturnType());
3859 aggregate->setPrecisionFromChildren();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02003860 if (aggregate->areChildrenConstQualified())
3861 {
3862 aggregate->getTypePointer()->setQualifier(EvqConst);
3863 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003864
3865 // Some built-in functions have out parameters too.
3866 functionCallLValueErrorCheck(fnCandidate, aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05303867
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003868 // See if we can constant fold a built-in. Note that this may be possible even
3869 // if it is not const-qualified.
Olli Etuahob43846e2015-06-02 18:18:57 +03003870 TIntermTyped *foldedNode = intermediate.foldAggregateBuiltIn(aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05303871 if (foldedNode)
3872 {
Arun Patole274f0702015-05-05 13:33:30 +05303873 callNode = foldedNode;
3874 }
Olli Etuahob43846e2015-06-02 18:18:57 +03003875 else
3876 {
3877 callNode = aggregate;
3878 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003879 }
3880 }
3881 else
3882 {
3883 // This is a real function call
3884
Jamie Madillb98c3a82015-07-23 14:26:04 -04003885 TIntermAggregate *aggregate =
3886 intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003887 aggregate->setType(fnCandidate->getReturnType());
3888
Jamie Madillb98c3a82015-07-23 14:26:04 -04003889 // this is how we know whether the given function is a builtIn function or a user
3890 // defined function
3891 // if builtIn == false, it's a userDefined -> could be an overloaded
3892 // builtIn function also
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003893 // if builtIn == true, it's definitely a builtIn function with EOpNull
3894 if (!builtIn)
3895 aggregate->setUserDefined();
3896 aggregate->setName(fnCandidate->getMangledName());
Corentin Wallez71d147f2015-02-11 11:15:24 -08003897 aggregate->setFunctionId(fnCandidate->getUniqueId());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003898
3899 // This needs to happen after the name is set
3900 if (builtIn)
3901 aggregate->setBuiltInFunctionPrecision();
3902
3903 callNode = aggregate;
3904
3905 functionCallLValueErrorCheck(fnCandidate, aggregate);
3906 }
3907 }
3908 else
3909 {
3910 // error message was put out by findFunction()
3911 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003912 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003913 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003914 callNode = intermediate.addConstantUnion(unionArray,
3915 TType(EbtFloat, EbpUndefined, EvqConst), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003916 recover();
3917 }
3918 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003919 return callNode;
3920}
3921
Jamie Madillb98c3a82015-07-23 14:26:04 -04003922TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
3923 TIntermTyped *trueBlock,
3924 TIntermTyped *falseBlock,
Olli Etuaho52901742015-04-15 13:42:45 +03003925 const TSourceLoc &loc)
3926{
3927 if (boolErrorCheck(loc, cond))
3928 recover();
3929
3930 if (trueBlock->getType() != falseBlock->getType())
3931 {
3932 binaryOpError(loc, ":", trueBlock->getCompleteString(), falseBlock->getCompleteString());
3933 recover();
3934 return falseBlock;
3935 }
Olli Etuahoa2d53032015-04-15 14:14:44 +03003936 // ESSL1 sections 5.2 and 5.7:
3937 // ESSL3 section 5.7:
3938 // Ternary operator is not among the operators allowed for structures/arrays.
3939 if (trueBlock->isArray() || trueBlock->getBasicType() == EbtStruct)
3940 {
3941 error(loc, "ternary operator is not allowed for structures or arrays", ":");
3942 recover();
3943 return falseBlock;
3944 }
Olli Etuaho52901742015-04-15 13:42:45 +03003945 return intermediate.addSelection(cond, trueBlock, falseBlock, loc);
3946}
Olli Etuaho49300862015-02-20 14:54:49 +02003947
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003948//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003949// Parse an array of strings using yyparse.
3950//
3951// Returns 0 for success.
3952//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003953int PaParseStrings(size_t count,
3954 const char *const string[],
3955 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05303956 TParseContext *context)
3957{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003958 if ((count == 0) || (string == NULL))
3959 return 1;
3960
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003961 if (glslang_initialize(context))
3962 return 1;
3963
alokp@chromium.org408c45e2012-04-05 15:54:43 +00003964 int error = glslang_scan(count, string, length, context);
3965 if (!error)
3966 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003967
alokp@chromium.org73bc2982012-06-19 18:48:05 +00003968 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00003969
alokp@chromium.org6b495712012-06-29 00:06:58 +00003970 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003971}