blob: 1d017caa1788fd697fc5af400330c4efae0aec2b [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
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001355 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001356 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001357 if (typeSpecifier.array)
1358 {
1359 error(typeSpecifier.line, "not supported", "first-class array");
1360 recover();
1361 returnType.clearArrayness();
1362 }
1363
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 {
Olli Etuahoabb0c382015-07-13 12:01:12 +03001379 if (!layoutQualifier.isEmpty())
1380 {
1381 if (globalErrorCheck(typeSpecifier.line, symbolTable.atGlobalLevel(), "layout"))
1382 {
1383 recover();
1384 }
1385 }
Jamie Madillb120eac2013-06-12 14:08:13 -04001386 switch (qualifier)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001387 {
Jamie Madill19571812013-08-12 15:26:34 -07001388 case EvqSmoothIn:
1389 case EvqSmoothOut:
1390 case EvqVertexOut:
1391 case EvqFragmentIn:
1392 case EvqCentroidOut:
1393 case EvqCentroidIn:
1394 if (typeSpecifier.type == EbtBool)
1395 {
1396 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1397 recover();
1398 }
1399 if (typeSpecifier.type == EbtInt || typeSpecifier.type == EbtUInt)
1400 {
1401 error(typeSpecifier.line, "must use 'flat' interpolation here", getQualifierString(qualifier));
1402 recover();
1403 }
1404 break;
1405
1406 case EvqVertexIn:
1407 case EvqFragmentOut:
1408 case EvqFlatIn:
1409 case EvqFlatOut:
Jamie Madillb120eac2013-06-12 14:08:13 -04001410 if (typeSpecifier.type == EbtBool)
1411 {
1412 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1413 recover();
1414 }
1415 break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001416
Jamie Madillb120eac2013-06-12 14:08:13 -04001417 default: break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001418 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001419 }
1420
1421 return returnType;
1422}
1423
Olli Etuahofa33d582015-04-09 14:33:12 +03001424TIntermAggregate *TParseContext::parseSingleDeclaration(TPublicType &publicType,
1425 const TSourceLoc &identifierOrTypeLocation,
1426 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001427{
Olli Etuahofa33d582015-04-09 14:33:12 +03001428 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001429
Olli Etuahobab4c082015-04-24 16:38:49 +03001430 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03001431
Olli Etuahobab4c082015-04-24 16:38:49 +03001432 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1433
1434 if (emptyDeclaration)
1435 {
1436 if (publicType.isUnsizedArray())
1437 {
1438 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an error.
1439 // It is assumed that this applies to empty declarations as well.
1440 error(identifierOrTypeLocation, "empty array declaration needs to specify a size", identifier.c_str());
1441 }
1442 }
1443 else
Jamie Madill60ed9812013-06-06 11:56:46 -04001444 {
Olli Etuahofa33d582015-04-09 14:33:12 +03001445 if (singleDeclarationErrorCheck(publicType, identifierOrTypeLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001446 recover();
1447
Olli Etuaho376f1b52015-04-13 13:23:41 +03001448 if (nonInitErrorCheck(identifierOrTypeLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001449 recover();
1450
Olli Etuaho2935c582015-04-08 14:32:06 +03001451 TVariable *variable = nullptr;
Olli Etuahofa33d582015-04-09 14:33:12 +03001452 if (!declareVariable(identifierOrTypeLocation, identifier, TType(publicType), &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001453 recover();
1454
1455 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001456 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001457 }
1458
Olli Etuahoe7847b02015-03-16 11:56:12 +02001459 return intermediate.makeAggregate(symbol, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001460}
1461
Olli Etuahoe7847b02015-03-16 11:56:12 +02001462TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1463 const TSourceLoc &identifierLocation,
1464 const TString &identifier,
1465 const TSourceLoc &indexLocation,
1466 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001467{
Olli Etuahofa33d582015-04-09 14:33:12 +03001468 mDeferredSingleDeclarationErrorCheck = false;
1469
1470 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001471 recover();
1472
Olli Etuaho376f1b52015-04-13 13:23:41 +03001473 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001474 recover();
1475
1476 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1477 {
1478 recover();
1479 }
1480
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001481 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001482
1483 int size;
1484 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1485 {
1486 recover();
1487 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001488 // Make the type an array even if size check failed.
1489 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1490 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001491
Olli Etuaho2935c582015-04-08 14:32:06 +03001492 TVariable *variable = nullptr;
1493 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001494 recover();
1495
Olli Etuahoe7847b02015-03-16 11:56:12 +02001496 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001497 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001498 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001499
Olli Etuahoe7847b02015-03-16 11:56:12 +02001500 return intermediate.makeAggregate(symbol, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001501}
1502
Jamie Madill06145232015-05-13 13:10:01 -04001503TIntermAggregate *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001504 const TSourceLoc &identifierLocation,
1505 const TString &identifier,
1506 const TSourceLoc &initLocation,
1507 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001508{
Olli Etuahofa33d582015-04-09 14:33:12 +03001509 mDeferredSingleDeclarationErrorCheck = false;
1510
1511 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001512 recover();
1513
Olli Etuahoe7847b02015-03-16 11:56:12 +02001514 TIntermNode *intermNode = nullptr;
1515 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04001516 {
1517 //
1518 // Build intermediate representation
1519 //
Olli Etuahoe7847b02015-03-16 11:56:12 +02001520 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001521 }
1522 else
1523 {
1524 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001525 return nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001526 }
1527}
1528
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001529TIntermAggregate *TParseContext::parseSingleArrayInitDeclaration(TPublicType &publicType,
1530 const TSourceLoc &identifierLocation,
1531 const TString &identifier,
1532 const TSourceLoc &indexLocation,
1533 TIntermTyped *indexExpression,
1534 const TSourceLoc &initLocation,
1535 TIntermTyped *initializer)
1536{
1537 mDeferredSingleDeclarationErrorCheck = false;
1538
1539 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1540 recover();
1541
1542 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1543 {
1544 recover();
1545 }
1546
1547 TPublicType arrayType(publicType);
1548
Olli Etuaho376f1b52015-04-13 13:23:41 +03001549 int size = 0;
1550 // If indexExpression is nullptr, then the array will eventually get its size implicitly from the initializer.
1551 if (indexExpression != nullptr && arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001552 {
1553 recover();
1554 }
1555 // Make the type an array even if size check failed.
1556 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1557 arrayType.setArraySize(size);
1558
1559 // initNode will correspond to the whole of "type b[n] = initializer".
1560 TIntermNode *initNode = nullptr;
1561 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1562 {
1563 return initNode ? intermediate.makeAggregate(initNode, initLocation) : nullptr;
1564 }
1565 else
1566 {
1567 recover();
1568 return nullptr;
1569 }
1570}
1571
Olli Etuahoe7847b02015-03-16 11:56:12 +02001572TIntermAggregate *TParseContext::parseInvariantDeclaration(const TSourceLoc &invariantLoc,
Jamie Madill47e3ec02014-08-20 16:38:33 -04001573 const TSourceLoc &identifierLoc,
1574 const TString *identifier,
1575 const TSymbol *symbol)
1576{
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001577 // invariant declaration
Jamie Madill47e3ec02014-08-20 16:38:33 -04001578 if (globalErrorCheck(invariantLoc, symbolTable.atGlobalLevel(), "invariant varying"))
1579 {
1580 recover();
1581 }
1582
1583 if (!symbol)
1584 {
1585 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
1586 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001587 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001588 }
1589 else
1590 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001591 const TString kGlFrontFacing("gl_FrontFacing");
1592 if (*identifier == kGlFrontFacing)
1593 {
1594 error(identifierLoc, "identifier should not be declared as invariant", identifier->c_str());
1595 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001596 return nullptr;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001597 }
Jamie Madill2c433252014-12-03 12:36:54 -05001598 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001599 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1600 ASSERT(variable);
1601 const TType &type = variable->getType();
1602 TIntermSymbol *intermSymbol = intermediate.addSymbol(variable->getUniqueId(),
1603 *identifier, type, identifierLoc);
1604
1605 TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc);
1606 aggregate->setOp(EOpInvariantDeclaration);
1607 return aggregate;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001608 }
1609}
1610
Olli Etuahoe7847b02015-03-16 11:56:12 +02001611TIntermAggregate *TParseContext::parseDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1612 const TSourceLoc &identifierLocation, const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001613{
Olli Etuahofa33d582015-04-09 14:33:12 +03001614 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1615 if (mDeferredSingleDeclarationErrorCheck)
1616 {
1617 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1618 recover();
1619 mDeferredSingleDeclarationErrorCheck = false;
1620 }
1621
Jamie Madill0bd18df2013-06-20 11:55:52 -04001622 if (locationDeclaratorListCheck(identifierLocation, publicType))
1623 recover();
1624
Olli Etuaho376f1b52015-04-13 13:23:41 +03001625 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001626 recover();
1627
Olli Etuaho2935c582015-04-08 14:32:06 +03001628 TVariable *variable = nullptr;
1629 if (!declareVariable(identifierLocation, identifier, TType(publicType), &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001630 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001631
1632 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1633 if (variable && symbol)
Jamie Madill502d66f2013-06-20 11:55:52 -04001634 symbol->setId(variable->getUniqueId());
1635
Olli Etuahoe7847b02015-03-16 11:56:12 +02001636 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001637}
1638
Olli Etuahoe7847b02015-03-16 11:56:12 +02001639TIntermAggregate *TParseContext::parseArrayDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1640 const TSourceLoc &identifierLocation, const TString &identifier,
1641 const TSourceLoc &arrayLocation, TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001642{
Olli Etuahofa33d582015-04-09 14:33:12 +03001643 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1644 if (mDeferredSingleDeclarationErrorCheck)
1645 {
1646 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1647 recover();
1648 mDeferredSingleDeclarationErrorCheck = false;
1649 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001650
Jamie Madill0bd18df2013-06-20 11:55:52 -04001651 if (locationDeclaratorListCheck(identifierLocation, publicType))
1652 recover();
1653
Olli Etuaho376f1b52015-04-13 13:23:41 +03001654 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001655 recover();
1656
1657 if (arrayTypeErrorCheck(arrayLocation, publicType) || arrayQualifierErrorCheck(arrayLocation, publicType))
1658 {
1659 recover();
1660 }
Olli Etuaho93a90fd2015-04-07 18:14:07 +03001661 else
Jamie Madill502d66f2013-06-20 11:55:52 -04001662 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001663 TType arrayType = TType(publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04001664 int size;
1665 if (arraySizeErrorCheck(arrayLocation, indexExpression, size))
Olli Etuahoe7847b02015-03-16 11:56:12 +02001666 {
Jamie Madill502d66f2013-06-20 11:55:52 -04001667 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001668 }
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001669 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001670
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001671 TVariable *variable = nullptr;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001672 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001673 recover();
Jamie Madill502d66f2013-06-20 11:55:52 -04001674
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001675 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
1676 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001677 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001678
1679 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001680 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001681
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001682 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001683}
1684
Jamie Madill06145232015-05-13 13:10:01 -04001685TIntermAggregate *TParseContext::parseInitDeclarator(const TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001686 const TSourceLoc &identifierLocation, const TString &identifier,
1687 const TSourceLoc &initLocation, TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001688{
Olli Etuahofa33d582015-04-09 14:33:12 +03001689 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1690 if (mDeferredSingleDeclarationErrorCheck)
1691 {
1692 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1693 recover();
1694 mDeferredSingleDeclarationErrorCheck = false;
1695 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001696
Jamie Madill0bd18df2013-06-20 11:55:52 -04001697 if (locationDeclaratorListCheck(identifierLocation, publicType))
1698 recover();
1699
Olli Etuahoe7847b02015-03-16 11:56:12 +02001700 TIntermNode *intermNode = nullptr;
1701 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04001702 {
1703 //
1704 // build the intermediate representation
1705 //
1706 if (intermNode)
1707 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001708 return intermediate.growAggregate(aggregateDeclaration, intermNode, initLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001709 }
1710 else
1711 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001712 return aggregateDeclaration;
Jamie Madill502d66f2013-06-20 11:55:52 -04001713 }
1714 }
1715 else
1716 {
1717 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001718 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001719 }
1720}
1721
Jamie Madill06145232015-05-13 13:10:01 -04001722TIntermAggregate *TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001723 TIntermAggregate *aggregateDeclaration,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301724 const TSourceLoc &identifierLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001725 const TString &identifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301726 const TSourceLoc &indexLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001727 TIntermTyped *indexExpression,
1728 const TSourceLoc &initLocation, TIntermTyped *initializer)
1729{
1730 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1731 if (mDeferredSingleDeclarationErrorCheck)
1732 {
1733 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1734 recover();
1735 mDeferredSingleDeclarationErrorCheck = false;
1736 }
1737
1738 if (locationDeclaratorListCheck(identifierLocation, publicType))
1739 recover();
1740
1741 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1742 {
1743 recover();
1744 }
1745
1746 TPublicType arrayType(publicType);
1747
Olli Etuaho376f1b52015-04-13 13:23:41 +03001748 int size = 0;
1749 // If indexExpression is nullptr, then the array will eventually get its size implicitly from the initializer.
1750 if (indexExpression != nullptr && arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001751 {
1752 recover();
1753 }
1754 // Make the type an array even if size check failed.
1755 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1756 arrayType.setArraySize(size);
1757
1758 // initNode will correspond to the whole of "b[n] = initializer".
1759 TIntermNode *initNode = nullptr;
1760 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1761 {
1762 if (initNode)
1763 {
1764 return intermediate.growAggregate(aggregateDeclaration, initNode, initLocation);
1765 }
1766 else
1767 {
1768 return aggregateDeclaration;
1769 }
1770 }
1771 else
1772 {
1773 recover();
1774 return nullptr;
1775 }
1776}
1777
Jamie Madilla295edf2013-06-06 11:56:48 -04001778void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1779{
1780 if (typeQualifier.qualifier != EvqUniform)
1781 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301782 error(typeQualifier.line,
1783 "invalid qualifier:",
1784 getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
Jamie Madilla295edf2013-06-06 11:56:48 -04001785 recover();
1786 return;
1787 }
1788
1789 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1790 ASSERT(!layoutQualifier.isEmpty());
1791
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001792 if (mShaderVersion < 300)
Jamie Madilla295edf2013-06-06 11:56:48 -04001793 {
1794 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1795 recover();
1796 return;
1797 }
1798
1799 if (layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1800 {
1801 recover();
1802 return;
1803 }
1804
Jamie Madill099c0f32013-06-20 11:55:52 -04001805 if (layoutQualifier.matrixPacking != EmpUnspecified)
1806 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001807 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04001808 }
1809
Geoff Langc6856732014-02-11 09:38:55 -05001810 if (layoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04001811 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001812 mDefaultBlockStorage = layoutQualifier.blockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04001813 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001814}
1815
Jamie Madill06145232015-05-13 13:10:01 -04001816TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001817{
Jamie Madill06145232015-05-13 13:10:01 -04001818 TPublicType publicType = publicTypeIn;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001819 TOperator op = EOpNull;
1820 if (publicType.userDef)
1821 {
1822 op = EOpConstructStruct;
1823 }
1824 else
1825 {
1826 switch (publicType.type)
1827 {
1828 case EbtFloat:
1829 if (publicType.isMatrix())
1830 {
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001831 switch(publicType.getCols())
1832 {
Alexis Hetu07e57df2015-06-16 16:55:52 -04001833 case 2:
1834 switch(publicType.getRows())
1835 {
1836 case 2: op = EOpConstructMat2; break;
1837 case 3: op = EOpConstructMat2x3; break;
1838 case 4: op = EOpConstructMat2x4; break;
1839 }
1840 break;
1841 case 3:
1842 switch(publicType.getRows())
1843 {
1844 case 2: op = EOpConstructMat3x2; break;
1845 case 3: op = EOpConstructMat3; break;
1846 case 4: op = EOpConstructMat3x4; break;
1847 }
1848 break;
1849 case 4:
1850 switch(publicType.getRows())
1851 {
1852 case 2: op = EOpConstructMat4x2; break;
1853 case 3: op = EOpConstructMat4x3; break;
1854 case 4: op = EOpConstructMat4; break;
1855 }
1856 break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001857 }
1858 }
1859 else
1860 {
1861 switch(publicType.getNominalSize())
1862 {
Jamie Madill28b97422013-07-08 14:01:38 -04001863 case 1: op = EOpConstructFloat; break;
1864 case 2: op = EOpConstructVec2; break;
1865 case 3: op = EOpConstructVec3; break;
1866 case 4: op = EOpConstructVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001867 }
1868 }
1869 break;
1870
1871 case EbtInt:
1872 switch(publicType.getNominalSize())
1873 {
Jamie Madill28b97422013-07-08 14:01:38 -04001874 case 1: op = EOpConstructInt; break;
1875 case 2: op = EOpConstructIVec2; break;
1876 case 3: op = EOpConstructIVec3; break;
1877 case 4: op = EOpConstructIVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001878 }
1879 break;
1880
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001881 case EbtUInt:
1882 switch(publicType.getNominalSize())
1883 {
Jamie Madill28b97422013-07-08 14:01:38 -04001884 case 1: op = EOpConstructUInt; break;
1885 case 2: op = EOpConstructUVec2; break;
1886 case 3: op = EOpConstructUVec3; break;
1887 case 4: op = EOpConstructUVec4; break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001888 }
1889 break;
1890
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001891 case EbtBool:
1892 switch(publicType.getNominalSize())
1893 {
Jamie Madill28b97422013-07-08 14:01:38 -04001894 case 1: op = EOpConstructBool; break;
1895 case 2: op = EOpConstructBVec2; break;
1896 case 3: op = EOpConstructBVec3; break;
1897 case 4: op = EOpConstructBVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001898 }
1899 break;
1900
1901 default: break;
1902 }
1903
1904 if (op == EOpNull)
1905 {
1906 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
1907 recover();
1908 publicType.type = EbtFloat;
1909 op = EOpConstructFloat;
1910 }
1911 }
1912
1913 TString tempString;
Dmitry Skiba7f17a502015-06-22 15:08:39 -07001914 const TType *type = new TType(publicType);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001915 return new TFunction(&tempString, type, op);
1916}
1917
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001918// This function is used to test for the correctness of the parameters passed to various constructor functions
Arun Patole7e7e68d2015-05-22 12:02:25 +05301919// and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001920//
1921// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1922//
Arun Patole7e7e68d2015-05-22 12:02:25 +05301923TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments, TType *type, TOperator op, TFunction *fnCall,
1924 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001925{
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001926 TIntermAggregate *aggregateArguments = arguments->getAsAggregate();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001927
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001928 if (!aggregateArguments)
1929 {
1930 aggregateArguments = new TIntermAggregate;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001931 aggregateArguments->getSequence()->push_back(arguments);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001932 }
1933
Olli Etuahof40319e2015-03-10 14:33:00 +02001934 if (type->isArray())
1935 {
1936 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of the array.
1937 TIntermSequence *args = aggregateArguments->getSequence();
1938 for (size_t i = 0; i < args->size(); i++)
1939 {
1940 const TType &argType = (*args)[i]->getAsTyped()->getType();
1941 // It has already been checked that the argument is not an array.
1942 ASSERT(!argType.isArray());
1943 if (!argType.sameElementType(*type))
1944 {
1945 error(line, "Array constructor argument has an incorrect type", "Error");
1946 recover();
1947 return nullptr;
1948 }
1949 }
1950 }
1951 else if (op == EOpConstructStruct)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001952 {
1953 const TFieldList &fields = type->getStruct()->fields();
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001954 TIntermSequence *args = aggregateArguments->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001955
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001956 for (size_t i = 0; i < fields.size(); i++)
1957 {
Nicolas Capensffd73872014-08-21 13:49:16 -04001958 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001959 {
1960 error(line, "Structure constructor arguments do not match structure fields", "Error");
1961 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001962
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001963 return 0;
1964 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001965 }
1966 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001967
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001968 // Turn the argument list itself into a constructor
Olli Etuaho21203702014-11-13 16:16:21 +02001969 TIntermAggregate *constructor = intermediate.setAggregateOperator(aggregateArguments, op, line);
1970 TIntermTyped *constConstructor = foldConstConstructor(constructor, *type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001971 if (constConstructor)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001972 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001973 return constConstructor;
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001974 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001975
Olli Etuaho21203702014-11-13 16:16:21 +02001976 // Structs should not be precision qualified, the individual members may be.
1977 // Built-in types on the other hand should be precision qualified.
1978 if (op != EOpConstructStruct)
1979 {
1980 constructor->setPrecisionFromChildren();
1981 type->setPrecision(constructor->getPrecision());
1982 }
1983
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001984 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001985}
1986
Arun Patole7e7e68d2015-05-22 12:02:25 +05301987TIntermTyped *TParseContext::foldConstConstructor(TIntermAggregate *aggrNode, const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001988{
Olli Etuahof40319e2015-03-10 14:33:00 +02001989 // TODO: Add support for folding array constructors
1990 bool canBeFolded = areAllChildConst(aggrNode) && !type.isArray();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001991 aggrNode->setType(type);
Arun Patole7e7e68d2015-05-22 12:02:25 +05301992 if (canBeFolded)
1993 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001994 bool returnVal = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301995 TConstantUnion *unionArray = new TConstantUnion[type.getObjectSize()];
1996 if (aggrNode->getSequence()->size() == 1)
1997 {
1998 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type,
1999 true);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002000 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302001 else
2002 {
2003 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(),
2004 type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002005 }
2006 if (returnVal)
2007 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002008
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002009 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
2010 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002011
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002012 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002013}
2014
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002015//
2016// This function returns the tree representation for the vector field(s) being accessed from contant vector.
2017// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
2018// 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 +05302019// 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 +00002020// a constant matrix.
2021//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302022TIntermTyped *TParseContext::addConstVectorNode(TVectorFields &fields, TIntermTyped *node, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002023{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302024 TIntermTyped *typedNode;
2025 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002026
Jamie Madillb11e2482015-05-04 14:21:22 -04002027 const TConstantUnion *unionArray;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302028 if (tempConstantNode)
2029 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002030 unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002031
Arun Patole7e7e68d2015-05-22 12:02:25 +05302032 if (!unionArray)
2033 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002034 return node;
2035 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302036 }
2037 else
2038 { // 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 +00002039 error(line, "Cannot offset into the vector", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002040 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002041
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002042 return 0;
2043 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002044
Arun Patole7e7e68d2015-05-22 12:02:25 +05302045 TConstantUnion *constArray = new TConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002046
Arun Patole7e7e68d2015-05-22 12:02:25 +05302047 for (int i = 0; i < fields.num; i++)
2048 {
2049 if (fields.offsets[i] >= node->getType().getNominalSize())
2050 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002051 std::stringstream extraInfoStream;
2052 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
2053 std::string extraInfo = extraInfoStream.str();
2054 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002055 recover();
2056 fields.offsets[i] = 0;
2057 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302058
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002059 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002060
Arun Patole7e7e68d2015-05-22 12:02:25 +05302061 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002062 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
2063 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002064}
2065
2066//
2067// This function returns the column being accessed from a constant matrix. The values are retrieved from
Arun Patole7e7e68d2015-05-22 12:02:25 +05302068// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
2069// 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 +00002070// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
2071//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302072TIntermTyped *TParseContext::addConstMatrixNode(int index, TIntermTyped *node, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002073{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302074 TIntermTyped *typedNode;
2075 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002076
Arun Patole7e7e68d2015-05-22 12:02:25 +05302077 if (index >= node->getType().getCols())
2078 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002079 std::stringstream extraInfoStream;
2080 extraInfoStream << "matrix field selection out of range '" << index << "'";
2081 std::string extraInfo = extraInfoStream.str();
2082 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002083 recover();
2084 index = 0;
2085 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002086
Arun Patole7e7e68d2015-05-22 12:02:25 +05302087 if (tempConstantNode)
2088 {
Jamie Madillb11e2482015-05-04 14:21:22 -04002089 TConstantUnion *unionArray = tempConstantNode->getUnionArrayPointer();
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002090 int size = tempConstantNode->getType().getCols();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002091 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302092 }
2093 else
2094 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002095 error(line, "Cannot offset into the matrix", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002096 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002097
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002098 return 0;
2099 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002100
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002101 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002102}
2103
2104
2105//
2106// 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 +05302107// the symbol table and parse-tree is built for the type of the element. The input
2108// 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 +00002109// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
2110//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302111TIntermTyped *TParseContext::addConstArrayNode(int index, TIntermTyped *node, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002112{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302113 TIntermTyped *typedNode;
2114 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002115 TType arrayElementType = node->getType();
2116 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002117
Arun Patole7e7e68d2015-05-22 12:02:25 +05302118 if (index >= node->getType().getArraySize())
2119 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002120 std::stringstream extraInfoStream;
2121 extraInfoStream << "array field selection out of range '" << index << "'";
2122 std::string extraInfo = extraInfoStream.str();
2123 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002124 recover();
2125 index = 0;
2126 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002127
Arun Patole7e7e68d2015-05-22 12:02:25 +05302128 if (tempConstantNode)
2129 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002130 size_t arrayElementSize = arrayElementType.getObjectSize();
Arun Patole7e7e68d2015-05-22 12:02:25 +05302131 TConstantUnion *unionArray = tempConstantNode->getUnionArrayPointer();
2132 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(),
2133 line);
2134 }
2135 else
2136 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002137 error(line, "Cannot offset into the array", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002138 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002139
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002140 return 0;
2141 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002142
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002143 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002144}
2145
2146
2147//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302148// 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 +00002149// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
2150// function and returns the parse-tree with the values of the embedded/nested struct.
2151//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302152TIntermTyped *TParseContext::addConstStruct(const TString &identifier, TIntermTyped *node, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002153{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302154 const TFieldList &fields = node->getType().getStruct()->fields();
Jamie Madill94bf7f22013-07-08 13:31:15 -04002155 size_t instanceSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002156
Arun Patole7e7e68d2015-05-22 12:02:25 +05302157 for (size_t index = 0; index < fields.size(); ++index)
2158 {
2159 if (fields[index]->name() == identifier)
2160 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002161 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302162 }
2163 else
2164 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002165 instanceSize += fields[index]->type()->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002166 }
2167 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002168
Jamie Madill94bf7f22013-07-08 13:31:15 -04002169 TIntermTyped *typedNode;
2170 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
Arun Patole7e7e68d2015-05-22 12:02:25 +05302171 if (tempConstantNode)
2172 {
2173 TConstantUnion *constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002174
Arun Patole7e7e68d2015-05-22 12:02:25 +05302175 // type will be changed in the calling function
2176 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line);
2177 }
2178 else
2179 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002180 error(line, "Cannot offset into the structure", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002181 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002182
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002183 return 0;
2184 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002185
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002186 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002187}
2188
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002189//
2190// Interface/uniform blocks
2191//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302192TIntermAggregate *TParseContext::addInterfaceBlock(const TPublicType &typeQualifier, const TSourceLoc &nameLine,
2193 const TString &blockName, TFieldList *fieldList,
2194 const TString *instanceName, const TSourceLoc &instanceLine,
2195 TIntermTyped *arrayIndex, const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002196{
2197 if (reservedErrorCheck(nameLine, blockName))
2198 recover();
2199
2200 if (typeQualifier.qualifier != EvqUniform)
2201 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302202 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
2203 "interface blocks must be uniform");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002204 recover();
2205 }
2206
Jamie Madill099c0f32013-06-20 11:55:52 -04002207 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
2208 if (layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002209 {
2210 recover();
2211 }
2212
Jamie Madill099c0f32013-06-20 11:55:52 -04002213 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2214 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002215 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002216 }
2217
Jamie Madill1566ef72013-06-20 11:55:54 -04002218 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2219 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002220 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002221 }
2222
Arun Patole7e7e68d2015-05-22 12:02:25 +05302223 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2224 if (!symbolTable.declare(blockNameSymbol))
2225 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002226 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
2227 recover();
2228 }
2229
Jamie Madill98493dd2013-07-08 14:39:03 -04002230 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302231 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2232 {
2233 TField *field = (*fieldList)[memberIndex];
2234 TType *fieldType = field->type();
2235 if (IsSampler(fieldType->getBasicType()))
2236 {
2237 error(field->line(), "unsupported type", fieldType->getBasicString(),
2238 "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002239 recover();
2240 }
2241
Jamie Madill98493dd2013-07-08 14:39:03 -04002242 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002243 switch (qualifier)
2244 {
2245 case EvqGlobal:
2246 case EvqUniform:
2247 break;
2248 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04002249 error(field->line(), "invalid qualifier on interface block member", getQualifierString(qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002250 recover();
2251 break;
2252 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002253
2254 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002255 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
2256 if (layoutLocationErrorCheck(field->line(), fieldLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002257 {
2258 recover();
2259 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002260
Jamie Madill98493dd2013-07-08 14:39:03 -04002261 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002262 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302263 error(field->line(), "invalid layout qualifier:", getBlockStorageString(fieldLayoutQualifier.blockStorage),
2264 "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04002265 recover();
2266 }
2267
Jamie Madill98493dd2013-07-08 14:39:03 -04002268 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002269 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002270 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002271 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002272 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002273 {
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002274 warning(field->line(), "extraneous layout qualifier:",
2275 getMatrixPackingString(fieldLayoutQualifier.matrixPacking), "only has an effect on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04002276 }
2277
Jamie Madill98493dd2013-07-08 14:39:03 -04002278 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002279 }
2280
Jamie Madill98493dd2013-07-08 14:39:03 -04002281 // add array index
2282 int arraySize = 0;
2283 if (arrayIndex != NULL)
2284 {
2285 if (arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize))
2286 recover();
2287 }
2288
Arun Patole7e7e68d2015-05-22 12:02:25 +05302289 TInterfaceBlock *interfaceBlock = new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize,
2290 blockLayoutQualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002291 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier, arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002292
2293 TString symbolName = "";
2294 int symbolId = 0;
2295
Jamie Madill98493dd2013-07-08 14:39:03 -04002296 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002297 {
2298 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002299 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2300 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302301 TField *field = (*fieldList)[memberIndex];
2302 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002303
2304 // set parent pointer of the field variable
2305 fieldType->setInterfaceBlock(interfaceBlock);
2306
Arun Patole7e7e68d2015-05-22 12:02:25 +05302307 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002308 fieldVariable->setQualifier(typeQualifier.qualifier);
2309
Arun Patole7e7e68d2015-05-22 12:02:25 +05302310 if (!symbolTable.declare(fieldVariable))
2311 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002312 error(field->line(), "redefinition", field->name().c_str(), "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002313 recover();
2314 }
2315 }
2316 }
2317 else
2318 {
Olli Etuahoe0f623a2015-07-10 11:58:30 +03002319 if (reservedErrorCheck(instanceLine, *instanceName))
2320 recover();
2321
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002322 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302323 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002324 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002325
Arun Patole7e7e68d2015-05-22 12:02:25 +05302326 if (!symbolTable.declare(instanceTypeDef))
2327 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002328 error(instanceLine, "redefinition", instanceName->c_str(), "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002329 recover();
2330 }
2331
2332 symbolId = instanceTypeDef->getUniqueId();
2333 symbolName = instanceTypeDef->getName();
2334 }
2335
Arun Patole7e7e68d2015-05-22 12:02:25 +05302336 TIntermAggregate *aggregate = intermediate.makeAggregate(intermediate.addSymbol(symbolId, symbolName,
2337 interfaceBlockType,
2338 typeQualifier.line),
2339 nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002340 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04002341
2342 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002343 return aggregate;
2344}
2345
Arun Patole7e7e68d2015-05-22 12:02:25 +05302346bool TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002347{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002348 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002349
2350 // Embedded structure definitions are not supported per GLSL ES spec.
2351 // They aren't allowed in GLSL either, but we need to detect this here
2352 // so we don't rely on the GLSL compiler to catch it.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302353 if (mStructNestingLevel > 1)
2354 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002355 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002356 return true;
2357 }
2358
2359 return false;
2360}
2361
2362void TParseContext::exitStructDeclaration()
2363{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002364 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002365}
2366
2367namespace {
2368
2369const int kWebGLMaxStructNesting = 4;
2370
2371} // namespace
2372
Arun Patole7e7e68d2015-05-22 12:02:25 +05302373bool TParseContext::structNestingErrorCheck(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002374{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302375 if (!IsWebGLBasedSpec(mShaderSpec))
2376 {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002377 return false;
2378 }
2379
Arun Patole7e7e68d2015-05-22 12:02:25 +05302380 if (field.type()->getBasicType() != EbtStruct)
2381 {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002382 return false;
2383 }
2384
2385 // We're already inside a structure definition at this point, so add
2386 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302387 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2388 {
Jamie Madill41a49272014-03-18 16:10:13 -04002389 std::stringstream reasonStream;
2390 reasonStream << "Reference of struct type "
2391 << field.type()->getStruct()->name().c_str()
2392 << " exceeds maximum allowed nesting level of "
2393 << kWebGLMaxStructNesting;
2394 std::string reason = reasonStream.str();
2395 error(line, reason.c_str(), field.name().c_str(), "");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002396 return true;
2397 }
2398
2399 return false;
2400}
2401
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002402//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002403// Parse an array index expression
2404//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302405TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression, const TSourceLoc &location,
2406 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002407{
2408 TIntermTyped *indexedExpression = NULL;
2409
2410 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2411 {
2412 if (baseExpression->getAsSymbolNode())
2413 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302414 error(location, " left of '[' is not of type array, matrix, or vector ",
2415 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002416 }
2417 else
2418 {
2419 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2420 }
2421 recover();
2422 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002423
Jamie Madill21c1e452014-12-29 11:33:41 -05002424 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2425
2426 if (indexExpression->getQualifier() == EvqConst && indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002427 {
Jamie Madill21c1e452014-12-29 11:33:41 -05002428 int index = indexConstantUnion->getIConst(0);
Jamie Madill7164cf42013-07-08 13:30:59 -04002429 if (index < 0)
2430 {
2431 std::stringstream infoStream;
2432 infoStream << index;
2433 std::string info = infoStream.str();
2434 error(location, "negative index", info.c_str());
2435 recover();
2436 index = 0;
2437 }
2438 if (baseExpression->getType().getQualifier() == EvqConst)
2439 {
2440 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002441 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002442 // constant folding for arrays
2443 indexedExpression = addConstArrayNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002444 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002445 else if (baseExpression->isVector())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002446 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002447 // constant folding for vectors
2448 TVectorFields fields;
2449 fields.num = 1;
2450 fields.offsets[0] = index; // need to do it this way because v.xy sends fields integer array
2451 indexedExpression = addConstVectorNode(fields, baseExpression, location);
2452 }
2453 else if (baseExpression->isMatrix())
2454 {
2455 // constant folding for matrices
2456 indexedExpression = addConstMatrixNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002457 }
2458 }
2459 else
2460 {
Jamie Madillb11e2482015-05-04 14:21:22 -04002461 int safeIndex = -1;
2462
Jamie Madill7164cf42013-07-08 13:30:59 -04002463 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002464 {
Jamie Madill18464b52013-07-08 14:01:55 -04002465 if (index >= baseExpression->getType().getArraySize())
Jamie Madill7164cf42013-07-08 13:30:59 -04002466 {
2467 std::stringstream extraInfoStream;
2468 extraInfoStream << "array index out of range '" << index << "'";
2469 std::string extraInfo = extraInfoStream.str();
2470 error(location, "", "[", extraInfo.c_str());
2471 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002472 safeIndex = baseExpression->getType().getArraySize() - 1;
Jamie Madill7164cf42013-07-08 13:30:59 -04002473 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302474 else if (baseExpression->getQualifier() == EvqFragData && index > 0 &&
2475 !isExtensionEnabled("GL_EXT_draw_buffers"))
Jamie Madill5d287f52013-07-12 15:38:19 -04002476 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302477 error(location, "",
2478 "[", "array indexes for gl_FragData must be zero when GL_EXT_draw_buffers is disabled");
Jamie Madill5d287f52013-07-12 15:38:19 -04002479 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002480 safeIndex = 0;
Jamie Madill5d287f52013-07-12 15:38:19 -04002481 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002482 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302483 else if ((baseExpression->isVector() || baseExpression->isMatrix()) &&
2484 baseExpression->getType().getNominalSize() <= index)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002485 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002486 std::stringstream extraInfoStream;
2487 extraInfoStream << "field selection out of range '" << index << "'";
2488 std::string extraInfo = extraInfoStream.str();
2489 error(location, "", "[", extraInfo.c_str());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002490 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002491 safeIndex = baseExpression->getType().getNominalSize() - 1;
Jamie Madill46131a32013-06-20 11:55:50 -04002492 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002493
Jamie Madillb11e2482015-05-04 14:21:22 -04002494 // Don't modify the data of the previous constant union, because it can point
2495 // to builtins, like gl_MaxDrawBuffers. Instead use a new sanitized object.
2496 if (safeIndex != -1)
2497 {
2498 TConstantUnion *safeConstantUnion = new TConstantUnion();
2499 safeConstantUnion->setIConst(safeIndex);
2500 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
2501 }
2502
Jamie Madill7164cf42013-07-08 13:30:59 -04002503 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002504 }
2505 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002506 else
2507 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002508 if (baseExpression->isInterfaceBlock())
Jamie Madill7164cf42013-07-08 13:30:59 -04002509 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302510 error(location, "",
2511 "[", "array indexes for interface blocks arrays must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002512 recover();
2513 }
Jamie Madill19571812013-08-12 15:26:34 -07002514 else if (baseExpression->getQualifier() == EvqFragmentOut)
Jamie Madill7164cf42013-07-08 13:30:59 -04002515 {
Jamie Madill19571812013-08-12 15:26:34 -07002516 error(location, "", "[", "array indexes for fragment outputs must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002517 recover();
2518 }
2519
2520 indexedExpression = intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
2521 }
2522
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002523 if (indexedExpression == 0)
2524 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002525 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002526 unionArray->setFConst(0.0f);
2527 indexedExpression = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
2528 }
2529 else if (baseExpression->isArray())
2530 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002531 const TType &baseType = baseExpression->getType();
2532 if (baseType.getStruct())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002533 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002534 TType copyOfType(baseType.getStruct());
2535 indexedExpression->setType(copyOfType);
2536 }
2537 else if (baseType.isInterfaceBlock())
2538 {
2539 TType copyOfType(baseType.getInterfaceBlock(), baseType.getQualifier(), baseType.getLayoutQualifier(), 0);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002540 indexedExpression->setType(copyOfType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002541 }
2542 else
2543 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302544 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2545 EvqTemporary, static_cast<unsigned char>(baseExpression->getNominalSize()),
2546 static_cast<unsigned char>(baseExpression->getSecondarySize())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002547 }
2548
2549 if (baseExpression->getType().getQualifier() == EvqConst)
2550 {
2551 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2552 }
2553 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002554 else if (baseExpression->isMatrix())
2555 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002556 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302557 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2558 qualifier, static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002559 }
2560 else if (baseExpression->isVector())
2561 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002562 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2563 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002564 }
2565 else
2566 {
2567 indexedExpression->setType(baseExpression->getType());
2568 }
2569
2570 return indexedExpression;
2571}
2572
Arun Patole7e7e68d2015-05-22 12:02:25 +05302573TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression, const TSourceLoc &dotLocation,
2574 const TString &fieldString, const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002575{
2576 TIntermTyped *indexedExpression = NULL;
2577
2578 if (baseExpression->isArray())
2579 {
2580 error(fieldLocation, "cannot apply dot operator to an array", ".");
2581 recover();
2582 }
2583
2584 if (baseExpression->isVector())
2585 {
2586 TVectorFields fields;
2587 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields, fieldLocation))
2588 {
2589 fields.num = 1;
2590 fields.offsets[0] = 0;
2591 recover();
2592 }
2593
2594 if (baseExpression->getType().getQualifier() == EvqConst)
2595 {
2596 // constant folding for vector fields
2597 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
2598 if (indexedExpression == 0)
2599 {
2600 recover();
2601 indexedExpression = baseExpression;
2602 }
2603 else
2604 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302605 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2606 EvqConst, (unsigned char) (fieldString).size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002607 }
2608 }
2609 else
2610 {
2611 TString vectorString = fieldString;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302612 TIntermTyped *index = intermediate.addSwizzle(fields, fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002613 indexedExpression = intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302614 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2615 EvqTemporary, (unsigned char) vectorString.size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002616 }
2617 }
2618 else if (baseExpression->isMatrix())
2619 {
2620 TMatrixFields fields;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302621 if (!parseMatrixFields(fieldString, baseExpression->getCols(), baseExpression->getRows(), fields,
2622 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002623 {
2624 fields.wholeRow = false;
2625 fields.wholeCol = false;
2626 fields.row = 0;
2627 fields.col = 0;
2628 recover();
2629 }
2630
2631 if (fields.wholeRow || fields.wholeCol)
2632 {
2633 error(dotLocation, " non-scalar fields not implemented yet", ".");
2634 recover();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002635 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002636 unionArray->setIConst(0);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302637 TIntermTyped *index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst),
2638 fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002639 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302640 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2641 EvqTemporary, static_cast<unsigned char>(baseExpression->getCols()),
2642 static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002643 }
2644 else
2645 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002646 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002647 unionArray->setIConst(fields.col * baseExpression->getRows() + fields.row);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302648 TIntermTyped *index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst),
2649 fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002650 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
2651 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision()));
2652 }
2653 }
2654 else if (baseExpression->getBasicType() == EbtStruct)
2655 {
2656 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302657 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04002658 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002659 {
2660 error(dotLocation, "structure has no fields", "Internal Error");
2661 recover();
2662 indexedExpression = baseExpression;
2663 }
2664 else
2665 {
2666 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002667 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002668 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002669 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002670 {
2671 fieldFound = true;
2672 break;
2673 }
2674 }
2675 if (fieldFound)
2676 {
2677 if (baseExpression->getType().getQualifier() == EvqConst)
2678 {
2679 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2680 if (indexedExpression == 0)
2681 {
2682 recover();
2683 indexedExpression = baseExpression;
2684 }
2685 else
2686 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002687 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002688 // change the qualifier of the return type, not of the structure field
2689 // as the structure definition is shared between various structures.
2690 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2691 }
2692 }
2693 else
2694 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002695 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002696 unionArray->setIConst(i);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302697 TIntermTyped *index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002698 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002699 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002700 }
2701 }
2702 else
2703 {
2704 error(dotLocation, " no such field in structure", fieldString.c_str());
2705 recover();
2706 indexedExpression = baseExpression;
2707 }
2708 }
2709 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002710 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002711 {
2712 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302713 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04002714 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002715 {
2716 error(dotLocation, "interface block has no fields", "Internal Error");
2717 recover();
2718 indexedExpression = baseExpression;
2719 }
2720 else
2721 {
2722 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002723 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002724 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002725 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002726 {
2727 fieldFound = true;
2728 break;
2729 }
2730 }
2731 if (fieldFound)
2732 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002733 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002734 unionArray->setIConst(i);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302735 TIntermTyped *index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
2736 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index,
2737 dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002738 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002739 }
2740 else
2741 {
2742 error(dotLocation, " no such field in interface block", fieldString.c_str());
2743 recover();
2744 indexedExpression = baseExpression;
2745 }
2746 }
2747 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002748 else
2749 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002750 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002751 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302752 error(dotLocation, " field selection requires structure, vector, or matrix on left hand side",
2753 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002754 }
2755 else
2756 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302757 error(dotLocation,
2758 " field selection requires structure, vector, matrix, or interface block on left hand side",
2759 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002760 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002761 recover();
2762 indexedExpression = baseExpression;
2763 }
2764
2765 return indexedExpression;
2766}
2767
Arun Patole7e7e68d2015-05-22 12:02:25 +05302768TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002769{
Jamie Madilla5efff92013-06-06 11:56:47 -04002770 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002771
Jamie Madilla5efff92013-06-06 11:56:47 -04002772 qualifier.location = -1;
2773 qualifier.matrixPacking = EmpUnspecified;
2774 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002775
2776 if (qualifierType == "shared")
2777 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002778 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002779 }
2780 else if (qualifierType == "packed")
2781 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002782 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002783 }
2784 else if (qualifierType == "std140")
2785 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002786 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002787 }
2788 else if (qualifierType == "row_major")
2789 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002790 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002791 }
2792 else if (qualifierType == "column_major")
2793 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002794 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002795 }
2796 else if (qualifierType == "location")
2797 {
2798 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "location requires an argument");
2799 recover();
2800 }
2801 else
2802 {
2803 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
2804 recover();
2805 }
2806
Jamie Madilla5efff92013-06-06 11:56:47 -04002807 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002808}
2809
Arun Patole7e7e68d2015-05-22 12:02:25 +05302810TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc &qualifierTypeLine,
2811 const TString &intValueString, int intValue,
2812 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002813{
Jamie Madilla5efff92013-06-06 11:56:47 -04002814 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002815
Jamie Madilla5efff92013-06-06 11:56:47 -04002816 qualifier.location = -1;
2817 qualifier.matrixPacking = EmpUnspecified;
2818 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002819
2820 if (qualifierType != "location")
2821 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302822 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
2823 "only location may have arguments");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002824 recover();
2825 }
2826 else
2827 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002828 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002829 if (intValue < 0)
2830 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002831 error(intValueLine, "out of range:", intValueString.c_str(), "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002832 recover();
2833 }
2834 else
2835 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002836 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002837 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002838 }
2839
Jamie Madilla5efff92013-06-06 11:56:47 -04002840 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002841}
2842
Jamie Madilla5efff92013-06-06 11:56:47 -04002843TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002844{
Jamie Madilla5efff92013-06-06 11:56:47 -04002845 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002846
Jamie Madilla5efff92013-06-06 11:56:47 -04002847 if (rightQualifier.location != -1)
2848 {
2849 joinedQualifier.location = rightQualifier.location;
2850 }
2851 if (rightQualifier.matrixPacking != EmpUnspecified)
2852 {
2853 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
2854 }
2855 if (rightQualifier.blockStorage != EbsUnspecified)
2856 {
2857 joinedQualifier.blockStorage = rightQualifier.blockStorage;
2858 }
2859
2860 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002861}
2862
Arun Patole7e7e68d2015-05-22 12:02:25 +05302863TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc,
2864 TQualifier interpolationQualifier,
2865 const TSourceLoc &storageLoc,
2866 TQualifier storageQualifier)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002867{
2868 TQualifier mergedQualifier = EvqSmoothIn;
2869
Arun Patole7e7e68d2015-05-22 12:02:25 +05302870 if (storageQualifier == EvqFragmentIn)
2871 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002872 if (interpolationQualifier == EvqSmooth)
2873 mergedQualifier = EvqSmoothIn;
2874 else if (interpolationQualifier == EvqFlat)
2875 mergedQualifier = EvqFlatIn;
2876 else UNREACHABLE();
2877 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302878 else if (storageQualifier == EvqCentroidIn)
2879 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002880 if (interpolationQualifier == EvqSmooth)
2881 mergedQualifier = EvqCentroidIn;
2882 else if (interpolationQualifier == EvqFlat)
2883 mergedQualifier = EvqFlatIn;
2884 else UNREACHABLE();
2885 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302886 else if (storageQualifier == EvqVertexOut)
2887 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002888 if (interpolationQualifier == EvqSmooth)
2889 mergedQualifier = EvqSmoothOut;
2890 else if (interpolationQualifier == EvqFlat)
2891 mergedQualifier = EvqFlatOut;
2892 else UNREACHABLE();
2893 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302894 else if (storageQualifier == EvqCentroidOut)
2895 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002896 if (interpolationQualifier == EvqSmooth)
2897 mergedQualifier = EvqCentroidOut;
2898 else if (interpolationQualifier == EvqFlat)
2899 mergedQualifier = EvqFlatOut;
2900 else UNREACHABLE();
2901 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302902 else
2903 {
2904 error(interpolationLoc, "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier",
2905 getInterpolationString(interpolationQualifier));
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002906 recover();
2907
2908 mergedQualifier = storageQualifier;
2909 }
2910
2911 TPublicType type;
2912 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
2913 return type;
2914}
2915
Arun Patole7e7e68d2015-05-22 12:02:25 +05302916TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier, TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002917{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002918 if (voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier.type))
2919 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002920 recover();
2921 }
2922
Arun Patole7e7e68d2015-05-22 12:02:25 +05302923 for (unsigned int i = 0; i < fieldList->size(); ++i)
2924 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002925 //
2926 // Careful not to replace already known aspects of type, like array-ness
2927 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05302928 TType *type = (*fieldList)[i]->type();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002929 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002930 type->setPrimarySize(typeSpecifier.primarySize);
2931 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002932 type->setPrecision(typeSpecifier.precision);
2933 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002934 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002935
2936 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05302937 if (type->isArray())
2938 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002939 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
2940 recover();
2941 }
2942 if (typeSpecifier.array)
2943 type->setArraySize(typeSpecifier.arraySize);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302944 if (typeSpecifier.userDef)
2945 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002946 type->setStruct(typeSpecifier.userDef->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002947 }
2948
Arun Patole7e7e68d2015-05-22 12:02:25 +05302949 if (structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i]))
2950 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002951 recover();
2952 }
2953 }
2954
Jamie Madill98493dd2013-07-08 14:39:03 -04002955 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002956}
2957
Arun Patole7e7e68d2015-05-22 12:02:25 +05302958TPublicType TParseContext::addStructure(const TSourceLoc &structLine, const TSourceLoc &nameLine,
2959 const TString *structName, TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002960{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302961 TStructure *structure = new TStructure(structName, fieldList);
2962 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002963
Jamie Madill9b820842015-02-12 10:40:10 -05002964 // Store a bool in the struct if we're at global scope, to allow us to
2965 // skip the local struct scoping workaround in HLSL.
Jamie Madillb960cc42015-02-12 15:33:20 +00002966 structure->setUniqueId(TSymbolTable::nextUniqueId());
Jamie Madill9b820842015-02-12 10:40:10 -05002967 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04002968
Jamie Madill98493dd2013-07-08 14:39:03 -04002969 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002970 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002971 if (reservedErrorCheck(nameLine, *structName))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002972 {
2973 recover();
2974 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302975 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
2976 if (!symbolTable.declare(userTypeDef))
2977 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002978 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002979 recover();
2980 }
2981 }
2982
2983 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04002984 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002985 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002986 const TField &field = *(*fieldList)[typeListIndex];
2987 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002988 switch (qualifier)
2989 {
2990 case EvqGlobal:
2991 case EvqTemporary:
2992 break;
2993 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04002994 error(field.line(), "invalid qualifier on struct member", getQualifierString(qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002995 recover();
2996 break;
2997 }
2998 }
2999
3000 TPublicType publicType;
3001 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003002 publicType.userDef = structureType;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003003 exitStructDeclaration();
3004
3005 return publicType;
3006}
3007
Olli Etuahoa3a36662015-02-17 13:46:51 +02003008TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init, TIntermAggregate *statementList, const TSourceLoc &loc)
3009{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003010 TBasicType switchType = init->getBasicType();
3011 if ((switchType != EbtInt && switchType != EbtUInt) ||
3012 init->isMatrix() ||
3013 init->isArray() ||
3014 init->isVector())
3015 {
3016 error(init->getLine(), "init-expression in a switch statement must be a scalar integer", "switch");
3017 recover();
3018 return nullptr;
3019 }
3020
Olli Etuahoac5274d2015-02-20 10:19:08 +02003021 if (statementList)
3022 {
3023 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
3024 {
3025 recover();
3026 return nullptr;
3027 }
3028 }
3029
Olli Etuahoa3a36662015-02-17 13:46:51 +02003030 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3031 if (node == nullptr)
3032 {
3033 error(loc, "erroneous switch statement", "switch");
3034 recover();
3035 return nullptr;
3036 }
3037 return node;
3038}
3039
3040TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3041{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003042 if (mSwitchNestingLevel == 0)
3043 {
3044 error(loc, "case labels need to be inside switch statements", "case");
3045 recover();
3046 return nullptr;
3047 }
3048 if (condition == nullptr)
3049 {
3050 error(loc, "case label must have a condition", "case");
3051 recover();
3052 return nullptr;
3053 }
3054 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
3055 condition->isMatrix() ||
3056 condition->isArray() ||
3057 condition->isVector())
3058 {
3059 error(condition->getLine(), "case label must be a scalar integer", "case");
3060 recover();
3061 }
3062 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
3063 if (conditionConst == nullptr)
3064 {
3065 error(condition->getLine(), "case label must be constant", "case");
3066 recover();
3067 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003068 TIntermCase *node = intermediate.addCase(condition, loc);
3069 if (node == nullptr)
3070 {
3071 error(loc, "erroneous case statement", "case");
3072 recover();
3073 return nullptr;
3074 }
3075 return node;
3076}
3077
3078TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3079{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003080 if (mSwitchNestingLevel == 0)
3081 {
3082 error(loc, "default labels need to be inside switch statements", "default");
3083 recover();
3084 return nullptr;
3085 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003086 TIntermCase *node = intermediate.addCase(nullptr, loc);
3087 if (node == nullptr)
3088 {
3089 error(loc, "erroneous default statement", "default");
3090 recover();
3091 return nullptr;
3092 }
3093 return node;
3094}
3095
Olli Etuahof6c694b2015-03-26 14:50:53 +02003096TIntermTyped *TParseContext::createUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc,
3097 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003098{
3099 if (child == nullptr)
3100 {
3101 return nullptr;
3102 }
3103
3104 switch (op)
3105 {
3106 case EOpLogicalNot:
3107 if (child->getBasicType() != EbtBool ||
3108 child->isMatrix() ||
3109 child->isArray() ||
3110 child->isVector())
3111 {
3112 return nullptr;
3113 }
3114 break;
3115 case EOpBitwiseNot:
3116 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3117 child->isMatrix() ||
3118 child->isArray())
3119 {
3120 return nullptr;
3121 }
3122 break;
3123 case EOpPostIncrement:
3124 case EOpPreIncrement:
3125 case EOpPostDecrement:
3126 case EOpPreDecrement:
3127 case EOpNegative:
3128 case EOpPositive:
3129 if (child->getBasicType() == EbtStruct ||
Olli Etuahodca3e792015-03-26 13:24:04 +02003130 child->getBasicType() == EbtBool ||
Olli Etuaho69c11b52015-03-26 12:59:00 +02003131 child->isArray())
3132 {
3133 return nullptr;
3134 }
Olli Etuahodca3e792015-03-26 13:24:04 +02003135 // Operators for built-ins are already type checked against their prototype.
Olli Etuaho69c11b52015-03-26 12:59:00 +02003136 default:
3137 break;
3138 }
3139
Olli Etuahof6c694b2015-03-26 14:50:53 +02003140 return intermediate.addUnaryMath(op, child, loc, funcReturnType);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003141}
3142
Olli Etuaho09b22472015-02-11 11:47:26 +02003143TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3144{
Olli Etuahof6c694b2015-03-26 14:50:53 +02003145 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003146 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003147 {
3148 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
3149 recover();
3150 return child;
3151 }
3152 return node;
3153}
3154
3155TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3156{
3157 if (lValueErrorCheck(loc, GetOperatorString(op), child))
3158 recover();
3159 return addUnaryMath(op, child, loc);
3160}
3161
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003162bool TParseContext::binaryOpCommonCheck(TOperator op, TIntermTyped *left, TIntermTyped *right,
Olli Etuahod6b14282015-03-17 14:31:35 +02003163 const TSourceLoc &loc)
3164{
3165 if (left->isArray() || right->isArray())
3166 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003167 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003168 {
3169 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3170 return false;
3171 }
3172
3173 if (left->isArray() != right->isArray())
3174 {
3175 error(loc, "array / non-array mismatch", GetOperatorString(op));
3176 return false;
3177 }
3178
3179 switch (op)
3180 {
3181 case EOpEqual:
3182 case EOpNotEqual:
3183 case EOpAssign:
3184 case EOpInitialize:
3185 break;
3186 default:
3187 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3188 return false;
3189 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003190 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003191 if (left->getArraySize() != right->getArraySize())
3192 {
3193 error(loc, "array size mismatch", GetOperatorString(op));
3194 return false;
3195 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003196 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003197
3198 // Check ops which require integer / ivec parameters
3199 bool isBitShift = false;
3200 switch (op)
3201 {
3202 case EOpBitShiftLeft:
3203 case EOpBitShiftRight:
3204 case EOpBitShiftLeftAssign:
3205 case EOpBitShiftRightAssign:
3206 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3207 // check that the basic type is an integer type.
3208 isBitShift = true;
3209 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3210 {
3211 return false;
3212 }
3213 break;
3214 case EOpBitwiseAnd:
3215 case EOpBitwiseXor:
3216 case EOpBitwiseOr:
3217 case EOpBitwiseAndAssign:
3218 case EOpBitwiseXorAssign:
3219 case EOpBitwiseOrAssign:
3220 // It is enough to check the type of only one operand, since later it
3221 // is checked that the operand types match.
3222 if (!IsInteger(left->getBasicType()))
3223 {
3224 return false;
3225 }
3226 break;
3227 default:
3228 break;
3229 }
3230
3231 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3232 // So the basic type should usually match.
3233 if (!isBitShift && left->getBasicType() != right->getBasicType())
3234 {
3235 return false;
3236 }
3237
Olli Etuaho9dd217b2015-03-20 14:24:31 +02003238 // Check that type sizes match exactly on ops that require that.
Olli Etuahoff699002015-03-23 14:38:42 +02003239 // Also check restrictions for structs that contain arrays or samplers.
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003240 switch(op)
3241 {
3242 case EOpAssign:
3243 case EOpInitialize:
3244 case EOpEqual:
3245 case EOpNotEqual:
Olli Etuaho9dd217b2015-03-20 14:24:31 +02003246 // ESSL 1.00 sections 5.7, 5.8, 5.9
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003247 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
Olli Etuaho9dd217b2015-03-20 14:24:31 +02003248 {
3249 error(loc, "undefined operation for structs containing arrays", GetOperatorString(op));
3250 return false;
3251 }
Olli Etuahoff699002015-03-23 14:38:42 +02003252 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3253 // we interpret the spec so that this extends to structs containing samplers,
3254 // similarly to ESSL 1.00 spec.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003255 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
Olli Etuahoff699002015-03-23 14:38:42 +02003256 left->getType().isStructureContainingSamplers())
3257 {
3258 error(loc, "undefined operation for structs containing samplers", GetOperatorString(op));
3259 return false;
3260 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003261 case EOpLessThan:
3262 case EOpGreaterThan:
3263 case EOpLessThanEqual:
3264 case EOpGreaterThanEqual:
3265 if ((left->getNominalSize() != right->getNominalSize()) ||
3266 (left->getSecondarySize() != right->getSecondarySize()))
3267 {
3268 return false;
3269 }
3270 default:
3271 break;
3272 }
3273
Olli Etuahod6b14282015-03-17 14:31:35 +02003274 return true;
3275}
3276
Olli Etuahofc1806e2015-03-17 13:03:11 +02003277TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op, TIntermTyped *left, TIntermTyped *right,
3278 const TSourceLoc &loc)
3279{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003280 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003281 return nullptr;
3282
Olli Etuahofc1806e2015-03-17 13:03:11 +02003283 switch (op)
3284 {
3285 case EOpEqual:
3286 case EOpNotEqual:
Olli Etuahofc1806e2015-03-17 13:03:11 +02003287 break;
3288 case EOpLessThan:
3289 case EOpGreaterThan:
3290 case EOpLessThanEqual:
3291 case EOpGreaterThanEqual:
Olli Etuahod6b14282015-03-17 14:31:35 +02003292 ASSERT(!left->isArray() && !right->isArray());
3293 if (left->isMatrix() || left->isVector() ||
Olli Etuahofc1806e2015-03-17 13:03:11 +02003294 left->getBasicType() == EbtStruct)
3295 {
3296 return nullptr;
3297 }
3298 break;
3299 case EOpLogicalOr:
3300 case EOpLogicalXor:
3301 case EOpLogicalAnd:
Olli Etuahod6b14282015-03-17 14:31:35 +02003302 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003303 if (left->getBasicType() != EbtBool ||
Olli Etuahod6b14282015-03-17 14:31:35 +02003304 left->isMatrix() || left->isVector())
Olli Etuahofc1806e2015-03-17 13:03:11 +02003305 {
3306 return nullptr;
3307 }
3308 break;
3309 case EOpAdd:
3310 case EOpSub:
3311 case EOpDiv:
3312 case EOpMul:
Olli Etuahod6b14282015-03-17 14:31:35 +02003313 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003314 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool)
3315 {
3316 return nullptr;
3317 }
3318 break;
3319 case EOpIMod:
Olli Etuahod6b14282015-03-17 14:31:35 +02003320 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003321 // Note that this is only for the % operator, not for mod()
3322 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
3323 {
3324 return nullptr;
3325 }
3326 break;
3327 // Note that for bitwise ops, type checking is done in promote() to
3328 // share code between ops and compound assignment
3329 default:
3330 break;
3331 }
3332
Olli Etuahofc1806e2015-03-17 13:03:11 +02003333 return intermediate.addBinaryMath(op, left, right, loc);
3334}
3335
Olli Etuaho09b22472015-02-11 11:47:26 +02003336TIntermTyped *TParseContext::addBinaryMath(TOperator op, TIntermTyped *left, TIntermTyped *right,
3337 const TSourceLoc &loc)
3338{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003339 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003340 if (node == 0)
3341 {
3342 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(), right->getCompleteString());
3343 recover();
3344 return left;
3345 }
3346 return node;
3347}
3348
3349TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op, TIntermTyped *left, TIntermTyped *right,
3350 const TSourceLoc &loc)
3351{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003352 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003353 if (node == 0)
3354 {
3355 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(), right->getCompleteString());
3356 recover();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003357 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02003358 unionArray->setBConst(false);
3359 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), loc);
3360 }
3361 return node;
3362}
3363
Olli Etuahod6b14282015-03-17 14:31:35 +02003364TIntermTyped *TParseContext::createAssign(TOperator op, TIntermTyped *left, TIntermTyped *right,
3365 const TSourceLoc &loc)
3366{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003367 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003368 {
3369 return intermediate.addAssign(op, left, right, loc);
3370 }
3371 return nullptr;
3372}
3373
3374TIntermTyped *TParseContext::addAssign(TOperator op, TIntermTyped *left, TIntermTyped *right,
3375 const TSourceLoc &loc)
3376{
3377 TIntermTyped *node = createAssign(op, left, right, loc);
3378 if (node == nullptr)
3379 {
3380 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
3381 recover();
3382 return left;
3383 }
3384 return node;
3385}
3386
Olli Etuaho49300862015-02-20 14:54:49 +02003387TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3388{
3389 switch (op)
3390 {
3391 case EOpContinue:
3392 if (mLoopNestingLevel <= 0)
3393 {
3394 error(loc, "continue statement only allowed in loops", "");
3395 recover();
3396 }
3397 break;
3398 case EOpBreak:
Olli Etuaho53f076f2015-02-20 10:55:14 +02003399 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
Olli Etuaho49300862015-02-20 14:54:49 +02003400 {
Olli Etuaho53f076f2015-02-20 10:55:14 +02003401 error(loc, "break statement only allowed in loops and switch statements", "");
Olli Etuaho49300862015-02-20 14:54:49 +02003402 recover();
3403 }
3404 break;
3405 case EOpReturn:
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003406 if (mCurrentFunctionType->getBasicType() != EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02003407 {
3408 error(loc, "non-void function must return a value", "return");
3409 recover();
3410 }
3411 break;
3412 default:
3413 // No checks for discard
3414 break;
3415 }
3416 return intermediate.addBranch(op, loc);
3417}
3418
3419TIntermBranch *TParseContext::addBranch(TOperator op, TIntermTyped *returnValue, const TSourceLoc &loc)
3420{
3421 ASSERT(op == EOpReturn);
3422 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003423 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02003424 {
3425 error(loc, "void function cannot return a value", "return");
3426 recover();
3427 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003428 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02003429 {
3430 error(loc, "function return is not matching type:", "return");
3431 recover();
3432 }
3433 return intermediate.addBranch(op, returnValue, loc);
3434}
3435
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003436TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall, TIntermNode *paramNode, TIntermNode *thisNode,
3437 const TSourceLoc &loc, bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003438{
3439 *fatalError = false;
3440 TOperator op = fnCall->getBuiltInOp();
3441 TIntermTyped *callNode = nullptr;
3442
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003443 if (thisNode != nullptr)
3444 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003445 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho96e67382015-04-23 14:27:02 +03003446 int arraySize = 0;
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003447 TIntermTyped *typedThis = thisNode->getAsTyped();
3448 if (fnCall->getName() != "length")
3449 {
3450 error(loc, "invalid method", fnCall->getName().c_str());
3451 recover();
3452 }
3453 else if (paramNode != nullptr)
3454 {
3455 error(loc, "method takes no parameters", "length");
3456 recover();
3457 }
3458 else if (typedThis == nullptr || !typedThis->isArray())
3459 {
3460 error(loc, "length can only be called on arrays", "length");
3461 recover();
3462 }
3463 else
3464 {
Olli Etuaho96e67382015-04-23 14:27:02 +03003465 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03003466 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003467 {
Olli Etuaho39282e12015-04-23 15:41:48 +03003468 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003469 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03003470 // (func()).length()
3471 // (int[3](0, 1, 2)).length()
3472 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid expression.
3473 // It allows "An array name with the length method applied" in contrast to GLSL 4.4 spec section 5.9
3474 // which allows "An array, vector or matrix expression with the length method applied".
3475 error(loc, "length can only be called on array names, not on array expressions", "length");
3476 recover();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003477 }
3478 }
Olli Etuaho96e67382015-04-23 14:27:02 +03003479 unionArray->setIConst(arraySize);
3480 callNode = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003481 }
3482 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003483 {
3484 //
3485 // Then this should be a constructor.
3486 // Don't go through the symbol table for constructors.
3487 // Their parameters will be verified algorithmically.
3488 //
3489 TType type(EbtVoid, EbpUndefined); // use this to get the type back
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003490 if (!constructorErrorCheck(loc, paramNode, *fnCall, op, &type))
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003491 {
3492 //
3493 // It's a constructor, of type 'type'.
3494 //
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003495 callNode = addConstructor(paramNode, &type, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003496 }
Olli Etuaho72ba85b2015-03-04 14:23:26 +02003497
3498 if (callNode == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003499 {
3500 recover();
3501 callNode = intermediate.setAggregateOperator(nullptr, op, loc);
3502 }
3503 callNode->setType(type);
3504 }
3505 else
3506 {
3507 //
3508 // Not a constructor. Find it in the symbol table.
3509 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303510 const TFunction *fnCandidate;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003511 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003512 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003513 if (fnCandidate)
3514 {
3515 //
3516 // A declared function.
3517 //
3518 if (builtIn && !fnCandidate->getExtension().empty() &&
3519 extensionErrorCheck(loc, fnCandidate->getExtension()))
3520 {
3521 recover();
3522 }
3523 op = fnCandidate->getBuiltInOp();
3524 if (builtIn && op != EOpNull)
3525 {
3526 //
3527 // A function call mapped to a built-in operation.
3528 //
3529 if (fnCandidate->getParamCount() == 1)
3530 {
3531 //
3532 // Treat it like a built-in unary operator.
3533 //
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003534 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc, &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003535 if (callNode == nullptr)
3536 {
3537 std::stringstream extraInfoStream;
3538 extraInfoStream << "built in unary operator function. Type: "
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003539 << static_cast<TIntermTyped*>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003540 std::string extraInfo = extraInfoStream.str();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003541 error(paramNode->getLine(), " wrong operand type", "Internal Error", extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003542 *fatalError = true;
3543 return nullptr;
3544 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003545 }
3546 else
3547 {
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003548 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003549 aggregate->setType(fnCandidate->getReturnType());
3550 aggregate->setPrecisionFromChildren();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003551
3552 // Some built-in functions have out parameters too.
3553 functionCallLValueErrorCheck(fnCandidate, aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05303554
3555 // See if we can constant fold a built-in.
Olli Etuahob43846e2015-06-02 18:18:57 +03003556 TIntermTyped *foldedNode = intermediate.foldAggregateBuiltIn(aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05303557 if (foldedNode)
3558 {
Arun Patole274f0702015-05-05 13:33:30 +05303559 callNode = foldedNode;
3560 }
Olli Etuahob43846e2015-06-02 18:18:57 +03003561 else
3562 {
3563 callNode = aggregate;
3564 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003565 }
3566 }
3567 else
3568 {
3569 // This is a real function call
3570
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003571 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003572 aggregate->setType(fnCandidate->getReturnType());
3573
3574 // this is how we know whether the given function is a builtIn function or a user defined function
3575 // if builtIn == false, it's a userDefined -> could be an overloaded builtIn function also
3576 // if builtIn == true, it's definitely a builtIn function with EOpNull
3577 if (!builtIn)
3578 aggregate->setUserDefined();
3579 aggregate->setName(fnCandidate->getMangledName());
Corentin Wallez71d147f2015-02-11 11:15:24 -08003580 aggregate->setFunctionId(fnCandidate->getUniqueId());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003581
3582 // This needs to happen after the name is set
3583 if (builtIn)
3584 aggregate->setBuiltInFunctionPrecision();
3585
3586 callNode = aggregate;
3587
3588 functionCallLValueErrorCheck(fnCandidate, aggregate);
3589 }
3590 }
3591 else
3592 {
3593 // error message was put out by findFunction()
3594 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003595 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003596 unionArray->setFConst(0.0f);
3597 callNode = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpUndefined, EvqConst), loc);
3598 recover();
3599 }
3600 }
3601 delete fnCall;
3602 return callNode;
3603}
3604
Olli Etuaho52901742015-04-15 13:42:45 +03003605TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond, TIntermTyped *trueBlock, TIntermTyped *falseBlock,
3606 const TSourceLoc &loc)
3607{
3608 if (boolErrorCheck(loc, cond))
3609 recover();
3610
3611 if (trueBlock->getType() != falseBlock->getType())
3612 {
3613 binaryOpError(loc, ":", trueBlock->getCompleteString(), falseBlock->getCompleteString());
3614 recover();
3615 return falseBlock;
3616 }
Olli Etuahoa2d53032015-04-15 14:14:44 +03003617 // ESSL1 sections 5.2 and 5.7:
3618 // ESSL3 section 5.7:
3619 // Ternary operator is not among the operators allowed for structures/arrays.
3620 if (trueBlock->isArray() || trueBlock->getBasicType() == EbtStruct)
3621 {
3622 error(loc, "ternary operator is not allowed for structures or arrays", ":");
3623 recover();
3624 return falseBlock;
3625 }
Olli Etuaho52901742015-04-15 13:42:45 +03003626 return intermediate.addSelection(cond, trueBlock, falseBlock, loc);
3627}
Olli Etuaho49300862015-02-20 14:54:49 +02003628
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003629//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003630// Parse an array of strings using yyparse.
3631//
3632// Returns 0 for success.
3633//
Arun Patole7e7e68d2015-05-22 12:02:25 +05303634int PaParseStrings(size_t count, const char *const string[], const int length[],
3635 TParseContext *context)
3636{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003637 if ((count == 0) || (string == NULL))
3638 return 1;
3639
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003640 if (glslang_initialize(context))
3641 return 1;
3642
alokp@chromium.org408c45e2012-04-05 15:54:43 +00003643 int error = glslang_scan(count, string, length, context);
3644 if (!error)
3645 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003646
alokp@chromium.org73bc2982012-06-19 18:48:05 +00003647 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00003648
alokp@chromium.org6b495712012-06-29 00:06:58 +00003649 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003650}
3651
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003652
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00003653