blob: 8486ac0e30be0556f56ee7f77b729dcbc4d4d429 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Jamie Madill6b9cb252013-10-17 10:45:47 -04007#include "compiler/translator/ParseContext.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +00008
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00009#include <stdarg.h>
apatrick@chromium.org8187fa82010-06-15 22:09:28 +000010#include <stdio.h>
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000011
daniel@transgaming.comb401a922012-10-26 18:58:24 +000012#include "compiler/preprocessor/SourceLocation.h"
Dmitry Skiba01971112015-07-10 14:54:00 -040013#include "compiler/translator/Cache.h"
Olli Etuahoac5274d2015-02-20 10:19:08 +020014#include "compiler/translator/glslang.h"
15#include "compiler/translator/ValidateSwitch.h"
Olli Etuahob0c645e2015-05-12 14:25:36 +030016#include "compiler/translator/ValidateGlobalInitializer.h"
Olli Etuaho37ad4742015-04-27 13:18:50 +030017#include "compiler/translator/util.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000018
alokp@chromium.org8b851c62012-06-15 16:25:11 +000019///////////////////////////////////////////////////////////////////////
20//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000021// Sub- vector and matrix fields
22//
23////////////////////////////////////////////////////////////////////////
24
25//
26// Look at a '.' field selector string and change it into offsets
27// for a vector.
28//
Jamie Madillb98c3a82015-07-23 14:26:04 -040029bool TParseContext::parseVectorFields(const TString &compString,
30 int vecSize,
31 TVectorFields &fields,
Arun Patole7e7e68d2015-05-22 12:02:25 +053032 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000033{
Jamie Madillb98c3a82015-07-23 14:26:04 -040034 fields.num = (int)compString.size();
Arun Patole7e7e68d2015-05-22 12:02:25 +053035 if (fields.num > 4)
36 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000037 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000038 return false;
39 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000040
Jamie Madillb98c3a82015-07-23 14:26:04 -040041 enum
42 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000043 exyzw,
44 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +000045 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000046 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000047
Arun Patole7e7e68d2015-05-22 12:02:25 +053048 for (int i = 0; i < fields.num; ++i)
49 {
50 switch (compString[i])
51 {
Jamie Madillb98c3a82015-07-23 14:26:04 -040052 case 'x':
53 fields.offsets[i] = 0;
54 fieldSet[i] = exyzw;
55 break;
56 case 'r':
57 fields.offsets[i] = 0;
58 fieldSet[i] = ergba;
59 break;
60 case 's':
61 fields.offsets[i] = 0;
62 fieldSet[i] = estpq;
63 break;
64 case 'y':
65 fields.offsets[i] = 1;
66 fieldSet[i] = exyzw;
67 break;
68 case 'g':
69 fields.offsets[i] = 1;
70 fieldSet[i] = ergba;
71 break;
72 case 't':
73 fields.offsets[i] = 1;
74 fieldSet[i] = estpq;
75 break;
76 case 'z':
77 fields.offsets[i] = 2;
78 fieldSet[i] = exyzw;
79 break;
80 case 'b':
81 fields.offsets[i] = 2;
82 fieldSet[i] = ergba;
83 break;
84 case 'p':
85 fields.offsets[i] = 2;
86 fieldSet[i] = estpq;
87 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053088
Jamie Madillb98c3a82015-07-23 14:26:04 -040089 case 'w':
90 fields.offsets[i] = 3;
91 fieldSet[i] = exyzw;
92 break;
93 case 'a':
94 fields.offsets[i] = 3;
95 fieldSet[i] = ergba;
96 break;
97 case 'q':
98 fields.offsets[i] = 3;
99 fieldSet[i] = estpq;
100 break;
101 default:
102 error(line, "illegal vector field selection", compString.c_str());
103 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000104 }
105 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000106
Arun Patole7e7e68d2015-05-22 12:02:25 +0530107 for (int i = 0; i < fields.num; ++i)
108 {
109 if (fields.offsets[i] >= vecSize)
110 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400111 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000112 return false;
113 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000114
Arun Patole7e7e68d2015-05-22 12:02:25 +0530115 if (i > 0)
116 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400117 if (fieldSet[i] != fieldSet[i - 1])
Arun Patole7e7e68d2015-05-22 12:02:25 +0530118 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400119 error(line, "illegal - vector component fields not from the same set",
120 compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000121 return false;
122 }
123 }
124 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000125
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000126 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000127}
128
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000129///////////////////////////////////////////////////////////////////////
130//
131// Errors
132//
133////////////////////////////////////////////////////////////////////////
134
135//
136// Track whether errors have occurred.
137//
138void TParseContext::recover()
139{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000140}
141
142//
143// Used by flex/bison to output all syntax and parsing errors.
144//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530145void TParseContext::error(const TSourceLoc &loc,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400146 const char *reason,
147 const char *token,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530148 const char *extraInfo)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000149{
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000150 pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -0400151 srcLoc.file = loc.first_file;
152 srcLoc.line = loc.first_line;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400153 mDiagnostics.writeInfo(pp::Diagnostics::PP_ERROR, srcLoc, reason, token, extraInfo);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000154}
155
Arun Patole7e7e68d2015-05-22 12:02:25 +0530156void TParseContext::warning(const TSourceLoc &loc,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400157 const char *reason,
158 const char *token,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530159 const char *extraInfo)
160{
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000161 pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -0400162 srcLoc.file = loc.first_file;
163 srcLoc.line = loc.first_line;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400164 mDiagnostics.writeInfo(pp::Diagnostics::PP_WARNING, srcLoc, reason, token, extraInfo);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000165}
166
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200167void TParseContext::outOfRangeError(bool isError,
168 const TSourceLoc &loc,
169 const char *reason,
170 const char *token,
171 const char *extraInfo)
172{
173 if (isError)
174 {
175 error(loc, reason, token, extraInfo);
176 recover();
177 }
178 else
179 {
180 warning(loc, reason, token, extraInfo);
181 }
182}
183
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000184//
185// Same error message for all places assignments don't work.
186//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530187void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000188{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000189 std::stringstream extraInfoStream;
190 extraInfoStream << "cannot convert from '" << right << "' to '" << left << "'";
191 std::string extraInfo = extraInfoStream.str();
192 error(line, "", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000193}
194
195//
196// Same error message for all places unary operations don't work.
197//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530198void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000199{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000200 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400201 extraInfoStream << "no operation '" << op << "' exists that takes an operand of type "
202 << operand << " (or there is no acceptable conversion)";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000203 std::string extraInfo = extraInfoStream.str();
204 error(line, " wrong operand type", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000205}
206
207//
208// Same error message for all binary operations don't work.
209//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400210void TParseContext::binaryOpError(const TSourceLoc &line,
211 const char *op,
212 TString left,
213 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000214{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000215 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400216 extraInfoStream << "no operation '" << op << "' exists that takes a left-hand operand of type '"
217 << left << "' and a right operand of type '" << right
218 << "' (or there is no acceptable conversion)";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000219 std::string extraInfo = extraInfoStream.str();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530220 error(line, " wrong operand types ", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000221}
222
Jamie Madillb98c3a82015-07-23 14:26:04 -0400223bool TParseContext::precisionErrorCheck(const TSourceLoc &line,
224 TPrecision precision,
225 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530226{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400227 if (!mChecksPrecisionErrors)
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000228 return false;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400229 switch (type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530230 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400231 case EbtFloat:
232 if (precision == EbpUndefined)
233 {
234 error(line, "No precision specified for (float)", "");
235 return true;
236 }
237 break;
238 case EbtInt:
239 if (precision == EbpUndefined)
240 {
241 error(line, "No precision specified (int)", "");
242 return true;
243 }
244 break;
245 default:
246 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000247 }
248 return false;
249}
250
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000251//
252// Both test and if necessary, spit out an error, to see if the node is really
253// an l-value that can be operated on this way.
254//
255// Returns true if the was an error.
256//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530257bool TParseContext::lValueErrorCheck(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000258{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400259 TIntermSymbol *symNode = node->getAsSymbolNode();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530260 TIntermBinary *binaryNode = node->getAsBinaryNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000261
Arun Patole7e7e68d2015-05-22 12:02:25 +0530262 if (binaryNode)
263 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000264 bool errorReturn;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000265
Jamie Madillb98c3a82015-07-23 14:26:04 -0400266 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530267 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400268 case EOpIndexDirect:
269 case EOpIndexIndirect:
270 case EOpIndexDirectStruct:
271 case EOpIndexDirectInterfaceBlock:
272 return lValueErrorCheck(line, op, binaryNode->getLeft());
273 case EOpVectorSwizzle:
274 errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft());
275 if (!errorReturn)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530276 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400277 int offset[4] = {0, 0, 0, 0};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000278
Jamie Madillb98c3a82015-07-23 14:26:04 -0400279 TIntermTyped *rightNode = binaryNode->getRight();
280 TIntermAggregate *aggrNode = rightNode->getAsAggregate();
281
282 for (TIntermSequence::iterator p = aggrNode->getSequence()->begin();
283 p != aggrNode->getSequence()->end(); p++)
284 {
285 int value = (*p)->getAsTyped()->getAsConstantUnion()->getIConst(0);
286 offset[value]++;
287 if (offset[value] > 1)
288 {
289 error(line, " l-value of swizzle cannot have duplicate components", op);
290
291 return true;
292 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000293 }
294 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000295
Jamie Madillb98c3a82015-07-23 14:26:04 -0400296 return errorReturn;
297 default:
298 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000299 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000300 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000301
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000302 return true;
303 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000304
Arun Patole7e7e68d2015-05-22 12:02:25 +0530305 const char *symbol = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000306 if (symNode != 0)
307 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000308
Arun Patole7e7e68d2015-05-22 12:02:25 +0530309 const char *message = 0;
310 switch (node->getQualifier())
311 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400312 case EvqConst:
313 message = "can't modify a const";
314 break;
315 case EvqConstReadOnly:
316 message = "can't modify a const";
317 break;
318 case EvqAttribute:
319 message = "can't modify an attribute";
320 break;
321 case EvqFragmentIn:
322 message = "can't modify an input";
323 break;
324 case EvqVertexIn:
325 message = "can't modify an input";
326 break;
327 case EvqUniform:
328 message = "can't modify a uniform";
329 break;
330 case EvqVaryingIn:
331 message = "can't modify a varying";
332 break;
333 case EvqFragCoord:
334 message = "can't modify gl_FragCoord";
335 break;
336 case EvqFrontFacing:
337 message = "can't modify gl_FrontFacing";
338 break;
339 case EvqPointCoord:
340 message = "can't modify gl_PointCoord";
341 break;
342 default:
343 //
344 // Type that can't be written to?
345 //
346 if (node->getBasicType() == EbtVoid)
347 {
348 message = "can't modify void";
349 }
350 if (IsSampler(node->getBasicType()))
351 {
352 message = "can't modify a sampler";
353 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000354 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000355
Arun Patole7e7e68d2015-05-22 12:02:25 +0530356 if (message == 0 && binaryNode == 0 && symNode == 0)
357 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000358 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000359
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000360 return true;
361 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000362
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000363 //
364 // Everything else is okay, no error.
365 //
366 if (message == 0)
367 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000368
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000369 //
370 // If we get here, we have an error and a message.
371 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530372 if (symNode)
373 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000374 std::stringstream extraInfoStream;
375 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
376 std::string extraInfo = extraInfoStream.str();
377 error(line, " l-value required", op, extraInfo.c_str());
378 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530379 else
380 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000381 std::stringstream extraInfoStream;
382 extraInfoStream << "(" << message << ")";
383 std::string extraInfo = extraInfoStream.str();
384 error(line, " l-value required", op, extraInfo.c_str());
385 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000386
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000387 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000388}
389
390//
391// Both test, and if necessary spit out an error, to see if the node is really
392// a constant.
393//
394// Returns true if the was an error.
395//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530396bool TParseContext::constErrorCheck(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000397{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000398 if (node->getQualifier() == EvqConst)
399 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000400
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000401 error(node->getLine(), "constant expression required", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000402
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000403 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000404}
405
406//
407// Both test, and if necessary spit out an error, to see if the node is really
408// an integer.
409//
410// Returns true if the was an error.
411//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530412bool TParseContext::integerErrorCheck(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000413{
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000414 if (node->isScalarInt())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000415 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000416
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000417 error(node->getLine(), "integer expression required", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000418
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000419 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000420}
421
422//
423// Both test, and if necessary spit out an error, to see if we are currently
424// globally scoped.
425//
426// Returns true if the was an error.
427//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530428bool TParseContext::globalErrorCheck(const TSourceLoc &line, bool global, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000429{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000430 if (global)
431 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000432
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000433 error(line, "only allowed at global scope", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000434
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000435 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000436}
437
438//
439// For now, keep it simple: if it starts "gl_", it's reserved, independent
440// of scope. Except, if the symbol table is at the built-in push-level,
441// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000442// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
443// webgl shader.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000444//
445// Returns true if there was an error.
446//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530447bool TParseContext::reservedErrorCheck(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000448{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530449 static const char *reservedErrMsg = "reserved built-in name";
450 if (!symbolTable.atBuiltInLevel())
451 {
452 if (identifier.compare(0, 3, "gl_") == 0)
453 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000454 error(line, reservedErrMsg, "gl_");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000455 return true;
456 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530457 if (IsWebGLBasedSpec(mShaderSpec))
458 {
459 if (identifier.compare(0, 6, "webgl_") == 0)
460 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000461 error(line, reservedErrMsg, "webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000462 return true;
463 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530464 if (identifier.compare(0, 7, "_webgl_") == 0)
465 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000466 error(line, reservedErrMsg, "_webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000467 return true;
468 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530469 if (mShaderSpec == SH_CSS_SHADERS_SPEC && identifier.compare(0, 4, "css_") == 0)
470 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000471 error(line, reservedErrMsg, "css_");
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000472 return true;
473 }
alokp@chromium.org613ef312010-07-21 18:54:22 +0000474 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530475 if (identifier.find("__") != TString::npos)
476 {
477 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400478 "identifiers containing two consecutive underscores (__) are reserved as "
479 "possible future keywords",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530480 identifier.c_str());
daniel@transgaming.combeadd5d2012-04-12 02:35:31 +0000481 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000482 }
483 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000484
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000485 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000486}
487
488//
489// Make sure there is enough data provided to the constructor to build
490// something of the type of the constructor. Also returns the type of
491// the constructor.
492//
493// Returns true if there was an error in construction.
494//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400495bool TParseContext::constructorErrorCheck(const TSourceLoc &line,
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200496 TIntermNode *argumentsNode,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400497 TFunction &function,
498 TOperator op,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530499 TType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000500{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000501 *type = function.getReturnType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000502
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000503 bool constructingMatrix = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400504 switch (op)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530505 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400506 case EOpConstructMat2:
507 case EOpConstructMat2x3:
508 case EOpConstructMat2x4:
509 case EOpConstructMat3x2:
510 case EOpConstructMat3:
511 case EOpConstructMat3x4:
512 case EOpConstructMat4x2:
513 case EOpConstructMat4x3:
514 case EOpConstructMat4:
515 constructingMatrix = true;
516 break;
517 default:
518 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000519 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000520
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000521 //
522 // Note: It's okay to have too many components available, but not okay to have unused
523 // arguments. 'full' will go to true when enough args have been seen. If we loop
524 // again, there is an extra argument, so 'overfull' will become true.
525 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000526
Jamie Madillb98c3a82015-07-23 14:26:04 -0400527 size_t size = 0;
528 bool constType = true;
529 bool full = false;
530 bool overFull = false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000531 bool matrixInMatrix = false;
532 bool arrayArg = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530533 for (size_t i = 0; i < function.getParamCount(); ++i)
534 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700535 const TConstParameter &param = function.getParam(i);
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000536 size += param.type->getObjectSize();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530537
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000538 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000539 matrixInMatrix = true;
540 if (full)
541 overFull = true;
542 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
543 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000544 if (param.type->getQualifier() != EvqConst)
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000545 constType = false;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000546 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000547 arrayArg = true;
548 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530549
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000550 if (constType)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000551 type->setQualifier(EvqConst);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000552
Olli Etuaho376f1b52015-04-13 13:23:41 +0300553 if (type->isArray())
554 {
555 if (type->isUnsizedArray())
556 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700557 type->setArraySize(static_cast<int>(function.getParamCount()));
Olli Etuaho376f1b52015-04-13 13:23:41 +0300558 }
559 else if (static_cast<size_t>(type->getArraySize()) != function.getParamCount())
560 {
561 error(line, "array constructor needs one argument per array element", "constructor");
562 return true;
563 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000564 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000565
Arun Patole7e7e68d2015-05-22 12:02:25 +0530566 if (arrayArg && op != EOpConstructStruct)
567 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000568 error(line, "constructing from a non-dereferenced array", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000569 return true;
570 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000571
Arun Patole7e7e68d2015-05-22 12:02:25 +0530572 if (matrixInMatrix && !type->isArray())
573 {
574 if (function.getParamCount() != 1)
575 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400576 error(line, "constructing matrix from matrix can only take one argument",
577 "constructor");
578 return true;
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000579 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000580 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000581
Arun Patole7e7e68d2015-05-22 12:02:25 +0530582 if (overFull)
583 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000584 error(line, "too many arguments", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000585 return true;
586 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530587
Jamie Madillb98c3a82015-07-23 14:26:04 -0400588 if (op == EOpConstructStruct && !type->isArray() &&
589 type->getStruct()->fields().size() != function.getParamCount())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530590 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400591 error(line,
592 "Number of constructor parameters does not match the number of structure fields",
593 "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000594 return true;
595 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000596
Arun Patole7e7e68d2015-05-22 12:02:25 +0530597 if (!type->isMatrix() || !matrixInMatrix)
598 {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000599 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
Arun Patole7e7e68d2015-05-22 12:02:25 +0530600 (op == EOpConstructStruct && size < type->getObjectSize()))
601 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000602 error(line, "not enough data provided for construction", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000603 return true;
604 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000605 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000606
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200607 if (argumentsNode == nullptr)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530608 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200609 error(line, "constructor does not have any arguments", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000610 return true;
611 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200612
613 TIntermAggregate *argumentsAgg = argumentsNode->getAsAggregate();
614 for (TIntermNode *&argNode : *argumentsAgg->getSequence())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530615 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200616 TIntermTyped *argTyped = argNode->getAsTyped();
617 ASSERT(argTyped != nullptr);
618 if (op != EOpConstructStruct && IsSampler(argTyped->getBasicType()))
619 {
620 error(line, "cannot convert a sampler", "constructor");
621 return true;
622 }
623 if (argTyped->getBasicType() == EbtVoid)
624 {
625 error(line, "cannot convert a void", "constructor");
626 return true;
627 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000628 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000629
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000630 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000631}
632
Jamie Madillb98c3a82015-07-23 14:26:04 -0400633// This function checks to see if a void variable has been declared and raise an error message for
634// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000635//
636// returns true in case of an error
637//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400638bool TParseContext::voidErrorCheck(const TSourceLoc &line,
639 const TString &identifier,
640 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000641{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300642 if (type == EbtVoid)
643 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000644 error(line, "illegal use of type 'void'", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000645 return true;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300646 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000647
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000648 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000649}
650
Jamie Madillb98c3a82015-07-23 14:26:04 -0400651// This function checks to see if the node (for the expression) contains a scalar boolean expression
652// or not
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000653//
654// returns true in case of an error
655//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530656bool TParseContext::boolErrorCheck(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000657{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530658 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector())
659 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000660 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000661 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530662 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000663
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000664 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000665}
666
Jamie Madillb98c3a82015-07-23 14:26:04 -0400667// This function checks to see if the node (for the expression) contains a scalar boolean expression
668// or not
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000669//
670// returns true in case of an error
671//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530672bool TParseContext::boolErrorCheck(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000673{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530674 if (pType.type != EbtBool || pType.isAggregate())
675 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000676 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000677 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530678 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000679
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000680 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000681}
682
Jamie Madillb98c3a82015-07-23 14:26:04 -0400683bool TParseContext::samplerErrorCheck(const TSourceLoc &line,
684 const TPublicType &pType,
685 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000686{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530687 if (pType.type == EbtStruct)
688 {
689 if (containsSampler(*pType.userDef))
690 {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000691 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530692
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000693 return true;
694 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530695
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000696 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530697 }
698 else if (IsSampler(pType.type))
699 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000700 error(line, reason, getBasicString(pType.type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000701
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000702 return true;
703 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000704
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000705 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000706}
707
Arun Patole7e7e68d2015-05-22 12:02:25 +0530708bool TParseContext::locationDeclaratorListCheck(const TSourceLoc &line, const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400709{
710 if (pType.layoutQualifier.location != -1)
711 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400712 error(line, "location must only be specified for a single input or output variable",
713 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400714 return true;
715 }
716
717 return false;
718}
719
Jamie Madillb98c3a82015-07-23 14:26:04 -0400720bool TParseContext::parameterSamplerErrorCheck(const TSourceLoc &line,
721 TQualifier qualifier,
722 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000723{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400724 if ((qualifier == EvqOut || qualifier == EvqInOut) && type.getBasicType() != EbtStruct &&
725 IsSampler(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530726 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000727 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000728 return true;
729 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000730
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000731 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000732}
733
Arun Patole7e7e68d2015-05-22 12:02:25 +0530734bool TParseContext::containsSampler(const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000735{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000736 if (IsSampler(type.getBasicType()))
737 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000738
Arun Patole7e7e68d2015-05-22 12:02:25 +0530739 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock())
740 {
741 const TFieldList &fields = type.getStruct()->fields();
742 for (unsigned int i = 0; i < fields.size(); ++i)
743 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400744 if (containsSampler(*fields[i]->type()))
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000745 return true;
746 }
747 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000748
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000749 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000750}
751
752//
753// Do size checking for an array type's size.
754//
755// Returns true if there was an error.
756//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530757bool TParseContext::arraySizeErrorCheck(const TSourceLoc &line, TIntermTyped *expr, int &size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000758{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530759 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000760
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200761 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
762 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
763 // fold as array size.
764 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000765 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000766 error(line, "array size must be a constant integer expression", "");
Olli Etuahoe7847b02015-03-16 11:56:12 +0200767 size = 1;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000768 return true;
769 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000770
Nicolas Capens906744a2014-06-06 15:18:07 -0400771 unsigned int unsignedSize = 0;
772
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000773 if (constant->getBasicType() == EbtUInt)
774 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400775 unsignedSize = constant->getUConst(0);
Jamie Madillb98c3a82015-07-23 14:26:04 -0400776 size = static_cast<int>(unsignedSize);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000777 }
778 else
779 {
780 size = constant->getIConst(0);
781
Nicolas Capens906744a2014-06-06 15:18:07 -0400782 if (size < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000783 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400784 error(line, "array size must be non-negative", "");
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000785 size = 1;
786 return true;
787 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400788
789 unsignedSize = static_cast<unsigned int>(size);
790 }
791
792 if (size == 0)
793 {
794 error(line, "array size must be greater than zero", "");
795 size = 1;
796 return true;
797 }
798
799 // The size of arrays is restricted here to prevent issues further down the
800 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
801 // 4096 registers so this should be reasonable even for aggressively optimizable code.
802 const unsigned int sizeLimit = 65536;
803
804 if (unsignedSize > sizeLimit)
805 {
806 error(line, "array size too large", "");
807 size = 1;
808 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000809 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000810
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000811 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000812}
813
814//
815// See if this qualifier can be an array.
816//
817// Returns true if there is an error.
818//
Olli Etuaho3739d232015-04-08 12:23:44 +0300819bool TParseContext::arrayQualifierErrorCheck(const TSourceLoc &line, const TPublicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000820{
Olli Etuaho3739d232015-04-08 12:23:44 +0300821 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqVertexIn) ||
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400822 (type.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300823 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400824 error(line, "cannot declare arrays of this qualifier",
825 TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000826 return true;
827 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000828
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000829 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000830}
831
832//
833// See if this type can be an array.
834//
835// Returns true if there is an error.
836//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530837bool TParseContext::arrayTypeErrorCheck(const TSourceLoc &line, const TPublicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000838{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000839 //
840 // Can the type be an array?
841 //
Jamie Madill06145232015-05-13 13:10:01 -0400842 if (type.array)
843 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000844 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000845 return true;
846 }
Olli Etuahocc36b982015-07-10 14:14:18 +0300847 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
848 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
849 // 4.3.4).
850 if (mShaderVersion >= 300 && type.type == EbtStruct && sh::IsVarying(type.qualifier))
851 {
852 error(line, "cannot declare arrays of structs of this qualifier",
853 TType(type).getCompleteString().c_str());
854 return true;
855 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000856
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000857 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000858}
859
860//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000861// Enforce non-initializer type/qualifier rules.
862//
863// Returns true if there was an error.
864//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400865bool TParseContext::nonInitErrorCheck(const TSourceLoc &line,
866 const TString &identifier,
867 TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000868{
Olli Etuaho3739d232015-04-08 12:23:44 +0300869 ASSERT(type != nullptr);
870 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000871 {
872 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300873 type->qualifier = EvqTemporary;
874
875 // Generate informative error messages for ESSL1.
876 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400877 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000878 {
Arun Patole7e7e68d2015-05-22 12:02:25 +0530879 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400880 "structures containing arrays may not be declared constant since they cannot be "
881 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530882 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000883 }
884 else
885 {
886 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
887 }
888
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000889 return true;
890 }
Olli Etuaho376f1b52015-04-13 13:23:41 +0300891 if (type->isUnsizedArray())
892 {
893 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
894 return true;
895 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000896 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000897}
898
Olli Etuaho2935c582015-04-08 14:32:06 +0300899// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000900// and update the symbol table.
901//
Olli Etuaho2935c582015-04-08 14:32:06 +0300902// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000903//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400904bool TParseContext::declareVariable(const TSourceLoc &line,
905 const TString &identifier,
906 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +0300907 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000908{
Olli Etuaho2935c582015-04-08 14:32:06 +0300909 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000910
Olli Etuaho2935c582015-04-08 14:32:06 +0300911 bool needsReservedErrorCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000912
Olli Etuaho2935c582015-04-08 14:32:06 +0300913 // gl_LastFragData may be redeclared with a new precision qualifier
914 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
915 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400916 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
917 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho2935c582015-04-08 14:32:06 +0300918 if (type.getArraySize() == maxDrawBuffers->getConstPointer()->getIConst())
919 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400920 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +0300921 {
922 needsReservedErrorCheck = extensionErrorCheck(line, builtInSymbol->getExtension());
923 }
924 }
925 else
926 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400927 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
928 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +0300929 return false;
930 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000931 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000932
Olli Etuaho2935c582015-04-08 14:32:06 +0300933 if (needsReservedErrorCheck && reservedErrorCheck(line, identifier))
934 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000935
Olli Etuaho2935c582015-04-08 14:32:06 +0300936 (*variable) = new TVariable(&identifier, type);
937 if (!symbolTable.declare(*variable))
938 {
939 error(line, "redefinition", identifier.c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -0400940 *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +0300941 return false;
942 }
943
944 if (voidErrorCheck(line, identifier, type.getBasicType()))
945 return false;
946
947 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000948}
949
Jamie Madillb98c3a82015-07-23 14:26:04 -0400950bool TParseContext::paramErrorCheck(const TSourceLoc &line,
951 TQualifier qualifier,
952 TQualifier paramQualifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530953 TType *type)
954{
955 if (qualifier != EvqConst && qualifier != EvqTemporary)
956 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000957 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000958 return true;
959 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530960 if (qualifier == EvqConst && paramQualifier != EvqIn)
961 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400962 error(line, "qualifier not allowed with ", getQualifierString(qualifier),
963 getQualifierString(paramQualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000964 return true;
965 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000966
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000967 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000968 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000969 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000970 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000971
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000972 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000973}
974
Arun Patole7e7e68d2015-05-22 12:02:25 +0530975bool TParseContext::extensionErrorCheck(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000976{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400977 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000978 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +0530979 if (iter == extBehavior.end())
980 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000981 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000982 return true;
983 }
zmo@google.comf5450912011-09-09 01:37:19 +0000984 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +0530985 if (iter->second == EBhDisable || iter->second == EBhUndefined)
986 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000987 error(line, "extension", extension.c_str(), "is disabled");
988 return true;
989 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530990 if (iter->second == EBhWarn)
991 {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000992 warning(line, "extension", extension.c_str(), "is being used");
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000993 return false;
994 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000995
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000996 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000997}
998
Jamie Madillb98c3a82015-07-23 14:26:04 -0400999// These checks are common for all declarations starting a declarator list, and declarators that
1000// follow an empty declaration.
Olli Etuahofa33d582015-04-09 14:33:12 +03001001//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001002bool TParseContext::singleDeclarationErrorCheck(const TPublicType &publicType,
1003 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001004{
Olli Etuahofa33d582015-04-09 14:33:12 +03001005 switch (publicType.qualifier)
1006 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001007 case EvqVaryingIn:
1008 case EvqVaryingOut:
1009 case EvqAttribute:
1010 case EvqVertexIn:
1011 case EvqFragmentOut:
1012 if (publicType.type == EbtStruct)
1013 {
1014 error(identifierLocation, "cannot be used with a structure",
1015 getQualifierString(publicType.qualifier));
1016 return true;
1017 }
Olli Etuahofa33d582015-04-09 14:33:12 +03001018
Jamie Madillb98c3a82015-07-23 14:26:04 -04001019 default:
1020 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001021 }
1022
Jamie Madillb98c3a82015-07-23 14:26:04 -04001023 if (publicType.qualifier != EvqUniform &&
1024 samplerErrorCheck(identifierLocation, publicType, "samplers must be uniform"))
Olli Etuahofa33d582015-04-09 14:33:12 +03001025 {
Jamie Madilla5efff92013-06-06 11:56:47 -04001026 return true;
Olli Etuahofa33d582015-04-09 14:33:12 +03001027 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001028
1029 // check for layout qualifier issues
1030 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
1031
1032 if (layoutQualifier.matrixPacking != EmpUnspecified)
1033 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001034 error(identifierLocation, "layout qualifier",
1035 getMatrixPackingString(layoutQualifier.matrixPacking),
Olli Etuahofa33d582015-04-09 14:33:12 +03001036 "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -04001037 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001038 }
1039
1040 if (layoutQualifier.blockStorage != EbsUnspecified)
1041 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001042 error(identifierLocation, "layout qualifier",
1043 getBlockStorageString(layoutQualifier.blockStorage),
Olli Etuahofa33d582015-04-09 14:33:12 +03001044 "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -04001045 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001046 }
1047
Olli Etuahofa33d582015-04-09 14:33:12 +03001048 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut &&
1049 layoutLocationErrorCheck(identifierLocation, publicType.layoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001050 {
Jamie Madill51a53c72013-06-19 09:24:43 -04001051 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001052 }
1053
1054 return false;
1055}
1056
Jamie Madillb98c3a82015-07-23 14:26:04 -04001057bool TParseContext::layoutLocationErrorCheck(const TSourceLoc &location,
1058 const TLayoutQualifier &layoutQualifier)
Jamie Madilla5efff92013-06-06 11:56:47 -04001059{
1060 if (layoutQualifier.location != -1)
1061 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001062 error(location, "invalid layout qualifier:", "location",
1063 "only valid on program inputs and outputs");
Jamie Madilla5efff92013-06-06 11:56:47 -04001064 return true;
1065 }
1066
1067 return false;
1068}
1069
Jamie Madillb98c3a82015-07-23 14:26:04 -04001070bool TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate,
1071 TIntermAggregate *aggregate)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001072{
1073 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1074 {
1075 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1076 if (qual == EvqOut || qual == EvqInOut)
1077 {
1078 TIntermTyped *node = (*(aggregate->getSequence()))[i]->getAsTyped();
1079 if (lValueErrorCheck(node->getLine(), "assign", node))
1080 {
1081 error(node->getLine(),
Jamie Madillb98c3a82015-07-23 14:26:04 -04001082 "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error");
Olli Etuahob6e07a62015-02-16 12:22:10 +02001083 recover();
1084 return true;
1085 }
1086 }
1087 }
1088 return false;
1089}
1090
Jamie Madillb98c3a82015-07-23 14:26:04 -04001091void TParseContext::es3InvariantErrorCheck(const TQualifier qualifier,
1092 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001093{
1094 if (!sh::IsVaryingOut(qualifier) && qualifier != EvqFragmentOut)
1095 {
1096 error(invariantLocation, "Only out variables can be invariant.", "invariant");
1097 recover();
1098 }
1099}
1100
Arun Patole7e7e68d2015-05-22 12:02:25 +05301101bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001102{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001103 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001104 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1105 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001106}
1107
Arun Patole7e7e68d2015-05-22 12:02:25 +05301108bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001109{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001110 return ::IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001111}
1112
Jamie Madillb98c3a82015-07-23 14:26:04 -04001113void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1114 const char *extName,
1115 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001116{
1117 pp::SourceLocation srcLoc;
1118 srcLoc.file = loc.first_file;
1119 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001120 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001121}
1122
Jamie Madillb98c3a82015-07-23 14:26:04 -04001123void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1124 const char *name,
1125 const char *value,
1126 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001127{
1128 pp::SourceLocation srcLoc;
1129 srcLoc.file = loc.first_file;
1130 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001131 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001132}
1133
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001134/////////////////////////////////////////////////////////////////////////////////
1135//
1136// Non-Errors.
1137//
1138/////////////////////////////////////////////////////////////////////////////////
1139
Jamie Madill5c097022014-08-20 16:38:32 -04001140const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1141 const TString *name,
1142 const TSymbol *symbol)
1143{
1144 const TVariable *variable = NULL;
1145
1146 if (!symbol)
1147 {
1148 error(location, "undeclared identifier", name->c_str());
1149 recover();
1150 }
1151 else if (!symbol->isVariable())
1152 {
1153 error(location, "variable expected", name->c_str());
1154 recover();
1155 }
1156 else
1157 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001158 variable = static_cast<const TVariable *>(symbol);
Jamie Madill5c097022014-08-20 16:38:32 -04001159
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001160 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
Jamie Madill5c097022014-08-20 16:38:32 -04001161 !variable->getExtension().empty() &&
1162 extensionErrorCheck(location, variable->getExtension()))
1163 {
1164 recover();
1165 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001166
1167 // Reject shaders using both gl_FragData and gl_FragColor
1168 TQualifier qualifier = variable->getType().getQualifier();
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001169 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001170 {
1171 mUsesFragData = true;
1172 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001173 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001174 {
1175 mUsesFragColor = true;
1176 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001177 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1178 {
1179 mUsesSecondaryOutputs = true;
1180 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001181
1182 // This validation is not quite correct - it's only an error to write to
1183 // both FragData and FragColor. For simplicity, and because users shouldn't
1184 // be rewarded for reading from undefined varaibles, return an error
1185 // if they are both referenced, rather than assigned.
1186 if (mUsesFragData && mUsesFragColor)
1187 {
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001188 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1189 if (mUsesSecondaryOutputs)
1190 {
1191 errorMessage =
1192 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1193 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1194 }
1195 error(location, errorMessage, name->c_str());
Jamie Madill14e95b32015-05-07 10:10:41 -04001196 recover();
1197 }
Jamie Madill5c097022014-08-20 16:38:32 -04001198 }
1199
1200 if (!variable)
1201 {
1202 TType type(EbtFloat, EbpUndefined);
1203 TVariable *fakeVariable = new TVariable(name, type);
1204 symbolTable.declare(fakeVariable);
1205 variable = fakeVariable;
1206 }
1207
1208 return variable;
1209}
1210
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001211TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1212 const TString *name,
1213 const TSymbol *symbol)
1214{
1215 const TVariable *variable = getNamedVariable(location, name, symbol);
1216
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001217 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001218 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001219 const TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001220 return intermediate.addConstantUnion(constArray, variable->getType(), location);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001221 }
1222 else
1223 {
1224 return intermediate.addSymbol(variable->getUniqueId(), variable->getName(),
1225 variable->getType(), location);
1226 }
1227}
1228
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001229//
1230// Look up a function name in the symbol table, and make sure it is a function.
1231//
1232// Return the function symbol if found, otherwise 0.
1233//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001234const TFunction *TParseContext::findFunction(const TSourceLoc &line,
1235 TFunction *call,
1236 int inputShaderVersion,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301237 bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001238{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001239 // First find by unmangled name to check whether the function name has been
1240 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001241 // If a function is found, check for one with a matching argument list.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301242 const TSymbol *symbol = symbolTable.find(call->getName(), inputShaderVersion, builtIn);
1243 if (symbol == 0 || symbol->isFunction())
1244 {
Austin Kinross3ae64652015-01-26 15:51:39 -08001245 symbol = symbolTable.find(call->getMangledName(), inputShaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001246 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001247
Arun Patole7e7e68d2015-05-22 12:02:25 +05301248 if (symbol == 0)
1249 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001250 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001251 return 0;
1252 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001253
Arun Patole7e7e68d2015-05-22 12:02:25 +05301254 if (!symbol->isFunction())
1255 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001256 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001257 return 0;
1258 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001259
Jamie Madillb98c3a82015-07-23 14:26:04 -04001260 return static_cast<const TFunction *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001261}
1262
1263//
1264// Initializers show up in several places in the grammar. Have one set of
1265// code to handle them here.
1266//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001267// Returns true on error, false if no error
1268//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001269bool TParseContext::executeInitializer(const TSourceLoc &line,
1270 const TString &identifier,
1271 const TPublicType &pType,
1272 TIntermTyped *initializer,
1273 TIntermNode **intermNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001274{
Olli Etuahoe7847b02015-03-16 11:56:12 +02001275 ASSERT(intermNode != nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001276 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001277
Olli Etuaho2935c582015-04-08 14:32:06 +03001278 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001279 if (type.isUnsizedArray())
1280 {
1281 type.setArraySize(initializer->getArraySize());
1282 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001283 if (!declareVariable(line, identifier, type, &variable))
1284 {
1285 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001286 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001287
Olli Etuahob0c645e2015-05-12 14:25:36 +03001288 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001289 if (symbolTable.atGlobalLevel() &&
1290 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001291 {
1292 // Error message does not completely match behavior with ESSL 1.00, but
1293 // we want to steer developers towards only using constant expressions.
1294 error(line, "global variable initializers must be constant expressions", "=");
1295 return true;
1296 }
1297 if (globalInitWarning)
1298 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001299 warning(
1300 line,
1301 "global variable initializers should be constant expressions "
1302 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1303 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001304 }
1305
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001306 //
1307 // identifier must be of type constant, a global, or a temporary
1308 //
1309 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301310 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1311 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001312 error(line, " cannot initialize this type of qualifier ",
1313 variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001314 return true;
1315 }
1316 //
1317 // test for and propagate constant
1318 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001319
Arun Patole7e7e68d2015-05-22 12:02:25 +05301320 if (qualifier == EvqConst)
1321 {
1322 if (qualifier != initializer->getType().getQualifier())
1323 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001324 std::stringstream extraInfoStream;
1325 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1326 std::string extraInfo = extraInfoStream.str();
1327 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001328 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001329 return true;
1330 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301331 if (type != initializer->getType())
1332 {
1333 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001334 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001335 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001336 return true;
1337 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001338
1339 // Save the constant folded value to the variable if possible. For example array
1340 // initializers are not folded, since that way copying the array literal to multiple places
1341 // in the shader is avoided.
1342 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1343 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301344 if (initializer->getAsConstantUnion())
1345 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001346 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001347 *intermNode = nullptr;
1348 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301349 }
1350 else if (initializer->getAsSymbolNode())
1351 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001352 const TSymbol *symbol =
1353 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1354 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001355
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001356 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001357 if (constArray)
1358 {
1359 variable->shareConstPointer(constArray);
1360 *intermNode = nullptr;
1361 return false;
1362 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001363 }
1364 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001365
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001366 TIntermSymbol *intermSymbol = intermediate.addSymbol(
1367 variable->getUniqueId(), variable->getName(), variable->getType(), line);
1368 *intermNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1369 if (*intermNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001370 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001371 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1372 return true;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001373 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001374
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001375 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001376}
1377
Jamie Madillb98c3a82015-07-23 14:26:04 -04001378TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier,
1379 bool invariant,
1380 TLayoutQualifier layoutQualifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301381 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001382{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001383 TPublicType returnType = typeSpecifier;
1384 returnType.qualifier = qualifier;
1385 returnType.invariant = invariant;
Jamie Madilla5efff92013-06-06 11:56:47 -04001386 returnType.layoutQualifier = layoutQualifier;
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001387
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001388 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001389 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001390 if (typeSpecifier.array)
1391 {
1392 error(typeSpecifier.line, "not supported", "first-class array");
1393 recover();
1394 returnType.clearArrayness();
1395 }
1396
Jamie Madillb98c3a82015-07-23 14:26:04 -04001397 if (qualifier == EvqAttribute &&
1398 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001399 {
1400 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1401 recover();
1402 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001403
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001404 if ((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1405 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1406 {
1407 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1408 recover();
1409 }
1410 }
1411 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001412 {
Olli Etuahoabb0c382015-07-13 12:01:12 +03001413 if (!layoutQualifier.isEmpty())
1414 {
1415 if (globalErrorCheck(typeSpecifier.line, symbolTable.atGlobalLevel(), "layout"))
1416 {
1417 recover();
1418 }
1419 }
Olli Etuahocc36b982015-07-10 14:14:18 +03001420 if (sh::IsVarying(qualifier) || qualifier == EvqVertexIn || qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001421 {
Olli Etuahocc36b982015-07-10 14:14:18 +03001422 es3InputOutputTypeCheck(qualifier, typeSpecifier, typeSpecifier.line);
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001423 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001424 }
1425
1426 return returnType;
1427}
1428
Olli Etuahocc36b982015-07-10 14:14:18 +03001429void TParseContext::es3InputOutputTypeCheck(const TQualifier qualifier,
1430 const TPublicType &type,
1431 const TSourceLoc &qualifierLocation)
1432{
1433 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
1434 if (type.type == EbtBool)
1435 {
1436 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
1437 recover();
1438 }
1439
1440 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
1441 switch (qualifier)
1442 {
1443 case EvqVertexIn:
1444 // ESSL 3.00 section 4.3.4
1445 if (type.array)
1446 {
1447 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
1448 recover();
1449 }
1450 // Vertex inputs with a struct type are disallowed in singleDeclarationErrorCheck
1451 return;
1452 case EvqFragmentOut:
1453 // ESSL 3.00 section 4.3.6
1454 if (type.isMatrix())
1455 {
1456 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
1457 recover();
1458 }
1459 // Fragment outputs with a struct type are disallowed in singleDeclarationErrorCheck
1460 return;
1461 default:
1462 break;
1463 }
1464
1465 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
1466 // restrictions.
1467 bool typeContainsIntegers =
1468 (type.type == EbtInt || type.type == EbtUInt || type.isStructureContainingType(EbtInt) ||
1469 type.isStructureContainingType(EbtUInt));
1470 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
1471 {
1472 error(qualifierLocation, "must use 'flat' interpolation here",
1473 getQualifierString(qualifier));
1474 recover();
1475 }
1476
1477 if (type.type == EbtStruct)
1478 {
1479 // ESSL 3.00 sections 4.3.4 and 4.3.6.
1480 // These restrictions are only implied by the ESSL 3.00 spec, but
1481 // the ESSL 3.10 spec lists these restrictions explicitly.
1482 if (type.array)
1483 {
1484 error(qualifierLocation, "cannot be an array of structures",
1485 getQualifierString(qualifier));
1486 recover();
1487 }
1488 if (type.isStructureContainingArrays())
1489 {
1490 error(qualifierLocation, "cannot be a structure containing an array",
1491 getQualifierString(qualifier));
1492 recover();
1493 }
1494 if (type.isStructureContainingType(EbtStruct))
1495 {
1496 error(qualifierLocation, "cannot be a structure containing a structure",
1497 getQualifierString(qualifier));
1498 recover();
1499 }
1500 if (type.isStructureContainingType(EbtBool))
1501 {
1502 error(qualifierLocation, "cannot be a structure containing a bool",
1503 getQualifierString(qualifier));
1504 recover();
1505 }
1506 }
1507}
1508
Olli Etuahofa33d582015-04-09 14:33:12 +03001509TIntermAggregate *TParseContext::parseSingleDeclaration(TPublicType &publicType,
1510 const TSourceLoc &identifierOrTypeLocation,
1511 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001512{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001513 TIntermSymbol *symbol =
1514 intermediate.addSymbol(0, identifier, TType(publicType), identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001515
Olli Etuahobab4c082015-04-24 16:38:49 +03001516 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03001517
Olli Etuahobab4c082015-04-24 16:38:49 +03001518 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1519
1520 if (emptyDeclaration)
1521 {
1522 if (publicType.isUnsizedArray())
1523 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001524 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1525 // error. It is assumed that this applies to empty declarations as well.
1526 error(identifierOrTypeLocation, "empty array declaration needs to specify a size",
1527 identifier.c_str());
Olli Etuahobab4c082015-04-24 16:38:49 +03001528 }
1529 }
1530 else
Jamie Madill60ed9812013-06-06 11:56:46 -04001531 {
Olli Etuahofa33d582015-04-09 14:33:12 +03001532 if (singleDeclarationErrorCheck(publicType, identifierOrTypeLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001533 recover();
1534
Olli Etuaho376f1b52015-04-13 13:23:41 +03001535 if (nonInitErrorCheck(identifierOrTypeLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001536 recover();
1537
Olli Etuaho2935c582015-04-08 14:32:06 +03001538 TVariable *variable = nullptr;
Olli Etuahofa33d582015-04-09 14:33:12 +03001539 if (!declareVariable(identifierOrTypeLocation, identifier, TType(publicType), &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001540 recover();
1541
1542 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001543 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001544 }
1545
Olli Etuahoe7847b02015-03-16 11:56:12 +02001546 return intermediate.makeAggregate(symbol, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001547}
1548
Olli Etuahoe7847b02015-03-16 11:56:12 +02001549TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1550 const TSourceLoc &identifierLocation,
1551 const TString &identifier,
1552 const TSourceLoc &indexLocation,
1553 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001554{
Olli Etuahofa33d582015-04-09 14:33:12 +03001555 mDeferredSingleDeclarationErrorCheck = false;
1556
1557 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001558 recover();
1559
Olli Etuaho376f1b52015-04-13 13:23:41 +03001560 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001561 recover();
1562
Jamie Madillb98c3a82015-07-23 14:26:04 -04001563 if (arrayTypeErrorCheck(indexLocation, publicType) ||
1564 arrayQualifierErrorCheck(indexLocation, publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001565 {
1566 recover();
1567 }
1568
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001569 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001570
1571 int size;
1572 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1573 {
1574 recover();
1575 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001576 // Make the type an array even if size check failed.
1577 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1578 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001579
Olli Etuaho2935c582015-04-08 14:32:06 +03001580 TVariable *variable = nullptr;
1581 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001582 recover();
1583
Olli Etuahoe7847b02015-03-16 11:56:12 +02001584 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001585 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001586 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001587
Olli Etuahoe7847b02015-03-16 11:56:12 +02001588 return intermediate.makeAggregate(symbol, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001589}
1590
Jamie Madill06145232015-05-13 13:10:01 -04001591TIntermAggregate *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001592 const TSourceLoc &identifierLocation,
1593 const TString &identifier,
1594 const TSourceLoc &initLocation,
1595 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001596{
Olli Etuahofa33d582015-04-09 14:33:12 +03001597 mDeferredSingleDeclarationErrorCheck = false;
1598
1599 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001600 recover();
1601
Olli Etuahoe7847b02015-03-16 11:56:12 +02001602 TIntermNode *intermNode = nullptr;
1603 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04001604 {
1605 //
1606 // Build intermediate representation
1607 //
Olli Etuahoe7847b02015-03-16 11:56:12 +02001608 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001609 }
1610 else
1611 {
1612 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001613 return nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001614 }
1615}
1616
Jamie Madillb98c3a82015-07-23 14:26:04 -04001617TIntermAggregate *TParseContext::parseSingleArrayInitDeclaration(
1618 TPublicType &publicType,
1619 const TSourceLoc &identifierLocation,
1620 const TString &identifier,
1621 const TSourceLoc &indexLocation,
1622 TIntermTyped *indexExpression,
1623 const TSourceLoc &initLocation,
1624 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001625{
1626 mDeferredSingleDeclarationErrorCheck = false;
1627
1628 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1629 recover();
1630
Jamie Madillb98c3a82015-07-23 14:26:04 -04001631 if (arrayTypeErrorCheck(indexLocation, publicType) ||
1632 arrayQualifierErrorCheck(indexLocation, publicType))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001633 {
1634 recover();
1635 }
1636
1637 TPublicType arrayType(publicType);
1638
Olli Etuaho376f1b52015-04-13 13:23:41 +03001639 int size = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001640 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
1641 // the initializer.
1642 if (indexExpression != nullptr &&
1643 arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001644 {
1645 recover();
1646 }
1647 // Make the type an array even if size check failed.
1648 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1649 arrayType.setArraySize(size);
1650
1651 // initNode will correspond to the whole of "type b[n] = initializer".
1652 TIntermNode *initNode = nullptr;
1653 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1654 {
1655 return initNode ? intermediate.makeAggregate(initNode, initLocation) : nullptr;
1656 }
1657 else
1658 {
1659 recover();
1660 return nullptr;
1661 }
1662}
1663
Olli Etuahoe7847b02015-03-16 11:56:12 +02001664TIntermAggregate *TParseContext::parseInvariantDeclaration(const TSourceLoc &invariantLoc,
Jamie Madill47e3ec02014-08-20 16:38:33 -04001665 const TSourceLoc &identifierLoc,
1666 const TString *identifier,
1667 const TSymbol *symbol)
1668{
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001669 // invariant declaration
Jamie Madill47e3ec02014-08-20 16:38:33 -04001670 if (globalErrorCheck(invariantLoc, symbolTable.atGlobalLevel(), "invariant varying"))
1671 {
1672 recover();
1673 }
1674
1675 if (!symbol)
1676 {
1677 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
1678 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001679 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001680 }
1681 else
1682 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001683 const TString kGlFrontFacing("gl_FrontFacing");
1684 if (*identifier == kGlFrontFacing)
1685 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001686 error(identifierLoc, "identifier should not be declared as invariant",
1687 identifier->c_str());
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001688 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001689 return nullptr;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001690 }
Jamie Madill2c433252014-12-03 12:36:54 -05001691 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001692 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1693 ASSERT(variable);
1694 const TType &type = variable->getType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04001695 TIntermSymbol *intermSymbol =
1696 intermediate.addSymbol(variable->getUniqueId(), *identifier, type, identifierLoc);
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001697
1698 TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc);
1699 aggregate->setOp(EOpInvariantDeclaration);
1700 return aggregate;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001701 }
1702}
1703
Jamie Madillb98c3a82015-07-23 14:26:04 -04001704TIntermAggregate *TParseContext::parseDeclarator(TPublicType &publicType,
1705 TIntermAggregate *aggregateDeclaration,
1706 const TSourceLoc &identifierLocation,
1707 const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001708{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001709 // If the declaration starting this declarator list was empty (example: int,), some checks were
1710 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001711 if (mDeferredSingleDeclarationErrorCheck)
1712 {
1713 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1714 recover();
1715 mDeferredSingleDeclarationErrorCheck = false;
1716 }
1717
Jamie Madill0bd18df2013-06-20 11:55:52 -04001718 if (locationDeclaratorListCheck(identifierLocation, publicType))
1719 recover();
1720
Olli Etuaho376f1b52015-04-13 13:23:41 +03001721 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001722 recover();
1723
Olli Etuaho2935c582015-04-08 14:32:06 +03001724 TVariable *variable = nullptr;
1725 if (!declareVariable(identifierLocation, identifier, TType(publicType), &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001726 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001727
Jamie Madillb98c3a82015-07-23 14:26:04 -04001728 TIntermSymbol *symbol =
1729 intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001730 if (variable && symbol)
Jamie Madill502d66f2013-06-20 11:55:52 -04001731 symbol->setId(variable->getUniqueId());
1732
Olli Etuahoe7847b02015-03-16 11:56:12 +02001733 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001734}
1735
Jamie Madillb98c3a82015-07-23 14:26:04 -04001736TIntermAggregate *TParseContext::parseArrayDeclarator(TPublicType &publicType,
1737 TIntermAggregate *aggregateDeclaration,
1738 const TSourceLoc &identifierLocation,
1739 const TString &identifier,
1740 const TSourceLoc &arrayLocation,
1741 TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001742{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001743 // If the declaration starting this declarator list was empty (example: int,), some checks were
1744 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001745 if (mDeferredSingleDeclarationErrorCheck)
1746 {
1747 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1748 recover();
1749 mDeferredSingleDeclarationErrorCheck = false;
1750 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001751
Jamie Madill0bd18df2013-06-20 11:55:52 -04001752 if (locationDeclaratorListCheck(identifierLocation, publicType))
1753 recover();
1754
Olli Etuaho376f1b52015-04-13 13:23:41 +03001755 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001756 recover();
1757
Jamie Madillb98c3a82015-07-23 14:26:04 -04001758 if (arrayTypeErrorCheck(arrayLocation, publicType) ||
1759 arrayQualifierErrorCheck(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001760 {
1761 recover();
1762 }
Olli Etuaho93a90fd2015-04-07 18:14:07 +03001763 else
Jamie Madill502d66f2013-06-20 11:55:52 -04001764 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001765 TType arrayType = TType(publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04001766 int size;
1767 if (arraySizeErrorCheck(arrayLocation, indexExpression, size))
Olli Etuahoe7847b02015-03-16 11:56:12 +02001768 {
Jamie Madill502d66f2013-06-20 11:55:52 -04001769 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001770 }
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001771 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001772
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001773 TVariable *variable = nullptr;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001774 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001775 recover();
Jamie Madill502d66f2013-06-20 11:55:52 -04001776
Jamie Madillb98c3a82015-07-23 14:26:04 -04001777 TIntermSymbol *symbol =
1778 intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001779 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001780 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001781
1782 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001783 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001784
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001785 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001786}
1787
Jamie Madillb98c3a82015-07-23 14:26:04 -04001788TIntermAggregate *TParseContext::parseInitDeclarator(const TPublicType &publicType,
1789 TIntermAggregate *aggregateDeclaration,
1790 const TSourceLoc &identifierLocation,
1791 const TString &identifier,
1792 const TSourceLoc &initLocation,
1793 TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001794{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001795 // If the declaration starting this declarator list was empty (example: int,), some checks were
1796 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001797 if (mDeferredSingleDeclarationErrorCheck)
1798 {
1799 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1800 recover();
1801 mDeferredSingleDeclarationErrorCheck = false;
1802 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001803
Jamie Madill0bd18df2013-06-20 11:55:52 -04001804 if (locationDeclaratorListCheck(identifierLocation, publicType))
1805 recover();
1806
Olli Etuahoe7847b02015-03-16 11:56:12 +02001807 TIntermNode *intermNode = nullptr;
1808 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04001809 {
1810 //
1811 // build the intermediate representation
1812 //
1813 if (intermNode)
1814 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001815 return intermediate.growAggregate(aggregateDeclaration, intermNode, initLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001816 }
1817 else
1818 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001819 return aggregateDeclaration;
Jamie Madill502d66f2013-06-20 11:55:52 -04001820 }
1821 }
1822 else
1823 {
1824 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001825 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001826 }
1827}
1828
Jamie Madill06145232015-05-13 13:10:01 -04001829TIntermAggregate *TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001830 TIntermAggregate *aggregateDeclaration,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301831 const TSourceLoc &identifierLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001832 const TString &identifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301833 const TSourceLoc &indexLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001834 TIntermTyped *indexExpression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001835 const TSourceLoc &initLocation,
1836 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001837{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001838 // If the declaration starting this declarator list was empty (example: int,), some checks were
1839 // not performed.
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001840 if (mDeferredSingleDeclarationErrorCheck)
1841 {
1842 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1843 recover();
1844 mDeferredSingleDeclarationErrorCheck = false;
1845 }
1846
1847 if (locationDeclaratorListCheck(identifierLocation, publicType))
1848 recover();
1849
Jamie Madillb98c3a82015-07-23 14:26:04 -04001850 if (arrayTypeErrorCheck(indexLocation, publicType) ||
1851 arrayQualifierErrorCheck(indexLocation, publicType))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001852 {
1853 recover();
1854 }
1855
1856 TPublicType arrayType(publicType);
1857
Olli Etuaho376f1b52015-04-13 13:23:41 +03001858 int size = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001859 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
1860 // the initializer.
1861 if (indexExpression != nullptr &&
1862 arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001863 {
1864 recover();
1865 }
1866 // Make the type an array even if size check failed.
1867 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1868 arrayType.setArraySize(size);
1869
1870 // initNode will correspond to the whole of "b[n] = initializer".
1871 TIntermNode *initNode = nullptr;
1872 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1873 {
1874 if (initNode)
1875 {
1876 return intermediate.growAggregate(aggregateDeclaration, initNode, initLocation);
1877 }
1878 else
1879 {
1880 return aggregateDeclaration;
1881 }
1882 }
1883 else
1884 {
1885 recover();
1886 return nullptr;
1887 }
1888}
1889
Jamie Madilla295edf2013-06-06 11:56:48 -04001890void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1891{
1892 if (typeQualifier.qualifier != EvqUniform)
1893 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001894 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
1895 "global layout must be uniform");
Jamie Madilla295edf2013-06-06 11:56:48 -04001896 recover();
1897 return;
1898 }
1899
1900 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1901 ASSERT(!layoutQualifier.isEmpty());
1902
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001903 if (mShaderVersion < 300)
Jamie Madilla295edf2013-06-06 11:56:48 -04001904 {
1905 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1906 recover();
1907 return;
1908 }
1909
1910 if (layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1911 {
1912 recover();
1913 return;
1914 }
1915
Jamie Madill099c0f32013-06-20 11:55:52 -04001916 if (layoutQualifier.matrixPacking != EmpUnspecified)
1917 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001918 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04001919 }
1920
Geoff Langc6856732014-02-11 09:38:55 -05001921 if (layoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04001922 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001923 mDefaultBlockStorage = layoutQualifier.blockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04001924 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001925}
1926
Jamie Madill185fb402015-06-12 15:48:48 -04001927void TParseContext::parseFunctionPrototype(const TSourceLoc &location,
1928 TFunction *function,
1929 TIntermAggregate **aggregateOut)
1930{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001931 const TSymbol *builtIn =
1932 symbolTable.findBuiltIn(function->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04001933
1934 if (builtIn)
1935 {
1936 error(location, "built-in functions cannot be redefined", function->getName().c_str());
1937 recover();
1938 }
1939
Jamie Madillb98c3a82015-07-23 14:26:04 -04001940 TFunction *prevDec =
1941 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Jamie Madill185fb402015-06-12 15:48:48 -04001942 //
1943 // Note: 'prevDec' could be 'function' if this is the first time we've seen function
1944 // as it would have just been put in the symbol table. Otherwise, we're looking up
1945 // an earlier occurance.
1946 //
1947 if (prevDec->isDefined())
1948 {
1949 // Then this function already has a body.
1950 error(location, "function already has a body", function->getName().c_str());
1951 recover();
1952 }
1953 prevDec->setDefined();
1954 //
1955 // Overload the unique ID of the definition to be the same unique ID as the declaration.
1956 // Eventually we will probably want to have only a single definition and just swap the
1957 // arguments to be the definition's arguments.
1958 //
1959 function->setUniqueId(prevDec->getUniqueId());
1960
1961 // Raise error message if main function takes any parameters or return anything other than void
1962 if (function->getName() == "main")
1963 {
1964 if (function->getParamCount() > 0)
1965 {
1966 error(location, "function cannot take any parameter(s)", function->getName().c_str());
1967 recover();
1968 }
1969 if (function->getReturnType().getBasicType() != EbtVoid)
1970 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001971 error(location, "", function->getReturnType().getBasicString(),
1972 "main function cannot return a value");
Jamie Madill185fb402015-06-12 15:48:48 -04001973 recover();
1974 }
1975 }
1976
1977 //
1978 // Remember the return type for later checking for RETURN statements.
1979 //
1980 setCurrentFunctionType(&(prevDec->getReturnType()));
1981 setFunctionReturnsValue(false);
1982
1983 //
1984 // Insert parameters into the symbol table.
1985 // If the parameter has no name, it's not an error, just don't insert it
1986 // (could be used for unused args).
1987 //
1988 // Also, accumulate the list of parameters into the HIL, so lower level code
1989 // knows where to find parameters.
1990 //
1991 TIntermAggregate *paramNodes = new TIntermAggregate;
1992 for (size_t i = 0; i < function->getParamCount(); i++)
1993 {
1994 const TConstParameter &param = function->getParam(i);
1995 if (param.name != 0)
1996 {
1997 TVariable *variable = new TVariable(param.name, *param.type);
1998 //
1999 // Insert the parameters with name in the symbol table.
2000 //
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002001 if (!symbolTable.declare(variable))
2002 {
Jamie Madill185fb402015-06-12 15:48:48 -04002003 error(location, "redefinition", variable->getName().c_str());
2004 recover();
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002005 paramNodes = intermediate.growAggregate(
2006 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
2007 continue;
Jamie Madill185fb402015-06-12 15:48:48 -04002008 }
2009
2010 //
2011 // Add the parameter to the HIL
2012 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002013 TIntermSymbol *symbol = intermediate.addSymbol(
2014 variable->getUniqueId(), variable->getName(), variable->getType(), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002015
2016 paramNodes = intermediate.growAggregate(paramNodes, symbol, location);
2017 }
2018 else
2019 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002020 paramNodes = intermediate.growAggregate(
2021 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002022 }
2023 }
2024 intermediate.setAggregateOperator(paramNodes, EOpParameters, location);
2025 *aggregateOut = paramNodes;
2026 setLoopNestingLevel(0);
2027}
2028
Jamie Madillb98c3a82015-07-23 14:26:04 -04002029TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002030{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002031 //
2032 // Multiple declarations of the same function are allowed.
2033 //
2034 // If this is a definition, the definition production code will check for redefinitions
2035 // (we don't know at this point if it's a definition or not).
2036 //
2037 // Redeclarations are allowed. But, return types and parameter qualifiers must match.
2038 //
2039 TFunction *prevDec =
2040 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
2041 if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002042 {
2043 if (prevDec->getReturnType() != function->getReturnType())
2044 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002045 error(location, "overloaded functions must have the same return type",
Jamie Madill185fb402015-06-12 15:48:48 -04002046 function->getReturnType().getBasicString());
2047 recover();
2048 }
2049 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2050 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002051 if (prevDec->getParam(i).type->getQualifier() !=
2052 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002053 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002054 error(location, "overloaded functions must have the same parameter qualifiers",
Jamie Madill185fb402015-06-12 15:48:48 -04002055 function->getParam(i).type->getQualifierString());
2056 recover();
2057 }
2058 }
2059 }
2060
2061 //
2062 // Check for previously declared variables using the same name.
2063 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002064 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002065 if (prevSym)
2066 {
2067 if (!prevSym->isFunction())
2068 {
2069 error(location, "redefinition", function->getName().c_str(), "function");
2070 recover();
2071 }
2072 }
2073 else
2074 {
2075 // Insert the unmangled name to detect potential future redefinition as a variable.
Jamie Madillb98c3a82015-07-23 14:26:04 -04002076 TFunction *newFunction =
2077 new TFunction(NewPoolTString(function->getName().c_str()), &function->getReturnType());
Jamie Madill185fb402015-06-12 15:48:48 -04002078 symbolTable.getOuterLevel()->insertUnmangled(newFunction);
2079 }
2080
2081 // We're at the inner scope level of the function's arguments and body statement.
2082 // Add the function prototype to the surrounding scope instead.
2083 symbolTable.getOuterLevel()->insert(function);
2084
2085 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002086 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2087 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002088 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2089 //
2090 return function;
2091}
2092
Jamie Madill06145232015-05-13 13:10:01 -04002093TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002094{
Jamie Madill06145232015-05-13 13:10:01 -04002095 TPublicType publicType = publicTypeIn;
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 Etuaho5c0e0232015-11-11 15:55:59 +02002376 const TConstantUnion *unionArray = node->getUnionArrayPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002377 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 Etuaho5c0e0232015-11-11 15:55:59 +02002404 size_t arrayElementSize = arrayElementType.getObjectSize();
2405 const TConstantUnion *unionArray = node->getUnionArrayPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002406 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 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002440 const 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
Olli Etuaho5c0e0232015-11-11 15:55:59 +02002799 // Data of constant unions can't be changed, because it may be shared with other
2800 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
2801 // sanitized object.
Jamie Madillb11e2482015-05-04 14:21:22 -04002802 if (safeIndex != -1)
2803 {
2804 TConstantUnion *safeConstantUnion = new TConstantUnion();
2805 safeConstantUnion->setIConst(safeIndex);
2806 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
2807 }
2808
Jamie Madillb98c3a82015-07-23 14:26:04 -04002809 indexedExpression =
2810 intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002811 }
2812 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002813 else
2814 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002815 indexedExpression =
2816 intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
Jamie Madill7164cf42013-07-08 13:30:59 -04002817 }
2818
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002819 if (indexedExpression == 0)
2820 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002821 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002822 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002823 indexedExpression =
2824 intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002825 }
2826 else if (baseExpression->isArray())
2827 {
Olli Etuahob3fbd862015-09-30 17:55:02 +03002828 TType indexedType = baseExpression->getType();
2829 indexedType.clearArrayness();
2830 indexedExpression->setType(indexedType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002831 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002832 else if (baseExpression->isMatrix())
2833 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002834 indexedExpression->setType(TType(baseExpression->getBasicType(),
Olli Etuahob3fbd862015-09-30 17:55:02 +03002835 baseExpression->getPrecision(), EvqTemporary,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002836 static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002837 }
2838 else if (baseExpression->isVector())
2839 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002840 indexedExpression->setType(
Olli Etuahob3fbd862015-09-30 17:55:02 +03002841 TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002842 }
2843 else
2844 {
2845 indexedExpression->setType(baseExpression->getType());
2846 }
2847
Olli Etuahob3fbd862015-09-30 17:55:02 +03002848 if (baseExpression->getType().getQualifier() == EvqConst &&
2849 indexExpression->getType().getQualifier() == EvqConst)
2850 {
2851 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2852 }
2853 else
2854 {
2855 indexedExpression->getTypePointer()->setQualifier(EvqTemporary);
2856 }
2857
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002858 return indexedExpression;
2859}
2860
Jamie Madillb98c3a82015-07-23 14:26:04 -04002861TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
2862 const TSourceLoc &dotLocation,
2863 const TString &fieldString,
2864 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002865{
2866 TIntermTyped *indexedExpression = NULL;
2867
2868 if (baseExpression->isArray())
2869 {
2870 error(fieldLocation, "cannot apply dot operator to an array", ".");
2871 recover();
2872 }
2873
2874 if (baseExpression->isVector())
2875 {
2876 TVectorFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002877 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields,
2878 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002879 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002880 fields.num = 1;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002881 fields.offsets[0] = 0;
2882 recover();
2883 }
2884
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002885 if (baseExpression->getAsConstantUnion())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002886 {
2887 // constant folding for vector fields
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002888 indexedExpression = addConstVectorNode(fields, baseExpression->getAsConstantUnion(),
2889 fieldLocation, true);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002890 }
2891 else
2892 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302893 TIntermTyped *index = intermediate.addSwizzle(fields, fieldLocation);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002894 indexedExpression =
2895 intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002896 }
2897 if (indexedExpression == nullptr)
2898 {
2899 recover();
2900 indexedExpression = baseExpression;
2901 }
2902 else
2903 {
2904 // Note that the qualifier set here will be corrected later.
Jamie Madillb98c3a82015-07-23 14:26:04 -04002905 indexedExpression->setType(TType(baseExpression->getBasicType(),
2906 baseExpression->getPrecision(), EvqTemporary,
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002907 (unsigned char)(fieldString).size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002908 }
2909 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002910 else if (baseExpression->getBasicType() == EbtStruct)
2911 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002912 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302913 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04002914 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002915 {
2916 error(dotLocation, "structure has no fields", "Internal Error");
2917 recover();
2918 indexedExpression = baseExpression;
2919 }
2920 else
2921 {
2922 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002923 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002924 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002925 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002926 {
2927 fieldFound = true;
2928 break;
2929 }
2930 }
2931 if (fieldFound)
2932 {
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002933 if (baseExpression->getAsConstantUnion())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002934 {
2935 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2936 if (indexedExpression == 0)
2937 {
2938 recover();
2939 indexedExpression = baseExpression;
2940 }
2941 else
2942 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002943 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002944 }
2945 }
2946 else
2947 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002948 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002949 unionArray->setIConst(i);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002950 TIntermTyped *index = intermediate.addConstantUnion(
2951 unionArray, *fields[i]->type(), fieldLocation);
2952 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression,
2953 index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002954 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002955 }
2956 }
2957 else
2958 {
2959 error(dotLocation, " no such field in structure", fieldString.c_str());
2960 recover();
2961 indexedExpression = baseExpression;
2962 }
2963 }
2964 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002965 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002966 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002967 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302968 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04002969 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002970 {
2971 error(dotLocation, "interface block has no fields", "Internal Error");
2972 recover();
2973 indexedExpression = baseExpression;
2974 }
2975 else
2976 {
2977 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002978 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002979 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002980 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002981 {
2982 fieldFound = true;
2983 break;
2984 }
2985 }
2986 if (fieldFound)
2987 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002988 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002989 unionArray->setIConst(i);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002990 TIntermTyped *index =
2991 intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
2992 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock,
2993 baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002994 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002995 }
2996 else
2997 {
2998 error(dotLocation, " no such field in interface block", fieldString.c_str());
2999 recover();
3000 indexedExpression = baseExpression;
3001 }
3002 }
3003 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003004 else
3005 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003006 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003007 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03003008 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303009 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003010 }
3011 else
3012 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303013 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003014 " field selection requires structure, vector, or interface block on left hand "
3015 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303016 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003017 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003018 recover();
3019 indexedExpression = baseExpression;
3020 }
3021
Olli Etuahob1edc4f2015-11-02 17:20:03 +02003022 if (baseExpression->getQualifier() == EvqConst)
3023 {
3024 indexedExpression->getTypePointer()->setQualifier(EvqConst);
3025 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003026 else
3027 {
3028 indexedExpression->getTypePointer()->setQualifier(EvqTemporary);
3029 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02003030
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003031 return indexedExpression;
3032}
3033
Jamie Madillb98c3a82015-07-23 14:26:04 -04003034TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3035 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003036{
Jamie Madilla5efff92013-06-06 11:56:47 -04003037 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003038
Jamie Madillb98c3a82015-07-23 14:26:04 -04003039 qualifier.location = -1;
Jamie Madilla5efff92013-06-06 11:56:47 -04003040 qualifier.matrixPacking = EmpUnspecified;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003041 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003042
3043 if (qualifierType == "shared")
3044 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003045 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003046 }
3047 else if (qualifierType == "packed")
3048 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003049 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003050 }
3051 else if (qualifierType == "std140")
3052 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003053 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003054 }
3055 else if (qualifierType == "row_major")
3056 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003057 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003058 }
3059 else if (qualifierType == "column_major")
3060 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003061 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003062 }
3063 else if (qualifierType == "location")
3064 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003065 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
3066 "location requires an argument");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003067 recover();
3068 }
3069 else
3070 {
3071 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
3072 recover();
3073 }
3074
Jamie Madilla5efff92013-06-06 11:56:47 -04003075 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003076}
3077
Jamie Madillb98c3a82015-07-23 14:26:04 -04003078TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3079 const TSourceLoc &qualifierTypeLine,
3080 const TString &intValueString,
3081 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303082 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003083{
Jamie Madilla5efff92013-06-06 11:56:47 -04003084 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003085
Jamie Madillb98c3a82015-07-23 14:26:04 -04003086 qualifier.location = -1;
Jamie Madilla5efff92013-06-06 11:56:47 -04003087 qualifier.matrixPacking = EmpUnspecified;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003088 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003089
3090 if (qualifierType != "location")
3091 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303092 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
3093 "only location may have arguments");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003094 recover();
3095 }
3096 else
3097 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003098 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003099 if (intValue < 0)
3100 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003101 error(intValueLine, "out of range:", intValueString.c_str(),
3102 "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003103 recover();
3104 }
3105 else
3106 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003107 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003108 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003109 }
3110
Jamie Madilla5efff92013-06-06 11:56:47 -04003111 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003112}
3113
Jamie Madillb98c3a82015-07-23 14:26:04 -04003114TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
3115 TLayoutQualifier rightQualifier)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003116{
Jamie Madilla5efff92013-06-06 11:56:47 -04003117 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003118
Jamie Madilla5efff92013-06-06 11:56:47 -04003119 if (rightQualifier.location != -1)
3120 {
3121 joinedQualifier.location = rightQualifier.location;
3122 }
3123 if (rightQualifier.matrixPacking != EmpUnspecified)
3124 {
3125 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
3126 }
3127 if (rightQualifier.blockStorage != EbsUnspecified)
3128 {
3129 joinedQualifier.blockStorage = rightQualifier.blockStorage;
3130 }
3131
3132 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003133}
3134
Arun Patole7e7e68d2015-05-22 12:02:25 +05303135TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc,
3136 TQualifier interpolationQualifier,
3137 const TSourceLoc &storageLoc,
3138 TQualifier storageQualifier)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003139{
3140 TQualifier mergedQualifier = EvqSmoothIn;
3141
Arun Patole7e7e68d2015-05-22 12:02:25 +05303142 if (storageQualifier == EvqFragmentIn)
3143 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003144 if (interpolationQualifier == EvqSmooth)
3145 mergedQualifier = EvqSmoothIn;
3146 else if (interpolationQualifier == EvqFlat)
3147 mergedQualifier = EvqFlatIn;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003148 else
3149 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003150 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303151 else if (storageQualifier == EvqCentroidIn)
3152 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003153 if (interpolationQualifier == EvqSmooth)
3154 mergedQualifier = EvqCentroidIn;
3155 else if (interpolationQualifier == EvqFlat)
3156 mergedQualifier = EvqFlatIn;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003157 else
3158 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003159 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303160 else if (storageQualifier == EvqVertexOut)
3161 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003162 if (interpolationQualifier == EvqSmooth)
3163 mergedQualifier = EvqSmoothOut;
3164 else if (interpolationQualifier == EvqFlat)
3165 mergedQualifier = EvqFlatOut;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003166 else
3167 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003168 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303169 else if (storageQualifier == EvqCentroidOut)
3170 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003171 if (interpolationQualifier == EvqSmooth)
3172 mergedQualifier = EvqCentroidOut;
3173 else if (interpolationQualifier == EvqFlat)
3174 mergedQualifier = EvqFlatOut;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003175 else
3176 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003177 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303178 else
3179 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003180 error(interpolationLoc,
3181 "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303182 getInterpolationString(interpolationQualifier));
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003183 recover();
3184
3185 mergedQualifier = storageQualifier;
3186 }
3187
3188 TPublicType type;
3189 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
3190 return type;
3191}
3192
Jamie Madillb98c3a82015-07-23 14:26:04 -04003193TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3194 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003195{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03003196 if (voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier.type))
3197 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003198 recover();
3199 }
3200
Arun Patole7e7e68d2015-05-22 12:02:25 +05303201 for (unsigned int i = 0; i < fieldList->size(); ++i)
3202 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003203 //
3204 // Careful not to replace already known aspects of type, like array-ness
3205 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303206 TType *type = (*fieldList)[i]->type();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003207 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003208 type->setPrimarySize(typeSpecifier.primarySize);
3209 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003210 type->setPrecision(typeSpecifier.precision);
3211 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003212 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003213
3214 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303215 if (type->isArray())
3216 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003217 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
3218 recover();
3219 }
3220 if (typeSpecifier.array)
3221 type->setArraySize(typeSpecifier.arraySize);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303222 if (typeSpecifier.userDef)
3223 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003224 type->setStruct(typeSpecifier.userDef->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003225 }
3226
Arun Patole7e7e68d2015-05-22 12:02:25 +05303227 if (structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i]))
3228 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003229 recover();
3230 }
3231 }
3232
Jamie Madill98493dd2013-07-08 14:39:03 -04003233 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003234}
3235
Jamie Madillb98c3a82015-07-23 14:26:04 -04003236TPublicType TParseContext::addStructure(const TSourceLoc &structLine,
3237 const TSourceLoc &nameLine,
3238 const TString *structName,
3239 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003240{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303241 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003242 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003243
Jamie Madill9b820842015-02-12 10:40:10 -05003244 // Store a bool in the struct if we're at global scope, to allow us to
3245 // skip the local struct scoping workaround in HLSL.
Jamie Madillb960cc42015-02-12 15:33:20 +00003246 structure->setUniqueId(TSymbolTable::nextUniqueId());
Jamie Madill9b820842015-02-12 10:40:10 -05003247 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003248
Jamie Madill98493dd2013-07-08 14:39:03 -04003249 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003250 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003251 if (reservedErrorCheck(nameLine, *structName))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003252 {
3253 recover();
3254 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303255 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
3256 if (!symbolTable.declare(userTypeDef))
3257 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003258 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003259 recover();
3260 }
3261 }
3262
3263 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04003264 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003265 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003266 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04003267 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003268 switch (qualifier)
3269 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003270 case EvqGlobal:
3271 case EvqTemporary:
3272 break;
3273 default:
3274 error(field.line(), "invalid qualifier on struct member",
3275 getQualifierString(qualifier));
3276 recover();
3277 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003278 }
3279 }
3280
3281 TPublicType publicType;
3282 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003283 publicType.userDef = structureType;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003284 exitStructDeclaration();
3285
3286 return publicType;
3287}
3288
Jamie Madillb98c3a82015-07-23 14:26:04 -04003289TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
3290 TIntermAggregate *statementList,
3291 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02003292{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003293 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003294 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02003295 init->isVector())
3296 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003297 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
3298 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003299 recover();
3300 return nullptr;
3301 }
3302
Olli Etuahoac5274d2015-02-20 10:19:08 +02003303 if (statementList)
3304 {
3305 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
3306 {
3307 recover();
3308 return nullptr;
3309 }
3310 }
3311
Olli Etuahoa3a36662015-02-17 13:46:51 +02003312 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3313 if (node == nullptr)
3314 {
3315 error(loc, "erroneous switch statement", "switch");
3316 recover();
3317 return nullptr;
3318 }
3319 return node;
3320}
3321
3322TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3323{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003324 if (mSwitchNestingLevel == 0)
3325 {
3326 error(loc, "case labels need to be inside switch statements", "case");
3327 recover();
3328 return nullptr;
3329 }
3330 if (condition == nullptr)
3331 {
3332 error(loc, "case label must have a condition", "case");
3333 recover();
3334 return nullptr;
3335 }
3336 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04003337 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02003338 {
3339 error(condition->getLine(), "case label must be a scalar integer", "case");
3340 recover();
3341 }
3342 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003343 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
3344 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
3345 // fold in case labels.
3346 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02003347 {
3348 error(condition->getLine(), "case label must be constant", "case");
3349 recover();
3350 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003351 TIntermCase *node = intermediate.addCase(condition, loc);
3352 if (node == nullptr)
3353 {
3354 error(loc, "erroneous case statement", "case");
3355 recover();
3356 return nullptr;
3357 }
3358 return node;
3359}
3360
3361TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3362{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003363 if (mSwitchNestingLevel == 0)
3364 {
3365 error(loc, "default labels need to be inside switch statements", "default");
3366 recover();
3367 return nullptr;
3368 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003369 TIntermCase *node = intermediate.addCase(nullptr, loc);
3370 if (node == nullptr)
3371 {
3372 error(loc, "erroneous default statement", "default");
3373 recover();
3374 return nullptr;
3375 }
3376 return node;
3377}
3378
Jamie Madillb98c3a82015-07-23 14:26:04 -04003379TIntermTyped *TParseContext::createUnaryMath(TOperator op,
3380 TIntermTyped *child,
3381 const TSourceLoc &loc,
3382 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003383{
3384 if (child == nullptr)
3385 {
3386 return nullptr;
3387 }
3388
3389 switch (op)
3390 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003391 case EOpLogicalNot:
3392 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
3393 child->isVector())
3394 {
3395 return nullptr;
3396 }
3397 break;
3398 case EOpBitwiseNot:
3399 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3400 child->isMatrix() || child->isArray())
3401 {
3402 return nullptr;
3403 }
3404 break;
3405 case EOpPostIncrement:
3406 case EOpPreIncrement:
3407 case EOpPostDecrement:
3408 case EOpPreDecrement:
3409 case EOpNegative:
3410 case EOpPositive:
3411 if (child->getBasicType() == EbtStruct || child->getBasicType() == EbtBool ||
3412 child->isArray())
3413 {
3414 return nullptr;
3415 }
3416 // Operators for built-ins are already type checked against their prototype.
3417 default:
3418 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003419 }
3420
Olli Etuahof6c694b2015-03-26 14:50:53 +02003421 return intermediate.addUnaryMath(op, child, loc, funcReturnType);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003422}
3423
Olli Etuaho09b22472015-02-11 11:47:26 +02003424TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3425{
Olli Etuahof6c694b2015-03-26 14:50:53 +02003426 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003427 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003428 {
3429 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
3430 recover();
3431 return child;
3432 }
3433 return node;
3434}
3435
Jamie Madillb98c3a82015-07-23 14:26:04 -04003436TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
3437 TIntermTyped *child,
3438 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003439{
3440 if (lValueErrorCheck(loc, GetOperatorString(op), child))
3441 recover();
3442 return addUnaryMath(op, child, loc);
3443}
3444
Jamie Madillb98c3a82015-07-23 14:26:04 -04003445bool TParseContext::binaryOpCommonCheck(TOperator op,
3446 TIntermTyped *left,
3447 TIntermTyped *right,
3448 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003449{
3450 if (left->isArray() || right->isArray())
3451 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003452 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003453 {
3454 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3455 return false;
3456 }
3457
3458 if (left->isArray() != right->isArray())
3459 {
3460 error(loc, "array / non-array mismatch", GetOperatorString(op));
3461 return false;
3462 }
3463
3464 switch (op)
3465 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003466 case EOpEqual:
3467 case EOpNotEqual:
3468 case EOpAssign:
3469 case EOpInitialize:
3470 break;
3471 default:
3472 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3473 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02003474 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003475 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003476 if (left->getArraySize() != right->getArraySize())
3477 {
3478 error(loc, "array size mismatch", GetOperatorString(op));
3479 return false;
3480 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003481 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003482
3483 // Check ops which require integer / ivec parameters
3484 bool isBitShift = false;
3485 switch (op)
3486 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003487 case EOpBitShiftLeft:
3488 case EOpBitShiftRight:
3489 case EOpBitShiftLeftAssign:
3490 case EOpBitShiftRightAssign:
3491 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3492 // check that the basic type is an integer type.
3493 isBitShift = true;
3494 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3495 {
3496 return false;
3497 }
3498 break;
3499 case EOpBitwiseAnd:
3500 case EOpBitwiseXor:
3501 case EOpBitwiseOr:
3502 case EOpBitwiseAndAssign:
3503 case EOpBitwiseXorAssign:
3504 case EOpBitwiseOrAssign:
3505 // It is enough to check the type of only one operand, since later it
3506 // is checked that the operand types match.
3507 if (!IsInteger(left->getBasicType()))
3508 {
3509 return false;
3510 }
3511 break;
3512 default:
3513 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003514 }
3515
3516 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3517 // So the basic type should usually match.
3518 if (!isBitShift && left->getBasicType() != right->getBasicType())
3519 {
3520 return false;
3521 }
3522
Olli Etuaho9dd217b2015-03-20 14:24:31 +02003523 // Check that type sizes match exactly on ops that require that.
Olli Etuahoff699002015-03-23 14:38:42 +02003524 // Also check restrictions for structs that contain arrays or samplers.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003525 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003526 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003527 case EOpAssign:
3528 case EOpInitialize:
3529 case EOpEqual:
3530 case EOpNotEqual:
3531 // ESSL 1.00 sections 5.7, 5.8, 5.9
3532 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
3533 {
3534 error(loc, "undefined operation for structs containing arrays",
3535 GetOperatorString(op));
3536 return false;
3537 }
3538 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3539 // we interpret the spec so that this extends to structs containing samplers,
3540 // similarly to ESSL 1.00 spec.
3541 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
3542 left->getType().isStructureContainingSamplers())
3543 {
3544 error(loc, "undefined operation for structs containing samplers",
3545 GetOperatorString(op));
3546 return false;
3547 }
3548 case EOpLessThan:
3549 case EOpGreaterThan:
3550 case EOpLessThanEqual:
3551 case EOpGreaterThanEqual:
3552 if ((left->getNominalSize() != right->getNominalSize()) ||
3553 (left->getSecondarySize() != right->getSecondarySize()))
3554 {
3555 return false;
3556 }
3557 default:
3558 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003559 }
3560
Olli Etuahod6b14282015-03-17 14:31:35 +02003561 return true;
3562}
3563
Jamie Madillb98c3a82015-07-23 14:26:04 -04003564TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
3565 TIntermTyped *left,
3566 TIntermTyped *right,
3567 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02003568{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003569 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003570 return nullptr;
3571
Olli Etuahofc1806e2015-03-17 13:03:11 +02003572 switch (op)
3573 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003574 case EOpEqual:
3575 case EOpNotEqual:
3576 break;
3577 case EOpLessThan:
3578 case EOpGreaterThan:
3579 case EOpLessThanEqual:
3580 case EOpGreaterThanEqual:
3581 ASSERT(!left->isArray() && !right->isArray());
3582 if (left->isMatrix() || left->isVector() || left->getBasicType() == EbtStruct)
3583 {
3584 return nullptr;
3585 }
3586 break;
3587 case EOpLogicalOr:
3588 case EOpLogicalXor:
3589 case EOpLogicalAnd:
3590 ASSERT(!left->isArray() && !right->isArray());
3591 if (left->getBasicType() != EbtBool || left->isMatrix() || left->isVector())
3592 {
3593 return nullptr;
3594 }
3595 break;
3596 case EOpAdd:
3597 case EOpSub:
3598 case EOpDiv:
3599 case EOpMul:
3600 ASSERT(!left->isArray() && !right->isArray());
3601 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool)
3602 {
3603 return nullptr;
3604 }
3605 break;
3606 case EOpIMod:
3607 ASSERT(!left->isArray() && !right->isArray());
3608 // Note that this is only for the % operator, not for mod()
3609 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool ||
3610 left->getBasicType() == EbtFloat)
3611 {
3612 return nullptr;
3613 }
3614 break;
3615 // Note that for bitwise ops, type checking is done in promote() to
3616 // share code between ops and compound assignment
3617 default:
3618 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003619 }
3620
Olli Etuahofc1806e2015-03-17 13:03:11 +02003621 return intermediate.addBinaryMath(op, left, right, loc);
3622}
3623
Jamie Madillb98c3a82015-07-23 14:26:04 -04003624TIntermTyped *TParseContext::addBinaryMath(TOperator op,
3625 TIntermTyped *left,
3626 TIntermTyped *right,
3627 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003628{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003629 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003630 if (node == 0)
3631 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003632 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3633 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003634 recover();
3635 return left;
3636 }
3637 return node;
3638}
3639
Jamie Madillb98c3a82015-07-23 14:26:04 -04003640TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
3641 TIntermTyped *left,
3642 TIntermTyped *right,
3643 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003644{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003645 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003646 if (node == 0)
3647 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003648 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3649 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003650 recover();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003651 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02003652 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003653 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
3654 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003655 }
3656 return node;
3657}
3658
Jamie Madillb98c3a82015-07-23 14:26:04 -04003659TIntermTyped *TParseContext::createAssign(TOperator op,
3660 TIntermTyped *left,
3661 TIntermTyped *right,
3662 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003663{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003664 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003665 {
3666 return intermediate.addAssign(op, left, right, loc);
3667 }
3668 return nullptr;
3669}
3670
Jamie Madillb98c3a82015-07-23 14:26:04 -04003671TIntermTyped *TParseContext::addAssign(TOperator op,
3672 TIntermTyped *left,
3673 TIntermTyped *right,
3674 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003675{
3676 TIntermTyped *node = createAssign(op, left, right, loc);
3677 if (node == nullptr)
3678 {
3679 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
3680 recover();
3681 return left;
3682 }
3683 return node;
3684}
3685
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003686TIntermTyped *TParseContext::addComma(TIntermTyped *left,
3687 TIntermTyped *right,
3688 const TSourceLoc &loc)
3689{
Olli Etuaho15200042015-11-04 16:56:31 +02003690 return intermediate.addComma(left, right, loc, mShaderVersion);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003691}
3692
Olli Etuaho49300862015-02-20 14:54:49 +02003693TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3694{
3695 switch (op)
3696 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003697 case EOpContinue:
3698 if (mLoopNestingLevel <= 0)
3699 {
3700 error(loc, "continue statement only allowed in loops", "");
3701 recover();
3702 }
3703 break;
3704 case EOpBreak:
3705 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
3706 {
3707 error(loc, "break statement only allowed in loops and switch statements", "");
3708 recover();
3709 }
3710 break;
3711 case EOpReturn:
3712 if (mCurrentFunctionType->getBasicType() != EbtVoid)
3713 {
3714 error(loc, "non-void function must return a value", "return");
3715 recover();
3716 }
3717 break;
3718 default:
3719 // No checks for discard
3720 break;
Olli Etuaho49300862015-02-20 14:54:49 +02003721 }
3722 return intermediate.addBranch(op, loc);
3723}
3724
Jamie Madillb98c3a82015-07-23 14:26:04 -04003725TIntermBranch *TParseContext::addBranch(TOperator op,
3726 TIntermTyped *returnValue,
3727 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02003728{
3729 ASSERT(op == EOpReturn);
3730 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003731 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02003732 {
3733 error(loc, "void function cannot return a value", "return");
3734 recover();
3735 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003736 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02003737 {
3738 error(loc, "function return is not matching type:", "return");
3739 recover();
3740 }
3741 return intermediate.addBranch(op, returnValue, loc);
3742}
3743
Jamie Madillb98c3a82015-07-23 14:26:04 -04003744TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
3745 TIntermNode *paramNode,
3746 TIntermNode *thisNode,
3747 const TSourceLoc &loc,
3748 bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003749{
Jamie Madillb98c3a82015-07-23 14:26:04 -04003750 *fatalError = false;
3751 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003752 TIntermTyped *callNode = nullptr;
3753
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003754 if (thisNode != nullptr)
3755 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003756 TConstantUnion *unionArray = new TConstantUnion[1];
Jamie Madillb98c3a82015-07-23 14:26:04 -04003757 int arraySize = 0;
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003758 TIntermTyped *typedThis = thisNode->getAsTyped();
3759 if (fnCall->getName() != "length")
3760 {
3761 error(loc, "invalid method", fnCall->getName().c_str());
3762 recover();
3763 }
3764 else if (paramNode != nullptr)
3765 {
3766 error(loc, "method takes no parameters", "length");
3767 recover();
3768 }
3769 else if (typedThis == nullptr || !typedThis->isArray())
3770 {
3771 error(loc, "length can only be called on arrays", "length");
3772 recover();
3773 }
3774 else
3775 {
Olli Etuaho96e67382015-04-23 14:27:02 +03003776 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03003777 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003778 {
Olli Etuaho39282e12015-04-23 15:41:48 +03003779 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003780 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03003781 // (func()).length()
3782 // (int[3](0, 1, 2)).length()
Jamie Madillb98c3a82015-07-23 14:26:04 -04003783 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
3784 // expression.
3785 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
3786 // spec section 5.9 which allows "An array, vector or matrix expression with the
3787 // length method applied".
3788 error(loc, "length can only be called on array names, not on array expressions",
3789 "length");
Olli Etuaho39282e12015-04-23 15:41:48 +03003790 recover();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003791 }
3792 }
Olli Etuaho96e67382015-04-23 14:27:02 +03003793 unionArray->setIConst(arraySize);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003794 callNode =
3795 intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003796 }
3797 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003798 {
3799 //
3800 // Then this should be a constructor.
3801 // Don't go through the symbol table for constructors.
3802 // Their parameters will be verified algorithmically.
3803 //
3804 TType type(EbtVoid, EbpUndefined); // use this to get the type back
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003805 if (!constructorErrorCheck(loc, paramNode, *fnCall, op, &type))
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003806 {
3807 //
3808 // It's a constructor, of type 'type'.
3809 //
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003810 callNode = addConstructor(paramNode, &type, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003811 }
Olli Etuaho72ba85b2015-03-04 14:23:26 +02003812
3813 if (callNode == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003814 {
3815 recover();
3816 callNode = intermediate.setAggregateOperator(nullptr, op, loc);
3817 }
3818 callNode->setType(type);
3819 }
3820 else
3821 {
3822 //
3823 // Not a constructor. Find it in the symbol table.
3824 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303825 const TFunction *fnCandidate;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003826 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003827 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003828 if (fnCandidate)
3829 {
3830 //
3831 // A declared function.
3832 //
3833 if (builtIn && !fnCandidate->getExtension().empty() &&
3834 extensionErrorCheck(loc, fnCandidate->getExtension()))
3835 {
3836 recover();
3837 }
3838 op = fnCandidate->getBuiltInOp();
3839 if (builtIn && op != EOpNull)
3840 {
3841 //
3842 // A function call mapped to a built-in operation.
3843 //
3844 if (fnCandidate->getParamCount() == 1)
3845 {
3846 //
3847 // Treat it like a built-in unary operator.
3848 //
Olli Etuaho15c2ac32015-11-09 15:51:43 +02003849 TIntermAggregate *paramAgg = paramNode->getAsAggregate();
3850 paramNode = paramAgg->getSequence()->front();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003851 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc,
3852 &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003853 if (callNode == nullptr)
3854 {
3855 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003856 extraInfoStream
3857 << "built in unary operator function. Type: "
3858 << static_cast<TIntermTyped *>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003859 std::string extraInfo = extraInfoStream.str();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003860 error(paramNode->getLine(), " wrong operand type", "Internal Error",
3861 extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003862 *fatalError = true;
3863 return nullptr;
3864 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003865 }
3866 else
3867 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003868 TIntermAggregate *aggregate =
3869 intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003870 aggregate->setType(fnCandidate->getReturnType());
3871 aggregate->setPrecisionFromChildren();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02003872 if (aggregate->areChildrenConstQualified())
3873 {
3874 aggregate->getTypePointer()->setQualifier(EvqConst);
3875 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003876
3877 // Some built-in functions have out parameters too.
3878 functionCallLValueErrorCheck(fnCandidate, aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05303879
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003880 // See if we can constant fold a built-in. Note that this may be possible even
3881 // if it is not const-qualified.
Olli Etuahob43846e2015-06-02 18:18:57 +03003882 TIntermTyped *foldedNode = intermediate.foldAggregateBuiltIn(aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05303883 if (foldedNode)
3884 {
Arun Patole274f0702015-05-05 13:33:30 +05303885 callNode = foldedNode;
3886 }
Olli Etuahob43846e2015-06-02 18:18:57 +03003887 else
3888 {
3889 callNode = aggregate;
3890 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003891 }
3892 }
3893 else
3894 {
3895 // This is a real function call
Jamie Madillb98c3a82015-07-23 14:26:04 -04003896 TIntermAggregate *aggregate =
3897 intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003898 aggregate->setType(fnCandidate->getReturnType());
3899
Jamie Madillb98c3a82015-07-23 14:26:04 -04003900 // this is how we know whether the given function is a builtIn function or a user
3901 // defined function
3902 // if builtIn == false, it's a userDefined -> could be an overloaded
3903 // builtIn function also
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003904 // if builtIn == true, it's definitely a builtIn function with EOpNull
3905 if (!builtIn)
3906 aggregate->setUserDefined();
3907 aggregate->setName(fnCandidate->getMangledName());
Corentin Wallez71d147f2015-02-11 11:15:24 -08003908 aggregate->setFunctionId(fnCandidate->getUniqueId());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003909
3910 // This needs to happen after the name is set
3911 if (builtIn)
3912 aggregate->setBuiltInFunctionPrecision();
3913
3914 callNode = aggregate;
3915
3916 functionCallLValueErrorCheck(fnCandidate, aggregate);
3917 }
3918 }
3919 else
3920 {
3921 // error message was put out by findFunction()
3922 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003923 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003924 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003925 callNode = intermediate.addConstantUnion(unionArray,
3926 TType(EbtFloat, EbpUndefined, EvqConst), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003927 recover();
3928 }
3929 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003930 return callNode;
3931}
3932
Jamie Madillb98c3a82015-07-23 14:26:04 -04003933TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
3934 TIntermTyped *trueBlock,
3935 TIntermTyped *falseBlock,
Olli Etuaho52901742015-04-15 13:42:45 +03003936 const TSourceLoc &loc)
3937{
3938 if (boolErrorCheck(loc, cond))
3939 recover();
3940
3941 if (trueBlock->getType() != falseBlock->getType())
3942 {
3943 binaryOpError(loc, ":", trueBlock->getCompleteString(), falseBlock->getCompleteString());
3944 recover();
3945 return falseBlock;
3946 }
Olli Etuahoa2d53032015-04-15 14:14:44 +03003947 // ESSL1 sections 5.2 and 5.7:
3948 // ESSL3 section 5.7:
3949 // Ternary operator is not among the operators allowed for structures/arrays.
3950 if (trueBlock->isArray() || trueBlock->getBasicType() == EbtStruct)
3951 {
3952 error(loc, "ternary operator is not allowed for structures or arrays", ":");
3953 recover();
3954 return falseBlock;
3955 }
Olli Etuaho52901742015-04-15 13:42:45 +03003956 return intermediate.addSelection(cond, trueBlock, falseBlock, loc);
3957}
Olli Etuaho49300862015-02-20 14:54:49 +02003958
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003959//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003960// Parse an array of strings using yyparse.
3961//
3962// Returns 0 for success.
3963//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003964int PaParseStrings(size_t count,
3965 const char *const string[],
3966 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05303967 TParseContext *context)
3968{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003969 if ((count == 0) || (string == NULL))
3970 return 1;
3971
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003972 if (glslang_initialize(context))
3973 return 1;
3974
alokp@chromium.org408c45e2012-04-05 15:54:43 +00003975 int error = glslang_scan(count, string, length, context);
3976 if (!error)
3977 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003978
alokp@chromium.org73bc2982012-06-19 18:48:05 +00003979 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00003980
alokp@chromium.org6b495712012-06-29 00:06:58 +00003981 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003982}