blob: a3d56464728764a231678c6dd7e7d6e965e52c99 [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());
Jamie Madill1a4b1b32015-07-23 18:27:13 -0400933 *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +0300934 return false;
935 }
936
937 if (voidErrorCheck(line, identifier, type.getBasicType()))
938 return false;
939
940 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000941}
942
Arun Patole7e7e68d2015-05-22 12:02:25 +0530943bool TParseContext::paramErrorCheck(const TSourceLoc &line, TQualifier qualifier, TQualifier paramQualifier,
944 TType *type)
945{
946 if (qualifier != EvqConst && qualifier != EvqTemporary)
947 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000948 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000949 return true;
950 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530951 if (qualifier == EvqConst && paramQualifier != EvqIn)
952 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000953 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
954 return true;
955 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000956
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000957 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000958 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000959 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000960 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000961
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000962 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000963}
964
Arun Patole7e7e68d2015-05-22 12:02:25 +0530965bool TParseContext::extensionErrorCheck(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000966{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530967 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000968 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +0530969 if (iter == extBehavior.end())
970 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000971 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000972 return true;
973 }
zmo@google.comf5450912011-09-09 01:37:19 +0000974 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +0530975 if (iter->second == EBhDisable || iter->second == EBhUndefined)
976 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000977 error(line, "extension", extension.c_str(), "is disabled");
978 return true;
979 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530980 if (iter->second == EBhWarn)
981 {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000982 warning(line, "extension", extension.c_str(), "is being used");
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000983 return false;
984 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000985
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000986 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000987}
988
Olli Etuahofa33d582015-04-09 14:33:12 +0300989// These checks are common for all declarations starting a declarator list, and declarators that follow an empty
990// declaration.
991//
Jamie Madill06145232015-05-13 13:10:01 -0400992bool TParseContext::singleDeclarationErrorCheck(const TPublicType &publicType, const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -0400993{
Olli Etuahofa33d582015-04-09 14:33:12 +0300994 switch (publicType.qualifier)
995 {
996 case EvqVaryingIn:
997 case EvqVaryingOut:
998 case EvqAttribute:
999 case EvqVertexIn:
1000 case EvqFragmentOut:
1001 if (publicType.type == EbtStruct)
1002 {
1003 error(identifierLocation, "cannot be used with a structure",
1004 getQualifierString(publicType.qualifier));
1005 return true;
1006 }
1007
1008 default: break;
1009 }
1010
1011 if (publicType.qualifier != EvqUniform && samplerErrorCheck(identifierLocation, publicType,
1012 "samplers must be uniform"))
1013 {
Jamie Madilla5efff92013-06-06 11:56:47 -04001014 return true;
Olli Etuahofa33d582015-04-09 14:33:12 +03001015 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001016
1017 // check for layout qualifier issues
1018 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
1019
1020 if (layoutQualifier.matrixPacking != EmpUnspecified)
1021 {
Olli Etuahofa33d582015-04-09 14:33:12 +03001022 error(identifierLocation, "layout qualifier", getMatrixPackingString(layoutQualifier.matrixPacking),
1023 "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -04001024 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001025 }
1026
1027 if (layoutQualifier.blockStorage != EbsUnspecified)
1028 {
Olli Etuahofa33d582015-04-09 14:33:12 +03001029 error(identifierLocation, "layout qualifier", getBlockStorageString(layoutQualifier.blockStorage),
1030 "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -04001031 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001032 }
1033
Olli Etuahofa33d582015-04-09 14:33:12 +03001034 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut &&
1035 layoutLocationErrorCheck(identifierLocation, publicType.layoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001036 {
Jamie Madill51a53c72013-06-19 09:24:43 -04001037 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001038 }
1039
1040 return false;
1041}
1042
Arun Patole7e7e68d2015-05-22 12:02:25 +05301043bool TParseContext::layoutLocationErrorCheck(const TSourceLoc &location, const TLayoutQualifier &layoutQualifier)
Jamie Madilla5efff92013-06-06 11:56:47 -04001044{
1045 if (layoutQualifier.location != -1)
1046 {
1047 error(location, "invalid layout qualifier:", "location", "only valid on program inputs and outputs");
1048 return true;
1049 }
1050
1051 return false;
1052}
1053
Olli Etuahob6e07a62015-02-16 12:22:10 +02001054bool TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate, TIntermAggregate *aggregate)
1055{
1056 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1057 {
1058 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1059 if (qual == EvqOut || qual == EvqInOut)
1060 {
1061 TIntermTyped *node = (*(aggregate->getSequence()))[i]->getAsTyped();
1062 if (lValueErrorCheck(node->getLine(), "assign", node))
1063 {
1064 error(node->getLine(),
1065 "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error");
1066 recover();
1067 return true;
1068 }
1069 }
1070 }
1071 return false;
1072}
1073
Olli Etuaho37ad4742015-04-27 13:18:50 +03001074void TParseContext::es3InvariantErrorCheck(const TQualifier qualifier, const TSourceLoc &invariantLocation)
1075{
1076 if (!sh::IsVaryingOut(qualifier) && qualifier != EvqFragmentOut)
1077 {
1078 error(invariantLocation, "Only out variables can be invariant.", "invariant");
1079 recover();
1080 }
1081}
1082
Arun Patole7e7e68d2015-05-22 12:02:25 +05301083bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001084{
Arun Patole7e7e68d2015-05-22 12:02:25 +05301085 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001086 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1087 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001088}
1089
Arun Patole7e7e68d2015-05-22 12:02:25 +05301090bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001091{
Arun Patole7e7e68d2015-05-22 12:02:25 +05301092 const TExtensionBehavior &extbehavior = extensionBehavior();
Shannon Woodsa49a9bf2013-08-02 17:23:14 -04001093 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001094
1095 if (iter == extbehavior.end())
1096 {
1097 return false;
1098 }
1099
1100 return (iter->second == EBhEnable || iter->second == EBhRequire);
1101}
1102
Arun Patole7e7e68d2015-05-22 12:02:25 +05301103void TParseContext::handleExtensionDirective(const TSourceLoc &loc, const char *extName, const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001104{
1105 pp::SourceLocation srcLoc;
1106 srcLoc.file = loc.first_file;
1107 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001108 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001109}
1110
Arun Patole7e7e68d2015-05-22 12:02:25 +05301111void TParseContext::handlePragmaDirective(const TSourceLoc &loc, const char *name, const char *value, bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001112{
1113 pp::SourceLocation srcLoc;
1114 srcLoc.file = loc.first_file;
1115 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001116 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001117}
1118
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001119/////////////////////////////////////////////////////////////////////////////////
1120//
1121// Non-Errors.
1122//
1123/////////////////////////////////////////////////////////////////////////////////
1124
Jamie Madill5c097022014-08-20 16:38:32 -04001125const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1126 const TString *name,
1127 const TSymbol *symbol)
1128{
1129 const TVariable *variable = NULL;
1130
1131 if (!symbol)
1132 {
1133 error(location, "undeclared identifier", name->c_str());
1134 recover();
1135 }
1136 else if (!symbol->isVariable())
1137 {
1138 error(location, "variable expected", name->c_str());
1139 recover();
1140 }
1141 else
1142 {
1143 variable = static_cast<const TVariable*>(symbol);
1144
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001145 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
Jamie Madill5c097022014-08-20 16:38:32 -04001146 !variable->getExtension().empty() &&
1147 extensionErrorCheck(location, variable->getExtension()))
1148 {
1149 recover();
1150 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001151
1152 // Reject shaders using both gl_FragData and gl_FragColor
1153 TQualifier qualifier = variable->getType().getQualifier();
1154 if (qualifier == EvqFragData)
1155 {
1156 mUsesFragData = true;
1157 }
1158 else if (qualifier == EvqFragColor)
1159 {
1160 mUsesFragColor = true;
1161 }
1162
1163 // This validation is not quite correct - it's only an error to write to
1164 // both FragData and FragColor. For simplicity, and because users shouldn't
1165 // be rewarded for reading from undefined varaibles, return an error
1166 // if they are both referenced, rather than assigned.
1167 if (mUsesFragData && mUsesFragColor)
1168 {
1169 error(location, "cannot use both gl_FragData and gl_FragColor", name->c_str());
1170 recover();
1171 }
Jamie Madill5c097022014-08-20 16:38:32 -04001172 }
1173
1174 if (!variable)
1175 {
1176 TType type(EbtFloat, EbpUndefined);
1177 TVariable *fakeVariable = new TVariable(name, type);
1178 symbolTable.declare(fakeVariable);
1179 variable = fakeVariable;
1180 }
1181
1182 return variable;
1183}
1184
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001185//
1186// Look up a function name in the symbol table, and make sure it is a function.
1187//
1188// Return the function symbol if found, otherwise 0.
1189//
Arun Patole7e7e68d2015-05-22 12:02:25 +05301190const TFunction *TParseContext::findFunction(const TSourceLoc &line, TFunction *call, int inputShaderVersion,
1191 bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001192{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001193 // First find by unmangled name to check whether the function name has been
1194 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001195 // If a function is found, check for one with a matching argument list.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301196 const TSymbol *symbol = symbolTable.find(call->getName(), inputShaderVersion, builtIn);
1197 if (symbol == 0 || symbol->isFunction())
1198 {
Austin Kinross3ae64652015-01-26 15:51:39 -08001199 symbol = symbolTable.find(call->getMangledName(), inputShaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001200 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001201
Arun Patole7e7e68d2015-05-22 12:02:25 +05301202 if (symbol == 0)
1203 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001204 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001205 return 0;
1206 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001207
Arun Patole7e7e68d2015-05-22 12:02:25 +05301208 if (!symbol->isFunction())
1209 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001210 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001211 return 0;
1212 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001213
1214 return static_cast<const TFunction*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001215}
1216
1217//
1218// Initializers show up in several places in the grammar. Have one set of
1219// code to handle them here.
1220//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001221// Returns true on error, false if no error
1222//
Jamie Madill06145232015-05-13 13:10:01 -04001223bool TParseContext::executeInitializer(const TSourceLoc &line, const TString &identifier, const TPublicType &pType,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001224 TIntermTyped *initializer, TIntermNode **intermNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001225{
Olli Etuahoe7847b02015-03-16 11:56:12 +02001226 ASSERT(intermNode != nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001227 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001228
Olli Etuaho2935c582015-04-08 14:32:06 +03001229 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001230 if (type.isUnsizedArray())
1231 {
1232 type.setArraySize(initializer->getArraySize());
1233 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001234 if (!declareVariable(line, identifier, type, &variable))
1235 {
1236 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001237 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001238
Olli Etuahob0c645e2015-05-12 14:25:36 +03001239 bool globalInitWarning = false;
1240 if (symbolTable.atGlobalLevel() && !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
1241 {
1242 // Error message does not completely match behavior with ESSL 1.00, but
1243 // we want to steer developers towards only using constant expressions.
1244 error(line, "global variable initializers must be constant expressions", "=");
1245 return true;
1246 }
1247 if (globalInitWarning)
1248 {
1249 warning(line, "global variable initializers should be constant expressions "
1250 "(uniforms and globals are allowed in global initializers for legacy compatibility)", "=");
1251 }
1252
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001253 //
1254 // identifier must be of type constant, a global, or a temporary
1255 //
1256 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301257 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1258 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001259 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001260 return true;
1261 }
1262 //
1263 // test for and propagate constant
1264 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001265
Arun Patole7e7e68d2015-05-22 12:02:25 +05301266 if (qualifier == EvqConst)
1267 {
1268 if (qualifier != initializer->getType().getQualifier())
1269 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001270 std::stringstream extraInfoStream;
1271 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1272 std::string extraInfo = extraInfoStream.str();
1273 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001274 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001275 return true;
1276 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301277 if (type != initializer->getType())
1278 {
1279 error(line, " non-matching types for const initializer ",
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001280 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001281 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001282 return true;
1283 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301284 if (initializer->getAsConstantUnion())
1285 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001286 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Arun Patole7e7e68d2015-05-22 12:02:25 +05301287 }
1288 else if (initializer->getAsSymbolNode())
1289 {
1290 const TSymbol *symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1291 const TVariable *tVar = static_cast<const TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001292
Arun Patole7e7e68d2015-05-22 12:02:25 +05301293 TConstantUnion *constArray = tVar->getConstPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001294 variable->shareConstPointer(constArray);
Arun Patole7e7e68d2015-05-22 12:02:25 +05301295 }
1296 else
1297 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001298 std::stringstream extraInfoStream;
1299 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1300 std::string extraInfo = extraInfoStream.str();
1301 error(line, " cannot assign to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001302 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001303 return true;
1304 }
1305 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001306
1307 if (qualifier != EvqConst)
1308 {
1309 TIntermSymbol *intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(),
1310 variable->getType(), line);
1311 *intermNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1312 if (*intermNode == nullptr)
1313 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001314 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1315 return true;
1316 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001317 }
1318 else
1319 {
1320 *intermNode = nullptr;
1321 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001322
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001323 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001324}
1325
Arun Patole7e7e68d2015-05-22 12:02:25 +05301326bool TParseContext::areAllChildConst(TIntermAggregate *aggrNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001327{
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001328 ASSERT(aggrNode != NULL);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001329 if (!aggrNode->isConstructor())
1330 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001331
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001332 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001333
Arun Patole7e7e68d2015-05-22 12:02:25 +05301334 // check if all the child nodes are constants so that they can be inserted into
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001335 // the parent node
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001336 TIntermSequence *sequence = aggrNode->getSequence() ;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301337 for (TIntermSequence::iterator p = sequence->begin(); p != sequence->end(); ++p)
1338 {
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001339 if (!(*p)->getAsTyped()->getAsConstantUnion())
1340 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001341 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001342
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001343 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001344}
1345
Olli Etuaho214c2d82015-04-27 14:49:13 +03001346TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier, bool invariant, TLayoutQualifier layoutQualifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301347 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001348{
1349 TPublicType returnType = typeSpecifier;
1350 returnType.qualifier = qualifier;
Olli Etuaho214c2d82015-04-27 14:49:13 +03001351 returnType.invariant = invariant;
Jamie Madilla5efff92013-06-06 11:56:47 -04001352 returnType.layoutQualifier = layoutQualifier;
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001353
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001354 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001355 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001356 if (typeSpecifier.array)
1357 {
1358 error(typeSpecifier.line, "not supported", "first-class array");
1359 recover();
1360 returnType.clearArrayness();
1361 }
1362
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001363 if (qualifier == EvqAttribute && (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1364 {
1365 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1366 recover();
1367 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001368
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001369 if ((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1370 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1371 {
1372 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1373 recover();
1374 }
1375 }
1376 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001377 {
Olli Etuahoabb0c382015-07-13 12:01:12 +03001378 if (!layoutQualifier.isEmpty())
1379 {
1380 if (globalErrorCheck(typeSpecifier.line, symbolTable.atGlobalLevel(), "layout"))
1381 {
1382 recover();
1383 }
1384 }
Jamie Madillb120eac2013-06-12 14:08:13 -04001385 switch (qualifier)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001386 {
Jamie Madill19571812013-08-12 15:26:34 -07001387 case EvqSmoothIn:
1388 case EvqSmoothOut:
1389 case EvqVertexOut:
1390 case EvqFragmentIn:
1391 case EvqCentroidOut:
1392 case EvqCentroidIn:
1393 if (typeSpecifier.type == EbtBool)
1394 {
1395 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1396 recover();
1397 }
1398 if (typeSpecifier.type == EbtInt || typeSpecifier.type == EbtUInt)
1399 {
1400 error(typeSpecifier.line, "must use 'flat' interpolation here", getQualifierString(qualifier));
1401 recover();
1402 }
1403 break;
1404
1405 case EvqVertexIn:
1406 case EvqFragmentOut:
1407 case EvqFlatIn:
1408 case EvqFlatOut:
Jamie Madillb120eac2013-06-12 14:08:13 -04001409 if (typeSpecifier.type == EbtBool)
1410 {
1411 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1412 recover();
1413 }
1414 break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001415
Jamie Madillb120eac2013-06-12 14:08:13 -04001416 default: break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001417 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001418 }
1419
1420 return returnType;
1421}
1422
Olli Etuahofa33d582015-04-09 14:33:12 +03001423TIntermAggregate *TParseContext::parseSingleDeclaration(TPublicType &publicType,
1424 const TSourceLoc &identifierOrTypeLocation,
1425 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001426{
Olli Etuahofa33d582015-04-09 14:33:12 +03001427 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001428
Olli Etuahobab4c082015-04-24 16:38:49 +03001429 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03001430
Olli Etuahobab4c082015-04-24 16:38:49 +03001431 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1432
1433 if (emptyDeclaration)
1434 {
1435 if (publicType.isUnsizedArray())
1436 {
1437 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an error.
1438 // It is assumed that this applies to empty declarations as well.
1439 error(identifierOrTypeLocation, "empty array declaration needs to specify a size", identifier.c_str());
1440 }
1441 }
1442 else
Jamie Madill60ed9812013-06-06 11:56:46 -04001443 {
Olli Etuahofa33d582015-04-09 14:33:12 +03001444 if (singleDeclarationErrorCheck(publicType, identifierOrTypeLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001445 recover();
1446
Olli Etuaho376f1b52015-04-13 13:23:41 +03001447 if (nonInitErrorCheck(identifierOrTypeLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001448 recover();
1449
Olli Etuaho2935c582015-04-08 14:32:06 +03001450 TVariable *variable = nullptr;
Olli Etuahofa33d582015-04-09 14:33:12 +03001451 if (!declareVariable(identifierOrTypeLocation, identifier, TType(publicType), &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001452 recover();
1453
1454 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001455 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001456 }
1457
Olli Etuahoe7847b02015-03-16 11:56:12 +02001458 return intermediate.makeAggregate(symbol, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001459}
1460
Olli Etuahoe7847b02015-03-16 11:56:12 +02001461TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1462 const TSourceLoc &identifierLocation,
1463 const TString &identifier,
1464 const TSourceLoc &indexLocation,
1465 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001466{
Olli Etuahofa33d582015-04-09 14:33:12 +03001467 mDeferredSingleDeclarationErrorCheck = false;
1468
1469 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001470 recover();
1471
Olli Etuaho376f1b52015-04-13 13:23:41 +03001472 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001473 recover();
1474
1475 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1476 {
1477 recover();
1478 }
1479
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001480 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001481
1482 int size;
1483 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1484 {
1485 recover();
1486 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001487 // Make the type an array even if size check failed.
1488 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1489 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001490
Olli Etuaho2935c582015-04-08 14:32:06 +03001491 TVariable *variable = nullptr;
1492 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001493 recover();
1494
Olli Etuahoe7847b02015-03-16 11:56:12 +02001495 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001496 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001497 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001498
Olli Etuahoe7847b02015-03-16 11:56:12 +02001499 return intermediate.makeAggregate(symbol, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001500}
1501
Jamie Madill06145232015-05-13 13:10:01 -04001502TIntermAggregate *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001503 const TSourceLoc &identifierLocation,
1504 const TString &identifier,
1505 const TSourceLoc &initLocation,
1506 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001507{
Olli Etuahofa33d582015-04-09 14:33:12 +03001508 mDeferredSingleDeclarationErrorCheck = false;
1509
1510 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001511 recover();
1512
Olli Etuahoe7847b02015-03-16 11:56:12 +02001513 TIntermNode *intermNode = nullptr;
1514 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04001515 {
1516 //
1517 // Build intermediate representation
1518 //
Olli Etuahoe7847b02015-03-16 11:56:12 +02001519 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001520 }
1521 else
1522 {
1523 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001524 return nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001525 }
1526}
1527
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001528TIntermAggregate *TParseContext::parseSingleArrayInitDeclaration(TPublicType &publicType,
1529 const TSourceLoc &identifierLocation,
1530 const TString &identifier,
1531 const TSourceLoc &indexLocation,
1532 TIntermTyped *indexExpression,
1533 const TSourceLoc &initLocation,
1534 TIntermTyped *initializer)
1535{
1536 mDeferredSingleDeclarationErrorCheck = false;
1537
1538 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1539 recover();
1540
1541 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1542 {
1543 recover();
1544 }
1545
1546 TPublicType arrayType(publicType);
1547
Olli Etuaho376f1b52015-04-13 13:23:41 +03001548 int size = 0;
1549 // If indexExpression is nullptr, then the array will eventually get its size implicitly from the initializer.
1550 if (indexExpression != nullptr && arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001551 {
1552 recover();
1553 }
1554 // Make the type an array even if size check failed.
1555 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1556 arrayType.setArraySize(size);
1557
1558 // initNode will correspond to the whole of "type b[n] = initializer".
1559 TIntermNode *initNode = nullptr;
1560 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1561 {
1562 return initNode ? intermediate.makeAggregate(initNode, initLocation) : nullptr;
1563 }
1564 else
1565 {
1566 recover();
1567 return nullptr;
1568 }
1569}
1570
Olli Etuahoe7847b02015-03-16 11:56:12 +02001571TIntermAggregate *TParseContext::parseInvariantDeclaration(const TSourceLoc &invariantLoc,
Jamie Madill47e3ec02014-08-20 16:38:33 -04001572 const TSourceLoc &identifierLoc,
1573 const TString *identifier,
1574 const TSymbol *symbol)
1575{
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001576 // invariant declaration
Jamie Madill47e3ec02014-08-20 16:38:33 -04001577 if (globalErrorCheck(invariantLoc, symbolTable.atGlobalLevel(), "invariant varying"))
1578 {
1579 recover();
1580 }
1581
1582 if (!symbol)
1583 {
1584 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
1585 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001586 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001587 }
1588 else
1589 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001590 const TString kGlFrontFacing("gl_FrontFacing");
1591 if (*identifier == kGlFrontFacing)
1592 {
1593 error(identifierLoc, "identifier should not be declared as invariant", identifier->c_str());
1594 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001595 return nullptr;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001596 }
Jamie Madill2c433252014-12-03 12:36:54 -05001597 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001598 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1599 ASSERT(variable);
1600 const TType &type = variable->getType();
1601 TIntermSymbol *intermSymbol = intermediate.addSymbol(variable->getUniqueId(),
1602 *identifier, type, identifierLoc);
1603
1604 TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc);
1605 aggregate->setOp(EOpInvariantDeclaration);
1606 return aggregate;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001607 }
1608}
1609
Olli Etuahoe7847b02015-03-16 11:56:12 +02001610TIntermAggregate *TParseContext::parseDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1611 const TSourceLoc &identifierLocation, const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001612{
Olli Etuahofa33d582015-04-09 14:33:12 +03001613 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1614 if (mDeferredSingleDeclarationErrorCheck)
1615 {
1616 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1617 recover();
1618 mDeferredSingleDeclarationErrorCheck = false;
1619 }
1620
Jamie Madill0bd18df2013-06-20 11:55:52 -04001621 if (locationDeclaratorListCheck(identifierLocation, publicType))
1622 recover();
1623
Olli Etuaho376f1b52015-04-13 13:23:41 +03001624 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001625 recover();
1626
Olli Etuaho2935c582015-04-08 14:32:06 +03001627 TVariable *variable = nullptr;
1628 if (!declareVariable(identifierLocation, identifier, TType(publicType), &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001629 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001630
1631 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1632 if (variable && symbol)
Jamie Madill502d66f2013-06-20 11:55:52 -04001633 symbol->setId(variable->getUniqueId());
1634
Olli Etuahoe7847b02015-03-16 11:56:12 +02001635 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001636}
1637
Olli Etuahoe7847b02015-03-16 11:56:12 +02001638TIntermAggregate *TParseContext::parseArrayDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1639 const TSourceLoc &identifierLocation, const TString &identifier,
1640 const TSourceLoc &arrayLocation, TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001641{
Olli Etuahofa33d582015-04-09 14:33:12 +03001642 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1643 if (mDeferredSingleDeclarationErrorCheck)
1644 {
1645 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1646 recover();
1647 mDeferredSingleDeclarationErrorCheck = false;
1648 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001649
Jamie Madill0bd18df2013-06-20 11:55:52 -04001650 if (locationDeclaratorListCheck(identifierLocation, publicType))
1651 recover();
1652
Olli Etuaho376f1b52015-04-13 13:23:41 +03001653 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001654 recover();
1655
1656 if (arrayTypeErrorCheck(arrayLocation, publicType) || arrayQualifierErrorCheck(arrayLocation, publicType))
1657 {
1658 recover();
1659 }
Olli Etuaho93a90fd2015-04-07 18:14:07 +03001660 else
Jamie Madill502d66f2013-06-20 11:55:52 -04001661 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001662 TType arrayType = TType(publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04001663 int size;
1664 if (arraySizeErrorCheck(arrayLocation, indexExpression, size))
Olli Etuahoe7847b02015-03-16 11:56:12 +02001665 {
Jamie Madill502d66f2013-06-20 11:55:52 -04001666 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001667 }
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001668 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001669
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001670 TVariable *variable = nullptr;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001671 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001672 recover();
Jamie Madill502d66f2013-06-20 11:55:52 -04001673
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001674 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
1675 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001676 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001677
1678 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001679 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001680
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001681 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001682}
1683
Jamie Madill06145232015-05-13 13:10:01 -04001684TIntermAggregate *TParseContext::parseInitDeclarator(const TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001685 const TSourceLoc &identifierLocation, const TString &identifier,
1686 const TSourceLoc &initLocation, TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001687{
Olli Etuahofa33d582015-04-09 14:33:12 +03001688 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1689 if (mDeferredSingleDeclarationErrorCheck)
1690 {
1691 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1692 recover();
1693 mDeferredSingleDeclarationErrorCheck = false;
1694 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001695
Jamie Madill0bd18df2013-06-20 11:55:52 -04001696 if (locationDeclaratorListCheck(identifierLocation, publicType))
1697 recover();
1698
Olli Etuahoe7847b02015-03-16 11:56:12 +02001699 TIntermNode *intermNode = nullptr;
1700 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04001701 {
1702 //
1703 // build the intermediate representation
1704 //
1705 if (intermNode)
1706 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001707 return intermediate.growAggregate(aggregateDeclaration, intermNode, initLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001708 }
1709 else
1710 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001711 return aggregateDeclaration;
Jamie Madill502d66f2013-06-20 11:55:52 -04001712 }
1713 }
1714 else
1715 {
1716 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001717 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001718 }
1719}
1720
Jamie Madill06145232015-05-13 13:10:01 -04001721TIntermAggregate *TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001722 TIntermAggregate *aggregateDeclaration,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301723 const TSourceLoc &identifierLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001724 const TString &identifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301725 const TSourceLoc &indexLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001726 TIntermTyped *indexExpression,
1727 const TSourceLoc &initLocation, TIntermTyped *initializer)
1728{
1729 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1730 if (mDeferredSingleDeclarationErrorCheck)
1731 {
1732 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1733 recover();
1734 mDeferredSingleDeclarationErrorCheck = false;
1735 }
1736
1737 if (locationDeclaratorListCheck(identifierLocation, publicType))
1738 recover();
1739
1740 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1741 {
1742 recover();
1743 }
1744
1745 TPublicType arrayType(publicType);
1746
Olli Etuaho376f1b52015-04-13 13:23:41 +03001747 int size = 0;
1748 // If indexExpression is nullptr, then the array will eventually get its size implicitly from the initializer.
1749 if (indexExpression != nullptr && arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001750 {
1751 recover();
1752 }
1753 // Make the type an array even if size check failed.
1754 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1755 arrayType.setArraySize(size);
1756
1757 // initNode will correspond to the whole of "b[n] = initializer".
1758 TIntermNode *initNode = nullptr;
1759 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1760 {
1761 if (initNode)
1762 {
1763 return intermediate.growAggregate(aggregateDeclaration, initNode, initLocation);
1764 }
1765 else
1766 {
1767 return aggregateDeclaration;
1768 }
1769 }
1770 else
1771 {
1772 recover();
1773 return nullptr;
1774 }
1775}
1776
Jamie Madilla295edf2013-06-06 11:56:48 -04001777void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1778{
1779 if (typeQualifier.qualifier != EvqUniform)
1780 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301781 error(typeQualifier.line,
1782 "invalid qualifier:",
1783 getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
Jamie Madilla295edf2013-06-06 11:56:48 -04001784 recover();
1785 return;
1786 }
1787
1788 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1789 ASSERT(!layoutQualifier.isEmpty());
1790
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001791 if (mShaderVersion < 300)
Jamie Madilla295edf2013-06-06 11:56:48 -04001792 {
1793 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1794 recover();
1795 return;
1796 }
1797
1798 if (layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1799 {
1800 recover();
1801 return;
1802 }
1803
Jamie Madill099c0f32013-06-20 11:55:52 -04001804 if (layoutQualifier.matrixPacking != EmpUnspecified)
1805 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001806 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04001807 }
1808
Geoff Langc6856732014-02-11 09:38:55 -05001809 if (layoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04001810 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001811 mDefaultBlockStorage = layoutQualifier.blockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04001812 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001813}
1814
Jamie Madill185fb402015-06-12 15:48:48 -04001815void TParseContext::parseFunctionPrototype(const TSourceLoc &location,
1816 TFunction *function,
1817 TIntermAggregate **aggregateOut)
1818{
1819 const TSymbol *builtIn = symbolTable.findBuiltIn(function->getMangledName(), getShaderVersion());
1820
1821 if (builtIn)
1822 {
1823 error(location, "built-in functions cannot be redefined", function->getName().c_str());
1824 recover();
1825 }
1826
1827 TFunction* prevDec = static_cast<TFunction*>(symbolTable.find(function->getMangledName(), getShaderVersion()));
1828 //
1829 // Note: 'prevDec' could be 'function' if this is the first time we've seen function
1830 // as it would have just been put in the symbol table. Otherwise, we're looking up
1831 // an earlier occurance.
1832 //
1833 if (prevDec->isDefined())
1834 {
1835 // Then this function already has a body.
1836 error(location, "function already has a body", function->getName().c_str());
1837 recover();
1838 }
1839 prevDec->setDefined();
1840 //
1841 // Overload the unique ID of the definition to be the same unique ID as the declaration.
1842 // Eventually we will probably want to have only a single definition and just swap the
1843 // arguments to be the definition's arguments.
1844 //
1845 function->setUniqueId(prevDec->getUniqueId());
1846
1847 // Raise error message if main function takes any parameters or return anything other than void
1848 if (function->getName() == "main")
1849 {
1850 if (function->getParamCount() > 0)
1851 {
1852 error(location, "function cannot take any parameter(s)", function->getName().c_str());
1853 recover();
1854 }
1855 if (function->getReturnType().getBasicType() != EbtVoid)
1856 {
1857 error(location, "", function->getReturnType().getBasicString(), "main function cannot return a value");
1858 recover();
1859 }
1860 }
1861
1862 //
1863 // Remember the return type for later checking for RETURN statements.
1864 //
1865 setCurrentFunctionType(&(prevDec->getReturnType()));
1866 setFunctionReturnsValue(false);
1867
1868 //
1869 // Insert parameters into the symbol table.
1870 // If the parameter has no name, it's not an error, just don't insert it
1871 // (could be used for unused args).
1872 //
1873 // Also, accumulate the list of parameters into the HIL, so lower level code
1874 // knows where to find parameters.
1875 //
1876 TIntermAggregate *paramNodes = new TIntermAggregate;
1877 for (size_t i = 0; i < function->getParamCount(); i++)
1878 {
1879 const TConstParameter &param = function->getParam(i);
1880 if (param.name != 0)
1881 {
1882 TVariable *variable = new TVariable(param.name, *param.type);
1883 //
1884 // Insert the parameters with name in the symbol table.
1885 //
Jamie Madill1a4b1b32015-07-23 18:27:13 -04001886 if (!symbolTable.declare(variable))
1887 {
Jamie Madill185fb402015-06-12 15:48:48 -04001888 error(location, "redefinition", variable->getName().c_str());
1889 recover();
Jamie Madill1a4b1b32015-07-23 18:27:13 -04001890 paramNodes = intermediate.growAggregate(
1891 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
1892 continue;
Jamie Madill185fb402015-06-12 15:48:48 -04001893 }
1894
1895 //
1896 // Add the parameter to the HIL
1897 //
1898 TIntermSymbol *symbol = intermediate.addSymbol(variable->getUniqueId(),
1899 variable->getName(),
1900 variable->getType(), location);
1901
1902 paramNodes = intermediate.growAggregate(paramNodes, symbol, location);
1903 }
1904 else
1905 {
1906 paramNodes = intermediate.growAggregate(paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
1907 }
1908 }
1909 intermediate.setAggregateOperator(paramNodes, EOpParameters, location);
1910 *aggregateOut = paramNodes;
1911 setLoopNestingLevel(0);
1912}
1913
1914TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location,
1915 TFunction *function)
1916{
1917 //
1918 // Multiple declarations of the same function are allowed.
1919 //
1920 // If this is a definition, the definition production code will check for redefinitions
1921 // (we don't know at this point if it's a definition or not).
1922 //
1923 // Redeclarations are allowed. But, return types and parameter qualifiers must match.
1924 //
1925 TFunction *prevDec = static_cast<TFunction*>(symbolTable.find(function->getMangledName(), getShaderVersion()));
1926 if (prevDec)
1927 {
1928 if (prevDec->getReturnType() != function->getReturnType())
1929 {
1930 error(location,
1931 "overloaded functions must have the same return type",
1932 function->getReturnType().getBasicString());
1933 recover();
1934 }
1935 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
1936 {
1937 if (prevDec->getParam(i).type->getQualifier() != function->getParam(i).type->getQualifier())
1938 {
1939 error(location,
1940 "overloaded functions must have the same parameter qualifiers",
1941 function->getParam(i).type->getQualifierString());
1942 recover();
1943 }
1944 }
1945 }
1946
1947 //
1948 // Check for previously declared variables using the same name.
1949 //
1950 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
1951 if (prevSym)
1952 {
1953 if (!prevSym->isFunction())
1954 {
1955 error(location, "redefinition", function->getName().c_str(), "function");
1956 recover();
1957 }
1958 }
1959 else
1960 {
1961 // Insert the unmangled name to detect potential future redefinition as a variable.
1962 TFunction *newFunction = new TFunction(NewPoolTString(function->getName().c_str()), &function->getReturnType());
1963 symbolTable.getOuterLevel()->insertUnmangled(newFunction);
1964 }
1965
1966 // We're at the inner scope level of the function's arguments and body statement.
1967 // Add the function prototype to the surrounding scope instead.
1968 symbolTable.getOuterLevel()->insert(function);
1969
1970 //
1971 // If this is a redeclaration, it could also be a definition,
1972 // in which case, we want to use the variable names from this one, and not the one that's
1973 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
1974 //
1975 return function;
1976}
1977
Jamie Madill06145232015-05-13 13:10:01 -04001978TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001979{
Jamie Madill06145232015-05-13 13:10:01 -04001980 TPublicType publicType = publicTypeIn;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001981 TOperator op = EOpNull;
1982 if (publicType.userDef)
1983 {
1984 op = EOpConstructStruct;
1985 }
1986 else
1987 {
1988 switch (publicType.type)
1989 {
1990 case EbtFloat:
1991 if (publicType.isMatrix())
1992 {
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001993 switch(publicType.getCols())
1994 {
Alexis Hetu07e57df2015-06-16 16:55:52 -04001995 case 2:
1996 switch(publicType.getRows())
1997 {
1998 case 2: op = EOpConstructMat2; break;
1999 case 3: op = EOpConstructMat2x3; break;
2000 case 4: op = EOpConstructMat2x4; break;
2001 }
2002 break;
2003 case 3:
2004 switch(publicType.getRows())
2005 {
2006 case 2: op = EOpConstructMat3x2; break;
2007 case 3: op = EOpConstructMat3; break;
2008 case 4: op = EOpConstructMat3x4; break;
2009 }
2010 break;
2011 case 4:
2012 switch(publicType.getRows())
2013 {
2014 case 2: op = EOpConstructMat4x2; break;
2015 case 3: op = EOpConstructMat4x3; break;
2016 case 4: op = EOpConstructMat4; break;
2017 }
2018 break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002019 }
2020 }
2021 else
2022 {
2023 switch(publicType.getNominalSize())
2024 {
Jamie Madill28b97422013-07-08 14:01:38 -04002025 case 1: op = EOpConstructFloat; break;
2026 case 2: op = EOpConstructVec2; break;
2027 case 3: op = EOpConstructVec3; break;
2028 case 4: op = EOpConstructVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002029 }
2030 }
2031 break;
2032
2033 case EbtInt:
2034 switch(publicType.getNominalSize())
2035 {
Jamie Madill28b97422013-07-08 14:01:38 -04002036 case 1: op = EOpConstructInt; break;
2037 case 2: op = EOpConstructIVec2; break;
2038 case 3: op = EOpConstructIVec3; break;
2039 case 4: op = EOpConstructIVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002040 }
2041 break;
2042
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002043 case EbtUInt:
2044 switch(publicType.getNominalSize())
2045 {
Jamie Madill28b97422013-07-08 14:01:38 -04002046 case 1: op = EOpConstructUInt; break;
2047 case 2: op = EOpConstructUVec2; break;
2048 case 3: op = EOpConstructUVec3; break;
2049 case 4: op = EOpConstructUVec4; break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00002050 }
2051 break;
2052
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002053 case EbtBool:
2054 switch(publicType.getNominalSize())
2055 {
Jamie Madill28b97422013-07-08 14:01:38 -04002056 case 1: op = EOpConstructBool; break;
2057 case 2: op = EOpConstructBVec2; break;
2058 case 3: op = EOpConstructBVec3; break;
2059 case 4: op = EOpConstructBVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002060 }
2061 break;
2062
2063 default: break;
2064 }
2065
2066 if (op == EOpNull)
2067 {
2068 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
2069 recover();
2070 publicType.type = EbtFloat;
2071 op = EOpConstructFloat;
2072 }
2073 }
2074
2075 TString tempString;
Dmitry Skiba7f17a502015-06-22 15:08:39 -07002076 const TType *type = new TType(publicType);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002077 return new TFunction(&tempString, type, op);
2078}
2079
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002080// This function is used to test for the correctness of the parameters passed to various constructor functions
Arun Patole7e7e68d2015-05-22 12:02:25 +05302081// and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002082//
2083// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
2084//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302085TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments, TType *type, TOperator op, TFunction *fnCall,
2086 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002087{
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002088 TIntermAggregate *aggregateArguments = arguments->getAsAggregate();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002089
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002090 if (!aggregateArguments)
2091 {
2092 aggregateArguments = new TIntermAggregate;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002093 aggregateArguments->getSequence()->push_back(arguments);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002094 }
2095
Olli Etuahof40319e2015-03-10 14:33:00 +02002096 if (type->isArray())
2097 {
2098 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of the array.
2099 TIntermSequence *args = aggregateArguments->getSequence();
2100 for (size_t i = 0; i < args->size(); i++)
2101 {
2102 const TType &argType = (*args)[i]->getAsTyped()->getType();
2103 // It has already been checked that the argument is not an array.
2104 ASSERT(!argType.isArray());
2105 if (!argType.sameElementType(*type))
2106 {
2107 error(line, "Array constructor argument has an incorrect type", "Error");
2108 recover();
2109 return nullptr;
2110 }
2111 }
2112 }
2113 else if (op == EOpConstructStruct)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002114 {
2115 const TFieldList &fields = type->getStruct()->fields();
Zhenyao Moe40d1e92014-07-16 17:40:36 -07002116 TIntermSequence *args = aggregateArguments->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002117
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002118 for (size_t i = 0; i < fields.size(); i++)
2119 {
Nicolas Capensffd73872014-08-21 13:49:16 -04002120 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002121 {
2122 error(line, "Structure constructor arguments do not match structure fields", "Error");
2123 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002124
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002125 return 0;
2126 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002127 }
2128 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002129
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002130 // Turn the argument list itself into a constructor
Olli Etuaho21203702014-11-13 16:16:21 +02002131 TIntermAggregate *constructor = intermediate.setAggregateOperator(aggregateArguments, op, line);
2132 TIntermTyped *constConstructor = foldConstConstructor(constructor, *type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002133 if (constConstructor)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002134 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002135 return constConstructor;
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002136 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002137
Olli Etuaho21203702014-11-13 16:16:21 +02002138 // Structs should not be precision qualified, the individual members may be.
2139 // Built-in types on the other hand should be precision qualified.
2140 if (op != EOpConstructStruct)
2141 {
2142 constructor->setPrecisionFromChildren();
2143 type->setPrecision(constructor->getPrecision());
2144 }
2145
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002146 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002147}
2148
Arun Patole7e7e68d2015-05-22 12:02:25 +05302149TIntermTyped *TParseContext::foldConstConstructor(TIntermAggregate *aggrNode, const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002150{
Olli Etuahof40319e2015-03-10 14:33:00 +02002151 // TODO: Add support for folding array constructors
2152 bool canBeFolded = areAllChildConst(aggrNode) && !type.isArray();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002153 aggrNode->setType(type);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302154 if (canBeFolded)
2155 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002156 bool returnVal = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302157 TConstantUnion *unionArray = new TConstantUnion[type.getObjectSize()];
2158 if (aggrNode->getSequence()->size() == 1)
2159 {
2160 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type,
2161 true);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002162 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302163 else
2164 {
2165 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(),
2166 type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002167 }
2168 if (returnVal)
2169 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002170
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002171 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
2172 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002173
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002174 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002175}
2176
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002177//
2178// This function returns the tree representation for the vector field(s) being accessed from contant vector.
2179// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
2180// 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 +05302181// 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 +00002182// a constant matrix.
2183//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302184TIntermTyped *TParseContext::addConstVectorNode(TVectorFields &fields, TIntermTyped *node, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002185{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302186 TIntermTyped *typedNode;
2187 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002188
Jamie Madillb11e2482015-05-04 14:21:22 -04002189 const TConstantUnion *unionArray;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302190 if (tempConstantNode)
2191 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002192 unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002193
Arun Patole7e7e68d2015-05-22 12:02:25 +05302194 if (!unionArray)
2195 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002196 return node;
2197 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302198 }
2199 else
2200 { // 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 +00002201 error(line, "Cannot offset into the vector", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002202 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002203
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002204 return 0;
2205 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002206
Arun Patole7e7e68d2015-05-22 12:02:25 +05302207 TConstantUnion *constArray = new TConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002208
Arun Patole7e7e68d2015-05-22 12:02:25 +05302209 for (int i = 0; i < fields.num; i++)
2210 {
2211 if (fields.offsets[i] >= node->getType().getNominalSize())
2212 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002213 std::stringstream extraInfoStream;
2214 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
2215 std::string extraInfo = extraInfoStream.str();
2216 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002217 recover();
2218 fields.offsets[i] = 0;
2219 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302220
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002221 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002222
Arun Patole7e7e68d2015-05-22 12:02:25 +05302223 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002224 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
2225 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002226}
2227
2228//
2229// This function returns the column being accessed from a constant matrix. The values are retrieved from
Arun Patole7e7e68d2015-05-22 12:02:25 +05302230// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
2231// 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 +00002232// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
2233//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302234TIntermTyped *TParseContext::addConstMatrixNode(int index, TIntermTyped *node, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002235{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302236 TIntermTyped *typedNode;
2237 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002238
Arun Patole7e7e68d2015-05-22 12:02:25 +05302239 if (index >= node->getType().getCols())
2240 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002241 std::stringstream extraInfoStream;
2242 extraInfoStream << "matrix field selection out of range '" << index << "'";
2243 std::string extraInfo = extraInfoStream.str();
2244 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002245 recover();
2246 index = 0;
2247 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002248
Arun Patole7e7e68d2015-05-22 12:02:25 +05302249 if (tempConstantNode)
2250 {
Jamie Madillb11e2482015-05-04 14:21:22 -04002251 TConstantUnion *unionArray = tempConstantNode->getUnionArrayPointer();
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002252 int size = tempConstantNode->getType().getCols();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002253 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302254 }
2255 else
2256 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002257 error(line, "Cannot offset into the matrix", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002258 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002259
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002260 return 0;
2261 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002262
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002263 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002264}
2265
2266
2267//
2268// 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 +05302269// the symbol table and parse-tree is built for the type of the element. The input
2270// 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 +00002271// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
2272//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302273TIntermTyped *TParseContext::addConstArrayNode(int index, TIntermTyped *node, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002274{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302275 TIntermTyped *typedNode;
2276 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002277 TType arrayElementType = node->getType();
2278 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002279
Arun Patole7e7e68d2015-05-22 12:02:25 +05302280 if (index >= node->getType().getArraySize())
2281 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002282 std::stringstream extraInfoStream;
2283 extraInfoStream << "array field selection out of range '" << index << "'";
2284 std::string extraInfo = extraInfoStream.str();
2285 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002286 recover();
2287 index = 0;
2288 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002289
Arun Patole7e7e68d2015-05-22 12:02:25 +05302290 if (tempConstantNode)
2291 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002292 size_t arrayElementSize = arrayElementType.getObjectSize();
Arun Patole7e7e68d2015-05-22 12:02:25 +05302293 TConstantUnion *unionArray = tempConstantNode->getUnionArrayPointer();
2294 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(),
2295 line);
2296 }
2297 else
2298 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002299 error(line, "Cannot offset into the array", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002300 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002301
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002302 return 0;
2303 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002304
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002305 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002306}
2307
2308
2309//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302310// 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 +00002311// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
2312// function and returns the parse-tree with the values of the embedded/nested struct.
2313//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302314TIntermTyped *TParseContext::addConstStruct(const TString &identifier, TIntermTyped *node, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002315{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302316 const TFieldList &fields = node->getType().getStruct()->fields();
Jamie Madill94bf7f22013-07-08 13:31:15 -04002317 size_t instanceSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002318
Arun Patole7e7e68d2015-05-22 12:02:25 +05302319 for (size_t index = 0; index < fields.size(); ++index)
2320 {
2321 if (fields[index]->name() == identifier)
2322 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002323 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302324 }
2325 else
2326 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002327 instanceSize += fields[index]->type()->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002328 }
2329 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002330
Jamie Madill94bf7f22013-07-08 13:31:15 -04002331 TIntermTyped *typedNode;
2332 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
Arun Patole7e7e68d2015-05-22 12:02:25 +05302333 if (tempConstantNode)
2334 {
2335 TConstantUnion *constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002336
Arun Patole7e7e68d2015-05-22 12:02:25 +05302337 // type will be changed in the calling function
2338 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line);
2339 }
2340 else
2341 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002342 error(line, "Cannot offset into the structure", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002343 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002344
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002345 return 0;
2346 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002347
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002348 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002349}
2350
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002351//
2352// Interface/uniform blocks
2353//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302354TIntermAggregate *TParseContext::addInterfaceBlock(const TPublicType &typeQualifier, const TSourceLoc &nameLine,
2355 const TString &blockName, TFieldList *fieldList,
2356 const TString *instanceName, const TSourceLoc &instanceLine,
2357 TIntermTyped *arrayIndex, const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002358{
2359 if (reservedErrorCheck(nameLine, blockName))
2360 recover();
2361
2362 if (typeQualifier.qualifier != EvqUniform)
2363 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302364 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
2365 "interface blocks must be uniform");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002366 recover();
2367 }
2368
Jamie Madill099c0f32013-06-20 11:55:52 -04002369 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
2370 if (layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002371 {
2372 recover();
2373 }
2374
Jamie Madill099c0f32013-06-20 11:55:52 -04002375 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2376 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002377 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002378 }
2379
Jamie Madill1566ef72013-06-20 11:55:54 -04002380 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2381 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002382 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002383 }
2384
Arun Patole7e7e68d2015-05-22 12:02:25 +05302385 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2386 if (!symbolTable.declare(blockNameSymbol))
2387 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002388 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
2389 recover();
2390 }
2391
Jamie Madill98493dd2013-07-08 14:39:03 -04002392 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302393 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2394 {
2395 TField *field = (*fieldList)[memberIndex];
2396 TType *fieldType = field->type();
2397 if (IsSampler(fieldType->getBasicType()))
2398 {
2399 error(field->line(), "unsupported type", fieldType->getBasicString(),
2400 "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002401 recover();
2402 }
2403
Jamie Madill98493dd2013-07-08 14:39:03 -04002404 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002405 switch (qualifier)
2406 {
2407 case EvqGlobal:
2408 case EvqUniform:
2409 break;
2410 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04002411 error(field->line(), "invalid qualifier on interface block member", getQualifierString(qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002412 recover();
2413 break;
2414 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002415
2416 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002417 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
2418 if (layoutLocationErrorCheck(field->line(), fieldLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002419 {
2420 recover();
2421 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002422
Jamie Madill98493dd2013-07-08 14:39:03 -04002423 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002424 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302425 error(field->line(), "invalid layout qualifier:", getBlockStorageString(fieldLayoutQualifier.blockStorage),
2426 "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04002427 recover();
2428 }
2429
Jamie Madill98493dd2013-07-08 14:39:03 -04002430 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002431 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002432 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002433 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002434 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002435 {
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002436 warning(field->line(), "extraneous layout qualifier:",
2437 getMatrixPackingString(fieldLayoutQualifier.matrixPacking), "only has an effect on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04002438 }
2439
Jamie Madill98493dd2013-07-08 14:39:03 -04002440 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002441 }
2442
Jamie Madill98493dd2013-07-08 14:39:03 -04002443 // add array index
2444 int arraySize = 0;
2445 if (arrayIndex != NULL)
2446 {
2447 if (arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize))
2448 recover();
2449 }
2450
Arun Patole7e7e68d2015-05-22 12:02:25 +05302451 TInterfaceBlock *interfaceBlock = new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize,
2452 blockLayoutQualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002453 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier, arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002454
2455 TString symbolName = "";
2456 int symbolId = 0;
2457
Jamie Madill98493dd2013-07-08 14:39:03 -04002458 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002459 {
2460 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002461 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2462 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302463 TField *field = (*fieldList)[memberIndex];
2464 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002465
2466 // set parent pointer of the field variable
2467 fieldType->setInterfaceBlock(interfaceBlock);
2468
Arun Patole7e7e68d2015-05-22 12:02:25 +05302469 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002470 fieldVariable->setQualifier(typeQualifier.qualifier);
2471
Arun Patole7e7e68d2015-05-22 12:02:25 +05302472 if (!symbolTable.declare(fieldVariable))
2473 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002474 error(field->line(), "redefinition", field->name().c_str(), "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002475 recover();
2476 }
2477 }
2478 }
2479 else
2480 {
Olli Etuahoe0f623a2015-07-10 11:58:30 +03002481 if (reservedErrorCheck(instanceLine, *instanceName))
2482 recover();
2483
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002484 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302485 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002486 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002487
Arun Patole7e7e68d2015-05-22 12:02:25 +05302488 if (!symbolTable.declare(instanceTypeDef))
2489 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002490 error(instanceLine, "redefinition", instanceName->c_str(), "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002491 recover();
2492 }
2493
2494 symbolId = instanceTypeDef->getUniqueId();
2495 symbolName = instanceTypeDef->getName();
2496 }
2497
Arun Patole7e7e68d2015-05-22 12:02:25 +05302498 TIntermAggregate *aggregate = intermediate.makeAggregate(intermediate.addSymbol(symbolId, symbolName,
2499 interfaceBlockType,
2500 typeQualifier.line),
2501 nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002502 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04002503
2504 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002505 return aggregate;
2506}
2507
Arun Patole7e7e68d2015-05-22 12:02:25 +05302508bool TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002509{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002510 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002511
2512 // Embedded structure definitions are not supported per GLSL ES spec.
2513 // They aren't allowed in GLSL either, but we need to detect this here
2514 // so we don't rely on the GLSL compiler to catch it.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302515 if (mStructNestingLevel > 1)
2516 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002517 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002518 return true;
2519 }
2520
2521 return false;
2522}
2523
2524void TParseContext::exitStructDeclaration()
2525{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002526 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002527}
2528
2529namespace {
2530
2531const int kWebGLMaxStructNesting = 4;
2532
2533} // namespace
2534
Arun Patole7e7e68d2015-05-22 12:02:25 +05302535bool TParseContext::structNestingErrorCheck(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002536{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302537 if (!IsWebGLBasedSpec(mShaderSpec))
2538 {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002539 return false;
2540 }
2541
Arun Patole7e7e68d2015-05-22 12:02:25 +05302542 if (field.type()->getBasicType() != EbtStruct)
2543 {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002544 return false;
2545 }
2546
2547 // We're already inside a structure definition at this point, so add
2548 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302549 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2550 {
Jamie Madill41a49272014-03-18 16:10:13 -04002551 std::stringstream reasonStream;
2552 reasonStream << "Reference of struct type "
2553 << field.type()->getStruct()->name().c_str()
2554 << " exceeds maximum allowed nesting level of "
2555 << kWebGLMaxStructNesting;
2556 std::string reason = reasonStream.str();
2557 error(line, reason.c_str(), field.name().c_str(), "");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002558 return true;
2559 }
2560
2561 return false;
2562}
2563
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002564//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002565// Parse an array index expression
2566//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302567TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression, const TSourceLoc &location,
2568 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002569{
2570 TIntermTyped *indexedExpression = NULL;
2571
2572 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2573 {
2574 if (baseExpression->getAsSymbolNode())
2575 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302576 error(location, " left of '[' is not of type array, matrix, or vector ",
2577 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002578 }
2579 else
2580 {
2581 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2582 }
2583 recover();
2584 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002585
Jamie Madill21c1e452014-12-29 11:33:41 -05002586 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2587
2588 if (indexExpression->getQualifier() == EvqConst && indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002589 {
Jamie Madill21c1e452014-12-29 11:33:41 -05002590 int index = indexConstantUnion->getIConst(0);
Jamie Madill7164cf42013-07-08 13:30:59 -04002591 if (index < 0)
2592 {
2593 std::stringstream infoStream;
2594 infoStream << index;
2595 std::string info = infoStream.str();
2596 error(location, "negative index", info.c_str());
2597 recover();
2598 index = 0;
2599 }
2600 if (baseExpression->getType().getQualifier() == EvqConst)
2601 {
2602 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002603 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002604 // constant folding for arrays
2605 indexedExpression = addConstArrayNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002606 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002607 else if (baseExpression->isVector())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002608 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002609 // constant folding for vectors
2610 TVectorFields fields;
2611 fields.num = 1;
2612 fields.offsets[0] = index; // need to do it this way because v.xy sends fields integer array
2613 indexedExpression = addConstVectorNode(fields, baseExpression, location);
2614 }
2615 else if (baseExpression->isMatrix())
2616 {
2617 // constant folding for matrices
2618 indexedExpression = addConstMatrixNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002619 }
2620 }
2621 else
2622 {
Jamie Madillb11e2482015-05-04 14:21:22 -04002623 int safeIndex = -1;
2624
Jamie Madill7164cf42013-07-08 13:30:59 -04002625 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002626 {
Jamie Madill18464b52013-07-08 14:01:55 -04002627 if (index >= baseExpression->getType().getArraySize())
Jamie Madill7164cf42013-07-08 13:30:59 -04002628 {
2629 std::stringstream extraInfoStream;
2630 extraInfoStream << "array index out of range '" << index << "'";
2631 std::string extraInfo = extraInfoStream.str();
2632 error(location, "", "[", extraInfo.c_str());
2633 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002634 safeIndex = baseExpression->getType().getArraySize() - 1;
Jamie Madill7164cf42013-07-08 13:30:59 -04002635 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302636 else if (baseExpression->getQualifier() == EvqFragData && index > 0 &&
2637 !isExtensionEnabled("GL_EXT_draw_buffers"))
Jamie Madill5d287f52013-07-12 15:38:19 -04002638 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302639 error(location, "",
2640 "[", "array indexes for gl_FragData must be zero when GL_EXT_draw_buffers is disabled");
Jamie Madill5d287f52013-07-12 15:38:19 -04002641 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002642 safeIndex = 0;
Jamie Madill5d287f52013-07-12 15:38:19 -04002643 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002644 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302645 else if ((baseExpression->isVector() || baseExpression->isMatrix()) &&
2646 baseExpression->getType().getNominalSize() <= index)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002647 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002648 std::stringstream extraInfoStream;
2649 extraInfoStream << "field selection out of range '" << index << "'";
2650 std::string extraInfo = extraInfoStream.str();
2651 error(location, "", "[", extraInfo.c_str());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002652 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002653 safeIndex = baseExpression->getType().getNominalSize() - 1;
Jamie Madill46131a32013-06-20 11:55:50 -04002654 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002655
Jamie Madillb11e2482015-05-04 14:21:22 -04002656 // Don't modify the data of the previous constant union, because it can point
2657 // to builtins, like gl_MaxDrawBuffers. Instead use a new sanitized object.
2658 if (safeIndex != -1)
2659 {
2660 TConstantUnion *safeConstantUnion = new TConstantUnion();
2661 safeConstantUnion->setIConst(safeIndex);
2662 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
2663 }
2664
Jamie Madill7164cf42013-07-08 13:30:59 -04002665 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002666 }
2667 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002668 else
2669 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002670 if (baseExpression->isInterfaceBlock())
Jamie Madill7164cf42013-07-08 13:30:59 -04002671 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302672 error(location, "",
2673 "[", "array indexes for interface blocks arrays must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002674 recover();
2675 }
Jamie Madill19571812013-08-12 15:26:34 -07002676 else if (baseExpression->getQualifier() == EvqFragmentOut)
Jamie Madill7164cf42013-07-08 13:30:59 -04002677 {
Jamie Madill19571812013-08-12 15:26:34 -07002678 error(location, "", "[", "array indexes for fragment outputs must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002679 recover();
2680 }
2681
2682 indexedExpression = intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
2683 }
2684
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002685 if (indexedExpression == 0)
2686 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002687 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002688 unionArray->setFConst(0.0f);
2689 indexedExpression = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
2690 }
2691 else if (baseExpression->isArray())
2692 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002693 const TType &baseType = baseExpression->getType();
2694 if (baseType.getStruct())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002695 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002696 TType copyOfType(baseType.getStruct());
2697 indexedExpression->setType(copyOfType);
2698 }
2699 else if (baseType.isInterfaceBlock())
2700 {
2701 TType copyOfType(baseType.getInterfaceBlock(), baseType.getQualifier(), baseType.getLayoutQualifier(), 0);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002702 indexedExpression->setType(copyOfType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002703 }
2704 else
2705 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302706 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2707 EvqTemporary, static_cast<unsigned char>(baseExpression->getNominalSize()),
2708 static_cast<unsigned char>(baseExpression->getSecondarySize())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002709 }
2710
2711 if (baseExpression->getType().getQualifier() == EvqConst)
2712 {
2713 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2714 }
2715 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002716 else if (baseExpression->isMatrix())
2717 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002718 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302719 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2720 qualifier, static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002721 }
2722 else if (baseExpression->isVector())
2723 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002724 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2725 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002726 }
2727 else
2728 {
2729 indexedExpression->setType(baseExpression->getType());
2730 }
2731
2732 return indexedExpression;
2733}
2734
Arun Patole7e7e68d2015-05-22 12:02:25 +05302735TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression, const TSourceLoc &dotLocation,
2736 const TString &fieldString, const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002737{
2738 TIntermTyped *indexedExpression = NULL;
2739
2740 if (baseExpression->isArray())
2741 {
2742 error(fieldLocation, "cannot apply dot operator to an array", ".");
2743 recover();
2744 }
2745
2746 if (baseExpression->isVector())
2747 {
2748 TVectorFields fields;
2749 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields, fieldLocation))
2750 {
2751 fields.num = 1;
2752 fields.offsets[0] = 0;
2753 recover();
2754 }
2755
2756 if (baseExpression->getType().getQualifier() == EvqConst)
2757 {
2758 // constant folding for vector fields
2759 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
2760 if (indexedExpression == 0)
2761 {
2762 recover();
2763 indexedExpression = baseExpression;
2764 }
2765 else
2766 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302767 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2768 EvqConst, (unsigned char) (fieldString).size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002769 }
2770 }
2771 else
2772 {
2773 TString vectorString = fieldString;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302774 TIntermTyped *index = intermediate.addSwizzle(fields, fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002775 indexedExpression = intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302776 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2777 EvqTemporary, (unsigned char) vectorString.size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002778 }
2779 }
2780 else if (baseExpression->isMatrix())
2781 {
2782 TMatrixFields fields;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302783 if (!parseMatrixFields(fieldString, baseExpression->getCols(), baseExpression->getRows(), fields,
2784 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002785 {
2786 fields.wholeRow = false;
2787 fields.wholeCol = false;
2788 fields.row = 0;
2789 fields.col = 0;
2790 recover();
2791 }
2792
2793 if (fields.wholeRow || fields.wholeCol)
2794 {
2795 error(dotLocation, " non-scalar fields not implemented yet", ".");
2796 recover();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002797 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002798 unionArray->setIConst(0);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302799 TIntermTyped *index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst),
2800 fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002801 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302802 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2803 EvqTemporary, static_cast<unsigned char>(baseExpression->getCols()),
2804 static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002805 }
2806 else
2807 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002808 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002809 unionArray->setIConst(fields.col * baseExpression->getRows() + fields.row);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302810 TIntermTyped *index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst),
2811 fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002812 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
2813 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision()));
2814 }
2815 }
2816 else if (baseExpression->getBasicType() == EbtStruct)
2817 {
2818 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302819 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04002820 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002821 {
2822 error(dotLocation, "structure has no fields", "Internal Error");
2823 recover();
2824 indexedExpression = baseExpression;
2825 }
2826 else
2827 {
2828 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002829 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002830 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002831 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002832 {
2833 fieldFound = true;
2834 break;
2835 }
2836 }
2837 if (fieldFound)
2838 {
2839 if (baseExpression->getType().getQualifier() == EvqConst)
2840 {
2841 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2842 if (indexedExpression == 0)
2843 {
2844 recover();
2845 indexedExpression = baseExpression;
2846 }
2847 else
2848 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002849 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002850 // change the qualifier of the return type, not of the structure field
2851 // as the structure definition is shared between various structures.
2852 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2853 }
2854 }
2855 else
2856 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002857 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002858 unionArray->setIConst(i);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302859 TIntermTyped *index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002860 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002861 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002862 }
2863 }
2864 else
2865 {
2866 error(dotLocation, " no such field in structure", fieldString.c_str());
2867 recover();
2868 indexedExpression = baseExpression;
2869 }
2870 }
2871 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002872 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002873 {
2874 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302875 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04002876 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002877 {
2878 error(dotLocation, "interface block has no fields", "Internal Error");
2879 recover();
2880 indexedExpression = baseExpression;
2881 }
2882 else
2883 {
2884 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002885 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002886 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002887 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002888 {
2889 fieldFound = true;
2890 break;
2891 }
2892 }
2893 if (fieldFound)
2894 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002895 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002896 unionArray->setIConst(i);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302897 TIntermTyped *index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
2898 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index,
2899 dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002900 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002901 }
2902 else
2903 {
2904 error(dotLocation, " no such field in interface block", fieldString.c_str());
2905 recover();
2906 indexedExpression = baseExpression;
2907 }
2908 }
2909 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002910 else
2911 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002912 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002913 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302914 error(dotLocation, " field selection requires structure, vector, or matrix on left hand side",
2915 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002916 }
2917 else
2918 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302919 error(dotLocation,
2920 " field selection requires structure, vector, matrix, or interface block on left hand side",
2921 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002922 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002923 recover();
2924 indexedExpression = baseExpression;
2925 }
2926
2927 return indexedExpression;
2928}
2929
Arun Patole7e7e68d2015-05-22 12:02:25 +05302930TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002931{
Jamie Madilla5efff92013-06-06 11:56:47 -04002932 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002933
Jamie Madilla5efff92013-06-06 11:56:47 -04002934 qualifier.location = -1;
2935 qualifier.matrixPacking = EmpUnspecified;
2936 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002937
2938 if (qualifierType == "shared")
2939 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002940 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002941 }
2942 else if (qualifierType == "packed")
2943 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002944 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002945 }
2946 else if (qualifierType == "std140")
2947 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002948 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002949 }
2950 else if (qualifierType == "row_major")
2951 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002952 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002953 }
2954 else if (qualifierType == "column_major")
2955 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002956 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002957 }
2958 else if (qualifierType == "location")
2959 {
2960 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "location requires an argument");
2961 recover();
2962 }
2963 else
2964 {
2965 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
2966 recover();
2967 }
2968
Jamie Madilla5efff92013-06-06 11:56:47 -04002969 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002970}
2971
Arun Patole7e7e68d2015-05-22 12:02:25 +05302972TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc &qualifierTypeLine,
2973 const TString &intValueString, int intValue,
2974 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002975{
Jamie Madilla5efff92013-06-06 11:56:47 -04002976 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002977
Jamie Madilla5efff92013-06-06 11:56:47 -04002978 qualifier.location = -1;
2979 qualifier.matrixPacking = EmpUnspecified;
2980 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002981
2982 if (qualifierType != "location")
2983 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302984 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
2985 "only location may have arguments");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002986 recover();
2987 }
2988 else
2989 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002990 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002991 if (intValue < 0)
2992 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002993 error(intValueLine, "out of range:", intValueString.c_str(), "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002994 recover();
2995 }
2996 else
2997 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002998 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002999 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003000 }
3001
Jamie Madilla5efff92013-06-06 11:56:47 -04003002 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003003}
3004
Jamie Madilla5efff92013-06-06 11:56:47 -04003005TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003006{
Jamie Madilla5efff92013-06-06 11:56:47 -04003007 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003008
Jamie Madilla5efff92013-06-06 11:56:47 -04003009 if (rightQualifier.location != -1)
3010 {
3011 joinedQualifier.location = rightQualifier.location;
3012 }
3013 if (rightQualifier.matrixPacking != EmpUnspecified)
3014 {
3015 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
3016 }
3017 if (rightQualifier.blockStorage != EbsUnspecified)
3018 {
3019 joinedQualifier.blockStorage = rightQualifier.blockStorage;
3020 }
3021
3022 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003023}
3024
Arun Patole7e7e68d2015-05-22 12:02:25 +05303025TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc,
3026 TQualifier interpolationQualifier,
3027 const TSourceLoc &storageLoc,
3028 TQualifier storageQualifier)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003029{
3030 TQualifier mergedQualifier = EvqSmoothIn;
3031
Arun Patole7e7e68d2015-05-22 12:02:25 +05303032 if (storageQualifier == EvqFragmentIn)
3033 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003034 if (interpolationQualifier == EvqSmooth)
3035 mergedQualifier = EvqSmoothIn;
3036 else if (interpolationQualifier == EvqFlat)
3037 mergedQualifier = EvqFlatIn;
3038 else UNREACHABLE();
3039 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303040 else if (storageQualifier == EvqCentroidIn)
3041 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003042 if (interpolationQualifier == EvqSmooth)
3043 mergedQualifier = EvqCentroidIn;
3044 else if (interpolationQualifier == EvqFlat)
3045 mergedQualifier = EvqFlatIn;
3046 else UNREACHABLE();
3047 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303048 else if (storageQualifier == EvqVertexOut)
3049 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003050 if (interpolationQualifier == EvqSmooth)
3051 mergedQualifier = EvqSmoothOut;
3052 else if (interpolationQualifier == EvqFlat)
3053 mergedQualifier = EvqFlatOut;
3054 else UNREACHABLE();
3055 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303056 else if (storageQualifier == EvqCentroidOut)
3057 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003058 if (interpolationQualifier == EvqSmooth)
3059 mergedQualifier = EvqCentroidOut;
3060 else if (interpolationQualifier == EvqFlat)
3061 mergedQualifier = EvqFlatOut;
3062 else UNREACHABLE();
3063 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303064 else
3065 {
3066 error(interpolationLoc, "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier",
3067 getInterpolationString(interpolationQualifier));
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003068 recover();
3069
3070 mergedQualifier = storageQualifier;
3071 }
3072
3073 TPublicType type;
3074 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
3075 return type;
3076}
3077
Arun Patole7e7e68d2015-05-22 12:02:25 +05303078TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier, TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003079{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03003080 if (voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier.type))
3081 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003082 recover();
3083 }
3084
Arun Patole7e7e68d2015-05-22 12:02:25 +05303085 for (unsigned int i = 0; i < fieldList->size(); ++i)
3086 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003087 //
3088 // Careful not to replace already known aspects of type, like array-ness
3089 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303090 TType *type = (*fieldList)[i]->type();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003091 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003092 type->setPrimarySize(typeSpecifier.primarySize);
3093 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003094 type->setPrecision(typeSpecifier.precision);
3095 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003096 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003097
3098 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303099 if (type->isArray())
3100 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003101 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
3102 recover();
3103 }
3104 if (typeSpecifier.array)
3105 type->setArraySize(typeSpecifier.arraySize);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303106 if (typeSpecifier.userDef)
3107 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003108 type->setStruct(typeSpecifier.userDef->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003109 }
3110
Arun Patole7e7e68d2015-05-22 12:02:25 +05303111 if (structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i]))
3112 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003113 recover();
3114 }
3115 }
3116
Jamie Madill98493dd2013-07-08 14:39:03 -04003117 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003118}
3119
Arun Patole7e7e68d2015-05-22 12:02:25 +05303120TPublicType TParseContext::addStructure(const TSourceLoc &structLine, const TSourceLoc &nameLine,
3121 const TString *structName, TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003122{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303123 TStructure *structure = new TStructure(structName, fieldList);
3124 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003125
Jamie Madill9b820842015-02-12 10:40:10 -05003126 // Store a bool in the struct if we're at global scope, to allow us to
3127 // skip the local struct scoping workaround in HLSL.
Jamie Madillb960cc42015-02-12 15:33:20 +00003128 structure->setUniqueId(TSymbolTable::nextUniqueId());
Jamie Madill9b820842015-02-12 10:40:10 -05003129 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003130
Jamie Madill98493dd2013-07-08 14:39:03 -04003131 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003132 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003133 if (reservedErrorCheck(nameLine, *structName))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003134 {
3135 recover();
3136 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05303137 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
3138 if (!symbolTable.declare(userTypeDef))
3139 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003140 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003141 recover();
3142 }
3143 }
3144
3145 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04003146 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003147 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003148 const TField &field = *(*fieldList)[typeListIndex];
3149 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003150 switch (qualifier)
3151 {
3152 case EvqGlobal:
3153 case EvqTemporary:
3154 break;
3155 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04003156 error(field.line(), "invalid qualifier on struct member", getQualifierString(qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003157 recover();
3158 break;
3159 }
3160 }
3161
3162 TPublicType publicType;
3163 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003164 publicType.userDef = structureType;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003165 exitStructDeclaration();
3166
3167 return publicType;
3168}
3169
Olli Etuahoa3a36662015-02-17 13:46:51 +02003170TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init, TIntermAggregate *statementList, const TSourceLoc &loc)
3171{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003172 TBasicType switchType = init->getBasicType();
3173 if ((switchType != EbtInt && switchType != EbtUInt) ||
3174 init->isMatrix() ||
3175 init->isArray() ||
3176 init->isVector())
3177 {
3178 error(init->getLine(), "init-expression in a switch statement must be a scalar integer", "switch");
3179 recover();
3180 return nullptr;
3181 }
3182
Olli Etuahoac5274d2015-02-20 10:19:08 +02003183 if (statementList)
3184 {
3185 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
3186 {
3187 recover();
3188 return nullptr;
3189 }
3190 }
3191
Olli Etuahoa3a36662015-02-17 13:46:51 +02003192 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3193 if (node == nullptr)
3194 {
3195 error(loc, "erroneous switch statement", "switch");
3196 recover();
3197 return nullptr;
3198 }
3199 return node;
3200}
3201
3202TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3203{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003204 if (mSwitchNestingLevel == 0)
3205 {
3206 error(loc, "case labels need to be inside switch statements", "case");
3207 recover();
3208 return nullptr;
3209 }
3210 if (condition == nullptr)
3211 {
3212 error(loc, "case label must have a condition", "case");
3213 recover();
3214 return nullptr;
3215 }
3216 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
3217 condition->isMatrix() ||
3218 condition->isArray() ||
3219 condition->isVector())
3220 {
3221 error(condition->getLine(), "case label must be a scalar integer", "case");
3222 recover();
3223 }
3224 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
3225 if (conditionConst == nullptr)
3226 {
3227 error(condition->getLine(), "case label must be constant", "case");
3228 recover();
3229 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003230 TIntermCase *node = intermediate.addCase(condition, loc);
3231 if (node == nullptr)
3232 {
3233 error(loc, "erroneous case statement", "case");
3234 recover();
3235 return nullptr;
3236 }
3237 return node;
3238}
3239
3240TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3241{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003242 if (mSwitchNestingLevel == 0)
3243 {
3244 error(loc, "default labels need to be inside switch statements", "default");
3245 recover();
3246 return nullptr;
3247 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003248 TIntermCase *node = intermediate.addCase(nullptr, loc);
3249 if (node == nullptr)
3250 {
3251 error(loc, "erroneous default statement", "default");
3252 recover();
3253 return nullptr;
3254 }
3255 return node;
3256}
3257
Olli Etuahof6c694b2015-03-26 14:50:53 +02003258TIntermTyped *TParseContext::createUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc,
3259 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003260{
3261 if (child == nullptr)
3262 {
3263 return nullptr;
3264 }
3265
3266 switch (op)
3267 {
3268 case EOpLogicalNot:
3269 if (child->getBasicType() != EbtBool ||
3270 child->isMatrix() ||
3271 child->isArray() ||
3272 child->isVector())
3273 {
3274 return nullptr;
3275 }
3276 break;
3277 case EOpBitwiseNot:
3278 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3279 child->isMatrix() ||
3280 child->isArray())
3281 {
3282 return nullptr;
3283 }
3284 break;
3285 case EOpPostIncrement:
3286 case EOpPreIncrement:
3287 case EOpPostDecrement:
3288 case EOpPreDecrement:
3289 case EOpNegative:
3290 case EOpPositive:
3291 if (child->getBasicType() == EbtStruct ||
Olli Etuahodca3e792015-03-26 13:24:04 +02003292 child->getBasicType() == EbtBool ||
Olli Etuaho69c11b52015-03-26 12:59:00 +02003293 child->isArray())
3294 {
3295 return nullptr;
3296 }
Olli Etuahodca3e792015-03-26 13:24:04 +02003297 // Operators for built-ins are already type checked against their prototype.
Olli Etuaho69c11b52015-03-26 12:59:00 +02003298 default:
3299 break;
3300 }
3301
Olli Etuahof6c694b2015-03-26 14:50:53 +02003302 return intermediate.addUnaryMath(op, child, loc, funcReturnType);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003303}
3304
Olli Etuaho09b22472015-02-11 11:47:26 +02003305TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3306{
Olli Etuahof6c694b2015-03-26 14:50:53 +02003307 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003308 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003309 {
3310 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
3311 recover();
3312 return child;
3313 }
3314 return node;
3315}
3316
3317TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3318{
3319 if (lValueErrorCheck(loc, GetOperatorString(op), child))
3320 recover();
3321 return addUnaryMath(op, child, loc);
3322}
3323
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003324bool TParseContext::binaryOpCommonCheck(TOperator op, TIntermTyped *left, TIntermTyped *right,
Olli Etuahod6b14282015-03-17 14:31:35 +02003325 const TSourceLoc &loc)
3326{
3327 if (left->isArray() || right->isArray())
3328 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003329 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003330 {
3331 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3332 return false;
3333 }
3334
3335 if (left->isArray() != right->isArray())
3336 {
3337 error(loc, "array / non-array mismatch", GetOperatorString(op));
3338 return false;
3339 }
3340
3341 switch (op)
3342 {
3343 case EOpEqual:
3344 case EOpNotEqual:
3345 case EOpAssign:
3346 case EOpInitialize:
3347 break;
3348 default:
3349 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3350 return false;
3351 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003352 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003353 if (left->getArraySize() != right->getArraySize())
3354 {
3355 error(loc, "array size mismatch", GetOperatorString(op));
3356 return false;
3357 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003358 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003359
3360 // Check ops which require integer / ivec parameters
3361 bool isBitShift = false;
3362 switch (op)
3363 {
3364 case EOpBitShiftLeft:
3365 case EOpBitShiftRight:
3366 case EOpBitShiftLeftAssign:
3367 case EOpBitShiftRightAssign:
3368 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3369 // check that the basic type is an integer type.
3370 isBitShift = true;
3371 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3372 {
3373 return false;
3374 }
3375 break;
3376 case EOpBitwiseAnd:
3377 case EOpBitwiseXor:
3378 case EOpBitwiseOr:
3379 case EOpBitwiseAndAssign:
3380 case EOpBitwiseXorAssign:
3381 case EOpBitwiseOrAssign:
3382 // It is enough to check the type of only one operand, since later it
3383 // is checked that the operand types match.
3384 if (!IsInteger(left->getBasicType()))
3385 {
3386 return false;
3387 }
3388 break;
3389 default:
3390 break;
3391 }
3392
3393 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3394 // So the basic type should usually match.
3395 if (!isBitShift && left->getBasicType() != right->getBasicType())
3396 {
3397 return false;
3398 }
3399
Olli Etuaho9dd217b2015-03-20 14:24:31 +02003400 // Check that type sizes match exactly on ops that require that.
Olli Etuahoff699002015-03-23 14:38:42 +02003401 // Also check restrictions for structs that contain arrays or samplers.
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003402 switch(op)
3403 {
3404 case EOpAssign:
3405 case EOpInitialize:
3406 case EOpEqual:
3407 case EOpNotEqual:
Olli Etuaho9dd217b2015-03-20 14:24:31 +02003408 // ESSL 1.00 sections 5.7, 5.8, 5.9
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003409 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
Olli Etuaho9dd217b2015-03-20 14:24:31 +02003410 {
3411 error(loc, "undefined operation for structs containing arrays", GetOperatorString(op));
3412 return false;
3413 }
Olli Etuahoff699002015-03-23 14:38:42 +02003414 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3415 // we interpret the spec so that this extends to structs containing samplers,
3416 // similarly to ESSL 1.00 spec.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003417 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
Olli Etuahoff699002015-03-23 14:38:42 +02003418 left->getType().isStructureContainingSamplers())
3419 {
3420 error(loc, "undefined operation for structs containing samplers", GetOperatorString(op));
3421 return false;
3422 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003423 case EOpLessThan:
3424 case EOpGreaterThan:
3425 case EOpLessThanEqual:
3426 case EOpGreaterThanEqual:
3427 if ((left->getNominalSize() != right->getNominalSize()) ||
3428 (left->getSecondarySize() != right->getSecondarySize()))
3429 {
3430 return false;
3431 }
3432 default:
3433 break;
3434 }
3435
Olli Etuahod6b14282015-03-17 14:31:35 +02003436 return true;
3437}
3438
Olli Etuahofc1806e2015-03-17 13:03:11 +02003439TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op, TIntermTyped *left, TIntermTyped *right,
3440 const TSourceLoc &loc)
3441{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003442 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003443 return nullptr;
3444
Olli Etuahofc1806e2015-03-17 13:03:11 +02003445 switch (op)
3446 {
3447 case EOpEqual:
3448 case EOpNotEqual:
Olli Etuahofc1806e2015-03-17 13:03:11 +02003449 break;
3450 case EOpLessThan:
3451 case EOpGreaterThan:
3452 case EOpLessThanEqual:
3453 case EOpGreaterThanEqual:
Olli Etuahod6b14282015-03-17 14:31:35 +02003454 ASSERT(!left->isArray() && !right->isArray());
3455 if (left->isMatrix() || left->isVector() ||
Olli Etuahofc1806e2015-03-17 13:03:11 +02003456 left->getBasicType() == EbtStruct)
3457 {
3458 return nullptr;
3459 }
3460 break;
3461 case EOpLogicalOr:
3462 case EOpLogicalXor:
3463 case EOpLogicalAnd:
Olli Etuahod6b14282015-03-17 14:31:35 +02003464 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003465 if (left->getBasicType() != EbtBool ||
Olli Etuahod6b14282015-03-17 14:31:35 +02003466 left->isMatrix() || left->isVector())
Olli Etuahofc1806e2015-03-17 13:03:11 +02003467 {
3468 return nullptr;
3469 }
3470 break;
3471 case EOpAdd:
3472 case EOpSub:
3473 case EOpDiv:
3474 case EOpMul:
Olli Etuahod6b14282015-03-17 14:31:35 +02003475 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003476 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool)
3477 {
3478 return nullptr;
3479 }
3480 break;
3481 case EOpIMod:
Olli Etuahod6b14282015-03-17 14:31:35 +02003482 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003483 // Note that this is only for the % operator, not for mod()
3484 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
3485 {
3486 return nullptr;
3487 }
3488 break;
3489 // Note that for bitwise ops, type checking is done in promote() to
3490 // share code between ops and compound assignment
3491 default:
3492 break;
3493 }
3494
Olli Etuahofc1806e2015-03-17 13:03:11 +02003495 return intermediate.addBinaryMath(op, left, right, loc);
3496}
3497
Olli Etuaho09b22472015-02-11 11:47:26 +02003498TIntermTyped *TParseContext::addBinaryMath(TOperator op, TIntermTyped *left, TIntermTyped *right,
3499 const TSourceLoc &loc)
3500{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003501 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003502 if (node == 0)
3503 {
3504 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(), right->getCompleteString());
3505 recover();
3506 return left;
3507 }
3508 return node;
3509}
3510
3511TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op, TIntermTyped *left, TIntermTyped *right,
3512 const TSourceLoc &loc)
3513{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003514 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003515 if (node == 0)
3516 {
3517 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(), right->getCompleteString());
3518 recover();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003519 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02003520 unionArray->setBConst(false);
3521 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), loc);
3522 }
3523 return node;
3524}
3525
Olli Etuahod6b14282015-03-17 14:31:35 +02003526TIntermTyped *TParseContext::createAssign(TOperator op, TIntermTyped *left, TIntermTyped *right,
3527 const TSourceLoc &loc)
3528{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003529 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003530 {
3531 return intermediate.addAssign(op, left, right, loc);
3532 }
3533 return nullptr;
3534}
3535
3536TIntermTyped *TParseContext::addAssign(TOperator op, TIntermTyped *left, TIntermTyped *right,
3537 const TSourceLoc &loc)
3538{
3539 TIntermTyped *node = createAssign(op, left, right, loc);
3540 if (node == nullptr)
3541 {
3542 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
3543 recover();
3544 return left;
3545 }
3546 return node;
3547}
3548
Olli Etuaho49300862015-02-20 14:54:49 +02003549TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3550{
3551 switch (op)
3552 {
3553 case EOpContinue:
3554 if (mLoopNestingLevel <= 0)
3555 {
3556 error(loc, "continue statement only allowed in loops", "");
3557 recover();
3558 }
3559 break;
3560 case EOpBreak:
Olli Etuaho53f076f2015-02-20 10:55:14 +02003561 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
Olli Etuaho49300862015-02-20 14:54:49 +02003562 {
Olli Etuaho53f076f2015-02-20 10:55:14 +02003563 error(loc, "break statement only allowed in loops and switch statements", "");
Olli Etuaho49300862015-02-20 14:54:49 +02003564 recover();
3565 }
3566 break;
3567 case EOpReturn:
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003568 if (mCurrentFunctionType->getBasicType() != EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02003569 {
3570 error(loc, "non-void function must return a value", "return");
3571 recover();
3572 }
3573 break;
3574 default:
3575 // No checks for discard
3576 break;
3577 }
3578 return intermediate.addBranch(op, loc);
3579}
3580
3581TIntermBranch *TParseContext::addBranch(TOperator op, TIntermTyped *returnValue, const TSourceLoc &loc)
3582{
3583 ASSERT(op == EOpReturn);
3584 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003585 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02003586 {
3587 error(loc, "void function cannot return a value", "return");
3588 recover();
3589 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003590 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02003591 {
3592 error(loc, "function return is not matching type:", "return");
3593 recover();
3594 }
3595 return intermediate.addBranch(op, returnValue, loc);
3596}
3597
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003598TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall, TIntermNode *paramNode, TIntermNode *thisNode,
3599 const TSourceLoc &loc, bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003600{
3601 *fatalError = false;
3602 TOperator op = fnCall->getBuiltInOp();
3603 TIntermTyped *callNode = nullptr;
3604
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003605 if (thisNode != nullptr)
3606 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003607 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho96e67382015-04-23 14:27:02 +03003608 int arraySize = 0;
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003609 TIntermTyped *typedThis = thisNode->getAsTyped();
3610 if (fnCall->getName() != "length")
3611 {
3612 error(loc, "invalid method", fnCall->getName().c_str());
3613 recover();
3614 }
3615 else if (paramNode != nullptr)
3616 {
3617 error(loc, "method takes no parameters", "length");
3618 recover();
3619 }
3620 else if (typedThis == nullptr || !typedThis->isArray())
3621 {
3622 error(loc, "length can only be called on arrays", "length");
3623 recover();
3624 }
3625 else
3626 {
Olli Etuaho96e67382015-04-23 14:27:02 +03003627 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03003628 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003629 {
Olli Etuaho39282e12015-04-23 15:41:48 +03003630 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003631 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03003632 // (func()).length()
3633 // (int[3](0, 1, 2)).length()
3634 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid expression.
3635 // It allows "An array name with the length method applied" in contrast to GLSL 4.4 spec section 5.9
3636 // which allows "An array, vector or matrix expression with the length method applied".
3637 error(loc, "length can only be called on array names, not on array expressions", "length");
3638 recover();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003639 }
3640 }
Olli Etuaho96e67382015-04-23 14:27:02 +03003641 unionArray->setIConst(arraySize);
3642 callNode = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003643 }
3644 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003645 {
3646 //
3647 // Then this should be a constructor.
3648 // Don't go through the symbol table for constructors.
3649 // Their parameters will be verified algorithmically.
3650 //
3651 TType type(EbtVoid, EbpUndefined); // use this to get the type back
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003652 if (!constructorErrorCheck(loc, paramNode, *fnCall, op, &type))
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003653 {
3654 //
3655 // It's a constructor, of type 'type'.
3656 //
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003657 callNode = addConstructor(paramNode, &type, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003658 }
Olli Etuaho72ba85b2015-03-04 14:23:26 +02003659
3660 if (callNode == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003661 {
3662 recover();
3663 callNode = intermediate.setAggregateOperator(nullptr, op, loc);
3664 }
3665 callNode->setType(type);
3666 }
3667 else
3668 {
3669 //
3670 // Not a constructor. Find it in the symbol table.
3671 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303672 const TFunction *fnCandidate;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003673 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003674 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003675 if (fnCandidate)
3676 {
3677 //
3678 // A declared function.
3679 //
3680 if (builtIn && !fnCandidate->getExtension().empty() &&
3681 extensionErrorCheck(loc, fnCandidate->getExtension()))
3682 {
3683 recover();
3684 }
3685 op = fnCandidate->getBuiltInOp();
3686 if (builtIn && op != EOpNull)
3687 {
3688 //
3689 // A function call mapped to a built-in operation.
3690 //
3691 if (fnCandidate->getParamCount() == 1)
3692 {
3693 //
3694 // Treat it like a built-in unary operator.
3695 //
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003696 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc, &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003697 if (callNode == nullptr)
3698 {
3699 std::stringstream extraInfoStream;
3700 extraInfoStream << "built in unary operator function. Type: "
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003701 << static_cast<TIntermTyped*>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003702 std::string extraInfo = extraInfoStream.str();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003703 error(paramNode->getLine(), " wrong operand type", "Internal Error", extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003704 *fatalError = true;
3705 return nullptr;
3706 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003707 }
3708 else
3709 {
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003710 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003711 aggregate->setType(fnCandidate->getReturnType());
3712 aggregate->setPrecisionFromChildren();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003713
3714 // Some built-in functions have out parameters too.
3715 functionCallLValueErrorCheck(fnCandidate, aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05303716
3717 // See if we can constant fold a built-in.
Olli Etuahob43846e2015-06-02 18:18:57 +03003718 TIntermTyped *foldedNode = intermediate.foldAggregateBuiltIn(aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05303719 if (foldedNode)
3720 {
Arun Patole274f0702015-05-05 13:33:30 +05303721 callNode = foldedNode;
3722 }
Olli Etuahob43846e2015-06-02 18:18:57 +03003723 else
3724 {
3725 callNode = aggregate;
3726 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003727 }
3728 }
3729 else
3730 {
3731 // This is a real function call
3732
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003733 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003734 aggregate->setType(fnCandidate->getReturnType());
3735
3736 // this is how we know whether the given function is a builtIn function or a user defined function
3737 // if builtIn == false, it's a userDefined -> could be an overloaded builtIn function also
3738 // if builtIn == true, it's definitely a builtIn function with EOpNull
3739 if (!builtIn)
3740 aggregate->setUserDefined();
3741 aggregate->setName(fnCandidate->getMangledName());
Corentin Wallez71d147f2015-02-11 11:15:24 -08003742 aggregate->setFunctionId(fnCandidate->getUniqueId());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003743
3744 // This needs to happen after the name is set
3745 if (builtIn)
3746 aggregate->setBuiltInFunctionPrecision();
3747
3748 callNode = aggregate;
3749
3750 functionCallLValueErrorCheck(fnCandidate, aggregate);
3751 }
3752 }
3753 else
3754 {
3755 // error message was put out by findFunction()
3756 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003757 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003758 unionArray->setFConst(0.0f);
3759 callNode = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpUndefined, EvqConst), loc);
3760 recover();
3761 }
3762 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003763 return callNode;
3764}
3765
Olli Etuaho52901742015-04-15 13:42:45 +03003766TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond, TIntermTyped *trueBlock, TIntermTyped *falseBlock,
3767 const TSourceLoc &loc)
3768{
3769 if (boolErrorCheck(loc, cond))
3770 recover();
3771
3772 if (trueBlock->getType() != falseBlock->getType())
3773 {
3774 binaryOpError(loc, ":", trueBlock->getCompleteString(), falseBlock->getCompleteString());
3775 recover();
3776 return falseBlock;
3777 }
Olli Etuahoa2d53032015-04-15 14:14:44 +03003778 // ESSL1 sections 5.2 and 5.7:
3779 // ESSL3 section 5.7:
3780 // Ternary operator is not among the operators allowed for structures/arrays.
3781 if (trueBlock->isArray() || trueBlock->getBasicType() == EbtStruct)
3782 {
3783 error(loc, "ternary operator is not allowed for structures or arrays", ":");
3784 recover();
3785 return falseBlock;
3786 }
Olli Etuaho52901742015-04-15 13:42:45 +03003787 return intermediate.addSelection(cond, trueBlock, falseBlock, loc);
3788}
Olli Etuaho49300862015-02-20 14:54:49 +02003789
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003790//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003791// Parse an array of strings using yyparse.
3792//
3793// Returns 0 for success.
3794//
Arun Patole7e7e68d2015-05-22 12:02:25 +05303795int PaParseStrings(size_t count, const char *const string[], const int length[],
3796 TParseContext *context)
3797{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003798 if ((count == 0) || (string == NULL))
3799 return 1;
3800
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003801 if (glslang_initialize(context))
3802 return 1;
3803
alokp@chromium.org408c45e2012-04-05 15:54:43 +00003804 int error = glslang_scan(count, string, length, context);
3805 if (!error)
3806 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003807
alokp@chromium.org73bc2982012-06-19 18:48:05 +00003808 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00003809
alokp@chromium.org6b495712012-06-29 00:06:58 +00003810 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003811}
3812
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003813
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00003814