blob: d9806b569e5be39a61cca14aef93439e14bde915 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Jamie Madill6b9cb252013-10-17 10:45:47 -04007#include "compiler/translator/ParseContext.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +00008
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00009#include <stdarg.h>
apatrick@chromium.org8187fa82010-06-15 22:09:28 +000010#include <stdio.h>
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000011
daniel@transgaming.comb401a922012-10-26 18:58:24 +000012#include "compiler/preprocessor/SourceLocation.h"
Dmitry Skiba01971112015-07-10 14:54:00 -040013#include "compiler/translator/Cache.h"
Olli Etuahoac5274d2015-02-20 10:19:08 +020014#include "compiler/translator/glslang.h"
15#include "compiler/translator/ValidateSwitch.h"
Olli Etuahob0c645e2015-05-12 14:25:36 +030016#include "compiler/translator/ValidateGlobalInitializer.h"
Olli Etuaho37ad4742015-04-27 13:18:50 +030017#include "compiler/translator/util.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000018
alokp@chromium.org8b851c62012-06-15 16:25:11 +000019///////////////////////////////////////////////////////////////////////
20//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000021// Sub- vector and matrix fields
22//
23////////////////////////////////////////////////////////////////////////
24
25//
26// Look at a '.' field selector string and change it into offsets
27// for a vector.
28//
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:
Alexis Hetu07e57df2015-06-16 16:55:52 -0400539 case EOpConstructMat2x3:
540 case EOpConstructMat2x4:
541 case EOpConstructMat3x2:
Arun Patole7e7e68d2015-05-22 12:02:25 +0530542 case EOpConstructMat3:
Alexis Hetu07e57df2015-06-16 16:55:52 -0400543 case EOpConstructMat3x4:
544 case EOpConstructMat4x2:
545 case EOpConstructMat4x3:
Arun Patole7e7e68d2015-05-22 12:02:25 +0530546 case EOpConstructMat4:
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000547 constructingMatrix = true;
548 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530549 default:
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000550 break;
551 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000552
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000553 //
554 // Note: It's okay to have too many components available, but not okay to have unused
555 // arguments. 'full' will go to true when enough args have been seen. If we loop
556 // again, there is an extra argument, so 'overfull' will become true.
557 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000558
Jamie Madill94bf7f22013-07-08 13:31:15 -0400559 size_t size = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000560 bool constType = true;
561 bool full = false;
562 bool overFull = false;
563 bool matrixInMatrix = false;
564 bool arrayArg = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530565 for (size_t i = 0; i < function.getParamCount(); ++i)
566 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700567 const TConstParameter &param = function.getParam(i);
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000568 size += param.type->getObjectSize();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530569
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000570 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000571 matrixInMatrix = true;
572 if (full)
573 overFull = true;
574 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
575 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000576 if (param.type->getQualifier() != EvqConst)
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000577 constType = false;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000578 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000579 arrayArg = true;
580 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530581
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000582 if (constType)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000583 type->setQualifier(EvqConst);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000584
Olli Etuaho376f1b52015-04-13 13:23:41 +0300585 if (type->isArray())
586 {
587 if (type->isUnsizedArray())
588 {
589 type->setArraySize(function.getParamCount());
590 }
591 else if (static_cast<size_t>(type->getArraySize()) != function.getParamCount())
592 {
593 error(line, "array constructor needs one argument per array element", "constructor");
594 return true;
595 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000596 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000597
Arun Patole7e7e68d2015-05-22 12:02:25 +0530598 if (arrayArg && op != EOpConstructStruct)
599 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000600 error(line, "constructing from a non-dereferenced array", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000601 return true;
602 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000603
Arun Patole7e7e68d2015-05-22 12:02:25 +0530604 if (matrixInMatrix && !type->isArray())
605 {
606 if (function.getParamCount() != 1)
607 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000608 error(line, "constructing matrix from matrix can only take one argument", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000609 return true;
610 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000611 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000612
Arun Patole7e7e68d2015-05-22 12:02:25 +0530613 if (overFull)
614 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000615 error(line, "too many arguments", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000616 return true;
617 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530618
619 if (op == EOpConstructStruct && !type->isArray() && type->getStruct()->fields().size() != function.getParamCount())
620 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000621 error(line, "Number of constructor parameters does not match the number of structure fields", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000622 return true;
623 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000624
Arun Patole7e7e68d2015-05-22 12:02:25 +0530625 if (!type->isMatrix() || !matrixInMatrix)
626 {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000627 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
Arun Patole7e7e68d2015-05-22 12:02:25 +0530628 (op == EOpConstructStruct && size < type->getObjectSize()))
629 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000630 error(line, "not enough data provided for construction", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000631 return true;
632 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000633 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000634
daniel@transgaming.com0b53fc02011-03-09 15:12:12 +0000635 TIntermTyped *typed = node ? node->getAsTyped() : 0;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530636 if (typed == 0)
637 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000638 error(line, "constructor argument does not have a type", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000639 return true;
640 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530641 if (op != EOpConstructStruct && IsSampler(typed->getBasicType()))
642 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000643 error(line, "cannot convert a sampler", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000644 return true;
645 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530646 if (typed->getBasicType() == EbtVoid)
647 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000648 error(line, "cannot convert a void", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000649 return true;
650 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000651
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000652 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000653}
654
655// This function checks to see if a void variable has been declared and raise an error message for such a case
656//
657// returns true in case of an error
658//
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300659bool TParseContext::voidErrorCheck(const TSourceLoc &line, const TString &identifier, const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000660{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300661 if (type == EbtVoid)
662 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000663 error(line, "illegal use of type 'void'", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000664 return true;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300665 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000666
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000667 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000668}
669
670// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
671//
672// returns true in case of an error
673//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530674bool TParseContext::boolErrorCheck(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000675{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530676 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector())
677 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000678 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000679 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530680 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000681
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000682 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000683}
684
685// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
686//
687// returns true in case of an error
688//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530689bool TParseContext::boolErrorCheck(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000690{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530691 if (pType.type != EbtBool || pType.isAggregate())
692 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000693 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000694 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530695 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000696
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000697 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000698}
699
Arun Patole7e7e68d2015-05-22 12:02:25 +0530700bool TParseContext::samplerErrorCheck(const TSourceLoc &line, const TPublicType &pType, const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000701{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530702 if (pType.type == EbtStruct)
703 {
704 if (containsSampler(*pType.userDef))
705 {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000706 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530707
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000708 return true;
709 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530710
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000711 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530712 }
713 else if (IsSampler(pType.type))
714 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000715 error(line, reason, getBasicString(pType.type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000716
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000717 return true;
718 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000719
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000720 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000721}
722
Arun Patole7e7e68d2015-05-22 12:02:25 +0530723bool TParseContext::locationDeclaratorListCheck(const TSourceLoc &line, const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400724{
725 if (pType.layoutQualifier.location != -1)
726 {
727 error(line, "location must only be specified for a single input or output variable", "location");
728 return true;
729 }
730
731 return false;
732}
733
Arun Patole7e7e68d2015-05-22 12:02:25 +0530734bool TParseContext::parameterSamplerErrorCheck(const TSourceLoc &line, TQualifier qualifier, const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000735{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530736 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
737 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType()))
738 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000739 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000740 return true;
741 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000742
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000743 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000744}
745
Arun Patole7e7e68d2015-05-22 12:02:25 +0530746bool TParseContext::containsSampler(const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000747{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000748 if (IsSampler(type.getBasicType()))
749 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000750
Arun Patole7e7e68d2015-05-22 12:02:25 +0530751 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock())
752 {
753 const TFieldList &fields = type.getStruct()->fields();
754 for (unsigned int i = 0; i < fields.size(); ++i)
755 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400756 if (containsSampler(*fields[i]->type()))
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000757 return true;
758 }
759 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000760
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000761 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000762}
763
764//
765// Do size checking for an array type's size.
766//
767// Returns true if there was an error.
768//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530769bool TParseContext::arraySizeErrorCheck(const TSourceLoc &line, TIntermTyped *expr, int &size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000770{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530771 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000772
Olli Etuahoe7847b02015-03-16 11:56:12 +0200773 if (constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000774 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000775 error(line, "array size must be a constant integer expression", "");
Olli Etuahoe7847b02015-03-16 11:56:12 +0200776 size = 1;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000777 return true;
778 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000779
Nicolas Capens906744a2014-06-06 15:18:07 -0400780 unsigned int unsignedSize = 0;
781
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000782 if (constant->getBasicType() == EbtUInt)
783 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400784 unsignedSize = constant->getUConst(0);
785 size = static_cast<int>(unsignedSize);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000786 }
787 else
788 {
789 size = constant->getIConst(0);
790
Nicolas Capens906744a2014-06-06 15:18:07 -0400791 if (size < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000792 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400793 error(line, "array size must be non-negative", "");
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000794 size = 1;
795 return true;
796 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400797
798 unsignedSize = static_cast<unsigned int>(size);
799 }
800
801 if (size == 0)
802 {
803 error(line, "array size must be greater than zero", "");
804 size = 1;
805 return true;
806 }
807
808 // The size of arrays is restricted here to prevent issues further down the
809 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
810 // 4096 registers so this should be reasonable even for aggressively optimizable code.
811 const unsigned int sizeLimit = 65536;
812
813 if (unsignedSize > sizeLimit)
814 {
815 error(line, "array size too large", "");
816 size = 1;
817 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000818 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000819
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000820 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000821}
822
823//
824// See if this qualifier can be an array.
825//
826// Returns true if there is an error.
827//
Olli Etuaho3739d232015-04-08 12:23:44 +0300828bool TParseContext::arrayQualifierErrorCheck(const TSourceLoc &line, const TPublicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000829{
Olli Etuaho3739d232015-04-08 12:23:44 +0300830 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqVertexIn) ||
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400831 (type.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300832 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000833 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000834 return true;
835 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000836
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000837 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000838}
839
840//
841// See if this type can be an array.
842//
843// Returns true if there is an error.
844//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530845bool TParseContext::arrayTypeErrorCheck(const TSourceLoc &line, const TPublicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000846{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000847 //
848 // Can the type be an array?
849 //
Jamie Madill06145232015-05-13 13:10:01 -0400850 if (type.array)
851 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000852 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000853 return true;
854 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000855
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000856 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000857}
858
859//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000860// Enforce non-initializer type/qualifier rules.
861//
862// Returns true if there was an error.
863//
Olli Etuaho376f1b52015-04-13 13:23:41 +0300864bool TParseContext::nonInitErrorCheck(const TSourceLoc &line, const TString &identifier, TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000865{
Olli Etuaho3739d232015-04-08 12:23:44 +0300866 ASSERT(type != nullptr);
867 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000868 {
869 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300870 type->qualifier = EvqTemporary;
871
872 // Generate informative error messages for ESSL1.
873 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400874 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000875 {
Arun Patole7e7e68d2015-05-22 12:02:25 +0530876 error(line,
877 "structures containing arrays may not be declared constant since they cannot be initialized",
878 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000879 }
880 else
881 {
882 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
883 }
884
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000885 return true;
886 }
Olli Etuaho376f1b52015-04-13 13:23:41 +0300887 if (type->isUnsizedArray())
888 {
889 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
890 return true;
891 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000892 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000893}
894
Olli Etuaho2935c582015-04-08 14:32:06 +0300895// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000896// and update the symbol table.
897//
Olli Etuaho2935c582015-04-08 14:32:06 +0300898// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000899//
Olli Etuaho2935c582015-04-08 14:32:06 +0300900bool TParseContext::declareVariable(const TSourceLoc &line, const TString &identifier, const TType &type,
901 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000902{
Olli Etuaho2935c582015-04-08 14:32:06 +0300903 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000904
Olli Etuaho2935c582015-04-08 14:32:06 +0300905 bool needsReservedErrorCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000906
Olli Etuaho2935c582015-04-08 14:32:06 +0300907 // gl_LastFragData may be redeclared with a new precision qualifier
908 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
909 {
910 const TVariable *maxDrawBuffers =
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400911 static_cast<const TVariable *>(symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho2935c582015-04-08 14:32:06 +0300912 if (type.getArraySize() == maxDrawBuffers->getConstPointer()->getIConst())
913 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400914 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +0300915 {
916 needsReservedErrorCheck = extensionErrorCheck(line, builtInSymbol->getExtension());
917 }
918 }
919 else
920 {
921 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers", identifier.c_str());
922 return false;
923 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000924 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000925
Olli Etuaho2935c582015-04-08 14:32:06 +0300926 if (needsReservedErrorCheck && reservedErrorCheck(line, identifier))
927 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000928
Olli Etuaho2935c582015-04-08 14:32:06 +0300929 (*variable) = new TVariable(&identifier, type);
930 if (!symbolTable.declare(*variable))
931 {
932 error(line, "redefinition", identifier.c_str());
933 delete (*variable);
934 (*variable) = nullptr;
935 return false;
936 }
937
938 if (voidErrorCheck(line, identifier, type.getBasicType()))
939 return false;
940
941 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000942}
943
Arun Patole7e7e68d2015-05-22 12:02:25 +0530944bool TParseContext::paramErrorCheck(const TSourceLoc &line, TQualifier qualifier, TQualifier paramQualifier,
945 TType *type)
946{
947 if (qualifier != EvqConst && qualifier != EvqTemporary)
948 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000949 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000950 return true;
951 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530952 if (qualifier == EvqConst && paramQualifier != EvqIn)
953 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000954 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
955 return true;
956 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000957
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000958 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000959 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000960 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000961 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000962
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000963 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000964}
965
Arun Patole7e7e68d2015-05-22 12:02:25 +0530966bool TParseContext::extensionErrorCheck(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000967{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530968 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000969 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +0530970 if (iter == extBehavior.end())
971 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000972 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000973 return true;
974 }
zmo@google.comf5450912011-09-09 01:37:19 +0000975 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +0530976 if (iter->second == EBhDisable || iter->second == EBhUndefined)
977 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000978 error(line, "extension", extension.c_str(), "is disabled");
979 return true;
980 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530981 if (iter->second == EBhWarn)
982 {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000983 warning(line, "extension", extension.c_str(), "is being used");
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000984 return false;
985 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000986
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000987 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000988}
989
Olli Etuahofa33d582015-04-09 14:33:12 +0300990// These checks are common for all declarations starting a declarator list, and declarators that follow an empty
991// declaration.
992//
Jamie Madill06145232015-05-13 13:10:01 -0400993bool TParseContext::singleDeclarationErrorCheck(const TPublicType &publicType, const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -0400994{
Olli Etuahofa33d582015-04-09 14:33:12 +0300995 switch (publicType.qualifier)
996 {
997 case EvqVaryingIn:
998 case EvqVaryingOut:
999 case EvqAttribute:
1000 case EvqVertexIn:
1001 case EvqFragmentOut:
1002 if (publicType.type == EbtStruct)
1003 {
1004 error(identifierLocation, "cannot be used with a structure",
1005 getQualifierString(publicType.qualifier));
1006 return true;
1007 }
1008
1009 default: break;
1010 }
1011
1012 if (publicType.qualifier != EvqUniform && samplerErrorCheck(identifierLocation, publicType,
1013 "samplers must be uniform"))
1014 {
Jamie Madilla5efff92013-06-06 11:56:47 -04001015 return true;
Olli Etuahofa33d582015-04-09 14:33:12 +03001016 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001017
1018 // check for layout qualifier issues
1019 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
1020
1021 if (layoutQualifier.matrixPacking != EmpUnspecified)
1022 {
Olli Etuahofa33d582015-04-09 14:33:12 +03001023 error(identifierLocation, "layout qualifier", getMatrixPackingString(layoutQualifier.matrixPacking),
1024 "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -04001025 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001026 }
1027
1028 if (layoutQualifier.blockStorage != EbsUnspecified)
1029 {
Olli Etuahofa33d582015-04-09 14:33:12 +03001030 error(identifierLocation, "layout qualifier", getBlockStorageString(layoutQualifier.blockStorage),
1031 "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -04001032 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001033 }
1034
Olli Etuahofa33d582015-04-09 14:33:12 +03001035 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut &&
1036 layoutLocationErrorCheck(identifierLocation, publicType.layoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001037 {
Jamie Madill51a53c72013-06-19 09:24:43 -04001038 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001039 }
1040
1041 return false;
1042}
1043
Arun Patole7e7e68d2015-05-22 12:02:25 +05301044bool TParseContext::layoutLocationErrorCheck(const TSourceLoc &location, const TLayoutQualifier &layoutQualifier)
Jamie Madilla5efff92013-06-06 11:56:47 -04001045{
1046 if (layoutQualifier.location != -1)
1047 {
1048 error(location, "invalid layout qualifier:", "location", "only valid on program inputs and outputs");
1049 return true;
1050 }
1051
1052 return false;
1053}
1054
Olli Etuahob6e07a62015-02-16 12:22:10 +02001055bool TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate, TIntermAggregate *aggregate)
1056{
1057 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1058 {
1059 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1060 if (qual == EvqOut || qual == EvqInOut)
1061 {
1062 TIntermTyped *node = (*(aggregate->getSequence()))[i]->getAsTyped();
1063 if (lValueErrorCheck(node->getLine(), "assign", node))
1064 {
1065 error(node->getLine(),
1066 "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error");
1067 recover();
1068 return true;
1069 }
1070 }
1071 }
1072 return false;
1073}
1074
Olli Etuaho37ad4742015-04-27 13:18:50 +03001075void TParseContext::es3InvariantErrorCheck(const TQualifier qualifier, const TSourceLoc &invariantLocation)
1076{
1077 if (!sh::IsVaryingOut(qualifier) && qualifier != EvqFragmentOut)
1078 {
1079 error(invariantLocation, "Only out variables can be invariant.", "invariant");
1080 recover();
1081 }
1082}
1083
Arun Patole7e7e68d2015-05-22 12:02:25 +05301084bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001085{
Arun Patole7e7e68d2015-05-22 12:02:25 +05301086 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001087 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1088 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001089}
1090
Arun Patole7e7e68d2015-05-22 12:02:25 +05301091bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001092{
Arun Patole7e7e68d2015-05-22 12:02:25 +05301093 const TExtensionBehavior &extbehavior = extensionBehavior();
Shannon Woodsa49a9bf2013-08-02 17:23:14 -04001094 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001095
1096 if (iter == extbehavior.end())
1097 {
1098 return false;
1099 }
1100
1101 return (iter->second == EBhEnable || iter->second == EBhRequire);
1102}
1103
Arun Patole7e7e68d2015-05-22 12:02:25 +05301104void TParseContext::handleExtensionDirective(const TSourceLoc &loc, const char *extName, const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001105{
1106 pp::SourceLocation srcLoc;
1107 srcLoc.file = loc.first_file;
1108 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001109 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001110}
1111
Arun Patole7e7e68d2015-05-22 12:02:25 +05301112void TParseContext::handlePragmaDirective(const TSourceLoc &loc, const char *name, const char *value, bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001113{
1114 pp::SourceLocation srcLoc;
1115 srcLoc.file = loc.first_file;
1116 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001117 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001118}
1119
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001120/////////////////////////////////////////////////////////////////////////////////
1121//
1122// Non-Errors.
1123//
1124/////////////////////////////////////////////////////////////////////////////////
1125
Jamie Madill5c097022014-08-20 16:38:32 -04001126const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1127 const TString *name,
1128 const TSymbol *symbol)
1129{
1130 const TVariable *variable = NULL;
1131
1132 if (!symbol)
1133 {
1134 error(location, "undeclared identifier", name->c_str());
1135 recover();
1136 }
1137 else if (!symbol->isVariable())
1138 {
1139 error(location, "variable expected", name->c_str());
1140 recover();
1141 }
1142 else
1143 {
1144 variable = static_cast<const TVariable*>(symbol);
1145
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001146 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
Jamie Madill5c097022014-08-20 16:38:32 -04001147 !variable->getExtension().empty() &&
1148 extensionErrorCheck(location, variable->getExtension()))
1149 {
1150 recover();
1151 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001152
1153 // Reject shaders using both gl_FragData and gl_FragColor
1154 TQualifier qualifier = variable->getType().getQualifier();
1155 if (qualifier == EvqFragData)
1156 {
1157 mUsesFragData = true;
1158 }
1159 else if (qualifier == EvqFragColor)
1160 {
1161 mUsesFragColor = true;
1162 }
1163
1164 // This validation is not quite correct - it's only an error to write to
1165 // both FragData and FragColor. For simplicity, and because users shouldn't
1166 // be rewarded for reading from undefined varaibles, return an error
1167 // if they are both referenced, rather than assigned.
1168 if (mUsesFragData && mUsesFragColor)
1169 {
1170 error(location, "cannot use both gl_FragData and gl_FragColor", name->c_str());
1171 recover();
1172 }
Jamie Madill5c097022014-08-20 16:38:32 -04001173 }
1174
1175 if (!variable)
1176 {
1177 TType type(EbtFloat, EbpUndefined);
1178 TVariable *fakeVariable = new TVariable(name, type);
1179 symbolTable.declare(fakeVariable);
1180 variable = fakeVariable;
1181 }
1182
1183 return variable;
1184}
1185
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001186//
1187// Look up a function name in the symbol table, and make sure it is a function.
1188//
1189// Return the function symbol if found, otherwise 0.
1190//
Arun Patole7e7e68d2015-05-22 12:02:25 +05301191const TFunction *TParseContext::findFunction(const TSourceLoc &line, TFunction *call, int inputShaderVersion,
1192 bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001193{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001194 // First find by unmangled name to check whether the function name has been
1195 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001196 // If a function is found, check for one with a matching argument list.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301197 const TSymbol *symbol = symbolTable.find(call->getName(), inputShaderVersion, builtIn);
1198 if (symbol == 0 || symbol->isFunction())
1199 {
Austin Kinross3ae64652015-01-26 15:51:39 -08001200 symbol = symbolTable.find(call->getMangledName(), inputShaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001201 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001202
Arun Patole7e7e68d2015-05-22 12:02:25 +05301203 if (symbol == 0)
1204 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001205 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001206 return 0;
1207 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001208
Arun Patole7e7e68d2015-05-22 12:02:25 +05301209 if (!symbol->isFunction())
1210 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001211 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001212 return 0;
1213 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001214
1215 return static_cast<const TFunction*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001216}
1217
1218//
1219// Initializers show up in several places in the grammar. Have one set of
1220// code to handle them here.
1221//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001222// Returns true on error, false if no error
1223//
Jamie Madill06145232015-05-13 13:10:01 -04001224bool TParseContext::executeInitializer(const TSourceLoc &line, const TString &identifier, const TPublicType &pType,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001225 TIntermTyped *initializer, TIntermNode **intermNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001226{
Olli Etuahoe7847b02015-03-16 11:56:12 +02001227 ASSERT(intermNode != nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001228 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001229
Olli Etuaho2935c582015-04-08 14:32:06 +03001230 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001231 if (type.isUnsizedArray())
1232 {
1233 type.setArraySize(initializer->getArraySize());
1234 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001235 if (!declareVariable(line, identifier, type, &variable))
1236 {
1237 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001238 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001239
Olli Etuahob0c645e2015-05-12 14:25:36 +03001240 bool globalInitWarning = false;
1241 if (symbolTable.atGlobalLevel() && !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
1242 {
1243 // Error message does not completely match behavior with ESSL 1.00, but
1244 // we want to steer developers towards only using constant expressions.
1245 error(line, "global variable initializers must be constant expressions", "=");
1246 return true;
1247 }
1248 if (globalInitWarning)
1249 {
1250 warning(line, "global variable initializers should be constant expressions "
1251 "(uniforms and globals are allowed in global initializers for legacy compatibility)", "=");
1252 }
1253
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001254 //
1255 // identifier must be of type constant, a global, or a temporary
1256 //
1257 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301258 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1259 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001260 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001261 return true;
1262 }
1263 //
1264 // test for and propagate constant
1265 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001266
Arun Patole7e7e68d2015-05-22 12:02:25 +05301267 if (qualifier == EvqConst)
1268 {
1269 if (qualifier != initializer->getType().getQualifier())
1270 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001271 std::stringstream extraInfoStream;
1272 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1273 std::string extraInfo = extraInfoStream.str();
1274 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001275 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001276 return true;
1277 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301278 if (type != initializer->getType())
1279 {
1280 error(line, " non-matching types for const initializer ",
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001281 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001282 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001283 return true;
1284 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301285 if (initializer->getAsConstantUnion())
1286 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001287 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Arun Patole7e7e68d2015-05-22 12:02:25 +05301288 }
1289 else if (initializer->getAsSymbolNode())
1290 {
1291 const TSymbol *symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1292 const TVariable *tVar = static_cast<const TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001293
Arun Patole7e7e68d2015-05-22 12:02:25 +05301294 TConstantUnion *constArray = tVar->getConstPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001295 variable->shareConstPointer(constArray);
Arun Patole7e7e68d2015-05-22 12:02:25 +05301296 }
1297 else
1298 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001299 std::stringstream extraInfoStream;
1300 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1301 std::string extraInfo = extraInfoStream.str();
1302 error(line, " cannot assign to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001303 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001304 return true;
1305 }
1306 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001307
1308 if (qualifier != EvqConst)
1309 {
1310 TIntermSymbol *intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(),
1311 variable->getType(), line);
1312 *intermNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1313 if (*intermNode == nullptr)
1314 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001315 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1316 return true;
1317 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001318 }
1319 else
1320 {
1321 *intermNode = nullptr;
1322 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001323
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001324 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001325}
1326
Arun Patole7e7e68d2015-05-22 12:02:25 +05301327bool TParseContext::areAllChildConst(TIntermAggregate *aggrNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001328{
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001329 ASSERT(aggrNode != NULL);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001330 if (!aggrNode->isConstructor())
1331 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001332
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001333 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001334
Arun Patole7e7e68d2015-05-22 12:02:25 +05301335 // check if all the child nodes are constants so that they can be inserted into
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001336 // the parent node
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001337 TIntermSequence *sequence = aggrNode->getSequence() ;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301338 for (TIntermSequence::iterator p = sequence->begin(); p != sequence->end(); ++p)
1339 {
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001340 if (!(*p)->getAsTyped()->getAsConstantUnion())
1341 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001342 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001343
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001344 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001345}
1346
Olli Etuaho214c2d82015-04-27 14:49:13 +03001347TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier, bool invariant, TLayoutQualifier layoutQualifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301348 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001349{
1350 TPublicType returnType = typeSpecifier;
1351 returnType.qualifier = qualifier;
Olli Etuaho214c2d82015-04-27 14:49:13 +03001352 returnType.invariant = invariant;
Jamie Madilla5efff92013-06-06 11:56:47 -04001353 returnType.layoutQualifier = layoutQualifier;
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001354
1355 if (typeSpecifier.array)
1356 {
1357 error(typeSpecifier.line, "not supported", "first-class array");
1358 recover();
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001359 returnType.clearArrayness();
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001360 }
1361
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001362 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001363 {
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001364 if (qualifier == EvqAttribute && (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1365 {
1366 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1367 recover();
1368 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001369
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001370 if ((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1371 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1372 {
1373 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1374 recover();
1375 }
1376 }
1377 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001378 {
Jamie Madillb120eac2013-06-12 14:08:13 -04001379 switch (qualifier)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001380 {
Jamie Madill19571812013-08-12 15:26:34 -07001381 case EvqSmoothIn:
1382 case EvqSmoothOut:
1383 case EvqVertexOut:
1384 case EvqFragmentIn:
1385 case EvqCentroidOut:
1386 case EvqCentroidIn:
1387 if (typeSpecifier.type == EbtBool)
1388 {
1389 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1390 recover();
1391 }
1392 if (typeSpecifier.type == EbtInt || typeSpecifier.type == EbtUInt)
1393 {
1394 error(typeSpecifier.line, "must use 'flat' interpolation here", getQualifierString(qualifier));
1395 recover();
1396 }
1397 break;
1398
1399 case EvqVertexIn:
1400 case EvqFragmentOut:
1401 case EvqFlatIn:
1402 case EvqFlatOut:
Jamie Madillb120eac2013-06-12 14:08:13 -04001403 if (typeSpecifier.type == EbtBool)
1404 {
1405 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1406 recover();
1407 }
1408 break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001409
Jamie Madillb120eac2013-06-12 14:08:13 -04001410 default: break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001411 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001412 }
1413
1414 return returnType;
1415}
1416
Olli Etuahofa33d582015-04-09 14:33:12 +03001417TIntermAggregate *TParseContext::parseSingleDeclaration(TPublicType &publicType,
1418 const TSourceLoc &identifierOrTypeLocation,
1419 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001420{
Olli Etuahofa33d582015-04-09 14:33:12 +03001421 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001422
Olli Etuahobab4c082015-04-24 16:38:49 +03001423 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03001424
Olli Etuahobab4c082015-04-24 16:38:49 +03001425 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1426
1427 if (emptyDeclaration)
1428 {
1429 if (publicType.isUnsizedArray())
1430 {
1431 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an error.
1432 // It is assumed that this applies to empty declarations as well.
1433 error(identifierOrTypeLocation, "empty array declaration needs to specify a size", identifier.c_str());
1434 }
1435 }
1436 else
Jamie Madill60ed9812013-06-06 11:56:46 -04001437 {
Olli Etuahofa33d582015-04-09 14:33:12 +03001438 if (singleDeclarationErrorCheck(publicType, identifierOrTypeLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001439 recover();
1440
Olli Etuaho376f1b52015-04-13 13:23:41 +03001441 if (nonInitErrorCheck(identifierOrTypeLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001442 recover();
1443
Olli Etuaho2935c582015-04-08 14:32:06 +03001444 TVariable *variable = nullptr;
Olli Etuahofa33d582015-04-09 14:33:12 +03001445 if (!declareVariable(identifierOrTypeLocation, identifier, TType(publicType), &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001446 recover();
1447
1448 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001449 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001450 }
1451
Olli Etuahoe7847b02015-03-16 11:56:12 +02001452 return intermediate.makeAggregate(symbol, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001453}
1454
Olli Etuahoe7847b02015-03-16 11:56:12 +02001455TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1456 const TSourceLoc &identifierLocation,
1457 const TString &identifier,
1458 const TSourceLoc &indexLocation,
1459 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001460{
Olli Etuahofa33d582015-04-09 14:33:12 +03001461 mDeferredSingleDeclarationErrorCheck = false;
1462
1463 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001464 recover();
1465
Olli Etuaho376f1b52015-04-13 13:23:41 +03001466 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001467 recover();
1468
1469 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1470 {
1471 recover();
1472 }
1473
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001474 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001475
1476 int size;
1477 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1478 {
1479 recover();
1480 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001481 // Make the type an array even if size check failed.
1482 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1483 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001484
Olli Etuaho2935c582015-04-08 14:32:06 +03001485 TVariable *variable = nullptr;
1486 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001487 recover();
1488
Olli Etuahoe7847b02015-03-16 11:56:12 +02001489 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001490 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001491 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001492
Olli Etuahoe7847b02015-03-16 11:56:12 +02001493 return intermediate.makeAggregate(symbol, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001494}
1495
Jamie Madill06145232015-05-13 13:10:01 -04001496TIntermAggregate *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001497 const TSourceLoc &identifierLocation,
1498 const TString &identifier,
1499 const TSourceLoc &initLocation,
1500 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001501{
Olli Etuahofa33d582015-04-09 14:33:12 +03001502 mDeferredSingleDeclarationErrorCheck = false;
1503
1504 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001505 recover();
1506
Olli Etuahoe7847b02015-03-16 11:56:12 +02001507 TIntermNode *intermNode = nullptr;
1508 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04001509 {
1510 //
1511 // Build intermediate representation
1512 //
Olli Etuahoe7847b02015-03-16 11:56:12 +02001513 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001514 }
1515 else
1516 {
1517 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001518 return nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001519 }
1520}
1521
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001522TIntermAggregate *TParseContext::parseSingleArrayInitDeclaration(TPublicType &publicType,
1523 const TSourceLoc &identifierLocation,
1524 const TString &identifier,
1525 const TSourceLoc &indexLocation,
1526 TIntermTyped *indexExpression,
1527 const TSourceLoc &initLocation,
1528 TIntermTyped *initializer)
1529{
1530 mDeferredSingleDeclarationErrorCheck = false;
1531
1532 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1533 recover();
1534
1535 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1536 {
1537 recover();
1538 }
1539
1540 TPublicType arrayType(publicType);
1541
Olli Etuaho376f1b52015-04-13 13:23:41 +03001542 int size = 0;
1543 // If indexExpression is nullptr, then the array will eventually get its size implicitly from the initializer.
1544 if (indexExpression != nullptr && arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001545 {
1546 recover();
1547 }
1548 // Make the type an array even if size check failed.
1549 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1550 arrayType.setArraySize(size);
1551
1552 // initNode will correspond to the whole of "type b[n] = initializer".
1553 TIntermNode *initNode = nullptr;
1554 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1555 {
1556 return initNode ? intermediate.makeAggregate(initNode, initLocation) : nullptr;
1557 }
1558 else
1559 {
1560 recover();
1561 return nullptr;
1562 }
1563}
1564
Olli Etuahoe7847b02015-03-16 11:56:12 +02001565TIntermAggregate *TParseContext::parseInvariantDeclaration(const TSourceLoc &invariantLoc,
Jamie Madill47e3ec02014-08-20 16:38:33 -04001566 const TSourceLoc &identifierLoc,
1567 const TString *identifier,
1568 const TSymbol *symbol)
1569{
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001570 // invariant declaration
Jamie Madill47e3ec02014-08-20 16:38:33 -04001571 if (globalErrorCheck(invariantLoc, symbolTable.atGlobalLevel(), "invariant varying"))
1572 {
1573 recover();
1574 }
1575
1576 if (!symbol)
1577 {
1578 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
1579 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001580 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001581 }
1582 else
1583 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001584 const TString kGlFrontFacing("gl_FrontFacing");
1585 if (*identifier == kGlFrontFacing)
1586 {
1587 error(identifierLoc, "identifier should not be declared as invariant", identifier->c_str());
1588 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001589 return nullptr;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001590 }
Jamie Madill2c433252014-12-03 12:36:54 -05001591 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001592 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1593 ASSERT(variable);
1594 const TType &type = variable->getType();
1595 TIntermSymbol *intermSymbol = intermediate.addSymbol(variable->getUniqueId(),
1596 *identifier, type, identifierLoc);
1597
1598 TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc);
1599 aggregate->setOp(EOpInvariantDeclaration);
1600 return aggregate;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001601 }
1602}
1603
Olli Etuahoe7847b02015-03-16 11:56:12 +02001604TIntermAggregate *TParseContext::parseDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1605 const TSourceLoc &identifierLocation, const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001606{
Olli Etuahofa33d582015-04-09 14:33:12 +03001607 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1608 if (mDeferredSingleDeclarationErrorCheck)
1609 {
1610 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1611 recover();
1612 mDeferredSingleDeclarationErrorCheck = false;
1613 }
1614
Jamie Madill0bd18df2013-06-20 11:55:52 -04001615 if (locationDeclaratorListCheck(identifierLocation, publicType))
1616 recover();
1617
Olli Etuaho376f1b52015-04-13 13:23:41 +03001618 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001619 recover();
1620
Olli Etuaho2935c582015-04-08 14:32:06 +03001621 TVariable *variable = nullptr;
1622 if (!declareVariable(identifierLocation, identifier, TType(publicType), &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001623 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001624
1625 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1626 if (variable && symbol)
Jamie Madill502d66f2013-06-20 11:55:52 -04001627 symbol->setId(variable->getUniqueId());
1628
Olli Etuahoe7847b02015-03-16 11:56:12 +02001629 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001630}
1631
Olli Etuahoe7847b02015-03-16 11:56:12 +02001632TIntermAggregate *TParseContext::parseArrayDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1633 const TSourceLoc &identifierLocation, const TString &identifier,
1634 const TSourceLoc &arrayLocation, TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001635{
Olli Etuahofa33d582015-04-09 14:33:12 +03001636 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1637 if (mDeferredSingleDeclarationErrorCheck)
1638 {
1639 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1640 recover();
1641 mDeferredSingleDeclarationErrorCheck = false;
1642 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001643
Jamie Madill0bd18df2013-06-20 11:55:52 -04001644 if (locationDeclaratorListCheck(identifierLocation, publicType))
1645 recover();
1646
Olli Etuaho376f1b52015-04-13 13:23:41 +03001647 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001648 recover();
1649
1650 if (arrayTypeErrorCheck(arrayLocation, publicType) || arrayQualifierErrorCheck(arrayLocation, publicType))
1651 {
1652 recover();
1653 }
Olli Etuaho93a90fd2015-04-07 18:14:07 +03001654 else
Jamie Madill502d66f2013-06-20 11:55:52 -04001655 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001656 TType arrayType = TType(publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04001657 int size;
1658 if (arraySizeErrorCheck(arrayLocation, indexExpression, size))
Olli Etuahoe7847b02015-03-16 11:56:12 +02001659 {
Jamie Madill502d66f2013-06-20 11:55:52 -04001660 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001661 }
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001662 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001663
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001664 TVariable *variable = nullptr;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001665 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001666 recover();
Jamie Madill502d66f2013-06-20 11:55:52 -04001667
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001668 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
1669 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001670 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001671
1672 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001673 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001674
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001675 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001676}
1677
Jamie Madill06145232015-05-13 13:10:01 -04001678TIntermAggregate *TParseContext::parseInitDeclarator(const TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001679 const TSourceLoc &identifierLocation, const TString &identifier,
1680 const TSourceLoc &initLocation, TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001681{
Olli Etuahofa33d582015-04-09 14:33:12 +03001682 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1683 if (mDeferredSingleDeclarationErrorCheck)
1684 {
1685 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1686 recover();
1687 mDeferredSingleDeclarationErrorCheck = false;
1688 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001689
Jamie Madill0bd18df2013-06-20 11:55:52 -04001690 if (locationDeclaratorListCheck(identifierLocation, publicType))
1691 recover();
1692
Olli Etuahoe7847b02015-03-16 11:56:12 +02001693 TIntermNode *intermNode = nullptr;
1694 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04001695 {
1696 //
1697 // build the intermediate representation
1698 //
1699 if (intermNode)
1700 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001701 return intermediate.growAggregate(aggregateDeclaration, intermNode, initLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001702 }
1703 else
1704 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001705 return aggregateDeclaration;
Jamie Madill502d66f2013-06-20 11:55:52 -04001706 }
1707 }
1708 else
1709 {
1710 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001711 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001712 }
1713}
1714
Jamie Madill06145232015-05-13 13:10:01 -04001715TIntermAggregate *TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001716 TIntermAggregate *aggregateDeclaration,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301717 const TSourceLoc &identifierLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001718 const TString &identifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301719 const TSourceLoc &indexLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001720 TIntermTyped *indexExpression,
1721 const TSourceLoc &initLocation, TIntermTyped *initializer)
1722{
1723 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1724 if (mDeferredSingleDeclarationErrorCheck)
1725 {
1726 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1727 recover();
1728 mDeferredSingleDeclarationErrorCheck = false;
1729 }
1730
1731 if (locationDeclaratorListCheck(identifierLocation, publicType))
1732 recover();
1733
1734 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1735 {
1736 recover();
1737 }
1738
1739 TPublicType arrayType(publicType);
1740
Olli Etuaho376f1b52015-04-13 13:23:41 +03001741 int size = 0;
1742 // If indexExpression is nullptr, then the array will eventually get its size implicitly from the initializer.
1743 if (indexExpression != nullptr && arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001744 {
1745 recover();
1746 }
1747 // Make the type an array even if size check failed.
1748 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1749 arrayType.setArraySize(size);
1750
1751 // initNode will correspond to the whole of "b[n] = initializer".
1752 TIntermNode *initNode = nullptr;
1753 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1754 {
1755 if (initNode)
1756 {
1757 return intermediate.growAggregate(aggregateDeclaration, initNode, initLocation);
1758 }
1759 else
1760 {
1761 return aggregateDeclaration;
1762 }
1763 }
1764 else
1765 {
1766 recover();
1767 return nullptr;
1768 }
1769}
1770
Jamie Madilla295edf2013-06-06 11:56:48 -04001771void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1772{
1773 if (typeQualifier.qualifier != EvqUniform)
1774 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301775 error(typeQualifier.line,
1776 "invalid qualifier:",
1777 getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
Jamie Madilla295edf2013-06-06 11:56:48 -04001778 recover();
1779 return;
1780 }
1781
1782 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1783 ASSERT(!layoutQualifier.isEmpty());
1784
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001785 if (mShaderVersion < 300)
Jamie Madilla295edf2013-06-06 11:56:48 -04001786 {
1787 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1788 recover();
1789 return;
1790 }
1791
1792 if (layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1793 {
1794 recover();
1795 return;
1796 }
1797
Jamie Madill099c0f32013-06-20 11:55:52 -04001798 if (layoutQualifier.matrixPacking != EmpUnspecified)
1799 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001800 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04001801 }
1802
Geoff Langc6856732014-02-11 09:38:55 -05001803 if (layoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04001804 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001805 mDefaultBlockStorage = layoutQualifier.blockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04001806 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001807}
1808
Jamie Madill06145232015-05-13 13:10:01 -04001809TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001810{
Jamie Madill06145232015-05-13 13:10:01 -04001811 TPublicType publicType = publicTypeIn;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001812 TOperator op = EOpNull;
1813 if (publicType.userDef)
1814 {
1815 op = EOpConstructStruct;
1816 }
1817 else
1818 {
1819 switch (publicType.type)
1820 {
1821 case EbtFloat:
1822 if (publicType.isMatrix())
1823 {
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001824 switch(publicType.getCols())
1825 {
Alexis Hetu07e57df2015-06-16 16:55:52 -04001826 case 2:
1827 switch(publicType.getRows())
1828 {
1829 case 2: op = EOpConstructMat2; break;
1830 case 3: op = EOpConstructMat2x3; break;
1831 case 4: op = EOpConstructMat2x4; break;
1832 }
1833 break;
1834 case 3:
1835 switch(publicType.getRows())
1836 {
1837 case 2: op = EOpConstructMat3x2; break;
1838 case 3: op = EOpConstructMat3; break;
1839 case 4: op = EOpConstructMat3x4; break;
1840 }
1841 break;
1842 case 4:
1843 switch(publicType.getRows())
1844 {
1845 case 2: op = EOpConstructMat4x2; break;
1846 case 3: op = EOpConstructMat4x3; break;
1847 case 4: op = EOpConstructMat4; break;
1848 }
1849 break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001850 }
1851 }
1852 else
1853 {
1854 switch(publicType.getNominalSize())
1855 {
Jamie Madill28b97422013-07-08 14:01:38 -04001856 case 1: op = EOpConstructFloat; break;
1857 case 2: op = EOpConstructVec2; break;
1858 case 3: op = EOpConstructVec3; break;
1859 case 4: op = EOpConstructVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001860 }
1861 }
1862 break;
1863
1864 case EbtInt:
1865 switch(publicType.getNominalSize())
1866 {
Jamie Madill28b97422013-07-08 14:01:38 -04001867 case 1: op = EOpConstructInt; break;
1868 case 2: op = EOpConstructIVec2; break;
1869 case 3: op = EOpConstructIVec3; break;
1870 case 4: op = EOpConstructIVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001871 }
1872 break;
1873
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001874 case EbtUInt:
1875 switch(publicType.getNominalSize())
1876 {
Jamie Madill28b97422013-07-08 14:01:38 -04001877 case 1: op = EOpConstructUInt; break;
1878 case 2: op = EOpConstructUVec2; break;
1879 case 3: op = EOpConstructUVec3; break;
1880 case 4: op = EOpConstructUVec4; break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001881 }
1882 break;
1883
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001884 case EbtBool:
1885 switch(publicType.getNominalSize())
1886 {
Jamie Madill28b97422013-07-08 14:01:38 -04001887 case 1: op = EOpConstructBool; break;
1888 case 2: op = EOpConstructBVec2; break;
1889 case 3: op = EOpConstructBVec3; break;
1890 case 4: op = EOpConstructBVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001891 }
1892 break;
1893
1894 default: break;
1895 }
1896
1897 if (op == EOpNull)
1898 {
1899 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
1900 recover();
1901 publicType.type = EbtFloat;
1902 op = EOpConstructFloat;
1903 }
1904 }
1905
1906 TString tempString;
Dmitry Skiba7f17a502015-06-22 15:08:39 -07001907 const TType *type = new TType(publicType);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001908 return new TFunction(&tempString, type, op);
1909}
1910
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001911// This function is used to test for the correctness of the parameters passed to various constructor functions
Arun Patole7e7e68d2015-05-22 12:02:25 +05301912// and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001913//
1914// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1915//
Arun Patole7e7e68d2015-05-22 12:02:25 +05301916TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments, TType *type, TOperator op, TFunction *fnCall,
1917 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001918{
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001919 TIntermAggregate *aggregateArguments = arguments->getAsAggregate();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001920
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001921 if (!aggregateArguments)
1922 {
1923 aggregateArguments = new TIntermAggregate;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001924 aggregateArguments->getSequence()->push_back(arguments);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001925 }
1926
Olli Etuahof40319e2015-03-10 14:33:00 +02001927 if (type->isArray())
1928 {
1929 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of the array.
1930 TIntermSequence *args = aggregateArguments->getSequence();
1931 for (size_t i = 0; i < args->size(); i++)
1932 {
1933 const TType &argType = (*args)[i]->getAsTyped()->getType();
1934 // It has already been checked that the argument is not an array.
1935 ASSERT(!argType.isArray());
1936 if (!argType.sameElementType(*type))
1937 {
1938 error(line, "Array constructor argument has an incorrect type", "Error");
1939 recover();
1940 return nullptr;
1941 }
1942 }
1943 }
1944 else if (op == EOpConstructStruct)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001945 {
1946 const TFieldList &fields = type->getStruct()->fields();
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001947 TIntermSequence *args = aggregateArguments->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001948
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001949 for (size_t i = 0; i < fields.size(); i++)
1950 {
Nicolas Capensffd73872014-08-21 13:49:16 -04001951 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001952 {
1953 error(line, "Structure constructor arguments do not match structure fields", "Error");
1954 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001955
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001956 return 0;
1957 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001958 }
1959 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001960
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001961 // Turn the argument list itself into a constructor
Olli Etuaho21203702014-11-13 16:16:21 +02001962 TIntermAggregate *constructor = intermediate.setAggregateOperator(aggregateArguments, op, line);
1963 TIntermTyped *constConstructor = foldConstConstructor(constructor, *type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001964 if (constConstructor)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001965 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001966 return constConstructor;
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001967 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001968
Olli Etuaho21203702014-11-13 16:16:21 +02001969 // Structs should not be precision qualified, the individual members may be.
1970 // Built-in types on the other hand should be precision qualified.
1971 if (op != EOpConstructStruct)
1972 {
1973 constructor->setPrecisionFromChildren();
1974 type->setPrecision(constructor->getPrecision());
1975 }
1976
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001977 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001978}
1979
Arun Patole7e7e68d2015-05-22 12:02:25 +05301980TIntermTyped *TParseContext::foldConstConstructor(TIntermAggregate *aggrNode, const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001981{
Olli Etuahof40319e2015-03-10 14:33:00 +02001982 // TODO: Add support for folding array constructors
1983 bool canBeFolded = areAllChildConst(aggrNode) && !type.isArray();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001984 aggrNode->setType(type);
Arun Patole7e7e68d2015-05-22 12:02:25 +05301985 if (canBeFolded)
1986 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001987 bool returnVal = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301988 TConstantUnion *unionArray = new TConstantUnion[type.getObjectSize()];
1989 if (aggrNode->getSequence()->size() == 1)
1990 {
1991 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type,
1992 true);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001993 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301994 else
1995 {
1996 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(),
1997 type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001998 }
1999 if (returnVal)
2000 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002001
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002002 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
2003 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002004
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002005 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002006}
2007
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002008//
2009// This function returns the tree representation for the vector field(s) being accessed from contant vector.
2010// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
2011// 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 +05302012// 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 +00002013// a constant matrix.
2014//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302015TIntermTyped *TParseContext::addConstVectorNode(TVectorFields &fields, TIntermTyped *node, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002016{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302017 TIntermTyped *typedNode;
2018 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002019
Jamie Madillb11e2482015-05-04 14:21:22 -04002020 const TConstantUnion *unionArray;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302021 if (tempConstantNode)
2022 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002023 unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002024
Arun Patole7e7e68d2015-05-22 12:02:25 +05302025 if (!unionArray)
2026 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002027 return node;
2028 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302029 }
2030 else
2031 { // 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 +00002032 error(line, "Cannot offset into the vector", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002033 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002034
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002035 return 0;
2036 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002037
Arun Patole7e7e68d2015-05-22 12:02:25 +05302038 TConstantUnion *constArray = new TConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002039
Arun Patole7e7e68d2015-05-22 12:02:25 +05302040 for (int i = 0; i < fields.num; i++)
2041 {
2042 if (fields.offsets[i] >= node->getType().getNominalSize())
2043 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002044 std::stringstream extraInfoStream;
2045 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
2046 std::string extraInfo = extraInfoStream.str();
2047 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002048 recover();
2049 fields.offsets[i] = 0;
2050 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302051
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002052 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002053
Arun Patole7e7e68d2015-05-22 12:02:25 +05302054 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002055 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
2056 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002057}
2058
2059//
2060// This function returns the column being accessed from a constant matrix. The values are retrieved from
Arun Patole7e7e68d2015-05-22 12:02:25 +05302061// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
2062// 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 +00002063// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
2064//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302065TIntermTyped *TParseContext::addConstMatrixNode(int index, TIntermTyped *node, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002066{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302067 TIntermTyped *typedNode;
2068 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002069
Arun Patole7e7e68d2015-05-22 12:02:25 +05302070 if (index >= node->getType().getCols())
2071 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002072 std::stringstream extraInfoStream;
2073 extraInfoStream << "matrix field selection out of range '" << index << "'";
2074 std::string extraInfo = extraInfoStream.str();
2075 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002076 recover();
2077 index = 0;
2078 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002079
Arun Patole7e7e68d2015-05-22 12:02:25 +05302080 if (tempConstantNode)
2081 {
Jamie Madillb11e2482015-05-04 14:21:22 -04002082 TConstantUnion *unionArray = tempConstantNode->getUnionArrayPointer();
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002083 int size = tempConstantNode->getType().getCols();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002084 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302085 }
2086 else
2087 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002088 error(line, "Cannot offset into the matrix", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002089 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002090
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002091 return 0;
2092 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002093
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002094 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002095}
2096
2097
2098//
2099// 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 +05302100// the symbol table and parse-tree is built for the type of the element. The input
2101// 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 +00002102// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
2103//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302104TIntermTyped *TParseContext::addConstArrayNode(int index, TIntermTyped *node, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002105{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302106 TIntermTyped *typedNode;
2107 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002108 TType arrayElementType = node->getType();
2109 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002110
Arun Patole7e7e68d2015-05-22 12:02:25 +05302111 if (index >= node->getType().getArraySize())
2112 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002113 std::stringstream extraInfoStream;
2114 extraInfoStream << "array field selection out of range '" << index << "'";
2115 std::string extraInfo = extraInfoStream.str();
2116 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002117 recover();
2118 index = 0;
2119 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002120
Arun Patole7e7e68d2015-05-22 12:02:25 +05302121 if (tempConstantNode)
2122 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002123 size_t arrayElementSize = arrayElementType.getObjectSize();
Arun Patole7e7e68d2015-05-22 12:02:25 +05302124 TConstantUnion *unionArray = tempConstantNode->getUnionArrayPointer();
2125 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(),
2126 line);
2127 }
2128 else
2129 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002130 error(line, "Cannot offset into the array", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002131 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002132
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002133 return 0;
2134 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002135
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002136 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002137}
2138
2139
2140//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302141// 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 +00002142// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
2143// function and returns the parse-tree with the values of the embedded/nested struct.
2144//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302145TIntermTyped *TParseContext::addConstStruct(const TString &identifier, TIntermTyped *node, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002146{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302147 const TFieldList &fields = node->getType().getStruct()->fields();
Jamie Madill94bf7f22013-07-08 13:31:15 -04002148 size_t instanceSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002149
Arun Patole7e7e68d2015-05-22 12:02:25 +05302150 for (size_t index = 0; index < fields.size(); ++index)
2151 {
2152 if (fields[index]->name() == identifier)
2153 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002154 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302155 }
2156 else
2157 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002158 instanceSize += fields[index]->type()->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002159 }
2160 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002161
Jamie Madill94bf7f22013-07-08 13:31:15 -04002162 TIntermTyped *typedNode;
2163 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
Arun Patole7e7e68d2015-05-22 12:02:25 +05302164 if (tempConstantNode)
2165 {
2166 TConstantUnion *constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002167
Arun Patole7e7e68d2015-05-22 12:02:25 +05302168 // type will be changed in the calling function
2169 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line);
2170 }
2171 else
2172 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002173 error(line, "Cannot offset into the structure", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002174 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002175
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002176 return 0;
2177 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002178
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002179 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002180}
2181
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002182//
2183// Interface/uniform blocks
2184//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302185TIntermAggregate *TParseContext::addInterfaceBlock(const TPublicType &typeQualifier, const TSourceLoc &nameLine,
2186 const TString &blockName, TFieldList *fieldList,
2187 const TString *instanceName, const TSourceLoc &instanceLine,
2188 TIntermTyped *arrayIndex, const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002189{
2190 if (reservedErrorCheck(nameLine, blockName))
2191 recover();
2192
2193 if (typeQualifier.qualifier != EvqUniform)
2194 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302195 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
2196 "interface blocks must be uniform");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002197 recover();
2198 }
2199
Jamie Madill099c0f32013-06-20 11:55:52 -04002200 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
2201 if (layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002202 {
2203 recover();
2204 }
2205
Jamie Madill099c0f32013-06-20 11:55:52 -04002206 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2207 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002208 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002209 }
2210
Jamie Madill1566ef72013-06-20 11:55:54 -04002211 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2212 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002213 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002214 }
2215
Arun Patole7e7e68d2015-05-22 12:02:25 +05302216 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2217 if (!symbolTable.declare(blockNameSymbol))
2218 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002219 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
2220 recover();
2221 }
2222
Jamie Madill98493dd2013-07-08 14:39:03 -04002223 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302224 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2225 {
2226 TField *field = (*fieldList)[memberIndex];
2227 TType *fieldType = field->type();
2228 if (IsSampler(fieldType->getBasicType()))
2229 {
2230 error(field->line(), "unsupported type", fieldType->getBasicString(),
2231 "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002232 recover();
2233 }
2234
Jamie Madill98493dd2013-07-08 14:39:03 -04002235 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002236 switch (qualifier)
2237 {
2238 case EvqGlobal:
2239 case EvqUniform:
2240 break;
2241 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04002242 error(field->line(), "invalid qualifier on interface block member", getQualifierString(qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002243 recover();
2244 break;
2245 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002246
2247 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002248 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
2249 if (layoutLocationErrorCheck(field->line(), fieldLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002250 {
2251 recover();
2252 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002253
Jamie Madill98493dd2013-07-08 14:39:03 -04002254 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002255 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302256 error(field->line(), "invalid layout qualifier:", getBlockStorageString(fieldLayoutQualifier.blockStorage),
2257 "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04002258 recover();
2259 }
2260
Jamie Madill98493dd2013-07-08 14:39:03 -04002261 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002262 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002263 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002264 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002265 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002266 {
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002267 warning(field->line(), "extraneous layout qualifier:",
2268 getMatrixPackingString(fieldLayoutQualifier.matrixPacking), "only has an effect on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04002269 }
2270
Jamie Madill98493dd2013-07-08 14:39:03 -04002271 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002272 }
2273
Jamie Madill98493dd2013-07-08 14:39:03 -04002274 // add array index
2275 int arraySize = 0;
2276 if (arrayIndex != NULL)
2277 {
2278 if (arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize))
2279 recover();
2280 }
2281
Arun Patole7e7e68d2015-05-22 12:02:25 +05302282 TInterfaceBlock *interfaceBlock = new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize,
2283 blockLayoutQualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002284 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier, arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002285
2286 TString symbolName = "";
2287 int symbolId = 0;
2288
Jamie Madill98493dd2013-07-08 14:39:03 -04002289 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002290 {
2291 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002292 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2293 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302294 TField *field = (*fieldList)[memberIndex];
2295 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002296
2297 // set parent pointer of the field variable
2298 fieldType->setInterfaceBlock(interfaceBlock);
2299
Arun Patole7e7e68d2015-05-22 12:02:25 +05302300 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002301 fieldVariable->setQualifier(typeQualifier.qualifier);
2302
Arun Patole7e7e68d2015-05-22 12:02:25 +05302303 if (!symbolTable.declare(fieldVariable))
2304 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002305 error(field->line(), "redefinition", field->name().c_str(), "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002306 recover();
2307 }
2308 }
2309 }
2310 else
2311 {
Olli Etuahoe0f623a2015-07-10 11:58:30 +03002312 if (reservedErrorCheck(instanceLine, *instanceName))
2313 recover();
2314
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002315 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302316 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002317 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002318
Arun Patole7e7e68d2015-05-22 12:02:25 +05302319 if (!symbolTable.declare(instanceTypeDef))
2320 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002321 error(instanceLine, "redefinition", instanceName->c_str(), "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002322 recover();
2323 }
2324
2325 symbolId = instanceTypeDef->getUniqueId();
2326 symbolName = instanceTypeDef->getName();
2327 }
2328
Arun Patole7e7e68d2015-05-22 12:02:25 +05302329 TIntermAggregate *aggregate = intermediate.makeAggregate(intermediate.addSymbol(symbolId, symbolName,
2330 interfaceBlockType,
2331 typeQualifier.line),
2332 nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002333 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04002334
2335 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002336 return aggregate;
2337}
2338
Arun Patole7e7e68d2015-05-22 12:02:25 +05302339bool TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002340{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002341 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002342
2343 // Embedded structure definitions are not supported per GLSL ES spec.
2344 // They aren't allowed in GLSL either, but we need to detect this here
2345 // so we don't rely on the GLSL compiler to catch it.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302346 if (mStructNestingLevel > 1)
2347 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002348 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002349 return true;
2350 }
2351
2352 return false;
2353}
2354
2355void TParseContext::exitStructDeclaration()
2356{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002357 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002358}
2359
2360namespace {
2361
2362const int kWebGLMaxStructNesting = 4;
2363
2364} // namespace
2365
Arun Patole7e7e68d2015-05-22 12:02:25 +05302366bool TParseContext::structNestingErrorCheck(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002367{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302368 if (!IsWebGLBasedSpec(mShaderSpec))
2369 {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002370 return false;
2371 }
2372
Arun Patole7e7e68d2015-05-22 12:02:25 +05302373 if (field.type()->getBasicType() != EbtStruct)
2374 {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002375 return false;
2376 }
2377
2378 // We're already inside a structure definition at this point, so add
2379 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302380 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2381 {
Jamie Madill41a49272014-03-18 16:10:13 -04002382 std::stringstream reasonStream;
2383 reasonStream << "Reference of struct type "
2384 << field.type()->getStruct()->name().c_str()
2385 << " exceeds maximum allowed nesting level of "
2386 << kWebGLMaxStructNesting;
2387 std::string reason = reasonStream.str();
2388 error(line, reason.c_str(), field.name().c_str(), "");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002389 return true;
2390 }
2391
2392 return false;
2393}
2394
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002395//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002396// Parse an array index expression
2397//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302398TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression, const TSourceLoc &location,
2399 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002400{
2401 TIntermTyped *indexedExpression = NULL;
2402
2403 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2404 {
2405 if (baseExpression->getAsSymbolNode())
2406 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302407 error(location, " left of '[' is not of type array, matrix, or vector ",
2408 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002409 }
2410 else
2411 {
2412 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2413 }
2414 recover();
2415 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002416
Jamie Madill21c1e452014-12-29 11:33:41 -05002417 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2418
2419 if (indexExpression->getQualifier() == EvqConst && indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002420 {
Jamie Madill21c1e452014-12-29 11:33:41 -05002421 int index = indexConstantUnion->getIConst(0);
Jamie Madill7164cf42013-07-08 13:30:59 -04002422 if (index < 0)
2423 {
2424 std::stringstream infoStream;
2425 infoStream << index;
2426 std::string info = infoStream.str();
2427 error(location, "negative index", info.c_str());
2428 recover();
2429 index = 0;
2430 }
2431 if (baseExpression->getType().getQualifier() == EvqConst)
2432 {
2433 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002434 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002435 // constant folding for arrays
2436 indexedExpression = addConstArrayNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002437 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002438 else if (baseExpression->isVector())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002439 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002440 // constant folding for vectors
2441 TVectorFields fields;
2442 fields.num = 1;
2443 fields.offsets[0] = index; // need to do it this way because v.xy sends fields integer array
2444 indexedExpression = addConstVectorNode(fields, baseExpression, location);
2445 }
2446 else if (baseExpression->isMatrix())
2447 {
2448 // constant folding for matrices
2449 indexedExpression = addConstMatrixNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002450 }
2451 }
2452 else
2453 {
Jamie Madillb11e2482015-05-04 14:21:22 -04002454 int safeIndex = -1;
2455
Jamie Madill7164cf42013-07-08 13:30:59 -04002456 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002457 {
Jamie Madill18464b52013-07-08 14:01:55 -04002458 if (index >= baseExpression->getType().getArraySize())
Jamie Madill7164cf42013-07-08 13:30:59 -04002459 {
2460 std::stringstream extraInfoStream;
2461 extraInfoStream << "array index out of range '" << index << "'";
2462 std::string extraInfo = extraInfoStream.str();
2463 error(location, "", "[", extraInfo.c_str());
2464 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002465 safeIndex = baseExpression->getType().getArraySize() - 1;
Jamie Madill7164cf42013-07-08 13:30:59 -04002466 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302467 else if (baseExpression->getQualifier() == EvqFragData && index > 0 &&
2468 !isExtensionEnabled("GL_EXT_draw_buffers"))
Jamie Madill5d287f52013-07-12 15:38:19 -04002469 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302470 error(location, "",
2471 "[", "array indexes for gl_FragData must be zero when GL_EXT_draw_buffers is disabled");
Jamie Madill5d287f52013-07-12 15:38:19 -04002472 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002473 safeIndex = 0;
Jamie Madill5d287f52013-07-12 15:38:19 -04002474 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002475 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302476 else if ((baseExpression->isVector() || baseExpression->isMatrix()) &&
2477 baseExpression->getType().getNominalSize() <= index)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002478 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002479 std::stringstream extraInfoStream;
2480 extraInfoStream << "field selection out of range '" << index << "'";
2481 std::string extraInfo = extraInfoStream.str();
2482 error(location, "", "[", extraInfo.c_str());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002483 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002484 safeIndex = baseExpression->getType().getNominalSize() - 1;
Jamie Madill46131a32013-06-20 11:55:50 -04002485 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002486
Jamie Madillb11e2482015-05-04 14:21:22 -04002487 // Don't modify the data of the previous constant union, because it can point
2488 // to builtins, like gl_MaxDrawBuffers. Instead use a new sanitized object.
2489 if (safeIndex != -1)
2490 {
2491 TConstantUnion *safeConstantUnion = new TConstantUnion();
2492 safeConstantUnion->setIConst(safeIndex);
2493 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
2494 }
2495
Jamie Madill7164cf42013-07-08 13:30:59 -04002496 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002497 }
2498 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002499 else
2500 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002501 if (baseExpression->isInterfaceBlock())
Jamie Madill7164cf42013-07-08 13:30:59 -04002502 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302503 error(location, "",
2504 "[", "array indexes for interface blocks arrays must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002505 recover();
2506 }
Jamie Madill19571812013-08-12 15:26:34 -07002507 else if (baseExpression->getQualifier() == EvqFragmentOut)
Jamie Madill7164cf42013-07-08 13:30:59 -04002508 {
Jamie Madill19571812013-08-12 15:26:34 -07002509 error(location, "", "[", "array indexes for fragment outputs must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002510 recover();
2511 }
2512
2513 indexedExpression = intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
2514 }
2515
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002516 if (indexedExpression == 0)
2517 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002518 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002519 unionArray->setFConst(0.0f);
2520 indexedExpression = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
2521 }
2522 else if (baseExpression->isArray())
2523 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002524 const TType &baseType = baseExpression->getType();
2525 if (baseType.getStruct())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002526 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002527 TType copyOfType(baseType.getStruct());
2528 indexedExpression->setType(copyOfType);
2529 }
2530 else if (baseType.isInterfaceBlock())
2531 {
2532 TType copyOfType(baseType.getInterfaceBlock(), baseType.getQualifier(), baseType.getLayoutQualifier(), 0);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002533 indexedExpression->setType(copyOfType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002534 }
2535 else
2536 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302537 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2538 EvqTemporary, static_cast<unsigned char>(baseExpression->getNominalSize()),
2539 static_cast<unsigned char>(baseExpression->getSecondarySize())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002540 }
2541
2542 if (baseExpression->getType().getQualifier() == EvqConst)
2543 {
2544 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2545 }
2546 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002547 else if (baseExpression->isMatrix())
2548 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002549 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302550 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2551 qualifier, static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002552 }
2553 else if (baseExpression->isVector())
2554 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002555 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2556 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002557 }
2558 else
2559 {
2560 indexedExpression->setType(baseExpression->getType());
2561 }
2562
2563 return indexedExpression;
2564}
2565
Arun Patole7e7e68d2015-05-22 12:02:25 +05302566TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression, const TSourceLoc &dotLocation,
2567 const TString &fieldString, const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002568{
2569 TIntermTyped *indexedExpression = NULL;
2570
2571 if (baseExpression->isArray())
2572 {
2573 error(fieldLocation, "cannot apply dot operator to an array", ".");
2574 recover();
2575 }
2576
2577 if (baseExpression->isVector())
2578 {
2579 TVectorFields fields;
2580 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields, fieldLocation))
2581 {
2582 fields.num = 1;
2583 fields.offsets[0] = 0;
2584 recover();
2585 }
2586
2587 if (baseExpression->getType().getQualifier() == EvqConst)
2588 {
2589 // constant folding for vector fields
2590 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
2591 if (indexedExpression == 0)
2592 {
2593 recover();
2594 indexedExpression = baseExpression;
2595 }
2596 else
2597 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302598 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2599 EvqConst, (unsigned char) (fieldString).size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002600 }
2601 }
2602 else
2603 {
2604 TString vectorString = fieldString;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302605 TIntermTyped *index = intermediate.addSwizzle(fields, fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002606 indexedExpression = intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302607 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2608 EvqTemporary, (unsigned char) vectorString.size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002609 }
2610 }
2611 else if (baseExpression->isMatrix())
2612 {
2613 TMatrixFields fields;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302614 if (!parseMatrixFields(fieldString, baseExpression->getCols(), baseExpression->getRows(), fields,
2615 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002616 {
2617 fields.wholeRow = false;
2618 fields.wholeCol = false;
2619 fields.row = 0;
2620 fields.col = 0;
2621 recover();
2622 }
2623
2624 if (fields.wholeRow || fields.wholeCol)
2625 {
2626 error(dotLocation, " non-scalar fields not implemented yet", ".");
2627 recover();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002628 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002629 unionArray->setIConst(0);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302630 TIntermTyped *index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst),
2631 fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002632 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302633 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2634 EvqTemporary, static_cast<unsigned char>(baseExpression->getCols()),
2635 static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002636 }
2637 else
2638 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002639 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002640 unionArray->setIConst(fields.col * baseExpression->getRows() + fields.row);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302641 TIntermTyped *index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst),
2642 fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002643 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
2644 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision()));
2645 }
2646 }
2647 else if (baseExpression->getBasicType() == EbtStruct)
2648 {
2649 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302650 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04002651 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002652 {
2653 error(dotLocation, "structure has no fields", "Internal Error");
2654 recover();
2655 indexedExpression = baseExpression;
2656 }
2657 else
2658 {
2659 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002660 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002661 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002662 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002663 {
2664 fieldFound = true;
2665 break;
2666 }
2667 }
2668 if (fieldFound)
2669 {
2670 if (baseExpression->getType().getQualifier() == EvqConst)
2671 {
2672 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2673 if (indexedExpression == 0)
2674 {
2675 recover();
2676 indexedExpression = baseExpression;
2677 }
2678 else
2679 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002680 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002681 // change the qualifier of the return type, not of the structure field
2682 // as the structure definition is shared between various structures.
2683 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2684 }
2685 }
2686 else
2687 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002688 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002689 unionArray->setIConst(i);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302690 TIntermTyped *index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002691 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002692 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002693 }
2694 }
2695 else
2696 {
2697 error(dotLocation, " no such field in structure", fieldString.c_str());
2698 recover();
2699 indexedExpression = baseExpression;
2700 }
2701 }
2702 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002703 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002704 {
2705 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302706 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04002707 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002708 {
2709 error(dotLocation, "interface block has no fields", "Internal Error");
2710 recover();
2711 indexedExpression = baseExpression;
2712 }
2713 else
2714 {
2715 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002716 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002717 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002718 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002719 {
2720 fieldFound = true;
2721 break;
2722 }
2723 }
2724 if (fieldFound)
2725 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002726 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002727 unionArray->setIConst(i);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302728 TIntermTyped *index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
2729 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index,
2730 dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002731 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002732 }
2733 else
2734 {
2735 error(dotLocation, " no such field in interface block", fieldString.c_str());
2736 recover();
2737 indexedExpression = baseExpression;
2738 }
2739 }
2740 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002741 else
2742 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002743 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002744 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302745 error(dotLocation, " field selection requires structure, vector, or matrix on left hand side",
2746 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002747 }
2748 else
2749 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302750 error(dotLocation,
2751 " field selection requires structure, vector, matrix, or interface block on left hand side",
2752 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002753 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002754 recover();
2755 indexedExpression = baseExpression;
2756 }
2757
2758 return indexedExpression;
2759}
2760
Arun Patole7e7e68d2015-05-22 12:02:25 +05302761TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002762{
Jamie Madilla5efff92013-06-06 11:56:47 -04002763 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002764
Jamie Madilla5efff92013-06-06 11:56:47 -04002765 qualifier.location = -1;
2766 qualifier.matrixPacking = EmpUnspecified;
2767 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002768
2769 if (qualifierType == "shared")
2770 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002771 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002772 }
2773 else if (qualifierType == "packed")
2774 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002775 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002776 }
2777 else if (qualifierType == "std140")
2778 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002779 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002780 }
2781 else if (qualifierType == "row_major")
2782 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002783 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002784 }
2785 else if (qualifierType == "column_major")
2786 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002787 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002788 }
2789 else if (qualifierType == "location")
2790 {
2791 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "location requires an argument");
2792 recover();
2793 }
2794 else
2795 {
2796 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
2797 recover();
2798 }
2799
Jamie Madilla5efff92013-06-06 11:56:47 -04002800 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002801}
2802
Arun Patole7e7e68d2015-05-22 12:02:25 +05302803TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc &qualifierTypeLine,
2804 const TString &intValueString, int intValue,
2805 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002806{
Jamie Madilla5efff92013-06-06 11:56:47 -04002807 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002808
Jamie Madilla5efff92013-06-06 11:56:47 -04002809 qualifier.location = -1;
2810 qualifier.matrixPacking = EmpUnspecified;
2811 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002812
2813 if (qualifierType != "location")
2814 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302815 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
2816 "only location may have arguments");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002817 recover();
2818 }
2819 else
2820 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002821 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002822 if (intValue < 0)
2823 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002824 error(intValueLine, "out of range:", intValueString.c_str(), "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002825 recover();
2826 }
2827 else
2828 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002829 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002830 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002831 }
2832
Jamie Madilla5efff92013-06-06 11:56:47 -04002833 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002834}
2835
Jamie Madilla5efff92013-06-06 11:56:47 -04002836TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002837{
Jamie Madilla5efff92013-06-06 11:56:47 -04002838 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002839
Jamie Madilla5efff92013-06-06 11:56:47 -04002840 if (rightQualifier.location != -1)
2841 {
2842 joinedQualifier.location = rightQualifier.location;
2843 }
2844 if (rightQualifier.matrixPacking != EmpUnspecified)
2845 {
2846 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
2847 }
2848 if (rightQualifier.blockStorage != EbsUnspecified)
2849 {
2850 joinedQualifier.blockStorage = rightQualifier.blockStorage;
2851 }
2852
2853 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002854}
2855
Arun Patole7e7e68d2015-05-22 12:02:25 +05302856TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc,
2857 TQualifier interpolationQualifier,
2858 const TSourceLoc &storageLoc,
2859 TQualifier storageQualifier)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002860{
2861 TQualifier mergedQualifier = EvqSmoothIn;
2862
Arun Patole7e7e68d2015-05-22 12:02:25 +05302863 if (storageQualifier == EvqFragmentIn)
2864 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002865 if (interpolationQualifier == EvqSmooth)
2866 mergedQualifier = EvqSmoothIn;
2867 else if (interpolationQualifier == EvqFlat)
2868 mergedQualifier = EvqFlatIn;
2869 else UNREACHABLE();
2870 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302871 else if (storageQualifier == EvqCentroidIn)
2872 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002873 if (interpolationQualifier == EvqSmooth)
2874 mergedQualifier = EvqCentroidIn;
2875 else if (interpolationQualifier == EvqFlat)
2876 mergedQualifier = EvqFlatIn;
2877 else UNREACHABLE();
2878 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302879 else if (storageQualifier == EvqVertexOut)
2880 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002881 if (interpolationQualifier == EvqSmooth)
2882 mergedQualifier = EvqSmoothOut;
2883 else if (interpolationQualifier == EvqFlat)
2884 mergedQualifier = EvqFlatOut;
2885 else UNREACHABLE();
2886 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302887 else if (storageQualifier == EvqCentroidOut)
2888 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002889 if (interpolationQualifier == EvqSmooth)
2890 mergedQualifier = EvqCentroidOut;
2891 else if (interpolationQualifier == EvqFlat)
2892 mergedQualifier = EvqFlatOut;
2893 else UNREACHABLE();
2894 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302895 else
2896 {
2897 error(interpolationLoc, "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier",
2898 getInterpolationString(interpolationQualifier));
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002899 recover();
2900
2901 mergedQualifier = storageQualifier;
2902 }
2903
2904 TPublicType type;
2905 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
2906 return type;
2907}
2908
Arun Patole7e7e68d2015-05-22 12:02:25 +05302909TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier, TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002910{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002911 if (voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier.type))
2912 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002913 recover();
2914 }
2915
Arun Patole7e7e68d2015-05-22 12:02:25 +05302916 for (unsigned int i = 0; i < fieldList->size(); ++i)
2917 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002918 //
2919 // Careful not to replace already known aspects of type, like array-ness
2920 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05302921 TType *type = (*fieldList)[i]->type();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002922 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002923 type->setPrimarySize(typeSpecifier.primarySize);
2924 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002925 type->setPrecision(typeSpecifier.precision);
2926 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002927 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002928
2929 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05302930 if (type->isArray())
2931 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002932 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
2933 recover();
2934 }
2935 if (typeSpecifier.array)
2936 type->setArraySize(typeSpecifier.arraySize);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302937 if (typeSpecifier.userDef)
2938 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002939 type->setStruct(typeSpecifier.userDef->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002940 }
2941
Arun Patole7e7e68d2015-05-22 12:02:25 +05302942 if (structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i]))
2943 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002944 recover();
2945 }
2946 }
2947
Jamie Madill98493dd2013-07-08 14:39:03 -04002948 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002949}
2950
Arun Patole7e7e68d2015-05-22 12:02:25 +05302951TPublicType TParseContext::addStructure(const TSourceLoc &structLine, const TSourceLoc &nameLine,
2952 const TString *structName, TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002953{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302954 TStructure *structure = new TStructure(structName, fieldList);
2955 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002956
Jamie Madill9b820842015-02-12 10:40:10 -05002957 // Store a bool in the struct if we're at global scope, to allow us to
2958 // skip the local struct scoping workaround in HLSL.
Jamie Madillb960cc42015-02-12 15:33:20 +00002959 structure->setUniqueId(TSymbolTable::nextUniqueId());
Jamie Madill9b820842015-02-12 10:40:10 -05002960 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04002961
Jamie Madill98493dd2013-07-08 14:39:03 -04002962 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002963 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002964 if (reservedErrorCheck(nameLine, *structName))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002965 {
2966 recover();
2967 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302968 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
2969 if (!symbolTable.declare(userTypeDef))
2970 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002971 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002972 recover();
2973 }
2974 }
2975
2976 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04002977 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002978 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002979 const TField &field = *(*fieldList)[typeListIndex];
2980 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002981 switch (qualifier)
2982 {
2983 case EvqGlobal:
2984 case EvqTemporary:
2985 break;
2986 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04002987 error(field.line(), "invalid qualifier on struct member", getQualifierString(qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002988 recover();
2989 break;
2990 }
2991 }
2992
2993 TPublicType publicType;
2994 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04002995 publicType.userDef = structureType;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002996 exitStructDeclaration();
2997
2998 return publicType;
2999}
3000
Olli Etuahoa3a36662015-02-17 13:46:51 +02003001TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init, TIntermAggregate *statementList, const TSourceLoc &loc)
3002{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003003 TBasicType switchType = init->getBasicType();
3004 if ((switchType != EbtInt && switchType != EbtUInt) ||
3005 init->isMatrix() ||
3006 init->isArray() ||
3007 init->isVector())
3008 {
3009 error(init->getLine(), "init-expression in a switch statement must be a scalar integer", "switch");
3010 recover();
3011 return nullptr;
3012 }
3013
Olli Etuahoac5274d2015-02-20 10:19:08 +02003014 if (statementList)
3015 {
3016 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
3017 {
3018 recover();
3019 return nullptr;
3020 }
3021 }
3022
Olli Etuahoa3a36662015-02-17 13:46:51 +02003023 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3024 if (node == nullptr)
3025 {
3026 error(loc, "erroneous switch statement", "switch");
3027 recover();
3028 return nullptr;
3029 }
3030 return node;
3031}
3032
3033TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3034{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003035 if (mSwitchNestingLevel == 0)
3036 {
3037 error(loc, "case labels need to be inside switch statements", "case");
3038 recover();
3039 return nullptr;
3040 }
3041 if (condition == nullptr)
3042 {
3043 error(loc, "case label must have a condition", "case");
3044 recover();
3045 return nullptr;
3046 }
3047 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
3048 condition->isMatrix() ||
3049 condition->isArray() ||
3050 condition->isVector())
3051 {
3052 error(condition->getLine(), "case label must be a scalar integer", "case");
3053 recover();
3054 }
3055 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
3056 if (conditionConst == nullptr)
3057 {
3058 error(condition->getLine(), "case label must be constant", "case");
3059 recover();
3060 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003061 TIntermCase *node = intermediate.addCase(condition, loc);
3062 if (node == nullptr)
3063 {
3064 error(loc, "erroneous case statement", "case");
3065 recover();
3066 return nullptr;
3067 }
3068 return node;
3069}
3070
3071TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3072{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003073 if (mSwitchNestingLevel == 0)
3074 {
3075 error(loc, "default labels need to be inside switch statements", "default");
3076 recover();
3077 return nullptr;
3078 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003079 TIntermCase *node = intermediate.addCase(nullptr, loc);
3080 if (node == nullptr)
3081 {
3082 error(loc, "erroneous default statement", "default");
3083 recover();
3084 return nullptr;
3085 }
3086 return node;
3087}
3088
Olli Etuahof6c694b2015-03-26 14:50:53 +02003089TIntermTyped *TParseContext::createUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc,
3090 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003091{
3092 if (child == nullptr)
3093 {
3094 return nullptr;
3095 }
3096
3097 switch (op)
3098 {
3099 case EOpLogicalNot:
3100 if (child->getBasicType() != EbtBool ||
3101 child->isMatrix() ||
3102 child->isArray() ||
3103 child->isVector())
3104 {
3105 return nullptr;
3106 }
3107 break;
3108 case EOpBitwiseNot:
3109 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3110 child->isMatrix() ||
3111 child->isArray())
3112 {
3113 return nullptr;
3114 }
3115 break;
3116 case EOpPostIncrement:
3117 case EOpPreIncrement:
3118 case EOpPostDecrement:
3119 case EOpPreDecrement:
3120 case EOpNegative:
3121 case EOpPositive:
3122 if (child->getBasicType() == EbtStruct ||
Olli Etuahodca3e792015-03-26 13:24:04 +02003123 child->getBasicType() == EbtBool ||
Olli Etuaho69c11b52015-03-26 12:59:00 +02003124 child->isArray())
3125 {
3126 return nullptr;
3127 }
Olli Etuahodca3e792015-03-26 13:24:04 +02003128 // Operators for built-ins are already type checked against their prototype.
Olli Etuaho69c11b52015-03-26 12:59:00 +02003129 default:
3130 break;
3131 }
3132
Olli Etuahof6c694b2015-03-26 14:50:53 +02003133 return intermediate.addUnaryMath(op, child, loc, funcReturnType);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003134}
3135
Olli Etuaho09b22472015-02-11 11:47:26 +02003136TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3137{
Olli Etuahof6c694b2015-03-26 14:50:53 +02003138 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003139 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003140 {
3141 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
3142 recover();
3143 return child;
3144 }
3145 return node;
3146}
3147
3148TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3149{
3150 if (lValueErrorCheck(loc, GetOperatorString(op), child))
3151 recover();
3152 return addUnaryMath(op, child, loc);
3153}
3154
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003155bool TParseContext::binaryOpCommonCheck(TOperator op, TIntermTyped *left, TIntermTyped *right,
Olli Etuahod6b14282015-03-17 14:31:35 +02003156 const TSourceLoc &loc)
3157{
3158 if (left->isArray() || right->isArray())
3159 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003160 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003161 {
3162 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3163 return false;
3164 }
3165
3166 if (left->isArray() != right->isArray())
3167 {
3168 error(loc, "array / non-array mismatch", GetOperatorString(op));
3169 return false;
3170 }
3171
3172 switch (op)
3173 {
3174 case EOpEqual:
3175 case EOpNotEqual:
3176 case EOpAssign:
3177 case EOpInitialize:
3178 break;
3179 default:
3180 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3181 return false;
3182 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003183 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003184 if (left->getArraySize() != right->getArraySize())
3185 {
3186 error(loc, "array size mismatch", GetOperatorString(op));
3187 return false;
3188 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003189 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003190
3191 // Check ops which require integer / ivec parameters
3192 bool isBitShift = false;
3193 switch (op)
3194 {
3195 case EOpBitShiftLeft:
3196 case EOpBitShiftRight:
3197 case EOpBitShiftLeftAssign:
3198 case EOpBitShiftRightAssign:
3199 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3200 // check that the basic type is an integer type.
3201 isBitShift = true;
3202 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3203 {
3204 return false;
3205 }
3206 break;
3207 case EOpBitwiseAnd:
3208 case EOpBitwiseXor:
3209 case EOpBitwiseOr:
3210 case EOpBitwiseAndAssign:
3211 case EOpBitwiseXorAssign:
3212 case EOpBitwiseOrAssign:
3213 // It is enough to check the type of only one operand, since later it
3214 // is checked that the operand types match.
3215 if (!IsInteger(left->getBasicType()))
3216 {
3217 return false;
3218 }
3219 break;
3220 default:
3221 break;
3222 }
3223
3224 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3225 // So the basic type should usually match.
3226 if (!isBitShift && left->getBasicType() != right->getBasicType())
3227 {
3228 return false;
3229 }
3230
Olli Etuaho9dd217b2015-03-20 14:24:31 +02003231 // Check that type sizes match exactly on ops that require that.
Olli Etuahoff699002015-03-23 14:38:42 +02003232 // Also check restrictions for structs that contain arrays or samplers.
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003233 switch(op)
3234 {
3235 case EOpAssign:
3236 case EOpInitialize:
3237 case EOpEqual:
3238 case EOpNotEqual:
Olli Etuaho9dd217b2015-03-20 14:24:31 +02003239 // ESSL 1.00 sections 5.7, 5.8, 5.9
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003240 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
Olli Etuaho9dd217b2015-03-20 14:24:31 +02003241 {
3242 error(loc, "undefined operation for structs containing arrays", GetOperatorString(op));
3243 return false;
3244 }
Olli Etuahoff699002015-03-23 14:38:42 +02003245 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3246 // we interpret the spec so that this extends to structs containing samplers,
3247 // similarly to ESSL 1.00 spec.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003248 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
Olli Etuahoff699002015-03-23 14:38:42 +02003249 left->getType().isStructureContainingSamplers())
3250 {
3251 error(loc, "undefined operation for structs containing samplers", GetOperatorString(op));
3252 return false;
3253 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003254 case EOpLessThan:
3255 case EOpGreaterThan:
3256 case EOpLessThanEqual:
3257 case EOpGreaterThanEqual:
3258 if ((left->getNominalSize() != right->getNominalSize()) ||
3259 (left->getSecondarySize() != right->getSecondarySize()))
3260 {
3261 return false;
3262 }
3263 default:
3264 break;
3265 }
3266
Olli Etuahod6b14282015-03-17 14:31:35 +02003267 return true;
3268}
3269
Olli Etuahofc1806e2015-03-17 13:03:11 +02003270TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op, TIntermTyped *left, TIntermTyped *right,
3271 const TSourceLoc &loc)
3272{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003273 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003274 return nullptr;
3275
Olli Etuahofc1806e2015-03-17 13:03:11 +02003276 switch (op)
3277 {
3278 case EOpEqual:
3279 case EOpNotEqual:
Olli Etuahofc1806e2015-03-17 13:03:11 +02003280 break;
3281 case EOpLessThan:
3282 case EOpGreaterThan:
3283 case EOpLessThanEqual:
3284 case EOpGreaterThanEqual:
Olli Etuahod6b14282015-03-17 14:31:35 +02003285 ASSERT(!left->isArray() && !right->isArray());
3286 if (left->isMatrix() || left->isVector() ||
Olli Etuahofc1806e2015-03-17 13:03:11 +02003287 left->getBasicType() == EbtStruct)
3288 {
3289 return nullptr;
3290 }
3291 break;
3292 case EOpLogicalOr:
3293 case EOpLogicalXor:
3294 case EOpLogicalAnd:
Olli Etuahod6b14282015-03-17 14:31:35 +02003295 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003296 if (left->getBasicType() != EbtBool ||
Olli Etuahod6b14282015-03-17 14:31:35 +02003297 left->isMatrix() || left->isVector())
Olli Etuahofc1806e2015-03-17 13:03:11 +02003298 {
3299 return nullptr;
3300 }
3301 break;
3302 case EOpAdd:
3303 case EOpSub:
3304 case EOpDiv:
3305 case EOpMul:
Olli Etuahod6b14282015-03-17 14:31:35 +02003306 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003307 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool)
3308 {
3309 return nullptr;
3310 }
3311 break;
3312 case EOpIMod:
Olli Etuahod6b14282015-03-17 14:31:35 +02003313 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003314 // Note that this is only for the % operator, not for mod()
3315 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
3316 {
3317 return nullptr;
3318 }
3319 break;
3320 // Note that for bitwise ops, type checking is done in promote() to
3321 // share code between ops and compound assignment
3322 default:
3323 break;
3324 }
3325
Olli Etuahofc1806e2015-03-17 13:03:11 +02003326 return intermediate.addBinaryMath(op, left, right, loc);
3327}
3328
Olli Etuaho09b22472015-02-11 11:47:26 +02003329TIntermTyped *TParseContext::addBinaryMath(TOperator op, TIntermTyped *left, TIntermTyped *right,
3330 const TSourceLoc &loc)
3331{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003332 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003333 if (node == 0)
3334 {
3335 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(), right->getCompleteString());
3336 recover();
3337 return left;
3338 }
3339 return node;
3340}
3341
3342TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op, TIntermTyped *left, TIntermTyped *right,
3343 const TSourceLoc &loc)
3344{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003345 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003346 if (node == 0)
3347 {
3348 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(), right->getCompleteString());
3349 recover();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003350 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02003351 unionArray->setBConst(false);
3352 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), loc);
3353 }
3354 return node;
3355}
3356
Olli Etuahod6b14282015-03-17 14:31:35 +02003357TIntermTyped *TParseContext::createAssign(TOperator op, TIntermTyped *left, TIntermTyped *right,
3358 const TSourceLoc &loc)
3359{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003360 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003361 {
3362 return intermediate.addAssign(op, left, right, loc);
3363 }
3364 return nullptr;
3365}
3366
3367TIntermTyped *TParseContext::addAssign(TOperator op, TIntermTyped *left, TIntermTyped *right,
3368 const TSourceLoc &loc)
3369{
3370 TIntermTyped *node = createAssign(op, left, right, loc);
3371 if (node == nullptr)
3372 {
3373 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
3374 recover();
3375 return left;
3376 }
3377 return node;
3378}
3379
Olli Etuaho49300862015-02-20 14:54:49 +02003380TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3381{
3382 switch (op)
3383 {
3384 case EOpContinue:
3385 if (mLoopNestingLevel <= 0)
3386 {
3387 error(loc, "continue statement only allowed in loops", "");
3388 recover();
3389 }
3390 break;
3391 case EOpBreak:
Olli Etuaho53f076f2015-02-20 10:55:14 +02003392 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
Olli Etuaho49300862015-02-20 14:54:49 +02003393 {
Olli Etuaho53f076f2015-02-20 10:55:14 +02003394 error(loc, "break statement only allowed in loops and switch statements", "");
Olli Etuaho49300862015-02-20 14:54:49 +02003395 recover();
3396 }
3397 break;
3398 case EOpReturn:
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003399 if (mCurrentFunctionType->getBasicType() != EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02003400 {
3401 error(loc, "non-void function must return a value", "return");
3402 recover();
3403 }
3404 break;
3405 default:
3406 // No checks for discard
3407 break;
3408 }
3409 return intermediate.addBranch(op, loc);
3410}
3411
3412TIntermBranch *TParseContext::addBranch(TOperator op, TIntermTyped *returnValue, const TSourceLoc &loc)
3413{
3414 ASSERT(op == EOpReturn);
3415 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003416 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02003417 {
3418 error(loc, "void function cannot return a value", "return");
3419 recover();
3420 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003421 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02003422 {
3423 error(loc, "function return is not matching type:", "return");
3424 recover();
3425 }
3426 return intermediate.addBranch(op, returnValue, loc);
3427}
3428
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003429TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall, TIntermNode *paramNode, TIntermNode *thisNode,
3430 const TSourceLoc &loc, bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003431{
3432 *fatalError = false;
3433 TOperator op = fnCall->getBuiltInOp();
3434 TIntermTyped *callNode = nullptr;
3435
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003436 if (thisNode != nullptr)
3437 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003438 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho96e67382015-04-23 14:27:02 +03003439 int arraySize = 0;
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003440 TIntermTyped *typedThis = thisNode->getAsTyped();
3441 if (fnCall->getName() != "length")
3442 {
3443 error(loc, "invalid method", fnCall->getName().c_str());
3444 recover();
3445 }
3446 else if (paramNode != nullptr)
3447 {
3448 error(loc, "method takes no parameters", "length");
3449 recover();
3450 }
3451 else if (typedThis == nullptr || !typedThis->isArray())
3452 {
3453 error(loc, "length can only be called on arrays", "length");
3454 recover();
3455 }
3456 else
3457 {
Olli Etuaho96e67382015-04-23 14:27:02 +03003458 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03003459 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003460 {
Olli Etuaho39282e12015-04-23 15:41:48 +03003461 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003462 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03003463 // (func()).length()
3464 // (int[3](0, 1, 2)).length()
3465 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid expression.
3466 // It allows "An array name with the length method applied" in contrast to GLSL 4.4 spec section 5.9
3467 // which allows "An array, vector or matrix expression with the length method applied".
3468 error(loc, "length can only be called on array names, not on array expressions", "length");
3469 recover();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003470 }
3471 }
Olli Etuaho96e67382015-04-23 14:27:02 +03003472 unionArray->setIConst(arraySize);
3473 callNode = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003474 }
3475 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003476 {
3477 //
3478 // Then this should be a constructor.
3479 // Don't go through the symbol table for constructors.
3480 // Their parameters will be verified algorithmically.
3481 //
3482 TType type(EbtVoid, EbpUndefined); // use this to get the type back
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003483 if (!constructorErrorCheck(loc, paramNode, *fnCall, op, &type))
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003484 {
3485 //
3486 // It's a constructor, of type 'type'.
3487 //
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003488 callNode = addConstructor(paramNode, &type, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003489 }
Olli Etuaho72ba85b2015-03-04 14:23:26 +02003490
3491 if (callNode == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003492 {
3493 recover();
3494 callNode = intermediate.setAggregateOperator(nullptr, op, loc);
3495 }
3496 callNode->setType(type);
3497 }
3498 else
3499 {
3500 //
3501 // Not a constructor. Find it in the symbol table.
3502 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303503 const TFunction *fnCandidate;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003504 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003505 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003506 if (fnCandidate)
3507 {
3508 //
3509 // A declared function.
3510 //
3511 if (builtIn && !fnCandidate->getExtension().empty() &&
3512 extensionErrorCheck(loc, fnCandidate->getExtension()))
3513 {
3514 recover();
3515 }
3516 op = fnCandidate->getBuiltInOp();
3517 if (builtIn && op != EOpNull)
3518 {
3519 //
3520 // A function call mapped to a built-in operation.
3521 //
3522 if (fnCandidate->getParamCount() == 1)
3523 {
3524 //
3525 // Treat it like a built-in unary operator.
3526 //
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003527 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc, &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003528 if (callNode == nullptr)
3529 {
3530 std::stringstream extraInfoStream;
3531 extraInfoStream << "built in unary operator function. Type: "
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003532 << static_cast<TIntermTyped*>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003533 std::string extraInfo = extraInfoStream.str();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003534 error(paramNode->getLine(), " wrong operand type", "Internal Error", extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003535 *fatalError = true;
3536 return nullptr;
3537 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003538 }
3539 else
3540 {
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003541 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003542 aggregate->setType(fnCandidate->getReturnType());
3543 aggregate->setPrecisionFromChildren();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003544
3545 // Some built-in functions have out parameters too.
3546 functionCallLValueErrorCheck(fnCandidate, aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05303547
3548 // See if we can constant fold a built-in.
Olli Etuahob43846e2015-06-02 18:18:57 +03003549 TIntermTyped *foldedNode = intermediate.foldAggregateBuiltIn(aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05303550 if (foldedNode)
3551 {
Arun Patole274f0702015-05-05 13:33:30 +05303552 callNode = foldedNode;
3553 }
Olli Etuahob43846e2015-06-02 18:18:57 +03003554 else
3555 {
3556 callNode = aggregate;
3557 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003558 }
3559 }
3560 else
3561 {
3562 // This is a real function call
3563
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003564 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003565 aggregate->setType(fnCandidate->getReturnType());
3566
3567 // this is how we know whether the given function is a builtIn function or a user defined function
3568 // if builtIn == false, it's a userDefined -> could be an overloaded builtIn function also
3569 // if builtIn == true, it's definitely a builtIn function with EOpNull
3570 if (!builtIn)
3571 aggregate->setUserDefined();
3572 aggregate->setName(fnCandidate->getMangledName());
Corentin Wallez71d147f2015-02-11 11:15:24 -08003573 aggregate->setFunctionId(fnCandidate->getUniqueId());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003574
3575 // This needs to happen after the name is set
3576 if (builtIn)
3577 aggregate->setBuiltInFunctionPrecision();
3578
3579 callNode = aggregate;
3580
3581 functionCallLValueErrorCheck(fnCandidate, aggregate);
3582 }
3583 }
3584 else
3585 {
3586 // error message was put out by findFunction()
3587 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003588 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003589 unionArray->setFConst(0.0f);
3590 callNode = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpUndefined, EvqConst), loc);
3591 recover();
3592 }
3593 }
3594 delete fnCall;
3595 return callNode;
3596}
3597
Olli Etuaho52901742015-04-15 13:42:45 +03003598TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond, TIntermTyped *trueBlock, TIntermTyped *falseBlock,
3599 const TSourceLoc &loc)
3600{
3601 if (boolErrorCheck(loc, cond))
3602 recover();
3603
3604 if (trueBlock->getType() != falseBlock->getType())
3605 {
3606 binaryOpError(loc, ":", trueBlock->getCompleteString(), falseBlock->getCompleteString());
3607 recover();
3608 return falseBlock;
3609 }
Olli Etuahoa2d53032015-04-15 14:14:44 +03003610 // ESSL1 sections 5.2 and 5.7:
3611 // ESSL3 section 5.7:
3612 // Ternary operator is not among the operators allowed for structures/arrays.
3613 if (trueBlock->isArray() || trueBlock->getBasicType() == EbtStruct)
3614 {
3615 error(loc, "ternary operator is not allowed for structures or arrays", ":");
3616 recover();
3617 return falseBlock;
3618 }
Olli Etuaho52901742015-04-15 13:42:45 +03003619 return intermediate.addSelection(cond, trueBlock, falseBlock, loc);
3620}
Olli Etuaho49300862015-02-20 14:54:49 +02003621
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003622//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003623// Parse an array of strings using yyparse.
3624//
3625// Returns 0 for success.
3626//
Arun Patole7e7e68d2015-05-22 12:02:25 +05303627int PaParseStrings(size_t count, const char *const string[], const int length[],
3628 TParseContext *context)
3629{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003630 if ((count == 0) || (string == NULL))
3631 return 1;
3632
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003633 if (glslang_initialize(context))
3634 return 1;
3635
alokp@chromium.org408c45e2012-04-05 15:54:43 +00003636 int error = glslang_scan(count, string, length, context);
3637 if (!error)
3638 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003639
alokp@chromium.org73bc2982012-06-19 18:48:05 +00003640 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00003641
alokp@chromium.org6b495712012-06-29 00:06:58 +00003642 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003643}
3644
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003645
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00003646