blob: b587f5e16aabffcb140d3277e0021ec96040ec15 [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 Skibab25d14e2015-06-23 17:43:14 -070013#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//
Arun Patole7e7e68d2015-05-22 12:02:25 +053029bool TParseContext::parseVectorFields(const TString &compString, int vecSize, TVectorFields &fields,
30 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000031{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000032 fields.num = (int) compString.size();
Arun Patole7e7e68d2015-05-22 12:02:25 +053033 if (fields.num > 4)
34 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000035 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000036 return false;
37 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000038
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000039 enum {
40 exyzw,
41 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +000042 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000043 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000044
Arun Patole7e7e68d2015-05-22 12:02:25 +053045 for (int i = 0; i < fields.num; ++i)
46 {
47 switch (compString[i])
48 {
49 case 'x':
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000050 fields.offsets[i] = 0;
51 fieldSet[i] = exyzw;
52 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053053 case 'r':
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000054 fields.offsets[i] = 0;
55 fieldSet[i] = ergba;
56 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053057 case 's':
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000058 fields.offsets[i] = 0;
59 fieldSet[i] = estpq;
60 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053061 case 'y':
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000062 fields.offsets[i] = 1;
63 fieldSet[i] = exyzw;
64 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053065 case 'g':
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000066 fields.offsets[i] = 1;
67 fieldSet[i] = ergba;
68 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053069 case 't':
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000070 fields.offsets[i] = 1;
71 fieldSet[i] = estpq;
72 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053073 case 'z':
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000074 fields.offsets[i] = 2;
75 fieldSet[i] = exyzw;
76 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053077 case 'b':
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000078 fields.offsets[i] = 2;
79 fieldSet[i] = ergba;
80 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053081 case 'p':
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000082 fields.offsets[i] = 2;
83 fieldSet[i] = estpq;
84 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053085
86 case 'w':
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000087 fields.offsets[i] = 3;
88 fieldSet[i] = exyzw;
89 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053090 case 'a':
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000091 fields.offsets[i] = 3;
92 fieldSet[i] = ergba;
93 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053094 case 'q':
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000095 fields.offsets[i] = 3;
96 fieldSet[i] = estpq;
97 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053098 default:
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000099 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000100 return false;
101 }
102 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000103
Arun Patole7e7e68d2015-05-22 12:02:25 +0530104 for (int i = 0; i < fields.num; ++i)
105 {
106 if (fields.offsets[i] >= vecSize)
107 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000108 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000109 return false;
110 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000111
Arun Patole7e7e68d2015-05-22 12:02:25 +0530112 if (i > 0)
113 {
114 if (fieldSet[i] != fieldSet[i-1])
115 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000116 error(line, "illegal - vector component fields not from the same set", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000117 return false;
118 }
119 }
120 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000121
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000122 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000123}
124
125
126//
127// Look at a '.' field selector string and change it into offsets
128// for a matrix.
129//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530130bool TParseContext::parseMatrixFields(const TString &compString, int matCols, int matRows, TMatrixFields &fields,
131 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000132{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000133 fields.wholeRow = false;
134 fields.wholeCol = false;
135 fields.row = -1;
136 fields.col = -1;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000137
Arun Patole7e7e68d2015-05-22 12:02:25 +0530138 if (compString.size() != 2)
139 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000140 error(line, "illegal length of matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000141 return false;
142 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000143
Arun Patole7e7e68d2015-05-22 12:02:25 +0530144 if (compString[0] == '_')
145 {
146 if (compString[1] < '0' || compString[1] > '3')
147 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000148 error(line, "illegal matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000149 return false;
150 }
151 fields.wholeCol = true;
152 fields.col = compString[1] - '0';
Arun Patole7e7e68d2015-05-22 12:02:25 +0530153 }
154 else if (compString[1] == '_')
155 {
156 if (compString[0] < '0' || compString[0] > '3')
157 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000158 error(line, "illegal matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000159 return false;
160 }
161 fields.wholeRow = true;
162 fields.row = compString[0] - '0';
Arun Patole7e7e68d2015-05-22 12:02:25 +0530163 }
164 else
165 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000166 if (compString[0] < '0' || compString[0] > '3' ||
Arun Patole7e7e68d2015-05-22 12:02:25 +0530167 compString[1] < '0' || compString[1] > '3')
168 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000169 error(line, "illegal matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000170 return false;
171 }
172 fields.row = compString[0] - '0';
173 fields.col = compString[1] - '0';
174 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000175
Arun Patole7e7e68d2015-05-22 12:02:25 +0530176 if (fields.row >= matRows || fields.col >= matCols)
177 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000178 error(line, "matrix field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000179 return false;
180 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000181
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000182 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000183}
184
185///////////////////////////////////////////////////////////////////////
186//
187// Errors
188//
189////////////////////////////////////////////////////////////////////////
190
191//
192// Track whether errors have occurred.
193//
194void TParseContext::recover()
195{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000196}
197
198//
199// Used by flex/bison to output all syntax and parsing errors.
200//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530201void TParseContext::error(const TSourceLoc &loc,
202 const char *reason, const char *token,
203 const char *extraInfo)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000204{
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000205 pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -0400206 srcLoc.file = loc.first_file;
207 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400208 mDiagnostics.writeInfo(pp::Diagnostics::PP_ERROR,
209 srcLoc, reason, token, extraInfo);
alokp@chromium.orgff42c632010-05-10 15:14:30 +0000210
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000211}
212
Arun Patole7e7e68d2015-05-22 12:02:25 +0530213void TParseContext::warning(const TSourceLoc &loc,
214 const char *reason, const char *token,
215 const char *extraInfo)
216{
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000217 pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -0400218 srcLoc.file = loc.first_file;
219 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400220 mDiagnostics.writeInfo(pp::Diagnostics::PP_WARNING,
221 srcLoc, reason, token, extraInfo);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000222}
223
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000224//
225// Same error message for all places assignments don't work.
226//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530227void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000228{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000229 std::stringstream extraInfoStream;
230 extraInfoStream << "cannot convert from '" << right << "' to '" << left << "'";
231 std::string extraInfo = extraInfoStream.str();
232 error(line, "", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000233}
234
235//
236// Same error message for all places unary operations don't work.
237//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530238void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000239{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000240 std::stringstream extraInfoStream;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530241 extraInfoStream << "no operation '" << op << "' exists that takes an operand of type " << operand
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000242 << " (or there is no acceptable conversion)";
243 std::string extraInfo = extraInfoStream.str();
244 error(line, " wrong operand type", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000245}
246
247//
248// Same error message for all binary operations don't work.
249//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530250void TParseContext::binaryOpError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000251{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000252 std::stringstream extraInfoStream;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530253 extraInfoStream << "no operation '" << op << "' exists that takes a left-hand operand of type '" << left
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000254 << "' and a right operand of type '" << right << "' (or there is no acceptable conversion)";
255 std::string extraInfo = extraInfoStream.str();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530256 error(line, " wrong operand types ", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000257}
258
Arun Patole7e7e68d2015-05-22 12:02:25 +0530259bool TParseContext::precisionErrorCheck(const TSourceLoc &line, TPrecision precision, TBasicType type)
260{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400261 if (!mChecksPrecisionErrors)
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000262 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530263 switch(type)
264 {
265 case EbtFloat:
266 if( precision == EbpUndefined )
267 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000268 error( line, "No precision specified for (float)", "" );
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000269 return true;
270 }
271 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530272 case EbtInt:
273 if( precision == EbpUndefined )
274 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000275 error( line, "No precision specified (int)", "" );
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000276 return true;
277 }
278 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530279 default:
daniel@transgaming.com0eb64c32011-03-15 18:23:51 +0000280 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000281 }
282 return false;
283}
284
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000285//
286// Both test and if necessary, spit out an error, to see if the node is really
287// an l-value that can be operated on this way.
288//
289// Returns true if the was an error.
290//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530291bool TParseContext::lValueErrorCheck(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000292{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530293 TIntermSymbol *symNode = node->getAsSymbolNode();
294 TIntermBinary *binaryNode = node->getAsBinaryNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000295
Arun Patole7e7e68d2015-05-22 12:02:25 +0530296 if (binaryNode)
297 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000298 bool errorReturn;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000299
Arun Patole7e7e68d2015-05-22 12:02:25 +0530300 switch(binaryNode->getOp())
301 {
302 case EOpIndexDirect:
303 case EOpIndexIndirect:
304 case EOpIndexDirectStruct:
305 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000306 return lValueErrorCheck(line, op, binaryNode->getLeft());
Arun Patole7e7e68d2015-05-22 12:02:25 +0530307 case EOpVectorSwizzle:
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000308 errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft());
Arun Patole7e7e68d2015-05-22 12:02:25 +0530309 if (!errorReturn)
310 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000311 int offset[4] = {0,0,0,0};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000312
Arun Patole7e7e68d2015-05-22 12:02:25 +0530313 TIntermTyped *rightNode = binaryNode->getRight();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000314 TIntermAggregate *aggrNode = rightNode->getAsAggregate();
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700315
316 for (TIntermSequence::iterator p = aggrNode->getSequence()->begin();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530317 p != aggrNode->getSequence()->end(); p++)
318 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +0000319 int value = (*p)->getAsTyped()->getAsConstantUnion()->getIConst(0);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700320 offset[value]++;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530321 if (offset[value] > 1)
322 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000323 error(line, " l-value of swizzle cannot have duplicate components", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000324
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000325 return true;
326 }
327 }
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700328 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000329
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000330 return errorReturn;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530331 default:
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000332 break;
333 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000334 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000335
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000336 return true;
337 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000338
339
Arun Patole7e7e68d2015-05-22 12:02:25 +0530340 const char *symbol = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000341 if (symNode != 0)
342 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000343
Arun Patole7e7e68d2015-05-22 12:02:25 +0530344 const char *message = 0;
345 switch (node->getQualifier())
346 {
347 case EvqConst:
348 message = "can't modify a const";
349 break;
350 case EvqConstReadOnly:
351 message = "can't modify a const";
352 break;
353 case EvqAttribute:
354 message = "can't modify an attribute";
355 break;
356 case EvqFragmentIn:
357 message = "can't modify an input";
358 break;
359 case EvqVertexIn:
360 message = "can't modify an input";
361 break;
362 case EvqUniform:
363 message = "can't modify a uniform";
364 break;
365 case EvqVaryingIn:
366 message = "can't modify a varying";
367 break;
368 case EvqFragCoord:
369 message = "can't modify gl_FragCoord";
370 break;
371 case EvqFrontFacing:
372 message = "can't modify gl_FrontFacing";
373 break;
374 case EvqPointCoord:
375 message = "can't modify gl_PointCoord";
376 break;
377 default:
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000378 //
379 // Type that can't be written to?
380 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530381 if (node->getBasicType() == EbtVoid)
382 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000383 message = "can't modify void";
Nicolas Capens344e7142013-06-24 15:39:21 -0400384 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530385 if (IsSampler(node->getBasicType()))
386 {
Nicolas Capens344e7142013-06-24 15:39:21 -0400387 message = "can't modify a sampler";
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000388 }
389 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000390
Arun Patole7e7e68d2015-05-22 12:02:25 +0530391 if (message == 0 && binaryNode == 0 && symNode == 0)
392 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000393 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000394
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000395 return true;
396 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000397
398
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000399 //
400 // Everything else is okay, no error.
401 //
402 if (message == 0)
403 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000404
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000405 //
406 // If we get here, we have an error and a message.
407 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530408 if (symNode)
409 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000410 std::stringstream extraInfoStream;
411 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
412 std::string extraInfo = extraInfoStream.str();
413 error(line, " l-value required", op, extraInfo.c_str());
414 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530415 else
416 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000417 std::stringstream extraInfoStream;
418 extraInfoStream << "(" << message << ")";
419 std::string extraInfo = extraInfoStream.str();
420 error(line, " l-value required", op, extraInfo.c_str());
421 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000422
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000423 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000424}
425
426//
427// Both test, and if necessary spit out an error, to see if the node is really
428// a constant.
429//
430// Returns true if the was an error.
431//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530432bool TParseContext::constErrorCheck(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000433{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000434 if (node->getQualifier() == EvqConst)
435 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000436
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000437 error(node->getLine(), "constant expression required", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000438
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000439 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000440}
441
442//
443// Both test, and if necessary spit out an error, to see if the node is really
444// an integer.
445//
446// Returns true if the was an error.
447//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530448bool TParseContext::integerErrorCheck(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000449{
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000450 if (node->isScalarInt())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000451 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000452
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000453 error(node->getLine(), "integer expression required", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000454
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000455 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000456}
457
458//
459// Both test, and if necessary spit out an error, to see if we are currently
460// globally scoped.
461//
462// Returns true if the was an error.
463//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530464bool TParseContext::globalErrorCheck(const TSourceLoc &line, bool global, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000465{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000466 if (global)
467 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000468
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000469 error(line, "only allowed at global scope", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000470
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000471 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000472}
473
474//
475// For now, keep it simple: if it starts "gl_", it's reserved, independent
476// of scope. Except, if the symbol table is at the built-in push-level,
477// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000478// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
479// webgl shader.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000480//
481// Returns true if there was an error.
482//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530483bool TParseContext::reservedErrorCheck(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000484{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530485 static const char *reservedErrMsg = "reserved built-in name";
486 if (!symbolTable.atBuiltInLevel())
487 {
488 if (identifier.compare(0, 3, "gl_") == 0)
489 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000490 error(line, reservedErrMsg, "gl_");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000491 return true;
492 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530493 if (IsWebGLBasedSpec(mShaderSpec))
494 {
495 if (identifier.compare(0, 6, "webgl_") == 0)
496 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000497 error(line, reservedErrMsg, "webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000498 return true;
499 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530500 if (identifier.compare(0, 7, "_webgl_") == 0)
501 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000502 error(line, reservedErrMsg, "_webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000503 return true;
504 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530505 if (mShaderSpec == SH_CSS_SHADERS_SPEC && identifier.compare(0, 4, "css_") == 0)
506 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000507 error(line, reservedErrMsg, "css_");
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000508 return true;
509 }
alokp@chromium.org613ef312010-07-21 18:54:22 +0000510 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530511 if (identifier.find("__") != TString::npos)
512 {
513 error(line,
514 "identifiers containing two consecutive underscores (__) are reserved as possible future keywords",
515 identifier.c_str());
daniel@transgaming.combeadd5d2012-04-12 02:35:31 +0000516 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000517 }
518 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000519
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000520 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000521}
522
523//
524// Make sure there is enough data provided to the constructor to build
525// something of the type of the constructor. Also returns the type of
526// the constructor.
527//
528// Returns true if there was an error in construction.
529//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530530bool TParseContext::constructorErrorCheck(const TSourceLoc &line, TIntermNode *node, TFunction &function, TOperator op,
531 TType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000532{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000533 *type = function.getReturnType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000534
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000535 bool constructingMatrix = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530536 switch(op)
537 {
538 case EOpConstructMat2:
539 case EOpConstructMat3:
540 case EOpConstructMat4:
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000541 constructingMatrix = true;
542 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530543 default:
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000544 break;
545 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000546
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000547 //
548 // Note: It's okay to have too many components available, but not okay to have unused
549 // arguments. 'full' will go to true when enough args have been seen. If we loop
550 // again, there is an extra argument, so 'overfull' will become true.
551 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000552
Jamie Madill94bf7f22013-07-08 13:31:15 -0400553 size_t size = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000554 bool constType = true;
555 bool full = false;
556 bool overFull = false;
557 bool matrixInMatrix = false;
558 bool arrayArg = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530559 for (size_t i = 0; i < function.getParamCount(); ++i)
560 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700561 const TConstParameter &param = function.getParam(i);
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000562 size += param.type->getObjectSize();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530563
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000564 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000565 matrixInMatrix = true;
566 if (full)
567 overFull = true;
568 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
569 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000570 if (param.type->getQualifier() != EvqConst)
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000571 constType = false;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000572 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000573 arrayArg = true;
574 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530575
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000576 if (constType)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000577 type->setQualifier(EvqConst);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000578
Olli Etuaho376f1b52015-04-13 13:23:41 +0300579 if (type->isArray())
580 {
581 if (type->isUnsizedArray())
582 {
583 type->setArraySize(function.getParamCount());
584 }
585 else if (static_cast<size_t>(type->getArraySize()) != function.getParamCount())
586 {
587 error(line, "array constructor needs one argument per array element", "constructor");
588 return true;
589 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000590 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000591
Arun Patole7e7e68d2015-05-22 12:02:25 +0530592 if (arrayArg && op != EOpConstructStruct)
593 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000594 error(line, "constructing from a non-dereferenced array", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000595 return true;
596 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000597
Arun Patole7e7e68d2015-05-22 12:02:25 +0530598 if (matrixInMatrix && !type->isArray())
599 {
600 if (function.getParamCount() != 1)
601 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000602 error(line, "constructing matrix from matrix can only take one argument", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000603 return true;
604 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000605 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000606
Arun Patole7e7e68d2015-05-22 12:02:25 +0530607 if (overFull)
608 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000609 error(line, "too many arguments", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000610 return true;
611 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530612
613 if (op == EOpConstructStruct && !type->isArray() && type->getStruct()->fields().size() != function.getParamCount())
614 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000615 error(line, "Number of constructor parameters does not match the number of structure fields", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000616 return true;
617 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000618
Arun Patole7e7e68d2015-05-22 12:02:25 +0530619 if (!type->isMatrix() || !matrixInMatrix)
620 {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000621 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
Arun Patole7e7e68d2015-05-22 12:02:25 +0530622 (op == EOpConstructStruct && size < type->getObjectSize()))
623 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000624 error(line, "not enough data provided for construction", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000625 return true;
626 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000627 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000628
daniel@transgaming.com0b53fc02011-03-09 15:12:12 +0000629 TIntermTyped *typed = node ? node->getAsTyped() : 0;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530630 if (typed == 0)
631 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000632 error(line, "constructor argument does not have a type", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000633 return true;
634 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530635 if (op != EOpConstructStruct && IsSampler(typed->getBasicType()))
636 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000637 error(line, "cannot convert a sampler", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000638 return true;
639 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530640 if (typed->getBasicType() == EbtVoid)
641 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000642 error(line, "cannot convert a void", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000643 return true;
644 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000645
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000646 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000647}
648
649// This function checks to see if a void variable has been declared and raise an error message for such a case
650//
651// returns true in case of an error
652//
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300653bool TParseContext::voidErrorCheck(const TSourceLoc &line, const TString &identifier, const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000654{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300655 if (type == EbtVoid)
656 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000657 error(line, "illegal use of type 'void'", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000658 return true;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300659 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000660
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000661 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000662}
663
664// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
665//
666// returns true in case of an error
667//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530668bool TParseContext::boolErrorCheck(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000669{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530670 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector())
671 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000672 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000673 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530674 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000675
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000676 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000677}
678
679// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
680//
681// returns true in case of an error
682//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530683bool TParseContext::boolErrorCheck(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000684{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530685 if (pType.type != EbtBool || pType.isAggregate())
686 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000687 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000688 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530689 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000690
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000691 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000692}
693
Arun Patole7e7e68d2015-05-22 12:02:25 +0530694bool TParseContext::samplerErrorCheck(const TSourceLoc &line, const TPublicType &pType, const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000695{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530696 if (pType.type == EbtStruct)
697 {
698 if (containsSampler(*pType.userDef))
699 {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000700 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530701
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000702 return true;
703 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530704
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000705 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530706 }
707 else if (IsSampler(pType.type))
708 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000709 error(line, reason, getBasicString(pType.type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000710
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000711 return true;
712 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000713
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000714 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000715}
716
Arun Patole7e7e68d2015-05-22 12:02:25 +0530717bool TParseContext::locationDeclaratorListCheck(const TSourceLoc &line, const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400718{
719 if (pType.layoutQualifier.location != -1)
720 {
721 error(line, "location must only be specified for a single input or output variable", "location");
722 return true;
723 }
724
725 return false;
726}
727
Arun Patole7e7e68d2015-05-22 12:02:25 +0530728bool TParseContext::parameterSamplerErrorCheck(const TSourceLoc &line, TQualifier qualifier, const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000729{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530730 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
731 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType()))
732 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000733 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000734 return true;
735 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000736
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000737 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000738}
739
Arun Patole7e7e68d2015-05-22 12:02:25 +0530740bool TParseContext::containsSampler(const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000741{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000742 if (IsSampler(type.getBasicType()))
743 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000744
Arun Patole7e7e68d2015-05-22 12:02:25 +0530745 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock())
746 {
747 const TFieldList &fields = type.getStruct()->fields();
748 for (unsigned int i = 0; i < fields.size(); ++i)
749 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400750 if (containsSampler(*fields[i]->type()))
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000751 return true;
752 }
753 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000754
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000755 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000756}
757
758//
759// Do size checking for an array type's size.
760//
761// Returns true if there was an error.
762//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530763bool TParseContext::arraySizeErrorCheck(const TSourceLoc &line, TIntermTyped *expr, int &size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000764{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530765 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000766
Olli Etuahoe7847b02015-03-16 11:56:12 +0200767 if (constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000768 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000769 error(line, "array size must be a constant integer expression", "");
Olli Etuahoe7847b02015-03-16 11:56:12 +0200770 size = 1;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000771 return true;
772 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000773
Nicolas Capens906744a2014-06-06 15:18:07 -0400774 unsigned int unsignedSize = 0;
775
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000776 if (constant->getBasicType() == EbtUInt)
777 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400778 unsignedSize = constant->getUConst(0);
779 size = static_cast<int>(unsignedSize);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000780 }
781 else
782 {
783 size = constant->getIConst(0);
784
Nicolas Capens906744a2014-06-06 15:18:07 -0400785 if (size < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000786 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400787 error(line, "array size must be non-negative", "");
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000788 size = 1;
789 return true;
790 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400791
792 unsignedSize = static_cast<unsigned int>(size);
793 }
794
795 if (size == 0)
796 {
797 error(line, "array size must be greater than zero", "");
798 size = 1;
799 return true;
800 }
801
802 // The size of arrays is restricted here to prevent issues further down the
803 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
804 // 4096 registers so this should be reasonable even for aggressively optimizable code.
805 const unsigned int sizeLimit = 65536;
806
807 if (unsignedSize > sizeLimit)
808 {
809 error(line, "array size too large", "");
810 size = 1;
811 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000812 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000813
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000814 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000815}
816
817//
818// See if this qualifier can be an array.
819//
820// Returns true if there is an error.
821//
Olli Etuaho3739d232015-04-08 12:23:44 +0300822bool TParseContext::arrayQualifierErrorCheck(const TSourceLoc &line, const TPublicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000823{
Olli Etuaho3739d232015-04-08 12:23:44 +0300824 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqVertexIn) ||
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400825 (type.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300826 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000827 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000828 return true;
829 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000830
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000831 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000832}
833
834//
835// See if this type can be an array.
836//
837// Returns true if there is an error.
838//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530839bool TParseContext::arrayTypeErrorCheck(const TSourceLoc &line, const TPublicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000840{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000841 //
842 // Can the type be an array?
843 //
Jamie Madill06145232015-05-13 13:10:01 -0400844 if (type.array)
845 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000846 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000847 return true;
848 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000849
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000850 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000851}
852
853//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000854// Enforce non-initializer type/qualifier rules.
855//
856// Returns true if there was an error.
857//
Olli Etuaho376f1b52015-04-13 13:23:41 +0300858bool TParseContext::nonInitErrorCheck(const TSourceLoc &line, const TString &identifier, TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000859{
Olli Etuaho3739d232015-04-08 12:23:44 +0300860 ASSERT(type != nullptr);
861 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000862 {
863 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300864 type->qualifier = EvqTemporary;
865
866 // Generate informative error messages for ESSL1.
867 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400868 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000869 {
Arun Patole7e7e68d2015-05-22 12:02:25 +0530870 error(line,
871 "structures containing arrays may not be declared constant since they cannot be initialized",
872 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000873 }
874 else
875 {
876 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
877 }
878
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000879 return true;
880 }
Olli Etuaho376f1b52015-04-13 13:23:41 +0300881 if (type->isUnsizedArray())
882 {
883 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
884 return true;
885 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000886 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000887}
888
Olli Etuaho2935c582015-04-08 14:32:06 +0300889// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000890// and update the symbol table.
891//
Olli Etuaho2935c582015-04-08 14:32:06 +0300892// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000893//
Olli Etuaho2935c582015-04-08 14:32:06 +0300894bool TParseContext::declareVariable(const TSourceLoc &line, const TString &identifier, const TType &type,
895 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000896{
Olli Etuaho2935c582015-04-08 14:32:06 +0300897 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000898
Olli Etuaho2935c582015-04-08 14:32:06 +0300899 bool needsReservedErrorCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000900
Olli Etuaho2935c582015-04-08 14:32:06 +0300901 // gl_LastFragData may be redeclared with a new precision qualifier
902 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
903 {
904 const TVariable *maxDrawBuffers =
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400905 static_cast<const TVariable *>(symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho2935c582015-04-08 14:32:06 +0300906 if (type.getArraySize() == maxDrawBuffers->getConstPointer()->getIConst())
907 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400908 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +0300909 {
910 needsReservedErrorCheck = extensionErrorCheck(line, builtInSymbol->getExtension());
911 }
912 }
913 else
914 {
915 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers", identifier.c_str());
916 return false;
917 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000918 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000919
Olli Etuaho2935c582015-04-08 14:32:06 +0300920 if (needsReservedErrorCheck && reservedErrorCheck(line, identifier))
921 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000922
Olli Etuaho2935c582015-04-08 14:32:06 +0300923 (*variable) = new TVariable(&identifier, type);
924 if (!symbolTable.declare(*variable))
925 {
926 error(line, "redefinition", identifier.c_str());
927 delete (*variable);
928 (*variable) = nullptr;
929 return false;
930 }
931
932 if (voidErrorCheck(line, identifier, type.getBasicType()))
933 return false;
934
935 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000936}
937
Arun Patole7e7e68d2015-05-22 12:02:25 +0530938bool TParseContext::paramErrorCheck(const TSourceLoc &line, TQualifier qualifier, TQualifier paramQualifier,
939 TType *type)
940{
941 if (qualifier != EvqConst && qualifier != EvqTemporary)
942 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000943 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000944 return true;
945 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530946 if (qualifier == EvqConst && paramQualifier != EvqIn)
947 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000948 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
949 return true;
950 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000951
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000952 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000953 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000954 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000955 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000956
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000957 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000958}
959
Arun Patole7e7e68d2015-05-22 12:02:25 +0530960bool TParseContext::extensionErrorCheck(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000961{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530962 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000963 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +0530964 if (iter == extBehavior.end())
965 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000966 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000967 return true;
968 }
zmo@google.comf5450912011-09-09 01:37:19 +0000969 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +0530970 if (iter->second == EBhDisable || iter->second == EBhUndefined)
971 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000972 error(line, "extension", extension.c_str(), "is disabled");
973 return true;
974 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530975 if (iter->second == EBhWarn)
976 {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000977 warning(line, "extension", extension.c_str(), "is being used");
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000978 return false;
979 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000980
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000981 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000982}
983
Olli Etuahofa33d582015-04-09 14:33:12 +0300984// These checks are common for all declarations starting a declarator list, and declarators that follow an empty
985// declaration.
986//
Jamie Madill06145232015-05-13 13:10:01 -0400987bool TParseContext::singleDeclarationErrorCheck(const TPublicType &publicType, const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -0400988{
Olli Etuahofa33d582015-04-09 14:33:12 +0300989 switch (publicType.qualifier)
990 {
991 case EvqVaryingIn:
992 case EvqVaryingOut:
993 case EvqAttribute:
994 case EvqVertexIn:
995 case EvqFragmentOut:
996 if (publicType.type == EbtStruct)
997 {
998 error(identifierLocation, "cannot be used with a structure",
999 getQualifierString(publicType.qualifier));
1000 return true;
1001 }
1002
1003 default: break;
1004 }
1005
1006 if (publicType.qualifier != EvqUniform && samplerErrorCheck(identifierLocation, publicType,
1007 "samplers must be uniform"))
1008 {
Jamie Madilla5efff92013-06-06 11:56:47 -04001009 return true;
Olli Etuahofa33d582015-04-09 14:33:12 +03001010 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001011
1012 // check for layout qualifier issues
1013 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
1014
1015 if (layoutQualifier.matrixPacking != EmpUnspecified)
1016 {
Olli Etuahofa33d582015-04-09 14:33:12 +03001017 error(identifierLocation, "layout qualifier", getMatrixPackingString(layoutQualifier.matrixPacking),
1018 "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -04001019 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001020 }
1021
1022 if (layoutQualifier.blockStorage != EbsUnspecified)
1023 {
Olli Etuahofa33d582015-04-09 14:33:12 +03001024 error(identifierLocation, "layout qualifier", getBlockStorageString(layoutQualifier.blockStorage),
1025 "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -04001026 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001027 }
1028
Olli Etuahofa33d582015-04-09 14:33:12 +03001029 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut &&
1030 layoutLocationErrorCheck(identifierLocation, publicType.layoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001031 {
Jamie Madill51a53c72013-06-19 09:24:43 -04001032 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001033 }
1034
1035 return false;
1036}
1037
Arun Patole7e7e68d2015-05-22 12:02:25 +05301038bool TParseContext::layoutLocationErrorCheck(const TSourceLoc &location, const TLayoutQualifier &layoutQualifier)
Jamie Madilla5efff92013-06-06 11:56:47 -04001039{
1040 if (layoutQualifier.location != -1)
1041 {
1042 error(location, "invalid layout qualifier:", "location", "only valid on program inputs and outputs");
1043 return true;
1044 }
1045
1046 return false;
1047}
1048
Olli Etuahob6e07a62015-02-16 12:22:10 +02001049bool TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate, TIntermAggregate *aggregate)
1050{
1051 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1052 {
1053 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1054 if (qual == EvqOut || qual == EvqInOut)
1055 {
1056 TIntermTyped *node = (*(aggregate->getSequence()))[i]->getAsTyped();
1057 if (lValueErrorCheck(node->getLine(), "assign", node))
1058 {
1059 error(node->getLine(),
1060 "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error");
1061 recover();
1062 return true;
1063 }
1064 }
1065 }
1066 return false;
1067}
1068
Olli Etuaho37ad4742015-04-27 13:18:50 +03001069void TParseContext::es3InvariantErrorCheck(const TQualifier qualifier, const TSourceLoc &invariantLocation)
1070{
1071 if (!sh::IsVaryingOut(qualifier) && qualifier != EvqFragmentOut)
1072 {
1073 error(invariantLocation, "Only out variables can be invariant.", "invariant");
1074 recover();
1075 }
1076}
1077
Arun Patole7e7e68d2015-05-22 12:02:25 +05301078bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001079{
Arun Patole7e7e68d2015-05-22 12:02:25 +05301080 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001081 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1082 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001083}
1084
Arun Patole7e7e68d2015-05-22 12:02:25 +05301085bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001086{
Arun Patole7e7e68d2015-05-22 12:02:25 +05301087 const TExtensionBehavior &extbehavior = extensionBehavior();
Shannon Woodsa49a9bf2013-08-02 17:23:14 -04001088 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001089
1090 if (iter == extbehavior.end())
1091 {
1092 return false;
1093 }
1094
1095 return (iter->second == EBhEnable || iter->second == EBhRequire);
1096}
1097
Arun Patole7e7e68d2015-05-22 12:02:25 +05301098void TParseContext::handleExtensionDirective(const TSourceLoc &loc, const char *extName, const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001099{
1100 pp::SourceLocation srcLoc;
1101 srcLoc.file = loc.first_file;
1102 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001103 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001104}
1105
Arun Patole7e7e68d2015-05-22 12:02:25 +05301106void TParseContext::handlePragmaDirective(const TSourceLoc &loc, const char *name, const char *value, bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001107{
1108 pp::SourceLocation srcLoc;
1109 srcLoc.file = loc.first_file;
1110 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001111 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001112}
1113
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001114/////////////////////////////////////////////////////////////////////////////////
1115//
1116// Non-Errors.
1117//
1118/////////////////////////////////////////////////////////////////////////////////
1119
Jamie Madill5c097022014-08-20 16:38:32 -04001120const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1121 const TString *name,
1122 const TSymbol *symbol)
1123{
1124 const TVariable *variable = NULL;
1125
1126 if (!symbol)
1127 {
1128 error(location, "undeclared identifier", name->c_str());
1129 recover();
1130 }
1131 else if (!symbol->isVariable())
1132 {
1133 error(location, "variable expected", name->c_str());
1134 recover();
1135 }
1136 else
1137 {
1138 variable = static_cast<const TVariable*>(symbol);
1139
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001140 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
Jamie Madill5c097022014-08-20 16:38:32 -04001141 !variable->getExtension().empty() &&
1142 extensionErrorCheck(location, variable->getExtension()))
1143 {
1144 recover();
1145 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001146
1147 // Reject shaders using both gl_FragData and gl_FragColor
1148 TQualifier qualifier = variable->getType().getQualifier();
1149 if (qualifier == EvqFragData)
1150 {
1151 mUsesFragData = true;
1152 }
1153 else if (qualifier == EvqFragColor)
1154 {
1155 mUsesFragColor = true;
1156 }
1157
1158 // This validation is not quite correct - it's only an error to write to
1159 // both FragData and FragColor. For simplicity, and because users shouldn't
1160 // be rewarded for reading from undefined varaibles, return an error
1161 // if they are both referenced, rather than assigned.
1162 if (mUsesFragData && mUsesFragColor)
1163 {
1164 error(location, "cannot use both gl_FragData and gl_FragColor", name->c_str());
1165 recover();
1166 }
Jamie Madill5c097022014-08-20 16:38:32 -04001167 }
1168
1169 if (!variable)
1170 {
1171 TType type(EbtFloat, EbpUndefined);
1172 TVariable *fakeVariable = new TVariable(name, type);
1173 symbolTable.declare(fakeVariable);
1174 variable = fakeVariable;
1175 }
1176
1177 return variable;
1178}
1179
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001180//
1181// Look up a function name in the symbol table, and make sure it is a function.
1182//
1183// Return the function symbol if found, otherwise 0.
1184//
Arun Patole7e7e68d2015-05-22 12:02:25 +05301185const TFunction *TParseContext::findFunction(const TSourceLoc &line, TFunction *call, int inputShaderVersion,
1186 bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001187{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001188 // First find by unmangled name to check whether the function name has been
1189 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001190 // If a function is found, check for one with a matching argument list.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301191 const TSymbol *symbol = symbolTable.find(call->getName(), inputShaderVersion, builtIn);
1192 if (symbol == 0 || symbol->isFunction())
1193 {
Austin Kinross3ae64652015-01-26 15:51:39 -08001194 symbol = symbolTable.find(call->getMangledName(), inputShaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001195 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001196
Arun Patole7e7e68d2015-05-22 12:02:25 +05301197 if (symbol == 0)
1198 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001199 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001200 return 0;
1201 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001202
Arun Patole7e7e68d2015-05-22 12:02:25 +05301203 if (!symbol->isFunction())
1204 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001205 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001206 return 0;
1207 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001208
1209 return static_cast<const TFunction*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001210}
1211
1212//
1213// Initializers show up in several places in the grammar. Have one set of
1214// code to handle them here.
1215//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001216// Returns true on error, false if no error
1217//
Jamie Madill06145232015-05-13 13:10:01 -04001218bool TParseContext::executeInitializer(const TSourceLoc &line, const TString &identifier, const TPublicType &pType,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001219 TIntermTyped *initializer, TIntermNode **intermNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001220{
Olli Etuahoe7847b02015-03-16 11:56:12 +02001221 ASSERT(intermNode != nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001222 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001223
Olli Etuaho2935c582015-04-08 14:32:06 +03001224 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001225 if (type.isUnsizedArray())
1226 {
1227 type.setArraySize(initializer->getArraySize());
1228 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001229 if (!declareVariable(line, identifier, type, &variable))
1230 {
1231 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001232 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001233
Olli Etuahob0c645e2015-05-12 14:25:36 +03001234 bool globalInitWarning = false;
1235 if (symbolTable.atGlobalLevel() && !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
1236 {
1237 // Error message does not completely match behavior with ESSL 1.00, but
1238 // we want to steer developers towards only using constant expressions.
1239 error(line, "global variable initializers must be constant expressions", "=");
1240 return true;
1241 }
1242 if (globalInitWarning)
1243 {
1244 warning(line, "global variable initializers should be constant expressions "
1245 "(uniforms and globals are allowed in global initializers for legacy compatibility)", "=");
1246 }
1247
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001248 //
1249 // identifier must be of type constant, a global, or a temporary
1250 //
1251 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301252 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1253 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001254 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001255 return true;
1256 }
1257 //
1258 // test for and propagate constant
1259 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001260
Arun Patole7e7e68d2015-05-22 12:02:25 +05301261 if (qualifier == EvqConst)
1262 {
1263 if (qualifier != initializer->getType().getQualifier())
1264 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001265 std::stringstream extraInfoStream;
1266 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1267 std::string extraInfo = extraInfoStream.str();
1268 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001269 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001270 return true;
1271 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301272 if (type != initializer->getType())
1273 {
1274 error(line, " non-matching types for const initializer ",
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001275 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001276 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001277 return true;
1278 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301279 if (initializer->getAsConstantUnion())
1280 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001281 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Arun Patole7e7e68d2015-05-22 12:02:25 +05301282 }
1283 else if (initializer->getAsSymbolNode())
1284 {
1285 const TSymbol *symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1286 const TVariable *tVar = static_cast<const TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001287
Arun Patole7e7e68d2015-05-22 12:02:25 +05301288 TConstantUnion *constArray = tVar->getConstPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001289 variable->shareConstPointer(constArray);
Arun Patole7e7e68d2015-05-22 12:02:25 +05301290 }
1291 else
1292 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001293 std::stringstream extraInfoStream;
1294 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1295 std::string extraInfo = extraInfoStream.str();
1296 error(line, " cannot assign to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001297 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001298 return true;
1299 }
1300 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001301
1302 if (qualifier != EvqConst)
1303 {
1304 TIntermSymbol *intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(),
1305 variable->getType(), line);
1306 *intermNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1307 if (*intermNode == nullptr)
1308 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001309 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1310 return true;
1311 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001312 }
1313 else
1314 {
1315 *intermNode = nullptr;
1316 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001317
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001318 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001319}
1320
Arun Patole7e7e68d2015-05-22 12:02:25 +05301321bool TParseContext::areAllChildConst(TIntermAggregate *aggrNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001322{
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001323 ASSERT(aggrNode != NULL);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001324 if (!aggrNode->isConstructor())
1325 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001326
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001327 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001328
Arun Patole7e7e68d2015-05-22 12:02:25 +05301329 // check if all the child nodes are constants so that they can be inserted into
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001330 // the parent node
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001331 TIntermSequence *sequence = aggrNode->getSequence() ;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301332 for (TIntermSequence::iterator p = sequence->begin(); p != sequence->end(); ++p)
1333 {
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001334 if (!(*p)->getAsTyped()->getAsConstantUnion())
1335 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001336 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001337
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001338 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001339}
1340
Olli Etuaho214c2d82015-04-27 14:49:13 +03001341TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier, bool invariant, TLayoutQualifier layoutQualifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301342 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001343{
1344 TPublicType returnType = typeSpecifier;
1345 returnType.qualifier = qualifier;
Olli Etuaho214c2d82015-04-27 14:49:13 +03001346 returnType.invariant = invariant;
Jamie Madilla5efff92013-06-06 11:56:47 -04001347 returnType.layoutQualifier = layoutQualifier;
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001348
1349 if (typeSpecifier.array)
1350 {
1351 error(typeSpecifier.line, "not supported", "first-class array");
1352 recover();
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001353 returnType.clearArrayness();
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001354 }
1355
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001356 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001357 {
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001358 if (qualifier == EvqAttribute && (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1359 {
1360 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1361 recover();
1362 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001363
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001364 if ((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1365 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1366 {
1367 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1368 recover();
1369 }
1370 }
1371 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001372 {
Jamie Madillb120eac2013-06-12 14:08:13 -04001373 switch (qualifier)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001374 {
Jamie Madill19571812013-08-12 15:26:34 -07001375 case EvqSmoothIn:
1376 case EvqSmoothOut:
1377 case EvqVertexOut:
1378 case EvqFragmentIn:
1379 case EvqCentroidOut:
1380 case EvqCentroidIn:
1381 if (typeSpecifier.type == EbtBool)
1382 {
1383 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1384 recover();
1385 }
1386 if (typeSpecifier.type == EbtInt || typeSpecifier.type == EbtUInt)
1387 {
1388 error(typeSpecifier.line, "must use 'flat' interpolation here", getQualifierString(qualifier));
1389 recover();
1390 }
1391 break;
1392
1393 case EvqVertexIn:
1394 case EvqFragmentOut:
1395 case EvqFlatIn:
1396 case EvqFlatOut:
Jamie Madillb120eac2013-06-12 14:08:13 -04001397 if (typeSpecifier.type == EbtBool)
1398 {
1399 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1400 recover();
1401 }
1402 break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001403
Jamie Madillb120eac2013-06-12 14:08:13 -04001404 default: break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001405 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001406 }
1407
1408 return returnType;
1409}
1410
Olli Etuahofa33d582015-04-09 14:33:12 +03001411TIntermAggregate *TParseContext::parseSingleDeclaration(TPublicType &publicType,
1412 const TSourceLoc &identifierOrTypeLocation,
1413 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001414{
Olli Etuahofa33d582015-04-09 14:33:12 +03001415 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001416
Olli Etuahobab4c082015-04-24 16:38:49 +03001417 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03001418
Olli Etuahobab4c082015-04-24 16:38:49 +03001419 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1420
1421 if (emptyDeclaration)
1422 {
1423 if (publicType.isUnsizedArray())
1424 {
1425 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an error.
1426 // It is assumed that this applies to empty declarations as well.
1427 error(identifierOrTypeLocation, "empty array declaration needs to specify a size", identifier.c_str());
1428 }
1429 }
1430 else
Jamie Madill60ed9812013-06-06 11:56:46 -04001431 {
Olli Etuahofa33d582015-04-09 14:33:12 +03001432 if (singleDeclarationErrorCheck(publicType, identifierOrTypeLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001433 recover();
1434
Olli Etuaho376f1b52015-04-13 13:23:41 +03001435 if (nonInitErrorCheck(identifierOrTypeLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001436 recover();
1437
Olli Etuaho2935c582015-04-08 14:32:06 +03001438 TVariable *variable = nullptr;
Olli Etuahofa33d582015-04-09 14:33:12 +03001439 if (!declareVariable(identifierOrTypeLocation, identifier, TType(publicType), &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001440 recover();
1441
1442 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001443 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001444 }
1445
Olli Etuahoe7847b02015-03-16 11:56:12 +02001446 return intermediate.makeAggregate(symbol, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001447}
1448
Olli Etuahoe7847b02015-03-16 11:56:12 +02001449TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1450 const TSourceLoc &identifierLocation,
1451 const TString &identifier,
1452 const TSourceLoc &indexLocation,
1453 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001454{
Olli Etuahofa33d582015-04-09 14:33:12 +03001455 mDeferredSingleDeclarationErrorCheck = false;
1456
1457 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001458 recover();
1459
Olli Etuaho376f1b52015-04-13 13:23:41 +03001460 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001461 recover();
1462
1463 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1464 {
1465 recover();
1466 }
1467
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001468 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001469
1470 int size;
1471 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1472 {
1473 recover();
1474 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001475 // Make the type an array even if size check failed.
1476 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1477 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001478
Olli Etuaho2935c582015-04-08 14:32:06 +03001479 TVariable *variable = nullptr;
1480 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001481 recover();
1482
Olli Etuahoe7847b02015-03-16 11:56:12 +02001483 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001484 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001485 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001486
Olli Etuahoe7847b02015-03-16 11:56:12 +02001487 return intermediate.makeAggregate(symbol, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001488}
1489
Jamie Madill06145232015-05-13 13:10:01 -04001490TIntermAggregate *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001491 const TSourceLoc &identifierLocation,
1492 const TString &identifier,
1493 const TSourceLoc &initLocation,
1494 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001495{
Olli Etuahofa33d582015-04-09 14:33:12 +03001496 mDeferredSingleDeclarationErrorCheck = false;
1497
1498 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001499 recover();
1500
Olli Etuahoe7847b02015-03-16 11:56:12 +02001501 TIntermNode *intermNode = nullptr;
1502 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04001503 {
1504 //
1505 // Build intermediate representation
1506 //
Olli Etuahoe7847b02015-03-16 11:56:12 +02001507 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001508 }
1509 else
1510 {
1511 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001512 return nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001513 }
1514}
1515
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001516TIntermAggregate *TParseContext::parseSingleArrayInitDeclaration(TPublicType &publicType,
1517 const TSourceLoc &identifierLocation,
1518 const TString &identifier,
1519 const TSourceLoc &indexLocation,
1520 TIntermTyped *indexExpression,
1521 const TSourceLoc &initLocation,
1522 TIntermTyped *initializer)
1523{
1524 mDeferredSingleDeclarationErrorCheck = false;
1525
1526 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1527 recover();
1528
1529 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1530 {
1531 recover();
1532 }
1533
1534 TPublicType arrayType(publicType);
1535
Olli Etuaho376f1b52015-04-13 13:23:41 +03001536 int size = 0;
1537 // If indexExpression is nullptr, then the array will eventually get its size implicitly from the initializer.
1538 if (indexExpression != nullptr && arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001539 {
1540 recover();
1541 }
1542 // Make the type an array even if size check failed.
1543 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1544 arrayType.setArraySize(size);
1545
1546 // initNode will correspond to the whole of "type b[n] = initializer".
1547 TIntermNode *initNode = nullptr;
1548 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1549 {
1550 return initNode ? intermediate.makeAggregate(initNode, initLocation) : nullptr;
1551 }
1552 else
1553 {
1554 recover();
1555 return nullptr;
1556 }
1557}
1558
Olli Etuahoe7847b02015-03-16 11:56:12 +02001559TIntermAggregate *TParseContext::parseInvariantDeclaration(const TSourceLoc &invariantLoc,
Jamie Madill47e3ec02014-08-20 16:38:33 -04001560 const TSourceLoc &identifierLoc,
1561 const TString *identifier,
1562 const TSymbol *symbol)
1563{
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001564 // invariant declaration
Jamie Madill47e3ec02014-08-20 16:38:33 -04001565 if (globalErrorCheck(invariantLoc, symbolTable.atGlobalLevel(), "invariant varying"))
1566 {
1567 recover();
1568 }
1569
1570 if (!symbol)
1571 {
1572 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
1573 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001574 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001575 }
1576 else
1577 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001578 const TString kGlFrontFacing("gl_FrontFacing");
1579 if (*identifier == kGlFrontFacing)
1580 {
1581 error(identifierLoc, "identifier should not be declared as invariant", identifier->c_str());
1582 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001583 return nullptr;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001584 }
Jamie Madill2c433252014-12-03 12:36:54 -05001585 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001586 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1587 ASSERT(variable);
1588 const TType &type = variable->getType();
1589 TIntermSymbol *intermSymbol = intermediate.addSymbol(variable->getUniqueId(),
1590 *identifier, type, identifierLoc);
1591
1592 TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc);
1593 aggregate->setOp(EOpInvariantDeclaration);
1594 return aggregate;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001595 }
1596}
1597
Olli Etuahoe7847b02015-03-16 11:56:12 +02001598TIntermAggregate *TParseContext::parseDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1599 const TSourceLoc &identifierLocation, const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001600{
Olli Etuahofa33d582015-04-09 14:33:12 +03001601 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1602 if (mDeferredSingleDeclarationErrorCheck)
1603 {
1604 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1605 recover();
1606 mDeferredSingleDeclarationErrorCheck = false;
1607 }
1608
Jamie Madill0bd18df2013-06-20 11:55:52 -04001609 if (locationDeclaratorListCheck(identifierLocation, publicType))
1610 recover();
1611
Olli Etuaho376f1b52015-04-13 13:23:41 +03001612 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001613 recover();
1614
Olli Etuaho2935c582015-04-08 14:32:06 +03001615 TVariable *variable = nullptr;
1616 if (!declareVariable(identifierLocation, identifier, TType(publicType), &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001617 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001618
1619 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1620 if (variable && symbol)
Jamie Madill502d66f2013-06-20 11:55:52 -04001621 symbol->setId(variable->getUniqueId());
1622
Olli Etuahoe7847b02015-03-16 11:56:12 +02001623 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001624}
1625
Olli Etuahoe7847b02015-03-16 11:56:12 +02001626TIntermAggregate *TParseContext::parseArrayDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1627 const TSourceLoc &identifierLocation, const TString &identifier,
1628 const TSourceLoc &arrayLocation, TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001629{
Olli Etuahofa33d582015-04-09 14:33:12 +03001630 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1631 if (mDeferredSingleDeclarationErrorCheck)
1632 {
1633 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1634 recover();
1635 mDeferredSingleDeclarationErrorCheck = false;
1636 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001637
Jamie Madill0bd18df2013-06-20 11:55:52 -04001638 if (locationDeclaratorListCheck(identifierLocation, publicType))
1639 recover();
1640
Olli Etuaho376f1b52015-04-13 13:23:41 +03001641 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001642 recover();
1643
1644 if (arrayTypeErrorCheck(arrayLocation, publicType) || arrayQualifierErrorCheck(arrayLocation, publicType))
1645 {
1646 recover();
1647 }
Olli Etuaho93a90fd2015-04-07 18:14:07 +03001648 else
Jamie Madill502d66f2013-06-20 11:55:52 -04001649 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001650 TType arrayType = TType(publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04001651 int size;
1652 if (arraySizeErrorCheck(arrayLocation, indexExpression, size))
Olli Etuahoe7847b02015-03-16 11:56:12 +02001653 {
Jamie Madill502d66f2013-06-20 11:55:52 -04001654 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001655 }
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001656 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001657
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001658 TVariable *variable = nullptr;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001659 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001660 recover();
Jamie Madill502d66f2013-06-20 11:55:52 -04001661
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001662 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
1663 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001664 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001665
1666 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001667 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001668
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001669 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001670}
1671
Jamie Madill06145232015-05-13 13:10:01 -04001672TIntermAggregate *TParseContext::parseInitDeclarator(const TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001673 const TSourceLoc &identifierLocation, const TString &identifier,
1674 const TSourceLoc &initLocation, TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001675{
Olli Etuahofa33d582015-04-09 14:33:12 +03001676 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1677 if (mDeferredSingleDeclarationErrorCheck)
1678 {
1679 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1680 recover();
1681 mDeferredSingleDeclarationErrorCheck = false;
1682 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001683
Jamie Madill0bd18df2013-06-20 11:55:52 -04001684 if (locationDeclaratorListCheck(identifierLocation, publicType))
1685 recover();
1686
Olli Etuahoe7847b02015-03-16 11:56:12 +02001687 TIntermNode *intermNode = nullptr;
1688 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04001689 {
1690 //
1691 // build the intermediate representation
1692 //
1693 if (intermNode)
1694 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001695 return intermediate.growAggregate(aggregateDeclaration, intermNode, initLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001696 }
1697 else
1698 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001699 return aggregateDeclaration;
Jamie Madill502d66f2013-06-20 11:55:52 -04001700 }
1701 }
1702 else
1703 {
1704 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001705 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001706 }
1707}
1708
Jamie Madill06145232015-05-13 13:10:01 -04001709TIntermAggregate *TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001710 TIntermAggregate *aggregateDeclaration,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301711 const TSourceLoc &identifierLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001712 const TString &identifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301713 const TSourceLoc &indexLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001714 TIntermTyped *indexExpression,
1715 const TSourceLoc &initLocation, TIntermTyped *initializer)
1716{
1717 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1718 if (mDeferredSingleDeclarationErrorCheck)
1719 {
1720 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1721 recover();
1722 mDeferredSingleDeclarationErrorCheck = false;
1723 }
1724
1725 if (locationDeclaratorListCheck(identifierLocation, publicType))
1726 recover();
1727
1728 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1729 {
1730 recover();
1731 }
1732
1733 TPublicType arrayType(publicType);
1734
Olli Etuaho376f1b52015-04-13 13:23:41 +03001735 int size = 0;
1736 // If indexExpression is nullptr, then the array will eventually get its size implicitly from the initializer.
1737 if (indexExpression != nullptr && arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001738 {
1739 recover();
1740 }
1741 // Make the type an array even if size check failed.
1742 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1743 arrayType.setArraySize(size);
1744
1745 // initNode will correspond to the whole of "b[n] = initializer".
1746 TIntermNode *initNode = nullptr;
1747 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1748 {
1749 if (initNode)
1750 {
1751 return intermediate.growAggregate(aggregateDeclaration, initNode, initLocation);
1752 }
1753 else
1754 {
1755 return aggregateDeclaration;
1756 }
1757 }
1758 else
1759 {
1760 recover();
1761 return nullptr;
1762 }
1763}
1764
Jamie Madilla295edf2013-06-06 11:56:48 -04001765void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1766{
1767 if (typeQualifier.qualifier != EvqUniform)
1768 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301769 error(typeQualifier.line,
1770 "invalid qualifier:",
1771 getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
Jamie Madilla295edf2013-06-06 11:56:48 -04001772 recover();
1773 return;
1774 }
1775
1776 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1777 ASSERT(!layoutQualifier.isEmpty());
1778
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001779 if (mShaderVersion < 300)
Jamie Madilla295edf2013-06-06 11:56:48 -04001780 {
1781 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1782 recover();
1783 return;
1784 }
1785
1786 if (layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1787 {
1788 recover();
1789 return;
1790 }
1791
Jamie Madill099c0f32013-06-20 11:55:52 -04001792 if (layoutQualifier.matrixPacking != EmpUnspecified)
1793 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001794 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04001795 }
1796
Geoff Langc6856732014-02-11 09:38:55 -05001797 if (layoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04001798 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001799 mDefaultBlockStorage = layoutQualifier.blockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04001800 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001801}
1802
Jamie Madill06145232015-05-13 13:10:01 -04001803TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001804{
Jamie Madill06145232015-05-13 13:10:01 -04001805 TPublicType publicType = publicTypeIn;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001806 TOperator op = EOpNull;
1807 if (publicType.userDef)
1808 {
1809 op = EOpConstructStruct;
1810 }
1811 else
1812 {
1813 switch (publicType.type)
1814 {
1815 case EbtFloat:
1816 if (publicType.isMatrix())
1817 {
1818 // TODO: non-square matrices
1819 switch(publicType.getCols())
1820 {
Jamie Madill28b97422013-07-08 14:01:38 -04001821 case 2: op = EOpConstructMat2; break;
1822 case 3: op = EOpConstructMat3; break;
1823 case 4: op = EOpConstructMat4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001824 }
1825 }
1826 else
1827 {
1828 switch(publicType.getNominalSize())
1829 {
Jamie Madill28b97422013-07-08 14:01:38 -04001830 case 1: op = EOpConstructFloat; break;
1831 case 2: op = EOpConstructVec2; break;
1832 case 3: op = EOpConstructVec3; break;
1833 case 4: op = EOpConstructVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001834 }
1835 }
1836 break;
1837
1838 case EbtInt:
1839 switch(publicType.getNominalSize())
1840 {
Jamie Madill28b97422013-07-08 14:01:38 -04001841 case 1: op = EOpConstructInt; break;
1842 case 2: op = EOpConstructIVec2; break;
1843 case 3: op = EOpConstructIVec3; break;
1844 case 4: op = EOpConstructIVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001845 }
1846 break;
1847
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001848 case EbtUInt:
1849 switch(publicType.getNominalSize())
1850 {
Jamie Madill28b97422013-07-08 14:01:38 -04001851 case 1: op = EOpConstructUInt; break;
1852 case 2: op = EOpConstructUVec2; break;
1853 case 3: op = EOpConstructUVec3; break;
1854 case 4: op = EOpConstructUVec4; break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001855 }
1856 break;
1857
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001858 case EbtBool:
1859 switch(publicType.getNominalSize())
1860 {
Jamie Madill28b97422013-07-08 14:01:38 -04001861 case 1: op = EOpConstructBool; break;
1862 case 2: op = EOpConstructBVec2; break;
1863 case 3: op = EOpConstructBVec3; break;
1864 case 4: op = EOpConstructBVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001865 }
1866 break;
1867
1868 default: break;
1869 }
1870
1871 if (op == EOpNull)
1872 {
1873 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
1874 recover();
1875 publicType.type = EbtFloat;
1876 op = EOpConstructFloat;
1877 }
1878 }
1879
1880 TString tempString;
Dmitry Skiba7f17a502015-06-22 15:08:39 -07001881 const TType *type = new TType(publicType);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001882 return new TFunction(&tempString, type, op);
1883}
1884
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001885// This function is used to test for the correctness of the parameters passed to various constructor functions
Arun Patole7e7e68d2015-05-22 12:02:25 +05301886// and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001887//
1888// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1889//
Arun Patole7e7e68d2015-05-22 12:02:25 +05301890TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments, TType *type, TOperator op, TFunction *fnCall,
1891 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001892{
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001893 TIntermAggregate *aggregateArguments = arguments->getAsAggregate();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001894
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001895 if (!aggregateArguments)
1896 {
1897 aggregateArguments = new TIntermAggregate;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001898 aggregateArguments->getSequence()->push_back(arguments);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001899 }
1900
Olli Etuahof40319e2015-03-10 14:33:00 +02001901 if (type->isArray())
1902 {
1903 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of the array.
1904 TIntermSequence *args = aggregateArguments->getSequence();
1905 for (size_t i = 0; i < args->size(); i++)
1906 {
1907 const TType &argType = (*args)[i]->getAsTyped()->getType();
1908 // It has already been checked that the argument is not an array.
1909 ASSERT(!argType.isArray());
1910 if (!argType.sameElementType(*type))
1911 {
1912 error(line, "Array constructor argument has an incorrect type", "Error");
1913 recover();
1914 return nullptr;
1915 }
1916 }
1917 }
1918 else if (op == EOpConstructStruct)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001919 {
1920 const TFieldList &fields = type->getStruct()->fields();
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001921 TIntermSequence *args = aggregateArguments->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001922
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001923 for (size_t i = 0; i < fields.size(); i++)
1924 {
Nicolas Capensffd73872014-08-21 13:49:16 -04001925 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001926 {
1927 error(line, "Structure constructor arguments do not match structure fields", "Error");
1928 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001929
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001930 return 0;
1931 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001932 }
1933 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001934
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001935 // Turn the argument list itself into a constructor
Olli Etuaho21203702014-11-13 16:16:21 +02001936 TIntermAggregate *constructor = intermediate.setAggregateOperator(aggregateArguments, op, line);
1937 TIntermTyped *constConstructor = foldConstConstructor(constructor, *type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001938 if (constConstructor)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001939 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001940 return constConstructor;
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001941 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001942
Olli Etuaho21203702014-11-13 16:16:21 +02001943 // Structs should not be precision qualified, the individual members may be.
1944 // Built-in types on the other hand should be precision qualified.
1945 if (op != EOpConstructStruct)
1946 {
1947 constructor->setPrecisionFromChildren();
1948 type->setPrecision(constructor->getPrecision());
1949 }
1950
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001951 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001952}
1953
Arun Patole7e7e68d2015-05-22 12:02:25 +05301954TIntermTyped *TParseContext::foldConstConstructor(TIntermAggregate *aggrNode, const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001955{
Olli Etuahof40319e2015-03-10 14:33:00 +02001956 // TODO: Add support for folding array constructors
1957 bool canBeFolded = areAllChildConst(aggrNode) && !type.isArray();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001958 aggrNode->setType(type);
Arun Patole7e7e68d2015-05-22 12:02:25 +05301959 if (canBeFolded)
1960 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001961 bool returnVal = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301962 TConstantUnion *unionArray = new TConstantUnion[type.getObjectSize()];
1963 if (aggrNode->getSequence()->size() == 1)
1964 {
1965 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type,
1966 true);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001967 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301968 else
1969 {
1970 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(),
1971 type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001972 }
1973 if (returnVal)
1974 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001975
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001976 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1977 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001978
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001979 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001980}
1981
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001982//
1983// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1984// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1985// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
Arun Patole7e7e68d2015-05-22 12:02:25 +05301986// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001987// a constant matrix.
1988//
Arun Patole7e7e68d2015-05-22 12:02:25 +05301989TIntermTyped *TParseContext::addConstVectorNode(TVectorFields &fields, TIntermTyped *node, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001990{
Arun Patole7e7e68d2015-05-22 12:02:25 +05301991 TIntermTyped *typedNode;
1992 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001993
Jamie Madillb11e2482015-05-04 14:21:22 -04001994 const TConstantUnion *unionArray;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301995 if (tempConstantNode)
1996 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001997 unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001998
Arun Patole7e7e68d2015-05-22 12:02:25 +05301999 if (!unionArray)
2000 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002001 return node;
2002 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302003 }
2004 else
2005 { // The node has to be either a symbol node or an aggregate node or a tempConstant node, else, its an error
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002006 error(line, "Cannot offset into the vector", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002007 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002008
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002009 return 0;
2010 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002011
Arun Patole7e7e68d2015-05-22 12:02:25 +05302012 TConstantUnion *constArray = new TConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002013
Arun Patole7e7e68d2015-05-22 12:02:25 +05302014 for (int i = 0; i < fields.num; i++)
2015 {
2016 if (fields.offsets[i] >= node->getType().getNominalSize())
2017 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002018 std::stringstream extraInfoStream;
2019 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
2020 std::string extraInfo = extraInfoStream.str();
2021 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002022 recover();
2023 fields.offsets[i] = 0;
2024 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302025
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002026 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002027
Arun Patole7e7e68d2015-05-22 12:02:25 +05302028 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002029 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
2030 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002031}
2032
2033//
2034// This function returns the column being accessed from a constant matrix. The values are retrieved from
Arun Patole7e7e68d2015-05-22 12:02:25 +05302035// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
2036// to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002037// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
2038//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302039TIntermTyped *TParseContext::addConstMatrixNode(int index, TIntermTyped *node, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002040{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302041 TIntermTyped *typedNode;
2042 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002043
Arun Patole7e7e68d2015-05-22 12:02:25 +05302044 if (index >= node->getType().getCols())
2045 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002046 std::stringstream extraInfoStream;
2047 extraInfoStream << "matrix field selection out of range '" << index << "'";
2048 std::string extraInfo = extraInfoStream.str();
2049 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002050 recover();
2051 index = 0;
2052 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002053
Arun Patole7e7e68d2015-05-22 12:02:25 +05302054 if (tempConstantNode)
2055 {
Jamie Madillb11e2482015-05-04 14:21:22 -04002056 TConstantUnion *unionArray = tempConstantNode->getUnionArrayPointer();
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002057 int size = tempConstantNode->getType().getCols();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002058 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302059 }
2060 else
2061 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002062 error(line, "Cannot offset into the matrix", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002063 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002064
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002065 return 0;
2066 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002067
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002068 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002069}
2070
2071
2072//
2073// This function returns an element of an array accessed from a constant array. The values are retrieved from
Arun Patole7e7e68d2015-05-22 12:02:25 +05302074// the symbol table and parse-tree is built for the type of the element. The input
2075// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002076// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
2077//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302078TIntermTyped *TParseContext::addConstArrayNode(int index, TIntermTyped *node, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002079{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302080 TIntermTyped *typedNode;
2081 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002082 TType arrayElementType = node->getType();
2083 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002084
Arun Patole7e7e68d2015-05-22 12:02:25 +05302085 if (index >= node->getType().getArraySize())
2086 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002087 std::stringstream extraInfoStream;
2088 extraInfoStream << "array field selection out of range '" << index << "'";
2089 std::string extraInfo = extraInfoStream.str();
2090 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002091 recover();
2092 index = 0;
2093 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002094
Arun Patole7e7e68d2015-05-22 12:02:25 +05302095 if (tempConstantNode)
2096 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002097 size_t arrayElementSize = arrayElementType.getObjectSize();
Arun Patole7e7e68d2015-05-22 12:02:25 +05302098 TConstantUnion *unionArray = tempConstantNode->getUnionArrayPointer();
2099 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(),
2100 line);
2101 }
2102 else
2103 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002104 error(line, "Cannot offset into the array", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002105 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002106
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002107 return 0;
2108 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002109
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002110 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002111}
2112
2113
2114//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302115// This function returns the value of a particular field inside a constant structure from the symbol table.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002116// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
2117// function and returns the parse-tree with the values of the embedded/nested struct.
2118//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302119TIntermTyped *TParseContext::addConstStruct(const TString &identifier, TIntermTyped *node, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002120{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302121 const TFieldList &fields = node->getType().getStruct()->fields();
Jamie Madill94bf7f22013-07-08 13:31:15 -04002122 size_t instanceSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002123
Arun Patole7e7e68d2015-05-22 12:02:25 +05302124 for (size_t index = 0; index < fields.size(); ++index)
2125 {
2126 if (fields[index]->name() == identifier)
2127 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002128 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302129 }
2130 else
2131 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002132 instanceSize += fields[index]->type()->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002133 }
2134 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002135
Jamie Madill94bf7f22013-07-08 13:31:15 -04002136 TIntermTyped *typedNode;
2137 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
Arun Patole7e7e68d2015-05-22 12:02:25 +05302138 if (tempConstantNode)
2139 {
2140 TConstantUnion *constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002141
Arun Patole7e7e68d2015-05-22 12:02:25 +05302142 // type will be changed in the calling function
2143 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line);
2144 }
2145 else
2146 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002147 error(line, "Cannot offset into the structure", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002148 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002149
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002150 return 0;
2151 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002152
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002153 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002154}
2155
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002156//
2157// Interface/uniform blocks
2158//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302159TIntermAggregate *TParseContext::addInterfaceBlock(const TPublicType &typeQualifier, const TSourceLoc &nameLine,
2160 const TString &blockName, TFieldList *fieldList,
2161 const TString *instanceName, const TSourceLoc &instanceLine,
2162 TIntermTyped *arrayIndex, const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002163{
2164 if (reservedErrorCheck(nameLine, blockName))
2165 recover();
2166
2167 if (typeQualifier.qualifier != EvqUniform)
2168 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302169 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
2170 "interface blocks must be uniform");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002171 recover();
2172 }
2173
Jamie Madill099c0f32013-06-20 11:55:52 -04002174 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
2175 if (layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002176 {
2177 recover();
2178 }
2179
Jamie Madill099c0f32013-06-20 11:55:52 -04002180 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2181 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002182 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002183 }
2184
Jamie Madill1566ef72013-06-20 11:55:54 -04002185 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2186 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002187 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002188 }
2189
Arun Patole7e7e68d2015-05-22 12:02:25 +05302190 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2191 if (!symbolTable.declare(blockNameSymbol))
2192 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002193 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
2194 recover();
2195 }
2196
Jamie Madill98493dd2013-07-08 14:39:03 -04002197 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302198 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2199 {
2200 TField *field = (*fieldList)[memberIndex];
2201 TType *fieldType = field->type();
2202 if (IsSampler(fieldType->getBasicType()))
2203 {
2204 error(field->line(), "unsupported type", fieldType->getBasicString(),
2205 "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002206 recover();
2207 }
2208
Jamie Madill98493dd2013-07-08 14:39:03 -04002209 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002210 switch (qualifier)
2211 {
2212 case EvqGlobal:
2213 case EvqUniform:
2214 break;
2215 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04002216 error(field->line(), "invalid qualifier on interface block member", getQualifierString(qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002217 recover();
2218 break;
2219 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002220
2221 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002222 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
2223 if (layoutLocationErrorCheck(field->line(), fieldLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002224 {
2225 recover();
2226 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002227
Jamie Madill98493dd2013-07-08 14:39:03 -04002228 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002229 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302230 error(field->line(), "invalid layout qualifier:", getBlockStorageString(fieldLayoutQualifier.blockStorage),
2231 "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04002232 recover();
2233 }
2234
Jamie Madill98493dd2013-07-08 14:39:03 -04002235 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002236 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002237 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002238 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002239 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002240 {
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002241 warning(field->line(), "extraneous layout qualifier:",
2242 getMatrixPackingString(fieldLayoutQualifier.matrixPacking), "only has an effect on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04002243 }
2244
Jamie Madill98493dd2013-07-08 14:39:03 -04002245 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002246 }
2247
Jamie Madill98493dd2013-07-08 14:39:03 -04002248 // add array index
2249 int arraySize = 0;
2250 if (arrayIndex != NULL)
2251 {
2252 if (arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize))
2253 recover();
2254 }
2255
Arun Patole7e7e68d2015-05-22 12:02:25 +05302256 TInterfaceBlock *interfaceBlock = new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize,
2257 blockLayoutQualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002258 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier, arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002259
2260 TString symbolName = "";
2261 int symbolId = 0;
2262
Jamie Madill98493dd2013-07-08 14:39:03 -04002263 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002264 {
2265 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002266 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2267 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302268 TField *field = (*fieldList)[memberIndex];
2269 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002270
2271 // set parent pointer of the field variable
2272 fieldType->setInterfaceBlock(interfaceBlock);
2273
Arun Patole7e7e68d2015-05-22 12:02:25 +05302274 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002275 fieldVariable->setQualifier(typeQualifier.qualifier);
2276
Arun Patole7e7e68d2015-05-22 12:02:25 +05302277 if (!symbolTable.declare(fieldVariable))
2278 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002279 error(field->line(), "redefinition", field->name().c_str(), "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002280 recover();
2281 }
2282 }
2283 }
2284 else
2285 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002286 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302287 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002288 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002289
Arun Patole7e7e68d2015-05-22 12:02:25 +05302290 if (!symbolTable.declare(instanceTypeDef))
2291 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002292 error(instanceLine, "redefinition", instanceName->c_str(), "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002293 recover();
2294 }
2295
2296 symbolId = instanceTypeDef->getUniqueId();
2297 symbolName = instanceTypeDef->getName();
2298 }
2299
Arun Patole7e7e68d2015-05-22 12:02:25 +05302300 TIntermAggregate *aggregate = intermediate.makeAggregate(intermediate.addSymbol(symbolId, symbolName,
2301 interfaceBlockType,
2302 typeQualifier.line),
2303 nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002304 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04002305
2306 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002307 return aggregate;
2308}
2309
Arun Patole7e7e68d2015-05-22 12:02:25 +05302310bool TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002311{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002312 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002313
2314 // Embedded structure definitions are not supported per GLSL ES spec.
2315 // They aren't allowed in GLSL either, but we need to detect this here
2316 // so we don't rely on the GLSL compiler to catch it.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302317 if (mStructNestingLevel > 1)
2318 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002319 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002320 return true;
2321 }
2322
2323 return false;
2324}
2325
2326void TParseContext::exitStructDeclaration()
2327{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002328 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002329}
2330
2331namespace {
2332
2333const int kWebGLMaxStructNesting = 4;
2334
2335} // namespace
2336
Arun Patole7e7e68d2015-05-22 12:02:25 +05302337bool TParseContext::structNestingErrorCheck(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002338{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302339 if (!IsWebGLBasedSpec(mShaderSpec))
2340 {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002341 return false;
2342 }
2343
Arun Patole7e7e68d2015-05-22 12:02:25 +05302344 if (field.type()->getBasicType() != EbtStruct)
2345 {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002346 return false;
2347 }
2348
2349 // We're already inside a structure definition at this point, so add
2350 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302351 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2352 {
Jamie Madill41a49272014-03-18 16:10:13 -04002353 std::stringstream reasonStream;
2354 reasonStream << "Reference of struct type "
2355 << field.type()->getStruct()->name().c_str()
2356 << " exceeds maximum allowed nesting level of "
2357 << kWebGLMaxStructNesting;
2358 std::string reason = reasonStream.str();
2359 error(line, reason.c_str(), field.name().c_str(), "");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002360 return true;
2361 }
2362
2363 return false;
2364}
2365
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002366//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002367// Parse an array index expression
2368//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302369TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression, const TSourceLoc &location,
2370 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002371{
2372 TIntermTyped *indexedExpression = NULL;
2373
2374 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2375 {
2376 if (baseExpression->getAsSymbolNode())
2377 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302378 error(location, " left of '[' is not of type array, matrix, or vector ",
2379 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002380 }
2381 else
2382 {
2383 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2384 }
2385 recover();
2386 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002387
Jamie Madill21c1e452014-12-29 11:33:41 -05002388 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2389
2390 if (indexExpression->getQualifier() == EvqConst && indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002391 {
Jamie Madill21c1e452014-12-29 11:33:41 -05002392 int index = indexConstantUnion->getIConst(0);
Jamie Madill7164cf42013-07-08 13:30:59 -04002393 if (index < 0)
2394 {
2395 std::stringstream infoStream;
2396 infoStream << index;
2397 std::string info = infoStream.str();
2398 error(location, "negative index", info.c_str());
2399 recover();
2400 index = 0;
2401 }
2402 if (baseExpression->getType().getQualifier() == EvqConst)
2403 {
2404 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002405 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002406 // constant folding for arrays
2407 indexedExpression = addConstArrayNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002408 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002409 else if (baseExpression->isVector())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002410 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002411 // constant folding for vectors
2412 TVectorFields fields;
2413 fields.num = 1;
2414 fields.offsets[0] = index; // need to do it this way because v.xy sends fields integer array
2415 indexedExpression = addConstVectorNode(fields, baseExpression, location);
2416 }
2417 else if (baseExpression->isMatrix())
2418 {
2419 // constant folding for matrices
2420 indexedExpression = addConstMatrixNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002421 }
2422 }
2423 else
2424 {
Jamie Madillb11e2482015-05-04 14:21:22 -04002425 int safeIndex = -1;
2426
Jamie Madill7164cf42013-07-08 13:30:59 -04002427 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002428 {
Jamie Madill18464b52013-07-08 14:01:55 -04002429 if (index >= baseExpression->getType().getArraySize())
Jamie Madill7164cf42013-07-08 13:30:59 -04002430 {
2431 std::stringstream extraInfoStream;
2432 extraInfoStream << "array index out of range '" << index << "'";
2433 std::string extraInfo = extraInfoStream.str();
2434 error(location, "", "[", extraInfo.c_str());
2435 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002436 safeIndex = baseExpression->getType().getArraySize() - 1;
Jamie Madill7164cf42013-07-08 13:30:59 -04002437 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302438 else if (baseExpression->getQualifier() == EvqFragData && index > 0 &&
2439 !isExtensionEnabled("GL_EXT_draw_buffers"))
Jamie Madill5d287f52013-07-12 15:38:19 -04002440 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302441 error(location, "",
2442 "[", "array indexes for gl_FragData must be zero when GL_EXT_draw_buffers is disabled");
Jamie Madill5d287f52013-07-12 15:38:19 -04002443 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002444 safeIndex = 0;
Jamie Madill5d287f52013-07-12 15:38:19 -04002445 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002446 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302447 else if ((baseExpression->isVector() || baseExpression->isMatrix()) &&
2448 baseExpression->getType().getNominalSize() <= index)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002449 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002450 std::stringstream extraInfoStream;
2451 extraInfoStream << "field selection out of range '" << index << "'";
2452 std::string extraInfo = extraInfoStream.str();
2453 error(location, "", "[", extraInfo.c_str());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002454 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002455 safeIndex = baseExpression->getType().getNominalSize() - 1;
Jamie Madill46131a32013-06-20 11:55:50 -04002456 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002457
Jamie Madillb11e2482015-05-04 14:21:22 -04002458 // Don't modify the data of the previous constant union, because it can point
2459 // to builtins, like gl_MaxDrawBuffers. Instead use a new sanitized object.
2460 if (safeIndex != -1)
2461 {
2462 TConstantUnion *safeConstantUnion = new TConstantUnion();
2463 safeConstantUnion->setIConst(safeIndex);
2464 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
2465 }
2466
Jamie Madill7164cf42013-07-08 13:30:59 -04002467 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002468 }
2469 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002470 else
2471 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002472 if (baseExpression->isInterfaceBlock())
Jamie Madill7164cf42013-07-08 13:30:59 -04002473 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302474 error(location, "",
2475 "[", "array indexes for interface blocks arrays must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002476 recover();
2477 }
Jamie Madill19571812013-08-12 15:26:34 -07002478 else if (baseExpression->getQualifier() == EvqFragmentOut)
Jamie Madill7164cf42013-07-08 13:30:59 -04002479 {
Jamie Madill19571812013-08-12 15:26:34 -07002480 error(location, "", "[", "array indexes for fragment outputs must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002481 recover();
2482 }
2483
2484 indexedExpression = intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
2485 }
2486
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002487 if (indexedExpression == 0)
2488 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002489 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002490 unionArray->setFConst(0.0f);
2491 indexedExpression = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
2492 }
2493 else if (baseExpression->isArray())
2494 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002495 const TType &baseType = baseExpression->getType();
2496 if (baseType.getStruct())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002497 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002498 TType copyOfType(baseType.getStruct());
2499 indexedExpression->setType(copyOfType);
2500 }
2501 else if (baseType.isInterfaceBlock())
2502 {
2503 TType copyOfType(baseType.getInterfaceBlock(), baseType.getQualifier(), baseType.getLayoutQualifier(), 0);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002504 indexedExpression->setType(copyOfType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002505 }
2506 else
2507 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302508 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2509 EvqTemporary, static_cast<unsigned char>(baseExpression->getNominalSize()),
2510 static_cast<unsigned char>(baseExpression->getSecondarySize())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002511 }
2512
2513 if (baseExpression->getType().getQualifier() == EvqConst)
2514 {
2515 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2516 }
2517 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002518 else if (baseExpression->isMatrix())
2519 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002520 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302521 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2522 qualifier, static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002523 }
2524 else if (baseExpression->isVector())
2525 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002526 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2527 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002528 }
2529 else
2530 {
2531 indexedExpression->setType(baseExpression->getType());
2532 }
2533
2534 return indexedExpression;
2535}
2536
Arun Patole7e7e68d2015-05-22 12:02:25 +05302537TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression, const TSourceLoc &dotLocation,
2538 const TString &fieldString, const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002539{
2540 TIntermTyped *indexedExpression = NULL;
2541
2542 if (baseExpression->isArray())
2543 {
2544 error(fieldLocation, "cannot apply dot operator to an array", ".");
2545 recover();
2546 }
2547
2548 if (baseExpression->isVector())
2549 {
2550 TVectorFields fields;
2551 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields, fieldLocation))
2552 {
2553 fields.num = 1;
2554 fields.offsets[0] = 0;
2555 recover();
2556 }
2557
2558 if (baseExpression->getType().getQualifier() == EvqConst)
2559 {
2560 // constant folding for vector fields
2561 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
2562 if (indexedExpression == 0)
2563 {
2564 recover();
2565 indexedExpression = baseExpression;
2566 }
2567 else
2568 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302569 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2570 EvqConst, (unsigned char) (fieldString).size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002571 }
2572 }
2573 else
2574 {
2575 TString vectorString = fieldString;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302576 TIntermTyped *index = intermediate.addSwizzle(fields, fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002577 indexedExpression = intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302578 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2579 EvqTemporary, (unsigned char) vectorString.size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002580 }
2581 }
2582 else if (baseExpression->isMatrix())
2583 {
2584 TMatrixFields fields;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302585 if (!parseMatrixFields(fieldString, baseExpression->getCols(), baseExpression->getRows(), fields,
2586 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002587 {
2588 fields.wholeRow = false;
2589 fields.wholeCol = false;
2590 fields.row = 0;
2591 fields.col = 0;
2592 recover();
2593 }
2594
2595 if (fields.wholeRow || fields.wholeCol)
2596 {
2597 error(dotLocation, " non-scalar fields not implemented yet", ".");
2598 recover();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002599 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002600 unionArray->setIConst(0);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302601 TIntermTyped *index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst),
2602 fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002603 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302604 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2605 EvqTemporary, static_cast<unsigned char>(baseExpression->getCols()),
2606 static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002607 }
2608 else
2609 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002610 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002611 unionArray->setIConst(fields.col * baseExpression->getRows() + fields.row);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302612 TIntermTyped *index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst),
2613 fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002614 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
2615 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision()));
2616 }
2617 }
2618 else if (baseExpression->getBasicType() == EbtStruct)
2619 {
2620 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302621 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04002622 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002623 {
2624 error(dotLocation, "structure has no fields", "Internal Error");
2625 recover();
2626 indexedExpression = baseExpression;
2627 }
2628 else
2629 {
2630 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002631 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002632 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002633 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002634 {
2635 fieldFound = true;
2636 break;
2637 }
2638 }
2639 if (fieldFound)
2640 {
2641 if (baseExpression->getType().getQualifier() == EvqConst)
2642 {
2643 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2644 if (indexedExpression == 0)
2645 {
2646 recover();
2647 indexedExpression = baseExpression;
2648 }
2649 else
2650 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002651 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002652 // change the qualifier of the return type, not of the structure field
2653 // as the structure definition is shared between various structures.
2654 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2655 }
2656 }
2657 else
2658 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002659 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002660 unionArray->setIConst(i);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302661 TIntermTyped *index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002662 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002663 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002664 }
2665 }
2666 else
2667 {
2668 error(dotLocation, " no such field in structure", fieldString.c_str());
2669 recover();
2670 indexedExpression = baseExpression;
2671 }
2672 }
2673 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002674 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002675 {
2676 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302677 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04002678 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002679 {
2680 error(dotLocation, "interface block has no fields", "Internal Error");
2681 recover();
2682 indexedExpression = baseExpression;
2683 }
2684 else
2685 {
2686 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002687 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002688 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002689 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002690 {
2691 fieldFound = true;
2692 break;
2693 }
2694 }
2695 if (fieldFound)
2696 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002697 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002698 unionArray->setIConst(i);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302699 TIntermTyped *index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
2700 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index,
2701 dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002702 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002703 }
2704 else
2705 {
2706 error(dotLocation, " no such field in interface block", fieldString.c_str());
2707 recover();
2708 indexedExpression = baseExpression;
2709 }
2710 }
2711 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002712 else
2713 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002714 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002715 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302716 error(dotLocation, " field selection requires structure, vector, or matrix on left hand side",
2717 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002718 }
2719 else
2720 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302721 error(dotLocation,
2722 " field selection requires structure, vector, matrix, or interface block on left hand side",
2723 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002724 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002725 recover();
2726 indexedExpression = baseExpression;
2727 }
2728
2729 return indexedExpression;
2730}
2731
Arun Patole7e7e68d2015-05-22 12:02:25 +05302732TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002733{
Jamie Madilla5efff92013-06-06 11:56:47 -04002734 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002735
Jamie Madilla5efff92013-06-06 11:56:47 -04002736 qualifier.location = -1;
2737 qualifier.matrixPacking = EmpUnspecified;
2738 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002739
2740 if (qualifierType == "shared")
2741 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002742 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002743 }
2744 else if (qualifierType == "packed")
2745 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002746 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002747 }
2748 else if (qualifierType == "std140")
2749 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002750 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002751 }
2752 else if (qualifierType == "row_major")
2753 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002754 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002755 }
2756 else if (qualifierType == "column_major")
2757 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002758 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002759 }
2760 else if (qualifierType == "location")
2761 {
2762 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "location requires an argument");
2763 recover();
2764 }
2765 else
2766 {
2767 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
2768 recover();
2769 }
2770
Jamie Madilla5efff92013-06-06 11:56:47 -04002771 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002772}
2773
Arun Patole7e7e68d2015-05-22 12:02:25 +05302774TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc &qualifierTypeLine,
2775 const TString &intValueString, int intValue,
2776 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002777{
Jamie Madilla5efff92013-06-06 11:56:47 -04002778 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002779
Jamie Madilla5efff92013-06-06 11:56:47 -04002780 qualifier.location = -1;
2781 qualifier.matrixPacking = EmpUnspecified;
2782 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002783
2784 if (qualifierType != "location")
2785 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302786 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
2787 "only location may have arguments");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002788 recover();
2789 }
2790 else
2791 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002792 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002793 if (intValue < 0)
2794 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002795 error(intValueLine, "out of range:", intValueString.c_str(), "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002796 recover();
2797 }
2798 else
2799 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002800 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002801 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002802 }
2803
Jamie Madilla5efff92013-06-06 11:56:47 -04002804 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002805}
2806
Jamie Madilla5efff92013-06-06 11:56:47 -04002807TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002808{
Jamie Madilla5efff92013-06-06 11:56:47 -04002809 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002810
Jamie Madilla5efff92013-06-06 11:56:47 -04002811 if (rightQualifier.location != -1)
2812 {
2813 joinedQualifier.location = rightQualifier.location;
2814 }
2815 if (rightQualifier.matrixPacking != EmpUnspecified)
2816 {
2817 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
2818 }
2819 if (rightQualifier.blockStorage != EbsUnspecified)
2820 {
2821 joinedQualifier.blockStorage = rightQualifier.blockStorage;
2822 }
2823
2824 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002825}
2826
Arun Patole7e7e68d2015-05-22 12:02:25 +05302827TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc,
2828 TQualifier interpolationQualifier,
2829 const TSourceLoc &storageLoc,
2830 TQualifier storageQualifier)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002831{
2832 TQualifier mergedQualifier = EvqSmoothIn;
2833
Arun Patole7e7e68d2015-05-22 12:02:25 +05302834 if (storageQualifier == EvqFragmentIn)
2835 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002836 if (interpolationQualifier == EvqSmooth)
2837 mergedQualifier = EvqSmoothIn;
2838 else if (interpolationQualifier == EvqFlat)
2839 mergedQualifier = EvqFlatIn;
2840 else UNREACHABLE();
2841 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302842 else if (storageQualifier == EvqCentroidIn)
2843 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002844 if (interpolationQualifier == EvqSmooth)
2845 mergedQualifier = EvqCentroidIn;
2846 else if (interpolationQualifier == EvqFlat)
2847 mergedQualifier = EvqFlatIn;
2848 else UNREACHABLE();
2849 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302850 else if (storageQualifier == EvqVertexOut)
2851 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002852 if (interpolationQualifier == EvqSmooth)
2853 mergedQualifier = EvqSmoothOut;
2854 else if (interpolationQualifier == EvqFlat)
2855 mergedQualifier = EvqFlatOut;
2856 else UNREACHABLE();
2857 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302858 else if (storageQualifier == EvqCentroidOut)
2859 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002860 if (interpolationQualifier == EvqSmooth)
2861 mergedQualifier = EvqCentroidOut;
2862 else if (interpolationQualifier == EvqFlat)
2863 mergedQualifier = EvqFlatOut;
2864 else UNREACHABLE();
2865 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302866 else
2867 {
2868 error(interpolationLoc, "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier",
2869 getInterpolationString(interpolationQualifier));
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002870 recover();
2871
2872 mergedQualifier = storageQualifier;
2873 }
2874
2875 TPublicType type;
2876 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
2877 return type;
2878}
2879
Arun Patole7e7e68d2015-05-22 12:02:25 +05302880TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier, TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002881{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002882 if (voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier.type))
2883 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002884 recover();
2885 }
2886
Arun Patole7e7e68d2015-05-22 12:02:25 +05302887 for (unsigned int i = 0; i < fieldList->size(); ++i)
2888 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002889 //
2890 // Careful not to replace already known aspects of type, like array-ness
2891 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05302892 TType *type = (*fieldList)[i]->type();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002893 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002894 type->setPrimarySize(typeSpecifier.primarySize);
2895 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002896 type->setPrecision(typeSpecifier.precision);
2897 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002898 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002899
2900 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05302901 if (type->isArray())
2902 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002903 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
2904 recover();
2905 }
2906 if (typeSpecifier.array)
2907 type->setArraySize(typeSpecifier.arraySize);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302908 if (typeSpecifier.userDef)
2909 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002910 type->setStruct(typeSpecifier.userDef->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002911 }
2912
Arun Patole7e7e68d2015-05-22 12:02:25 +05302913 if (structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i]))
2914 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002915 recover();
2916 }
2917 }
2918
Jamie Madill98493dd2013-07-08 14:39:03 -04002919 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002920}
2921
Arun Patole7e7e68d2015-05-22 12:02:25 +05302922TPublicType TParseContext::addStructure(const TSourceLoc &structLine, const TSourceLoc &nameLine,
2923 const TString *structName, TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002924{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302925 TStructure *structure = new TStructure(structName, fieldList);
2926 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002927
Jamie Madill9b820842015-02-12 10:40:10 -05002928 // Store a bool in the struct if we're at global scope, to allow us to
2929 // skip the local struct scoping workaround in HLSL.
Jamie Madillb960cc42015-02-12 15:33:20 +00002930 structure->setUniqueId(TSymbolTable::nextUniqueId());
Jamie Madill9b820842015-02-12 10:40:10 -05002931 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04002932
Jamie Madill98493dd2013-07-08 14:39:03 -04002933 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002934 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002935 if (reservedErrorCheck(nameLine, *structName))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002936 {
2937 recover();
2938 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302939 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
2940 if (!symbolTable.declare(userTypeDef))
2941 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002942 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002943 recover();
2944 }
2945 }
2946
2947 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04002948 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002949 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002950 const TField &field = *(*fieldList)[typeListIndex];
2951 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002952 switch (qualifier)
2953 {
2954 case EvqGlobal:
2955 case EvqTemporary:
2956 break;
2957 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04002958 error(field.line(), "invalid qualifier on struct member", getQualifierString(qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002959 recover();
2960 break;
2961 }
2962 }
2963
2964 TPublicType publicType;
2965 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04002966 publicType.userDef = structureType;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002967 exitStructDeclaration();
2968
2969 return publicType;
2970}
2971
Olli Etuahoa3a36662015-02-17 13:46:51 +02002972TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init, TIntermAggregate *statementList, const TSourceLoc &loc)
2973{
Olli Etuaho53f076f2015-02-20 10:55:14 +02002974 TBasicType switchType = init->getBasicType();
2975 if ((switchType != EbtInt && switchType != EbtUInt) ||
2976 init->isMatrix() ||
2977 init->isArray() ||
2978 init->isVector())
2979 {
2980 error(init->getLine(), "init-expression in a switch statement must be a scalar integer", "switch");
2981 recover();
2982 return nullptr;
2983 }
2984
Olli Etuahoac5274d2015-02-20 10:19:08 +02002985 if (statementList)
2986 {
2987 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
2988 {
2989 recover();
2990 return nullptr;
2991 }
2992 }
2993
Olli Etuahoa3a36662015-02-17 13:46:51 +02002994 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
2995 if (node == nullptr)
2996 {
2997 error(loc, "erroneous switch statement", "switch");
2998 recover();
2999 return nullptr;
3000 }
3001 return node;
3002}
3003
3004TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3005{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003006 if (mSwitchNestingLevel == 0)
3007 {
3008 error(loc, "case labels need to be inside switch statements", "case");
3009 recover();
3010 return nullptr;
3011 }
3012 if (condition == nullptr)
3013 {
3014 error(loc, "case label must have a condition", "case");
3015 recover();
3016 return nullptr;
3017 }
3018 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
3019 condition->isMatrix() ||
3020 condition->isArray() ||
3021 condition->isVector())
3022 {
3023 error(condition->getLine(), "case label must be a scalar integer", "case");
3024 recover();
3025 }
3026 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
3027 if (conditionConst == nullptr)
3028 {
3029 error(condition->getLine(), "case label must be constant", "case");
3030 recover();
3031 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003032 TIntermCase *node = intermediate.addCase(condition, loc);
3033 if (node == nullptr)
3034 {
3035 error(loc, "erroneous case statement", "case");
3036 recover();
3037 return nullptr;
3038 }
3039 return node;
3040}
3041
3042TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3043{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003044 if (mSwitchNestingLevel == 0)
3045 {
3046 error(loc, "default labels need to be inside switch statements", "default");
3047 recover();
3048 return nullptr;
3049 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003050 TIntermCase *node = intermediate.addCase(nullptr, loc);
3051 if (node == nullptr)
3052 {
3053 error(loc, "erroneous default statement", "default");
3054 recover();
3055 return nullptr;
3056 }
3057 return node;
3058}
3059
Olli Etuahof6c694b2015-03-26 14:50:53 +02003060TIntermTyped *TParseContext::createUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc,
3061 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003062{
3063 if (child == nullptr)
3064 {
3065 return nullptr;
3066 }
3067
3068 switch (op)
3069 {
3070 case EOpLogicalNot:
3071 if (child->getBasicType() != EbtBool ||
3072 child->isMatrix() ||
3073 child->isArray() ||
3074 child->isVector())
3075 {
3076 return nullptr;
3077 }
3078 break;
3079 case EOpBitwiseNot:
3080 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3081 child->isMatrix() ||
3082 child->isArray())
3083 {
3084 return nullptr;
3085 }
3086 break;
3087 case EOpPostIncrement:
3088 case EOpPreIncrement:
3089 case EOpPostDecrement:
3090 case EOpPreDecrement:
3091 case EOpNegative:
3092 case EOpPositive:
3093 if (child->getBasicType() == EbtStruct ||
Olli Etuahodca3e792015-03-26 13:24:04 +02003094 child->getBasicType() == EbtBool ||
Olli Etuaho69c11b52015-03-26 12:59:00 +02003095 child->isArray())
3096 {
3097 return nullptr;
3098 }
Olli Etuahodca3e792015-03-26 13:24:04 +02003099 // Operators for built-ins are already type checked against their prototype.
Olli Etuaho69c11b52015-03-26 12:59:00 +02003100 default:
3101 break;
3102 }
3103
Olli Etuahof6c694b2015-03-26 14:50:53 +02003104 return intermediate.addUnaryMath(op, child, loc, funcReturnType);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003105}
3106
Olli Etuaho09b22472015-02-11 11:47:26 +02003107TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3108{
Olli Etuahof6c694b2015-03-26 14:50:53 +02003109 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003110 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003111 {
3112 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
3113 recover();
3114 return child;
3115 }
3116 return node;
3117}
3118
3119TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3120{
3121 if (lValueErrorCheck(loc, GetOperatorString(op), child))
3122 recover();
3123 return addUnaryMath(op, child, loc);
3124}
3125
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003126bool TParseContext::binaryOpCommonCheck(TOperator op, TIntermTyped *left, TIntermTyped *right,
Olli Etuahod6b14282015-03-17 14:31:35 +02003127 const TSourceLoc &loc)
3128{
3129 if (left->isArray() || right->isArray())
3130 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003131 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003132 {
3133 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3134 return false;
3135 }
3136
3137 if (left->isArray() != right->isArray())
3138 {
3139 error(loc, "array / non-array mismatch", GetOperatorString(op));
3140 return false;
3141 }
3142
3143 switch (op)
3144 {
3145 case EOpEqual:
3146 case EOpNotEqual:
3147 case EOpAssign:
3148 case EOpInitialize:
3149 break;
3150 default:
3151 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3152 return false;
3153 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003154 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003155 if (left->getArraySize() != right->getArraySize())
3156 {
3157 error(loc, "array size mismatch", GetOperatorString(op));
3158 return false;
3159 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003160 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003161
3162 // Check ops which require integer / ivec parameters
3163 bool isBitShift = false;
3164 switch (op)
3165 {
3166 case EOpBitShiftLeft:
3167 case EOpBitShiftRight:
3168 case EOpBitShiftLeftAssign:
3169 case EOpBitShiftRightAssign:
3170 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3171 // check that the basic type is an integer type.
3172 isBitShift = true;
3173 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3174 {
3175 return false;
3176 }
3177 break;
3178 case EOpBitwiseAnd:
3179 case EOpBitwiseXor:
3180 case EOpBitwiseOr:
3181 case EOpBitwiseAndAssign:
3182 case EOpBitwiseXorAssign:
3183 case EOpBitwiseOrAssign:
3184 // It is enough to check the type of only one operand, since later it
3185 // is checked that the operand types match.
3186 if (!IsInteger(left->getBasicType()))
3187 {
3188 return false;
3189 }
3190 break;
3191 default:
3192 break;
3193 }
3194
3195 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3196 // So the basic type should usually match.
3197 if (!isBitShift && left->getBasicType() != right->getBasicType())
3198 {
3199 return false;
3200 }
3201
Olli Etuaho9dd217b2015-03-20 14:24:31 +02003202 // Check that type sizes match exactly on ops that require that.
Olli Etuahoff699002015-03-23 14:38:42 +02003203 // Also check restrictions for structs that contain arrays or samplers.
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003204 switch(op)
3205 {
3206 case EOpAssign:
3207 case EOpInitialize:
3208 case EOpEqual:
3209 case EOpNotEqual:
Olli Etuaho9dd217b2015-03-20 14:24:31 +02003210 // ESSL 1.00 sections 5.7, 5.8, 5.9
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003211 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
Olli Etuaho9dd217b2015-03-20 14:24:31 +02003212 {
3213 error(loc, "undefined operation for structs containing arrays", GetOperatorString(op));
3214 return false;
3215 }
Olli Etuahoff699002015-03-23 14:38:42 +02003216 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3217 // we interpret the spec so that this extends to structs containing samplers,
3218 // similarly to ESSL 1.00 spec.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003219 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
Olli Etuahoff699002015-03-23 14:38:42 +02003220 left->getType().isStructureContainingSamplers())
3221 {
3222 error(loc, "undefined operation for structs containing samplers", GetOperatorString(op));
3223 return false;
3224 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003225 case EOpLessThan:
3226 case EOpGreaterThan:
3227 case EOpLessThanEqual:
3228 case EOpGreaterThanEqual:
3229 if ((left->getNominalSize() != right->getNominalSize()) ||
3230 (left->getSecondarySize() != right->getSecondarySize()))
3231 {
3232 return false;
3233 }
3234 default:
3235 break;
3236 }
3237
Olli Etuahod6b14282015-03-17 14:31:35 +02003238 return true;
3239}
3240
Olli Etuahofc1806e2015-03-17 13:03:11 +02003241TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op, TIntermTyped *left, TIntermTyped *right,
3242 const TSourceLoc &loc)
3243{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003244 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003245 return nullptr;
3246
Olli Etuahofc1806e2015-03-17 13:03:11 +02003247 switch (op)
3248 {
3249 case EOpEqual:
3250 case EOpNotEqual:
Olli Etuahofc1806e2015-03-17 13:03:11 +02003251 break;
3252 case EOpLessThan:
3253 case EOpGreaterThan:
3254 case EOpLessThanEqual:
3255 case EOpGreaterThanEqual:
Olli Etuahod6b14282015-03-17 14:31:35 +02003256 ASSERT(!left->isArray() && !right->isArray());
3257 if (left->isMatrix() || left->isVector() ||
Olli Etuahofc1806e2015-03-17 13:03:11 +02003258 left->getBasicType() == EbtStruct)
3259 {
3260 return nullptr;
3261 }
3262 break;
3263 case EOpLogicalOr:
3264 case EOpLogicalXor:
3265 case EOpLogicalAnd:
Olli Etuahod6b14282015-03-17 14:31:35 +02003266 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003267 if (left->getBasicType() != EbtBool ||
Olli Etuahod6b14282015-03-17 14:31:35 +02003268 left->isMatrix() || left->isVector())
Olli Etuahofc1806e2015-03-17 13:03:11 +02003269 {
3270 return nullptr;
3271 }
3272 break;
3273 case EOpAdd:
3274 case EOpSub:
3275 case EOpDiv:
3276 case EOpMul:
Olli Etuahod6b14282015-03-17 14:31:35 +02003277 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003278 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool)
3279 {
3280 return nullptr;
3281 }
3282 break;
3283 case EOpIMod:
Olli Etuahod6b14282015-03-17 14:31:35 +02003284 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003285 // Note that this is only for the % operator, not for mod()
3286 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
3287 {
3288 return nullptr;
3289 }
3290 break;
3291 // Note that for bitwise ops, type checking is done in promote() to
3292 // share code between ops and compound assignment
3293 default:
3294 break;
3295 }
3296
Olli Etuahofc1806e2015-03-17 13:03:11 +02003297 return intermediate.addBinaryMath(op, left, right, loc);
3298}
3299
Olli Etuaho09b22472015-02-11 11:47:26 +02003300TIntermTyped *TParseContext::addBinaryMath(TOperator op, TIntermTyped *left, TIntermTyped *right,
3301 const TSourceLoc &loc)
3302{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003303 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003304 if (node == 0)
3305 {
3306 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(), right->getCompleteString());
3307 recover();
3308 return left;
3309 }
3310 return node;
3311}
3312
3313TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op, TIntermTyped *left, TIntermTyped *right,
3314 const TSourceLoc &loc)
3315{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003316 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003317 if (node == 0)
3318 {
3319 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(), right->getCompleteString());
3320 recover();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003321 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02003322 unionArray->setBConst(false);
3323 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), loc);
3324 }
3325 return node;
3326}
3327
Olli Etuahod6b14282015-03-17 14:31:35 +02003328TIntermTyped *TParseContext::createAssign(TOperator op, TIntermTyped *left, TIntermTyped *right,
3329 const TSourceLoc &loc)
3330{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003331 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003332 {
3333 return intermediate.addAssign(op, left, right, loc);
3334 }
3335 return nullptr;
3336}
3337
3338TIntermTyped *TParseContext::addAssign(TOperator op, TIntermTyped *left, TIntermTyped *right,
3339 const TSourceLoc &loc)
3340{
3341 TIntermTyped *node = createAssign(op, left, right, loc);
3342 if (node == nullptr)
3343 {
3344 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
3345 recover();
3346 return left;
3347 }
3348 return node;
3349}
3350
Olli Etuaho49300862015-02-20 14:54:49 +02003351TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3352{
3353 switch (op)
3354 {
3355 case EOpContinue:
3356 if (mLoopNestingLevel <= 0)
3357 {
3358 error(loc, "continue statement only allowed in loops", "");
3359 recover();
3360 }
3361 break;
3362 case EOpBreak:
Olli Etuaho53f076f2015-02-20 10:55:14 +02003363 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
Olli Etuaho49300862015-02-20 14:54:49 +02003364 {
Olli Etuaho53f076f2015-02-20 10:55:14 +02003365 error(loc, "break statement only allowed in loops and switch statements", "");
Olli Etuaho49300862015-02-20 14:54:49 +02003366 recover();
3367 }
3368 break;
3369 case EOpReturn:
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003370 if (mCurrentFunctionType->getBasicType() != EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02003371 {
3372 error(loc, "non-void function must return a value", "return");
3373 recover();
3374 }
3375 break;
3376 default:
3377 // No checks for discard
3378 break;
3379 }
3380 return intermediate.addBranch(op, loc);
3381}
3382
3383TIntermBranch *TParseContext::addBranch(TOperator op, TIntermTyped *returnValue, const TSourceLoc &loc)
3384{
3385 ASSERT(op == EOpReturn);
3386 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003387 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02003388 {
3389 error(loc, "void function cannot return a value", "return");
3390 recover();
3391 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003392 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02003393 {
3394 error(loc, "function return is not matching type:", "return");
3395 recover();
3396 }
3397 return intermediate.addBranch(op, returnValue, loc);
3398}
3399
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003400TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall, TIntermNode *paramNode, TIntermNode *thisNode,
3401 const TSourceLoc &loc, bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003402{
3403 *fatalError = false;
3404 TOperator op = fnCall->getBuiltInOp();
3405 TIntermTyped *callNode = nullptr;
3406
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003407 if (thisNode != nullptr)
3408 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003409 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho96e67382015-04-23 14:27:02 +03003410 int arraySize = 0;
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003411 TIntermTyped *typedThis = thisNode->getAsTyped();
3412 if (fnCall->getName() != "length")
3413 {
3414 error(loc, "invalid method", fnCall->getName().c_str());
3415 recover();
3416 }
3417 else if (paramNode != nullptr)
3418 {
3419 error(loc, "method takes no parameters", "length");
3420 recover();
3421 }
3422 else if (typedThis == nullptr || !typedThis->isArray())
3423 {
3424 error(loc, "length can only be called on arrays", "length");
3425 recover();
3426 }
3427 else
3428 {
Olli Etuaho96e67382015-04-23 14:27:02 +03003429 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03003430 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003431 {
Olli Etuaho39282e12015-04-23 15:41:48 +03003432 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003433 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03003434 // (func()).length()
3435 // (int[3](0, 1, 2)).length()
3436 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid expression.
3437 // It allows "An array name with the length method applied" in contrast to GLSL 4.4 spec section 5.9
3438 // which allows "An array, vector or matrix expression with the length method applied".
3439 error(loc, "length can only be called on array names, not on array expressions", "length");
3440 recover();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003441 }
3442 }
Olli Etuaho96e67382015-04-23 14:27:02 +03003443 unionArray->setIConst(arraySize);
3444 callNode = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003445 }
3446 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003447 {
3448 //
3449 // Then this should be a constructor.
3450 // Don't go through the symbol table for constructors.
3451 // Their parameters will be verified algorithmically.
3452 //
3453 TType type(EbtVoid, EbpUndefined); // use this to get the type back
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003454 if (!constructorErrorCheck(loc, paramNode, *fnCall, op, &type))
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003455 {
3456 //
3457 // It's a constructor, of type 'type'.
3458 //
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003459 callNode = addConstructor(paramNode, &type, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003460 }
Olli Etuaho72ba85b2015-03-04 14:23:26 +02003461
3462 if (callNode == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003463 {
3464 recover();
3465 callNode = intermediate.setAggregateOperator(nullptr, op, loc);
3466 }
3467 callNode->setType(type);
3468 }
3469 else
3470 {
3471 //
3472 // Not a constructor. Find it in the symbol table.
3473 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303474 const TFunction *fnCandidate;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003475 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003476 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003477 if (fnCandidate)
3478 {
3479 //
3480 // A declared function.
3481 //
3482 if (builtIn && !fnCandidate->getExtension().empty() &&
3483 extensionErrorCheck(loc, fnCandidate->getExtension()))
3484 {
3485 recover();
3486 }
3487 op = fnCandidate->getBuiltInOp();
3488 if (builtIn && op != EOpNull)
3489 {
3490 //
3491 // A function call mapped to a built-in operation.
3492 //
3493 if (fnCandidate->getParamCount() == 1)
3494 {
3495 //
3496 // Treat it like a built-in unary operator.
3497 //
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003498 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc, &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003499 if (callNode == nullptr)
3500 {
3501 std::stringstream extraInfoStream;
3502 extraInfoStream << "built in unary operator function. Type: "
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003503 << static_cast<TIntermTyped*>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003504 std::string extraInfo = extraInfoStream.str();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003505 error(paramNode->getLine(), " wrong operand type", "Internal Error", extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003506 *fatalError = true;
3507 return nullptr;
3508 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003509 }
3510 else
3511 {
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003512 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003513 aggregate->setType(fnCandidate->getReturnType());
3514 aggregate->setPrecisionFromChildren();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003515
3516 // Some built-in functions have out parameters too.
3517 functionCallLValueErrorCheck(fnCandidate, aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05303518
3519 // See if we can constant fold a built-in.
Olli Etuahob43846e2015-06-02 18:18:57 +03003520 TIntermTyped *foldedNode = intermediate.foldAggregateBuiltIn(aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05303521 if (foldedNode)
3522 {
Arun Patole274f0702015-05-05 13:33:30 +05303523 callNode = foldedNode;
3524 }
Olli Etuahob43846e2015-06-02 18:18:57 +03003525 else
3526 {
3527 callNode = aggregate;
3528 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003529 }
3530 }
3531 else
3532 {
3533 // This is a real function call
3534
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003535 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003536 aggregate->setType(fnCandidate->getReturnType());
3537
3538 // this is how we know whether the given function is a builtIn function or a user defined function
3539 // if builtIn == false, it's a userDefined -> could be an overloaded builtIn function also
3540 // if builtIn == true, it's definitely a builtIn function with EOpNull
3541 if (!builtIn)
3542 aggregate->setUserDefined();
3543 aggregate->setName(fnCandidate->getMangledName());
Corentin Wallez71d147f2015-02-11 11:15:24 -08003544 aggregate->setFunctionId(fnCandidate->getUniqueId());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003545
3546 // This needs to happen after the name is set
3547 if (builtIn)
3548 aggregate->setBuiltInFunctionPrecision();
3549
3550 callNode = aggregate;
3551
3552 functionCallLValueErrorCheck(fnCandidate, aggregate);
3553 }
3554 }
3555 else
3556 {
3557 // error message was put out by findFunction()
3558 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003559 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003560 unionArray->setFConst(0.0f);
3561 callNode = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpUndefined, EvqConst), loc);
3562 recover();
3563 }
3564 }
3565 delete fnCall;
3566 return callNode;
3567}
3568
Olli Etuaho52901742015-04-15 13:42:45 +03003569TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond, TIntermTyped *trueBlock, TIntermTyped *falseBlock,
3570 const TSourceLoc &loc)
3571{
3572 if (boolErrorCheck(loc, cond))
3573 recover();
3574
3575 if (trueBlock->getType() != falseBlock->getType())
3576 {
3577 binaryOpError(loc, ":", trueBlock->getCompleteString(), falseBlock->getCompleteString());
3578 recover();
3579 return falseBlock;
3580 }
Olli Etuahoa2d53032015-04-15 14:14:44 +03003581 // ESSL1 sections 5.2 and 5.7:
3582 // ESSL3 section 5.7:
3583 // Ternary operator is not among the operators allowed for structures/arrays.
3584 if (trueBlock->isArray() || trueBlock->getBasicType() == EbtStruct)
3585 {
3586 error(loc, "ternary operator is not allowed for structures or arrays", ":");
3587 recover();
3588 return falseBlock;
3589 }
Olli Etuaho52901742015-04-15 13:42:45 +03003590 return intermediate.addSelection(cond, trueBlock, falseBlock, loc);
3591}
Olli Etuaho49300862015-02-20 14:54:49 +02003592
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003593//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003594// Parse an array of strings using yyparse.
3595//
3596// Returns 0 for success.
3597//
Arun Patole7e7e68d2015-05-22 12:02:25 +05303598int PaParseStrings(size_t count, const char *const string[], const int length[],
3599 TParseContext *context)
3600{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003601 if ((count == 0) || (string == NULL))
3602 return 1;
3603
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003604 if (glslang_initialize(context))
3605 return 1;
3606
alokp@chromium.org408c45e2012-04-05 15:54:43 +00003607 int error = glslang_scan(count, string, length, context);
3608 if (!error)
3609 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003610
alokp@chromium.org73bc2982012-06-19 18:48:05 +00003611 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00003612
alokp@chromium.org6b495712012-06-29 00:06:58 +00003613 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003614}
3615
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003616
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00003617