blob: 8e95560af0f35e72562d1feb9fb107951fbd1d46 [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:
538 case EOpConstructMat3:
539 case EOpConstructMat4:
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000540 constructingMatrix = true;
541 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530542 default:
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000543 break;
544 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000545
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000546 //
547 // Note: It's okay to have too many components available, but not okay to have unused
548 // arguments. 'full' will go to true when enough args have been seen. If we loop
549 // again, there is an extra argument, so 'overfull' will become true.
550 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000551
Jamie Madill94bf7f22013-07-08 13:31:15 -0400552 size_t size = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000553 bool constType = true;
554 bool full = false;
555 bool overFull = false;
556 bool matrixInMatrix = false;
557 bool arrayArg = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530558 for (size_t i = 0; i < function.getParamCount(); ++i)
559 {
560 const TParameter &param = function.getParam(i);
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000561 size += param.type->getObjectSize();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530562
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000563 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000564 matrixInMatrix = true;
565 if (full)
566 overFull = true;
567 if (op != EOpConstructStruct && !type->isArray() && size >= type->getObjectSize())
568 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000569 if (param.type->getQualifier() != EvqConst)
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000570 constType = false;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000571 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000572 arrayArg = true;
573 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530574
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000575 if (constType)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000576 type->setQualifier(EvqConst);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000577
Olli Etuaho376f1b52015-04-13 13:23:41 +0300578 if (type->isArray())
579 {
580 if (type->isUnsizedArray())
581 {
582 type->setArraySize(function.getParamCount());
583 }
584 else if (static_cast<size_t>(type->getArraySize()) != function.getParamCount())
585 {
586 error(line, "array constructor needs one argument per array element", "constructor");
587 return true;
588 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000589 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000590
Arun Patole7e7e68d2015-05-22 12:02:25 +0530591 if (arrayArg && op != EOpConstructStruct)
592 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000593 error(line, "constructing from a non-dereferenced array", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000594 return true;
595 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000596
Arun Patole7e7e68d2015-05-22 12:02:25 +0530597 if (matrixInMatrix && !type->isArray())
598 {
599 if (function.getParamCount() != 1)
600 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000601 error(line, "constructing matrix from matrix can only take one argument", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000602 return true;
603 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000604 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000605
Arun Patole7e7e68d2015-05-22 12:02:25 +0530606 if (overFull)
607 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000608 error(line, "too many arguments", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000609 return true;
610 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530611
612 if (op == EOpConstructStruct && !type->isArray() && type->getStruct()->fields().size() != function.getParamCount())
613 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000614 error(line, "Number of constructor parameters does not match the number of structure fields", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000615 return true;
616 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000617
Arun Patole7e7e68d2015-05-22 12:02:25 +0530618 if (!type->isMatrix() || !matrixInMatrix)
619 {
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000620 if ((op != EOpConstructStruct && size != 1 && size < type->getObjectSize()) ||
Arun Patole7e7e68d2015-05-22 12:02:25 +0530621 (op == EOpConstructStruct && size < type->getObjectSize()))
622 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000623 error(line, "not enough data provided for construction", "constructor");
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000624 return true;
625 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000626 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000627
daniel@transgaming.com0b53fc02011-03-09 15:12:12 +0000628 TIntermTyped *typed = node ? node->getAsTyped() : 0;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530629 if (typed == 0)
630 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000631 error(line, "constructor argument does not have a type", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000632 return true;
633 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530634 if (op != EOpConstructStruct && IsSampler(typed->getBasicType()))
635 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000636 error(line, "cannot convert a sampler", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000637 return true;
638 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530639 if (typed->getBasicType() == EbtVoid)
640 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000641 error(line, "cannot convert a void", "constructor");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000642 return true;
643 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000644
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000645 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000646}
647
648// This function checks to see if a void variable has been declared and raise an error message for such a case
649//
650// returns true in case of an error
651//
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300652bool TParseContext::voidErrorCheck(const TSourceLoc &line, const TString &identifier, const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000653{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300654 if (type == EbtVoid)
655 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000656 error(line, "illegal use of type 'void'", identifier.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000657 return true;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300658 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000659
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000660 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000661}
662
663// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
664//
665// returns true in case of an error
666//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530667bool TParseContext::boolErrorCheck(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000668{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530669 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector())
670 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000671 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000672 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530673 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000674
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000675 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000676}
677
678// This function checks to see if the node (for the expression) contains a scalar boolean expression or not
679//
680// returns true in case of an error
681//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530682bool TParseContext::boolErrorCheck(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000683{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530684 if (pType.type != EbtBool || pType.isAggregate())
685 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000686 error(line, "boolean expression expected", "");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000687 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530688 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000689
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000690 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000691}
692
Arun Patole7e7e68d2015-05-22 12:02:25 +0530693bool TParseContext::samplerErrorCheck(const TSourceLoc &line, const TPublicType &pType, const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000694{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530695 if (pType.type == EbtStruct)
696 {
697 if (containsSampler(*pType.userDef))
698 {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000699 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530700
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000701 return true;
702 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530703
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000704 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530705 }
706 else if (IsSampler(pType.type))
707 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000708 error(line, reason, getBasicString(pType.type));
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000709
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000710 return true;
711 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000712
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000713 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000714}
715
Arun Patole7e7e68d2015-05-22 12:02:25 +0530716bool TParseContext::locationDeclaratorListCheck(const TSourceLoc &line, const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400717{
718 if (pType.layoutQualifier.location != -1)
719 {
720 error(line, "location must only be specified for a single input or output variable", "location");
721 return true;
722 }
723
724 return false;
725}
726
Arun Patole7e7e68d2015-05-22 12:02:25 +0530727bool TParseContext::parameterSamplerErrorCheck(const TSourceLoc &line, TQualifier qualifier, const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000728{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530729 if ((qualifier == EvqOut || qualifier == EvqInOut) &&
730 type.getBasicType() != EbtStruct && IsSampler(type.getBasicType()))
731 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000732 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000733 return true;
734 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000735
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000736 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000737}
738
Arun Patole7e7e68d2015-05-22 12:02:25 +0530739bool TParseContext::containsSampler(const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000740{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000741 if (IsSampler(type.getBasicType()))
742 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000743
Arun Patole7e7e68d2015-05-22 12:02:25 +0530744 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock())
745 {
746 const TFieldList &fields = type.getStruct()->fields();
747 for (unsigned int i = 0; i < fields.size(); ++i)
748 {
Jamie Madill98493dd2013-07-08 14:39:03 -0400749 if (containsSampler(*fields[i]->type()))
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000750 return true;
751 }
752 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000753
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000754 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000755}
756
757//
758// Do size checking for an array type's size.
759//
760// Returns true if there was an error.
761//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530762bool TParseContext::arraySizeErrorCheck(const TSourceLoc &line, TIntermTyped *expr, int &size)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000763{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530764 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000765
Olli Etuahoe7847b02015-03-16 11:56:12 +0200766 if (constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000767 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000768 error(line, "array size must be a constant integer expression", "");
Olli Etuahoe7847b02015-03-16 11:56:12 +0200769 size = 1;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000770 return true;
771 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000772
Nicolas Capens906744a2014-06-06 15:18:07 -0400773 unsigned int unsignedSize = 0;
774
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000775 if (constant->getBasicType() == EbtUInt)
776 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400777 unsignedSize = constant->getUConst(0);
778 size = static_cast<int>(unsignedSize);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000779 }
780 else
781 {
782 size = constant->getIConst(0);
783
Nicolas Capens906744a2014-06-06 15:18:07 -0400784 if (size < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000785 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400786 error(line, "array size must be non-negative", "");
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000787 size = 1;
788 return true;
789 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400790
791 unsignedSize = static_cast<unsigned int>(size);
792 }
793
794 if (size == 0)
795 {
796 error(line, "array size must be greater than zero", "");
797 size = 1;
798 return true;
799 }
800
801 // The size of arrays is restricted here to prevent issues further down the
802 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
803 // 4096 registers so this should be reasonable even for aggressively optimizable code.
804 const unsigned int sizeLimit = 65536;
805
806 if (unsignedSize > sizeLimit)
807 {
808 error(line, "array size too large", "");
809 size = 1;
810 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000811 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000812
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000813 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000814}
815
816//
817// See if this qualifier can be an array.
818//
819// Returns true if there is an error.
820//
Olli Etuaho3739d232015-04-08 12:23:44 +0300821bool TParseContext::arrayQualifierErrorCheck(const TSourceLoc &line, const TPublicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000822{
Olli Etuaho3739d232015-04-08 12:23:44 +0300823 if ((type.qualifier == EvqAttribute) || (type.qualifier == EvqVertexIn) ||
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400824 (type.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300825 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000826 error(line, "cannot declare arrays of this qualifier", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000827 return true;
828 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000829
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000830 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000831}
832
833//
834// See if this type can be an array.
835//
836// Returns true if there is an error.
837//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530838bool TParseContext::arrayTypeErrorCheck(const TSourceLoc &line, const TPublicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000839{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000840 //
841 // Can the type be an array?
842 //
Jamie Madill06145232015-05-13 13:10:01 -0400843 if (type.array)
844 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000845 error(line, "cannot declare arrays of arrays", TType(type).getCompleteString().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000846 return true;
847 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000848
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000849 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000850}
851
852//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000853// Enforce non-initializer type/qualifier rules.
854//
855// Returns true if there was an error.
856//
Olli Etuaho376f1b52015-04-13 13:23:41 +0300857bool TParseContext::nonInitErrorCheck(const TSourceLoc &line, const TString &identifier, TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000858{
Olli Etuaho3739d232015-04-08 12:23:44 +0300859 ASSERT(type != nullptr);
860 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000861 {
862 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300863 type->qualifier = EvqTemporary;
864
865 // Generate informative error messages for ESSL1.
866 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400867 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000868 {
Arun Patole7e7e68d2015-05-22 12:02:25 +0530869 error(line,
870 "structures containing arrays may not be declared constant since they cannot be initialized",
871 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000872 }
873 else
874 {
875 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
876 }
877
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000878 return true;
879 }
Olli Etuaho376f1b52015-04-13 13:23:41 +0300880 if (type->isUnsizedArray())
881 {
882 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
883 return true;
884 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000885 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000886}
887
Olli Etuaho2935c582015-04-08 14:32:06 +0300888// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000889// and update the symbol table.
890//
Olli Etuaho2935c582015-04-08 14:32:06 +0300891// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000892//
Olli Etuaho2935c582015-04-08 14:32:06 +0300893bool TParseContext::declareVariable(const TSourceLoc &line, const TString &identifier, const TType &type,
894 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000895{
Olli Etuaho2935c582015-04-08 14:32:06 +0300896 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000897
Olli Etuaho2935c582015-04-08 14:32:06 +0300898 bool needsReservedErrorCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000899
Olli Etuaho2935c582015-04-08 14:32:06 +0300900 // gl_LastFragData may be redeclared with a new precision qualifier
901 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
902 {
903 const TVariable *maxDrawBuffers =
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400904 static_cast<const TVariable *>(symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho2935c582015-04-08 14:32:06 +0300905 if (type.getArraySize() == maxDrawBuffers->getConstPointer()->getIConst())
906 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400907 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +0300908 {
909 needsReservedErrorCheck = extensionErrorCheck(line, builtInSymbol->getExtension());
910 }
911 }
912 else
913 {
914 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers", identifier.c_str());
915 return false;
916 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000917 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000918
Olli Etuaho2935c582015-04-08 14:32:06 +0300919 if (needsReservedErrorCheck && reservedErrorCheck(line, identifier))
920 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000921
Olli Etuaho2935c582015-04-08 14:32:06 +0300922 (*variable) = new TVariable(&identifier, type);
923 if (!symbolTable.declare(*variable))
924 {
925 error(line, "redefinition", identifier.c_str());
926 delete (*variable);
927 (*variable) = nullptr;
928 return false;
929 }
930
931 if (voidErrorCheck(line, identifier, type.getBasicType()))
932 return false;
933
934 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000935}
936
Arun Patole7e7e68d2015-05-22 12:02:25 +0530937bool TParseContext::paramErrorCheck(const TSourceLoc &line, TQualifier qualifier, TQualifier paramQualifier,
938 TType *type)
939{
940 if (qualifier != EvqConst && qualifier != EvqTemporary)
941 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000942 error(line, "qualifier not allowed on function parameter", getQualifierString(qualifier));
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000943 return true;
944 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530945 if (qualifier == EvqConst && paramQualifier != EvqIn)
946 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000947 error(line, "qualifier not allowed with ", getQualifierString(qualifier), getQualifierString(paramQualifier));
948 return true;
949 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000950
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000951 if (qualifier == EvqConst)
alokp@chromium.org58e54292010-08-24 21:40:03 +0000952 type->setQualifier(EvqConstReadOnly);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000953 else
alokp@chromium.org58e54292010-08-24 21:40:03 +0000954 type->setQualifier(paramQualifier);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000955
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000956 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000957}
958
Arun Patole7e7e68d2015-05-22 12:02:25 +0530959bool TParseContext::extensionErrorCheck(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000960{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530961 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000962 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +0530963 if (iter == extBehavior.end())
964 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000965 error(line, "extension", extension.c_str(), "is not supported");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000966 return true;
967 }
zmo@google.comf5450912011-09-09 01:37:19 +0000968 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +0530969 if (iter->second == EBhDisable || iter->second == EBhUndefined)
970 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000971 error(line, "extension", extension.c_str(), "is disabled");
972 return true;
973 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530974 if (iter->second == EBhWarn)
975 {
alokp@chromium.org8b851c62012-06-15 16:25:11 +0000976 warning(line, "extension", extension.c_str(), "is being used");
alokp@chromium.org8815d7f2010-09-09 17:30:03 +0000977 return false;
978 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000979
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000980 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000981}
982
Olli Etuahofa33d582015-04-09 14:33:12 +0300983// These checks are common for all declarations starting a declarator list, and declarators that follow an empty
984// declaration.
985//
Jamie Madill06145232015-05-13 13:10:01 -0400986bool TParseContext::singleDeclarationErrorCheck(const TPublicType &publicType, const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -0400987{
Olli Etuahofa33d582015-04-09 14:33:12 +0300988 switch (publicType.qualifier)
989 {
990 case EvqVaryingIn:
991 case EvqVaryingOut:
992 case EvqAttribute:
993 case EvqVertexIn:
994 case EvqFragmentOut:
995 if (publicType.type == EbtStruct)
996 {
997 error(identifierLocation, "cannot be used with a structure",
998 getQualifierString(publicType.qualifier));
999 return true;
1000 }
1001
1002 default: break;
1003 }
1004
1005 if (publicType.qualifier != EvqUniform && samplerErrorCheck(identifierLocation, publicType,
1006 "samplers must be uniform"))
1007 {
Jamie Madilla5efff92013-06-06 11:56:47 -04001008 return true;
Olli Etuahofa33d582015-04-09 14:33:12 +03001009 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001010
1011 // check for layout qualifier issues
1012 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
1013
1014 if (layoutQualifier.matrixPacking != EmpUnspecified)
1015 {
Olli Etuahofa33d582015-04-09 14:33:12 +03001016 error(identifierLocation, "layout qualifier", getMatrixPackingString(layoutQualifier.matrixPacking),
1017 "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -04001018 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001019 }
1020
1021 if (layoutQualifier.blockStorage != EbsUnspecified)
1022 {
Olli Etuahofa33d582015-04-09 14:33:12 +03001023 error(identifierLocation, "layout qualifier", getBlockStorageString(layoutQualifier.blockStorage),
1024 "only valid for interface blocks");
Jamie Madill51a53c72013-06-19 09:24:43 -04001025 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001026 }
1027
Olli Etuahofa33d582015-04-09 14:33:12 +03001028 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut &&
1029 layoutLocationErrorCheck(identifierLocation, publicType.layoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04001030 {
Jamie Madill51a53c72013-06-19 09:24:43 -04001031 return true;
Jamie Madilla5efff92013-06-06 11:56:47 -04001032 }
1033
1034 return false;
1035}
1036
Arun Patole7e7e68d2015-05-22 12:02:25 +05301037bool TParseContext::layoutLocationErrorCheck(const TSourceLoc &location, const TLayoutQualifier &layoutQualifier)
Jamie Madilla5efff92013-06-06 11:56:47 -04001038{
1039 if (layoutQualifier.location != -1)
1040 {
1041 error(location, "invalid layout qualifier:", "location", "only valid on program inputs and outputs");
1042 return true;
1043 }
1044
1045 return false;
1046}
1047
Olli Etuahob6e07a62015-02-16 12:22:10 +02001048bool TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate, TIntermAggregate *aggregate)
1049{
1050 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1051 {
1052 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1053 if (qual == EvqOut || qual == EvqInOut)
1054 {
1055 TIntermTyped *node = (*(aggregate->getSequence()))[i]->getAsTyped();
1056 if (lValueErrorCheck(node->getLine(), "assign", node))
1057 {
1058 error(node->getLine(),
1059 "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error");
1060 recover();
1061 return true;
1062 }
1063 }
1064 }
1065 return false;
1066}
1067
Olli Etuaho37ad4742015-04-27 13:18:50 +03001068void TParseContext::es3InvariantErrorCheck(const TQualifier qualifier, const TSourceLoc &invariantLocation)
1069{
1070 if (!sh::IsVaryingOut(qualifier) && qualifier != EvqFragmentOut)
1071 {
1072 error(invariantLocation, "Only out variables can be invariant.", "invariant");
1073 recover();
1074 }
1075}
1076
Arun Patole7e7e68d2015-05-22 12:02:25 +05301077bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001078{
Arun Patole7e7e68d2015-05-22 12:02:25 +05301079 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001080 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1081 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001082}
1083
Arun Patole7e7e68d2015-05-22 12:02:25 +05301084bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001085{
Arun Patole7e7e68d2015-05-22 12:02:25 +05301086 const TExtensionBehavior &extbehavior = extensionBehavior();
Shannon Woodsa49a9bf2013-08-02 17:23:14 -04001087 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001088
1089 if (iter == extbehavior.end())
1090 {
1091 return false;
1092 }
1093
1094 return (iter->second == EBhEnable || iter->second == EBhRequire);
1095}
1096
Arun Patole7e7e68d2015-05-22 12:02:25 +05301097void TParseContext::handleExtensionDirective(const TSourceLoc &loc, const char *extName, const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001098{
1099 pp::SourceLocation srcLoc;
1100 srcLoc.file = loc.first_file;
1101 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001102 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001103}
1104
Arun Patole7e7e68d2015-05-22 12:02:25 +05301105void TParseContext::handlePragmaDirective(const TSourceLoc &loc, const char *name, const char *value, bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001106{
1107 pp::SourceLocation srcLoc;
1108 srcLoc.file = loc.first_file;
1109 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001110 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001111}
1112
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001113/////////////////////////////////////////////////////////////////////////////////
1114//
1115// Non-Errors.
1116//
1117/////////////////////////////////////////////////////////////////////////////////
1118
Jamie Madill5c097022014-08-20 16:38:32 -04001119const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1120 const TString *name,
1121 const TSymbol *symbol)
1122{
1123 const TVariable *variable = NULL;
1124
1125 if (!symbol)
1126 {
1127 error(location, "undeclared identifier", name->c_str());
1128 recover();
1129 }
1130 else if (!symbol->isVariable())
1131 {
1132 error(location, "variable expected", name->c_str());
1133 recover();
1134 }
1135 else
1136 {
1137 variable = static_cast<const TVariable*>(symbol);
1138
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001139 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
Jamie Madill5c097022014-08-20 16:38:32 -04001140 !variable->getExtension().empty() &&
1141 extensionErrorCheck(location, variable->getExtension()))
1142 {
1143 recover();
1144 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001145
1146 // Reject shaders using both gl_FragData and gl_FragColor
1147 TQualifier qualifier = variable->getType().getQualifier();
1148 if (qualifier == EvqFragData)
1149 {
1150 mUsesFragData = true;
1151 }
1152 else if (qualifier == EvqFragColor)
1153 {
1154 mUsesFragColor = true;
1155 }
1156
1157 // This validation is not quite correct - it's only an error to write to
1158 // both FragData and FragColor. For simplicity, and because users shouldn't
1159 // be rewarded for reading from undefined varaibles, return an error
1160 // if they are both referenced, rather than assigned.
1161 if (mUsesFragData && mUsesFragColor)
1162 {
1163 error(location, "cannot use both gl_FragData and gl_FragColor", name->c_str());
1164 recover();
1165 }
Jamie Madill5c097022014-08-20 16:38:32 -04001166 }
1167
1168 if (!variable)
1169 {
1170 TType type(EbtFloat, EbpUndefined);
1171 TVariable *fakeVariable = new TVariable(name, type);
1172 symbolTable.declare(fakeVariable);
1173 variable = fakeVariable;
1174 }
1175
1176 return variable;
1177}
1178
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001179//
1180// Look up a function name in the symbol table, and make sure it is a function.
1181//
1182// Return the function symbol if found, otherwise 0.
1183//
Arun Patole7e7e68d2015-05-22 12:02:25 +05301184const TFunction *TParseContext::findFunction(const TSourceLoc &line, TFunction *call, int inputShaderVersion,
1185 bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001186{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001187 // First find by unmangled name to check whether the function name has been
1188 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001189 // If a function is found, check for one with a matching argument list.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301190 const TSymbol *symbol = symbolTable.find(call->getName(), inputShaderVersion, builtIn);
1191 if (symbol == 0 || symbol->isFunction())
1192 {
Austin Kinross3ae64652015-01-26 15:51:39 -08001193 symbol = symbolTable.find(call->getMangledName(), inputShaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001194 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001195
Arun Patole7e7e68d2015-05-22 12:02:25 +05301196 if (symbol == 0)
1197 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001198 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001199 return 0;
1200 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001201
Arun Patole7e7e68d2015-05-22 12:02:25 +05301202 if (!symbol->isFunction())
1203 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001204 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001205 return 0;
1206 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001207
1208 return static_cast<const TFunction*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001209}
1210
1211//
1212// Initializers show up in several places in the grammar. Have one set of
1213// code to handle them here.
1214//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001215// Returns true on error, false if no error
1216//
Jamie Madill06145232015-05-13 13:10:01 -04001217bool TParseContext::executeInitializer(const TSourceLoc &line, const TString &identifier, const TPublicType &pType,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001218 TIntermTyped *initializer, TIntermNode **intermNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001219{
Olli Etuahoe7847b02015-03-16 11:56:12 +02001220 ASSERT(intermNode != nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001221 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001222
Olli Etuaho2935c582015-04-08 14:32:06 +03001223 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001224 if (type.isUnsizedArray())
1225 {
1226 type.setArraySize(initializer->getArraySize());
1227 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001228 if (!declareVariable(line, identifier, type, &variable))
1229 {
1230 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001231 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001232
Olli Etuahob0c645e2015-05-12 14:25:36 +03001233 bool globalInitWarning = false;
1234 if (symbolTable.atGlobalLevel() && !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
1235 {
1236 // Error message does not completely match behavior with ESSL 1.00, but
1237 // we want to steer developers towards only using constant expressions.
1238 error(line, "global variable initializers must be constant expressions", "=");
1239 return true;
1240 }
1241 if (globalInitWarning)
1242 {
1243 warning(line, "global variable initializers should be constant expressions "
1244 "(uniforms and globals are allowed in global initializers for legacy compatibility)", "=");
1245 }
1246
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001247 //
1248 // identifier must be of type constant, a global, or a temporary
1249 //
1250 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301251 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1252 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001253 error(line, " cannot initialize this type of qualifier ", variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001254 return true;
1255 }
1256 //
1257 // test for and propagate constant
1258 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001259
Arun Patole7e7e68d2015-05-22 12:02:25 +05301260 if (qualifier == EvqConst)
1261 {
1262 if (qualifier != initializer->getType().getQualifier())
1263 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001264 std::stringstream extraInfoStream;
1265 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1266 std::string extraInfo = extraInfoStream.str();
1267 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001268 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001269 return true;
1270 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301271 if (type != initializer->getType())
1272 {
1273 error(line, " non-matching types for const initializer ",
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001274 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001275 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001276 return true;
1277 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301278 if (initializer->getAsConstantUnion())
1279 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001280 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Arun Patole7e7e68d2015-05-22 12:02:25 +05301281 }
1282 else if (initializer->getAsSymbolNode())
1283 {
1284 const TSymbol *symbol = symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1285 const TVariable *tVar = static_cast<const TVariable*>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001286
Arun Patole7e7e68d2015-05-22 12:02:25 +05301287 TConstantUnion *constArray = tVar->getConstPointer();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001288 variable->shareConstPointer(constArray);
Arun Patole7e7e68d2015-05-22 12:02:25 +05301289 }
1290 else
1291 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001292 std::stringstream extraInfoStream;
1293 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1294 std::string extraInfo = extraInfoStream.str();
1295 error(line, " cannot assign to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001296 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001297 return true;
1298 }
1299 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001300
1301 if (qualifier != EvqConst)
1302 {
1303 TIntermSymbol *intermSymbol = intermediate.addSymbol(variable->getUniqueId(), variable->getName(),
1304 variable->getType(), line);
1305 *intermNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1306 if (*intermNode == nullptr)
1307 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001308 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1309 return true;
1310 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001311 }
1312 else
1313 {
1314 *intermNode = nullptr;
1315 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001316
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001317 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001318}
1319
Arun Patole7e7e68d2015-05-22 12:02:25 +05301320bool TParseContext::areAllChildConst(TIntermAggregate *aggrNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001321{
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001322 ASSERT(aggrNode != NULL);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001323 if (!aggrNode->isConstructor())
1324 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001325
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001326 bool allConstant = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001327
Arun Patole7e7e68d2015-05-22 12:02:25 +05301328 // check if all the child nodes are constants so that they can be inserted into
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001329 // the parent node
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001330 TIntermSequence *sequence = aggrNode->getSequence() ;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301331 for (TIntermSequence::iterator p = sequence->begin(); p != sequence->end(); ++p)
1332 {
alokp@chromium.orgd300f5b2010-10-14 16:10:20 +00001333 if (!(*p)->getAsTyped()->getAsConstantUnion())
1334 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001335 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001336
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001337 return allConstant;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001338}
1339
Olli Etuaho214c2d82015-04-27 14:49:13 +03001340TPublicType TParseContext::addFullySpecifiedType(TQualifier qualifier, bool invariant, TLayoutQualifier layoutQualifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301341 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001342{
1343 TPublicType returnType = typeSpecifier;
1344 returnType.qualifier = qualifier;
Olli Etuaho214c2d82015-04-27 14:49:13 +03001345 returnType.invariant = invariant;
Jamie Madilla5efff92013-06-06 11:56:47 -04001346 returnType.layoutQualifier = layoutQualifier;
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001347
1348 if (typeSpecifier.array)
1349 {
1350 error(typeSpecifier.line, "not supported", "first-class array");
1351 recover();
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001352 returnType.clearArrayness();
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001353 }
1354
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001355 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001356 {
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001357 if (qualifier == EvqAttribute && (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1358 {
1359 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1360 recover();
1361 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001362
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001363 if ((qualifier == EvqVaryingIn || qualifier == EvqVaryingOut) &&
1364 (typeSpecifier.type == EbtBool || typeSpecifier.type == EbtInt))
1365 {
1366 error(typeSpecifier.line, "cannot be bool or int", getQualifierString(qualifier));
1367 recover();
1368 }
1369 }
1370 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001371 {
Jamie Madillb120eac2013-06-12 14:08:13 -04001372 switch (qualifier)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001373 {
Jamie Madill19571812013-08-12 15:26:34 -07001374 case EvqSmoothIn:
1375 case EvqSmoothOut:
1376 case EvqVertexOut:
1377 case EvqFragmentIn:
1378 case EvqCentroidOut:
1379 case EvqCentroidIn:
1380 if (typeSpecifier.type == EbtBool)
1381 {
1382 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1383 recover();
1384 }
1385 if (typeSpecifier.type == EbtInt || typeSpecifier.type == EbtUInt)
1386 {
1387 error(typeSpecifier.line, "must use 'flat' interpolation here", getQualifierString(qualifier));
1388 recover();
1389 }
1390 break;
1391
1392 case EvqVertexIn:
1393 case EvqFragmentOut:
1394 case EvqFlatIn:
1395 case EvqFlatOut:
Jamie Madillb120eac2013-06-12 14:08:13 -04001396 if (typeSpecifier.type == EbtBool)
1397 {
1398 error(typeSpecifier.line, "cannot be bool", getQualifierString(qualifier));
1399 recover();
1400 }
1401 break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001402
Jamie Madillb120eac2013-06-12 14:08:13 -04001403 default: break;
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001404 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001405 }
1406
1407 return returnType;
1408}
1409
Olli Etuahofa33d582015-04-09 14:33:12 +03001410TIntermAggregate *TParseContext::parseSingleDeclaration(TPublicType &publicType,
1411 const TSourceLoc &identifierOrTypeLocation,
1412 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001413{
Olli Etuahofa33d582015-04-09 14:33:12 +03001414 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001415
Olli Etuahobab4c082015-04-24 16:38:49 +03001416 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03001417
Olli Etuahobab4c082015-04-24 16:38:49 +03001418 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1419
1420 if (emptyDeclaration)
1421 {
1422 if (publicType.isUnsizedArray())
1423 {
1424 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an error.
1425 // It is assumed that this applies to empty declarations as well.
1426 error(identifierOrTypeLocation, "empty array declaration needs to specify a size", identifier.c_str());
1427 }
1428 }
1429 else
Jamie Madill60ed9812013-06-06 11:56:46 -04001430 {
Olli Etuahofa33d582015-04-09 14:33:12 +03001431 if (singleDeclarationErrorCheck(publicType, identifierOrTypeLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001432 recover();
1433
Olli Etuaho376f1b52015-04-13 13:23:41 +03001434 if (nonInitErrorCheck(identifierOrTypeLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001435 recover();
1436
Olli Etuaho2935c582015-04-08 14:32:06 +03001437 TVariable *variable = nullptr;
Olli Etuahofa33d582015-04-09 14:33:12 +03001438 if (!declareVariable(identifierOrTypeLocation, identifier, TType(publicType), &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001439 recover();
1440
1441 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001442 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001443 }
1444
Olli Etuahoe7847b02015-03-16 11:56:12 +02001445 return intermediate.makeAggregate(symbol, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001446}
1447
Olli Etuahoe7847b02015-03-16 11:56:12 +02001448TIntermAggregate *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1449 const TSourceLoc &identifierLocation,
1450 const TString &identifier,
1451 const TSourceLoc &indexLocation,
1452 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001453{
Olli Etuahofa33d582015-04-09 14:33:12 +03001454 mDeferredSingleDeclarationErrorCheck = false;
1455
1456 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001457 recover();
1458
Olli Etuaho376f1b52015-04-13 13:23:41 +03001459 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill60ed9812013-06-06 11:56:46 -04001460 recover();
1461
1462 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1463 {
1464 recover();
1465 }
1466
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001467 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001468
1469 int size;
1470 if (arraySizeErrorCheck(identifierLocation, indexExpression, size))
1471 {
1472 recover();
1473 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001474 // Make the type an array even if size check failed.
1475 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1476 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001477
Olli Etuaho2935c582015-04-08 14:32:06 +03001478 TVariable *variable = nullptr;
1479 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill60ed9812013-06-06 11:56:46 -04001480 recover();
1481
Olli Etuahoe7847b02015-03-16 11:56:12 +02001482 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001483 if (variable && symbol)
Jamie Madill60ed9812013-06-06 11:56:46 -04001484 symbol->setId(variable->getUniqueId());
Jamie Madill60ed9812013-06-06 11:56:46 -04001485
Olli Etuahoe7847b02015-03-16 11:56:12 +02001486 return intermediate.makeAggregate(symbol, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001487}
1488
Jamie Madill06145232015-05-13 13:10:01 -04001489TIntermAggregate *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001490 const TSourceLoc &identifierLocation,
1491 const TString &identifier,
1492 const TSourceLoc &initLocation,
1493 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001494{
Olli Etuahofa33d582015-04-09 14:33:12 +03001495 mDeferredSingleDeclarationErrorCheck = false;
1496
1497 if (singleDeclarationErrorCheck(publicType, identifierLocation))
Jamie Madill60ed9812013-06-06 11:56:46 -04001498 recover();
1499
Olli Etuahoe7847b02015-03-16 11:56:12 +02001500 TIntermNode *intermNode = nullptr;
1501 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04001502 {
1503 //
1504 // Build intermediate representation
1505 //
Olli Etuahoe7847b02015-03-16 11:56:12 +02001506 return intermNode ? intermediate.makeAggregate(intermNode, initLocation) : nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001507 }
1508 else
1509 {
1510 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001511 return nullptr;
Jamie Madill60ed9812013-06-06 11:56:46 -04001512 }
1513}
1514
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001515TIntermAggregate *TParseContext::parseSingleArrayInitDeclaration(TPublicType &publicType,
1516 const TSourceLoc &identifierLocation,
1517 const TString &identifier,
1518 const TSourceLoc &indexLocation,
1519 TIntermTyped *indexExpression,
1520 const TSourceLoc &initLocation,
1521 TIntermTyped *initializer)
1522{
1523 mDeferredSingleDeclarationErrorCheck = false;
1524
1525 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1526 recover();
1527
1528 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1529 {
1530 recover();
1531 }
1532
1533 TPublicType arrayType(publicType);
1534
Olli Etuaho376f1b52015-04-13 13:23:41 +03001535 int size = 0;
1536 // If indexExpression is nullptr, then the array will eventually get its size implicitly from the initializer.
1537 if (indexExpression != nullptr && arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001538 {
1539 recover();
1540 }
1541 // Make the type an array even if size check failed.
1542 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1543 arrayType.setArraySize(size);
1544
1545 // initNode will correspond to the whole of "type b[n] = initializer".
1546 TIntermNode *initNode = nullptr;
1547 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1548 {
1549 return initNode ? intermediate.makeAggregate(initNode, initLocation) : nullptr;
1550 }
1551 else
1552 {
1553 recover();
1554 return nullptr;
1555 }
1556}
1557
Olli Etuahoe7847b02015-03-16 11:56:12 +02001558TIntermAggregate *TParseContext::parseInvariantDeclaration(const TSourceLoc &invariantLoc,
Jamie Madill47e3ec02014-08-20 16:38:33 -04001559 const TSourceLoc &identifierLoc,
1560 const TString *identifier,
1561 const TSymbol *symbol)
1562{
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001563 // invariant declaration
Jamie Madill47e3ec02014-08-20 16:38:33 -04001564 if (globalErrorCheck(invariantLoc, symbolTable.atGlobalLevel(), "invariant varying"))
1565 {
1566 recover();
1567 }
1568
1569 if (!symbol)
1570 {
1571 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
1572 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001573 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001574 }
1575 else
1576 {
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001577 const TString kGlFrontFacing("gl_FrontFacing");
1578 if (*identifier == kGlFrontFacing)
1579 {
1580 error(identifierLoc, "identifier should not be declared as invariant", identifier->c_str());
1581 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001582 return nullptr;
Zhenyao Mo94ac7b72014-10-15 18:22:08 -07001583 }
Jamie Madill2c433252014-12-03 12:36:54 -05001584 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001585 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
1586 ASSERT(variable);
1587 const TType &type = variable->getType();
1588 TIntermSymbol *intermSymbol = intermediate.addSymbol(variable->getUniqueId(),
1589 *identifier, type, identifierLoc);
1590
1591 TIntermAggregate *aggregate = intermediate.makeAggregate(intermSymbol, identifierLoc);
1592 aggregate->setOp(EOpInvariantDeclaration);
1593 return aggregate;
Jamie Madill47e3ec02014-08-20 16:38:33 -04001594 }
1595}
1596
Olli Etuahoe7847b02015-03-16 11:56:12 +02001597TIntermAggregate *TParseContext::parseDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1598 const TSourceLoc &identifierLocation, const TString &identifier)
Jamie Madill502d66f2013-06-20 11:55:52 -04001599{
Olli Etuahofa33d582015-04-09 14:33:12 +03001600 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1601 if (mDeferredSingleDeclarationErrorCheck)
1602 {
1603 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1604 recover();
1605 mDeferredSingleDeclarationErrorCheck = false;
1606 }
1607
Jamie Madill0bd18df2013-06-20 11:55:52 -04001608 if (locationDeclaratorListCheck(identifierLocation, publicType))
1609 recover();
1610
Olli Etuaho376f1b52015-04-13 13:23:41 +03001611 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001612 recover();
1613
Olli Etuaho2935c582015-04-08 14:32:06 +03001614 TVariable *variable = nullptr;
1615 if (!declareVariable(identifierLocation, identifier, TType(publicType), &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001616 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001617
1618 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
1619 if (variable && symbol)
Jamie Madill502d66f2013-06-20 11:55:52 -04001620 symbol->setId(variable->getUniqueId());
1621
Olli Etuahoe7847b02015-03-16 11:56:12 +02001622 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001623}
1624
Olli Etuahoe7847b02015-03-16 11:56:12 +02001625TIntermAggregate *TParseContext::parseArrayDeclarator(TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
1626 const TSourceLoc &identifierLocation, const TString &identifier,
1627 const TSourceLoc &arrayLocation, TIntermTyped *indexExpression)
Jamie Madill502d66f2013-06-20 11:55:52 -04001628{
Olli Etuahofa33d582015-04-09 14:33:12 +03001629 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1630 if (mDeferredSingleDeclarationErrorCheck)
1631 {
1632 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1633 recover();
1634 mDeferredSingleDeclarationErrorCheck = false;
1635 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001636
Jamie Madill0bd18df2013-06-20 11:55:52 -04001637 if (locationDeclaratorListCheck(identifierLocation, publicType))
1638 recover();
1639
Olli Etuaho376f1b52015-04-13 13:23:41 +03001640 if (nonInitErrorCheck(identifierLocation, identifier, &publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04001641 recover();
1642
1643 if (arrayTypeErrorCheck(arrayLocation, publicType) || arrayQualifierErrorCheck(arrayLocation, publicType))
1644 {
1645 recover();
1646 }
Olli Etuaho93a90fd2015-04-07 18:14:07 +03001647 else
Jamie Madill502d66f2013-06-20 11:55:52 -04001648 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001649 TType arrayType = TType(publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04001650 int size;
1651 if (arraySizeErrorCheck(arrayLocation, indexExpression, size))
Olli Etuahoe7847b02015-03-16 11:56:12 +02001652 {
Jamie Madill502d66f2013-06-20 11:55:52 -04001653 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001654 }
Olli Etuaho693c9aa2015-04-07 17:50:36 +03001655 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001656
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001657 TVariable *variable = nullptr;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001658 if (!declareVariable(identifierLocation, identifier, arrayType, &variable))
Jamie Madill502d66f2013-06-20 11:55:52 -04001659 recover();
Jamie Madill502d66f2013-06-20 11:55:52 -04001660
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001661 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
1662 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001663 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02001664
1665 return intermediate.growAggregate(aggregateDeclaration, symbol, identifierLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001666 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001667
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001668 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001669}
1670
Jamie Madill06145232015-05-13 13:10:01 -04001671TIntermAggregate *TParseContext::parseInitDeclarator(const TPublicType &publicType, TIntermAggregate *aggregateDeclaration,
Olli Etuahoe7847b02015-03-16 11:56:12 +02001672 const TSourceLoc &identifierLocation, const TString &identifier,
1673 const TSourceLoc &initLocation, TIntermTyped *initializer)
Jamie Madill502d66f2013-06-20 11:55:52 -04001674{
Olli Etuahofa33d582015-04-09 14:33:12 +03001675 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1676 if (mDeferredSingleDeclarationErrorCheck)
1677 {
1678 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1679 recover();
1680 mDeferredSingleDeclarationErrorCheck = false;
1681 }
Jamie Madill502d66f2013-06-20 11:55:52 -04001682
Jamie Madill0bd18df2013-06-20 11:55:52 -04001683 if (locationDeclaratorListCheck(identifierLocation, publicType))
1684 recover();
1685
Olli Etuahoe7847b02015-03-16 11:56:12 +02001686 TIntermNode *intermNode = nullptr;
1687 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &intermNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04001688 {
1689 //
1690 // build the intermediate representation
1691 //
1692 if (intermNode)
1693 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001694 return intermediate.growAggregate(aggregateDeclaration, intermNode, initLocation);
Jamie Madill502d66f2013-06-20 11:55:52 -04001695 }
1696 else
1697 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02001698 return aggregateDeclaration;
Jamie Madill502d66f2013-06-20 11:55:52 -04001699 }
1700 }
1701 else
1702 {
1703 recover();
Olli Etuahoe7847b02015-03-16 11:56:12 +02001704 return nullptr;
Jamie Madill502d66f2013-06-20 11:55:52 -04001705 }
1706}
1707
Jamie Madill06145232015-05-13 13:10:01 -04001708TIntermAggregate *TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001709 TIntermAggregate *aggregateDeclaration,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301710 const TSourceLoc &identifierLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001711 const TString &identifier,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301712 const TSourceLoc &indexLocation,
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001713 TIntermTyped *indexExpression,
1714 const TSourceLoc &initLocation, TIntermTyped *initializer)
1715{
1716 // If the declaration starting this declarator list was empty (example: int,), some checks were not performed.
1717 if (mDeferredSingleDeclarationErrorCheck)
1718 {
1719 if (singleDeclarationErrorCheck(publicType, identifierLocation))
1720 recover();
1721 mDeferredSingleDeclarationErrorCheck = false;
1722 }
1723
1724 if (locationDeclaratorListCheck(identifierLocation, publicType))
1725 recover();
1726
1727 if (arrayTypeErrorCheck(indexLocation, publicType) || arrayQualifierErrorCheck(indexLocation, publicType))
1728 {
1729 recover();
1730 }
1731
1732 TPublicType arrayType(publicType);
1733
Olli Etuaho376f1b52015-04-13 13:23:41 +03001734 int size = 0;
1735 // If indexExpression is nullptr, then the array will eventually get its size implicitly from the initializer.
1736 if (indexExpression != nullptr && arraySizeErrorCheck(identifierLocation, indexExpression, size))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001737 {
1738 recover();
1739 }
1740 // Make the type an array even if size check failed.
1741 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1742 arrayType.setArraySize(size);
1743
1744 // initNode will correspond to the whole of "b[n] = initializer".
1745 TIntermNode *initNode = nullptr;
1746 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
1747 {
1748 if (initNode)
1749 {
1750 return intermediate.growAggregate(aggregateDeclaration, initNode, initLocation);
1751 }
1752 else
1753 {
1754 return aggregateDeclaration;
1755 }
1756 }
1757 else
1758 {
1759 recover();
1760 return nullptr;
1761 }
1762}
1763
Jamie Madilla295edf2013-06-06 11:56:48 -04001764void TParseContext::parseGlobalLayoutQualifier(const TPublicType &typeQualifier)
1765{
1766 if (typeQualifier.qualifier != EvqUniform)
1767 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301768 error(typeQualifier.line,
1769 "invalid qualifier:",
1770 getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
Jamie Madilla295edf2013-06-06 11:56:48 -04001771 recover();
1772 return;
1773 }
1774
1775 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
1776 ASSERT(!layoutQualifier.isEmpty());
1777
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001778 if (mShaderVersion < 300)
Jamie Madilla295edf2013-06-06 11:56:48 -04001779 {
1780 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 only", "layout");
1781 recover();
1782 return;
1783 }
1784
1785 if (layoutLocationErrorCheck(typeQualifier.line, typeQualifier.layoutQualifier))
1786 {
1787 recover();
1788 return;
1789 }
1790
Jamie Madill099c0f32013-06-20 11:55:52 -04001791 if (layoutQualifier.matrixPacking != EmpUnspecified)
1792 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001793 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04001794 }
1795
Geoff Langc6856732014-02-11 09:38:55 -05001796 if (layoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04001797 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001798 mDefaultBlockStorage = layoutQualifier.blockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04001799 }
Jamie Madilla295edf2013-06-06 11:56:48 -04001800}
1801
Jamie Madill06145232015-05-13 13:10:01 -04001802TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001803{
Jamie Madill06145232015-05-13 13:10:01 -04001804 TPublicType publicType = publicTypeIn;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001805 TOperator op = EOpNull;
1806 if (publicType.userDef)
1807 {
1808 op = EOpConstructStruct;
1809 }
1810 else
1811 {
1812 switch (publicType.type)
1813 {
1814 case EbtFloat:
1815 if (publicType.isMatrix())
1816 {
1817 // TODO: non-square matrices
1818 switch(publicType.getCols())
1819 {
Jamie Madill28b97422013-07-08 14:01:38 -04001820 case 2: op = EOpConstructMat2; break;
1821 case 3: op = EOpConstructMat3; break;
1822 case 4: op = EOpConstructMat4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001823 }
1824 }
1825 else
1826 {
1827 switch(publicType.getNominalSize())
1828 {
Jamie Madill28b97422013-07-08 14:01:38 -04001829 case 1: op = EOpConstructFloat; break;
1830 case 2: op = EOpConstructVec2; break;
1831 case 3: op = EOpConstructVec3; break;
1832 case 4: op = EOpConstructVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001833 }
1834 }
1835 break;
1836
1837 case EbtInt:
1838 switch(publicType.getNominalSize())
1839 {
Jamie Madill28b97422013-07-08 14:01:38 -04001840 case 1: op = EOpConstructInt; break;
1841 case 2: op = EOpConstructIVec2; break;
1842 case 3: op = EOpConstructIVec3; break;
1843 case 4: op = EOpConstructIVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001844 }
1845 break;
1846
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001847 case EbtUInt:
1848 switch(publicType.getNominalSize())
1849 {
Jamie Madill28b97422013-07-08 14:01:38 -04001850 case 1: op = EOpConstructUInt; break;
1851 case 2: op = EOpConstructUVec2; break;
1852 case 3: op = EOpConstructUVec3; break;
1853 case 4: op = EOpConstructUVec4; break;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +00001854 }
1855 break;
1856
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001857 case EbtBool:
1858 switch(publicType.getNominalSize())
1859 {
Jamie Madill28b97422013-07-08 14:01:38 -04001860 case 1: op = EOpConstructBool; break;
1861 case 2: op = EOpConstructBVec2; break;
1862 case 3: op = EOpConstructBVec3; break;
1863 case 4: op = EOpConstructBVec4; break;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00001864 }
1865 break;
1866
1867 default: break;
1868 }
1869
1870 if (op == EOpNull)
1871 {
1872 error(publicType.line, "cannot construct this type", getBasicString(publicType.type));
1873 recover();
1874 publicType.type = EbtFloat;
1875 op = EOpConstructFloat;
1876 }
1877 }
1878
1879 TString tempString;
1880 TType type(publicType);
1881 return new TFunction(&tempString, type, op);
1882}
1883
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001884// This function is used to test for the correctness of the parameters passed to various constructor functions
Arun Patole7e7e68d2015-05-22 12:02:25 +05301885// and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001886//
1887// Returns 0 for an error or the constructed node (aggregate or typed) for no error.
1888//
Arun Patole7e7e68d2015-05-22 12:02:25 +05301889TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments, TType *type, TOperator op, TFunction *fnCall,
1890 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001891{
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001892 TIntermAggregate *aggregateArguments = arguments->getAsAggregate();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001893
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001894 if (!aggregateArguments)
1895 {
1896 aggregateArguments = new TIntermAggregate;
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001897 aggregateArguments->getSequence()->push_back(arguments);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001898 }
1899
Olli Etuahof40319e2015-03-10 14:33:00 +02001900 if (type->isArray())
1901 {
1902 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of the array.
1903 TIntermSequence *args = aggregateArguments->getSequence();
1904 for (size_t i = 0; i < args->size(); i++)
1905 {
1906 const TType &argType = (*args)[i]->getAsTyped()->getType();
1907 // It has already been checked that the argument is not an array.
1908 ASSERT(!argType.isArray());
1909 if (!argType.sameElementType(*type))
1910 {
1911 error(line, "Array constructor argument has an incorrect type", "Error");
1912 recover();
1913 return nullptr;
1914 }
1915 }
1916 }
1917 else if (op == EOpConstructStruct)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001918 {
1919 const TFieldList &fields = type->getStruct()->fields();
Zhenyao Moe40d1e92014-07-16 17:40:36 -07001920 TIntermSequence *args = aggregateArguments->getSequence();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001921
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001922 for (size_t i = 0; i < fields.size(); i++)
1923 {
Nicolas Capensffd73872014-08-21 13:49:16 -04001924 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001925 {
1926 error(line, "Structure constructor arguments do not match structure fields", "Error");
1927 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001928
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001929 return 0;
1930 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001931 }
1932 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001933
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001934 // Turn the argument list itself into a constructor
Olli Etuaho21203702014-11-13 16:16:21 +02001935 TIntermAggregate *constructor = intermediate.setAggregateOperator(aggregateArguments, op, line);
1936 TIntermTyped *constConstructor = foldConstConstructor(constructor, *type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001937 if (constConstructor)
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001938 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001939 return constConstructor;
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04001940 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001941
Olli Etuaho21203702014-11-13 16:16:21 +02001942 // Structs should not be precision qualified, the individual members may be.
1943 // Built-in types on the other hand should be precision qualified.
1944 if (op != EOpConstructStruct)
1945 {
1946 constructor->setPrecisionFromChildren();
1947 type->setPrecision(constructor->getPrecision());
1948 }
1949
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001950 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001951}
1952
Arun Patole7e7e68d2015-05-22 12:02:25 +05301953TIntermTyped *TParseContext::foldConstConstructor(TIntermAggregate *aggrNode, const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001954{
Olli Etuahof40319e2015-03-10 14:33:00 +02001955 // TODO: Add support for folding array constructors
1956 bool canBeFolded = areAllChildConst(aggrNode) && !type.isArray();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001957 aggrNode->setType(type);
Arun Patole7e7e68d2015-05-22 12:02:25 +05301958 if (canBeFolded)
1959 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001960 bool returnVal = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301961 TConstantUnion *unionArray = new TConstantUnion[type.getObjectSize()];
1962 if (aggrNode->getSequence()->size() == 1)
1963 {
1964 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(), type,
1965 true);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001966 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301967 else
1968 {
1969 returnVal = intermediate.parseConstTree(aggrNode->getLine(), aggrNode, unionArray, aggrNode->getOp(),
1970 type);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001971 }
1972 if (returnVal)
1973 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001974
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001975 return intermediate.addConstantUnion(unionArray, type, aggrNode->getLine());
1976 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001977
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001978 return 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001979}
1980
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001981//
1982// This function returns the tree representation for the vector field(s) being accessed from contant vector.
1983// If only one component of vector is accessed (v.x or v[0] where v is a contant vector), then a contant node is
1984// 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 +05301985// 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 +00001986// a constant matrix.
1987//
Arun Patole7e7e68d2015-05-22 12:02:25 +05301988TIntermTyped *TParseContext::addConstVectorNode(TVectorFields &fields, TIntermTyped *node, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001989{
Arun Patole7e7e68d2015-05-22 12:02:25 +05301990 TIntermTyped *typedNode;
1991 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001992
Jamie Madillb11e2482015-05-04 14:21:22 -04001993 const TConstantUnion *unionArray;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301994 if (tempConstantNode)
1995 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001996 unionArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001997
Arun Patole7e7e68d2015-05-22 12:02:25 +05301998 if (!unionArray)
1999 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002000 return node;
2001 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302002 }
2003 else
2004 { // 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 +00002005 error(line, "Cannot offset into the vector", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002006 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002007
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002008 return 0;
2009 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002010
Arun Patole7e7e68d2015-05-22 12:02:25 +05302011 TConstantUnion *constArray = new TConstantUnion[fields.num];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002012
Arun Patole7e7e68d2015-05-22 12:02:25 +05302013 for (int i = 0; i < fields.num; i++)
2014 {
2015 if (fields.offsets[i] >= node->getType().getNominalSize())
2016 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002017 std::stringstream extraInfoStream;
2018 extraInfoStream << "vector field selection out of range '" << fields.offsets[i] << "'";
2019 std::string extraInfo = extraInfoStream.str();
2020 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002021 recover();
2022 fields.offsets[i] = 0;
2023 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302024
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002025 constArray[i] = unionArray[fields.offsets[i]];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002026
Arun Patole7e7e68d2015-05-22 12:02:25 +05302027 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002028 typedNode = intermediate.addConstantUnion(constArray, node->getType(), line);
2029 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002030}
2031
2032//
2033// This function returns the column being accessed from a constant matrix. The values are retrieved from
Arun Patole7e7e68d2015-05-22 12:02:25 +05302034// the symbol table and parse-tree is built for a vector (each column of a matrix is a vector). The input
2035// 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 +00002036// constant matrix or it could be the tree representation of the constant matrix (s.m1[0] where s is a constant structure)
2037//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302038TIntermTyped *TParseContext::addConstMatrixNode(int index, TIntermTyped *node, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002039{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302040 TIntermTyped *typedNode;
2041 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002042
Arun Patole7e7e68d2015-05-22 12:02:25 +05302043 if (index >= node->getType().getCols())
2044 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002045 std::stringstream extraInfoStream;
2046 extraInfoStream << "matrix field selection out of range '" << index << "'";
2047 std::string extraInfo = extraInfoStream.str();
2048 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002049 recover();
2050 index = 0;
2051 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002052
Arun Patole7e7e68d2015-05-22 12:02:25 +05302053 if (tempConstantNode)
2054 {
Jamie Madillb11e2482015-05-04 14:21:22 -04002055 TConstantUnion *unionArray = tempConstantNode->getUnionArrayPointer();
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002056 int size = tempConstantNode->getType().getCols();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002057 typedNode = intermediate.addConstantUnion(&unionArray[size*index], tempConstantNode->getType(), line);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302058 }
2059 else
2060 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002061 error(line, "Cannot offset into the matrix", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002062 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002063
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002064 return 0;
2065 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002066
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002067 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002068}
2069
2070
2071//
2072// 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 +05302073// the symbol table and parse-tree is built for the type of the element. The input
2074// 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 +00002075// constant array or it could be the tree representation of the constant array (s.a1[0] where s is a constant structure)
2076//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302077TIntermTyped *TParseContext::addConstArrayNode(int index, TIntermTyped *node, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002078{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302079 TIntermTyped *typedNode;
2080 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002081 TType arrayElementType = node->getType();
2082 arrayElementType.clearArrayness();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002083
Arun Patole7e7e68d2015-05-22 12:02:25 +05302084 if (index >= node->getType().getArraySize())
2085 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002086 std::stringstream extraInfoStream;
2087 extraInfoStream << "array field selection out of range '" << index << "'";
2088 std::string extraInfo = extraInfoStream.str();
2089 error(line, "", "[", extraInfo.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002090 recover();
2091 index = 0;
2092 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002093
Arun Patole7e7e68d2015-05-22 12:02:25 +05302094 if (tempConstantNode)
2095 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04002096 size_t arrayElementSize = arrayElementType.getObjectSize();
Arun Patole7e7e68d2015-05-22 12:02:25 +05302097 TConstantUnion *unionArray = tempConstantNode->getUnionArrayPointer();
2098 typedNode = intermediate.addConstantUnion(&unionArray[arrayElementSize * index], tempConstantNode->getType(),
2099 line);
2100 }
2101 else
2102 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002103 error(line, "Cannot offset into the array", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002104 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002105
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002106 return 0;
2107 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002108
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002109 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002110}
2111
2112
2113//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302114// 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 +00002115// If there is an embedded/nested struct, it appropriately calls addConstStructNested or addConstStructFromAggr
2116// function and returns the parse-tree with the values of the embedded/nested struct.
2117//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302118TIntermTyped *TParseContext::addConstStruct(const TString &identifier, TIntermTyped *node, const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002119{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302120 const TFieldList &fields = node->getType().getStruct()->fields();
Jamie Madill94bf7f22013-07-08 13:31:15 -04002121 size_t instanceSize = 0;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002122
Arun Patole7e7e68d2015-05-22 12:02:25 +05302123 for (size_t index = 0; index < fields.size(); ++index)
2124 {
2125 if (fields[index]->name() == identifier)
2126 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002127 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302128 }
2129 else
2130 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002131 instanceSize += fields[index]->type()->getObjectSize();
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002132 }
2133 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002134
Jamie Madill94bf7f22013-07-08 13:31:15 -04002135 TIntermTyped *typedNode;
2136 TIntermConstantUnion *tempConstantNode = node->getAsConstantUnion();
Arun Patole7e7e68d2015-05-22 12:02:25 +05302137 if (tempConstantNode)
2138 {
2139 TConstantUnion *constArray = tempConstantNode->getUnionArrayPointer();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002140
Arun Patole7e7e68d2015-05-22 12:02:25 +05302141 // type will be changed in the calling function
2142 typedNode = intermediate.addConstantUnion(constArray+instanceSize, tempConstantNode->getType(), line);
2143 }
2144 else
2145 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002146 error(line, "Cannot offset into the structure", "Error");
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002147 recover();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002148
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002149 return 0;
2150 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002151
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002152 return typedNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002153}
2154
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002155//
2156// Interface/uniform blocks
2157//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302158TIntermAggregate *TParseContext::addInterfaceBlock(const TPublicType &typeQualifier, const TSourceLoc &nameLine,
2159 const TString &blockName, TFieldList *fieldList,
2160 const TString *instanceName, const TSourceLoc &instanceLine,
2161 TIntermTyped *arrayIndex, const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002162{
2163 if (reservedErrorCheck(nameLine, blockName))
2164 recover();
2165
2166 if (typeQualifier.qualifier != EvqUniform)
2167 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302168 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
2169 "interface blocks must be uniform");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002170 recover();
2171 }
2172
Jamie Madill099c0f32013-06-20 11:55:52 -04002173 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
2174 if (layoutLocationErrorCheck(typeQualifier.line, blockLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002175 {
2176 recover();
2177 }
2178
Jamie Madill099c0f32013-06-20 11:55:52 -04002179 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2180 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002181 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002182 }
2183
Jamie Madill1566ef72013-06-20 11:55:54 -04002184 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2185 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002186 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002187 }
2188
Arun Patole7e7e68d2015-05-22 12:02:25 +05302189 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2190 if (!symbolTable.declare(blockNameSymbol))
2191 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002192 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
2193 recover();
2194 }
2195
Jamie Madill98493dd2013-07-08 14:39:03 -04002196 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302197 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2198 {
2199 TField *field = (*fieldList)[memberIndex];
2200 TType *fieldType = field->type();
2201 if (IsSampler(fieldType->getBasicType()))
2202 {
2203 error(field->line(), "unsupported type", fieldType->getBasicString(),
2204 "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002205 recover();
2206 }
2207
Jamie Madill98493dd2013-07-08 14:39:03 -04002208 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002209 switch (qualifier)
2210 {
2211 case EvqGlobal:
2212 case EvqUniform:
2213 break;
2214 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04002215 error(field->line(), "invalid qualifier on interface block member", getQualifierString(qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002216 recover();
2217 break;
2218 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002219
2220 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002221 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
2222 if (layoutLocationErrorCheck(field->line(), fieldLayoutQualifier))
Jamie Madilla5efff92013-06-06 11:56:47 -04002223 {
2224 recover();
2225 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002226
Jamie Madill98493dd2013-07-08 14:39:03 -04002227 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002228 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302229 error(field->line(), "invalid layout qualifier:", getBlockStorageString(fieldLayoutQualifier.blockStorage),
2230 "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04002231 recover();
2232 }
2233
Jamie Madill98493dd2013-07-08 14:39:03 -04002234 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002235 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002236 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002237 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002238 else if (!fieldType->isMatrix())
Jamie Madill099c0f32013-06-20 11:55:52 -04002239 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302240 error(field->line(), "invalid layout qualifier:",
2241 getMatrixPackingString(fieldLayoutQualifier.matrixPacking), "can only be used on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04002242 recover();
2243 }
2244
Jamie Madill98493dd2013-07-08 14:39:03 -04002245 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002246 }
2247
Jamie Madill98493dd2013-07-08 14:39:03 -04002248 // add array index
2249 int arraySize = 0;
2250 if (arrayIndex != NULL)
2251 {
2252 if (arraySizeErrorCheck(arrayIndexLine, arrayIndex, arraySize))
2253 recover();
2254 }
2255
Arun Patole7e7e68d2015-05-22 12:02:25 +05302256 TInterfaceBlock *interfaceBlock = new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize,
2257 blockLayoutQualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002258 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier, arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002259
2260 TString symbolName = "";
2261 int symbolId = 0;
2262
Jamie Madill98493dd2013-07-08 14:39:03 -04002263 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002264 {
2265 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002266 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2267 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302268 TField *field = (*fieldList)[memberIndex];
2269 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002270
2271 // set parent pointer of the field variable
2272 fieldType->setInterfaceBlock(interfaceBlock);
2273
Arun Patole7e7e68d2015-05-22 12:02:25 +05302274 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002275 fieldVariable->setQualifier(typeQualifier.qualifier);
2276
Arun Patole7e7e68d2015-05-22 12:02:25 +05302277 if (!symbolTable.declare(fieldVariable))
2278 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002279 error(field->line(), "redefinition", field->name().c_str(), "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002280 recover();
2281 }
2282 }
2283 }
2284 else
2285 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002286 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302287 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002288 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002289
Arun Patole7e7e68d2015-05-22 12:02:25 +05302290 if (!symbolTable.declare(instanceTypeDef))
2291 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002292 error(instanceLine, "redefinition", instanceName->c_str(), "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002293 recover();
2294 }
2295
2296 symbolId = instanceTypeDef->getUniqueId();
2297 symbolName = instanceTypeDef->getName();
2298 }
2299
Arun Patole7e7e68d2015-05-22 12:02:25 +05302300 TIntermAggregate *aggregate = intermediate.makeAggregate(intermediate.addSymbol(symbolId, symbolName,
2301 interfaceBlockType,
2302 typeQualifier.line),
2303 nameLine);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002304 aggregate->setOp(EOpDeclaration);
Jamie Madill98493dd2013-07-08 14:39:03 -04002305
2306 exitStructDeclaration();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002307 return aggregate;
2308}
2309
Arun Patole7e7e68d2015-05-22 12:02:25 +05302310bool TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002311{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002312 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002313
2314 // Embedded structure definitions are not supported per GLSL ES spec.
2315 // They aren't allowed in GLSL either, but we need to detect this here
2316 // so we don't rely on the GLSL compiler to catch it.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302317 if (mStructNestingLevel > 1)
2318 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002319 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002320 return true;
2321 }
2322
2323 return false;
2324}
2325
2326void TParseContext::exitStructDeclaration()
2327{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002328 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002329}
2330
2331namespace {
2332
2333const int kWebGLMaxStructNesting = 4;
2334
2335} // namespace
2336
Arun Patole7e7e68d2015-05-22 12:02:25 +05302337bool TParseContext::structNestingErrorCheck(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002338{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302339 if (!IsWebGLBasedSpec(mShaderSpec))
2340 {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002341 return false;
2342 }
2343
Arun Patole7e7e68d2015-05-22 12:02:25 +05302344 if (field.type()->getBasicType() != EbtStruct)
2345 {
kbr@chromium.org476541f2011-10-27 21:14:51 +00002346 return false;
2347 }
2348
2349 // We're already inside a structure definition at this point, so add
2350 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302351 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2352 {
Jamie Madill41a49272014-03-18 16:10:13 -04002353 std::stringstream reasonStream;
2354 reasonStream << "Reference of struct type "
2355 << field.type()->getStruct()->name().c_str()
2356 << " exceeds maximum allowed nesting level of "
2357 << kWebGLMaxStructNesting;
2358 std::string reason = reasonStream.str();
2359 error(line, reason.c_str(), field.name().c_str(), "");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002360 return true;
2361 }
2362
2363 return false;
2364}
2365
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002366//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002367// Parse an array index expression
2368//
Arun Patole7e7e68d2015-05-22 12:02:25 +05302369TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression, const TSourceLoc &location,
2370 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002371{
2372 TIntermTyped *indexedExpression = NULL;
2373
2374 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2375 {
2376 if (baseExpression->getAsSymbolNode())
2377 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302378 error(location, " left of '[' is not of type array, matrix, or vector ",
2379 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002380 }
2381 else
2382 {
2383 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2384 }
2385 recover();
2386 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002387
Jamie Madill21c1e452014-12-29 11:33:41 -05002388 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2389
2390 if (indexExpression->getQualifier() == EvqConst && indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002391 {
Jamie Madill21c1e452014-12-29 11:33:41 -05002392 int index = indexConstantUnion->getIConst(0);
Jamie Madill7164cf42013-07-08 13:30:59 -04002393 if (index < 0)
2394 {
2395 std::stringstream infoStream;
2396 infoStream << index;
2397 std::string info = infoStream.str();
2398 error(location, "negative index", info.c_str());
2399 recover();
2400 index = 0;
2401 }
2402 if (baseExpression->getType().getQualifier() == EvqConst)
2403 {
2404 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002405 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002406 // constant folding for arrays
2407 indexedExpression = addConstArrayNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002408 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002409 else if (baseExpression->isVector())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002410 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002411 // constant folding for vectors
2412 TVectorFields fields;
2413 fields.num = 1;
2414 fields.offsets[0] = index; // need to do it this way because v.xy sends fields integer array
2415 indexedExpression = addConstVectorNode(fields, baseExpression, location);
2416 }
2417 else if (baseExpression->isMatrix())
2418 {
2419 // constant folding for matrices
2420 indexedExpression = addConstMatrixNode(index, baseExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002421 }
2422 }
2423 else
2424 {
Jamie Madillb11e2482015-05-04 14:21:22 -04002425 int safeIndex = -1;
2426
Jamie Madill7164cf42013-07-08 13:30:59 -04002427 if (baseExpression->isArray())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002428 {
Jamie Madill18464b52013-07-08 14:01:55 -04002429 if (index >= baseExpression->getType().getArraySize())
Jamie Madill7164cf42013-07-08 13:30:59 -04002430 {
2431 std::stringstream extraInfoStream;
2432 extraInfoStream << "array index out of range '" << index << "'";
2433 std::string extraInfo = extraInfoStream.str();
2434 error(location, "", "[", extraInfo.c_str());
2435 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002436 safeIndex = baseExpression->getType().getArraySize() - 1;
Jamie Madill7164cf42013-07-08 13:30:59 -04002437 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302438 else if (baseExpression->getQualifier() == EvqFragData && index > 0 &&
2439 !isExtensionEnabled("GL_EXT_draw_buffers"))
Jamie Madill5d287f52013-07-12 15:38:19 -04002440 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302441 error(location, "",
2442 "[", "array indexes for gl_FragData must be zero when GL_EXT_draw_buffers is disabled");
Jamie Madill5d287f52013-07-12 15:38:19 -04002443 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002444 safeIndex = 0;
Jamie Madill5d287f52013-07-12 15:38:19 -04002445 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002446 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302447 else if ((baseExpression->isVector() || baseExpression->isMatrix()) &&
2448 baseExpression->getType().getNominalSize() <= index)
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002449 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002450 std::stringstream extraInfoStream;
2451 extraInfoStream << "field selection out of range '" << index << "'";
2452 std::string extraInfo = extraInfoStream.str();
2453 error(location, "", "[", extraInfo.c_str());
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002454 recover();
Jamie Madillb11e2482015-05-04 14:21:22 -04002455 safeIndex = baseExpression->getType().getNominalSize() - 1;
Jamie Madill46131a32013-06-20 11:55:50 -04002456 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002457
Jamie Madillb11e2482015-05-04 14:21:22 -04002458 // Don't modify the data of the previous constant union, because it can point
2459 // to builtins, like gl_MaxDrawBuffers. Instead use a new sanitized object.
2460 if (safeIndex != -1)
2461 {
2462 TConstantUnion *safeConstantUnion = new TConstantUnion();
2463 safeConstantUnion->setIConst(safeIndex);
2464 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
2465 }
2466
Jamie Madill7164cf42013-07-08 13:30:59 -04002467 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002468 }
2469 }
Jamie Madill7164cf42013-07-08 13:30:59 -04002470 else
2471 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002472 if (baseExpression->isInterfaceBlock())
Jamie Madill7164cf42013-07-08 13:30:59 -04002473 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302474 error(location, "",
2475 "[", "array indexes for interface blocks arrays must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002476 recover();
2477 }
Jamie Madill19571812013-08-12 15:26:34 -07002478 else if (baseExpression->getQualifier() == EvqFragmentOut)
Jamie Madill7164cf42013-07-08 13:30:59 -04002479 {
Jamie Madill19571812013-08-12 15:26:34 -07002480 error(location, "", "[", "array indexes for fragment outputs must be constant integral expressions");
Jamie Madill7164cf42013-07-08 13:30:59 -04002481 recover();
2482 }
2483
2484 indexedExpression = intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location);
2485 }
2486
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002487 if (indexedExpression == 0)
2488 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002489 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002490 unionArray->setFConst(0.0f);
2491 indexedExpression = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst), location);
2492 }
2493 else if (baseExpression->isArray())
2494 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002495 const TType &baseType = baseExpression->getType();
2496 if (baseType.getStruct())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002497 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002498 TType copyOfType(baseType.getStruct());
2499 indexedExpression->setType(copyOfType);
2500 }
2501 else if (baseType.isInterfaceBlock())
2502 {
2503 TType copyOfType(baseType.getInterfaceBlock(), baseType.getQualifier(), baseType.getLayoutQualifier(), 0);
shannonwoods@chromium.orge429ab72013-05-30 00:12:52 +00002504 indexedExpression->setType(copyOfType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002505 }
2506 else
2507 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302508 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2509 EvqTemporary, static_cast<unsigned char>(baseExpression->getNominalSize()),
2510 static_cast<unsigned char>(baseExpression->getSecondarySize())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002511 }
2512
2513 if (baseExpression->getType().getQualifier() == EvqConst)
2514 {
2515 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2516 }
2517 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002518 else if (baseExpression->isMatrix())
2519 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002520 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302521 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2522 qualifier, static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002523 }
2524 else if (baseExpression->isVector())
2525 {
Jamie Madill7164cf42013-07-08 13:30:59 -04002526 TQualifier qualifier = baseExpression->getType().getQualifier() == EvqConst ? EvqConst : EvqTemporary;
2527 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(), qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002528 }
2529 else
2530 {
2531 indexedExpression->setType(baseExpression->getType());
2532 }
2533
2534 return indexedExpression;
2535}
2536
Arun Patole7e7e68d2015-05-22 12:02:25 +05302537TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression, const TSourceLoc &dotLocation,
2538 const TString &fieldString, const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002539{
2540 TIntermTyped *indexedExpression = NULL;
2541
2542 if (baseExpression->isArray())
2543 {
2544 error(fieldLocation, "cannot apply dot operator to an array", ".");
2545 recover();
2546 }
2547
2548 if (baseExpression->isVector())
2549 {
2550 TVectorFields fields;
2551 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields, fieldLocation))
2552 {
2553 fields.num = 1;
2554 fields.offsets[0] = 0;
2555 recover();
2556 }
2557
2558 if (baseExpression->getType().getQualifier() == EvqConst)
2559 {
2560 // constant folding for vector fields
2561 indexedExpression = addConstVectorNode(fields, baseExpression, fieldLocation);
2562 if (indexedExpression == 0)
2563 {
2564 recover();
2565 indexedExpression = baseExpression;
2566 }
2567 else
2568 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302569 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2570 EvqConst, (unsigned char) (fieldString).size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002571 }
2572 }
2573 else
2574 {
2575 TString vectorString = fieldString;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302576 TIntermTyped *index = intermediate.addSwizzle(fields, fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002577 indexedExpression = intermediate.addIndex(EOpVectorSwizzle, baseExpression, index, dotLocation);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302578 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2579 EvqTemporary, (unsigned char) vectorString.size()));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002580 }
2581 }
2582 else if (baseExpression->isMatrix())
2583 {
2584 TMatrixFields fields;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302585 if (!parseMatrixFields(fieldString, baseExpression->getCols(), baseExpression->getRows(), fields,
2586 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002587 {
2588 fields.wholeRow = false;
2589 fields.wholeCol = false;
2590 fields.row = 0;
2591 fields.col = 0;
2592 recover();
2593 }
2594
2595 if (fields.wholeRow || fields.wholeCol)
2596 {
2597 error(dotLocation, " non-scalar fields not implemented yet", ".");
2598 recover();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002599 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002600 unionArray->setIConst(0);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302601 TIntermTyped *index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst),
2602 fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002603 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302604 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision(),
2605 EvqTemporary, static_cast<unsigned char>(baseExpression->getCols()),
2606 static_cast<unsigned char>(baseExpression->getRows())));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002607 }
2608 else
2609 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002610 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002611 unionArray->setIConst(fields.col * baseExpression->getRows() + fields.row);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302612 TIntermTyped *index = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst),
2613 fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002614 indexedExpression = intermediate.addIndex(EOpIndexDirect, baseExpression, index, dotLocation);
2615 indexedExpression->setType(TType(baseExpression->getBasicType(), baseExpression->getPrecision()));
2616 }
2617 }
2618 else if (baseExpression->getBasicType() == EbtStruct)
2619 {
2620 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302621 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04002622 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002623 {
2624 error(dotLocation, "structure has no fields", "Internal Error");
2625 recover();
2626 indexedExpression = baseExpression;
2627 }
2628 else
2629 {
2630 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002631 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002632 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002633 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002634 {
2635 fieldFound = true;
2636 break;
2637 }
2638 }
2639 if (fieldFound)
2640 {
2641 if (baseExpression->getType().getQualifier() == EvqConst)
2642 {
2643 indexedExpression = addConstStruct(fieldString, baseExpression, dotLocation);
2644 if (indexedExpression == 0)
2645 {
2646 recover();
2647 indexedExpression = baseExpression;
2648 }
2649 else
2650 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002651 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002652 // change the qualifier of the return type, not of the structure field
2653 // as the structure definition is shared between various structures.
2654 indexedExpression->getTypePointer()->setQualifier(EvqConst);
2655 }
2656 }
2657 else
2658 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002659 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002660 unionArray->setIConst(i);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302661 TIntermTyped *index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002662 indexedExpression = intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index, dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002663 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002664 }
2665 }
2666 else
2667 {
2668 error(dotLocation, " no such field in structure", fieldString.c_str());
2669 recover();
2670 indexedExpression = baseExpression;
2671 }
2672 }
2673 }
Jamie Madill98493dd2013-07-08 14:39:03 -04002674 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002675 {
2676 bool fieldFound = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05302677 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04002678 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002679 {
2680 error(dotLocation, "interface block has no fields", "Internal Error");
2681 recover();
2682 indexedExpression = baseExpression;
2683 }
2684 else
2685 {
2686 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04002687 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002688 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002689 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002690 {
2691 fieldFound = true;
2692 break;
2693 }
2694 }
2695 if (fieldFound)
2696 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04002697 TConstantUnion *unionArray = new TConstantUnion[1];
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002698 unionArray->setIConst(i);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302699 TIntermTyped *index = intermediate.addConstantUnion(unionArray, *fields[i]->type(), fieldLocation);
2700 indexedExpression = intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index,
2701 dotLocation);
Jamie Madill98493dd2013-07-08 14:39:03 -04002702 indexedExpression->setType(*fields[i]->type());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002703 }
2704 else
2705 {
2706 error(dotLocation, " no such field in interface block", fieldString.c_str());
2707 recover();
2708 indexedExpression = baseExpression;
2709 }
2710 }
2711 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002712 else
2713 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002714 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002715 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302716 error(dotLocation, " field selection requires structure, vector, or matrix on left hand side",
2717 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002718 }
2719 else
2720 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302721 error(dotLocation,
2722 " field selection requires structure, vector, matrix, or interface block on left hand side",
2723 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002724 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002725 recover();
2726 indexedExpression = baseExpression;
2727 }
2728
2729 return indexedExpression;
2730}
2731
Arun Patole7e7e68d2015-05-22 12:02:25 +05302732TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002733{
Jamie Madilla5efff92013-06-06 11:56:47 -04002734 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002735
Jamie Madilla5efff92013-06-06 11:56:47 -04002736 qualifier.location = -1;
2737 qualifier.matrixPacking = EmpUnspecified;
2738 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002739
2740 if (qualifierType == "shared")
2741 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002742 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002743 }
2744 else if (qualifierType == "packed")
2745 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002746 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002747 }
2748 else if (qualifierType == "std140")
2749 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002750 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002751 }
2752 else if (qualifierType == "row_major")
2753 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002754 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002755 }
2756 else if (qualifierType == "column_major")
2757 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002758 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002759 }
2760 else if (qualifierType == "location")
2761 {
2762 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(), "location requires an argument");
2763 recover();
2764 }
2765 else
2766 {
2767 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
2768 recover();
2769 }
2770
Jamie Madilla5efff92013-06-06 11:56:47 -04002771 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002772}
2773
Arun Patole7e7e68d2015-05-22 12:02:25 +05302774TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType, const TSourceLoc &qualifierTypeLine,
2775 const TString &intValueString, int intValue,
2776 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002777{
Jamie Madilla5efff92013-06-06 11:56:47 -04002778 TLayoutQualifier qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002779
Jamie Madilla5efff92013-06-06 11:56:47 -04002780 qualifier.location = -1;
2781 qualifier.matrixPacking = EmpUnspecified;
2782 qualifier.blockStorage = EbsUnspecified;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002783
2784 if (qualifierType != "location")
2785 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302786 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
2787 "only location may have arguments");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002788 recover();
2789 }
2790 else
2791 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002792 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002793 if (intValue < 0)
2794 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04002795 error(intValueLine, "out of range:", intValueString.c_str(), "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002796 recover();
2797 }
2798 else
2799 {
Jamie Madilla5efff92013-06-06 11:56:47 -04002800 qualifier.location = intValue;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002801 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002802 }
2803
Jamie Madilla5efff92013-06-06 11:56:47 -04002804 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002805}
2806
Jamie Madilla5efff92013-06-06 11:56:47 -04002807TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier, TLayoutQualifier rightQualifier)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002808{
Jamie Madilla5efff92013-06-06 11:56:47 -04002809 TLayoutQualifier joinedQualifier = leftQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002810
Jamie Madilla5efff92013-06-06 11:56:47 -04002811 if (rightQualifier.location != -1)
2812 {
2813 joinedQualifier.location = rightQualifier.location;
2814 }
2815 if (rightQualifier.matrixPacking != EmpUnspecified)
2816 {
2817 joinedQualifier.matrixPacking = rightQualifier.matrixPacking;
2818 }
2819 if (rightQualifier.blockStorage != EbsUnspecified)
2820 {
2821 joinedQualifier.blockStorage = rightQualifier.blockStorage;
2822 }
2823
2824 return joinedQualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00002825}
2826
Arun Patole7e7e68d2015-05-22 12:02:25 +05302827TPublicType TParseContext::joinInterpolationQualifiers(const TSourceLoc &interpolationLoc,
2828 TQualifier interpolationQualifier,
2829 const TSourceLoc &storageLoc,
2830 TQualifier storageQualifier)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002831{
2832 TQualifier mergedQualifier = EvqSmoothIn;
2833
Arun Patole7e7e68d2015-05-22 12:02:25 +05302834 if (storageQualifier == EvqFragmentIn)
2835 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002836 if (interpolationQualifier == EvqSmooth)
2837 mergedQualifier = EvqSmoothIn;
2838 else if (interpolationQualifier == EvqFlat)
2839 mergedQualifier = EvqFlatIn;
2840 else UNREACHABLE();
2841 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302842 else if (storageQualifier == EvqCentroidIn)
2843 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002844 if (interpolationQualifier == EvqSmooth)
2845 mergedQualifier = EvqCentroidIn;
2846 else if (interpolationQualifier == EvqFlat)
2847 mergedQualifier = EvqFlatIn;
2848 else UNREACHABLE();
2849 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302850 else if (storageQualifier == EvqVertexOut)
2851 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002852 if (interpolationQualifier == EvqSmooth)
2853 mergedQualifier = EvqSmoothOut;
2854 else if (interpolationQualifier == EvqFlat)
2855 mergedQualifier = EvqFlatOut;
2856 else UNREACHABLE();
2857 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302858 else if (storageQualifier == EvqCentroidOut)
2859 {
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002860 if (interpolationQualifier == EvqSmooth)
2861 mergedQualifier = EvqCentroidOut;
2862 else if (interpolationQualifier == EvqFlat)
2863 mergedQualifier = EvqFlatOut;
2864 else UNREACHABLE();
2865 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302866 else
2867 {
2868 error(interpolationLoc, "interpolation qualifier requires a fragment 'in' or vertex 'out' storage qualifier",
2869 getInterpolationString(interpolationQualifier));
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04002870 recover();
2871
2872 mergedQualifier = storageQualifier;
2873 }
2874
2875 TPublicType type;
2876 type.setBasic(EbtVoid, mergedQualifier, storageLoc);
2877 return type;
2878}
2879
Arun Patole7e7e68d2015-05-22 12:02:25 +05302880TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier, TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002881{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002882 if (voidErrorCheck(typeSpecifier.line, (*fieldList)[0]->name(), typeSpecifier.type))
2883 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002884 recover();
2885 }
2886
Arun Patole7e7e68d2015-05-22 12:02:25 +05302887 for (unsigned int i = 0; i < fieldList->size(); ++i)
2888 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002889 //
2890 // Careful not to replace already known aspects of type, like array-ness
2891 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05302892 TType *type = (*fieldList)[i]->type();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002893 type->setBasicType(typeSpecifier.type);
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002894 type->setPrimarySize(typeSpecifier.primarySize);
2895 type->setSecondarySize(typeSpecifier.secondarySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002896 type->setPrecision(typeSpecifier.precision);
2897 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002898 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002899
2900 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05302901 if (type->isArray())
2902 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002903 if (arrayTypeErrorCheck(typeSpecifier.line, typeSpecifier))
2904 recover();
2905 }
2906 if (typeSpecifier.array)
2907 type->setArraySize(typeSpecifier.arraySize);
Arun Patole7e7e68d2015-05-22 12:02:25 +05302908 if (typeSpecifier.userDef)
2909 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002910 type->setStruct(typeSpecifier.userDef->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002911 }
2912
Arun Patole7e7e68d2015-05-22 12:02:25 +05302913 if (structNestingErrorCheck(typeSpecifier.line, *(*fieldList)[i]))
2914 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002915 recover();
2916 }
2917 }
2918
Jamie Madill98493dd2013-07-08 14:39:03 -04002919 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002920}
2921
Arun Patole7e7e68d2015-05-22 12:02:25 +05302922TPublicType TParseContext::addStructure(const TSourceLoc &structLine, const TSourceLoc &nameLine,
2923 const TString *structName, TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002924{
Arun Patole7e7e68d2015-05-22 12:02:25 +05302925 TStructure *structure = new TStructure(structName, fieldList);
2926 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002927
Jamie Madill9b820842015-02-12 10:40:10 -05002928 // Store a bool in the struct if we're at global scope, to allow us to
2929 // skip the local struct scoping workaround in HLSL.
Jamie Madillb960cc42015-02-12 15:33:20 +00002930 structure->setUniqueId(TSymbolTable::nextUniqueId());
Jamie Madill9b820842015-02-12 10:40:10 -05002931 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04002932
Jamie Madill98493dd2013-07-08 14:39:03 -04002933 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002934 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002935 if (reservedErrorCheck(nameLine, *structName))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002936 {
2937 recover();
2938 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05302939 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
2940 if (!symbolTable.declare(userTypeDef))
2941 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002942 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002943 recover();
2944 }
2945 }
2946
2947 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04002948 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002949 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002950 const TField &field = *(*fieldList)[typeListIndex];
2951 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002952 switch (qualifier)
2953 {
2954 case EvqGlobal:
2955 case EvqTemporary:
2956 break;
2957 default:
Jamie Madill98493dd2013-07-08 14:39:03 -04002958 error(field.line(), "invalid qualifier on struct member", getQualifierString(qualifier));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002959 recover();
2960 break;
2961 }
2962 }
2963
2964 TPublicType publicType;
2965 publicType.setBasic(EbtStruct, EvqTemporary, structLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04002966 publicType.userDef = structureType;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002967 exitStructDeclaration();
2968
2969 return publicType;
2970}
2971
Olli Etuahoa3a36662015-02-17 13:46:51 +02002972TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init, TIntermAggregate *statementList, const TSourceLoc &loc)
2973{
Olli Etuaho53f076f2015-02-20 10:55:14 +02002974 TBasicType switchType = init->getBasicType();
2975 if ((switchType != EbtInt && switchType != EbtUInt) ||
2976 init->isMatrix() ||
2977 init->isArray() ||
2978 init->isVector())
2979 {
2980 error(init->getLine(), "init-expression in a switch statement must be a scalar integer", "switch");
2981 recover();
2982 return nullptr;
2983 }
2984
Olli Etuahoac5274d2015-02-20 10:19:08 +02002985 if (statementList)
2986 {
2987 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
2988 {
2989 recover();
2990 return nullptr;
2991 }
2992 }
2993
Olli Etuahoa3a36662015-02-17 13:46:51 +02002994 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
2995 if (node == nullptr)
2996 {
2997 error(loc, "erroneous switch statement", "switch");
2998 recover();
2999 return nullptr;
3000 }
3001 return node;
3002}
3003
3004TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3005{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003006 if (mSwitchNestingLevel == 0)
3007 {
3008 error(loc, "case labels need to be inside switch statements", "case");
3009 recover();
3010 return nullptr;
3011 }
3012 if (condition == nullptr)
3013 {
3014 error(loc, "case label must have a condition", "case");
3015 recover();
3016 return nullptr;
3017 }
3018 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
3019 condition->isMatrix() ||
3020 condition->isArray() ||
3021 condition->isVector())
3022 {
3023 error(condition->getLine(), "case label must be a scalar integer", "case");
3024 recover();
3025 }
3026 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
3027 if (conditionConst == nullptr)
3028 {
3029 error(condition->getLine(), "case label must be constant", "case");
3030 recover();
3031 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003032 TIntermCase *node = intermediate.addCase(condition, loc);
3033 if (node == nullptr)
3034 {
3035 error(loc, "erroneous case statement", "case");
3036 recover();
3037 return nullptr;
3038 }
3039 return node;
3040}
3041
3042TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3043{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003044 if (mSwitchNestingLevel == 0)
3045 {
3046 error(loc, "default labels need to be inside switch statements", "default");
3047 recover();
3048 return nullptr;
3049 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003050 TIntermCase *node = intermediate.addCase(nullptr, loc);
3051 if (node == nullptr)
3052 {
3053 error(loc, "erroneous default statement", "default");
3054 recover();
3055 return nullptr;
3056 }
3057 return node;
3058}
3059
Olli Etuahof6c694b2015-03-26 14:50:53 +02003060TIntermTyped *TParseContext::createUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc,
3061 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003062{
3063 if (child == nullptr)
3064 {
3065 return nullptr;
3066 }
3067
3068 switch (op)
3069 {
3070 case EOpLogicalNot:
3071 if (child->getBasicType() != EbtBool ||
3072 child->isMatrix() ||
3073 child->isArray() ||
3074 child->isVector())
3075 {
3076 return nullptr;
3077 }
3078 break;
3079 case EOpBitwiseNot:
3080 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3081 child->isMatrix() ||
3082 child->isArray())
3083 {
3084 return nullptr;
3085 }
3086 break;
3087 case EOpPostIncrement:
3088 case EOpPreIncrement:
3089 case EOpPostDecrement:
3090 case EOpPreDecrement:
3091 case EOpNegative:
3092 case EOpPositive:
3093 if (child->getBasicType() == EbtStruct ||
Olli Etuahodca3e792015-03-26 13:24:04 +02003094 child->getBasicType() == EbtBool ||
Olli Etuaho69c11b52015-03-26 12:59:00 +02003095 child->isArray())
3096 {
3097 return nullptr;
3098 }
Olli Etuahodca3e792015-03-26 13:24:04 +02003099 // Operators for built-ins are already type checked against their prototype.
Olli Etuaho69c11b52015-03-26 12:59:00 +02003100 default:
3101 break;
3102 }
3103
Olli Etuahof6c694b2015-03-26 14:50:53 +02003104 return intermediate.addUnaryMath(op, child, loc, funcReturnType);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003105}
3106
Olli Etuaho09b22472015-02-11 11:47:26 +02003107TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3108{
Olli Etuahof6c694b2015-03-26 14:50:53 +02003109 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003110 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003111 {
3112 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
3113 recover();
3114 return child;
3115 }
3116 return node;
3117}
3118
3119TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3120{
3121 if (lValueErrorCheck(loc, GetOperatorString(op), child))
3122 recover();
3123 return addUnaryMath(op, child, loc);
3124}
3125
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003126bool TParseContext::binaryOpCommonCheck(TOperator op, TIntermTyped *left, TIntermTyped *right,
Olli Etuahod6b14282015-03-17 14:31:35 +02003127 const TSourceLoc &loc)
3128{
3129 if (left->isArray() || right->isArray())
3130 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003131 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003132 {
3133 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3134 return false;
3135 }
3136
3137 if (left->isArray() != right->isArray())
3138 {
3139 error(loc, "array / non-array mismatch", GetOperatorString(op));
3140 return false;
3141 }
3142
3143 switch (op)
3144 {
3145 case EOpEqual:
3146 case EOpNotEqual:
3147 case EOpAssign:
3148 case EOpInitialize:
3149 break;
3150 default:
3151 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3152 return false;
3153 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003154 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003155 if (left->getArraySize() != right->getArraySize())
3156 {
3157 error(loc, "array size mismatch", GetOperatorString(op));
3158 return false;
3159 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003160 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003161
3162 // Check ops which require integer / ivec parameters
3163 bool isBitShift = false;
3164 switch (op)
3165 {
3166 case EOpBitShiftLeft:
3167 case EOpBitShiftRight:
3168 case EOpBitShiftLeftAssign:
3169 case EOpBitShiftRightAssign:
3170 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3171 // check that the basic type is an integer type.
3172 isBitShift = true;
3173 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3174 {
3175 return false;
3176 }
3177 break;
3178 case EOpBitwiseAnd:
3179 case EOpBitwiseXor:
3180 case EOpBitwiseOr:
3181 case EOpBitwiseAndAssign:
3182 case EOpBitwiseXorAssign:
3183 case EOpBitwiseOrAssign:
3184 // It is enough to check the type of only one operand, since later it
3185 // is checked that the operand types match.
3186 if (!IsInteger(left->getBasicType()))
3187 {
3188 return false;
3189 }
3190 break;
3191 default:
3192 break;
3193 }
3194
3195 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3196 // So the basic type should usually match.
3197 if (!isBitShift && left->getBasicType() != right->getBasicType())
3198 {
3199 return false;
3200 }
3201
Olli Etuaho9dd217b2015-03-20 14:24:31 +02003202 // Check that type sizes match exactly on ops that require that.
Olli Etuahoff699002015-03-23 14:38:42 +02003203 // Also check restrictions for structs that contain arrays or samplers.
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003204 switch(op)
3205 {
3206 case EOpAssign:
3207 case EOpInitialize:
3208 case EOpEqual:
3209 case EOpNotEqual:
Olli Etuaho9dd217b2015-03-20 14:24:31 +02003210 // ESSL 1.00 sections 5.7, 5.8, 5.9
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003211 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
Olli Etuaho9dd217b2015-03-20 14:24:31 +02003212 {
3213 error(loc, "undefined operation for structs containing arrays", GetOperatorString(op));
3214 return false;
3215 }
Olli Etuahoff699002015-03-23 14:38:42 +02003216 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3217 // we interpret the spec so that this extends to structs containing samplers,
3218 // similarly to ESSL 1.00 spec.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003219 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
Olli Etuahoff699002015-03-23 14:38:42 +02003220 left->getType().isStructureContainingSamplers())
3221 {
3222 error(loc, "undefined operation for structs containing samplers", GetOperatorString(op));
3223 return false;
3224 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003225 case EOpLessThan:
3226 case EOpGreaterThan:
3227 case EOpLessThanEqual:
3228 case EOpGreaterThanEqual:
3229 if ((left->getNominalSize() != right->getNominalSize()) ||
3230 (left->getSecondarySize() != right->getSecondarySize()))
3231 {
3232 return false;
3233 }
3234 default:
3235 break;
3236 }
3237
Olli Etuahod6b14282015-03-17 14:31:35 +02003238 return true;
3239}
3240
Olli Etuahofc1806e2015-03-17 13:03:11 +02003241TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op, TIntermTyped *left, TIntermTyped *right,
3242 const TSourceLoc &loc)
3243{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003244 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003245 return nullptr;
3246
Olli Etuahofc1806e2015-03-17 13:03:11 +02003247 switch (op)
3248 {
3249 case EOpEqual:
3250 case EOpNotEqual:
Olli Etuahofc1806e2015-03-17 13:03:11 +02003251 break;
3252 case EOpLessThan:
3253 case EOpGreaterThan:
3254 case EOpLessThanEqual:
3255 case EOpGreaterThanEqual:
Olli Etuahod6b14282015-03-17 14:31:35 +02003256 ASSERT(!left->isArray() && !right->isArray());
3257 if (left->isMatrix() || left->isVector() ||
Olli Etuahofc1806e2015-03-17 13:03:11 +02003258 left->getBasicType() == EbtStruct)
3259 {
3260 return nullptr;
3261 }
3262 break;
3263 case EOpLogicalOr:
3264 case EOpLogicalXor:
3265 case EOpLogicalAnd:
Olli Etuahod6b14282015-03-17 14:31:35 +02003266 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003267 if (left->getBasicType() != EbtBool ||
Olli Etuahod6b14282015-03-17 14:31:35 +02003268 left->isMatrix() || left->isVector())
Olli Etuahofc1806e2015-03-17 13:03:11 +02003269 {
3270 return nullptr;
3271 }
3272 break;
3273 case EOpAdd:
3274 case EOpSub:
3275 case EOpDiv:
3276 case EOpMul:
Olli Etuahod6b14282015-03-17 14:31:35 +02003277 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003278 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool)
3279 {
3280 return nullptr;
3281 }
3282 break;
3283 case EOpIMod:
Olli Etuahod6b14282015-03-17 14:31:35 +02003284 ASSERT(!left->isArray() && !right->isArray());
Olli Etuahofc1806e2015-03-17 13:03:11 +02003285 // Note that this is only for the % operator, not for mod()
3286 if (left->getBasicType() == EbtStruct || left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
3287 {
3288 return nullptr;
3289 }
3290 break;
3291 // Note that for bitwise ops, type checking is done in promote() to
3292 // share code between ops and compound assignment
3293 default:
3294 break;
3295 }
3296
Olli Etuahofc1806e2015-03-17 13:03:11 +02003297 return intermediate.addBinaryMath(op, left, right, loc);
3298}
3299
Olli Etuaho09b22472015-02-11 11:47:26 +02003300TIntermTyped *TParseContext::addBinaryMath(TOperator op, TIntermTyped *left, TIntermTyped *right,
3301 const TSourceLoc &loc)
3302{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003303 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003304 if (node == 0)
3305 {
3306 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(), right->getCompleteString());
3307 recover();
3308 return left;
3309 }
3310 return node;
3311}
3312
3313TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op, TIntermTyped *left, TIntermTyped *right,
3314 const TSourceLoc &loc)
3315{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003316 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003317 if (node == 0)
3318 {
3319 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(), right->getCompleteString());
3320 recover();
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003321 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02003322 unionArray->setBConst(false);
3323 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst), loc);
3324 }
3325 return node;
3326}
3327
Olli Etuahod6b14282015-03-17 14:31:35 +02003328TIntermTyped *TParseContext::createAssign(TOperator op, TIntermTyped *left, TIntermTyped *right,
3329 const TSourceLoc &loc)
3330{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003331 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003332 {
3333 return intermediate.addAssign(op, left, right, loc);
3334 }
3335 return nullptr;
3336}
3337
3338TIntermTyped *TParseContext::addAssign(TOperator op, TIntermTyped *left, TIntermTyped *right,
3339 const TSourceLoc &loc)
3340{
3341 TIntermTyped *node = createAssign(op, left, right, loc);
3342 if (node == nullptr)
3343 {
3344 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
3345 recover();
3346 return left;
3347 }
3348 return node;
3349}
3350
Olli Etuaho49300862015-02-20 14:54:49 +02003351TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
3352{
3353 switch (op)
3354 {
3355 case EOpContinue:
3356 if (mLoopNestingLevel <= 0)
3357 {
3358 error(loc, "continue statement only allowed in loops", "");
3359 recover();
3360 }
3361 break;
3362 case EOpBreak:
Olli Etuaho53f076f2015-02-20 10:55:14 +02003363 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
Olli Etuaho49300862015-02-20 14:54:49 +02003364 {
Olli Etuaho53f076f2015-02-20 10:55:14 +02003365 error(loc, "break statement only allowed in loops and switch statements", "");
Olli Etuaho49300862015-02-20 14:54:49 +02003366 recover();
3367 }
3368 break;
3369 case EOpReturn:
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003370 if (mCurrentFunctionType->getBasicType() != EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02003371 {
3372 error(loc, "non-void function must return a value", "return");
3373 recover();
3374 }
3375 break;
3376 default:
3377 // No checks for discard
3378 break;
3379 }
3380 return intermediate.addBranch(op, loc);
3381}
3382
3383TIntermBranch *TParseContext::addBranch(TOperator op, TIntermTyped *returnValue, const TSourceLoc &loc)
3384{
3385 ASSERT(op == EOpReturn);
3386 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003387 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02003388 {
3389 error(loc, "void function cannot return a value", "return");
3390 recover();
3391 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003392 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02003393 {
3394 error(loc, "function return is not matching type:", "return");
3395 recover();
3396 }
3397 return intermediate.addBranch(op, returnValue, loc);
3398}
3399
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003400TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall, TIntermNode *paramNode, TIntermNode *thisNode,
3401 const TSourceLoc &loc, bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003402{
3403 *fatalError = false;
3404 TOperator op = fnCall->getBuiltInOp();
3405 TIntermTyped *callNode = nullptr;
3406
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003407 if (thisNode != nullptr)
3408 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003409 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho96e67382015-04-23 14:27:02 +03003410 int arraySize = 0;
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003411 TIntermTyped *typedThis = thisNode->getAsTyped();
3412 if (fnCall->getName() != "length")
3413 {
3414 error(loc, "invalid method", fnCall->getName().c_str());
3415 recover();
3416 }
3417 else if (paramNode != nullptr)
3418 {
3419 error(loc, "method takes no parameters", "length");
3420 recover();
3421 }
3422 else if (typedThis == nullptr || !typedThis->isArray())
3423 {
3424 error(loc, "length can only be called on arrays", "length");
3425 recover();
3426 }
3427 else
3428 {
Olli Etuaho96e67382015-04-23 14:27:02 +03003429 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03003430 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003431 {
Olli Etuaho39282e12015-04-23 15:41:48 +03003432 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003433 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03003434 // (func()).length()
3435 // (int[3](0, 1, 2)).length()
3436 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid expression.
3437 // It allows "An array name with the length method applied" in contrast to GLSL 4.4 spec section 5.9
3438 // which allows "An array, vector or matrix expression with the length method applied".
3439 error(loc, "length can only be called on array names, not on array expressions", "length");
3440 recover();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003441 }
3442 }
Olli Etuaho96e67382015-04-23 14:27:02 +03003443 unionArray->setIConst(arraySize);
3444 callNode = intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003445 }
3446 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003447 {
3448 //
3449 // Then this should be a constructor.
3450 // Don't go through the symbol table for constructors.
3451 // Their parameters will be verified algorithmically.
3452 //
3453 TType type(EbtVoid, EbpUndefined); // use this to get the type back
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003454 if (!constructorErrorCheck(loc, paramNode, *fnCall, op, &type))
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003455 {
3456 //
3457 // It's a constructor, of type 'type'.
3458 //
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003459 callNode = addConstructor(paramNode, &type, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003460 }
Olli Etuaho72ba85b2015-03-04 14:23:26 +02003461
3462 if (callNode == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003463 {
3464 recover();
3465 callNode = intermediate.setAggregateOperator(nullptr, op, loc);
3466 }
3467 callNode->setType(type);
3468 }
3469 else
3470 {
3471 //
3472 // Not a constructor. Find it in the symbol table.
3473 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303474 const TFunction *fnCandidate;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003475 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003476 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003477 if (fnCandidate)
3478 {
3479 //
3480 // A declared function.
3481 //
3482 if (builtIn && !fnCandidate->getExtension().empty() &&
3483 extensionErrorCheck(loc, fnCandidate->getExtension()))
3484 {
3485 recover();
3486 }
3487 op = fnCandidate->getBuiltInOp();
3488 if (builtIn && op != EOpNull)
3489 {
3490 //
3491 // A function call mapped to a built-in operation.
3492 //
3493 if (fnCandidate->getParamCount() == 1)
3494 {
3495 //
3496 // Treat it like a built-in unary operator.
3497 //
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003498 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc, &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003499 if (callNode == nullptr)
3500 {
3501 std::stringstream extraInfoStream;
3502 extraInfoStream << "built in unary operator function. Type: "
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003503 << static_cast<TIntermTyped*>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003504 std::string extraInfo = extraInfoStream.str();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003505 error(paramNode->getLine(), " wrong operand type", "Internal Error", extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003506 *fatalError = true;
3507 return nullptr;
3508 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003509 }
3510 else
3511 {
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003512 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003513 aggregate->setType(fnCandidate->getReturnType());
3514 aggregate->setPrecisionFromChildren();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003515
3516 // Some built-in functions have out parameters too.
3517 functionCallLValueErrorCheck(fnCandidate, aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05303518
3519 // See if we can constant fold a built-in.
Olli Etuahob43846e2015-06-02 18:18:57 +03003520 TIntermTyped *foldedNode = intermediate.foldAggregateBuiltIn(aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05303521 if (foldedNode)
3522 {
Arun Patole274f0702015-05-05 13:33:30 +05303523 callNode = foldedNode;
3524 }
Olli Etuahob43846e2015-06-02 18:18:57 +03003525 else
3526 {
3527 callNode = aggregate;
3528 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003529 }
3530 }
3531 else
3532 {
3533 // This is a real function call
3534
Olli Etuahoffe6edf2015-04-13 17:32:03 +03003535 TIntermAggregate *aggregate = intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003536 aggregate->setType(fnCandidate->getReturnType());
3537
3538 // this is how we know whether the given function is a builtIn function or a user defined function
3539 // if builtIn == false, it's a userDefined -> could be an overloaded builtIn function also
3540 // if builtIn == true, it's definitely a builtIn function with EOpNull
3541 if (!builtIn)
3542 aggregate->setUserDefined();
3543 aggregate->setName(fnCandidate->getMangledName());
Corentin Wallez71d147f2015-02-11 11:15:24 -08003544 aggregate->setFunctionId(fnCandidate->getUniqueId());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003545
3546 // This needs to happen after the name is set
3547 if (builtIn)
3548 aggregate->setBuiltInFunctionPrecision();
3549
3550 callNode = aggregate;
3551
3552 functionCallLValueErrorCheck(fnCandidate, aggregate);
3553 }
3554 }
3555 else
3556 {
3557 // error message was put out by findFunction()
3558 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003559 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02003560 unionArray->setFConst(0.0f);
3561 callNode = intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpUndefined, EvqConst), loc);
3562 recover();
3563 }
3564 }
3565 delete fnCall;
3566 return callNode;
3567}
3568
Olli Etuaho52901742015-04-15 13:42:45 +03003569TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond, TIntermTyped *trueBlock, TIntermTyped *falseBlock,
3570 const TSourceLoc &loc)
3571{
3572 if (boolErrorCheck(loc, cond))
3573 recover();
3574
3575 if (trueBlock->getType() != falseBlock->getType())
3576 {
3577 binaryOpError(loc, ":", trueBlock->getCompleteString(), falseBlock->getCompleteString());
3578 recover();
3579 return falseBlock;
3580 }
Olli Etuahoa2d53032015-04-15 14:14:44 +03003581 // ESSL1 sections 5.2 and 5.7:
3582 // ESSL3 section 5.7:
3583 // Ternary operator is not among the operators allowed for structures/arrays.
3584 if (trueBlock->isArray() || trueBlock->getBasicType() == EbtStruct)
3585 {
3586 error(loc, "ternary operator is not allowed for structures or arrays", ":");
3587 recover();
3588 return falseBlock;
3589 }
Olli Etuaho52901742015-04-15 13:42:45 +03003590 return intermediate.addSelection(cond, trueBlock, falseBlock, loc);
3591}
Olli Etuaho49300862015-02-20 14:54:49 +02003592
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003593//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003594// Parse an array of strings using yyparse.
3595//
3596// Returns 0 for success.
3597//
Arun Patole7e7e68d2015-05-22 12:02:25 +05303598int PaParseStrings(size_t count, const char *const string[], const int length[],
3599 TParseContext *context)
3600{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003601 if ((count == 0) || (string == NULL))
3602 return 1;
3603
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003604 if (glslang_initialize(context))
3605 return 1;
3606
alokp@chromium.org408c45e2012-04-05 15:54:43 +00003607 int error = glslang_scan(count, string, length, context);
3608 if (!error)
3609 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003610
alokp@chromium.org73bc2982012-06-19 18:48:05 +00003611 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00003612
alokp@chromium.org6b495712012-06-29 00:06:58 +00003613 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003614}
3615
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003616
alokp@chromium.org34b99cd2010-07-27 18:37:55 +00003617