blob: f12ecc1b6cd8be4caaf9898e52cdad7709bf6e1b [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"
Olli Etuahoac5274d2015-02-20 10:19:08 +020013#include "compiler/translator/glslang.h"
14#include "compiler/translator/ValidateSwitch.h"
Olli Etuahob0c645e2015-05-12 14:25:36 +030015#include "compiler/translator/ValidateGlobalInitializer.h"
Olli Etuaho37ad4742015-04-27 13:18:50 +030016#include "compiler/translator/util.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000017
alokp@chromium.org8b851c62012-06-15 16:25:11 +000018///////////////////////////////////////////////////////////////////////
19//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000020// Sub- vector and matrix fields
21//
22////////////////////////////////////////////////////////////////////////
23
24//
25// Look at a '.' field selector string and change it into offsets
26// for a vector.
27//
Arun Patole7e7e68d2015-05-22 12:02:25 +053028bool TParseContext::parseVectorFields(const TString &compString, int vecSize, TVectorFields &fields,
29 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000030{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000031 fields.num = (int) compString.size();
Arun Patole7e7e68d2015-05-22 12:02:25 +053032 if (fields.num > 4)
33 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000034 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000035 return false;
36 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000037
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000038 enum {
39 exyzw,
40 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +000041 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000042 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000043
Arun Patole7e7e68d2015-05-22 12:02:25 +053044 for (int i = 0; i < fields.num; ++i)
45 {
46 switch (compString[i])
47 {
48 case 'x':
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000049 fields.offsets[i] = 0;
50 fieldSet[i] = exyzw;
51 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053052 case 'r':
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000053 fields.offsets[i] = 0;
54 fieldSet[i] = ergba;
55 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053056 case 's':
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000057 fields.offsets[i] = 0;
58 fieldSet[i] = estpq;
59 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053060 case 'y':
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000061 fields.offsets[i] = 1;
62 fieldSet[i] = exyzw;
63 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053064 case 'g':
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000065 fields.offsets[i] = 1;
66 fieldSet[i] = ergba;
67 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053068 case 't':
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000069 fields.offsets[i] = 1;
70 fieldSet[i] = estpq;
71 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053072 case 'z':
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000073 fields.offsets[i] = 2;
74 fieldSet[i] = exyzw;
75 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053076 case 'b':
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000077 fields.offsets[i] = 2;
78 fieldSet[i] = ergba;
79 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053080 case 'p':
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000081 fields.offsets[i] = 2;
82 fieldSet[i] = estpq;
83 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053084
85 case 'w':
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000086 fields.offsets[i] = 3;
87 fieldSet[i] = exyzw;
88 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053089 case 'a':
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000090 fields.offsets[i] = 3;
91 fieldSet[i] = ergba;
92 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053093 case 'q':
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000094 fields.offsets[i] = 3;
95 fieldSet[i] = estpq;
96 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +053097 default:
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +000098 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +000099 return false;
100 }
101 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000102
Arun Patole7e7e68d2015-05-22 12:02:25 +0530103 for (int i = 0; i < fields.num; ++i)
104 {
105 if (fields.offsets[i] >= vecSize)
106 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000107 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000108 return false;
109 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000110
Arun Patole7e7e68d2015-05-22 12:02:25 +0530111 if (i > 0)
112 {
113 if (fieldSet[i] != fieldSet[i-1])
114 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000115 error(line, "illegal - vector component fields not from the same set", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000116 return false;
117 }
118 }
119 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000120
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000121 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000122}
123
124
125//
126// Look at a '.' field selector string and change it into offsets
127// for a matrix.
128//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530129bool TParseContext::parseMatrixFields(const TString &compString, int matCols, int matRows, TMatrixFields &fields,
130 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000131{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000132 fields.wholeRow = false;
133 fields.wholeCol = false;
134 fields.row = -1;
135 fields.col = -1;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000136
Arun Patole7e7e68d2015-05-22 12:02:25 +0530137 if (compString.size() != 2)
138 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000139 error(line, "illegal length of matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000140 return false;
141 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000142
Arun Patole7e7e68d2015-05-22 12:02:25 +0530143 if (compString[0] == '_')
144 {
145 if (compString[1] < '0' || compString[1] > '3')
146 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000147 error(line, "illegal matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000148 return false;
149 }
150 fields.wholeCol = true;
151 fields.col = compString[1] - '0';
Arun Patole7e7e68d2015-05-22 12:02:25 +0530152 }
153 else if (compString[1] == '_')
154 {
155 if (compString[0] < '0' || compString[0] > '3')
156 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000157 error(line, "illegal matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000158 return false;
159 }
160 fields.wholeRow = true;
161 fields.row = compString[0] - '0';
Arun Patole7e7e68d2015-05-22 12:02:25 +0530162 }
163 else
164 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000165 if (compString[0] < '0' || compString[0] > '3' ||
Arun Patole7e7e68d2015-05-22 12:02:25 +0530166 compString[1] < '0' || compString[1] > '3')
167 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000168 error(line, "illegal matrix field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000169 return false;
170 }
171 fields.row = compString[0] - '0';
172 fields.col = compString[1] - '0';
173 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000174
Arun Patole7e7e68d2015-05-22 12:02:25 +0530175 if (fields.row >= matRows || fields.col >= matCols)
176 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000177 error(line, "matrix field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000178 return false;
179 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000180
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000181 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000182}
183
184///////////////////////////////////////////////////////////////////////
185//
186// Errors
187//
188////////////////////////////////////////////////////////////////////////
189
190//
191// Track whether errors have occurred.
192//
193void TParseContext::recover()
194{
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000195}
196
197//
198// Used by flex/bison to output all syntax and parsing errors.
199//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530200void TParseContext::error(const TSourceLoc &loc,
201 const char *reason, const char *token,
202 const char *extraInfo)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000203{
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000204 pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -0400205 srcLoc.file = loc.first_file;
206 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400207 mDiagnostics.writeInfo(pp::Diagnostics::PP_ERROR,
208 srcLoc, reason, token, extraInfo);
alokp@chromium.orgff42c632010-05-10 15:14:30 +0000209
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000210}
211
Arun Patole7e7e68d2015-05-22 12:02:25 +0530212void TParseContext::warning(const TSourceLoc &loc,
213 const char *reason, const char *token,
214 const char *extraInfo)
215{
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000216 pp::SourceLocation srcLoc;
Jamie Madill075edd82013-07-08 13:30:19 -0400217 srcLoc.file = loc.first_file;
218 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400219 mDiagnostics.writeInfo(pp::Diagnostics::PP_WARNING,
220 srcLoc, reason, token, extraInfo);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000221}
222
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000223//
224// Same error message for all places assignments don't work.
225//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530226void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000227{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000228 std::stringstream extraInfoStream;
229 extraInfoStream << "cannot convert from '" << right << "' to '" << left << "'";
230 std::string extraInfo = extraInfoStream.str();
231 error(line, "", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000232}
233
234//
235// Same error message for all places unary operations don't work.
236//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530237void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000238{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000239 std::stringstream extraInfoStream;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530240 extraInfoStream << "no operation '" << op << "' exists that takes an operand of type " << operand
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000241 << " (or there is no acceptable conversion)";
242 std::string extraInfo = extraInfoStream.str();
243 error(line, " wrong operand type", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000244}
245
246//
247// Same error message for all binary operations don't work.
248//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530249void TParseContext::binaryOpError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000250{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000251 std::stringstream extraInfoStream;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530252 extraInfoStream << "no operation '" << op << "' exists that takes a left-hand operand of type '" << left
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000253 << "' and a right operand of type '" << right << "' (or there is no acceptable conversion)";
254 std::string extraInfo = extraInfoStream.str();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530255 error(line, " wrong operand types ", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000256}
257
Arun Patole7e7e68d2015-05-22 12:02:25 +0530258bool TParseContext::precisionErrorCheck(const TSourceLoc &line, TPrecision precision, TBasicType type)
259{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400260 if (!mChecksPrecisionErrors)
zmo@google.comdc4b4f82011-06-17 00:42:53 +0000261 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530262 switch(type)
263 {
264 case EbtFloat:
265 if( precision == EbpUndefined )
266 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000267 error( line, "No precision specified for (float)", "" );
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000268 return true;
269 }
270 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530271 case EbtInt:
272 if( precision == EbpUndefined )
273 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000274 error( line, "No precision specified (int)", "" );
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000275 return true;
276 }
277 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530278 default:
daniel@transgaming.com0eb64c32011-03-15 18:23:51 +0000279 return false;
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000280 }
281 return false;
282}
283
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000284//
285// Both test and if necessary, spit out an error, to see if the node is really
286// an l-value that can be operated on this way.
287//
288// Returns true if the was an error.
289//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530290bool TParseContext::lValueErrorCheck(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000291{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530292 TIntermSymbol *symNode = node->getAsSymbolNode();
293 TIntermBinary *binaryNode = node->getAsBinaryNode();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000294
Arun Patole7e7e68d2015-05-22 12:02:25 +0530295 if (binaryNode)
296 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000297 bool errorReturn;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000298
Arun Patole7e7e68d2015-05-22 12:02:25 +0530299 switch(binaryNode->getOp())
300 {
301 case EOpIndexDirect:
302 case EOpIndexIndirect:
303 case EOpIndexDirectStruct:
304 case EOpIndexDirectInterfaceBlock:
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000305 return lValueErrorCheck(line, op, binaryNode->getLeft());
Arun Patole7e7e68d2015-05-22 12:02:25 +0530306 case EOpVectorSwizzle:
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000307 errorReturn = lValueErrorCheck(line, op, binaryNode->getLeft());
Arun Patole7e7e68d2015-05-22 12:02:25 +0530308 if (!errorReturn)
309 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000310 int offset[4] = {0,0,0,0};
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000311
Arun Patole7e7e68d2015-05-22 12:02:25 +0530312 TIntermTyped *rightNode = binaryNode->getRight();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000313 TIntermAggregate *aggrNode = rightNode->getAsAggregate();
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700314
315 for (TIntermSequence::iterator p = aggrNode->getSequence()->begin();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530316 p != aggrNode->getSequence()->end(); p++)
317 {
shannon.woods%transgaming.com@gtempaccount.comc0d0c222013-04-13 03:29:36 +0000318 int value = (*p)->getAsTyped()->getAsConstantUnion()->getIConst(0);
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700319 offset[value]++;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530320 if (offset[value] > 1)
321 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000322 error(line, " l-value of swizzle cannot have duplicate components", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000323
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000324 return true;
325 }
326 }
Zhenyao Moe40d1e92014-07-16 17:40:36 -0700327 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000328
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000329 return errorReturn;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530330 default:
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000331 break;
332 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000333 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000334
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000335 return true;
336 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000337
338
Arun Patole7e7e68d2015-05-22 12:02:25 +0530339 const char *symbol = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000340 if (symNode != 0)
341 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000342
Arun Patole7e7e68d2015-05-22 12:02:25 +0530343 const char *message = 0;
344 switch (node->getQualifier())
345 {
346 case EvqConst:
347 message = "can't modify a const";
348 break;
349 case EvqConstReadOnly:
350 message = "can't modify a const";
351 break;
352 case EvqAttribute:
353 message = "can't modify an attribute";
354 break;
355 case EvqFragmentIn:
356 message = "can't modify an input";
357 break;
358 case EvqVertexIn:
359 message = "can't modify an input";
360 break;
361 case EvqUniform:
362 message = "can't modify a uniform";
363 break;
364 case EvqVaryingIn:
365 message = "can't modify a varying";
366 break;
367 case EvqFragCoord:
368 message = "can't modify gl_FragCoord";
369 break;
370 case EvqFrontFacing:
371 message = "can't modify gl_FrontFacing";
372 break;
373 case EvqPointCoord:
374 message = "can't modify gl_PointCoord";
375 break;
376 default:
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000377 //
378 // Type that can't be written to?
379 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530380 if (node->getBasicType() == EbtVoid)
381 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000382 message = "can't modify void";
Nicolas Capens344e7142013-06-24 15:39:21 -0400383 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530384 if (IsSampler(node->getBasicType()))
385 {
Nicolas Capens344e7142013-06-24 15:39:21 -0400386 message = "can't modify a sampler";
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000387 }
388 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000389
Arun Patole7e7e68d2015-05-22 12:02:25 +0530390 if (message == 0 && binaryNode == 0 && symNode == 0)
391 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000392 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000393
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000394 return true;
395 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000396
397
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000398 //
399 // Everything else is okay, no error.
400 //
401 if (message == 0)
402 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000403
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000404 //
405 // If we get here, we have an error and a message.
406 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530407 if (symNode)
408 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000409 std::stringstream extraInfoStream;
410 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
411 std::string extraInfo = extraInfoStream.str();
412 error(line, " l-value required", op, extraInfo.c_str());
413 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530414 else
415 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000416 std::stringstream extraInfoStream;
417 extraInfoStream << "(" << message << ")";
418 std::string extraInfo = extraInfoStream.str();
419 error(line, " l-value required", op, extraInfo.c_str());
420 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000421
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000422 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000423}
424
425//
426// Both test, and if necessary spit out an error, to see if the node is really
427// a constant.
428//
429// Returns true if the was an error.
430//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530431bool TParseContext::constErrorCheck(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000432{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000433 if (node->getQualifier() == EvqConst)
434 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000435
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000436 error(node->getLine(), "constant expression required", "");
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000437
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000438 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000439}
440
441//
442// Both test, and if necessary spit out an error, to see if the node is really
443// an integer.
444//
445// Returns true if the was an error.
446//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530447bool TParseContext::integerErrorCheck(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000448{
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000449 if (node->isScalarInt())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000450 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000451
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000452 error(node->getLine(), "integer expression required", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000453
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000454 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000455}
456
457//
458// Both test, and if necessary spit out an error, to see if we are currently
459// globally scoped.
460//
461// Returns true if the was an error.
462//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530463bool TParseContext::globalErrorCheck(const TSourceLoc &line, bool global, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000464{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000465 if (global)
466 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000467
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000468 error(line, "only allowed at global scope", token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000469
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000470 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000471}
472
473//
474// For now, keep it simple: if it starts "gl_", it's reserved, independent
475// of scope. Except, if the symbol table is at the built-in push-level,
476// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000477// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
478// webgl shader.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000479//
480// Returns true if there was an error.
481//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530482bool TParseContext::reservedErrorCheck(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000483{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530484 static const char *reservedErrMsg = "reserved built-in name";
485 if (!symbolTable.atBuiltInLevel())
486 {
487 if (identifier.compare(0, 3, "gl_") == 0)
488 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000489 error(line, reservedErrMsg, "gl_");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000490 return true;
491 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530492 if (IsWebGLBasedSpec(mShaderSpec))
493 {
494 if (identifier.compare(0, 6, "webgl_") == 0)
495 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000496 error(line, reservedErrMsg, "webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000497 return true;
498 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530499 if (identifier.compare(0, 7, "_webgl_") == 0)
500 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000501 error(line, reservedErrMsg, "_webgl_");
alokp@chromium.org613ef312010-07-21 18:54:22 +0000502 return true;
503 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530504 if (mShaderSpec == SH_CSS_SHADERS_SPEC && identifier.compare(0, 4, "css_") == 0)
505 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000506 error(line, reservedErrMsg, "css_");
maxvujovic@gmail.com430f5e02012-06-08 17:47:59 +0000507 return true;
508 }
alokp@chromium.org613ef312010-07-21 18:54:22 +0000509 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530510 if (identifier.find("__") != TString::npos)
511 {
512 error(line,
513 "identifiers containing two consecutive underscores (__) are reserved as possible future keywords",
514 identifier.c_str());
daniel@transgaming.combeadd5d2012-04-12 02:35:31 +0000515 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000516 }
517 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000518
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000519 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000520}
521
522//
523// Make sure there is enough data provided to the constructor to build
524// something of the type of the constructor. Also returns the type of
525// the constructor.
526//
527// Returns true if there was an error in construction.
528//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530529bool TParseContext::constructorErrorCheck(const TSourceLoc &line, TIntermNode *node, TFunction &function, TOperator op,
530 TType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000531{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000532 *type = function.getReturnType();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000533
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000534 bool constructingMatrix = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530535 switch(op)
536 {
537 case EOpConstructMat2:
Alexis Hetu07e57df2015-06-16 16:55:52 -0400538 case EOpConstructMat2x3:
539 case EOpConstructMat2x4:
540 case EOpConstructMat3x2:
Arun Patole7e7e68d2015-05-22 12:02:25 +0530541 case EOpConstructMat3:
Alexis Hetu07e57df2015-06-16 16:55:52 -0400542 case EOpConstructMat3x4:
543 case EOpConstructMat4x2:
544 case EOpConstructMat4x3:
Arun Patole7e7e68d2015-05-22 12:02:25 +0530545 case EOpConstructMat4:
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000546 constructingMatrix = true;
547 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530548 default:
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000549 break;
550 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000551
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000552 //
553 // Note: It's okay to have too many components available, but not okay to have unused
554 // arguments. 'full' will go to true when enough args have been seen. If we loop
555 // again, there is an extra argument, so 'overfull' will become true.
556 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000557
Jamie Madill94bf7f22013-07-08 13:31:15 -0400558 size_t size = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000559 bool constType = true;
560 bool full = false;
561 bool overFull = false;
562 bool matrixInMatrix = false;
563 bool arrayArg = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530564 for (size_t i = 0; i < function.getParamCount(); ++i)
565 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700566 const TConstParameter &param = function.getParam(i);
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000567 size += param.type->getObjectSize();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530568
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000569 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000570 matrixInMatrix = true;
571 if (full)
572 overFull = true;
573 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
574 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000575 if (param.type->getQualifier() != EvqConst)
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000576 constType = false;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000577 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000578 arrayArg = true;
579 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530580
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000581 if (constType)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000582 type->setQualifier(EvqConst);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000583
Olli Etuaho376f1b52015-04-13 13:23:41 +0300584 if (type->isArray())
585 {
586 if (type->isUnsizedArray())
587 {
588 type->setArraySize(function.getParamCount());
589 }
590 else if (static_cast<size_t>(type->getArraySize()) != function.getParamCount())
591 {
592 error(line, "array constructor needs one argument per array element", "constructor");
593 return true;
594 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000595 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000596
Arun Patole7e7e68d2015-05-22 12:02:25 +0530597 if (arrayArg && op != EOpConstructStruct)
598 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000599 error(line, "constructing from a non-dereferenced array", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000600 return true;
601 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000602
Arun Patole7e7e68d2015-05-22 12:02:25 +0530603 if (matrixInMatrix && !type->isArray())
604 {
605 if (function.getParamCount() != 1)
606 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000607 error(line, "constructing matrix from matrix can only take one argument", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000608 return true;
609 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000610 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000611
Arun Patole7e7e68d2015-05-22 12:02:25 +0530612 if (overFull)
613 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000614 error(line, "too many arguments", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000615 return true;
616 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530617
618 if (op == EOpConstructStruct && !type->isArray() && type->getStruct()->fields().size() != function.getParamCount())
619 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000620 error(line, "Number of constructor parameters does not match the number of structure fields", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000621 return true;
622 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000623
Arun Patole7e7e68d2015-05-22 12:02:25 +0530624 if (!type->isMatrix() || !matrixInMatrix)
625 {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000626 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
Arun Patole7e7e68d2015-05-22 12:02:25 +0530627 (op == EOpConstructStruct && size < type->getObjectSize()))
628 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000629 error(line, "not enough data provided for construction", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000630 return true;
631 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000632 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000633
daniel@transgaming.com0b53fc02011-03-09 15:12:12 +0000634 TIntermTyped *typed = node ? node->getAsTyped() : 0;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530635 if (typed == 0)
636 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000637 error(line, "constructor argument does not have a type", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000638 return true;
639 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530640 if (op != EOpConstructStruct && IsSampler(typed->getBasicType()))
641 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000642 error(line, "cannot convert a sampler", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000643 return true;
644 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530645 if (typed->getBasicType() == EbtVoid)
646 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000647 error(line, "cannot convert a void", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000648 return true;
649 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000650
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000651 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000652}
653
654// This function checks to see if a void variable has been declared and raise an error message for such a case
655//
656// returns true in case of an error
657//
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300658bool TParseContext::voidErrorCheck(const TSourceLoc &line, const TString &identifier, const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000659{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300660 if (type == EbtVoid)
661 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000662 error(line, "illegal use of type 'void'", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000663 return true;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300664 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000665
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000666 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000667}
668
669// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
670//
671// returns true in case of an error
672//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530673bool TParseContext::boolErrorCheck(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000674{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530675 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector())
676 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000677 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000678 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530679 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000680
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000681 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000682}
683
684// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
685//
686// returns true in case of an error
687//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530688bool TParseContext::boolErrorCheck(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000689{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530690 if (pType.type != EbtBool || pType.isAggregate())
691 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000692 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000693 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530694 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000695
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000696 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000697}
698
Arun Patole7e7e68d2015-05-22 12:02:25 +0530699bool TParseContext::samplerErrorCheck(const TSourceLoc &line, const TPublicType &pType, const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000700{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530701 if (pType.type == EbtStruct)
702 {
703 if (containsSampler(*pType.userDef))
704 {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000705 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530706
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000707 return true;
708 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530709
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000710 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530711 }
712 else if (IsSampler(pType.type))
713 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000714 error(line, reason, getBasicString(pType.type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000715
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000716 return true;
717 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000718
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000719 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000720}
721
Arun Patole7e7e68d2015-05-22 12:02:25 +0530722bool TParseContext::locationDeclaratorListCheck(const TSourceLoc &line, const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400723{
724 if (pType.layoutQualifier.location != -1)
725 {
726 error(line, "location must only be specified for a single input or output variable", "location");
727 return true;
728 }
729
730 return false;
731}
732
Arun Patole7e7e68d2015-05-22 12:02:25 +0530733bool TParseContext::parameterSamplerErrorCheck(const TSourceLoc &line, TQualifier qualifier, const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000734{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530735 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
736 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType()))
737 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000738 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000739 return true;
740 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000741
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000742 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000743}
744
Arun Patole7e7e68d2015-05-22 12:02:25 +0530745bool TParseContext::containsSampler(const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000746{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000747 if (IsSampler(type.getBasicType()))
748 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000749
Arun Patole7e7e68d2015-05-22 12:02:25 +0530750 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock())
751 {
752 const TFieldList &fields = type.getStruct()->fields();
753 for (unsigned int i = 0; i < fields.size(); ++i)
754 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400755 if (containsSampler(*fields[i]->type()))
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000756 return true;
757 }
758 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000759
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000760 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000761}
762
763//
764// Do size checking for an array type's size.
765//
766// Returns true if there was an error.
767//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530768bool TParseContext::arraySizeErrorCheck(const TSourceLoc &line, TIntermTyped *expr, int &size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000769{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530770 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000771
Olli Etuahoe7847b02015-03-16 11:56:12 +0200772 if (constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000773 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000774 error(line, "array size must be a constant integer expression", "");
Olli Etuahoe7847b02015-03-16 11:56:12 +0200775 size = 1;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000776 return true;
777 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000778
Nicolas Capens906744a2014-06-06 15:18:07 -0400779 unsigned int unsignedSize = 0;
780
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000781 if (constant->getBasicType() == EbtUInt)
782 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400783 unsignedSize = constant->getUConst(0);
784 size = static_cast<int>(unsignedSize);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000785 }
786 else
787 {
788 size = constant->getIConst(0);
789
Nicolas Capens906744a2014-06-06 15:18:07 -0400790 if (size < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000791 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400792 error(line, "array size must be non-negative", "");
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000793 size = 1;
794 return true;
795 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400796
797 unsignedSize = static_cast<unsigned int>(size);
798 }
799
800 if (size == 0)
801 {
802 error(line, "array size must be greater than zero", "");
803 size = 1;
804 return true;
805 }
806
807 // The size of arrays is restricted here to prevent issues further down the
808 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
809 // 4096 registers so this should be reasonable even for aggressively optimizable code.
810 const unsigned int sizeLimit = 65536;
811
812 if (unsignedSize > sizeLimit)
813 {
814 error(line, "array size too large", "");
815 size = 1;
816 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000817 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000818
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000819 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000820}
821
822//
823// See if this qualifier can be an array.
824//
825// Returns true if there is an error.
826//
Olli Etuaho3739d232015-04-08 12:23:44 +0300827bool TParseContext::arrayQualifierErrorCheck(const TSourceLoc &line, const TPublicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000828{
Olli Etuaho3739d232015-04-08 12:23:44 +0300829 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqVertexIn) ||
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400830 (type.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300831 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000832 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000833 return true;
834 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000835
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000836 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000837}
838
839//
840// See if this type can be an array.
841//
842// Returns true if there is an error.
843//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530844bool TParseContext::arrayTypeErrorCheck(const TSourceLoc &line, const TPublicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000845{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000846 //
847 // Can the type be an array?
848 //
Jamie Madill06145232015-05-13 13:10:01 -0400849 if (type.array)
850 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000851 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000852 return true;
853 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000854
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000855 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000856}
857
858//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000859// Enforce non-initializer type/qualifier rules.
860//
861// Returns true if there was an error.
862//
Olli Etuaho376f1b52015-04-13 13:23:41 +0300863bool TParseContext::nonInitErrorCheck(const TSourceLoc &line, const TString &identifier, TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000864{
Olli Etuaho3739d232015-04-08 12:23:44 +0300865 ASSERT(type != nullptr);
866 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000867 {
868 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300869 type->qualifier = EvqTemporary;
870
871 // Generate informative error messages for ESSL1.
872 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400873 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000874 {
Arun Patole7e7e68d2015-05-22 12:02:25 +0530875 error(line,
876 "structures containing arrays may not be declared constant since they cannot be initialized",
877 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000878 }
879 else
880 {
881 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
882 }
883
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000884 return true;
885 }
Olli Etuaho376f1b52015-04-13 13:23:41 +0300886 if (type->isUnsizedArray())
887 {
888 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
889 return true;
890 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000891 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000892}
893
Olli Etuaho2935c582015-04-08 14:32:06 +0300894// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000895// and update the symbol table.
896//
Olli Etuaho2935c582015-04-08 14:32:06 +0300897// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000898//
Olli Etuaho2935c582015-04-08 14:32:06 +0300899bool TParseContext::declareVariable(const TSourceLoc &line, const TString &identifier, const TType &type,
900 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000901{
Olli Etuaho2935c582015-04-08 14:32:06 +0300902 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000903
Olli Etuaho2935c582015-04-08 14:32:06 +0300904 bool needsReservedErrorCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000905
Olli Etuaho2935c582015-04-08 14:32:06 +0300906 // gl_LastFragData may be redeclared with a new precision qualifier
907 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
908 {
909 const TVariable *maxDrawBuffers =
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400910 static_cast<const TVariable *>(symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho2935c582015-04-08 14:32:06 +0300911 if (type.getArraySize() == maxDrawBuffers->getConstPointer()->getIConst())
912 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400913 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +0300914 {
915 needsReservedErrorCheck = extensionErrorCheck(line, builtInSymbol->getExtension());
916 }
917 }
918 else
919 {
920 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers", identifier.c_str());
921 return false;
922 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000923 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000924
Olli Etuaho2935c582015-04-08 14:32:06 +0300925 if (needsReservedErrorCheck && reservedErrorCheck(line, identifier))
926 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000927
Olli Etuaho2935c582015-04-08 14:32:06 +0300928 (*variable) = new TVariable(&identifier, type);
929 if (!symbolTable.declare(*variable))
930 {
931 error(line, "redefinition", identifier.c_str());
932 delete (*variable);
933 (*variable) = nullptr;
934 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
1354 if (typeSpecifier.array)
1355 {
1356 error(typeSpecifier.line, "not supported", "first-class array");
1357 recover();
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001358 returnType.clearArrayness();
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001359 }
1360
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001361 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001362 {
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 {
Jamie Madillb120eac2013-06-12 14:08:13 -04001378 switch (qualifier)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001379 {
Jamie Madill19571812013-08-12 15:26:34 -07001380 case EvqSmoothIn:
1381 case EvqSmoothOut:
1382 case EvqVertexOut:
1383 case EvqFragmentIn:
1384 case EvqCentroidOut:
1385 case EvqCentroidIn:
1386 if (typeSpecifier.type == EbtBool)
1387 {
1388 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1389 recover();
1390 }
1391 if (typeSpecifier.type == EbtInt || typeSpecifier.type == EbtUInt)
1392 {
1393 error(typeSpecifier.line, "must use 'flat' interpolation here", getQualifierString(qualifier));
1394 recover();
1395 }
1396 break;
1397
1398 case EvqVertexIn:
1399 case EvqFragmentOut:
1400 case EvqFlatIn:
1401 case EvqFlatOut:
Jamie Madillb120eac2013-06-12 14:08:13 -04001402 if (typeSpecifier.type == EbtBool)
1403 {
1404 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1405 recover();
1406 }
1407 break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001408
Jamie Madillb120eac2013-06-12 14:08:13 -04001409 default: break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001410 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001411 }
1412
1413 return returnType;
1414}
1415
Olli Etuahofa33d582015-04-09 14:33:12 +03001416TIntermAggregate *TParseContext::parseSingleDeclaration(TPublicType &publicType,
1417 const TSourceLoc &identifierOrTypeLocation,
1418 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001419{
Olli Etuahofa33d582015-04-09 14:33:12 +03001420 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001421
Olli Etuahobab4c082015-04-24 16:38:49 +03001422 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03001423
Olli Etuahobab4c082015-04-24 16:38:49 +03001424 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1425
1426 if (emptyDeclaration)
1427 {
1428 if (publicType.isUnsizedArray())
1429 {
1430 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an error.
1431 // It is assumed that this applies to empty declarations as well.
1432 error(identifierOrTypeLocation, "empty array declaration needs to specify a size", identifier.c_str());
1433 }
1434 }
1435 else
Jamie Madill60ed9812013-06-06 11:56:46 -04001436 {
Olli Etuahofa33d582015-04-09 14:33:12 +03001437 if (singleDeclarationErrorCheck(publicType, identifierOrTypeLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001438 recover();
1439
Olli Etuaho376f1b52015-04-13 13:23:41 +03001440 if (nonInitErrorCheck(identifierOrTypeLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001441 recover();
1442
Olli Etuaho2935c582015-04-08 14:32:06 +03001443 TVariable *variable = nullptr;
Olli Etuahofa33d582015-04-09 14:33:12 +03001444 if (!declareVariable(identifierOrTypeLocation, identifier, TType(publicType), &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001445 recover();
1446
1447 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001448 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001449 }
1450
Olli Etuahoe7847b02015-03-16 11:56:12 +02001451 return intermediate.makeAggregate(symbol, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001452}
1453
Olli Etuahoe7847b02015-03-16 11:56:12 +02001454TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1455 const TSourceLoc &identifierLocation,
1456 const TString &identifier,
1457 const TSourceLoc &indexLocation,
1458 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001459{
Olli Etuahofa33d582015-04-09 14:33:12 +03001460 mDeferredSingleDeclarationErrorCheck = false;
1461
1462 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001463 recover();
1464
Olli Etuaho376f1b52015-04-13 13:23:41 +03001465 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001466 recover();
1467
1468 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1469 {
1470 recover();
1471 }
1472
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001473 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001474
1475 int size;
1476 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1477 {
1478 recover();
1479 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001480 // Make the type an array even if size check failed.
1481 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1482 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001483
Olli Etuaho2935c582015-04-08 14:32:06 +03001484 TVariable *variable = nullptr;
1485 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001486 recover();
1487
Olli Etuahoe7847b02015-03-16 11:56:12 +02001488 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001489 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001490 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001491
Olli Etuahoe7847b02015-03-16 11:56:12 +02001492 return intermediate.makeAggregate(symbol, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001493}
1494
Jamie Madill06145232015-05-13 13:10:01 -04001495TIntermAggregate *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001496 const TSourceLoc &identifierLocation,
1497 const TString &identifier,
1498 const TSourceLoc &initLocation,
1499 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001500{
Olli Etuahofa33d582015-04-09 14:33:12 +03001501 mDeferredSingleDeclarationErrorCheck = false;
1502
1503 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001504 recover();
1505
Olli Etuahoe7847b02015-03-16 11:56:12 +02001506 TIntermNode *intermNode = nullptr;
1507 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04001508 {
1509 //
1510 // Build intermediate representation
1511 //
Olli Etuahoe7847b02015-03-16 11:56:12 +02001512 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001513 }
1514 else
1515 {
1516 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001517 return nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001518 }
1519}
1520
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001521TIntermAggregate *TParseContext::parseSingleArrayInitDeclaration(TPublicType &publicType,
1522 const TSourceLoc &identifierLocation,
1523 const TString &identifier,
1524 const TSourceLoc &indexLocation,
1525 TIntermTyped *indexExpression,
1526 const TSourceLoc &initLocation,
1527 TIntermTyped *initializer)
1528{
1529 mDeferredSingleDeclarationErrorCheck = false;
1530
1531 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1532 recover();
1533
1534 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1535 {
1536 recover();
1537 }
1538
1539 TPublicType arrayType(publicType);
1540
Olli Etuaho376f1b52015-04-13 13:23:41 +03001541 int size = 0;
1542 // If indexExpression is nullptr, then the array will eventually get its size implicitly from the initializer.
1543 if (indexExpression != nullptr && arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001544 {
1545 recover();
1546 }
1547 // Make the type an array even if size check failed.
1548 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1549 arrayType.setArraySize(size);
1550
1551 // initNode will correspond to the whole of "type b[n] = initializer".
1552 TIntermNode *initNode = nullptr;
1553 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1554 {
1555 return initNode ? intermediate.makeAggregate(initNode, initLocation) : nullptr;
1556 }
1557 else
1558 {
1559 recover();
1560 return nullptr;
1561 }
1562}
1563
Olli Etuahoe7847b02015-03-16 11:56:12 +02001564TIntermAggregate *TParseContext::parseInvariantDeclaration(const TSourceLoc &invariantLoc,
Jamie Madill47e3ec02014-08-20 16:38:33 -04001565 const TSourceLoc &identifierLoc,
1566 const TString *identifier,
1567 const TSymbol *symbol)
1568{
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001569 // invariant declaration
Jamie Madill47e3ec02014-08-20 16:38:33 -04001570 if (globalErrorCheck(invariantLoc, symbolTable.atGlobalLevel(), "invariant varying"))
1571 {
1572 recover();
1573 }
1574
1575 if (!symbol)
1576 {
1577 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
1578 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001579 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001580 }
1581 else
1582 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001583 const TString kGlFrontFacing("gl_FrontFacing");
1584 if (*identifier == kGlFrontFacing)
1585 {
1586 error(identifierLoc, "identifier should not be declared as invariant", identifier->c_str());
1587 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001588 return nullptr;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001589 }
Jamie Madill2c433252014-12-03 12:36:54 -05001590 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001591 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1592 ASSERT(variable);
1593 const TType &type = variable->getType();
1594 TIntermSymbol *intermSymbol = intermediate.addSymbol(variable->getUniqueId(),
1595 *identifier, type, identifierLoc);
1596
1597 TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc);
1598 aggregate->setOp(EOpInvariantDeclaration);
1599 return aggregate;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001600 }
1601}
1602
Olli Etuahoe7847b02015-03-16 11:56:12 +02001603TIntermAggregate *TParseContext::parseDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1604 const TSourceLoc &identifierLocation, const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001605{
Olli Etuahofa33d582015-04-09 14:33:12 +03001606 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1607 if (mDeferredSingleDeclarationErrorCheck)
1608 {
1609 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1610 recover();
1611 mDeferredSingleDeclarationErrorCheck = false;
1612 }
1613
Jamie Madill0bd18df2013-06-20 11:55:52 -04001614 if (locationDeclaratorListCheck(identifierLocation, publicType))
1615 recover();
1616
Olli Etuaho376f1b52015-04-13 13:23:41 +03001617 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001618 recover();
1619
Olli Etuaho2935c582015-04-08 14:32:06 +03001620 TVariable *variable = nullptr;
1621 if (!declareVariable(identifierLocation, identifier, TType(publicType), &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001622 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001623
1624 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1625 if (variable && symbol)
Jamie Madill502d66f2013-06-20 11:55:52 -04001626 symbol->setId(variable->getUniqueId());
1627
Olli Etuahoe7847b02015-03-16 11:56:12 +02001628 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001629}
1630
Olli Etuahoe7847b02015-03-16 11:56:12 +02001631TIntermAggregate *TParseContext::parseArrayDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1632 const TSourceLoc &identifierLocation, const TString &identifier,
1633 const TSourceLoc &arrayLocation, TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001634{
Olli Etuahofa33d582015-04-09 14:33:12 +03001635 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1636 if (mDeferredSingleDeclarationErrorCheck)
1637 {
1638 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1639 recover();
1640 mDeferredSingleDeclarationErrorCheck = false;
1641 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001642
Jamie Madill0bd18df2013-06-20 11:55:52 -04001643 if (locationDeclaratorListCheck(identifierLocation, publicType))
1644 recover();
1645
Olli Etuaho376f1b52015-04-13 13:23:41 +03001646 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001647 recover();
1648
1649 if (arrayTypeErrorCheck(arrayLocation, publicType) || arrayQualifierErrorCheck(arrayLocation, publicType))
1650 {
1651 recover();
1652 }
Olli Etuaho93a90fd2015-04-07 18:14:07 +03001653 else
Jamie Madill502d66f2013-06-20 11:55:52 -04001654 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001655 TType arrayType = TType(publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04001656 int size;
1657 if (arraySizeErrorCheck(arrayLocation, indexExpression, size))
Olli Etuahoe7847b02015-03-16 11:56:12 +02001658 {
Jamie Madill502d66f2013-06-20 11:55:52 -04001659 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001660 }
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001661 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001662
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001663 TVariable *variable = nullptr;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001664 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001665 recover();
Jamie Madill502d66f2013-06-20 11:55:52 -04001666
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001667 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
1668 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001669 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001670
1671 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001672 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001673
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001674 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001675}
1676
Jamie Madill06145232015-05-13 13:10:01 -04001677TIntermAggregate *TParseContext::parseInitDeclarator(const TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001678 const TSourceLoc &identifierLocation, const TString &identifier,
1679 const TSourceLoc &initLocation, TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001680{
Olli Etuahofa33d582015-04-09 14:33:12 +03001681 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1682 if (mDeferredSingleDeclarationErrorCheck)
1683 {
1684 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1685 recover();
1686 mDeferredSingleDeclarationErrorCheck = false;
1687 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001688
Jamie Madill0bd18df2013-06-20 11:55:52 -04001689 if (locationDeclaratorListCheck(identifierLocation, publicType))
1690 recover();
1691
Olli Etuahoe7847b02015-03-16 11:56:12 +02001692 TIntermNode *intermNode = nullptr;
1693 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04001694 {
1695 //
1696 // build the intermediate representation
1697 //
1698 if (intermNode)
1699 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001700 return intermediate.growAggregate(aggregateDeclaration, intermNode, initLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001701 }
1702 else
1703 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001704 return aggregateDeclaration;
Jamie Madill502d66f2013-06-20 11:55:52 -04001705 }
1706 }
1707 else
1708 {
1709 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001710 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001711 }
1712}
1713
Jamie Madill06145232015-05-13 13:10:01 -04001714TIntermAggregate *TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001715 TIntermAggregate *aggregateDeclaration,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301716 const TSourceLoc &identifierLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001717 const TString &identifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301718 const TSourceLoc &indexLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001719 TIntermTyped *indexExpression,
1720 const TSourceLoc &initLocation, TIntermTyped *initializer)
1721{
1722 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1723 if (mDeferredSingleDeclarationErrorCheck)
1724 {
1725 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1726 recover();
1727 mDeferredSingleDeclarationErrorCheck = false;
1728 }
1729
1730 if (locationDeclaratorListCheck(identifierLocation, publicType))
1731 recover();
1732
1733 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1734 {
1735 recover();
1736 }
1737
1738 TPublicType arrayType(publicType);
1739
Olli Etuaho376f1b52015-04-13 13:23:41 +03001740 int size = 0;
1741 // If indexExpression is nullptr, then the array will eventually get its size implicitly from the initializer.
1742 if (indexExpression != nullptr && arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001743 {
1744 recover();
1745 }
1746 // Make the type an array even if size check failed.
1747 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1748 arrayType.setArraySize(size);
1749
1750 // initNode will correspond to the whole of "b[n] = initializer".
1751 TIntermNode *initNode = nullptr;
1752 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1753 {
1754 if (initNode)
1755 {
1756 return intermediate.growAggregate(aggregateDeclaration, initNode, initLocation);
1757 }
1758 else
1759 {
1760 return aggregateDeclaration;
1761 }
1762 }
1763 else
1764 {
1765 recover();
1766 return nullptr;
1767 }
1768}
1769
Jamie Madilla295edf2013-06-06 11:56:48 -04001770void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1771{
1772 if (typeQualifier.qualifier != EvqUniform)
1773 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301774 error(typeQualifier.line,
1775 "invalid qualifier:",
1776 getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
Jamie Madilla295edf2013-06-06 11:56:48 -04001777 recover();
1778 return;
1779 }
1780
1781 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1782 ASSERT(!layoutQualifier.isEmpty());
1783
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001784 if (mShaderVersion < 300)
Jamie Madilla295edf2013-06-06 11:56:48 -04001785 {
1786 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1787 recover();
1788 return;
1789 }
1790
1791 if (layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1792 {
1793 recover();
1794 return;
1795 }
1796
Jamie Madill099c0f32013-06-20 11:55:52 -04001797 if (layoutQualifier.matrixPacking != EmpUnspecified)
1798 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001799 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04001800 }
1801
Geoff Langc6856732014-02-11 09:38:55 -05001802 if (layoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04001803 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001804 mDefaultBlockStorage = layoutQualifier.blockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04001805 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001806}
1807
Jamie Madill06145232015-05-13 13:10:01 -04001808TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001809{
Jamie Madill06145232015-05-13 13:10:01 -04001810 TPublicType publicType = publicTypeIn;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001811 TOperator op = EOpNull;
1812 if (publicType.userDef)
1813 {
1814 op = EOpConstructStruct;
1815 }
1816 else
1817 {
1818 switch (publicType.type)
1819 {
1820 case EbtFloat:
1821 if (publicType.isMatrix())
1822 {
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001823 switch(publicType.getCols())
1824 {
Alexis Hetu07e57df2015-06-16 16:55:52 -04001825 case 2:
1826 switch(publicType.getRows())
1827 {
1828 case 2: op = EOpConstructMat2; break;
1829 case 3: op = EOpConstructMat2x3; break;
1830 case 4: op = EOpConstructMat2x4; break;
1831 }
1832 break;
1833 case 3:
1834 switch(publicType.getRows())
1835 {
1836 case 2: op = EOpConstructMat3x2; break;
1837 case 3: op = EOpConstructMat3; break;
1838 case 4: op = EOpConstructMat3x4; break;
1839 }
1840 break;
1841 case 4:
1842 switch(publicType.getRows())
1843 {
1844 case 2: op = EOpConstructMat4x2; break;
1845 case 3: op = EOpConstructMat4x3; break;
1846 case 4: op = EOpConstructMat4; break;
1847 }
1848 break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001849 }
1850 }
1851 else
1852 {
1853 switch(publicType.getNominalSize())
1854 {
Jamie Madill28b97422013-07-08 14:01:38 -04001855 case 1: op = EOpConstructFloat; break;
1856 case 2: op = EOpConstructVec2; break;
1857 case 3: op = EOpConstructVec3; break;
1858 case 4: op = EOpConstructVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001859 }
1860 }
1861 break;
1862
1863 case EbtInt:
1864 switch(publicType.getNominalSize())
1865 {
Jamie Madill28b97422013-07-08 14:01:38 -04001866 case 1: op = EOpConstructInt; break;
1867 case 2: op = EOpConstructIVec2; break;
1868 case 3: op = EOpConstructIVec3; break;
1869 case 4: op = EOpConstructIVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001870 }
1871 break;
1872
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001873 case EbtUInt:
1874 switch(publicType.getNominalSize())
1875 {
Jamie Madill28b97422013-07-08 14:01:38 -04001876 case 1: op = EOpConstructUInt; break;
1877 case 2: op = EOpConstructUVec2; break;
1878 case 3: op = EOpConstructUVec3; break;
1879 case 4: op = EOpConstructUVec4; break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001880 }
1881 break;
1882
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001883 case EbtBool:
1884 switch(publicType.getNominalSize())
1885 {
Jamie Madill28b97422013-07-08 14:01:38 -04001886 case 1: op = EOpConstructBool; break;
1887 case 2: op = EOpConstructBVec2; break;
1888 case 3: op = EOpConstructBVec3; break;
1889 case 4: op = EOpConstructBVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001890 }
1891 break;
1892
1893 default: break;
1894 }
1895
1896 if (op == EOpNull)
1897 {
1898 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
1899 recover();
1900 publicType.type = EbtFloat;
1901 op = EOpConstructFloat;
1902 }
1903 }
1904
1905 TString tempString;
Dmitry Skiba7f17a502015-06-22 15:08:39 -07001906 const TType *type = new TType(publicType);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001907 return new TFunction(&tempString, type, op);
1908}
1909
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001910// This function is used to test for the correctness of the parameters passed to various constructor functions
Arun Patole7e7e68d2015-05-22 12:02:25 +05301911// and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001912//
1913// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1914//
Arun Patole7e7e68d2015-05-22 12:02:25 +05301915TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments, TType *type, TOperator op, TFunction *fnCall,
1916 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001917{
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001918 TIntermAggregate *aggregateArguments = arguments->getAsAggregate();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001919
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001920 if (!aggregateArguments)
1921 {
1922 aggregateArguments = new TIntermAggregate;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001923 aggregateArguments->getSequence()->push_back(arguments);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001924 }
1925
Olli Etuahof40319e2015-03-10 14:33:00 +02001926 if (type->isArray())
1927 {
1928 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of the array.
1929 TIntermSequence *args = aggregateArguments->getSequence();
1930 for (size_t i = 0; i < args->size(); i++)
1931 {
1932 const TType &argType = (*args)[i]->getAsTyped()->getType();
1933 // It has already been checked that the argument is not an array.
1934 ASSERT(!argType.isArray());
1935 if (!argType.sameElementType(*type))
1936 {
1937 error(line, "Array constructor argument has an incorrect type", "Error");
1938 recover();
1939 return nullptr;
1940 }
1941 }
1942 }
1943 else if (op == EOpConstructStruct)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001944 {
1945 const TFieldList &fields = type->getStruct()->fields();
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001946 TIntermSequence *args = aggregateArguments->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001947
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001948 for (size_t i = 0; i < fields.size(); i++)
1949 {
Nicolas Capensffd73872014-08-21 13:49:16 -04001950 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001951 {
1952 error(line, "Structure constructor arguments do not match structure fields", "Error");
1953 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001954
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001955 return 0;
1956 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001957 }
1958 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001959
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001960 // Turn the argument list itself into a constructor
Olli Etuaho21203702014-11-13 16:16:21 +02001961 TIntermAggregate *constructor = intermediate.setAggregateOperator(aggregateArguments, op, line);
1962 TIntermTyped *constConstructor = foldConstConstructor(constructor, *type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001963 if (constConstructor)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001964 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001965 return constConstructor;
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001966 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001967
Olli Etuaho21203702014-11-13 16:16:21 +02001968 // Structs should not be precision qualified, the individual members may be.
1969 // Built-in types on the other hand should be precision qualified.
1970 if (op != EOpConstructStruct)
1971 {
1972 constructor->setPrecisionFromChildren();
1973 type->setPrecision(constructor->getPrecision());
1974 }
1975
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001976 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001977}
1978
Arun Patole7e7e68d2015-05-22 12:02:25 +05301979TIntermTyped *TParseContext::foldConstConstructor(TIntermAggregate *aggrNode, const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001980{
Olli Etuahof40319e2015-03-10 14:33:00 +02001981 // TODO: Add support for folding array constructors
1982 bool canBeFolded = areAllChildConst(aggrNode) && !type.isArray();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001983 aggrNode->setType(type);
Arun Patole7e7e68d2015-05-22 12:02:25 +05301984 if (canBeFolded)
1985 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001986 bool returnVal = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301987 TConstantUnion *unionArray = new TConstantUnion[type.getObjectSize()];
1988 if (aggrNode->getSequence()->size() == 1)
1989 {
1990 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type,
1991 true);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001992 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301993 else
1994 {
1995 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(),
1996 type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001997 }
1998 if (returnVal)
1999 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002000
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002001 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
2002 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002003
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002004 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002005}
2006
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002007//
2008// This function returns the tree representation for the vector field(s) being accessed from contant vector.
2009// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
2010// 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 +05302011// 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 +00002012// a constant matrix.
2013//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302014TIntermTyped *TParseContext::addConstVectorNode(TVectorFields &fields, TIntermTyped *node, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002015{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302016 TIntermTyped *typedNode;
2017 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002018
Jamie Madillb11e2482015-05-04 14:21:22 -04002019 const TConstantUnion *unionArray;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302020 if (tempConstantNode)
2021 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002022 unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002023
Arun Patole7e7e68d2015-05-22 12:02:25 +05302024 if (!unionArray)
2025 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002026 return node;
2027 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302028 }
2029 else
2030 { // 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 +00002031 error(line, "Cannot offset into the vector", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002032 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002033
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002034 return 0;
2035 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002036
Arun Patole7e7e68d2015-05-22 12:02:25 +05302037 TConstantUnion *constArray = new TConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002038
Arun Patole7e7e68d2015-05-22 12:02:25 +05302039 for (int i = 0; i < fields.num; i++)
2040 {
2041 if (fields.offsets[i] >= node->getType().getNominalSize())
2042 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002043 std::stringstream extraInfoStream;
2044 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
2045 std::string extraInfo = extraInfoStream.str();
2046 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002047 recover();
2048 fields.offsets[i] = 0;
2049 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302050
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002051 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002052
Arun Patole7e7e68d2015-05-22 12:02:25 +05302053 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002054 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
2055 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002056}
2057
2058//
2059// This function returns the column being accessed from a constant matrix. The values are retrieved from
Arun Patole7e7e68d2015-05-22 12:02:25 +05302060// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
2061// 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 +00002062// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
2063//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302064TIntermTyped *TParseContext::addConstMatrixNode(int index, TIntermTyped *node, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002065{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302066 TIntermTyped *typedNode;
2067 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002068
Arun Patole7e7e68d2015-05-22 12:02:25 +05302069 if (index >= node->getType().getCols())
2070 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002071 std::stringstream extraInfoStream;
2072 extraInfoStream << "matrix field selection out of range '" << index << "'";
2073 std::string extraInfo = extraInfoStream.str();
2074 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002075 recover();
2076 index = 0;
2077 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002078
Arun Patole7e7e68d2015-05-22 12:02:25 +05302079 if (tempConstantNode)
2080 {
Jamie Madillb11e2482015-05-04 14:21:22 -04002081 TConstantUnion *unionArray = tempConstantNode->getUnionArrayPointer();
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002082 int size = tempConstantNode->getType().getCols();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002083 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302084 }
2085 else
2086 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002087 error(line, "Cannot offset into the matrix", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002088 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002089
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002090 return 0;
2091 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002092
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002093 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002094}
2095
2096
2097//
2098// 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 +05302099// the symbol table and parse-tree is built for the type of the element. The input
2100// 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 +00002101// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
2102//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302103TIntermTyped *TParseContext::addConstArrayNode(int index, TIntermTyped *node, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002104{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302105 TIntermTyped *typedNode;
2106 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002107 TType arrayElementType = node->getType();
2108 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002109
Arun Patole7e7e68d2015-05-22 12:02:25 +05302110 if (index >= node->getType().getArraySize())
2111 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002112 std::stringstream extraInfoStream;
2113 extraInfoStream << "array field selection out of range '" << index << "'";
2114 std::string extraInfo = extraInfoStream.str();
2115 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002116 recover();
2117 index = 0;
2118 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002119
Arun Patole7e7e68d2015-05-22 12:02:25 +05302120 if (tempConstantNode)
2121 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002122 size_t arrayElementSize = arrayElementType.getObjectSize();
Arun Patole7e7e68d2015-05-22 12:02:25 +05302123 TConstantUnion *unionArray = tempConstantNode->getUnionArrayPointer();
2124 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(),
2125 line);
2126 }
2127 else
2128 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002129 error(line, "Cannot offset into the array", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002130 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002131
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002132 return 0;
2133 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002134
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002135 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002136}
2137
2138
2139//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302140// 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 +00002141// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
2142// function and returns the parse-tree with the values of the embedded/nested struct.
2143//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302144TIntermTyped *TParseContext::addConstStruct(const TString &identifier, TIntermTyped *node, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002145{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302146 const TFieldList &fields = node->getType().getStruct()->fields();
Jamie Madill94bf7f22013-07-08 13:31:15 -04002147 size_t instanceSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002148
Arun Patole7e7e68d2015-05-22 12:02:25 +05302149 for (size_t index = 0; index < fields.size(); ++index)
2150 {
2151 if (fields[index]->name() == identifier)
2152 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002153 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302154 }
2155 else
2156 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002157 instanceSize += fields[index]->type()->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002158 }
2159 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002160
Jamie Madill94bf7f22013-07-08 13:31:15 -04002161 TIntermTyped *typedNode;
2162 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
Arun Patole7e7e68d2015-05-22 12:02:25 +05302163 if (tempConstantNode)
2164 {
2165 TConstantUnion *constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002166
Arun Patole7e7e68d2015-05-22 12:02:25 +05302167 // type will be changed in the calling function
2168 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line);
2169 }
2170 else
2171 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002172 error(line, "Cannot offset into the structure", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002173 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002174
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002175 return 0;
2176 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002177
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002178 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002179}
2180
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002181//
2182// Interface/uniform blocks
2183//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302184TIntermAggregate *TParseContext::addInterfaceBlock(const TPublicType &typeQualifier, const TSourceLoc &nameLine,
2185 const TString &blockName, TFieldList *fieldList,
2186 const TString *instanceName, const TSourceLoc &instanceLine,
2187 TIntermTyped *arrayIndex, const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002188{
2189 if (reservedErrorCheck(nameLine, blockName))
2190 recover();
2191
2192 if (typeQualifier.qualifier != EvqUniform)
2193 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302194 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
2195 "interface blocks must be uniform");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002196 recover();
2197 }
2198
Jamie Madill099c0f32013-06-20 11:55:52 -04002199 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
2200 if (layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002201 {
2202 recover();
2203 }
2204
Jamie Madill099c0f32013-06-20 11:55:52 -04002205 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2206 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002207 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002208 }
2209
Jamie Madill1566ef72013-06-20 11:55:54 -04002210 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2211 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002212 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002213 }
2214
Arun Patole7e7e68d2015-05-22 12:02:25 +05302215 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2216 if (!symbolTable.declare(blockNameSymbol))
2217 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002218 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
2219 recover();
2220 }
2221
Jamie Madill98493dd2013-07-08 14:39:03 -04002222 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302223 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2224 {
2225 TField *field = (*fieldList)[memberIndex];
2226 TType *fieldType = field->type();
2227 if (IsSampler(fieldType->getBasicType()))
2228 {
2229 error(field->line(), "unsupported type", fieldType->getBasicString(),
2230 "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002231 recover();
2232 }
2233
Jamie Madill98493dd2013-07-08 14:39:03 -04002234 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002235 switch (qualifier)
2236 {
2237 case EvqGlobal:
2238 case EvqUniform:
2239 break;
2240 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04002241 error(field->line(), "invalid qualifier on interface block member", getQualifierString(qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002242 recover();
2243 break;
2244 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002245
2246 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002247 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
2248 if (layoutLocationErrorCheck(field->line(), fieldLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002249 {
2250 recover();
2251 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002252
Jamie Madill98493dd2013-07-08 14:39:03 -04002253 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002254 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302255 error(field->line(), "invalid layout qualifier:", getBlockStorageString(fieldLayoutQualifier.blockStorage),
2256 "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04002257 recover();
2258 }
2259
Jamie Madill98493dd2013-07-08 14:39:03 -04002260 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002261 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002262 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002263 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002264 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002265 {
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002266 warning(field->line(), "extraneous layout qualifier:",
2267 getMatrixPackingString(fieldLayoutQualifier.matrixPacking), "only has an effect on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04002268 }
2269
Jamie Madill98493dd2013-07-08 14:39:03 -04002270 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002271 }
2272
Jamie Madill98493dd2013-07-08 14:39:03 -04002273 // add array index
2274 int arraySize = 0;
2275 if (arrayIndex != NULL)
2276 {
2277 if (arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize))
2278 recover();
2279 }
2280
Arun Patole7e7e68d2015-05-22 12:02:25 +05302281 TInterfaceBlock *interfaceBlock = new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize,
2282 blockLayoutQualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002283 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier, arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002284
2285 TString symbolName = "";
2286 int symbolId = 0;
2287
Jamie Madill98493dd2013-07-08 14:39:03 -04002288 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002289 {
2290 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002291 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2292 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302293 TField *field = (*fieldList)[memberIndex];
2294 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002295
2296 // set parent pointer of the field variable
2297 fieldType->setInterfaceBlock(interfaceBlock);
2298
Arun Patole7e7e68d2015-05-22 12:02:25 +05302299 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002300 fieldVariable->setQualifier(typeQualifier.qualifier);
2301
Arun Patole7e7e68d2015-05-22 12:02:25 +05302302 if (!symbolTable.declare(fieldVariable))
2303 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002304 error(field->line(), "redefinition", field->name().c_str(), "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002305 recover();
2306 }
2307 }
2308 }
2309 else
2310 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002311 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302312 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002313 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002314
Arun Patole7e7e68d2015-05-22 12:02:25 +05302315 if (!symbolTable.declare(instanceTypeDef))
2316 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002317 error(instanceLine, "redefinition", instanceName->c_str(), "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002318 recover();
2319 }
2320
2321 symbolId = instanceTypeDef->getUniqueId();
2322 symbolName = instanceTypeDef->getName();
2323 }
2324
Arun Patole7e7e68d2015-05-22 12:02:25 +05302325 TIntermAggregate *aggregate = intermediate.makeAggregate(intermediate.addSymbol(symbolId, symbolName,
2326 interfaceBlockType,
2327 typeQualifier.line),
2328 nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002329 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04002330
2331 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002332 return aggregate;
2333}
2334
Arun Patole7e7e68d2015-05-22 12:02:25 +05302335bool TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002336{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002337 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002338
2339 // Embedded structure definitions are not supported per GLSL ES spec.
2340 // They aren't allowed in GLSL either, but we need to detect this here
2341 // so we don't rely on the GLSL compiler to catch it.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302342 if (mStructNestingLevel > 1)
2343 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002344 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002345 return true;
2346 }
2347
2348 return false;
2349}
2350
2351void TParseContext::exitStructDeclaration()
2352{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002353 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002354}
2355
2356namespace {
2357
2358const int kWebGLMaxStructNesting = 4;
2359
2360} // namespace
2361
Arun Patole7e7e68d2015-05-22 12:02:25 +05302362bool TParseContext::structNestingErrorCheck(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002363{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302364 if (!IsWebGLBasedSpec(mShaderSpec))
2365 {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002366 return false;
2367 }
2368
Arun Patole7e7e68d2015-05-22 12:02:25 +05302369 if (field.type()->getBasicType() != EbtStruct)
2370 {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002371 return false;
2372 }
2373
2374 // We're already inside a structure definition at this point, so add
2375 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302376 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2377 {
Jamie Madill41a49272014-03-18 16:10:13 -04002378 std::stringstream reasonStream;
2379 reasonStream << "Reference of struct type "
2380 << field.type()->getStruct()->name().c_str()
2381 << " exceeds maximum allowed nesting level of "
2382 << kWebGLMaxStructNesting;
2383 std::string reason = reasonStream.str();
2384 error(line, reason.c_str(), field.name().c_str(), "");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002385 return true;
2386 }
2387
2388 return false;
2389}
2390
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002391//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002392// Parse an array index expression
2393//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302394TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression, const TSourceLoc &location,
2395 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002396{
2397 TIntermTyped *indexedExpression = NULL;
2398
2399 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2400 {
2401 if (baseExpression->getAsSymbolNode())
2402 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302403 error(location, " left of '[' is not of type array, matrix, or vector ",
2404 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002405 }
2406 else
2407 {
2408 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2409 }
2410 recover();
2411 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002412
Jamie Madill21c1e452014-12-29 11:33:41 -05002413 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2414
2415 if (indexExpression->getQualifier() == EvqConst && indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002416 {
Jamie Madill21c1e452014-12-29 11:33:41 -05002417 int index = indexConstantUnion->getIConst(0);
Jamie Madill7164cf42013-07-08 13:30:59 -04002418 if (index < 0)
2419 {
2420 std::stringstream infoStream;
2421 infoStream << index;
2422 std::string info = infoStream.str();
2423 error(location, "negative index", info.c_str());
2424 recover();
2425 index = 0;
2426 }
2427 if (baseExpression->getType().getQualifier() == EvqConst)
2428 {
2429 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002430 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002431 // constant folding for arrays
2432 indexedExpression = addConstArrayNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002433 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002434 else if (baseExpression->isVector())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002435 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002436 // constant folding for vectors
2437 TVectorFields fields;
2438 fields.num = 1;
2439 fields.offsets[0] = index; // need to do it this way because v.xy sends fields integer array
2440 indexedExpression = addConstVectorNode(fields, baseExpression, location);
2441 }
2442 else if (baseExpression->isMatrix())
2443 {
2444 // constant folding for matrices
2445 indexedExpression = addConstMatrixNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002446 }
2447 }
2448 else
2449 {
Jamie Madillb11e2482015-05-04 14:21:22 -04002450 int safeIndex = -1;
2451
Jamie Madill7164cf42013-07-08 13:30:59 -04002452 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002453 {
Jamie Madill18464b52013-07-08 14:01:55 -04002454 if (index >= baseExpression->getType().getArraySize())
Jamie Madill7164cf42013-07-08 13:30:59 -04002455 {
2456 std::stringstream extraInfoStream;
2457 extraInfoStream << "array index out of range '" << index << "'";
2458 std::string extraInfo = extraInfoStream.str();
2459 error(location, "", "[", extraInfo.c_str());
2460 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002461 safeIndex = baseExpression->getType().getArraySize() - 1;
Jamie Madill7164cf42013-07-08 13:30:59 -04002462 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302463 else if (baseExpression->getQualifier() == EvqFragData && index > 0 &&
2464 !isExtensionEnabled("GL_EXT_draw_buffers"))
Jamie Madill5d287f52013-07-12 15:38:19 -04002465 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302466 error(location, "",
2467 "[", "array indexes for gl_FragData must be zero when GL_EXT_draw_buffers is disabled");
Jamie Madill5d287f52013-07-12 15:38:19 -04002468 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002469 safeIndex = 0;
Jamie Madill5d287f52013-07-12 15:38:19 -04002470 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002471 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302472 else if ((baseExpression->isVector() || baseExpression->isMatrix()) &&
2473 baseExpression->getType().getNominalSize() <= index)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002474 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002475 std::stringstream extraInfoStream;
2476 extraInfoStream << "field selection out of range '" << index << "'";
2477 std::string extraInfo = extraInfoStream.str();
2478 error(location, "", "[", extraInfo.c_str());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002479 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002480 safeIndex = baseExpression->getType().getNominalSize() - 1;
Jamie Madill46131a32013-06-20 11:55:50 -04002481 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002482
Jamie Madillb11e2482015-05-04 14:21:22 -04002483 // Don't modify the data of the previous constant union, because it can point
2484 // to builtins, like gl_MaxDrawBuffers. Instead use a new sanitized object.
2485 if (safeIndex != -1)
2486 {
2487 TConstantUnion *safeConstantUnion = new TConstantUnion();
2488 safeConstantUnion->setIConst(safeIndex);
2489 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
2490 }
2491
Jamie Madill7164cf42013-07-08 13:30:59 -04002492 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002493 }
2494 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002495 else
2496 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002497 if (baseExpression->isInterfaceBlock())
Jamie Madill7164cf42013-07-08 13:30:59 -04002498 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302499 error(location, "",
2500 "[", "array indexes for interface blocks arrays must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002501 recover();
2502 }
Jamie Madill19571812013-08-12 15:26:34 -07002503 else if (baseExpression->getQualifier() == EvqFragmentOut)
Jamie Madill7164cf42013-07-08 13:30:59 -04002504 {
Jamie Madill19571812013-08-12 15:26:34 -07002505 error(location, "", "[", "array indexes for fragment outputs must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002506 recover();
2507 }
2508
2509 indexedExpression = intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
2510 }
2511
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002512 if (indexedExpression == 0)
2513 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002514 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002515 unionArray->setFConst(0.0f);
2516 indexedExpression = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
2517 }
2518 else if (baseExpression->isArray())
2519 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002520 const TType &baseType = baseExpression->getType();
2521 if (baseType.getStruct())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002522 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002523 TType copyOfType(baseType.getStruct());
2524 indexedExpression->setType(copyOfType);
2525 }
2526 else if (baseType.isInterfaceBlock())
2527 {
2528 TType copyOfType(baseType.getInterfaceBlock(), baseType.getQualifier(), baseType.getLayoutQualifier(), 0);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002529 indexedExpression->setType(copyOfType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002530 }
2531 else
2532 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302533 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2534 EvqTemporary, static_cast<unsigned char>(baseExpression->getNominalSize()),
2535 static_cast<unsigned char>(baseExpression->getSecondarySize())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002536 }
2537
2538 if (baseExpression->getType().getQualifier() == EvqConst)
2539 {
2540 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2541 }
2542 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002543 else if (baseExpression->isMatrix())
2544 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002545 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302546 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2547 qualifier, static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002548 }
2549 else if (baseExpression->isVector())
2550 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002551 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2552 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002553 }
2554 else
2555 {
2556 indexedExpression->setType(baseExpression->getType());
2557 }
2558
2559 return indexedExpression;
2560}
2561
Arun Patole7e7e68d2015-05-22 12:02:25 +05302562TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression, const TSourceLoc &dotLocation,
2563 const TString &fieldString, const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002564{
2565 TIntermTyped *indexedExpression = NULL;
2566
2567 if (baseExpression->isArray())
2568 {
2569 error(fieldLocation, "cannot apply dot operator to an array", ".");
2570 recover();
2571 }
2572
2573 if (baseExpression->isVector())
2574 {
2575 TVectorFields fields;
2576 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields, fieldLocation))
2577 {
2578 fields.num = 1;
2579 fields.offsets[0] = 0;
2580 recover();
2581 }
2582
2583 if (baseExpression->getType().getQualifier() == EvqConst)
2584 {
2585 // constant folding for vector fields
2586 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
2587 if (indexedExpression == 0)
2588 {
2589 recover();
2590 indexedExpression = baseExpression;
2591 }
2592 else
2593 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302594 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2595 EvqConst, (unsigned char) (fieldString).size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002596 }
2597 }
2598 else
2599 {
2600 TString vectorString = fieldString;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302601 TIntermTyped *index = intermediate.addSwizzle(fields, fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002602 indexedExpression = intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302603 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2604 EvqTemporary, (unsigned char) vectorString.size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002605 }
2606 }
2607 else if (baseExpression->isMatrix())
2608 {
2609 TMatrixFields fields;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302610 if (!parseMatrixFields(fieldString, baseExpression->getCols(), baseExpression->getRows(), fields,
2611 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002612 {
2613 fields.wholeRow = false;
2614 fields.wholeCol = false;
2615 fields.row = 0;
2616 fields.col = 0;
2617 recover();
2618 }
2619
2620 if (fields.wholeRow || fields.wholeCol)
2621 {
2622 error(dotLocation, " non-scalar fields not implemented yet", ".");
2623 recover();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002624 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002625 unionArray->setIConst(0);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302626 TIntermTyped *index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst),
2627 fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002628 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302629 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2630 EvqTemporary, static_cast<unsigned char>(baseExpression->getCols()),
2631 static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002632 }
2633 else
2634 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002635 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002636 unionArray->setIConst(fields.col * baseExpression->getRows() + fields.row);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302637 TIntermTyped *index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst),
2638 fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002639 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
2640 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision()));
2641 }
2642 }
2643 else if (baseExpression->getBasicType() == EbtStruct)
2644 {
2645 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302646 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04002647 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002648 {
2649 error(dotLocation, "structure has no fields", "Internal Error");
2650 recover();
2651 indexedExpression = baseExpression;
2652 }
2653 else
2654 {
2655 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002656 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002657 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002658 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002659 {
2660 fieldFound = true;
2661 break;
2662 }
2663 }
2664 if (fieldFound)
2665 {
2666 if (baseExpression->getType().getQualifier() == EvqConst)
2667 {
2668 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2669 if (indexedExpression == 0)
2670 {
2671 recover();
2672 indexedExpression = baseExpression;
2673 }
2674 else
2675 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002676 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002677 // change the qualifier of the return type, not of the structure field
2678 // as the structure definition is shared between various structures.
2679 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2680 }
2681 }
2682 else
2683 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002684 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002685 unionArray->setIConst(i);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302686 TIntermTyped *index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002687 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002688 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002689 }
2690 }
2691 else
2692 {
2693 error(dotLocation, " no such field in structure", fieldString.c_str());
2694 recover();
2695 indexedExpression = baseExpression;
2696 }
2697 }
2698 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002699 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002700 {
2701 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302702 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04002703 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002704 {
2705 error(dotLocation, "interface block has no fields", "Internal Error");
2706 recover();
2707 indexedExpression = baseExpression;
2708 }
2709 else
2710 {
2711 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002712 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002713 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002714 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002715 {
2716 fieldFound = true;
2717 break;
2718 }
2719 }
2720 if (fieldFound)
2721 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002722 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002723 unionArray->setIConst(i);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302724 TIntermTyped *index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
2725 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index,
2726 dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002727 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002728 }
2729 else
2730 {
2731 error(dotLocation, " no such field in interface block", fieldString.c_str());
2732 recover();
2733 indexedExpression = baseExpression;
2734 }
2735 }
2736 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002737 else
2738 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002739 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002740 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302741 error(dotLocation, " field selection requires structure, vector, or matrix on left hand side",
2742 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002743 }
2744 else
2745 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302746 error(dotLocation,
2747 " field selection requires structure, vector, matrix, or interface block on left hand side",
2748 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002749 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002750 recover();
2751 indexedExpression = baseExpression;
2752 }
2753
2754 return indexedExpression;
2755}
2756
Arun Patole7e7e68d2015-05-22 12:02:25 +05302757TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002758{
Jamie Madilla5efff92013-06-06 11:56:47 -04002759 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002760
Jamie Madilla5efff92013-06-06 11:56:47 -04002761 qualifier.location = -1;
2762 qualifier.matrixPacking = EmpUnspecified;
2763 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002764
2765 if (qualifierType == "shared")
2766 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002767 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002768 }
2769 else if (qualifierType == "packed")
2770 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002771 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002772 }
2773 else if (qualifierType == "std140")
2774 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002775 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002776 }
2777 else if (qualifierType == "row_major")
2778 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002779 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002780 }
2781 else if (qualifierType == "column_major")
2782 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002783 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002784 }
2785 else if (qualifierType == "location")
2786 {
2787 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "location requires an argument");
2788 recover();
2789 }
2790 else
2791 {
2792 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
2793 recover();
2794 }
2795
Jamie Madilla5efff92013-06-06 11:56:47 -04002796 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002797}
2798
Arun Patole7e7e68d2015-05-22 12:02:25 +05302799TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc &qualifierTypeLine,
2800 const TString &intValueString, int intValue,
2801 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002802{
Jamie Madilla5efff92013-06-06 11:56:47 -04002803 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002804
Jamie Madilla5efff92013-06-06 11:56:47 -04002805 qualifier.location = -1;
2806 qualifier.matrixPacking = EmpUnspecified;
2807 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002808
2809 if (qualifierType != "location")
2810 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302811 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
2812 "only location may have arguments");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002813 recover();
2814 }
2815 else
2816 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002817 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002818 if (intValue < 0)
2819 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002820 error(intValueLine, "out of range:", intValueString.c_str(), "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002821 recover();
2822 }
2823 else
2824 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002825 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002826 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002827 }
2828
Jamie Madilla5efff92013-06-06 11:56:47 -04002829 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002830}
2831
Jamie Madilla5efff92013-06-06 11:56:47 -04002832TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002833{
Jamie Madilla5efff92013-06-06 11:56:47 -04002834 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002835
Jamie Madilla5efff92013-06-06 11:56:47 -04002836 if (rightQualifier.location != -1)
2837 {
2838 joinedQualifier.location = rightQualifier.location;
2839 }
2840 if (rightQualifier.matrixPacking != EmpUnspecified)
2841 {
2842 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
2843 }
2844 if (rightQualifier.blockStorage != EbsUnspecified)
2845 {
2846 joinedQualifier.blockStorage = rightQualifier.blockStorage;
2847 }
2848
2849 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002850}
2851
Arun Patole7e7e68d2015-05-22 12:02:25 +05302852TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc,
2853 TQualifier interpolationQualifier,
2854 const TSourceLoc &storageLoc,
2855 TQualifier storageQualifier)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002856{
2857 TQualifier mergedQualifier = EvqSmoothIn;
2858
Arun Patole7e7e68d2015-05-22 12:02:25 +05302859 if (storageQualifier == EvqFragmentIn)
2860 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002861 if (interpolationQualifier == EvqSmooth)
2862 mergedQualifier = EvqSmoothIn;
2863 else if (interpolationQualifier == EvqFlat)
2864 mergedQualifier = EvqFlatIn;
2865 else UNREACHABLE();
2866 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302867 else if (storageQualifier == EvqCentroidIn)
2868 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002869 if (interpolationQualifier == EvqSmooth)
2870 mergedQualifier = EvqCentroidIn;
2871 else if (interpolationQualifier == EvqFlat)
2872 mergedQualifier = EvqFlatIn;
2873 else UNREACHABLE();
2874 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302875 else if (storageQualifier == EvqVertexOut)
2876 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002877 if (interpolationQualifier == EvqSmooth)
2878 mergedQualifier = EvqSmoothOut;
2879 else if (interpolationQualifier == EvqFlat)
2880 mergedQualifier = EvqFlatOut;
2881 else UNREACHABLE();
2882 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302883 else if (storageQualifier == EvqCentroidOut)
2884 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002885 if (interpolationQualifier == EvqSmooth)
2886 mergedQualifier = EvqCentroidOut;
2887 else if (interpolationQualifier == EvqFlat)
2888 mergedQualifier = EvqFlatOut;
2889 else UNREACHABLE();
2890 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302891 else
2892 {
2893 error(interpolationLoc, "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier",
2894 getInterpolationString(interpolationQualifier));
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002895 recover();
2896
2897 mergedQualifier = storageQualifier;
2898 }
2899
2900 TPublicType type;
2901 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
2902 return type;
2903}
2904
Arun Patole7e7e68d2015-05-22 12:02:25 +05302905TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier, TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002906{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002907 if (voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier.type))
2908 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002909 recover();
2910 }
2911
Arun Patole7e7e68d2015-05-22 12:02:25 +05302912 for (unsigned int i = 0; i < fieldList->size(); ++i)
2913 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002914 //
2915 // Careful not to replace already known aspects of type, like array-ness
2916 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05302917 TType *type = (*fieldList)[i]->type();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002918 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002919 type->setPrimarySize(typeSpecifier.primarySize);
2920 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002921 type->setPrecision(typeSpecifier.precision);
2922 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002923 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002924
2925 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05302926 if (type->isArray())
2927 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002928 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
2929 recover();
2930 }
2931 if (typeSpecifier.array)
2932 type->setArraySize(typeSpecifier.arraySize);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302933 if (typeSpecifier.userDef)
2934 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002935 type->setStruct(typeSpecifier.userDef->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002936 }
2937
Arun Patole7e7e68d2015-05-22 12:02:25 +05302938 if (structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i]))
2939 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002940 recover();
2941 }
2942 }
2943
Jamie Madill98493dd2013-07-08 14:39:03 -04002944 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002945}
2946
Arun Patole7e7e68d2015-05-22 12:02:25 +05302947TPublicType TParseContext::addStructure(const TSourceLoc &structLine, const TSourceLoc &nameLine,
2948 const TString *structName, TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002949{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302950 TStructure *structure = new TStructure(structName, fieldList);
2951 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002952
Jamie Madill9b820842015-02-12 10:40:10 -05002953 // Store a bool in the struct if we're at global scope, to allow us to
2954 // skip the local struct scoping workaround in HLSL.
Jamie Madillb960cc42015-02-12 15:33:20 +00002955 structure->setUniqueId(TSymbolTable::nextUniqueId());
Jamie Madill9b820842015-02-12 10:40:10 -05002956 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04002957
Jamie Madill98493dd2013-07-08 14:39:03 -04002958 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002959 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002960 if (reservedErrorCheck(nameLine, *structName))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002961 {
2962 recover();
2963 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302964 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
2965 if (!symbolTable.declare(userTypeDef))
2966 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002967 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002968 recover();
2969 }
2970 }
2971
2972 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04002973 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002974 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002975 const TField &field = *(*fieldList)[typeListIndex];
2976 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002977 switch (qualifier)
2978 {
2979 case EvqGlobal:
2980 case EvqTemporary:
2981 break;
2982 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04002983 error(field.line(), "invalid qualifier on struct member", getQualifierString(qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002984 recover();
2985 break;
2986 }
2987 }
2988
2989 TPublicType publicType;
2990 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04002991 publicType.userDef = structureType;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002992 exitStructDeclaration();
2993
2994 return publicType;
2995}
2996
Olli Etuahoa3a36662015-02-17 13:46:51 +02002997TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init, TIntermAggregate *statementList, const TSourceLoc &loc)
2998{
Olli Etuaho53f076f2015-02-20 10:55:14 +02002999 TBasicType switchType = init->getBasicType();
3000 if ((switchType != EbtInt && switchType != EbtUInt) ||
3001 init->isMatrix() ||
3002 init->isArray() ||
3003 init->isVector())
3004 {
3005 error(init->getLine(), "init-expression in a switch statement must be a scalar integer", "switch");
3006 recover();
3007 return nullptr;
3008 }
3009
Olli Etuahoac5274d2015-02-20 10:19:08 +02003010 if (statementList)
3011 {
3012 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
3013 {
3014 recover();
3015 return nullptr;
3016 }
3017 }
3018
Olli Etuahoa3a36662015-02-17 13:46:51 +02003019 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3020 if (node == nullptr)
3021 {
3022 error(loc, "erroneous switch statement", "switch");
3023 recover();
3024 return nullptr;
3025 }
3026 return node;
3027}
3028
3029TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3030{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003031 if (mSwitchNestingLevel == 0)
3032 {
3033 error(loc, "case labels need to be inside switch statements", "case");
3034 recover();
3035 return nullptr;
3036 }
3037 if (condition == nullptr)
3038 {
3039 error(loc, "case label must have a condition", "case");
3040 recover();
3041 return nullptr;
3042 }
3043 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
3044 condition->isMatrix() ||
3045 condition->isArray() ||
3046 condition->isVector())
3047 {
3048 error(condition->getLine(), "case label must be a scalar integer", "case");
3049 recover();
3050 }
3051 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
3052 if (conditionConst == nullptr)
3053 {
3054 error(condition->getLine(), "case label must be constant", "case");
3055 recover();
3056 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003057 TIntermCase *node = intermediate.addCase(condition, loc);
3058 if (node == nullptr)
3059 {
3060 error(loc, "erroneous case statement", "case");
3061 recover();
3062 return nullptr;
3063 }
3064 return node;
3065}
3066
3067TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3068{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003069 if (mSwitchNestingLevel == 0)
3070 {
3071 error(loc, "default labels need to be inside switch statements", "default");
3072 recover();
3073 return nullptr;
3074 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003075 TIntermCase *node = intermediate.addCase(nullptr, loc);
3076 if (node == nullptr)
3077 {
3078 error(loc, "erroneous default statement", "default");
3079 recover();
3080 return nullptr;
3081 }
3082 return node;
3083}
3084
Olli Etuahof6c694b2015-03-26 14:50:53 +02003085TIntermTyped *TParseContext::createUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc,
3086 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003087{
3088 if (child == nullptr)
3089 {
3090 return nullptr;
3091 }
3092
3093 switch (op)
3094 {
3095 case EOpLogicalNot:
3096 if (child->getBasicType() != EbtBool ||
3097 child->isMatrix() ||
3098 child->isArray() ||
3099 child->isVector())
3100 {
3101 return nullptr;
3102 }
3103 break;
3104 case EOpBitwiseNot:
3105 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3106 child->isMatrix() ||
3107 child->isArray())
3108 {
3109 return nullptr;
3110 }
3111 break;
3112 case EOpPostIncrement:
3113 case EOpPreIncrement:
3114 case EOpPostDecrement:
3115 case EOpPreDecrement:
3116 case EOpNegative:
3117 case EOpPositive:
3118 if (child->getBasicType() == EbtStruct ||
Olli Etuahodca3e792015-03-26 13:24:04 +02003119 child->getBasicType() == EbtBool ||
Olli Etuaho69c11b52015-03-26 12:59:00 +02003120 child->isArray())
3121 {
3122 return nullptr;
3123 }
Olli Etuahodca3e792015-03-26 13:24:04 +02003124 // Operators for built-ins are already type checked against their prototype.
Olli Etuaho69c11b52015-03-26 12:59:00 +02003125 default:
3126 break;
3127 }
3128
Olli Etuahof6c694b2015-03-26 14:50:53 +02003129 return intermediate.addUnaryMath(op, child, loc, funcReturnType);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003130}
3131
Olli Etuaho09b22472015-02-11 11:47:26 +02003132TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3133{
Olli Etuahof6c694b2015-03-26 14:50:53 +02003134 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003135 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003136 {
3137 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
3138 recover();
3139 return child;
3140 }
3141 return node;
3142}
3143
3144TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3145{
3146 if (lValueErrorCheck(loc, GetOperatorString(op), child))
3147 recover();
3148 return addUnaryMath(op, child, loc);
3149}
3150
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003151bool TParseContext::binaryOpCommonCheck(TOperator op, TIntermTyped *left, TIntermTyped *right,
Olli Etuahod6b14282015-03-17 14:31:35 +02003152 const TSourceLoc &loc)
3153{
3154 if (left->isArray() || right->isArray())
3155 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003156 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003157 {
3158 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3159 return false;
3160 }
3161
3162 if (left->isArray() != right->isArray())
3163 {
3164 error(loc, "array / non-array mismatch", GetOperatorString(op));
3165 return false;
3166 }
3167
3168 switch (op)
3169 {
3170 case EOpEqual:
3171 case EOpNotEqual:
3172 case EOpAssign:
3173 case EOpInitialize:
3174 break;
3175 default:
3176 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3177 return false;
3178 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003179 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003180 if (left->getArraySize() != right->getArraySize())
3181 {
3182 error(loc, "array size mismatch", GetOperatorString(op));
3183 return false;
3184 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003185 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003186
3187 // Check ops which require integer / ivec parameters
3188 bool isBitShift = false;
3189 switch (op)
3190 {
3191 case EOpBitShiftLeft:
3192 case EOpBitShiftRight:
3193 case EOpBitShiftLeftAssign:
3194 case EOpBitShiftRightAssign:
3195 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3196 // check that the basic type is an integer type.
3197 isBitShift = true;
3198 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3199 {
3200 return false;
3201 }
3202 break;
3203 case EOpBitwiseAnd:
3204 case EOpBitwiseXor:
3205 case EOpBitwiseOr:
3206 case EOpBitwiseAndAssign:
3207 case EOpBitwiseXorAssign:
3208 case EOpBitwiseOrAssign:
3209 // It is enough to check the type of only one operand, since later it
3210 // is checked that the operand types match.
3211 if (!IsInteger(left->getBasicType()))
3212 {
3213 return false;
3214 }
3215 break;
3216 default:
3217 break;
3218 }
3219
3220 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3221 // So the basic type should usually match.
3222 if (!isBitShift && left->getBasicType() != right->getBasicType())
3223 {
3224 return false;
3225 }
3226
Olli Etuaho9dd217b2015-03-20 14:24:31 +02003227 // Check that type sizes match exactly on ops that require that.
Olli Etuahoff699002015-03-23 14:38:42 +02003228 // Also check restrictions for structs that contain arrays or samplers.
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003229 switch(op)
3230 {
3231 case EOpAssign:
3232 case EOpInitialize:
3233 case EOpEqual:
3234 case EOpNotEqual:
Olli Etuaho9dd217b2015-03-20 14:24:31 +02003235 // ESSL 1.00 sections 5.7, 5.8, 5.9
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003236 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
Olli Etuaho9dd217b2015-03-20 14:24:31 +02003237 {
3238 error(loc, "undefined operation for structs containing arrays", GetOperatorString(op));
3239 return false;
3240 }
Olli Etuahoff699002015-03-23 14:38:42 +02003241 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3242 // we interpret the spec so that this extends to structs containing samplers,
3243 // similarly to ESSL 1.00 spec.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003244 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
Olli Etuahoff699002015-03-23 14:38:42 +02003245 left->getType().isStructureContainingSamplers())
3246 {
3247 error(loc, "undefined operation for structs containing samplers", GetOperatorString(op));
3248 return false;
3249 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003250 case EOpLessThan:
3251 case EOpGreaterThan:
3252 case EOpLessThanEqual:
3253 case EOpGreaterThanEqual:
3254 if ((left->getNominalSize() != right->getNominalSize()) ||
3255 (left->getSecondarySize() != right->getSecondarySize()))
3256 {
3257 return false;
3258 }
3259 default:
3260 break;
3261 }
3262
Olli Etuahod6b14282015-03-17 14:31:35 +02003263 return true;
3264}
3265
Olli Etuahofc1806e2015-03-17 13:03:11 +02003266TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op, TIntermTyped *left, TIntermTyped *right,
3267 const TSourceLoc &loc)
3268{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003269 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003270 return nullptr;
3271
Olli Etuahofc1806e2015-03-17 13:03:11 +02003272 switch (op)
3273 {
3274 case EOpEqual:
3275 case EOpNotEqual:
Olli Etuahofc1806e2015-03-17 13:03:11 +02003276 break;
3277 case EOpLessThan:
3278 case EOpGreaterThan:
3279 case EOpLessThanEqual:
3280 case EOpGreaterThanEqual:
Olli Etuahod6b14282015-03-17 14:31:35 +02003281 ASSERT(!left->isArray() && !right->isArray());
3282 if (left->isMatrix() || left->isVector() ||
Olli Etuahofc1806e2015-03-17 13:03:11 +02003283 left->getBasicType() == EbtStruct)
3284 {
3285 return nullptr;
3286 }
3287 break;
3288 case EOpLogicalOr:
3289 case EOpLogicalXor:
3290 case EOpLogicalAnd:
Olli Etuahod6b14282015-03-17 14:31:35 +02003291 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003292 if (left->getBasicType() != EbtBool ||
Olli Etuahod6b14282015-03-17 14:31:35 +02003293 left->isMatrix() || left->isVector())
Olli Etuahofc1806e2015-03-17 13:03:11 +02003294 {
3295 return nullptr;
3296 }
3297 break;
3298 case EOpAdd:
3299 case EOpSub:
3300 case EOpDiv:
3301 case EOpMul:
Olli Etuahod6b14282015-03-17 14:31:35 +02003302 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003303 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool)
3304 {
3305 return nullptr;
3306 }
3307 break;
3308 case EOpIMod:
Olli Etuahod6b14282015-03-17 14:31:35 +02003309 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003310 // Note that this is only for the % operator, not for mod()
3311 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
3312 {
3313 return nullptr;
3314 }
3315 break;
3316 // Note that for bitwise ops, type checking is done in promote() to
3317 // share code between ops and compound assignment
3318 default:
3319 break;
3320 }
3321
Olli Etuahofc1806e2015-03-17 13:03:11 +02003322 return intermediate.addBinaryMath(op, left, right, loc);
3323}
3324
Olli Etuaho09b22472015-02-11 11:47:26 +02003325TIntermTyped *TParseContext::addBinaryMath(TOperator op, TIntermTyped *left, TIntermTyped *right,
3326 const TSourceLoc &loc)
3327{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003328 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003329 if (node == 0)
3330 {
3331 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(), right->getCompleteString());
3332 recover();
3333 return left;
3334 }
3335 return node;
3336}
3337
3338TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op, TIntermTyped *left, TIntermTyped *right,
3339 const TSourceLoc &loc)
3340{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003341 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003342 if (node == 0)
3343 {
3344 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(), right->getCompleteString());
3345 recover();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003346 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02003347 unionArray->setBConst(false);
3348 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), loc);
3349 }
3350 return node;
3351}
3352
Olli Etuahod6b14282015-03-17 14:31:35 +02003353TIntermTyped *TParseContext::createAssign(TOperator op, TIntermTyped *left, TIntermTyped *right,
3354 const TSourceLoc &loc)
3355{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003356 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003357 {
3358 return intermediate.addAssign(op, left, right, loc);
3359 }
3360 return nullptr;
3361}
3362
3363TIntermTyped *TParseContext::addAssign(TOperator op, TIntermTyped *left, TIntermTyped *right,
3364 const TSourceLoc &loc)
3365{
3366 TIntermTyped *node = createAssign(op, left, right, loc);
3367 if (node == nullptr)
3368 {
3369 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
3370 recover();
3371 return left;
3372 }
3373 return node;
3374}
3375
Olli Etuaho49300862015-02-20 14:54:49 +02003376TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3377{
3378 switch (op)
3379 {
3380 case EOpContinue:
3381 if (mLoopNestingLevel <= 0)
3382 {
3383 error(loc, "continue statement only allowed in loops", "");
3384 recover();
3385 }
3386 break;
3387 case EOpBreak:
Olli Etuaho53f076f2015-02-20 10:55:14 +02003388 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
Olli Etuaho49300862015-02-20 14:54:49 +02003389 {
Olli Etuaho53f076f2015-02-20 10:55:14 +02003390 error(loc, "break statement only allowed in loops and switch statements", "");
Olli Etuaho49300862015-02-20 14:54:49 +02003391 recover();
3392 }
3393 break;
3394 case EOpReturn:
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003395 if (mCurrentFunctionType->getBasicType() != EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02003396 {
3397 error(loc, "non-void function must return a value", "return");
3398 recover();
3399 }
3400 break;
3401 default:
3402 // No checks for discard
3403 break;
3404 }
3405 return intermediate.addBranch(op, loc);
3406}
3407
3408TIntermBranch *TParseContext::addBranch(TOperator op, TIntermTyped *returnValue, const TSourceLoc &loc)
3409{
3410 ASSERT(op == EOpReturn);
3411 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003412 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02003413 {
3414 error(loc, "void function cannot return a value", "return");
3415 recover();
3416 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003417 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02003418 {
3419 error(loc, "function return is not matching type:", "return");
3420 recover();
3421 }
3422 return intermediate.addBranch(op, returnValue, loc);
3423}
3424
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003425TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall, TIntermNode *paramNode, TIntermNode *thisNode,
3426 const TSourceLoc &loc, bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003427{
3428 *fatalError = false;
3429 TOperator op = fnCall->getBuiltInOp();
3430 TIntermTyped *callNode = nullptr;
3431
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003432 if (thisNode != nullptr)
3433 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003434 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho96e67382015-04-23 14:27:02 +03003435 int arraySize = 0;
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003436 TIntermTyped *typedThis = thisNode->getAsTyped();
3437 if (fnCall->getName() != "length")
3438 {
3439 error(loc, "invalid method", fnCall->getName().c_str());
3440 recover();
3441 }
3442 else if (paramNode != nullptr)
3443 {
3444 error(loc, "method takes no parameters", "length");
3445 recover();
3446 }
3447 else if (typedThis == nullptr || !typedThis->isArray())
3448 {
3449 error(loc, "length can only be called on arrays", "length");
3450 recover();
3451 }
3452 else
3453 {
Olli Etuaho96e67382015-04-23 14:27:02 +03003454 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03003455 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003456 {
Olli Etuaho39282e12015-04-23 15:41:48 +03003457 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003458 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03003459 // (func()).length()
3460 // (int[3](0, 1, 2)).length()
3461 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid expression.
3462 // It allows "An array name with the length method applied" in contrast to GLSL 4.4 spec section 5.9
3463 // which allows "An array, vector or matrix expression with the length method applied".
3464 error(loc, "length can only be called on array names, not on array expressions", "length");
3465 recover();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003466 }
3467 }
Olli Etuaho96e67382015-04-23 14:27:02 +03003468 unionArray->setIConst(arraySize);
3469 callNode = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003470 }
3471 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003472 {
3473 //
3474 // Then this should be a constructor.
3475 // Don't go through the symbol table for constructors.
3476 // Their parameters will be verified algorithmically.
3477 //
3478 TType type(EbtVoid, EbpUndefined); // use this to get the type back
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003479 if (!constructorErrorCheck(loc, paramNode, *fnCall, op, &type))
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003480 {
3481 //
3482 // It's a constructor, of type 'type'.
3483 //
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003484 callNode = addConstructor(paramNode, &type, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003485 }
Olli Etuaho72ba85b2015-03-04 14:23:26 +02003486
3487 if (callNode == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003488 {
3489 recover();
3490 callNode = intermediate.setAggregateOperator(nullptr, op, loc);
3491 }
3492 callNode->setType(type);
3493 }
3494 else
3495 {
3496 //
3497 // Not a constructor. Find it in the symbol table.
3498 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303499 const TFunction *fnCandidate;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003500 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003501 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003502 if (fnCandidate)
3503 {
3504 //
3505 // A declared function.
3506 //
3507 if (builtIn && !fnCandidate->getExtension().empty() &&
3508 extensionErrorCheck(loc, fnCandidate->getExtension()))
3509 {
3510 recover();
3511 }
3512 op = fnCandidate->getBuiltInOp();
3513 if (builtIn && op != EOpNull)
3514 {
3515 //
3516 // A function call mapped to a built-in operation.
3517 //
3518 if (fnCandidate->getParamCount() == 1)
3519 {
3520 //
3521 // Treat it like a built-in unary operator.
3522 //
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003523 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc, &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003524 if (callNode == nullptr)
3525 {
3526 std::stringstream extraInfoStream;
3527 extraInfoStream << "built in unary operator function. Type: "
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003528 << static_cast<TIntermTyped*>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003529 std::string extraInfo = extraInfoStream.str();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003530 error(paramNode->getLine(), " wrong operand type", "Internal Error", extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003531 *fatalError = true;
3532 return nullptr;
3533 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003534 }
3535 else
3536 {
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003537 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003538 aggregate->setType(fnCandidate->getReturnType());
3539 aggregate->setPrecisionFromChildren();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003540
3541 // Some built-in functions have out parameters too.
3542 functionCallLValueErrorCheck(fnCandidate, aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05303543
3544 // See if we can constant fold a built-in.
Olli Etuahob43846e2015-06-02 18:18:57 +03003545 TIntermTyped *foldedNode = intermediate.foldAggregateBuiltIn(aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05303546 if (foldedNode)
3547 {
Arun Patole274f0702015-05-05 13:33:30 +05303548 callNode = foldedNode;
3549 }
Olli Etuahob43846e2015-06-02 18:18:57 +03003550 else
3551 {
3552 callNode = aggregate;
3553 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003554 }
3555 }
3556 else
3557 {
3558 // This is a real function call
3559
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003560 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003561 aggregate->setType(fnCandidate->getReturnType());
3562
3563 // this is how we know whether the given function is a builtIn function or a user defined function
3564 // if builtIn == false, it's a userDefined -> could be an overloaded builtIn function also
3565 // if builtIn == true, it's definitely a builtIn function with EOpNull
3566 if (!builtIn)
3567 aggregate->setUserDefined();
3568 aggregate->setName(fnCandidate->getMangledName());
Corentin Wallez71d147f2015-02-11 11:15:24 -08003569 aggregate->setFunctionId(fnCandidate->getUniqueId());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003570
3571 // This needs to happen after the name is set
3572 if (builtIn)
3573 aggregate->setBuiltInFunctionPrecision();
3574
3575 callNode = aggregate;
3576
3577 functionCallLValueErrorCheck(fnCandidate, aggregate);
3578 }
3579 }
3580 else
3581 {
3582 // error message was put out by findFunction()
3583 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003584 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003585 unionArray->setFConst(0.0f);
3586 callNode = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpUndefined, EvqConst), loc);
3587 recover();
3588 }
3589 }
3590 delete fnCall;
3591 return callNode;
3592}
3593
Olli Etuaho52901742015-04-15 13:42:45 +03003594TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond, TIntermTyped *trueBlock, TIntermTyped *falseBlock,
3595 const TSourceLoc &loc)
3596{
3597 if (boolErrorCheck(loc, cond))
3598 recover();
3599
3600 if (trueBlock->getType() != falseBlock->getType())
3601 {
3602 binaryOpError(loc, ":", trueBlock->getCompleteString(), falseBlock->getCompleteString());
3603 recover();
3604 return falseBlock;
3605 }
Olli Etuahoa2d53032015-04-15 14:14:44 +03003606 // ESSL1 sections 5.2 and 5.7:
3607 // ESSL3 section 5.7:
3608 // Ternary operator is not among the operators allowed for structures/arrays.
3609 if (trueBlock->isArray() || trueBlock->getBasicType() == EbtStruct)
3610 {
3611 error(loc, "ternary operator is not allowed for structures or arrays", ":");
3612 recover();
3613 return falseBlock;
3614 }
Olli Etuaho52901742015-04-15 13:42:45 +03003615 return intermediate.addSelection(cond, trueBlock, falseBlock, loc);
3616}
Olli Etuaho49300862015-02-20 14:54:49 +02003617
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003618//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003619// Parse an array of strings using yyparse.
3620//
3621// Returns 0 for success.
3622//
Arun Patole7e7e68d2015-05-22 12:02:25 +05303623int PaParseStrings(size_t count, const char *const string[], const int length[],
3624 TParseContext *context)
3625{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003626 if ((count == 0) || (string == NULL))
3627 return 1;
3628
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003629 if (glslang_initialize(context))
3630 return 1;
3631
alokp@chromium.org408c45e2012-04-05 15:54:43 +00003632 int error = glslang_scan(count, string, length, context);
3633 if (!error)
3634 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003635
alokp@chromium.org73bc2982012-06-19 18:48:05 +00003636 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00003637
alokp@chromium.org6b495712012-06-29 00:06:58 +00003638 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003639}
3640
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003641
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00003642