blob: 50bd30c3983c79e2c116258dd91e917059d03462 [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Jamie Madill6b9cb252013-10-17 10:45:47 -04007#include "compiler/translator/ParseContext.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +00008
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00009#include <stdarg.h>
apatrick@chromium.org8187fa82010-06-15 22:09:28 +000010#include <stdio.h>
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000011
daniel@transgaming.comb401a922012-10-26 18:58:24 +000012#include "compiler/preprocessor/SourceLocation.h"
Dmitry Skiba01971112015-07-10 14:54:00 -040013#include "compiler/translator/Cache.h"
Olli Etuahoac5274d2015-02-20 10:19:08 +020014#include "compiler/translator/glslang.h"
15#include "compiler/translator/ValidateSwitch.h"
Olli Etuahob0c645e2015-05-12 14:25:36 +030016#include "compiler/translator/ValidateGlobalInitializer.h"
Olli Etuaho37ad4742015-04-27 13:18:50 +030017#include "compiler/translator/util.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000018
alokp@chromium.org8b851c62012-06-15 16:25:11 +000019///////////////////////////////////////////////////////////////////////
20//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000021// Sub- vector and matrix fields
22//
23////////////////////////////////////////////////////////////////////////
24
25//
26// Look at a '.' field selector string and change it into offsets
27// for a vector.
28//
Arun Patole7e7e68d2015-05-22 12:02:25 +053029bool TParseContext::parseVectorFields(const TString &compString, int vecSize, TVectorFields &fields,
30 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000031{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000032 fields.num = (int) compString.size();
Arun Patole7e7e68d2015-05-22 12:02:25 +053033 if (fields.num > 4)
34 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000035 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000036 return false;
37 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000038
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000039 enum {
40 exyzw,
41 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +000042 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000043 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000044
Arun Patole7e7e68d2015-05-22 12:02:25 +053045 for (int i = 0; i < fields.num; ++i)
46 {
47 switch (compString[i])
48 {
49 case 'x':
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000050 fields.offsets[i] = 0;
51 fieldSet[i] = exyzw;
52 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053053 case 'r':
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000054 fields.offsets[i] = 0;
55 fieldSet[i] = ergba;
56 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053057 case 's':
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000058 fields.offsets[i] = 0;
59 fieldSet[i] = estpq;
60 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053061 case 'y':
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000062 fields.offsets[i] = 1;
63 fieldSet[i] = exyzw;
64 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053065 case 'g':
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000066 fields.offsets[i] = 1;
67 fieldSet[i] = ergba;
68 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053069 case 't':
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000070 fields.offsets[i] = 1;
71 fieldSet[i] = estpq;
72 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053073 case 'z':
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000074 fields.offsets[i] = 2;
75 fieldSet[i] = exyzw;
76 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053077 case 'b':
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000078 fields.offsets[i] = 2;
79 fieldSet[i] = ergba;
80 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053081 case 'p':
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000082 fields.offsets[i] = 2;
83 fieldSet[i] = estpq;
84 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053085
86 case 'w':
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000087 fields.offsets[i] = 3;
88 fieldSet[i] = exyzw;
89 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053090 case 'a':
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000091 fields.offsets[i] = 3;
92 fieldSet[i] = ergba;
93 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053094 case 'q':
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000095 fields.offsets[i] = 3;
96 fieldSet[i] = estpq;
97 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053098 default:
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000099 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000100 return false;
101 }
102 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000103
Arun Patole7e7e68d2015-05-22 12:02:25 +0530104 for (int i = 0; i < fields.num; ++i)
105 {
106 if (fields.offsets[i] >= vecSize)
107 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000108 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000109 return false;
110 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000111
Arun Patole7e7e68d2015-05-22 12:02:25 +0530112 if (i > 0)
113 {
114 if (fieldSet[i] != fieldSet[i-1])
115 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000116 error(line, "illegal - vector component fields not from the same set", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000117 return false;
118 }
119 }
120 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000121
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000122 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000123}
124
125
126//
127// Look at a '.' field selector string and change it into offsets
128// for a matrix.
129//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530130bool TParseContext::parseMatrixFields(const TString &compString, int matCols, int matRows, TMatrixFields &fields,
131 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000132{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000133 fields.wholeRow = false;
134 fields.wholeCol = false;
135 fields.row = -1;
136 fields.col = -1;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000137
Arun Patole7e7e68d2015-05-22 12:02:25 +0530138 if (compString.size() != 2)
139 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000140 error(line, "illegal length of matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000141 return false;
142 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000143
Arun Patole7e7e68d2015-05-22 12:02:25 +0530144 if (compString[0] == '_')
145 {
146 if (compString[1] < '0' || compString[1] > '3')
147 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000148 error(line, "illegal matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000149 return false;
150 }
151 fields.wholeCol = true;
152 fields.col = compString[1] - '0';
Arun Patole7e7e68d2015-05-22 12:02:25 +0530153 }
154 else if (compString[1] == '_')
155 {
156 if (compString[0] < '0' || compString[0] > '3')
157 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000158 error(line, "illegal matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000159 return false;
160 }
161 fields.wholeRow = true;
162 fields.row = compString[0] - '0';
Arun Patole7e7e68d2015-05-22 12:02:25 +0530163 }
164 else
165 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000166 if (compString[0] < '0' || compString[0] > '3' ||
Arun Patole7e7e68d2015-05-22 12:02:25 +0530167 compString[1] < '0' || compString[1] > '3')
168 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000169 error(line, "illegal matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000170 return false;
171 }
172 fields.row = compString[0] - '0';
173 fields.col = compString[1] - '0';
174 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000175
Arun Patole7e7e68d2015-05-22 12:02:25 +0530176 if (fields.row >= matRows || fields.col >= matCols)
177 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000178 error(line, "matrix field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000179 return false;
180 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000181
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000182 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000183}
184
185///////////////////////////////////////////////////////////////////////
186//
187// Errors
188//
189////////////////////////////////////////////////////////////////////////
190
191//
192// Track whether errors have occurred.
193//
194void TParseContext::recover()
195{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000196}
197
198//
199// Used by flex/bison to output all syntax and parsing errors.
200//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530201void TParseContext::error(const TSourceLoc &loc,
202 const char *reason, const char *token,
203 const char *extraInfo)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000204{
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000205 pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -0400206 srcLoc.file = loc.first_file;
207 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400208 mDiagnostics.writeInfo(pp::Diagnostics::PP_ERROR,
209 srcLoc, reason, token, extraInfo);
alokp@chromium.orgff42c632010-05-10 15:14:30 +0000210
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000211}
212
Arun Patole7e7e68d2015-05-22 12:02:25 +0530213void TParseContext::warning(const TSourceLoc &loc,
214 const char *reason, const char *token,
215 const char *extraInfo)
216{
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000217 pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -0400218 srcLoc.file = loc.first_file;
219 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400220 mDiagnostics.writeInfo(pp::Diagnostics::PP_WARNING,
221 srcLoc, reason, token, extraInfo);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000222}
223
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000224//
225// Same error message for all places assignments don't work.
226//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530227void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000228{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000229 std::stringstream extraInfoStream;
230 extraInfoStream << "cannot convert from '" << right << "' to '" << left << "'";
231 std::string extraInfo = extraInfoStream.str();
232 error(line, "", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000233}
234
235//
236// Same error message for all places unary operations don't work.
237//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530238void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000239{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000240 std::stringstream extraInfoStream;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530241 extraInfoStream << "no operation '" << op << "' exists that takes an operand of type " << operand
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000242 << " (or there is no acceptable conversion)";
243 std::string extraInfo = extraInfoStream.str();
244 error(line, " wrong operand type", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000245}
246
247//
248// Same error message for all binary operations don't work.
249//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530250void TParseContext::binaryOpError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000251{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000252 std::stringstream extraInfoStream;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530253 extraInfoStream << "no operation '" << op << "' exists that takes a left-hand operand of type '" << left
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000254 << "' and a right operand of type '" << right << "' (or there is no acceptable conversion)";
255 std::string extraInfo = extraInfoStream.str();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530256 error(line, " wrong operand types ", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000257}
258
Arun Patole7e7e68d2015-05-22 12:02:25 +0530259bool TParseContext::precisionErrorCheck(const TSourceLoc &line, TPrecision precision, TBasicType type)
260{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400261 if (!mChecksPrecisionErrors)
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000262 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530263 switch(type)
264 {
265 case EbtFloat:
266 if( precision == EbpUndefined )
267 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000268 error( line, "No precision specified for (float)", "" );
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000269 return true;
270 }
271 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530272 case EbtInt:
273 if( precision == EbpUndefined )
274 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000275 error( line, "No precision specified (int)", "" );
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000276 return true;
277 }
278 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530279 default:
daniel@transgaming.com0eb64c32011-03-15 18:23:51 +0000280 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000281 }
282 return false;
283}
284
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000285//
286// Both test and if necessary, spit out an error, to see if the node is really
287// an l-value that can be operated on this way.
288//
289// Returns true if the was an error.
290//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530291bool TParseContext::lValueErrorCheck(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000292{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530293 TIntermSymbol *symNode = node->getAsSymbolNode();
294 TIntermBinary *binaryNode = node->getAsBinaryNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000295
Arun Patole7e7e68d2015-05-22 12:02:25 +0530296 if (binaryNode)
297 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000298 bool errorReturn;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000299
Arun Patole7e7e68d2015-05-22 12:02:25 +0530300 switch(binaryNode->getOp())
301 {
302 case EOpIndexDirect:
303 case EOpIndexIndirect:
304 case EOpIndexDirectStruct:
305 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000306 return lValueErrorCheck(line, op, binaryNode->getLeft());
Arun Patole7e7e68d2015-05-22 12:02:25 +0530307 case EOpVectorSwizzle:
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000308 errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft());
Arun Patole7e7e68d2015-05-22 12:02:25 +0530309 if (!errorReturn)
310 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000311 int offset[4] = {0,0,0,0};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000312
Arun Patole7e7e68d2015-05-22 12:02:25 +0530313 TIntermTyped *rightNode = binaryNode->getRight();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000314 TIntermAggregate *aggrNode = rightNode->getAsAggregate();
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700315
316 for (TIntermSequence::iterator p = aggrNode->getSequence()->begin();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530317 p != aggrNode->getSequence()->end(); p++)
318 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +0000319 int value = (*p)->getAsTyped()->getAsConstantUnion()->getIConst(0);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700320 offset[value]++;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530321 if (offset[value] > 1)
322 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000323 error(line, " l-value of swizzle cannot have duplicate components", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000324
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000325 return true;
326 }
327 }
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700328 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000329
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000330 return errorReturn;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530331 default:
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000332 break;
333 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000334 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000335
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000336 return true;
337 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000338
339
Arun Patole7e7e68d2015-05-22 12:02:25 +0530340 const char *symbol = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000341 if (symNode != 0)
342 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000343
Arun Patole7e7e68d2015-05-22 12:02:25 +0530344 const char *message = 0;
345 switch (node->getQualifier())
346 {
347 case EvqConst:
348 message = "can't modify a const";
349 break;
350 case EvqConstReadOnly:
351 message = "can't modify a const";
352 break;
353 case EvqAttribute:
354 message = "can't modify an attribute";
355 break;
356 case EvqFragmentIn:
357 message = "can't modify an input";
358 break;
359 case EvqVertexIn:
360 message = "can't modify an input";
361 break;
362 case EvqUniform:
363 message = "can't modify a uniform";
364 break;
365 case EvqVaryingIn:
366 message = "can't modify a varying";
367 break;
368 case EvqFragCoord:
369 message = "can't modify gl_FragCoord";
370 break;
371 case EvqFrontFacing:
372 message = "can't modify gl_FrontFacing";
373 break;
374 case EvqPointCoord:
375 message = "can't modify gl_PointCoord";
376 break;
377 default:
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000378 //
379 // Type that can't be written to?
380 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530381 if (node->getBasicType() == EbtVoid)
382 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000383 message = "can't modify void";
Nicolas Capens344e7142013-06-24 15:39:21 -0400384 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530385 if (IsSampler(node->getBasicType()))
386 {
Nicolas Capens344e7142013-06-24 15:39:21 -0400387 message = "can't modify a sampler";
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000388 }
389 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000390
Arun Patole7e7e68d2015-05-22 12:02:25 +0530391 if (message == 0 && binaryNode == 0 && symNode == 0)
392 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000393 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000394
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000395 return true;
396 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000397
398
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000399 //
400 // Everything else is okay, no error.
401 //
402 if (message == 0)
403 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000404
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000405 //
406 // If we get here, we have an error and a message.
407 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530408 if (symNode)
409 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000410 std::stringstream extraInfoStream;
411 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
412 std::string extraInfo = extraInfoStream.str();
413 error(line, " l-value required", op, extraInfo.c_str());
414 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530415 else
416 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000417 std::stringstream extraInfoStream;
418 extraInfoStream << "(" << message << ")";
419 std::string extraInfo = extraInfoStream.str();
420 error(line, " l-value required", op, extraInfo.c_str());
421 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000422
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000423 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000424}
425
426//
427// Both test, and if necessary spit out an error, to see if the node is really
428// a constant.
429//
430// Returns true if the was an error.
431//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530432bool TParseContext::constErrorCheck(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000433{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000434 if (node->getQualifier() == EvqConst)
435 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000436
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000437 error(node->getLine(), "constant expression required", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000438
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000439 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000440}
441
442//
443// Both test, and if necessary spit out an error, to see if the node is really
444// an integer.
445//
446// Returns true if the was an error.
447//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530448bool TParseContext::integerErrorCheck(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000449{
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000450 if (node->isScalarInt())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000451 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000452
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000453 error(node->getLine(), "integer expression required", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000454
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000455 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000456}
457
458//
459// Both test, and if necessary spit out an error, to see if we are currently
460// globally scoped.
461//
462// Returns true if the was an error.
463//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530464bool TParseContext::globalErrorCheck(const TSourceLoc &line, bool global, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000465{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000466 if (global)
467 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000468
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000469 error(line, "only allowed at global scope", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000470
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000471 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000472}
473
474//
475// For now, keep it simple: if it starts "gl_", it's reserved, independent
476// of scope. Except, if the symbol table is at the built-in push-level,
477// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000478// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
479// webgl shader.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000480//
481// Returns true if there was an error.
482//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530483bool TParseContext::reservedErrorCheck(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000484{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530485 static const char *reservedErrMsg = "reserved built-in name";
486 if (!symbolTable.atBuiltInLevel())
487 {
488 if (identifier.compare(0, 3, "gl_") == 0)
489 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000490 error(line, reservedErrMsg, "gl_");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000491 return true;
492 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530493 if (IsWebGLBasedSpec(mShaderSpec))
494 {
495 if (identifier.compare(0, 6, "webgl_") == 0)
496 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000497 error(line, reservedErrMsg, "webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000498 return true;
499 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530500 if (identifier.compare(0, 7, "_webgl_") == 0)
501 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000502 error(line, reservedErrMsg, "_webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000503 return true;
504 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530505 if (mShaderSpec == SH_CSS_SHADERS_SPEC && identifier.compare(0, 4, "css_") == 0)
506 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000507 error(line, reservedErrMsg, "css_");
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000508 return true;
509 }
alokp@chromium.org613ef312010-07-21 18:54:22 +0000510 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530511 if (identifier.find("__") != TString::npos)
512 {
513 error(line,
514 "identifiers containing two consecutive underscores (__) are reserved as possible future keywords",
515 identifier.c_str());
daniel@transgaming.combeadd5d2012-04-12 02:35:31 +0000516 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000517 }
518 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000519
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000520 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000521}
522
523//
524// Make sure there is enough data provided to the constructor to build
525// something of the type of the constructor. Also returns the type of
526// the constructor.
527//
528// Returns true if there was an error in construction.
529//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530530bool TParseContext::constructorErrorCheck(const TSourceLoc &line, TIntermNode *node, TFunction &function, TOperator op,
531 TType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000532{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000533 *type = function.getReturnType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000534
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000535 bool constructingMatrix = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530536 switch(op)
537 {
538 case EOpConstructMat2:
Alexis Hetu07e57df2015-06-16 16:55:52 -0400539 case EOpConstructMat2x3:
540 case EOpConstructMat2x4:
541 case EOpConstructMat3x2:
Arun Patole7e7e68d2015-05-22 12:02:25 +0530542 case EOpConstructMat3:
Alexis Hetu07e57df2015-06-16 16:55:52 -0400543 case EOpConstructMat3x4:
544 case EOpConstructMat4x2:
545 case EOpConstructMat4x3:
Arun Patole7e7e68d2015-05-22 12:02:25 +0530546 case EOpConstructMat4:
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000547 constructingMatrix = true;
548 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530549 default:
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000550 break;
551 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000552
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000553 //
554 // Note: It's okay to have too many components available, but not okay to have unused
555 // arguments. 'full' will go to true when enough args have been seen. If we loop
556 // again, there is an extra argument, so 'overfull' will become true.
557 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000558
Jamie Madill94bf7f22013-07-08 13:31:15 -0400559 size_t size = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000560 bool constType = true;
561 bool full = false;
562 bool overFull = false;
563 bool matrixInMatrix = false;
564 bool arrayArg = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530565 for (size_t i = 0; i < function.getParamCount(); ++i)
566 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700567 const TConstParameter &param = function.getParam(i);
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000568 size += param.type->getObjectSize();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530569
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000570 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000571 matrixInMatrix = true;
572 if (full)
573 overFull = true;
574 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
575 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000576 if (param.type->getQualifier() != EvqConst)
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000577 constType = false;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000578 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000579 arrayArg = true;
580 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530581
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000582 if (constType)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000583 type->setQualifier(EvqConst);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000584
Olli Etuaho376f1b52015-04-13 13:23:41 +0300585 if (type->isArray())
586 {
587 if (type->isUnsizedArray())
588 {
589 type->setArraySize(function.getParamCount());
590 }
591 else if (static_cast<size_t>(type->getArraySize()) != function.getParamCount())
592 {
593 error(line, "array constructor needs one argument per array element", "constructor");
594 return true;
595 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000596 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000597
Arun Patole7e7e68d2015-05-22 12:02:25 +0530598 if (arrayArg && op != EOpConstructStruct)
599 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000600 error(line, "constructing from a non-dereferenced array", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000601 return true;
602 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000603
Arun Patole7e7e68d2015-05-22 12:02:25 +0530604 if (matrixInMatrix && !type->isArray())
605 {
606 if (function.getParamCount() != 1)
607 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000608 error(line, "constructing matrix from matrix can only take one argument", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000609 return true;
610 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000611 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000612
Arun Patole7e7e68d2015-05-22 12:02:25 +0530613 if (overFull)
614 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000615 error(line, "too many arguments", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000616 return true;
617 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530618
619 if (op == EOpConstructStruct && !type->isArray() && type->getStruct()->fields().size() != function.getParamCount())
620 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000621 error(line, "Number of constructor parameters does not match the number of structure fields", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000622 return true;
623 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000624
Arun Patole7e7e68d2015-05-22 12:02:25 +0530625 if (!type->isMatrix() || !matrixInMatrix)
626 {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000627 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
Arun Patole7e7e68d2015-05-22 12:02:25 +0530628 (op == EOpConstructStruct && size < type->getObjectSize()))
629 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000630 error(line, "not enough data provided for construction", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000631 return true;
632 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000633 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000634
daniel@transgaming.com0b53fc02011-03-09 15:12:12 +0000635 TIntermTyped *typed = node ? node->getAsTyped() : 0;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530636 if (typed == 0)
637 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000638 error(line, "constructor argument does not have a type", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000639 return true;
640 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530641 if (op != EOpConstructStruct && IsSampler(typed->getBasicType()))
642 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000643 error(line, "cannot convert a sampler", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000644 return true;
645 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530646 if (typed->getBasicType() == EbtVoid)
647 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000648 error(line, "cannot convert a void", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000649 return true;
650 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000651
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000652 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000653}
654
655// This function checks to see if a void variable has been declared and raise an error message for such a case
656//
657// returns true in case of an error
658//
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300659bool TParseContext::voidErrorCheck(const TSourceLoc &line, const TString &identifier, const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000660{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300661 if (type == EbtVoid)
662 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000663 error(line, "illegal use of type 'void'", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000664 return true;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300665 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000666
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000667 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000668}
669
670// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
671//
672// returns true in case of an error
673//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530674bool TParseContext::boolErrorCheck(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000675{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530676 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector())
677 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000678 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000679 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530680 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000681
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000682 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000683}
684
685// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
686//
687// returns true in case of an error
688//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530689bool TParseContext::boolErrorCheck(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000690{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530691 if (pType.type != EbtBool || pType.isAggregate())
692 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000693 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000694 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530695 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000696
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000697 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000698}
699
Arun Patole7e7e68d2015-05-22 12:02:25 +0530700bool TParseContext::samplerErrorCheck(const TSourceLoc &line, const TPublicType &pType, const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000701{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530702 if (pType.type == EbtStruct)
703 {
704 if (containsSampler(*pType.userDef))
705 {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000706 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530707
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000708 return true;
709 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530710
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000711 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530712 }
713 else if (IsSampler(pType.type))
714 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000715 error(line, reason, getBasicString(pType.type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000716
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000717 return true;
718 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000719
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000720 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000721}
722
Arun Patole7e7e68d2015-05-22 12:02:25 +0530723bool TParseContext::locationDeclaratorListCheck(const TSourceLoc &line, const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400724{
725 if (pType.layoutQualifier.location != -1)
726 {
727 error(line, "location must only be specified for a single input or output variable", "location");
728 return true;
729 }
730
731 return false;
732}
733
Arun Patole7e7e68d2015-05-22 12:02:25 +0530734bool TParseContext::parameterSamplerErrorCheck(const TSourceLoc &line, TQualifier qualifier, const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000735{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530736 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
737 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType()))
738 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000739 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000740 return true;
741 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000742
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000743 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000744}
745
Arun Patole7e7e68d2015-05-22 12:02:25 +0530746bool TParseContext::containsSampler(const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000747{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000748 if (IsSampler(type.getBasicType()))
749 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000750
Arun Patole7e7e68d2015-05-22 12:02:25 +0530751 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock())
752 {
753 const TFieldList &fields = type.getStruct()->fields();
754 for (unsigned int i = 0; i < fields.size(); ++i)
755 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400756 if (containsSampler(*fields[i]->type()))
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000757 return true;
758 }
759 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000760
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000761 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000762}
763
764//
765// Do size checking for an array type's size.
766//
767// Returns true if there was an error.
768//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530769bool TParseContext::arraySizeErrorCheck(const TSourceLoc &line, TIntermTyped *expr, int &size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000770{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530771 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000772
Olli Etuahoe7847b02015-03-16 11:56:12 +0200773 if (constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000774 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000775 error(line, "array size must be a constant integer expression", "");
Olli Etuahoe7847b02015-03-16 11:56:12 +0200776 size = 1;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000777 return true;
778 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000779
Nicolas Capens906744a2014-06-06 15:18:07 -0400780 unsigned int unsignedSize = 0;
781
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000782 if (constant->getBasicType() == EbtUInt)
783 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400784 unsignedSize = constant->getUConst(0);
785 size = static_cast<int>(unsignedSize);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000786 }
787 else
788 {
789 size = constant->getIConst(0);
790
Nicolas Capens906744a2014-06-06 15:18:07 -0400791 if (size < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000792 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400793 error(line, "array size must be non-negative", "");
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000794 size = 1;
795 return true;
796 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400797
798 unsignedSize = static_cast<unsigned int>(size);
799 }
800
801 if (size == 0)
802 {
803 error(line, "array size must be greater than zero", "");
804 size = 1;
805 return true;
806 }
807
808 // The size of arrays is restricted here to prevent issues further down the
809 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
810 // 4096 registers so this should be reasonable even for aggressively optimizable code.
811 const unsigned int sizeLimit = 65536;
812
813 if (unsignedSize > sizeLimit)
814 {
815 error(line, "array size too large", "");
816 size = 1;
817 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000818 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000819
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000820 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000821}
822
823//
824// See if this qualifier can be an array.
825//
826// Returns true if there is an error.
827//
Olli Etuaho3739d232015-04-08 12:23:44 +0300828bool TParseContext::arrayQualifierErrorCheck(const TSourceLoc &line, const TPublicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000829{
Olli Etuaho3739d232015-04-08 12:23:44 +0300830 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqVertexIn) ||
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400831 (type.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300832 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000833 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000834 return true;
835 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000836
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000837 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000838}
839
840//
841// See if this type can be an array.
842//
843// Returns true if there is an error.
844//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530845bool TParseContext::arrayTypeErrorCheck(const TSourceLoc &line, const TPublicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000846{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000847 //
848 // Can the type be an array?
849 //
Jamie Madill06145232015-05-13 13:10:01 -0400850 if (type.array)
851 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000852 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000853 return true;
854 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000855
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000856 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000857}
858
859//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000860// Enforce non-initializer type/qualifier rules.
861//
862// Returns true if there was an error.
863//
Olli Etuaho376f1b52015-04-13 13:23:41 +0300864bool TParseContext::nonInitErrorCheck(const TSourceLoc &line, const TString &identifier, TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000865{
Olli Etuaho3739d232015-04-08 12:23:44 +0300866 ASSERT(type != nullptr);
867 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000868 {
869 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300870 type->qualifier = EvqTemporary;
871
872 // Generate informative error messages for ESSL1.
873 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400874 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000875 {
Arun Patole7e7e68d2015-05-22 12:02:25 +0530876 error(line,
877 "structures containing arrays may not be declared constant since they cannot be initialized",
878 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000879 }
880 else
881 {
882 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
883 }
884
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000885 return true;
886 }
Olli Etuaho376f1b52015-04-13 13:23:41 +0300887 if (type->isUnsizedArray())
888 {
889 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
890 return true;
891 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000892 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000893}
894
Olli Etuaho2935c582015-04-08 14:32:06 +0300895// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000896// and update the symbol table.
897//
Olli Etuaho2935c582015-04-08 14:32:06 +0300898// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000899//
Olli Etuaho2935c582015-04-08 14:32:06 +0300900bool TParseContext::declareVariable(const TSourceLoc &line, const TString &identifier, const TType &type,
901 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000902{
Olli Etuaho2935c582015-04-08 14:32:06 +0300903 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000904
Olli Etuaho2935c582015-04-08 14:32:06 +0300905 bool needsReservedErrorCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000906
Olli Etuaho2935c582015-04-08 14:32:06 +0300907 // gl_LastFragData may be redeclared with a new precision qualifier
908 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
909 {
910 const TVariable *maxDrawBuffers =
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400911 static_cast<const TVariable *>(symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho2935c582015-04-08 14:32:06 +0300912 if (type.getArraySize() == maxDrawBuffers->getConstPointer()->getIConst())
913 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400914 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +0300915 {
916 needsReservedErrorCheck = extensionErrorCheck(line, builtInSymbol->getExtension());
917 }
918 }
919 else
920 {
921 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers", identifier.c_str());
922 return false;
923 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000924 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000925
Olli Etuaho2935c582015-04-08 14:32:06 +0300926 if (needsReservedErrorCheck && reservedErrorCheck(line, identifier))
927 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000928
Olli Etuaho2935c582015-04-08 14:32:06 +0300929 (*variable) = new TVariable(&identifier, type);
930 if (!symbolTable.declare(*variable))
931 {
932 error(line, "redefinition", identifier.c_str());
933 delete (*variable);
934 (*variable) = nullptr;
935 return false;
936 }
937
938 if (voidErrorCheck(line, identifier, type.getBasicType()))
939 return false;
940
941 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000942}
943
Arun Patole7e7e68d2015-05-22 12:02:25 +0530944bool TParseContext::paramErrorCheck(const TSourceLoc &line, TQualifier qualifier, TQualifier paramQualifier,
945 TType *type)
946{
947 if (qualifier != EvqConst && qualifier != EvqTemporary)
948 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000949 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000950 return true;
951 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530952 if (qualifier == EvqConst && paramQualifier != EvqIn)
953 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000954 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
955 return true;
956 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000957
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000958 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000959 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000960 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000961 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000962
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000963 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000964}
965
Arun Patole7e7e68d2015-05-22 12:02:25 +0530966bool TParseContext::extensionErrorCheck(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000967{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530968 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000969 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +0530970 if (iter == extBehavior.end())
971 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000972 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000973 return true;
974 }
zmo@google.comf5450912011-09-09 01:37:19 +0000975 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +0530976 if (iter->second == EBhDisable || iter->second == EBhUndefined)
977 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000978 error(line, "extension", extension.c_str(), "is disabled");
979 return true;
980 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530981 if (iter->second == EBhWarn)
982 {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000983 warning(line, "extension", extension.c_str(), "is being used");
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000984 return false;
985 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000986
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000987 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000988}
989
Olli Etuahofa33d582015-04-09 14:33:12 +0300990// These checks are common for all declarations starting a declarator list, and declarators that follow an empty
991// declaration.
992//
Jamie Madill06145232015-05-13 13:10:01 -0400993bool TParseContext::singleDeclarationErrorCheck(const TPublicType &publicType, const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -0400994{
Olli Etuahofa33d582015-04-09 14:33:12 +0300995 switch (publicType.qualifier)
996 {
997 case EvqVaryingIn:
998 case EvqVaryingOut:
999 case EvqAttribute:
1000 case EvqVertexIn:
1001 case EvqFragmentOut:
1002 if (publicType.type == EbtStruct)
1003 {
1004 error(identifierLocation, "cannot be used with a structure",
1005 getQualifierString(publicType.qualifier));
1006 return true;
1007 }
1008
1009 default: break;
1010 }
1011
1012 if (publicType.qualifier != EvqUniform && samplerErrorCheck(identifierLocation, publicType,
1013 "samplers must be uniform"))
1014 {
Jamie Madilla5efff92013-06-06 11:56:47 -04001015 return true;
Olli Etuahofa33d582015-04-09 14:33:12 +03001016 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001017
1018 // check for layout qualifier issues
1019 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
1020
1021 if (layoutQualifier.matrixPacking != EmpUnspecified)
1022 {
Olli Etuahofa33d582015-04-09 14:33:12 +03001023 error(identifierLocation, "layout qualifier", getMatrixPackingString(layoutQualifier.matrixPacking),
1024 "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -04001025 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001026 }
1027
1028 if (layoutQualifier.blockStorage != EbsUnspecified)
1029 {
Olli Etuahofa33d582015-04-09 14:33:12 +03001030 error(identifierLocation, "layout qualifier", getBlockStorageString(layoutQualifier.blockStorage),
1031 "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -04001032 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001033 }
1034
Olli Etuahofa33d582015-04-09 14:33:12 +03001035 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut &&
1036 layoutLocationErrorCheck(identifierLocation, publicType.layoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001037 {
Jamie Madill51a53c72013-06-19 09:24:43 -04001038 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001039 }
1040
1041 return false;
1042}
1043
Arun Patole7e7e68d2015-05-22 12:02:25 +05301044bool TParseContext::layoutLocationErrorCheck(const TSourceLoc &location, const TLayoutQualifier &layoutQualifier)
Jamie Madilla5efff92013-06-06 11:56:47 -04001045{
1046 if (layoutQualifier.location != -1)
1047 {
1048 error(location, "invalid layout qualifier:", "location", "only valid on program inputs and outputs");
1049 return true;
1050 }
1051
1052 return false;
1053}
1054
Olli Etuahob6e07a62015-02-16 12:22:10 +02001055bool TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate, TIntermAggregate *aggregate)
1056{
1057 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1058 {
1059 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1060 if (qual == EvqOut || qual == EvqInOut)
1061 {
1062 TIntermTyped *node = (*(aggregate->getSequence()))[i]->getAsTyped();
1063 if (lValueErrorCheck(node->getLine(), "assign", node))
1064 {
1065 error(node->getLine(),
1066 "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error");
1067 recover();
1068 return true;
1069 }
1070 }
1071 }
1072 return false;
1073}
1074
Olli Etuaho37ad4742015-04-27 13:18:50 +03001075void TParseContext::es3InvariantErrorCheck(const TQualifier qualifier, const TSourceLoc &invariantLocation)
1076{
1077 if (!sh::IsVaryingOut(qualifier) && qualifier != EvqFragmentOut)
1078 {
1079 error(invariantLocation, "Only out variables can be invariant.", "invariant");
1080 recover();
1081 }
1082}
1083
Arun Patole7e7e68d2015-05-22 12:02:25 +05301084bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001085{
Arun Patole7e7e68d2015-05-22 12:02:25 +05301086 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001087 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1088 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001089}
1090
Arun Patole7e7e68d2015-05-22 12:02:25 +05301091bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001092{
Arun Patole7e7e68d2015-05-22 12:02:25 +05301093 const TExtensionBehavior &extbehavior = extensionBehavior();
Shannon Woodsa49a9bf2013-08-02 17:23:14 -04001094 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001095
1096 if (iter == extbehavior.end())
1097 {
1098 return false;
1099 }
1100
1101 return (iter->second == EBhEnable || iter->second == EBhRequire);
1102}
1103
Arun Patole7e7e68d2015-05-22 12:02:25 +05301104void TParseContext::handleExtensionDirective(const TSourceLoc &loc, const char *extName, const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001105{
1106 pp::SourceLocation srcLoc;
1107 srcLoc.file = loc.first_file;
1108 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001109 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001110}
1111
Arun Patole7e7e68d2015-05-22 12:02:25 +05301112void TParseContext::handlePragmaDirective(const TSourceLoc &loc, const char *name, const char *value, bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001113{
1114 pp::SourceLocation srcLoc;
1115 srcLoc.file = loc.first_file;
1116 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001117 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001118}
1119
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001120/////////////////////////////////////////////////////////////////////////////////
1121//
1122// Non-Errors.
1123//
1124/////////////////////////////////////////////////////////////////////////////////
1125
Jamie Madill5c097022014-08-20 16:38:32 -04001126const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1127 const TString *name,
1128 const TSymbol *symbol)
1129{
1130 const TVariable *variable = NULL;
1131
1132 if (!symbol)
1133 {
1134 error(location, "undeclared identifier", name->c_str());
1135 recover();
1136 }
1137 else if (!symbol->isVariable())
1138 {
1139 error(location, "variable expected", name->c_str());
1140 recover();
1141 }
1142 else
1143 {
1144 variable = static_cast<const TVariable*>(symbol);
1145
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001146 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
Jamie Madill5c097022014-08-20 16:38:32 -04001147 !variable->getExtension().empty() &&
1148 extensionErrorCheck(location, variable->getExtension()))
1149 {
1150 recover();
1151 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001152
1153 // Reject shaders using both gl_FragData and gl_FragColor
1154 TQualifier qualifier = variable->getType().getQualifier();
1155 if (qualifier == EvqFragData)
1156 {
1157 mUsesFragData = true;
1158 }
1159 else if (qualifier == EvqFragColor)
1160 {
1161 mUsesFragColor = true;
1162 }
1163
1164 // This validation is not quite correct - it's only an error to write to
1165 // both FragData and FragColor. For simplicity, and because users shouldn't
1166 // be rewarded for reading from undefined varaibles, return an error
1167 // if they are both referenced, rather than assigned.
1168 if (mUsesFragData && mUsesFragColor)
1169 {
1170 error(location, "cannot use both gl_FragData and gl_FragColor", name->c_str());
1171 recover();
1172 }
Jamie Madill5c097022014-08-20 16:38:32 -04001173 }
1174
1175 if (!variable)
1176 {
1177 TType type(EbtFloat, EbpUndefined);
1178 TVariable *fakeVariable = new TVariable(name, type);
1179 symbolTable.declare(fakeVariable);
1180 variable = fakeVariable;
1181 }
1182
1183 return variable;
1184}
1185
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001186//
1187// Look up a function name in the symbol table, and make sure it is a function.
1188//
1189// Return the function symbol if found, otherwise 0.
1190//
Arun Patole7e7e68d2015-05-22 12:02:25 +05301191const TFunction *TParseContext::findFunction(const TSourceLoc &line, TFunction *call, int inputShaderVersion,
1192 bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001193{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001194 // First find by unmangled name to check whether the function name has been
1195 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001196 // If a function is found, check for one with a matching argument list.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301197 const TSymbol *symbol = symbolTable.find(call->getName(), inputShaderVersion, builtIn);
1198 if (symbol == 0 || symbol->isFunction())
1199 {
Austin Kinross3ae64652015-01-26 15:51:39 -08001200 symbol = symbolTable.find(call->getMangledName(), inputShaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001201 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001202
Arun Patole7e7e68d2015-05-22 12:02:25 +05301203 if (symbol == 0)
1204 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001205 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001206 return 0;
1207 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001208
Arun Patole7e7e68d2015-05-22 12:02:25 +05301209 if (!symbol->isFunction())
1210 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001211 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001212 return 0;
1213 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001214
1215 return static_cast<const TFunction*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001216}
1217
1218//
1219// Initializers show up in several places in the grammar. Have one set of
1220// code to handle them here.
1221//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001222// Returns true on error, false if no error
1223//
Jamie Madill06145232015-05-13 13:10:01 -04001224bool TParseContext::executeInitializer(const TSourceLoc &line, const TString &identifier, const TPublicType &pType,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001225 TIntermTyped *initializer, TIntermNode **intermNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001226{
Olli Etuahoe7847b02015-03-16 11:56:12 +02001227 ASSERT(intermNode != nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001228 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001229
Olli Etuaho2935c582015-04-08 14:32:06 +03001230 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001231 if (type.isUnsizedArray())
1232 {
1233 type.setArraySize(initializer->getArraySize());
1234 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001235 if (!declareVariable(line, identifier, type, &variable))
1236 {
1237 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001238 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001239
Olli Etuahob0c645e2015-05-12 14:25:36 +03001240 bool globalInitWarning = false;
1241 if (symbolTable.atGlobalLevel() && !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
1242 {
1243 // Error message does not completely match behavior with ESSL 1.00, but
1244 // we want to steer developers towards only using constant expressions.
1245 error(line, "global variable initializers must be constant expressions", "=");
1246 return true;
1247 }
1248 if (globalInitWarning)
1249 {
1250 warning(line, "global variable initializers should be constant expressions "
1251 "(uniforms and globals are allowed in global initializers for legacy compatibility)", "=");
1252 }
1253
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001254 //
1255 // identifier must be of type constant, a global, or a temporary
1256 //
1257 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301258 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1259 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001260 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001261 return true;
1262 }
1263 //
1264 // test for and propagate constant
1265 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001266
Arun Patole7e7e68d2015-05-22 12:02:25 +05301267 if (qualifier == EvqConst)
1268 {
1269 if (qualifier != initializer->getType().getQualifier())
1270 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001271 std::stringstream extraInfoStream;
1272 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1273 std::string extraInfo = extraInfoStream.str();
1274 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001275 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001276 return true;
1277 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301278 if (type != initializer->getType())
1279 {
1280 error(line, " non-matching types for const initializer ",
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001281 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001282 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001283 return true;
1284 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301285 if (initializer->getAsConstantUnion())
1286 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001287 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Arun Patole7e7e68d2015-05-22 12:02:25 +05301288 }
1289 else if (initializer->getAsSymbolNode())
1290 {
1291 const TSymbol *symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1292 const TVariable *tVar = static_cast<const TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001293
Arun Patole7e7e68d2015-05-22 12:02:25 +05301294 TConstantUnion *constArray = tVar->getConstPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001295 variable->shareConstPointer(constArray);
Arun Patole7e7e68d2015-05-22 12:02:25 +05301296 }
1297 else
1298 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001299 std::stringstream extraInfoStream;
1300 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1301 std::string extraInfo = extraInfoStream.str();
1302 error(line, " cannot assign to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001303 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001304 return true;
1305 }
1306 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001307
1308 if (qualifier != EvqConst)
1309 {
1310 TIntermSymbol *intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(),
1311 variable->getType(), line);
1312 *intermNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1313 if (*intermNode == nullptr)
1314 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001315 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1316 return true;
1317 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001318 }
1319 else
1320 {
1321 *intermNode = nullptr;
1322 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001323
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001324 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001325}
1326
Arun Patole7e7e68d2015-05-22 12:02:25 +05301327bool TParseContext::areAllChildConst(TIntermAggregate *aggrNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001328{
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001329 ASSERT(aggrNode != NULL);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001330 if (!aggrNode->isConstructor())
1331 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001332
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001333 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001334
Arun Patole7e7e68d2015-05-22 12:02:25 +05301335 // check if all the child nodes are constants so that they can be inserted into
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001336 // the parent node
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001337 TIntermSequence *sequence = aggrNode->getSequence() ;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301338 for (TIntermSequence::iterator p = sequence->begin(); p != sequence->end(); ++p)
1339 {
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001340 if (!(*p)->getAsTyped()->getAsConstantUnion())
1341 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001342 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001343
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001344 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001345}
1346
Olli Etuaho214c2d82015-04-27 14:49:13 +03001347TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier, bool invariant, TLayoutQualifier layoutQualifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301348 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001349{
1350 TPublicType returnType = typeSpecifier;
1351 returnType.qualifier = qualifier;
Olli Etuaho214c2d82015-04-27 14:49:13 +03001352 returnType.invariant = invariant;
Jamie Madilla5efff92013-06-06 11:56:47 -04001353 returnType.layoutQualifier = layoutQualifier;
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001354
1355 if (typeSpecifier.array)
1356 {
1357 error(typeSpecifier.line, "not supported", "first-class array");
1358 recover();
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001359 returnType.clearArrayness();
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001360 }
1361
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001362 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001363 {
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001364 if (qualifier == EvqAttribute && (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1365 {
1366 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1367 recover();
1368 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001369
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001370 if ((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1371 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1372 {
1373 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1374 recover();
1375 }
1376 }
1377 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001378 {
Jamie Madillb120eac2013-06-12 14:08:13 -04001379 switch (qualifier)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001380 {
Jamie Madill19571812013-08-12 15:26:34 -07001381 case EvqSmoothIn:
1382 case EvqSmoothOut:
1383 case EvqVertexOut:
1384 case EvqFragmentIn:
1385 case EvqCentroidOut:
1386 case EvqCentroidIn:
1387 if (typeSpecifier.type == EbtBool)
1388 {
1389 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1390 recover();
1391 }
1392 if (typeSpecifier.type == EbtInt || typeSpecifier.type == EbtUInt)
1393 {
1394 error(typeSpecifier.line, "must use 'flat' interpolation here", getQualifierString(qualifier));
1395 recover();
1396 }
1397 break;
1398
1399 case EvqVertexIn:
1400 case EvqFragmentOut:
1401 case EvqFlatIn:
1402 case EvqFlatOut:
Jamie Madillb120eac2013-06-12 14:08:13 -04001403 if (typeSpecifier.type == EbtBool)
1404 {
1405 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1406 recover();
1407 }
1408 break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001409
Jamie Madillb120eac2013-06-12 14:08:13 -04001410 default: break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001411 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001412 }
1413
1414 return returnType;
1415}
1416
Olli Etuahofa33d582015-04-09 14:33:12 +03001417TIntermAggregate *TParseContext::parseSingleDeclaration(TPublicType &publicType,
1418 const TSourceLoc &identifierOrTypeLocation,
1419 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001420{
Olli Etuahofa33d582015-04-09 14:33:12 +03001421 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001422
Olli Etuahobab4c082015-04-24 16:38:49 +03001423 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03001424
Olli Etuahobab4c082015-04-24 16:38:49 +03001425 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1426
1427 if (emptyDeclaration)
1428 {
1429 if (publicType.isUnsizedArray())
1430 {
1431 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an error.
1432 // It is assumed that this applies to empty declarations as well.
1433 error(identifierOrTypeLocation, "empty array declaration needs to specify a size", identifier.c_str());
1434 }
1435 }
1436 else
Jamie Madill60ed9812013-06-06 11:56:46 -04001437 {
Olli Etuahofa33d582015-04-09 14:33:12 +03001438 if (singleDeclarationErrorCheck(publicType, identifierOrTypeLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001439 recover();
1440
Olli Etuaho376f1b52015-04-13 13:23:41 +03001441 if (nonInitErrorCheck(identifierOrTypeLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001442 recover();
1443
Olli Etuaho2935c582015-04-08 14:32:06 +03001444 TVariable *variable = nullptr;
Olli Etuahofa33d582015-04-09 14:33:12 +03001445 if (!declareVariable(identifierOrTypeLocation, identifier, TType(publicType), &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001446 recover();
1447
1448 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001449 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001450 }
1451
Olli Etuahoe7847b02015-03-16 11:56:12 +02001452 return intermediate.makeAggregate(symbol, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001453}
1454
Olli Etuahoe7847b02015-03-16 11:56:12 +02001455TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1456 const TSourceLoc &identifierLocation,
1457 const TString &identifier,
1458 const TSourceLoc &indexLocation,
1459 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001460{
Olli Etuahofa33d582015-04-09 14:33:12 +03001461 mDeferredSingleDeclarationErrorCheck = false;
1462
1463 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001464 recover();
1465
Olli Etuaho376f1b52015-04-13 13:23:41 +03001466 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001467 recover();
1468
1469 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1470 {
1471 recover();
1472 }
1473
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001474 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001475
1476 int size;
1477 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1478 {
1479 recover();
1480 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001481 // Make the type an array even if size check failed.
1482 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1483 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001484
Olli Etuaho2935c582015-04-08 14:32:06 +03001485 TVariable *variable = nullptr;
1486 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001487 recover();
1488
Olli Etuahoe7847b02015-03-16 11:56:12 +02001489 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001490 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001491 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001492
Olli Etuahoe7847b02015-03-16 11:56:12 +02001493 return intermediate.makeAggregate(symbol, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001494}
1495
Jamie Madill06145232015-05-13 13:10:01 -04001496TIntermAggregate *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001497 const TSourceLoc &identifierLocation,
1498 const TString &identifier,
1499 const TSourceLoc &initLocation,
1500 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001501{
Olli Etuahofa33d582015-04-09 14:33:12 +03001502 mDeferredSingleDeclarationErrorCheck = false;
1503
1504 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001505 recover();
1506
Olli Etuahoe7847b02015-03-16 11:56:12 +02001507 TIntermNode *intermNode = nullptr;
1508 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04001509 {
1510 //
1511 // Build intermediate representation
1512 //
Olli Etuahoe7847b02015-03-16 11:56:12 +02001513 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001514 }
1515 else
1516 {
1517 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001518 return nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001519 }
1520}
1521
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001522TIntermAggregate *TParseContext::parseSingleArrayInitDeclaration(TPublicType &publicType,
1523 const TSourceLoc &identifierLocation,
1524 const TString &identifier,
1525 const TSourceLoc &indexLocation,
1526 TIntermTyped *indexExpression,
1527 const TSourceLoc &initLocation,
1528 TIntermTyped *initializer)
1529{
1530 mDeferredSingleDeclarationErrorCheck = false;
1531
1532 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1533 recover();
1534
1535 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1536 {
1537 recover();
1538 }
1539
1540 TPublicType arrayType(publicType);
1541
Olli Etuaho376f1b52015-04-13 13:23:41 +03001542 int size = 0;
1543 // If indexExpression is nullptr, then the array will eventually get its size implicitly from the initializer.
1544 if (indexExpression != nullptr && arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001545 {
1546 recover();
1547 }
1548 // Make the type an array even if size check failed.
1549 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1550 arrayType.setArraySize(size);
1551
1552 // initNode will correspond to the whole of "type b[n] = initializer".
1553 TIntermNode *initNode = nullptr;
1554 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1555 {
1556 return initNode ? intermediate.makeAggregate(initNode, initLocation) : nullptr;
1557 }
1558 else
1559 {
1560 recover();
1561 return nullptr;
1562 }
1563}
1564
Olli Etuahoe7847b02015-03-16 11:56:12 +02001565TIntermAggregate *TParseContext::parseInvariantDeclaration(const TSourceLoc &invariantLoc,
Jamie Madill47e3ec02014-08-20 16:38:33 -04001566 const TSourceLoc &identifierLoc,
1567 const TString *identifier,
1568 const TSymbol *symbol)
1569{
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001570 // invariant declaration
Jamie Madill47e3ec02014-08-20 16:38:33 -04001571 if (globalErrorCheck(invariantLoc, symbolTable.atGlobalLevel(), "invariant varying"))
1572 {
1573 recover();
1574 }
1575
1576 if (!symbol)
1577 {
1578 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
1579 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001580 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001581 }
1582 else
1583 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001584 const TString kGlFrontFacing("gl_FrontFacing");
1585 if (*identifier == kGlFrontFacing)
1586 {
1587 error(identifierLoc, "identifier should not be declared as invariant", identifier->c_str());
1588 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001589 return nullptr;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001590 }
Jamie Madill2c433252014-12-03 12:36:54 -05001591 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001592 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1593 ASSERT(variable);
1594 const TType &type = variable->getType();
1595 TIntermSymbol *intermSymbol = intermediate.addSymbol(variable->getUniqueId(),
1596 *identifier, type, identifierLoc);
1597
1598 TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc);
1599 aggregate->setOp(EOpInvariantDeclaration);
1600 return aggregate;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001601 }
1602}
1603
Olli Etuahoe7847b02015-03-16 11:56:12 +02001604TIntermAggregate *TParseContext::parseDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1605 const TSourceLoc &identifierLocation, const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001606{
Olli Etuahofa33d582015-04-09 14:33:12 +03001607 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1608 if (mDeferredSingleDeclarationErrorCheck)
1609 {
1610 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1611 recover();
1612 mDeferredSingleDeclarationErrorCheck = false;
1613 }
1614
Jamie Madill0bd18df2013-06-20 11:55:52 -04001615 if (locationDeclaratorListCheck(identifierLocation, publicType))
1616 recover();
1617
Olli Etuaho376f1b52015-04-13 13:23:41 +03001618 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001619 recover();
1620
Olli Etuaho2935c582015-04-08 14:32:06 +03001621 TVariable *variable = nullptr;
1622 if (!declareVariable(identifierLocation, identifier, TType(publicType), &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001623 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001624
1625 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1626 if (variable && symbol)
Jamie Madill502d66f2013-06-20 11:55:52 -04001627 symbol->setId(variable->getUniqueId());
1628
Olli Etuahoe7847b02015-03-16 11:56:12 +02001629 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001630}
1631
Olli Etuahoe7847b02015-03-16 11:56:12 +02001632TIntermAggregate *TParseContext::parseArrayDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1633 const TSourceLoc &identifierLocation, const TString &identifier,
1634 const TSourceLoc &arrayLocation, TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001635{
Olli Etuahofa33d582015-04-09 14:33:12 +03001636 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1637 if (mDeferredSingleDeclarationErrorCheck)
1638 {
1639 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1640 recover();
1641 mDeferredSingleDeclarationErrorCheck = false;
1642 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001643
Jamie Madill0bd18df2013-06-20 11:55:52 -04001644 if (locationDeclaratorListCheck(identifierLocation, publicType))
1645 recover();
1646
Olli Etuaho376f1b52015-04-13 13:23:41 +03001647 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001648 recover();
1649
1650 if (arrayTypeErrorCheck(arrayLocation, publicType) || arrayQualifierErrorCheck(arrayLocation, publicType))
1651 {
1652 recover();
1653 }
Olli Etuaho93a90fd2015-04-07 18:14:07 +03001654 else
Jamie Madill502d66f2013-06-20 11:55:52 -04001655 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001656 TType arrayType = TType(publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04001657 int size;
1658 if (arraySizeErrorCheck(arrayLocation, indexExpression, size))
Olli Etuahoe7847b02015-03-16 11:56:12 +02001659 {
Jamie Madill502d66f2013-06-20 11:55:52 -04001660 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001661 }
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001662 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001663
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001664 TVariable *variable = nullptr;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001665 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001666 recover();
Jamie Madill502d66f2013-06-20 11:55:52 -04001667
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001668 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
1669 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001670 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001671
1672 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001673 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001674
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001675 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001676}
1677
Jamie Madill06145232015-05-13 13:10:01 -04001678TIntermAggregate *TParseContext::parseInitDeclarator(const TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001679 const TSourceLoc &identifierLocation, const TString &identifier,
1680 const TSourceLoc &initLocation, TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001681{
Olli Etuahofa33d582015-04-09 14:33:12 +03001682 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1683 if (mDeferredSingleDeclarationErrorCheck)
1684 {
1685 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1686 recover();
1687 mDeferredSingleDeclarationErrorCheck = false;
1688 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001689
Jamie Madill0bd18df2013-06-20 11:55:52 -04001690 if (locationDeclaratorListCheck(identifierLocation, publicType))
1691 recover();
1692
Olli Etuahoe7847b02015-03-16 11:56:12 +02001693 TIntermNode *intermNode = nullptr;
1694 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04001695 {
1696 //
1697 // build the intermediate representation
1698 //
1699 if (intermNode)
1700 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001701 return intermediate.growAggregate(aggregateDeclaration, intermNode, initLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001702 }
1703 else
1704 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001705 return aggregateDeclaration;
Jamie Madill502d66f2013-06-20 11:55:52 -04001706 }
1707 }
1708 else
1709 {
1710 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001711 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001712 }
1713}
1714
Jamie Madill06145232015-05-13 13:10:01 -04001715TIntermAggregate *TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001716 TIntermAggregate *aggregateDeclaration,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301717 const TSourceLoc &identifierLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001718 const TString &identifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301719 const TSourceLoc &indexLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001720 TIntermTyped *indexExpression,
1721 const TSourceLoc &initLocation, TIntermTyped *initializer)
1722{
1723 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1724 if (mDeferredSingleDeclarationErrorCheck)
1725 {
1726 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1727 recover();
1728 mDeferredSingleDeclarationErrorCheck = false;
1729 }
1730
1731 if (locationDeclaratorListCheck(identifierLocation, publicType))
1732 recover();
1733
1734 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1735 {
1736 recover();
1737 }
1738
1739 TPublicType arrayType(publicType);
1740
Olli Etuaho376f1b52015-04-13 13:23:41 +03001741 int size = 0;
1742 // If indexExpression is nullptr, then the array will eventually get its size implicitly from the initializer.
1743 if (indexExpression != nullptr && arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001744 {
1745 recover();
1746 }
1747 // Make the type an array even if size check failed.
1748 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1749 arrayType.setArraySize(size);
1750
1751 // initNode will correspond to the whole of "b[n] = initializer".
1752 TIntermNode *initNode = nullptr;
1753 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1754 {
1755 if (initNode)
1756 {
1757 return intermediate.growAggregate(aggregateDeclaration, initNode, initLocation);
1758 }
1759 else
1760 {
1761 return aggregateDeclaration;
1762 }
1763 }
1764 else
1765 {
1766 recover();
1767 return nullptr;
1768 }
1769}
1770
Jamie Madilla295edf2013-06-06 11:56:48 -04001771void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1772{
1773 if (typeQualifier.qualifier != EvqUniform)
1774 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301775 error(typeQualifier.line,
1776 "invalid qualifier:",
1777 getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
Jamie Madilla295edf2013-06-06 11:56:48 -04001778 recover();
1779 return;
1780 }
1781
1782 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1783 ASSERT(!layoutQualifier.isEmpty());
1784
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001785 if (mShaderVersion < 300)
Jamie Madilla295edf2013-06-06 11:56:48 -04001786 {
1787 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1788 recover();
1789 return;
1790 }
1791
1792 if (layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1793 {
1794 recover();
1795 return;
1796 }
1797
Jamie Madill099c0f32013-06-20 11:55:52 -04001798 if (layoutQualifier.matrixPacking != EmpUnspecified)
1799 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001800 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04001801 }
1802
Geoff Langc6856732014-02-11 09:38:55 -05001803 if (layoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04001804 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001805 mDefaultBlockStorage = layoutQualifier.blockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04001806 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001807}
1808
Jamie Madill06145232015-05-13 13:10:01 -04001809TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001810{
Jamie Madill06145232015-05-13 13:10:01 -04001811 TPublicType publicType = publicTypeIn;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001812 TOperator op = EOpNull;
1813 if (publicType.userDef)
1814 {
1815 op = EOpConstructStruct;
1816 }
1817 else
1818 {
1819 switch (publicType.type)
1820 {
1821 case EbtFloat:
1822 if (publicType.isMatrix())
1823 {
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001824 switch(publicType.getCols())
1825 {
Alexis Hetu07e57df2015-06-16 16:55:52 -04001826 case 2:
1827 switch(publicType.getRows())
1828 {
1829 case 2: op = EOpConstructMat2; break;
1830 case 3: op = EOpConstructMat2x3; break;
1831 case 4: op = EOpConstructMat2x4; break;
1832 }
1833 break;
1834 case 3:
1835 switch(publicType.getRows())
1836 {
1837 case 2: op = EOpConstructMat3x2; break;
1838 case 3: op = EOpConstructMat3; break;
1839 case 4: op = EOpConstructMat3x4; break;
1840 }
1841 break;
1842 case 4:
1843 switch(publicType.getRows())
1844 {
1845 case 2: op = EOpConstructMat4x2; break;
1846 case 3: op = EOpConstructMat4x3; break;
1847 case 4: op = EOpConstructMat4; break;
1848 }
1849 break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001850 }
1851 }
1852 else
1853 {
1854 switch(publicType.getNominalSize())
1855 {
Jamie Madill28b97422013-07-08 14:01:38 -04001856 case 1: op = EOpConstructFloat; break;
1857 case 2: op = EOpConstructVec2; break;
1858 case 3: op = EOpConstructVec3; break;
1859 case 4: op = EOpConstructVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001860 }
1861 }
1862 break;
1863
1864 case EbtInt:
1865 switch(publicType.getNominalSize())
1866 {
Jamie Madill28b97422013-07-08 14:01:38 -04001867 case 1: op = EOpConstructInt; break;
1868 case 2: op = EOpConstructIVec2; break;
1869 case 3: op = EOpConstructIVec3; break;
1870 case 4: op = EOpConstructIVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001871 }
1872 break;
1873
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001874 case EbtUInt:
1875 switch(publicType.getNominalSize())
1876 {
Jamie Madill28b97422013-07-08 14:01:38 -04001877 case 1: op = EOpConstructUInt; break;
1878 case 2: op = EOpConstructUVec2; break;
1879 case 3: op = EOpConstructUVec3; break;
1880 case 4: op = EOpConstructUVec4; break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001881 }
1882 break;
1883
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001884 case EbtBool:
1885 switch(publicType.getNominalSize())
1886 {
Jamie Madill28b97422013-07-08 14:01:38 -04001887 case 1: op = EOpConstructBool; break;
1888 case 2: op = EOpConstructBVec2; break;
1889 case 3: op = EOpConstructBVec3; break;
1890 case 4: op = EOpConstructBVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001891 }
1892 break;
1893
1894 default: break;
1895 }
1896
1897 if (op == EOpNull)
1898 {
1899 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
1900 recover();
1901 publicType.type = EbtFloat;
1902 op = EOpConstructFloat;
1903 }
1904 }
1905
1906 TString tempString;
Dmitry Skiba7f17a502015-06-22 15:08:39 -07001907 const TType *type = new TType(publicType);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001908 return new TFunction(&tempString, type, op);
1909}
1910
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001911// This function is used to test for the correctness of the parameters passed to various constructor functions
Arun Patole7e7e68d2015-05-22 12:02:25 +05301912// and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001913//
1914// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1915//
Arun Patole7e7e68d2015-05-22 12:02:25 +05301916TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments, TType *type, TOperator op, TFunction *fnCall,
1917 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001918{
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001919 TIntermAggregate *aggregateArguments = arguments->getAsAggregate();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001920
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001921 if (!aggregateArguments)
1922 {
1923 aggregateArguments = new TIntermAggregate;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001924 aggregateArguments->getSequence()->push_back(arguments);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001925 }
1926
Olli Etuahof40319e2015-03-10 14:33:00 +02001927 if (type->isArray())
1928 {
1929 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of the array.
1930 TIntermSequence *args = aggregateArguments->getSequence();
1931 for (size_t i = 0; i < args->size(); i++)
1932 {
1933 const TType &argType = (*args)[i]->getAsTyped()->getType();
1934 // It has already been checked that the argument is not an array.
1935 ASSERT(!argType.isArray());
1936 if (!argType.sameElementType(*type))
1937 {
1938 error(line, "Array constructor argument has an incorrect type", "Error");
1939 recover();
1940 return nullptr;
1941 }
1942 }
1943 }
1944 else if (op == EOpConstructStruct)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001945 {
1946 const TFieldList &fields = type->getStruct()->fields();
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001947 TIntermSequence *args = aggregateArguments->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001948
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001949 for (size_t i = 0; i < fields.size(); i++)
1950 {
Nicolas Capensffd73872014-08-21 13:49:16 -04001951 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001952 {
1953 error(line, "Structure constructor arguments do not match structure fields", "Error");
1954 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001955
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001956 return 0;
1957 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001958 }
1959 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001960
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001961 // Turn the argument list itself into a constructor
Olli Etuaho21203702014-11-13 16:16:21 +02001962 TIntermAggregate *constructor = intermediate.setAggregateOperator(aggregateArguments, op, line);
1963 TIntermTyped *constConstructor = foldConstConstructor(constructor, *type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001964 if (constConstructor)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001965 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001966 return constConstructor;
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001967 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001968
Olli Etuaho21203702014-11-13 16:16:21 +02001969 // Structs should not be precision qualified, the individual members may be.
1970 // Built-in types on the other hand should be precision qualified.
1971 if (op != EOpConstructStruct)
1972 {
1973 constructor->setPrecisionFromChildren();
1974 type->setPrecision(constructor->getPrecision());
1975 }
1976
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001977 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001978}
1979
Arun Patole7e7e68d2015-05-22 12:02:25 +05301980TIntermTyped *TParseContext::foldConstConstructor(TIntermAggregate *aggrNode, const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001981{
Olli Etuahof40319e2015-03-10 14:33:00 +02001982 // TODO: Add support for folding array constructors
1983 bool canBeFolded = areAllChildConst(aggrNode) && !type.isArray();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001984 aggrNode->setType(type);
Arun Patole7e7e68d2015-05-22 12:02:25 +05301985 if (canBeFolded)
1986 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001987 bool returnVal = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301988 TConstantUnion *unionArray = new TConstantUnion[type.getObjectSize()];
1989 if (aggrNode->getSequence()->size() == 1)
1990 {
1991 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type,
1992 true);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001993 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301994 else
1995 {
1996 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(),
1997 type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001998 }
1999 if (returnVal)
2000 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002001
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002002 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
2003 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002004
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002005 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002006}
2007
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002008//
2009// This function returns the tree representation for the vector field(s) being accessed from contant vector.
2010// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
2011// returned, else an aggregate node is returned (for v.xy). The input to this function could either be the symbol
Arun Patole7e7e68d2015-05-22 12:02:25 +05302012// node or it could be the intermediate tree representation of accessing fields in a constant structure or column of
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002013// a constant matrix.
2014//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302015TIntermTyped *TParseContext::addConstVectorNode(TVectorFields &fields, TIntermTyped *node, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002016{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302017 TIntermTyped *typedNode;
2018 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002019
Jamie Madillb11e2482015-05-04 14:21:22 -04002020 const TConstantUnion *unionArray;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302021 if (tempConstantNode)
2022 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002023 unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002024
Arun Patole7e7e68d2015-05-22 12:02:25 +05302025 if (!unionArray)
2026 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002027 return node;
2028 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302029 }
2030 else
2031 { // The node has to be either a symbol node or an aggregate node or a tempConstant node, else, its an error
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002032 error(line, "Cannot offset into the vector", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002033 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002034
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002035 return 0;
2036 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002037
Arun Patole7e7e68d2015-05-22 12:02:25 +05302038 TConstantUnion *constArray = new TConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002039
Arun Patole7e7e68d2015-05-22 12:02:25 +05302040 for (int i = 0; i < fields.num; i++)
2041 {
2042 if (fields.offsets[i] >= node->getType().getNominalSize())
2043 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002044 std::stringstream extraInfoStream;
2045 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
2046 std::string extraInfo = extraInfoStream.str();
2047 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002048 recover();
2049 fields.offsets[i] = 0;
2050 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302051
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002052 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002053
Arun Patole7e7e68d2015-05-22 12:02:25 +05302054 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002055 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
2056 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002057}
2058
2059//
2060// This function returns the column being accessed from a constant matrix. The values are retrieved from
Arun Patole7e7e68d2015-05-22 12:02:25 +05302061// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
2062// to the function could either be a symbol node (m[0] where m is a constant matrix)that represents a
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002063// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
2064//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302065TIntermTyped *TParseContext::addConstMatrixNode(int index, TIntermTyped *node, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002066{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302067 TIntermTyped *typedNode;
2068 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002069
Arun Patole7e7e68d2015-05-22 12:02:25 +05302070 if (index >= node->getType().getCols())
2071 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002072 std::stringstream extraInfoStream;
2073 extraInfoStream << "matrix field selection out of range '" << index << "'";
2074 std::string extraInfo = extraInfoStream.str();
2075 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002076 recover();
2077 index = 0;
2078 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002079
Arun Patole7e7e68d2015-05-22 12:02:25 +05302080 if (tempConstantNode)
2081 {
Jamie Madillb11e2482015-05-04 14:21:22 -04002082 TConstantUnion *unionArray = tempConstantNode->getUnionArrayPointer();
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002083 int size = tempConstantNode->getType().getCols();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002084 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302085 }
2086 else
2087 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002088 error(line, "Cannot offset into the matrix", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002089 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002090
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002091 return 0;
2092 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002093
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002094 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002095}
2096
2097
2098//
2099// This function returns an element of an array accessed from a constant array. The values are retrieved from
Arun Patole7e7e68d2015-05-22 12:02:25 +05302100// the symbol table and parse-tree is built for the type of the element. The input
2101// to the function could either be a symbol node (a[0] where a is a constant array)that represents a
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002102// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
2103//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302104TIntermTyped *TParseContext::addConstArrayNode(int index, TIntermTyped *node, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002105{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302106 TIntermTyped *typedNode;
2107 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002108 TType arrayElementType = node->getType();
2109 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002110
Arun Patole7e7e68d2015-05-22 12:02:25 +05302111 if (index >= node->getType().getArraySize())
2112 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002113 std::stringstream extraInfoStream;
2114 extraInfoStream << "array field selection out of range '" << index << "'";
2115 std::string extraInfo = extraInfoStream.str();
2116 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002117 recover();
2118 index = 0;
2119 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002120
Arun Patole7e7e68d2015-05-22 12:02:25 +05302121 if (tempConstantNode)
2122 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002123 size_t arrayElementSize = arrayElementType.getObjectSize();
Arun Patole7e7e68d2015-05-22 12:02:25 +05302124 TConstantUnion *unionArray = tempConstantNode->getUnionArrayPointer();
2125 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(),
2126 line);
2127 }
2128 else
2129 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002130 error(line, "Cannot offset into the array", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002131 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002132
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002133 return 0;
2134 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002135
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002136 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002137}
2138
2139
2140//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302141// This function returns the value of a particular field inside a constant structure from the symbol table.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002142// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
2143// function and returns the parse-tree with the values of the embedded/nested struct.
2144//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302145TIntermTyped *TParseContext::addConstStruct(const TString &identifier, TIntermTyped *node, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002146{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302147 const TFieldList &fields = node->getType().getStruct()->fields();
Jamie Madill94bf7f22013-07-08 13:31:15 -04002148 size_t instanceSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002149
Arun Patole7e7e68d2015-05-22 12:02:25 +05302150 for (size_t index = 0; index < fields.size(); ++index)
2151 {
2152 if (fields[index]->name() == identifier)
2153 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002154 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302155 }
2156 else
2157 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002158 instanceSize += fields[index]->type()->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002159 }
2160 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002161
Jamie Madill94bf7f22013-07-08 13:31:15 -04002162 TIntermTyped *typedNode;
2163 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
Arun Patole7e7e68d2015-05-22 12:02:25 +05302164 if (tempConstantNode)
2165 {
2166 TConstantUnion *constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002167
Arun Patole7e7e68d2015-05-22 12:02:25 +05302168 // type will be changed in the calling function
2169 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line);
2170 }
2171 else
2172 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002173 error(line, "Cannot offset into the structure", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002174 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002175
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002176 return 0;
2177 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002178
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002179 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002180}
2181
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002182//
2183// Interface/uniform blocks
2184//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302185TIntermAggregate *TParseContext::addInterfaceBlock(const TPublicType &typeQualifier, const TSourceLoc &nameLine,
2186 const TString &blockName, TFieldList *fieldList,
2187 const TString *instanceName, const TSourceLoc &instanceLine,
2188 TIntermTyped *arrayIndex, const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002189{
2190 if (reservedErrorCheck(nameLine, blockName))
2191 recover();
2192
2193 if (typeQualifier.qualifier != EvqUniform)
2194 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302195 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
2196 "interface blocks must be uniform");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002197 recover();
2198 }
2199
Jamie Madill099c0f32013-06-20 11:55:52 -04002200 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
2201 if (layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002202 {
2203 recover();
2204 }
2205
Jamie Madill099c0f32013-06-20 11:55:52 -04002206 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2207 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002208 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002209 }
2210
Jamie Madill1566ef72013-06-20 11:55:54 -04002211 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2212 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002213 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002214 }
2215
Arun Patole7e7e68d2015-05-22 12:02:25 +05302216 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2217 if (!symbolTable.declare(blockNameSymbol))
2218 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002219 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
2220 recover();
2221 }
2222
Jamie Madill98493dd2013-07-08 14:39:03 -04002223 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302224 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2225 {
2226 TField *field = (*fieldList)[memberIndex];
2227 TType *fieldType = field->type();
2228 if (IsSampler(fieldType->getBasicType()))
2229 {
2230 error(field->line(), "unsupported type", fieldType->getBasicString(),
2231 "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002232 recover();
2233 }
2234
Jamie Madill98493dd2013-07-08 14:39:03 -04002235 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002236 switch (qualifier)
2237 {
2238 case EvqGlobal:
2239 case EvqUniform:
2240 break;
2241 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04002242 error(field->line(), "invalid qualifier on interface block member", getQualifierString(qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002243 recover();
2244 break;
2245 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002246
2247 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002248 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
2249 if (layoutLocationErrorCheck(field->line(), fieldLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002250 {
2251 recover();
2252 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002253
Jamie Madill98493dd2013-07-08 14:39:03 -04002254 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002255 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302256 error(field->line(), "invalid layout qualifier:", getBlockStorageString(fieldLayoutQualifier.blockStorage),
2257 "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04002258 recover();
2259 }
2260
Jamie Madill98493dd2013-07-08 14:39:03 -04002261 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002262 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002263 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002264 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002265 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002266 {
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002267 warning(field->line(), "extraneous layout qualifier:",
2268 getMatrixPackingString(fieldLayoutQualifier.matrixPacking), "only has an effect on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04002269 }
2270
Jamie Madill98493dd2013-07-08 14:39:03 -04002271 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002272 }
2273
Jamie Madill98493dd2013-07-08 14:39:03 -04002274 // add array index
2275 int arraySize = 0;
2276 if (arrayIndex != NULL)
2277 {
2278 if (arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize))
2279 recover();
2280 }
2281
Arun Patole7e7e68d2015-05-22 12:02:25 +05302282 TInterfaceBlock *interfaceBlock = new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize,
2283 blockLayoutQualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002284 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier, arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002285
2286 TString symbolName = "";
2287 int symbolId = 0;
2288
Jamie Madill98493dd2013-07-08 14:39:03 -04002289 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002290 {
2291 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002292 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2293 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302294 TField *field = (*fieldList)[memberIndex];
2295 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002296
2297 // set parent pointer of the field variable
2298 fieldType->setInterfaceBlock(interfaceBlock);
2299
Arun Patole7e7e68d2015-05-22 12:02:25 +05302300 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002301 fieldVariable->setQualifier(typeQualifier.qualifier);
2302
Arun Patole7e7e68d2015-05-22 12:02:25 +05302303 if (!symbolTable.declare(fieldVariable))
2304 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002305 error(field->line(), "redefinition", field->name().c_str(), "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002306 recover();
2307 }
2308 }
2309 }
2310 else
2311 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002312 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302313 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002314 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002315
Arun Patole7e7e68d2015-05-22 12:02:25 +05302316 if (!symbolTable.declare(instanceTypeDef))
2317 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002318 error(instanceLine, "redefinition", instanceName->c_str(), "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002319 recover();
2320 }
2321
2322 symbolId = instanceTypeDef->getUniqueId();
2323 symbolName = instanceTypeDef->getName();
2324 }
2325
Arun Patole7e7e68d2015-05-22 12:02:25 +05302326 TIntermAggregate *aggregate = intermediate.makeAggregate(intermediate.addSymbol(symbolId, symbolName,
2327 interfaceBlockType,
2328 typeQualifier.line),
2329 nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002330 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04002331
2332 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002333 return aggregate;
2334}
2335
Arun Patole7e7e68d2015-05-22 12:02:25 +05302336bool TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002337{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002338 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002339
2340 // Embedded structure definitions are not supported per GLSL ES spec.
2341 // They aren't allowed in GLSL either, but we need to detect this here
2342 // so we don't rely on the GLSL compiler to catch it.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302343 if (mStructNestingLevel > 1)
2344 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002345 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002346 return true;
2347 }
2348
2349 return false;
2350}
2351
2352void TParseContext::exitStructDeclaration()
2353{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002354 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002355}
2356
2357namespace {
2358
2359const int kWebGLMaxStructNesting = 4;
2360
2361} // namespace
2362
Arun Patole7e7e68d2015-05-22 12:02:25 +05302363bool TParseContext::structNestingErrorCheck(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002364{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302365 if (!IsWebGLBasedSpec(mShaderSpec))
2366 {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002367 return false;
2368 }
2369
Arun Patole7e7e68d2015-05-22 12:02:25 +05302370 if (field.type()->getBasicType() != EbtStruct)
2371 {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002372 return false;
2373 }
2374
2375 // We're already inside a structure definition at this point, so add
2376 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302377 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2378 {
Jamie Madill41a49272014-03-18 16:10:13 -04002379 std::stringstream reasonStream;
2380 reasonStream << "Reference of struct type "
2381 << field.type()->getStruct()->name().c_str()
2382 << " exceeds maximum allowed nesting level of "
2383 << kWebGLMaxStructNesting;
2384 std::string reason = reasonStream.str();
2385 error(line, reason.c_str(), field.name().c_str(), "");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002386 return true;
2387 }
2388
2389 return false;
2390}
2391
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002392//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002393// Parse an array index expression
2394//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302395TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression, const TSourceLoc &location,
2396 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002397{
2398 TIntermTyped *indexedExpression = NULL;
2399
2400 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2401 {
2402 if (baseExpression->getAsSymbolNode())
2403 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302404 error(location, " left of '[' is not of type array, matrix, or vector ",
2405 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002406 }
2407 else
2408 {
2409 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2410 }
2411 recover();
2412 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002413
Jamie Madill21c1e452014-12-29 11:33:41 -05002414 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2415
2416 if (indexExpression->getQualifier() == EvqConst && indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002417 {
Jamie Madill21c1e452014-12-29 11:33:41 -05002418 int index = indexConstantUnion->getIConst(0);
Jamie Madill7164cf42013-07-08 13:30:59 -04002419 if (index < 0)
2420 {
2421 std::stringstream infoStream;
2422 infoStream << index;
2423 std::string info = infoStream.str();
2424 error(location, "negative index", info.c_str());
2425 recover();
2426 index = 0;
2427 }
2428 if (baseExpression->getType().getQualifier() == EvqConst)
2429 {
2430 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002431 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002432 // constant folding for arrays
2433 indexedExpression = addConstArrayNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002434 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002435 else if (baseExpression->isVector())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002436 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002437 // constant folding for vectors
2438 TVectorFields fields;
2439 fields.num = 1;
2440 fields.offsets[0] = index; // need to do it this way because v.xy sends fields integer array
2441 indexedExpression = addConstVectorNode(fields, baseExpression, location);
2442 }
2443 else if (baseExpression->isMatrix())
2444 {
2445 // constant folding for matrices
2446 indexedExpression = addConstMatrixNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002447 }
2448 }
2449 else
2450 {
Jamie Madillb11e2482015-05-04 14:21:22 -04002451 int safeIndex = -1;
2452
Jamie Madill7164cf42013-07-08 13:30:59 -04002453 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002454 {
Jamie Madill18464b52013-07-08 14:01:55 -04002455 if (index >= baseExpression->getType().getArraySize())
Jamie Madill7164cf42013-07-08 13:30:59 -04002456 {
2457 std::stringstream extraInfoStream;
2458 extraInfoStream << "array index out of range '" << index << "'";
2459 std::string extraInfo = extraInfoStream.str();
2460 error(location, "", "[", extraInfo.c_str());
2461 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002462 safeIndex = baseExpression->getType().getArraySize() - 1;
Jamie Madill7164cf42013-07-08 13:30:59 -04002463 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302464 else if (baseExpression->getQualifier() == EvqFragData && index > 0 &&
2465 !isExtensionEnabled("GL_EXT_draw_buffers"))
Jamie Madill5d287f52013-07-12 15:38:19 -04002466 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302467 error(location, "",
2468 "[", "array indexes for gl_FragData must be zero when GL_EXT_draw_buffers is disabled");
Jamie Madill5d287f52013-07-12 15:38:19 -04002469 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002470 safeIndex = 0;
Jamie Madill5d287f52013-07-12 15:38:19 -04002471 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002472 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302473 else if ((baseExpression->isVector() || baseExpression->isMatrix()) &&
2474 baseExpression->getType().getNominalSize() <= index)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002475 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002476 std::stringstream extraInfoStream;
2477 extraInfoStream << "field selection out of range '" << index << "'";
2478 std::string extraInfo = extraInfoStream.str();
2479 error(location, "", "[", extraInfo.c_str());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002480 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002481 safeIndex = baseExpression->getType().getNominalSize() - 1;
Jamie Madill46131a32013-06-20 11:55:50 -04002482 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002483
Jamie Madillb11e2482015-05-04 14:21:22 -04002484 // Don't modify the data of the previous constant union, because it can point
2485 // to builtins, like gl_MaxDrawBuffers. Instead use a new sanitized object.
2486 if (safeIndex != -1)
2487 {
2488 TConstantUnion *safeConstantUnion = new TConstantUnion();
2489 safeConstantUnion->setIConst(safeIndex);
2490 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
2491 }
2492
Jamie Madill7164cf42013-07-08 13:30:59 -04002493 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002494 }
2495 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002496 else
2497 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002498 if (baseExpression->isInterfaceBlock())
Jamie Madill7164cf42013-07-08 13:30:59 -04002499 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302500 error(location, "",
2501 "[", "array indexes for interface blocks arrays must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002502 recover();
2503 }
Jamie Madill19571812013-08-12 15:26:34 -07002504 else if (baseExpression->getQualifier() == EvqFragmentOut)
Jamie Madill7164cf42013-07-08 13:30:59 -04002505 {
Jamie Madill19571812013-08-12 15:26:34 -07002506 error(location, "", "[", "array indexes for fragment outputs must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002507 recover();
2508 }
2509
2510 indexedExpression = intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
2511 }
2512
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002513 if (indexedExpression == 0)
2514 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002515 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002516 unionArray->setFConst(0.0f);
2517 indexedExpression = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
2518 }
2519 else if (baseExpression->isArray())
2520 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002521 const TType &baseType = baseExpression->getType();
2522 if (baseType.getStruct())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002523 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002524 TType copyOfType(baseType.getStruct());
2525 indexedExpression->setType(copyOfType);
2526 }
2527 else if (baseType.isInterfaceBlock())
2528 {
2529 TType copyOfType(baseType.getInterfaceBlock(), baseType.getQualifier(), baseType.getLayoutQualifier(), 0);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002530 indexedExpression->setType(copyOfType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002531 }
2532 else
2533 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302534 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2535 EvqTemporary, static_cast<unsigned char>(baseExpression->getNominalSize()),
2536 static_cast<unsigned char>(baseExpression->getSecondarySize())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002537 }
2538
2539 if (baseExpression->getType().getQualifier() == EvqConst)
2540 {
2541 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2542 }
2543 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002544 else if (baseExpression->isMatrix())
2545 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002546 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302547 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2548 qualifier, static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002549 }
2550 else if (baseExpression->isVector())
2551 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002552 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2553 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002554 }
2555 else
2556 {
2557 indexedExpression->setType(baseExpression->getType());
2558 }
2559
2560 return indexedExpression;
2561}
2562
Arun Patole7e7e68d2015-05-22 12:02:25 +05302563TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression, const TSourceLoc &dotLocation,
2564 const TString &fieldString, const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002565{
2566 TIntermTyped *indexedExpression = NULL;
2567
2568 if (baseExpression->isArray())
2569 {
2570 error(fieldLocation, "cannot apply dot operator to an array", ".");
2571 recover();
2572 }
2573
2574 if (baseExpression->isVector())
2575 {
2576 TVectorFields fields;
2577 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields, fieldLocation))
2578 {
2579 fields.num = 1;
2580 fields.offsets[0] = 0;
2581 recover();
2582 }
2583
2584 if (baseExpression->getType().getQualifier() == EvqConst)
2585 {
2586 // constant folding for vector fields
2587 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
2588 if (indexedExpression == 0)
2589 {
2590 recover();
2591 indexedExpression = baseExpression;
2592 }
2593 else
2594 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302595 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2596 EvqConst, (unsigned char) (fieldString).size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002597 }
2598 }
2599 else
2600 {
2601 TString vectorString = fieldString;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302602 TIntermTyped *index = intermediate.addSwizzle(fields, fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002603 indexedExpression = intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302604 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2605 EvqTemporary, (unsigned char) vectorString.size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002606 }
2607 }
2608 else if (baseExpression->isMatrix())
2609 {
2610 TMatrixFields fields;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302611 if (!parseMatrixFields(fieldString, baseExpression->getCols(), baseExpression->getRows(), fields,
2612 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002613 {
2614 fields.wholeRow = false;
2615 fields.wholeCol = false;
2616 fields.row = 0;
2617 fields.col = 0;
2618 recover();
2619 }
2620
2621 if (fields.wholeRow || fields.wholeCol)
2622 {
2623 error(dotLocation, " non-scalar fields not implemented yet", ".");
2624 recover();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002625 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002626 unionArray->setIConst(0);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302627 TIntermTyped *index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst),
2628 fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002629 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302630 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2631 EvqTemporary, static_cast<unsigned char>(baseExpression->getCols()),
2632 static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002633 }
2634 else
2635 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002636 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002637 unionArray->setIConst(fields.col * baseExpression->getRows() + fields.row);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302638 TIntermTyped *index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst),
2639 fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002640 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
2641 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision()));
2642 }
2643 }
2644 else if (baseExpression->getBasicType() == EbtStruct)
2645 {
2646 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302647 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04002648 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002649 {
2650 error(dotLocation, "structure has no fields", "Internal Error");
2651 recover();
2652 indexedExpression = baseExpression;
2653 }
2654 else
2655 {
2656 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002657 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002658 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002659 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002660 {
2661 fieldFound = true;
2662 break;
2663 }
2664 }
2665 if (fieldFound)
2666 {
2667 if (baseExpression->getType().getQualifier() == EvqConst)
2668 {
2669 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2670 if (indexedExpression == 0)
2671 {
2672 recover();
2673 indexedExpression = baseExpression;
2674 }
2675 else
2676 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002677 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002678 // change the qualifier of the return type, not of the structure field
2679 // as the structure definition is shared between various structures.
2680 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2681 }
2682 }
2683 else
2684 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002685 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002686 unionArray->setIConst(i);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302687 TIntermTyped *index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002688 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002689 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002690 }
2691 }
2692 else
2693 {
2694 error(dotLocation, " no such field in structure", fieldString.c_str());
2695 recover();
2696 indexedExpression = baseExpression;
2697 }
2698 }
2699 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002700 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002701 {
2702 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302703 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04002704 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002705 {
2706 error(dotLocation, "interface block has no fields", "Internal Error");
2707 recover();
2708 indexedExpression = baseExpression;
2709 }
2710 else
2711 {
2712 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002713 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002714 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002715 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002716 {
2717 fieldFound = true;
2718 break;
2719 }
2720 }
2721 if (fieldFound)
2722 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002723 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002724 unionArray->setIConst(i);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302725 TIntermTyped *index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
2726 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index,
2727 dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002728 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002729 }
2730 else
2731 {
2732 error(dotLocation, " no such field in interface block", fieldString.c_str());
2733 recover();
2734 indexedExpression = baseExpression;
2735 }
2736 }
2737 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002738 else
2739 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002740 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002741 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302742 error(dotLocation, " field selection requires structure, vector, or matrix on left hand side",
2743 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002744 }
2745 else
2746 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302747 error(dotLocation,
2748 " field selection requires structure, vector, matrix, or interface block on left hand side",
2749 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002750 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002751 recover();
2752 indexedExpression = baseExpression;
2753 }
2754
2755 return indexedExpression;
2756}
2757
Arun Patole7e7e68d2015-05-22 12:02:25 +05302758TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002759{
Jamie Madilla5efff92013-06-06 11:56:47 -04002760 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002761
Jamie Madilla5efff92013-06-06 11:56:47 -04002762 qualifier.location = -1;
2763 qualifier.matrixPacking = EmpUnspecified;
2764 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002765
2766 if (qualifierType == "shared")
2767 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002768 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002769 }
2770 else if (qualifierType == "packed")
2771 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002772 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002773 }
2774 else if (qualifierType == "std140")
2775 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002776 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002777 }
2778 else if (qualifierType == "row_major")
2779 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002780 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002781 }
2782 else if (qualifierType == "column_major")
2783 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002784 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002785 }
2786 else if (qualifierType == "location")
2787 {
2788 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "location requires an argument");
2789 recover();
2790 }
2791 else
2792 {
2793 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
2794 recover();
2795 }
2796
Jamie Madilla5efff92013-06-06 11:56:47 -04002797 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002798}
2799
Arun Patole7e7e68d2015-05-22 12:02:25 +05302800TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc &qualifierTypeLine,
2801 const TString &intValueString, int intValue,
2802 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002803{
Jamie Madilla5efff92013-06-06 11:56:47 -04002804 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002805
Jamie Madilla5efff92013-06-06 11:56:47 -04002806 qualifier.location = -1;
2807 qualifier.matrixPacking = EmpUnspecified;
2808 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002809
2810 if (qualifierType != "location")
2811 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302812 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
2813 "only location may have arguments");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002814 recover();
2815 }
2816 else
2817 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002818 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002819 if (intValue < 0)
2820 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002821 error(intValueLine, "out of range:", intValueString.c_str(), "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002822 recover();
2823 }
2824 else
2825 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002826 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002827 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002828 }
2829
Jamie Madilla5efff92013-06-06 11:56:47 -04002830 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002831}
2832
Jamie Madilla5efff92013-06-06 11:56:47 -04002833TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002834{
Jamie Madilla5efff92013-06-06 11:56:47 -04002835 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002836
Jamie Madilla5efff92013-06-06 11:56:47 -04002837 if (rightQualifier.location != -1)
2838 {
2839 joinedQualifier.location = rightQualifier.location;
2840 }
2841 if (rightQualifier.matrixPacking != EmpUnspecified)
2842 {
2843 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
2844 }
2845 if (rightQualifier.blockStorage != EbsUnspecified)
2846 {
2847 joinedQualifier.blockStorage = rightQualifier.blockStorage;
2848 }
2849
2850 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002851}
2852
Arun Patole7e7e68d2015-05-22 12:02:25 +05302853TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc,
2854 TQualifier interpolationQualifier,
2855 const TSourceLoc &storageLoc,
2856 TQualifier storageQualifier)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002857{
2858 TQualifier mergedQualifier = EvqSmoothIn;
2859
Arun Patole7e7e68d2015-05-22 12:02:25 +05302860 if (storageQualifier == EvqFragmentIn)
2861 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002862 if (interpolationQualifier == EvqSmooth)
2863 mergedQualifier = EvqSmoothIn;
2864 else if (interpolationQualifier == EvqFlat)
2865 mergedQualifier = EvqFlatIn;
2866 else UNREACHABLE();
2867 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302868 else if (storageQualifier == EvqCentroidIn)
2869 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002870 if (interpolationQualifier == EvqSmooth)
2871 mergedQualifier = EvqCentroidIn;
2872 else if (interpolationQualifier == EvqFlat)
2873 mergedQualifier = EvqFlatIn;
2874 else UNREACHABLE();
2875 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302876 else if (storageQualifier == EvqVertexOut)
2877 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002878 if (interpolationQualifier == EvqSmooth)
2879 mergedQualifier = EvqSmoothOut;
2880 else if (interpolationQualifier == EvqFlat)
2881 mergedQualifier = EvqFlatOut;
2882 else UNREACHABLE();
2883 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302884 else if (storageQualifier == EvqCentroidOut)
2885 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002886 if (interpolationQualifier == EvqSmooth)
2887 mergedQualifier = EvqCentroidOut;
2888 else if (interpolationQualifier == EvqFlat)
2889 mergedQualifier = EvqFlatOut;
2890 else UNREACHABLE();
2891 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302892 else
2893 {
2894 error(interpolationLoc, "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier",
2895 getInterpolationString(interpolationQualifier));
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002896 recover();
2897
2898 mergedQualifier = storageQualifier;
2899 }
2900
2901 TPublicType type;
2902 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
2903 return type;
2904}
2905
Arun Patole7e7e68d2015-05-22 12:02:25 +05302906TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier, TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002907{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002908 if (voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier.type))
2909 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002910 recover();
2911 }
2912
Arun Patole7e7e68d2015-05-22 12:02:25 +05302913 for (unsigned int i = 0; i < fieldList->size(); ++i)
2914 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002915 //
2916 // Careful not to replace already known aspects of type, like array-ness
2917 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05302918 TType *type = (*fieldList)[i]->type();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002919 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002920 type->setPrimarySize(typeSpecifier.primarySize);
2921 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002922 type->setPrecision(typeSpecifier.precision);
2923 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002924 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002925
2926 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05302927 if (type->isArray())
2928 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002929 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
2930 recover();
2931 }
2932 if (typeSpecifier.array)
2933 type->setArraySize(typeSpecifier.arraySize);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302934 if (typeSpecifier.userDef)
2935 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002936 type->setStruct(typeSpecifier.userDef->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002937 }
2938
Arun Patole7e7e68d2015-05-22 12:02:25 +05302939 if (structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i]))
2940 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002941 recover();
2942 }
2943 }
2944
Jamie Madill98493dd2013-07-08 14:39:03 -04002945 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002946}
2947
Arun Patole7e7e68d2015-05-22 12:02:25 +05302948TPublicType TParseContext::addStructure(const TSourceLoc &structLine, const TSourceLoc &nameLine,
2949 const TString *structName, TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002950{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302951 TStructure *structure = new TStructure(structName, fieldList);
2952 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002953
Jamie Madill9b820842015-02-12 10:40:10 -05002954 // Store a bool in the struct if we're at global scope, to allow us to
2955 // skip the local struct scoping workaround in HLSL.
Jamie Madillb960cc42015-02-12 15:33:20 +00002956 structure->setUniqueId(TSymbolTable::nextUniqueId());
Jamie Madill9b820842015-02-12 10:40:10 -05002957 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04002958
Jamie Madill98493dd2013-07-08 14:39:03 -04002959 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002960 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002961 if (reservedErrorCheck(nameLine, *structName))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002962 {
2963 recover();
2964 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302965 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
2966 if (!symbolTable.declare(userTypeDef))
2967 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002968 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002969 recover();
2970 }
2971 }
2972
2973 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04002974 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002975 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002976 const TField &field = *(*fieldList)[typeListIndex];
2977 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002978 switch (qualifier)
2979 {
2980 case EvqGlobal:
2981 case EvqTemporary:
2982 break;
2983 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04002984 error(field.line(), "invalid qualifier on struct member", getQualifierString(qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002985 recover();
2986 break;
2987 }
2988 }
2989
2990 TPublicType publicType;
2991 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04002992 publicType.userDef = structureType;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002993 exitStructDeclaration();
2994
2995 return publicType;
2996}
2997
Olli Etuahoa3a36662015-02-17 13:46:51 +02002998TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init, TIntermAggregate *statementList, const TSourceLoc &loc)
2999{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003000 TBasicType switchType = init->getBasicType();
3001 if ((switchType != EbtInt && switchType != EbtUInt) ||
3002 init->isMatrix() ||
3003 init->isArray() ||
3004 init->isVector())
3005 {
3006 error(init->getLine(), "init-expression in a switch statement must be a scalar integer", "switch");
3007 recover();
3008 return nullptr;
3009 }
3010
Olli Etuahoac5274d2015-02-20 10:19:08 +02003011 if (statementList)
3012 {
3013 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
3014 {
3015 recover();
3016 return nullptr;
3017 }
3018 }
3019
Olli Etuahoa3a36662015-02-17 13:46:51 +02003020 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3021 if (node == nullptr)
3022 {
3023 error(loc, "erroneous switch statement", "switch");
3024 recover();
3025 return nullptr;
3026 }
3027 return node;
3028}
3029
3030TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3031{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003032 if (mSwitchNestingLevel == 0)
3033 {
3034 error(loc, "case labels need to be inside switch statements", "case");
3035 recover();
3036 return nullptr;
3037 }
3038 if (condition == nullptr)
3039 {
3040 error(loc, "case label must have a condition", "case");
3041 recover();
3042 return nullptr;
3043 }
3044 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
3045 condition->isMatrix() ||
3046 condition->isArray() ||
3047 condition->isVector())
3048 {
3049 error(condition->getLine(), "case label must be a scalar integer", "case");
3050 recover();
3051 }
3052 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
3053 if (conditionConst == nullptr)
3054 {
3055 error(condition->getLine(), "case label must be constant", "case");
3056 recover();
3057 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003058 TIntermCase *node = intermediate.addCase(condition, loc);
3059 if (node == nullptr)
3060 {
3061 error(loc, "erroneous case statement", "case");
3062 recover();
3063 return nullptr;
3064 }
3065 return node;
3066}
3067
3068TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3069{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003070 if (mSwitchNestingLevel == 0)
3071 {
3072 error(loc, "default labels need to be inside switch statements", "default");
3073 recover();
3074 return nullptr;
3075 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003076 TIntermCase *node = intermediate.addCase(nullptr, loc);
3077 if (node == nullptr)
3078 {
3079 error(loc, "erroneous default statement", "default");
3080 recover();
3081 return nullptr;
3082 }
3083 return node;
3084}
3085
Olli Etuahof6c694b2015-03-26 14:50:53 +02003086TIntermTyped *TParseContext::createUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc,
3087 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003088{
3089 if (child == nullptr)
3090 {
3091 return nullptr;
3092 }
3093
3094 switch (op)
3095 {
3096 case EOpLogicalNot:
3097 if (child->getBasicType() != EbtBool ||
3098 child->isMatrix() ||
3099 child->isArray() ||
3100 child->isVector())
3101 {
3102 return nullptr;
3103 }
3104 break;
3105 case EOpBitwiseNot:
3106 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3107 child->isMatrix() ||
3108 child->isArray())
3109 {
3110 return nullptr;
3111 }
3112 break;
3113 case EOpPostIncrement:
3114 case EOpPreIncrement:
3115 case EOpPostDecrement:
3116 case EOpPreDecrement:
3117 case EOpNegative:
3118 case EOpPositive:
3119 if (child->getBasicType() == EbtStruct ||
Olli Etuahodca3e792015-03-26 13:24:04 +02003120 child->getBasicType() == EbtBool ||
Olli Etuaho69c11b52015-03-26 12:59:00 +02003121 child->isArray())
3122 {
3123 return nullptr;
3124 }
Olli Etuahodca3e792015-03-26 13:24:04 +02003125 // Operators for built-ins are already type checked against their prototype.
Olli Etuaho69c11b52015-03-26 12:59:00 +02003126 default:
3127 break;
3128 }
3129
Olli Etuahof6c694b2015-03-26 14:50:53 +02003130 return intermediate.addUnaryMath(op, child, loc, funcReturnType);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003131}
3132
Olli Etuaho09b22472015-02-11 11:47:26 +02003133TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3134{
Olli Etuahof6c694b2015-03-26 14:50:53 +02003135 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003136 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003137 {
3138 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
3139 recover();
3140 return child;
3141 }
3142 return node;
3143}
3144
3145TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3146{
3147 if (lValueErrorCheck(loc, GetOperatorString(op), child))
3148 recover();
3149 return addUnaryMath(op, child, loc);
3150}
3151
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003152bool TParseContext::binaryOpCommonCheck(TOperator op, TIntermTyped *left, TIntermTyped *right,
Olli Etuahod6b14282015-03-17 14:31:35 +02003153 const TSourceLoc &loc)
3154{
3155 if (left->isArray() || right->isArray())
3156 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003157 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003158 {
3159 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3160 return false;
3161 }
3162
3163 if (left->isArray() != right->isArray())
3164 {
3165 error(loc, "array / non-array mismatch", GetOperatorString(op));
3166 return false;
3167 }
3168
3169 switch (op)
3170 {
3171 case EOpEqual:
3172 case EOpNotEqual:
3173 case EOpAssign:
3174 case EOpInitialize:
3175 break;
3176 default:
3177 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3178 return false;
3179 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003180 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003181 if (left->getArraySize() != right->getArraySize())
3182 {
3183 error(loc, "array size mismatch", GetOperatorString(op));
3184 return false;
3185 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003186 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003187
3188 // Check ops which require integer / ivec parameters
3189 bool isBitShift = false;
3190 switch (op)
3191 {
3192 case EOpBitShiftLeft:
3193 case EOpBitShiftRight:
3194 case EOpBitShiftLeftAssign:
3195 case EOpBitShiftRightAssign:
3196 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3197 // check that the basic type is an integer type.
3198 isBitShift = true;
3199 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3200 {
3201 return false;
3202 }
3203 break;
3204 case EOpBitwiseAnd:
3205 case EOpBitwiseXor:
3206 case EOpBitwiseOr:
3207 case EOpBitwiseAndAssign:
3208 case EOpBitwiseXorAssign:
3209 case EOpBitwiseOrAssign:
3210 // It is enough to check the type of only one operand, since later it
3211 // is checked that the operand types match.
3212 if (!IsInteger(left->getBasicType()))
3213 {
3214 return false;
3215 }
3216 break;
3217 default:
3218 break;
3219 }
3220
3221 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3222 // So the basic type should usually match.
3223 if (!isBitShift && left->getBasicType() != right->getBasicType())
3224 {
3225 return false;
3226 }
3227
Olli Etuaho9dd217b2015-03-20 14:24:31 +02003228 // Check that type sizes match exactly on ops that require that.
Olli Etuahoff699002015-03-23 14:38:42 +02003229 // Also check restrictions for structs that contain arrays or samplers.
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003230 switch(op)
3231 {
3232 case EOpAssign:
3233 case EOpInitialize:
3234 case EOpEqual:
3235 case EOpNotEqual:
Olli Etuaho9dd217b2015-03-20 14:24:31 +02003236 // ESSL 1.00 sections 5.7, 5.8, 5.9
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003237 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
Olli Etuaho9dd217b2015-03-20 14:24:31 +02003238 {
3239 error(loc, "undefined operation for structs containing arrays", GetOperatorString(op));
3240 return false;
3241 }
Olli Etuahoff699002015-03-23 14:38:42 +02003242 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3243 // we interpret the spec so that this extends to structs containing samplers,
3244 // similarly to ESSL 1.00 spec.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003245 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
Olli Etuahoff699002015-03-23 14:38:42 +02003246 left->getType().isStructureContainingSamplers())
3247 {
3248 error(loc, "undefined operation for structs containing samplers", GetOperatorString(op));
3249 return false;
3250 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003251 case EOpLessThan:
3252 case EOpGreaterThan:
3253 case EOpLessThanEqual:
3254 case EOpGreaterThanEqual:
3255 if ((left->getNominalSize() != right->getNominalSize()) ||
3256 (left->getSecondarySize() != right->getSecondarySize()))
3257 {
3258 return false;
3259 }
3260 default:
3261 break;
3262 }
3263
Olli Etuahod6b14282015-03-17 14:31:35 +02003264 return true;
3265}
3266
Olli Etuahofc1806e2015-03-17 13:03:11 +02003267TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op, TIntermTyped *left, TIntermTyped *right,
3268 const TSourceLoc &loc)
3269{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003270 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003271 return nullptr;
3272
Olli Etuahofc1806e2015-03-17 13:03:11 +02003273 switch (op)
3274 {
3275 case EOpEqual:
3276 case EOpNotEqual:
Olli Etuahofc1806e2015-03-17 13:03:11 +02003277 break;
3278 case EOpLessThan:
3279 case EOpGreaterThan:
3280 case EOpLessThanEqual:
3281 case EOpGreaterThanEqual:
Olli Etuahod6b14282015-03-17 14:31:35 +02003282 ASSERT(!left->isArray() && !right->isArray());
3283 if (left->isMatrix() || left->isVector() ||
Olli Etuahofc1806e2015-03-17 13:03:11 +02003284 left->getBasicType() == EbtStruct)
3285 {
3286 return nullptr;
3287 }
3288 break;
3289 case EOpLogicalOr:
3290 case EOpLogicalXor:
3291 case EOpLogicalAnd:
Olli Etuahod6b14282015-03-17 14:31:35 +02003292 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003293 if (left->getBasicType() != EbtBool ||
Olli Etuahod6b14282015-03-17 14:31:35 +02003294 left->isMatrix() || left->isVector())
Olli Etuahofc1806e2015-03-17 13:03:11 +02003295 {
3296 return nullptr;
3297 }
3298 break;
3299 case EOpAdd:
3300 case EOpSub:
3301 case EOpDiv:
3302 case EOpMul:
Olli Etuahod6b14282015-03-17 14:31:35 +02003303 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003304 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool)
3305 {
3306 return nullptr;
3307 }
3308 break;
3309 case EOpIMod:
Olli Etuahod6b14282015-03-17 14:31:35 +02003310 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003311 // Note that this is only for the % operator, not for mod()
3312 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
3313 {
3314 return nullptr;
3315 }
3316 break;
3317 // Note that for bitwise ops, type checking is done in promote() to
3318 // share code between ops and compound assignment
3319 default:
3320 break;
3321 }
3322
Olli Etuahofc1806e2015-03-17 13:03:11 +02003323 return intermediate.addBinaryMath(op, left, right, loc);
3324}
3325
Olli Etuaho09b22472015-02-11 11:47:26 +02003326TIntermTyped *TParseContext::addBinaryMath(TOperator op, TIntermTyped *left, TIntermTyped *right,
3327 const TSourceLoc &loc)
3328{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003329 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003330 if (node == 0)
3331 {
3332 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(), right->getCompleteString());
3333 recover();
3334 return left;
3335 }
3336 return node;
3337}
3338
3339TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op, TIntermTyped *left, TIntermTyped *right,
3340 const TSourceLoc &loc)
3341{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003342 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003343 if (node == 0)
3344 {
3345 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(), right->getCompleteString());
3346 recover();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003347 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02003348 unionArray->setBConst(false);
3349 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), loc);
3350 }
3351 return node;
3352}
3353
Olli Etuahod6b14282015-03-17 14:31:35 +02003354TIntermTyped *TParseContext::createAssign(TOperator op, TIntermTyped *left, TIntermTyped *right,
3355 const TSourceLoc &loc)
3356{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003357 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003358 {
3359 return intermediate.addAssign(op, left, right, loc);
3360 }
3361 return nullptr;
3362}
3363
3364TIntermTyped *TParseContext::addAssign(TOperator op, TIntermTyped *left, TIntermTyped *right,
3365 const TSourceLoc &loc)
3366{
3367 TIntermTyped *node = createAssign(op, left, right, loc);
3368 if (node == nullptr)
3369 {
3370 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
3371 recover();
3372 return left;
3373 }
3374 return node;
3375}
3376
Olli Etuaho49300862015-02-20 14:54:49 +02003377TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3378{
3379 switch (op)
3380 {
3381 case EOpContinue:
3382 if (mLoopNestingLevel <= 0)
3383 {
3384 error(loc, "continue statement only allowed in loops", "");
3385 recover();
3386 }
3387 break;
3388 case EOpBreak:
Olli Etuaho53f076f2015-02-20 10:55:14 +02003389 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
Olli Etuaho49300862015-02-20 14:54:49 +02003390 {
Olli Etuaho53f076f2015-02-20 10:55:14 +02003391 error(loc, "break statement only allowed in loops and switch statements", "");
Olli Etuaho49300862015-02-20 14:54:49 +02003392 recover();
3393 }
3394 break;
3395 case EOpReturn:
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003396 if (mCurrentFunctionType->getBasicType() != EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02003397 {
3398 error(loc, "non-void function must return a value", "return");
3399 recover();
3400 }
3401 break;
3402 default:
3403 // No checks for discard
3404 break;
3405 }
3406 return intermediate.addBranch(op, loc);
3407}
3408
3409TIntermBranch *TParseContext::addBranch(TOperator op, TIntermTyped *returnValue, const TSourceLoc &loc)
3410{
3411 ASSERT(op == EOpReturn);
3412 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003413 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02003414 {
3415 error(loc, "void function cannot return a value", "return");
3416 recover();
3417 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003418 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02003419 {
3420 error(loc, "function return is not matching type:", "return");
3421 recover();
3422 }
3423 return intermediate.addBranch(op, returnValue, loc);
3424}
3425
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003426TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall, TIntermNode *paramNode, TIntermNode *thisNode,
3427 const TSourceLoc &loc, bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003428{
3429 *fatalError = false;
3430 TOperator op = fnCall->getBuiltInOp();
3431 TIntermTyped *callNode = nullptr;
3432
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003433 if (thisNode != nullptr)
3434 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003435 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho96e67382015-04-23 14:27:02 +03003436 int arraySize = 0;
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003437 TIntermTyped *typedThis = thisNode->getAsTyped();
3438 if (fnCall->getName() != "length")
3439 {
3440 error(loc, "invalid method", fnCall->getName().c_str());
3441 recover();
3442 }
3443 else if (paramNode != nullptr)
3444 {
3445 error(loc, "method takes no parameters", "length");
3446 recover();
3447 }
3448 else if (typedThis == nullptr || !typedThis->isArray())
3449 {
3450 error(loc, "length can only be called on arrays", "length");
3451 recover();
3452 }
3453 else
3454 {
Olli Etuaho96e67382015-04-23 14:27:02 +03003455 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03003456 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003457 {
Olli Etuaho39282e12015-04-23 15:41:48 +03003458 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003459 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03003460 // (func()).length()
3461 // (int[3](0, 1, 2)).length()
3462 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid expression.
3463 // It allows "An array name with the length method applied" in contrast to GLSL 4.4 spec section 5.9
3464 // which allows "An array, vector or matrix expression with the length method applied".
3465 error(loc, "length can only be called on array names, not on array expressions", "length");
3466 recover();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003467 }
3468 }
Olli Etuaho96e67382015-04-23 14:27:02 +03003469 unionArray->setIConst(arraySize);
3470 callNode = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003471 }
3472 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003473 {
3474 //
3475 // Then this should be a constructor.
3476 // Don't go through the symbol table for constructors.
3477 // Their parameters will be verified algorithmically.
3478 //
3479 TType type(EbtVoid, EbpUndefined); // use this to get the type back
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003480 if (!constructorErrorCheck(loc, paramNode, *fnCall, op, &type))
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003481 {
3482 //
3483 // It's a constructor, of type 'type'.
3484 //
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003485 callNode = addConstructor(paramNode, &type, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003486 }
Olli Etuaho72ba85b2015-03-04 14:23:26 +02003487
3488 if (callNode == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003489 {
3490 recover();
3491 callNode = intermediate.setAggregateOperator(nullptr, op, loc);
3492 }
3493 callNode->setType(type);
3494 }
3495 else
3496 {
3497 //
3498 // Not a constructor. Find it in the symbol table.
3499 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303500 const TFunction *fnCandidate;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003501 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003502 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003503 if (fnCandidate)
3504 {
3505 //
3506 // A declared function.
3507 //
3508 if (builtIn && !fnCandidate->getExtension().empty() &&
3509 extensionErrorCheck(loc, fnCandidate->getExtension()))
3510 {
3511 recover();
3512 }
3513 op = fnCandidate->getBuiltInOp();
3514 if (builtIn && op != EOpNull)
3515 {
3516 //
3517 // A function call mapped to a built-in operation.
3518 //
3519 if (fnCandidate->getParamCount() == 1)
3520 {
3521 //
3522 // Treat it like a built-in unary operator.
3523 //
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003524 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc, &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003525 if (callNode == nullptr)
3526 {
3527 std::stringstream extraInfoStream;
3528 extraInfoStream << "built in unary operator function. Type: "
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003529 << static_cast<TIntermTyped*>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003530 std::string extraInfo = extraInfoStream.str();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003531 error(paramNode->getLine(), " wrong operand type", "Internal Error", extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003532 *fatalError = true;
3533 return nullptr;
3534 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003535 }
3536 else
3537 {
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003538 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003539 aggregate->setType(fnCandidate->getReturnType());
3540 aggregate->setPrecisionFromChildren();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003541
3542 // Some built-in functions have out parameters too.
3543 functionCallLValueErrorCheck(fnCandidate, aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05303544
3545 // See if we can constant fold a built-in.
Olli Etuahob43846e2015-06-02 18:18:57 +03003546 TIntermTyped *foldedNode = intermediate.foldAggregateBuiltIn(aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05303547 if (foldedNode)
3548 {
Arun Patole274f0702015-05-05 13:33:30 +05303549 callNode = foldedNode;
3550 }
Olli Etuahob43846e2015-06-02 18:18:57 +03003551 else
3552 {
3553 callNode = aggregate;
3554 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003555 }
3556 }
3557 else
3558 {
3559 // This is a real function call
3560
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003561 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003562 aggregate->setType(fnCandidate->getReturnType());
3563
3564 // this is how we know whether the given function is a builtIn function or a user defined function
3565 // if builtIn == false, it's a userDefined -> could be an overloaded builtIn function also
3566 // if builtIn == true, it's definitely a builtIn function with EOpNull
3567 if (!builtIn)
3568 aggregate->setUserDefined();
3569 aggregate->setName(fnCandidate->getMangledName());
Corentin Wallez71d147f2015-02-11 11:15:24 -08003570 aggregate->setFunctionId(fnCandidate->getUniqueId());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003571
3572 // This needs to happen after the name is set
3573 if (builtIn)
3574 aggregate->setBuiltInFunctionPrecision();
3575
3576 callNode = aggregate;
3577
3578 functionCallLValueErrorCheck(fnCandidate, aggregate);
3579 }
3580 }
3581 else
3582 {
3583 // error message was put out by findFunction()
3584 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003585 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003586 unionArray->setFConst(0.0f);
3587 callNode = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpUndefined, EvqConst), loc);
3588 recover();
3589 }
3590 }
3591 delete fnCall;
3592 return callNode;
3593}
3594
Olli Etuaho52901742015-04-15 13:42:45 +03003595TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond, TIntermTyped *trueBlock, TIntermTyped *falseBlock,
3596 const TSourceLoc &loc)
3597{
3598 if (boolErrorCheck(loc, cond))
3599 recover();
3600
3601 if (trueBlock->getType() != falseBlock->getType())
3602 {
3603 binaryOpError(loc, ":", trueBlock->getCompleteString(), falseBlock->getCompleteString());
3604 recover();
3605 return falseBlock;
3606 }
Olli Etuahoa2d53032015-04-15 14:14:44 +03003607 // ESSL1 sections 5.2 and 5.7:
3608 // ESSL3 section 5.7:
3609 // Ternary operator is not among the operators allowed for structures/arrays.
3610 if (trueBlock->isArray() || trueBlock->getBasicType() == EbtStruct)
3611 {
3612 error(loc, "ternary operator is not allowed for structures or arrays", ":");
3613 recover();
3614 return falseBlock;
3615 }
Olli Etuaho52901742015-04-15 13:42:45 +03003616 return intermediate.addSelection(cond, trueBlock, falseBlock, loc);
3617}
Olli Etuaho49300862015-02-20 14:54:49 +02003618
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003619//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003620// Parse an array of strings using yyparse.
3621//
3622// Returns 0 for success.
3623//
Arun Patole7e7e68d2015-05-22 12:02:25 +05303624int PaParseStrings(size_t count, const char *const string[], const int length[],
3625 TParseContext *context)
3626{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003627 if ((count == 0) || (string == NULL))
3628 return 1;
3629
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003630 if (glslang_initialize(context))
3631 return 1;
3632
alokp@chromium.org408c45e2012-04-05 15:54:43 +00003633 int error = glslang_scan(count, string, length, context);
3634 if (!error)
3635 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003636
alokp@chromium.org73bc2982012-06-19 18:48:05 +00003637 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00003638
alokp@chromium.org6b495712012-06-29 00:06:58 +00003639 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003640}
3641
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003642
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00003643