blob: 4ad597c4f2fa11211adb61b3df3cfcaf764e866d [file] [log] [blame]
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001//
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002// Copyright (c) 2002-2014 The ANGLE Project Authors. All rights reserved.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5//
6
Jamie Madill6b9cb252013-10-17 10:45:47 -04007#include "compiler/translator/ParseContext.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +00008
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00009#include <stdarg.h>
apatrick@chromium.org8187fa82010-06-15 22:09:28 +000010#include <stdio.h>
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000011
daniel@transgaming.comb401a922012-10-26 18:58:24 +000012#include "compiler/preprocessor/SourceLocation.h"
Dmitry Skiba01971112015-07-10 14:54:00 -040013#include "compiler/translator/Cache.h"
Olli Etuahoac5274d2015-02-20 10:19:08 +020014#include "compiler/translator/glslang.h"
15#include "compiler/translator/ValidateSwitch.h"
Olli Etuahob0c645e2015-05-12 14:25:36 +030016#include "compiler/translator/ValidateGlobalInitializer.h"
Olli Etuaho37ad4742015-04-27 13:18:50 +030017#include "compiler/translator/util.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000018
Jamie Madill45bcc782016-11-07 13:58:48 -050019namespace sh
20{
21
alokp@chromium.org8b851c62012-06-15 16:25:11 +000022///////////////////////////////////////////////////////////////////////
23//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000024// Sub- vector and matrix fields
25//
26////////////////////////////////////////////////////////////////////////
27
Martin Radev2cc85b32016-08-05 16:22:53 +030028namespace
29{
30
31const int kWebGLMaxStructNesting = 4;
32
33bool ContainsSampler(const TType &type)
34{
35 if (IsSampler(type.getBasicType()))
36 return true;
37
38 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock())
39 {
40 const TFieldList &fields = type.getStruct()->fields();
41 for (unsigned int i = 0; i < fields.size(); ++i)
42 {
43 if (ContainsSampler(*fields[i]->type()))
44 return true;
45 }
46 }
47
48 return false;
49}
50
51bool ContainsImage(const TType &type)
52{
53 if (IsImage(type.getBasicType()))
54 return true;
55
56 if (type.getBasicType() == EbtStruct || type.isInterfaceBlock())
57 {
58 const TFieldList &fields = type.getStruct()->fields();
59 for (unsigned int i = 0; i < fields.size(); ++i)
60 {
61 if (ContainsImage(*fields[i]->type()))
62 return true;
63 }
64 }
65
66 return false;
67}
68
69} // namespace
70
Jamie Madillacb4b812016-11-07 13:50:29 -050071TParseContext::TParseContext(TSymbolTable &symt,
72 TExtensionBehavior &ext,
73 sh::GLenum type,
74 ShShaderSpec spec,
75 ShCompileOptions options,
76 bool checksPrecErrors,
77 TInfoSink &is,
78 const ShBuiltInResources &resources)
79 : intermediate(),
80 symbolTable(symt),
81 mDeferredSingleDeclarationErrorCheck(false),
82 mShaderType(type),
83 mShaderSpec(spec),
84 mCompileOptions(options),
85 mShaderVersion(100),
86 mTreeRoot(nullptr),
87 mLoopNestingLevel(0),
88 mStructNestingLevel(0),
89 mSwitchNestingLevel(0),
90 mCurrentFunctionType(nullptr),
91 mFunctionReturnsValue(false),
92 mChecksPrecisionErrors(checksPrecErrors),
93 mFragmentPrecisionHighOnESSL1(false),
94 mDefaultMatrixPacking(EmpColumnMajor),
95 mDefaultBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
96 mDiagnostics(is),
97 mDirectiveHandler(ext,
98 mDiagnostics,
99 mShaderVersion,
100 mShaderType,
101 resources.WEBGL_debug_shader_precision == 1),
102 mPreprocessor(&mDiagnostics, &mDirectiveHandler),
103 mScanner(nullptr),
104 mUsesFragData(false),
105 mUsesFragColor(false),
106 mUsesSecondaryOutputs(false),
107 mMinProgramTexelOffset(resources.MinProgramTexelOffset),
108 mMaxProgramTexelOffset(resources.MaxProgramTexelOffset),
109 mComputeShaderLocalSizeDeclared(false),
110 mDeclaringFunction(false)
111{
112 mComputeShaderLocalSize.fill(-1);
113}
114
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000115//
116// Look at a '.' field selector string and change it into offsets
117// for a vector.
118//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400119bool TParseContext::parseVectorFields(const TString &compString,
120 int vecSize,
121 TVectorFields &fields,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530122 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000123{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400124 fields.num = (int)compString.size();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530125 if (fields.num > 4)
126 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000127 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000128 return false;
129 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000130
Jamie Madillb98c3a82015-07-23 14:26:04 -0400131 enum
132 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000133 exyzw,
134 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +0000135 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000136 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000137
Arun Patole7e7e68d2015-05-22 12:02:25 +0530138 for (int i = 0; i < fields.num; ++i)
139 {
140 switch (compString[i])
141 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400142 case 'x':
143 fields.offsets[i] = 0;
144 fieldSet[i] = exyzw;
145 break;
146 case 'r':
147 fields.offsets[i] = 0;
148 fieldSet[i] = ergba;
149 break;
150 case 's':
151 fields.offsets[i] = 0;
152 fieldSet[i] = estpq;
153 break;
154 case 'y':
155 fields.offsets[i] = 1;
156 fieldSet[i] = exyzw;
157 break;
158 case 'g':
159 fields.offsets[i] = 1;
160 fieldSet[i] = ergba;
161 break;
162 case 't':
163 fields.offsets[i] = 1;
164 fieldSet[i] = estpq;
165 break;
166 case 'z':
167 fields.offsets[i] = 2;
168 fieldSet[i] = exyzw;
169 break;
170 case 'b':
171 fields.offsets[i] = 2;
172 fieldSet[i] = ergba;
173 break;
174 case 'p':
175 fields.offsets[i] = 2;
176 fieldSet[i] = estpq;
177 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530178
Jamie Madillb98c3a82015-07-23 14:26:04 -0400179 case 'w':
180 fields.offsets[i] = 3;
181 fieldSet[i] = exyzw;
182 break;
183 case 'a':
184 fields.offsets[i] = 3;
185 fieldSet[i] = ergba;
186 break;
187 case 'q':
188 fields.offsets[i] = 3;
189 fieldSet[i] = estpq;
190 break;
191 default:
192 error(line, "illegal vector field selection", compString.c_str());
193 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000194 }
195 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000196
Arun Patole7e7e68d2015-05-22 12:02:25 +0530197 for (int i = 0; i < fields.num; ++i)
198 {
199 if (fields.offsets[i] >= vecSize)
200 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400201 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000202 return false;
203 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000204
Arun Patole7e7e68d2015-05-22 12:02:25 +0530205 if (i > 0)
206 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400207 if (fieldSet[i] != fieldSet[i - 1])
Arun Patole7e7e68d2015-05-22 12:02:25 +0530208 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400209 error(line, "illegal - vector component fields not from the same set",
210 compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000211 return false;
212 }
213 }
214 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000215
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000216 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000217}
218
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000219///////////////////////////////////////////////////////////////////////
220//
221// Errors
222//
223////////////////////////////////////////////////////////////////////////
224
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000225
226//
227// Used by flex/bison to output all syntax and parsing errors.
228//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530229void TParseContext::error(const TSourceLoc &loc,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400230 const char *reason,
231 const char *token,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530232 const char *extraInfo)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000233{
Olli Etuaho1cc598f2016-08-18 13:50:30 +0300234 mDiagnostics.error(loc, reason, token, extraInfo);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000235}
236
Arun Patole7e7e68d2015-05-22 12:02:25 +0530237void TParseContext::warning(const TSourceLoc &loc,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400238 const char *reason,
239 const char *token,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530240 const char *extraInfo)
241{
Olli Etuaho1cc598f2016-08-18 13:50:30 +0300242 mDiagnostics.warning(loc, reason, token, extraInfo);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000243}
244
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200245void TParseContext::outOfRangeError(bool isError,
246 const TSourceLoc &loc,
247 const char *reason,
248 const char *token,
249 const char *extraInfo)
250{
251 if (isError)
252 {
253 error(loc, reason, token, extraInfo);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200254 }
255 else
256 {
257 warning(loc, reason, token, extraInfo);
258 }
259}
260
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000261//
262// Same error message for all places assignments don't work.
263//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530264void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000265{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000266 std::stringstream extraInfoStream;
267 extraInfoStream << "cannot convert from '" << right << "' to '" << left << "'";
268 std::string extraInfo = extraInfoStream.str();
269 error(line, "", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000270}
271
272//
273// Same error message for all places unary operations don't work.
274//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530275void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000276{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000277 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400278 extraInfoStream << "no operation '" << op << "' exists that takes an operand of type "
279 << operand << " (or there is no acceptable conversion)";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000280 std::string extraInfo = extraInfoStream.str();
281 error(line, " wrong operand type", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000282}
283
284//
285// Same error message for all binary operations don't work.
286//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400287void TParseContext::binaryOpError(const TSourceLoc &line,
288 const char *op,
289 TString left,
290 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000291{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000292 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400293 extraInfoStream << "no operation '" << op << "' exists that takes a left-hand operand of type '"
294 << left << "' and a right operand of type '" << right
295 << "' (or there is no acceptable conversion)";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000296 std::string extraInfo = extraInfoStream.str();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530297 error(line, " wrong operand types ", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000298}
299
Olli Etuaho856c4972016-08-08 11:38:39 +0300300void TParseContext::checkPrecisionSpecified(const TSourceLoc &line,
301 TPrecision precision,
302 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530303{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400304 if (!mChecksPrecisionErrors)
Olli Etuaho383b7912016-08-05 11:22:59 +0300305 return;
Martin Radev70866b82016-07-22 15:27:42 +0300306
307 if (precision != EbpUndefined && !SupportsPrecision(type))
308 {
309 error(line, "illegal type for precision qualifier", getBasicString(type));
310 }
311
Olli Etuaho183d7e22015-11-20 15:59:09 +0200312 if (precision == EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530313 {
Olli Etuaho183d7e22015-11-20 15:59:09 +0200314 switch (type)
315 {
316 case EbtFloat:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400317 error(line, "No precision specified for (float)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300318 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200319 case EbtInt:
320 case EbtUInt:
321 UNREACHABLE(); // there's always a predeclared qualifier
Jamie Madillb98c3a82015-07-23 14:26:04 -0400322 error(line, "No precision specified (int)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300323 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200324 default:
325 if (IsSampler(type))
326 {
327 error(line, "No precision specified (sampler)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300328 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200329 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300330 if (IsImage(type))
331 {
332 error(line, "No precision specified (image)", "");
333 return;
334 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200335 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000336 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000337}
338
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000339// Both test and if necessary, spit out an error, to see if the node is really
340// an l-value that can be operated on this way.
Olli Etuaho856c4972016-08-08 11:38:39 +0300341bool TParseContext::checkCanBeLValue(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000342{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400343 TIntermSymbol *symNode = node->getAsSymbolNode();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530344 TIntermBinary *binaryNode = node->getAsBinaryNode();
Olli Etuahob6fa0432016-09-28 16:28:05 +0100345 TIntermSwizzle *swizzleNode = node->getAsSwizzleNode();
346
347 if (swizzleNode)
348 {
349 bool ok = checkCanBeLValue(line, op, swizzleNode->getOperand());
350 if (ok && swizzleNode->hasDuplicateOffsets())
351 {
352 error(line, " l-value of swizzle cannot have duplicate components", op);
353 return false;
354 }
355 return ok;
356 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000357
Arun Patole7e7e68d2015-05-22 12:02:25 +0530358 if (binaryNode)
359 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400360 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530361 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400362 case EOpIndexDirect:
363 case EOpIndexIndirect:
364 case EOpIndexDirectStruct:
365 case EOpIndexDirectInterfaceBlock:
Olli Etuaho856c4972016-08-08 11:38:39 +0300366 return checkCanBeLValue(line, op, binaryNode->getLeft());
Jamie Madillb98c3a82015-07-23 14:26:04 -0400367 default:
368 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000369 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000370 error(line, " l-value required", op);
Olli Etuaho8a176262016-08-16 14:23:01 +0300371 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000372 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000373
Arun Patole7e7e68d2015-05-22 12:02:25 +0530374 const char *symbol = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000375 if (symNode != 0)
376 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000377
Arun Patole7e7e68d2015-05-22 12:02:25 +0530378 const char *message = 0;
379 switch (node->getQualifier())
380 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400381 case EvqConst:
382 message = "can't modify a const";
383 break;
384 case EvqConstReadOnly:
385 message = "can't modify a const";
386 break;
387 case EvqAttribute:
388 message = "can't modify an attribute";
389 break;
390 case EvqFragmentIn:
391 message = "can't modify an input";
392 break;
393 case EvqVertexIn:
394 message = "can't modify an input";
395 break;
396 case EvqUniform:
397 message = "can't modify a uniform";
398 break;
399 case EvqVaryingIn:
400 message = "can't modify a varying";
401 break;
402 case EvqFragCoord:
403 message = "can't modify gl_FragCoord";
404 break;
405 case EvqFrontFacing:
406 message = "can't modify gl_FrontFacing";
407 break;
408 case EvqPointCoord:
409 message = "can't modify gl_PointCoord";
410 break;
Martin Radevb0883602016-08-04 17:48:58 +0300411 case EvqNumWorkGroups:
412 message = "can't modify gl_NumWorkGroups";
413 break;
414 case EvqWorkGroupSize:
415 message = "can't modify gl_WorkGroupSize";
416 break;
417 case EvqWorkGroupID:
418 message = "can't modify gl_WorkGroupID";
419 break;
420 case EvqLocalInvocationID:
421 message = "can't modify gl_LocalInvocationID";
422 break;
423 case EvqGlobalInvocationID:
424 message = "can't modify gl_GlobalInvocationID";
425 break;
426 case EvqLocalInvocationIndex:
427 message = "can't modify gl_LocalInvocationIndex";
428 break;
Martin Radev802abe02016-08-04 17:48:32 +0300429 case EvqComputeIn:
430 message = "can't modify work group size variable";
431 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400432 default:
433 //
434 // Type that can't be written to?
435 //
436 if (node->getBasicType() == EbtVoid)
437 {
438 message = "can't modify void";
439 }
440 if (IsSampler(node->getBasicType()))
441 {
442 message = "can't modify a sampler";
443 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300444 if (IsImage(node->getBasicType()))
445 {
446 message = "can't modify an image";
447 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000448 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000449
Arun Patole7e7e68d2015-05-22 12:02:25 +0530450 if (message == 0 && binaryNode == 0 && symNode == 0)
451 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000452 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000453
Olli Etuaho8a176262016-08-16 14:23:01 +0300454 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000455 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000456
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000457 //
458 // Everything else is okay, no error.
459 //
460 if (message == 0)
Olli Etuaho8a176262016-08-16 14:23:01 +0300461 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000462
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000463 //
464 // If we get here, we have an error and a message.
465 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530466 if (symNode)
467 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000468 std::stringstream extraInfoStream;
469 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
470 std::string extraInfo = extraInfoStream.str();
471 error(line, " l-value required", op, extraInfo.c_str());
472 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530473 else
474 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000475 std::stringstream extraInfoStream;
476 extraInfoStream << "(" << message << ")";
477 std::string extraInfo = extraInfoStream.str();
478 error(line, " l-value required", op, extraInfo.c_str());
479 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000480
Olli Etuaho8a176262016-08-16 14:23:01 +0300481 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000482}
483
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000484// Both test, and if necessary spit out an error, to see if the node is really
485// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300486void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000487{
Olli Etuaho383b7912016-08-05 11:22:59 +0300488 if (node->getQualifier() != EvqConst)
489 {
490 error(node->getLine(), "constant expression required", "");
491 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000492}
493
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000494// Both test, and if necessary spit out an error, to see if the node is really
495// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300496void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000497{
Olli Etuaho383b7912016-08-05 11:22:59 +0300498 if (!node->isScalarInt())
499 {
500 error(node->getLine(), "integer expression required", token);
501 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000502}
503
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000504// Both test, and if necessary spit out an error, to see if we are currently
505// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800506bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000507{
Olli Etuaho856c4972016-08-08 11:38:39 +0300508 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300509 {
510 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800511 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300512 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800513 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000514}
515
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000516// For now, keep it simple: if it starts "gl_", it's reserved, independent
517// of scope. Except, if the symbol table is at the built-in push-level,
518// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000519// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
520// webgl shader.
Olli Etuaho856c4972016-08-08 11:38:39 +0300521bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000522{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530523 static const char *reservedErrMsg = "reserved built-in name";
524 if (!symbolTable.atBuiltInLevel())
525 {
526 if (identifier.compare(0, 3, "gl_") == 0)
527 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000528 error(line, reservedErrMsg, "gl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300529 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000530 }
Jamie Madillacb4b812016-11-07 13:50:29 -0500531 if (sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530532 {
533 if (identifier.compare(0, 6, "webgl_") == 0)
534 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000535 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300536 return false;
alokp@chromium.org613ef312010-07-21 18:54:22 +0000537 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530538 if (identifier.compare(0, 7, "_webgl_") == 0)
539 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000540 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300541 return false;
alokp@chromium.org613ef312010-07-21 18:54:22 +0000542 }
543 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530544 if (identifier.find("__") != TString::npos)
545 {
546 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400547 "identifiers containing two consecutive underscores (__) are reserved as "
548 "possible future keywords",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530549 identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300550 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000551 }
552 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000553
Olli Etuaho8a176262016-08-16 14:23:01 +0300554 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000555}
556
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000557// Make sure there is enough data provided to the constructor to build
558// something of the type of the constructor. Also returns the type of
559// the constructor.
Olli Etuaho856c4972016-08-08 11:38:39 +0300560bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
561 TIntermNode *argumentsNode,
562 const TFunction &function,
563 TOperator op,
564 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000565{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000566 bool constructingMatrix = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400567 switch (op)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530568 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400569 case EOpConstructMat2:
570 case EOpConstructMat2x3:
571 case EOpConstructMat2x4:
572 case EOpConstructMat3x2:
573 case EOpConstructMat3:
574 case EOpConstructMat3x4:
575 case EOpConstructMat4x2:
576 case EOpConstructMat4x3:
577 case EOpConstructMat4:
578 constructingMatrix = true;
579 break;
580 default:
581 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000582 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000583
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000584 //
585 // Note: It's okay to have too many components available, but not okay to have unused
586 // arguments. 'full' will go to true when enough args have been seen. If we loop
587 // again, there is an extra argument, so 'overfull' will become true.
588 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000589
Jamie Madillb98c3a82015-07-23 14:26:04 -0400590 size_t size = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400591 bool full = false;
592 bool overFull = false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000593 bool matrixInMatrix = false;
594 bool arrayArg = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530595 for (size_t i = 0; i < function.getParamCount(); ++i)
596 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700597 const TConstParameter &param = function.getParam(i);
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000598 size += param.type->getObjectSize();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530599
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000600 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000601 matrixInMatrix = true;
602 if (full)
603 overFull = true;
Olli Etuaho856c4972016-08-08 11:38:39 +0300604 if (op != EOpConstructStruct && !type.isArray() && size >= type.getObjectSize())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000605 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000606 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000607 arrayArg = true;
608 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530609
Olli Etuaho856c4972016-08-08 11:38:39 +0300610 if (type.isArray())
Olli Etuaho376f1b52015-04-13 13:23:41 +0300611 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300612 // The size of an unsized constructor should already have been determined.
613 ASSERT(!type.isUnsizedArray());
614 if (static_cast<size_t>(type.getArraySize()) != function.getParamCount())
Olli Etuaho376f1b52015-04-13 13:23:41 +0300615 {
616 error(line, "array constructor needs one argument per array element", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300617 return false;
Olli Etuaho376f1b52015-04-13 13:23:41 +0300618 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000619 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000620
Arun Patole7e7e68d2015-05-22 12:02:25 +0530621 if (arrayArg && op != EOpConstructStruct)
622 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000623 error(line, "constructing from a non-dereferenced array", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300624 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000625 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000626
Olli Etuaho856c4972016-08-08 11:38:39 +0300627 if (matrixInMatrix && !type.isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530628 {
629 if (function.getParamCount() != 1)
630 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400631 error(line, "constructing matrix from matrix can only take one argument",
632 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300633 return false;
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000634 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000635 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000636
Arun Patole7e7e68d2015-05-22 12:02:25 +0530637 if (overFull)
638 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000639 error(line, "too many arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300640 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000641 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530642
Olli Etuaho856c4972016-08-08 11:38:39 +0300643 if (op == EOpConstructStruct && !type.isArray() &&
644 type.getStruct()->fields().size() != function.getParamCount())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530645 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400646 error(line,
647 "Number of constructor parameters does not match the number of structure fields",
648 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300649 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000650 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000651
Olli Etuaho856c4972016-08-08 11:38:39 +0300652 if (!type.isMatrix() || !matrixInMatrix)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530653 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300654 if ((op != EOpConstructStruct && size != 1 && size < type.getObjectSize()) ||
655 (op == EOpConstructStruct && size < type.getObjectSize()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530656 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000657 error(line, "not enough data provided for construction", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300658 return false;
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000659 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000660 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000661
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200662 if (argumentsNode == nullptr)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530663 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200664 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300665 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000666 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200667
668 TIntermAggregate *argumentsAgg = argumentsNode->getAsAggregate();
669 for (TIntermNode *&argNode : *argumentsAgg->getSequence())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530670 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200671 TIntermTyped *argTyped = argNode->getAsTyped();
672 ASSERT(argTyped != nullptr);
673 if (op != EOpConstructStruct && IsSampler(argTyped->getBasicType()))
674 {
675 error(line, "cannot convert a sampler", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300676 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200677 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300678 if (op != EOpConstructStruct && IsImage(argTyped->getBasicType()))
679 {
680 error(line, "cannot convert an image", "constructor");
681 return false;
682 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200683 if (argTyped->getBasicType() == EbtVoid)
684 {
685 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300686 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200687 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000688 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000689
Olli Etuaho856c4972016-08-08 11:38:39 +0300690 if (type.isArray())
691 {
692 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
693 // the array.
694 for (TIntermNode *&argNode : *argumentsAgg->getSequence())
695 {
696 const TType &argType = argNode->getAsTyped()->getType();
697 // It has already been checked that the argument is not an array.
698 ASSERT(!argType.isArray());
699 if (!argType.sameElementType(type))
700 {
701 error(line, "Array constructor argument has an incorrect type", "Error");
Olli Etuaho8a176262016-08-16 14:23:01 +0300702 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300703 }
704 }
705 }
706 else if (op == EOpConstructStruct)
707 {
708 const TFieldList &fields = type.getStruct()->fields();
709 TIntermSequence *args = argumentsAgg->getSequence();
710
711 for (size_t i = 0; i < fields.size(); i++)
712 {
713 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
714 {
715 error(line, "Structure constructor arguments do not match structure fields",
716 "Error");
Olli Etuaho8a176262016-08-16 14:23:01 +0300717 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300718 }
719 }
720 }
721
Olli Etuaho8a176262016-08-16 14:23:01 +0300722 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000723}
724
Jamie Madillb98c3a82015-07-23 14:26:04 -0400725// This function checks to see if a void variable has been declared and raise an error message for
726// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000727//
728// returns true in case of an error
729//
Olli Etuaho856c4972016-08-08 11:38:39 +0300730bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400731 const TString &identifier,
732 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000733{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300734 if (type == EbtVoid)
735 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000736 error(line, "illegal use of type 'void'", identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300737 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300738 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000739
Olli Etuaho8a176262016-08-16 14:23:01 +0300740 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000741}
742
Jamie Madillb98c3a82015-07-23 14:26:04 -0400743// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300744// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300745void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000746{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530747 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector())
748 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000749 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530750 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000751}
752
Jamie Madillb98c3a82015-07-23 14:26:04 -0400753// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300754// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300755void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000756{
Martin Radev4a9cd802016-09-01 16:51:51 +0300757 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530758 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000759 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530760 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000761}
762
Olli Etuaho856c4972016-08-08 11:38:39 +0300763bool TParseContext::checkIsNotSampler(const TSourceLoc &line,
Martin Radev4a9cd802016-09-01 16:51:51 +0300764 const TTypeSpecifierNonArray &pType,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400765 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000766{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530767 if (pType.type == EbtStruct)
768 {
Martin Radev2cc85b32016-08-05 16:22:53 +0300769 if (ContainsSampler(*pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530770 {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000771 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
Olli Etuaho8a176262016-08-16 14:23:01 +0300772 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000773 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530774
Olli Etuaho8a176262016-08-16 14:23:01 +0300775 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530776 }
777 else if (IsSampler(pType.type))
778 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000779 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300780 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000781 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000782
Olli Etuaho8a176262016-08-16 14:23:01 +0300783 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000784}
785
Martin Radev2cc85b32016-08-05 16:22:53 +0300786bool TParseContext::checkIsNotImage(const TSourceLoc &line,
787 const TTypeSpecifierNonArray &pType,
788 const char *reason)
789{
790 if (pType.type == EbtStruct)
791 {
792 if (ContainsImage(*pType.userDef))
793 {
794 error(line, reason, getBasicString(pType.type), "(structure contains an image)");
795
796 return false;
797 }
798
799 return true;
800 }
801 else if (IsImage(pType.type))
802 {
803 error(line, reason, getBasicString(pType.type));
804
805 return false;
806 }
807
808 return true;
809}
810
Olli Etuaho856c4972016-08-08 11:38:39 +0300811void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
812 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400813{
814 if (pType.layoutQualifier.location != -1)
815 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400816 error(line, "location must only be specified for a single input or output variable",
817 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400818 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400819}
820
Olli Etuaho856c4972016-08-08 11:38:39 +0300821void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
822 const TLayoutQualifier &layoutQualifier)
823{
824 if (layoutQualifier.location != -1)
825 {
826 error(location, "invalid layout qualifier:", "location",
827 "only valid on program inputs and outputs");
828 }
829}
830
Martin Radev2cc85b32016-08-05 16:22:53 +0300831void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
832 TQualifier qualifier,
833 const TType &type)
834{
835 checkOutParameterIsNotSampler(line, qualifier, type);
836 checkOutParameterIsNotImage(line, qualifier, type);
837}
838
Olli Etuaho856c4972016-08-08 11:38:39 +0300839void TParseContext::checkOutParameterIsNotSampler(const TSourceLoc &line,
840 TQualifier qualifier,
841 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000842{
Martin Radev2cc85b32016-08-05 16:22:53 +0300843 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
844 if (IsSampler(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530845 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000846 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000847 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000848}
849
Martin Radev2cc85b32016-08-05 16:22:53 +0300850void TParseContext::checkOutParameterIsNotImage(const TSourceLoc &line,
851 TQualifier qualifier,
852 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000853{
Martin Radev2cc85b32016-08-05 16:22:53 +0300854 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
855 if (IsImage(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530856 {
Martin Radev2cc85b32016-08-05 16:22:53 +0300857 error(line, "images cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000858 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000859}
860
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000861// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300862unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000863{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530864 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000865
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200866 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
867 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
868 // fold as array size.
869 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000870 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000871 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300872 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000873 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000874
Olli Etuaho856c4972016-08-08 11:38:39 +0300875 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400876
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000877 if (constant->getBasicType() == EbtUInt)
878 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300879 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000880 }
881 else
882 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300883 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000884
Olli Etuaho856c4972016-08-08 11:38:39 +0300885 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000886 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400887 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300888 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000889 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400890
Olli Etuaho856c4972016-08-08 11:38:39 +0300891 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400892 }
893
Olli Etuaho856c4972016-08-08 11:38:39 +0300894 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400895 {
896 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300897 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400898 }
899
900 // The size of arrays is restricted here to prevent issues further down the
901 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
902 // 4096 registers so this should be reasonable even for aggressively optimizable code.
903 const unsigned int sizeLimit = 65536;
904
Olli Etuaho856c4972016-08-08 11:38:39 +0300905 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -0400906 {
907 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300908 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000909 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300910
911 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000912}
913
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000914// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +0300915bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
916 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000917{
Olli Etuaho8a176262016-08-16 14:23:01 +0300918 if ((elementQualifier.qualifier == EvqAttribute) ||
919 (elementQualifier.qualifier == EvqVertexIn) ||
920 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300921 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400922 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300923 TType(elementQualifier).getQualifierString());
924 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000925 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000926
Olli Etuaho8a176262016-08-16 14:23:01 +0300927 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000928}
929
Olli Etuaho8a176262016-08-16 14:23:01 +0300930// See if this element type can be formed into an array.
931bool TParseContext::checkIsValidTypeForArray(const TSourceLoc &line, const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000932{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000933 //
934 // Can the type be an array?
935 //
Olli Etuaho8a176262016-08-16 14:23:01 +0300936 if (elementType.array)
Jamie Madill06145232015-05-13 13:10:01 -0400937 {
Olli Etuaho8a176262016-08-16 14:23:01 +0300938 error(line, "cannot declare arrays of arrays",
939 TType(elementType).getCompleteString().c_str());
940 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000941 }
Olli Etuahocc36b982015-07-10 14:14:18 +0300942 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
943 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
944 // 4.3.4).
Martin Radev4a9cd802016-09-01 16:51:51 +0300945 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Olli Etuaho8a176262016-08-16 14:23:01 +0300946 sh::IsVarying(elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +0300947 {
948 error(line, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300949 TType(elementType).getCompleteString().c_str());
950 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +0300951 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000952
Olli Etuaho8a176262016-08-16 14:23:01 +0300953 return true;
954}
955
956// Check if this qualified element type can be formed into an array.
957bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
958 const TPublicType &elementType)
959{
960 if (checkIsValidTypeForArray(indexLocation, elementType))
961 {
962 return checkIsValidQualifierForArray(indexLocation, elementType);
963 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000964 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000965}
966
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000967// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +0300968void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
969 const TString &identifier,
970 TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000971{
Olli Etuaho3739d232015-04-08 12:23:44 +0300972 ASSERT(type != nullptr);
973 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000974 {
975 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300976 type->qualifier = EvqTemporary;
977
978 // Generate informative error messages for ESSL1.
979 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400980 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000981 {
Arun Patole7e7e68d2015-05-22 12:02:25 +0530982 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400983 "structures containing arrays may not be declared constant since they cannot be "
984 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530985 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000986 }
987 else
988 {
989 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
990 }
Olli Etuaho383b7912016-08-05 11:22:59 +0300991 return;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000992 }
Olli Etuaho376f1b52015-04-13 13:23:41 +0300993 if (type->isUnsizedArray())
994 {
995 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
Olli Etuaho376f1b52015-04-13 13:23:41 +0300996 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000997}
998
Olli Etuaho2935c582015-04-08 14:32:06 +0300999// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001000// and update the symbol table.
1001//
Olli Etuaho2935c582015-04-08 14:32:06 +03001002// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001003//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001004bool TParseContext::declareVariable(const TSourceLoc &line,
1005 const TString &identifier,
1006 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001007 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001008{
Olli Etuaho2935c582015-04-08 14:32:06 +03001009 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001010
Olli Etuaho856c4972016-08-08 11:38:39 +03001011 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001012
Olli Etuaho2935c582015-04-08 14:32:06 +03001013 // gl_LastFragData may be redeclared with a new precision qualifier
1014 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1015 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001016 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1017 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho856c4972016-08-08 11:38:39 +03001018 if (static_cast<int>(type.getArraySize()) == maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001019 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001020 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001021 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001022 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->getExtension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001023 }
1024 }
1025 else
1026 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001027 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1028 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001029 return false;
1030 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001031 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001032
Olli Etuaho8a176262016-08-16 14:23:01 +03001033 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001034 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001035
Olli Etuaho2935c582015-04-08 14:32:06 +03001036 (*variable) = new TVariable(&identifier, type);
1037 if (!symbolTable.declare(*variable))
1038 {
1039 error(line, "redefinition", identifier.c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -04001040 *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +03001041 return false;
1042 }
1043
Olli Etuaho8a176262016-08-16 14:23:01 +03001044 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001045 return false;
1046
1047 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001048}
1049
Martin Radev70866b82016-07-22 15:27:42 +03001050void TParseContext::checkIsParameterQualifierValid(
1051 const TSourceLoc &line,
1052 const TTypeQualifierBuilder &typeQualifierBuilder,
1053 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301054{
Olli Etuaho613b9592016-09-05 12:05:53 +03001055 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(&mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001056
1057 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301058 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001059 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1060 }
1061
1062 if (!IsImage(type->getBasicType()))
1063 {
1064 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, line);
1065 }
1066 else
1067 {
1068 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001069 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001070
Martin Radev70866b82016-07-22 15:27:42 +03001071 type->setQualifier(typeQualifier.qualifier);
1072
1073 if (typeQualifier.precision != EbpUndefined)
1074 {
1075 type->setPrecision(typeQualifier.precision);
1076 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001077}
1078
Olli Etuaho856c4972016-08-08 11:38:39 +03001079bool TParseContext::checkCanUseExtension(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001080{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001081 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001082 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +05301083 if (iter == extBehavior.end())
1084 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001085 error(line, "extension", extension.c_str(), "is not supported");
Olli Etuaho8a176262016-08-16 14:23:01 +03001086 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001087 }
zmo@google.comf5450912011-09-09 01:37:19 +00001088 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +05301089 if (iter->second == EBhDisable || iter->second == EBhUndefined)
1090 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001091 error(line, "extension", extension.c_str(), "is disabled");
Olli Etuaho8a176262016-08-16 14:23:01 +03001092 return false;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001093 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301094 if (iter->second == EBhWarn)
1095 {
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001096 warning(line, "extension", extension.c_str(), "is being used");
Olli Etuaho8a176262016-08-16 14:23:01 +03001097 return true;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001098 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001099
Olli Etuaho8a176262016-08-16 14:23:01 +03001100 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001101}
1102
Jamie Madillb98c3a82015-07-23 14:26:04 -04001103// These checks are common for all declarations starting a declarator list, and declarators that
1104// follow an empty declaration.
Olli Etuaho383b7912016-08-05 11:22:59 +03001105void TParseContext::singleDeclarationErrorCheck(const TPublicType &publicType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001106 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001107{
Olli Etuahofa33d582015-04-09 14:33:12 +03001108 switch (publicType.qualifier)
1109 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001110 case EvqVaryingIn:
1111 case EvqVaryingOut:
1112 case EvqAttribute:
1113 case EvqVertexIn:
1114 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001115 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001116 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001117 {
1118 error(identifierLocation, "cannot be used with a structure",
1119 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001120 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001121 }
Olli Etuahofa33d582015-04-09 14:33:12 +03001122
Jamie Madillb98c3a82015-07-23 14:26:04 -04001123 default:
1124 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001125 }
1126
Jamie Madillb98c3a82015-07-23 14:26:04 -04001127 if (publicType.qualifier != EvqUniform &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001128 !checkIsNotSampler(identifierLocation, publicType.typeSpecifierNonArray,
1129 "samplers must be uniform"))
Olli Etuahofa33d582015-04-09 14:33:12 +03001130 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001131 return;
Olli Etuahofa33d582015-04-09 14:33:12 +03001132 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001133 if (publicType.qualifier != EvqUniform &&
1134 !checkIsNotImage(identifierLocation, publicType.typeSpecifierNonArray,
1135 "images must be uniform"))
1136 {
1137 return;
1138 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001139
1140 // check for layout qualifier issues
1141 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
1142
1143 if (layoutQualifier.matrixPacking != EmpUnspecified)
1144 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001145 error(identifierLocation, "layout qualifier",
1146 getMatrixPackingString(layoutQualifier.matrixPacking),
Olli Etuahofa33d582015-04-09 14:33:12 +03001147 "only valid for interface blocks");
Olli Etuaho383b7912016-08-05 11:22:59 +03001148 return;
Jamie Madilla5efff92013-06-06 11:56:47 -04001149 }
1150
1151 if (layoutQualifier.blockStorage != EbsUnspecified)
1152 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001153 error(identifierLocation, "layout qualifier",
1154 getBlockStorageString(layoutQualifier.blockStorage),
Olli Etuahofa33d582015-04-09 14:33:12 +03001155 "only valid for interface blocks");
Olli Etuaho383b7912016-08-05 11:22:59 +03001156 return;
Jamie Madilla5efff92013-06-06 11:56:47 -04001157 }
1158
Olli Etuaho383b7912016-08-05 11:22:59 +03001159 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut)
Jamie Madilla5efff92013-06-06 11:56:47 -04001160 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001161 checkLocationIsNotSpecified(identifierLocation, publicType.layoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04001162 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001163
1164 if (IsImage(publicType.getBasicType()))
1165 {
1166
1167 switch (layoutQualifier.imageInternalFormat)
1168 {
1169 case EiifRGBA32F:
1170 case EiifRGBA16F:
1171 case EiifR32F:
1172 case EiifRGBA8:
1173 case EiifRGBA8_SNORM:
1174 if (!IsFloatImage(publicType.getBasicType()))
1175 {
1176 error(identifierLocation,
1177 "internal image format requires a floating image type",
1178 getBasicString(publicType.getBasicType()));
1179 return;
1180 }
1181 break;
1182 case EiifRGBA32I:
1183 case EiifRGBA16I:
1184 case EiifRGBA8I:
1185 case EiifR32I:
1186 if (!IsIntegerImage(publicType.getBasicType()))
1187 {
1188 error(identifierLocation,
1189 "internal image format requires an integer image type",
1190 getBasicString(publicType.getBasicType()));
1191 return;
1192 }
1193 break;
1194 case EiifRGBA32UI:
1195 case EiifRGBA16UI:
1196 case EiifRGBA8UI:
1197 case EiifR32UI:
1198 if (!IsUnsignedImage(publicType.getBasicType()))
1199 {
1200 error(identifierLocation,
1201 "internal image format requires an unsigned image type",
1202 getBasicString(publicType.getBasicType()));
1203 return;
1204 }
1205 break;
1206 case EiifUnspecified:
1207 error(identifierLocation, "layout qualifier", "No image internal format specified");
1208 return;
1209 default:
1210 error(identifierLocation, "layout qualifier", "unrecognized token");
1211 return;
1212 }
1213
1214 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1215 switch (layoutQualifier.imageInternalFormat)
1216 {
1217 case EiifR32F:
1218 case EiifR32I:
1219 case EiifR32UI:
1220 break;
1221 default:
1222 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1223 {
1224 error(identifierLocation, "layout qualifier",
1225 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1226 "image variables must be qualified readonly and/or writeonly");
1227 return;
1228 }
1229 break;
1230 }
1231 }
1232 else
1233 {
1234
1235 if (!checkInternalFormatIsNotSpecified(identifierLocation,
1236 layoutQualifier.imageInternalFormat))
1237 {
1238 return;
1239 }
1240
1241 if (!checkIsMemoryQualifierNotSpecified(publicType.memoryQualifier, identifierLocation))
1242 {
1243 return;
1244 }
1245 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001246}
1247
Olli Etuaho856c4972016-08-08 11:38:39 +03001248void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1249 const TString &layoutQualifierName,
1250 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001251{
1252
1253 if (mShaderVersion < versionRequired)
1254 {
1255 error(location, "invalid layout qualifier:", layoutQualifierName.c_str(), "not supported");
Martin Radev802abe02016-08-04 17:48:32 +03001256 }
1257}
1258
Olli Etuaho856c4972016-08-08 11:38:39 +03001259bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1260 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001261{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001262 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001263 for (size_t i = 0u; i < localSize.size(); ++i)
1264 {
1265 if (localSize[i] != -1)
1266 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03001267 error(location, "invalid layout qualifier:", getWorkGroupSizeString(i),
Martin Radev802abe02016-08-04 17:48:32 +03001268 "only valid when used with 'in' in a compute shader global layout declaration");
Olli Etuaho8a176262016-08-16 14:23:01 +03001269 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001270 }
1271 }
1272
Olli Etuaho8a176262016-08-16 14:23:01 +03001273 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001274}
1275
Martin Radev2cc85b32016-08-05 16:22:53 +03001276bool TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
1277 TLayoutImageInternalFormat internalFormat)
1278{
1279 if (internalFormat != EiifUnspecified)
1280 {
1281 error(location, "invalid layout qualifier:", getImageInternalFormatString(internalFormat),
1282 "only valid when used with images");
1283 return false;
1284 }
1285 return true;
1286}
1287
Olli Etuaho383b7912016-08-05 11:22:59 +03001288void TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate,
Olli Etuaho856c4972016-08-08 11:38:39 +03001289 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001290{
1291 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1292 {
1293 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1294 if (qual == EvqOut || qual == EvqInOut)
1295 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001296 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
Olli Etuaho8a176262016-08-16 14:23:01 +03001297 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001298 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001299 error(argument->getLine(),
Jamie Madillb98c3a82015-07-23 14:26:04 -04001300 "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error");
Olli Etuaho383b7912016-08-05 11:22:59 +03001301 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001302 }
1303 }
1304 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001305}
1306
Martin Radev70866b82016-07-22 15:27:42 +03001307void TParseContext::checkInvariantVariableQualifier(bool invariant,
1308 const TQualifier qualifier,
1309 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001310{
Martin Radev70866b82016-07-22 15:27:42 +03001311 if (!invariant)
1312 return;
1313
1314 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001315 {
Martin Radev70866b82016-07-22 15:27:42 +03001316 // input variables in the fragment shader can be also qualified as invariant
1317 if (!sh::CanBeInvariantESSL1(qualifier))
1318 {
1319 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1320 }
1321 }
1322 else
1323 {
1324 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1325 {
1326 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1327 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001328 }
1329}
1330
Arun Patole7e7e68d2015-05-22 12:02:25 +05301331bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001332{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001333 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001334 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1335 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001336}
1337
Arun Patole7e7e68d2015-05-22 12:02:25 +05301338bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001339{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001340 return ::IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001341}
1342
Jamie Madillb98c3a82015-07-23 14:26:04 -04001343void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1344 const char *extName,
1345 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001346{
1347 pp::SourceLocation srcLoc;
1348 srcLoc.file = loc.first_file;
1349 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001350 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001351}
1352
Jamie Madillb98c3a82015-07-23 14:26:04 -04001353void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1354 const char *name,
1355 const char *value,
1356 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001357{
1358 pp::SourceLocation srcLoc;
1359 srcLoc.file = loc.first_file;
1360 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001361 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001362}
1363
Martin Radev4c4c8e72016-08-04 12:25:34 +03001364sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001365{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001366 sh::WorkGroupSize result;
Martin Radev802abe02016-08-04 17:48:32 +03001367 for (size_t i = 0u; i < result.size(); ++i)
1368 {
1369 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1370 {
1371 result[i] = 1;
1372 }
1373 else
1374 {
1375 result[i] = mComputeShaderLocalSize[i];
1376 }
1377 }
1378 return result;
1379}
1380
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001381/////////////////////////////////////////////////////////////////////////////////
1382//
1383// Non-Errors.
1384//
1385/////////////////////////////////////////////////////////////////////////////////
1386
Jamie Madill5c097022014-08-20 16:38:32 -04001387const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1388 const TString *name,
1389 const TSymbol *symbol)
1390{
1391 const TVariable *variable = NULL;
1392
1393 if (!symbol)
1394 {
1395 error(location, "undeclared identifier", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001396 }
1397 else if (!symbol->isVariable())
1398 {
1399 error(location, "variable expected", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001400 }
1401 else
1402 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001403 variable = static_cast<const TVariable *>(symbol);
Jamie Madill5c097022014-08-20 16:38:32 -04001404
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001405 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
Olli Etuaho383b7912016-08-05 11:22:59 +03001406 !variable->getExtension().empty())
Jamie Madill5c097022014-08-20 16:38:32 -04001407 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001408 checkCanUseExtension(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001409 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001410
1411 // Reject shaders using both gl_FragData and gl_FragColor
1412 TQualifier qualifier = variable->getType().getQualifier();
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001413 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001414 {
1415 mUsesFragData = true;
1416 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001417 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001418 {
1419 mUsesFragColor = true;
1420 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001421 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1422 {
1423 mUsesSecondaryOutputs = true;
1424 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001425
1426 // This validation is not quite correct - it's only an error to write to
1427 // both FragData and FragColor. For simplicity, and because users shouldn't
1428 // be rewarded for reading from undefined varaibles, return an error
1429 // if they are both referenced, rather than assigned.
1430 if (mUsesFragData && mUsesFragColor)
1431 {
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001432 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1433 if (mUsesSecondaryOutputs)
1434 {
1435 errorMessage =
1436 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1437 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1438 }
1439 error(location, errorMessage, name->c_str());
Jamie Madill14e95b32015-05-07 10:10:41 -04001440 }
Martin Radevb0883602016-08-04 17:48:58 +03001441
1442 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1443 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1444 qualifier == EvqWorkGroupSize)
1445 {
1446 error(location,
1447 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1448 "gl_WorkGroupSize");
1449 }
Jamie Madill5c097022014-08-20 16:38:32 -04001450 }
1451
1452 if (!variable)
1453 {
1454 TType type(EbtFloat, EbpUndefined);
1455 TVariable *fakeVariable = new TVariable(name, type);
1456 symbolTable.declare(fakeVariable);
1457 variable = fakeVariable;
1458 }
1459
1460 return variable;
1461}
1462
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001463TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1464 const TString *name,
1465 const TSymbol *symbol)
1466{
1467 const TVariable *variable = getNamedVariable(location, name, symbol);
1468
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001469 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001470 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001471 const TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001472 return intermediate.addConstantUnion(constArray, variable->getType(), location);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001473 }
1474 else
1475 {
1476 return intermediate.addSymbol(variable->getUniqueId(), variable->getName(),
1477 variable->getType(), location);
1478 }
1479}
1480
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001481//
1482// Look up a function name in the symbol table, and make sure it is a function.
1483//
1484// Return the function symbol if found, otherwise 0.
1485//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001486const TFunction *TParseContext::findFunction(const TSourceLoc &line,
1487 TFunction *call,
1488 int inputShaderVersion,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301489 bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001490{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001491 // First find by unmangled name to check whether the function name has been
1492 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001493 // If a function is found, check for one with a matching argument list.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301494 const TSymbol *symbol = symbolTable.find(call->getName(), inputShaderVersion, builtIn);
1495 if (symbol == 0 || symbol->isFunction())
1496 {
Austin Kinross3ae64652015-01-26 15:51:39 -08001497 symbol = symbolTable.find(call->getMangledName(), inputShaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001498 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001499
Arun Patole7e7e68d2015-05-22 12:02:25 +05301500 if (symbol == 0)
1501 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001502 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001503 return 0;
1504 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001505
Arun Patole7e7e68d2015-05-22 12:02:25 +05301506 if (!symbol->isFunction())
1507 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001508 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001509 return 0;
1510 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001511
Jamie Madillb98c3a82015-07-23 14:26:04 -04001512 return static_cast<const TFunction *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001513}
1514
1515//
1516// Initializers show up in several places in the grammar. Have one set of
1517// code to handle them here.
1518//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001519// Returns true on error, false if no error
1520//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001521bool TParseContext::executeInitializer(const TSourceLoc &line,
1522 const TString &identifier,
1523 const TPublicType &pType,
1524 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001525 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001526{
Olli Etuaho13389b62016-10-16 11:48:18 +01001527 ASSERT(initNode != nullptr);
1528 ASSERT(*initNode == nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001529 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001530
Olli Etuaho2935c582015-04-08 14:32:06 +03001531 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001532 if (type.isUnsizedArray())
1533 {
Olli Etuaho02bd82c2016-11-03 10:29:43 +00001534 // We have not checked yet whether the initializer actually is an array or not.
1535 if (initializer->isArray())
1536 {
1537 type.setArraySize(initializer->getArraySize());
1538 }
1539 else
1540 {
1541 // Having a non-array initializer for an unsized array will result in an error later,
1542 // so we don't generate an error message here.
1543 type.setArraySize(1u);
1544 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001545 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001546 if (!declareVariable(line, identifier, type, &variable))
1547 {
1548 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001549 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001550
Olli Etuahob0c645e2015-05-12 14:25:36 +03001551 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001552 if (symbolTable.atGlobalLevel() &&
1553 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001554 {
1555 // Error message does not completely match behavior with ESSL 1.00, but
1556 // we want to steer developers towards only using constant expressions.
1557 error(line, "global variable initializers must be constant expressions", "=");
1558 return true;
1559 }
1560 if (globalInitWarning)
1561 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001562 warning(
1563 line,
1564 "global variable initializers should be constant expressions "
1565 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1566 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001567 }
1568
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001569 //
1570 // identifier must be of type constant, a global, or a temporary
1571 //
1572 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301573 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1574 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001575 error(line, " cannot initialize this type of qualifier ",
1576 variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001577 return true;
1578 }
1579 //
1580 // test for and propagate constant
1581 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001582
Arun Patole7e7e68d2015-05-22 12:02:25 +05301583 if (qualifier == EvqConst)
1584 {
1585 if (qualifier != initializer->getType().getQualifier())
1586 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001587 std::stringstream extraInfoStream;
1588 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1589 std::string extraInfo = extraInfoStream.str();
1590 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001591 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001592 return true;
1593 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301594 if (type != initializer->getType())
1595 {
1596 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001597 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001598 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001599 return true;
1600 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001601
1602 // Save the constant folded value to the variable if possible. For example array
1603 // initializers are not folded, since that way copying the array literal to multiple places
1604 // in the shader is avoided.
1605 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1606 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301607 if (initializer->getAsConstantUnion())
1608 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001609 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho13389b62016-10-16 11:48:18 +01001610 *initNode = nullptr;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001611 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301612 }
1613 else if (initializer->getAsSymbolNode())
1614 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001615 const TSymbol *symbol =
1616 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1617 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001618
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001619 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001620 if (constArray)
1621 {
1622 variable->shareConstPointer(constArray);
Olli Etuaho13389b62016-10-16 11:48:18 +01001623 *initNode = nullptr;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001624 return false;
1625 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001626 }
1627 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001628
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001629 TIntermSymbol *intermSymbol = intermediate.addSymbol(
1630 variable->getUniqueId(), variable->getName(), variable->getType(), line);
Olli Etuaho13389b62016-10-16 11:48:18 +01001631 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1632 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001633 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001634 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1635 return true;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001636 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001637
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001638 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001639}
1640
Olli Etuaho0e3aee32016-10-27 12:56:38 +01001641void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
1642{
1643 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
1644 typeSpecifier->getBasicType());
1645
1646 if (mShaderVersion < 300 && typeSpecifier->array)
1647 {
1648 error(typeSpecifier->getLine(), "not supported", "first-class array");
1649 typeSpecifier->clearArrayness();
1650 }
1651}
1652
Martin Radev70866b82016-07-22 15:27:42 +03001653TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301654 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001655{
Olli Etuaho613b9592016-09-05 12:05:53 +03001656 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001657
Martin Radev70866b82016-07-22 15:27:42 +03001658 TPublicType returnType = typeSpecifier;
1659 returnType.qualifier = typeQualifier.qualifier;
1660 returnType.invariant = typeQualifier.invariant;
1661 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03001662 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03001663 returnType.precision = typeSpecifier.precision;
1664
1665 if (typeQualifier.precision != EbpUndefined)
1666 {
1667 returnType.precision = typeQualifier.precision;
1668 }
1669
Martin Radev4a9cd802016-09-01 16:51:51 +03001670 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
1671 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03001672
Martin Radev4a9cd802016-09-01 16:51:51 +03001673 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
1674 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03001675
Martin Radev4a9cd802016-09-01 16:51:51 +03001676 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03001677
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001678 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001679 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001680 if (typeSpecifier.array)
1681 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001682 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001683 returnType.clearArrayness();
1684 }
1685
Martin Radev70866b82016-07-22 15:27:42 +03001686 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001687 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001688 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001689 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001690 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001691 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001692
Martin Radev70866b82016-07-22 15:27:42 +03001693 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001694 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001695 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001696 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001697 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001698 }
1699 }
1700 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001701 {
Martin Radev70866b82016-07-22 15:27:42 +03001702 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03001703 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001704 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03001705 }
Martin Radev70866b82016-07-22 15:27:42 +03001706 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
1707 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001708 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001709 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
1710 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001711 }
Martin Radev70866b82016-07-22 15:27:42 +03001712 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03001713 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001714 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03001715 "in");
Martin Radev802abe02016-08-04 17:48:32 +03001716 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001717 }
1718
1719 return returnType;
1720}
1721
Olli Etuaho856c4972016-08-08 11:38:39 +03001722void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
1723 const TPublicType &type,
1724 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03001725{
1726 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03001727 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03001728 {
1729 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001730 }
1731
1732 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
1733 switch (qualifier)
1734 {
1735 case EvqVertexIn:
1736 // ESSL 3.00 section 4.3.4
1737 if (type.array)
1738 {
1739 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001740 }
1741 // Vertex inputs with a struct type are disallowed in singleDeclarationErrorCheck
1742 return;
1743 case EvqFragmentOut:
1744 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03001745 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03001746 {
1747 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001748 }
1749 // Fragment outputs with a struct type are disallowed in singleDeclarationErrorCheck
1750 return;
1751 default:
1752 break;
1753 }
1754
1755 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
1756 // restrictions.
1757 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03001758 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
1759 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03001760 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
1761 {
1762 error(qualifierLocation, "must use 'flat' interpolation here",
1763 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001764 }
1765
Martin Radev4a9cd802016-09-01 16:51:51 +03001766 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03001767 {
1768 // ESSL 3.00 sections 4.3.4 and 4.3.6.
1769 // These restrictions are only implied by the ESSL 3.00 spec, but
1770 // the ESSL 3.10 spec lists these restrictions explicitly.
1771 if (type.array)
1772 {
1773 error(qualifierLocation, "cannot be an array of structures",
1774 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001775 }
1776 if (type.isStructureContainingArrays())
1777 {
1778 error(qualifierLocation, "cannot be a structure containing an array",
1779 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001780 }
1781 if (type.isStructureContainingType(EbtStruct))
1782 {
1783 error(qualifierLocation, "cannot be a structure containing a structure",
1784 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001785 }
1786 if (type.isStructureContainingType(EbtBool))
1787 {
1788 error(qualifierLocation, "cannot be a structure containing a bool",
1789 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001790 }
1791 }
1792}
1793
Martin Radev2cc85b32016-08-05 16:22:53 +03001794void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
1795{
1796 if (qualifier.getType() == QtStorage)
1797 {
1798 const TStorageQualifierWrapper &storageQualifier =
1799 static_cast<const TStorageQualifierWrapper &>(qualifier);
1800 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
1801 !symbolTable.atGlobalLevel())
1802 {
1803 error(storageQualifier.getLine(),
1804 "Local variables can only use the const storage qualifier.",
1805 storageQualifier.getQualifierString().c_str());
1806 }
1807 }
1808}
1809
1810bool TParseContext::checkIsMemoryQualifierNotSpecified(const TMemoryQualifier &memoryQualifier,
1811 const TSourceLoc &location)
1812{
1813 if (memoryQualifier.readonly)
1814 {
1815 error(location, "Only allowed with images.", "readonly");
1816 return false;
1817 }
1818 if (memoryQualifier.writeonly)
1819 {
1820 error(location, "Only allowed with images.", "writeonly");
1821 return false;
1822 }
Martin Radev049edfa2016-11-11 14:35:37 +02001823 if (memoryQualifier.coherent)
1824 {
1825 error(location, "Only allowed with images.", "coherent");
1826 return false;
1827 }
1828 if (memoryQualifier.restrictQualifier)
1829 {
1830 error(location, "Only allowed with images.", "restrict");
1831 return false;
1832 }
1833 if (memoryQualifier.volatileQualifier)
1834 {
1835 error(location, "Only allowed with images.", "volatile");
1836 return false;
1837 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001838 return true;
1839}
1840
Olli Etuaho13389b62016-10-16 11:48:18 +01001841TIntermDeclaration *TParseContext::parseSingleDeclaration(
1842 TPublicType &publicType,
1843 const TSourceLoc &identifierOrTypeLocation,
1844 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001845{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001846 TType type(publicType);
1847 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
1848 mDirectiveHandler.pragma().stdgl.invariantAll)
1849 {
1850 TQualifier qualifier = type.getQualifier();
1851
1852 // The directive handler has already taken care of rejecting invalid uses of this pragma
1853 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
1854 // affected variable declarations:
1855 //
1856 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
1857 // elsewhere, in TranslatorGLSL.)
1858 //
1859 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
1860 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
1861 // the way this is currently implemented we have to enable this compiler option before
1862 // parsing the shader and determining the shading language version it uses. If this were
1863 // implemented as a post-pass, the workaround could be more targeted.
1864 //
1865 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
1866 // the specification, but there are desktop OpenGL drivers that expect that this is the
1867 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
1868 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
1869 {
1870 type.setInvariant(true);
1871 }
1872 }
1873
1874 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, type, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001875
Olli Etuahobab4c082015-04-24 16:38:49 +03001876 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03001877
Olli Etuahobab4c082015-04-24 16:38:49 +03001878 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1879
Olli Etuaho13389b62016-10-16 11:48:18 +01001880 TIntermDeclaration *declaration = new TIntermDeclaration();
1881 declaration->setLine(identifierOrTypeLocation);
1882
Olli Etuahobab4c082015-04-24 16:38:49 +03001883 if (emptyDeclaration)
1884 {
1885 if (publicType.isUnsizedArray())
1886 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001887 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1888 // error. It is assumed that this applies to empty declarations as well.
1889 error(identifierOrTypeLocation, "empty array declaration needs to specify a size",
1890 identifier.c_str());
Olli Etuahobab4c082015-04-24 16:38:49 +03001891 }
1892 }
1893 else
Jamie Madill60ed9812013-06-06 11:56:46 -04001894 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001895 singleDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001896
Olli Etuaho856c4972016-08-08 11:38:39 +03001897 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001898
Olli Etuaho2935c582015-04-08 14:32:06 +03001899 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001900 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04001901
1902 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01001903 {
Jamie Madill60ed9812013-06-06 11:56:46 -04001904 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01001905 }
Jamie Madill60ed9812013-06-06 11:56:46 -04001906 }
1907
Olli Etuaho13389b62016-10-16 11:48:18 +01001908 // We append the symbol even if the declaration is empty, mainly because of struct declarations
1909 // that may just declare a type.
1910 declaration->appendDeclarator(symbol);
1911
1912 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04001913}
1914
Olli Etuaho13389b62016-10-16 11:48:18 +01001915TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1916 const TSourceLoc &identifierLocation,
1917 const TString &identifier,
1918 const TSourceLoc &indexLocation,
1919 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001920{
Olli Etuahofa33d582015-04-09 14:33:12 +03001921 mDeferredSingleDeclarationErrorCheck = false;
1922
Olli Etuaho383b7912016-08-05 11:22:59 +03001923 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001924
Olli Etuaho856c4972016-08-08 11:38:39 +03001925 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001926
Olli Etuaho8a176262016-08-16 14:23:01 +03001927 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001928
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001929 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001930
Olli Etuaho856c4972016-08-08 11:38:39 +03001931 unsigned int size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001932 // Make the type an array even if size check failed.
1933 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1934 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001935
Olli Etuaho2935c582015-04-08 14:32:06 +03001936 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03001937 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04001938
Olli Etuaho13389b62016-10-16 11:48:18 +01001939 TIntermDeclaration *declaration = new TIntermDeclaration();
1940 declaration->setLine(identifierLocation);
1941
Olli Etuahoe7847b02015-03-16 11:56:12 +02001942 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001943 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01001944 {
Jamie Madill60ed9812013-06-06 11:56:46 -04001945 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01001946 declaration->appendDeclarator(symbol);
1947 }
Jamie Madill60ed9812013-06-06 11:56:46 -04001948
Olli Etuaho13389b62016-10-16 11:48:18 +01001949 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04001950}
1951
Olli Etuaho13389b62016-10-16 11:48:18 +01001952TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
1953 const TSourceLoc &identifierLocation,
1954 const TString &identifier,
1955 const TSourceLoc &initLocation,
1956 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001957{
Olli Etuahofa33d582015-04-09 14:33:12 +03001958 mDeferredSingleDeclarationErrorCheck = false;
1959
Olli Etuaho383b7912016-08-05 11:22:59 +03001960 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001961
Olli Etuaho13389b62016-10-16 11:48:18 +01001962 TIntermDeclaration *declaration = new TIntermDeclaration();
1963 declaration->setLine(identifierLocation);
1964
1965 TIntermBinary *initNode = nullptr;
1966 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04001967 {
Olli Etuaho13389b62016-10-16 11:48:18 +01001968 if (initNode)
1969 {
1970 declaration->appendDeclarator(initNode);
1971 }
Jamie Madill60ed9812013-06-06 11:56:46 -04001972 }
Olli Etuaho13389b62016-10-16 11:48:18 +01001973 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04001974}
1975
Olli Etuaho13389b62016-10-16 11:48:18 +01001976TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Jamie Madillb98c3a82015-07-23 14:26:04 -04001977 TPublicType &publicType,
1978 const TSourceLoc &identifierLocation,
1979 const TString &identifier,
1980 const TSourceLoc &indexLocation,
1981 TIntermTyped *indexExpression,
1982 const TSourceLoc &initLocation,
1983 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001984{
1985 mDeferredSingleDeclarationErrorCheck = false;
1986
Olli Etuaho383b7912016-08-05 11:22:59 +03001987 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001988
Olli Etuaho8a176262016-08-16 14:23:01 +03001989 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001990
1991 TPublicType arrayType(publicType);
1992
Olli Etuaho856c4972016-08-08 11:38:39 +03001993 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001994 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
1995 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03001996 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001997 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001998 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03001999 }
2000 // Make the type an array even if size check failed.
2001 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2002 arrayType.setArraySize(size);
2003
Olli Etuaho13389b62016-10-16 11:48:18 +01002004 TIntermDeclaration *declaration = new TIntermDeclaration();
2005 declaration->setLine(identifierLocation);
2006
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002007 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002008 TIntermBinary *initNode = nullptr;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002009 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
2010 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002011 if (initNode)
2012 {
2013 declaration->appendDeclarator(initNode);
2014 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002015 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002016
2017 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002018}
2019
Martin Radev70866b82016-07-22 15:27:42 +03002020TIntermAggregate *TParseContext::parseInvariantDeclaration(
2021 const TTypeQualifierBuilder &typeQualifierBuilder,
2022 const TSourceLoc &identifierLoc,
2023 const TString *identifier,
2024 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002025{
Olli Etuaho613b9592016-09-05 12:05:53 +03002026 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002027
Martin Radev70866b82016-07-22 15:27:42 +03002028 if (!typeQualifier.invariant)
2029 {
2030 error(identifierLoc, "Expected invariant", identifier->c_str());
2031 return nullptr;
2032 }
2033 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2034 {
2035 return nullptr;
2036 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002037 if (!symbol)
2038 {
2039 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002040 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002041 }
Martin Radev70866b82016-07-22 15:27:42 +03002042 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002043 {
Martin Radev70866b82016-07-22 15:27:42 +03002044 error(identifierLoc, "invariant declaration specifies qualifier",
2045 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002046 }
Martin Radev70866b82016-07-22 15:27:42 +03002047 if (typeQualifier.precision != EbpUndefined)
2048 {
2049 error(identifierLoc, "invariant declaration specifies precision",
2050 getPrecisionString(typeQualifier.precision));
2051 }
2052 if (!typeQualifier.layoutQualifier.isEmpty())
2053 {
2054 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2055 }
2056
2057 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
2058 ASSERT(variable);
2059 const TType &type = variable->getType();
2060
2061 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2062 typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002063 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002064
2065 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2066
2067 TIntermSymbol *intermSymbol =
2068 intermediate.addSymbol(variable->getUniqueId(), *identifier, type, identifierLoc);
2069
Olli Etuaho32db19b2016-10-04 14:43:16 +01002070 TIntermAggregate *aggregate = TIntermediate::MakeAggregate(intermSymbol, identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002071 aggregate->setOp(EOpInvariantDeclaration);
2072 return aggregate;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002073}
2074
Olli Etuaho13389b62016-10-16 11:48:18 +01002075void TParseContext::parseDeclarator(TPublicType &publicType,
2076 const TSourceLoc &identifierLocation,
2077 const TString &identifier,
2078 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002079{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002080 // If the declaration starting this declarator list was empty (example: int,), some checks were
2081 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002082 if (mDeferredSingleDeclarationErrorCheck)
2083 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002084 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002085 mDeferredSingleDeclarationErrorCheck = false;
2086 }
2087
Olli Etuaho856c4972016-08-08 11:38:39 +03002088 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002089
Olli Etuaho856c4972016-08-08 11:38:39 +03002090 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002091
Olli Etuaho2935c582015-04-08 14:32:06 +03002092 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002093 declareVariable(identifierLocation, identifier, TType(publicType), &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002094
Jamie Madillb98c3a82015-07-23 14:26:04 -04002095 TIntermSymbol *symbol =
2096 intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002097 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01002098 {
Jamie Madill502d66f2013-06-20 11:55:52 -04002099 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01002100 declarationOut->appendDeclarator(symbol);
2101 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002102}
2103
Olli Etuaho13389b62016-10-16 11:48:18 +01002104void TParseContext::parseArrayDeclarator(TPublicType &publicType,
2105 const TSourceLoc &identifierLocation,
2106 const TString &identifier,
2107 const TSourceLoc &arrayLocation,
2108 TIntermTyped *indexExpression,
2109 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002110{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002111 // If the declaration starting this declarator list was empty (example: int,), some checks were
2112 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002113 if (mDeferredSingleDeclarationErrorCheck)
2114 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002115 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002116 mDeferredSingleDeclarationErrorCheck = false;
2117 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002118
Olli Etuaho856c4972016-08-08 11:38:39 +03002119 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002120
Olli Etuaho856c4972016-08-08 11:38:39 +03002121 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002122
Olli Etuaho8a176262016-08-16 14:23:01 +03002123 if (checkIsValidTypeAndQualifierForArray(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002124 {
Olli Etuahoe7847b02015-03-16 11:56:12 +02002125 TType arrayType = TType(publicType);
Olli Etuaho856c4972016-08-08 11:38:39 +03002126 unsigned int size = checkIsValidArraySize(arrayLocation, indexExpression);
Olli Etuaho693c9aa2015-04-07 17:50:36 +03002127 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002128
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002129 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002130 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002131
Jamie Madillb98c3a82015-07-23 14:26:04 -04002132 TIntermSymbol *symbol =
2133 intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002134 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002135 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002136
Olli Etuaho13389b62016-10-16 11:48:18 +01002137 declarationOut->appendDeclarator(symbol);
Jamie Madill502d66f2013-06-20 11:55:52 -04002138 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002139}
2140
Olli Etuaho13389b62016-10-16 11:48:18 +01002141void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2142 const TSourceLoc &identifierLocation,
2143 const TString &identifier,
2144 const TSourceLoc &initLocation,
2145 TIntermTyped *initializer,
2146 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002147{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002148 // If the declaration starting this declarator list was empty (example: int,), some checks were
2149 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002150 if (mDeferredSingleDeclarationErrorCheck)
2151 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002152 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002153 mDeferredSingleDeclarationErrorCheck = false;
2154 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002155
Olli Etuaho856c4972016-08-08 11:38:39 +03002156 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002157
Olli Etuaho13389b62016-10-16 11:48:18 +01002158 TIntermBinary *initNode = nullptr;
2159 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002160 {
2161 //
2162 // build the intermediate representation
2163 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002164 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002165 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002166 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002167 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002168 }
2169}
2170
Olli Etuaho13389b62016-10-16 11:48:18 +01002171void TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
2172 const TSourceLoc &identifierLocation,
2173 const TString &identifier,
2174 const TSourceLoc &indexLocation,
2175 TIntermTyped *indexExpression,
2176 const TSourceLoc &initLocation,
2177 TIntermTyped *initializer,
2178 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002179{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002180 // If the declaration starting this declarator list was empty (example: int,), some checks were
2181 // not performed.
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002182 if (mDeferredSingleDeclarationErrorCheck)
2183 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002184 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002185 mDeferredSingleDeclarationErrorCheck = false;
2186 }
2187
Olli Etuaho856c4972016-08-08 11:38:39 +03002188 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002189
Olli Etuaho8a176262016-08-16 14:23:01 +03002190 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002191
2192 TPublicType arrayType(publicType);
2193
Olli Etuaho856c4972016-08-08 11:38:39 +03002194 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002195 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2196 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002197 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002198 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002199 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002200 }
2201 // Make the type an array even if size check failed.
2202 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2203 arrayType.setArraySize(size);
2204
2205 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002206 TIntermBinary *initNode = nullptr;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002207 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
2208 {
2209 if (initNode)
2210 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002211 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002212 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002213 }
2214}
2215
Martin Radev70866b82016-07-22 15:27:42 +03002216void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002217{
Olli Etuaho613b9592016-09-05 12:05:53 +03002218 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002219 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002220
Martin Radev70866b82016-07-22 15:27:42 +03002221 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2222 typeQualifier.line);
2223
Jamie Madillc2128ff2016-07-04 10:26:17 -04002224 // It should never be the case, but some strange parser errors can send us here.
2225 if (layoutQualifier.isEmpty())
2226 {
2227 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002228 return;
2229 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002230
Martin Radev802abe02016-08-04 17:48:32 +03002231 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002232 {
Martin Radev802abe02016-08-04 17:48:32 +03002233 error(typeQualifier.line, "invalid combination:", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002234 return;
2235 }
2236
Martin Radev2cc85b32016-08-05 16:22:53 +03002237 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
2238
2239 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2240
Martin Radev802abe02016-08-04 17:48:32 +03002241 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002242 {
Martin Radev802abe02016-08-04 17:48:32 +03002243 if (mComputeShaderLocalSizeDeclared &&
2244 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2245 {
2246 error(typeQualifier.line, "Work group size does not match the previous declaration",
2247 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002248 return;
2249 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002250
Martin Radev802abe02016-08-04 17:48:32 +03002251 if (mShaderVersion < 310)
2252 {
2253 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002254 return;
2255 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002256
Martin Radev4c4c8e72016-08-04 12:25:34 +03002257 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03002258 {
2259 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002260 return;
2261 }
2262
2263 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
2264 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
2265
2266 const TConstantUnion *maxComputeWorkGroupSizeData =
2267 maxComputeWorkGroupSize->getConstPointer();
2268
2269 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
2270 {
2271 if (layoutQualifier.localSize[i] != -1)
2272 {
2273 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
2274 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
2275 if (mComputeShaderLocalSize[i] < 1 ||
2276 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
2277 {
2278 std::stringstream errorMessageStream;
2279 errorMessageStream << "Value must be at least 1 and no greater than "
2280 << maxComputeWorkGroupSizeValue;
2281 const std::string &errorMessage = errorMessageStream.str();
2282
Martin Radev4c4c8e72016-08-04 12:25:34 +03002283 error(typeQualifier.line, "invalid value:", getWorkGroupSizeString(i),
Martin Radev802abe02016-08-04 17:48:32 +03002284 errorMessage.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03002285 return;
2286 }
2287 }
2288 }
2289
2290 mComputeShaderLocalSizeDeclared = true;
2291 }
2292 else
Jamie Madill1566ef72013-06-20 11:55:54 -04002293 {
Martin Radev802abe02016-08-04 17:48:32 +03002294
Olli Etuaho8a176262016-08-16 14:23:01 +03002295 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03002296 {
Martin Radev802abe02016-08-04 17:48:32 +03002297 return;
2298 }
2299
2300 if (typeQualifier.qualifier != EvqUniform)
2301 {
2302 error(typeQualifier.line, "invalid qualifier:",
2303 getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
Martin Radev802abe02016-08-04 17:48:32 +03002304 return;
2305 }
2306
2307 if (mShaderVersion < 300)
2308 {
2309 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
2310 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002311 return;
2312 }
2313
Olli Etuaho856c4972016-08-08 11:38:39 +03002314 checkLocationIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002315
2316 if (layoutQualifier.matrixPacking != EmpUnspecified)
2317 {
2318 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
2319 }
2320
2321 if (layoutQualifier.blockStorage != EbsUnspecified)
2322 {
2323 mDefaultBlockStorage = layoutQualifier.blockStorage;
2324 }
Jamie Madill1566ef72013-06-20 11:55:54 -04002325 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002326}
2327
Olli Etuaho476197f2016-10-11 13:59:08 +01002328TIntermAggregate *TParseContext::addFunctionPrototypeDeclaration(const TFunction &parsedFunction,
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002329 const TSourceLoc &location)
2330{
Olli Etuaho476197f2016-10-11 13:59:08 +01002331 // Note: function found from the symbol table could be the same as parsedFunction if this is the
2332 // first declaration. Either way the instance in the symbol table is used to track whether the
2333 // function is declared multiple times.
2334 TFunction *function = static_cast<TFunction *>(
2335 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
2336 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02002337 {
2338 // ESSL 1.00.17 section 4.2.7.
2339 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
2340 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02002341 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002342 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02002343
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002344 TIntermAggregate *prototype = new TIntermAggregate;
Olli Etuaho476197f2016-10-11 13:59:08 +01002345 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
2346 // point to the data that already exists in the symbol table.
2347 prototype->setType(function->getReturnType());
Olli Etuahobd674552016-10-06 13:28:42 +01002348 prototype->getFunctionSymbolInfo()->setFromFunction(*function);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002349
Olli Etuaho476197f2016-10-11 13:59:08 +01002350 for (size_t i = 0; i < function->getParamCount(); i++)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002351 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002352 const TConstParameter &param = function->getParam(i);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002353 if (param.name != 0)
2354 {
2355 TVariable variable(param.name, *param.type);
2356
2357 TIntermSymbol *paramSymbol = intermediate.addSymbol(
2358 variable.getUniqueId(), variable.getName(), variable.getType(), location);
2359 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
2360 }
2361 else
2362 {
2363 TIntermSymbol *paramSymbol = intermediate.addSymbol(0, "", *param.type, location);
2364 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
2365 }
2366 }
2367
2368 prototype->setOp(EOpPrototype);
2369
2370 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002371
2372 if (!symbolTable.atGlobalLevel())
2373 {
2374 // ESSL 3.00.4 section 4.2.4.
2375 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002376 }
2377
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002378 return prototype;
2379}
2380
Olli Etuaho336b1472016-10-05 16:37:55 +01002381TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
2382 const TFunction &function,
2383 TIntermAggregate *functionParameters,
2384 TIntermBlock *functionBody,
2385 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002386{
Olli Etuahof51fdd22016-10-03 10:03:40 +01002387 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002388 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
2389 {
2390 error(location, "function does not return a value:", "", function.getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002391 }
2392
Olli Etuahof51fdd22016-10-03 10:03:40 +01002393 if (functionBody == nullptr)
2394 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01002395 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002396 functionBody->setLine(location);
2397 }
Olli Etuaho336b1472016-10-05 16:37:55 +01002398 TIntermFunctionDefinition *functionNode =
2399 new TIntermFunctionDefinition(function.getReturnType(), functionParameters, functionBody);
2400 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01002401
Olli Etuahobd674552016-10-06 13:28:42 +01002402 functionNode->getFunctionSymbolInfo()->setFromFunction(function);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002403
2404 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002405 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002406}
2407
Olli Etuaho476197f2016-10-11 13:59:08 +01002408void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
2409 TFunction **function,
2410 TIntermAggregate **aggregateOut)
Jamie Madill185fb402015-06-12 15:48:48 -04002411{
Olli Etuaho476197f2016-10-11 13:59:08 +01002412 ASSERT(function);
2413 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002414 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01002415 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002416
2417 if (builtIn)
2418 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002419 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002420 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002421 else
Jamie Madill185fb402015-06-12 15:48:48 -04002422 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002423 TFunction *prevDec = static_cast<TFunction *>(
2424 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
2425
2426 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
2427 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
2428 // occurance.
2429 if (*function != prevDec)
2430 {
2431 // Swap the parameters of the previous declaration to the parameters of the function
2432 // definition (parameter names may differ).
2433 prevDec->swapParameters(**function);
2434
2435 // The function definition will share the same symbol as any previous declaration.
2436 *function = prevDec;
2437 }
2438
2439 if ((*function)->isDefined())
2440 {
2441 error(location, "function already has a body", (*function)->getName().c_str());
2442 }
2443
2444 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04002445 }
Jamie Madill185fb402015-06-12 15:48:48 -04002446
2447 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuaho476197f2016-10-11 13:59:08 +01002448 if ((*function)->getName() == "main")
Jamie Madill185fb402015-06-12 15:48:48 -04002449 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002450 if ((*function)->getParamCount() > 0)
Jamie Madill185fb402015-06-12 15:48:48 -04002451 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002452 error(location, "function cannot take any parameter(s)",
2453 (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002454 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002455 if ((*function)->getReturnType().getBasicType() != EbtVoid)
Jamie Madill185fb402015-06-12 15:48:48 -04002456 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002457 error(location, "", (*function)->getReturnType().getBasicString(),
Jamie Madillb98c3a82015-07-23 14:26:04 -04002458 "main function cannot return a value");
Jamie Madill185fb402015-06-12 15:48:48 -04002459 }
2460 }
2461
2462 //
2463 // Remember the return type for later checking for RETURN statements.
2464 //
Olli Etuaho476197f2016-10-11 13:59:08 +01002465 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002466 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04002467
2468 //
2469 // Insert parameters into the symbol table.
2470 // If the parameter has no name, it's not an error, just don't insert it
2471 // (could be used for unused args).
2472 //
2473 // Also, accumulate the list of parameters into the HIL, so lower level code
2474 // knows where to find parameters.
2475 //
2476 TIntermAggregate *paramNodes = new TIntermAggregate;
Olli Etuaho476197f2016-10-11 13:59:08 +01002477 for (size_t i = 0; i < (*function)->getParamCount(); i++)
Jamie Madill185fb402015-06-12 15:48:48 -04002478 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002479 const TConstParameter &param = (*function)->getParam(i);
Jamie Madill185fb402015-06-12 15:48:48 -04002480 if (param.name != 0)
2481 {
2482 TVariable *variable = new TVariable(param.name, *param.type);
2483 //
2484 // Insert the parameters with name in the symbol table.
2485 //
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002486 if (!symbolTable.declare(variable))
2487 {
Jamie Madill185fb402015-06-12 15:48:48 -04002488 error(location, "redefinition", variable->getName().c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002489 paramNodes = intermediate.growAggregate(
2490 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
2491 continue;
Jamie Madill185fb402015-06-12 15:48:48 -04002492 }
2493
2494 //
2495 // Add the parameter to the HIL
2496 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002497 TIntermSymbol *symbol = intermediate.addSymbol(
2498 variable->getUniqueId(), variable->getName(), variable->getType(), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002499
2500 paramNodes = intermediate.growAggregate(paramNodes, symbol, location);
2501 }
2502 else
2503 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002504 paramNodes = intermediate.growAggregate(
2505 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002506 }
2507 }
2508 intermediate.setAggregateOperator(paramNodes, EOpParameters, location);
2509 *aggregateOut = paramNodes;
2510 setLoopNestingLevel(0);
2511}
2512
Jamie Madillb98c3a82015-07-23 14:26:04 -04002513TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002514{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002515 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002516 // We don't know at this point whether this is a function definition or a prototype.
2517 // The definition production code will check for redefinitions.
2518 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002519 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002520 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
2521 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002522 //
2523 TFunction *prevDec =
2524 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302525
2526 if (getShaderVersion() >= 300 && symbolTable.hasUnmangledBuiltIn(function->getName().c_str()))
2527 {
2528 // With ESSL 3.00, names of built-in functions cannot be redeclared as functions.
2529 // Therefore overloading or redefining builtin functions is an error.
2530 error(location, "Name of a built-in function cannot be redeclared as function",
2531 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302532 }
2533 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002534 {
2535 if (prevDec->getReturnType() != function->getReturnType())
2536 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002537 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002538 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04002539 }
2540 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2541 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002542 if (prevDec->getParam(i).type->getQualifier() !=
2543 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002544 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002545 error(location,
2546 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002547 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04002548 }
2549 }
2550 }
2551
2552 //
2553 // Check for previously declared variables using the same name.
2554 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002555 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002556 if (prevSym)
2557 {
2558 if (!prevSym->isFunction())
2559 {
2560 error(location, "redefinition", function->getName().c_str(), "function");
Jamie Madill185fb402015-06-12 15:48:48 -04002561 }
2562 }
2563 else
2564 {
2565 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01002566 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04002567 }
2568
2569 // We're at the inner scope level of the function's arguments and body statement.
2570 // Add the function prototype to the surrounding scope instead.
2571 symbolTable.getOuterLevel()->insert(function);
2572
2573 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002574 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2575 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002576 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2577 //
2578 return function;
2579}
2580
Olli Etuaho9de84a52016-06-14 17:36:01 +03002581TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
2582 const TString *name,
2583 const TSourceLoc &location)
2584{
2585 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
2586 {
2587 error(location, "no qualifiers allowed for function return",
2588 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03002589 }
2590 if (!type.layoutQualifier.isEmpty())
2591 {
2592 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03002593 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002594 // make sure a sampler or an image is not involved as well...
Martin Radev4a9cd802016-09-01 16:51:51 +03002595 checkIsNotSampler(location, type.typeSpecifierNonArray,
2596 "samplers can't be function return values");
Martin Radev2cc85b32016-08-05 16:22:53 +03002597 checkIsNotImage(location, type.typeSpecifierNonArray, "images can't be function return values");
Olli Etuahoe29324f2016-06-15 10:58:03 +03002598 if (mShaderVersion < 300)
2599 {
2600 // Array return values are forbidden, but there's also no valid syntax for declaring array
2601 // return values in ESSL 1.00.
2602 ASSERT(type.arraySize == 0 || mDiagnostics.numErrors() > 0);
2603
2604 if (type.isStructureContainingArrays())
2605 {
2606 // ESSL 1.00.17 section 6.1 Function Definitions
2607 error(location, "structures containing arrays can't be function return values",
2608 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03002609 }
2610 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03002611
2612 // Add the function as a prototype after parsing it (we do not support recursion)
2613 return new TFunction(name, new TType(type));
2614}
2615
Jamie Madill06145232015-05-13 13:10:01 -04002616TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002617{
Jamie Madill06145232015-05-13 13:10:01 -04002618 TPublicType publicType = publicTypeIn;
Martin Radev4a9cd802016-09-01 16:51:51 +03002619 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02002620 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002621 error(publicType.getLine(), "constructor can't be a structure definition",
2622 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02002623 }
2624
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002625 TOperator op = EOpNull;
Martin Radev4a9cd802016-09-01 16:51:51 +03002626 if (publicType.getUserDef())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002627 {
2628 op = EOpConstructStruct;
2629 }
2630 else
2631 {
Geoff Lang156d7192016-07-21 16:11:00 -04002632 op = sh::TypeToConstructorOperator(TType(publicType));
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002633 if (op == EOpNull)
2634 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002635 error(publicType.getLine(), "cannot construct this type",
2636 getBasicString(publicType.getBasicType()));
2637 publicType.setBasicType(EbtFloat);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002638 op = EOpConstructFloat;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002639 }
2640 }
2641
2642 TString tempString;
Dmitry Skiba7f17a502015-06-22 15:08:39 -07002643 const TType *type = new TType(publicType);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002644 return new TFunction(&tempString, type, op);
2645}
2646
Jamie Madillb98c3a82015-07-23 14:26:04 -04002647// This function is used to test for the correctness of the parameters passed to various constructor
2648// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002649//
Olli Etuaho856c4972016-08-08 11:38:39 +03002650// Returns a node to add to the tree regardless of if an error was generated or not.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002651//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002652TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002653 TOperator op,
2654 TFunction *fnCall,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302655 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002656{
Olli Etuaho856c4972016-08-08 11:38:39 +03002657 TType type = fnCall->getReturnType();
2658 if (type.isUnsizedArray())
2659 {
Olli Etuahobbe9fb52016-11-03 17:16:05 +00002660 if (fnCall->getParamCount() == 0)
2661 {
2662 error(line, "implicitly sized array constructor must have at least one argument", "[]");
2663 type.setArraySize(1u);
2664 return TIntermTyped::CreateZero(type);
2665 }
Olli Etuaho856c4972016-08-08 11:38:39 +03002666 type.setArraySize(static_cast<unsigned int>(fnCall->getParamCount()));
2667 }
2668 bool constType = true;
2669 for (size_t i = 0; i < fnCall->getParamCount(); ++i)
2670 {
2671 const TConstParameter &param = fnCall->getParam(i);
2672 if (param.type->getQualifier() != EvqConst)
2673 constType = false;
2674 }
2675 if (constType)
2676 type.setQualifier(EvqConst);
2677
Olli Etuaho8a176262016-08-16 14:23:01 +03002678 if (!checkConstructorArguments(line, arguments, *fnCall, op, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03002679 {
2680 TIntermTyped *dummyNode = intermediate.setAggregateOperator(nullptr, op, line);
2681 dummyNode->setType(type);
2682 return dummyNode;
2683 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002684 TIntermAggregate *constructor = arguments->getAsAggregate();
2685 ASSERT(constructor != nullptr);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002686
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002687 // Turn the argument list itself into a constructor
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002688 constructor->setOp(op);
2689 constructor->setLine(line);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002690 ASSERT(constructor->isConstructor());
2691
2692 // Need to set type before setPrecisionFromChildren() because bool doesn't have precision.
Olli Etuaho856c4972016-08-08 11:38:39 +03002693 constructor->setType(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002694
Olli Etuaho21203702014-11-13 16:16:21 +02002695 // Structs should not be precision qualified, the individual members may be.
2696 // Built-in types on the other hand should be precision qualified.
2697 if (op != EOpConstructStruct)
2698 {
2699 constructor->setPrecisionFromChildren();
Olli Etuaho856c4972016-08-08 11:38:39 +03002700 type.setPrecision(constructor->getPrecision());
Olli Etuaho21203702014-11-13 16:16:21 +02002701 }
2702
Olli Etuaho856c4972016-08-08 11:38:39 +03002703 constructor->setType(type);
2704
Olli Etuahof119a262016-08-19 15:54:22 +03002705 TIntermTyped *constConstructor = intermediate.foldAggregateBuiltIn(constructor, &mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002706 if (constConstructor)
2707 {
2708 return constConstructor;
2709 }
2710
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002711 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002712}
2713
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002714//
2715// Interface/uniform blocks
2716//
Olli Etuaho13389b62016-10-16 11:48:18 +01002717TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03002718 const TTypeQualifierBuilder &typeQualifierBuilder,
2719 const TSourceLoc &nameLine,
2720 const TString &blockName,
2721 TFieldList *fieldList,
2722 const TString *instanceName,
2723 const TSourceLoc &instanceLine,
2724 TIntermTyped *arrayIndex,
2725 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002726{
Olli Etuaho856c4972016-08-08 11:38:39 +03002727 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002728
Olli Etuaho613b9592016-09-05 12:05:53 +03002729 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03002730
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002731 if (typeQualifier.qualifier != EvqUniform)
2732 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302733 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
2734 "interface blocks must be uniform");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002735 }
2736
Martin Radev70866b82016-07-22 15:27:42 +03002737 if (typeQualifier.invariant)
2738 {
2739 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
2740 }
2741
Martin Radev2cc85b32016-08-05 16:22:53 +03002742 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
2743
Jamie Madill099c0f32013-06-20 11:55:52 -04002744 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03002745 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002746
Jamie Madill099c0f32013-06-20 11:55:52 -04002747 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2748 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002749 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002750 }
2751
Jamie Madill1566ef72013-06-20 11:55:54 -04002752 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2753 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002754 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002755 }
2756
Olli Etuaho856c4972016-08-08 11:38:39 +03002757 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002758
Martin Radev2cc85b32016-08-05 16:22:53 +03002759 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
2760
Arun Patole7e7e68d2015-05-22 12:02:25 +05302761 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2762 if (!symbolTable.declare(blockNameSymbol))
2763 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002764 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002765 }
2766
Jamie Madill98493dd2013-07-08 14:39:03 -04002767 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302768 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2769 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002770 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302771 TType *fieldType = field->type();
2772 if (IsSampler(fieldType->getBasicType()))
2773 {
2774 error(field->line(), "unsupported type", fieldType->getBasicString(),
2775 "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002776 }
2777
Martin Radev2cc85b32016-08-05 16:22:53 +03002778 if (IsImage(fieldType->getBasicType()))
2779 {
2780 error(field->line(), "unsupported type", fieldType->getBasicString(),
2781 "image types are not allowed in interface blocks");
2782 }
2783
Jamie Madill98493dd2013-07-08 14:39:03 -04002784 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002785 switch (qualifier)
2786 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002787 case EvqGlobal:
2788 case EvqUniform:
2789 break;
2790 default:
2791 error(field->line(), "invalid qualifier on interface block member",
2792 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04002793 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002794 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002795
Martin Radev70866b82016-07-22 15:27:42 +03002796 if (fieldType->isInvariant())
2797 {
2798 error(field->line(), "invalid qualifier on interface block member", "invariant");
2799 }
2800
Jamie Madilla5efff92013-06-06 11:56:47 -04002801 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002802 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03002803 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
Jamie Madill099c0f32013-06-20 11:55:52 -04002804
Jamie Madill98493dd2013-07-08 14:39:03 -04002805 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002806 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002807 error(field->line(), "invalid layout qualifier:",
2808 getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04002809 }
2810
Jamie Madill98493dd2013-07-08 14:39:03 -04002811 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002812 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002813 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002814 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002815 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002816 {
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002817 warning(field->line(), "extraneous layout qualifier:",
Jamie Madillb98c3a82015-07-23 14:26:04 -04002818 getMatrixPackingString(fieldLayoutQualifier.matrixPacking),
2819 "only has an effect on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04002820 }
2821
Jamie Madill98493dd2013-07-08 14:39:03 -04002822 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002823 }
2824
Jamie Madill98493dd2013-07-08 14:39:03 -04002825 // add array index
Olli Etuaho856c4972016-08-08 11:38:39 +03002826 unsigned int arraySize = 0;
2827 if (arrayIndex != nullptr)
Jamie Madill98493dd2013-07-08 14:39:03 -04002828 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002829 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04002830 }
2831
Jamie Madillb98c3a82015-07-23 14:26:04 -04002832 TInterfaceBlock *interfaceBlock =
2833 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2834 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
2835 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002836
2837 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04002838 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002839
Jamie Madill98493dd2013-07-08 14:39:03 -04002840 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002841 {
2842 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002843 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2844 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002845 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302846 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002847
2848 // set parent pointer of the field variable
2849 fieldType->setInterfaceBlock(interfaceBlock);
2850
Arun Patole7e7e68d2015-05-22 12:02:25 +05302851 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002852 fieldVariable->setQualifier(typeQualifier.qualifier);
2853
Arun Patole7e7e68d2015-05-22 12:02:25 +05302854 if (!symbolTable.declare(fieldVariable))
2855 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002856 error(field->line(), "redefinition", field->name().c_str(),
2857 "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002858 }
2859 }
2860 }
2861 else
2862 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002863 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03002864
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002865 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302866 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002867 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002868
Arun Patole7e7e68d2015-05-22 12:02:25 +05302869 if (!symbolTable.declare(instanceTypeDef))
2870 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002871 error(instanceLine, "redefinition", instanceName->c_str(),
2872 "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002873 }
2874
Jamie Madillb98c3a82015-07-23 14:26:04 -04002875 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002876 symbolName = instanceTypeDef->getName();
2877 }
2878
Olli Etuaho13389b62016-10-16 11:48:18 +01002879 TIntermSymbol *blockSymbol =
2880 intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line);
2881 TIntermDeclaration *declaration = new TIntermDeclaration();
2882 declaration->appendDeclarator(blockSymbol);
2883 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04002884
2885 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01002886 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002887}
2888
Olli Etuaho383b7912016-08-05 11:22:59 +03002889void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002890{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002891 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002892
2893 // Embedded structure definitions are not supported per GLSL ES spec.
2894 // They aren't allowed in GLSL either, but we need to detect this here
2895 // so we don't rely on the GLSL compiler to catch it.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302896 if (mStructNestingLevel > 1)
2897 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002898 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002899 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002900}
2901
2902void TParseContext::exitStructDeclaration()
2903{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002904 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002905}
2906
Olli Etuaho8a176262016-08-16 14:23:01 +03002907void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002908{
Jamie Madillacb4b812016-11-07 13:50:29 -05002909 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05302910 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002911 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002912 }
2913
Arun Patole7e7e68d2015-05-22 12:02:25 +05302914 if (field.type()->getBasicType() != EbtStruct)
2915 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002916 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002917 }
2918
2919 // We're already inside a structure definition at this point, so add
2920 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302921 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2922 {
Jamie Madill41a49272014-03-18 16:10:13 -04002923 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002924 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
2925 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04002926 std::string reason = reasonStream.str();
2927 error(line, reason.c_str(), field.name().c_str(), "");
Olli Etuaho8a176262016-08-16 14:23:01 +03002928 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002929 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002930}
2931
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002932//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002933// Parse an array index expression
2934//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002935TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
2936 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302937 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002938{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002939 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2940 {
2941 if (baseExpression->getAsSymbolNode())
2942 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302943 error(location, " left of '[' is not of type array, matrix, or vector ",
2944 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002945 }
2946 else
2947 {
2948 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2949 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002950
2951 TConstantUnion *unionArray = new TConstantUnion[1];
2952 unionArray->setFConst(0.0f);
2953 return intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst),
2954 location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002955 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002956
Jamie Madill21c1e452014-12-29 11:33:41 -05002957 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2958
Olli Etuaho36b05142015-11-12 13:10:42 +02002959 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
2960 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
2961 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
2962 // index is a constant expression.
2963 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
2964 {
2965 if (baseExpression->isInterfaceBlock())
2966 {
2967 error(
2968 location, "", "[",
2969 "array indexes for interface blocks arrays must be constant integral expressions");
Olli Etuaho36b05142015-11-12 13:10:42 +02002970 }
2971 else if (baseExpression->getQualifier() == EvqFragmentOut)
2972 {
2973 error(location, "", "[",
2974 "array indexes for fragment outputs must be constant integral expressions");
Olli Etuaho36b05142015-11-12 13:10:42 +02002975 }
Olli Etuaho3e960462015-11-12 15:58:39 +02002976 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
2977 {
2978 error(location, "", "[", "array index for gl_FragData must be constant zero");
Olli Etuaho3e960462015-11-12 15:58:39 +02002979 }
Olli Etuaho36b05142015-11-12 13:10:42 +02002980 }
2981
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002982 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04002983 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002984 // If an out-of-range index is not qualified as constant, the behavior in the spec is
2985 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
2986 // constant fold expressions that are not constant expressions). The most compatible way to
2987 // handle this case is to report a warning instead of an error and force the index to be in
2988 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002989 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Jamie Madill21c1e452014-12-29 11:33:41 -05002990 int index = indexConstantUnion->getIConst(0);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002991
2992 int safeIndex = -1;
2993
2994 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04002995 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002996 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03002997 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002998 if (mShaderSpec == SH_WEBGL2_SPEC)
2999 {
3000 // Error has been already generated if index is not const.
3001 if (indexExpression->getQualifier() == EvqConst)
3002 {
3003 error(location, "", "[",
3004 "array index for gl_FragData must be constant zero");
3005 }
3006 safeIndex = 0;
3007 }
3008 else if (!isExtensionEnabled("GL_EXT_draw_buffers"))
3009 {
3010 outOfRangeError(outOfRangeIndexIsError, location, "", "[",
3011 "array index for gl_FragData must be zero when "
3012 "GL_EXT_draw_buffers is disabled");
3013 safeIndex = 0;
3014 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03003015 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003016 // Only do generic out-of-range check if similar error hasn't already been reported.
3017 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003018 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003019 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3020 baseExpression->getArraySize(),
3021 "array index out of range", "[]");
3022 }
3023 }
3024 else if (baseExpression->isMatrix())
3025 {
3026 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03003027 baseExpression->getType().getCols(),
3028 "matrix field selection out of range", "[]");
Jamie Madill7164cf42013-07-08 13:30:59 -04003029 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003030 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04003031 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003032 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3033 baseExpression->getType().getNominalSize(),
3034 "vector field selection out of range", "[]");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003035 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003036
3037 ASSERT(safeIndex >= 0);
3038 // Data of constant unions can't be changed, because it may be shared with other
3039 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
3040 // sanitized object.
3041 if (safeIndex != index)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003042 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003043 TConstantUnion *safeConstantUnion = new TConstantUnion();
3044 safeConstantUnion->setIConst(safeIndex);
3045 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003046 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003047
3048 return intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location,
3049 &mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003050 }
Jamie Madill7164cf42013-07-08 13:30:59 -04003051 else
3052 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003053 return intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location,
3054 &mDiagnostics);
Jamie Madill7164cf42013-07-08 13:30:59 -04003055 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003056}
3057
Olli Etuaho90892fb2016-07-14 14:44:51 +03003058int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
3059 const TSourceLoc &location,
3060 int index,
3061 int arraySize,
3062 const char *reason,
3063 const char *token)
3064{
3065 if (index >= arraySize || index < 0)
3066 {
3067 std::stringstream extraInfoStream;
3068 extraInfoStream << "'" << index << "'";
3069 std::string extraInfo = extraInfoStream.str();
3070 outOfRangeError(outOfRangeIndexIsError, location, reason, token, extraInfo.c_str());
3071 if (index < 0)
3072 {
3073 return 0;
3074 }
3075 else
3076 {
3077 return arraySize - 1;
3078 }
3079 }
3080 return index;
3081}
3082
Jamie Madillb98c3a82015-07-23 14:26:04 -04003083TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3084 const TSourceLoc &dotLocation,
3085 const TString &fieldString,
3086 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003087{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003088 if (baseExpression->isArray())
3089 {
3090 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003091 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003092 }
3093
3094 if (baseExpression->isVector())
3095 {
3096 TVectorFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003097 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields,
3098 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003099 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003100 fields.num = 1;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003101 fields.offsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003102 }
3103
Olli Etuahob6fa0432016-09-28 16:28:05 +01003104 return TIntermediate::AddSwizzle(baseExpression, fields, dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003105 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003106 else if (baseExpression->getBasicType() == EbtStruct)
3107 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303108 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003109 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003110 {
3111 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003112 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003113 }
3114 else
3115 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003116 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003117 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003118 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003119 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003120 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003121 {
3122 fieldFound = true;
3123 break;
3124 }
3125 }
3126 if (fieldFound)
3127 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003128 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3129 index->setLine(fieldLocation);
3130 return intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index,
3131 dotLocation, &mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003132 }
3133 else
3134 {
3135 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003136 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003137 }
3138 }
3139 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003140 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003141 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303142 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003143 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003144 {
3145 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003146 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003147 }
3148 else
3149 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003150 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003151 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003152 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003153 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003154 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003155 {
3156 fieldFound = true;
3157 break;
3158 }
3159 }
3160 if (fieldFound)
3161 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003162 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3163 index->setLine(fieldLocation);
3164 return intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index,
3165 dotLocation, &mDiagnostics);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003166 }
3167 else
3168 {
3169 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003170 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003171 }
3172 }
3173 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003174 else
3175 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003176 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003177 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03003178 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303179 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003180 }
3181 else
3182 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303183 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003184 " field selection requires structure, vector, or interface block on left hand "
3185 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303186 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003187 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003188 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003189 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003190}
3191
Jamie Madillb98c3a82015-07-23 14:26:04 -04003192TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3193 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003194{
Martin Radev802abe02016-08-04 17:48:32 +03003195 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003196
3197 if (qualifierType == "shared")
3198 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003199 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003200 {
3201 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
3202 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003203 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003204 }
3205 else if (qualifierType == "packed")
3206 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003207 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003208 {
3209 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
3210 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003211 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003212 }
3213 else if (qualifierType == "std140")
3214 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003215 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003216 }
3217 else if (qualifierType == "row_major")
3218 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003219 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003220 }
3221 else if (qualifierType == "column_major")
3222 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003223 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003224 }
3225 else if (qualifierType == "location")
3226 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003227 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
3228 "location requires an argument");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003229 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003230 else if (qualifierType == "rgba32f")
3231 {
3232 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3233 qualifier.imageInternalFormat = EiifRGBA32F;
3234 }
3235 else if (qualifierType == "rgba16f")
3236 {
3237 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3238 qualifier.imageInternalFormat = EiifRGBA16F;
3239 }
3240 else if (qualifierType == "r32f")
3241 {
3242 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3243 qualifier.imageInternalFormat = EiifR32F;
3244 }
3245 else if (qualifierType == "rgba8")
3246 {
3247 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3248 qualifier.imageInternalFormat = EiifRGBA8;
3249 }
3250 else if (qualifierType == "rgba8_snorm")
3251 {
3252 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3253 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
3254 }
3255 else if (qualifierType == "rgba32i")
3256 {
3257 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3258 qualifier.imageInternalFormat = EiifRGBA32I;
3259 }
3260 else if (qualifierType == "rgba16i")
3261 {
3262 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3263 qualifier.imageInternalFormat = EiifRGBA16I;
3264 }
3265 else if (qualifierType == "rgba8i")
3266 {
3267 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3268 qualifier.imageInternalFormat = EiifRGBA8I;
3269 }
3270 else if (qualifierType == "r32i")
3271 {
3272 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3273 qualifier.imageInternalFormat = EiifR32I;
3274 }
3275 else if (qualifierType == "rgba32ui")
3276 {
3277 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3278 qualifier.imageInternalFormat = EiifRGBA32UI;
3279 }
3280 else if (qualifierType == "rgba16ui")
3281 {
3282 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3283 qualifier.imageInternalFormat = EiifRGBA16UI;
3284 }
3285 else if (qualifierType == "rgba8ui")
3286 {
3287 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3288 qualifier.imageInternalFormat = EiifRGBA8UI;
3289 }
3290 else if (qualifierType == "r32ui")
3291 {
3292 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3293 qualifier.imageInternalFormat = EiifR32UI;
3294 }
3295
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003296 else
3297 {
3298 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003299 }
3300
Jamie Madilla5efff92013-06-06 11:56:47 -04003301 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003302}
3303
Martin Radev802abe02016-08-04 17:48:32 +03003304void TParseContext::parseLocalSize(const TString &qualifierType,
3305 const TSourceLoc &qualifierTypeLine,
3306 int intValue,
3307 const TSourceLoc &intValueLine,
3308 const std::string &intValueString,
3309 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03003310 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03003311{
Olli Etuaho856c4972016-08-08 11:38:39 +03003312 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03003313 if (intValue < 1)
3314 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03003315 std::string errorMessage = std::string(getWorkGroupSizeString(index)) + " must be positive";
Martin Radev802abe02016-08-04 17:48:32 +03003316 error(intValueLine, "out of range:", intValueString.c_str(), errorMessage.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003317 }
3318 (*localSize)[index] = intValue;
3319}
3320
Jamie Madillb98c3a82015-07-23 14:26:04 -04003321TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3322 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003323 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303324 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003325{
Martin Radev802abe02016-08-04 17:48:32 +03003326 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003327
Martin Radev802abe02016-08-04 17:48:32 +03003328 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003329
Martin Radev802abe02016-08-04 17:48:32 +03003330 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003331 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003332 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003333 if (intValue < 0)
3334 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003335 error(intValueLine, "out of range:", intValueString.c_str(),
3336 "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003337 }
3338 else
3339 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003340 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03003341 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003342 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003343 }
Martin Radev802abe02016-08-04 17:48:32 +03003344 else if (qualifierType == "local_size_x")
3345 {
3346 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
3347 &qualifier.localSize);
3348 }
3349 else if (qualifierType == "local_size_y")
3350 {
3351 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
3352 &qualifier.localSize);
3353 }
3354 else if (qualifierType == "local_size_z")
3355 {
3356 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
3357 &qualifier.localSize);
3358 }
3359 else
3360 {
3361 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003362 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003363
Jamie Madilla5efff92013-06-06 11:56:47 -04003364 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003365}
3366
Olli Etuaho613b9592016-09-05 12:05:53 +03003367TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
3368{
3369 return new TTypeQualifierBuilder(
3370 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
3371 mShaderVersion);
3372}
3373
Jamie Madillb98c3a82015-07-23 14:26:04 -04003374TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03003375 TLayoutQualifier rightQualifier,
3376 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003377{
Martin Radevc28888b2016-07-22 15:27:42 +03003378 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
3379 &mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003380}
3381
Martin Radev70866b82016-07-22 15:27:42 +03003382TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
3383 const TTypeQualifierBuilder &typeQualifierBuilder,
3384 TPublicType *typeSpecifier,
3385 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003386{
Olli Etuaho613b9592016-09-05 12:05:53 +03003387 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003388
Martin Radev70866b82016-07-22 15:27:42 +03003389 typeSpecifier->qualifier = typeQualifier.qualifier;
3390 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03003391 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03003392 typeSpecifier->invariant = typeQualifier.invariant;
3393 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05303394 {
Martin Radev70866b82016-07-22 15:27:42 +03003395 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003396 }
Martin Radev70866b82016-07-22 15:27:42 +03003397 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003398}
3399
Jamie Madillb98c3a82015-07-23 14:26:04 -04003400TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3401 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003402{
Martin Radev4a9cd802016-09-01 16:51:51 +03003403 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
3404 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03003405
Martin Radev4a9cd802016-09-01 16:51:51 +03003406 checkIsNonVoid(typeSpecifier.getLine(), (*fieldList)[0]->name(), typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003407
Martin Radev4a9cd802016-09-01 16:51:51 +03003408 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003409
Arun Patole7e7e68d2015-05-22 12:02:25 +05303410 for (unsigned int i = 0; i < fieldList->size(); ++i)
3411 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003412 //
3413 // Careful not to replace already known aspects of type, like array-ness
3414 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303415 TType *type = (*fieldList)[i]->type();
Martin Radev4a9cd802016-09-01 16:51:51 +03003416 type->setBasicType(typeSpecifier.getBasicType());
3417 type->setPrimarySize(typeSpecifier.getPrimarySize());
3418 type->setSecondarySize(typeSpecifier.getSecondarySize());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003419 type->setPrecision(typeSpecifier.precision);
3420 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003421 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
Martin Radev2cc85b32016-08-05 16:22:53 +03003422 type->setMemoryQualifier(typeSpecifier.memoryQualifier);
Martin Radev70866b82016-07-22 15:27:42 +03003423 type->setInvariant(typeSpecifier.invariant);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003424
3425 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303426 if (type->isArray())
3427 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003428 checkIsValidTypeForArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003429 }
3430 if (typeSpecifier.array)
Olli Etuaho856c4972016-08-08 11:38:39 +03003431 type->setArraySize(static_cast<unsigned int>(typeSpecifier.arraySize));
Martin Radev4a9cd802016-09-01 16:51:51 +03003432 if (typeSpecifier.getUserDef())
Arun Patole7e7e68d2015-05-22 12:02:25 +05303433 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003434 type->setStruct(typeSpecifier.getUserDef()->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003435 }
3436
Martin Radev4a9cd802016-09-01 16:51:51 +03003437 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*fieldList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003438 }
3439
Jamie Madill98493dd2013-07-08 14:39:03 -04003440 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003441}
3442
Martin Radev4a9cd802016-09-01 16:51:51 +03003443TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
3444 const TSourceLoc &nameLine,
3445 const TString *structName,
3446 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003447{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303448 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003449 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003450
Jamie Madill9b820842015-02-12 10:40:10 -05003451 // Store a bool in the struct if we're at global scope, to allow us to
3452 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05003453 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003454
Jamie Madill98493dd2013-07-08 14:39:03 -04003455 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003456 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003457 checkIsNotReserved(nameLine, *structName);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303458 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
3459 if (!symbolTable.declare(userTypeDef))
3460 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003461 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003462 }
3463 }
3464
3465 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04003466 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003467 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003468 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04003469 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003470 switch (qualifier)
3471 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003472 case EvqGlobal:
3473 case EvqTemporary:
3474 break;
3475 default:
3476 error(field.line(), "invalid qualifier on struct member",
3477 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003478 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003479 }
Martin Radev70866b82016-07-22 15:27:42 +03003480 if (field.type()->isInvariant())
3481 {
3482 error(field.line(), "invalid qualifier on struct member", "invariant");
3483 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003484 if (IsImage(field.type()->getBasicType()))
3485 {
3486 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
3487 }
3488
3489 checkIsMemoryQualifierNotSpecified(field.type()->getMemoryQualifier(), field.line());
Martin Radev70866b82016-07-22 15:27:42 +03003490
3491 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003492 }
3493
Martin Radev4a9cd802016-09-01 16:51:51 +03003494 TTypeSpecifierNonArray typeSpecifierNonArray;
3495 typeSpecifierNonArray.initialize(EbtStruct, structLine);
3496 typeSpecifierNonArray.userDef = structureType;
3497 typeSpecifierNonArray.isStructSpecifier = true;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003498 exitStructDeclaration();
3499
Martin Radev4a9cd802016-09-01 16:51:51 +03003500 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003501}
3502
Jamie Madillb98c3a82015-07-23 14:26:04 -04003503TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003504 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003505 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02003506{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003507 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003508 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02003509 init->isVector())
3510 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003511 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
3512 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003513 return nullptr;
3514 }
3515
Olli Etuahoac5274d2015-02-20 10:19:08 +02003516 if (statementList)
3517 {
3518 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
3519 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02003520 return nullptr;
3521 }
3522 }
3523
Olli Etuahoa3a36662015-02-17 13:46:51 +02003524 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3525 if (node == nullptr)
3526 {
3527 error(loc, "erroneous switch statement", "switch");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003528 return nullptr;
3529 }
3530 return node;
3531}
3532
3533TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3534{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003535 if (mSwitchNestingLevel == 0)
3536 {
3537 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003538 return nullptr;
3539 }
3540 if (condition == nullptr)
3541 {
3542 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003543 return nullptr;
3544 }
3545 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04003546 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02003547 {
3548 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003549 }
3550 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003551 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
3552 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
3553 // fold in case labels.
3554 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02003555 {
3556 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003557 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003558 TIntermCase *node = intermediate.addCase(condition, loc);
3559 if (node == nullptr)
3560 {
3561 error(loc, "erroneous case statement", "case");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003562 return nullptr;
3563 }
3564 return node;
3565}
3566
3567TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3568{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003569 if (mSwitchNestingLevel == 0)
3570 {
3571 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003572 return nullptr;
3573 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003574 TIntermCase *node = intermediate.addCase(nullptr, loc);
3575 if (node == nullptr)
3576 {
3577 error(loc, "erroneous default statement", "default");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003578 return nullptr;
3579 }
3580 return node;
3581}
3582
Jamie Madillb98c3a82015-07-23 14:26:04 -04003583TIntermTyped *TParseContext::createUnaryMath(TOperator op,
3584 TIntermTyped *child,
3585 const TSourceLoc &loc,
3586 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003587{
3588 if (child == nullptr)
3589 {
3590 return nullptr;
3591 }
3592
3593 switch (op)
3594 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003595 case EOpLogicalNot:
3596 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
3597 child->isVector())
3598 {
3599 return nullptr;
3600 }
3601 break;
3602 case EOpBitwiseNot:
3603 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3604 child->isMatrix() || child->isArray())
3605 {
3606 return nullptr;
3607 }
3608 break;
3609 case EOpPostIncrement:
3610 case EOpPreIncrement:
3611 case EOpPostDecrement:
3612 case EOpPreDecrement:
3613 case EOpNegative:
3614 case EOpPositive:
3615 if (child->getBasicType() == EbtStruct || child->getBasicType() == EbtBool ||
Martin Radev2cc85b32016-08-05 16:22:53 +03003616 child->isArray() || IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04003617 {
3618 return nullptr;
3619 }
3620 // Operators for built-ins are already type checked against their prototype.
3621 default:
3622 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003623 }
3624
Olli Etuahof119a262016-08-19 15:54:22 +03003625 TIntermUnary *node = new TIntermUnary(op, child);
3626 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03003627
3628 TIntermTyped *foldedNode = node->fold(&mDiagnostics);
3629 if (foldedNode)
3630 return foldedNode;
3631
3632 return node;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003633}
3634
Olli Etuaho09b22472015-02-11 11:47:26 +02003635TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3636{
Olli Etuahof6c694b2015-03-26 14:50:53 +02003637 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003638 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003639 {
3640 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003641 return child;
3642 }
3643 return node;
3644}
3645
Jamie Madillb98c3a82015-07-23 14:26:04 -04003646TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
3647 TIntermTyped *child,
3648 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003649{
Olli Etuaho856c4972016-08-08 11:38:39 +03003650 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02003651 return addUnaryMath(op, child, loc);
3652}
3653
Jamie Madillb98c3a82015-07-23 14:26:04 -04003654bool TParseContext::binaryOpCommonCheck(TOperator op,
3655 TIntermTyped *left,
3656 TIntermTyped *right,
3657 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003658{
Olli Etuaho244be012016-08-18 15:26:02 +03003659 if (left->getType().getStruct() || right->getType().getStruct())
3660 {
3661 switch (op)
3662 {
3663 case EOpIndexDirectStruct:
3664 ASSERT(left->getType().getStruct());
3665 break;
3666 case EOpEqual:
3667 case EOpNotEqual:
3668 case EOpAssign:
3669 case EOpInitialize:
3670 if (left->getType() != right->getType())
3671 {
3672 return false;
3673 }
3674 break;
3675 default:
3676 error(loc, "Invalid operation for structs", GetOperatorString(op));
3677 return false;
3678 }
3679 }
3680
Olli Etuahod6b14282015-03-17 14:31:35 +02003681 if (left->isArray() || right->isArray())
3682 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003683 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003684 {
3685 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3686 return false;
3687 }
3688
3689 if (left->isArray() != right->isArray())
3690 {
3691 error(loc, "array / non-array mismatch", GetOperatorString(op));
3692 return false;
3693 }
3694
3695 switch (op)
3696 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003697 case EOpEqual:
3698 case EOpNotEqual:
3699 case EOpAssign:
3700 case EOpInitialize:
3701 break;
3702 default:
3703 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3704 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02003705 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003706 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003707 if (left->getArraySize() != right->getArraySize())
3708 {
3709 error(loc, "array size mismatch", GetOperatorString(op));
3710 return false;
3711 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003712 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003713
3714 // Check ops which require integer / ivec parameters
3715 bool isBitShift = false;
3716 switch (op)
3717 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003718 case EOpBitShiftLeft:
3719 case EOpBitShiftRight:
3720 case EOpBitShiftLeftAssign:
3721 case EOpBitShiftRightAssign:
3722 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3723 // check that the basic type is an integer type.
3724 isBitShift = true;
3725 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3726 {
3727 return false;
3728 }
3729 break;
3730 case EOpBitwiseAnd:
3731 case EOpBitwiseXor:
3732 case EOpBitwiseOr:
3733 case EOpBitwiseAndAssign:
3734 case EOpBitwiseXorAssign:
3735 case EOpBitwiseOrAssign:
3736 // It is enough to check the type of only one operand, since later it
3737 // is checked that the operand types match.
3738 if (!IsInteger(left->getBasicType()))
3739 {
3740 return false;
3741 }
3742 break;
3743 default:
3744 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003745 }
3746
3747 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3748 // So the basic type should usually match.
3749 if (!isBitShift && left->getBasicType() != right->getBasicType())
3750 {
3751 return false;
3752 }
3753
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003754 // Check that:
3755 // 1. Type sizes match exactly on ops that require that.
3756 // 2. Restrictions for structs that contain arrays or samplers are respected.
3757 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003758 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003759 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003760 case EOpAssign:
3761 case EOpInitialize:
3762 case EOpEqual:
3763 case EOpNotEqual:
3764 // ESSL 1.00 sections 5.7, 5.8, 5.9
3765 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
3766 {
3767 error(loc, "undefined operation for structs containing arrays",
3768 GetOperatorString(op));
3769 return false;
3770 }
3771 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3772 // we interpret the spec so that this extends to structs containing samplers,
3773 // similarly to ESSL 1.00 spec.
3774 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
3775 left->getType().isStructureContainingSamplers())
3776 {
3777 error(loc, "undefined operation for structs containing samplers",
3778 GetOperatorString(op));
3779 return false;
3780 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003781
3782 if ((op == EOpAssign || op == EOpInitialize) &&
3783 left->getType().isStructureContainingImages())
3784 {
3785 error(loc, "undefined operation for structs containing images",
3786 GetOperatorString(op));
3787 return false;
3788 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003789 case EOpLessThan:
3790 case EOpGreaterThan:
3791 case EOpLessThanEqual:
3792 case EOpGreaterThanEqual:
3793 if ((left->getNominalSize() != right->getNominalSize()) ||
3794 (left->getSecondarySize() != right->getSecondarySize()))
3795 {
3796 return false;
3797 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003798 break;
3799 case EOpAdd:
3800 case EOpSub:
3801 case EOpDiv:
3802 case EOpIMod:
3803 case EOpBitShiftLeft:
3804 case EOpBitShiftRight:
3805 case EOpBitwiseAnd:
3806 case EOpBitwiseXor:
3807 case EOpBitwiseOr:
3808 case EOpAddAssign:
3809 case EOpSubAssign:
3810 case EOpDivAssign:
3811 case EOpIModAssign:
3812 case EOpBitShiftLeftAssign:
3813 case EOpBitShiftRightAssign:
3814 case EOpBitwiseAndAssign:
3815 case EOpBitwiseXorAssign:
3816 case EOpBitwiseOrAssign:
3817 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
3818 {
3819 return false;
3820 }
3821
3822 // Are the sizes compatible?
3823 if (left->getNominalSize() != right->getNominalSize() ||
3824 left->getSecondarySize() != right->getSecondarySize())
3825 {
3826 // If the nominal sizes of operands do not match:
3827 // One of them must be a scalar.
3828 if (!left->isScalar() && !right->isScalar())
3829 return false;
3830
3831 // In the case of compound assignment other than multiply-assign,
3832 // the right side needs to be a scalar. Otherwise a vector/matrix
3833 // would be assigned to a scalar. A scalar can't be shifted by a
3834 // vector either.
3835 if (!right->isScalar() &&
3836 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
3837 return false;
3838 }
3839 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003840 default:
3841 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003842 }
3843
Olli Etuahod6b14282015-03-17 14:31:35 +02003844 return true;
3845}
3846
Olli Etuaho1dded802016-08-18 18:13:13 +03003847bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
3848 const TType &left,
3849 const TType &right)
3850{
3851 switch (op)
3852 {
3853 case EOpMul:
3854 case EOpMulAssign:
3855 return left.getNominalSize() == right.getNominalSize() &&
3856 left.getSecondarySize() == right.getSecondarySize();
3857 case EOpVectorTimesScalar:
3858 return true;
3859 case EOpVectorTimesScalarAssign:
3860 ASSERT(!left.isMatrix() && !right.isMatrix());
3861 return left.isVector() && !right.isVector();
3862 case EOpVectorTimesMatrix:
3863 return left.getNominalSize() == right.getRows();
3864 case EOpVectorTimesMatrixAssign:
3865 ASSERT(!left.isMatrix() && right.isMatrix());
3866 return left.isVector() && left.getNominalSize() == right.getRows() &&
3867 left.getNominalSize() == right.getCols();
3868 case EOpMatrixTimesVector:
3869 return left.getCols() == right.getNominalSize();
3870 case EOpMatrixTimesScalar:
3871 return true;
3872 case EOpMatrixTimesScalarAssign:
3873 ASSERT(left.isMatrix() && !right.isMatrix());
3874 return !right.isVector();
3875 case EOpMatrixTimesMatrix:
3876 return left.getCols() == right.getRows();
3877 case EOpMatrixTimesMatrixAssign:
3878 ASSERT(left.isMatrix() && right.isMatrix());
3879 // We need to check two things:
3880 // 1. The matrix multiplication step is valid.
3881 // 2. The result will have the same number of columns as the lvalue.
3882 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
3883
3884 default:
3885 UNREACHABLE();
3886 return false;
3887 }
3888}
3889
Jamie Madillb98c3a82015-07-23 14:26:04 -04003890TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
3891 TIntermTyped *left,
3892 TIntermTyped *right,
3893 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02003894{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003895 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003896 return nullptr;
3897
Olli Etuahofc1806e2015-03-17 13:03:11 +02003898 switch (op)
3899 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003900 case EOpEqual:
3901 case EOpNotEqual:
3902 break;
3903 case EOpLessThan:
3904 case EOpGreaterThan:
3905 case EOpLessThanEqual:
3906 case EOpGreaterThanEqual:
Olli Etuaho244be012016-08-18 15:26:02 +03003907 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3908 !right->getType().getStruct());
3909 if (left->isMatrix() || left->isVector())
Jamie Madillb98c3a82015-07-23 14:26:04 -04003910 {
3911 return nullptr;
3912 }
3913 break;
3914 case EOpLogicalOr:
3915 case EOpLogicalXor:
3916 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03003917 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3918 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00003919 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04003920 {
3921 return nullptr;
3922 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00003923 // Basic types matching should have been already checked.
3924 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003925 break;
3926 case EOpAdd:
3927 case EOpSub:
3928 case EOpDiv:
3929 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03003930 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3931 !right->getType().getStruct());
3932 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04003933 {
3934 return nullptr;
3935 }
3936 break;
3937 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03003938 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3939 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003940 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03003941 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04003942 {
3943 return nullptr;
3944 }
3945 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003946 default:
3947 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003948 }
3949
Olli Etuaho1dded802016-08-18 18:13:13 +03003950 if (op == EOpMul)
3951 {
3952 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
3953 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
3954 {
3955 return nullptr;
3956 }
3957 }
3958
Olli Etuaho3fdec912016-08-18 15:08:06 +03003959 TIntermBinary *node = new TIntermBinary(op, left, right);
3960 node->setLine(loc);
3961
Olli Etuaho3fdec912016-08-18 15:08:06 +03003962 // See if we can fold constants.
3963 TIntermTyped *foldedNode = node->fold(&mDiagnostics);
3964 if (foldedNode)
3965 return foldedNode;
3966
3967 return node;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003968}
3969
Jamie Madillb98c3a82015-07-23 14:26:04 -04003970TIntermTyped *TParseContext::addBinaryMath(TOperator op,
3971 TIntermTyped *left,
3972 TIntermTyped *right,
3973 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003974{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003975 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003976 if (node == 0)
3977 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003978 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3979 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003980 return left;
3981 }
3982 return node;
3983}
3984
Jamie Madillb98c3a82015-07-23 14:26:04 -04003985TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
3986 TIntermTyped *left,
3987 TIntermTyped *right,
3988 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003989{
Olli Etuahofc1806e2015-03-17 13:03:11 +02003990 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003991 if (node == 0)
3992 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003993 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
3994 right->getCompleteString());
Jamie Madill6ba6ead2015-05-04 14:21:21 -04003995 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02003996 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003997 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
3998 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02003999 }
4000 return node;
4001}
4002
Olli Etuaho13389b62016-10-16 11:48:18 +01004003TIntermBinary *TParseContext::createAssign(TOperator op,
4004 TIntermTyped *left,
4005 TIntermTyped *right,
4006 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004007{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004008 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02004009 {
Olli Etuaho1dded802016-08-18 18:13:13 +03004010 if (op == EOpMulAssign)
4011 {
4012 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
4013 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4014 {
4015 return nullptr;
4016 }
4017 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03004018 TIntermBinary *node = new TIntermBinary(op, left, right);
4019 node->setLine(loc);
4020
Olli Etuaho3fdec912016-08-18 15:08:06 +03004021 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02004022 }
4023 return nullptr;
4024}
4025
Jamie Madillb98c3a82015-07-23 14:26:04 -04004026TIntermTyped *TParseContext::addAssign(TOperator op,
4027 TIntermTyped *left,
4028 TIntermTyped *right,
4029 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004030{
4031 TIntermTyped *node = createAssign(op, left, right, loc);
4032 if (node == nullptr)
4033 {
4034 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02004035 return left;
4036 }
4037 return node;
4038}
4039
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004040TIntermTyped *TParseContext::addComma(TIntermTyped *left,
4041 TIntermTyped *right,
4042 const TSourceLoc &loc)
4043{
Corentin Wallez0d959252016-07-12 17:26:32 -04004044 // WebGL2 section 5.26, the following results in an error:
4045 // "Sequence operator applied to void, arrays, or structs containing arrays"
4046 if (mShaderSpec == SH_WEBGL2_SPEC && (left->isArray() || left->getBasicType() == EbtVoid ||
4047 left->getType().isStructureContainingArrays() ||
4048 right->isArray() || right->getBasicType() == EbtVoid ||
4049 right->getType().isStructureContainingArrays()))
4050 {
4051 error(loc,
4052 "sequence operator is not allowed for void, arrays, or structs containing arrays",
4053 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04004054 }
4055
Olli Etuaho4db7ded2016-10-13 12:23:11 +01004056 return TIntermediate::AddComma(left, right, loc, mShaderVersion);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004057}
4058
Olli Etuaho49300862015-02-20 14:54:49 +02004059TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
4060{
4061 switch (op)
4062 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004063 case EOpContinue:
4064 if (mLoopNestingLevel <= 0)
4065 {
4066 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004067 }
4068 break;
4069 case EOpBreak:
4070 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
4071 {
4072 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004073 }
4074 break;
4075 case EOpReturn:
4076 if (mCurrentFunctionType->getBasicType() != EbtVoid)
4077 {
4078 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004079 }
4080 break;
4081 default:
4082 // No checks for discard
4083 break;
Olli Etuaho49300862015-02-20 14:54:49 +02004084 }
4085 return intermediate.addBranch(op, loc);
4086}
4087
Jamie Madillb98c3a82015-07-23 14:26:04 -04004088TIntermBranch *TParseContext::addBranch(TOperator op,
4089 TIntermTyped *returnValue,
4090 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02004091{
4092 ASSERT(op == EOpReturn);
4093 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004094 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02004095 {
4096 error(loc, "void function cannot return a value", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02004097 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004098 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02004099 {
4100 error(loc, "function return is not matching type:", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02004101 }
4102 return intermediate.addBranch(op, returnValue, loc);
4103}
4104
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004105void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
4106{
4107 ASSERT(!functionCall->isUserDefined());
Olli Etuahobd674552016-10-06 13:28:42 +01004108 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004109 TIntermNode *offset = nullptr;
4110 TIntermSequence *arguments = functionCall->getSequence();
4111 if (name.compare(0, 16, "texelFetchOffset") == 0 ||
4112 name.compare(0, 16, "textureLodOffset") == 0 ||
4113 name.compare(0, 20, "textureProjLodOffset") == 0 ||
4114 name.compare(0, 17, "textureGradOffset") == 0 ||
4115 name.compare(0, 21, "textureProjGradOffset") == 0)
4116 {
4117 offset = arguments->back();
4118 }
4119 else if (name.compare(0, 13, "textureOffset") == 0 ||
4120 name.compare(0, 17, "textureProjOffset") == 0)
4121 {
4122 // A bias parameter might follow the offset parameter.
4123 ASSERT(arguments->size() >= 3);
4124 offset = (*arguments)[2];
4125 }
4126 if (offset != nullptr)
4127 {
4128 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
4129 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
4130 {
4131 TString unmangledName = TFunction::unmangleName(name);
4132 error(functionCall->getLine(), "Texture offset must be a constant expression",
4133 unmangledName.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004134 }
4135 else
4136 {
4137 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
4138 size_t size = offsetConstantUnion->getType().getObjectSize();
4139 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
4140 for (size_t i = 0u; i < size; ++i)
4141 {
4142 int offsetValue = values[i].getIConst();
4143 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
4144 {
4145 std::stringstream tokenStream;
4146 tokenStream << offsetValue;
4147 std::string token = tokenStream.str();
4148 error(offset->getLine(), "Texture offset value out of valid range",
4149 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004150 }
4151 }
4152 }
4153 }
4154}
4155
Martin Radev2cc85b32016-08-05 16:22:53 +03004156// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
4157void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
4158{
4159 ASSERT(!functionCall->isUserDefined());
4160 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
4161
4162 if (name.compare(0, 5, "image") == 0)
4163 {
4164 TIntermSequence *arguments = functionCall->getSequence();
4165 TIntermNode *imageNode = (*arguments)[0];
4166 TIntermSymbol *imageSymbol = imageNode->getAsSymbolNode();
4167
4168 const TMemoryQualifier &memoryQualifier = imageSymbol->getMemoryQualifier();
4169
4170 if (name.compare(5, 5, "Store") == 0)
4171 {
4172 if (memoryQualifier.readonly)
4173 {
4174 error(imageNode->getLine(),
4175 "'imageStore' cannot be used with images qualified as 'readonly'",
4176 imageSymbol->getSymbol().c_str());
4177 }
4178 }
4179 else if (name.compare(5, 4, "Load") == 0)
4180 {
4181 if (memoryQualifier.writeonly)
4182 {
4183 error(imageNode->getLine(),
4184 "'imageLoad' cannot be used with images qualified as 'writeonly'",
4185 imageSymbol->getSymbol().c_str());
4186 }
4187 }
4188 }
4189}
4190
4191// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
4192void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
4193 const TFunction *functionDefinition,
4194 const TIntermAggregate *functionCall)
4195{
4196 ASSERT(functionCall->isUserDefined());
4197
4198 const TIntermSequence &arguments = *functionCall->getSequence();
4199
4200 ASSERT(functionDefinition->getParamCount() == arguments.size());
4201
4202 for (size_t i = 0; i < arguments.size(); ++i)
4203 {
4204 const TType &functionArgumentType = arguments[i]->getAsTyped()->getType();
4205 const TType &functionParameterType = *functionDefinition->getParam(i).type;
4206 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
4207
4208 if (IsImage(functionArgumentType.getBasicType()))
4209 {
4210 const TMemoryQualifier &functionArgumentMemoryQualifier =
4211 functionArgumentType.getMemoryQualifier();
4212 const TMemoryQualifier &functionParameterMemoryQualifier =
4213 functionParameterType.getMemoryQualifier();
4214 if (functionArgumentMemoryQualifier.readonly &&
4215 !functionParameterMemoryQualifier.readonly)
4216 {
4217 error(functionCall->getLine(),
4218 "Function call discards the 'readonly' qualifier from image",
4219 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4220 }
4221
4222 if (functionArgumentMemoryQualifier.writeonly &&
4223 !functionParameterMemoryQualifier.writeonly)
4224 {
4225 error(functionCall->getLine(),
4226 "Function call discards the 'writeonly' qualifier from image",
4227 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4228 }
Martin Radev049edfa2016-11-11 14:35:37 +02004229
4230 if (functionArgumentMemoryQualifier.coherent &&
4231 !functionParameterMemoryQualifier.coherent)
4232 {
4233 error(functionCall->getLine(),
4234 "Function call discards the 'coherent' qualifier from image",
4235 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4236 }
4237
4238 if (functionArgumentMemoryQualifier.volatileQualifier &&
4239 !functionParameterMemoryQualifier.volatileQualifier)
4240 {
4241 error(functionCall->getLine(),
4242 "Function call discards the 'volatile' qualifier from image",
4243 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4244 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004245 }
4246 }
4247}
4248
Jamie Madillb98c3a82015-07-23 14:26:04 -04004249TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
4250 TIntermNode *paramNode,
4251 TIntermNode *thisNode,
4252 const TSourceLoc &loc,
4253 bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004254{
Jamie Madillb98c3a82015-07-23 14:26:04 -04004255 *fatalError = false;
4256 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004257 TIntermTyped *callNode = nullptr;
4258
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004259 if (thisNode != nullptr)
4260 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004261 TConstantUnion *unionArray = new TConstantUnion[1];
Jamie Madillb98c3a82015-07-23 14:26:04 -04004262 int arraySize = 0;
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004263 TIntermTyped *typedThis = thisNode->getAsTyped();
4264 if (fnCall->getName() != "length")
4265 {
4266 error(loc, "invalid method", fnCall->getName().c_str());
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004267 }
4268 else if (paramNode != nullptr)
4269 {
4270 error(loc, "method takes no parameters", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004271 }
4272 else if (typedThis == nullptr || !typedThis->isArray())
4273 {
4274 error(loc, "length can only be called on arrays", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004275 }
4276 else
4277 {
Olli Etuaho96e67382015-04-23 14:27:02 +03004278 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03004279 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004280 {
Olli Etuaho39282e12015-04-23 15:41:48 +03004281 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004282 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03004283 // (func()).length()
4284 // (int[3](0, 1, 2)).length()
Jamie Madillb98c3a82015-07-23 14:26:04 -04004285 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
4286 // expression.
4287 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
4288 // spec section 5.9 which allows "An array, vector or matrix expression with the
4289 // length method applied".
4290 error(loc, "length can only be called on array names, not on array expressions",
4291 "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004292 }
4293 }
Olli Etuaho96e67382015-04-23 14:27:02 +03004294 unionArray->setIConst(arraySize);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004295 callNode =
4296 intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004297 }
4298 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004299 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004300 // Then this should be a constructor.
Olli Etuaho856c4972016-08-08 11:38:39 +03004301 callNode = addConstructor(paramNode, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004302 }
4303 else
4304 {
4305 //
4306 // Not a constructor. Find it in the symbol table.
4307 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05304308 const TFunction *fnCandidate;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004309 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004310 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004311 if (fnCandidate)
4312 {
4313 //
4314 // A declared function.
4315 //
Olli Etuaho383b7912016-08-05 11:22:59 +03004316 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004317 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004318 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004319 }
4320 op = fnCandidate->getBuiltInOp();
4321 if (builtIn && op != EOpNull)
4322 {
4323 //
4324 // A function call mapped to a built-in operation.
4325 //
4326 if (fnCandidate->getParamCount() == 1)
4327 {
4328 //
4329 // Treat it like a built-in unary operator.
4330 //
Olli Etuaho15c2ac32015-11-09 15:51:43 +02004331 TIntermAggregate *paramAgg = paramNode->getAsAggregate();
4332 paramNode = paramAgg->getSequence()->front();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004333 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc,
4334 &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004335 if (callNode == nullptr)
4336 {
4337 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004338 extraInfoStream
4339 << "built in unary operator function. Type: "
4340 << static_cast<TIntermTyped *>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004341 std::string extraInfo = extraInfoStream.str();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004342 error(paramNode->getLine(), " wrong operand type", "Internal Error",
4343 extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004344 *fatalError = true;
4345 return nullptr;
4346 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004347 }
4348 else
4349 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004350 TIntermAggregate *aggregate =
4351 intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004352 aggregate->setType(fnCandidate->getReturnType());
4353 aggregate->setPrecisionFromChildren();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02004354 if (aggregate->areChildrenConstQualified())
4355 {
4356 aggregate->getTypePointer()->setQualifier(EvqConst);
4357 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004358
4359 // Some built-in functions have out parameters too.
4360 functionCallLValueErrorCheck(fnCandidate, aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05304361
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004362 // See if we can constant fold a built-in. Note that this may be possible even
4363 // if it is not const-qualified.
Olli Etuahof119a262016-08-19 15:54:22 +03004364 TIntermTyped *foldedNode =
4365 intermediate.foldAggregateBuiltIn(aggregate, &mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05304366 if (foldedNode)
4367 {
Arun Patole274f0702015-05-05 13:33:30 +05304368 callNode = foldedNode;
4369 }
Olli Etuahob43846e2015-06-02 18:18:57 +03004370 else
4371 {
4372 callNode = aggregate;
4373 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004374 }
4375 }
4376 else
4377 {
4378 // This is a real function call
Jamie Madillb98c3a82015-07-23 14:26:04 -04004379 TIntermAggregate *aggregate =
4380 intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004381 aggregate->setType(fnCandidate->getReturnType());
4382
Jamie Madillb98c3a82015-07-23 14:26:04 -04004383 // this is how we know whether the given function is a builtIn function or a user
4384 // defined function
4385 // if builtIn == false, it's a userDefined -> could be an overloaded
4386 // builtIn function also
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004387 // if builtIn == true, it's definitely a builtIn function with EOpNull
4388 if (!builtIn)
4389 aggregate->setUserDefined();
Olli Etuahobd674552016-10-06 13:28:42 +01004390 aggregate->getFunctionSymbolInfo()->setFromFunction(*fnCandidate);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004391
Olli Etuahobd674552016-10-06 13:28:42 +01004392 // This needs to happen after the function info including name is set
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004393 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004394 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004395 aggregate->setBuiltInFunctionPrecision();
4396
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004397 checkTextureOffsetConst(aggregate);
Martin Radev2cc85b32016-08-05 16:22:53 +03004398
4399 checkImageMemoryAccessForBuiltinFunctions(aggregate);
4400 }
4401 else
4402 {
4403 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, aggregate);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004404 }
4405
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004406 callNode = aggregate;
4407
4408 functionCallLValueErrorCheck(fnCandidate, aggregate);
4409 }
4410 }
4411 else
4412 {
4413 // error message was put out by findFunction()
4414 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004415 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004416 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004417 callNode = intermediate.addConstantUnion(unionArray,
4418 TType(EbtFloat, EbpUndefined, EvqConst), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004419 }
4420 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004421 return callNode;
4422}
4423
Jamie Madillb98c3a82015-07-23 14:26:04 -04004424TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004425 TIntermTyped *trueExpression,
4426 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03004427 const TSourceLoc &loc)
4428{
Olli Etuaho856c4972016-08-08 11:38:39 +03004429 checkIsScalarBool(loc, cond);
Olli Etuaho52901742015-04-15 13:42:45 +03004430
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004431 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03004432 {
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004433 binaryOpError(loc, ":", trueExpression->getCompleteString(),
4434 falseExpression->getCompleteString());
4435 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03004436 }
Olli Etuahode318b22016-10-25 16:18:25 +01004437 if (IsOpaqueType(trueExpression->getBasicType()))
4438 {
4439 // ESSL 1.00 section 4.1.7
4440 // ESSL 3.00 section 4.1.7
4441 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
4442 // Note that structs containing opaque types don't need to be checked as structs are
4443 // forbidden below.
4444 error(loc, "ternary operator is not allowed for opaque types", ":");
4445 return falseExpression;
4446 }
4447
Olli Etuahoa2d53032015-04-15 14:14:44 +03004448 // ESSL1 sections 5.2 and 5.7:
4449 // ESSL3 section 5.7:
4450 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004451 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03004452 {
4453 error(loc, "ternary operator is not allowed for structures or arrays", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004454 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03004455 }
Corentin Wallez0d959252016-07-12 17:26:32 -04004456 // WebGL2 section 5.26, the following results in an error:
4457 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004458 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04004459 {
4460 error(loc, "ternary operator is not allowed for void", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004461 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04004462 }
4463
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004464 return TIntermediate::AddTernarySelection(cond, trueExpression, falseExpression, loc);
Olli Etuaho52901742015-04-15 13:42:45 +03004465}
Olli Etuaho49300862015-02-20 14:54:49 +02004466
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004467//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004468// Parse an array of strings using yyparse.
4469//
4470// Returns 0 for success.
4471//
Jamie Madillb98c3a82015-07-23 14:26:04 -04004472int PaParseStrings(size_t count,
4473 const char *const string[],
4474 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05304475 TParseContext *context)
4476{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004477 if ((count == 0) || (string == NULL))
4478 return 1;
4479
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004480 if (glslang_initialize(context))
4481 return 1;
4482
alokp@chromium.org408c45e2012-04-05 15:54:43 +00004483 int error = glslang_scan(count, string, length, context);
4484 if (!error)
4485 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004486
alokp@chromium.org73bc2982012-06-19 18:48:05 +00004487 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00004488
alokp@chromium.org6b495712012-06-29 00:06:58 +00004489 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004490}
Jamie Madill45bcc782016-11-07 13:58:48 -05004491
4492} // namespace sh