blob: 5cae37cec440320fa0347df0785e56d66318751a [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Jamie Madill6b9cb252013-10-17 10:45:47 -04007#include "compiler/translator/ParseContext.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +00008
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00009#include <stdarg.h>
apatrick@chromium.org8187fa82010-06-15 22:09:28 +000010#include <stdio.h>
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000011
daniel@transgaming.comb401a922012-10-26 18:58:24 +000012#include "compiler/preprocessor/SourceLocation.h"
Dmitry Skiba01971112015-07-10 14:54:00 -040013#include "compiler/translator/Cache.h"
Olli Etuahoac5274d2015-02-20 10:19:08 +020014#include "compiler/translator/glslang.h"
15#include "compiler/translator/ValidateSwitch.h"
Olli Etuahob0c645e2015-05-12 14:25:36 +030016#include "compiler/translator/ValidateGlobalInitializer.h"
Olli Etuaho37ad4742015-04-27 13:18:50 +030017#include "compiler/translator/util.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000018
alokp@chromium.org8b851c62012-06-15 16:25:11 +000019///////////////////////////////////////////////////////////////////////
20//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000021// Sub- vector and matrix fields
22//
23////////////////////////////////////////////////////////////////////////
24
25//
26// Look at a '.' field selector string and change it into offsets
27// for a vector.
28//
Jamie Madillb98c3a82015-07-23 14:26:04 -040029bool TParseContext::parseVectorFields(const TString &compString,
30 int vecSize,
31 TVectorFields &fields,
Arun Patole7e7e68d2015-05-22 12:02:25 +053032 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000033{
Jamie Madillb98c3a82015-07-23 14:26:04 -040034 fields.num = (int)compString.size();
Arun Patole7e7e68d2015-05-22 12:02:25 +053035 if (fields.num > 4)
36 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000037 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000038 return false;
39 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000040
Jamie Madillb98c3a82015-07-23 14:26:04 -040041 enum
42 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000043 exyzw,
44 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +000045 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000046 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000047
Arun Patole7e7e68d2015-05-22 12:02:25 +053048 for (int i = 0; i < fields.num; ++i)
49 {
50 switch (compString[i])
51 {
Jamie Madillb98c3a82015-07-23 14:26:04 -040052 case 'x':
53 fields.offsets[i] = 0;
54 fieldSet[i] = exyzw;
55 break;
56 case 'r':
57 fields.offsets[i] = 0;
58 fieldSet[i] = ergba;
59 break;
60 case 's':
61 fields.offsets[i] = 0;
62 fieldSet[i] = estpq;
63 break;
64 case 'y':
65 fields.offsets[i] = 1;
66 fieldSet[i] = exyzw;
67 break;
68 case 'g':
69 fields.offsets[i] = 1;
70 fieldSet[i] = ergba;
71 break;
72 case 't':
73 fields.offsets[i] = 1;
74 fieldSet[i] = estpq;
75 break;
76 case 'z':
77 fields.offsets[i] = 2;
78 fieldSet[i] = exyzw;
79 break;
80 case 'b':
81 fields.offsets[i] = 2;
82 fieldSet[i] = ergba;
83 break;
84 case 'p':
85 fields.offsets[i] = 2;
86 fieldSet[i] = estpq;
87 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053088
Jamie Madillb98c3a82015-07-23 14:26:04 -040089 case 'w':
90 fields.offsets[i] = 3;
91 fieldSet[i] = exyzw;
92 break;
93 case 'a':
94 fields.offsets[i] = 3;
95 fieldSet[i] = ergba;
96 break;
97 case 'q':
98 fields.offsets[i] = 3;
99 fieldSet[i] = estpq;
100 break;
101 default:
102 error(line, "illegal vector field selection", compString.c_str());
103 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000104 }
105 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000106
Arun Patole7e7e68d2015-05-22 12:02:25 +0530107 for (int i = 0; i < fields.num; ++i)
108 {
109 if (fields.offsets[i] >= vecSize)
110 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400111 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000112 return false;
113 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000114
Arun Patole7e7e68d2015-05-22 12:02:25 +0530115 if (i > 0)
116 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400117 if (fieldSet[i] != fieldSet[i - 1])
Arun Patole7e7e68d2015-05-22 12:02:25 +0530118 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400119 error(line, "illegal - vector component fields not from the same set",
120 compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000121 return false;
122 }
123 }
124 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000125
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000126 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000127}
128
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000129///////////////////////////////////////////////////////////////////////
130//
131// Errors
132//
133////////////////////////////////////////////////////////////////////////
134
135//
136// Track whether errors have occurred.
137//
138void TParseContext::recover()
139{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000140}
141
142//
143// Used by flex/bison to output all syntax and parsing errors.
144//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530145void TParseContext::error(const TSourceLoc &loc,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400146 const char *reason,
147 const char *token,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530148 const char *extraInfo)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000149{
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000150 pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -0400151 srcLoc.file = loc.first_file;
152 srcLoc.line = loc.first_line;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400153 mDiagnostics.writeInfo(pp::Diagnostics::PP_ERROR, srcLoc, reason, token, extraInfo);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000154}
155
Arun Patole7e7e68d2015-05-22 12:02:25 +0530156void TParseContext::warning(const TSourceLoc &loc,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400157 const char *reason,
158 const char *token,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530159 const char *extraInfo)
160{
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000161 pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -0400162 srcLoc.file = loc.first_file;
163 srcLoc.line = loc.first_line;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400164 mDiagnostics.writeInfo(pp::Diagnostics::PP_WARNING, srcLoc, reason, token, extraInfo);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000165}
166
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200167void TParseContext::outOfRangeError(bool isError,
168 const TSourceLoc &loc,
169 const char *reason,
170 const char *token,
171 const char *extraInfo)
172{
173 if (isError)
174 {
175 error(loc, reason, token, extraInfo);
176 recover();
177 }
178 else
179 {
180 warning(loc, reason, token, extraInfo);
181 }
182}
183
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000184//
185// Same error message for all places assignments don't work.
186//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530187void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000188{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000189 std::stringstream extraInfoStream;
190 extraInfoStream << "cannot convert from '" << right << "' to '" << left << "'";
191 std::string extraInfo = extraInfoStream.str();
192 error(line, "", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000193}
194
195//
196// Same error message for all places unary operations don't work.
197//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530198void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000199{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000200 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400201 extraInfoStream << "no operation '" << op << "' exists that takes an operand of type "
202 << operand << " (or there is no acceptable conversion)";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000203 std::string extraInfo = extraInfoStream.str();
204 error(line, " wrong operand type", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000205}
206
207//
208// Same error message for all binary operations don't work.
209//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400210void TParseContext::binaryOpError(const TSourceLoc &line,
211 const char *op,
212 TString left,
213 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000214{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000215 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400216 extraInfoStream << "no operation '" << op << "' exists that takes a left-hand operand of type '"
217 << left << "' and a right operand of type '" << right
218 << "' (or there is no acceptable conversion)";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000219 std::string extraInfo = extraInfoStream.str();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530220 error(line, " wrong operand types ", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000221}
222
Jamie Madillb98c3a82015-07-23 14:26:04 -0400223bool TParseContext::precisionErrorCheck(const TSourceLoc &line,
224 TPrecision precision,
225 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530226{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400227 if (!mChecksPrecisionErrors)
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000228 return false;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400229 switch (type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530230 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400231 case EbtFloat:
232 if (precision == EbpUndefined)
233 {
234 error(line, "No precision specified for (float)", "");
235 return true;
236 }
237 break;
238 case EbtInt:
239 if (precision == EbpUndefined)
240 {
241 error(line, "No precision specified (int)", "");
242 return true;
243 }
244 break;
245 default:
246 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000247 }
248 return false;
249}
250
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000251//
252// Both test and if necessary, spit out an error, to see if the node is really
253// an l-value that can be operated on this way.
254//
255// Returns true if the was an error.
256//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530257bool TParseContext::lValueErrorCheck(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000258{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400259 TIntermSymbol *symNode = node->getAsSymbolNode();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530260 TIntermBinary *binaryNode = node->getAsBinaryNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000261
Arun Patole7e7e68d2015-05-22 12:02:25 +0530262 if (binaryNode)
263 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000264 bool errorReturn;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000265
Jamie Madillb98c3a82015-07-23 14:26:04 -0400266 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530267 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400268 case EOpIndexDirect:
269 case EOpIndexIndirect:
270 case EOpIndexDirectStruct:
271 case EOpIndexDirectInterfaceBlock:
272 return lValueErrorCheck(line, op, binaryNode->getLeft());
273 case EOpVectorSwizzle:
274 errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft());
275 if (!errorReturn)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530276 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400277 int offset[4] = {0, 0, 0, 0};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000278
Jamie Madillb98c3a82015-07-23 14:26:04 -0400279 TIntermTyped *rightNode = binaryNode->getRight();
280 TIntermAggregate *aggrNode = rightNode->getAsAggregate();
281
282 for (TIntermSequence::iterator p = aggrNode->getSequence()->begin();
283 p != aggrNode->getSequence()->end(); p++)
284 {
285 int value = (*p)->getAsTyped()->getAsConstantUnion()->getIConst(0);
286 offset[value]++;
287 if (offset[value] > 1)
288 {
289 error(line, " l-value of swizzle cannot have duplicate components", op);
290
291 return true;
292 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000293 }
294 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000295
Jamie Madillb98c3a82015-07-23 14:26:04 -0400296 return errorReturn;
297 default:
298 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000299 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000300 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000301
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000302 return true;
303 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000304
Arun Patole7e7e68d2015-05-22 12:02:25 +0530305 const char *symbol = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000306 if (symNode != 0)
307 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000308
Arun Patole7e7e68d2015-05-22 12:02:25 +0530309 const char *message = 0;
310 switch (node->getQualifier())
311 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400312 case EvqConst:
313 message = "can't modify a const";
314 break;
315 case EvqConstReadOnly:
316 message = "can't modify a const";
317 break;
318 case EvqAttribute:
319 message = "can't modify an attribute";
320 break;
321 case EvqFragmentIn:
322 message = "can't modify an input";
323 break;
324 case EvqVertexIn:
325 message = "can't modify an input";
326 break;
327 case EvqUniform:
328 message = "can't modify a uniform";
329 break;
330 case EvqVaryingIn:
331 message = "can't modify a varying";
332 break;
333 case EvqFragCoord:
334 message = "can't modify gl_FragCoord";
335 break;
336 case EvqFrontFacing:
337 message = "can't modify gl_FrontFacing";
338 break;
339 case EvqPointCoord:
340 message = "can't modify gl_PointCoord";
341 break;
342 default:
343 //
344 // Type that can't be written to?
345 //
346 if (node->getBasicType() == EbtVoid)
347 {
348 message = "can't modify void";
349 }
350 if (IsSampler(node->getBasicType()))
351 {
352 message = "can't modify a sampler";
353 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000354 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000355
Arun Patole7e7e68d2015-05-22 12:02:25 +0530356 if (message == 0 && binaryNode == 0 && symNode == 0)
357 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000358 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000359
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000360 return true;
361 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000362
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000363 //
364 // Everything else is okay, no error.
365 //
366 if (message == 0)
367 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000368
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000369 //
370 // If we get here, we have an error and a message.
371 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530372 if (symNode)
373 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000374 std::stringstream extraInfoStream;
375 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
376 std::string extraInfo = extraInfoStream.str();
377 error(line, " l-value required", op, extraInfo.c_str());
378 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530379 else
380 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000381 std::stringstream extraInfoStream;
382 extraInfoStream << "(" << message << ")";
383 std::string extraInfo = extraInfoStream.str();
384 error(line, " l-value required", op, extraInfo.c_str());
385 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000386
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000387 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000388}
389
390//
391// Both test, and if necessary spit out an error, to see if the node is really
392// a constant.
393//
394// Returns true if the was an error.
395//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530396bool TParseContext::constErrorCheck(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000397{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000398 if (node->getQualifier() == EvqConst)
399 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000400
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000401 error(node->getLine(), "constant expression required", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000402
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000403 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000404}
405
406//
407// Both test, and if necessary spit out an error, to see if the node is really
408// an integer.
409//
410// Returns true if the was an error.
411//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530412bool TParseContext::integerErrorCheck(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000413{
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000414 if (node->isScalarInt())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000415 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000416
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000417 error(node->getLine(), "integer expression required", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000418
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000419 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000420}
421
422//
423// Both test, and if necessary spit out an error, to see if we are currently
424// globally scoped.
425//
426// Returns true if the was an error.
427//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530428bool TParseContext::globalErrorCheck(const TSourceLoc &line, bool global, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000429{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000430 if (global)
431 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000432
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000433 error(line, "only allowed at global scope", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000434
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000435 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000436}
437
438//
439// For now, keep it simple: if it starts "gl_", it's reserved, independent
440// of scope. Except, if the symbol table is at the built-in push-level,
441// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000442// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
443// webgl shader.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000444//
445// Returns true if there was an error.
446//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530447bool TParseContext::reservedErrorCheck(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000448{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530449 static const char *reservedErrMsg = "reserved built-in name";
450 if (!symbolTable.atBuiltInLevel())
451 {
452 if (identifier.compare(0, 3, "gl_") == 0)
453 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000454 error(line, reservedErrMsg, "gl_");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000455 return true;
456 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530457 if (IsWebGLBasedSpec(mShaderSpec))
458 {
459 if (identifier.compare(0, 6, "webgl_") == 0)
460 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000461 error(line, reservedErrMsg, "webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000462 return true;
463 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530464 if (identifier.compare(0, 7, "_webgl_") == 0)
465 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000466 error(line, reservedErrMsg, "_webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000467 return true;
468 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530469 if (mShaderSpec == SH_CSS_SHADERS_SPEC && identifier.compare(0, 4, "css_") == 0)
470 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000471 error(line, reservedErrMsg, "css_");
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000472 return true;
473 }
alokp@chromium.org613ef312010-07-21 18:54:22 +0000474 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530475 if (identifier.find("__") != TString::npos)
476 {
477 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400478 "identifiers containing two consecutive underscores (__) are reserved as "
479 "possible future keywords",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530480 identifier.c_str());
daniel@transgaming.combeadd5d2012-04-12 02:35:31 +0000481 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000482 }
483 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000484
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000485 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000486}
487
488//
489// Make sure there is enough data provided to the constructor to build
490// something of the type of the constructor. Also returns the type of
491// the constructor.
492//
493// Returns true if there was an error in construction.
494//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400495bool TParseContext::constructorErrorCheck(const TSourceLoc &line,
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200496 TIntermNode *argumentsNode,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400497 TFunction &function,
498 TOperator op,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530499 TType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000500{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000501 *type = function.getReturnType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000502
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000503 bool constructingMatrix = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400504 switch (op)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530505 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400506 case EOpConstructMat2:
507 case EOpConstructMat2x3:
508 case EOpConstructMat2x4:
509 case EOpConstructMat3x2:
510 case EOpConstructMat3:
511 case EOpConstructMat3x4:
512 case EOpConstructMat4x2:
513 case EOpConstructMat4x3:
514 case EOpConstructMat4:
515 constructingMatrix = true;
516 break;
517 default:
518 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000519 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000520
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000521 //
522 // Note: It's okay to have too many components available, but not okay to have unused
523 // arguments. 'full' will go to true when enough args have been seen. If we loop
524 // again, there is an extra argument, so 'overfull' will become true.
525 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000526
Jamie Madillb98c3a82015-07-23 14:26:04 -0400527 size_t size = 0;
528 bool constType = true;
529 bool full = false;
530 bool overFull = false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000531 bool matrixInMatrix = false;
532 bool arrayArg = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530533 for (size_t i = 0; i < function.getParamCount(); ++i)
534 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700535 const TConstParameter &param = function.getParam(i);
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000536 size += param.type->getObjectSize();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530537
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000538 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000539 matrixInMatrix = true;
540 if (full)
541 overFull = true;
542 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
543 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000544 if (param.type->getQualifier() != EvqConst)
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000545 constType = false;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000546 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000547 arrayArg = true;
548 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530549
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000550 if (constType)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000551 type->setQualifier(EvqConst);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000552
Olli Etuaho376f1b52015-04-13 13:23:41 +0300553 if (type->isArray())
554 {
555 if (type->isUnsizedArray())
556 {
Cooper Partin4d61f7e2015-08-12 10:56:50 -0700557 type->setArraySize(static_cast<int>(function.getParamCount()));
Olli Etuaho376f1b52015-04-13 13:23:41 +0300558 }
559 else if (static_cast<size_t>(type->getArraySize()) != function.getParamCount())
560 {
561 error(line, "array constructor needs one argument per array element", "constructor");
562 return true;
563 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000564 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000565
Arun Patole7e7e68d2015-05-22 12:02:25 +0530566 if (arrayArg && op != EOpConstructStruct)
567 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000568 error(line, "constructing from a non-dereferenced array", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000569 return true;
570 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000571
Arun Patole7e7e68d2015-05-22 12:02:25 +0530572 if (matrixInMatrix && !type->isArray())
573 {
574 if (function.getParamCount() != 1)
575 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400576 error(line, "constructing matrix from matrix can only take one argument",
577 "constructor");
578 return true;
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000579 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000580 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000581
Arun Patole7e7e68d2015-05-22 12:02:25 +0530582 if (overFull)
583 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000584 error(line, "too many arguments", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000585 return true;
586 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530587
Jamie Madillb98c3a82015-07-23 14:26:04 -0400588 if (op == EOpConstructStruct && !type->isArray() &&
589 type->getStruct()->fields().size() != function.getParamCount())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530590 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400591 error(line,
592 "Number of constructor parameters does not match the number of structure fields",
593 "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000594 return true;
595 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000596
Arun Patole7e7e68d2015-05-22 12:02:25 +0530597 if (!type->isMatrix() || !matrixInMatrix)
598 {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000599 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
Arun Patole7e7e68d2015-05-22 12:02:25 +0530600 (op == EOpConstructStruct && size < type->getObjectSize()))
601 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000602 error(line, "not enough data provided for construction", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000603 return true;
604 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000605 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000606
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200607 if (argumentsNode == nullptr)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530608 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200609 error(line, "constructor does not have any arguments", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000610 return true;
611 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200612
613 TIntermAggregate *argumentsAgg = argumentsNode->getAsAggregate();
614 for (TIntermNode *&argNode : *argumentsAgg->getSequence())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530615 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200616 TIntermTyped *argTyped = argNode->getAsTyped();
617 ASSERT(argTyped != nullptr);
618 if (op != EOpConstructStruct && IsSampler(argTyped->getBasicType()))
619 {
620 error(line, "cannot convert a sampler", "constructor");
621 return true;
622 }
623 if (argTyped->getBasicType() == EbtVoid)
624 {
625 error(line, "cannot convert a void", "constructor");
626 return true;
627 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000628 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000629
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000630 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000631}
632
Jamie Madillb98c3a82015-07-23 14:26:04 -0400633// This function checks to see if a void variable has been declared and raise an error message for
634// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000635//
636// returns true in case of an error
637//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400638bool TParseContext::voidErrorCheck(const TSourceLoc &line,
639 const TString &identifier,
640 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000641{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300642 if (type == EbtVoid)
643 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000644 error(line, "illegal use of type 'void'", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000645 return true;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300646 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000647
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000648 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000649}
650
Jamie Madillb98c3a82015-07-23 14:26:04 -0400651// This function checks to see if the node (for the expression) contains a scalar boolean expression
652// or not
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000653//
654// returns true in case of an error
655//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530656bool TParseContext::boolErrorCheck(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000657{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530658 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector())
659 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000660 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000661 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530662 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000663
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000664 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000665}
666
Jamie Madillb98c3a82015-07-23 14:26:04 -0400667// This function checks to see if the node (for the expression) contains a scalar boolean expression
668// or not
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000669//
670// returns true in case of an error
671//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530672bool TParseContext::boolErrorCheck(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000673{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530674 if (pType.type != EbtBool || pType.isAggregate())
675 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000676 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000677 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530678 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000679
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000680 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000681}
682
Jamie Madillb98c3a82015-07-23 14:26:04 -0400683bool TParseContext::samplerErrorCheck(const TSourceLoc &line,
684 const TPublicType &pType,
685 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000686{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530687 if (pType.type == EbtStruct)
688 {
689 if (containsSampler(*pType.userDef))
690 {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000691 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530692
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000693 return true;
694 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530695
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000696 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530697 }
698 else if (IsSampler(pType.type))
699 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000700 error(line, reason, getBasicString(pType.type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000701
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000702 return true;
703 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000704
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000705 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000706}
707
Arun Patole7e7e68d2015-05-22 12:02:25 +0530708bool TParseContext::locationDeclaratorListCheck(const TSourceLoc &line, const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400709{
710 if (pType.layoutQualifier.location != -1)
711 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400712 error(line, "location must only be specified for a single input or output variable",
713 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400714 return true;
715 }
716
717 return false;
718}
719
Jamie Madillb98c3a82015-07-23 14:26:04 -0400720bool TParseContext::parameterSamplerErrorCheck(const TSourceLoc &line,
721 TQualifier qualifier,
722 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000723{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400724 if ((qualifier == EvqOut || qualifier == EvqInOut) && type.getBasicType() != EbtStruct &&
725 IsSampler(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530726 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000727 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000728 return true;
729 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000730
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000731 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000732}
733
Arun Patole7e7e68d2015-05-22 12:02:25 +0530734bool TParseContext::containsSampler(const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000735{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000736 if (IsSampler(type.getBasicType()))
737 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000738
Arun Patole7e7e68d2015-05-22 12:02:25 +0530739 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock())
740 {
741 const TFieldList &fields = type.getStruct()->fields();
742 for (unsigned int i = 0; i < fields.size(); ++i)
743 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400744 if (containsSampler(*fields[i]->type()))
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000745 return true;
746 }
747 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000748
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000749 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000750}
751
752//
753// Do size checking for an array type's size.
754//
755// Returns true if there was an error.
756//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530757bool TParseContext::arraySizeErrorCheck(const TSourceLoc &line, TIntermTyped *expr, int &size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000758{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530759 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000760
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200761 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
762 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
763 // fold as array size.
764 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000765 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000766 error(line, "array size must be a constant integer expression", "");
Olli Etuahoe7847b02015-03-16 11:56:12 +0200767 size = 1;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000768 return true;
769 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000770
Nicolas Capens906744a2014-06-06 15:18:07 -0400771 unsigned int unsignedSize = 0;
772
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000773 if (constant->getBasicType() == EbtUInt)
774 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400775 unsignedSize = constant->getUConst(0);
Jamie Madillb98c3a82015-07-23 14:26:04 -0400776 size = static_cast<int>(unsignedSize);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000777 }
778 else
779 {
780 size = constant->getIConst(0);
781
Nicolas Capens906744a2014-06-06 15:18:07 -0400782 if (size < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000783 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400784 error(line, "array size must be non-negative", "");
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000785 size = 1;
786 return true;
787 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400788
789 unsignedSize = static_cast<unsigned int>(size);
790 }
791
792 if (size == 0)
793 {
794 error(line, "array size must be greater than zero", "");
795 size = 1;
796 return true;
797 }
798
799 // The size of arrays is restricted here to prevent issues further down the
800 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
801 // 4096 registers so this should be reasonable even for aggressively optimizable code.
802 const unsigned int sizeLimit = 65536;
803
804 if (unsignedSize > sizeLimit)
805 {
806 error(line, "array size too large", "");
807 size = 1;
808 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000809 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000810
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000811 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000812}
813
814//
815// See if this qualifier can be an array.
816//
817// Returns true if there is an error.
818//
Olli Etuaho3739d232015-04-08 12:23:44 +0300819bool TParseContext::arrayQualifierErrorCheck(const TSourceLoc &line, const TPublicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000820{
Olli Etuaho3739d232015-04-08 12:23:44 +0300821 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqVertexIn) ||
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400822 (type.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300823 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400824 error(line, "cannot declare arrays of this qualifier",
825 TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000826 return true;
827 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000828
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000829 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000830}
831
832//
833// See if this type can be an array.
834//
835// Returns true if there is an error.
836//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530837bool TParseContext::arrayTypeErrorCheck(const TSourceLoc &line, const TPublicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000838{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000839 //
840 // Can the type be an array?
841 //
Jamie Madill06145232015-05-13 13:10:01 -0400842 if (type.array)
843 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000844 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000845 return true;
846 }
Olli Etuahocc36b982015-07-10 14:14:18 +0300847 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
848 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
849 // 4.3.4).
850 if (mShaderVersion >= 300 && type.type == EbtStruct && sh::IsVarying(type.qualifier))
851 {
852 error(line, "cannot declare arrays of structs of this qualifier",
853 TType(type).getCompleteString().c_str());
854 return true;
855 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000856
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000857 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000858}
859
860//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000861// Enforce non-initializer type/qualifier rules.
862//
863// Returns true if there was an error.
864//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400865bool TParseContext::nonInitErrorCheck(const TSourceLoc &line,
866 const TString &identifier,
867 TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000868{
Olli Etuaho3739d232015-04-08 12:23:44 +0300869 ASSERT(type != nullptr);
870 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000871 {
872 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300873 type->qualifier = EvqTemporary;
874
875 // Generate informative error messages for ESSL1.
876 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400877 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000878 {
Arun Patole7e7e68d2015-05-22 12:02:25 +0530879 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400880 "structures containing arrays may not be declared constant since they cannot be "
881 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530882 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000883 }
884 else
885 {
886 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
887 }
888
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000889 return true;
890 }
Olli Etuaho376f1b52015-04-13 13:23:41 +0300891 if (type->isUnsizedArray())
892 {
893 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
894 return true;
895 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000896 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000897}
898
Olli Etuaho2935c582015-04-08 14:32:06 +0300899// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000900// and update the symbol table.
901//
Olli Etuaho2935c582015-04-08 14:32:06 +0300902// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000903//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400904bool TParseContext::declareVariable(const TSourceLoc &line,
905 const TString &identifier,
906 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +0300907 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000908{
Olli Etuaho2935c582015-04-08 14:32:06 +0300909 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000910
Olli Etuaho2935c582015-04-08 14:32:06 +0300911 bool needsReservedErrorCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000912
Olli Etuaho2935c582015-04-08 14:32:06 +0300913 // gl_LastFragData may be redeclared with a new precision qualifier
914 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
915 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400916 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
917 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho2935c582015-04-08 14:32:06 +0300918 if (type.getArraySize() == maxDrawBuffers->getConstPointer()->getIConst())
919 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400920 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +0300921 {
922 needsReservedErrorCheck = extensionErrorCheck(line, builtInSymbol->getExtension());
923 }
924 }
925 else
926 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400927 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
928 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +0300929 return false;
930 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000931 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000932
Olli Etuaho2935c582015-04-08 14:32:06 +0300933 if (needsReservedErrorCheck && reservedErrorCheck(line, identifier))
934 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000935
Olli Etuaho2935c582015-04-08 14:32:06 +0300936 (*variable) = new TVariable(&identifier, type);
937 if (!symbolTable.declare(*variable))
938 {
939 error(line, "redefinition", identifier.c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -0400940 *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +0300941 return false;
942 }
943
944 if (voidErrorCheck(line, identifier, type.getBasicType()))
945 return false;
946
947 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000948}
949
Jamie Madillb98c3a82015-07-23 14:26:04 -0400950bool TParseContext::paramErrorCheck(const TSourceLoc &line,
951 TQualifier qualifier,
952 TQualifier paramQualifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530953 TType *type)
954{
955 if (qualifier != EvqConst && qualifier != EvqTemporary)
956 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000957 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000958 return true;
959 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530960 if (qualifier == EvqConst && paramQualifier != EvqIn)
961 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400962 error(line, "qualifier not allowed with ", getQualifierString(qualifier),
963 getQualifierString(paramQualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000964 return true;
965 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000966
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000967 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000968 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000969 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000970 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000971
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000972 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000973}
974
Arun Patole7e7e68d2015-05-22 12:02:25 +0530975bool TParseContext::extensionErrorCheck(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000976{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400977 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000978 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +0530979 if (iter == extBehavior.end())
980 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000981 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000982 return true;
983 }
zmo@google.comf5450912011-09-09 01:37:19 +0000984 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +0530985 if (iter->second == EBhDisable || iter->second == EBhUndefined)
986 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000987 error(line, "extension", extension.c_str(), "is disabled");
988 return true;
989 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530990 if (iter->second == EBhWarn)
991 {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000992 warning(line, "extension", extension.c_str(), "is being used");
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000993 return false;
994 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000995
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000996 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000997}
998
Jamie Madillb98c3a82015-07-23 14:26:04 -0400999// These checks are common for all declarations starting a declarator list, and declarators that
1000// follow an empty declaration.
Olli Etuahofa33d582015-04-09 14:33:12 +03001001//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001002bool TParseContext::singleDeclarationErrorCheck(const TPublicType &publicType,
1003 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001004{
Olli Etuahofa33d582015-04-09 14:33:12 +03001005 switch (publicType.qualifier)
1006 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001007 case EvqVaryingIn:
1008 case EvqVaryingOut:
1009 case EvqAttribute:
1010 case EvqVertexIn:
1011 case EvqFragmentOut:
1012 if (publicType.type == EbtStruct)
1013 {
1014 error(identifierLocation, "cannot be used with a structure",
1015 getQualifierString(publicType.qualifier));
1016 return true;
1017 }
Olli Etuahofa33d582015-04-09 14:33:12 +03001018
Jamie Madillb98c3a82015-07-23 14:26:04 -04001019 default:
1020 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001021 }
1022
Jamie Madillb98c3a82015-07-23 14:26:04 -04001023 if (publicType.qualifier != EvqUniform &&
1024 samplerErrorCheck(identifierLocation, publicType, "samplers must be uniform"))
Olli Etuahofa33d582015-04-09 14:33:12 +03001025 {
Jamie Madilla5efff92013-06-06 11:56:47 -04001026 return true;
Olli Etuahofa33d582015-04-09 14:33:12 +03001027 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001028
1029 // check for layout qualifier issues
1030 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
1031
1032 if (layoutQualifier.matrixPacking != EmpUnspecified)
1033 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001034 error(identifierLocation, "layout qualifier",
1035 getMatrixPackingString(layoutQualifier.matrixPacking),
Olli Etuahofa33d582015-04-09 14:33:12 +03001036 "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -04001037 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001038 }
1039
1040 if (layoutQualifier.blockStorage != EbsUnspecified)
1041 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001042 error(identifierLocation, "layout qualifier",
1043 getBlockStorageString(layoutQualifier.blockStorage),
Olli Etuahofa33d582015-04-09 14:33:12 +03001044 "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -04001045 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001046 }
1047
Olli Etuahofa33d582015-04-09 14:33:12 +03001048 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut &&
1049 layoutLocationErrorCheck(identifierLocation, publicType.layoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001050 {
Jamie Madill51a53c72013-06-19 09:24:43 -04001051 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001052 }
1053
1054 return false;
1055}
1056
Jamie Madillb98c3a82015-07-23 14:26:04 -04001057bool TParseContext::layoutLocationErrorCheck(const TSourceLoc &location,
1058 const TLayoutQualifier &layoutQualifier)
Jamie Madilla5efff92013-06-06 11:56:47 -04001059{
1060 if (layoutQualifier.location != -1)
1061 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001062 error(location, "invalid layout qualifier:", "location",
1063 "only valid on program inputs and outputs");
Jamie Madilla5efff92013-06-06 11:56:47 -04001064 return true;
1065 }
1066
1067 return false;
1068}
1069
Jamie Madillb98c3a82015-07-23 14:26:04 -04001070bool TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate,
1071 TIntermAggregate *aggregate)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001072{
1073 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1074 {
1075 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1076 if (qual == EvqOut || qual == EvqInOut)
1077 {
1078 TIntermTyped *node = (*(aggregate->getSequence()))[i]->getAsTyped();
1079 if (lValueErrorCheck(node->getLine(), "assign", node))
1080 {
1081 error(node->getLine(),
Jamie Madillb98c3a82015-07-23 14:26:04 -04001082 "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error");
Olli Etuahob6e07a62015-02-16 12:22:10 +02001083 recover();
1084 return true;
1085 }
1086 }
1087 }
1088 return false;
1089}
1090
Jamie Madillb98c3a82015-07-23 14:26:04 -04001091void TParseContext::es3InvariantErrorCheck(const TQualifier qualifier,
1092 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001093{
1094 if (!sh::IsVaryingOut(qualifier) && qualifier != EvqFragmentOut)
1095 {
1096 error(invariantLocation, "Only out variables can be invariant.", "invariant");
1097 recover();
1098 }
1099}
1100
Arun Patole7e7e68d2015-05-22 12:02:25 +05301101bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001102{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001103 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001104 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1105 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001106}
1107
Arun Patole7e7e68d2015-05-22 12:02:25 +05301108bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001109{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001110 return ::IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001111}
1112
Jamie Madillb98c3a82015-07-23 14:26:04 -04001113void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1114 const char *extName,
1115 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001116{
1117 pp::SourceLocation srcLoc;
1118 srcLoc.file = loc.first_file;
1119 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001120 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001121}
1122
Jamie Madillb98c3a82015-07-23 14:26:04 -04001123void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1124 const char *name,
1125 const char *value,
1126 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001127{
1128 pp::SourceLocation srcLoc;
1129 srcLoc.file = loc.first_file;
1130 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001131 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001132}
1133
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001134/////////////////////////////////////////////////////////////////////////////////
1135//
1136// Non-Errors.
1137//
1138/////////////////////////////////////////////////////////////////////////////////
1139
Jamie Madill5c097022014-08-20 16:38:32 -04001140const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1141 const TString *name,
1142 const TSymbol *symbol)
1143{
1144 const TVariable *variable = NULL;
1145
1146 if (!symbol)
1147 {
1148 error(location, "undeclared identifier", name->c_str());
1149 recover();
1150 }
1151 else if (!symbol->isVariable())
1152 {
1153 error(location, "variable expected", name->c_str());
1154 recover();
1155 }
1156 else
1157 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001158 variable = static_cast<const TVariable *>(symbol);
Jamie Madill5c097022014-08-20 16:38:32 -04001159
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001160 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
Jamie Madill5c097022014-08-20 16:38:32 -04001161 !variable->getExtension().empty() &&
1162 extensionErrorCheck(location, variable->getExtension()))
1163 {
1164 recover();
1165 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001166
1167 // Reject shaders using both gl_FragData and gl_FragColor
1168 TQualifier qualifier = variable->getType().getQualifier();
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001169 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001170 {
1171 mUsesFragData = true;
1172 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001173 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001174 {
1175 mUsesFragColor = true;
1176 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001177 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1178 {
1179 mUsesSecondaryOutputs = true;
1180 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001181
1182 // This validation is not quite correct - it's only an error to write to
1183 // both FragData and FragColor. For simplicity, and because users shouldn't
1184 // be rewarded for reading from undefined varaibles, return an error
1185 // if they are both referenced, rather than assigned.
1186 if (mUsesFragData && mUsesFragColor)
1187 {
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001188 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1189 if (mUsesSecondaryOutputs)
1190 {
1191 errorMessage =
1192 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1193 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1194 }
1195 error(location, errorMessage, name->c_str());
Jamie Madill14e95b32015-05-07 10:10:41 -04001196 recover();
1197 }
Jamie Madill5c097022014-08-20 16:38:32 -04001198 }
1199
1200 if (!variable)
1201 {
1202 TType type(EbtFloat, EbpUndefined);
1203 TVariable *fakeVariable = new TVariable(name, type);
1204 symbolTable.declare(fakeVariable);
1205 variable = fakeVariable;
1206 }
1207
1208 return variable;
1209}
1210
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001211TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1212 const TString *name,
1213 const TSymbol *symbol)
1214{
1215 const TVariable *variable = getNamedVariable(location, name, symbol);
1216
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001217 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001218 {
1219 TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001220 return intermediate.addConstantUnion(constArray, variable->getType(), location);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001221 }
1222 else
1223 {
1224 return intermediate.addSymbol(variable->getUniqueId(), variable->getName(),
1225 variable->getType(), location);
1226 }
1227}
1228
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001229//
1230// Look up a function name in the symbol table, and make sure it is a function.
1231//
1232// Return the function symbol if found, otherwise 0.
1233//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001234const TFunction *TParseContext::findFunction(const TSourceLoc &line,
1235 TFunction *call,
1236 int inputShaderVersion,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301237 bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001238{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001239 // First find by unmangled name to check whether the function name has been
1240 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001241 // If a function is found, check for one with a matching argument list.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301242 const TSymbol *symbol = symbolTable.find(call->getName(), inputShaderVersion, builtIn);
1243 if (symbol == 0 || symbol->isFunction())
1244 {
Austin Kinross3ae64652015-01-26 15:51:39 -08001245 symbol = symbolTable.find(call->getMangledName(), inputShaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001246 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001247
Arun Patole7e7e68d2015-05-22 12:02:25 +05301248 if (symbol == 0)
1249 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001250 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001251 return 0;
1252 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001253
Arun Patole7e7e68d2015-05-22 12:02:25 +05301254 if (!symbol->isFunction())
1255 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001256 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001257 return 0;
1258 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001259
Jamie Madillb98c3a82015-07-23 14:26:04 -04001260 return static_cast<const TFunction *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001261}
1262
1263//
1264// Initializers show up in several places in the grammar. Have one set of
1265// code to handle them here.
1266//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001267// Returns true on error, false if no error
1268//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001269bool TParseContext::executeInitializer(const TSourceLoc &line,
1270 const TString &identifier,
1271 const TPublicType &pType,
1272 TIntermTyped *initializer,
1273 TIntermNode **intermNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001274{
Olli Etuahoe7847b02015-03-16 11:56:12 +02001275 ASSERT(intermNode != nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001276 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001277
Olli Etuaho2935c582015-04-08 14:32:06 +03001278 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001279 if (type.isUnsizedArray())
1280 {
1281 type.setArraySize(initializer->getArraySize());
1282 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001283 if (!declareVariable(line, identifier, type, &variable))
1284 {
1285 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001286 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001287
Olli Etuahob0c645e2015-05-12 14:25:36 +03001288 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001289 if (symbolTable.atGlobalLevel() &&
1290 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001291 {
1292 // Error message does not completely match behavior with ESSL 1.00, but
1293 // we want to steer developers towards only using constant expressions.
1294 error(line, "global variable initializers must be constant expressions", "=");
1295 return true;
1296 }
1297 if (globalInitWarning)
1298 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001299 warning(
1300 line,
1301 "global variable initializers should be constant expressions "
1302 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1303 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001304 }
1305
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001306 //
1307 // identifier must be of type constant, a global, or a temporary
1308 //
1309 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301310 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1311 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001312 error(line, " cannot initialize this type of qualifier ",
1313 variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001314 return true;
1315 }
1316 //
1317 // test for and propagate constant
1318 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001319
Arun Patole7e7e68d2015-05-22 12:02:25 +05301320 if (qualifier == EvqConst)
1321 {
1322 if (qualifier != initializer->getType().getQualifier())
1323 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001324 std::stringstream extraInfoStream;
1325 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1326 std::string extraInfo = extraInfoStream.str();
1327 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001328 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001329 return true;
1330 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301331 if (type != initializer->getType())
1332 {
1333 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001334 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001335 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001336 return true;
1337 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001338
1339 // Save the constant folded value to the variable if possible. For example array
1340 // initializers are not folded, since that way copying the array literal to multiple places
1341 // in the shader is avoided.
1342 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1343 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301344 if (initializer->getAsConstantUnion())
1345 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001346 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001347 *intermNode = nullptr;
1348 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301349 }
1350 else if (initializer->getAsSymbolNode())
1351 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001352 const TSymbol *symbol =
1353 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1354 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001355
Arun Patole7e7e68d2015-05-22 12:02:25 +05301356 TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001357 if (constArray)
1358 {
1359 variable->shareConstPointer(constArray);
1360 *intermNode = nullptr;
1361 return false;
1362 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001363 }
1364 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001365
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001366 TIntermSymbol *intermSymbol = intermediate.addSymbol(
1367 variable->getUniqueId(), variable->getName(), variable->getType(), line);
1368 *intermNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1369 if (*intermNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001370 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001371 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1372 return true;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001373 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001374
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001375 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001376}
1377
Jamie Madillb98c3a82015-07-23 14:26:04 -04001378TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier,
1379 bool invariant,
1380 TLayoutQualifier layoutQualifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301381 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001382{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001383 TPublicType returnType = typeSpecifier;
1384 returnType.qualifier = qualifier;
1385 returnType.invariant = invariant;
Jamie Madilla5efff92013-06-06 11:56:47 -04001386 returnType.layoutQualifier = layoutQualifier;
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001387
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001388 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001389 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001390 if (typeSpecifier.array)
1391 {
1392 error(typeSpecifier.line, "not supported", "first-class array");
1393 recover();
1394 returnType.clearArrayness();
1395 }
1396
Jamie Madillb98c3a82015-07-23 14:26:04 -04001397 if (qualifier == EvqAttribute &&
1398 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001399 {
1400 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1401 recover();
1402 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001403
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001404 if ((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1405 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1406 {
1407 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1408 recover();
1409 }
1410 }
1411 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001412 {
Olli Etuahoabb0c382015-07-13 12:01:12 +03001413 if (!layoutQualifier.isEmpty())
1414 {
1415 if (globalErrorCheck(typeSpecifier.line, symbolTable.atGlobalLevel(), "layout"))
1416 {
1417 recover();
1418 }
1419 }
Olli Etuahocc36b982015-07-10 14:14:18 +03001420 if (sh::IsVarying(qualifier) || qualifier == EvqVertexIn || qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001421 {
Olli Etuahocc36b982015-07-10 14:14:18 +03001422 es3InputOutputTypeCheck(qualifier, typeSpecifier, typeSpecifier.line);
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001423 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001424 }
1425
1426 return returnType;
1427}
1428
Olli Etuahocc36b982015-07-10 14:14:18 +03001429void TParseContext::es3InputOutputTypeCheck(const TQualifier qualifier,
1430 const TPublicType &type,
1431 const TSourceLoc &qualifierLocation)
1432{
1433 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
1434 if (type.type == EbtBool)
1435 {
1436 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
1437 recover();
1438 }
1439
1440 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
1441 switch (qualifier)
1442 {
1443 case EvqVertexIn:
1444 // ESSL 3.00 section 4.3.4
1445 if (type.array)
1446 {
1447 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
1448 recover();
1449 }
1450 // Vertex inputs with a struct type are disallowed in singleDeclarationErrorCheck
1451 return;
1452 case EvqFragmentOut:
1453 // ESSL 3.00 section 4.3.6
1454 if (type.isMatrix())
1455 {
1456 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
1457 recover();
1458 }
1459 // Fragment outputs with a struct type are disallowed in singleDeclarationErrorCheck
1460 return;
1461 default:
1462 break;
1463 }
1464
1465 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
1466 // restrictions.
1467 bool typeContainsIntegers =
1468 (type.type == EbtInt || type.type == EbtUInt || type.isStructureContainingType(EbtInt) ||
1469 type.isStructureContainingType(EbtUInt));
1470 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
1471 {
1472 error(qualifierLocation, "must use 'flat' interpolation here",
1473 getQualifierString(qualifier));
1474 recover();
1475 }
1476
1477 if (type.type == EbtStruct)
1478 {
1479 // ESSL 3.00 sections 4.3.4 and 4.3.6.
1480 // These restrictions are only implied by the ESSL 3.00 spec, but
1481 // the ESSL 3.10 spec lists these restrictions explicitly.
1482 if (type.array)
1483 {
1484 error(qualifierLocation, "cannot be an array of structures",
1485 getQualifierString(qualifier));
1486 recover();
1487 }
1488 if (type.isStructureContainingArrays())
1489 {
1490 error(qualifierLocation, "cannot be a structure containing an array",
1491 getQualifierString(qualifier));
1492 recover();
1493 }
1494 if (type.isStructureContainingType(EbtStruct))
1495 {
1496 error(qualifierLocation, "cannot be a structure containing a structure",
1497 getQualifierString(qualifier));
1498 recover();
1499 }
1500 if (type.isStructureContainingType(EbtBool))
1501 {
1502 error(qualifierLocation, "cannot be a structure containing a bool",
1503 getQualifierString(qualifier));
1504 recover();
1505 }
1506 }
1507}
1508
Olli Etuahofa33d582015-04-09 14:33:12 +03001509TIntermAggregate *TParseContext::parseSingleDeclaration(TPublicType &publicType,
1510 const TSourceLoc &identifierOrTypeLocation,
1511 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001512{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001513 TIntermSymbol *symbol =
1514 intermediate.addSymbol(0, identifier, TType(publicType), identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001515
Olli Etuahobab4c082015-04-24 16:38:49 +03001516 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03001517
Olli Etuahobab4c082015-04-24 16:38:49 +03001518 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1519
1520 if (emptyDeclaration)
1521 {
1522 if (publicType.isUnsizedArray())
1523 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001524 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1525 // error. It is assumed that this applies to empty declarations as well.
1526 error(identifierOrTypeLocation, "empty array declaration needs to specify a size",
1527 identifier.c_str());
Olli Etuahobab4c082015-04-24 16:38:49 +03001528 }
1529 }
1530 else
Jamie Madill60ed9812013-06-06 11:56:46 -04001531 {
Olli Etuahofa33d582015-04-09 14:33:12 +03001532 if (singleDeclarationErrorCheck(publicType, identifierOrTypeLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001533 recover();
1534
Olli Etuaho376f1b52015-04-13 13:23:41 +03001535 if (nonInitErrorCheck(identifierOrTypeLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001536 recover();
1537
Olli Etuaho2935c582015-04-08 14:32:06 +03001538 TVariable *variable = nullptr;
Olli Etuahofa33d582015-04-09 14:33:12 +03001539 if (!declareVariable(identifierOrTypeLocation, identifier, TType(publicType), &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001540 recover();
1541
1542 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001543 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001544 }
1545
Olli Etuahoe7847b02015-03-16 11:56:12 +02001546 return intermediate.makeAggregate(symbol, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001547}
1548
Olli Etuahoe7847b02015-03-16 11:56:12 +02001549TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1550 const TSourceLoc &identifierLocation,
1551 const TString &identifier,
1552 const TSourceLoc &indexLocation,
1553 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001554{
Olli Etuahofa33d582015-04-09 14:33:12 +03001555 mDeferredSingleDeclarationErrorCheck = false;
1556
1557 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001558 recover();
1559
Olli Etuaho376f1b52015-04-13 13:23:41 +03001560 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001561 recover();
1562
Jamie Madillb98c3a82015-07-23 14:26:04 -04001563 if (arrayTypeErrorCheck(indexLocation, publicType) ||
1564 arrayQualifierErrorCheck(indexLocation, publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001565 {
1566 recover();
1567 }
1568
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001569 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001570
1571 int size;
1572 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1573 {
1574 recover();
1575 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001576 // Make the type an array even if size check failed.
1577 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1578 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001579
Olli Etuaho2935c582015-04-08 14:32:06 +03001580 TVariable *variable = nullptr;
1581 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001582 recover();
1583
Olli Etuahoe7847b02015-03-16 11:56:12 +02001584 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001585 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001586 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001587
Olli Etuahoe7847b02015-03-16 11:56:12 +02001588 return intermediate.makeAggregate(symbol, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001589}
1590
Jamie Madill06145232015-05-13 13:10:01 -04001591TIntermAggregate *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001592 const TSourceLoc &identifierLocation,
1593 const TString &identifier,
1594 const TSourceLoc &initLocation,
1595 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001596{
Olli Etuahofa33d582015-04-09 14:33:12 +03001597 mDeferredSingleDeclarationErrorCheck = false;
1598
1599 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001600 recover();
1601
Olli Etuahoe7847b02015-03-16 11:56:12 +02001602 TIntermNode *intermNode = nullptr;
1603 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04001604 {
1605 //
1606 // Build intermediate representation
1607 //
Olli Etuahoe7847b02015-03-16 11:56:12 +02001608 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001609 }
1610 else
1611 {
1612 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001613 return nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001614 }
1615}
1616
Jamie Madillb98c3a82015-07-23 14:26:04 -04001617TIntermAggregate *TParseContext::parseSingleArrayInitDeclaration(
1618 TPublicType &publicType,
1619 const TSourceLoc &identifierLocation,
1620 const TString &identifier,
1621 const TSourceLoc &indexLocation,
1622 TIntermTyped *indexExpression,
1623 const TSourceLoc &initLocation,
1624 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001625{
1626 mDeferredSingleDeclarationErrorCheck = false;
1627
1628 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1629 recover();
1630
Jamie Madillb98c3a82015-07-23 14:26:04 -04001631 if (arrayTypeErrorCheck(indexLocation, publicType) ||
1632 arrayQualifierErrorCheck(indexLocation, publicType))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001633 {
1634 recover();
1635 }
1636
1637 TPublicType arrayType(publicType);
1638
Olli Etuaho376f1b52015-04-13 13:23:41 +03001639 int size = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001640 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
1641 // the initializer.
1642 if (indexExpression != nullptr &&
1643 arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001644 {
1645 recover();
1646 }
1647 // Make the type an array even if size check failed.
1648 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1649 arrayType.setArraySize(size);
1650
1651 // initNode will correspond to the whole of "type b[n] = initializer".
1652 TIntermNode *initNode = nullptr;
1653 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1654 {
1655 return initNode ? intermediate.makeAggregate(initNode, initLocation) : nullptr;
1656 }
1657 else
1658 {
1659 recover();
1660 return nullptr;
1661 }
1662}
1663
Olli Etuahoe7847b02015-03-16 11:56:12 +02001664TIntermAggregate *TParseContext::parseInvariantDeclaration(const TSourceLoc &invariantLoc,
Jamie Madill47e3ec02014-08-20 16:38:33 -04001665 const TSourceLoc &identifierLoc,
1666 const TString *identifier,
1667 const TSymbol *symbol)
1668{
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001669 // invariant declaration
Jamie Madill47e3ec02014-08-20 16:38:33 -04001670 if (globalErrorCheck(invariantLoc, symbolTable.atGlobalLevel(), "invariant varying"))
1671 {
1672 recover();
1673 }
1674
1675 if (!symbol)
1676 {
1677 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
1678 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001679 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001680 }
1681 else
1682 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001683 const TString kGlFrontFacing("gl_FrontFacing");
1684 if (*identifier == kGlFrontFacing)
1685 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001686 error(identifierLoc, "identifier should not be declared as invariant",
1687 identifier->c_str());
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001688 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001689 return nullptr;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001690 }
Jamie Madill2c433252014-12-03 12:36:54 -05001691 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001692 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1693 ASSERT(variable);
1694 const TType &type = variable->getType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04001695 TIntermSymbol *intermSymbol =
1696 intermediate.addSymbol(variable->getUniqueId(), *identifier, type, identifierLoc);
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001697
1698 TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc);
1699 aggregate->setOp(EOpInvariantDeclaration);
1700 return aggregate;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001701 }
1702}
1703
Jamie Madillb98c3a82015-07-23 14:26:04 -04001704TIntermAggregate *TParseContext::parseDeclarator(TPublicType &publicType,
1705 TIntermAggregate *aggregateDeclaration,
1706 const TSourceLoc &identifierLocation,
1707 const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001708{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001709 // If the declaration starting this declarator list was empty (example: int,), some checks were
1710 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001711 if (mDeferredSingleDeclarationErrorCheck)
1712 {
1713 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1714 recover();
1715 mDeferredSingleDeclarationErrorCheck = false;
1716 }
1717
Jamie Madill0bd18df2013-06-20 11:55:52 -04001718 if (locationDeclaratorListCheck(identifierLocation, publicType))
1719 recover();
1720
Olli Etuaho376f1b52015-04-13 13:23:41 +03001721 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001722 recover();
1723
Olli Etuaho2935c582015-04-08 14:32:06 +03001724 TVariable *variable = nullptr;
1725 if (!declareVariable(identifierLocation, identifier, TType(publicType), &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001726 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001727
Jamie Madillb98c3a82015-07-23 14:26:04 -04001728 TIntermSymbol *symbol =
1729 intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001730 if (variable && symbol)
Jamie Madill502d66f2013-06-20 11:55:52 -04001731 symbol->setId(variable->getUniqueId());
1732
Olli Etuahoe7847b02015-03-16 11:56:12 +02001733 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001734}
1735
Jamie Madillb98c3a82015-07-23 14:26:04 -04001736TIntermAggregate *TParseContext::parseArrayDeclarator(TPublicType &publicType,
1737 TIntermAggregate *aggregateDeclaration,
1738 const TSourceLoc &identifierLocation,
1739 const TString &identifier,
1740 const TSourceLoc &arrayLocation,
1741 TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001742{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001743 // If the declaration starting this declarator list was empty (example: int,), some checks were
1744 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001745 if (mDeferredSingleDeclarationErrorCheck)
1746 {
1747 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1748 recover();
1749 mDeferredSingleDeclarationErrorCheck = false;
1750 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001751
Jamie Madill0bd18df2013-06-20 11:55:52 -04001752 if (locationDeclaratorListCheck(identifierLocation, publicType))
1753 recover();
1754
Olli Etuaho376f1b52015-04-13 13:23:41 +03001755 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001756 recover();
1757
Jamie Madillb98c3a82015-07-23 14:26:04 -04001758 if (arrayTypeErrorCheck(arrayLocation, publicType) ||
1759 arrayQualifierErrorCheck(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001760 {
1761 recover();
1762 }
Olli Etuaho93a90fd2015-04-07 18:14:07 +03001763 else
Jamie Madill502d66f2013-06-20 11:55:52 -04001764 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001765 TType arrayType = TType(publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04001766 int size;
1767 if (arraySizeErrorCheck(arrayLocation, indexExpression, size))
Olli Etuahoe7847b02015-03-16 11:56:12 +02001768 {
Jamie Madill502d66f2013-06-20 11:55:52 -04001769 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001770 }
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001771 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001772
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001773 TVariable *variable = nullptr;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001774 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001775 recover();
Jamie Madill502d66f2013-06-20 11:55:52 -04001776
Jamie Madillb98c3a82015-07-23 14:26:04 -04001777 TIntermSymbol *symbol =
1778 intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001779 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001780 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001781
1782 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001783 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001784
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001785 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001786}
1787
Jamie Madillb98c3a82015-07-23 14:26:04 -04001788TIntermAggregate *TParseContext::parseInitDeclarator(const TPublicType &publicType,
1789 TIntermAggregate *aggregateDeclaration,
1790 const TSourceLoc &identifierLocation,
1791 const TString &identifier,
1792 const TSourceLoc &initLocation,
1793 TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001794{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001795 // If the declaration starting this declarator list was empty (example: int,), some checks were
1796 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001797 if (mDeferredSingleDeclarationErrorCheck)
1798 {
1799 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1800 recover();
1801 mDeferredSingleDeclarationErrorCheck = false;
1802 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001803
Jamie Madill0bd18df2013-06-20 11:55:52 -04001804 if (locationDeclaratorListCheck(identifierLocation, publicType))
1805 recover();
1806
Olli Etuahoe7847b02015-03-16 11:56:12 +02001807 TIntermNode *intermNode = nullptr;
1808 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04001809 {
1810 //
1811 // build the intermediate representation
1812 //
1813 if (intermNode)
1814 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001815 return intermediate.growAggregate(aggregateDeclaration, intermNode, initLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001816 }
1817 else
1818 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001819 return aggregateDeclaration;
Jamie Madill502d66f2013-06-20 11:55:52 -04001820 }
1821 }
1822 else
1823 {
1824 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001825 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001826 }
1827}
1828
Jamie Madill06145232015-05-13 13:10:01 -04001829TIntermAggregate *TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001830 TIntermAggregate *aggregateDeclaration,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301831 const TSourceLoc &identifierLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001832 const TString &identifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301833 const TSourceLoc &indexLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001834 TIntermTyped *indexExpression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001835 const TSourceLoc &initLocation,
1836 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001837{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001838 // If the declaration starting this declarator list was empty (example: int,), some checks were
1839 // not performed.
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001840 if (mDeferredSingleDeclarationErrorCheck)
1841 {
1842 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1843 recover();
1844 mDeferredSingleDeclarationErrorCheck = false;
1845 }
1846
1847 if (locationDeclaratorListCheck(identifierLocation, publicType))
1848 recover();
1849
Jamie Madillb98c3a82015-07-23 14:26:04 -04001850 if (arrayTypeErrorCheck(indexLocation, publicType) ||
1851 arrayQualifierErrorCheck(indexLocation, publicType))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001852 {
1853 recover();
1854 }
1855
1856 TPublicType arrayType(publicType);
1857
Olli Etuaho376f1b52015-04-13 13:23:41 +03001858 int size = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001859 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
1860 // the initializer.
1861 if (indexExpression != nullptr &&
1862 arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001863 {
1864 recover();
1865 }
1866 // Make the type an array even if size check failed.
1867 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1868 arrayType.setArraySize(size);
1869
1870 // initNode will correspond to the whole of "b[n] = initializer".
1871 TIntermNode *initNode = nullptr;
1872 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1873 {
1874 if (initNode)
1875 {
1876 return intermediate.growAggregate(aggregateDeclaration, initNode, initLocation);
1877 }
1878 else
1879 {
1880 return aggregateDeclaration;
1881 }
1882 }
1883 else
1884 {
1885 recover();
1886 return nullptr;
1887 }
1888}
1889
Jamie Madilla295edf2013-06-06 11:56:48 -04001890void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1891{
1892 if (typeQualifier.qualifier != EvqUniform)
1893 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001894 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
1895 "global layout must be uniform");
Jamie Madilla295edf2013-06-06 11:56:48 -04001896 recover();
1897 return;
1898 }
1899
1900 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1901 ASSERT(!layoutQualifier.isEmpty());
1902
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001903 if (mShaderVersion < 300)
Jamie Madilla295edf2013-06-06 11:56:48 -04001904 {
1905 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1906 recover();
1907 return;
1908 }
1909
1910 if (layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1911 {
1912 recover();
1913 return;
1914 }
1915
Jamie Madill099c0f32013-06-20 11:55:52 -04001916 if (layoutQualifier.matrixPacking != EmpUnspecified)
1917 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001918 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04001919 }
1920
Geoff Langc6856732014-02-11 09:38:55 -05001921 if (layoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04001922 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001923 mDefaultBlockStorage = layoutQualifier.blockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04001924 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001925}
1926
Jamie Madill185fb402015-06-12 15:48:48 -04001927void TParseContext::parseFunctionPrototype(const TSourceLoc &location,
1928 TFunction *function,
1929 TIntermAggregate **aggregateOut)
1930{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001931 const TSymbol *builtIn =
1932 symbolTable.findBuiltIn(function->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04001933
1934 if (builtIn)
1935 {
1936 error(location, "built-in functions cannot be redefined", function->getName().c_str());
1937 recover();
1938 }
1939
Jamie Madillb98c3a82015-07-23 14:26:04 -04001940 TFunction *prevDec =
1941 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Jamie Madill185fb402015-06-12 15:48:48 -04001942 //
1943 // Note: 'prevDec' could be 'function' if this is the first time we've seen function
1944 // as it would have just been put in the symbol table. Otherwise, we're looking up
1945 // an earlier occurance.
1946 //
1947 if (prevDec->isDefined())
1948 {
1949 // Then this function already has a body.
1950 error(location, "function already has a body", function->getName().c_str());
1951 recover();
1952 }
1953 prevDec->setDefined();
1954 //
1955 // Overload the unique ID of the definition to be the same unique ID as the declaration.
1956 // Eventually we will probably want to have only a single definition and just swap the
1957 // arguments to be the definition's arguments.
1958 //
1959 function->setUniqueId(prevDec->getUniqueId());
1960
1961 // Raise error message if main function takes any parameters or return anything other than void
1962 if (function->getName() == "main")
1963 {
1964 if (function->getParamCount() > 0)
1965 {
1966 error(location, "function cannot take any parameter(s)", function->getName().c_str());
1967 recover();
1968 }
1969 if (function->getReturnType().getBasicType() != EbtVoid)
1970 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001971 error(location, "", function->getReturnType().getBasicString(),
1972 "main function cannot return a value");
Jamie Madill185fb402015-06-12 15:48:48 -04001973 recover();
1974 }
1975 }
1976
1977 //
1978 // Remember the return type for later checking for RETURN statements.
1979 //
1980 setCurrentFunctionType(&(prevDec->getReturnType()));
1981 setFunctionReturnsValue(false);
1982
1983 //
1984 // Insert parameters into the symbol table.
1985 // If the parameter has no name, it's not an error, just don't insert it
1986 // (could be used for unused args).
1987 //
1988 // Also, accumulate the list of parameters into the HIL, so lower level code
1989 // knows where to find parameters.
1990 //
1991 TIntermAggregate *paramNodes = new TIntermAggregate;
1992 for (size_t i = 0; i < function->getParamCount(); i++)
1993 {
1994 const TConstParameter &param = function->getParam(i);
1995 if (param.name != 0)
1996 {
1997 TVariable *variable = new TVariable(param.name, *param.type);
1998 //
1999 // Insert the parameters with name in the symbol table.
2000 //
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002001 if (!symbolTable.declare(variable))
2002 {
Jamie Madill185fb402015-06-12 15:48:48 -04002003 error(location, "redefinition", variable->getName().c_str());
2004 recover();
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002005 paramNodes = intermediate.growAggregate(
2006 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
2007 continue;
Jamie Madill185fb402015-06-12 15:48:48 -04002008 }
2009
2010 //
2011 // Add the parameter to the HIL
2012 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002013 TIntermSymbol *symbol = intermediate.addSymbol(
2014 variable->getUniqueId(), variable->getName(), variable->getType(), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002015
2016 paramNodes = intermediate.growAggregate(paramNodes, symbol, location);
2017 }
2018 else
2019 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002020 paramNodes = intermediate.growAggregate(
2021 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002022 }
2023 }
2024 intermediate.setAggregateOperator(paramNodes, EOpParameters, location);
2025 *aggregateOut = paramNodes;
2026 setLoopNestingLevel(0);
2027}
2028
Jamie Madillb98c3a82015-07-23 14:26:04 -04002029TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002030{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002031 //
2032 // Multiple declarations of the same function are allowed.
2033 //
2034 // If this is a definition, the definition production code will check for redefinitions
2035 // (we don't know at this point if it's a definition or not).
2036 //
2037 // Redeclarations are allowed. But, return types and parameter qualifiers must match.
2038 //
2039 TFunction *prevDec =
2040 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
2041 if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002042 {
2043 if (prevDec->getReturnType() != function->getReturnType())
2044 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002045 error(location, "overloaded functions must have the same return type",
Jamie Madill185fb402015-06-12 15:48:48 -04002046 function->getReturnType().getBasicString());
2047 recover();
2048 }
2049 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2050 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002051 if (prevDec->getParam(i).type->getQualifier() !=
2052 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002053 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002054 error(location, "overloaded functions must have the same parameter qualifiers",
Jamie Madill185fb402015-06-12 15:48:48 -04002055 function->getParam(i).type->getQualifierString());
2056 recover();
2057 }
2058 }
2059 }
2060
2061 //
2062 // Check for previously declared variables using the same name.
2063 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002064 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002065 if (prevSym)
2066 {
2067 if (!prevSym->isFunction())
2068 {
2069 error(location, "redefinition", function->getName().c_str(), "function");
2070 recover();
2071 }
2072 }
2073 else
2074 {
2075 // Insert the unmangled name to detect potential future redefinition as a variable.
Jamie Madillb98c3a82015-07-23 14:26:04 -04002076 TFunction *newFunction =
2077 new TFunction(NewPoolTString(function->getName().c_str()), &function->getReturnType());
Jamie Madill185fb402015-06-12 15:48:48 -04002078 symbolTable.getOuterLevel()->insertUnmangled(newFunction);
2079 }
2080
2081 // We're at the inner scope level of the function's arguments and body statement.
2082 // Add the function prototype to the surrounding scope instead.
2083 symbolTable.getOuterLevel()->insert(function);
2084
2085 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002086 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2087 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002088 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2089 //
2090 return function;
2091}
2092
Jamie Madill06145232015-05-13 13:10:01 -04002093TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002094{
Jamie Madill06145232015-05-13 13:10:01 -04002095 TPublicType publicType = publicTypeIn;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002096 TOperator op = EOpNull;
2097 if (publicType.userDef)
2098 {
2099 op = EOpConstructStruct;
2100 }
2101 else
2102 {
2103 switch (publicType.type)
2104 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002105 case EbtFloat:
2106 if (publicType.isMatrix())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002107 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002108 switch (publicType.getCols())
Alexis Hetu07e57df2015-06-16 16:55:52 -04002109 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002110 case 2:
2111 switch (publicType.getRows())
2112 {
2113 case 2:
2114 op = EOpConstructMat2;
2115 break;
2116 case 3:
2117 op = EOpConstructMat2x3;
2118 break;
2119 case 4:
2120 op = EOpConstructMat2x4;
2121 break;
2122 }
2123 break;
2124 case 3:
2125 switch (publicType.getRows())
2126 {
2127 case 2:
2128 op = EOpConstructMat3x2;
2129 break;
2130 case 3:
2131 op = EOpConstructMat3;
2132 break;
2133 case 4:
2134 op = EOpConstructMat3x4;
2135 break;
2136 }
2137 break;
2138 case 4:
2139 switch (publicType.getRows())
2140 {
2141 case 2:
2142 op = EOpConstructMat4x2;
2143 break;
2144 case 3:
2145 op = EOpConstructMat4x3;
2146 break;
2147 case 4:
2148 op = EOpConstructMat4;
2149 break;
2150 }
2151 break;
Alexis Hetu07e57df2015-06-16 16:55:52 -04002152 }
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002153 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04002154 else
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002155 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002156 switch (publicType.getNominalSize())
2157 {
2158 case 1:
2159 op = EOpConstructFloat;
2160 break;
2161 case 2:
2162 op = EOpConstructVec2;
2163 break;
2164 case 3:
2165 op = EOpConstructVec3;
2166 break;
2167 case 4:
2168 op = EOpConstructVec4;
2169 break;
2170 }
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002171 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04002172 break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002173
Jamie Madillb98c3a82015-07-23 14:26:04 -04002174 case EbtInt:
2175 switch (publicType.getNominalSize())
2176 {
2177 case 1:
2178 op = EOpConstructInt;
2179 break;
2180 case 2:
2181 op = EOpConstructIVec2;
2182 break;
2183 case 3:
2184 op = EOpConstructIVec3;
2185 break;
2186 case 4:
2187 op = EOpConstructIVec4;
2188 break;
2189 }
2190 break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002191
Jamie Madillb98c3a82015-07-23 14:26:04 -04002192 case EbtUInt:
2193 switch (publicType.getNominalSize())
2194 {
2195 case 1:
2196 op = EOpConstructUInt;
2197 break;
2198 case 2:
2199 op = EOpConstructUVec2;
2200 break;
2201 case 3:
2202 op = EOpConstructUVec3;
2203 break;
2204 case 4:
2205 op = EOpConstructUVec4;
2206 break;
2207 }
2208 break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002209
Jamie Madillb98c3a82015-07-23 14:26:04 -04002210 case EbtBool:
2211 switch (publicType.getNominalSize())
2212 {
2213 case 1:
2214 op = EOpConstructBool;
2215 break;
2216 case 2:
2217 op = EOpConstructBVec2;
2218 break;
2219 case 3:
2220 op = EOpConstructBVec3;
2221 break;
2222 case 4:
2223 op = EOpConstructBVec4;
2224 break;
2225 }
2226 break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002227
Jamie Madillb98c3a82015-07-23 14:26:04 -04002228 default:
2229 break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002230 }
2231
2232 if (op == EOpNull)
2233 {
2234 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
2235 recover();
2236 publicType.type = EbtFloat;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002237 op = EOpConstructFloat;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002238 }
2239 }
2240
2241 TString tempString;
Dmitry Skiba7f17a502015-06-22 15:08:39 -07002242 const TType *type = new TType(publicType);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002243 return new TFunction(&tempString, type, op);
2244}
2245
Jamie Madillb98c3a82015-07-23 14:26:04 -04002246// This function is used to test for the correctness of the parameters passed to various constructor
2247// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002248//
2249// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
2250//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002251TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments,
2252 TType *type,
2253 TOperator op,
2254 TFunction *fnCall,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302255 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002256{
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002257 TIntermAggregate *constructor = arguments->getAsAggregate();
2258 ASSERT(constructor != nullptr);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002259
Olli Etuahof40319e2015-03-10 14:33:00 +02002260 if (type->isArray())
2261 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002262 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
2263 // the array.
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002264 TIntermSequence *args = constructor->getSequence();
Olli Etuahof40319e2015-03-10 14:33:00 +02002265 for (size_t i = 0; i < args->size(); i++)
2266 {
2267 const TType &argType = (*args)[i]->getAsTyped()->getType();
2268 // It has already been checked that the argument is not an array.
2269 ASSERT(!argType.isArray());
2270 if (!argType.sameElementType(*type))
2271 {
2272 error(line, "Array constructor argument has an incorrect type", "Error");
2273 recover();
2274 return nullptr;
2275 }
2276 }
2277 }
2278 else if (op == EOpConstructStruct)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002279 {
2280 const TFieldList &fields = type->getStruct()->fields();
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002281 TIntermSequence *args = constructor->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002282
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002283 for (size_t i = 0; i < fields.size(); i++)
2284 {
Nicolas Capensffd73872014-08-21 13:49:16 -04002285 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002286 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002287 error(line, "Structure constructor arguments do not match structure fields",
2288 "Error");
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002289 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002290
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002291 return 0;
2292 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002293 }
2294 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002295
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002296 // Turn the argument list itself into a constructor
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002297 constructor->setOp(op);
2298 constructor->setLine(line);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002299 ASSERT(constructor->isConstructor());
2300
2301 // Need to set type before setPrecisionFromChildren() because bool doesn't have precision.
2302 constructor->setType(*type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002303
Olli Etuaho21203702014-11-13 16:16:21 +02002304 // Structs should not be precision qualified, the individual members may be.
2305 // Built-in types on the other hand should be precision qualified.
2306 if (op != EOpConstructStruct)
2307 {
2308 constructor->setPrecisionFromChildren();
2309 type->setPrecision(constructor->getPrecision());
2310 }
2311
Olli Etuaho1d122782015-11-06 15:35:17 +02002312 TIntermTyped *constConstructor = intermediate.foldAggregateBuiltIn(constructor);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002313 if (constConstructor)
2314 {
2315 return constConstructor;
2316 }
2317
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002318 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002319}
2320
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002321//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002322// This function returns the tree representation for the vector field(s) being accessed from contant
2323// vector.
2324// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a
2325// contant node is returned, else an aggregate node is returned (for v.xy). The input to this
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002326// function could either be the symbol node or it could be the intermediate tree representation of
2327// accessing fields in a constant structure or column of a constant matrix.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002328//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002329TIntermTyped *TParseContext::addConstVectorNode(TVectorFields &fields,
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002330 TIntermConstantUnion *node,
2331 const TSourceLoc &line,
2332 bool outOfRangeIndexIsError)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002333{
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002334 const TConstantUnion *unionArray = node->getUnionArrayPointer();
2335 ASSERT(unionArray);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002336
Arun Patole7e7e68d2015-05-22 12:02:25 +05302337 TConstantUnion *constArray = new TConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002338
Arun Patole7e7e68d2015-05-22 12:02:25 +05302339 for (int i = 0; i < fields.num; i++)
2340 {
2341 if (fields.offsets[i] >= node->getType().getNominalSize())
2342 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002343 std::stringstream extraInfoStream;
2344 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
2345 std::string extraInfo = extraInfoStream.str();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002346 outOfRangeError(outOfRangeIndexIsError, line, "", "[", extraInfo.c_str());
2347 fields.offsets[i] = node->getType().getNominalSize() - 1;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002348 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302349
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002350 constArray[i] = unionArray[fields.offsets[i]];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302351 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002352 return intermediate.addConstantUnion(constArray, node->getType(), line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002353}
2354
2355//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002356// This function returns the column being accessed from a constant matrix. The values are retrieved
2357// from the symbol table and parse-tree is built for a vector (each column of a matrix is a vector).
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002358// The input to the function could either be a symbol node (m[0] where m is a constant matrix)that
2359// represents a constant matrix or it could be the tree representation of the constant matrix
2360// (s.m1[0] where s is a constant structure)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002361//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002362TIntermTyped *TParseContext::addConstMatrixNode(int index,
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002363 TIntermConstantUnion *node,
2364 const TSourceLoc &line,
2365 bool outOfRangeIndexIsError)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002366{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302367 if (index >= node->getType().getCols())
2368 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002369 std::stringstream extraInfoStream;
2370 extraInfoStream << "matrix field selection out of range '" << index << "'";
2371 std::string extraInfo = extraInfoStream.str();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002372 outOfRangeError(outOfRangeIndexIsError, line, "", "[", extraInfo.c_str());
2373 index = node->getType().getCols() - 1;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002374 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002375
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002376 TConstantUnion *unionArray = node->getUnionArrayPointer();
2377 int size = node->getType().getCols();
2378 return intermediate.addConstantUnion(&unionArray[size * index], node->getType(), line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002379}
2380
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002381//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002382// This function returns an element of an array accessed from a constant array. The values are
2383// retrieved from the symbol table and parse-tree is built for the type of the element. The input
Arun Patole7e7e68d2015-05-22 12:02:25 +05302384// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
Jamie Madillb98c3a82015-07-23 14:26:04 -04002385// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a
2386// constant structure)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002387//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002388TIntermTyped *TParseContext::addConstArrayNode(int index,
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002389 TIntermConstantUnion *node,
2390 const TSourceLoc &line,
2391 bool outOfRangeIndexIsError)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002392{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002393 TType arrayElementType = node->getType();
2394 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002395
Arun Patole7e7e68d2015-05-22 12:02:25 +05302396 if (index >= node->getType().getArraySize())
2397 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002398 std::stringstream extraInfoStream;
2399 extraInfoStream << "array field selection out of range '" << index << "'";
2400 std::string extraInfo = extraInfoStream.str();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002401 outOfRangeError(outOfRangeIndexIsError, line, "", "[", extraInfo.c_str());
2402 index = node->getType().getArraySize() - 1;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002403 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002404 size_t arrayElementSize = arrayElementType.getObjectSize();
2405 TConstantUnion *unionArray = node->getUnionArrayPointer();
2406 return intermediate.addConstantUnion(&unionArray[arrayElementSize * index], node->getType(),
2407 line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002408}
2409
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002410//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002411// This function returns the value of a particular field inside a constant structure from the symbol
2412// table.
2413// If there is an embedded/nested struct, it appropriately calls addConstStructNested or
2414// addConstStructFromAggr function and returns the parse-tree with the values of the embedded/nested
2415// struct.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002416//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002417TIntermTyped *TParseContext::addConstStruct(const TString &identifier,
2418 TIntermTyped *node,
2419 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002420{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302421 const TFieldList &fields = node->getType().getStruct()->fields();
Jamie Madillb98c3a82015-07-23 14:26:04 -04002422 size_t instanceSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002423
Arun Patole7e7e68d2015-05-22 12:02:25 +05302424 for (size_t index = 0; index < fields.size(); ++index)
2425 {
2426 if (fields[index]->name() == identifier)
2427 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002428 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302429 }
2430 else
2431 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002432 instanceSize += fields[index]->type()->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002433 }
2434 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002435
Jamie Madill94bf7f22013-07-08 13:31:15 -04002436 TIntermTyped *typedNode;
2437 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
Arun Patole7e7e68d2015-05-22 12:02:25 +05302438 if (tempConstantNode)
2439 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002440 TConstantUnion *constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002441
Jamie Madillb98c3a82015-07-23 14:26:04 -04002442 // type will be changed in the calling function
2443 typedNode = intermediate.addConstantUnion(constArray + instanceSize,
2444 tempConstantNode->getType(), line);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302445 }
2446 else
2447 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002448 error(line, "Cannot offset into the structure", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002449 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002450
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002451 return 0;
2452 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002453
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002454 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002455}
2456
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002457//
2458// Interface/uniform blocks
2459//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002460TIntermAggregate *TParseContext::addInterfaceBlock(const TPublicType &typeQualifier,
2461 const TSourceLoc &nameLine,
2462 const TString &blockName,
2463 TFieldList *fieldList,
2464 const TString *instanceName,
2465 const TSourceLoc &instanceLine,
2466 TIntermTyped *arrayIndex,
2467 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002468{
2469 if (reservedErrorCheck(nameLine, blockName))
2470 recover();
2471
2472 if (typeQualifier.qualifier != EvqUniform)
2473 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302474 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
2475 "interface blocks must be uniform");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002476 recover();
2477 }
2478
Jamie Madill099c0f32013-06-20 11:55:52 -04002479 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
2480 if (layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002481 {
2482 recover();
2483 }
2484
Jamie Madill099c0f32013-06-20 11:55:52 -04002485 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2486 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002487 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002488 }
2489
Jamie Madill1566ef72013-06-20 11:55:54 -04002490 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2491 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002492 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002493 }
2494
Arun Patole7e7e68d2015-05-22 12:02:25 +05302495 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2496 if (!symbolTable.declare(blockNameSymbol))
2497 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002498 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
2499 recover();
2500 }
2501
Jamie Madill98493dd2013-07-08 14:39:03 -04002502 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302503 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2504 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002505 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302506 TType *fieldType = field->type();
2507 if (IsSampler(fieldType->getBasicType()))
2508 {
2509 error(field->line(), "unsupported type", fieldType->getBasicString(),
2510 "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002511 recover();
2512 }
2513
Jamie Madill98493dd2013-07-08 14:39:03 -04002514 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002515 switch (qualifier)
2516 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002517 case EvqGlobal:
2518 case EvqUniform:
2519 break;
2520 default:
2521 error(field->line(), "invalid qualifier on interface block member",
2522 getQualifierString(qualifier));
2523 recover();
2524 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002525 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002526
2527 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002528 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
2529 if (layoutLocationErrorCheck(field->line(), fieldLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002530 {
2531 recover();
2532 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002533
Jamie Madill98493dd2013-07-08 14:39:03 -04002534 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002535 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002536 error(field->line(), "invalid layout qualifier:",
2537 getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04002538 recover();
2539 }
2540
Jamie Madill98493dd2013-07-08 14:39:03 -04002541 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002542 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002543 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002544 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002545 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002546 {
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002547 warning(field->line(), "extraneous layout qualifier:",
Jamie Madillb98c3a82015-07-23 14:26:04 -04002548 getMatrixPackingString(fieldLayoutQualifier.matrixPacking),
2549 "only has an effect on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04002550 }
2551
Jamie Madill98493dd2013-07-08 14:39:03 -04002552 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002553 }
2554
Jamie Madill98493dd2013-07-08 14:39:03 -04002555 // add array index
2556 int arraySize = 0;
2557 if (arrayIndex != NULL)
2558 {
2559 if (arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize))
2560 recover();
2561 }
2562
Jamie Madillb98c3a82015-07-23 14:26:04 -04002563 TInterfaceBlock *interfaceBlock =
2564 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2565 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
2566 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002567
2568 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04002569 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002570
Jamie Madill98493dd2013-07-08 14:39:03 -04002571 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002572 {
2573 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002574 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2575 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002576 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302577 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002578
2579 // set parent pointer of the field variable
2580 fieldType->setInterfaceBlock(interfaceBlock);
2581
Arun Patole7e7e68d2015-05-22 12:02:25 +05302582 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002583 fieldVariable->setQualifier(typeQualifier.qualifier);
2584
Arun Patole7e7e68d2015-05-22 12:02:25 +05302585 if (!symbolTable.declare(fieldVariable))
2586 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002587 error(field->line(), "redefinition", field->name().c_str(),
2588 "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002589 recover();
2590 }
2591 }
2592 }
2593 else
2594 {
Olli Etuahoe0f623a2015-07-10 11:58:30 +03002595 if (reservedErrorCheck(instanceLine, *instanceName))
2596 recover();
2597
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002598 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302599 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002600 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002601
Arun Patole7e7e68d2015-05-22 12:02:25 +05302602 if (!symbolTable.declare(instanceTypeDef))
2603 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002604 error(instanceLine, "redefinition", instanceName->c_str(),
2605 "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002606 recover();
2607 }
2608
Jamie Madillb98c3a82015-07-23 14:26:04 -04002609 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002610 symbolName = instanceTypeDef->getName();
2611 }
2612
Jamie Madillb98c3a82015-07-23 14:26:04 -04002613 TIntermAggregate *aggregate = intermediate.makeAggregate(
2614 intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line),
2615 nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002616 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04002617
2618 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002619 return aggregate;
2620}
2621
Arun Patole7e7e68d2015-05-22 12:02:25 +05302622bool TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002623{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002624 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002625
2626 // Embedded structure definitions are not supported per GLSL ES spec.
2627 // They aren't allowed in GLSL either, but we need to detect this here
2628 // so we don't rely on the GLSL compiler to catch it.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302629 if (mStructNestingLevel > 1)
2630 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002631 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002632 return true;
2633 }
2634
2635 return false;
2636}
2637
2638void TParseContext::exitStructDeclaration()
2639{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002640 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002641}
2642
Jamie Madillb98c3a82015-07-23 14:26:04 -04002643namespace
2644{
kbr@chromium.org476541f2011-10-27 21:14:51 +00002645const int kWebGLMaxStructNesting = 4;
2646
2647} // namespace
2648
Arun Patole7e7e68d2015-05-22 12:02:25 +05302649bool TParseContext::structNestingErrorCheck(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002650{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302651 if (!IsWebGLBasedSpec(mShaderSpec))
2652 {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002653 return false;
2654 }
2655
Arun Patole7e7e68d2015-05-22 12:02:25 +05302656 if (field.type()->getBasicType() != EbtStruct)
2657 {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002658 return false;
2659 }
2660
2661 // We're already inside a structure definition at this point, so add
2662 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302663 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2664 {
Jamie Madill41a49272014-03-18 16:10:13 -04002665 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002666 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
2667 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04002668 std::string reason = reasonStream.str();
2669 error(line, reason.c_str(), field.name().c_str(), "");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002670 return true;
2671 }
2672
2673 return false;
2674}
2675
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002676//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002677// Parse an array index expression
2678//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002679TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
2680 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302681 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002682{
2683 TIntermTyped *indexedExpression = NULL;
2684
2685 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2686 {
2687 if (baseExpression->getAsSymbolNode())
2688 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302689 error(location, " left of '[' is not of type array, matrix, or vector ",
2690 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002691 }
2692 else
2693 {
2694 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2695 }
2696 recover();
2697 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002698
Jamie Madill21c1e452014-12-29 11:33:41 -05002699 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2700
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002701 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002702 {
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002703 // If the index is not qualified as constant, the behavior in the spec is undefined. This
2704 // applies even if ANGLE has been able to constant fold it (ANGLE may constant fold
2705 // expressions that are not constant expressions). The most compatible way to handle this
2706 // case is to report a warning instead of an error and force the index to be in the
2707 // correct range.
2708 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Jamie Madill21c1e452014-12-29 11:33:41 -05002709 int index = indexConstantUnion->getIConst(0);
Jamie Madill7164cf42013-07-08 13:30:59 -04002710 if (index < 0)
2711 {
2712 std::stringstream infoStream;
2713 infoStream << index;
2714 std::string info = infoStream.str();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002715 outOfRangeError(outOfRangeIndexIsError, location, "negative index", info.c_str());
Jamie Madill7164cf42013-07-08 13:30:59 -04002716 index = 0;
2717 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002718 TIntermConstantUnion *baseConstantUnion = baseExpression->getAsConstantUnion();
2719 if (baseConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002720 {
2721 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002722 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002723 // constant folding for array indexing
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002724 indexedExpression =
2725 addConstArrayNode(index, baseConstantUnion, location, outOfRangeIndexIsError);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002726 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002727 else if (baseExpression->isVector())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002728 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002729 // constant folding for vector indexing
Jamie Madill7164cf42013-07-08 13:30:59 -04002730 TVectorFields fields;
2731 fields.num = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002732 fields.offsets[0] =
2733 index; // need to do it this way because v.xy sends fields integer array
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002734 indexedExpression =
2735 addConstVectorNode(fields, baseConstantUnion, location, outOfRangeIndexIsError);
Jamie Madill7164cf42013-07-08 13:30:59 -04002736 }
2737 else if (baseExpression->isMatrix())
2738 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002739 // constant folding for matrix indexing
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002740 indexedExpression =
2741 addConstMatrixNode(index, baseConstantUnion, location, outOfRangeIndexIsError);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002742 }
2743 }
2744 else
2745 {
Jamie Madillb11e2482015-05-04 14:21:22 -04002746 int safeIndex = -1;
2747
Jamie Madill7164cf42013-07-08 13:30:59 -04002748 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002749 {
Jamie Madill18464b52013-07-08 14:01:55 -04002750 if (index >= baseExpression->getType().getArraySize())
Jamie Madill7164cf42013-07-08 13:30:59 -04002751 {
2752 std::stringstream extraInfoStream;
2753 extraInfoStream << "array index out of range '" << index << "'";
2754 std::string extraInfo = extraInfoStream.str();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002755 outOfRangeError(outOfRangeIndexIsError, location, "", "[", extraInfo.c_str());
Jamie Madillb11e2482015-05-04 14:21:22 -04002756 safeIndex = baseExpression->getType().getArraySize() - 1;
Jamie Madill7164cf42013-07-08 13:30:59 -04002757 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302758 else if (baseExpression->getQualifier() == EvqFragData && index > 0 &&
2759 !isExtensionEnabled("GL_EXT_draw_buffers"))
Jamie Madill5d287f52013-07-12 15:38:19 -04002760 {
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002761 outOfRangeError(
2762 outOfRangeIndexIsError, location, "", "[",
2763 "array indexes for gl_FragData must be zero when GL_EXT_draw_buffers is "
2764 "disabled");
Jamie Madillb11e2482015-05-04 14:21:22 -04002765 safeIndex = 0;
Jamie Madill5d287f52013-07-12 15:38:19 -04002766 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002767 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302768 else if ((baseExpression->isVector() || baseExpression->isMatrix()) &&
Jamie Madillb98c3a82015-07-23 14:26:04 -04002769 baseExpression->getType().getNominalSize() <= index)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002770 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002771 std::stringstream extraInfoStream;
2772 extraInfoStream << "field selection out of range '" << index << "'";
2773 std::string extraInfo = extraInfoStream.str();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002774 outOfRangeError(outOfRangeIndexIsError, location, "", "[", extraInfo.c_str());
Jamie Madillb11e2482015-05-04 14:21:22 -04002775 safeIndex = baseExpression->getType().getNominalSize() - 1;
Jamie Madill46131a32013-06-20 11:55:50 -04002776 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002777
Jamie Madillb11e2482015-05-04 14:21:22 -04002778 // Don't modify the data of the previous constant union, because it can point
2779 // to builtins, like gl_MaxDrawBuffers. Instead use a new sanitized object.
2780 if (safeIndex != -1)
2781 {
2782 TConstantUnion *safeConstantUnion = new TConstantUnion();
2783 safeConstantUnion->setIConst(safeIndex);
2784 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
2785 }
2786
Jamie Madillb98c3a82015-07-23 14:26:04 -04002787 indexedExpression =
2788 intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002789 }
2790 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002791 else
2792 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002793 if (baseExpression->isInterfaceBlock())
Jamie Madill7164cf42013-07-08 13:30:59 -04002794 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002795 error(
2796 location, "", "[",
2797 "array indexes for interface blocks arrays must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002798 recover();
2799 }
Jamie Madill19571812013-08-12 15:26:34 -07002800 else if (baseExpression->getQualifier() == EvqFragmentOut)
Jamie Madill7164cf42013-07-08 13:30:59 -04002801 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002802 error(location, "", "[",
2803 "array indexes for fragment outputs must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002804 recover();
2805 }
2806
Jamie Madillb98c3a82015-07-23 14:26:04 -04002807 indexedExpression =
2808 intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
Jamie Madill7164cf42013-07-08 13:30:59 -04002809 }
2810
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002811 if (indexedExpression == 0)
2812 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002813 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002814 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002815 indexedExpression =
2816 intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002817 }
2818 else if (baseExpression->isArray())
2819 {
Olli Etuahob3fbd862015-09-30 17:55:02 +03002820 TType indexedType = baseExpression->getType();
2821 indexedType.clearArrayness();
2822 indexedExpression->setType(indexedType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002823 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002824 else if (baseExpression->isMatrix())
2825 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002826 indexedExpression->setType(TType(baseExpression->getBasicType(),
Olli Etuahob3fbd862015-09-30 17:55:02 +03002827 baseExpression->getPrecision(), EvqTemporary,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002828 static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002829 }
2830 else if (baseExpression->isVector())
2831 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002832 indexedExpression->setType(
Olli Etuahob3fbd862015-09-30 17:55:02 +03002833 TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002834 }
2835 else
2836 {
2837 indexedExpression->setType(baseExpression->getType());
2838 }
2839
Olli Etuahob3fbd862015-09-30 17:55:02 +03002840 if (baseExpression->getType().getQualifier() == EvqConst &&
2841 indexExpression->getType().getQualifier() == EvqConst)
2842 {
2843 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2844 }
2845 else
2846 {
2847 indexedExpression->getTypePointer()->setQualifier(EvqTemporary);
2848 }
2849
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002850 return indexedExpression;
2851}
2852
Jamie Madillb98c3a82015-07-23 14:26:04 -04002853TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
2854 const TSourceLoc &dotLocation,
2855 const TString &fieldString,
2856 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002857{
2858 TIntermTyped *indexedExpression = NULL;
2859
2860 if (baseExpression->isArray())
2861 {
2862 error(fieldLocation, "cannot apply dot operator to an array", ".");
2863 recover();
2864 }
2865
2866 if (baseExpression->isVector())
2867 {
2868 TVectorFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002869 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields,
2870 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002871 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002872 fields.num = 1;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002873 fields.offsets[0] = 0;
2874 recover();
2875 }
2876
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002877 if (baseExpression->getAsConstantUnion())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002878 {
2879 // constant folding for vector fields
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002880 indexedExpression = addConstVectorNode(fields, baseExpression->getAsConstantUnion(),
2881 fieldLocation, true);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002882 }
2883 else
2884 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302885 TIntermTyped *index = intermediate.addSwizzle(fields, fieldLocation);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002886 indexedExpression =
2887 intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002888 }
2889 if (indexedExpression == nullptr)
2890 {
2891 recover();
2892 indexedExpression = baseExpression;
2893 }
2894 else
2895 {
2896 // Note that the qualifier set here will be corrected later.
Jamie Madillb98c3a82015-07-23 14:26:04 -04002897 indexedExpression->setType(TType(baseExpression->getBasicType(),
2898 baseExpression->getPrecision(), EvqTemporary,
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002899 (unsigned char)(fieldString).size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002900 }
2901 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002902 else if (baseExpression->getBasicType() == EbtStruct)
2903 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002904 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302905 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04002906 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002907 {
2908 error(dotLocation, "structure has no fields", "Internal Error");
2909 recover();
2910 indexedExpression = baseExpression;
2911 }
2912 else
2913 {
2914 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002915 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002916 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002917 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002918 {
2919 fieldFound = true;
2920 break;
2921 }
2922 }
2923 if (fieldFound)
2924 {
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002925 if (baseExpression->getAsConstantUnion())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002926 {
2927 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2928 if (indexedExpression == 0)
2929 {
2930 recover();
2931 indexedExpression = baseExpression;
2932 }
2933 else
2934 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002935 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002936 }
2937 }
2938 else
2939 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002940 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002941 unionArray->setIConst(i);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002942 TIntermTyped *index = intermediate.addConstantUnion(
2943 unionArray, *fields[i]->type(), fieldLocation);
2944 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression,
2945 index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002946 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002947 }
2948 }
2949 else
2950 {
2951 error(dotLocation, " no such field in structure", fieldString.c_str());
2952 recover();
2953 indexedExpression = baseExpression;
2954 }
2955 }
2956 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002957 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002958 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002959 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302960 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04002961 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002962 {
2963 error(dotLocation, "interface block has no fields", "Internal Error");
2964 recover();
2965 indexedExpression = baseExpression;
2966 }
2967 else
2968 {
2969 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002970 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002971 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002972 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002973 {
2974 fieldFound = true;
2975 break;
2976 }
2977 }
2978 if (fieldFound)
2979 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002980 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002981 unionArray->setIConst(i);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002982 TIntermTyped *index =
2983 intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
2984 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock,
2985 baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002986 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002987 }
2988 else
2989 {
2990 error(dotLocation, " no such field in interface block", fieldString.c_str());
2991 recover();
2992 indexedExpression = baseExpression;
2993 }
2994 }
2995 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002996 else
2997 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002998 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002999 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03003000 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303001 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003002 }
3003 else
3004 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303005 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003006 " field selection requires structure, vector, or interface block on left hand "
3007 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303008 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003009 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003010 recover();
3011 indexedExpression = baseExpression;
3012 }
3013
Olli Etuahob1edc4f2015-11-02 17:20:03 +02003014 if (baseExpression->getQualifier() == EvqConst)
3015 {
3016 indexedExpression->getTypePointer()->setQualifier(EvqConst);
3017 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003018 else
3019 {
3020 indexedExpression->getTypePointer()->setQualifier(EvqTemporary);
3021 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02003022
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003023 return indexedExpression;
3024}
3025
Jamie Madillb98c3a82015-07-23 14:26:04 -04003026TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3027 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003028{
Jamie Madilla5efff92013-06-06 11:56:47 -04003029 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003030
Jamie Madillb98c3a82015-07-23 14:26:04 -04003031 qualifier.location = -1;
Jamie Madilla5efff92013-06-06 11:56:47 -04003032 qualifier.matrixPacking = EmpUnspecified;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003033 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003034
3035 if (qualifierType == "shared")
3036 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003037 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003038 }
3039 else if (qualifierType == "packed")
3040 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003041 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003042 }
3043 else if (qualifierType == "std140")
3044 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003045 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003046 }
3047 else if (qualifierType == "row_major")
3048 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003049 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003050 }
3051 else if (qualifierType == "column_major")
3052 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003053 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003054 }
3055 else if (qualifierType == "location")
3056 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003057 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
3058 "location requires an argument");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003059 recover();
3060 }
3061 else
3062 {
3063 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
3064 recover();
3065 }
3066
Jamie Madilla5efff92013-06-06 11:56:47 -04003067 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003068}
3069
Jamie Madillb98c3a82015-07-23 14:26:04 -04003070TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3071 const TSourceLoc &qualifierTypeLine,
3072 const TString &intValueString,
3073 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303074 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003075{
Jamie Madilla5efff92013-06-06 11:56:47 -04003076 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003077
Jamie Madillb98c3a82015-07-23 14:26:04 -04003078 qualifier.location = -1;
Jamie Madilla5efff92013-06-06 11:56:47 -04003079 qualifier.matrixPacking = EmpUnspecified;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003080 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003081
3082 if (qualifierType != "location")
3083 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303084 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
3085 "only location may have arguments");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003086 recover();
3087 }
3088 else
3089 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003090 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003091 if (intValue < 0)
3092 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003093 error(intValueLine, "out of range:", intValueString.c_str(),
3094 "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003095 recover();
3096 }
3097 else
3098 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003099 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003100 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003101 }
3102
Jamie Madilla5efff92013-06-06 11:56:47 -04003103 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003104}
3105
Jamie Madillb98c3a82015-07-23 14:26:04 -04003106TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
3107 TLayoutQualifier rightQualifier)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003108{
Jamie Madilla5efff92013-06-06 11:56:47 -04003109 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003110
Jamie Madilla5efff92013-06-06 11:56:47 -04003111 if (rightQualifier.location != -1)
3112 {
3113 joinedQualifier.location = rightQualifier.location;
3114 }
3115 if (rightQualifier.matrixPacking != EmpUnspecified)
3116 {
3117 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
3118 }
3119 if (rightQualifier.blockStorage != EbsUnspecified)
3120 {
3121 joinedQualifier.blockStorage = rightQualifier.blockStorage;
3122 }
3123
3124 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003125}
3126
Arun Patole7e7e68d2015-05-22 12:02:25 +05303127TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc,
3128 TQualifier interpolationQualifier,
3129 const TSourceLoc &storageLoc,
3130 TQualifier storageQualifier)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003131{
3132 TQualifier mergedQualifier = EvqSmoothIn;
3133
Arun Patole7e7e68d2015-05-22 12:02:25 +05303134 if (storageQualifier == EvqFragmentIn)
3135 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003136 if (interpolationQualifier == EvqSmooth)
3137 mergedQualifier = EvqSmoothIn;
3138 else if (interpolationQualifier == EvqFlat)
3139 mergedQualifier = EvqFlatIn;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003140 else
3141 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003142 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303143 else if (storageQualifier == EvqCentroidIn)
3144 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003145 if (interpolationQualifier == EvqSmooth)
3146 mergedQualifier = EvqCentroidIn;
3147 else if (interpolationQualifier == EvqFlat)
3148 mergedQualifier = EvqFlatIn;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003149 else
3150 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003151 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303152 else if (storageQualifier == EvqVertexOut)
3153 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003154 if (interpolationQualifier == EvqSmooth)
3155 mergedQualifier = EvqSmoothOut;
3156 else if (interpolationQualifier == EvqFlat)
3157 mergedQualifier = EvqFlatOut;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003158 else
3159 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003160 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303161 else if (storageQualifier == EvqCentroidOut)
3162 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003163 if (interpolationQualifier == EvqSmooth)
3164 mergedQualifier = EvqCentroidOut;
3165 else if (interpolationQualifier == EvqFlat)
3166 mergedQualifier = EvqFlatOut;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003167 else
3168 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003169 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303170 else
3171 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003172 error(interpolationLoc,
3173 "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303174 getInterpolationString(interpolationQualifier));
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003175 recover();
3176
3177 mergedQualifier = storageQualifier;
3178 }
3179
3180 TPublicType type;
3181 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
3182 return type;
3183}
3184
Jamie Madillb98c3a82015-07-23 14:26:04 -04003185TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3186 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003187{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03003188 if (voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier.type))
3189 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003190 recover();
3191 }
3192
Arun Patole7e7e68d2015-05-22 12:02:25 +05303193 for (unsigned int i = 0; i < fieldList->size(); ++i)
3194 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003195 //
3196 // Careful not to replace already known aspects of type, like array-ness
3197 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303198 TType *type = (*fieldList)[i]->type();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003199 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003200 type->setPrimarySize(typeSpecifier.primarySize);
3201 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003202 type->setPrecision(typeSpecifier.precision);
3203 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003204 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003205
3206 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303207 if (type->isArray())
3208 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003209 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
3210 recover();
3211 }
3212 if (typeSpecifier.array)
3213 type->setArraySize(typeSpecifier.arraySize);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303214 if (typeSpecifier.userDef)
3215 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003216 type->setStruct(typeSpecifier.userDef->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003217 }
3218
Arun Patole7e7e68d2015-05-22 12:02:25 +05303219 if (structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i]))
3220 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003221 recover();
3222 }
3223 }
3224
Jamie Madill98493dd2013-07-08 14:39:03 -04003225 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003226}
3227
Jamie Madillb98c3a82015-07-23 14:26:04 -04003228TPublicType TParseContext::addStructure(const TSourceLoc &structLine,
3229 const TSourceLoc &nameLine,
3230 const TString *structName,
3231 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003232{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303233 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003234 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003235
Jamie Madill9b820842015-02-12 10:40:10 -05003236 // Store a bool in the struct if we're at global scope, to allow us to
3237 // skip the local struct scoping workaround in HLSL.
Jamie Madillb960cc42015-02-12 15:33:20 +00003238 structure->setUniqueId(TSymbolTable::nextUniqueId());
Jamie Madill9b820842015-02-12 10:40:10 -05003239 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003240
Jamie Madill98493dd2013-07-08 14:39:03 -04003241 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003242 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003243 if (reservedErrorCheck(nameLine, *structName))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003244 {
3245 recover();
3246 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303247 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
3248 if (!symbolTable.declare(userTypeDef))
3249 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003250 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003251 recover();
3252 }
3253 }
3254
3255 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04003256 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003257 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003258 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04003259 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003260 switch (qualifier)
3261 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003262 case EvqGlobal:
3263 case EvqTemporary:
3264 break;
3265 default:
3266 error(field.line(), "invalid qualifier on struct member",
3267 getQualifierString(qualifier));
3268 recover();
3269 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003270 }
3271 }
3272
3273 TPublicType publicType;
3274 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003275 publicType.userDef = structureType;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003276 exitStructDeclaration();
3277
3278 return publicType;
3279}
3280
Jamie Madillb98c3a82015-07-23 14:26:04 -04003281TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
3282 TIntermAggregate *statementList,
3283 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02003284{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003285 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003286 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02003287 init->isVector())
3288 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003289 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
3290 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003291 recover();
3292 return nullptr;
3293 }
3294
Olli Etuahoac5274d2015-02-20 10:19:08 +02003295 if (statementList)
3296 {
3297 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
3298 {
3299 recover();
3300 return nullptr;
3301 }
3302 }
3303
Olli Etuahoa3a36662015-02-17 13:46:51 +02003304 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3305 if (node == nullptr)
3306 {
3307 error(loc, "erroneous switch statement", "switch");
3308 recover();
3309 return nullptr;
3310 }
3311 return node;
3312}
3313
3314TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3315{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003316 if (mSwitchNestingLevel == 0)
3317 {
3318 error(loc, "case labels need to be inside switch statements", "case");
3319 recover();
3320 return nullptr;
3321 }
3322 if (condition == nullptr)
3323 {
3324 error(loc, "case label must have a condition", "case");
3325 recover();
3326 return nullptr;
3327 }
3328 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04003329 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02003330 {
3331 error(condition->getLine(), "case label must be a scalar integer", "case");
3332 recover();
3333 }
3334 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003335 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
3336 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
3337 // fold in case labels.
3338 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02003339 {
3340 error(condition->getLine(), "case label must be constant", "case");
3341 recover();
3342 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003343 TIntermCase *node = intermediate.addCase(condition, loc);
3344 if (node == nullptr)
3345 {
3346 error(loc, "erroneous case statement", "case");
3347 recover();
3348 return nullptr;
3349 }
3350 return node;
3351}
3352
3353TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3354{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003355 if (mSwitchNestingLevel == 0)
3356 {
3357 error(loc, "default labels need to be inside switch statements", "default");
3358 recover();
3359 return nullptr;
3360 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003361 TIntermCase *node = intermediate.addCase(nullptr, loc);
3362 if (node == nullptr)
3363 {
3364 error(loc, "erroneous default statement", "default");
3365 recover();
3366 return nullptr;
3367 }
3368 return node;
3369}
3370
Jamie Madillb98c3a82015-07-23 14:26:04 -04003371TIntermTyped *TParseContext::createUnaryMath(TOperator op,
3372 TIntermTyped *child,
3373 const TSourceLoc &loc,
3374 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003375{
3376 if (child == nullptr)
3377 {
3378 return nullptr;
3379 }
3380
3381 switch (op)
3382 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003383 case EOpLogicalNot:
3384 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
3385 child->isVector())
3386 {
3387 return nullptr;
3388 }
3389 break;
3390 case EOpBitwiseNot:
3391 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3392 child->isMatrix() || child->isArray())
3393 {
3394 return nullptr;
3395 }
3396 break;
3397 case EOpPostIncrement:
3398 case EOpPreIncrement:
3399 case EOpPostDecrement:
3400 case EOpPreDecrement:
3401 case EOpNegative:
3402 case EOpPositive:
3403 if (child->getBasicType() == EbtStruct || child->getBasicType() == EbtBool ||
3404 child->isArray())
3405 {
3406 return nullptr;
3407 }
3408 // Operators for built-ins are already type checked against their prototype.
3409 default:
3410 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003411 }
3412
Olli Etuahof6c694b2015-03-26 14:50:53 +02003413 return intermediate.addUnaryMath(op, child, loc, funcReturnType);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003414}
3415
Olli Etuaho09b22472015-02-11 11:47:26 +02003416TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3417{
Olli Etuahof6c694b2015-03-26 14:50:53 +02003418 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003419 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003420 {
3421 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
3422 recover();
3423 return child;
3424 }
3425 return node;
3426}
3427
Jamie Madillb98c3a82015-07-23 14:26:04 -04003428TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
3429 TIntermTyped *child,
3430 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003431{
3432 if (lValueErrorCheck(loc, GetOperatorString(op), child))
3433 recover();
3434 return addUnaryMath(op, child, loc);
3435}
3436
Jamie Madillb98c3a82015-07-23 14:26:04 -04003437bool TParseContext::binaryOpCommonCheck(TOperator op,
3438 TIntermTyped *left,
3439 TIntermTyped *right,
3440 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003441{
3442 if (left->isArray() || right->isArray())
3443 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003444 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003445 {
3446 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3447 return false;
3448 }
3449
3450 if (left->isArray() != right->isArray())
3451 {
3452 error(loc, "array / non-array mismatch", GetOperatorString(op));
3453 return false;
3454 }
3455
3456 switch (op)
3457 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003458 case EOpEqual:
3459 case EOpNotEqual:
3460 case EOpAssign:
3461 case EOpInitialize:
3462 break;
3463 default:
3464 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3465 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02003466 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003467 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003468 if (left->getArraySize() != right->getArraySize())
3469 {
3470 error(loc, "array size mismatch", GetOperatorString(op));
3471 return false;
3472 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003473 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003474
3475 // Check ops which require integer / ivec parameters
3476 bool isBitShift = false;
3477 switch (op)
3478 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003479 case EOpBitShiftLeft:
3480 case EOpBitShiftRight:
3481 case EOpBitShiftLeftAssign:
3482 case EOpBitShiftRightAssign:
3483 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3484 // check that the basic type is an integer type.
3485 isBitShift = true;
3486 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3487 {
3488 return false;
3489 }
3490 break;
3491 case EOpBitwiseAnd:
3492 case EOpBitwiseXor:
3493 case EOpBitwiseOr:
3494 case EOpBitwiseAndAssign:
3495 case EOpBitwiseXorAssign:
3496 case EOpBitwiseOrAssign:
3497 // It is enough to check the type of only one operand, since later it
3498 // is checked that the operand types match.
3499 if (!IsInteger(left->getBasicType()))
3500 {
3501 return false;
3502 }
3503 break;
3504 default:
3505 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003506 }
3507
3508 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3509 // So the basic type should usually match.
3510 if (!isBitShift && left->getBasicType() != right->getBasicType())
3511 {
3512 return false;
3513 }
3514
Olli Etuaho9dd217b2015-03-20 14:24:31 +02003515 // Check that type sizes match exactly on ops that require that.
Olli Etuahoff699002015-03-23 14:38:42 +02003516 // Also check restrictions for structs that contain arrays or samplers.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003517 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003518 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003519 case EOpAssign:
3520 case EOpInitialize:
3521 case EOpEqual:
3522 case EOpNotEqual:
3523 // ESSL 1.00 sections 5.7, 5.8, 5.9
3524 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
3525 {
3526 error(loc, "undefined operation for structs containing arrays",
3527 GetOperatorString(op));
3528 return false;
3529 }
3530 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3531 // we interpret the spec so that this extends to structs containing samplers,
3532 // similarly to ESSL 1.00 spec.
3533 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
3534 left->getType().isStructureContainingSamplers())
3535 {
3536 error(loc, "undefined operation for structs containing samplers",
3537 GetOperatorString(op));
3538 return false;
3539 }
3540 case EOpLessThan:
3541 case EOpGreaterThan:
3542 case EOpLessThanEqual:
3543 case EOpGreaterThanEqual:
3544 if ((left->getNominalSize() != right->getNominalSize()) ||
3545 (left->getSecondarySize() != right->getSecondarySize()))
3546 {
3547 return false;
3548 }
3549 default:
3550 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003551 }
3552
Olli Etuahod6b14282015-03-17 14:31:35 +02003553 return true;
3554}
3555
Jamie Madillb98c3a82015-07-23 14:26:04 -04003556TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
3557 TIntermTyped *left,
3558 TIntermTyped *right,
3559 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02003560{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003561 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003562 return nullptr;
3563
Olli Etuahofc1806e2015-03-17 13:03:11 +02003564 switch (op)
3565 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003566 case EOpEqual:
3567 case EOpNotEqual:
3568 break;
3569 case EOpLessThan:
3570 case EOpGreaterThan:
3571 case EOpLessThanEqual:
3572 case EOpGreaterThanEqual:
3573 ASSERT(!left->isArray() && !right->isArray());
3574 if (left->isMatrix() || left->isVector() || left->getBasicType() == EbtStruct)
3575 {
3576 return nullptr;
3577 }
3578 break;
3579 case EOpLogicalOr:
3580 case EOpLogicalXor:
3581 case EOpLogicalAnd:
3582 ASSERT(!left->isArray() && !right->isArray());
3583 if (left->getBasicType() != EbtBool || left->isMatrix() || left->isVector())
3584 {
3585 return nullptr;
3586 }
3587 break;
3588 case EOpAdd:
3589 case EOpSub:
3590 case EOpDiv:
3591 case EOpMul:
3592 ASSERT(!left->isArray() && !right->isArray());
3593 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool)
3594 {
3595 return nullptr;
3596 }
3597 break;
3598 case EOpIMod:
3599 ASSERT(!left->isArray() && !right->isArray());
3600 // Note that this is only for the % operator, not for mod()
3601 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool ||
3602 left->getBasicType() == EbtFloat)
3603 {
3604 return nullptr;
3605 }
3606 break;
3607 // Note that for bitwise ops, type checking is done in promote() to
3608 // share code between ops and compound assignment
3609 default:
3610 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003611 }
3612
Olli Etuahofc1806e2015-03-17 13:03:11 +02003613 return intermediate.addBinaryMath(op, left, right, loc);
3614}
3615
Jamie Madillb98c3a82015-07-23 14:26:04 -04003616TIntermTyped *TParseContext::addBinaryMath(TOperator op,
3617 TIntermTyped *left,
3618 TIntermTyped *right,
3619 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003620{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003621 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003622 if (node == 0)
3623 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003624 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3625 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003626 recover();
3627 return left;
3628 }
3629 return node;
3630}
3631
Jamie Madillb98c3a82015-07-23 14:26:04 -04003632TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
3633 TIntermTyped *left,
3634 TIntermTyped *right,
3635 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003636{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003637 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003638 if (node == 0)
3639 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003640 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3641 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003642 recover();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003643 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02003644 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003645 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
3646 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003647 }
3648 return node;
3649}
3650
Jamie Madillb98c3a82015-07-23 14:26:04 -04003651TIntermTyped *TParseContext::createAssign(TOperator op,
3652 TIntermTyped *left,
3653 TIntermTyped *right,
3654 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003655{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003656 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003657 {
3658 return intermediate.addAssign(op, left, right, loc);
3659 }
3660 return nullptr;
3661}
3662
Jamie Madillb98c3a82015-07-23 14:26:04 -04003663TIntermTyped *TParseContext::addAssign(TOperator op,
3664 TIntermTyped *left,
3665 TIntermTyped *right,
3666 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003667{
3668 TIntermTyped *node = createAssign(op, left, right, loc);
3669 if (node == nullptr)
3670 {
3671 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
3672 recover();
3673 return left;
3674 }
3675 return node;
3676}
3677
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003678TIntermTyped *TParseContext::addComma(TIntermTyped *left,
3679 TIntermTyped *right,
3680 const TSourceLoc &loc)
3681{
Olli Etuaho15200042015-11-04 16:56:31 +02003682 return intermediate.addComma(left, right, loc, mShaderVersion);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02003683}
3684
Olli Etuaho49300862015-02-20 14:54:49 +02003685TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3686{
3687 switch (op)
3688 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003689 case EOpContinue:
3690 if (mLoopNestingLevel <= 0)
3691 {
3692 error(loc, "continue statement only allowed in loops", "");
3693 recover();
3694 }
3695 break;
3696 case EOpBreak:
3697 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
3698 {
3699 error(loc, "break statement only allowed in loops and switch statements", "");
3700 recover();
3701 }
3702 break;
3703 case EOpReturn:
3704 if (mCurrentFunctionType->getBasicType() != EbtVoid)
3705 {
3706 error(loc, "non-void function must return a value", "return");
3707 recover();
3708 }
3709 break;
3710 default:
3711 // No checks for discard
3712 break;
Olli Etuaho49300862015-02-20 14:54:49 +02003713 }
3714 return intermediate.addBranch(op, loc);
3715}
3716
Jamie Madillb98c3a82015-07-23 14:26:04 -04003717TIntermBranch *TParseContext::addBranch(TOperator op,
3718 TIntermTyped *returnValue,
3719 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02003720{
3721 ASSERT(op == EOpReturn);
3722 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003723 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02003724 {
3725 error(loc, "void function cannot return a value", "return");
3726 recover();
3727 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003728 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02003729 {
3730 error(loc, "function return is not matching type:", "return");
3731 recover();
3732 }
3733 return intermediate.addBranch(op, returnValue, loc);
3734}
3735
Jamie Madillb98c3a82015-07-23 14:26:04 -04003736TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
3737 TIntermNode *paramNode,
3738 TIntermNode *thisNode,
3739 const TSourceLoc &loc,
3740 bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003741{
Jamie Madillb98c3a82015-07-23 14:26:04 -04003742 *fatalError = false;
3743 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003744 TIntermTyped *callNode = nullptr;
3745
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003746 if (thisNode != nullptr)
3747 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003748 TConstantUnion *unionArray = new TConstantUnion[1];
Jamie Madillb98c3a82015-07-23 14:26:04 -04003749 int arraySize = 0;
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003750 TIntermTyped *typedThis = thisNode->getAsTyped();
3751 if (fnCall->getName() != "length")
3752 {
3753 error(loc, "invalid method", fnCall->getName().c_str());
3754 recover();
3755 }
3756 else if (paramNode != nullptr)
3757 {
3758 error(loc, "method takes no parameters", "length");
3759 recover();
3760 }
3761 else if (typedThis == nullptr || !typedThis->isArray())
3762 {
3763 error(loc, "length can only be called on arrays", "length");
3764 recover();
3765 }
3766 else
3767 {
Olli Etuaho96e67382015-04-23 14:27:02 +03003768 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03003769 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003770 {
Olli Etuaho39282e12015-04-23 15:41:48 +03003771 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003772 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03003773 // (func()).length()
3774 // (int[3](0, 1, 2)).length()
Jamie Madillb98c3a82015-07-23 14:26:04 -04003775 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
3776 // expression.
3777 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
3778 // spec section 5.9 which allows "An array, vector or matrix expression with the
3779 // length method applied".
3780 error(loc, "length can only be called on array names, not on array expressions",
3781 "length");
Olli Etuaho39282e12015-04-23 15:41:48 +03003782 recover();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003783 }
3784 }
Olli Etuaho96e67382015-04-23 14:27:02 +03003785 unionArray->setIConst(arraySize);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003786 callNode =
3787 intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003788 }
3789 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003790 {
3791 //
3792 // Then this should be a constructor.
3793 // Don't go through the symbol table for constructors.
3794 // Their parameters will be verified algorithmically.
3795 //
3796 TType type(EbtVoid, EbpUndefined); // use this to get the type back
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003797 if (!constructorErrorCheck(loc, paramNode, *fnCall, op, &type))
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003798 {
3799 //
3800 // It's a constructor, of type 'type'.
3801 //
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003802 callNode = addConstructor(paramNode, &type, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003803 }
Olli Etuaho72ba85b2015-03-04 14:23:26 +02003804
3805 if (callNode == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003806 {
3807 recover();
3808 callNode = intermediate.setAggregateOperator(nullptr, op, loc);
3809 }
3810 callNode->setType(type);
3811 }
3812 else
3813 {
3814 //
3815 // Not a constructor. Find it in the symbol table.
3816 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303817 const TFunction *fnCandidate;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003818 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003819 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003820 if (fnCandidate)
3821 {
3822 //
3823 // A declared function.
3824 //
3825 if (builtIn && !fnCandidate->getExtension().empty() &&
3826 extensionErrorCheck(loc, fnCandidate->getExtension()))
3827 {
3828 recover();
3829 }
3830 op = fnCandidate->getBuiltInOp();
3831 if (builtIn && op != EOpNull)
3832 {
3833 //
3834 // A function call mapped to a built-in operation.
3835 //
3836 if (fnCandidate->getParamCount() == 1)
3837 {
3838 //
3839 // Treat it like a built-in unary operator.
3840 //
Olli Etuaho15c2ac32015-11-09 15:51:43 +02003841 TIntermAggregate *paramAgg = paramNode->getAsAggregate();
3842 paramNode = paramAgg->getSequence()->front();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003843 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc,
3844 &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003845 if (callNode == nullptr)
3846 {
3847 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003848 extraInfoStream
3849 << "built in unary operator function. Type: "
3850 << static_cast<TIntermTyped *>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003851 std::string extraInfo = extraInfoStream.str();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003852 error(paramNode->getLine(), " wrong operand type", "Internal Error",
3853 extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003854 *fatalError = true;
3855 return nullptr;
3856 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003857 }
3858 else
3859 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003860 TIntermAggregate *aggregate =
3861 intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003862 aggregate->setType(fnCandidate->getReturnType());
3863 aggregate->setPrecisionFromChildren();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02003864 if (aggregate->areChildrenConstQualified())
3865 {
3866 aggregate->getTypePointer()->setQualifier(EvqConst);
3867 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003868
3869 // Some built-in functions have out parameters too.
3870 functionCallLValueErrorCheck(fnCandidate, aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05303871
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003872 // See if we can constant fold a built-in. Note that this may be possible even
3873 // if it is not const-qualified.
Olli Etuahob43846e2015-06-02 18:18:57 +03003874 TIntermTyped *foldedNode = intermediate.foldAggregateBuiltIn(aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05303875 if (foldedNode)
3876 {
Arun Patole274f0702015-05-05 13:33:30 +05303877 callNode = foldedNode;
3878 }
Olli Etuahob43846e2015-06-02 18:18:57 +03003879 else
3880 {
3881 callNode = aggregate;
3882 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003883 }
3884 }
3885 else
3886 {
3887 // This is a real function call
Jamie Madillb98c3a82015-07-23 14:26:04 -04003888 TIntermAggregate *aggregate =
3889 intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003890 aggregate->setType(fnCandidate->getReturnType());
3891
Jamie Madillb98c3a82015-07-23 14:26:04 -04003892 // this is how we know whether the given function is a builtIn function or a user
3893 // defined function
3894 // if builtIn == false, it's a userDefined -> could be an overloaded
3895 // builtIn function also
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003896 // if builtIn == true, it's definitely a builtIn function with EOpNull
3897 if (!builtIn)
3898 aggregate->setUserDefined();
3899 aggregate->setName(fnCandidate->getMangledName());
Corentin Wallez71d147f2015-02-11 11:15:24 -08003900 aggregate->setFunctionId(fnCandidate->getUniqueId());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003901
3902 // This needs to happen after the name is set
3903 if (builtIn)
3904 aggregate->setBuiltInFunctionPrecision();
3905
3906 callNode = aggregate;
3907
3908 functionCallLValueErrorCheck(fnCandidate, aggregate);
3909 }
3910 }
3911 else
3912 {
3913 // error message was put out by findFunction()
3914 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003915 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003916 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003917 callNode = intermediate.addConstantUnion(unionArray,
3918 TType(EbtFloat, EbpUndefined, EvqConst), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003919 recover();
3920 }
3921 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003922 return callNode;
3923}
3924
Jamie Madillb98c3a82015-07-23 14:26:04 -04003925TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
3926 TIntermTyped *trueBlock,
3927 TIntermTyped *falseBlock,
Olli Etuaho52901742015-04-15 13:42:45 +03003928 const TSourceLoc &loc)
3929{
3930 if (boolErrorCheck(loc, cond))
3931 recover();
3932
3933 if (trueBlock->getType() != falseBlock->getType())
3934 {
3935 binaryOpError(loc, ":", trueBlock->getCompleteString(), falseBlock->getCompleteString());
3936 recover();
3937 return falseBlock;
3938 }
Olli Etuahoa2d53032015-04-15 14:14:44 +03003939 // ESSL1 sections 5.2 and 5.7:
3940 // ESSL3 section 5.7:
3941 // Ternary operator is not among the operators allowed for structures/arrays.
3942 if (trueBlock->isArray() || trueBlock->getBasicType() == EbtStruct)
3943 {
3944 error(loc, "ternary operator is not allowed for structures or arrays", ":");
3945 recover();
3946 return falseBlock;
3947 }
Olli Etuaho52901742015-04-15 13:42:45 +03003948 return intermediate.addSelection(cond, trueBlock, falseBlock, loc);
3949}
Olli Etuaho49300862015-02-20 14:54:49 +02003950
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003951//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003952// Parse an array of strings using yyparse.
3953//
3954// Returns 0 for success.
3955//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003956int PaParseStrings(size_t count,
3957 const char *const string[],
3958 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05303959 TParseContext *context)
3960{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003961 if ((count == 0) || (string == NULL))
3962 return 1;
3963
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003964 if (glslang_initialize(context))
3965 return 1;
3966
alokp@chromium.org408c45e2012-04-05 15:54:43 +00003967 int error = glslang_scan(count, string, length, context);
3968 if (!error)
3969 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003970
alokp@chromium.org73bc2982012-06-19 18:48:05 +00003971 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00003972
alokp@chromium.org6b495712012-06-29 00:06:58 +00003973 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003974}