blob: fd9d2c077479bf031816ceac16b1e314b5651e67 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Jamie Madill6b9cb252013-10-17 10:45:47 -04007#include "compiler/translator/ParseContext.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +00008
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00009#include <stdarg.h>
apatrick@chromium.org8187fa82010-06-15 22:09:28 +000010#include <stdio.h>
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000011
daniel@transgaming.comb401a922012-10-26 18:58:24 +000012#include "compiler/preprocessor/SourceLocation.h"
Dmitry Skiba01971112015-07-10 14:54:00 -040013#include "compiler/translator/Cache.h"
Olli Etuahoac5274d2015-02-20 10:19:08 +020014#include "compiler/translator/glslang.h"
15#include "compiler/translator/ValidateSwitch.h"
Olli Etuahob0c645e2015-05-12 14:25:36 +030016#include "compiler/translator/ValidateGlobalInitializer.h"
Olli Etuaho37ad4742015-04-27 13:18:50 +030017#include "compiler/translator/util.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000018
alokp@chromium.org8b851c62012-06-15 16:25:11 +000019///////////////////////////////////////////////////////////////////////
20//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000021// Sub- vector and matrix fields
22//
23////////////////////////////////////////////////////////////////////////
24
25//
26// Look at a '.' field selector string and change it into offsets
27// for a vector.
28//
Jamie Madillb98c3a82015-07-23 14:26:04 -040029bool TParseContext::parseVectorFields(const TString &compString,
30 int vecSize,
31 TVectorFields &fields,
Arun Patole7e7e68d2015-05-22 12:02:25 +053032 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000033{
Jamie Madillb98c3a82015-07-23 14:26:04 -040034 fields.num = (int)compString.size();
Arun Patole7e7e68d2015-05-22 12:02:25 +053035 if (fields.num > 4)
36 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000037 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000038 return false;
39 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000040
Jamie Madillb98c3a82015-07-23 14:26:04 -040041 enum
42 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000043 exyzw,
44 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +000045 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000046 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000047
Arun Patole7e7e68d2015-05-22 12:02:25 +053048 for (int i = 0; i < fields.num; ++i)
49 {
50 switch (compString[i])
51 {
Jamie Madillb98c3a82015-07-23 14:26:04 -040052 case 'x':
53 fields.offsets[i] = 0;
54 fieldSet[i] = exyzw;
55 break;
56 case 'r':
57 fields.offsets[i] = 0;
58 fieldSet[i] = ergba;
59 break;
60 case 's':
61 fields.offsets[i] = 0;
62 fieldSet[i] = estpq;
63 break;
64 case 'y':
65 fields.offsets[i] = 1;
66 fieldSet[i] = exyzw;
67 break;
68 case 'g':
69 fields.offsets[i] = 1;
70 fieldSet[i] = ergba;
71 break;
72 case 't':
73 fields.offsets[i] = 1;
74 fieldSet[i] = estpq;
75 break;
76 case 'z':
77 fields.offsets[i] = 2;
78 fieldSet[i] = exyzw;
79 break;
80 case 'b':
81 fields.offsets[i] = 2;
82 fieldSet[i] = ergba;
83 break;
84 case 'p':
85 fields.offsets[i] = 2;
86 fieldSet[i] = estpq;
87 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053088
Jamie Madillb98c3a82015-07-23 14:26:04 -040089 case 'w':
90 fields.offsets[i] = 3;
91 fieldSet[i] = exyzw;
92 break;
93 case 'a':
94 fields.offsets[i] = 3;
95 fieldSet[i] = ergba;
96 break;
97 case 'q':
98 fields.offsets[i] = 3;
99 fieldSet[i] = estpq;
100 break;
101 default:
102 error(line, "illegal vector field selection", compString.c_str());
103 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000104 }
105 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000106
Arun Patole7e7e68d2015-05-22 12:02:25 +0530107 for (int i = 0; i < fields.num; ++i)
108 {
109 if (fields.offsets[i] >= vecSize)
110 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400111 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000112 return false;
113 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000114
Arun Patole7e7e68d2015-05-22 12:02:25 +0530115 if (i > 0)
116 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400117 if (fieldSet[i] != fieldSet[i - 1])
Arun Patole7e7e68d2015-05-22 12:02:25 +0530118 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400119 error(line, "illegal - vector component fields not from the same set",
120 compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000121 return false;
122 }
123 }
124 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000125
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000126 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000127}
128
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000129///////////////////////////////////////////////////////////////////////
130//
131// Errors
132//
133////////////////////////////////////////////////////////////////////////
134
135//
136// Track whether errors have occurred.
137//
138void TParseContext::recover()
139{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000140}
141
142//
143// Used by flex/bison to output all syntax and parsing errors.
144//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530145void TParseContext::error(const TSourceLoc &loc,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400146 const char *reason,
147 const char *token,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530148 const char *extraInfo)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000149{
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000150 pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -0400151 srcLoc.file = loc.first_file;
152 srcLoc.line = loc.first_line;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400153 mDiagnostics.writeInfo(pp::Diagnostics::PP_ERROR, srcLoc, reason, token, extraInfo);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000154}
155
Arun Patole7e7e68d2015-05-22 12:02:25 +0530156void TParseContext::warning(const TSourceLoc &loc,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400157 const char *reason,
158 const char *token,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530159 const char *extraInfo)
160{
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000161 pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -0400162 srcLoc.file = loc.first_file;
163 srcLoc.line = loc.first_line;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400164 mDiagnostics.writeInfo(pp::Diagnostics::PP_WARNING, srcLoc, reason, token, extraInfo);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000165}
166
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200167void TParseContext::outOfRangeError(bool isError,
168 const TSourceLoc &loc,
169 const char *reason,
170 const char *token,
171 const char *extraInfo)
172{
173 if (isError)
174 {
175 error(loc, reason, token, extraInfo);
176 recover();
177 }
178 else
179 {
180 warning(loc, reason, token, extraInfo);
181 }
182}
183
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000184//
185// Same error message for all places assignments don't work.
186//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530187void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000188{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000189 std::stringstream extraInfoStream;
190 extraInfoStream << "cannot convert from '" << right << "' to '" << left << "'";
191 std::string extraInfo = extraInfoStream.str();
192 error(line, "", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000193}
194
195//
196// Same error message for all places unary operations don't work.
197//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530198void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000199{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000200 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400201 extraInfoStream << "no operation '" << op << "' exists that takes an operand of type "
202 << operand << " (or there is no acceptable conversion)";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000203 std::string extraInfo = extraInfoStream.str();
204 error(line, " wrong operand type", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000205}
206
207//
208// Same error message for all binary operations don't work.
209//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400210void TParseContext::binaryOpError(const TSourceLoc &line,
211 const char *op,
212 TString left,
213 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000214{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000215 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400216 extraInfoStream << "no operation '" << op << "' exists that takes a left-hand operand of type '"
217 << left << "' and a right operand of type '" << right
218 << "' (or there is no acceptable conversion)";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000219 std::string extraInfo = extraInfoStream.str();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530220 error(line, " wrong operand types ", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000221}
222
Jamie Madillb98c3a82015-07-23 14:26:04 -0400223bool TParseContext::precisionErrorCheck(const TSourceLoc &line,
224 TPrecision precision,
225 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530226{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400227 if (!mChecksPrecisionErrors)
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000228 return false;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400229 switch (type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530230 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400231 case EbtFloat:
232 if (precision == EbpUndefined)
233 {
234 error(line, "No precision specified for (float)", "");
235 return true;
236 }
237 break;
238 case EbtInt:
239 if (precision == EbpUndefined)
240 {
241 error(line, "No precision specified (int)", "");
242 return true;
243 }
244 break;
245 default:
246 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000247 }
248 return false;
249}
250
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000251//
252// Both test and if necessary, spit out an error, to see if the node is really
253// an l-value that can be operated on this way.
254//
255// Returns true if the was an error.
256//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530257bool TParseContext::lValueErrorCheck(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000258{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400259 TIntermSymbol *symNode = node->getAsSymbolNode();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530260 TIntermBinary *binaryNode = node->getAsBinaryNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000261
Arun Patole7e7e68d2015-05-22 12:02:25 +0530262 if (binaryNode)
263 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000264 bool errorReturn;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000265
Jamie Madillb98c3a82015-07-23 14:26:04 -0400266 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530267 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400268 case EOpIndexDirect:
269 case EOpIndexIndirect:
270 case EOpIndexDirectStruct:
271 case EOpIndexDirectInterfaceBlock:
272 return lValueErrorCheck(line, op, binaryNode->getLeft());
273 case EOpVectorSwizzle:
274 errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft());
275 if (!errorReturn)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530276 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400277 int offset[4] = {0, 0, 0, 0};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000278
Jamie Madillb98c3a82015-07-23 14:26:04 -0400279 TIntermTyped *rightNode = binaryNode->getRight();
280 TIntermAggregate *aggrNode = rightNode->getAsAggregate();
281
282 for (TIntermSequence::iterator p = aggrNode->getSequence()->begin();
283 p != aggrNode->getSequence()->end(); p++)
284 {
285 int value = (*p)->getAsTyped()->getAsConstantUnion()->getIConst(0);
286 offset[value]++;
287 if (offset[value] > 1)
288 {
289 error(line, " l-value of swizzle cannot have duplicate components", op);
290
291 return true;
292 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000293 }
294 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000295
Jamie Madillb98c3a82015-07-23 14:26:04 -0400296 return errorReturn;
297 default:
298 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000299 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000300 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000301
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000302 return true;
303 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000304
Arun Patole7e7e68d2015-05-22 12:02:25 +0530305 const char *symbol = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000306 if (symNode != 0)
307 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000308
Arun Patole7e7e68d2015-05-22 12:02:25 +0530309 const char *message = 0;
310 switch (node->getQualifier())
311 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400312 case EvqConst:
313 message = "can't modify a const";
314 break;
315 case EvqConstReadOnly:
316 message = "can't modify a const";
317 break;
318 case EvqAttribute:
319 message = "can't modify an attribute";
320 break;
321 case EvqFragmentIn:
322 message = "can't modify an input";
323 break;
324 case EvqVertexIn:
325 message = "can't modify an input";
326 break;
327 case EvqUniform:
328 message = "can't modify a uniform";
329 break;
330 case EvqVaryingIn:
331 message = "can't modify a varying";
332 break;
333 case EvqFragCoord:
334 message = "can't modify gl_FragCoord";
335 break;
336 case EvqFrontFacing:
337 message = "can't modify gl_FrontFacing";
338 break;
339 case EvqPointCoord:
340 message = "can't modify gl_PointCoord";
341 break;
342 default:
343 //
344 // Type that can't be written to?
345 //
346 if (node->getBasicType() == EbtVoid)
347 {
348 message = "can't modify void";
349 }
350 if (IsSampler(node->getBasicType()))
351 {
352 message = "can't modify a sampler";
353 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000354 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000355
Arun Patole7e7e68d2015-05-22 12:02:25 +0530356 if (message == 0 && binaryNode == 0 && symNode == 0)
357 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000358 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000359
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000360 return true;
361 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000362
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000363 //
364 // Everything else is okay, no error.
365 //
366 if (message == 0)
367 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000368
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000369 //
370 // If we get here, we have an error and a message.
371 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530372 if (symNode)
373 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000374 std::stringstream extraInfoStream;
375 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
376 std::string extraInfo = extraInfoStream.str();
377 error(line, " l-value required", op, extraInfo.c_str());
378 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530379 else
380 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000381 std::stringstream extraInfoStream;
382 extraInfoStream << "(" << message << ")";
383 std::string extraInfo = extraInfoStream.str();
384 error(line, " l-value required", op, extraInfo.c_str());
385 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000386
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000387 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000388}
389
390//
391// Both test, and if necessary spit out an error, to see if the node is really
392// a constant.
393//
394// Returns true if the was an error.
395//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530396bool TParseContext::constErrorCheck(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000397{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000398 if (node->getQualifier() == EvqConst)
399 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000400
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000401 error(node->getLine(), "constant expression required", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000402
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000403 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000404}
405
406//
407// Both test, and if necessary spit out an error, to see if the node is really
408// an integer.
409//
410// Returns true if the was an error.
411//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530412bool TParseContext::integerErrorCheck(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000413{
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000414 if (node->isScalarInt())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000415 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000416
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000417 error(node->getLine(), "integer expression required", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000418
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000419 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000420}
421
422//
423// Both test, and if necessary spit out an error, to see if we are currently
424// globally scoped.
425//
426// Returns true if the was an error.
427//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530428bool TParseContext::globalErrorCheck(const TSourceLoc &line, bool global, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000429{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000430 if (global)
431 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000432
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000433 error(line, "only allowed at global scope", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000434
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000435 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000436}
437
438//
439// For now, keep it simple: if it starts "gl_", it's reserved, independent
440// of scope. Except, if the symbol table is at the built-in push-level,
441// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000442// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
443// webgl shader.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000444//
445// Returns true if there was an error.
446//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530447bool TParseContext::reservedErrorCheck(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000448{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530449 static const char *reservedErrMsg = "reserved built-in name";
450 if (!symbolTable.atBuiltInLevel())
451 {
452 if (identifier.compare(0, 3, "gl_") == 0)
453 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000454 error(line, reservedErrMsg, "gl_");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000455 return true;
456 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530457 if (IsWebGLBasedSpec(mShaderSpec))
458 {
459 if (identifier.compare(0, 6, "webgl_") == 0)
460 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000461 error(line, reservedErrMsg, "webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000462 return true;
463 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530464 if (identifier.compare(0, 7, "_webgl_") == 0)
465 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000466 error(line, reservedErrMsg, "_webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000467 return true;
468 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530469 if (mShaderSpec == SH_CSS_SHADERS_SPEC && identifier.compare(0, 4, "css_") == 0)
470 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000471 error(line, reservedErrMsg, "css_");
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000472 return true;
473 }
alokp@chromium.org613ef312010-07-21 18:54:22 +0000474 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530475 if (identifier.find("__") != TString::npos)
476 {
477 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400478 "identifiers containing two consecutive underscores (__) are reserved as "
479 "possible future keywords",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530480 identifier.c_str());
daniel@transgaming.combeadd5d2012-04-12 02:35:31 +0000481 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000482 }
483 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000484
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000485 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000486}
487
488//
489// Make sure there is enough data provided to the constructor to build
490// something of the type of the constructor. Also returns the type of
491// the constructor.
492//
493// Returns true if there was an error in construction.
494//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400495bool TParseContext::constructorErrorCheck(const TSourceLoc &line,
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200496 TIntermNode *argumentsNode,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400497 TFunction &function,
498 TOperator op,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530499 TType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000500{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000501 *type = function.getReturnType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000502
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000503 bool constructingMatrix = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400504 switch (op)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530505 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400506 case EOpConstructMat2:
507 case EOpConstructMat2x3:
508 case EOpConstructMat2x4:
509 case EOpConstructMat3x2:
510 case EOpConstructMat3:
511 case EOpConstructMat3x4:
512 case EOpConstructMat4x2:
513 case EOpConstructMat4x3:
514 case EOpConstructMat4:
515 constructingMatrix = true;
516 break;
517 default:
518 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000519 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000520
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000521 //
522 // Note: It's okay to have too many components available, but not okay to have unused
523 // arguments. 'full' will go to true when enough args have been seen. If we loop
524 // again, there is an extra argument, so 'overfull' will become true.
525 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000526
Jamie Madillb98c3a82015-07-23 14:26:04 -0400527 size_t size = 0;
528 bool constType = true;
529 bool full = false;
530 bool overFull = false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000531 bool matrixInMatrix = false;
532 bool arrayArg = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530533 for (size_t i = 0; i < function.getParamCount(); ++i)
534 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700535 const TConstParameter &param = function.getParam(i);
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000536 size += param.type->getObjectSize();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530537
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000538 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000539 matrixInMatrix = true;
540 if (full)
541 overFull = true;
542 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
543 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000544 if (param.type->getQualifier() != EvqConst)
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000545 constType = false;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000546 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000547 arrayArg = true;
548 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530549
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000550 if (constType)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000551 type->setQualifier(EvqConst);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000552
Olli Etuaho376f1b52015-04-13 13:23:41 +0300553 if (type->isArray())
554 {
555 if (type->isUnsizedArray())
556 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700557 type->setArraySize(static_cast<int>(function.getParamCount()));
Olli Etuaho376f1b52015-04-13 13:23:41 +0300558 }
559 else if (static_cast<size_t>(type->getArraySize()) != function.getParamCount())
560 {
561 error(line, "array constructor needs one argument per array element", "constructor");
562 return true;
563 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000564 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000565
Arun Patole7e7e68d2015-05-22 12:02:25 +0530566 if (arrayArg && op != EOpConstructStruct)
567 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000568 error(line, "constructing from a non-dereferenced array", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000569 return true;
570 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000571
Arun Patole7e7e68d2015-05-22 12:02:25 +0530572 if (matrixInMatrix && !type->isArray())
573 {
574 if (function.getParamCount() != 1)
575 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400576 error(line, "constructing matrix from matrix can only take one argument",
577 "constructor");
578 return true;
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000579 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000580 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000581
Arun Patole7e7e68d2015-05-22 12:02:25 +0530582 if (overFull)
583 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000584 error(line, "too many arguments", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000585 return true;
586 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530587
Jamie Madillb98c3a82015-07-23 14:26:04 -0400588 if (op == EOpConstructStruct && !type->isArray() &&
589 type->getStruct()->fields().size() != function.getParamCount())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530590 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400591 error(line,
592 "Number of constructor parameters does not match the number of structure fields",
593 "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000594 return true;
595 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000596
Arun Patole7e7e68d2015-05-22 12:02:25 +0530597 if (!type->isMatrix() || !matrixInMatrix)
598 {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000599 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
Arun Patole7e7e68d2015-05-22 12:02:25 +0530600 (op == EOpConstructStruct && size < type->getObjectSize()))
601 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000602 error(line, "not enough data provided for construction", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000603 return true;
604 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000605 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000606
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200607 if (argumentsNode == nullptr)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530608 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200609 error(line, "constructor does not have any arguments", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000610 return true;
611 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200612
613 TIntermAggregate *argumentsAgg = argumentsNode->getAsAggregate();
614 for (TIntermNode *&argNode : *argumentsAgg->getSequence())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530615 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200616 TIntermTyped *argTyped = argNode->getAsTyped();
617 ASSERT(argTyped != nullptr);
618 if (op != EOpConstructStruct && IsSampler(argTyped->getBasicType()))
619 {
620 error(line, "cannot convert a sampler", "constructor");
621 return true;
622 }
623 if (argTyped->getBasicType() == EbtVoid)
624 {
625 error(line, "cannot convert a void", "constructor");
626 return true;
627 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000628 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000629
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000630 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000631}
632
Jamie Madillb98c3a82015-07-23 14:26:04 -0400633// This function checks to see if a void variable has been declared and raise an error message for
634// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000635//
636// returns true in case of an error
637//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400638bool TParseContext::voidErrorCheck(const TSourceLoc &line,
639 const TString &identifier,
640 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000641{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300642 if (type == EbtVoid)
643 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000644 error(line, "illegal use of type 'void'", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000645 return true;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300646 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000647
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000648 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000649}
650
Jamie Madillb98c3a82015-07-23 14:26:04 -0400651// This function checks to see if the node (for the expression) contains a scalar boolean expression
652// or not
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000653//
654// returns true in case of an error
655//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530656bool TParseContext::boolErrorCheck(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000657{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530658 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector())
659 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000660 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000661 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530662 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000663
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000664 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000665}
666
Jamie Madillb98c3a82015-07-23 14:26:04 -0400667// This function checks to see if the node (for the expression) contains a scalar boolean expression
668// or not
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000669//
670// returns true in case of an error
671//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530672bool TParseContext::boolErrorCheck(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000673{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530674 if (pType.type != EbtBool || pType.isAggregate())
675 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000676 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000677 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530678 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000679
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000680 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000681}
682
Jamie Madillb98c3a82015-07-23 14:26:04 -0400683bool TParseContext::samplerErrorCheck(const TSourceLoc &line,
684 const TPublicType &pType,
685 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000686{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530687 if (pType.type == EbtStruct)
688 {
689 if (containsSampler(*pType.userDef))
690 {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000691 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530692
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000693 return true;
694 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530695
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000696 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530697 }
698 else if (IsSampler(pType.type))
699 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000700 error(line, reason, getBasicString(pType.type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000701
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000702 return true;
703 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000704
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000705 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000706}
707
Arun Patole7e7e68d2015-05-22 12:02:25 +0530708bool TParseContext::locationDeclaratorListCheck(const TSourceLoc &line, const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400709{
710 if (pType.layoutQualifier.location != -1)
711 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400712 error(line, "location must only be specified for a single input or output variable",
713 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400714 return true;
715 }
716
717 return false;
718}
719
Jamie Madillb98c3a82015-07-23 14:26:04 -0400720bool TParseContext::parameterSamplerErrorCheck(const TSourceLoc &line,
721 TQualifier qualifier,
722 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000723{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400724 if ((qualifier == EvqOut || qualifier == EvqInOut) && type.getBasicType() != EbtStruct &&
725 IsSampler(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530726 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000727 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000728 return true;
729 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000730
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000731 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000732}
733
Arun Patole7e7e68d2015-05-22 12:02:25 +0530734bool TParseContext::containsSampler(const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000735{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000736 if (IsSampler(type.getBasicType()))
737 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000738
Arun Patole7e7e68d2015-05-22 12:02:25 +0530739 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock())
740 {
741 const TFieldList &fields = type.getStruct()->fields();
742 for (unsigned int i = 0; i < fields.size(); ++i)
743 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400744 if (containsSampler(*fields[i]->type()))
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000745 return true;
746 }
747 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000748
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000749 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000750}
751
752//
753// Do size checking for an array type's size.
754//
755// Returns true if there was an error.
756//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530757bool TParseContext::arraySizeErrorCheck(const TSourceLoc &line, TIntermTyped *expr, int &size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000758{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530759 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000760
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200761 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
762 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
763 // fold as array size.
764 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000765 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000766 error(line, "array size must be a constant integer expression", "");
Olli Etuahoe7847b02015-03-16 11:56:12 +0200767 size = 1;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000768 return true;
769 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000770
Nicolas Capens906744a2014-06-06 15:18:07 -0400771 unsigned int unsignedSize = 0;
772
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000773 if (constant->getBasicType() == EbtUInt)
774 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400775 unsignedSize = constant->getUConst(0);
Jamie Madillb98c3a82015-07-23 14:26:04 -0400776 size = static_cast<int>(unsignedSize);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000777 }
778 else
779 {
780 size = constant->getIConst(0);
781
Nicolas Capens906744a2014-06-06 15:18:07 -0400782 if (size < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000783 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400784 error(line, "array size must be non-negative", "");
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000785 size = 1;
786 return true;
787 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400788
789 unsignedSize = static_cast<unsigned int>(size);
790 }
791
792 if (size == 0)
793 {
794 error(line, "array size must be greater than zero", "");
795 size = 1;
796 return true;
797 }
798
799 // The size of arrays is restricted here to prevent issues further down the
800 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
801 // 4096 registers so this should be reasonable even for aggressively optimizable code.
802 const unsigned int sizeLimit = 65536;
803
804 if (unsignedSize > sizeLimit)
805 {
806 error(line, "array size too large", "");
807 size = 1;
808 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000809 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000810
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000811 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000812}
813
814//
815// See if this qualifier can be an array.
816//
817// Returns true if there is an error.
818//
Olli Etuaho3739d232015-04-08 12:23:44 +0300819bool TParseContext::arrayQualifierErrorCheck(const TSourceLoc &line, const TPublicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000820{
Olli Etuaho3739d232015-04-08 12:23:44 +0300821 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqVertexIn) ||
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400822 (type.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300823 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400824 error(line, "cannot declare arrays of this qualifier",
825 TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000826 return true;
827 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000828
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000829 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000830}
831
832//
833// See if this type can be an array.
834//
835// Returns true if there is an error.
836//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530837bool TParseContext::arrayTypeErrorCheck(const TSourceLoc &line, const TPublicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000838{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000839 //
840 // Can the type be an array?
841 //
Jamie Madill06145232015-05-13 13:10:01 -0400842 if (type.array)
843 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000844 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000845 return true;
846 }
Olli Etuahocc36b982015-07-10 14:14:18 +0300847 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
848 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
849 // 4.3.4).
850 if (mShaderVersion >= 300 && type.type == EbtStruct && sh::IsVarying(type.qualifier))
851 {
852 error(line, "cannot declare arrays of structs of this qualifier",
853 TType(type).getCompleteString().c_str());
854 return true;
855 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000856
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000857 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000858}
859
860//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000861// Enforce non-initializer type/qualifier rules.
862//
863// Returns true if there was an error.
864//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400865bool TParseContext::nonInitErrorCheck(const TSourceLoc &line,
866 const TString &identifier,
867 TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000868{
Olli Etuaho3739d232015-04-08 12:23:44 +0300869 ASSERT(type != nullptr);
870 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000871 {
872 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300873 type->qualifier = EvqTemporary;
874
875 // Generate informative error messages for ESSL1.
876 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400877 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000878 {
Arun Patole7e7e68d2015-05-22 12:02:25 +0530879 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400880 "structures containing arrays may not be declared constant since they cannot be "
881 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530882 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000883 }
884 else
885 {
886 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
887 }
888
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000889 return true;
890 }
Olli Etuaho376f1b52015-04-13 13:23:41 +0300891 if (type->isUnsizedArray())
892 {
893 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
894 return true;
895 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000896 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000897}
898
Olli Etuaho2935c582015-04-08 14:32:06 +0300899// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000900// and update the symbol table.
901//
Olli Etuaho2935c582015-04-08 14:32:06 +0300902// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000903//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400904bool TParseContext::declareVariable(const TSourceLoc &line,
905 const TString &identifier,
906 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +0300907 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000908{
Olli Etuaho2935c582015-04-08 14:32:06 +0300909 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000910
Olli Etuaho2935c582015-04-08 14:32:06 +0300911 bool needsReservedErrorCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000912
Olli Etuaho2935c582015-04-08 14:32:06 +0300913 // gl_LastFragData may be redeclared with a new precision qualifier
914 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
915 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400916 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
917 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho2935c582015-04-08 14:32:06 +0300918 if (type.getArraySize() == maxDrawBuffers->getConstPointer()->getIConst())
919 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400920 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +0300921 {
922 needsReservedErrorCheck = extensionErrorCheck(line, builtInSymbol->getExtension());
923 }
924 }
925 else
926 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400927 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
928 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +0300929 return false;
930 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000931 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000932
Olli Etuaho2935c582015-04-08 14:32:06 +0300933 if (needsReservedErrorCheck && reservedErrorCheck(line, identifier))
934 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000935
Olli Etuaho2935c582015-04-08 14:32:06 +0300936 (*variable) = new TVariable(&identifier, type);
937 if (!symbolTable.declare(*variable))
938 {
939 error(line, "redefinition", identifier.c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -0400940 *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +0300941 return false;
942 }
943
944 if (voidErrorCheck(line, identifier, type.getBasicType()))
945 return false;
946
947 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000948}
949
Jamie Madillb98c3a82015-07-23 14:26:04 -0400950bool TParseContext::paramErrorCheck(const TSourceLoc &line,
951 TQualifier qualifier,
952 TQualifier paramQualifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530953 TType *type)
954{
955 if (qualifier != EvqConst && qualifier != EvqTemporary)
956 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000957 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000958 return true;
959 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530960 if (qualifier == EvqConst && paramQualifier != EvqIn)
961 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400962 error(line, "qualifier not allowed with ", getQualifierString(qualifier),
963 getQualifierString(paramQualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000964 return true;
965 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000966
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000967 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000968 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000969 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000970 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000971
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000972 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000973}
974
Arun Patole7e7e68d2015-05-22 12:02:25 +0530975bool TParseContext::extensionErrorCheck(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000976{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400977 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000978 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +0530979 if (iter == extBehavior.end())
980 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000981 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000982 return true;
983 }
zmo@google.comf5450912011-09-09 01:37:19 +0000984 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +0530985 if (iter->second == EBhDisable || iter->second == EBhUndefined)
986 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000987 error(line, "extension", extension.c_str(), "is disabled");
988 return true;
989 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530990 if (iter->second == EBhWarn)
991 {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000992 warning(line, "extension", extension.c_str(), "is being used");
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000993 return false;
994 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000995
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000996 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000997}
998
Jamie Madillb98c3a82015-07-23 14:26:04 -0400999// These checks are common for all declarations starting a declarator list, and declarators that
1000// follow an empty declaration.
Olli Etuahofa33d582015-04-09 14:33:12 +03001001//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001002bool TParseContext::singleDeclarationErrorCheck(const TPublicType &publicType,
1003 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001004{
Olli Etuahofa33d582015-04-09 14:33:12 +03001005 switch (publicType.qualifier)
1006 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001007 case EvqVaryingIn:
1008 case EvqVaryingOut:
1009 case EvqAttribute:
1010 case EvqVertexIn:
1011 case EvqFragmentOut:
1012 if (publicType.type == EbtStruct)
1013 {
1014 error(identifierLocation, "cannot be used with a structure",
1015 getQualifierString(publicType.qualifier));
1016 return true;
1017 }
Olli Etuahofa33d582015-04-09 14:33:12 +03001018
Jamie Madillb98c3a82015-07-23 14:26:04 -04001019 default:
1020 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001021 }
1022
Jamie Madillb98c3a82015-07-23 14:26:04 -04001023 if (publicType.qualifier != EvqUniform &&
1024 samplerErrorCheck(identifierLocation, publicType, "samplers must be uniform"))
Olli Etuahofa33d582015-04-09 14:33:12 +03001025 {
Jamie Madilla5efff92013-06-06 11:56:47 -04001026 return true;
Olli Etuahofa33d582015-04-09 14:33:12 +03001027 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001028
1029 // check for layout qualifier issues
1030 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
1031
1032 if (layoutQualifier.matrixPacking != EmpUnspecified)
1033 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001034 error(identifierLocation, "layout qualifier",
1035 getMatrixPackingString(layoutQualifier.matrixPacking),
Olli Etuahofa33d582015-04-09 14:33:12 +03001036 "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -04001037 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001038 }
1039
1040 if (layoutQualifier.blockStorage != EbsUnspecified)
1041 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001042 error(identifierLocation, "layout qualifier",
1043 getBlockStorageString(layoutQualifier.blockStorage),
Olli Etuahofa33d582015-04-09 14:33:12 +03001044 "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -04001045 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001046 }
1047
Olli Etuahofa33d582015-04-09 14:33:12 +03001048 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut &&
1049 layoutLocationErrorCheck(identifierLocation, publicType.layoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001050 {
Jamie Madill51a53c72013-06-19 09:24:43 -04001051 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001052 }
1053
1054 return false;
1055}
1056
Jamie Madillb98c3a82015-07-23 14:26:04 -04001057bool TParseContext::layoutLocationErrorCheck(const TSourceLoc &location,
1058 const TLayoutQualifier &layoutQualifier)
Jamie Madilla5efff92013-06-06 11:56:47 -04001059{
1060 if (layoutQualifier.location != -1)
1061 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001062 error(location, "invalid layout qualifier:", "location",
1063 "only valid on program inputs and outputs");
Jamie Madilla5efff92013-06-06 11:56:47 -04001064 return true;
1065 }
1066
1067 return false;
1068}
1069
Jamie Madillb98c3a82015-07-23 14:26:04 -04001070bool TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate,
1071 TIntermAggregate *aggregate)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001072{
1073 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1074 {
1075 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1076 if (qual == EvqOut || qual == EvqInOut)
1077 {
1078 TIntermTyped *node = (*(aggregate->getSequence()))[i]->getAsTyped();
1079 if (lValueErrorCheck(node->getLine(), "assign", node))
1080 {
1081 error(node->getLine(),
Jamie Madillb98c3a82015-07-23 14:26:04 -04001082 "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error");
Olli Etuahob6e07a62015-02-16 12:22:10 +02001083 recover();
1084 return true;
1085 }
1086 }
1087 }
1088 return false;
1089}
1090
Jamie Madillb98c3a82015-07-23 14:26:04 -04001091void TParseContext::es3InvariantErrorCheck(const TQualifier qualifier,
1092 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001093{
1094 if (!sh::IsVaryingOut(qualifier) && qualifier != EvqFragmentOut)
1095 {
1096 error(invariantLocation, "Only out variables can be invariant.", "invariant");
1097 recover();
1098 }
1099}
1100
Arun Patole7e7e68d2015-05-22 12:02:25 +05301101bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001102{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001103 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001104 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1105 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001106}
1107
Arun Patole7e7e68d2015-05-22 12:02:25 +05301108bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001109{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001110 return ::IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001111}
1112
Jamie Madillb98c3a82015-07-23 14:26:04 -04001113void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1114 const char *extName,
1115 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001116{
1117 pp::SourceLocation srcLoc;
1118 srcLoc.file = loc.first_file;
1119 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001120 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001121}
1122
Jamie Madillb98c3a82015-07-23 14:26:04 -04001123void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1124 const char *name,
1125 const char *value,
1126 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001127{
1128 pp::SourceLocation srcLoc;
1129 srcLoc.file = loc.first_file;
1130 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001131 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001132}
1133
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001134/////////////////////////////////////////////////////////////////////////////////
1135//
1136// Non-Errors.
1137//
1138/////////////////////////////////////////////////////////////////////////////////
1139
Jamie Madill5c097022014-08-20 16:38:32 -04001140const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1141 const TString *name,
1142 const TSymbol *symbol)
1143{
1144 const TVariable *variable = NULL;
1145
1146 if (!symbol)
1147 {
1148 error(location, "undeclared identifier", name->c_str());
1149 recover();
1150 }
1151 else if (!symbol->isVariable())
1152 {
1153 error(location, "variable expected", name->c_str());
1154 recover();
1155 }
1156 else
1157 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001158 variable = static_cast<const TVariable *>(symbol);
Jamie Madill5c097022014-08-20 16:38:32 -04001159
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001160 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
Jamie Madill5c097022014-08-20 16:38:32 -04001161 !variable->getExtension().empty() &&
1162 extensionErrorCheck(location, variable->getExtension()))
1163 {
1164 recover();
1165 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001166
1167 // Reject shaders using both gl_FragData and gl_FragColor
1168 TQualifier qualifier = variable->getType().getQualifier();
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001169 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001170 {
1171 mUsesFragData = true;
1172 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001173 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001174 {
1175 mUsesFragColor = true;
1176 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001177 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1178 {
1179 mUsesSecondaryOutputs = true;
1180 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001181
1182 // This validation is not quite correct - it's only an error to write to
1183 // both FragData and FragColor. For simplicity, and because users shouldn't
1184 // be rewarded for reading from undefined varaibles, return an error
1185 // if they are both referenced, rather than assigned.
1186 if (mUsesFragData && mUsesFragColor)
1187 {
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001188 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1189 if (mUsesSecondaryOutputs)
1190 {
1191 errorMessage =
1192 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1193 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1194 }
1195 error(location, errorMessage, name->c_str());
Jamie Madill14e95b32015-05-07 10:10:41 -04001196 recover();
1197 }
Jamie Madill5c097022014-08-20 16:38:32 -04001198 }
1199
1200 if (!variable)
1201 {
1202 TType type(EbtFloat, EbpUndefined);
1203 TVariable *fakeVariable = new TVariable(name, type);
1204 symbolTable.declare(fakeVariable);
1205 variable = fakeVariable;
1206 }
1207
1208 return variable;
1209}
1210
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001211TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1212 const TString *name,
1213 const TSymbol *symbol)
1214{
1215 const TVariable *variable = getNamedVariable(location, name, symbol);
1216
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001217 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001218 {
1219 TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001220 return intermediate.addConstantUnion(constArray, variable->getType(), location);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001221 }
1222 else
1223 {
1224 return intermediate.addSymbol(variable->getUniqueId(), variable->getName(),
1225 variable->getType(), location);
1226 }
1227}
1228
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001229//
1230// Look up a function name in the symbol table, and make sure it is a function.
1231//
1232// Return the function symbol if found, otherwise 0.
1233//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001234const TFunction *TParseContext::findFunction(const TSourceLoc &line,
1235 TFunction *call,
1236 int inputShaderVersion,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301237 bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001238{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001239 // First find by unmangled name to check whether the function name has been
1240 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001241 // If a function is found, check for one with a matching argument list.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301242 const TSymbol *symbol = symbolTable.find(call->getName(), inputShaderVersion, builtIn);
1243 if (symbol == 0 || symbol->isFunction())
1244 {
Austin Kinross3ae64652015-01-26 15:51:39 -08001245 symbol = symbolTable.find(call->getMangledName(), inputShaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001246 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001247
Arun Patole7e7e68d2015-05-22 12:02:25 +05301248 if (symbol == 0)
1249 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001250 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001251 return 0;
1252 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001253
Arun Patole7e7e68d2015-05-22 12:02:25 +05301254 if (!symbol->isFunction())
1255 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001256 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001257 return 0;
1258 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001259
Jamie Madillb98c3a82015-07-23 14:26:04 -04001260 return static_cast<const TFunction *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001261}
1262
1263//
1264// Initializers show up in several places in the grammar. Have one set of
1265// code to handle them here.
1266//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001267// Returns true on error, false if no error
1268//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001269bool TParseContext::executeInitializer(const TSourceLoc &line,
1270 const TString &identifier,
1271 const TPublicType &pType,
1272 TIntermTyped *initializer,
1273 TIntermNode **intermNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001274{
Olli Etuahoe7847b02015-03-16 11:56:12 +02001275 ASSERT(intermNode != nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001276 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001277
Olli Etuaho2935c582015-04-08 14:32:06 +03001278 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001279 if (type.isUnsizedArray())
1280 {
1281 type.setArraySize(initializer->getArraySize());
1282 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001283 if (!declareVariable(line, identifier, type, &variable))
1284 {
1285 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001286 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001287
Olli Etuahob0c645e2015-05-12 14:25:36 +03001288 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001289 if (symbolTable.atGlobalLevel() &&
1290 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001291 {
1292 // Error message does not completely match behavior with ESSL 1.00, but
1293 // we want to steer developers towards only using constant expressions.
1294 error(line, "global variable initializers must be constant expressions", "=");
1295 return true;
1296 }
1297 if (globalInitWarning)
1298 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001299 warning(
1300 line,
1301 "global variable initializers should be constant expressions "
1302 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1303 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001304 }
1305
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001306 //
1307 // identifier must be of type constant, a global, or a temporary
1308 //
1309 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301310 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1311 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001312 error(line, " cannot initialize this type of qualifier ",
1313 variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001314 return true;
1315 }
1316 //
1317 // test for and propagate constant
1318 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001319
Arun Patole7e7e68d2015-05-22 12:02:25 +05301320 if (qualifier == EvqConst)
1321 {
1322 if (qualifier != initializer->getType().getQualifier())
1323 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001324 std::stringstream extraInfoStream;
1325 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1326 std::string extraInfo = extraInfoStream.str();
1327 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001328 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001329 return true;
1330 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301331 if (type != initializer->getType())
1332 {
1333 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001334 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001335 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001336 return true;
1337 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001338
1339 // Save the constant folded value to the variable if possible. For example array
1340 // initializers are not folded, since that way copying the array literal to multiple places
1341 // in the shader is avoided.
1342 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1343 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301344 if (initializer->getAsConstantUnion())
1345 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001346 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001347 *intermNode = nullptr;
1348 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301349 }
1350 else if (initializer->getAsSymbolNode())
1351 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001352 const TSymbol *symbol =
1353 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1354 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001355
Arun Patole7e7e68d2015-05-22 12:02:25 +05301356 TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001357 if (constArray)
1358 {
1359 variable->shareConstPointer(constArray);
1360 *intermNode = nullptr;
1361 return false;
1362 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001363 }
1364 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001365
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001366 TIntermSymbol *intermSymbol = intermediate.addSymbol(
1367 variable->getUniqueId(), variable->getName(), variable->getType(), line);
1368 *intermNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1369 if (*intermNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001370 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001371 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1372 return true;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001373 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001374
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001375 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001376}
1377
Jamie Madillb98c3a82015-07-23 14:26:04 -04001378TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier,
1379 bool invariant,
1380 TLayoutQualifier layoutQualifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301381 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001382{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001383 TPublicType returnType = typeSpecifier;
1384 returnType.qualifier = qualifier;
1385 returnType.invariant = invariant;
Jamie Madilla5efff92013-06-06 11:56:47 -04001386 returnType.layoutQualifier = layoutQualifier;
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001387
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001388 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001389 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001390 if (typeSpecifier.array)
1391 {
1392 error(typeSpecifier.line, "not supported", "first-class array");
1393 recover();
1394 returnType.clearArrayness();
1395 }
1396
Jamie Madillb98c3a82015-07-23 14:26:04 -04001397 if (qualifier == EvqAttribute &&
1398 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001399 {
1400 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1401 recover();
1402 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001403
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001404 if ((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1405 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1406 {
1407 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1408 recover();
1409 }
1410 }
1411 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001412 {
Olli Etuahoabb0c382015-07-13 12:01:12 +03001413 if (!layoutQualifier.isEmpty())
1414 {
1415 if (globalErrorCheck(typeSpecifier.line, symbolTable.atGlobalLevel(), "layout"))
1416 {
1417 recover();
1418 }
1419 }
Olli Etuahocc36b982015-07-10 14:14:18 +03001420 if (sh::IsVarying(qualifier) || qualifier == EvqVertexIn || qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001421 {
Olli Etuahocc36b982015-07-10 14:14:18 +03001422 es3InputOutputTypeCheck(qualifier, typeSpecifier, typeSpecifier.line);
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001423 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001424 }
1425
1426 return returnType;
1427}
1428
Olli Etuahocc36b982015-07-10 14:14:18 +03001429void TParseContext::es3InputOutputTypeCheck(const TQualifier qualifier,
1430 const TPublicType &type,
1431 const TSourceLoc &qualifierLocation)
1432{
1433 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
1434 if (type.type == EbtBool)
1435 {
1436 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
1437 recover();
1438 }
1439
1440 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
1441 switch (qualifier)
1442 {
1443 case EvqVertexIn:
1444 // ESSL 3.00 section 4.3.4
1445 if (type.array)
1446 {
1447 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
1448 recover();
1449 }
1450 // Vertex inputs with a struct type are disallowed in singleDeclarationErrorCheck
1451 return;
1452 case EvqFragmentOut:
1453 // ESSL 3.00 section 4.3.6
1454 if (type.isMatrix())
1455 {
1456 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
1457 recover();
1458 }
1459 // Fragment outputs with a struct type are disallowed in singleDeclarationErrorCheck
1460 return;
1461 default:
1462 break;
1463 }
1464
1465 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
1466 // restrictions.
1467 bool typeContainsIntegers =
1468 (type.type == EbtInt || type.type == EbtUInt || type.isStructureContainingType(EbtInt) ||
1469 type.isStructureContainingType(EbtUInt));
1470 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
1471 {
1472 error(qualifierLocation, "must use 'flat' interpolation here",
1473 getQualifierString(qualifier));
1474 recover();
1475 }
1476
1477 if (type.type == EbtStruct)
1478 {
1479 // ESSL 3.00 sections 4.3.4 and 4.3.6.
1480 // These restrictions are only implied by the ESSL 3.00 spec, but
1481 // the ESSL 3.10 spec lists these restrictions explicitly.
1482 if (type.array)
1483 {
1484 error(qualifierLocation, "cannot be an array of structures",
1485 getQualifierString(qualifier));
1486 recover();
1487 }
1488 if (type.isStructureContainingArrays())
1489 {
1490 error(qualifierLocation, "cannot be a structure containing an array",
1491 getQualifierString(qualifier));
1492 recover();
1493 }
1494 if (type.isStructureContainingType(EbtStruct))
1495 {
1496 error(qualifierLocation, "cannot be a structure containing a structure",
1497 getQualifierString(qualifier));
1498 recover();
1499 }
1500 if (type.isStructureContainingType(EbtBool))
1501 {
1502 error(qualifierLocation, "cannot be a structure containing a bool",
1503 getQualifierString(qualifier));
1504 recover();
1505 }
1506 }
1507}
1508
Olli Etuahofa33d582015-04-09 14:33:12 +03001509TIntermAggregate *TParseContext::parseSingleDeclaration(TPublicType &publicType,
1510 const TSourceLoc &identifierOrTypeLocation,
1511 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001512{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001513 TIntermSymbol *symbol =
1514 intermediate.addSymbol(0, identifier, TType(publicType), identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001515
Olli Etuahobab4c082015-04-24 16:38:49 +03001516 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03001517
Olli Etuahobab4c082015-04-24 16:38:49 +03001518 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1519
1520 if (emptyDeclaration)
1521 {
1522 if (publicType.isUnsizedArray())
1523 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001524 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1525 // error. It is assumed that this applies to empty declarations as well.
1526 error(identifierOrTypeLocation, "empty array declaration needs to specify a size",
1527 identifier.c_str());
Olli Etuahobab4c082015-04-24 16:38:49 +03001528 }
1529 }
1530 else
Jamie Madill60ed9812013-06-06 11:56:46 -04001531 {
Olli Etuahofa33d582015-04-09 14:33:12 +03001532 if (singleDeclarationErrorCheck(publicType, identifierOrTypeLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001533 recover();
1534
Olli Etuaho376f1b52015-04-13 13:23:41 +03001535 if (nonInitErrorCheck(identifierOrTypeLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001536 recover();
1537
Olli Etuaho2935c582015-04-08 14:32:06 +03001538 TVariable *variable = nullptr;
Olli Etuahofa33d582015-04-09 14:33:12 +03001539 if (!declareVariable(identifierOrTypeLocation, identifier, TType(publicType), &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001540 recover();
1541
1542 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001543 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001544 }
1545
Olli Etuahoe7847b02015-03-16 11:56:12 +02001546 return intermediate.makeAggregate(symbol, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001547}
1548
Olli Etuahoe7847b02015-03-16 11:56:12 +02001549TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1550 const TSourceLoc &identifierLocation,
1551 const TString &identifier,
1552 const TSourceLoc &indexLocation,
1553 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001554{
Olli Etuahofa33d582015-04-09 14:33:12 +03001555 mDeferredSingleDeclarationErrorCheck = false;
1556
1557 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001558 recover();
1559
Olli Etuaho376f1b52015-04-13 13:23:41 +03001560 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001561 recover();
1562
Jamie Madillb98c3a82015-07-23 14:26:04 -04001563 if (arrayTypeErrorCheck(indexLocation, publicType) ||
1564 arrayQualifierErrorCheck(indexLocation, publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001565 {
1566 recover();
1567 }
1568
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001569 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001570
1571 int size;
1572 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1573 {
1574 recover();
1575 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001576 // Make the type an array even if size check failed.
1577 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1578 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001579
Olli Etuaho2935c582015-04-08 14:32:06 +03001580 TVariable *variable = nullptr;
1581 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001582 recover();
1583
Olli Etuahoe7847b02015-03-16 11:56:12 +02001584 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001585 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001586 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001587
Olli Etuahoe7847b02015-03-16 11:56:12 +02001588 return intermediate.makeAggregate(symbol, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001589}
1590
Jamie Madill06145232015-05-13 13:10:01 -04001591TIntermAggregate *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001592 const TSourceLoc &identifierLocation,
1593 const TString &identifier,
1594 const TSourceLoc &initLocation,
1595 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001596{
Olli Etuahofa33d582015-04-09 14:33:12 +03001597 mDeferredSingleDeclarationErrorCheck = false;
1598
1599 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001600 recover();
1601
Olli Etuahoe7847b02015-03-16 11:56:12 +02001602 TIntermNode *intermNode = nullptr;
1603 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04001604 {
1605 //
1606 // Build intermediate representation
1607 //
Olli Etuahoe7847b02015-03-16 11:56:12 +02001608 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001609 }
1610 else
1611 {
1612 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001613 return nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001614 }
1615}
1616
Jamie Madillb98c3a82015-07-23 14:26:04 -04001617TIntermAggregate *TParseContext::parseSingleArrayInitDeclaration(
1618 TPublicType &publicType,
1619 const TSourceLoc &identifierLocation,
1620 const TString &identifier,
1621 const TSourceLoc &indexLocation,
1622 TIntermTyped *indexExpression,
1623 const TSourceLoc &initLocation,
1624 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001625{
1626 mDeferredSingleDeclarationErrorCheck = false;
1627
1628 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1629 recover();
1630
Jamie Madillb98c3a82015-07-23 14:26:04 -04001631 if (arrayTypeErrorCheck(indexLocation, publicType) ||
1632 arrayQualifierErrorCheck(indexLocation, publicType))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001633 {
1634 recover();
1635 }
1636
1637 TPublicType arrayType(publicType);
1638
Olli Etuaho376f1b52015-04-13 13:23:41 +03001639 int size = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001640 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
1641 // the initializer.
1642 if (indexExpression != nullptr &&
1643 arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001644 {
1645 recover();
1646 }
1647 // Make the type an array even if size check failed.
1648 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1649 arrayType.setArraySize(size);
1650
1651 // initNode will correspond to the whole of "type b[n] = initializer".
1652 TIntermNode *initNode = nullptr;
1653 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1654 {
1655 return initNode ? intermediate.makeAggregate(initNode, initLocation) : nullptr;
1656 }
1657 else
1658 {
1659 recover();
1660 return nullptr;
1661 }
1662}
1663
Olli Etuahoe7847b02015-03-16 11:56:12 +02001664TIntermAggregate *TParseContext::parseInvariantDeclaration(const TSourceLoc &invariantLoc,
Jamie Madill47e3ec02014-08-20 16:38:33 -04001665 const TSourceLoc &identifierLoc,
1666 const TString *identifier,
1667 const TSymbol *symbol)
1668{
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001669 // invariant declaration
Jamie Madill47e3ec02014-08-20 16:38:33 -04001670 if (globalErrorCheck(invariantLoc, symbolTable.atGlobalLevel(), "invariant varying"))
1671 {
1672 recover();
1673 }
1674
1675 if (!symbol)
1676 {
1677 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
1678 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001679 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001680 }
1681 else
1682 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001683 const TString kGlFrontFacing("gl_FrontFacing");
1684 if (*identifier == kGlFrontFacing)
1685 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001686 error(identifierLoc, "identifier should not be declared as invariant",
1687 identifier->c_str());
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001688 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001689 return nullptr;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001690 }
Jamie Madill2c433252014-12-03 12:36:54 -05001691 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001692 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1693 ASSERT(variable);
1694 const TType &type = variable->getType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04001695 TIntermSymbol *intermSymbol =
1696 intermediate.addSymbol(variable->getUniqueId(), *identifier, type, identifierLoc);
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001697
1698 TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc);
1699 aggregate->setOp(EOpInvariantDeclaration);
1700 return aggregate;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001701 }
1702}
1703
Jamie Madillb98c3a82015-07-23 14:26:04 -04001704TIntermAggregate *TParseContext::parseDeclarator(TPublicType &publicType,
1705 TIntermAggregate *aggregateDeclaration,
1706 const TSourceLoc &identifierLocation,
1707 const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001708{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001709 // If the declaration starting this declarator list was empty (example: int,), some checks were
1710 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001711 if (mDeferredSingleDeclarationErrorCheck)
1712 {
1713 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1714 recover();
1715 mDeferredSingleDeclarationErrorCheck = false;
1716 }
1717
Jamie Madill0bd18df2013-06-20 11:55:52 -04001718 if (locationDeclaratorListCheck(identifierLocation, publicType))
1719 recover();
1720
Olli Etuaho376f1b52015-04-13 13:23:41 +03001721 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001722 recover();
1723
Olli Etuaho2935c582015-04-08 14:32:06 +03001724 TVariable *variable = nullptr;
1725 if (!declareVariable(identifierLocation, identifier, TType(publicType), &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001726 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001727
Jamie Madillb98c3a82015-07-23 14:26:04 -04001728 TIntermSymbol *symbol =
1729 intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001730 if (variable && symbol)
Jamie Madill502d66f2013-06-20 11:55:52 -04001731 symbol->setId(variable->getUniqueId());
1732
Olli Etuahoe7847b02015-03-16 11:56:12 +02001733 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001734}
1735
Jamie Madillb98c3a82015-07-23 14:26:04 -04001736TIntermAggregate *TParseContext::parseArrayDeclarator(TPublicType &publicType,
1737 TIntermAggregate *aggregateDeclaration,
1738 const TSourceLoc &identifierLocation,
1739 const TString &identifier,
1740 const TSourceLoc &arrayLocation,
1741 TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001742{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001743 // If the declaration starting this declarator list was empty (example: int,), some checks were
1744 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001745 if (mDeferredSingleDeclarationErrorCheck)
1746 {
1747 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1748 recover();
1749 mDeferredSingleDeclarationErrorCheck = false;
1750 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001751
Jamie Madill0bd18df2013-06-20 11:55:52 -04001752 if (locationDeclaratorListCheck(identifierLocation, publicType))
1753 recover();
1754
Olli Etuaho376f1b52015-04-13 13:23:41 +03001755 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001756 recover();
1757
Jamie Madillb98c3a82015-07-23 14:26:04 -04001758 if (arrayTypeErrorCheck(arrayLocation, publicType) ||
1759 arrayQualifierErrorCheck(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001760 {
1761 recover();
1762 }
Olli Etuaho93a90fd2015-04-07 18:14:07 +03001763 else
Jamie Madill502d66f2013-06-20 11:55:52 -04001764 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001765 TType arrayType = TType(publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04001766 int size;
1767 if (arraySizeErrorCheck(arrayLocation, indexExpression, size))
Olli Etuahoe7847b02015-03-16 11:56:12 +02001768 {
Jamie Madill502d66f2013-06-20 11:55:52 -04001769 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001770 }
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001771 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001772
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001773 TVariable *variable = nullptr;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001774 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001775 recover();
Jamie Madill502d66f2013-06-20 11:55:52 -04001776
Jamie Madillb98c3a82015-07-23 14:26:04 -04001777 TIntermSymbol *symbol =
1778 intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001779 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001780 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001781
1782 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001783 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001784
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001785 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001786}
1787
Jamie Madillb98c3a82015-07-23 14:26:04 -04001788TIntermAggregate *TParseContext::parseInitDeclarator(const TPublicType &publicType,
1789 TIntermAggregate *aggregateDeclaration,
1790 const TSourceLoc &identifierLocation,
1791 const TString &identifier,
1792 const TSourceLoc &initLocation,
1793 TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001794{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001795 // If the declaration starting this declarator list was empty (example: int,), some checks were
1796 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001797 if (mDeferredSingleDeclarationErrorCheck)
1798 {
1799 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1800 recover();
1801 mDeferredSingleDeclarationErrorCheck = false;
1802 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001803
Jamie Madill0bd18df2013-06-20 11:55:52 -04001804 if (locationDeclaratorListCheck(identifierLocation, publicType))
1805 recover();
1806
Olli Etuahoe7847b02015-03-16 11:56:12 +02001807 TIntermNode *intermNode = nullptr;
1808 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04001809 {
1810 //
1811 // build the intermediate representation
1812 //
1813 if (intermNode)
1814 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001815 return intermediate.growAggregate(aggregateDeclaration, intermNode, initLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001816 }
1817 else
1818 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001819 return aggregateDeclaration;
Jamie Madill502d66f2013-06-20 11:55:52 -04001820 }
1821 }
1822 else
1823 {
1824 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001825 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001826 }
1827}
1828
Jamie Madill06145232015-05-13 13:10:01 -04001829TIntermAggregate *TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001830 TIntermAggregate *aggregateDeclaration,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301831 const TSourceLoc &identifierLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001832 const TString &identifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301833 const TSourceLoc &indexLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001834 TIntermTyped *indexExpression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001835 const TSourceLoc &initLocation,
1836 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001837{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001838 // If the declaration starting this declarator list was empty (example: int,), some checks were
1839 // not performed.
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001840 if (mDeferredSingleDeclarationErrorCheck)
1841 {
1842 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1843 recover();
1844 mDeferredSingleDeclarationErrorCheck = false;
1845 }
1846
1847 if (locationDeclaratorListCheck(identifierLocation, publicType))
1848 recover();
1849
Jamie Madillb98c3a82015-07-23 14:26:04 -04001850 if (arrayTypeErrorCheck(indexLocation, publicType) ||
1851 arrayQualifierErrorCheck(indexLocation, publicType))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001852 {
1853 recover();
1854 }
1855
1856 TPublicType arrayType(publicType);
1857
Olli Etuaho376f1b52015-04-13 13:23:41 +03001858 int size = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001859 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
1860 // the initializer.
1861 if (indexExpression != nullptr &&
1862 arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001863 {
1864 recover();
1865 }
1866 // Make the type an array even if size check failed.
1867 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1868 arrayType.setArraySize(size);
1869
1870 // initNode will correspond to the whole of "b[n] = initializer".
1871 TIntermNode *initNode = nullptr;
1872 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1873 {
1874 if (initNode)
1875 {
1876 return intermediate.growAggregate(aggregateDeclaration, initNode, initLocation);
1877 }
1878 else
1879 {
1880 return aggregateDeclaration;
1881 }
1882 }
1883 else
1884 {
1885 recover();
1886 return nullptr;
1887 }
1888}
1889
Jamie Madilla295edf2013-06-06 11:56:48 -04001890void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1891{
1892 if (typeQualifier.qualifier != EvqUniform)
1893 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001894 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
1895 "global layout must be uniform");
Jamie Madilla295edf2013-06-06 11:56:48 -04001896 recover();
1897 return;
1898 }
1899
1900 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1901 ASSERT(!layoutQualifier.isEmpty());
1902
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001903 if (mShaderVersion < 300)
Jamie Madilla295edf2013-06-06 11:56:48 -04001904 {
1905 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1906 recover();
1907 return;
1908 }
1909
1910 if (layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1911 {
1912 recover();
1913 return;
1914 }
1915
Jamie Madill099c0f32013-06-20 11:55:52 -04001916 if (layoutQualifier.matrixPacking != EmpUnspecified)
1917 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001918 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04001919 }
1920
Geoff Langc6856732014-02-11 09:38:55 -05001921 if (layoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04001922 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001923 mDefaultBlockStorage = layoutQualifier.blockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04001924 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001925}
1926
Jamie Madill185fb402015-06-12 15:48:48 -04001927void TParseContext::parseFunctionPrototype(const TSourceLoc &location,
1928 TFunction *function,
1929 TIntermAggregate **aggregateOut)
1930{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001931 const TSymbol *builtIn =
1932 symbolTable.findBuiltIn(function->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04001933
1934 if (builtIn)
1935 {
1936 error(location, "built-in functions cannot be redefined", function->getName().c_str());
1937 recover();
1938 }
1939
Jamie Madillb98c3a82015-07-23 14:26:04 -04001940 TFunction *prevDec =
1941 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Jamie Madill185fb402015-06-12 15:48:48 -04001942 //
1943 // Note: 'prevDec' could be 'function' if this is the first time we've seen function
1944 // as it would have just been put in the symbol table. Otherwise, we're looking up
1945 // an earlier occurance.
1946 //
1947 if (prevDec->isDefined())
1948 {
1949 // Then this function already has a body.
1950 error(location, "function already has a body", function->getName().c_str());
1951 recover();
1952 }
1953 prevDec->setDefined();
1954 //
1955 // Overload the unique ID of the definition to be the same unique ID as the declaration.
1956 // Eventually we will probably want to have only a single definition and just swap the
1957 // arguments to be the definition's arguments.
1958 //
1959 function->setUniqueId(prevDec->getUniqueId());
1960
1961 // Raise error message if main function takes any parameters or return anything other than void
1962 if (function->getName() == "main")
1963 {
1964 if (function->getParamCount() > 0)
1965 {
1966 error(location, "function cannot take any parameter(s)", function->getName().c_str());
1967 recover();
1968 }
1969 if (function->getReturnType().getBasicType() != EbtVoid)
1970 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001971 error(location, "", function->getReturnType().getBasicString(),
1972 "main function cannot return a value");
Jamie Madill185fb402015-06-12 15:48:48 -04001973 recover();
1974 }
1975 }
1976
1977 //
1978 // Remember the return type for later checking for RETURN statements.
1979 //
1980 setCurrentFunctionType(&(prevDec->getReturnType()));
1981 setFunctionReturnsValue(false);
1982
1983 //
1984 // Insert parameters into the symbol table.
1985 // If the parameter has no name, it's not an error, just don't insert it
1986 // (could be used for unused args).
1987 //
1988 // Also, accumulate the list of parameters into the HIL, so lower level code
1989 // knows where to find parameters.
1990 //
1991 TIntermAggregate *paramNodes = new TIntermAggregate;
1992 for (size_t i = 0; i < function->getParamCount(); i++)
1993 {
1994 const TConstParameter &param = function->getParam(i);
1995 if (param.name != 0)
1996 {
1997 TVariable *variable = new TVariable(param.name, *param.type);
1998 //
1999 // Insert the parameters with name in the symbol table.
2000 //
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002001 if (!symbolTable.declare(variable))
2002 {
Jamie Madill185fb402015-06-12 15:48:48 -04002003 error(location, "redefinition", variable->getName().c_str());
2004 recover();
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002005 paramNodes = intermediate.growAggregate(
2006 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
2007 continue;
Jamie Madill185fb402015-06-12 15:48:48 -04002008 }
2009
2010 //
2011 // Add the parameter to the HIL
2012 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002013 TIntermSymbol *symbol = intermediate.addSymbol(
2014 variable->getUniqueId(), variable->getName(), variable->getType(), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002015
2016 paramNodes = intermediate.growAggregate(paramNodes, symbol, location);
2017 }
2018 else
2019 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002020 paramNodes = intermediate.growAggregate(
2021 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002022 }
2023 }
2024 intermediate.setAggregateOperator(paramNodes, EOpParameters, location);
2025 *aggregateOut = paramNodes;
2026 setLoopNestingLevel(0);
2027}
2028
Jamie Madillb98c3a82015-07-23 14:26:04 -04002029TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002030{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002031 //
2032 // Multiple declarations of the same function are allowed.
2033 //
2034 // If this is a definition, the definition production code will check for redefinitions
2035 // (we don't know at this point if it's a definition or not).
2036 //
2037 // Redeclarations are allowed. But, return types and parameter qualifiers must match.
2038 //
2039 TFunction *prevDec =
2040 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
2041 if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002042 {
2043 if (prevDec->getReturnType() != function->getReturnType())
2044 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002045 error(location, "overloaded functions must have the same return type",
Jamie Madill185fb402015-06-12 15:48:48 -04002046 function->getReturnType().getBasicString());
2047 recover();
2048 }
2049 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2050 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002051 if (prevDec->getParam(i).type->getQualifier() !=
2052 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002053 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002054 error(location, "overloaded functions must have the same parameter qualifiers",
Jamie Madill185fb402015-06-12 15:48:48 -04002055 function->getParam(i).type->getQualifierString());
2056 recover();
2057 }
2058 }
2059 }
2060
2061 //
2062 // Check for previously declared variables using the same name.
2063 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002064 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002065 if (prevSym)
2066 {
2067 if (!prevSym->isFunction())
2068 {
2069 error(location, "redefinition", function->getName().c_str(), "function");
2070 recover();
2071 }
2072 }
2073 else
2074 {
2075 // Insert the unmangled name to detect potential future redefinition as a variable.
Jamie Madillb98c3a82015-07-23 14:26:04 -04002076 TFunction *newFunction =
2077 new TFunction(NewPoolTString(function->getName().c_str()), &function->getReturnType());
Jamie Madill185fb402015-06-12 15:48:48 -04002078 symbolTable.getOuterLevel()->insertUnmangled(newFunction);
2079 }
2080
2081 // We're at the inner scope level of the function's arguments and body statement.
2082 // Add the function prototype to the surrounding scope instead.
2083 symbolTable.getOuterLevel()->insert(function);
2084
2085 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002086 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2087 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002088 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2089 //
2090 return function;
2091}
2092
Jamie Madill06145232015-05-13 13:10:01 -04002093TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002094{
Jamie Madill06145232015-05-13 13:10:01 -04002095 TPublicType publicType = publicTypeIn;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002096 TOperator op = EOpNull;
2097 if (publicType.userDef)
2098 {
2099 op = EOpConstructStruct;
2100 }
2101 else
2102 {
2103 switch (publicType.type)
2104 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002105 case EbtFloat:
2106 if (publicType.isMatrix())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002107 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002108 switch (publicType.getCols())
Alexis Hetu07e57df2015-06-16 16:55:52 -04002109 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002110 case 2:
2111 switch (publicType.getRows())
2112 {
2113 case 2:
2114 op = EOpConstructMat2;
2115 break;
2116 case 3:
2117 op = EOpConstructMat2x3;
2118 break;
2119 case 4:
2120 op = EOpConstructMat2x4;
2121 break;
2122 }
2123 break;
2124 case 3:
2125 switch (publicType.getRows())
2126 {
2127 case 2:
2128 op = EOpConstructMat3x2;
2129 break;
2130 case 3:
2131 op = EOpConstructMat3;
2132 break;
2133 case 4:
2134 op = EOpConstructMat3x4;
2135 break;
2136 }
2137 break;
2138 case 4:
2139 switch (publicType.getRows())
2140 {
2141 case 2:
2142 op = EOpConstructMat4x2;
2143 break;
2144 case 3:
2145 op = EOpConstructMat4x3;
2146 break;
2147 case 4:
2148 op = EOpConstructMat4;
2149 break;
2150 }
2151 break;
Alexis Hetu07e57df2015-06-16 16:55:52 -04002152 }
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002153 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04002154 else
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002155 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002156 switch (publicType.getNominalSize())
2157 {
2158 case 1:
2159 op = EOpConstructFloat;
2160 break;
2161 case 2:
2162 op = EOpConstructVec2;
2163 break;
2164 case 3:
2165 op = EOpConstructVec3;
2166 break;
2167 case 4:
2168 op = EOpConstructVec4;
2169 break;
2170 }
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002171 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04002172 break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002173
Jamie Madillb98c3a82015-07-23 14:26:04 -04002174 case EbtInt:
2175 switch (publicType.getNominalSize())
2176 {
2177 case 1:
2178 op = EOpConstructInt;
2179 break;
2180 case 2:
2181 op = EOpConstructIVec2;
2182 break;
2183 case 3:
2184 op = EOpConstructIVec3;
2185 break;
2186 case 4:
2187 op = EOpConstructIVec4;
2188 break;
2189 }
2190 break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002191
Jamie Madillb98c3a82015-07-23 14:26:04 -04002192 case EbtUInt:
2193 switch (publicType.getNominalSize())
2194 {
2195 case 1:
2196 op = EOpConstructUInt;
2197 break;
2198 case 2:
2199 op = EOpConstructUVec2;
2200 break;
2201 case 3:
2202 op = EOpConstructUVec3;
2203 break;
2204 case 4:
2205 op = EOpConstructUVec4;
2206 break;
2207 }
2208 break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002209
Jamie Madillb98c3a82015-07-23 14:26:04 -04002210 case EbtBool:
2211 switch (publicType.getNominalSize())
2212 {
2213 case 1:
2214 op = EOpConstructBool;
2215 break;
2216 case 2:
2217 op = EOpConstructBVec2;
2218 break;
2219 case 3:
2220 op = EOpConstructBVec3;
2221 break;
2222 case 4:
2223 op = EOpConstructBVec4;
2224 break;
2225 }
2226 break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002227
Jamie Madillb98c3a82015-07-23 14:26:04 -04002228 default:
2229 break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002230 }
2231
2232 if (op == EOpNull)
2233 {
2234 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
2235 recover();
2236 publicType.type = EbtFloat;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002237 op = EOpConstructFloat;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002238 }
2239 }
2240
2241 TString tempString;
Dmitry Skiba7f17a502015-06-22 15:08:39 -07002242 const TType *type = new TType(publicType);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002243 return new TFunction(&tempString, type, op);
2244}
2245
Jamie Madillb98c3a82015-07-23 14:26:04 -04002246// This function is used to test for the correctness of the parameters passed to various constructor
2247// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002248//
2249// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
2250//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002251TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments,
2252 TType *type,
2253 TOperator op,
2254 TFunction *fnCall,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302255 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002256{
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002257 TIntermAggregate *constructor = arguments->getAsAggregate();
2258 ASSERT(constructor != nullptr);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002259
Olli Etuahof40319e2015-03-10 14:33:00 +02002260 if (type->isArray())
2261 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002262 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
2263 // the array.
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002264 TIntermSequence *args = constructor->getSequence();
Olli Etuahof40319e2015-03-10 14:33:00 +02002265 for (size_t i = 0; i < args->size(); i++)
2266 {
2267 const TType &argType = (*args)[i]->getAsTyped()->getType();
2268 // It has already been checked that the argument is not an array.
2269 ASSERT(!argType.isArray());
2270 if (!argType.sameElementType(*type))
2271 {
2272 error(line, "Array constructor argument has an incorrect type", "Error");
2273 recover();
2274 return nullptr;
2275 }
2276 }
2277 }
2278 else if (op == EOpConstructStruct)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002279 {
2280 const TFieldList &fields = type->getStruct()->fields();
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002281 TIntermSequence *args = constructor->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002282
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002283 for (size_t i = 0; i < fields.size(); i++)
2284 {
Nicolas Capensffd73872014-08-21 13:49:16 -04002285 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002286 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002287 error(line, "Structure constructor arguments do not match structure fields",
2288 "Error");
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002289 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002290
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002291 return 0;
2292 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002293 }
2294 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002295
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002296 // Turn the argument list itself into a constructor
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002297 constructor->setOp(op);
2298 constructor->setLine(line);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002299 ASSERT(constructor->isConstructor());
2300
2301 // Need to set type before setPrecisionFromChildren() because bool doesn't have precision.
2302 constructor->setType(*type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002303
Olli Etuaho21203702014-11-13 16:16:21 +02002304 // Structs should not be precision qualified, the individual members may be.
2305 // Built-in types on the other hand should be precision qualified.
2306 if (op != EOpConstructStruct)
2307 {
2308 constructor->setPrecisionFromChildren();
2309 type->setPrecision(constructor->getPrecision());
2310 }
2311
Olli Etuaho1d122782015-11-06 15:35:17 +02002312 TIntermTyped *constConstructor = intermediate.foldAggregateBuiltIn(constructor);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002313 if (constConstructor)
2314 {
2315 return constConstructor;
2316 }
2317
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002318 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002319}
2320
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002321//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002322// This function returns the tree representation for the vector field(s) being accessed from contant
2323// vector.
2324// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a
2325// contant node is returned, else an aggregate node is returned (for v.xy). The input to this
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002326// function could either be the symbol node or it could be the intermediate tree representation of
2327// accessing fields in a constant structure or column of a constant matrix.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002328//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002329TIntermTyped *TParseContext::addConstVectorNode(TVectorFields &fields,
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002330 TIntermConstantUnion *node,
2331 const TSourceLoc &line,
2332 bool outOfRangeIndexIsError)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002333{
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002334 const TConstantUnion *unionArray = node->getUnionArrayPointer();
2335 ASSERT(unionArray);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002336
Arun Patole7e7e68d2015-05-22 12:02:25 +05302337 TConstantUnion *constArray = new TConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002338
Arun Patole7e7e68d2015-05-22 12:02:25 +05302339 for (int i = 0; i < fields.num; i++)
2340 {
2341 if (fields.offsets[i] >= node->getType().getNominalSize())
2342 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002343 std::stringstream extraInfoStream;
2344 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
2345 std::string extraInfo = extraInfoStream.str();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002346 outOfRangeError(outOfRangeIndexIsError, line, "", "[", extraInfo.c_str());
2347 fields.offsets[i] = node->getType().getNominalSize() - 1;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002348 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302349
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002350 constArray[i] = unionArray[fields.offsets[i]];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302351 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002352 return intermediate.addConstantUnion(constArray, node->getType(), line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002353}
2354
2355//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002356// This function returns the column being accessed from a constant matrix. The values are retrieved
2357// 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 +02002358// The input to the function could either be a symbol node (m[0] where m is a constant matrix)that
2359// represents a constant matrix or it could be the tree representation of the constant matrix
2360// (s.m1[0] where s is a constant structure)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002361//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002362TIntermTyped *TParseContext::addConstMatrixNode(int index,
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002363 TIntermConstantUnion *node,
2364 const TSourceLoc &line,
2365 bool outOfRangeIndexIsError)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002366{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302367 if (index >= node->getType().getCols())
2368 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002369 std::stringstream extraInfoStream;
2370 extraInfoStream << "matrix field selection out of range '" << index << "'";
2371 std::string extraInfo = extraInfoStream.str();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002372 outOfRangeError(outOfRangeIndexIsError, line, "", "[", extraInfo.c_str());
2373 index = node->getType().getCols() - 1;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002374 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002375
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002376 TConstantUnion *unionArray = node->getUnionArrayPointer();
2377 int size = node->getType().getCols();
2378 return intermediate.addConstantUnion(&unionArray[size * index], node->getType(), line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002379}
2380
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002381//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002382// This function returns an element of an array accessed from a constant array. The values are
2383// 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 +05302384// 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 -04002385// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a
2386// constant structure)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002387//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002388TIntermTyped *TParseContext::addConstArrayNode(int index,
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002389 TIntermConstantUnion *node,
2390 const TSourceLoc &line,
2391 bool outOfRangeIndexIsError)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002392{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002393 TType arrayElementType = node->getType();
2394 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002395
Arun Patole7e7e68d2015-05-22 12:02:25 +05302396 if (index >= node->getType().getArraySize())
2397 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002398 std::stringstream extraInfoStream;
2399 extraInfoStream << "array field selection out of range '" << index << "'";
2400 std::string extraInfo = extraInfoStream.str();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002401 outOfRangeError(outOfRangeIndexIsError, line, "", "[", extraInfo.c_str());
2402 index = node->getType().getArraySize() - 1;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002403 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002404 size_t arrayElementSize = arrayElementType.getObjectSize();
2405 TConstantUnion *unionArray = node->getUnionArrayPointer();
2406 return intermediate.addConstantUnion(&unionArray[arrayElementSize * index], node->getType(),
2407 line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002408}
2409
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002410//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002411// This function returns the value of a particular field inside a constant structure from the symbol
2412// table.
2413// If there is an embedded/nested struct, it appropriately calls addConstStructNested or
2414// addConstStructFromAggr function and returns the parse-tree with the values of the embedded/nested
2415// struct.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002416//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002417TIntermTyped *TParseContext::addConstStruct(const TString &identifier,
2418 TIntermTyped *node,
2419 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002420{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302421 const TFieldList &fields = node->getType().getStruct()->fields();
Jamie Madillb98c3a82015-07-23 14:26:04 -04002422 size_t instanceSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002423
Arun Patole7e7e68d2015-05-22 12:02:25 +05302424 for (size_t index = 0; index < fields.size(); ++index)
2425 {
2426 if (fields[index]->name() == identifier)
2427 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002428 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302429 }
2430 else
2431 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002432 instanceSize += fields[index]->type()->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002433 }
2434 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002435
Jamie Madill94bf7f22013-07-08 13:31:15 -04002436 TIntermTyped *typedNode;
2437 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
Arun Patole7e7e68d2015-05-22 12:02:25 +05302438 if (tempConstantNode)
2439 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002440 TConstantUnion *constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002441
Jamie Madillb98c3a82015-07-23 14:26:04 -04002442 // type will be changed in the calling function
2443 typedNode = intermediate.addConstantUnion(constArray + instanceSize,
2444 tempConstantNode->getType(), line);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302445 }
2446 else
2447 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002448 error(line, "Cannot offset into the structure", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002449 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002450
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002451 return 0;
2452 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002453
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002454 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002455}
2456
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002457//
2458// Interface/uniform blocks
2459//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002460TIntermAggregate *TParseContext::addInterfaceBlock(const TPublicType &typeQualifier,
2461 const TSourceLoc &nameLine,
2462 const TString &blockName,
2463 TFieldList *fieldList,
2464 const TString *instanceName,
2465 const TSourceLoc &instanceLine,
2466 TIntermTyped *arrayIndex,
2467 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002468{
2469 if (reservedErrorCheck(nameLine, blockName))
2470 recover();
2471
2472 if (typeQualifier.qualifier != EvqUniform)
2473 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302474 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
2475 "interface blocks must be uniform");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002476 recover();
2477 }
2478
Jamie Madill099c0f32013-06-20 11:55:52 -04002479 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
2480 if (layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002481 {
2482 recover();
2483 }
2484
Jamie Madill099c0f32013-06-20 11:55:52 -04002485 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2486 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002487 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002488 }
2489
Jamie Madill1566ef72013-06-20 11:55:54 -04002490 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2491 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002492 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002493 }
2494
Arun Patole7e7e68d2015-05-22 12:02:25 +05302495 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2496 if (!symbolTable.declare(blockNameSymbol))
2497 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002498 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
2499 recover();
2500 }
2501
Jamie Madill98493dd2013-07-08 14:39:03 -04002502 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302503 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2504 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002505 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302506 TType *fieldType = field->type();
2507 if (IsSampler(fieldType->getBasicType()))
2508 {
2509 error(field->line(), "unsupported type", fieldType->getBasicString(),
2510 "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002511 recover();
2512 }
2513
Jamie Madill98493dd2013-07-08 14:39:03 -04002514 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002515 switch (qualifier)
2516 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002517 case EvqGlobal:
2518 case EvqUniform:
2519 break;
2520 default:
2521 error(field->line(), "invalid qualifier on interface block member",
2522 getQualifierString(qualifier));
2523 recover();
2524 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002525 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002526
2527 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002528 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
2529 if (layoutLocationErrorCheck(field->line(), fieldLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002530 {
2531 recover();
2532 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002533
Jamie Madill98493dd2013-07-08 14:39:03 -04002534 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002535 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002536 error(field->line(), "invalid layout qualifier:",
2537 getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04002538 recover();
2539 }
2540
Jamie Madill98493dd2013-07-08 14:39:03 -04002541 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002542 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002543 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002544 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002545 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002546 {
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002547 warning(field->line(), "extraneous layout qualifier:",
Jamie Madillb98c3a82015-07-23 14:26:04 -04002548 getMatrixPackingString(fieldLayoutQualifier.matrixPacking),
2549 "only has an effect on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04002550 }
2551
Jamie Madill98493dd2013-07-08 14:39:03 -04002552 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002553 }
2554
Jamie Madill98493dd2013-07-08 14:39:03 -04002555 // add array index
2556 int arraySize = 0;
2557 if (arrayIndex != NULL)
2558 {
2559 if (arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize))
2560 recover();
2561 }
2562
Jamie Madillb98c3a82015-07-23 14:26:04 -04002563 TInterfaceBlock *interfaceBlock =
2564 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2565 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
2566 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002567
2568 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04002569 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002570
Jamie Madill98493dd2013-07-08 14:39:03 -04002571 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002572 {
2573 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002574 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2575 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002576 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302577 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002578
2579 // set parent pointer of the field variable
2580 fieldType->setInterfaceBlock(interfaceBlock);
2581
Arun Patole7e7e68d2015-05-22 12:02:25 +05302582 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002583 fieldVariable->setQualifier(typeQualifier.qualifier);
2584
Arun Patole7e7e68d2015-05-22 12:02:25 +05302585 if (!symbolTable.declare(fieldVariable))
2586 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002587 error(field->line(), "redefinition", field->name().c_str(),
2588 "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002589 recover();
2590 }
2591 }
2592 }
2593 else
2594 {
Olli Etuahoe0f623a2015-07-10 11:58:30 +03002595 if (reservedErrorCheck(instanceLine, *instanceName))
2596 recover();
2597
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002598 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302599 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002600 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002601
Arun Patole7e7e68d2015-05-22 12:02:25 +05302602 if (!symbolTable.declare(instanceTypeDef))
2603 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002604 error(instanceLine, "redefinition", instanceName->c_str(),
2605 "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002606 recover();
2607 }
2608
Jamie Madillb98c3a82015-07-23 14:26:04 -04002609 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002610 symbolName = instanceTypeDef->getName();
2611 }
2612
Jamie Madillb98c3a82015-07-23 14:26:04 -04002613 TIntermAggregate *aggregate = intermediate.makeAggregate(
2614 intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line),
2615 nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002616 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04002617
2618 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002619 return aggregate;
2620}
2621
Arun Patole7e7e68d2015-05-22 12:02:25 +05302622bool TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002623{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002624 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002625
2626 // Embedded structure definitions are not supported per GLSL ES spec.
2627 // They aren't allowed in GLSL either, but we need to detect this here
2628 // so we don't rely on the GLSL compiler to catch it.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302629 if (mStructNestingLevel > 1)
2630 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002631 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002632 return true;
2633 }
2634
2635 return false;
2636}
2637
2638void TParseContext::exitStructDeclaration()
2639{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002640 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002641}
2642
Jamie Madillb98c3a82015-07-23 14:26:04 -04002643namespace
2644{
kbr@chromium.org476541f2011-10-27 21:14:51 +00002645const int kWebGLMaxStructNesting = 4;
2646
2647} // namespace
2648
Arun Patole7e7e68d2015-05-22 12:02:25 +05302649bool TParseContext::structNestingErrorCheck(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002650{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302651 if (!IsWebGLBasedSpec(mShaderSpec))
2652 {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002653 return false;
2654 }
2655
Arun Patole7e7e68d2015-05-22 12:02:25 +05302656 if (field.type()->getBasicType() != EbtStruct)
2657 {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002658 return false;
2659 }
2660
2661 // We're already inside a structure definition at this point, so add
2662 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302663 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2664 {
Jamie Madill41a49272014-03-18 16:10:13 -04002665 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002666 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
2667 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04002668 std::string reason = reasonStream.str();
2669 error(line, reason.c_str(), field.name().c_str(), "");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002670 return true;
2671 }
2672
2673 return false;
2674}
2675
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002676//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002677// Parse an array index expression
2678//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002679TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
2680 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302681 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002682{
2683 TIntermTyped *indexedExpression = NULL;
2684
2685 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2686 {
2687 if (baseExpression->getAsSymbolNode())
2688 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302689 error(location, " left of '[' is not of type array, matrix, or vector ",
2690 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002691 }
2692 else
2693 {
2694 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2695 }
2696 recover();
2697 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002698
Jamie Madill21c1e452014-12-29 11:33:41 -05002699 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2700
Olli Etuaho36b05142015-11-12 13:10:42 +02002701 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
2702 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
2703 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
2704 // index is a constant expression.
2705 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
2706 {
2707 if (baseExpression->isInterfaceBlock())
2708 {
2709 error(
2710 location, "", "[",
2711 "array indexes for interface blocks arrays must be constant integral expressions");
2712 recover();
2713 }
2714 else if (baseExpression->getQualifier() == EvqFragmentOut)
2715 {
2716 error(location, "", "[",
2717 "array indexes for fragment outputs must be constant integral expressions");
2718 recover();
2719 }
2720 }
2721
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002722 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002723 {
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002724 // If the index is not qualified as constant, the behavior in the spec is undefined. This
2725 // applies even if ANGLE has been able to constant fold it (ANGLE may constant fold
2726 // expressions that are not constant expressions). The most compatible way to handle this
2727 // case is to report a warning instead of an error and force the index to be in the
2728 // correct range.
2729 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Jamie Madill21c1e452014-12-29 11:33:41 -05002730 int index = indexConstantUnion->getIConst(0);
Jamie Madill7164cf42013-07-08 13:30:59 -04002731 if (index < 0)
2732 {
2733 std::stringstream infoStream;
2734 infoStream << index;
2735 std::string info = infoStream.str();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002736 outOfRangeError(outOfRangeIndexIsError, location, "negative index", info.c_str());
Jamie Madill7164cf42013-07-08 13:30:59 -04002737 index = 0;
2738 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002739 TIntermConstantUnion *baseConstantUnion = baseExpression->getAsConstantUnion();
2740 if (baseConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002741 {
2742 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002743 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002744 // constant folding for array indexing
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002745 indexedExpression =
2746 addConstArrayNode(index, baseConstantUnion, location, outOfRangeIndexIsError);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002747 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002748 else if (baseExpression->isVector())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002749 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002750 // constant folding for vector indexing
Jamie Madill7164cf42013-07-08 13:30:59 -04002751 TVectorFields fields;
2752 fields.num = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002753 fields.offsets[0] =
2754 index; // need to do it this way because v.xy sends fields integer array
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002755 indexedExpression =
2756 addConstVectorNode(fields, baseConstantUnion, location, outOfRangeIndexIsError);
Jamie Madill7164cf42013-07-08 13:30:59 -04002757 }
2758 else if (baseExpression->isMatrix())
2759 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002760 // constant folding for matrix indexing
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002761 indexedExpression =
2762 addConstMatrixNode(index, baseConstantUnion, location, outOfRangeIndexIsError);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002763 }
2764 }
2765 else
2766 {
Jamie Madillb11e2482015-05-04 14:21:22 -04002767 int safeIndex = -1;
2768
Jamie Madill7164cf42013-07-08 13:30:59 -04002769 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002770 {
Jamie Madill18464b52013-07-08 14:01:55 -04002771 if (index >= baseExpression->getType().getArraySize())
Jamie Madill7164cf42013-07-08 13:30:59 -04002772 {
2773 std::stringstream extraInfoStream;
2774 extraInfoStream << "array index out of range '" << index << "'";
2775 std::string extraInfo = extraInfoStream.str();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002776 outOfRangeError(outOfRangeIndexIsError, location, "", "[", extraInfo.c_str());
Jamie Madillb11e2482015-05-04 14:21:22 -04002777 safeIndex = baseExpression->getType().getArraySize() - 1;
Jamie Madill7164cf42013-07-08 13:30:59 -04002778 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302779 else if (baseExpression->getQualifier() == EvqFragData && index > 0 &&
2780 !isExtensionEnabled("GL_EXT_draw_buffers"))
Jamie Madill5d287f52013-07-12 15:38:19 -04002781 {
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002782 outOfRangeError(
2783 outOfRangeIndexIsError, location, "", "[",
2784 "array indexes for gl_FragData must be zero when GL_EXT_draw_buffers is "
2785 "disabled");
Jamie Madillb11e2482015-05-04 14:21:22 -04002786 safeIndex = 0;
Jamie Madill5d287f52013-07-12 15:38:19 -04002787 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002788 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302789 else if ((baseExpression->isVector() || baseExpression->isMatrix()) &&
Jamie Madillb98c3a82015-07-23 14:26:04 -04002790 baseExpression->getType().getNominalSize() <= index)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002791 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002792 std::stringstream extraInfoStream;
2793 extraInfoStream << "field selection out of range '" << index << "'";
2794 std::string extraInfo = extraInfoStream.str();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002795 outOfRangeError(outOfRangeIndexIsError, location, "", "[", extraInfo.c_str());
Jamie Madillb11e2482015-05-04 14:21:22 -04002796 safeIndex = baseExpression->getType().getNominalSize() - 1;
Jamie Madill46131a32013-06-20 11:55:50 -04002797 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002798
Jamie Madillb11e2482015-05-04 14:21:22 -04002799 // Don't modify the data of the previous constant union, because it can point
2800 // to builtins, like gl_MaxDrawBuffers. Instead use a new sanitized object.
2801 if (safeIndex != -1)
2802 {
2803 TConstantUnion *safeConstantUnion = new TConstantUnion();
2804 safeConstantUnion->setIConst(safeIndex);
2805 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
2806 }
2807
Jamie Madillb98c3a82015-07-23 14:26:04 -04002808 indexedExpression =
2809 intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002810 }
2811 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002812 else
2813 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002814 indexedExpression =
2815 intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
Jamie Madill7164cf42013-07-08 13:30:59 -04002816 }
2817
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002818 if (indexedExpression == 0)
2819 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002820 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002821 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002822 indexedExpression =
2823 intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002824 }
2825 else if (baseExpression->isArray())
2826 {
Olli Etuahob3fbd862015-09-30 17:55:02 +03002827 TType indexedType = baseExpression->getType();
2828 indexedType.clearArrayness();
2829 indexedExpression->setType(indexedType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002830 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002831 else if (baseExpression->isMatrix())
2832 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002833 indexedExpression->setType(TType(baseExpression->getBasicType(),
Olli Etuahob3fbd862015-09-30 17:55:02 +03002834 baseExpression->getPrecision(), EvqTemporary,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002835 static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002836 }
2837 else if (baseExpression->isVector())
2838 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002839 indexedExpression->setType(
Olli Etuahob3fbd862015-09-30 17:55:02 +03002840 TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002841 }
2842 else
2843 {
2844 indexedExpression->setType(baseExpression->getType());
2845 }
2846
Olli Etuahob3fbd862015-09-30 17:55:02 +03002847 if (baseExpression->getType().getQualifier() == EvqConst &&
2848 indexExpression->getType().getQualifier() == EvqConst)
2849 {
2850 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2851 }
2852 else
2853 {
2854 indexedExpression->getTypePointer()->setQualifier(EvqTemporary);
2855 }
2856
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002857 return indexedExpression;
2858}
2859
Jamie Madillb98c3a82015-07-23 14:26:04 -04002860TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
2861 const TSourceLoc &dotLocation,
2862 const TString &fieldString,
2863 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002864{
2865 TIntermTyped *indexedExpression = NULL;
2866
2867 if (baseExpression->isArray())
2868 {
2869 error(fieldLocation, "cannot apply dot operator to an array", ".");
2870 recover();
2871 }
2872
2873 if (baseExpression->isVector())
2874 {
2875 TVectorFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002876 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields,
2877 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002878 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002879 fields.num = 1;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002880 fields.offsets[0] = 0;
2881 recover();
2882 }
2883
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002884 if (baseExpression->getAsConstantUnion())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002885 {
2886 // constant folding for vector fields
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002887 indexedExpression = addConstVectorNode(fields, baseExpression->getAsConstantUnion(),
2888 fieldLocation, true);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002889 }
2890 else
2891 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302892 TIntermTyped *index = intermediate.addSwizzle(fields, fieldLocation);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002893 indexedExpression =
2894 intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002895 }
2896 if (indexedExpression == nullptr)
2897 {
2898 recover();
2899 indexedExpression = baseExpression;
2900 }
2901 else
2902 {
2903 // Note that the qualifier set here will be corrected later.
Jamie Madillb98c3a82015-07-23 14:26:04 -04002904 indexedExpression->setType(TType(baseExpression->getBasicType(),
2905 baseExpression->getPrecision(), EvqTemporary,
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002906 (unsigned char)(fieldString).size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002907 }
2908 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002909 else if (baseExpression->getBasicType() == EbtStruct)
2910 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002911 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302912 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04002913 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002914 {
2915 error(dotLocation, "structure has no fields", "Internal Error");
2916 recover();
2917 indexedExpression = baseExpression;
2918 }
2919 else
2920 {
2921 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002922 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002923 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002924 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002925 {
2926 fieldFound = true;
2927 break;
2928 }
2929 }
2930 if (fieldFound)
2931 {
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002932 if (baseExpression->getAsConstantUnion())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002933 {
2934 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2935 if (indexedExpression == 0)
2936 {
2937 recover();
2938 indexedExpression = baseExpression;
2939 }
2940 else
2941 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002942 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002943 }
2944 }
2945 else
2946 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002947 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002948 unionArray->setIConst(i);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002949 TIntermTyped *index = intermediate.addConstantUnion(
2950 unionArray, *fields[i]->type(), fieldLocation);
2951 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression,
2952 index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002953 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002954 }
2955 }
2956 else
2957 {
2958 error(dotLocation, " no such field in structure", fieldString.c_str());
2959 recover();
2960 indexedExpression = baseExpression;
2961 }
2962 }
2963 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002964 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002965 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002966 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302967 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04002968 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002969 {
2970 error(dotLocation, "interface block has no fields", "Internal Error");
2971 recover();
2972 indexedExpression = baseExpression;
2973 }
2974 else
2975 {
2976 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002977 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002978 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002979 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002980 {
2981 fieldFound = true;
2982 break;
2983 }
2984 }
2985 if (fieldFound)
2986 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002987 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002988 unionArray->setIConst(i);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002989 TIntermTyped *index =
2990 intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
2991 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock,
2992 baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002993 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002994 }
2995 else
2996 {
2997 error(dotLocation, " no such field in interface block", fieldString.c_str());
2998 recover();
2999 indexedExpression = baseExpression;
3000 }
3001 }
3002 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003003 else
3004 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003005 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003006 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03003007 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303008 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003009 }
3010 else
3011 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303012 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003013 " field selection requires structure, vector, or interface block on left hand "
3014 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303015 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003016 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003017 recover();
3018 indexedExpression = baseExpression;
3019 }
3020
Olli Etuahob1edc4f2015-11-02 17:20:03 +02003021 if (baseExpression->getQualifier() == EvqConst)
3022 {
3023 indexedExpression->getTypePointer()->setQualifier(EvqConst);
3024 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003025 else
3026 {
3027 indexedExpression->getTypePointer()->setQualifier(EvqTemporary);
3028 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02003029
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003030 return indexedExpression;
3031}
3032
Jamie Madillb98c3a82015-07-23 14:26:04 -04003033TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3034 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003035{
Jamie Madilla5efff92013-06-06 11:56:47 -04003036 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003037
Jamie Madillb98c3a82015-07-23 14:26:04 -04003038 qualifier.location = -1;
Jamie Madilla5efff92013-06-06 11:56:47 -04003039 qualifier.matrixPacking = EmpUnspecified;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003040 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003041
3042 if (qualifierType == "shared")
3043 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003044 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003045 }
3046 else if (qualifierType == "packed")
3047 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003048 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003049 }
3050 else if (qualifierType == "std140")
3051 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003052 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003053 }
3054 else if (qualifierType == "row_major")
3055 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003056 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003057 }
3058 else if (qualifierType == "column_major")
3059 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003060 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003061 }
3062 else if (qualifierType == "location")
3063 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003064 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
3065 "location requires an argument");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003066 recover();
3067 }
3068 else
3069 {
3070 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
3071 recover();
3072 }
3073
Jamie Madilla5efff92013-06-06 11:56:47 -04003074 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003075}
3076
Jamie Madillb98c3a82015-07-23 14:26:04 -04003077TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3078 const TSourceLoc &qualifierTypeLine,
3079 const TString &intValueString,
3080 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303081 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003082{
Jamie Madilla5efff92013-06-06 11:56:47 -04003083 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003084
Jamie Madillb98c3a82015-07-23 14:26:04 -04003085 qualifier.location = -1;
Jamie Madilla5efff92013-06-06 11:56:47 -04003086 qualifier.matrixPacking = EmpUnspecified;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003087 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003088
3089 if (qualifierType != "location")
3090 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303091 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
3092 "only location may have arguments");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003093 recover();
3094 }
3095 else
3096 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003097 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003098 if (intValue < 0)
3099 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003100 error(intValueLine, "out of range:", intValueString.c_str(),
3101 "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003102 recover();
3103 }
3104 else
3105 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003106 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003107 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003108 }
3109
Jamie Madilla5efff92013-06-06 11:56:47 -04003110 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003111}
3112
Jamie Madillb98c3a82015-07-23 14:26:04 -04003113TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
3114 TLayoutQualifier rightQualifier)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003115{
Jamie Madilla5efff92013-06-06 11:56:47 -04003116 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003117
Jamie Madilla5efff92013-06-06 11:56:47 -04003118 if (rightQualifier.location != -1)
3119 {
3120 joinedQualifier.location = rightQualifier.location;
3121 }
3122 if (rightQualifier.matrixPacking != EmpUnspecified)
3123 {
3124 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
3125 }
3126 if (rightQualifier.blockStorage != EbsUnspecified)
3127 {
3128 joinedQualifier.blockStorage = rightQualifier.blockStorage;
3129 }
3130
3131 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003132}
3133
Arun Patole7e7e68d2015-05-22 12:02:25 +05303134TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc,
3135 TQualifier interpolationQualifier,
3136 const TSourceLoc &storageLoc,
3137 TQualifier storageQualifier)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003138{
3139 TQualifier mergedQualifier = EvqSmoothIn;
3140
Arun Patole7e7e68d2015-05-22 12:02:25 +05303141 if (storageQualifier == EvqFragmentIn)
3142 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003143 if (interpolationQualifier == EvqSmooth)
3144 mergedQualifier = EvqSmoothIn;
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 == EvqCentroidIn)
3151 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003152 if (interpolationQualifier == EvqSmooth)
3153 mergedQualifier = EvqCentroidIn;
3154 else if (interpolationQualifier == EvqFlat)
3155 mergedQualifier = EvqFlatIn;
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 == EvqVertexOut)
3160 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003161 if (interpolationQualifier == EvqSmooth)
3162 mergedQualifier = EvqSmoothOut;
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 if (storageQualifier == EvqCentroidOut)
3169 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003170 if (interpolationQualifier == EvqSmooth)
3171 mergedQualifier = EvqCentroidOut;
3172 else if (interpolationQualifier == EvqFlat)
3173 mergedQualifier = EvqFlatOut;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003174 else
3175 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003176 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303177 else
3178 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003179 error(interpolationLoc,
3180 "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303181 getInterpolationString(interpolationQualifier));
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003182 recover();
3183
3184 mergedQualifier = storageQualifier;
3185 }
3186
3187 TPublicType type;
3188 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
3189 return type;
3190}
3191
Jamie Madillb98c3a82015-07-23 14:26:04 -04003192TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3193 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003194{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03003195 if (voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier.type))
3196 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003197 recover();
3198 }
3199
Arun Patole7e7e68d2015-05-22 12:02:25 +05303200 for (unsigned int i = 0; i < fieldList->size(); ++i)
3201 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003202 //
3203 // Careful not to replace already known aspects of type, like array-ness
3204 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303205 TType *type = (*fieldList)[i]->type();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003206 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003207 type->setPrimarySize(typeSpecifier.primarySize);
3208 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003209 type->setPrecision(typeSpecifier.precision);
3210 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003211 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003212
3213 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303214 if (type->isArray())
3215 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003216 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
3217 recover();
3218 }
3219 if (typeSpecifier.array)
3220 type->setArraySize(typeSpecifier.arraySize);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303221 if (typeSpecifier.userDef)
3222 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003223 type->setStruct(typeSpecifier.userDef->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003224 }
3225
Arun Patole7e7e68d2015-05-22 12:02:25 +05303226 if (structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i]))
3227 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003228 recover();
3229 }
3230 }
3231
Jamie Madill98493dd2013-07-08 14:39:03 -04003232 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003233}
3234
Jamie Madillb98c3a82015-07-23 14:26:04 -04003235TPublicType TParseContext::addStructure(const TSourceLoc &structLine,
3236 const TSourceLoc &nameLine,
3237 const TString *structName,
3238 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003239{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303240 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003241 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003242
Jamie Madill9b820842015-02-12 10:40:10 -05003243 // Store a bool in the struct if we're at global scope, to allow us to
3244 // skip the local struct scoping workaround in HLSL.
Jamie Madillb960cc42015-02-12 15:33:20 +00003245 structure->setUniqueId(TSymbolTable::nextUniqueId());
Jamie Madill9b820842015-02-12 10:40:10 -05003246 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003247
Jamie Madill98493dd2013-07-08 14:39:03 -04003248 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003249 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003250 if (reservedErrorCheck(nameLine, *structName))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003251 {
3252 recover();
3253 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303254 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
3255 if (!symbolTable.declare(userTypeDef))
3256 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003257 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003258 recover();
3259 }
3260 }
3261
3262 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04003263 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003264 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003265 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04003266 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003267 switch (qualifier)
3268 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003269 case EvqGlobal:
3270 case EvqTemporary:
3271 break;
3272 default:
3273 error(field.line(), "invalid qualifier on struct member",
3274 getQualifierString(qualifier));
3275 recover();
3276 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003277 }
3278 }
3279
3280 TPublicType publicType;
3281 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003282 publicType.userDef = structureType;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003283 exitStructDeclaration();
3284
3285 return publicType;
3286}
3287
Jamie Madillb98c3a82015-07-23 14:26:04 -04003288TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
3289 TIntermAggregate *statementList,
3290 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02003291{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003292 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003293 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02003294 init->isVector())
3295 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003296 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
3297 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003298 recover();
3299 return nullptr;
3300 }
3301
Olli Etuahoac5274d2015-02-20 10:19:08 +02003302 if (statementList)
3303 {
3304 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
3305 {
3306 recover();
3307 return nullptr;
3308 }
3309 }
3310
Olli Etuahoa3a36662015-02-17 13:46:51 +02003311 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3312 if (node == nullptr)
3313 {
3314 error(loc, "erroneous switch statement", "switch");
3315 recover();
3316 return nullptr;
3317 }
3318 return node;
3319}
3320
3321TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3322{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003323 if (mSwitchNestingLevel == 0)
3324 {
3325 error(loc, "case labels need to be inside switch statements", "case");
3326 recover();
3327 return nullptr;
3328 }
3329 if (condition == nullptr)
3330 {
3331 error(loc, "case label must have a condition", "case");
3332 recover();
3333 return nullptr;
3334 }
3335 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04003336 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02003337 {
3338 error(condition->getLine(), "case label must be a scalar integer", "case");
3339 recover();
3340 }
3341 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003342 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
3343 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
3344 // fold in case labels.
3345 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02003346 {
3347 error(condition->getLine(), "case label must be constant", "case");
3348 recover();
3349 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003350 TIntermCase *node = intermediate.addCase(condition, loc);
3351 if (node == nullptr)
3352 {
3353 error(loc, "erroneous case statement", "case");
3354 recover();
3355 return nullptr;
3356 }
3357 return node;
3358}
3359
3360TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3361{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003362 if (mSwitchNestingLevel == 0)
3363 {
3364 error(loc, "default labels need to be inside switch statements", "default");
3365 recover();
3366 return nullptr;
3367 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003368 TIntermCase *node = intermediate.addCase(nullptr, loc);
3369 if (node == nullptr)
3370 {
3371 error(loc, "erroneous default statement", "default");
3372 recover();
3373 return nullptr;
3374 }
3375 return node;
3376}
3377
Jamie Madillb98c3a82015-07-23 14:26:04 -04003378TIntermTyped *TParseContext::createUnaryMath(TOperator op,
3379 TIntermTyped *child,
3380 const TSourceLoc &loc,
3381 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003382{
3383 if (child == nullptr)
3384 {
3385 return nullptr;
3386 }
3387
3388 switch (op)
3389 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003390 case EOpLogicalNot:
3391 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
3392 child->isVector())
3393 {
3394 return nullptr;
3395 }
3396 break;
3397 case EOpBitwiseNot:
3398 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3399 child->isMatrix() || child->isArray())
3400 {
3401 return nullptr;
3402 }
3403 break;
3404 case EOpPostIncrement:
3405 case EOpPreIncrement:
3406 case EOpPostDecrement:
3407 case EOpPreDecrement:
3408 case EOpNegative:
3409 case EOpPositive:
3410 if (child->getBasicType() == EbtStruct || child->getBasicType() == EbtBool ||
3411 child->isArray())
3412 {
3413 return nullptr;
3414 }
3415 // Operators for built-ins are already type checked against their prototype.
3416 default:
3417 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003418 }
3419
Olli Etuahof6c694b2015-03-26 14:50:53 +02003420 return intermediate.addUnaryMath(op, child, loc, funcReturnType);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003421}
3422
Olli Etuaho09b22472015-02-11 11:47:26 +02003423TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3424{
Olli Etuahof6c694b2015-03-26 14:50:53 +02003425 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003426 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003427 {
3428 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
3429 recover();
3430 return child;
3431 }
3432 return node;
3433}
3434
Jamie Madillb98c3a82015-07-23 14:26:04 -04003435TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
3436 TIntermTyped *child,
3437 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003438{
3439 if (lValueErrorCheck(loc, GetOperatorString(op), child))
3440 recover();
3441 return addUnaryMath(op, child, loc);
3442}
3443
Jamie Madillb98c3a82015-07-23 14:26:04 -04003444bool TParseContext::binaryOpCommonCheck(TOperator op,
3445 TIntermTyped *left,
3446 TIntermTyped *right,
3447 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003448{
3449 if (left->isArray() || right->isArray())
3450 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003451 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003452 {
3453 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3454 return false;
3455 }
3456
3457 if (left->isArray() != right->isArray())
3458 {
3459 error(loc, "array / non-array mismatch", GetOperatorString(op));
3460 return false;
3461 }
3462
3463 switch (op)
3464 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003465 case EOpEqual:
3466 case EOpNotEqual:
3467 case EOpAssign:
3468 case EOpInitialize:
3469 break;
3470 default:
3471 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3472 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02003473 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003474 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003475 if (left->getArraySize() != right->getArraySize())
3476 {
3477 error(loc, "array size mismatch", GetOperatorString(op));
3478 return false;
3479 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003480 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003481
3482 // Check ops which require integer / ivec parameters
3483 bool isBitShift = false;
3484 switch (op)
3485 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003486 case EOpBitShiftLeft:
3487 case EOpBitShiftRight:
3488 case EOpBitShiftLeftAssign:
3489 case EOpBitShiftRightAssign:
3490 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3491 // check that the basic type is an integer type.
3492 isBitShift = true;
3493 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3494 {
3495 return false;
3496 }
3497 break;
3498 case EOpBitwiseAnd:
3499 case EOpBitwiseXor:
3500 case EOpBitwiseOr:
3501 case EOpBitwiseAndAssign:
3502 case EOpBitwiseXorAssign:
3503 case EOpBitwiseOrAssign:
3504 // It is enough to check the type of only one operand, since later it
3505 // is checked that the operand types match.
3506 if (!IsInteger(left->getBasicType()))
3507 {
3508 return false;
3509 }
3510 break;
3511 default:
3512 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003513 }
3514
3515 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3516 // So the basic type should usually match.
3517 if (!isBitShift && left->getBasicType() != right->getBasicType())
3518 {
3519 return false;
3520 }
3521
Olli Etuaho9dd217b2015-03-20 14:24:31 +02003522 // Check that type sizes match exactly on ops that require that.
Olli Etuahoff699002015-03-23 14:38:42 +02003523 // Also check restrictions for structs that contain arrays or samplers.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003524 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003525 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003526 case EOpAssign:
3527 case EOpInitialize:
3528 case EOpEqual:
3529 case EOpNotEqual:
3530 // ESSL 1.00 sections 5.7, 5.8, 5.9
3531 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
3532 {
3533 error(loc, "undefined operation for structs containing arrays",
3534 GetOperatorString(op));
3535 return false;
3536 }
3537 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3538 // we interpret the spec so that this extends to structs containing samplers,
3539 // similarly to ESSL 1.00 spec.
3540 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
3541 left->getType().isStructureContainingSamplers())
3542 {
3543 error(loc, "undefined operation for structs containing samplers",
3544 GetOperatorString(op));
3545 return false;
3546 }
3547 case EOpLessThan:
3548 case EOpGreaterThan:
3549 case EOpLessThanEqual:
3550 case EOpGreaterThanEqual:
3551 if ((left->getNominalSize() != right->getNominalSize()) ||
3552 (left->getSecondarySize() != right->getSecondarySize()))
3553 {
3554 return false;
3555 }
3556 default:
3557 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003558 }
3559
Olli Etuahod6b14282015-03-17 14:31:35 +02003560 return true;
3561}
3562
Jamie Madillb98c3a82015-07-23 14:26:04 -04003563TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
3564 TIntermTyped *left,
3565 TIntermTyped *right,
3566 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02003567{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003568 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003569 return nullptr;
3570
Olli Etuahofc1806e2015-03-17 13:03:11 +02003571 switch (op)
3572 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003573 case EOpEqual:
3574 case EOpNotEqual:
3575 break;
3576 case EOpLessThan:
3577 case EOpGreaterThan:
3578 case EOpLessThanEqual:
3579 case EOpGreaterThanEqual:
3580 ASSERT(!left->isArray() && !right->isArray());
3581 if (left->isMatrix() || left->isVector() || left->getBasicType() == EbtStruct)
3582 {
3583 return nullptr;
3584 }
3585 break;
3586 case EOpLogicalOr:
3587 case EOpLogicalXor:
3588 case EOpLogicalAnd:
3589 ASSERT(!left->isArray() && !right->isArray());
3590 if (left->getBasicType() != EbtBool || left->isMatrix() || left->isVector())
3591 {
3592 return nullptr;
3593 }
3594 break;
3595 case EOpAdd:
3596 case EOpSub:
3597 case EOpDiv:
3598 case EOpMul:
3599 ASSERT(!left->isArray() && !right->isArray());
3600 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool)
3601 {
3602 return nullptr;
3603 }
3604 break;
3605 case EOpIMod:
3606 ASSERT(!left->isArray() && !right->isArray());
3607 // Note that this is only for the % operator, not for mod()
3608 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool ||
3609 left->getBasicType() == EbtFloat)
3610 {
3611 return nullptr;
3612 }
3613 break;
3614 // Note that for bitwise ops, type checking is done in promote() to
3615 // share code between ops and compound assignment
3616 default:
3617 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003618 }
3619
Olli Etuahofc1806e2015-03-17 13:03:11 +02003620 return intermediate.addBinaryMath(op, left, right, loc);
3621}
3622
Jamie Madillb98c3a82015-07-23 14:26:04 -04003623TIntermTyped *TParseContext::addBinaryMath(TOperator op,
3624 TIntermTyped *left,
3625 TIntermTyped *right,
3626 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003627{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003628 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003629 if (node == 0)
3630 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003631 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3632 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003633 recover();
3634 return left;
3635 }
3636 return node;
3637}
3638
Jamie Madillb98c3a82015-07-23 14:26:04 -04003639TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
3640 TIntermTyped *left,
3641 TIntermTyped *right,
3642 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003643{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003644 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003645 if (node == 0)
3646 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003647 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3648 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003649 recover();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003650 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02003651 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003652 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
3653 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003654 }
3655 return node;
3656}
3657
Jamie Madillb98c3a82015-07-23 14:26:04 -04003658TIntermTyped *TParseContext::createAssign(TOperator op,
3659 TIntermTyped *left,
3660 TIntermTyped *right,
3661 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003662{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003663 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003664 {
3665 return intermediate.addAssign(op, left, right, loc);
3666 }
3667 return nullptr;
3668}
3669
Jamie Madillb98c3a82015-07-23 14:26:04 -04003670TIntermTyped *TParseContext::addAssign(TOperator op,
3671 TIntermTyped *left,
3672 TIntermTyped *right,
3673 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003674{
3675 TIntermTyped *node = createAssign(op, left, right, loc);
3676 if (node == nullptr)
3677 {
3678 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
3679 recover();
3680 return left;
3681 }
3682 return node;
3683}
3684
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003685TIntermTyped *TParseContext::addComma(TIntermTyped *left,
3686 TIntermTyped *right,
3687 const TSourceLoc &loc)
3688{
Olli Etuaho15200042015-11-04 16:56:31 +02003689 return intermediate.addComma(left, right, loc, mShaderVersion);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003690}
3691
Olli Etuaho49300862015-02-20 14:54:49 +02003692TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3693{
3694 switch (op)
3695 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003696 case EOpContinue:
3697 if (mLoopNestingLevel <= 0)
3698 {
3699 error(loc, "continue statement only allowed in loops", "");
3700 recover();
3701 }
3702 break;
3703 case EOpBreak:
3704 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
3705 {
3706 error(loc, "break statement only allowed in loops and switch statements", "");
3707 recover();
3708 }
3709 break;
3710 case EOpReturn:
3711 if (mCurrentFunctionType->getBasicType() != EbtVoid)
3712 {
3713 error(loc, "non-void function must return a value", "return");
3714 recover();
3715 }
3716 break;
3717 default:
3718 // No checks for discard
3719 break;
Olli Etuaho49300862015-02-20 14:54:49 +02003720 }
3721 return intermediate.addBranch(op, loc);
3722}
3723
Jamie Madillb98c3a82015-07-23 14:26:04 -04003724TIntermBranch *TParseContext::addBranch(TOperator op,
3725 TIntermTyped *returnValue,
3726 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02003727{
3728 ASSERT(op == EOpReturn);
3729 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003730 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02003731 {
3732 error(loc, "void function cannot return a value", "return");
3733 recover();
3734 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003735 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02003736 {
3737 error(loc, "function return is not matching type:", "return");
3738 recover();
3739 }
3740 return intermediate.addBranch(op, returnValue, loc);
3741}
3742
Jamie Madillb98c3a82015-07-23 14:26:04 -04003743TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
3744 TIntermNode *paramNode,
3745 TIntermNode *thisNode,
3746 const TSourceLoc &loc,
3747 bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003748{
Jamie Madillb98c3a82015-07-23 14:26:04 -04003749 *fatalError = false;
3750 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003751 TIntermTyped *callNode = nullptr;
3752
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003753 if (thisNode != nullptr)
3754 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003755 TConstantUnion *unionArray = new TConstantUnion[1];
Jamie Madillb98c3a82015-07-23 14:26:04 -04003756 int arraySize = 0;
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003757 TIntermTyped *typedThis = thisNode->getAsTyped();
3758 if (fnCall->getName() != "length")
3759 {
3760 error(loc, "invalid method", fnCall->getName().c_str());
3761 recover();
3762 }
3763 else if (paramNode != nullptr)
3764 {
3765 error(loc, "method takes no parameters", "length");
3766 recover();
3767 }
3768 else if (typedThis == nullptr || !typedThis->isArray())
3769 {
3770 error(loc, "length can only be called on arrays", "length");
3771 recover();
3772 }
3773 else
3774 {
Olli Etuaho96e67382015-04-23 14:27:02 +03003775 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03003776 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003777 {
Olli Etuaho39282e12015-04-23 15:41:48 +03003778 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003779 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03003780 // (func()).length()
3781 // (int[3](0, 1, 2)).length()
Jamie Madillb98c3a82015-07-23 14:26:04 -04003782 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
3783 // expression.
3784 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
3785 // spec section 5.9 which allows "An array, vector or matrix expression with the
3786 // length method applied".
3787 error(loc, "length can only be called on array names, not on array expressions",
3788 "length");
Olli Etuaho39282e12015-04-23 15:41:48 +03003789 recover();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003790 }
3791 }
Olli Etuaho96e67382015-04-23 14:27:02 +03003792 unionArray->setIConst(arraySize);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003793 callNode =
3794 intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003795 }
3796 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003797 {
3798 //
3799 // Then this should be a constructor.
3800 // Don't go through the symbol table for constructors.
3801 // Their parameters will be verified algorithmically.
3802 //
3803 TType type(EbtVoid, EbpUndefined); // use this to get the type back
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003804 if (!constructorErrorCheck(loc, paramNode, *fnCall, op, &type))
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003805 {
3806 //
3807 // It's a constructor, of type 'type'.
3808 //
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003809 callNode = addConstructor(paramNode, &type, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003810 }
Olli Etuaho72ba85b2015-03-04 14:23:26 +02003811
3812 if (callNode == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003813 {
3814 recover();
3815 callNode = intermediate.setAggregateOperator(nullptr, op, loc);
3816 }
3817 callNode->setType(type);
3818 }
3819 else
3820 {
3821 //
3822 // Not a constructor. Find it in the symbol table.
3823 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303824 const TFunction *fnCandidate;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003825 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003826 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003827 if (fnCandidate)
3828 {
3829 //
3830 // A declared function.
3831 //
3832 if (builtIn && !fnCandidate->getExtension().empty() &&
3833 extensionErrorCheck(loc, fnCandidate->getExtension()))
3834 {
3835 recover();
3836 }
3837 op = fnCandidate->getBuiltInOp();
3838 if (builtIn && op != EOpNull)
3839 {
3840 //
3841 // A function call mapped to a built-in operation.
3842 //
3843 if (fnCandidate->getParamCount() == 1)
3844 {
3845 //
3846 // Treat it like a built-in unary operator.
3847 //
Olli Etuaho15c2ac32015-11-09 15:51:43 +02003848 TIntermAggregate *paramAgg = paramNode->getAsAggregate();
3849 paramNode = paramAgg->getSequence()->front();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003850 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc,
3851 &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003852 if (callNode == nullptr)
3853 {
3854 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003855 extraInfoStream
3856 << "built in unary operator function. Type: "
3857 << static_cast<TIntermTyped *>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003858 std::string extraInfo = extraInfoStream.str();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003859 error(paramNode->getLine(), " wrong operand type", "Internal Error",
3860 extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003861 *fatalError = true;
3862 return nullptr;
3863 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003864 }
3865 else
3866 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003867 TIntermAggregate *aggregate =
3868 intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003869 aggregate->setType(fnCandidate->getReturnType());
3870 aggregate->setPrecisionFromChildren();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02003871 if (aggregate->areChildrenConstQualified())
3872 {
3873 aggregate->getTypePointer()->setQualifier(EvqConst);
3874 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003875
3876 // Some built-in functions have out parameters too.
3877 functionCallLValueErrorCheck(fnCandidate, aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05303878
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003879 // See if we can constant fold a built-in. Note that this may be possible even
3880 // if it is not const-qualified.
Olli Etuahob43846e2015-06-02 18:18:57 +03003881 TIntermTyped *foldedNode = intermediate.foldAggregateBuiltIn(aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05303882 if (foldedNode)
3883 {
Arun Patole274f0702015-05-05 13:33:30 +05303884 callNode = foldedNode;
3885 }
Olli Etuahob43846e2015-06-02 18:18:57 +03003886 else
3887 {
3888 callNode = aggregate;
3889 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003890 }
3891 }
3892 else
3893 {
3894 // This is a real function call
Jamie Madillb98c3a82015-07-23 14:26:04 -04003895 TIntermAggregate *aggregate =
3896 intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003897 aggregate->setType(fnCandidate->getReturnType());
3898
Jamie Madillb98c3a82015-07-23 14:26:04 -04003899 // this is how we know whether the given function is a builtIn function or a user
3900 // defined function
3901 // if builtIn == false, it's a userDefined -> could be an overloaded
3902 // builtIn function also
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003903 // if builtIn == true, it's definitely a builtIn function with EOpNull
3904 if (!builtIn)
3905 aggregate->setUserDefined();
3906 aggregate->setName(fnCandidate->getMangledName());
Corentin Wallez71d147f2015-02-11 11:15:24 -08003907 aggregate->setFunctionId(fnCandidate->getUniqueId());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003908
3909 // This needs to happen after the name is set
3910 if (builtIn)
3911 aggregate->setBuiltInFunctionPrecision();
3912
3913 callNode = aggregate;
3914
3915 functionCallLValueErrorCheck(fnCandidate, aggregate);
3916 }
3917 }
3918 else
3919 {
3920 // error message was put out by findFunction()
3921 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003922 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003923 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003924 callNode = intermediate.addConstantUnion(unionArray,
3925 TType(EbtFloat, EbpUndefined, EvqConst), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003926 recover();
3927 }
3928 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003929 return callNode;
3930}
3931
Jamie Madillb98c3a82015-07-23 14:26:04 -04003932TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
3933 TIntermTyped *trueBlock,
3934 TIntermTyped *falseBlock,
Olli Etuaho52901742015-04-15 13:42:45 +03003935 const TSourceLoc &loc)
3936{
3937 if (boolErrorCheck(loc, cond))
3938 recover();
3939
3940 if (trueBlock->getType() != falseBlock->getType())
3941 {
3942 binaryOpError(loc, ":", trueBlock->getCompleteString(), falseBlock->getCompleteString());
3943 recover();
3944 return falseBlock;
3945 }
Olli Etuahoa2d53032015-04-15 14:14:44 +03003946 // ESSL1 sections 5.2 and 5.7:
3947 // ESSL3 section 5.7:
3948 // Ternary operator is not among the operators allowed for structures/arrays.
3949 if (trueBlock->isArray() || trueBlock->getBasicType() == EbtStruct)
3950 {
3951 error(loc, "ternary operator is not allowed for structures or arrays", ":");
3952 recover();
3953 return falseBlock;
3954 }
Olli Etuaho52901742015-04-15 13:42:45 +03003955 return intermediate.addSelection(cond, trueBlock, falseBlock, loc);
3956}
Olli Etuaho49300862015-02-20 14:54:49 +02003957
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003958//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003959// Parse an array of strings using yyparse.
3960//
3961// Returns 0 for success.
3962//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003963int PaParseStrings(size_t count,
3964 const char *const string[],
3965 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05303966 TParseContext *context)
3967{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003968 if ((count == 0) || (string == NULL))
3969 return 1;
3970
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003971 if (glslang_initialize(context))
3972 return 1;
3973
alokp@chromium.org408c45e2012-04-05 15:54:43 +00003974 int error = glslang_scan(count, string, length, context);
3975 if (!error)
3976 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003977
alokp@chromium.org73bc2982012-06-19 18:48:05 +00003978 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00003979
alokp@chromium.org6b495712012-06-29 00:06:58 +00003980 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003981}