blob: dd77b63791d980461f8d9fba9be4179934c34ccb [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// Look at a '.' field selector string and change it into offsets
131// for a matrix.
132//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400133bool TParseContext::parseMatrixFields(const TString &compString,
134 int matCols,
135 int matRows,
136 TMatrixFields &fields,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530137 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000138{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000139 fields.wholeRow = false;
140 fields.wholeCol = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400141 fields.row = -1;
142 fields.col = -1;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000143
Arun Patole7e7e68d2015-05-22 12:02:25 +0530144 if (compString.size() != 2)
145 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000146 error(line, "illegal length of matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000147 return false;
148 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000149
Arun Patole7e7e68d2015-05-22 12:02:25 +0530150 if (compString[0] == '_')
151 {
152 if (compString[1] < '0' || compString[1] > '3')
153 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000154 error(line, "illegal matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000155 return false;
156 }
157 fields.wholeCol = true;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400158 fields.col = compString[1] - '0';
Arun Patole7e7e68d2015-05-22 12:02:25 +0530159 }
160 else if (compString[1] == '_')
161 {
162 if (compString[0] < '0' || compString[0] > '3')
163 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000164 error(line, "illegal matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000165 return false;
166 }
167 fields.wholeRow = true;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400168 fields.row = compString[0] - '0';
Arun Patole7e7e68d2015-05-22 12:02:25 +0530169 }
170 else
171 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400172 if (compString[0] < '0' || compString[0] > '3' || compString[1] < '0' ||
173 compString[1] > '3')
Arun Patole7e7e68d2015-05-22 12:02:25 +0530174 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000175 error(line, "illegal matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000176 return false;
177 }
178 fields.row = compString[0] - '0';
179 fields.col = compString[1] - '0';
180 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000181
Arun Patole7e7e68d2015-05-22 12:02:25 +0530182 if (fields.row >= matRows || fields.col >= matCols)
183 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000184 error(line, "matrix field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000185 return false;
186 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000187
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000188 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000189}
190
191///////////////////////////////////////////////////////////////////////
192//
193// Errors
194//
195////////////////////////////////////////////////////////////////////////
196
197//
198// Track whether errors have occurred.
199//
200void TParseContext::recover()
201{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000202}
203
204//
205// Used by flex/bison to output all syntax and parsing errors.
206//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530207void TParseContext::error(const TSourceLoc &loc,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400208 const char *reason,
209 const char *token,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530210 const char *extraInfo)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000211{
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000212 pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -0400213 srcLoc.file = loc.first_file;
214 srcLoc.line = loc.first_line;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400215 mDiagnostics.writeInfo(pp::Diagnostics::PP_ERROR, srcLoc, reason, token, extraInfo);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000216}
217
Arun Patole7e7e68d2015-05-22 12:02:25 +0530218void TParseContext::warning(const TSourceLoc &loc,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400219 const char *reason,
220 const char *token,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530221 const char *extraInfo)
222{
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000223 pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -0400224 srcLoc.file = loc.first_file;
225 srcLoc.line = loc.first_line;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400226 mDiagnostics.writeInfo(pp::Diagnostics::PP_WARNING, srcLoc, reason, token, extraInfo);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000227}
228
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000229//
230// Same error message for all places assignments don't work.
231//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530232void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000233{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000234 std::stringstream extraInfoStream;
235 extraInfoStream << "cannot convert from '" << right << "' to '" << left << "'";
236 std::string extraInfo = extraInfoStream.str();
237 error(line, "", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000238}
239
240//
241// Same error message for all places unary operations don't work.
242//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530243void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000244{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000245 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400246 extraInfoStream << "no operation '" << op << "' exists that takes an operand of type "
247 << operand << " (or there is no acceptable conversion)";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000248 std::string extraInfo = extraInfoStream.str();
249 error(line, " wrong operand type", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000250}
251
252//
253// Same error message for all binary operations don't work.
254//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400255void TParseContext::binaryOpError(const TSourceLoc &line,
256 const char *op,
257 TString left,
258 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000259{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000260 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400261 extraInfoStream << "no operation '" << op << "' exists that takes a left-hand operand of type '"
262 << left << "' and a right operand of type '" << right
263 << "' (or there is no acceptable conversion)";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000264 std::string extraInfo = extraInfoStream.str();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530265 error(line, " wrong operand types ", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000266}
267
Jamie Madillb98c3a82015-07-23 14:26:04 -0400268bool TParseContext::precisionErrorCheck(const TSourceLoc &line,
269 TPrecision precision,
270 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530271{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400272 if (!mChecksPrecisionErrors)
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000273 return false;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400274 switch (type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530275 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400276 case EbtFloat:
277 if (precision == EbpUndefined)
278 {
279 error(line, "No precision specified for (float)", "");
280 return true;
281 }
282 break;
283 case EbtInt:
284 if (precision == EbpUndefined)
285 {
286 error(line, "No precision specified (int)", "");
287 return true;
288 }
289 break;
290 default:
291 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000292 }
293 return false;
294}
295
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000296//
297// Both test and if necessary, spit out an error, to see if the node is really
298// an l-value that can be operated on this way.
299//
300// Returns true if the was an error.
301//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530302bool TParseContext::lValueErrorCheck(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000303{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400304 TIntermSymbol *symNode = node->getAsSymbolNode();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530305 TIntermBinary *binaryNode = node->getAsBinaryNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000306
Arun Patole7e7e68d2015-05-22 12:02:25 +0530307 if (binaryNode)
308 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000309 bool errorReturn;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000310
Jamie Madillb98c3a82015-07-23 14:26:04 -0400311 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530312 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400313 case EOpIndexDirect:
314 case EOpIndexIndirect:
315 case EOpIndexDirectStruct:
316 case EOpIndexDirectInterfaceBlock:
317 return lValueErrorCheck(line, op, binaryNode->getLeft());
318 case EOpVectorSwizzle:
319 errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft());
320 if (!errorReturn)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530321 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400322 int offset[4] = {0, 0, 0, 0};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000323
Jamie Madillb98c3a82015-07-23 14:26:04 -0400324 TIntermTyped *rightNode = binaryNode->getRight();
325 TIntermAggregate *aggrNode = rightNode->getAsAggregate();
326
327 for (TIntermSequence::iterator p = aggrNode->getSequence()->begin();
328 p != aggrNode->getSequence()->end(); p++)
329 {
330 int value = (*p)->getAsTyped()->getAsConstantUnion()->getIConst(0);
331 offset[value]++;
332 if (offset[value] > 1)
333 {
334 error(line, " l-value of swizzle cannot have duplicate components", op);
335
336 return true;
337 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000338 }
339 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000340
Jamie Madillb98c3a82015-07-23 14:26:04 -0400341 return errorReturn;
342 default:
343 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000344 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000345 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000346
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000347 return true;
348 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000349
Arun Patole7e7e68d2015-05-22 12:02:25 +0530350 const char *symbol = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000351 if (symNode != 0)
352 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000353
Arun Patole7e7e68d2015-05-22 12:02:25 +0530354 const char *message = 0;
355 switch (node->getQualifier())
356 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400357 case EvqConst:
358 message = "can't modify a const";
359 break;
360 case EvqConstReadOnly:
361 message = "can't modify a const";
362 break;
363 case EvqAttribute:
364 message = "can't modify an attribute";
365 break;
366 case EvqFragmentIn:
367 message = "can't modify an input";
368 break;
369 case EvqVertexIn:
370 message = "can't modify an input";
371 break;
372 case EvqUniform:
373 message = "can't modify a uniform";
374 break;
375 case EvqVaryingIn:
376 message = "can't modify a varying";
377 break;
378 case EvqFragCoord:
379 message = "can't modify gl_FragCoord";
380 break;
381 case EvqFrontFacing:
382 message = "can't modify gl_FrontFacing";
383 break;
384 case EvqPointCoord:
385 message = "can't modify gl_PointCoord";
386 break;
387 default:
388 //
389 // Type that can't be written to?
390 //
391 if (node->getBasicType() == EbtVoid)
392 {
393 message = "can't modify void";
394 }
395 if (IsSampler(node->getBasicType()))
396 {
397 message = "can't modify a sampler";
398 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000399 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000400
Arun Patole7e7e68d2015-05-22 12:02:25 +0530401 if (message == 0 && binaryNode == 0 && symNode == 0)
402 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000403 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000404
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000405 return true;
406 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000407
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000408 //
409 // Everything else is okay, no error.
410 //
411 if (message == 0)
412 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000413
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000414 //
415 // If we get here, we have an error and a message.
416 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530417 if (symNode)
418 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000419 std::stringstream extraInfoStream;
420 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
421 std::string extraInfo = extraInfoStream.str();
422 error(line, " l-value required", op, extraInfo.c_str());
423 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530424 else
425 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000426 std::stringstream extraInfoStream;
427 extraInfoStream << "(" << message << ")";
428 std::string extraInfo = extraInfoStream.str();
429 error(line, " l-value required", op, extraInfo.c_str());
430 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000431
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000432 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000433}
434
435//
436// Both test, and if necessary spit out an error, to see if the node is really
437// a constant.
438//
439// Returns true if the was an error.
440//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530441bool TParseContext::constErrorCheck(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000442{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000443 if (node->getQualifier() == EvqConst)
444 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000445
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000446 error(node->getLine(), "constant expression required", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000447
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000448 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000449}
450
451//
452// Both test, and if necessary spit out an error, to see if the node is really
453// an integer.
454//
455// Returns true if the was an error.
456//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530457bool TParseContext::integerErrorCheck(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000458{
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000459 if (node->isScalarInt())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000460 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000461
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000462 error(node->getLine(), "integer expression required", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000463
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000464 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000465}
466
467//
468// Both test, and if necessary spit out an error, to see if we are currently
469// globally scoped.
470//
471// Returns true if the was an error.
472//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530473bool TParseContext::globalErrorCheck(const TSourceLoc &line, bool global, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000474{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000475 if (global)
476 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000477
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000478 error(line, "only allowed at global scope", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000479
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000480 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000481}
482
483//
484// For now, keep it simple: if it starts "gl_", it's reserved, independent
485// of scope. Except, if the symbol table is at the built-in push-level,
486// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000487// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
488// webgl shader.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000489//
490// Returns true if there was an error.
491//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530492bool TParseContext::reservedErrorCheck(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000493{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530494 static const char *reservedErrMsg = "reserved built-in name";
495 if (!symbolTable.atBuiltInLevel())
496 {
497 if (identifier.compare(0, 3, "gl_") == 0)
498 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000499 error(line, reservedErrMsg, "gl_");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000500 return true;
501 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530502 if (IsWebGLBasedSpec(mShaderSpec))
503 {
504 if (identifier.compare(0, 6, "webgl_") == 0)
505 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000506 error(line, reservedErrMsg, "webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000507 return true;
508 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530509 if (identifier.compare(0, 7, "_webgl_") == 0)
510 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000511 error(line, reservedErrMsg, "_webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000512 return true;
513 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530514 if (mShaderSpec == SH_CSS_SHADERS_SPEC && identifier.compare(0, 4, "css_") == 0)
515 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000516 error(line, reservedErrMsg, "css_");
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000517 return true;
518 }
alokp@chromium.org613ef312010-07-21 18:54:22 +0000519 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530520 if (identifier.find("__") != TString::npos)
521 {
522 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400523 "identifiers containing two consecutive underscores (__) are reserved as "
524 "possible future keywords",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530525 identifier.c_str());
daniel@transgaming.combeadd5d2012-04-12 02:35:31 +0000526 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000527 }
528 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000529
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000530 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000531}
532
533//
534// Make sure there is enough data provided to the constructor to build
535// something of the type of the constructor. Also returns the type of
536// the constructor.
537//
538// Returns true if there was an error in construction.
539//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400540bool TParseContext::constructorErrorCheck(const TSourceLoc &line,
541 TIntermNode *node,
542 TFunction &function,
543 TOperator op,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530544 TType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000545{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000546 *type = function.getReturnType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000547
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000548 bool constructingMatrix = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400549 switch (op)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530550 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400551 case EOpConstructMat2:
552 case EOpConstructMat2x3:
553 case EOpConstructMat2x4:
554 case EOpConstructMat3x2:
555 case EOpConstructMat3:
556 case EOpConstructMat3x4:
557 case EOpConstructMat4x2:
558 case EOpConstructMat4x3:
559 case EOpConstructMat4:
560 constructingMatrix = true;
561 break;
562 default:
563 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000564 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000565
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000566 //
567 // Note: It's okay to have too many components available, but not okay to have unused
568 // arguments. 'full' will go to true when enough args have been seen. If we loop
569 // again, there is an extra argument, so 'overfull' will become true.
570 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000571
Jamie Madillb98c3a82015-07-23 14:26:04 -0400572 size_t size = 0;
573 bool constType = true;
574 bool full = false;
575 bool overFull = false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000576 bool matrixInMatrix = false;
577 bool arrayArg = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530578 for (size_t i = 0; i < function.getParamCount(); ++i)
579 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700580 const TConstParameter &param = function.getParam(i);
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000581 size += param.type->getObjectSize();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530582
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000583 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000584 matrixInMatrix = true;
585 if (full)
586 overFull = true;
587 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
588 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000589 if (param.type->getQualifier() != EvqConst)
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000590 constType = false;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000591 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000592 arrayArg = true;
593 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530594
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000595 if (constType)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000596 type->setQualifier(EvqConst);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000597
Olli Etuaho376f1b52015-04-13 13:23:41 +0300598 if (type->isArray())
599 {
600 if (type->isUnsizedArray())
601 {
Cooper Partinc5cf9bc2015-08-06 10:46:48 -0700602 type->setArraySize(static_cast<int>(function.getParamCount()));
Olli Etuaho376f1b52015-04-13 13:23:41 +0300603 }
604 else if (static_cast<size_t>(type->getArraySize()) != function.getParamCount())
605 {
606 error(line, "array constructor needs one argument per array element", "constructor");
607 return true;
608 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000609 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000610
Arun Patole7e7e68d2015-05-22 12:02:25 +0530611 if (arrayArg && op != EOpConstructStruct)
612 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000613 error(line, "constructing from a non-dereferenced array", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000614 return true;
615 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000616
Arun Patole7e7e68d2015-05-22 12:02:25 +0530617 if (matrixInMatrix && !type->isArray())
618 {
619 if (function.getParamCount() != 1)
620 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400621 error(line, "constructing matrix from matrix can only take one argument",
622 "constructor");
623 return true;
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000624 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000625 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000626
Arun Patole7e7e68d2015-05-22 12:02:25 +0530627 if (overFull)
628 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000629 error(line, "too many arguments", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000630 return true;
631 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530632
Jamie Madillb98c3a82015-07-23 14:26:04 -0400633 if (op == EOpConstructStruct && !type->isArray() &&
634 type->getStruct()->fields().size() != function.getParamCount())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530635 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400636 error(line,
637 "Number of constructor parameters does not match the number of structure fields",
638 "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000639 return true;
640 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000641
Arun Patole7e7e68d2015-05-22 12:02:25 +0530642 if (!type->isMatrix() || !matrixInMatrix)
643 {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000644 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
Arun Patole7e7e68d2015-05-22 12:02:25 +0530645 (op == EOpConstructStruct && size < type->getObjectSize()))
646 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000647 error(line, "not enough data provided for construction", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000648 return true;
649 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000650 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000651
daniel@transgaming.com0b53fc02011-03-09 15:12:12 +0000652 TIntermTyped *typed = node ? node->getAsTyped() : 0;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530653 if (typed == 0)
654 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000655 error(line, "constructor argument does not have a type", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000656 return true;
657 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530658 if (op != EOpConstructStruct && IsSampler(typed->getBasicType()))
659 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000660 error(line, "cannot convert a sampler", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000661 return true;
662 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530663 if (typed->getBasicType() == EbtVoid)
664 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000665 error(line, "cannot convert a void", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000666 return true;
667 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000668
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000669 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000670}
671
Jamie Madillb98c3a82015-07-23 14:26:04 -0400672// This function checks to see if a void variable has been declared and raise an error message for
673// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000674//
675// returns true in case of an error
676//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400677bool TParseContext::voidErrorCheck(const TSourceLoc &line,
678 const TString &identifier,
679 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000680{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300681 if (type == EbtVoid)
682 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000683 error(line, "illegal use of type 'void'", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000684 return true;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300685 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000686
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000687 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000688}
689
Jamie Madillb98c3a82015-07-23 14:26:04 -0400690// This function checks to see if the node (for the expression) contains a scalar boolean expression
691// or not
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000692//
693// returns true in case of an error
694//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530695bool TParseContext::boolErrorCheck(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000696{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530697 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector())
698 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000699 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000700 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530701 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000702
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000703 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000704}
705
Jamie Madillb98c3a82015-07-23 14:26:04 -0400706// This function checks to see if the node (for the expression) contains a scalar boolean expression
707// or not
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000708//
709// returns true in case of an error
710//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530711bool TParseContext::boolErrorCheck(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000712{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530713 if (pType.type != EbtBool || pType.isAggregate())
714 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000715 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000716 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530717 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000718
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000719 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000720}
721
Jamie Madillb98c3a82015-07-23 14:26:04 -0400722bool TParseContext::samplerErrorCheck(const TSourceLoc &line,
723 const TPublicType &pType,
724 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000725{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530726 if (pType.type == EbtStruct)
727 {
728 if (containsSampler(*pType.userDef))
729 {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000730 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530731
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000732 return true;
733 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530734
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000735 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530736 }
737 else if (IsSampler(pType.type))
738 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000739 error(line, reason, getBasicString(pType.type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000740
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000741 return true;
742 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000743
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000744 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000745}
746
Arun Patole7e7e68d2015-05-22 12:02:25 +0530747bool TParseContext::locationDeclaratorListCheck(const TSourceLoc &line, const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400748{
749 if (pType.layoutQualifier.location != -1)
750 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400751 error(line, "location must only be specified for a single input or output variable",
752 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400753 return true;
754 }
755
756 return false;
757}
758
Jamie Madillb98c3a82015-07-23 14:26:04 -0400759bool TParseContext::parameterSamplerErrorCheck(const TSourceLoc &line,
760 TQualifier qualifier,
761 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000762{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400763 if ((qualifier == EvqOut || qualifier == EvqInOut) && type.getBasicType() != EbtStruct &&
764 IsSampler(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530765 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000766 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000767 return true;
768 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000769
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000770 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000771}
772
Arun Patole7e7e68d2015-05-22 12:02:25 +0530773bool TParseContext::containsSampler(const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000774{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000775 if (IsSampler(type.getBasicType()))
776 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000777
Arun Patole7e7e68d2015-05-22 12:02:25 +0530778 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock())
779 {
780 const TFieldList &fields = type.getStruct()->fields();
781 for (unsigned int i = 0; i < fields.size(); ++i)
782 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400783 if (containsSampler(*fields[i]->type()))
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000784 return true;
785 }
786 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000787
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000788 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000789}
790
791//
792// Do size checking for an array type's size.
793//
794// Returns true if there was an error.
795//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530796bool TParseContext::arraySizeErrorCheck(const TSourceLoc &line, TIntermTyped *expr, int &size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000797{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530798 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000799
Olli Etuahoe7847b02015-03-16 11:56:12 +0200800 if (constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000801 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000802 error(line, "array size must be a constant integer expression", "");
Olli Etuahoe7847b02015-03-16 11:56:12 +0200803 size = 1;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000804 return true;
805 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000806
Nicolas Capens906744a2014-06-06 15:18:07 -0400807 unsigned int unsignedSize = 0;
808
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000809 if (constant->getBasicType() == EbtUInt)
810 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400811 unsignedSize = constant->getUConst(0);
Jamie Madillb98c3a82015-07-23 14:26:04 -0400812 size = static_cast<int>(unsignedSize);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000813 }
814 else
815 {
816 size = constant->getIConst(0);
817
Nicolas Capens906744a2014-06-06 15:18:07 -0400818 if (size < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000819 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400820 error(line, "array size must be non-negative", "");
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000821 size = 1;
822 return true;
823 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400824
825 unsignedSize = static_cast<unsigned int>(size);
826 }
827
828 if (size == 0)
829 {
830 error(line, "array size must be greater than zero", "");
831 size = 1;
832 return true;
833 }
834
835 // The size of arrays is restricted here to prevent issues further down the
836 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
837 // 4096 registers so this should be reasonable even for aggressively optimizable code.
838 const unsigned int sizeLimit = 65536;
839
840 if (unsignedSize > sizeLimit)
841 {
842 error(line, "array size too large", "");
843 size = 1;
844 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000845 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000846
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000847 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000848}
849
850//
851// See if this qualifier can be an array.
852//
853// Returns true if there is an error.
854//
Olli Etuaho3739d232015-04-08 12:23:44 +0300855bool TParseContext::arrayQualifierErrorCheck(const TSourceLoc &line, const TPublicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000856{
Olli Etuaho3739d232015-04-08 12:23:44 +0300857 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqVertexIn) ||
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400858 (type.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300859 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400860 error(line, "cannot declare arrays of this qualifier",
861 TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000862 return true;
863 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000864
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000865 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000866}
867
868//
869// See if this type can be an array.
870//
871// Returns true if there is an error.
872//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530873bool TParseContext::arrayTypeErrorCheck(const TSourceLoc &line, const TPublicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000874{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000875 //
876 // Can the type be an array?
877 //
Jamie Madill06145232015-05-13 13:10:01 -0400878 if (type.array)
879 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000880 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000881 return true;
882 }
Olli Etuahocc36b982015-07-10 14:14:18 +0300883 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
884 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
885 // 4.3.4).
886 if (mShaderVersion >= 300 && type.type == EbtStruct && sh::IsVarying(type.qualifier))
887 {
888 error(line, "cannot declare arrays of structs of this qualifier",
889 TType(type).getCompleteString().c_str());
890 return true;
891 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000892
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000893 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000894}
895
896//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000897// Enforce non-initializer type/qualifier rules.
898//
899// Returns true if there was an error.
900//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400901bool TParseContext::nonInitErrorCheck(const TSourceLoc &line,
902 const TString &identifier,
903 TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000904{
Olli Etuaho3739d232015-04-08 12:23:44 +0300905 ASSERT(type != nullptr);
906 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000907 {
908 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300909 type->qualifier = EvqTemporary;
910
911 // Generate informative error messages for ESSL1.
912 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400913 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000914 {
Arun Patole7e7e68d2015-05-22 12:02:25 +0530915 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400916 "structures containing arrays may not be declared constant since they cannot be "
917 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530918 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000919 }
920 else
921 {
922 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
923 }
924
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000925 return true;
926 }
Olli Etuaho376f1b52015-04-13 13:23:41 +0300927 if (type->isUnsizedArray())
928 {
929 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
930 return true;
931 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000932 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000933}
934
Olli Etuaho2935c582015-04-08 14:32:06 +0300935// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000936// and update the symbol table.
937//
Olli Etuaho2935c582015-04-08 14:32:06 +0300938// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000939//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400940bool TParseContext::declareVariable(const TSourceLoc &line,
941 const TString &identifier,
942 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +0300943 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000944{
Olli Etuaho2935c582015-04-08 14:32:06 +0300945 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000946
Olli Etuaho2935c582015-04-08 14:32:06 +0300947 bool needsReservedErrorCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000948
Olli Etuaho2935c582015-04-08 14:32:06 +0300949 // gl_LastFragData may be redeclared with a new precision qualifier
950 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
951 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400952 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
953 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho2935c582015-04-08 14:32:06 +0300954 if (type.getArraySize() == maxDrawBuffers->getConstPointer()->getIConst())
955 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400956 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +0300957 {
958 needsReservedErrorCheck = extensionErrorCheck(line, builtInSymbol->getExtension());
959 }
960 }
961 else
962 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400963 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
964 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +0300965 return false;
966 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000967 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000968
Olli Etuaho2935c582015-04-08 14:32:06 +0300969 if (needsReservedErrorCheck && reservedErrorCheck(line, identifier))
970 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000971
Olli Etuaho2935c582015-04-08 14:32:06 +0300972 (*variable) = new TVariable(&identifier, type);
973 if (!symbolTable.declare(*variable))
974 {
975 error(line, "redefinition", identifier.c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -0400976 *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +0300977 return false;
978 }
979
980 if (voidErrorCheck(line, identifier, type.getBasicType()))
981 return false;
982
983 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000984}
985
Jamie Madillb98c3a82015-07-23 14:26:04 -0400986bool TParseContext::paramErrorCheck(const TSourceLoc &line,
987 TQualifier qualifier,
988 TQualifier paramQualifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530989 TType *type)
990{
991 if (qualifier != EvqConst && qualifier != EvqTemporary)
992 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000993 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000994 return true;
995 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530996 if (qualifier == EvqConst && paramQualifier != EvqIn)
997 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400998 error(line, "qualifier not allowed with ", getQualifierString(qualifier),
999 getQualifierString(paramQualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001000 return true;
1001 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001002
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001003 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +00001004 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001005 else
alokp@chromium.org58e54292010-08-24 21:40:03 +00001006 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001007
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001008 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001009}
1010
Arun Patole7e7e68d2015-05-22 12:02:25 +05301011bool TParseContext::extensionErrorCheck(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001012{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001013 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001014 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +05301015 if (iter == extBehavior.end())
1016 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001017 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001018 return true;
1019 }
zmo@google.comf5450912011-09-09 01:37:19 +00001020 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +05301021 if (iter->second == EBhDisable || iter->second == EBhUndefined)
1022 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001023 error(line, "extension", extension.c_str(), "is disabled");
1024 return true;
1025 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301026 if (iter->second == EBhWarn)
1027 {
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001028 warning(line, "extension", extension.c_str(), "is being used");
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001029 return false;
1030 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001031
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001032 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001033}
1034
Jamie Madillb98c3a82015-07-23 14:26:04 -04001035// These checks are common for all declarations starting a declarator list, and declarators that
1036// follow an empty declaration.
Olli Etuahofa33d582015-04-09 14:33:12 +03001037//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001038bool TParseContext::singleDeclarationErrorCheck(const TPublicType &publicType,
1039 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001040{
Olli Etuahofa33d582015-04-09 14:33:12 +03001041 switch (publicType.qualifier)
1042 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001043 case EvqVaryingIn:
1044 case EvqVaryingOut:
1045 case EvqAttribute:
1046 case EvqVertexIn:
1047 case EvqFragmentOut:
1048 if (publicType.type == EbtStruct)
1049 {
1050 error(identifierLocation, "cannot be used with a structure",
1051 getQualifierString(publicType.qualifier));
1052 return true;
1053 }
Olli Etuahofa33d582015-04-09 14:33:12 +03001054
Jamie Madillb98c3a82015-07-23 14:26:04 -04001055 default:
1056 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001057 }
1058
Jamie Madillb98c3a82015-07-23 14:26:04 -04001059 if (publicType.qualifier != EvqUniform &&
1060 samplerErrorCheck(identifierLocation, publicType, "samplers must be uniform"))
Olli Etuahofa33d582015-04-09 14:33:12 +03001061 {
Jamie Madilla5efff92013-06-06 11:56:47 -04001062 return true;
Olli Etuahofa33d582015-04-09 14:33:12 +03001063 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001064
1065 // check for layout qualifier issues
1066 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
1067
1068 if (layoutQualifier.matrixPacking != EmpUnspecified)
1069 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001070 error(identifierLocation, "layout qualifier",
1071 getMatrixPackingString(layoutQualifier.matrixPacking),
Olli Etuahofa33d582015-04-09 14:33:12 +03001072 "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -04001073 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001074 }
1075
1076 if (layoutQualifier.blockStorage != EbsUnspecified)
1077 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001078 error(identifierLocation, "layout qualifier",
1079 getBlockStorageString(layoutQualifier.blockStorage),
Olli Etuahofa33d582015-04-09 14:33:12 +03001080 "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -04001081 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001082 }
1083
Olli Etuahofa33d582015-04-09 14:33:12 +03001084 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut &&
1085 layoutLocationErrorCheck(identifierLocation, publicType.layoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001086 {
Jamie Madill51a53c72013-06-19 09:24:43 -04001087 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001088 }
1089
1090 return false;
1091}
1092
Jamie Madillb98c3a82015-07-23 14:26:04 -04001093bool TParseContext::layoutLocationErrorCheck(const TSourceLoc &location,
1094 const TLayoutQualifier &layoutQualifier)
Jamie Madilla5efff92013-06-06 11:56:47 -04001095{
1096 if (layoutQualifier.location != -1)
1097 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001098 error(location, "invalid layout qualifier:", "location",
1099 "only valid on program inputs and outputs");
Jamie Madilla5efff92013-06-06 11:56:47 -04001100 return true;
1101 }
1102
1103 return false;
1104}
1105
Jamie Madillb98c3a82015-07-23 14:26:04 -04001106bool TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate,
1107 TIntermAggregate *aggregate)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001108{
1109 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1110 {
1111 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1112 if (qual == EvqOut || qual == EvqInOut)
1113 {
1114 TIntermTyped *node = (*(aggregate->getSequence()))[i]->getAsTyped();
1115 if (lValueErrorCheck(node->getLine(), "assign", node))
1116 {
1117 error(node->getLine(),
Jamie Madillb98c3a82015-07-23 14:26:04 -04001118 "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error");
Olli Etuahob6e07a62015-02-16 12:22:10 +02001119 recover();
1120 return true;
1121 }
1122 }
1123 }
1124 return false;
1125}
1126
Jamie Madillb98c3a82015-07-23 14:26:04 -04001127void TParseContext::es3InvariantErrorCheck(const TQualifier qualifier,
1128 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001129{
1130 if (!sh::IsVaryingOut(qualifier) && qualifier != EvqFragmentOut)
1131 {
1132 error(invariantLocation, "Only out variables can be invariant.", "invariant");
1133 recover();
1134 }
1135}
1136
Arun Patole7e7e68d2015-05-22 12:02:25 +05301137bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001138{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001139 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001140 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1141 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001142}
1143
Arun Patole7e7e68d2015-05-22 12:02:25 +05301144bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001145{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001146 return ::IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001147}
1148
Jamie Madillb98c3a82015-07-23 14:26:04 -04001149void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1150 const char *extName,
1151 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001152{
1153 pp::SourceLocation srcLoc;
1154 srcLoc.file = loc.first_file;
1155 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001156 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001157}
1158
Jamie Madillb98c3a82015-07-23 14:26:04 -04001159void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1160 const char *name,
1161 const char *value,
1162 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001163{
1164 pp::SourceLocation srcLoc;
1165 srcLoc.file = loc.first_file;
1166 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001167 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001168}
1169
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001170/////////////////////////////////////////////////////////////////////////////////
1171//
1172// Non-Errors.
1173//
1174/////////////////////////////////////////////////////////////////////////////////
1175
Jamie Madill5c097022014-08-20 16:38:32 -04001176const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1177 const TString *name,
1178 const TSymbol *symbol)
1179{
1180 const TVariable *variable = NULL;
1181
1182 if (!symbol)
1183 {
1184 error(location, "undeclared identifier", name->c_str());
1185 recover();
1186 }
1187 else if (!symbol->isVariable())
1188 {
1189 error(location, "variable expected", name->c_str());
1190 recover();
1191 }
1192 else
1193 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001194 variable = static_cast<const TVariable *>(symbol);
Jamie Madill5c097022014-08-20 16:38:32 -04001195
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001196 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
Jamie Madill5c097022014-08-20 16:38:32 -04001197 !variable->getExtension().empty() &&
1198 extensionErrorCheck(location, variable->getExtension()))
1199 {
1200 recover();
1201 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001202
1203 // Reject shaders using both gl_FragData and gl_FragColor
1204 TQualifier qualifier = variable->getType().getQualifier();
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001205 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001206 {
1207 mUsesFragData = true;
1208 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001209 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001210 {
1211 mUsesFragColor = true;
1212 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001213 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1214 {
1215 mUsesSecondaryOutputs = true;
1216 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001217
1218 // This validation is not quite correct - it's only an error to write to
1219 // both FragData and FragColor. For simplicity, and because users shouldn't
1220 // be rewarded for reading from undefined varaibles, return an error
1221 // if they are both referenced, rather than assigned.
1222 if (mUsesFragData && mUsesFragColor)
1223 {
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001224 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1225 if (mUsesSecondaryOutputs)
1226 {
1227 errorMessage =
1228 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1229 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1230 }
1231 error(location, errorMessage, name->c_str());
Jamie Madill14e95b32015-05-07 10:10:41 -04001232 recover();
1233 }
Jamie Madill5c097022014-08-20 16:38:32 -04001234 }
1235
1236 if (!variable)
1237 {
1238 TType type(EbtFloat, EbpUndefined);
1239 TVariable *fakeVariable = new TVariable(name, type);
1240 symbolTable.declare(fakeVariable);
1241 variable = fakeVariable;
1242 }
1243
1244 return variable;
1245}
1246
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001247//
1248// Look up a function name in the symbol table, and make sure it is a function.
1249//
1250// Return the function symbol if found, otherwise 0.
1251//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001252const TFunction *TParseContext::findFunction(const TSourceLoc &line,
1253 TFunction *call,
1254 int inputShaderVersion,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301255 bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001256{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001257 // First find by unmangled name to check whether the function name has been
1258 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001259 // If a function is found, check for one with a matching argument list.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301260 const TSymbol *symbol = symbolTable.find(call->getName(), inputShaderVersion, builtIn);
1261 if (symbol == 0 || symbol->isFunction())
1262 {
Austin Kinross3ae64652015-01-26 15:51:39 -08001263 symbol = symbolTable.find(call->getMangledName(), inputShaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001264 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001265
Arun Patole7e7e68d2015-05-22 12:02:25 +05301266 if (symbol == 0)
1267 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001268 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001269 return 0;
1270 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001271
Arun Patole7e7e68d2015-05-22 12:02:25 +05301272 if (!symbol->isFunction())
1273 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001274 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001275 return 0;
1276 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001277
Jamie Madillb98c3a82015-07-23 14:26:04 -04001278 return static_cast<const TFunction *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001279}
1280
1281//
1282// Initializers show up in several places in the grammar. Have one set of
1283// code to handle them here.
1284//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001285// Returns true on error, false if no error
1286//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001287bool TParseContext::executeInitializer(const TSourceLoc &line,
1288 const TString &identifier,
1289 const TPublicType &pType,
1290 TIntermTyped *initializer,
1291 TIntermNode **intermNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001292{
Olli Etuahoe7847b02015-03-16 11:56:12 +02001293 ASSERT(intermNode != nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001294 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001295
Olli Etuaho2935c582015-04-08 14:32:06 +03001296 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001297 if (type.isUnsizedArray())
1298 {
1299 type.setArraySize(initializer->getArraySize());
1300 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001301 if (!declareVariable(line, identifier, type, &variable))
1302 {
1303 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001304 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001305
Olli Etuahob0c645e2015-05-12 14:25:36 +03001306 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001307 if (symbolTable.atGlobalLevel() &&
1308 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001309 {
1310 // Error message does not completely match behavior with ESSL 1.00, but
1311 // we want to steer developers towards only using constant expressions.
1312 error(line, "global variable initializers must be constant expressions", "=");
1313 return true;
1314 }
1315 if (globalInitWarning)
1316 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001317 warning(
1318 line,
1319 "global variable initializers should be constant expressions "
1320 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1321 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001322 }
1323
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001324 //
1325 // identifier must be of type constant, a global, or a temporary
1326 //
1327 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301328 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1329 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001330 error(line, " cannot initialize this type of qualifier ",
1331 variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001332 return true;
1333 }
1334 //
1335 // test for and propagate constant
1336 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001337
Arun Patole7e7e68d2015-05-22 12:02:25 +05301338 if (qualifier == EvqConst)
1339 {
1340 if (qualifier != initializer->getType().getQualifier())
1341 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001342 std::stringstream extraInfoStream;
1343 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1344 std::string extraInfo = extraInfoStream.str();
1345 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001346 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001347 return true;
1348 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301349 if (type != initializer->getType())
1350 {
1351 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001352 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001353 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001354 return true;
1355 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301356 if (initializer->getAsConstantUnion())
1357 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001358 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Arun Patole7e7e68d2015-05-22 12:02:25 +05301359 }
1360 else if (initializer->getAsSymbolNode())
1361 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001362 const TSymbol *symbol =
1363 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1364 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001365
Arun Patole7e7e68d2015-05-22 12:02:25 +05301366 TConstantUnion *constArray = tVar->getConstPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001367 variable->shareConstPointer(constArray);
Arun Patole7e7e68d2015-05-22 12:02:25 +05301368 }
1369 else
1370 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001371 std::stringstream extraInfoStream;
1372 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1373 std::string extraInfo = extraInfoStream.str();
1374 error(line, " cannot assign to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001375 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001376 return true;
1377 }
1378 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001379
1380 if (qualifier != EvqConst)
1381 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001382 TIntermSymbol *intermSymbol = intermediate.addSymbol(
1383 variable->getUniqueId(), variable->getName(), variable->getType(), line);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001384 *intermNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1385 if (*intermNode == nullptr)
1386 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001387 assignError(line, "=", intermSymbol->getCompleteString(),
1388 initializer->getCompleteString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001389 return true;
1390 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001391 }
1392 else
1393 {
1394 *intermNode = nullptr;
1395 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001396
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001397 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001398}
1399
Arun Patole7e7e68d2015-05-22 12:02:25 +05301400bool TParseContext::areAllChildConst(TIntermAggregate *aggrNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001401{
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001402 ASSERT(aggrNode != NULL);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001403 if (!aggrNode->isConstructor())
1404 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001405
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001406 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001407
Arun Patole7e7e68d2015-05-22 12:02:25 +05301408 // check if all the child nodes are constants so that they can be inserted into
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001409 // the parent node
Jamie Madillb98c3a82015-07-23 14:26:04 -04001410 TIntermSequence *sequence = aggrNode->getSequence();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301411 for (TIntermSequence::iterator p = sequence->begin(); p != sequence->end(); ++p)
1412 {
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001413 if (!(*p)->getAsTyped()->getAsConstantUnion())
1414 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001415 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001416
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001417 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001418}
1419
Jamie Madillb98c3a82015-07-23 14:26:04 -04001420TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier,
1421 bool invariant,
1422 TLayoutQualifier layoutQualifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301423 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001424{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001425 TPublicType returnType = typeSpecifier;
1426 returnType.qualifier = qualifier;
1427 returnType.invariant = invariant;
Jamie Madilla5efff92013-06-06 11:56:47 -04001428 returnType.layoutQualifier = layoutQualifier;
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001429
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001430 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001431 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001432 if (typeSpecifier.array)
1433 {
1434 error(typeSpecifier.line, "not supported", "first-class array");
1435 recover();
1436 returnType.clearArrayness();
1437 }
1438
Jamie Madillb98c3a82015-07-23 14:26:04 -04001439 if (qualifier == EvqAttribute &&
1440 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001441 {
1442 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1443 recover();
1444 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001445
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001446 if ((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1447 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1448 {
1449 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1450 recover();
1451 }
1452 }
1453 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001454 {
Olli Etuahoabb0c382015-07-13 12:01:12 +03001455 if (!layoutQualifier.isEmpty())
1456 {
1457 if (globalErrorCheck(typeSpecifier.line, symbolTable.atGlobalLevel(), "layout"))
1458 {
1459 recover();
1460 }
1461 }
Olli Etuahocc36b982015-07-10 14:14:18 +03001462 if (sh::IsVarying(qualifier) || qualifier == EvqVertexIn || qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001463 {
Olli Etuahocc36b982015-07-10 14:14:18 +03001464 es3InputOutputTypeCheck(qualifier, typeSpecifier, typeSpecifier.line);
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001465 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001466 }
1467
1468 return returnType;
1469}
1470
Olli Etuahocc36b982015-07-10 14:14:18 +03001471void TParseContext::es3InputOutputTypeCheck(const TQualifier qualifier,
1472 const TPublicType &type,
1473 const TSourceLoc &qualifierLocation)
1474{
1475 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
1476 if (type.type == EbtBool)
1477 {
1478 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
1479 recover();
1480 }
1481
1482 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
1483 switch (qualifier)
1484 {
1485 case EvqVertexIn:
1486 // ESSL 3.00 section 4.3.4
1487 if (type.array)
1488 {
1489 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
1490 recover();
1491 }
1492 // Vertex inputs with a struct type are disallowed in singleDeclarationErrorCheck
1493 return;
1494 case EvqFragmentOut:
1495 // ESSL 3.00 section 4.3.6
1496 if (type.isMatrix())
1497 {
1498 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
1499 recover();
1500 }
1501 // Fragment outputs with a struct type are disallowed in singleDeclarationErrorCheck
1502 return;
1503 default:
1504 break;
1505 }
1506
1507 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
1508 // restrictions.
1509 bool typeContainsIntegers =
1510 (type.type == EbtInt || type.type == EbtUInt || type.isStructureContainingType(EbtInt) ||
1511 type.isStructureContainingType(EbtUInt));
1512 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
1513 {
1514 error(qualifierLocation, "must use 'flat' interpolation here",
1515 getQualifierString(qualifier));
1516 recover();
1517 }
1518
1519 if (type.type == EbtStruct)
1520 {
1521 // ESSL 3.00 sections 4.3.4 and 4.3.6.
1522 // These restrictions are only implied by the ESSL 3.00 spec, but
1523 // the ESSL 3.10 spec lists these restrictions explicitly.
1524 if (type.array)
1525 {
1526 error(qualifierLocation, "cannot be an array of structures",
1527 getQualifierString(qualifier));
1528 recover();
1529 }
1530 if (type.isStructureContainingArrays())
1531 {
1532 error(qualifierLocation, "cannot be a structure containing an array",
1533 getQualifierString(qualifier));
1534 recover();
1535 }
1536 if (type.isStructureContainingType(EbtStruct))
1537 {
1538 error(qualifierLocation, "cannot be a structure containing a structure",
1539 getQualifierString(qualifier));
1540 recover();
1541 }
1542 if (type.isStructureContainingType(EbtBool))
1543 {
1544 error(qualifierLocation, "cannot be a structure containing a bool",
1545 getQualifierString(qualifier));
1546 recover();
1547 }
1548 }
1549}
1550
Olli Etuahofa33d582015-04-09 14:33:12 +03001551TIntermAggregate *TParseContext::parseSingleDeclaration(TPublicType &publicType,
1552 const TSourceLoc &identifierOrTypeLocation,
1553 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001554{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001555 TIntermSymbol *symbol =
1556 intermediate.addSymbol(0, identifier, TType(publicType), identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001557
Olli Etuahobab4c082015-04-24 16:38:49 +03001558 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03001559
Olli Etuahobab4c082015-04-24 16:38:49 +03001560 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1561
1562 if (emptyDeclaration)
1563 {
1564 if (publicType.isUnsizedArray())
1565 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001566 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1567 // error. It is assumed that this applies to empty declarations as well.
1568 error(identifierOrTypeLocation, "empty array declaration needs to specify a size",
1569 identifier.c_str());
Olli Etuahobab4c082015-04-24 16:38:49 +03001570 }
1571 }
1572 else
Jamie Madill60ed9812013-06-06 11:56:46 -04001573 {
Olli Etuahofa33d582015-04-09 14:33:12 +03001574 if (singleDeclarationErrorCheck(publicType, identifierOrTypeLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001575 recover();
1576
Olli Etuaho376f1b52015-04-13 13:23:41 +03001577 if (nonInitErrorCheck(identifierOrTypeLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001578 recover();
1579
Olli Etuaho2935c582015-04-08 14:32:06 +03001580 TVariable *variable = nullptr;
Olli Etuahofa33d582015-04-09 14:33:12 +03001581 if (!declareVariable(identifierOrTypeLocation, identifier, TType(publicType), &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001582 recover();
1583
1584 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001585 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001586 }
1587
Olli Etuahoe7847b02015-03-16 11:56:12 +02001588 return intermediate.makeAggregate(symbol, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001589}
1590
Olli Etuahoe7847b02015-03-16 11:56:12 +02001591TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1592 const TSourceLoc &identifierLocation,
1593 const TString &identifier,
1594 const TSourceLoc &indexLocation,
1595 TIntermTyped *indexExpression)
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 Etuaho376f1b52015-04-13 13:23:41 +03001602 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001603 recover();
1604
Jamie Madillb98c3a82015-07-23 14:26:04 -04001605 if (arrayTypeErrorCheck(indexLocation, publicType) ||
1606 arrayQualifierErrorCheck(indexLocation, publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001607 {
1608 recover();
1609 }
1610
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001611 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001612
1613 int size;
1614 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1615 {
1616 recover();
1617 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001618 // Make the type an array even if size check failed.
1619 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1620 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001621
Olli Etuaho2935c582015-04-08 14:32:06 +03001622 TVariable *variable = nullptr;
1623 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001624 recover();
1625
Olli Etuahoe7847b02015-03-16 11:56:12 +02001626 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001627 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001628 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001629
Olli Etuahoe7847b02015-03-16 11:56:12 +02001630 return intermediate.makeAggregate(symbol, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001631}
1632
Jamie Madill06145232015-05-13 13:10:01 -04001633TIntermAggregate *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001634 const TSourceLoc &identifierLocation,
1635 const TString &identifier,
1636 const TSourceLoc &initLocation,
1637 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001638{
Olli Etuahofa33d582015-04-09 14:33:12 +03001639 mDeferredSingleDeclarationErrorCheck = false;
1640
1641 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001642 recover();
1643
Olli Etuahoe7847b02015-03-16 11:56:12 +02001644 TIntermNode *intermNode = nullptr;
1645 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04001646 {
1647 //
1648 // Build intermediate representation
1649 //
Olli Etuahoe7847b02015-03-16 11:56:12 +02001650 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001651 }
1652 else
1653 {
1654 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001655 return nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001656 }
1657}
1658
Jamie Madillb98c3a82015-07-23 14:26:04 -04001659TIntermAggregate *TParseContext::parseSingleArrayInitDeclaration(
1660 TPublicType &publicType,
1661 const TSourceLoc &identifierLocation,
1662 const TString &identifier,
1663 const TSourceLoc &indexLocation,
1664 TIntermTyped *indexExpression,
1665 const TSourceLoc &initLocation,
1666 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001667{
1668 mDeferredSingleDeclarationErrorCheck = false;
1669
1670 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1671 recover();
1672
Jamie Madillb98c3a82015-07-23 14:26:04 -04001673 if (arrayTypeErrorCheck(indexLocation, publicType) ||
1674 arrayQualifierErrorCheck(indexLocation, publicType))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001675 {
1676 recover();
1677 }
1678
1679 TPublicType arrayType(publicType);
1680
Olli Etuaho376f1b52015-04-13 13:23:41 +03001681 int size = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001682 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
1683 // the initializer.
1684 if (indexExpression != nullptr &&
1685 arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001686 {
1687 recover();
1688 }
1689 // Make the type an array even if size check failed.
1690 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1691 arrayType.setArraySize(size);
1692
1693 // initNode will correspond to the whole of "type b[n] = initializer".
1694 TIntermNode *initNode = nullptr;
1695 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1696 {
1697 return initNode ? intermediate.makeAggregate(initNode, initLocation) : nullptr;
1698 }
1699 else
1700 {
1701 recover();
1702 return nullptr;
1703 }
1704}
1705
Olli Etuahoe7847b02015-03-16 11:56:12 +02001706TIntermAggregate *TParseContext::parseInvariantDeclaration(const TSourceLoc &invariantLoc,
Jamie Madill47e3ec02014-08-20 16:38:33 -04001707 const TSourceLoc &identifierLoc,
1708 const TString *identifier,
1709 const TSymbol *symbol)
1710{
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001711 // invariant declaration
Jamie Madill47e3ec02014-08-20 16:38:33 -04001712 if (globalErrorCheck(invariantLoc, symbolTable.atGlobalLevel(), "invariant varying"))
1713 {
1714 recover();
1715 }
1716
1717 if (!symbol)
1718 {
1719 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
1720 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001721 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001722 }
1723 else
1724 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001725 const TString kGlFrontFacing("gl_FrontFacing");
1726 if (*identifier == kGlFrontFacing)
1727 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001728 error(identifierLoc, "identifier should not be declared as invariant",
1729 identifier->c_str());
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001730 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001731 return nullptr;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001732 }
Jamie Madill2c433252014-12-03 12:36:54 -05001733 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001734 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1735 ASSERT(variable);
1736 const TType &type = variable->getType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04001737 TIntermSymbol *intermSymbol =
1738 intermediate.addSymbol(variable->getUniqueId(), *identifier, type, identifierLoc);
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001739
1740 TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc);
1741 aggregate->setOp(EOpInvariantDeclaration);
1742 return aggregate;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001743 }
1744}
1745
Jamie Madillb98c3a82015-07-23 14:26:04 -04001746TIntermAggregate *TParseContext::parseDeclarator(TPublicType &publicType,
1747 TIntermAggregate *aggregateDeclaration,
1748 const TSourceLoc &identifierLocation,
1749 const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001750{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001751 // If the declaration starting this declarator list was empty (example: int,), some checks were
1752 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001753 if (mDeferredSingleDeclarationErrorCheck)
1754 {
1755 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1756 recover();
1757 mDeferredSingleDeclarationErrorCheck = false;
1758 }
1759
Jamie Madill0bd18df2013-06-20 11:55:52 -04001760 if (locationDeclaratorListCheck(identifierLocation, publicType))
1761 recover();
1762
Olli Etuaho376f1b52015-04-13 13:23:41 +03001763 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001764 recover();
1765
Olli Etuaho2935c582015-04-08 14:32:06 +03001766 TVariable *variable = nullptr;
1767 if (!declareVariable(identifierLocation, identifier, TType(publicType), &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001768 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001769
Jamie Madillb98c3a82015-07-23 14:26:04 -04001770 TIntermSymbol *symbol =
1771 intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001772 if (variable && symbol)
Jamie Madill502d66f2013-06-20 11:55:52 -04001773 symbol->setId(variable->getUniqueId());
1774
Olli Etuahoe7847b02015-03-16 11:56:12 +02001775 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001776}
1777
Jamie Madillb98c3a82015-07-23 14:26:04 -04001778TIntermAggregate *TParseContext::parseArrayDeclarator(TPublicType &publicType,
1779 TIntermAggregate *aggregateDeclaration,
1780 const TSourceLoc &identifierLocation,
1781 const TString &identifier,
1782 const TSourceLoc &arrayLocation,
1783 TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001784{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001785 // If the declaration starting this declarator list was empty (example: int,), some checks were
1786 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001787 if (mDeferredSingleDeclarationErrorCheck)
1788 {
1789 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1790 recover();
1791 mDeferredSingleDeclarationErrorCheck = false;
1792 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001793
Jamie Madill0bd18df2013-06-20 11:55:52 -04001794 if (locationDeclaratorListCheck(identifierLocation, publicType))
1795 recover();
1796
Olli Etuaho376f1b52015-04-13 13:23:41 +03001797 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001798 recover();
1799
Jamie Madillb98c3a82015-07-23 14:26:04 -04001800 if (arrayTypeErrorCheck(arrayLocation, publicType) ||
1801 arrayQualifierErrorCheck(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001802 {
1803 recover();
1804 }
Olli Etuaho93a90fd2015-04-07 18:14:07 +03001805 else
Jamie Madill502d66f2013-06-20 11:55:52 -04001806 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001807 TType arrayType = TType(publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04001808 int size;
1809 if (arraySizeErrorCheck(arrayLocation, indexExpression, size))
Olli Etuahoe7847b02015-03-16 11:56:12 +02001810 {
Jamie Madill502d66f2013-06-20 11:55:52 -04001811 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001812 }
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001813 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001814
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001815 TVariable *variable = nullptr;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001816 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001817 recover();
Jamie Madill502d66f2013-06-20 11:55:52 -04001818
Jamie Madillb98c3a82015-07-23 14:26:04 -04001819 TIntermSymbol *symbol =
1820 intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001821 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001822 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001823
1824 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001825 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001826
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001827 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001828}
1829
Jamie Madillb98c3a82015-07-23 14:26:04 -04001830TIntermAggregate *TParseContext::parseInitDeclarator(const TPublicType &publicType,
1831 TIntermAggregate *aggregateDeclaration,
1832 const TSourceLoc &identifierLocation,
1833 const TString &identifier,
1834 const TSourceLoc &initLocation,
1835 TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001836{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001837 // If the declaration starting this declarator list was empty (example: int,), some checks were
1838 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001839 if (mDeferredSingleDeclarationErrorCheck)
1840 {
1841 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1842 recover();
1843 mDeferredSingleDeclarationErrorCheck = false;
1844 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001845
Jamie Madill0bd18df2013-06-20 11:55:52 -04001846 if (locationDeclaratorListCheck(identifierLocation, publicType))
1847 recover();
1848
Olli Etuahoe7847b02015-03-16 11:56:12 +02001849 TIntermNode *intermNode = nullptr;
1850 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04001851 {
1852 //
1853 // build the intermediate representation
1854 //
1855 if (intermNode)
1856 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001857 return intermediate.growAggregate(aggregateDeclaration, intermNode, initLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001858 }
1859 else
1860 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001861 return aggregateDeclaration;
Jamie Madill502d66f2013-06-20 11:55:52 -04001862 }
1863 }
1864 else
1865 {
1866 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001867 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001868 }
1869}
1870
Jamie Madill06145232015-05-13 13:10:01 -04001871TIntermAggregate *TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001872 TIntermAggregate *aggregateDeclaration,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301873 const TSourceLoc &identifierLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001874 const TString &identifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301875 const TSourceLoc &indexLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001876 TIntermTyped *indexExpression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001877 const TSourceLoc &initLocation,
1878 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001879{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001880 // If the declaration starting this declarator list was empty (example: int,), some checks were
1881 // not performed.
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001882 if (mDeferredSingleDeclarationErrorCheck)
1883 {
1884 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1885 recover();
1886 mDeferredSingleDeclarationErrorCheck = false;
1887 }
1888
1889 if (locationDeclaratorListCheck(identifierLocation, publicType))
1890 recover();
1891
Jamie Madillb98c3a82015-07-23 14:26:04 -04001892 if (arrayTypeErrorCheck(indexLocation, publicType) ||
1893 arrayQualifierErrorCheck(indexLocation, publicType))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001894 {
1895 recover();
1896 }
1897
1898 TPublicType arrayType(publicType);
1899
Olli Etuaho376f1b52015-04-13 13:23:41 +03001900 int size = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001901 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
1902 // the initializer.
1903 if (indexExpression != nullptr &&
1904 arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001905 {
1906 recover();
1907 }
1908 // Make the type an array even if size check failed.
1909 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1910 arrayType.setArraySize(size);
1911
1912 // initNode will correspond to the whole of "b[n] = initializer".
1913 TIntermNode *initNode = nullptr;
1914 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1915 {
1916 if (initNode)
1917 {
1918 return intermediate.growAggregate(aggregateDeclaration, initNode, initLocation);
1919 }
1920 else
1921 {
1922 return aggregateDeclaration;
1923 }
1924 }
1925 else
1926 {
1927 recover();
1928 return nullptr;
1929 }
1930}
1931
Jamie Madilla295edf2013-06-06 11:56:48 -04001932void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1933{
1934 if (typeQualifier.qualifier != EvqUniform)
1935 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001936 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
1937 "global layout must be uniform");
Jamie Madilla295edf2013-06-06 11:56:48 -04001938 recover();
1939 return;
1940 }
1941
1942 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1943 ASSERT(!layoutQualifier.isEmpty());
1944
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001945 if (mShaderVersion < 300)
Jamie Madilla295edf2013-06-06 11:56:48 -04001946 {
1947 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1948 recover();
1949 return;
1950 }
1951
1952 if (layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1953 {
1954 recover();
1955 return;
1956 }
1957
Jamie Madill099c0f32013-06-20 11:55:52 -04001958 if (layoutQualifier.matrixPacking != EmpUnspecified)
1959 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001960 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04001961 }
1962
Geoff Langc6856732014-02-11 09:38:55 -05001963 if (layoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04001964 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001965 mDefaultBlockStorage = layoutQualifier.blockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04001966 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001967}
1968
Jamie Madill185fb402015-06-12 15:48:48 -04001969void TParseContext::parseFunctionPrototype(const TSourceLoc &location,
1970 TFunction *function,
1971 TIntermAggregate **aggregateOut)
1972{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001973 const TSymbol *builtIn =
1974 symbolTable.findBuiltIn(function->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04001975
1976 if (builtIn)
1977 {
1978 error(location, "built-in functions cannot be redefined", function->getName().c_str());
1979 recover();
1980 }
1981
Jamie Madillb98c3a82015-07-23 14:26:04 -04001982 TFunction *prevDec =
1983 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Jamie Madill185fb402015-06-12 15:48:48 -04001984 //
1985 // Note: 'prevDec' could be 'function' if this is the first time we've seen function
1986 // as it would have just been put in the symbol table. Otherwise, we're looking up
1987 // an earlier occurance.
1988 //
1989 if (prevDec->isDefined())
1990 {
1991 // Then this function already has a body.
1992 error(location, "function already has a body", function->getName().c_str());
1993 recover();
1994 }
1995 prevDec->setDefined();
1996 //
1997 // Overload the unique ID of the definition to be the same unique ID as the declaration.
1998 // Eventually we will probably want to have only a single definition and just swap the
1999 // arguments to be the definition's arguments.
2000 //
2001 function->setUniqueId(prevDec->getUniqueId());
2002
2003 // Raise error message if main function takes any parameters or return anything other than void
2004 if (function->getName() == "main")
2005 {
2006 if (function->getParamCount() > 0)
2007 {
2008 error(location, "function cannot take any parameter(s)", function->getName().c_str());
2009 recover();
2010 }
2011 if (function->getReturnType().getBasicType() != EbtVoid)
2012 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002013 error(location, "", function->getReturnType().getBasicString(),
2014 "main function cannot return a value");
Jamie Madill185fb402015-06-12 15:48:48 -04002015 recover();
2016 }
2017 }
2018
2019 //
2020 // Remember the return type for later checking for RETURN statements.
2021 //
2022 setCurrentFunctionType(&(prevDec->getReturnType()));
2023 setFunctionReturnsValue(false);
2024
2025 //
2026 // Insert parameters into the symbol table.
2027 // If the parameter has no name, it's not an error, just don't insert it
2028 // (could be used for unused args).
2029 //
2030 // Also, accumulate the list of parameters into the HIL, so lower level code
2031 // knows where to find parameters.
2032 //
2033 TIntermAggregate *paramNodes = new TIntermAggregate;
2034 for (size_t i = 0; i < function->getParamCount(); i++)
2035 {
2036 const TConstParameter &param = function->getParam(i);
2037 if (param.name != 0)
2038 {
2039 TVariable *variable = new TVariable(param.name, *param.type);
2040 //
2041 // Insert the parameters with name in the symbol table.
2042 //
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002043 if (!symbolTable.declare(variable))
2044 {
Jamie Madill185fb402015-06-12 15:48:48 -04002045 error(location, "redefinition", variable->getName().c_str());
2046 recover();
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002047 paramNodes = intermediate.growAggregate(
2048 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
2049 continue;
Jamie Madill185fb402015-06-12 15:48:48 -04002050 }
2051
2052 //
2053 // Add the parameter to the HIL
2054 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002055 TIntermSymbol *symbol = intermediate.addSymbol(
2056 variable->getUniqueId(), variable->getName(), variable->getType(), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002057
2058 paramNodes = intermediate.growAggregate(paramNodes, symbol, location);
2059 }
2060 else
2061 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002062 paramNodes = intermediate.growAggregate(
2063 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002064 }
2065 }
2066 intermediate.setAggregateOperator(paramNodes, EOpParameters, location);
2067 *aggregateOut = paramNodes;
2068 setLoopNestingLevel(0);
2069}
2070
Jamie Madillb98c3a82015-07-23 14:26:04 -04002071TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002072{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002073 //
2074 // Multiple declarations of the same function are allowed.
2075 //
2076 // If this is a definition, the definition production code will check for redefinitions
2077 // (we don't know at this point if it's a definition or not).
2078 //
2079 // Redeclarations are allowed. But, return types and parameter qualifiers must match.
2080 //
2081 TFunction *prevDec =
2082 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
2083 if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002084 {
2085 if (prevDec->getReturnType() != function->getReturnType())
2086 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002087 error(location, "overloaded functions must have the same return type",
Jamie Madill185fb402015-06-12 15:48:48 -04002088 function->getReturnType().getBasicString());
2089 recover();
2090 }
2091 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2092 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002093 if (prevDec->getParam(i).type->getQualifier() !=
2094 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002095 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002096 error(location, "overloaded functions must have the same parameter qualifiers",
Jamie Madill185fb402015-06-12 15:48:48 -04002097 function->getParam(i).type->getQualifierString());
2098 recover();
2099 }
2100 }
2101 }
2102
2103 //
2104 // Check for previously declared variables using the same name.
2105 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002106 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002107 if (prevSym)
2108 {
2109 if (!prevSym->isFunction())
2110 {
2111 error(location, "redefinition", function->getName().c_str(), "function");
2112 recover();
2113 }
2114 }
2115 else
2116 {
2117 // Insert the unmangled name to detect potential future redefinition as a variable.
Jamie Madillb98c3a82015-07-23 14:26:04 -04002118 TFunction *newFunction =
2119 new TFunction(NewPoolTString(function->getName().c_str()), &function->getReturnType());
Jamie Madill185fb402015-06-12 15:48:48 -04002120 symbolTable.getOuterLevel()->insertUnmangled(newFunction);
2121 }
2122
2123 // We're at the inner scope level of the function's arguments and body statement.
2124 // Add the function prototype to the surrounding scope instead.
2125 symbolTable.getOuterLevel()->insert(function);
2126
2127 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002128 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2129 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002130 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2131 //
2132 return function;
2133}
2134
Jamie Madill06145232015-05-13 13:10:01 -04002135TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002136{
Jamie Madill06145232015-05-13 13:10:01 -04002137 TPublicType publicType = publicTypeIn;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002138 TOperator op = EOpNull;
2139 if (publicType.userDef)
2140 {
2141 op = EOpConstructStruct;
2142 }
2143 else
2144 {
2145 switch (publicType.type)
2146 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002147 case EbtFloat:
2148 if (publicType.isMatrix())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002149 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002150 switch (publicType.getCols())
Alexis Hetu07e57df2015-06-16 16:55:52 -04002151 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002152 case 2:
2153 switch (publicType.getRows())
2154 {
2155 case 2:
2156 op = EOpConstructMat2;
2157 break;
2158 case 3:
2159 op = EOpConstructMat2x3;
2160 break;
2161 case 4:
2162 op = EOpConstructMat2x4;
2163 break;
2164 }
2165 break;
2166 case 3:
2167 switch (publicType.getRows())
2168 {
2169 case 2:
2170 op = EOpConstructMat3x2;
2171 break;
2172 case 3:
2173 op = EOpConstructMat3;
2174 break;
2175 case 4:
2176 op = EOpConstructMat3x4;
2177 break;
2178 }
2179 break;
2180 case 4:
2181 switch (publicType.getRows())
2182 {
2183 case 2:
2184 op = EOpConstructMat4x2;
2185 break;
2186 case 3:
2187 op = EOpConstructMat4x3;
2188 break;
2189 case 4:
2190 op = EOpConstructMat4;
2191 break;
2192 }
2193 break;
Alexis Hetu07e57df2015-06-16 16:55:52 -04002194 }
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002195 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04002196 else
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002197 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002198 switch (publicType.getNominalSize())
2199 {
2200 case 1:
2201 op = EOpConstructFloat;
2202 break;
2203 case 2:
2204 op = EOpConstructVec2;
2205 break;
2206 case 3:
2207 op = EOpConstructVec3;
2208 break;
2209 case 4:
2210 op = EOpConstructVec4;
2211 break;
2212 }
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002213 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04002214 break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002215
Jamie Madillb98c3a82015-07-23 14:26:04 -04002216 case EbtInt:
2217 switch (publicType.getNominalSize())
2218 {
2219 case 1:
2220 op = EOpConstructInt;
2221 break;
2222 case 2:
2223 op = EOpConstructIVec2;
2224 break;
2225 case 3:
2226 op = EOpConstructIVec3;
2227 break;
2228 case 4:
2229 op = EOpConstructIVec4;
2230 break;
2231 }
2232 break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002233
Jamie Madillb98c3a82015-07-23 14:26:04 -04002234 case EbtUInt:
2235 switch (publicType.getNominalSize())
2236 {
2237 case 1:
2238 op = EOpConstructUInt;
2239 break;
2240 case 2:
2241 op = EOpConstructUVec2;
2242 break;
2243 case 3:
2244 op = EOpConstructUVec3;
2245 break;
2246 case 4:
2247 op = EOpConstructUVec4;
2248 break;
2249 }
2250 break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002251
Jamie Madillb98c3a82015-07-23 14:26:04 -04002252 case EbtBool:
2253 switch (publicType.getNominalSize())
2254 {
2255 case 1:
2256 op = EOpConstructBool;
2257 break;
2258 case 2:
2259 op = EOpConstructBVec2;
2260 break;
2261 case 3:
2262 op = EOpConstructBVec3;
2263 break;
2264 case 4:
2265 op = EOpConstructBVec4;
2266 break;
2267 }
2268 break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002269
Jamie Madillb98c3a82015-07-23 14:26:04 -04002270 default:
2271 break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002272 }
2273
2274 if (op == EOpNull)
2275 {
2276 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
2277 recover();
2278 publicType.type = EbtFloat;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002279 op = EOpConstructFloat;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002280 }
2281 }
2282
2283 TString tempString;
Dmitry Skiba7f17a502015-06-22 15:08:39 -07002284 const TType *type = new TType(publicType);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002285 return new TFunction(&tempString, type, op);
2286}
2287
Jamie Madillb98c3a82015-07-23 14:26:04 -04002288// This function is used to test for the correctness of the parameters passed to various constructor
2289// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002290//
2291// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
2292//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002293TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments,
2294 TType *type,
2295 TOperator op,
2296 TFunction *fnCall,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302297 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002298{
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002299 TIntermAggregate *aggregateArguments = arguments->getAsAggregate();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002300
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002301 if (!aggregateArguments)
2302 {
2303 aggregateArguments = new TIntermAggregate;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002304 aggregateArguments->getSequence()->push_back(arguments);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002305 }
2306
Olli Etuahof40319e2015-03-10 14:33:00 +02002307 if (type->isArray())
2308 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002309 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
2310 // the array.
Olli Etuahof40319e2015-03-10 14:33:00 +02002311 TIntermSequence *args = aggregateArguments->getSequence();
2312 for (size_t i = 0; i < args->size(); i++)
2313 {
2314 const TType &argType = (*args)[i]->getAsTyped()->getType();
2315 // It has already been checked that the argument is not an array.
2316 ASSERT(!argType.isArray());
2317 if (!argType.sameElementType(*type))
2318 {
2319 error(line, "Array constructor argument has an incorrect type", "Error");
2320 recover();
2321 return nullptr;
2322 }
2323 }
2324 }
2325 else if (op == EOpConstructStruct)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002326 {
2327 const TFieldList &fields = type->getStruct()->fields();
Jamie Madillb98c3a82015-07-23 14:26:04 -04002328 TIntermSequence *args = aggregateArguments->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002329
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002330 for (size_t i = 0; i < fields.size(); i++)
2331 {
Nicolas Capensffd73872014-08-21 13:49:16 -04002332 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002333 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002334 error(line, "Structure constructor arguments do not match structure fields",
2335 "Error");
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002336 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002337
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002338 return 0;
2339 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002340 }
2341 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002342
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002343 // Turn the argument list itself into a constructor
Jamie Madillb98c3a82015-07-23 14:26:04 -04002344 TIntermAggregate *constructor = intermediate.setAggregateOperator(aggregateArguments, op, line);
Olli Etuaho21203702014-11-13 16:16:21 +02002345 TIntermTyped *constConstructor = foldConstConstructor(constructor, *type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002346 if (constConstructor)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002347 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002348 return constConstructor;
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002349 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002350
Olli Etuaho21203702014-11-13 16:16:21 +02002351 // Structs should not be precision qualified, the individual members may be.
2352 // Built-in types on the other hand should be precision qualified.
2353 if (op != EOpConstructStruct)
2354 {
2355 constructor->setPrecisionFromChildren();
2356 type->setPrecision(constructor->getPrecision());
2357 }
2358
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002359 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002360}
2361
Arun Patole7e7e68d2015-05-22 12:02:25 +05302362TIntermTyped *TParseContext::foldConstConstructor(TIntermAggregate *aggrNode, const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002363{
Olli Etuahof40319e2015-03-10 14:33:00 +02002364 // TODO: Add support for folding array constructors
2365 bool canBeFolded = areAllChildConst(aggrNode) && !type.isArray();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002366 aggrNode->setType(type);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302367 if (canBeFolded)
2368 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002369 bool returnVal = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302370 TConstantUnion *unionArray = new TConstantUnion[type.getObjectSize()];
2371 if (aggrNode->getSequence()->size() == 1)
2372 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002373 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray,
2374 aggrNode->getOp(), type, true);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002375 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302376 else
2377 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002378 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray,
2379 aggrNode->getOp(), type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002380 }
2381 if (returnVal)
2382 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002383
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002384 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
2385 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002386
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002387 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002388}
2389
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002390//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002391// This function returns the tree representation for the vector field(s) being accessed from contant
2392// vector.
2393// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a
2394// contant node is returned, else an aggregate node is returned (for v.xy). The input to this
2395// function could either
2396// be the symbol node or it could be the intermediate tree representation of accessing fields in a
2397// constant
2398// structure or column of a constant matrix.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002399//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002400TIntermTyped *TParseContext::addConstVectorNode(TVectorFields &fields,
2401 TIntermTyped *node,
2402 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002403{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302404 TIntermTyped *typedNode;
2405 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002406
Jamie Madillb11e2482015-05-04 14:21:22 -04002407 const TConstantUnion *unionArray;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302408 if (tempConstantNode)
2409 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002410 unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002411
Arun Patole7e7e68d2015-05-22 12:02:25 +05302412 if (!unionArray)
2413 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002414 return node;
2415 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302416 }
2417 else
Jamie Madillb98c3a82015-07-23 14:26:04 -04002418 { // The node has to be either a symbol node or an aggregate node or a tempConstant node, else,
2419 // its an error
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002420 error(line, "Cannot offset into the vector", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002421 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002422
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002423 return 0;
2424 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002425
Arun Patole7e7e68d2015-05-22 12:02:25 +05302426 TConstantUnion *constArray = new TConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002427
Arun Patole7e7e68d2015-05-22 12:02:25 +05302428 for (int i = 0; i < fields.num; i++)
2429 {
2430 if (fields.offsets[i] >= node->getType().getNominalSize())
2431 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002432 std::stringstream extraInfoStream;
2433 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
2434 std::string extraInfo = extraInfoStream.str();
2435 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002436 recover();
2437 fields.offsets[i] = 0;
2438 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302439
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002440 constArray[i] = unionArray[fields.offsets[i]];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302441 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002442 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
2443 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002444}
2445
2446//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002447// This function returns the column being accessed from a constant matrix. The values are retrieved
2448// from the symbol table and parse-tree is built for a vector (each column of a matrix is a vector).
2449// The
2450// input to the function could either be a symbol node (m[0] where m is a constant matrix)that
2451// represents
2452// a constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s
2453// is a constant structure)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002454//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002455TIntermTyped *TParseContext::addConstMatrixNode(int index,
2456 TIntermTyped *node,
2457 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002458{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302459 TIntermTyped *typedNode;
2460 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002461
Arun Patole7e7e68d2015-05-22 12:02:25 +05302462 if (index >= node->getType().getCols())
2463 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002464 std::stringstream extraInfoStream;
2465 extraInfoStream << "matrix field selection out of range '" << index << "'";
2466 std::string extraInfo = extraInfoStream.str();
2467 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002468 recover();
2469 index = 0;
2470 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002471
Arun Patole7e7e68d2015-05-22 12:02:25 +05302472 if (tempConstantNode)
2473 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002474 TConstantUnion *unionArray = tempConstantNode->getUnionArrayPointer();
2475 int size = tempConstantNode->getType().getCols();
2476 typedNode = intermediate.addConstantUnion(&unionArray[size * index],
2477 tempConstantNode->getType(), line);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302478 }
2479 else
2480 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002481 error(line, "Cannot offset into the matrix", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002482 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002483
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002484 return 0;
2485 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002486
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002487 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002488}
2489
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002490//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002491// This function returns an element of an array accessed from a constant array. The values are
2492// 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 +05302493// 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 -04002494// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a
2495// constant structure)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002496//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002497TIntermTyped *TParseContext::addConstArrayNode(int index,
2498 TIntermTyped *node,
2499 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002500{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302501 TIntermTyped *typedNode;
2502 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002503 TType arrayElementType = node->getType();
2504 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002505
Arun Patole7e7e68d2015-05-22 12:02:25 +05302506 if (index >= node->getType().getArraySize())
2507 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002508 std::stringstream extraInfoStream;
2509 extraInfoStream << "array field selection out of range '" << index << "'";
2510 std::string extraInfo = extraInfoStream.str();
2511 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002512 recover();
2513 index = 0;
2514 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002515
Arun Patole7e7e68d2015-05-22 12:02:25 +05302516 if (tempConstantNode)
2517 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002518 size_t arrayElementSize = arrayElementType.getObjectSize();
Arun Patole7e7e68d2015-05-22 12:02:25 +05302519 TConstantUnion *unionArray = tempConstantNode->getUnionArrayPointer();
Jamie Madillb98c3a82015-07-23 14:26:04 -04002520 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index],
2521 tempConstantNode->getType(), line);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302522 }
2523 else
2524 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002525 error(line, "Cannot offset into the array", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002526 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002527
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002528 return 0;
2529 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002530
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002531 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002532}
2533
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002534//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002535// This function returns the value of a particular field inside a constant structure from the symbol
2536// table.
2537// If there is an embedded/nested struct, it appropriately calls addConstStructNested or
2538// addConstStructFromAggr function and returns the parse-tree with the values of the embedded/nested
2539// struct.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002540//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002541TIntermTyped *TParseContext::addConstStruct(const TString &identifier,
2542 TIntermTyped *node,
2543 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002544{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302545 const TFieldList &fields = node->getType().getStruct()->fields();
Jamie Madillb98c3a82015-07-23 14:26:04 -04002546 size_t instanceSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002547
Arun Patole7e7e68d2015-05-22 12:02:25 +05302548 for (size_t index = 0; index < fields.size(); ++index)
2549 {
2550 if (fields[index]->name() == identifier)
2551 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002552 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302553 }
2554 else
2555 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002556 instanceSize += fields[index]->type()->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002557 }
2558 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002559
Jamie Madill94bf7f22013-07-08 13:31:15 -04002560 TIntermTyped *typedNode;
2561 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
Arun Patole7e7e68d2015-05-22 12:02:25 +05302562 if (tempConstantNode)
2563 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002564 TConstantUnion *constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002565
Jamie Madillb98c3a82015-07-23 14:26:04 -04002566 // type will be changed in the calling function
2567 typedNode = intermediate.addConstantUnion(constArray + instanceSize,
2568 tempConstantNode->getType(), line);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302569 }
2570 else
2571 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002572 error(line, "Cannot offset into the structure", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002573 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002574
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002575 return 0;
2576 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002577
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002578 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002579}
2580
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002581//
2582// Interface/uniform blocks
2583//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002584TIntermAggregate *TParseContext::addInterfaceBlock(const TPublicType &typeQualifier,
2585 const TSourceLoc &nameLine,
2586 const TString &blockName,
2587 TFieldList *fieldList,
2588 const TString *instanceName,
2589 const TSourceLoc &instanceLine,
2590 TIntermTyped *arrayIndex,
2591 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002592{
2593 if (reservedErrorCheck(nameLine, blockName))
2594 recover();
2595
2596 if (typeQualifier.qualifier != EvqUniform)
2597 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302598 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
2599 "interface blocks must be uniform");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002600 recover();
2601 }
2602
Jamie Madill099c0f32013-06-20 11:55:52 -04002603 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
2604 if (layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002605 {
2606 recover();
2607 }
2608
Jamie Madill099c0f32013-06-20 11:55:52 -04002609 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2610 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002611 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002612 }
2613
Jamie Madill1566ef72013-06-20 11:55:54 -04002614 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2615 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002616 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002617 }
2618
Arun Patole7e7e68d2015-05-22 12:02:25 +05302619 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2620 if (!symbolTable.declare(blockNameSymbol))
2621 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002622 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
2623 recover();
2624 }
2625
Jamie Madill98493dd2013-07-08 14:39:03 -04002626 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302627 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2628 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002629 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302630 TType *fieldType = field->type();
2631 if (IsSampler(fieldType->getBasicType()))
2632 {
2633 error(field->line(), "unsupported type", fieldType->getBasicString(),
2634 "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002635 recover();
2636 }
2637
Jamie Madill98493dd2013-07-08 14:39:03 -04002638 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002639 switch (qualifier)
2640 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002641 case EvqGlobal:
2642 case EvqUniform:
2643 break;
2644 default:
2645 error(field->line(), "invalid qualifier on interface block member",
2646 getQualifierString(qualifier));
2647 recover();
2648 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002649 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002650
2651 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002652 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
2653 if (layoutLocationErrorCheck(field->line(), fieldLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002654 {
2655 recover();
2656 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002657
Jamie Madill98493dd2013-07-08 14:39:03 -04002658 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002659 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002660 error(field->line(), "invalid layout qualifier:",
2661 getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04002662 recover();
2663 }
2664
Jamie Madill98493dd2013-07-08 14:39:03 -04002665 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002666 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002667 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002668 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002669 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002670 {
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002671 warning(field->line(), "extraneous layout qualifier:",
Jamie Madillb98c3a82015-07-23 14:26:04 -04002672 getMatrixPackingString(fieldLayoutQualifier.matrixPacking),
2673 "only has an effect on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04002674 }
2675
Jamie Madill98493dd2013-07-08 14:39:03 -04002676 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002677 }
2678
Jamie Madill98493dd2013-07-08 14:39:03 -04002679 // add array index
2680 int arraySize = 0;
2681 if (arrayIndex != NULL)
2682 {
2683 if (arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize))
2684 recover();
2685 }
2686
Jamie Madillb98c3a82015-07-23 14:26:04 -04002687 TInterfaceBlock *interfaceBlock =
2688 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2689 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
2690 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002691
2692 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04002693 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002694
Jamie Madill98493dd2013-07-08 14:39:03 -04002695 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002696 {
2697 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002698 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2699 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002700 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302701 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002702
2703 // set parent pointer of the field variable
2704 fieldType->setInterfaceBlock(interfaceBlock);
2705
Arun Patole7e7e68d2015-05-22 12:02:25 +05302706 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002707 fieldVariable->setQualifier(typeQualifier.qualifier);
2708
Arun Patole7e7e68d2015-05-22 12:02:25 +05302709 if (!symbolTable.declare(fieldVariable))
2710 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002711 error(field->line(), "redefinition", field->name().c_str(),
2712 "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002713 recover();
2714 }
2715 }
2716 }
2717 else
2718 {
Olli Etuahoe0f623a2015-07-10 11:58:30 +03002719 if (reservedErrorCheck(instanceLine, *instanceName))
2720 recover();
2721
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002722 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302723 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002724 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002725
Arun Patole7e7e68d2015-05-22 12:02:25 +05302726 if (!symbolTable.declare(instanceTypeDef))
2727 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002728 error(instanceLine, "redefinition", instanceName->c_str(),
2729 "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002730 recover();
2731 }
2732
Jamie Madillb98c3a82015-07-23 14:26:04 -04002733 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002734 symbolName = instanceTypeDef->getName();
2735 }
2736
Jamie Madillb98c3a82015-07-23 14:26:04 -04002737 TIntermAggregate *aggregate = intermediate.makeAggregate(
2738 intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line),
2739 nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002740 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04002741
2742 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002743 return aggregate;
2744}
2745
Arun Patole7e7e68d2015-05-22 12:02:25 +05302746bool TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002747{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002748 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002749
2750 // Embedded structure definitions are not supported per GLSL ES spec.
2751 // They aren't allowed in GLSL either, but we need to detect this here
2752 // so we don't rely on the GLSL compiler to catch it.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302753 if (mStructNestingLevel > 1)
2754 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002755 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002756 return true;
2757 }
2758
2759 return false;
2760}
2761
2762void TParseContext::exitStructDeclaration()
2763{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002764 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002765}
2766
Jamie Madillb98c3a82015-07-23 14:26:04 -04002767namespace
2768{
kbr@chromium.org476541f2011-10-27 21:14:51 +00002769const int kWebGLMaxStructNesting = 4;
2770
2771} // namespace
2772
Arun Patole7e7e68d2015-05-22 12:02:25 +05302773bool TParseContext::structNestingErrorCheck(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002774{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302775 if (!IsWebGLBasedSpec(mShaderSpec))
2776 {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002777 return false;
2778 }
2779
Arun Patole7e7e68d2015-05-22 12:02:25 +05302780 if (field.type()->getBasicType() != EbtStruct)
2781 {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002782 return false;
2783 }
2784
2785 // We're already inside a structure definition at this point, so add
2786 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302787 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2788 {
Jamie Madill41a49272014-03-18 16:10:13 -04002789 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002790 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
2791 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04002792 std::string reason = reasonStream.str();
2793 error(line, reason.c_str(), field.name().c_str(), "");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002794 return true;
2795 }
2796
2797 return false;
2798}
2799
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002800//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002801// Parse an array index expression
2802//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002803TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
2804 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302805 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002806{
2807 TIntermTyped *indexedExpression = NULL;
2808
2809 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2810 {
2811 if (baseExpression->getAsSymbolNode())
2812 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302813 error(location, " left of '[' is not of type array, matrix, or vector ",
2814 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002815 }
2816 else
2817 {
2818 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2819 }
2820 recover();
2821 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002822
Jamie Madill21c1e452014-12-29 11:33:41 -05002823 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2824
2825 if (indexExpression->getQualifier() == EvqConst && indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002826 {
Jamie Madill21c1e452014-12-29 11:33:41 -05002827 int index = indexConstantUnion->getIConst(0);
Jamie Madill7164cf42013-07-08 13:30:59 -04002828 if (index < 0)
2829 {
2830 std::stringstream infoStream;
2831 infoStream << index;
2832 std::string info = infoStream.str();
2833 error(location, "negative index", info.c_str());
2834 recover();
2835 index = 0;
2836 }
2837 if (baseExpression->getType().getQualifier() == EvqConst)
2838 {
2839 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002840 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002841 // constant folding for arrays
2842 indexedExpression = addConstArrayNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002843 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002844 else if (baseExpression->isVector())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002845 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002846 // constant folding for vectors
2847 TVectorFields fields;
2848 fields.num = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002849 fields.offsets[0] =
2850 index; // need to do it this way because v.xy sends fields integer array
Jamie Madill7164cf42013-07-08 13:30:59 -04002851 indexedExpression = addConstVectorNode(fields, baseExpression, location);
2852 }
2853 else if (baseExpression->isMatrix())
2854 {
2855 // constant folding for matrices
2856 indexedExpression = addConstMatrixNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002857 }
2858 }
2859 else
2860 {
Jamie Madillb11e2482015-05-04 14:21:22 -04002861 int safeIndex = -1;
2862
Jamie Madill7164cf42013-07-08 13:30:59 -04002863 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002864 {
Jamie Madill18464b52013-07-08 14:01:55 -04002865 if (index >= baseExpression->getType().getArraySize())
Jamie Madill7164cf42013-07-08 13:30:59 -04002866 {
2867 std::stringstream extraInfoStream;
2868 extraInfoStream << "array index out of range '" << index << "'";
2869 std::string extraInfo = extraInfoStream.str();
2870 error(location, "", "[", extraInfo.c_str());
2871 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002872 safeIndex = baseExpression->getType().getArraySize() - 1;
Jamie Madill7164cf42013-07-08 13:30:59 -04002873 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302874 else if (baseExpression->getQualifier() == EvqFragData && index > 0 &&
2875 !isExtensionEnabled("GL_EXT_draw_buffers"))
Jamie Madill5d287f52013-07-12 15:38:19 -04002876 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002877 error(location, "", "[",
2878 "array indexes for gl_FragData must be zero when GL_EXT_draw_buffers is "
2879 "disabled");
Jamie Madill5d287f52013-07-12 15:38:19 -04002880 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002881 safeIndex = 0;
Jamie Madill5d287f52013-07-12 15:38:19 -04002882 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002883 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302884 else if ((baseExpression->isVector() || baseExpression->isMatrix()) &&
Jamie Madillb98c3a82015-07-23 14:26:04 -04002885 baseExpression->getType().getNominalSize() <= index)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002886 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002887 std::stringstream extraInfoStream;
2888 extraInfoStream << "field selection out of range '" << index << "'";
2889 std::string extraInfo = extraInfoStream.str();
2890 error(location, "", "[", extraInfo.c_str());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002891 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002892 safeIndex = baseExpression->getType().getNominalSize() - 1;
Jamie Madill46131a32013-06-20 11:55:50 -04002893 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002894
Jamie Madillb11e2482015-05-04 14:21:22 -04002895 // Don't modify the data of the previous constant union, because it can point
2896 // to builtins, like gl_MaxDrawBuffers. Instead use a new sanitized object.
2897 if (safeIndex != -1)
2898 {
2899 TConstantUnion *safeConstantUnion = new TConstantUnion();
2900 safeConstantUnion->setIConst(safeIndex);
2901 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
2902 }
2903
Jamie Madillb98c3a82015-07-23 14:26:04 -04002904 indexedExpression =
2905 intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002906 }
2907 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002908 else
2909 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002910 if (baseExpression->isInterfaceBlock())
Jamie Madill7164cf42013-07-08 13:30:59 -04002911 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002912 error(
2913 location, "", "[",
2914 "array indexes for interface blocks arrays must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002915 recover();
2916 }
Jamie Madill19571812013-08-12 15:26:34 -07002917 else if (baseExpression->getQualifier() == EvqFragmentOut)
Jamie Madill7164cf42013-07-08 13:30:59 -04002918 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002919 error(location, "", "[",
2920 "array indexes for fragment outputs must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002921 recover();
2922 }
2923
Jamie Madillb98c3a82015-07-23 14:26:04 -04002924 indexedExpression =
2925 intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
Jamie Madill7164cf42013-07-08 13:30:59 -04002926 }
2927
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002928 if (indexedExpression == 0)
2929 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002930 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002931 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002932 indexedExpression =
2933 intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002934 }
2935 else if (baseExpression->isArray())
2936 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002937 const TType &baseType = baseExpression->getType();
2938 if (baseType.getStruct())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002939 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002940 TType copyOfType(baseType.getStruct());
2941 indexedExpression->setType(copyOfType);
2942 }
2943 else if (baseType.isInterfaceBlock())
2944 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002945 TType copyOfType(baseType.getInterfaceBlock(), baseType.getQualifier(),
2946 baseType.getLayoutQualifier(), 0);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002947 indexedExpression->setType(copyOfType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002948 }
2949 else
2950 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002951 indexedExpression->setType(
2952 TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary,
2953 static_cast<unsigned char>(baseExpression->getNominalSize()),
2954 static_cast<unsigned char>(baseExpression->getSecondarySize())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002955 }
2956
2957 if (baseExpression->getType().getQualifier() == EvqConst)
2958 {
2959 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2960 }
2961 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002962 else if (baseExpression->isMatrix())
2963 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002964 TQualifier qualifier =
2965 baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2966 indexedExpression->setType(TType(baseExpression->getBasicType(),
2967 baseExpression->getPrecision(), qualifier,
2968 static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002969 }
2970 else if (baseExpression->isVector())
2971 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002972 TQualifier qualifier =
2973 baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2974 indexedExpression->setType(
2975 TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002976 }
2977 else
2978 {
2979 indexedExpression->setType(baseExpression->getType());
2980 }
2981
2982 return indexedExpression;
2983}
2984
Jamie Madillb98c3a82015-07-23 14:26:04 -04002985TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
2986 const TSourceLoc &dotLocation,
2987 const TString &fieldString,
2988 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002989{
2990 TIntermTyped *indexedExpression = NULL;
2991
2992 if (baseExpression->isArray())
2993 {
2994 error(fieldLocation, "cannot apply dot operator to an array", ".");
2995 recover();
2996 }
2997
2998 if (baseExpression->isVector())
2999 {
3000 TVectorFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003001 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields,
3002 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003003 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003004 fields.num = 1;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003005 fields.offsets[0] = 0;
3006 recover();
3007 }
3008
3009 if (baseExpression->getType().getQualifier() == EvqConst)
3010 {
3011 // constant folding for vector fields
3012 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
3013 if (indexedExpression == 0)
3014 {
3015 recover();
3016 indexedExpression = baseExpression;
3017 }
3018 else
3019 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003020 indexedExpression->setType(TType(baseExpression->getBasicType(),
3021 baseExpression->getPrecision(), EvqConst,
3022 (unsigned char)(fieldString).size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003023 }
3024 }
3025 else
3026 {
3027 TString vectorString = fieldString;
Arun Patole7e7e68d2015-05-22 12:02:25 +05303028 TIntermTyped *index = intermediate.addSwizzle(fields, fieldLocation);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003029 indexedExpression =
3030 intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
3031 indexedExpression->setType(TType(baseExpression->getBasicType(),
3032 baseExpression->getPrecision(), EvqTemporary,
3033 (unsigned char)vectorString.size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003034 }
3035 }
3036 else if (baseExpression->isMatrix())
3037 {
3038 TMatrixFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003039 if (!parseMatrixFields(fieldString, baseExpression->getCols(), baseExpression->getRows(),
3040 fields, fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003041 {
3042 fields.wholeRow = false;
3043 fields.wholeCol = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003044 fields.row = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003045 fields.col = 0;
3046 recover();
3047 }
3048
3049 if (fields.wholeRow || fields.wholeCol)
3050 {
3051 error(dotLocation, " non-scalar fields not implemented yet", ".");
3052 recover();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003053 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003054 unionArray->setIConst(0);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003055 TIntermTyped *index = intermediate.addConstantUnion(
3056 unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
3057 indexedExpression =
3058 intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
3059 indexedExpression->setType(
3060 TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary,
3061 static_cast<unsigned char>(baseExpression->getCols()),
3062 static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003063 }
3064 else
3065 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003066 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003067 unionArray->setIConst(fields.col * baseExpression->getRows() + fields.row);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003068 TIntermTyped *index = intermediate.addConstantUnion(
3069 unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
3070 indexedExpression =
3071 intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
3072 indexedExpression->setType(
3073 TType(baseExpression->getBasicType(), baseExpression->getPrecision()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003074 }
3075 }
3076 else if (baseExpression->getBasicType() == EbtStruct)
3077 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003078 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05303079 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003080 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003081 {
3082 error(dotLocation, "structure has no fields", "Internal Error");
3083 recover();
3084 indexedExpression = baseExpression;
3085 }
3086 else
3087 {
3088 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003089 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003090 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003091 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003092 {
3093 fieldFound = true;
3094 break;
3095 }
3096 }
3097 if (fieldFound)
3098 {
3099 if (baseExpression->getType().getQualifier() == EvqConst)
3100 {
3101 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
3102 if (indexedExpression == 0)
3103 {
3104 recover();
3105 indexedExpression = baseExpression;
3106 }
3107 else
3108 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003109 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003110 // change the qualifier of the return type, not of the structure field
3111 // as the structure definition is shared between various structures.
3112 indexedExpression->getTypePointer()->setQualifier(EvqConst);
3113 }
3114 }
3115 else
3116 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003117 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003118 unionArray->setIConst(i);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003119 TIntermTyped *index = intermediate.addConstantUnion(
3120 unionArray, *fields[i]->type(), fieldLocation);
3121 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression,
3122 index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04003123 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003124 }
3125 }
3126 else
3127 {
3128 error(dotLocation, " no such field in structure", fieldString.c_str());
3129 recover();
3130 indexedExpression = baseExpression;
3131 }
3132 }
3133 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003134 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003135 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003136 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05303137 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003138 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003139 {
3140 error(dotLocation, "interface block has no fields", "Internal Error");
3141 recover();
3142 indexedExpression = baseExpression;
3143 }
3144 else
3145 {
3146 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003147 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003148 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003149 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003150 {
3151 fieldFound = true;
3152 break;
3153 }
3154 }
3155 if (fieldFound)
3156 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003157 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003158 unionArray->setIConst(i);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003159 TIntermTyped *index =
3160 intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
3161 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock,
3162 baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04003163 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003164 }
3165 else
3166 {
3167 error(dotLocation, " no such field in interface block", fieldString.c_str());
3168 recover();
3169 indexedExpression = baseExpression;
3170 }
3171 }
3172 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003173 else
3174 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003175 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003176 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003177 error(dotLocation,
3178 " field selection requires structure, vector, or matrix on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303179 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003180 }
3181 else
3182 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303183 error(dotLocation,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003184 " field selection requires structure, vector, matrix, or interface block on left "
3185 "hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303186 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003187 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003188 recover();
3189 indexedExpression = baseExpression;
3190 }
3191
3192 return indexedExpression;
3193}
3194
Jamie Madillb98c3a82015-07-23 14:26:04 -04003195TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3196 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003197{
Jamie Madilla5efff92013-06-06 11:56:47 -04003198 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003199
Jamie Madillb98c3a82015-07-23 14:26:04 -04003200 qualifier.location = -1;
Jamie Madilla5efff92013-06-06 11:56:47 -04003201 qualifier.matrixPacking = EmpUnspecified;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003202 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003203
3204 if (qualifierType == "shared")
3205 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003206 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003207 }
3208 else if (qualifierType == "packed")
3209 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003210 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003211 }
3212 else if (qualifierType == "std140")
3213 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003214 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003215 }
3216 else if (qualifierType == "row_major")
3217 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003218 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003219 }
3220 else if (qualifierType == "column_major")
3221 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003222 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003223 }
3224 else if (qualifierType == "location")
3225 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003226 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
3227 "location requires an argument");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003228 recover();
3229 }
3230 else
3231 {
3232 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
3233 recover();
3234 }
3235
Jamie Madilla5efff92013-06-06 11:56:47 -04003236 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003237}
3238
Jamie Madillb98c3a82015-07-23 14:26:04 -04003239TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3240 const TSourceLoc &qualifierTypeLine,
3241 const TString &intValueString,
3242 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303243 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003244{
Jamie Madilla5efff92013-06-06 11:56:47 -04003245 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003246
Jamie Madillb98c3a82015-07-23 14:26:04 -04003247 qualifier.location = -1;
Jamie Madilla5efff92013-06-06 11:56:47 -04003248 qualifier.matrixPacking = EmpUnspecified;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003249 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003250
3251 if (qualifierType != "location")
3252 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303253 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
3254 "only location may have arguments");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003255 recover();
3256 }
3257 else
3258 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003259 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003260 if (intValue < 0)
3261 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003262 error(intValueLine, "out of range:", intValueString.c_str(),
3263 "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003264 recover();
3265 }
3266 else
3267 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003268 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003269 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003270 }
3271
Jamie Madilla5efff92013-06-06 11:56:47 -04003272 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003273}
3274
Jamie Madillb98c3a82015-07-23 14:26:04 -04003275TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
3276 TLayoutQualifier rightQualifier)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003277{
Jamie Madilla5efff92013-06-06 11:56:47 -04003278 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003279
Jamie Madilla5efff92013-06-06 11:56:47 -04003280 if (rightQualifier.location != -1)
3281 {
3282 joinedQualifier.location = rightQualifier.location;
3283 }
3284 if (rightQualifier.matrixPacking != EmpUnspecified)
3285 {
3286 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
3287 }
3288 if (rightQualifier.blockStorage != EbsUnspecified)
3289 {
3290 joinedQualifier.blockStorage = rightQualifier.blockStorage;
3291 }
3292
3293 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003294}
3295
Arun Patole7e7e68d2015-05-22 12:02:25 +05303296TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc,
3297 TQualifier interpolationQualifier,
3298 const TSourceLoc &storageLoc,
3299 TQualifier storageQualifier)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003300{
3301 TQualifier mergedQualifier = EvqSmoothIn;
3302
Arun Patole7e7e68d2015-05-22 12:02:25 +05303303 if (storageQualifier == EvqFragmentIn)
3304 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003305 if (interpolationQualifier == EvqSmooth)
3306 mergedQualifier = EvqSmoothIn;
3307 else if (interpolationQualifier == EvqFlat)
3308 mergedQualifier = EvqFlatIn;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003309 else
3310 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003311 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303312 else if (storageQualifier == EvqCentroidIn)
3313 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003314 if (interpolationQualifier == EvqSmooth)
3315 mergedQualifier = EvqCentroidIn;
3316 else if (interpolationQualifier == EvqFlat)
3317 mergedQualifier = EvqFlatIn;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003318 else
3319 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003320 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303321 else if (storageQualifier == EvqVertexOut)
3322 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003323 if (interpolationQualifier == EvqSmooth)
3324 mergedQualifier = EvqSmoothOut;
3325 else if (interpolationQualifier == EvqFlat)
3326 mergedQualifier = EvqFlatOut;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003327 else
3328 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003329 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303330 else if (storageQualifier == EvqCentroidOut)
3331 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003332 if (interpolationQualifier == EvqSmooth)
3333 mergedQualifier = EvqCentroidOut;
3334 else if (interpolationQualifier == EvqFlat)
3335 mergedQualifier = EvqFlatOut;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003336 else
3337 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003338 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303339 else
3340 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003341 error(interpolationLoc,
3342 "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303343 getInterpolationString(interpolationQualifier));
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003344 recover();
3345
3346 mergedQualifier = storageQualifier;
3347 }
3348
3349 TPublicType type;
3350 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
3351 return type;
3352}
3353
Jamie Madillb98c3a82015-07-23 14:26:04 -04003354TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3355 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003356{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03003357 if (voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier.type))
3358 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003359 recover();
3360 }
3361
Arun Patole7e7e68d2015-05-22 12:02:25 +05303362 for (unsigned int i = 0; i < fieldList->size(); ++i)
3363 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003364 //
3365 // Careful not to replace already known aspects of type, like array-ness
3366 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303367 TType *type = (*fieldList)[i]->type();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003368 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003369 type->setPrimarySize(typeSpecifier.primarySize);
3370 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003371 type->setPrecision(typeSpecifier.precision);
3372 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003373 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003374
3375 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303376 if (type->isArray())
3377 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003378 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
3379 recover();
3380 }
3381 if (typeSpecifier.array)
3382 type->setArraySize(typeSpecifier.arraySize);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303383 if (typeSpecifier.userDef)
3384 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003385 type->setStruct(typeSpecifier.userDef->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003386 }
3387
Arun Patole7e7e68d2015-05-22 12:02:25 +05303388 if (structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i]))
3389 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003390 recover();
3391 }
3392 }
3393
Jamie Madill98493dd2013-07-08 14:39:03 -04003394 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003395}
3396
Jamie Madillb98c3a82015-07-23 14:26:04 -04003397TPublicType TParseContext::addStructure(const TSourceLoc &structLine,
3398 const TSourceLoc &nameLine,
3399 const TString *structName,
3400 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003401{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303402 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003403 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003404
Jamie Madill9b820842015-02-12 10:40:10 -05003405 // Store a bool in the struct if we're at global scope, to allow us to
3406 // skip the local struct scoping workaround in HLSL.
Jamie Madillb960cc42015-02-12 15:33:20 +00003407 structure->setUniqueId(TSymbolTable::nextUniqueId());
Jamie Madill9b820842015-02-12 10:40:10 -05003408 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003409
Jamie Madill98493dd2013-07-08 14:39:03 -04003410 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003411 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003412 if (reservedErrorCheck(nameLine, *structName))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003413 {
3414 recover();
3415 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303416 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
3417 if (!symbolTable.declare(userTypeDef))
3418 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003419 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003420 recover();
3421 }
3422 }
3423
3424 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04003425 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003426 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003427 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04003428 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003429 switch (qualifier)
3430 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003431 case EvqGlobal:
3432 case EvqTemporary:
3433 break;
3434 default:
3435 error(field.line(), "invalid qualifier on struct member",
3436 getQualifierString(qualifier));
3437 recover();
3438 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003439 }
3440 }
3441
3442 TPublicType publicType;
3443 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003444 publicType.userDef = structureType;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003445 exitStructDeclaration();
3446
3447 return publicType;
3448}
3449
Jamie Madillb98c3a82015-07-23 14:26:04 -04003450TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
3451 TIntermAggregate *statementList,
3452 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02003453{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003454 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003455 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02003456 init->isVector())
3457 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003458 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
3459 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003460 recover();
3461 return nullptr;
3462 }
3463
Olli Etuahoac5274d2015-02-20 10:19:08 +02003464 if (statementList)
3465 {
3466 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
3467 {
3468 recover();
3469 return nullptr;
3470 }
3471 }
3472
Olli Etuahoa3a36662015-02-17 13:46:51 +02003473 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3474 if (node == nullptr)
3475 {
3476 error(loc, "erroneous switch statement", "switch");
3477 recover();
3478 return nullptr;
3479 }
3480 return node;
3481}
3482
3483TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3484{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003485 if (mSwitchNestingLevel == 0)
3486 {
3487 error(loc, "case labels need to be inside switch statements", "case");
3488 recover();
3489 return nullptr;
3490 }
3491 if (condition == nullptr)
3492 {
3493 error(loc, "case label must have a condition", "case");
3494 recover();
3495 return nullptr;
3496 }
3497 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04003498 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02003499 {
3500 error(condition->getLine(), "case label must be a scalar integer", "case");
3501 recover();
3502 }
3503 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
3504 if (conditionConst == nullptr)
3505 {
3506 error(condition->getLine(), "case label must be constant", "case");
3507 recover();
3508 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003509 TIntermCase *node = intermediate.addCase(condition, loc);
3510 if (node == nullptr)
3511 {
3512 error(loc, "erroneous case statement", "case");
3513 recover();
3514 return nullptr;
3515 }
3516 return node;
3517}
3518
3519TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3520{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003521 if (mSwitchNestingLevel == 0)
3522 {
3523 error(loc, "default labels need to be inside switch statements", "default");
3524 recover();
3525 return nullptr;
3526 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003527 TIntermCase *node = intermediate.addCase(nullptr, loc);
3528 if (node == nullptr)
3529 {
3530 error(loc, "erroneous default statement", "default");
3531 recover();
3532 return nullptr;
3533 }
3534 return node;
3535}
3536
Jamie Madillb98c3a82015-07-23 14:26:04 -04003537TIntermTyped *TParseContext::createUnaryMath(TOperator op,
3538 TIntermTyped *child,
3539 const TSourceLoc &loc,
3540 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003541{
3542 if (child == nullptr)
3543 {
3544 return nullptr;
3545 }
3546
3547 switch (op)
3548 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003549 case EOpLogicalNot:
3550 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
3551 child->isVector())
3552 {
3553 return nullptr;
3554 }
3555 break;
3556 case EOpBitwiseNot:
3557 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3558 child->isMatrix() || child->isArray())
3559 {
3560 return nullptr;
3561 }
3562 break;
3563 case EOpPostIncrement:
3564 case EOpPreIncrement:
3565 case EOpPostDecrement:
3566 case EOpPreDecrement:
3567 case EOpNegative:
3568 case EOpPositive:
3569 if (child->getBasicType() == EbtStruct || child->getBasicType() == EbtBool ||
3570 child->isArray())
3571 {
3572 return nullptr;
3573 }
3574 // Operators for built-ins are already type checked against their prototype.
3575 default:
3576 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003577 }
3578
Olli Etuahof6c694b2015-03-26 14:50:53 +02003579 return intermediate.addUnaryMath(op, child, loc, funcReturnType);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003580}
3581
Olli Etuaho09b22472015-02-11 11:47:26 +02003582TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3583{
Olli Etuahof6c694b2015-03-26 14:50:53 +02003584 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003585 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003586 {
3587 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
3588 recover();
3589 return child;
3590 }
3591 return node;
3592}
3593
Jamie Madillb98c3a82015-07-23 14:26:04 -04003594TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
3595 TIntermTyped *child,
3596 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003597{
3598 if (lValueErrorCheck(loc, GetOperatorString(op), child))
3599 recover();
3600 return addUnaryMath(op, child, loc);
3601}
3602
Jamie Madillb98c3a82015-07-23 14:26:04 -04003603bool TParseContext::binaryOpCommonCheck(TOperator op,
3604 TIntermTyped *left,
3605 TIntermTyped *right,
3606 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003607{
3608 if (left->isArray() || right->isArray())
3609 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003610 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003611 {
3612 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3613 return false;
3614 }
3615
3616 if (left->isArray() != right->isArray())
3617 {
3618 error(loc, "array / non-array mismatch", GetOperatorString(op));
3619 return false;
3620 }
3621
3622 switch (op)
3623 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003624 case EOpEqual:
3625 case EOpNotEqual:
3626 case EOpAssign:
3627 case EOpInitialize:
3628 break;
3629 default:
3630 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3631 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02003632 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003633 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003634 if (left->getArraySize() != right->getArraySize())
3635 {
3636 error(loc, "array size mismatch", GetOperatorString(op));
3637 return false;
3638 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003639 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003640
3641 // Check ops which require integer / ivec parameters
3642 bool isBitShift = false;
3643 switch (op)
3644 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003645 case EOpBitShiftLeft:
3646 case EOpBitShiftRight:
3647 case EOpBitShiftLeftAssign:
3648 case EOpBitShiftRightAssign:
3649 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3650 // check that the basic type is an integer type.
3651 isBitShift = true;
3652 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3653 {
3654 return false;
3655 }
3656 break;
3657 case EOpBitwiseAnd:
3658 case EOpBitwiseXor:
3659 case EOpBitwiseOr:
3660 case EOpBitwiseAndAssign:
3661 case EOpBitwiseXorAssign:
3662 case EOpBitwiseOrAssign:
3663 // It is enough to check the type of only one operand, since later it
3664 // is checked that the operand types match.
3665 if (!IsInteger(left->getBasicType()))
3666 {
3667 return false;
3668 }
3669 break;
3670 default:
3671 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003672 }
3673
3674 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3675 // So the basic type should usually match.
3676 if (!isBitShift && left->getBasicType() != right->getBasicType())
3677 {
3678 return false;
3679 }
3680
Olli Etuaho9dd217b2015-03-20 14:24:31 +02003681 // Check that type sizes match exactly on ops that require that.
Olli Etuahoff699002015-03-23 14:38:42 +02003682 // Also check restrictions for structs that contain arrays or samplers.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003683 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003684 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003685 case EOpAssign:
3686 case EOpInitialize:
3687 case EOpEqual:
3688 case EOpNotEqual:
3689 // ESSL 1.00 sections 5.7, 5.8, 5.9
3690 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
3691 {
3692 error(loc, "undefined operation for structs containing arrays",
3693 GetOperatorString(op));
3694 return false;
3695 }
3696 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3697 // we interpret the spec so that this extends to structs containing samplers,
3698 // similarly to ESSL 1.00 spec.
3699 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
3700 left->getType().isStructureContainingSamplers())
3701 {
3702 error(loc, "undefined operation for structs containing samplers",
3703 GetOperatorString(op));
3704 return false;
3705 }
3706 case EOpLessThan:
3707 case EOpGreaterThan:
3708 case EOpLessThanEqual:
3709 case EOpGreaterThanEqual:
3710 if ((left->getNominalSize() != right->getNominalSize()) ||
3711 (left->getSecondarySize() != right->getSecondarySize()))
3712 {
3713 return false;
3714 }
3715 default:
3716 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003717 }
3718
Olli Etuahod6b14282015-03-17 14:31:35 +02003719 return true;
3720}
3721
Jamie Madillb98c3a82015-07-23 14:26:04 -04003722TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
3723 TIntermTyped *left,
3724 TIntermTyped *right,
3725 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02003726{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003727 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003728 return nullptr;
3729
Olli Etuahofc1806e2015-03-17 13:03:11 +02003730 switch (op)
3731 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003732 case EOpEqual:
3733 case EOpNotEqual:
3734 break;
3735 case EOpLessThan:
3736 case EOpGreaterThan:
3737 case EOpLessThanEqual:
3738 case EOpGreaterThanEqual:
3739 ASSERT(!left->isArray() && !right->isArray());
3740 if (left->isMatrix() || left->isVector() || left->getBasicType() == EbtStruct)
3741 {
3742 return nullptr;
3743 }
3744 break;
3745 case EOpLogicalOr:
3746 case EOpLogicalXor:
3747 case EOpLogicalAnd:
3748 ASSERT(!left->isArray() && !right->isArray());
3749 if (left->getBasicType() != EbtBool || left->isMatrix() || left->isVector())
3750 {
3751 return nullptr;
3752 }
3753 break;
3754 case EOpAdd:
3755 case EOpSub:
3756 case EOpDiv:
3757 case EOpMul:
3758 ASSERT(!left->isArray() && !right->isArray());
3759 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool)
3760 {
3761 return nullptr;
3762 }
3763 break;
3764 case EOpIMod:
3765 ASSERT(!left->isArray() && !right->isArray());
3766 // Note that this is only for the % operator, not for mod()
3767 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool ||
3768 left->getBasicType() == EbtFloat)
3769 {
3770 return nullptr;
3771 }
3772 break;
3773 // Note that for bitwise ops, type checking is done in promote() to
3774 // share code between ops and compound assignment
3775 default:
3776 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003777 }
3778
Olli Etuahofc1806e2015-03-17 13:03:11 +02003779 return intermediate.addBinaryMath(op, left, right, loc);
3780}
3781
Jamie Madillb98c3a82015-07-23 14:26:04 -04003782TIntermTyped *TParseContext::addBinaryMath(TOperator op,
3783 TIntermTyped *left,
3784 TIntermTyped *right,
3785 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003786{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003787 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003788 if (node == 0)
3789 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003790 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3791 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003792 recover();
3793 return left;
3794 }
3795 return node;
3796}
3797
Jamie Madillb98c3a82015-07-23 14:26:04 -04003798TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
3799 TIntermTyped *left,
3800 TIntermTyped *right,
3801 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003802{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003803 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003804 if (node == 0)
3805 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003806 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3807 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003808 recover();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003809 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02003810 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003811 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
3812 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003813 }
3814 return node;
3815}
3816
Jamie Madillb98c3a82015-07-23 14:26:04 -04003817TIntermTyped *TParseContext::createAssign(TOperator op,
3818 TIntermTyped *left,
3819 TIntermTyped *right,
3820 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003821{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003822 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003823 {
3824 return intermediate.addAssign(op, left, right, loc);
3825 }
3826 return nullptr;
3827}
3828
Jamie Madillb98c3a82015-07-23 14:26:04 -04003829TIntermTyped *TParseContext::addAssign(TOperator op,
3830 TIntermTyped *left,
3831 TIntermTyped *right,
3832 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003833{
3834 TIntermTyped *node = createAssign(op, left, right, loc);
3835 if (node == nullptr)
3836 {
3837 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
3838 recover();
3839 return left;
3840 }
3841 return node;
3842}
3843
Olli Etuaho49300862015-02-20 14:54:49 +02003844TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3845{
3846 switch (op)
3847 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003848 case EOpContinue:
3849 if (mLoopNestingLevel <= 0)
3850 {
3851 error(loc, "continue statement only allowed in loops", "");
3852 recover();
3853 }
3854 break;
3855 case EOpBreak:
3856 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
3857 {
3858 error(loc, "break statement only allowed in loops and switch statements", "");
3859 recover();
3860 }
3861 break;
3862 case EOpReturn:
3863 if (mCurrentFunctionType->getBasicType() != EbtVoid)
3864 {
3865 error(loc, "non-void function must return a value", "return");
3866 recover();
3867 }
3868 break;
3869 default:
3870 // No checks for discard
3871 break;
Olli Etuaho49300862015-02-20 14:54:49 +02003872 }
3873 return intermediate.addBranch(op, loc);
3874}
3875
Jamie Madillb98c3a82015-07-23 14:26:04 -04003876TIntermBranch *TParseContext::addBranch(TOperator op,
3877 TIntermTyped *returnValue,
3878 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02003879{
3880 ASSERT(op == EOpReturn);
3881 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003882 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02003883 {
3884 error(loc, "void function cannot return a value", "return");
3885 recover();
3886 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003887 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02003888 {
3889 error(loc, "function return is not matching type:", "return");
3890 recover();
3891 }
3892 return intermediate.addBranch(op, returnValue, loc);
3893}
3894
Jamie Madillb98c3a82015-07-23 14:26:04 -04003895TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
3896 TIntermNode *paramNode,
3897 TIntermNode *thisNode,
3898 const TSourceLoc &loc,
3899 bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003900{
Jamie Madillb98c3a82015-07-23 14:26:04 -04003901 *fatalError = false;
3902 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003903 TIntermTyped *callNode = nullptr;
3904
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003905 if (thisNode != nullptr)
3906 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003907 TConstantUnion *unionArray = new TConstantUnion[1];
Jamie Madillb98c3a82015-07-23 14:26:04 -04003908 int arraySize = 0;
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003909 TIntermTyped *typedThis = thisNode->getAsTyped();
3910 if (fnCall->getName() != "length")
3911 {
3912 error(loc, "invalid method", fnCall->getName().c_str());
3913 recover();
3914 }
3915 else if (paramNode != nullptr)
3916 {
3917 error(loc, "method takes no parameters", "length");
3918 recover();
3919 }
3920 else if (typedThis == nullptr || !typedThis->isArray())
3921 {
3922 error(loc, "length can only be called on arrays", "length");
3923 recover();
3924 }
3925 else
3926 {
Olli Etuaho96e67382015-04-23 14:27:02 +03003927 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03003928 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003929 {
Olli Etuaho39282e12015-04-23 15:41:48 +03003930 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003931 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03003932 // (func()).length()
3933 // (int[3](0, 1, 2)).length()
Jamie Madillb98c3a82015-07-23 14:26:04 -04003934 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
3935 // expression.
3936 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
3937 // spec section 5.9 which allows "An array, vector or matrix expression with the
3938 // length method applied".
3939 error(loc, "length can only be called on array names, not on array expressions",
3940 "length");
Olli Etuaho39282e12015-04-23 15:41:48 +03003941 recover();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003942 }
3943 }
Olli Etuaho96e67382015-04-23 14:27:02 +03003944 unionArray->setIConst(arraySize);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003945 callNode =
3946 intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003947 }
3948 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003949 {
3950 //
3951 // Then this should be a constructor.
3952 // Don't go through the symbol table for constructors.
3953 // Their parameters will be verified algorithmically.
3954 //
3955 TType type(EbtVoid, EbpUndefined); // use this to get the type back
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003956 if (!constructorErrorCheck(loc, paramNode, *fnCall, op, &type))
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003957 {
3958 //
3959 // It's a constructor, of type 'type'.
3960 //
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003961 callNode = addConstructor(paramNode, &type, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003962 }
Olli Etuaho72ba85b2015-03-04 14:23:26 +02003963
3964 if (callNode == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003965 {
3966 recover();
3967 callNode = intermediate.setAggregateOperator(nullptr, op, loc);
3968 }
3969 callNode->setType(type);
3970 }
3971 else
3972 {
3973 //
3974 // Not a constructor. Find it in the symbol table.
3975 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303976 const TFunction *fnCandidate;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003977 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003978 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003979 if (fnCandidate)
3980 {
3981 //
3982 // A declared function.
3983 //
3984 if (builtIn && !fnCandidate->getExtension().empty() &&
3985 extensionErrorCheck(loc, fnCandidate->getExtension()))
3986 {
3987 recover();
3988 }
3989 op = fnCandidate->getBuiltInOp();
3990 if (builtIn && op != EOpNull)
3991 {
3992 //
3993 // A function call mapped to a built-in operation.
3994 //
3995 if (fnCandidate->getParamCount() == 1)
3996 {
3997 //
3998 // Treat it like a built-in unary operator.
3999 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04004000 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc,
4001 &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004002 if (callNode == nullptr)
4003 {
4004 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004005 extraInfoStream
4006 << "built in unary operator function. Type: "
4007 << static_cast<TIntermTyped *>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004008 std::string extraInfo = extraInfoStream.str();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004009 error(paramNode->getLine(), " wrong operand type", "Internal Error",
4010 extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004011 *fatalError = true;
4012 return nullptr;
4013 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004014 }
4015 else
4016 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004017 TIntermAggregate *aggregate =
4018 intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004019 aggregate->setType(fnCandidate->getReturnType());
4020 aggregate->setPrecisionFromChildren();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004021
4022 // Some built-in functions have out parameters too.
4023 functionCallLValueErrorCheck(fnCandidate, aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05304024
4025 // See if we can constant fold a built-in.
Olli Etuahob43846e2015-06-02 18:18:57 +03004026 TIntermTyped *foldedNode = intermediate.foldAggregateBuiltIn(aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05304027 if (foldedNode)
4028 {
Arun Patole274f0702015-05-05 13:33:30 +05304029 callNode = foldedNode;
4030 }
Olli Etuahob43846e2015-06-02 18:18:57 +03004031 else
4032 {
4033 callNode = aggregate;
4034 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004035 }
4036 }
4037 else
4038 {
4039 // This is a real function call
4040
Jamie Madillb98c3a82015-07-23 14:26:04 -04004041 TIntermAggregate *aggregate =
4042 intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004043 aggregate->setType(fnCandidate->getReturnType());
4044
Jamie Madillb98c3a82015-07-23 14:26:04 -04004045 // this is how we know whether the given function is a builtIn function or a user
4046 // defined function
4047 // if builtIn == false, it's a userDefined -> could be an overloaded
4048 // builtIn function also
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004049 // if builtIn == true, it's definitely a builtIn function with EOpNull
4050 if (!builtIn)
4051 aggregate->setUserDefined();
4052 aggregate->setName(fnCandidate->getMangledName());
Corentin Wallez71d147f2015-02-11 11:15:24 -08004053 aggregate->setFunctionId(fnCandidate->getUniqueId());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004054
4055 // This needs to happen after the name is set
4056 if (builtIn)
4057 aggregate->setBuiltInFunctionPrecision();
4058
4059 callNode = aggregate;
4060
4061 functionCallLValueErrorCheck(fnCandidate, aggregate);
4062 }
4063 }
4064 else
4065 {
4066 // error message was put out by findFunction()
4067 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004068 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004069 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004070 callNode = intermediate.addConstantUnion(unionArray,
4071 TType(EbtFloat, EbpUndefined, EvqConst), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004072 recover();
4073 }
4074 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004075 return callNode;
4076}
4077
Jamie Madillb98c3a82015-07-23 14:26:04 -04004078TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
4079 TIntermTyped *trueBlock,
4080 TIntermTyped *falseBlock,
Olli Etuaho52901742015-04-15 13:42:45 +03004081 const TSourceLoc &loc)
4082{
4083 if (boolErrorCheck(loc, cond))
4084 recover();
4085
4086 if (trueBlock->getType() != falseBlock->getType())
4087 {
4088 binaryOpError(loc, ":", trueBlock->getCompleteString(), falseBlock->getCompleteString());
4089 recover();
4090 return falseBlock;
4091 }
Olli Etuahoa2d53032015-04-15 14:14:44 +03004092 // ESSL1 sections 5.2 and 5.7:
4093 // ESSL3 section 5.7:
4094 // Ternary operator is not among the operators allowed for structures/arrays.
4095 if (trueBlock->isArray() || trueBlock->getBasicType() == EbtStruct)
4096 {
4097 error(loc, "ternary operator is not allowed for structures or arrays", ":");
4098 recover();
4099 return falseBlock;
4100 }
Olli Etuaho52901742015-04-15 13:42:45 +03004101 return intermediate.addSelection(cond, trueBlock, falseBlock, loc);
4102}
Olli Etuaho49300862015-02-20 14:54:49 +02004103
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004104//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004105// Parse an array of strings using yyparse.
4106//
4107// Returns 0 for success.
4108//
Jamie Madillb98c3a82015-07-23 14:26:04 -04004109int PaParseStrings(size_t count,
4110 const char *const string[],
4111 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05304112 TParseContext *context)
4113{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004114 if ((count == 0) || (string == NULL))
4115 return 1;
4116
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004117 if (glslang_initialize(context))
4118 return 1;
4119
alokp@chromium.org408c45e2012-04-05 15:54:43 +00004120 int error = glslang_scan(count, string, length, context);
4121 if (!error)
4122 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004123
alokp@chromium.org73bc2982012-06-19 18:48:05 +00004124 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00004125
alokp@chromium.org6b495712012-06-29 00:06:58 +00004126 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004127}