blob: e7069e4bb77296dc75505ac0fbd3e5314a0e836c [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 {
602 type->setArraySize(function.getParamCount());
603 }
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{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001146 const TExtensionBehavior &extbehavior = extensionBehavior();
Shannon Woodsa49a9bf2013-08-02 17:23:14 -04001147 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001148
1149 if (iter == extbehavior.end())
1150 {
1151 return false;
1152 }
1153
1154 return (iter->second == EBhEnable || iter->second == EBhRequire);
1155}
1156
Jamie Madillb98c3a82015-07-23 14:26:04 -04001157void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1158 const char *extName,
1159 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001160{
1161 pp::SourceLocation srcLoc;
1162 srcLoc.file = loc.first_file;
1163 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001164 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001165}
1166
Jamie Madillb98c3a82015-07-23 14:26:04 -04001167void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1168 const char *name,
1169 const char *value,
1170 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001171{
1172 pp::SourceLocation srcLoc;
1173 srcLoc.file = loc.first_file;
1174 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001175 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001176}
1177
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001178/////////////////////////////////////////////////////////////////////////////////
1179//
1180// Non-Errors.
1181//
1182/////////////////////////////////////////////////////////////////////////////////
1183
Jamie Madill5c097022014-08-20 16:38:32 -04001184const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1185 const TString *name,
1186 const TSymbol *symbol)
1187{
1188 const TVariable *variable = NULL;
1189
1190 if (!symbol)
1191 {
1192 error(location, "undeclared identifier", name->c_str());
1193 recover();
1194 }
1195 else if (!symbol->isVariable())
1196 {
1197 error(location, "variable expected", name->c_str());
1198 recover();
1199 }
1200 else
1201 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001202 variable = static_cast<const TVariable *>(symbol);
Jamie Madill5c097022014-08-20 16:38:32 -04001203
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001204 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
Jamie Madill5c097022014-08-20 16:38:32 -04001205 !variable->getExtension().empty() &&
1206 extensionErrorCheck(location, variable->getExtension()))
1207 {
1208 recover();
1209 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001210
1211 // Reject shaders using both gl_FragData and gl_FragColor
1212 TQualifier qualifier = variable->getType().getQualifier();
1213 if (qualifier == EvqFragData)
1214 {
1215 mUsesFragData = true;
1216 }
1217 else if (qualifier == EvqFragColor)
1218 {
1219 mUsesFragColor = true;
1220 }
1221
1222 // This validation is not quite correct - it's only an error to write to
1223 // both FragData and FragColor. For simplicity, and because users shouldn't
1224 // be rewarded for reading from undefined varaibles, return an error
1225 // if they are both referenced, rather than assigned.
1226 if (mUsesFragData && mUsesFragColor)
1227 {
1228 error(location, "cannot use both gl_FragData and gl_FragColor", name->c_str());
1229 recover();
1230 }
Jamie Madill5c097022014-08-20 16:38:32 -04001231 }
1232
1233 if (!variable)
1234 {
1235 TType type(EbtFloat, EbpUndefined);
1236 TVariable *fakeVariable = new TVariable(name, type);
1237 symbolTable.declare(fakeVariable);
1238 variable = fakeVariable;
1239 }
1240
1241 return variable;
1242}
1243
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001244//
1245// Look up a function name in the symbol table, and make sure it is a function.
1246//
1247// Return the function symbol if found, otherwise 0.
1248//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001249const TFunction *TParseContext::findFunction(const TSourceLoc &line,
1250 TFunction *call,
1251 int inputShaderVersion,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301252 bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001253{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001254 // First find by unmangled name to check whether the function name has been
1255 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001256 // If a function is found, check for one with a matching argument list.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301257 const TSymbol *symbol = symbolTable.find(call->getName(), inputShaderVersion, builtIn);
1258 if (symbol == 0 || symbol->isFunction())
1259 {
Austin Kinross3ae64652015-01-26 15:51:39 -08001260 symbol = symbolTable.find(call->getMangledName(), inputShaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001261 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001262
Arun Patole7e7e68d2015-05-22 12:02:25 +05301263 if (symbol == 0)
1264 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001265 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001266 return 0;
1267 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001268
Arun Patole7e7e68d2015-05-22 12:02:25 +05301269 if (!symbol->isFunction())
1270 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001271 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001272 return 0;
1273 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001274
Jamie Madillb98c3a82015-07-23 14:26:04 -04001275 return static_cast<const TFunction *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001276}
1277
1278//
1279// Initializers show up in several places in the grammar. Have one set of
1280// code to handle them here.
1281//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001282// Returns true on error, false if no error
1283//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001284bool TParseContext::executeInitializer(const TSourceLoc &line,
1285 const TString &identifier,
1286 const TPublicType &pType,
1287 TIntermTyped *initializer,
1288 TIntermNode **intermNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001289{
Olli Etuahoe7847b02015-03-16 11:56:12 +02001290 ASSERT(intermNode != nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001291 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001292
Olli Etuaho2935c582015-04-08 14:32:06 +03001293 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001294 if (type.isUnsizedArray())
1295 {
1296 type.setArraySize(initializer->getArraySize());
1297 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001298 if (!declareVariable(line, identifier, type, &variable))
1299 {
1300 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001301 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001302
Olli Etuahob0c645e2015-05-12 14:25:36 +03001303 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001304 if (symbolTable.atGlobalLevel() &&
1305 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001306 {
1307 // Error message does not completely match behavior with ESSL 1.00, but
1308 // we want to steer developers towards only using constant expressions.
1309 error(line, "global variable initializers must be constant expressions", "=");
1310 return true;
1311 }
1312 if (globalInitWarning)
1313 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001314 warning(
1315 line,
1316 "global variable initializers should be constant expressions "
1317 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1318 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001319 }
1320
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001321 //
1322 // identifier must be of type constant, a global, or a temporary
1323 //
1324 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301325 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1326 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001327 error(line, " cannot initialize this type of qualifier ",
1328 variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001329 return true;
1330 }
1331 //
1332 // test for and propagate constant
1333 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001334
Arun Patole7e7e68d2015-05-22 12:02:25 +05301335 if (qualifier == EvqConst)
1336 {
1337 if (qualifier != initializer->getType().getQualifier())
1338 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001339 std::stringstream extraInfoStream;
1340 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1341 std::string extraInfo = extraInfoStream.str();
1342 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001343 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001344 return true;
1345 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301346 if (type != initializer->getType())
1347 {
1348 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001349 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001350 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001351 return true;
1352 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301353 if (initializer->getAsConstantUnion())
1354 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001355 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Arun Patole7e7e68d2015-05-22 12:02:25 +05301356 }
1357 else if (initializer->getAsSymbolNode())
1358 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001359 const TSymbol *symbol =
1360 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1361 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001362
Arun Patole7e7e68d2015-05-22 12:02:25 +05301363 TConstantUnion *constArray = tVar->getConstPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001364 variable->shareConstPointer(constArray);
Arun Patole7e7e68d2015-05-22 12:02:25 +05301365 }
1366 else
1367 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001368 std::stringstream extraInfoStream;
1369 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1370 std::string extraInfo = extraInfoStream.str();
1371 error(line, " cannot assign to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001372 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001373 return true;
1374 }
1375 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001376
1377 if (qualifier != EvqConst)
1378 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001379 TIntermSymbol *intermSymbol = intermediate.addSymbol(
1380 variable->getUniqueId(), variable->getName(), variable->getType(), line);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001381 *intermNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1382 if (*intermNode == nullptr)
1383 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001384 assignError(line, "=", intermSymbol->getCompleteString(),
1385 initializer->getCompleteString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001386 return true;
1387 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001388 }
1389 else
1390 {
1391 *intermNode = nullptr;
1392 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001393
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001394 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001395}
1396
Arun Patole7e7e68d2015-05-22 12:02:25 +05301397bool TParseContext::areAllChildConst(TIntermAggregate *aggrNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001398{
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001399 ASSERT(aggrNode != NULL);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001400 if (!aggrNode->isConstructor())
1401 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001402
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001403 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001404
Arun Patole7e7e68d2015-05-22 12:02:25 +05301405 // check if all the child nodes are constants so that they can be inserted into
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001406 // the parent node
Jamie Madillb98c3a82015-07-23 14:26:04 -04001407 TIntermSequence *sequence = aggrNode->getSequence();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301408 for (TIntermSequence::iterator p = sequence->begin(); p != sequence->end(); ++p)
1409 {
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001410 if (!(*p)->getAsTyped()->getAsConstantUnion())
1411 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001412 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001413
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001414 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001415}
1416
Jamie Madillb98c3a82015-07-23 14:26:04 -04001417TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier,
1418 bool invariant,
1419 TLayoutQualifier layoutQualifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301420 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001421{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001422 TPublicType returnType = typeSpecifier;
1423 returnType.qualifier = qualifier;
1424 returnType.invariant = invariant;
Jamie Madilla5efff92013-06-06 11:56:47 -04001425 returnType.layoutQualifier = layoutQualifier;
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001426
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001427 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001428 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001429 if (typeSpecifier.array)
1430 {
1431 error(typeSpecifier.line, "not supported", "first-class array");
1432 recover();
1433 returnType.clearArrayness();
1434 }
1435
Jamie Madillb98c3a82015-07-23 14:26:04 -04001436 if (qualifier == EvqAttribute &&
1437 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001438 {
1439 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1440 recover();
1441 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001442
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001443 if ((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1444 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1445 {
1446 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1447 recover();
1448 }
1449 }
1450 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001451 {
Olli Etuahoabb0c382015-07-13 12:01:12 +03001452 if (!layoutQualifier.isEmpty())
1453 {
1454 if (globalErrorCheck(typeSpecifier.line, symbolTable.atGlobalLevel(), "layout"))
1455 {
1456 recover();
1457 }
1458 }
Olli Etuahocc36b982015-07-10 14:14:18 +03001459 if (sh::IsVarying(qualifier) || qualifier == EvqVertexIn || qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001460 {
Olli Etuahocc36b982015-07-10 14:14:18 +03001461 es3InputOutputTypeCheck(qualifier, typeSpecifier, typeSpecifier.line);
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001462 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001463 }
1464
1465 return returnType;
1466}
1467
Olli Etuahocc36b982015-07-10 14:14:18 +03001468void TParseContext::es3InputOutputTypeCheck(const TQualifier qualifier,
1469 const TPublicType &type,
1470 const TSourceLoc &qualifierLocation)
1471{
1472 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
1473 if (type.type == EbtBool)
1474 {
1475 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
1476 recover();
1477 }
1478
1479 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
1480 switch (qualifier)
1481 {
1482 case EvqVertexIn:
1483 // ESSL 3.00 section 4.3.4
1484 if (type.array)
1485 {
1486 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
1487 recover();
1488 }
1489 // Vertex inputs with a struct type are disallowed in singleDeclarationErrorCheck
1490 return;
1491 case EvqFragmentOut:
1492 // ESSL 3.00 section 4.3.6
1493 if (type.isMatrix())
1494 {
1495 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
1496 recover();
1497 }
1498 // Fragment outputs with a struct type are disallowed in singleDeclarationErrorCheck
1499 return;
1500 default:
1501 break;
1502 }
1503
1504 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
1505 // restrictions.
1506 bool typeContainsIntegers =
1507 (type.type == EbtInt || type.type == EbtUInt || type.isStructureContainingType(EbtInt) ||
1508 type.isStructureContainingType(EbtUInt));
1509 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
1510 {
1511 error(qualifierLocation, "must use 'flat' interpolation here",
1512 getQualifierString(qualifier));
1513 recover();
1514 }
1515
1516 if (type.type == EbtStruct)
1517 {
1518 // ESSL 3.00 sections 4.3.4 and 4.3.6.
1519 // These restrictions are only implied by the ESSL 3.00 spec, but
1520 // the ESSL 3.10 spec lists these restrictions explicitly.
1521 if (type.array)
1522 {
1523 error(qualifierLocation, "cannot be an array of structures",
1524 getQualifierString(qualifier));
1525 recover();
1526 }
1527 if (type.isStructureContainingArrays())
1528 {
1529 error(qualifierLocation, "cannot be a structure containing an array",
1530 getQualifierString(qualifier));
1531 recover();
1532 }
1533 if (type.isStructureContainingType(EbtStruct))
1534 {
1535 error(qualifierLocation, "cannot be a structure containing a structure",
1536 getQualifierString(qualifier));
1537 recover();
1538 }
1539 if (type.isStructureContainingType(EbtBool))
1540 {
1541 error(qualifierLocation, "cannot be a structure containing a bool",
1542 getQualifierString(qualifier));
1543 recover();
1544 }
1545 }
1546}
1547
Olli Etuahofa33d582015-04-09 14:33:12 +03001548TIntermAggregate *TParseContext::parseSingleDeclaration(TPublicType &publicType,
1549 const TSourceLoc &identifierOrTypeLocation,
1550 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001551{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001552 TIntermSymbol *symbol =
1553 intermediate.addSymbol(0, identifier, TType(publicType), identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001554
Olli Etuahobab4c082015-04-24 16:38:49 +03001555 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03001556
Olli Etuahobab4c082015-04-24 16:38:49 +03001557 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1558
1559 if (emptyDeclaration)
1560 {
1561 if (publicType.isUnsizedArray())
1562 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001563 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1564 // error. It is assumed that this applies to empty declarations as well.
1565 error(identifierOrTypeLocation, "empty array declaration needs to specify a size",
1566 identifier.c_str());
Olli Etuahobab4c082015-04-24 16:38:49 +03001567 }
1568 }
1569 else
Jamie Madill60ed9812013-06-06 11:56:46 -04001570 {
Olli Etuahofa33d582015-04-09 14:33:12 +03001571 if (singleDeclarationErrorCheck(publicType, identifierOrTypeLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001572 recover();
1573
Olli Etuaho376f1b52015-04-13 13:23:41 +03001574 if (nonInitErrorCheck(identifierOrTypeLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001575 recover();
1576
Olli Etuaho2935c582015-04-08 14:32:06 +03001577 TVariable *variable = nullptr;
Olli Etuahofa33d582015-04-09 14:33:12 +03001578 if (!declareVariable(identifierOrTypeLocation, identifier, TType(publicType), &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001579 recover();
1580
1581 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001582 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001583 }
1584
Olli Etuahoe7847b02015-03-16 11:56:12 +02001585 return intermediate.makeAggregate(symbol, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001586}
1587
Olli Etuahoe7847b02015-03-16 11:56:12 +02001588TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1589 const TSourceLoc &identifierLocation,
1590 const TString &identifier,
1591 const TSourceLoc &indexLocation,
1592 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001593{
Olli Etuahofa33d582015-04-09 14:33:12 +03001594 mDeferredSingleDeclarationErrorCheck = false;
1595
1596 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001597 recover();
1598
Olli Etuaho376f1b52015-04-13 13:23:41 +03001599 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001600 recover();
1601
Jamie Madillb98c3a82015-07-23 14:26:04 -04001602 if (arrayTypeErrorCheck(indexLocation, publicType) ||
1603 arrayQualifierErrorCheck(indexLocation, publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001604 {
1605 recover();
1606 }
1607
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001608 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001609
1610 int size;
1611 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1612 {
1613 recover();
1614 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001615 // Make the type an array even if size check failed.
1616 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1617 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001618
Olli Etuaho2935c582015-04-08 14:32:06 +03001619 TVariable *variable = nullptr;
1620 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001621 recover();
1622
Olli Etuahoe7847b02015-03-16 11:56:12 +02001623 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001624 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001625 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001626
Olli Etuahoe7847b02015-03-16 11:56:12 +02001627 return intermediate.makeAggregate(symbol, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001628}
1629
Jamie Madill06145232015-05-13 13:10:01 -04001630TIntermAggregate *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001631 const TSourceLoc &identifierLocation,
1632 const TString &identifier,
1633 const TSourceLoc &initLocation,
1634 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001635{
Olli Etuahofa33d582015-04-09 14:33:12 +03001636 mDeferredSingleDeclarationErrorCheck = false;
1637
1638 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001639 recover();
1640
Olli Etuahoe7847b02015-03-16 11:56:12 +02001641 TIntermNode *intermNode = nullptr;
1642 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04001643 {
1644 //
1645 // Build intermediate representation
1646 //
Olli Etuahoe7847b02015-03-16 11:56:12 +02001647 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001648 }
1649 else
1650 {
1651 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001652 return nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001653 }
1654}
1655
Jamie Madillb98c3a82015-07-23 14:26:04 -04001656TIntermAggregate *TParseContext::parseSingleArrayInitDeclaration(
1657 TPublicType &publicType,
1658 const TSourceLoc &identifierLocation,
1659 const TString &identifier,
1660 const TSourceLoc &indexLocation,
1661 TIntermTyped *indexExpression,
1662 const TSourceLoc &initLocation,
1663 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001664{
1665 mDeferredSingleDeclarationErrorCheck = false;
1666
1667 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1668 recover();
1669
Jamie Madillb98c3a82015-07-23 14:26:04 -04001670 if (arrayTypeErrorCheck(indexLocation, publicType) ||
1671 arrayQualifierErrorCheck(indexLocation, publicType))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001672 {
1673 recover();
1674 }
1675
1676 TPublicType arrayType(publicType);
1677
Olli Etuaho376f1b52015-04-13 13:23:41 +03001678 int size = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001679 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
1680 // the initializer.
1681 if (indexExpression != nullptr &&
1682 arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001683 {
1684 recover();
1685 }
1686 // Make the type an array even if size check failed.
1687 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1688 arrayType.setArraySize(size);
1689
1690 // initNode will correspond to the whole of "type b[n] = initializer".
1691 TIntermNode *initNode = nullptr;
1692 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1693 {
1694 return initNode ? intermediate.makeAggregate(initNode, initLocation) : nullptr;
1695 }
1696 else
1697 {
1698 recover();
1699 return nullptr;
1700 }
1701}
1702
Olli Etuahoe7847b02015-03-16 11:56:12 +02001703TIntermAggregate *TParseContext::parseInvariantDeclaration(const TSourceLoc &invariantLoc,
Jamie Madill47e3ec02014-08-20 16:38:33 -04001704 const TSourceLoc &identifierLoc,
1705 const TString *identifier,
1706 const TSymbol *symbol)
1707{
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001708 // invariant declaration
Jamie Madill47e3ec02014-08-20 16:38:33 -04001709 if (globalErrorCheck(invariantLoc, symbolTable.atGlobalLevel(), "invariant varying"))
1710 {
1711 recover();
1712 }
1713
1714 if (!symbol)
1715 {
1716 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
1717 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001718 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001719 }
1720 else
1721 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001722 const TString kGlFrontFacing("gl_FrontFacing");
1723 if (*identifier == kGlFrontFacing)
1724 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001725 error(identifierLoc, "identifier should not be declared as invariant",
1726 identifier->c_str());
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001727 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001728 return nullptr;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001729 }
Jamie Madill2c433252014-12-03 12:36:54 -05001730 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001731 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1732 ASSERT(variable);
1733 const TType &type = variable->getType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04001734 TIntermSymbol *intermSymbol =
1735 intermediate.addSymbol(variable->getUniqueId(), *identifier, type, identifierLoc);
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001736
1737 TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc);
1738 aggregate->setOp(EOpInvariantDeclaration);
1739 return aggregate;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001740 }
1741}
1742
Jamie Madillb98c3a82015-07-23 14:26:04 -04001743TIntermAggregate *TParseContext::parseDeclarator(TPublicType &publicType,
1744 TIntermAggregate *aggregateDeclaration,
1745 const TSourceLoc &identifierLocation,
1746 const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001747{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001748 // If the declaration starting this declarator list was empty (example: int,), some checks were
1749 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001750 if (mDeferredSingleDeclarationErrorCheck)
1751 {
1752 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1753 recover();
1754 mDeferredSingleDeclarationErrorCheck = false;
1755 }
1756
Jamie Madill0bd18df2013-06-20 11:55:52 -04001757 if (locationDeclaratorListCheck(identifierLocation, publicType))
1758 recover();
1759
Olli Etuaho376f1b52015-04-13 13:23:41 +03001760 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001761 recover();
1762
Olli Etuaho2935c582015-04-08 14:32:06 +03001763 TVariable *variable = nullptr;
1764 if (!declareVariable(identifierLocation, identifier, TType(publicType), &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001765 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001766
Jamie Madillb98c3a82015-07-23 14:26:04 -04001767 TIntermSymbol *symbol =
1768 intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001769 if (variable && symbol)
Jamie Madill502d66f2013-06-20 11:55:52 -04001770 symbol->setId(variable->getUniqueId());
1771
Olli Etuahoe7847b02015-03-16 11:56:12 +02001772 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001773}
1774
Jamie Madillb98c3a82015-07-23 14:26:04 -04001775TIntermAggregate *TParseContext::parseArrayDeclarator(TPublicType &publicType,
1776 TIntermAggregate *aggregateDeclaration,
1777 const TSourceLoc &identifierLocation,
1778 const TString &identifier,
1779 const TSourceLoc &arrayLocation,
1780 TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001781{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001782 // If the declaration starting this declarator list was empty (example: int,), some checks were
1783 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001784 if (mDeferredSingleDeclarationErrorCheck)
1785 {
1786 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1787 recover();
1788 mDeferredSingleDeclarationErrorCheck = false;
1789 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001790
Jamie Madill0bd18df2013-06-20 11:55:52 -04001791 if (locationDeclaratorListCheck(identifierLocation, publicType))
1792 recover();
1793
Olli Etuaho376f1b52015-04-13 13:23:41 +03001794 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001795 recover();
1796
Jamie Madillb98c3a82015-07-23 14:26:04 -04001797 if (arrayTypeErrorCheck(arrayLocation, publicType) ||
1798 arrayQualifierErrorCheck(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001799 {
1800 recover();
1801 }
Olli Etuaho93a90fd2015-04-07 18:14:07 +03001802 else
Jamie Madill502d66f2013-06-20 11:55:52 -04001803 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001804 TType arrayType = TType(publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04001805 int size;
1806 if (arraySizeErrorCheck(arrayLocation, indexExpression, size))
Olli Etuahoe7847b02015-03-16 11:56:12 +02001807 {
Jamie Madill502d66f2013-06-20 11:55:52 -04001808 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001809 }
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001810 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001811
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001812 TVariable *variable = nullptr;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001813 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001814 recover();
Jamie Madill502d66f2013-06-20 11:55:52 -04001815
Jamie Madillb98c3a82015-07-23 14:26:04 -04001816 TIntermSymbol *symbol =
1817 intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001818 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001819 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001820
1821 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001822 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001823
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001824 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001825}
1826
Jamie Madillb98c3a82015-07-23 14:26:04 -04001827TIntermAggregate *TParseContext::parseInitDeclarator(const TPublicType &publicType,
1828 TIntermAggregate *aggregateDeclaration,
1829 const TSourceLoc &identifierLocation,
1830 const TString &identifier,
1831 const TSourceLoc &initLocation,
1832 TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001833{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001834 // If the declaration starting this declarator list was empty (example: int,), some checks were
1835 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03001836 if (mDeferredSingleDeclarationErrorCheck)
1837 {
1838 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1839 recover();
1840 mDeferredSingleDeclarationErrorCheck = false;
1841 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001842
Jamie Madill0bd18df2013-06-20 11:55:52 -04001843 if (locationDeclaratorListCheck(identifierLocation, publicType))
1844 recover();
1845
Olli Etuahoe7847b02015-03-16 11:56:12 +02001846 TIntermNode *intermNode = nullptr;
1847 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04001848 {
1849 //
1850 // build the intermediate representation
1851 //
1852 if (intermNode)
1853 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001854 return intermediate.growAggregate(aggregateDeclaration, intermNode, initLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001855 }
1856 else
1857 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001858 return aggregateDeclaration;
Jamie Madill502d66f2013-06-20 11:55:52 -04001859 }
1860 }
1861 else
1862 {
1863 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001864 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001865 }
1866}
1867
Jamie Madill06145232015-05-13 13:10:01 -04001868TIntermAggregate *TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001869 TIntermAggregate *aggregateDeclaration,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301870 const TSourceLoc &identifierLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001871 const TString &identifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301872 const TSourceLoc &indexLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001873 TIntermTyped *indexExpression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001874 const TSourceLoc &initLocation,
1875 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001876{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001877 // If the declaration starting this declarator list was empty (example: int,), some checks were
1878 // not performed.
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001879 if (mDeferredSingleDeclarationErrorCheck)
1880 {
1881 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1882 recover();
1883 mDeferredSingleDeclarationErrorCheck = false;
1884 }
1885
1886 if (locationDeclaratorListCheck(identifierLocation, publicType))
1887 recover();
1888
Jamie Madillb98c3a82015-07-23 14:26:04 -04001889 if (arrayTypeErrorCheck(indexLocation, publicType) ||
1890 arrayQualifierErrorCheck(indexLocation, publicType))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001891 {
1892 recover();
1893 }
1894
1895 TPublicType arrayType(publicType);
1896
Olli Etuaho376f1b52015-04-13 13:23:41 +03001897 int size = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001898 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
1899 // the initializer.
1900 if (indexExpression != nullptr &&
1901 arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001902 {
1903 recover();
1904 }
1905 // Make the type an array even if size check failed.
1906 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1907 arrayType.setArraySize(size);
1908
1909 // initNode will correspond to the whole of "b[n] = initializer".
1910 TIntermNode *initNode = nullptr;
1911 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1912 {
1913 if (initNode)
1914 {
1915 return intermediate.growAggregate(aggregateDeclaration, initNode, initLocation);
1916 }
1917 else
1918 {
1919 return aggregateDeclaration;
1920 }
1921 }
1922 else
1923 {
1924 recover();
1925 return nullptr;
1926 }
1927}
1928
Jamie Madilla295edf2013-06-06 11:56:48 -04001929void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1930{
1931 if (typeQualifier.qualifier != EvqUniform)
1932 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001933 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
1934 "global layout must be uniform");
Jamie Madilla295edf2013-06-06 11:56:48 -04001935 recover();
1936 return;
1937 }
1938
1939 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1940 ASSERT(!layoutQualifier.isEmpty());
1941
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001942 if (mShaderVersion < 300)
Jamie Madilla295edf2013-06-06 11:56:48 -04001943 {
1944 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1945 recover();
1946 return;
1947 }
1948
1949 if (layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1950 {
1951 recover();
1952 return;
1953 }
1954
Jamie Madill099c0f32013-06-20 11:55:52 -04001955 if (layoutQualifier.matrixPacking != EmpUnspecified)
1956 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001957 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04001958 }
1959
Geoff Langc6856732014-02-11 09:38:55 -05001960 if (layoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04001961 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001962 mDefaultBlockStorage = layoutQualifier.blockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04001963 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001964}
1965
Jamie Madill185fb402015-06-12 15:48:48 -04001966void TParseContext::parseFunctionPrototype(const TSourceLoc &location,
1967 TFunction *function,
1968 TIntermAggregate **aggregateOut)
1969{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001970 const TSymbol *builtIn =
1971 symbolTable.findBuiltIn(function->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04001972
1973 if (builtIn)
1974 {
1975 error(location, "built-in functions cannot be redefined", function->getName().c_str());
1976 recover();
1977 }
1978
Jamie Madillb98c3a82015-07-23 14:26:04 -04001979 TFunction *prevDec =
1980 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Jamie Madill185fb402015-06-12 15:48:48 -04001981 //
1982 // Note: 'prevDec' could be 'function' if this is the first time we've seen function
1983 // as it would have just been put in the symbol table. Otherwise, we're looking up
1984 // an earlier occurance.
1985 //
1986 if (prevDec->isDefined())
1987 {
1988 // Then this function already has a body.
1989 error(location, "function already has a body", function->getName().c_str());
1990 recover();
1991 }
1992 prevDec->setDefined();
1993 //
1994 // Overload the unique ID of the definition to be the same unique ID as the declaration.
1995 // Eventually we will probably want to have only a single definition and just swap the
1996 // arguments to be the definition's arguments.
1997 //
1998 function->setUniqueId(prevDec->getUniqueId());
1999
2000 // Raise error message if main function takes any parameters or return anything other than void
2001 if (function->getName() == "main")
2002 {
2003 if (function->getParamCount() > 0)
2004 {
2005 error(location, "function cannot take any parameter(s)", function->getName().c_str());
2006 recover();
2007 }
2008 if (function->getReturnType().getBasicType() != EbtVoid)
2009 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002010 error(location, "", function->getReturnType().getBasicString(),
2011 "main function cannot return a value");
Jamie Madill185fb402015-06-12 15:48:48 -04002012 recover();
2013 }
2014 }
2015
2016 //
2017 // Remember the return type for later checking for RETURN statements.
2018 //
2019 setCurrentFunctionType(&(prevDec->getReturnType()));
2020 setFunctionReturnsValue(false);
2021
2022 //
2023 // Insert parameters into the symbol table.
2024 // If the parameter has no name, it's not an error, just don't insert it
2025 // (could be used for unused args).
2026 //
2027 // Also, accumulate the list of parameters into the HIL, so lower level code
2028 // knows where to find parameters.
2029 //
2030 TIntermAggregate *paramNodes = new TIntermAggregate;
2031 for (size_t i = 0; i < function->getParamCount(); i++)
2032 {
2033 const TConstParameter &param = function->getParam(i);
2034 if (param.name != 0)
2035 {
2036 TVariable *variable = new TVariable(param.name, *param.type);
2037 //
2038 // Insert the parameters with name in the symbol table.
2039 //
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002040 if (!symbolTable.declare(variable))
2041 {
Jamie Madill185fb402015-06-12 15:48:48 -04002042 error(location, "redefinition", variable->getName().c_str());
2043 recover();
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002044 paramNodes = intermediate.growAggregate(
2045 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
2046 continue;
Jamie Madill185fb402015-06-12 15:48:48 -04002047 }
2048
2049 //
2050 // Add the parameter to the HIL
2051 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002052 TIntermSymbol *symbol = intermediate.addSymbol(
2053 variable->getUniqueId(), variable->getName(), variable->getType(), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002054
2055 paramNodes = intermediate.growAggregate(paramNodes, symbol, location);
2056 }
2057 else
2058 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002059 paramNodes = intermediate.growAggregate(
2060 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002061 }
2062 }
2063 intermediate.setAggregateOperator(paramNodes, EOpParameters, location);
2064 *aggregateOut = paramNodes;
2065 setLoopNestingLevel(0);
2066}
2067
Jamie Madillb98c3a82015-07-23 14:26:04 -04002068TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002069{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002070 //
2071 // Multiple declarations of the same function are allowed.
2072 //
2073 // If this is a definition, the definition production code will check for redefinitions
2074 // (we don't know at this point if it's a definition or not).
2075 //
2076 // Redeclarations are allowed. But, return types and parameter qualifiers must match.
2077 //
2078 TFunction *prevDec =
2079 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
2080 if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002081 {
2082 if (prevDec->getReturnType() != function->getReturnType())
2083 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002084 error(location, "overloaded functions must have the same return type",
Jamie Madill185fb402015-06-12 15:48:48 -04002085 function->getReturnType().getBasicString());
2086 recover();
2087 }
2088 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2089 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002090 if (prevDec->getParam(i).type->getQualifier() !=
2091 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002092 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002093 error(location, "overloaded functions must have the same parameter qualifiers",
Jamie Madill185fb402015-06-12 15:48:48 -04002094 function->getParam(i).type->getQualifierString());
2095 recover();
2096 }
2097 }
2098 }
2099
2100 //
2101 // Check for previously declared variables using the same name.
2102 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002103 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002104 if (prevSym)
2105 {
2106 if (!prevSym->isFunction())
2107 {
2108 error(location, "redefinition", function->getName().c_str(), "function");
2109 recover();
2110 }
2111 }
2112 else
2113 {
2114 // Insert the unmangled name to detect potential future redefinition as a variable.
Jamie Madillb98c3a82015-07-23 14:26:04 -04002115 TFunction *newFunction =
2116 new TFunction(NewPoolTString(function->getName().c_str()), &function->getReturnType());
Jamie Madill185fb402015-06-12 15:48:48 -04002117 symbolTable.getOuterLevel()->insertUnmangled(newFunction);
2118 }
2119
2120 // We're at the inner scope level of the function's arguments and body statement.
2121 // Add the function prototype to the surrounding scope instead.
2122 symbolTable.getOuterLevel()->insert(function);
2123
2124 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002125 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2126 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002127 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2128 //
2129 return function;
2130}
2131
Jamie Madill06145232015-05-13 13:10:01 -04002132TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002133{
Jamie Madill06145232015-05-13 13:10:01 -04002134 TPublicType publicType = publicTypeIn;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002135 TOperator op = EOpNull;
2136 if (publicType.userDef)
2137 {
2138 op = EOpConstructStruct;
2139 }
2140 else
2141 {
2142 switch (publicType.type)
2143 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002144 case EbtFloat:
2145 if (publicType.isMatrix())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002146 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002147 switch (publicType.getCols())
Alexis Hetu07e57df2015-06-16 16:55:52 -04002148 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002149 case 2:
2150 switch (publicType.getRows())
2151 {
2152 case 2:
2153 op = EOpConstructMat2;
2154 break;
2155 case 3:
2156 op = EOpConstructMat2x3;
2157 break;
2158 case 4:
2159 op = EOpConstructMat2x4;
2160 break;
2161 }
2162 break;
2163 case 3:
2164 switch (publicType.getRows())
2165 {
2166 case 2:
2167 op = EOpConstructMat3x2;
2168 break;
2169 case 3:
2170 op = EOpConstructMat3;
2171 break;
2172 case 4:
2173 op = EOpConstructMat3x4;
2174 break;
2175 }
2176 break;
2177 case 4:
2178 switch (publicType.getRows())
2179 {
2180 case 2:
2181 op = EOpConstructMat4x2;
2182 break;
2183 case 3:
2184 op = EOpConstructMat4x3;
2185 break;
2186 case 4:
2187 op = EOpConstructMat4;
2188 break;
2189 }
2190 break;
Alexis Hetu07e57df2015-06-16 16:55:52 -04002191 }
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002192 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04002193 else
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002194 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002195 switch (publicType.getNominalSize())
2196 {
2197 case 1:
2198 op = EOpConstructFloat;
2199 break;
2200 case 2:
2201 op = EOpConstructVec2;
2202 break;
2203 case 3:
2204 op = EOpConstructVec3;
2205 break;
2206 case 4:
2207 op = EOpConstructVec4;
2208 break;
2209 }
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002210 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04002211 break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002212
Jamie Madillb98c3a82015-07-23 14:26:04 -04002213 case EbtInt:
2214 switch (publicType.getNominalSize())
2215 {
2216 case 1:
2217 op = EOpConstructInt;
2218 break;
2219 case 2:
2220 op = EOpConstructIVec2;
2221 break;
2222 case 3:
2223 op = EOpConstructIVec3;
2224 break;
2225 case 4:
2226 op = EOpConstructIVec4;
2227 break;
2228 }
2229 break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002230
Jamie Madillb98c3a82015-07-23 14:26:04 -04002231 case EbtUInt:
2232 switch (publicType.getNominalSize())
2233 {
2234 case 1:
2235 op = EOpConstructUInt;
2236 break;
2237 case 2:
2238 op = EOpConstructUVec2;
2239 break;
2240 case 3:
2241 op = EOpConstructUVec3;
2242 break;
2243 case 4:
2244 op = EOpConstructUVec4;
2245 break;
2246 }
2247 break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002248
Jamie Madillb98c3a82015-07-23 14:26:04 -04002249 case EbtBool:
2250 switch (publicType.getNominalSize())
2251 {
2252 case 1:
2253 op = EOpConstructBool;
2254 break;
2255 case 2:
2256 op = EOpConstructBVec2;
2257 break;
2258 case 3:
2259 op = EOpConstructBVec3;
2260 break;
2261 case 4:
2262 op = EOpConstructBVec4;
2263 break;
2264 }
2265 break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002266
Jamie Madillb98c3a82015-07-23 14:26:04 -04002267 default:
2268 break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002269 }
2270
2271 if (op == EOpNull)
2272 {
2273 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
2274 recover();
2275 publicType.type = EbtFloat;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002276 op = EOpConstructFloat;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002277 }
2278 }
2279
2280 TString tempString;
Dmitry Skiba7f17a502015-06-22 15:08:39 -07002281 const TType *type = new TType(publicType);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002282 return new TFunction(&tempString, type, op);
2283}
2284
Jamie Madillb98c3a82015-07-23 14:26:04 -04002285// This function is used to test for the correctness of the parameters passed to various constructor
2286// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002287//
2288// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
2289//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002290TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments,
2291 TType *type,
2292 TOperator op,
2293 TFunction *fnCall,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302294 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002295{
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002296 TIntermAggregate *aggregateArguments = arguments->getAsAggregate();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002297
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002298 if (!aggregateArguments)
2299 {
2300 aggregateArguments = new TIntermAggregate;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002301 aggregateArguments->getSequence()->push_back(arguments);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002302 }
2303
Olli Etuahof40319e2015-03-10 14:33:00 +02002304 if (type->isArray())
2305 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002306 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
2307 // the array.
Olli Etuahof40319e2015-03-10 14:33:00 +02002308 TIntermSequence *args = aggregateArguments->getSequence();
2309 for (size_t i = 0; i < args->size(); i++)
2310 {
2311 const TType &argType = (*args)[i]->getAsTyped()->getType();
2312 // It has already been checked that the argument is not an array.
2313 ASSERT(!argType.isArray());
2314 if (!argType.sameElementType(*type))
2315 {
2316 error(line, "Array constructor argument has an incorrect type", "Error");
2317 recover();
2318 return nullptr;
2319 }
2320 }
2321 }
2322 else if (op == EOpConstructStruct)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002323 {
2324 const TFieldList &fields = type->getStruct()->fields();
Jamie Madillb98c3a82015-07-23 14:26:04 -04002325 TIntermSequence *args = aggregateArguments->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002326
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002327 for (size_t i = 0; i < fields.size(); i++)
2328 {
Nicolas Capensffd73872014-08-21 13:49:16 -04002329 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002330 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002331 error(line, "Structure constructor arguments do not match structure fields",
2332 "Error");
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002333 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002334
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002335 return 0;
2336 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002337 }
2338 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002339
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002340 // Turn the argument list itself into a constructor
Jamie Madillb98c3a82015-07-23 14:26:04 -04002341 TIntermAggregate *constructor = intermediate.setAggregateOperator(aggregateArguments, op, line);
Olli Etuaho21203702014-11-13 16:16:21 +02002342 TIntermTyped *constConstructor = foldConstConstructor(constructor, *type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002343 if (constConstructor)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002344 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002345 return constConstructor;
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002346 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002347
Olli Etuaho21203702014-11-13 16:16:21 +02002348 // Structs should not be precision qualified, the individual members may be.
2349 // Built-in types on the other hand should be precision qualified.
2350 if (op != EOpConstructStruct)
2351 {
2352 constructor->setPrecisionFromChildren();
2353 type->setPrecision(constructor->getPrecision());
2354 }
2355
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002356 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002357}
2358
Arun Patole7e7e68d2015-05-22 12:02:25 +05302359TIntermTyped *TParseContext::foldConstConstructor(TIntermAggregate *aggrNode, const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002360{
Olli Etuahof40319e2015-03-10 14:33:00 +02002361 // TODO: Add support for folding array constructors
2362 bool canBeFolded = areAllChildConst(aggrNode) && !type.isArray();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002363 aggrNode->setType(type);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302364 if (canBeFolded)
2365 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002366 bool returnVal = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302367 TConstantUnion *unionArray = new TConstantUnion[type.getObjectSize()];
2368 if (aggrNode->getSequence()->size() == 1)
2369 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002370 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray,
2371 aggrNode->getOp(), type, true);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002372 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302373 else
2374 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002375 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray,
2376 aggrNode->getOp(), type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002377 }
2378 if (returnVal)
2379 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002380
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002381 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
2382 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002383
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002384 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002385}
2386
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002387//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002388// This function returns the tree representation for the vector field(s) being accessed from contant
2389// vector.
2390// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a
2391// contant node is returned, else an aggregate node is returned (for v.xy). The input to this
2392// function could either
2393// be the symbol node or it could be the intermediate tree representation of accessing fields in a
2394// constant
2395// structure or column of a constant matrix.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002396//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002397TIntermTyped *TParseContext::addConstVectorNode(TVectorFields &fields,
2398 TIntermTyped *node,
2399 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002400{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302401 TIntermTyped *typedNode;
2402 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002403
Jamie Madillb11e2482015-05-04 14:21:22 -04002404 const TConstantUnion *unionArray;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302405 if (tempConstantNode)
2406 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002407 unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002408
Arun Patole7e7e68d2015-05-22 12:02:25 +05302409 if (!unionArray)
2410 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002411 return node;
2412 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302413 }
2414 else
Jamie Madillb98c3a82015-07-23 14:26:04 -04002415 { // The node has to be either a symbol node or an aggregate node or a tempConstant node, else,
2416 // its an error
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002417 error(line, "Cannot offset into the vector", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002418 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002419
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002420 return 0;
2421 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002422
Arun Patole7e7e68d2015-05-22 12:02:25 +05302423 TConstantUnion *constArray = new TConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002424
Arun Patole7e7e68d2015-05-22 12:02:25 +05302425 for (int i = 0; i < fields.num; i++)
2426 {
2427 if (fields.offsets[i] >= node->getType().getNominalSize())
2428 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002429 std::stringstream extraInfoStream;
2430 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
2431 std::string extraInfo = extraInfoStream.str();
2432 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002433 recover();
2434 fields.offsets[i] = 0;
2435 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302436
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002437 constArray[i] = unionArray[fields.offsets[i]];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302438 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002439 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
2440 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002441}
2442
2443//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002444// This function returns the column being accessed from a constant matrix. The values are retrieved
2445// from the symbol table and parse-tree is built for a vector (each column of a matrix is a vector).
2446// The
2447// input to the function could either be a symbol node (m[0] where m is a constant matrix)that
2448// represents
2449// a constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s
2450// is a constant structure)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002451//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002452TIntermTyped *TParseContext::addConstMatrixNode(int index,
2453 TIntermTyped *node,
2454 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002455{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302456 TIntermTyped *typedNode;
2457 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002458
Arun Patole7e7e68d2015-05-22 12:02:25 +05302459 if (index >= node->getType().getCols())
2460 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002461 std::stringstream extraInfoStream;
2462 extraInfoStream << "matrix field selection out of range '" << index << "'";
2463 std::string extraInfo = extraInfoStream.str();
2464 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002465 recover();
2466 index = 0;
2467 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002468
Arun Patole7e7e68d2015-05-22 12:02:25 +05302469 if (tempConstantNode)
2470 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002471 TConstantUnion *unionArray = tempConstantNode->getUnionArrayPointer();
2472 int size = tempConstantNode->getType().getCols();
2473 typedNode = intermediate.addConstantUnion(&unionArray[size * index],
2474 tempConstantNode->getType(), line);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302475 }
2476 else
2477 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002478 error(line, "Cannot offset into the matrix", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002479 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002480
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002481 return 0;
2482 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002483
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002484 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002485}
2486
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002487//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002488// This function returns an element of an array accessed from a constant array. The values are
2489// 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 +05302490// 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 -04002491// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a
2492// constant structure)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002493//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002494TIntermTyped *TParseContext::addConstArrayNode(int index,
2495 TIntermTyped *node,
2496 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002497{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302498 TIntermTyped *typedNode;
2499 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002500 TType arrayElementType = node->getType();
2501 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002502
Arun Patole7e7e68d2015-05-22 12:02:25 +05302503 if (index >= node->getType().getArraySize())
2504 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002505 std::stringstream extraInfoStream;
2506 extraInfoStream << "array field selection out of range '" << index << "'";
2507 std::string extraInfo = extraInfoStream.str();
2508 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002509 recover();
2510 index = 0;
2511 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002512
Arun Patole7e7e68d2015-05-22 12:02:25 +05302513 if (tempConstantNode)
2514 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002515 size_t arrayElementSize = arrayElementType.getObjectSize();
Arun Patole7e7e68d2015-05-22 12:02:25 +05302516 TConstantUnion *unionArray = tempConstantNode->getUnionArrayPointer();
Jamie Madillb98c3a82015-07-23 14:26:04 -04002517 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index],
2518 tempConstantNode->getType(), line);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302519 }
2520 else
2521 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002522 error(line, "Cannot offset into the array", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002523 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002524
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002525 return 0;
2526 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002527
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002528 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002529}
2530
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002531//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002532// This function returns the value of a particular field inside a constant structure from the symbol
2533// table.
2534// If there is an embedded/nested struct, it appropriately calls addConstStructNested or
2535// addConstStructFromAggr function and returns the parse-tree with the values of the embedded/nested
2536// struct.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002537//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002538TIntermTyped *TParseContext::addConstStruct(const TString &identifier,
2539 TIntermTyped *node,
2540 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002541{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302542 const TFieldList &fields = node->getType().getStruct()->fields();
Jamie Madillb98c3a82015-07-23 14:26:04 -04002543 size_t instanceSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002544
Arun Patole7e7e68d2015-05-22 12:02:25 +05302545 for (size_t index = 0; index < fields.size(); ++index)
2546 {
2547 if (fields[index]->name() == identifier)
2548 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002549 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302550 }
2551 else
2552 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002553 instanceSize += fields[index]->type()->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002554 }
2555 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002556
Jamie Madill94bf7f22013-07-08 13:31:15 -04002557 TIntermTyped *typedNode;
2558 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
Arun Patole7e7e68d2015-05-22 12:02:25 +05302559 if (tempConstantNode)
2560 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002561 TConstantUnion *constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002562
Jamie Madillb98c3a82015-07-23 14:26:04 -04002563 // type will be changed in the calling function
2564 typedNode = intermediate.addConstantUnion(constArray + instanceSize,
2565 tempConstantNode->getType(), line);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302566 }
2567 else
2568 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002569 error(line, "Cannot offset into the structure", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002570 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002571
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002572 return 0;
2573 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002574
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002575 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002576}
2577
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002578//
2579// Interface/uniform blocks
2580//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002581TIntermAggregate *TParseContext::addInterfaceBlock(const TPublicType &typeQualifier,
2582 const TSourceLoc &nameLine,
2583 const TString &blockName,
2584 TFieldList *fieldList,
2585 const TString *instanceName,
2586 const TSourceLoc &instanceLine,
2587 TIntermTyped *arrayIndex,
2588 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002589{
2590 if (reservedErrorCheck(nameLine, blockName))
2591 recover();
2592
2593 if (typeQualifier.qualifier != EvqUniform)
2594 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302595 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
2596 "interface blocks must be uniform");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002597 recover();
2598 }
2599
Jamie Madill099c0f32013-06-20 11:55:52 -04002600 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
2601 if (layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002602 {
2603 recover();
2604 }
2605
Jamie Madill099c0f32013-06-20 11:55:52 -04002606 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2607 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002608 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002609 }
2610
Jamie Madill1566ef72013-06-20 11:55:54 -04002611 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2612 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002613 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002614 }
2615
Arun Patole7e7e68d2015-05-22 12:02:25 +05302616 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2617 if (!symbolTable.declare(blockNameSymbol))
2618 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002619 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
2620 recover();
2621 }
2622
Jamie Madill98493dd2013-07-08 14:39:03 -04002623 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302624 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2625 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002626 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302627 TType *fieldType = field->type();
2628 if (IsSampler(fieldType->getBasicType()))
2629 {
2630 error(field->line(), "unsupported type", fieldType->getBasicString(),
2631 "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002632 recover();
2633 }
2634
Jamie Madill98493dd2013-07-08 14:39:03 -04002635 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002636 switch (qualifier)
2637 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002638 case EvqGlobal:
2639 case EvqUniform:
2640 break;
2641 default:
2642 error(field->line(), "invalid qualifier on interface block member",
2643 getQualifierString(qualifier));
2644 recover();
2645 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002646 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002647
2648 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002649 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
2650 if (layoutLocationErrorCheck(field->line(), fieldLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002651 {
2652 recover();
2653 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002654
Jamie Madill98493dd2013-07-08 14:39:03 -04002655 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002656 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002657 error(field->line(), "invalid layout qualifier:",
2658 getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04002659 recover();
2660 }
2661
Jamie Madill98493dd2013-07-08 14:39:03 -04002662 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002663 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002664 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002665 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002666 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002667 {
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002668 warning(field->line(), "extraneous layout qualifier:",
Jamie Madillb98c3a82015-07-23 14:26:04 -04002669 getMatrixPackingString(fieldLayoutQualifier.matrixPacking),
2670 "only has an effect on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04002671 }
2672
Jamie Madill98493dd2013-07-08 14:39:03 -04002673 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002674 }
2675
Jamie Madill98493dd2013-07-08 14:39:03 -04002676 // add array index
2677 int arraySize = 0;
2678 if (arrayIndex != NULL)
2679 {
2680 if (arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize))
2681 recover();
2682 }
2683
Jamie Madillb98c3a82015-07-23 14:26:04 -04002684 TInterfaceBlock *interfaceBlock =
2685 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2686 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
2687 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002688
2689 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04002690 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002691
Jamie Madill98493dd2013-07-08 14:39:03 -04002692 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002693 {
2694 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002695 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2696 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002697 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302698 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002699
2700 // set parent pointer of the field variable
2701 fieldType->setInterfaceBlock(interfaceBlock);
2702
Arun Patole7e7e68d2015-05-22 12:02:25 +05302703 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002704 fieldVariable->setQualifier(typeQualifier.qualifier);
2705
Arun Patole7e7e68d2015-05-22 12:02:25 +05302706 if (!symbolTable.declare(fieldVariable))
2707 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002708 error(field->line(), "redefinition", field->name().c_str(),
2709 "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002710 recover();
2711 }
2712 }
2713 }
2714 else
2715 {
Olli Etuahoe0f623a2015-07-10 11:58:30 +03002716 if (reservedErrorCheck(instanceLine, *instanceName))
2717 recover();
2718
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002719 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302720 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002721 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002722
Arun Patole7e7e68d2015-05-22 12:02:25 +05302723 if (!symbolTable.declare(instanceTypeDef))
2724 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002725 error(instanceLine, "redefinition", instanceName->c_str(),
2726 "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002727 recover();
2728 }
2729
Jamie Madillb98c3a82015-07-23 14:26:04 -04002730 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002731 symbolName = instanceTypeDef->getName();
2732 }
2733
Jamie Madillb98c3a82015-07-23 14:26:04 -04002734 TIntermAggregate *aggregate = intermediate.makeAggregate(
2735 intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line),
2736 nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002737 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04002738
2739 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002740 return aggregate;
2741}
2742
Arun Patole7e7e68d2015-05-22 12:02:25 +05302743bool TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002744{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002745 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002746
2747 // Embedded structure definitions are not supported per GLSL ES spec.
2748 // They aren't allowed in GLSL either, but we need to detect this here
2749 // so we don't rely on the GLSL compiler to catch it.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302750 if (mStructNestingLevel > 1)
2751 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002752 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002753 return true;
2754 }
2755
2756 return false;
2757}
2758
2759void TParseContext::exitStructDeclaration()
2760{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002761 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002762}
2763
Jamie Madillb98c3a82015-07-23 14:26:04 -04002764namespace
2765{
kbr@chromium.org476541f2011-10-27 21:14:51 +00002766const int kWebGLMaxStructNesting = 4;
2767
2768} // namespace
2769
Arun Patole7e7e68d2015-05-22 12:02:25 +05302770bool TParseContext::structNestingErrorCheck(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002771{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302772 if (!IsWebGLBasedSpec(mShaderSpec))
2773 {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002774 return false;
2775 }
2776
Arun Patole7e7e68d2015-05-22 12:02:25 +05302777 if (field.type()->getBasicType() != EbtStruct)
2778 {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002779 return false;
2780 }
2781
2782 // We're already inside a structure definition at this point, so add
2783 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302784 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2785 {
Jamie Madill41a49272014-03-18 16:10:13 -04002786 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002787 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
2788 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04002789 std::string reason = reasonStream.str();
2790 error(line, reason.c_str(), field.name().c_str(), "");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002791 return true;
2792 }
2793
2794 return false;
2795}
2796
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002797//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002798// Parse an array index expression
2799//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002800TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
2801 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302802 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002803{
2804 TIntermTyped *indexedExpression = NULL;
2805
2806 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2807 {
2808 if (baseExpression->getAsSymbolNode())
2809 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302810 error(location, " left of '[' is not of type array, matrix, or vector ",
2811 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002812 }
2813 else
2814 {
2815 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2816 }
2817 recover();
2818 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002819
Jamie Madill21c1e452014-12-29 11:33:41 -05002820 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2821
2822 if (indexExpression->getQualifier() == EvqConst && indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002823 {
Jamie Madill21c1e452014-12-29 11:33:41 -05002824 int index = indexConstantUnion->getIConst(0);
Jamie Madill7164cf42013-07-08 13:30:59 -04002825 if (index < 0)
2826 {
2827 std::stringstream infoStream;
2828 infoStream << index;
2829 std::string info = infoStream.str();
2830 error(location, "negative index", info.c_str());
2831 recover();
2832 index = 0;
2833 }
2834 if (baseExpression->getType().getQualifier() == EvqConst)
2835 {
2836 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002837 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002838 // constant folding for arrays
2839 indexedExpression = addConstArrayNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002840 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002841 else if (baseExpression->isVector())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002842 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002843 // constant folding for vectors
2844 TVectorFields fields;
2845 fields.num = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002846 fields.offsets[0] =
2847 index; // need to do it this way because v.xy sends fields integer array
Jamie Madill7164cf42013-07-08 13:30:59 -04002848 indexedExpression = addConstVectorNode(fields, baseExpression, location);
2849 }
2850 else if (baseExpression->isMatrix())
2851 {
2852 // constant folding for matrices
2853 indexedExpression = addConstMatrixNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002854 }
2855 }
2856 else
2857 {
Jamie Madillb11e2482015-05-04 14:21:22 -04002858 int safeIndex = -1;
2859
Jamie Madill7164cf42013-07-08 13:30:59 -04002860 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002861 {
Jamie Madill18464b52013-07-08 14:01:55 -04002862 if (index >= baseExpression->getType().getArraySize())
Jamie Madill7164cf42013-07-08 13:30:59 -04002863 {
2864 std::stringstream extraInfoStream;
2865 extraInfoStream << "array index out of range '" << index << "'";
2866 std::string extraInfo = extraInfoStream.str();
2867 error(location, "", "[", extraInfo.c_str());
2868 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002869 safeIndex = baseExpression->getType().getArraySize() - 1;
Jamie Madill7164cf42013-07-08 13:30:59 -04002870 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302871 else if (baseExpression->getQualifier() == EvqFragData && index > 0 &&
2872 !isExtensionEnabled("GL_EXT_draw_buffers"))
Jamie Madill5d287f52013-07-12 15:38:19 -04002873 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002874 error(location, "", "[",
2875 "array indexes for gl_FragData must be zero when GL_EXT_draw_buffers is "
2876 "disabled");
Jamie Madill5d287f52013-07-12 15:38:19 -04002877 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002878 safeIndex = 0;
Jamie Madill5d287f52013-07-12 15:38:19 -04002879 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002880 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302881 else if ((baseExpression->isVector() || baseExpression->isMatrix()) &&
Jamie Madillb98c3a82015-07-23 14:26:04 -04002882 baseExpression->getType().getNominalSize() <= index)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002883 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002884 std::stringstream extraInfoStream;
2885 extraInfoStream << "field selection out of range '" << index << "'";
2886 std::string extraInfo = extraInfoStream.str();
2887 error(location, "", "[", extraInfo.c_str());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002888 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002889 safeIndex = baseExpression->getType().getNominalSize() - 1;
Jamie Madill46131a32013-06-20 11:55:50 -04002890 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002891
Jamie Madillb11e2482015-05-04 14:21:22 -04002892 // Don't modify the data of the previous constant union, because it can point
2893 // to builtins, like gl_MaxDrawBuffers. Instead use a new sanitized object.
2894 if (safeIndex != -1)
2895 {
2896 TConstantUnion *safeConstantUnion = new TConstantUnion();
2897 safeConstantUnion->setIConst(safeIndex);
2898 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
2899 }
2900
Jamie Madillb98c3a82015-07-23 14:26:04 -04002901 indexedExpression =
2902 intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002903 }
2904 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002905 else
2906 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002907 if (baseExpression->isInterfaceBlock())
Jamie Madill7164cf42013-07-08 13:30:59 -04002908 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002909 error(
2910 location, "", "[",
2911 "array indexes for interface blocks arrays must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002912 recover();
2913 }
Jamie Madill19571812013-08-12 15:26:34 -07002914 else if (baseExpression->getQualifier() == EvqFragmentOut)
Jamie Madill7164cf42013-07-08 13:30:59 -04002915 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002916 error(location, "", "[",
2917 "array indexes for fragment outputs must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002918 recover();
2919 }
2920
Jamie Madillb98c3a82015-07-23 14:26:04 -04002921 indexedExpression =
2922 intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
Jamie Madill7164cf42013-07-08 13:30:59 -04002923 }
2924
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002925 if (indexedExpression == 0)
2926 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002927 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002928 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002929 indexedExpression =
2930 intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002931 }
2932 else if (baseExpression->isArray())
2933 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002934 const TType &baseType = baseExpression->getType();
2935 if (baseType.getStruct())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002936 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002937 TType copyOfType(baseType.getStruct());
2938 indexedExpression->setType(copyOfType);
2939 }
2940 else if (baseType.isInterfaceBlock())
2941 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002942 TType copyOfType(baseType.getInterfaceBlock(), baseType.getQualifier(),
2943 baseType.getLayoutQualifier(), 0);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002944 indexedExpression->setType(copyOfType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002945 }
2946 else
2947 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002948 indexedExpression->setType(
2949 TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary,
2950 static_cast<unsigned char>(baseExpression->getNominalSize()),
2951 static_cast<unsigned char>(baseExpression->getSecondarySize())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002952 }
2953
2954 if (baseExpression->getType().getQualifier() == EvqConst)
2955 {
2956 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2957 }
2958 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002959 else if (baseExpression->isMatrix())
2960 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002961 TQualifier qualifier =
2962 baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2963 indexedExpression->setType(TType(baseExpression->getBasicType(),
2964 baseExpression->getPrecision(), qualifier,
2965 static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002966 }
2967 else if (baseExpression->isVector())
2968 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002969 TQualifier qualifier =
2970 baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2971 indexedExpression->setType(
2972 TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002973 }
2974 else
2975 {
2976 indexedExpression->setType(baseExpression->getType());
2977 }
2978
2979 return indexedExpression;
2980}
2981
Jamie Madillb98c3a82015-07-23 14:26:04 -04002982TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
2983 const TSourceLoc &dotLocation,
2984 const TString &fieldString,
2985 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002986{
2987 TIntermTyped *indexedExpression = NULL;
2988
2989 if (baseExpression->isArray())
2990 {
2991 error(fieldLocation, "cannot apply dot operator to an array", ".");
2992 recover();
2993 }
2994
2995 if (baseExpression->isVector())
2996 {
2997 TVectorFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002998 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields,
2999 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003000 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003001 fields.num = 1;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003002 fields.offsets[0] = 0;
3003 recover();
3004 }
3005
3006 if (baseExpression->getType().getQualifier() == EvqConst)
3007 {
3008 // constant folding for vector fields
3009 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
3010 if (indexedExpression == 0)
3011 {
3012 recover();
3013 indexedExpression = baseExpression;
3014 }
3015 else
3016 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003017 indexedExpression->setType(TType(baseExpression->getBasicType(),
3018 baseExpression->getPrecision(), EvqConst,
3019 (unsigned char)(fieldString).size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003020 }
3021 }
3022 else
3023 {
3024 TString vectorString = fieldString;
Arun Patole7e7e68d2015-05-22 12:02:25 +05303025 TIntermTyped *index = intermediate.addSwizzle(fields, fieldLocation);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003026 indexedExpression =
3027 intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
3028 indexedExpression->setType(TType(baseExpression->getBasicType(),
3029 baseExpression->getPrecision(), EvqTemporary,
3030 (unsigned char)vectorString.size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003031 }
3032 }
3033 else if (baseExpression->isMatrix())
3034 {
3035 TMatrixFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003036 if (!parseMatrixFields(fieldString, baseExpression->getCols(), baseExpression->getRows(),
3037 fields, fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003038 {
3039 fields.wholeRow = false;
3040 fields.wholeCol = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003041 fields.row = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003042 fields.col = 0;
3043 recover();
3044 }
3045
3046 if (fields.wholeRow || fields.wholeCol)
3047 {
3048 error(dotLocation, " non-scalar fields not implemented yet", ".");
3049 recover();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003050 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003051 unionArray->setIConst(0);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003052 TIntermTyped *index = intermediate.addConstantUnion(
3053 unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
3054 indexedExpression =
3055 intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
3056 indexedExpression->setType(
3057 TType(baseExpression->getBasicType(), baseExpression->getPrecision(), EvqTemporary,
3058 static_cast<unsigned char>(baseExpression->getCols()),
3059 static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003060 }
3061 else
3062 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003063 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003064 unionArray->setIConst(fields.col * baseExpression->getRows() + fields.row);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003065 TIntermTyped *index = intermediate.addConstantUnion(
3066 unionArray, TType(EbtInt, EbpUndefined, EvqConst), fieldLocation);
3067 indexedExpression =
3068 intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
3069 indexedExpression->setType(
3070 TType(baseExpression->getBasicType(), baseExpression->getPrecision()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003071 }
3072 }
3073 else if (baseExpression->getBasicType() == EbtStruct)
3074 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003075 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05303076 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003077 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003078 {
3079 error(dotLocation, "structure has no fields", "Internal Error");
3080 recover();
3081 indexedExpression = baseExpression;
3082 }
3083 else
3084 {
3085 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003086 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003087 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003088 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003089 {
3090 fieldFound = true;
3091 break;
3092 }
3093 }
3094 if (fieldFound)
3095 {
3096 if (baseExpression->getType().getQualifier() == EvqConst)
3097 {
3098 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
3099 if (indexedExpression == 0)
3100 {
3101 recover();
3102 indexedExpression = baseExpression;
3103 }
3104 else
3105 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003106 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003107 // change the qualifier of the return type, not of the structure field
3108 // as the structure definition is shared between various structures.
3109 indexedExpression->getTypePointer()->setQualifier(EvqConst);
3110 }
3111 }
3112 else
3113 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003114 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003115 unionArray->setIConst(i);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003116 TIntermTyped *index = intermediate.addConstantUnion(
3117 unionArray, *fields[i]->type(), fieldLocation);
3118 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression,
3119 index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04003120 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003121 }
3122 }
3123 else
3124 {
3125 error(dotLocation, " no such field in structure", fieldString.c_str());
3126 recover();
3127 indexedExpression = baseExpression;
3128 }
3129 }
3130 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003131 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003132 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003133 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05303134 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003135 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003136 {
3137 error(dotLocation, "interface block has no fields", "Internal Error");
3138 recover();
3139 indexedExpression = baseExpression;
3140 }
3141 else
3142 {
3143 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003144 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003145 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003146 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003147 {
3148 fieldFound = true;
3149 break;
3150 }
3151 }
3152 if (fieldFound)
3153 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003154 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003155 unionArray->setIConst(i);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003156 TIntermTyped *index =
3157 intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
3158 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock,
3159 baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04003160 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003161 }
3162 else
3163 {
3164 error(dotLocation, " no such field in interface block", fieldString.c_str());
3165 recover();
3166 indexedExpression = baseExpression;
3167 }
3168 }
3169 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003170 else
3171 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003172 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003173 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003174 error(dotLocation,
3175 " field selection requires structure, vector, or matrix on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303176 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003177 }
3178 else
3179 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303180 error(dotLocation,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003181 " field selection requires structure, vector, matrix, or interface block on left "
3182 "hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303183 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003184 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003185 recover();
3186 indexedExpression = baseExpression;
3187 }
3188
3189 return indexedExpression;
3190}
3191
Jamie Madillb98c3a82015-07-23 14:26:04 -04003192TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3193 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003194{
Jamie Madilla5efff92013-06-06 11:56:47 -04003195 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003196
Jamie Madillb98c3a82015-07-23 14:26:04 -04003197 qualifier.location = -1;
Jamie Madilla5efff92013-06-06 11:56:47 -04003198 qualifier.matrixPacking = EmpUnspecified;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003199 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003200
3201 if (qualifierType == "shared")
3202 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003203 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003204 }
3205 else if (qualifierType == "packed")
3206 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003207 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003208 }
3209 else if (qualifierType == "std140")
3210 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003211 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003212 }
3213 else if (qualifierType == "row_major")
3214 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003215 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003216 }
3217 else if (qualifierType == "column_major")
3218 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003219 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003220 }
3221 else if (qualifierType == "location")
3222 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003223 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
3224 "location requires an argument");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003225 recover();
3226 }
3227 else
3228 {
3229 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
3230 recover();
3231 }
3232
Jamie Madilla5efff92013-06-06 11:56:47 -04003233 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003234}
3235
Jamie Madillb98c3a82015-07-23 14:26:04 -04003236TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3237 const TSourceLoc &qualifierTypeLine,
3238 const TString &intValueString,
3239 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303240 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003241{
Jamie Madilla5efff92013-06-06 11:56:47 -04003242 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003243
Jamie Madillb98c3a82015-07-23 14:26:04 -04003244 qualifier.location = -1;
Jamie Madilla5efff92013-06-06 11:56:47 -04003245 qualifier.matrixPacking = EmpUnspecified;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003246 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003247
3248 if (qualifierType != "location")
3249 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303250 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
3251 "only location may have arguments");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003252 recover();
3253 }
3254 else
3255 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003256 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003257 if (intValue < 0)
3258 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003259 error(intValueLine, "out of range:", intValueString.c_str(),
3260 "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003261 recover();
3262 }
3263 else
3264 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003265 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003266 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003267 }
3268
Jamie Madilla5efff92013-06-06 11:56:47 -04003269 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003270}
3271
Jamie Madillb98c3a82015-07-23 14:26:04 -04003272TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
3273 TLayoutQualifier rightQualifier)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003274{
Jamie Madilla5efff92013-06-06 11:56:47 -04003275 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003276
Jamie Madilla5efff92013-06-06 11:56:47 -04003277 if (rightQualifier.location != -1)
3278 {
3279 joinedQualifier.location = rightQualifier.location;
3280 }
3281 if (rightQualifier.matrixPacking != EmpUnspecified)
3282 {
3283 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
3284 }
3285 if (rightQualifier.blockStorage != EbsUnspecified)
3286 {
3287 joinedQualifier.blockStorage = rightQualifier.blockStorage;
3288 }
3289
3290 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003291}
3292
Arun Patole7e7e68d2015-05-22 12:02:25 +05303293TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc,
3294 TQualifier interpolationQualifier,
3295 const TSourceLoc &storageLoc,
3296 TQualifier storageQualifier)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003297{
3298 TQualifier mergedQualifier = EvqSmoothIn;
3299
Arun Patole7e7e68d2015-05-22 12:02:25 +05303300 if (storageQualifier == EvqFragmentIn)
3301 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003302 if (interpolationQualifier == EvqSmooth)
3303 mergedQualifier = EvqSmoothIn;
3304 else if (interpolationQualifier == EvqFlat)
3305 mergedQualifier = EvqFlatIn;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003306 else
3307 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003308 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303309 else if (storageQualifier == EvqCentroidIn)
3310 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003311 if (interpolationQualifier == EvqSmooth)
3312 mergedQualifier = EvqCentroidIn;
3313 else if (interpolationQualifier == EvqFlat)
3314 mergedQualifier = EvqFlatIn;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003315 else
3316 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003317 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303318 else if (storageQualifier == EvqVertexOut)
3319 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003320 if (interpolationQualifier == EvqSmooth)
3321 mergedQualifier = EvqSmoothOut;
3322 else if (interpolationQualifier == EvqFlat)
3323 mergedQualifier = EvqFlatOut;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003324 else
3325 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003326 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303327 else if (storageQualifier == EvqCentroidOut)
3328 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003329 if (interpolationQualifier == EvqSmooth)
3330 mergedQualifier = EvqCentroidOut;
3331 else if (interpolationQualifier == EvqFlat)
3332 mergedQualifier = EvqFlatOut;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003333 else
3334 UNREACHABLE();
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003335 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303336 else
3337 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003338 error(interpolationLoc,
3339 "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303340 getInterpolationString(interpolationQualifier));
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003341 recover();
3342
3343 mergedQualifier = storageQualifier;
3344 }
3345
3346 TPublicType type;
3347 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
3348 return type;
3349}
3350
Jamie Madillb98c3a82015-07-23 14:26:04 -04003351TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3352 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003353{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03003354 if (voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier.type))
3355 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003356 recover();
3357 }
3358
Arun Patole7e7e68d2015-05-22 12:02:25 +05303359 for (unsigned int i = 0; i < fieldList->size(); ++i)
3360 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003361 //
3362 // Careful not to replace already known aspects of type, like array-ness
3363 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303364 TType *type = (*fieldList)[i]->type();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003365 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003366 type->setPrimarySize(typeSpecifier.primarySize);
3367 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003368 type->setPrecision(typeSpecifier.precision);
3369 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003370 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003371
3372 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303373 if (type->isArray())
3374 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003375 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
3376 recover();
3377 }
3378 if (typeSpecifier.array)
3379 type->setArraySize(typeSpecifier.arraySize);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303380 if (typeSpecifier.userDef)
3381 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003382 type->setStruct(typeSpecifier.userDef->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003383 }
3384
Arun Patole7e7e68d2015-05-22 12:02:25 +05303385 if (structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i]))
3386 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003387 recover();
3388 }
3389 }
3390
Jamie Madill98493dd2013-07-08 14:39:03 -04003391 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003392}
3393
Jamie Madillb98c3a82015-07-23 14:26:04 -04003394TPublicType TParseContext::addStructure(const TSourceLoc &structLine,
3395 const TSourceLoc &nameLine,
3396 const TString *structName,
3397 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003398{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303399 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003400 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003401
Jamie Madill9b820842015-02-12 10:40:10 -05003402 // Store a bool in the struct if we're at global scope, to allow us to
3403 // skip the local struct scoping workaround in HLSL.
Jamie Madillb960cc42015-02-12 15:33:20 +00003404 structure->setUniqueId(TSymbolTable::nextUniqueId());
Jamie Madill9b820842015-02-12 10:40:10 -05003405 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003406
Jamie Madill98493dd2013-07-08 14:39:03 -04003407 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003408 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003409 if (reservedErrorCheck(nameLine, *structName))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003410 {
3411 recover();
3412 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303413 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
3414 if (!symbolTable.declare(userTypeDef))
3415 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003416 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003417 recover();
3418 }
3419 }
3420
3421 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04003422 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003423 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003424 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04003425 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003426 switch (qualifier)
3427 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003428 case EvqGlobal:
3429 case EvqTemporary:
3430 break;
3431 default:
3432 error(field.line(), "invalid qualifier on struct member",
3433 getQualifierString(qualifier));
3434 recover();
3435 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003436 }
3437 }
3438
3439 TPublicType publicType;
3440 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003441 publicType.userDef = structureType;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003442 exitStructDeclaration();
3443
3444 return publicType;
3445}
3446
Jamie Madillb98c3a82015-07-23 14:26:04 -04003447TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
3448 TIntermAggregate *statementList,
3449 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02003450{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003451 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003452 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02003453 init->isVector())
3454 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003455 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
3456 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003457 recover();
3458 return nullptr;
3459 }
3460
Olli Etuahoac5274d2015-02-20 10:19:08 +02003461 if (statementList)
3462 {
3463 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
3464 {
3465 recover();
3466 return nullptr;
3467 }
3468 }
3469
Olli Etuahoa3a36662015-02-17 13:46:51 +02003470 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3471 if (node == nullptr)
3472 {
3473 error(loc, "erroneous switch statement", "switch");
3474 recover();
3475 return nullptr;
3476 }
3477 return node;
3478}
3479
3480TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3481{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003482 if (mSwitchNestingLevel == 0)
3483 {
3484 error(loc, "case labels need to be inside switch statements", "case");
3485 recover();
3486 return nullptr;
3487 }
3488 if (condition == nullptr)
3489 {
3490 error(loc, "case label must have a condition", "case");
3491 recover();
3492 return nullptr;
3493 }
3494 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04003495 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02003496 {
3497 error(condition->getLine(), "case label must be a scalar integer", "case");
3498 recover();
3499 }
3500 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
3501 if (conditionConst == nullptr)
3502 {
3503 error(condition->getLine(), "case label must be constant", "case");
3504 recover();
3505 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003506 TIntermCase *node = intermediate.addCase(condition, loc);
3507 if (node == nullptr)
3508 {
3509 error(loc, "erroneous case statement", "case");
3510 recover();
3511 return nullptr;
3512 }
3513 return node;
3514}
3515
3516TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3517{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003518 if (mSwitchNestingLevel == 0)
3519 {
3520 error(loc, "default labels need to be inside switch statements", "default");
3521 recover();
3522 return nullptr;
3523 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003524 TIntermCase *node = intermediate.addCase(nullptr, loc);
3525 if (node == nullptr)
3526 {
3527 error(loc, "erroneous default statement", "default");
3528 recover();
3529 return nullptr;
3530 }
3531 return node;
3532}
3533
Jamie Madillb98c3a82015-07-23 14:26:04 -04003534TIntermTyped *TParseContext::createUnaryMath(TOperator op,
3535 TIntermTyped *child,
3536 const TSourceLoc &loc,
3537 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003538{
3539 if (child == nullptr)
3540 {
3541 return nullptr;
3542 }
3543
3544 switch (op)
3545 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003546 case EOpLogicalNot:
3547 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
3548 child->isVector())
3549 {
3550 return nullptr;
3551 }
3552 break;
3553 case EOpBitwiseNot:
3554 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3555 child->isMatrix() || child->isArray())
3556 {
3557 return nullptr;
3558 }
3559 break;
3560 case EOpPostIncrement:
3561 case EOpPreIncrement:
3562 case EOpPostDecrement:
3563 case EOpPreDecrement:
3564 case EOpNegative:
3565 case EOpPositive:
3566 if (child->getBasicType() == EbtStruct || child->getBasicType() == EbtBool ||
3567 child->isArray())
3568 {
3569 return nullptr;
3570 }
3571 // Operators for built-ins are already type checked against their prototype.
3572 default:
3573 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003574 }
3575
Olli Etuahof6c694b2015-03-26 14:50:53 +02003576 return intermediate.addUnaryMath(op, child, loc, funcReturnType);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003577}
3578
Olli Etuaho09b22472015-02-11 11:47:26 +02003579TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3580{
Olli Etuahof6c694b2015-03-26 14:50:53 +02003581 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003582 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003583 {
3584 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
3585 recover();
3586 return child;
3587 }
3588 return node;
3589}
3590
Jamie Madillb98c3a82015-07-23 14:26:04 -04003591TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
3592 TIntermTyped *child,
3593 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003594{
3595 if (lValueErrorCheck(loc, GetOperatorString(op), child))
3596 recover();
3597 return addUnaryMath(op, child, loc);
3598}
3599
Jamie Madillb98c3a82015-07-23 14:26:04 -04003600bool TParseContext::binaryOpCommonCheck(TOperator op,
3601 TIntermTyped *left,
3602 TIntermTyped *right,
3603 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003604{
3605 if (left->isArray() || right->isArray())
3606 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003607 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003608 {
3609 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3610 return false;
3611 }
3612
3613 if (left->isArray() != right->isArray())
3614 {
3615 error(loc, "array / non-array mismatch", GetOperatorString(op));
3616 return false;
3617 }
3618
3619 switch (op)
3620 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003621 case EOpEqual:
3622 case EOpNotEqual:
3623 case EOpAssign:
3624 case EOpInitialize:
3625 break;
3626 default:
3627 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3628 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02003629 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003630 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003631 if (left->getArraySize() != right->getArraySize())
3632 {
3633 error(loc, "array size mismatch", GetOperatorString(op));
3634 return false;
3635 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003636 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003637
3638 // Check ops which require integer / ivec parameters
3639 bool isBitShift = false;
3640 switch (op)
3641 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003642 case EOpBitShiftLeft:
3643 case EOpBitShiftRight:
3644 case EOpBitShiftLeftAssign:
3645 case EOpBitShiftRightAssign:
3646 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3647 // check that the basic type is an integer type.
3648 isBitShift = true;
3649 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3650 {
3651 return false;
3652 }
3653 break;
3654 case EOpBitwiseAnd:
3655 case EOpBitwiseXor:
3656 case EOpBitwiseOr:
3657 case EOpBitwiseAndAssign:
3658 case EOpBitwiseXorAssign:
3659 case EOpBitwiseOrAssign:
3660 // It is enough to check the type of only one operand, since later it
3661 // is checked that the operand types match.
3662 if (!IsInteger(left->getBasicType()))
3663 {
3664 return false;
3665 }
3666 break;
3667 default:
3668 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003669 }
3670
3671 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3672 // So the basic type should usually match.
3673 if (!isBitShift && left->getBasicType() != right->getBasicType())
3674 {
3675 return false;
3676 }
3677
Olli Etuaho9dd217b2015-03-20 14:24:31 +02003678 // Check that type sizes match exactly on ops that require that.
Olli Etuahoff699002015-03-23 14:38:42 +02003679 // Also check restrictions for structs that contain arrays or samplers.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003680 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003681 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003682 case EOpAssign:
3683 case EOpInitialize:
3684 case EOpEqual:
3685 case EOpNotEqual:
3686 // ESSL 1.00 sections 5.7, 5.8, 5.9
3687 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
3688 {
3689 error(loc, "undefined operation for structs containing arrays",
3690 GetOperatorString(op));
3691 return false;
3692 }
3693 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3694 // we interpret the spec so that this extends to structs containing samplers,
3695 // similarly to ESSL 1.00 spec.
3696 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
3697 left->getType().isStructureContainingSamplers())
3698 {
3699 error(loc, "undefined operation for structs containing samplers",
3700 GetOperatorString(op));
3701 return false;
3702 }
3703 case EOpLessThan:
3704 case EOpGreaterThan:
3705 case EOpLessThanEqual:
3706 case EOpGreaterThanEqual:
3707 if ((left->getNominalSize() != right->getNominalSize()) ||
3708 (left->getSecondarySize() != right->getSecondarySize()))
3709 {
3710 return false;
3711 }
3712 default:
3713 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003714 }
3715
Olli Etuahod6b14282015-03-17 14:31:35 +02003716 return true;
3717}
3718
Jamie Madillb98c3a82015-07-23 14:26:04 -04003719TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
3720 TIntermTyped *left,
3721 TIntermTyped *right,
3722 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02003723{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003724 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003725 return nullptr;
3726
Olli Etuahofc1806e2015-03-17 13:03:11 +02003727 switch (op)
3728 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003729 case EOpEqual:
3730 case EOpNotEqual:
3731 break;
3732 case EOpLessThan:
3733 case EOpGreaterThan:
3734 case EOpLessThanEqual:
3735 case EOpGreaterThanEqual:
3736 ASSERT(!left->isArray() && !right->isArray());
3737 if (left->isMatrix() || left->isVector() || left->getBasicType() == EbtStruct)
3738 {
3739 return nullptr;
3740 }
3741 break;
3742 case EOpLogicalOr:
3743 case EOpLogicalXor:
3744 case EOpLogicalAnd:
3745 ASSERT(!left->isArray() && !right->isArray());
3746 if (left->getBasicType() != EbtBool || left->isMatrix() || left->isVector())
3747 {
3748 return nullptr;
3749 }
3750 break;
3751 case EOpAdd:
3752 case EOpSub:
3753 case EOpDiv:
3754 case EOpMul:
3755 ASSERT(!left->isArray() && !right->isArray());
3756 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool)
3757 {
3758 return nullptr;
3759 }
3760 break;
3761 case EOpIMod:
3762 ASSERT(!left->isArray() && !right->isArray());
3763 // Note that this is only for the % operator, not for mod()
3764 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool ||
3765 left->getBasicType() == EbtFloat)
3766 {
3767 return nullptr;
3768 }
3769 break;
3770 // Note that for bitwise ops, type checking is done in promote() to
3771 // share code between ops and compound assignment
3772 default:
3773 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003774 }
3775
Olli Etuahofc1806e2015-03-17 13:03:11 +02003776 return intermediate.addBinaryMath(op, left, right, loc);
3777}
3778
Jamie Madillb98c3a82015-07-23 14:26:04 -04003779TIntermTyped *TParseContext::addBinaryMath(TOperator op,
3780 TIntermTyped *left,
3781 TIntermTyped *right,
3782 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003783{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003784 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003785 if (node == 0)
3786 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003787 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3788 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003789 recover();
3790 return left;
3791 }
3792 return node;
3793}
3794
Jamie Madillb98c3a82015-07-23 14:26:04 -04003795TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
3796 TIntermTyped *left,
3797 TIntermTyped *right,
3798 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003799{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003800 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003801 if (node == 0)
3802 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003803 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3804 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003805 recover();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003806 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02003807 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003808 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
3809 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003810 }
3811 return node;
3812}
3813
Jamie Madillb98c3a82015-07-23 14:26:04 -04003814TIntermTyped *TParseContext::createAssign(TOperator op,
3815 TIntermTyped *left,
3816 TIntermTyped *right,
3817 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003818{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003819 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003820 {
3821 return intermediate.addAssign(op, left, right, loc);
3822 }
3823 return nullptr;
3824}
3825
Jamie Madillb98c3a82015-07-23 14:26:04 -04003826TIntermTyped *TParseContext::addAssign(TOperator op,
3827 TIntermTyped *left,
3828 TIntermTyped *right,
3829 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003830{
3831 TIntermTyped *node = createAssign(op, left, right, loc);
3832 if (node == nullptr)
3833 {
3834 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
3835 recover();
3836 return left;
3837 }
3838 return node;
3839}
3840
Olli Etuaho49300862015-02-20 14:54:49 +02003841TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3842{
3843 switch (op)
3844 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003845 case EOpContinue:
3846 if (mLoopNestingLevel <= 0)
3847 {
3848 error(loc, "continue statement only allowed in loops", "");
3849 recover();
3850 }
3851 break;
3852 case EOpBreak:
3853 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
3854 {
3855 error(loc, "break statement only allowed in loops and switch statements", "");
3856 recover();
3857 }
3858 break;
3859 case EOpReturn:
3860 if (mCurrentFunctionType->getBasicType() != EbtVoid)
3861 {
3862 error(loc, "non-void function must return a value", "return");
3863 recover();
3864 }
3865 break;
3866 default:
3867 // No checks for discard
3868 break;
Olli Etuaho49300862015-02-20 14:54:49 +02003869 }
3870 return intermediate.addBranch(op, loc);
3871}
3872
Jamie Madillb98c3a82015-07-23 14:26:04 -04003873TIntermBranch *TParseContext::addBranch(TOperator op,
3874 TIntermTyped *returnValue,
3875 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02003876{
3877 ASSERT(op == EOpReturn);
3878 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003879 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02003880 {
3881 error(loc, "void function cannot return a value", "return");
3882 recover();
3883 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003884 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02003885 {
3886 error(loc, "function return is not matching type:", "return");
3887 recover();
3888 }
3889 return intermediate.addBranch(op, returnValue, loc);
3890}
3891
Jamie Madillb98c3a82015-07-23 14:26:04 -04003892TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
3893 TIntermNode *paramNode,
3894 TIntermNode *thisNode,
3895 const TSourceLoc &loc,
3896 bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003897{
Jamie Madillb98c3a82015-07-23 14:26:04 -04003898 *fatalError = false;
3899 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003900 TIntermTyped *callNode = nullptr;
3901
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003902 if (thisNode != nullptr)
3903 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003904 TConstantUnion *unionArray = new TConstantUnion[1];
Jamie Madillb98c3a82015-07-23 14:26:04 -04003905 int arraySize = 0;
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003906 TIntermTyped *typedThis = thisNode->getAsTyped();
3907 if (fnCall->getName() != "length")
3908 {
3909 error(loc, "invalid method", fnCall->getName().c_str());
3910 recover();
3911 }
3912 else if (paramNode != nullptr)
3913 {
3914 error(loc, "method takes no parameters", "length");
3915 recover();
3916 }
3917 else if (typedThis == nullptr || !typedThis->isArray())
3918 {
3919 error(loc, "length can only be called on arrays", "length");
3920 recover();
3921 }
3922 else
3923 {
Olli Etuaho96e67382015-04-23 14:27:02 +03003924 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03003925 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003926 {
Olli Etuaho39282e12015-04-23 15:41:48 +03003927 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003928 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03003929 // (func()).length()
3930 // (int[3](0, 1, 2)).length()
Jamie Madillb98c3a82015-07-23 14:26:04 -04003931 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
3932 // expression.
3933 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
3934 // spec section 5.9 which allows "An array, vector or matrix expression with the
3935 // length method applied".
3936 error(loc, "length can only be called on array names, not on array expressions",
3937 "length");
Olli Etuaho39282e12015-04-23 15:41:48 +03003938 recover();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003939 }
3940 }
Olli Etuaho96e67382015-04-23 14:27:02 +03003941 unionArray->setIConst(arraySize);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003942 callNode =
3943 intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003944 }
3945 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003946 {
3947 //
3948 // Then this should be a constructor.
3949 // Don't go through the symbol table for constructors.
3950 // Their parameters will be verified algorithmically.
3951 //
3952 TType type(EbtVoid, EbpUndefined); // use this to get the type back
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003953 if (!constructorErrorCheck(loc, paramNode, *fnCall, op, &type))
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003954 {
3955 //
3956 // It's a constructor, of type 'type'.
3957 //
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003958 callNode = addConstructor(paramNode, &type, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003959 }
Olli Etuaho72ba85b2015-03-04 14:23:26 +02003960
3961 if (callNode == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003962 {
3963 recover();
3964 callNode = intermediate.setAggregateOperator(nullptr, op, loc);
3965 }
3966 callNode->setType(type);
3967 }
3968 else
3969 {
3970 //
3971 // Not a constructor. Find it in the symbol table.
3972 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303973 const TFunction *fnCandidate;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003974 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003975 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003976 if (fnCandidate)
3977 {
3978 //
3979 // A declared function.
3980 //
3981 if (builtIn && !fnCandidate->getExtension().empty() &&
3982 extensionErrorCheck(loc, fnCandidate->getExtension()))
3983 {
3984 recover();
3985 }
3986 op = fnCandidate->getBuiltInOp();
3987 if (builtIn && op != EOpNull)
3988 {
3989 //
3990 // A function call mapped to a built-in operation.
3991 //
3992 if (fnCandidate->getParamCount() == 1)
3993 {
3994 //
3995 // Treat it like a built-in unary operator.
3996 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003997 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc,
3998 &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003999 if (callNode == nullptr)
4000 {
4001 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004002 extraInfoStream
4003 << "built in unary operator function. Type: "
4004 << static_cast<TIntermTyped *>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004005 std::string extraInfo = extraInfoStream.str();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004006 error(paramNode->getLine(), " wrong operand type", "Internal Error",
4007 extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004008 *fatalError = true;
4009 return nullptr;
4010 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004011 }
4012 else
4013 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004014 TIntermAggregate *aggregate =
4015 intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004016 aggregate->setType(fnCandidate->getReturnType());
4017 aggregate->setPrecisionFromChildren();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004018
4019 // Some built-in functions have out parameters too.
4020 functionCallLValueErrorCheck(fnCandidate, aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05304021
4022 // See if we can constant fold a built-in.
Olli Etuahob43846e2015-06-02 18:18:57 +03004023 TIntermTyped *foldedNode = intermediate.foldAggregateBuiltIn(aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05304024 if (foldedNode)
4025 {
Arun Patole274f0702015-05-05 13:33:30 +05304026 callNode = foldedNode;
4027 }
Olli Etuahob43846e2015-06-02 18:18:57 +03004028 else
4029 {
4030 callNode = aggregate;
4031 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004032 }
4033 }
4034 else
4035 {
4036 // This is a real function call
4037
Jamie Madillb98c3a82015-07-23 14:26:04 -04004038 TIntermAggregate *aggregate =
4039 intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004040 aggregate->setType(fnCandidate->getReturnType());
4041
Jamie Madillb98c3a82015-07-23 14:26:04 -04004042 // this is how we know whether the given function is a builtIn function or a user
4043 // defined function
4044 // if builtIn == false, it's a userDefined -> could be an overloaded
4045 // builtIn function also
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004046 // if builtIn == true, it's definitely a builtIn function with EOpNull
4047 if (!builtIn)
4048 aggregate->setUserDefined();
4049 aggregate->setName(fnCandidate->getMangledName());
Corentin Wallez71d147f2015-02-11 11:15:24 -08004050 aggregate->setFunctionId(fnCandidate->getUniqueId());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004051
4052 // This needs to happen after the name is set
4053 if (builtIn)
4054 aggregate->setBuiltInFunctionPrecision();
4055
4056 callNode = aggregate;
4057
4058 functionCallLValueErrorCheck(fnCandidate, aggregate);
4059 }
4060 }
4061 else
4062 {
4063 // error message was put out by findFunction()
4064 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004065 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004066 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004067 callNode = intermediate.addConstantUnion(unionArray,
4068 TType(EbtFloat, EbpUndefined, EvqConst), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004069 recover();
4070 }
4071 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004072 return callNode;
4073}
4074
Jamie Madillb98c3a82015-07-23 14:26:04 -04004075TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
4076 TIntermTyped *trueBlock,
4077 TIntermTyped *falseBlock,
Olli Etuaho52901742015-04-15 13:42:45 +03004078 const TSourceLoc &loc)
4079{
4080 if (boolErrorCheck(loc, cond))
4081 recover();
4082
4083 if (trueBlock->getType() != falseBlock->getType())
4084 {
4085 binaryOpError(loc, ":", trueBlock->getCompleteString(), falseBlock->getCompleteString());
4086 recover();
4087 return falseBlock;
4088 }
Olli Etuahoa2d53032015-04-15 14:14:44 +03004089 // ESSL1 sections 5.2 and 5.7:
4090 // ESSL3 section 5.7:
4091 // Ternary operator is not among the operators allowed for structures/arrays.
4092 if (trueBlock->isArray() || trueBlock->getBasicType() == EbtStruct)
4093 {
4094 error(loc, "ternary operator is not allowed for structures or arrays", ":");
4095 recover();
4096 return falseBlock;
4097 }
Olli Etuaho52901742015-04-15 13:42:45 +03004098 return intermediate.addSelection(cond, trueBlock, falseBlock, loc);
4099}
Olli Etuaho49300862015-02-20 14:54:49 +02004100
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004101//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004102// Parse an array of strings using yyparse.
4103//
4104// Returns 0 for success.
4105//
Jamie Madillb98c3a82015-07-23 14:26:04 -04004106int PaParseStrings(size_t count,
4107 const char *const string[],
4108 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05304109 TParseContext *context)
4110{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004111 if ((count == 0) || (string == NULL))
4112 return 1;
4113
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004114 if (glslang_initialize(context))
4115 return 1;
4116
alokp@chromium.org408c45e2012-04-05 15:54:43 +00004117 int error = glslang_scan(count, string, length, context);
4118 if (!error)
4119 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004120
alokp@chromium.org73bc2982012-06-19 18:48:05 +00004121 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00004122
alokp@chromium.org6b495712012-06-29 00:06:58 +00004123 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004124}