blob: d9618df84b9def47e32c7997d068c8462968bd50 [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),
Olli Etuahof1cf5e62016-11-22 17:36:49 +0000102 mPreprocessor(&mDiagnostics, &mDirectiveHandler, pp::PreprocessorSettings()),
Jamie Madillacb4b812016-11-07 13:50:29 -0500103 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// Used by flex/bison to output all syntax and parsing errors.
227//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530228void TParseContext::error(const TSourceLoc &loc,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400229 const char *reason,
230 const char *token,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530231 const char *extraInfo)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000232{
Olli Etuaho1cc598f2016-08-18 13:50:30 +0300233 mDiagnostics.error(loc, reason, token, extraInfo);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000234}
235
Arun Patole7e7e68d2015-05-22 12:02:25 +0530236void TParseContext::warning(const TSourceLoc &loc,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400237 const char *reason,
238 const char *token,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530239 const char *extraInfo)
240{
Olli Etuaho1cc598f2016-08-18 13:50:30 +0300241 mDiagnostics.warning(loc, reason, token, extraInfo);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000242}
243
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200244void TParseContext::outOfRangeError(bool isError,
245 const TSourceLoc &loc,
246 const char *reason,
247 const char *token,
248 const char *extraInfo)
249{
250 if (isError)
251 {
252 error(loc, reason, token, extraInfo);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200253 }
254 else
255 {
256 warning(loc, reason, token, extraInfo);
257 }
258}
259
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000260//
261// Same error message for all places assignments don't work.
262//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530263void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000264{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000265 std::stringstream extraInfoStream;
266 extraInfoStream << "cannot convert from '" << right << "' to '" << left << "'";
267 std::string extraInfo = extraInfoStream.str();
268 error(line, "", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000269}
270
271//
272// Same error message for all places unary operations don't work.
273//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530274void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000275{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000276 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400277 extraInfoStream << "no operation '" << op << "' exists that takes an operand of type "
278 << operand << " (or there is no acceptable conversion)";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000279 std::string extraInfo = extraInfoStream.str();
280 error(line, " wrong operand type", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000281}
282
283//
284// Same error message for all binary operations don't work.
285//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400286void TParseContext::binaryOpError(const TSourceLoc &line,
287 const char *op,
288 TString left,
289 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000290{
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000291 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400292 extraInfoStream << "no operation '" << op << "' exists that takes a left-hand operand of type '"
293 << left << "' and a right operand of type '" << right
294 << "' (or there is no acceptable conversion)";
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000295 std::string extraInfo = extraInfoStream.str();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530296 error(line, " wrong operand types ", op, extraInfo.c_str());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000297}
298
Olli Etuaho856c4972016-08-08 11:38:39 +0300299void TParseContext::checkPrecisionSpecified(const TSourceLoc &line,
300 TPrecision precision,
301 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530302{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400303 if (!mChecksPrecisionErrors)
Olli Etuaho383b7912016-08-05 11:22:59 +0300304 return;
Martin Radev70866b82016-07-22 15:27:42 +0300305
306 if (precision != EbpUndefined && !SupportsPrecision(type))
307 {
308 error(line, "illegal type for precision qualifier", getBasicString(type));
309 }
310
Olli Etuaho183d7e22015-11-20 15:59:09 +0200311 if (precision == EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530312 {
Olli Etuaho183d7e22015-11-20 15:59:09 +0200313 switch (type)
314 {
315 case EbtFloat:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400316 error(line, "No precision specified for (float)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300317 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200318 case EbtInt:
319 case EbtUInt:
320 UNREACHABLE(); // there's always a predeclared qualifier
Jamie Madillb98c3a82015-07-23 14:26:04 -0400321 error(line, "No precision specified (int)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300322 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200323 default:
324 if (IsSampler(type))
325 {
326 error(line, "No precision specified (sampler)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300327 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200328 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300329 if (IsImage(type))
330 {
331 error(line, "No precision specified (image)", "");
332 return;
333 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200334 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000335 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000336}
337
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000338// Both test and if necessary, spit out an error, to see if the node is really
339// an l-value that can be operated on this way.
Olli Etuaho856c4972016-08-08 11:38:39 +0300340bool TParseContext::checkCanBeLValue(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000341{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500342 TIntermSymbol *symNode = node->getAsSymbolNode();
343 TIntermBinary *binaryNode = node->getAsBinaryNode();
Olli Etuahob6fa0432016-09-28 16:28:05 +0100344 TIntermSwizzle *swizzleNode = node->getAsSwizzleNode();
345
346 if (swizzleNode)
347 {
348 bool ok = checkCanBeLValue(line, op, swizzleNode->getOperand());
349 if (ok && swizzleNode->hasDuplicateOffsets())
350 {
351 error(line, " l-value of swizzle cannot have duplicate components", op);
352 return false;
353 }
354 return ok;
355 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000356
Arun Patole7e7e68d2015-05-22 12:02:25 +0530357 if (binaryNode)
358 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400359 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530360 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400361 case EOpIndexDirect:
362 case EOpIndexIndirect:
363 case EOpIndexDirectStruct:
364 case EOpIndexDirectInterfaceBlock:
Olli Etuaho856c4972016-08-08 11:38:39 +0300365 return checkCanBeLValue(line, op, binaryNode->getLeft());
Jamie Madillb98c3a82015-07-23 14:26:04 -0400366 default:
367 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000368 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000369 error(line, " l-value required", op);
Olli Etuaho8a176262016-08-16 14:23:01 +0300370 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000371 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000372
Arun Patole7e7e68d2015-05-22 12:02:25 +0530373 const char *symbol = 0;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000374 if (symNode != 0)
375 symbol = symNode->getSymbol().c_str();
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000376
Arun Patole7e7e68d2015-05-22 12:02:25 +0530377 const char *message = 0;
378 switch (node->getQualifier())
379 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400380 case EvqConst:
381 message = "can't modify a const";
382 break;
383 case EvqConstReadOnly:
384 message = "can't modify a const";
385 break;
386 case EvqAttribute:
387 message = "can't modify an attribute";
388 break;
389 case EvqFragmentIn:
390 message = "can't modify an input";
391 break;
392 case EvqVertexIn:
393 message = "can't modify an input";
394 break;
395 case EvqUniform:
396 message = "can't modify a uniform";
397 break;
398 case EvqVaryingIn:
399 message = "can't modify a varying";
400 break;
401 case EvqFragCoord:
402 message = "can't modify gl_FragCoord";
403 break;
404 case EvqFrontFacing:
405 message = "can't modify gl_FrontFacing";
406 break;
407 case EvqPointCoord:
408 message = "can't modify gl_PointCoord";
409 break;
Martin Radevb0883602016-08-04 17:48:58 +0300410 case EvqNumWorkGroups:
411 message = "can't modify gl_NumWorkGroups";
412 break;
413 case EvqWorkGroupSize:
414 message = "can't modify gl_WorkGroupSize";
415 break;
416 case EvqWorkGroupID:
417 message = "can't modify gl_WorkGroupID";
418 break;
419 case EvqLocalInvocationID:
420 message = "can't modify gl_LocalInvocationID";
421 break;
422 case EvqGlobalInvocationID:
423 message = "can't modify gl_GlobalInvocationID";
424 break;
425 case EvqLocalInvocationIndex:
426 message = "can't modify gl_LocalInvocationIndex";
427 break;
Martin Radev802abe02016-08-04 17:48:32 +0300428 case EvqComputeIn:
429 message = "can't modify work group size variable";
430 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400431 default:
432 //
433 // Type that can't be written to?
434 //
435 if (node->getBasicType() == EbtVoid)
436 {
437 message = "can't modify void";
438 }
439 if (IsSampler(node->getBasicType()))
440 {
441 message = "can't modify a sampler";
442 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300443 if (IsImage(node->getBasicType()))
444 {
445 message = "can't modify an image";
446 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000447 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000448
Arun Patole7e7e68d2015-05-22 12:02:25 +0530449 if (message == 0 && binaryNode == 0 && symNode == 0)
450 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000451 error(line, " l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000452
Olli Etuaho8a176262016-08-16 14:23:01 +0300453 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000454 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000455
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000456 //
457 // Everything else is okay, no error.
458 //
459 if (message == 0)
Olli Etuaho8a176262016-08-16 14:23:01 +0300460 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000461
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000462 //
463 // If we get here, we have an error and a message.
464 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530465 if (symNode)
466 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000467 std::stringstream extraInfoStream;
468 extraInfoStream << "\"" << symbol << "\" (" << message << ")";
469 std::string extraInfo = extraInfoStream.str();
470 error(line, " l-value required", op, extraInfo.c_str());
471 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530472 else
473 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000474 std::stringstream extraInfoStream;
475 extraInfoStream << "(" << message << ")";
476 std::string extraInfo = extraInfoStream.str();
477 error(line, " l-value required", op, extraInfo.c_str());
478 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000479
Olli Etuaho8a176262016-08-16 14:23:01 +0300480 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000481}
482
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000483// Both test, and if necessary spit out an error, to see if the node is really
484// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300485void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000486{
Olli Etuaho383b7912016-08-05 11:22:59 +0300487 if (node->getQualifier() != EvqConst)
488 {
489 error(node->getLine(), "constant expression required", "");
490 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000491}
492
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000493// Both test, and if necessary spit out an error, to see if the node is really
494// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300495void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000496{
Olli Etuaho383b7912016-08-05 11:22:59 +0300497 if (!node->isScalarInt())
498 {
499 error(node->getLine(), "integer expression required", token);
500 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000501}
502
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000503// Both test, and if necessary spit out an error, to see if we are currently
504// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800505bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000506{
Olli Etuaho856c4972016-08-08 11:38:39 +0300507 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300508 {
509 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800510 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300511 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800512 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000513}
514
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000515// For now, keep it simple: if it starts "gl_", it's reserved, independent
516// of scope. Except, if the symbol table is at the built-in push-level,
517// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000518// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
519// webgl shader.
Olli Etuaho856c4972016-08-08 11:38:39 +0300520bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000521{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530522 static const char *reservedErrMsg = "reserved built-in name";
523 if (!symbolTable.atBuiltInLevel())
524 {
525 if (identifier.compare(0, 3, "gl_") == 0)
526 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000527 error(line, reservedErrMsg, "gl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300528 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000529 }
Jamie Madillacb4b812016-11-07 13:50:29 -0500530 if (sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530531 {
532 if (identifier.compare(0, 6, "webgl_") == 0)
533 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000534 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300535 return false;
alokp@chromium.org613ef312010-07-21 18:54:22 +0000536 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530537 if (identifier.compare(0, 7, "_webgl_") == 0)
538 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000539 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300540 return false;
alokp@chromium.org613ef312010-07-21 18:54:22 +0000541 }
542 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530543 if (identifier.find("__") != TString::npos)
544 {
545 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400546 "identifiers containing two consecutive underscores (__) are reserved as "
547 "possible future keywords",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530548 identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300549 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000550 }
551 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000552
Olli Etuaho8a176262016-08-16 14:23:01 +0300553 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000554}
555
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000556// Make sure there is enough data provided to the constructor to build
557// something of the type of the constructor. Also returns the type of
558// the constructor.
Olli Etuaho856c4972016-08-08 11:38:39 +0300559bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
560 TIntermNode *argumentsNode,
561 const TFunction &function,
562 TOperator op,
563 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000564{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000565 bool constructingMatrix = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400566 switch (op)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530567 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400568 case EOpConstructMat2:
569 case EOpConstructMat2x3:
570 case EOpConstructMat2x4:
571 case EOpConstructMat3x2:
572 case EOpConstructMat3:
573 case EOpConstructMat3x4:
574 case EOpConstructMat4x2:
575 case EOpConstructMat4x3:
576 case EOpConstructMat4:
577 constructingMatrix = true;
578 break;
579 default:
580 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000581 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000582
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000583 //
584 // Note: It's okay to have too many components available, but not okay to have unused
585 // arguments. 'full' will go to true when enough args have been seen. If we loop
586 // again, there is an extra argument, so 'overfull' will become true.
587 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000588
Jamie Madillb98c3a82015-07-23 14:26:04 -0400589 size_t size = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400590 bool full = false;
591 bool overFull = false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000592 bool matrixInMatrix = false;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500593 bool arrayArg = false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530594 for (size_t i = 0; i < function.getParamCount(); ++i)
595 {
Dmitry Skibaefa3d8e2015-06-22 14:52:10 -0700596 const TConstParameter &param = function.getParam(i);
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000597 size += param.type->getObjectSize();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530598
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000599 if (constructingMatrix && param.type->isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000600 matrixInMatrix = true;
601 if (full)
602 overFull = true;
Olli Etuaho856c4972016-08-08 11:38:39 +0300603 if (op != EOpConstructStruct && !type.isArray() && size >= type.getObjectSize())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000604 full = true;
alokp@chromium.orgb19403a2010-09-08 17:56:26 +0000605 if (param.type->isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000606 arrayArg = true;
607 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530608
Olli Etuaho856c4972016-08-08 11:38:39 +0300609 if (type.isArray())
Olli Etuaho376f1b52015-04-13 13:23:41 +0300610 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300611 // The size of an unsized constructor should already have been determined.
612 ASSERT(!type.isUnsizedArray());
613 if (static_cast<size_t>(type.getArraySize()) != function.getParamCount())
Olli Etuaho376f1b52015-04-13 13:23:41 +0300614 {
615 error(line, "array constructor needs one argument per array element", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300616 return false;
Olli Etuaho376f1b52015-04-13 13:23:41 +0300617 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000618 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000619
Arun Patole7e7e68d2015-05-22 12:02:25 +0530620 if (arrayArg && op != EOpConstructStruct)
621 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000622 error(line, "constructing from a non-dereferenced array", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300623 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000624 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000625
Olli Etuaho856c4972016-08-08 11:38:39 +0300626 if (matrixInMatrix && !type.isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530627 {
628 if (function.getParamCount() != 1)
629 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400630 error(line, "constructing matrix from matrix can only take one argument",
631 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300632 return false;
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000633 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000634 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000635
Arun Patole7e7e68d2015-05-22 12:02:25 +0530636 if (overFull)
637 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000638 error(line, "too many arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300639 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000640 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530641
Olli Etuaho856c4972016-08-08 11:38:39 +0300642 if (op == EOpConstructStruct && !type.isArray() &&
643 type.getStruct()->fields().size() != function.getParamCount())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530644 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400645 error(line,
646 "Number of constructor parameters does not match the number of structure fields",
647 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300648 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000649 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000650
Olli Etuaho856c4972016-08-08 11:38:39 +0300651 if (!type.isMatrix() || !matrixInMatrix)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530652 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300653 if ((op != EOpConstructStruct && size != 1 && size < type.getObjectSize()) ||
654 (op == EOpConstructStruct && size < type.getObjectSize()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530655 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000656 error(line, "not enough data provided for construction", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300657 return false;
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000658 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000659 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000660
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200661 if (argumentsNode == nullptr)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530662 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200663 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300664 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000665 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200666
667 TIntermAggregate *argumentsAgg = argumentsNode->getAsAggregate();
668 for (TIntermNode *&argNode : *argumentsAgg->getSequence())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530669 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200670 TIntermTyped *argTyped = argNode->getAsTyped();
671 ASSERT(argTyped != nullptr);
672 if (op != EOpConstructStruct && IsSampler(argTyped->getBasicType()))
673 {
674 error(line, "cannot convert a sampler", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300675 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200676 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300677 if (op != EOpConstructStruct && IsImage(argTyped->getBasicType()))
678 {
679 error(line, "cannot convert an image", "constructor");
680 return false;
681 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200682 if (argTyped->getBasicType() == EbtVoid)
683 {
684 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300685 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200686 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000687 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000688
Olli Etuaho856c4972016-08-08 11:38:39 +0300689 if (type.isArray())
690 {
691 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
692 // the array.
693 for (TIntermNode *&argNode : *argumentsAgg->getSequence())
694 {
695 const TType &argType = argNode->getAsTyped()->getType();
696 // It has already been checked that the argument is not an array.
697 ASSERT(!argType.isArray());
698 if (!argType.sameElementType(type))
699 {
700 error(line, "Array constructor argument has an incorrect type", "Error");
Olli Etuaho8a176262016-08-16 14:23:01 +0300701 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300702 }
703 }
704 }
705 else if (op == EOpConstructStruct)
706 {
707 const TFieldList &fields = type.getStruct()->fields();
708 TIntermSequence *args = argumentsAgg->getSequence();
709
710 for (size_t i = 0; i < fields.size(); i++)
711 {
712 if (i >= args->size() || (*args)[i]->getAsTyped()->getType() != *fields[i]->type())
713 {
714 error(line, "Structure constructor arguments do not match structure fields",
715 "Error");
Olli Etuaho8a176262016-08-16 14:23:01 +0300716 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300717 }
718 }
719 }
720
Olli Etuaho8a176262016-08-16 14:23:01 +0300721 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000722}
723
Jamie Madillb98c3a82015-07-23 14:26:04 -0400724// This function checks to see if a void variable has been declared and raise an error message for
725// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000726//
727// returns true in case of an error
728//
Olli Etuaho856c4972016-08-08 11:38:39 +0300729bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400730 const TString &identifier,
731 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000732{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300733 if (type == EbtVoid)
734 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000735 error(line, "illegal use of type 'void'", identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300736 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300737 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000738
Olli Etuaho8a176262016-08-16 14:23:01 +0300739 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000740}
741
Jamie Madillb98c3a82015-07-23 14:26:04 -0400742// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300743// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300744void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000745{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530746 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector())
747 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000748 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530749 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000750}
751
Jamie Madillb98c3a82015-07-23 14:26:04 -0400752// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300753// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300754void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000755{
Martin Radev4a9cd802016-09-01 16:51:51 +0300756 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530757 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000758 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530759 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000760}
761
Olli Etuaho856c4972016-08-08 11:38:39 +0300762bool TParseContext::checkIsNotSampler(const TSourceLoc &line,
Martin Radev4a9cd802016-09-01 16:51:51 +0300763 const TTypeSpecifierNonArray &pType,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400764 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000765{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530766 if (pType.type == EbtStruct)
767 {
Martin Radev2cc85b32016-08-05 16:22:53 +0300768 if (ContainsSampler(*pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530769 {
alokp@chromium.org58e54292010-08-24 21:40:03 +0000770 error(line, reason, getBasicString(pType.type), "(structure contains a sampler)");
Olli Etuaho8a176262016-08-16 14:23:01 +0300771 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000772 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530773
Olli Etuaho8a176262016-08-16 14:23:01 +0300774 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530775 }
776 else if (IsSampler(pType.type))
777 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000778 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300779 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000780 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000781
Olli Etuaho8a176262016-08-16 14:23:01 +0300782 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000783}
784
Martin Radev2cc85b32016-08-05 16:22:53 +0300785bool TParseContext::checkIsNotImage(const TSourceLoc &line,
786 const TTypeSpecifierNonArray &pType,
787 const char *reason)
788{
789 if (pType.type == EbtStruct)
790 {
791 if (ContainsImage(*pType.userDef))
792 {
793 error(line, reason, getBasicString(pType.type), "(structure contains an image)");
794
795 return false;
796 }
797
798 return true;
799 }
800 else if (IsImage(pType.type))
801 {
802 error(line, reason, getBasicString(pType.type));
803
804 return false;
805 }
806
807 return true;
808}
809
Olli Etuaho856c4972016-08-08 11:38:39 +0300810void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
811 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400812{
813 if (pType.layoutQualifier.location != -1)
814 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400815 error(line, "location must only be specified for a single input or output variable",
816 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400817 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400818}
819
Olli Etuaho856c4972016-08-08 11:38:39 +0300820void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
821 const TLayoutQualifier &layoutQualifier)
822{
823 if (layoutQualifier.location != -1)
824 {
825 error(location, "invalid layout qualifier:", "location",
826 "only valid on program inputs and outputs");
827 }
828}
829
Martin Radev2cc85b32016-08-05 16:22:53 +0300830void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
831 TQualifier qualifier,
832 const TType &type)
833{
834 checkOutParameterIsNotSampler(line, qualifier, type);
835 checkOutParameterIsNotImage(line, qualifier, type);
836}
837
Olli Etuaho856c4972016-08-08 11:38:39 +0300838void TParseContext::checkOutParameterIsNotSampler(const TSourceLoc &line,
839 TQualifier qualifier,
840 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000841{
Martin Radev2cc85b32016-08-05 16:22:53 +0300842 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
843 if (IsSampler(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530844 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000845 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000846 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000847}
848
Martin Radev2cc85b32016-08-05 16:22:53 +0300849void TParseContext::checkOutParameterIsNotImage(const TSourceLoc &line,
850 TQualifier qualifier,
851 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000852{
Martin Radev2cc85b32016-08-05 16:22:53 +0300853 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
854 if (IsImage(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530855 {
Martin Radev2cc85b32016-08-05 16:22:53 +0300856 error(line, "images cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000857 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000858}
859
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000860// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300861unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000862{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530863 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000864
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200865 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
866 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
867 // fold as array size.
868 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000869 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000870 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300871 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000872 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000873
Olli Etuaho856c4972016-08-08 11:38:39 +0300874 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400875
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000876 if (constant->getBasicType() == EbtUInt)
877 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300878 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000879 }
880 else
881 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300882 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000883
Olli Etuaho856c4972016-08-08 11:38:39 +0300884 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000885 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400886 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300887 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000888 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400889
Olli Etuaho856c4972016-08-08 11:38:39 +0300890 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400891 }
892
Olli Etuaho856c4972016-08-08 11:38:39 +0300893 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400894 {
895 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300896 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400897 }
898
899 // The size of arrays is restricted here to prevent issues further down the
900 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
901 // 4096 registers so this should be reasonable even for aggressively optimizable code.
902 const unsigned int sizeLimit = 65536;
903
Olli Etuaho856c4972016-08-08 11:38:39 +0300904 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -0400905 {
906 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300907 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000908 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300909
910 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000911}
912
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000913// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +0300914bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
915 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000916{
Olli Etuaho8a176262016-08-16 14:23:01 +0300917 if ((elementQualifier.qualifier == EvqAttribute) ||
918 (elementQualifier.qualifier == EvqVertexIn) ||
919 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300920 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400921 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300922 TType(elementQualifier).getQualifierString());
923 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000924 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000925
Olli Etuaho8a176262016-08-16 14:23:01 +0300926 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000927}
928
Olli Etuaho8a176262016-08-16 14:23:01 +0300929// See if this element type can be formed into an array.
930bool TParseContext::checkIsValidTypeForArray(const TSourceLoc &line, const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000931{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000932 //
933 // Can the type be an array?
934 //
Olli Etuaho8a176262016-08-16 14:23:01 +0300935 if (elementType.array)
Jamie Madill06145232015-05-13 13:10:01 -0400936 {
Olli Etuaho8a176262016-08-16 14:23:01 +0300937 error(line, "cannot declare arrays of arrays",
938 TType(elementType).getCompleteString().c_str());
939 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000940 }
Olli Etuahocc36b982015-07-10 14:14:18 +0300941 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
942 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
943 // 4.3.4).
Martin Radev4a9cd802016-09-01 16:51:51 +0300944 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Olli Etuaho8a176262016-08-16 14:23:01 +0300945 sh::IsVarying(elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +0300946 {
947 error(line, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300948 TType(elementType).getCompleteString().c_str());
949 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +0300950 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000951
Olli Etuaho8a176262016-08-16 14:23:01 +0300952 return true;
953}
954
955// Check if this qualified element type can be formed into an array.
956bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
957 const TPublicType &elementType)
958{
959 if (checkIsValidTypeForArray(indexLocation, elementType))
960 {
961 return checkIsValidQualifierForArray(indexLocation, elementType);
962 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000963 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000964}
965
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000966// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +0300967void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
968 const TString &identifier,
969 TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000970{
Olli Etuaho3739d232015-04-08 12:23:44 +0300971 ASSERT(type != nullptr);
972 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000973 {
974 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +0300975 type->qualifier = EvqTemporary;
976
977 // Generate informative error messages for ESSL1.
978 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400979 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000980 {
Arun Patole7e7e68d2015-05-22 12:02:25 +0530981 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400982 "structures containing arrays may not be declared constant since they cannot be "
983 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530984 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +0000985 }
986 else
987 {
988 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
989 }
Olli Etuaho383b7912016-08-05 11:22:59 +0300990 return;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000991 }
Olli Etuaho376f1b52015-04-13 13:23:41 +0300992 if (type->isUnsizedArray())
993 {
994 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
Olli Etuaho376f1b52015-04-13 13:23:41 +0300995 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000996}
997
Olli Etuaho2935c582015-04-08 14:32:06 +0300998// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000999// and update the symbol table.
1000//
Olli Etuaho2935c582015-04-08 14:32:06 +03001001// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001002//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001003bool TParseContext::declareVariable(const TSourceLoc &line,
1004 const TString &identifier,
1005 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001006 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001007{
Olli Etuaho2935c582015-04-08 14:32:06 +03001008 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001009
Olli Etuaho856c4972016-08-08 11:38:39 +03001010 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001011
Olli Etuaho2935c582015-04-08 14:32:06 +03001012 // gl_LastFragData may be redeclared with a new precision qualifier
1013 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1014 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001015 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1016 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho856c4972016-08-08 11:38:39 +03001017 if (static_cast<int>(type.getArraySize()) == maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001018 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001019 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001020 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001021 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->getExtension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001022 }
1023 }
1024 else
1025 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001026 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1027 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001028 return false;
1029 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001030 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001031
Olli Etuaho8a176262016-08-16 14:23:01 +03001032 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001033 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001034
Olli Etuaho2935c582015-04-08 14:32:06 +03001035 (*variable) = new TVariable(&identifier, type);
1036 if (!symbolTable.declare(*variable))
1037 {
1038 error(line, "redefinition", identifier.c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -04001039 *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +03001040 return false;
1041 }
1042
Olli Etuaho8a176262016-08-16 14:23:01 +03001043 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001044 return false;
1045
1046 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001047}
1048
Martin Radev70866b82016-07-22 15:27:42 +03001049void TParseContext::checkIsParameterQualifierValid(
1050 const TSourceLoc &line,
1051 const TTypeQualifierBuilder &typeQualifierBuilder,
1052 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301053{
Olli Etuaho613b9592016-09-05 12:05:53 +03001054 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(&mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001055
1056 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301057 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001058 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1059 }
1060
1061 if (!IsImage(type->getBasicType()))
1062 {
1063 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, line);
1064 }
1065 else
1066 {
1067 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001068 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001069
Martin Radev70866b82016-07-22 15:27:42 +03001070 type->setQualifier(typeQualifier.qualifier);
1071
1072 if (typeQualifier.precision != EbpUndefined)
1073 {
1074 type->setPrecision(typeQualifier.precision);
1075 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001076}
1077
Olli Etuaho856c4972016-08-08 11:38:39 +03001078bool TParseContext::checkCanUseExtension(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001079{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001080 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001081 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +05301082 if (iter == extBehavior.end())
1083 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001084 error(line, "extension", extension.c_str(), "is not supported");
Olli Etuaho8a176262016-08-16 14:23:01 +03001085 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001086 }
zmo@google.comf5450912011-09-09 01:37:19 +00001087 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +05301088 if (iter->second == EBhDisable || iter->second == EBhUndefined)
1089 {
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001090 error(line, "extension", extension.c_str(), "is disabled");
Olli Etuaho8a176262016-08-16 14:23:01 +03001091 return false;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001092 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301093 if (iter->second == EBhWarn)
1094 {
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001095 warning(line, "extension", extension.c_str(), "is being used");
Olli Etuaho8a176262016-08-16 14:23:01 +03001096 return true;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001097 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001098
Olli Etuaho8a176262016-08-16 14:23:01 +03001099 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001100}
1101
Martin Radevb8b01222016-11-20 23:25:53 +02001102void TParseContext::emptyDeclarationErrorCheck(const TPublicType &publicType,
1103 const TSourceLoc &location)
1104{
1105 if (publicType.isUnsizedArray())
1106 {
1107 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1108 // error. It is assumed that this applies to empty declarations as well.
1109 error(location, "empty array declaration needs to specify a size", "");
1110 }
1111 if (publicType.qualifier == EvqShared && !publicType.layoutQualifier.isEmpty())
1112 {
1113 error(location, "Shared memory declarations cannot have layout specified", "layout");
1114 }
1115}
1116
Jamie Madillb98c3a82015-07-23 14:26:04 -04001117// These checks are common for all declarations starting a declarator list, and declarators that
1118// follow an empty declaration.
Olli Etuaho383b7912016-08-05 11:22:59 +03001119void TParseContext::singleDeclarationErrorCheck(const TPublicType &publicType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001120 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001121{
Olli Etuahofa33d582015-04-09 14:33:12 +03001122 switch (publicType.qualifier)
1123 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001124 case EvqVaryingIn:
1125 case EvqVaryingOut:
1126 case EvqAttribute:
1127 case EvqVertexIn:
1128 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001129 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001130 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001131 {
1132 error(identifierLocation, "cannot be used with a structure",
1133 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001134 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001135 }
Olli Etuahofa33d582015-04-09 14:33:12 +03001136
Jamie Madillb98c3a82015-07-23 14:26:04 -04001137 default:
1138 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001139 }
1140
Jamie Madillb98c3a82015-07-23 14:26:04 -04001141 if (publicType.qualifier != EvqUniform &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001142 !checkIsNotSampler(identifierLocation, publicType.typeSpecifierNonArray,
1143 "samplers must be uniform"))
Olli Etuahofa33d582015-04-09 14:33:12 +03001144 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001145 return;
Olli Etuahofa33d582015-04-09 14:33:12 +03001146 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001147 if (publicType.qualifier != EvqUniform &&
1148 !checkIsNotImage(identifierLocation, publicType.typeSpecifierNonArray,
1149 "images must be uniform"))
1150 {
1151 return;
1152 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001153
1154 // check for layout qualifier issues
1155 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
1156
1157 if (layoutQualifier.matrixPacking != EmpUnspecified)
1158 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001159 error(identifierLocation, "layout qualifier",
1160 getMatrixPackingString(layoutQualifier.matrixPacking),
Olli Etuahofa33d582015-04-09 14:33:12 +03001161 "only valid for interface blocks");
Olli Etuaho383b7912016-08-05 11:22:59 +03001162 return;
Jamie Madilla5efff92013-06-06 11:56:47 -04001163 }
1164
1165 if (layoutQualifier.blockStorage != EbsUnspecified)
1166 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001167 error(identifierLocation, "layout qualifier",
1168 getBlockStorageString(layoutQualifier.blockStorage),
Olli Etuahofa33d582015-04-09 14:33:12 +03001169 "only valid for interface blocks");
Olli Etuaho383b7912016-08-05 11:22:59 +03001170 return;
Jamie Madilla5efff92013-06-06 11:56:47 -04001171 }
1172
Olli Etuaho383b7912016-08-05 11:22:59 +03001173 if (publicType.qualifier != EvqVertexIn && publicType.qualifier != EvqFragmentOut)
Jamie Madilla5efff92013-06-06 11:56:47 -04001174 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001175 checkLocationIsNotSpecified(identifierLocation, publicType.layoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04001176 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001177
1178 if (IsImage(publicType.getBasicType()))
1179 {
1180
1181 switch (layoutQualifier.imageInternalFormat)
1182 {
1183 case EiifRGBA32F:
1184 case EiifRGBA16F:
1185 case EiifR32F:
1186 case EiifRGBA8:
1187 case EiifRGBA8_SNORM:
1188 if (!IsFloatImage(publicType.getBasicType()))
1189 {
1190 error(identifierLocation,
1191 "internal image format requires a floating image type",
1192 getBasicString(publicType.getBasicType()));
1193 return;
1194 }
1195 break;
1196 case EiifRGBA32I:
1197 case EiifRGBA16I:
1198 case EiifRGBA8I:
1199 case EiifR32I:
1200 if (!IsIntegerImage(publicType.getBasicType()))
1201 {
1202 error(identifierLocation,
1203 "internal image format requires an integer image type",
1204 getBasicString(publicType.getBasicType()));
1205 return;
1206 }
1207 break;
1208 case EiifRGBA32UI:
1209 case EiifRGBA16UI:
1210 case EiifRGBA8UI:
1211 case EiifR32UI:
1212 if (!IsUnsignedImage(publicType.getBasicType()))
1213 {
1214 error(identifierLocation,
1215 "internal image format requires an unsigned image type",
1216 getBasicString(publicType.getBasicType()));
1217 return;
1218 }
1219 break;
1220 case EiifUnspecified:
1221 error(identifierLocation, "layout qualifier", "No image internal format specified");
1222 return;
1223 default:
1224 error(identifierLocation, "layout qualifier", "unrecognized token");
1225 return;
1226 }
1227
1228 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1229 switch (layoutQualifier.imageInternalFormat)
1230 {
1231 case EiifR32F:
1232 case EiifR32I:
1233 case EiifR32UI:
1234 break;
1235 default:
1236 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1237 {
1238 error(identifierLocation, "layout qualifier",
1239 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1240 "image variables must be qualified readonly and/or writeonly");
1241 return;
1242 }
1243 break;
1244 }
1245 }
1246 else
1247 {
1248
1249 if (!checkInternalFormatIsNotSpecified(identifierLocation,
1250 layoutQualifier.imageInternalFormat))
1251 {
1252 return;
1253 }
1254
1255 if (!checkIsMemoryQualifierNotSpecified(publicType.memoryQualifier, identifierLocation))
1256 {
1257 return;
1258 }
1259 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001260}
1261
Olli Etuaho856c4972016-08-08 11:38:39 +03001262void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1263 const TString &layoutQualifierName,
1264 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001265{
1266
1267 if (mShaderVersion < versionRequired)
1268 {
1269 error(location, "invalid layout qualifier:", layoutQualifierName.c_str(), "not supported");
Martin Radev802abe02016-08-04 17:48:32 +03001270 }
1271}
1272
Olli Etuaho856c4972016-08-08 11:38:39 +03001273bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1274 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001275{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001276 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001277 for (size_t i = 0u; i < localSize.size(); ++i)
1278 {
1279 if (localSize[i] != -1)
1280 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03001281 error(location, "invalid layout qualifier:", getWorkGroupSizeString(i),
Martin Radev802abe02016-08-04 17:48:32 +03001282 "only valid when used with 'in' in a compute shader global layout declaration");
Olli Etuaho8a176262016-08-16 14:23:01 +03001283 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001284 }
1285 }
1286
Olli Etuaho8a176262016-08-16 14:23:01 +03001287 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001288}
1289
Martin Radev2cc85b32016-08-05 16:22:53 +03001290bool TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
1291 TLayoutImageInternalFormat internalFormat)
1292{
1293 if (internalFormat != EiifUnspecified)
1294 {
1295 error(location, "invalid layout qualifier:", getImageInternalFormatString(internalFormat),
1296 "only valid when used with images");
1297 return false;
1298 }
1299 return true;
1300}
1301
Olli Etuaho383b7912016-08-05 11:22:59 +03001302void TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate,
Olli Etuaho856c4972016-08-08 11:38:39 +03001303 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001304{
1305 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1306 {
1307 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1308 if (qual == EvqOut || qual == EvqInOut)
1309 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001310 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
Olli Etuaho8a176262016-08-16 14:23:01 +03001311 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001312 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001313 error(argument->getLine(),
Jamie Madillb98c3a82015-07-23 14:26:04 -04001314 "Constant value cannot be passed for 'out' or 'inout' parameters.", "Error");
Olli Etuaho383b7912016-08-05 11:22:59 +03001315 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001316 }
1317 }
1318 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001319}
1320
Martin Radev70866b82016-07-22 15:27:42 +03001321void TParseContext::checkInvariantVariableQualifier(bool invariant,
1322 const TQualifier qualifier,
1323 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001324{
Martin Radev70866b82016-07-22 15:27:42 +03001325 if (!invariant)
1326 return;
1327
1328 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001329 {
Martin Radev70866b82016-07-22 15:27:42 +03001330 // input variables in the fragment shader can be also qualified as invariant
1331 if (!sh::CanBeInvariantESSL1(qualifier))
1332 {
1333 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1334 }
1335 }
1336 else
1337 {
1338 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1339 {
1340 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1341 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001342 }
1343}
1344
Arun Patole7e7e68d2015-05-22 12:02:25 +05301345bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001346{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001347 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001348 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1349 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001350}
1351
Arun Patole7e7e68d2015-05-22 12:02:25 +05301352bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001353{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001354 return ::IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001355}
1356
Jamie Madillb98c3a82015-07-23 14:26:04 -04001357void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1358 const char *extName,
1359 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001360{
1361 pp::SourceLocation srcLoc;
1362 srcLoc.file = loc.first_file;
1363 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001364 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001365}
1366
Jamie Madillb98c3a82015-07-23 14:26:04 -04001367void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1368 const char *name,
1369 const char *value,
1370 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001371{
1372 pp::SourceLocation srcLoc;
1373 srcLoc.file = loc.first_file;
1374 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001375 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001376}
1377
Martin Radev4c4c8e72016-08-04 12:25:34 +03001378sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001379{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001380 sh::WorkGroupSize result;
Martin Radev802abe02016-08-04 17:48:32 +03001381 for (size_t i = 0u; i < result.size(); ++i)
1382 {
1383 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1384 {
1385 result[i] = 1;
1386 }
1387 else
1388 {
1389 result[i] = mComputeShaderLocalSize[i];
1390 }
1391 }
1392 return result;
1393}
1394
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001395/////////////////////////////////////////////////////////////////////////////////
1396//
1397// Non-Errors.
1398//
1399/////////////////////////////////////////////////////////////////////////////////
1400
Jamie Madill5c097022014-08-20 16:38:32 -04001401const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1402 const TString *name,
1403 const TSymbol *symbol)
1404{
1405 const TVariable *variable = NULL;
1406
1407 if (!symbol)
1408 {
1409 error(location, "undeclared identifier", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001410 }
1411 else if (!symbol->isVariable())
1412 {
1413 error(location, "variable expected", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001414 }
1415 else
1416 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001417 variable = static_cast<const TVariable *>(symbol);
Jamie Madill5c097022014-08-20 16:38:32 -04001418
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001419 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
Olli Etuaho383b7912016-08-05 11:22:59 +03001420 !variable->getExtension().empty())
Jamie Madill5c097022014-08-20 16:38:32 -04001421 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001422 checkCanUseExtension(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001423 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001424
1425 // Reject shaders using both gl_FragData and gl_FragColor
1426 TQualifier qualifier = variable->getType().getQualifier();
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001427 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001428 {
1429 mUsesFragData = true;
1430 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001431 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001432 {
1433 mUsesFragColor = true;
1434 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001435 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1436 {
1437 mUsesSecondaryOutputs = true;
1438 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001439
1440 // This validation is not quite correct - it's only an error to write to
1441 // both FragData and FragColor. For simplicity, and because users shouldn't
1442 // be rewarded for reading from undefined varaibles, return an error
1443 // if they are both referenced, rather than assigned.
1444 if (mUsesFragData && mUsesFragColor)
1445 {
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001446 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1447 if (mUsesSecondaryOutputs)
1448 {
1449 errorMessage =
1450 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1451 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1452 }
1453 error(location, errorMessage, name->c_str());
Jamie Madill14e95b32015-05-07 10:10:41 -04001454 }
Martin Radevb0883602016-08-04 17:48:58 +03001455
1456 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1457 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1458 qualifier == EvqWorkGroupSize)
1459 {
1460 error(location,
1461 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1462 "gl_WorkGroupSize");
1463 }
Jamie Madill5c097022014-08-20 16:38:32 -04001464 }
1465
1466 if (!variable)
1467 {
1468 TType type(EbtFloat, EbpUndefined);
1469 TVariable *fakeVariable = new TVariable(name, type);
1470 symbolTable.declare(fakeVariable);
1471 variable = fakeVariable;
1472 }
1473
1474 return variable;
1475}
1476
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001477TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1478 const TString *name,
1479 const TSymbol *symbol)
1480{
1481 const TVariable *variable = getNamedVariable(location, name, symbol);
1482
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001483 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001484 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001485 const TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001486 return intermediate.addConstantUnion(constArray, variable->getType(), location);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001487 }
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001488 else if (variable->getType().getQualifier() == EvqWorkGroupSize &&
1489 mComputeShaderLocalSizeDeclared)
1490 {
1491 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1492 // needs to be added to the AST as a constant and not as a symbol.
1493 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1494 TConstantUnion *constArray = new TConstantUnion[3];
1495 for (size_t i = 0; i < 3; ++i)
1496 {
1497 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1498 }
1499
1500 ASSERT(variable->getType().getBasicType() == EbtUInt);
1501 ASSERT(variable->getType().getObjectSize() == 3);
1502
1503 TType type(variable->getType());
1504 type.setQualifier(EvqConst);
1505 return intermediate.addConstantUnion(constArray, type, location);
1506 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001507 else
1508 {
1509 return intermediate.addSymbol(variable->getUniqueId(), variable->getName(),
1510 variable->getType(), location);
1511 }
1512}
1513
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001514//
1515// Look up a function name in the symbol table, and make sure it is a function.
1516//
1517// Return the function symbol if found, otherwise 0.
1518//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001519const TFunction *TParseContext::findFunction(const TSourceLoc &line,
1520 TFunction *call,
1521 int inputShaderVersion,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301522 bool *builtIn)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001523{
alokp@chromium.org0a576182010-08-09 17:16:27 +00001524 // First find by unmangled name to check whether the function name has been
1525 // hidden by a variable name or struct typename.
Nicolas Capensd4a9b8d2013-07-18 11:01:22 -04001526 // If a function is found, check for one with a matching argument list.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301527 const TSymbol *symbol = symbolTable.find(call->getName(), inputShaderVersion, builtIn);
1528 if (symbol == 0 || symbol->isFunction())
1529 {
Austin Kinross3ae64652015-01-26 15:51:39 -08001530 symbol = symbolTable.find(call->getMangledName(), inputShaderVersion, builtIn);
alokp@chromium.org0a576182010-08-09 17:16:27 +00001531 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001532
Arun Patole7e7e68d2015-05-22 12:02:25 +05301533 if (symbol == 0)
1534 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001535 error(line, "no matching overloaded function found", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001536 return 0;
1537 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001538
Arun Patole7e7e68d2015-05-22 12:02:25 +05301539 if (!symbol->isFunction())
1540 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001541 error(line, "function name expected", call->getName().c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001542 return 0;
1543 }
alokp@chromium.org0a576182010-08-09 17:16:27 +00001544
Jamie Madillb98c3a82015-07-23 14:26:04 -04001545 return static_cast<const TFunction *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001546}
1547
1548//
1549// Initializers show up in several places in the grammar. Have one set of
1550// code to handle them here.
1551//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001552// Returns true on error, false if no error
1553//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001554bool TParseContext::executeInitializer(const TSourceLoc &line,
1555 const TString &identifier,
1556 const TPublicType &pType,
1557 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001558 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001559{
Olli Etuaho13389b62016-10-16 11:48:18 +01001560 ASSERT(initNode != nullptr);
1561 ASSERT(*initNode == nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001562 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001563
Olli Etuaho2935c582015-04-08 14:32:06 +03001564 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001565 if (type.isUnsizedArray())
1566 {
Olli Etuaho02bd82c2016-11-03 10:29:43 +00001567 // We have not checked yet whether the initializer actually is an array or not.
1568 if (initializer->isArray())
1569 {
1570 type.setArraySize(initializer->getArraySize());
1571 }
1572 else
1573 {
1574 // Having a non-array initializer for an unsized array will result in an error later,
1575 // so we don't generate an error message here.
1576 type.setArraySize(1u);
1577 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001578 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001579 if (!declareVariable(line, identifier, type, &variable))
1580 {
1581 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001582 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001583
Olli Etuahob0c645e2015-05-12 14:25:36 +03001584 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001585 if (symbolTable.atGlobalLevel() &&
1586 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001587 {
1588 // Error message does not completely match behavior with ESSL 1.00, but
1589 // we want to steer developers towards only using constant expressions.
1590 error(line, "global variable initializers must be constant expressions", "=");
1591 return true;
1592 }
1593 if (globalInitWarning)
1594 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001595 warning(
1596 line,
1597 "global variable initializers should be constant expressions "
1598 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1599 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001600 }
1601
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001602 //
1603 // identifier must be of type constant, a global, or a temporary
1604 //
1605 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301606 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1607 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001608 error(line, " cannot initialize this type of qualifier ",
1609 variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001610 return true;
1611 }
1612 //
1613 // test for and propagate constant
1614 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001615
Arun Patole7e7e68d2015-05-22 12:02:25 +05301616 if (qualifier == EvqConst)
1617 {
1618 if (qualifier != initializer->getType().getQualifier())
1619 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00001620 std::stringstream extraInfoStream;
1621 extraInfoStream << "'" << variable->getType().getCompleteString() << "'";
1622 std::string extraInfo = extraInfoStream.str();
1623 error(line, " assigning non-constant to", "=", extraInfo.c_str());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001624 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001625 return true;
1626 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301627 if (type != initializer->getType())
1628 {
1629 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001630 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001631 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001632 return true;
1633 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001634
1635 // Save the constant folded value to the variable if possible. For example array
1636 // initializers are not folded, since that way copying the array literal to multiple places
1637 // in the shader is avoided.
1638 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1639 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301640 if (initializer->getAsConstantUnion())
1641 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001642 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho13389b62016-10-16 11:48:18 +01001643 *initNode = nullptr;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001644 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301645 }
1646 else if (initializer->getAsSymbolNode())
1647 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001648 const TSymbol *symbol =
1649 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1650 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001651
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001652 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001653 if (constArray)
1654 {
1655 variable->shareConstPointer(constArray);
Olli Etuaho13389b62016-10-16 11:48:18 +01001656 *initNode = nullptr;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001657 return false;
1658 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001659 }
1660 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001661
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001662 TIntermSymbol *intermSymbol = intermediate.addSymbol(
1663 variable->getUniqueId(), variable->getName(), variable->getType(), line);
Olli Etuaho13389b62016-10-16 11:48:18 +01001664 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1665 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001666 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001667 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1668 return true;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001669 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001670
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001671 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001672}
1673
Olli Etuaho0e3aee32016-10-27 12:56:38 +01001674void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
1675{
1676 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
1677 typeSpecifier->getBasicType());
1678
1679 if (mShaderVersion < 300 && typeSpecifier->array)
1680 {
1681 error(typeSpecifier->getLine(), "not supported", "first-class array");
1682 typeSpecifier->clearArrayness();
1683 }
1684}
1685
Martin Radev70866b82016-07-22 15:27:42 +03001686TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301687 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001688{
Olli Etuaho613b9592016-09-05 12:05:53 +03001689 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001690
Martin Radev70866b82016-07-22 15:27:42 +03001691 TPublicType returnType = typeSpecifier;
1692 returnType.qualifier = typeQualifier.qualifier;
1693 returnType.invariant = typeQualifier.invariant;
1694 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03001695 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03001696 returnType.precision = typeSpecifier.precision;
1697
1698 if (typeQualifier.precision != EbpUndefined)
1699 {
1700 returnType.precision = typeQualifier.precision;
1701 }
1702
Martin Radev4a9cd802016-09-01 16:51:51 +03001703 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
1704 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03001705
Martin Radev4a9cd802016-09-01 16:51:51 +03001706 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
1707 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03001708
Martin Radev4a9cd802016-09-01 16:51:51 +03001709 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03001710
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001711 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001712 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001713 if (typeSpecifier.array)
1714 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001715 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001716 returnType.clearArrayness();
1717 }
1718
Martin Radev70866b82016-07-22 15:27:42 +03001719 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001720 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001721 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001722 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001723 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001724 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001725
Martin Radev70866b82016-07-22 15:27:42 +03001726 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001727 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001728 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001729 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001730 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001731 }
1732 }
1733 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001734 {
Martin Radev70866b82016-07-22 15:27:42 +03001735 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03001736 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001737 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03001738 }
Martin Radev70866b82016-07-22 15:27:42 +03001739 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
1740 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001741 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001742 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
1743 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001744 }
Martin Radev70866b82016-07-22 15:27:42 +03001745 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03001746 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001747 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03001748 "in");
Martin Radev802abe02016-08-04 17:48:32 +03001749 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001750 }
1751
1752 return returnType;
1753}
1754
Olli Etuaho856c4972016-08-08 11:38:39 +03001755void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
1756 const TPublicType &type,
1757 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03001758{
1759 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03001760 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03001761 {
1762 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001763 }
1764
1765 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
1766 switch (qualifier)
1767 {
1768 case EvqVertexIn:
1769 // ESSL 3.00 section 4.3.4
1770 if (type.array)
1771 {
1772 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001773 }
1774 // Vertex inputs with a struct type are disallowed in singleDeclarationErrorCheck
1775 return;
1776 case EvqFragmentOut:
1777 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03001778 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03001779 {
1780 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001781 }
1782 // Fragment outputs with a struct type are disallowed in singleDeclarationErrorCheck
1783 return;
1784 default:
1785 break;
1786 }
1787
1788 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
1789 // restrictions.
1790 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03001791 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
1792 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03001793 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
1794 {
1795 error(qualifierLocation, "must use 'flat' interpolation here",
1796 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001797 }
1798
Martin Radev4a9cd802016-09-01 16:51:51 +03001799 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03001800 {
1801 // ESSL 3.00 sections 4.3.4 and 4.3.6.
1802 // These restrictions are only implied by the ESSL 3.00 spec, but
1803 // the ESSL 3.10 spec lists these restrictions explicitly.
1804 if (type.array)
1805 {
1806 error(qualifierLocation, "cannot be an array of structures",
1807 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001808 }
1809 if (type.isStructureContainingArrays())
1810 {
1811 error(qualifierLocation, "cannot be a structure containing an array",
1812 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001813 }
1814 if (type.isStructureContainingType(EbtStruct))
1815 {
1816 error(qualifierLocation, "cannot be a structure containing a structure",
1817 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001818 }
1819 if (type.isStructureContainingType(EbtBool))
1820 {
1821 error(qualifierLocation, "cannot be a structure containing a bool",
1822 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001823 }
1824 }
1825}
1826
Martin Radev2cc85b32016-08-05 16:22:53 +03001827void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
1828{
1829 if (qualifier.getType() == QtStorage)
1830 {
1831 const TStorageQualifierWrapper &storageQualifier =
1832 static_cast<const TStorageQualifierWrapper &>(qualifier);
1833 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
1834 !symbolTable.atGlobalLevel())
1835 {
1836 error(storageQualifier.getLine(),
1837 "Local variables can only use the const storage qualifier.",
1838 storageQualifier.getQualifierString().c_str());
1839 }
1840 }
1841}
1842
1843bool TParseContext::checkIsMemoryQualifierNotSpecified(const TMemoryQualifier &memoryQualifier,
1844 const TSourceLoc &location)
1845{
1846 if (memoryQualifier.readonly)
1847 {
1848 error(location, "Only allowed with images.", "readonly");
1849 return false;
1850 }
1851 if (memoryQualifier.writeonly)
1852 {
1853 error(location, "Only allowed with images.", "writeonly");
1854 return false;
1855 }
Martin Radev049edfa2016-11-11 14:35:37 +02001856 if (memoryQualifier.coherent)
1857 {
1858 error(location, "Only allowed with images.", "coherent");
1859 return false;
1860 }
1861 if (memoryQualifier.restrictQualifier)
1862 {
1863 error(location, "Only allowed with images.", "restrict");
1864 return false;
1865 }
1866 if (memoryQualifier.volatileQualifier)
1867 {
1868 error(location, "Only allowed with images.", "volatile");
1869 return false;
1870 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001871 return true;
1872}
1873
Olli Etuaho13389b62016-10-16 11:48:18 +01001874TIntermDeclaration *TParseContext::parseSingleDeclaration(
1875 TPublicType &publicType,
1876 const TSourceLoc &identifierOrTypeLocation,
1877 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001878{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001879 TType type(publicType);
1880 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
1881 mDirectiveHandler.pragma().stdgl.invariantAll)
1882 {
1883 TQualifier qualifier = type.getQualifier();
1884
1885 // The directive handler has already taken care of rejecting invalid uses of this pragma
1886 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
1887 // affected variable declarations:
1888 //
1889 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
1890 // elsewhere, in TranslatorGLSL.)
1891 //
1892 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
1893 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
1894 // the way this is currently implemented we have to enable this compiler option before
1895 // parsing the shader and determining the shading language version it uses. If this were
1896 // implemented as a post-pass, the workaround could be more targeted.
1897 //
1898 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
1899 // the specification, but there are desktop OpenGL drivers that expect that this is the
1900 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
1901 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
1902 {
1903 type.setInvariant(true);
1904 }
1905 }
1906
1907 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, type, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001908
Olli Etuahobab4c082015-04-24 16:38:49 +03001909 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03001910
Olli Etuahobab4c082015-04-24 16:38:49 +03001911 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1912
Olli Etuaho13389b62016-10-16 11:48:18 +01001913 TIntermDeclaration *declaration = new TIntermDeclaration();
1914 declaration->setLine(identifierOrTypeLocation);
1915
Olli Etuahobab4c082015-04-24 16:38:49 +03001916 if (emptyDeclaration)
1917 {
Martin Radevb8b01222016-11-20 23:25:53 +02001918 emptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Olli Etuahobab4c082015-04-24 16:38:49 +03001919 }
1920 else
Jamie Madill60ed9812013-06-06 11:56:46 -04001921 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001922 singleDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001923
Olli Etuaho856c4972016-08-08 11:38:39 +03001924 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001925
Olli Etuaho2935c582015-04-08 14:32:06 +03001926 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001927 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04001928
1929 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01001930 {
Jamie Madill60ed9812013-06-06 11:56:46 -04001931 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01001932 }
Jamie Madill60ed9812013-06-06 11:56:46 -04001933 }
1934
Olli Etuaho13389b62016-10-16 11:48:18 +01001935 // We append the symbol even if the declaration is empty, mainly because of struct declarations
1936 // that may just declare a type.
1937 declaration->appendDeclarator(symbol);
1938
1939 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04001940}
1941
Olli Etuaho13389b62016-10-16 11:48:18 +01001942TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
1943 const TSourceLoc &identifierLocation,
1944 const TString &identifier,
1945 const TSourceLoc &indexLocation,
1946 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04001947{
Olli Etuahofa33d582015-04-09 14:33:12 +03001948 mDeferredSingleDeclarationErrorCheck = false;
1949
Olli Etuaho383b7912016-08-05 11:22:59 +03001950 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001951
Olli Etuaho856c4972016-08-08 11:38:39 +03001952 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001953
Olli Etuaho8a176262016-08-16 14:23:01 +03001954 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001955
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03001956 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04001957
Olli Etuaho856c4972016-08-08 11:38:39 +03001958 unsigned int size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuahoe7847b02015-03-16 11:56:12 +02001959 // Make the type an array even if size check failed.
1960 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
1961 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04001962
Olli Etuaho2935c582015-04-08 14:32:06 +03001963 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03001964 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04001965
Olli Etuaho13389b62016-10-16 11:48:18 +01001966 TIntermDeclaration *declaration = new TIntermDeclaration();
1967 declaration->setLine(identifierLocation);
1968
Olli Etuahoe7847b02015-03-16 11:56:12 +02001969 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001970 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01001971 {
Jamie Madill60ed9812013-06-06 11:56:46 -04001972 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01001973 declaration->appendDeclarator(symbol);
1974 }
Jamie Madill60ed9812013-06-06 11:56:46 -04001975
Olli Etuaho13389b62016-10-16 11:48:18 +01001976 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04001977}
1978
Olli Etuaho13389b62016-10-16 11:48:18 +01001979TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
1980 const TSourceLoc &identifierLocation,
1981 const TString &identifier,
1982 const TSourceLoc &initLocation,
1983 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04001984{
Olli Etuahofa33d582015-04-09 14:33:12 +03001985 mDeferredSingleDeclarationErrorCheck = false;
1986
Olli Etuaho383b7912016-08-05 11:22:59 +03001987 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001988
Olli Etuaho13389b62016-10-16 11:48:18 +01001989 TIntermDeclaration *declaration = new TIntermDeclaration();
1990 declaration->setLine(identifierLocation);
1991
1992 TIntermBinary *initNode = nullptr;
1993 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04001994 {
Olli Etuaho13389b62016-10-16 11:48:18 +01001995 if (initNode)
1996 {
1997 declaration->appendDeclarator(initNode);
1998 }
Jamie Madill60ed9812013-06-06 11:56:46 -04001999 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002000 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002001}
2002
Olli Etuaho13389b62016-10-16 11:48:18 +01002003TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Jamie Madillb98c3a82015-07-23 14:26:04 -04002004 TPublicType &publicType,
2005 const TSourceLoc &identifierLocation,
2006 const TString &identifier,
2007 const TSourceLoc &indexLocation,
2008 TIntermTyped *indexExpression,
2009 const TSourceLoc &initLocation,
2010 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002011{
2012 mDeferredSingleDeclarationErrorCheck = false;
2013
Olli Etuaho383b7912016-08-05 11:22:59 +03002014 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002015
Olli Etuaho8a176262016-08-16 14:23:01 +03002016 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002017
2018 TPublicType arrayType(publicType);
2019
Olli Etuaho856c4972016-08-08 11:38:39 +03002020 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002021 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2022 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002023 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002024 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002025 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002026 }
2027 // Make the type an array even if size check failed.
2028 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2029 arrayType.setArraySize(size);
2030
Olli Etuaho13389b62016-10-16 11:48:18 +01002031 TIntermDeclaration *declaration = new TIntermDeclaration();
2032 declaration->setLine(identifierLocation);
2033
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002034 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002035 TIntermBinary *initNode = nullptr;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002036 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
2037 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002038 if (initNode)
2039 {
2040 declaration->appendDeclarator(initNode);
2041 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002042 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002043
2044 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002045}
2046
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002047TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002048 const TTypeQualifierBuilder &typeQualifierBuilder,
2049 const TSourceLoc &identifierLoc,
2050 const TString *identifier,
2051 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002052{
Olli Etuaho613b9592016-09-05 12:05:53 +03002053 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002054
Martin Radev70866b82016-07-22 15:27:42 +03002055 if (!typeQualifier.invariant)
2056 {
2057 error(identifierLoc, "Expected invariant", identifier->c_str());
2058 return nullptr;
2059 }
2060 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2061 {
2062 return nullptr;
2063 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002064 if (!symbol)
2065 {
2066 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002067 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002068 }
Martin Radev70866b82016-07-22 15:27:42 +03002069 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002070 {
Martin Radev70866b82016-07-22 15:27:42 +03002071 error(identifierLoc, "invariant declaration specifies qualifier",
2072 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002073 }
Martin Radev70866b82016-07-22 15:27:42 +03002074 if (typeQualifier.precision != EbpUndefined)
2075 {
2076 error(identifierLoc, "invariant declaration specifies precision",
2077 getPrecisionString(typeQualifier.precision));
2078 }
2079 if (!typeQualifier.layoutQualifier.isEmpty())
2080 {
2081 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2082 }
2083
2084 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
2085 ASSERT(variable);
2086 const TType &type = variable->getType();
2087
2088 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2089 typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002090 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002091
2092 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2093
2094 TIntermSymbol *intermSymbol =
2095 intermediate.addSymbol(variable->getUniqueId(), *identifier, type, identifierLoc);
2096
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002097 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002098}
2099
Olli Etuaho13389b62016-10-16 11:48:18 +01002100void TParseContext::parseDeclarator(TPublicType &publicType,
2101 const TSourceLoc &identifierLocation,
2102 const TString &identifier,
2103 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002104{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002105 // If the declaration starting this declarator list was empty (example: int,), some checks were
2106 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002107 if (mDeferredSingleDeclarationErrorCheck)
2108 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002109 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002110 mDeferredSingleDeclarationErrorCheck = false;
2111 }
2112
Olli Etuaho856c4972016-08-08 11:38:39 +03002113 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002114
Olli Etuaho856c4972016-08-08 11:38:39 +03002115 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002116
Olli Etuaho2935c582015-04-08 14:32:06 +03002117 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002118 declareVariable(identifierLocation, identifier, TType(publicType), &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002119
Jamie Madillb98c3a82015-07-23 14:26:04 -04002120 TIntermSymbol *symbol =
2121 intermediate.addSymbol(0, identifier, TType(publicType), identifierLocation);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002122 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01002123 {
Jamie Madill502d66f2013-06-20 11:55:52 -04002124 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01002125 declarationOut->appendDeclarator(symbol);
2126 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002127}
2128
Olli Etuaho13389b62016-10-16 11:48:18 +01002129void TParseContext::parseArrayDeclarator(TPublicType &publicType,
2130 const TSourceLoc &identifierLocation,
2131 const TString &identifier,
2132 const TSourceLoc &arrayLocation,
2133 TIntermTyped *indexExpression,
2134 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002135{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002136 // If the declaration starting this declarator list was empty (example: int,), some checks were
2137 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002138 if (mDeferredSingleDeclarationErrorCheck)
2139 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002140 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002141 mDeferredSingleDeclarationErrorCheck = false;
2142 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002143
Olli Etuaho856c4972016-08-08 11:38:39 +03002144 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002145
Olli Etuaho856c4972016-08-08 11:38:39 +03002146 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002147
Olli Etuaho8a176262016-08-16 14:23:01 +03002148 if (checkIsValidTypeAndQualifierForArray(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002149 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002150 TType arrayType = TType(publicType);
Olli Etuaho856c4972016-08-08 11:38:39 +03002151 unsigned int size = checkIsValidArraySize(arrayLocation, indexExpression);
Olli Etuaho693c9aa2015-04-07 17:50:36 +03002152 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002153
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002154 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002155 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002156
Jamie Madillb98c3a82015-07-23 14:26:04 -04002157 TIntermSymbol *symbol =
2158 intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002159 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002160 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002161
Olli Etuaho13389b62016-10-16 11:48:18 +01002162 declarationOut->appendDeclarator(symbol);
Jamie Madill502d66f2013-06-20 11:55:52 -04002163 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002164}
2165
Olli Etuaho13389b62016-10-16 11:48:18 +01002166void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2167 const TSourceLoc &identifierLocation,
2168 const TString &identifier,
2169 const TSourceLoc &initLocation,
2170 TIntermTyped *initializer,
2171 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002172{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002173 // If the declaration starting this declarator list was empty (example: int,), some checks were
2174 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002175 if (mDeferredSingleDeclarationErrorCheck)
2176 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002177 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002178 mDeferredSingleDeclarationErrorCheck = false;
2179 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002180
Olli Etuaho856c4972016-08-08 11:38:39 +03002181 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002182
Olli Etuaho13389b62016-10-16 11:48:18 +01002183 TIntermBinary *initNode = nullptr;
2184 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002185 {
2186 //
2187 // build the intermediate representation
2188 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002189 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002190 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002191 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002192 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002193 }
2194}
2195
Olli Etuaho13389b62016-10-16 11:48:18 +01002196void TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
2197 const TSourceLoc &identifierLocation,
2198 const TString &identifier,
2199 const TSourceLoc &indexLocation,
2200 TIntermTyped *indexExpression,
2201 const TSourceLoc &initLocation,
2202 TIntermTyped *initializer,
2203 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002204{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002205 // If the declaration starting this declarator list was empty (example: int,), some checks were
2206 // not performed.
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002207 if (mDeferredSingleDeclarationErrorCheck)
2208 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002209 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002210 mDeferredSingleDeclarationErrorCheck = false;
2211 }
2212
Olli Etuaho856c4972016-08-08 11:38:39 +03002213 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002214
Olli Etuaho8a176262016-08-16 14:23:01 +03002215 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002216
2217 TPublicType arrayType(publicType);
2218
Olli Etuaho856c4972016-08-08 11:38:39 +03002219 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002220 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2221 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002222 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002223 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002224 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002225 }
2226 // Make the type an array even if size check failed.
2227 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2228 arrayType.setArraySize(size);
2229
2230 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002231 TIntermBinary *initNode = nullptr;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002232 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
2233 {
2234 if (initNode)
2235 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002236 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002237 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002238 }
2239}
2240
Martin Radev70866b82016-07-22 15:27:42 +03002241void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002242{
Olli Etuaho613b9592016-09-05 12:05:53 +03002243 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002244 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002245
Martin Radev70866b82016-07-22 15:27:42 +03002246 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2247 typeQualifier.line);
2248
Jamie Madillc2128ff2016-07-04 10:26:17 -04002249 // It should never be the case, but some strange parser errors can send us here.
2250 if (layoutQualifier.isEmpty())
2251 {
2252 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002253 return;
2254 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002255
Martin Radev802abe02016-08-04 17:48:32 +03002256 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002257 {
Martin Radev802abe02016-08-04 17:48:32 +03002258 error(typeQualifier.line, "invalid combination:", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002259 return;
2260 }
2261
Martin Radev2cc85b32016-08-05 16:22:53 +03002262 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
2263
2264 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2265
Martin Radev802abe02016-08-04 17:48:32 +03002266 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002267 {
Martin Radev802abe02016-08-04 17:48:32 +03002268 if (mComputeShaderLocalSizeDeclared &&
2269 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2270 {
2271 error(typeQualifier.line, "Work group size does not match the previous declaration",
2272 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002273 return;
2274 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002275
Martin Radev802abe02016-08-04 17:48:32 +03002276 if (mShaderVersion < 310)
2277 {
2278 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002279 return;
2280 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002281
Martin Radev4c4c8e72016-08-04 12:25:34 +03002282 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03002283 {
2284 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002285 return;
2286 }
2287
2288 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
2289 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
2290
2291 const TConstantUnion *maxComputeWorkGroupSizeData =
2292 maxComputeWorkGroupSize->getConstPointer();
2293
2294 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
2295 {
2296 if (layoutQualifier.localSize[i] != -1)
2297 {
2298 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
2299 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
2300 if (mComputeShaderLocalSize[i] < 1 ||
2301 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
2302 {
2303 std::stringstream errorMessageStream;
2304 errorMessageStream << "Value must be at least 1 and no greater than "
2305 << maxComputeWorkGroupSizeValue;
2306 const std::string &errorMessage = errorMessageStream.str();
2307
Martin Radev4c4c8e72016-08-04 12:25:34 +03002308 error(typeQualifier.line, "invalid value:", getWorkGroupSizeString(i),
Martin Radev802abe02016-08-04 17:48:32 +03002309 errorMessage.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03002310 return;
2311 }
2312 }
2313 }
2314
2315 mComputeShaderLocalSizeDeclared = true;
2316 }
2317 else
Jamie Madill1566ef72013-06-20 11:55:54 -04002318 {
Martin Radev802abe02016-08-04 17:48:32 +03002319
Olli Etuaho8a176262016-08-16 14:23:01 +03002320 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03002321 {
Martin Radev802abe02016-08-04 17:48:32 +03002322 return;
2323 }
2324
2325 if (typeQualifier.qualifier != EvqUniform)
2326 {
2327 error(typeQualifier.line, "invalid qualifier:",
2328 getQualifierString(typeQualifier.qualifier), "global layout must be uniform");
Martin Radev802abe02016-08-04 17:48:32 +03002329 return;
2330 }
2331
2332 if (mShaderVersion < 300)
2333 {
2334 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
2335 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002336 return;
2337 }
2338
Olli Etuaho856c4972016-08-08 11:38:39 +03002339 checkLocationIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002340
2341 if (layoutQualifier.matrixPacking != EmpUnspecified)
2342 {
2343 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
2344 }
2345
2346 if (layoutQualifier.blockStorage != EbsUnspecified)
2347 {
2348 mDefaultBlockStorage = layoutQualifier.blockStorage;
2349 }
Jamie Madill1566ef72013-06-20 11:55:54 -04002350 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002351}
2352
Olli Etuaho476197f2016-10-11 13:59:08 +01002353TIntermAggregate *TParseContext::addFunctionPrototypeDeclaration(const TFunction &parsedFunction,
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002354 const TSourceLoc &location)
2355{
Olli Etuaho476197f2016-10-11 13:59:08 +01002356 // Note: function found from the symbol table could be the same as parsedFunction if this is the
2357 // first declaration. Either way the instance in the symbol table is used to track whether the
2358 // function is declared multiple times.
2359 TFunction *function = static_cast<TFunction *>(
2360 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
2361 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02002362 {
2363 // ESSL 1.00.17 section 4.2.7.
2364 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
2365 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02002366 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002367 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02002368
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002369 TIntermAggregate *prototype = new TIntermAggregate;
Olli Etuaho476197f2016-10-11 13:59:08 +01002370 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
2371 // point to the data that already exists in the symbol table.
2372 prototype->setType(function->getReturnType());
Olli Etuahobd674552016-10-06 13:28:42 +01002373 prototype->getFunctionSymbolInfo()->setFromFunction(*function);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002374
Olli Etuaho476197f2016-10-11 13:59:08 +01002375 for (size_t i = 0; i < function->getParamCount(); i++)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002376 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002377 const TConstParameter &param = function->getParam(i);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002378 if (param.name != 0)
2379 {
2380 TVariable variable(param.name, *param.type);
2381
2382 TIntermSymbol *paramSymbol = intermediate.addSymbol(
2383 variable.getUniqueId(), variable.getName(), variable.getType(), location);
2384 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
2385 }
2386 else
2387 {
2388 TIntermSymbol *paramSymbol = intermediate.addSymbol(0, "", *param.type, location);
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002389 prototype = intermediate.growAggregate(prototype, paramSymbol, location);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002390 }
2391 }
2392
2393 prototype->setOp(EOpPrototype);
2394
2395 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002396
2397 if (!symbolTable.atGlobalLevel())
2398 {
2399 // ESSL 3.00.4 section 4.2.4.
2400 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002401 }
2402
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002403 return prototype;
2404}
2405
Olli Etuaho336b1472016-10-05 16:37:55 +01002406TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
2407 const TFunction &function,
2408 TIntermAggregate *functionParameters,
2409 TIntermBlock *functionBody,
2410 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002411{
Olli Etuahof51fdd22016-10-03 10:03:40 +01002412 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002413 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
2414 {
2415 error(location, "function does not return a value:", "", function.getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002416 }
2417
Olli Etuahof51fdd22016-10-03 10:03:40 +01002418 if (functionBody == nullptr)
2419 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01002420 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002421 functionBody->setLine(location);
2422 }
Olli Etuaho336b1472016-10-05 16:37:55 +01002423 TIntermFunctionDefinition *functionNode =
2424 new TIntermFunctionDefinition(function.getReturnType(), functionParameters, functionBody);
2425 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01002426
Olli Etuahobd674552016-10-06 13:28:42 +01002427 functionNode->getFunctionSymbolInfo()->setFromFunction(function);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002428
2429 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002430 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002431}
2432
Olli Etuaho476197f2016-10-11 13:59:08 +01002433void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
2434 TFunction **function,
2435 TIntermAggregate **aggregateOut)
Jamie Madill185fb402015-06-12 15:48:48 -04002436{
Olli Etuaho476197f2016-10-11 13:59:08 +01002437 ASSERT(function);
2438 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002439 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01002440 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002441
2442 if (builtIn)
2443 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002444 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002445 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002446 else
Jamie Madill185fb402015-06-12 15:48:48 -04002447 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002448 TFunction *prevDec = static_cast<TFunction *>(
2449 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
2450
2451 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
2452 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
2453 // occurance.
2454 if (*function != prevDec)
2455 {
2456 // Swap the parameters of the previous declaration to the parameters of the function
2457 // definition (parameter names may differ).
2458 prevDec->swapParameters(**function);
2459
2460 // The function definition will share the same symbol as any previous declaration.
2461 *function = prevDec;
2462 }
2463
2464 if ((*function)->isDefined())
2465 {
2466 error(location, "function already has a body", (*function)->getName().c_str());
2467 }
2468
2469 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04002470 }
Jamie Madill185fb402015-06-12 15:48:48 -04002471
2472 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuaho476197f2016-10-11 13:59:08 +01002473 if ((*function)->getName() == "main")
Jamie Madill185fb402015-06-12 15:48:48 -04002474 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002475 if ((*function)->getParamCount() > 0)
Jamie Madill185fb402015-06-12 15:48:48 -04002476 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002477 error(location, "function cannot take any parameter(s)",
2478 (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002479 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002480 if ((*function)->getReturnType().getBasicType() != EbtVoid)
Jamie Madill185fb402015-06-12 15:48:48 -04002481 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002482 error(location, "", (*function)->getReturnType().getBasicString(),
Jamie Madillb98c3a82015-07-23 14:26:04 -04002483 "main function cannot return a value");
Jamie Madill185fb402015-06-12 15:48:48 -04002484 }
2485 }
2486
2487 //
2488 // Remember the return type for later checking for RETURN statements.
2489 //
Olli Etuaho476197f2016-10-11 13:59:08 +01002490 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002491 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04002492
2493 //
2494 // Insert parameters into the symbol table.
2495 // If the parameter has no name, it's not an error, just don't insert it
2496 // (could be used for unused args).
2497 //
2498 // Also, accumulate the list of parameters into the HIL, so lower level code
2499 // knows where to find parameters.
2500 //
2501 TIntermAggregate *paramNodes = new TIntermAggregate;
Olli Etuaho476197f2016-10-11 13:59:08 +01002502 for (size_t i = 0; i < (*function)->getParamCount(); i++)
Jamie Madill185fb402015-06-12 15:48:48 -04002503 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002504 const TConstParameter &param = (*function)->getParam(i);
Jamie Madill185fb402015-06-12 15:48:48 -04002505 if (param.name != 0)
2506 {
2507 TVariable *variable = new TVariable(param.name, *param.type);
2508 //
2509 // Insert the parameters with name in the symbol table.
2510 //
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002511 if (!symbolTable.declare(variable))
2512 {
Jamie Madill185fb402015-06-12 15:48:48 -04002513 error(location, "redefinition", variable->getName().c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -04002514 paramNodes = intermediate.growAggregate(
2515 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
2516 continue;
Jamie Madill185fb402015-06-12 15:48:48 -04002517 }
2518
2519 //
2520 // Add the parameter to the HIL
2521 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002522 TIntermSymbol *symbol = intermediate.addSymbol(
2523 variable->getUniqueId(), variable->getName(), variable->getType(), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002524
2525 paramNodes = intermediate.growAggregate(paramNodes, symbol, location);
2526 }
2527 else
2528 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002529 paramNodes = intermediate.growAggregate(
2530 paramNodes, intermediate.addSymbol(0, "", *param.type, location), location);
Jamie Madill185fb402015-06-12 15:48:48 -04002531 }
2532 }
2533 intermediate.setAggregateOperator(paramNodes, EOpParameters, location);
2534 *aggregateOut = paramNodes;
2535 setLoopNestingLevel(0);
2536}
2537
Jamie Madillb98c3a82015-07-23 14:26:04 -04002538TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002539{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002540 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002541 // We don't know at this point whether this is a function definition or a prototype.
2542 // The definition production code will check for redefinitions.
2543 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002544 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002545 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
2546 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002547 //
2548 TFunction *prevDec =
2549 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302550
Martin Radevda6254b2016-12-14 17:00:36 +02002551 if (getShaderVersion() >= 300 &&
2552 symbolTable.hasUnmangledBuiltInForShaderVersion(function->getName().c_str(),
2553 getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302554 {
Martin Radevda6254b2016-12-14 17:00:36 +02002555 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302556 // Therefore overloading or redefining builtin functions is an error.
2557 error(location, "Name of a built-in function cannot be redeclared as function",
2558 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302559 }
2560 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002561 {
2562 if (prevDec->getReturnType() != function->getReturnType())
2563 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002564 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002565 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04002566 }
2567 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2568 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002569 if (prevDec->getParam(i).type->getQualifier() !=
2570 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002571 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002572 error(location,
2573 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002574 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04002575 }
2576 }
2577 }
2578
2579 //
2580 // Check for previously declared variables using the same name.
2581 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002582 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002583 if (prevSym)
2584 {
2585 if (!prevSym->isFunction())
2586 {
2587 error(location, "redefinition", function->getName().c_str(), "function");
Jamie Madill185fb402015-06-12 15:48:48 -04002588 }
2589 }
2590 else
2591 {
2592 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01002593 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04002594 }
2595
2596 // We're at the inner scope level of the function's arguments and body statement.
2597 // Add the function prototype to the surrounding scope instead.
2598 symbolTable.getOuterLevel()->insert(function);
2599
2600 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002601 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2602 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002603 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2604 //
2605 return function;
2606}
2607
Olli Etuaho9de84a52016-06-14 17:36:01 +03002608TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
2609 const TString *name,
2610 const TSourceLoc &location)
2611{
2612 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
2613 {
2614 error(location, "no qualifiers allowed for function return",
2615 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03002616 }
2617 if (!type.layoutQualifier.isEmpty())
2618 {
2619 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03002620 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002621 // make sure a sampler or an image is not involved as well...
Martin Radev4a9cd802016-09-01 16:51:51 +03002622 checkIsNotSampler(location, type.typeSpecifierNonArray,
2623 "samplers can't be function return values");
Martin Radev2cc85b32016-08-05 16:22:53 +03002624 checkIsNotImage(location, type.typeSpecifierNonArray, "images can't be function return values");
Olli Etuahoe29324f2016-06-15 10:58:03 +03002625 if (mShaderVersion < 300)
2626 {
2627 // Array return values are forbidden, but there's also no valid syntax for declaring array
2628 // return values in ESSL 1.00.
2629 ASSERT(type.arraySize == 0 || mDiagnostics.numErrors() > 0);
2630
2631 if (type.isStructureContainingArrays())
2632 {
2633 // ESSL 1.00.17 section 6.1 Function Definitions
2634 error(location, "structures containing arrays can't be function return values",
2635 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03002636 }
2637 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03002638
2639 // Add the function as a prototype after parsing it (we do not support recursion)
2640 return new TFunction(name, new TType(type));
2641}
2642
Jamie Madill06145232015-05-13 13:10:01 -04002643TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002644{
Jamie Madill06145232015-05-13 13:10:01 -04002645 TPublicType publicType = publicTypeIn;
Martin Radev4a9cd802016-09-01 16:51:51 +03002646 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02002647 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002648 error(publicType.getLine(), "constructor can't be a structure definition",
2649 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02002650 }
2651
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002652 TOperator op = EOpNull;
Martin Radev4a9cd802016-09-01 16:51:51 +03002653 if (publicType.getUserDef())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002654 {
2655 op = EOpConstructStruct;
2656 }
2657 else
2658 {
Geoff Lang156d7192016-07-21 16:11:00 -04002659 op = sh::TypeToConstructorOperator(TType(publicType));
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002660 if (op == EOpNull)
2661 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002662 error(publicType.getLine(), "cannot construct this type",
2663 getBasicString(publicType.getBasicType()));
2664 publicType.setBasicType(EbtFloat);
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002665 op = EOpConstructFloat;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002666 }
2667 }
2668
2669 TString tempString;
Dmitry Skiba7f17a502015-06-22 15:08:39 -07002670 const TType *type = new TType(publicType);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002671 return new TFunction(&tempString, type, op);
2672}
2673
Jamie Madillb98c3a82015-07-23 14:26:04 -04002674// This function is used to test for the correctness of the parameters passed to various constructor
2675// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002676//
Olli Etuaho856c4972016-08-08 11:38:39 +03002677// 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 +00002678//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002679TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002680 TOperator op,
2681 TFunction *fnCall,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302682 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002683{
Olli Etuaho856c4972016-08-08 11:38:39 +03002684 TType type = fnCall->getReturnType();
2685 if (type.isUnsizedArray())
2686 {
Olli Etuahobbe9fb52016-11-03 17:16:05 +00002687 if (fnCall->getParamCount() == 0)
2688 {
2689 error(line, "implicitly sized array constructor must have at least one argument", "[]");
2690 type.setArraySize(1u);
2691 return TIntermTyped::CreateZero(type);
2692 }
Olli Etuaho856c4972016-08-08 11:38:39 +03002693 type.setArraySize(static_cast<unsigned int>(fnCall->getParamCount()));
2694 }
2695 bool constType = true;
2696 for (size_t i = 0; i < fnCall->getParamCount(); ++i)
2697 {
2698 const TConstParameter &param = fnCall->getParam(i);
2699 if (param.type->getQualifier() != EvqConst)
2700 constType = false;
2701 }
2702 if (constType)
2703 type.setQualifier(EvqConst);
2704
Olli Etuaho8a176262016-08-16 14:23:01 +03002705 if (!checkConstructorArguments(line, arguments, *fnCall, op, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03002706 {
2707 TIntermTyped *dummyNode = intermediate.setAggregateOperator(nullptr, op, line);
2708 dummyNode->setType(type);
2709 return dummyNode;
2710 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002711 TIntermAggregate *constructor = arguments->getAsAggregate();
2712 ASSERT(constructor != nullptr);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002713
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002714 // Turn the argument list itself into a constructor
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002715 constructor->setOp(op);
2716 constructor->setLine(line);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002717 ASSERT(constructor->isConstructor());
2718
2719 // Need to set type before setPrecisionFromChildren() because bool doesn't have precision.
Olli Etuaho856c4972016-08-08 11:38:39 +03002720 constructor->setType(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002721
Olli Etuaho21203702014-11-13 16:16:21 +02002722 // Structs should not be precision qualified, the individual members may be.
2723 // Built-in types on the other hand should be precision qualified.
2724 if (op != EOpConstructStruct)
2725 {
2726 constructor->setPrecisionFromChildren();
Olli Etuaho856c4972016-08-08 11:38:39 +03002727 type.setPrecision(constructor->getPrecision());
Olli Etuaho21203702014-11-13 16:16:21 +02002728 }
2729
Olli Etuaho856c4972016-08-08 11:38:39 +03002730 constructor->setType(type);
2731
Olli Etuahof119a262016-08-19 15:54:22 +03002732 TIntermTyped *constConstructor = intermediate.foldAggregateBuiltIn(constructor, &mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002733 if (constConstructor)
2734 {
2735 return constConstructor;
2736 }
2737
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002738 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002739}
2740
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002741//
2742// Interface/uniform blocks
2743//
Olli Etuaho13389b62016-10-16 11:48:18 +01002744TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03002745 const TTypeQualifierBuilder &typeQualifierBuilder,
2746 const TSourceLoc &nameLine,
2747 const TString &blockName,
2748 TFieldList *fieldList,
2749 const TString *instanceName,
2750 const TSourceLoc &instanceLine,
2751 TIntermTyped *arrayIndex,
2752 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002753{
Olli Etuaho856c4972016-08-08 11:38:39 +03002754 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002755
Olli Etuaho613b9592016-09-05 12:05:53 +03002756 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03002757
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002758 if (typeQualifier.qualifier != EvqUniform)
2759 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302760 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
2761 "interface blocks must be uniform");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002762 }
2763
Martin Radev70866b82016-07-22 15:27:42 +03002764 if (typeQualifier.invariant)
2765 {
2766 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
2767 }
2768
Martin Radev2cc85b32016-08-05 16:22:53 +03002769 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
2770
Jamie Madill099c0f32013-06-20 11:55:52 -04002771 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03002772 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002773
Jamie Madill099c0f32013-06-20 11:55:52 -04002774 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2775 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002776 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002777 }
2778
Jamie Madill1566ef72013-06-20 11:55:54 -04002779 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2780 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002781 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002782 }
2783
Olli Etuaho856c4972016-08-08 11:38:39 +03002784 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002785
Martin Radev2cc85b32016-08-05 16:22:53 +03002786 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
2787
Arun Patole7e7e68d2015-05-22 12:02:25 +05302788 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2789 if (!symbolTable.declare(blockNameSymbol))
2790 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002791 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002792 }
2793
Jamie Madill98493dd2013-07-08 14:39:03 -04002794 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302795 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2796 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002797 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302798 TType *fieldType = field->type();
2799 if (IsSampler(fieldType->getBasicType()))
2800 {
2801 error(field->line(), "unsupported type", fieldType->getBasicString(),
2802 "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002803 }
2804
Martin Radev2cc85b32016-08-05 16:22:53 +03002805 if (IsImage(fieldType->getBasicType()))
2806 {
2807 error(field->line(), "unsupported type", fieldType->getBasicString(),
2808 "image types are not allowed in interface blocks");
2809 }
2810
Jamie Madill98493dd2013-07-08 14:39:03 -04002811 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002812 switch (qualifier)
2813 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002814 case EvqGlobal:
2815 case EvqUniform:
2816 break;
2817 default:
2818 error(field->line(), "invalid qualifier on interface block member",
2819 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04002820 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002821 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002822
Martin Radev70866b82016-07-22 15:27:42 +03002823 if (fieldType->isInvariant())
2824 {
2825 error(field->line(), "invalid qualifier on interface block member", "invariant");
2826 }
2827
Jamie Madilla5efff92013-06-06 11:56:47 -04002828 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002829 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03002830 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
Jamie Madill099c0f32013-06-20 11:55:52 -04002831
Jamie Madill98493dd2013-07-08 14:39:03 -04002832 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002833 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002834 error(field->line(), "invalid layout qualifier:",
2835 getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04002836 }
2837
Jamie Madill98493dd2013-07-08 14:39:03 -04002838 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002839 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002840 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002841 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002842 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002843 {
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002844 warning(field->line(), "extraneous layout qualifier:",
Jamie Madillb98c3a82015-07-23 14:26:04 -04002845 getMatrixPackingString(fieldLayoutQualifier.matrixPacking),
2846 "only has an effect on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04002847 }
2848
Jamie Madill98493dd2013-07-08 14:39:03 -04002849 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002850 }
2851
Jamie Madill98493dd2013-07-08 14:39:03 -04002852 // add array index
Olli Etuaho856c4972016-08-08 11:38:39 +03002853 unsigned int arraySize = 0;
2854 if (arrayIndex != nullptr)
Jamie Madill98493dd2013-07-08 14:39:03 -04002855 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002856 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04002857 }
2858
Jamie Madillb98c3a82015-07-23 14:26:04 -04002859 TInterfaceBlock *interfaceBlock =
2860 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2861 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
2862 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002863
2864 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04002865 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002866
Jamie Madill98493dd2013-07-08 14:39:03 -04002867 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002868 {
2869 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002870 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2871 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002872 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302873 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002874
2875 // set parent pointer of the field variable
2876 fieldType->setInterfaceBlock(interfaceBlock);
2877
Arun Patole7e7e68d2015-05-22 12:02:25 +05302878 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002879 fieldVariable->setQualifier(typeQualifier.qualifier);
2880
Arun Patole7e7e68d2015-05-22 12:02:25 +05302881 if (!symbolTable.declare(fieldVariable))
2882 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002883 error(field->line(), "redefinition", field->name().c_str(),
2884 "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002885 }
2886 }
2887 }
2888 else
2889 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002890 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03002891
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002892 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302893 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002894 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002895
Arun Patole7e7e68d2015-05-22 12:02:25 +05302896 if (!symbolTable.declare(instanceTypeDef))
2897 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002898 error(instanceLine, "redefinition", instanceName->c_str(),
2899 "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002900 }
2901
Jamie Madillb98c3a82015-07-23 14:26:04 -04002902 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002903 symbolName = instanceTypeDef->getName();
2904 }
2905
Olli Etuaho13389b62016-10-16 11:48:18 +01002906 TIntermSymbol *blockSymbol =
2907 intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line);
2908 TIntermDeclaration *declaration = new TIntermDeclaration();
2909 declaration->appendDeclarator(blockSymbol);
2910 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04002911
2912 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01002913 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002914}
2915
Olli Etuaho383b7912016-08-05 11:22:59 +03002916void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002917{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002918 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002919
2920 // Embedded structure definitions are not supported per GLSL ES spec.
2921 // They aren't allowed in GLSL either, but we need to detect this here
2922 // so we don't rely on the GLSL compiler to catch it.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302923 if (mStructNestingLevel > 1)
2924 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002925 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002926 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002927}
2928
2929void TParseContext::exitStructDeclaration()
2930{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002931 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002932}
2933
Olli Etuaho8a176262016-08-16 14:23:01 +03002934void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002935{
Jamie Madillacb4b812016-11-07 13:50:29 -05002936 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05302937 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002938 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002939 }
2940
Arun Patole7e7e68d2015-05-22 12:02:25 +05302941 if (field.type()->getBasicType() != EbtStruct)
2942 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002943 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002944 }
2945
2946 // We're already inside a structure definition at this point, so add
2947 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302948 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2949 {
Jamie Madill41a49272014-03-18 16:10:13 -04002950 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002951 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
2952 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04002953 std::string reason = reasonStream.str();
2954 error(line, reason.c_str(), field.name().c_str(), "");
Olli Etuaho8a176262016-08-16 14:23:01 +03002955 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002956 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002957}
2958
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002959//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002960// Parse an array index expression
2961//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002962TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
2963 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302964 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002965{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002966 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2967 {
2968 if (baseExpression->getAsSymbolNode())
2969 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302970 error(location, " left of '[' is not of type array, matrix, or vector ",
2971 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002972 }
2973 else
2974 {
2975 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2976 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002977
2978 TConstantUnion *unionArray = new TConstantUnion[1];
2979 unionArray->setFConst(0.0f);
2980 return intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst),
2981 location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002982 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002983
Jamie Madill21c1e452014-12-29 11:33:41 -05002984 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2985
Olli Etuaho36b05142015-11-12 13:10:42 +02002986 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
2987 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
2988 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
2989 // index is a constant expression.
2990 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
2991 {
2992 if (baseExpression->isInterfaceBlock())
2993 {
2994 error(
2995 location, "", "[",
2996 "array indexes for interface blocks arrays must be constant integral expressions");
Olli Etuaho36b05142015-11-12 13:10:42 +02002997 }
2998 else if (baseExpression->getQualifier() == EvqFragmentOut)
2999 {
3000 error(location, "", "[",
3001 "array indexes for fragment outputs must be constant integral expressions");
Olli Etuaho36b05142015-11-12 13:10:42 +02003002 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003003 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3004 {
3005 error(location, "", "[", "array index for gl_FragData must be constant zero");
Olli Etuaho3e960462015-11-12 15:58:39 +02003006 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003007 }
3008
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003009 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003010 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003011 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3012 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3013 // constant fold expressions that are not constant expressions). The most compatible way to
3014 // handle this case is to report a warning instead of an error and force the index to be in
3015 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003016 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003017 int index = indexConstantUnion->getIConst(0);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003018
3019 int safeIndex = -1;
3020
3021 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04003022 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003023 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003024 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003025 if (mShaderSpec == SH_WEBGL2_SPEC)
3026 {
3027 // Error has been already generated if index is not const.
3028 if (indexExpression->getQualifier() == EvqConst)
3029 {
3030 error(location, "", "[",
3031 "array index for gl_FragData must be constant zero");
3032 }
3033 safeIndex = 0;
3034 }
3035 else if (!isExtensionEnabled("GL_EXT_draw_buffers"))
3036 {
3037 outOfRangeError(outOfRangeIndexIsError, location, "", "[",
3038 "array index for gl_FragData must be zero when "
3039 "GL_EXT_draw_buffers is disabled");
3040 safeIndex = 0;
3041 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03003042 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003043 // Only do generic out-of-range check if similar error hasn't already been reported.
3044 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003045 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003046 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3047 baseExpression->getArraySize(),
3048 "array index out of range", "[]");
3049 }
3050 }
3051 else if (baseExpression->isMatrix())
3052 {
3053 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03003054 baseExpression->getType().getCols(),
3055 "matrix field selection out of range", "[]");
Jamie Madill7164cf42013-07-08 13:30:59 -04003056 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003057 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04003058 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003059 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3060 baseExpression->getType().getNominalSize(),
3061 "vector field selection out of range", "[]");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003062 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003063
3064 ASSERT(safeIndex >= 0);
3065 // Data of constant unions can't be changed, because it may be shared with other
3066 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
3067 // sanitized object.
3068 if (safeIndex != index)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003069 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003070 TConstantUnion *safeConstantUnion = new TConstantUnion();
3071 safeConstantUnion->setIConst(safeIndex);
3072 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003073 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003074
3075 return intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location,
3076 &mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003077 }
Jamie Madill7164cf42013-07-08 13:30:59 -04003078 else
3079 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003080 return intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location,
3081 &mDiagnostics);
Jamie Madill7164cf42013-07-08 13:30:59 -04003082 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003083}
3084
Olli Etuaho90892fb2016-07-14 14:44:51 +03003085int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
3086 const TSourceLoc &location,
3087 int index,
3088 int arraySize,
3089 const char *reason,
3090 const char *token)
3091{
3092 if (index >= arraySize || index < 0)
3093 {
3094 std::stringstream extraInfoStream;
3095 extraInfoStream << "'" << index << "'";
3096 std::string extraInfo = extraInfoStream.str();
3097 outOfRangeError(outOfRangeIndexIsError, location, reason, token, extraInfo.c_str());
3098 if (index < 0)
3099 {
3100 return 0;
3101 }
3102 else
3103 {
3104 return arraySize - 1;
3105 }
3106 }
3107 return index;
3108}
3109
Jamie Madillb98c3a82015-07-23 14:26:04 -04003110TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3111 const TSourceLoc &dotLocation,
3112 const TString &fieldString,
3113 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003114{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003115 if (baseExpression->isArray())
3116 {
3117 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003118 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003119 }
3120
3121 if (baseExpression->isVector())
3122 {
3123 TVectorFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003124 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields,
3125 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003126 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003127 fields.num = 1;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003128 fields.offsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003129 }
3130
Olli Etuahob6fa0432016-09-28 16:28:05 +01003131 return TIntermediate::AddSwizzle(baseExpression, fields, dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003132 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003133 else if (baseExpression->getBasicType() == EbtStruct)
3134 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303135 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003136 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003137 {
3138 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003139 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003140 }
3141 else
3142 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003143 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003144 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003145 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003146 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003147 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003148 {
3149 fieldFound = true;
3150 break;
3151 }
3152 }
3153 if (fieldFound)
3154 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003155 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3156 index->setLine(fieldLocation);
3157 return intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index,
3158 dotLocation, &mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003159 }
3160 else
3161 {
3162 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003163 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003164 }
3165 }
3166 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003167 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003168 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303169 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003170 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003171 {
3172 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003173 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003174 }
3175 else
3176 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003177 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003178 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003179 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003180 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003181 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003182 {
3183 fieldFound = true;
3184 break;
3185 }
3186 }
3187 if (fieldFound)
3188 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003189 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3190 index->setLine(fieldLocation);
3191 return intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index,
3192 dotLocation, &mDiagnostics);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003193 }
3194 else
3195 {
3196 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003197 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003198 }
3199 }
3200 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003201 else
3202 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003203 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003204 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03003205 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303206 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003207 }
3208 else
3209 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303210 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003211 " field selection requires structure, vector, or interface block on left hand "
3212 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303213 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003214 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003215 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003216 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003217}
3218
Jamie Madillb98c3a82015-07-23 14:26:04 -04003219TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3220 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003221{
Martin Radev802abe02016-08-04 17:48:32 +03003222 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003223
3224 if (qualifierType == "shared")
3225 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003226 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003227 {
3228 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
3229 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003230 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003231 }
3232 else if (qualifierType == "packed")
3233 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003234 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003235 {
3236 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
3237 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003238 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003239 }
3240 else if (qualifierType == "std140")
3241 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003242 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003243 }
3244 else if (qualifierType == "row_major")
3245 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003246 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003247 }
3248 else if (qualifierType == "column_major")
3249 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003250 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003251 }
3252 else if (qualifierType == "location")
3253 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003254 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
3255 "location requires an argument");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003256 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003257 else if (qualifierType == "rgba32f")
3258 {
3259 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3260 qualifier.imageInternalFormat = EiifRGBA32F;
3261 }
3262 else if (qualifierType == "rgba16f")
3263 {
3264 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3265 qualifier.imageInternalFormat = EiifRGBA16F;
3266 }
3267 else if (qualifierType == "r32f")
3268 {
3269 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3270 qualifier.imageInternalFormat = EiifR32F;
3271 }
3272 else if (qualifierType == "rgba8")
3273 {
3274 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3275 qualifier.imageInternalFormat = EiifRGBA8;
3276 }
3277 else if (qualifierType == "rgba8_snorm")
3278 {
3279 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3280 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
3281 }
3282 else if (qualifierType == "rgba32i")
3283 {
3284 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3285 qualifier.imageInternalFormat = EiifRGBA32I;
3286 }
3287 else if (qualifierType == "rgba16i")
3288 {
3289 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3290 qualifier.imageInternalFormat = EiifRGBA16I;
3291 }
3292 else if (qualifierType == "rgba8i")
3293 {
3294 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3295 qualifier.imageInternalFormat = EiifRGBA8I;
3296 }
3297 else if (qualifierType == "r32i")
3298 {
3299 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3300 qualifier.imageInternalFormat = EiifR32I;
3301 }
3302 else if (qualifierType == "rgba32ui")
3303 {
3304 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3305 qualifier.imageInternalFormat = EiifRGBA32UI;
3306 }
3307 else if (qualifierType == "rgba16ui")
3308 {
3309 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3310 qualifier.imageInternalFormat = EiifRGBA16UI;
3311 }
3312 else if (qualifierType == "rgba8ui")
3313 {
3314 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3315 qualifier.imageInternalFormat = EiifRGBA8UI;
3316 }
3317 else if (qualifierType == "r32ui")
3318 {
3319 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3320 qualifier.imageInternalFormat = EiifR32UI;
3321 }
3322
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003323 else
3324 {
3325 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003326 }
3327
Jamie Madilla5efff92013-06-06 11:56:47 -04003328 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003329}
3330
Martin Radev802abe02016-08-04 17:48:32 +03003331void TParseContext::parseLocalSize(const TString &qualifierType,
3332 const TSourceLoc &qualifierTypeLine,
3333 int intValue,
3334 const TSourceLoc &intValueLine,
3335 const std::string &intValueString,
3336 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03003337 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03003338{
Olli Etuaho856c4972016-08-08 11:38:39 +03003339 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03003340 if (intValue < 1)
3341 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03003342 std::string errorMessage = std::string(getWorkGroupSizeString(index)) + " must be positive";
Martin Radev802abe02016-08-04 17:48:32 +03003343 error(intValueLine, "out of range:", intValueString.c_str(), errorMessage.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003344 }
3345 (*localSize)[index] = intValue;
3346}
3347
Jamie Madillb98c3a82015-07-23 14:26:04 -04003348TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3349 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003350 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303351 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003352{
Martin Radev802abe02016-08-04 17:48:32 +03003353 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003354
Martin Radev802abe02016-08-04 17:48:32 +03003355 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003356
Martin Radev802abe02016-08-04 17:48:32 +03003357 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003358 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003359 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003360 if (intValue < 0)
3361 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003362 error(intValueLine, "out of range:", intValueString.c_str(),
3363 "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003364 }
3365 else
3366 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003367 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03003368 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003369 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003370 }
Martin Radev802abe02016-08-04 17:48:32 +03003371 else if (qualifierType == "local_size_x")
3372 {
3373 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
3374 &qualifier.localSize);
3375 }
3376 else if (qualifierType == "local_size_y")
3377 {
3378 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
3379 &qualifier.localSize);
3380 }
3381 else if (qualifierType == "local_size_z")
3382 {
3383 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
3384 &qualifier.localSize);
3385 }
3386 else
3387 {
3388 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003389 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003390
Jamie Madilla5efff92013-06-06 11:56:47 -04003391 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003392}
3393
Olli Etuaho613b9592016-09-05 12:05:53 +03003394TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
3395{
3396 return new TTypeQualifierBuilder(
3397 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
3398 mShaderVersion);
3399}
3400
Jamie Madillb98c3a82015-07-23 14:26:04 -04003401TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03003402 TLayoutQualifier rightQualifier,
3403 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003404{
Martin Radevc28888b2016-07-22 15:27:42 +03003405 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
3406 &mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003407}
3408
Martin Radev70866b82016-07-22 15:27:42 +03003409TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
3410 const TTypeQualifierBuilder &typeQualifierBuilder,
3411 TPublicType *typeSpecifier,
3412 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003413{
Olli Etuaho613b9592016-09-05 12:05:53 +03003414 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003415
Martin Radev70866b82016-07-22 15:27:42 +03003416 typeSpecifier->qualifier = typeQualifier.qualifier;
3417 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03003418 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03003419 typeSpecifier->invariant = typeQualifier.invariant;
3420 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05303421 {
Martin Radev70866b82016-07-22 15:27:42 +03003422 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003423 }
Martin Radev70866b82016-07-22 15:27:42 +03003424 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003425}
3426
Jamie Madillb98c3a82015-07-23 14:26:04 -04003427TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3428 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003429{
Martin Radev4a9cd802016-09-01 16:51:51 +03003430 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
3431 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03003432
Martin Radev4a9cd802016-09-01 16:51:51 +03003433 checkIsNonVoid(typeSpecifier.getLine(), (*fieldList)[0]->name(), typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003434
Martin Radev4a9cd802016-09-01 16:51:51 +03003435 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003436
Arun Patole7e7e68d2015-05-22 12:02:25 +05303437 for (unsigned int i = 0; i < fieldList->size(); ++i)
3438 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003439 //
3440 // Careful not to replace already known aspects of type, like array-ness
3441 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303442 TType *type = (*fieldList)[i]->type();
Martin Radev4a9cd802016-09-01 16:51:51 +03003443 type->setBasicType(typeSpecifier.getBasicType());
3444 type->setPrimarySize(typeSpecifier.getPrimarySize());
3445 type->setSecondarySize(typeSpecifier.getSecondarySize());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003446 type->setPrecision(typeSpecifier.precision);
3447 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003448 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
Martin Radev2cc85b32016-08-05 16:22:53 +03003449 type->setMemoryQualifier(typeSpecifier.memoryQualifier);
Martin Radev70866b82016-07-22 15:27:42 +03003450 type->setInvariant(typeSpecifier.invariant);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003451
3452 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303453 if (type->isArray())
3454 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003455 checkIsValidTypeForArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003456 }
3457 if (typeSpecifier.array)
Olli Etuaho856c4972016-08-08 11:38:39 +03003458 type->setArraySize(static_cast<unsigned int>(typeSpecifier.arraySize));
Martin Radev4a9cd802016-09-01 16:51:51 +03003459 if (typeSpecifier.getUserDef())
Arun Patole7e7e68d2015-05-22 12:02:25 +05303460 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003461 type->setStruct(typeSpecifier.getUserDef()->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003462 }
3463
Martin Radev4a9cd802016-09-01 16:51:51 +03003464 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*fieldList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003465 }
3466
Jamie Madill98493dd2013-07-08 14:39:03 -04003467 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003468}
3469
Martin Radev4a9cd802016-09-01 16:51:51 +03003470TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
3471 const TSourceLoc &nameLine,
3472 const TString *structName,
3473 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003474{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303475 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003476 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003477
Jamie Madill9b820842015-02-12 10:40:10 -05003478 // Store a bool in the struct if we're at global scope, to allow us to
3479 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05003480 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003481
Jamie Madill98493dd2013-07-08 14:39:03 -04003482 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003483 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003484 checkIsNotReserved(nameLine, *structName);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303485 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
3486 if (!symbolTable.declare(userTypeDef))
3487 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003488 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003489 }
3490 }
3491
3492 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04003493 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003494 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003495 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04003496 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003497 switch (qualifier)
3498 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003499 case EvqGlobal:
3500 case EvqTemporary:
3501 break;
3502 default:
3503 error(field.line(), "invalid qualifier on struct member",
3504 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003505 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003506 }
Martin Radev70866b82016-07-22 15:27:42 +03003507 if (field.type()->isInvariant())
3508 {
3509 error(field.line(), "invalid qualifier on struct member", "invariant");
3510 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003511 if (IsImage(field.type()->getBasicType()))
3512 {
3513 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
3514 }
3515
3516 checkIsMemoryQualifierNotSpecified(field.type()->getMemoryQualifier(), field.line());
Martin Radev70866b82016-07-22 15:27:42 +03003517
3518 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003519 }
3520
Martin Radev4a9cd802016-09-01 16:51:51 +03003521 TTypeSpecifierNonArray typeSpecifierNonArray;
3522 typeSpecifierNonArray.initialize(EbtStruct, structLine);
3523 typeSpecifierNonArray.userDef = structureType;
3524 typeSpecifierNonArray.isStructSpecifier = true;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003525 exitStructDeclaration();
3526
Martin Radev4a9cd802016-09-01 16:51:51 +03003527 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003528}
3529
Jamie Madillb98c3a82015-07-23 14:26:04 -04003530TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003531 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003532 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02003533{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003534 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003535 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02003536 init->isVector())
3537 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003538 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
3539 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003540 return nullptr;
3541 }
3542
Olli Etuahoac5274d2015-02-20 10:19:08 +02003543 if (statementList)
3544 {
3545 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
3546 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02003547 return nullptr;
3548 }
3549 }
3550
Olli Etuahoa3a36662015-02-17 13:46:51 +02003551 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3552 if (node == nullptr)
3553 {
3554 error(loc, "erroneous switch statement", "switch");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003555 return nullptr;
3556 }
3557 return node;
3558}
3559
3560TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3561{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003562 if (mSwitchNestingLevel == 0)
3563 {
3564 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003565 return nullptr;
3566 }
3567 if (condition == nullptr)
3568 {
3569 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003570 return nullptr;
3571 }
3572 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04003573 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02003574 {
3575 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003576 }
3577 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003578 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
3579 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
3580 // fold in case labels.
3581 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02003582 {
3583 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003584 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003585 TIntermCase *node = intermediate.addCase(condition, loc);
3586 if (node == nullptr)
3587 {
3588 error(loc, "erroneous case statement", "case");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003589 return nullptr;
3590 }
3591 return node;
3592}
3593
3594TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3595{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003596 if (mSwitchNestingLevel == 0)
3597 {
3598 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003599 return nullptr;
3600 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003601 TIntermCase *node = intermediate.addCase(nullptr, loc);
3602 if (node == nullptr)
3603 {
3604 error(loc, "erroneous default statement", "default");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003605 return nullptr;
3606 }
3607 return node;
3608}
3609
Jamie Madillb98c3a82015-07-23 14:26:04 -04003610TIntermTyped *TParseContext::createUnaryMath(TOperator op,
3611 TIntermTyped *child,
3612 const TSourceLoc &loc,
3613 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003614{
3615 if (child == nullptr)
3616 {
3617 return nullptr;
3618 }
3619
3620 switch (op)
3621 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003622 case EOpLogicalNot:
3623 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
3624 child->isVector())
3625 {
3626 return nullptr;
3627 }
3628 break;
3629 case EOpBitwiseNot:
3630 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3631 child->isMatrix() || child->isArray())
3632 {
3633 return nullptr;
3634 }
3635 break;
3636 case EOpPostIncrement:
3637 case EOpPreIncrement:
3638 case EOpPostDecrement:
3639 case EOpPreDecrement:
3640 case EOpNegative:
3641 case EOpPositive:
3642 if (child->getBasicType() == EbtStruct || child->getBasicType() == EbtBool ||
Martin Radev2cc85b32016-08-05 16:22:53 +03003643 child->isArray() || IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04003644 {
3645 return nullptr;
3646 }
3647 // Operators for built-ins are already type checked against their prototype.
3648 default:
3649 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003650 }
3651
Olli Etuahof119a262016-08-19 15:54:22 +03003652 TIntermUnary *node = new TIntermUnary(op, child);
3653 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03003654
3655 TIntermTyped *foldedNode = node->fold(&mDiagnostics);
3656 if (foldedNode)
3657 return foldedNode;
3658
3659 return node;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003660}
3661
Olli Etuaho09b22472015-02-11 11:47:26 +02003662TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3663{
Olli Etuahof6c694b2015-03-26 14:50:53 +02003664 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003665 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003666 {
3667 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003668 return child;
3669 }
3670 return node;
3671}
3672
Jamie Madillb98c3a82015-07-23 14:26:04 -04003673TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
3674 TIntermTyped *child,
3675 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003676{
Olli Etuaho856c4972016-08-08 11:38:39 +03003677 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02003678 return addUnaryMath(op, child, loc);
3679}
3680
Jamie Madillb98c3a82015-07-23 14:26:04 -04003681bool TParseContext::binaryOpCommonCheck(TOperator op,
3682 TIntermTyped *left,
3683 TIntermTyped *right,
3684 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003685{
Olli Etuaho244be012016-08-18 15:26:02 +03003686 if (left->getType().getStruct() || right->getType().getStruct())
3687 {
3688 switch (op)
3689 {
3690 case EOpIndexDirectStruct:
3691 ASSERT(left->getType().getStruct());
3692 break;
3693 case EOpEqual:
3694 case EOpNotEqual:
3695 case EOpAssign:
3696 case EOpInitialize:
3697 if (left->getType() != right->getType())
3698 {
3699 return false;
3700 }
3701 break;
3702 default:
3703 error(loc, "Invalid operation for structs", GetOperatorString(op));
3704 return false;
3705 }
3706 }
3707
Olli Etuahod6b14282015-03-17 14:31:35 +02003708 if (left->isArray() || right->isArray())
3709 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003710 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003711 {
3712 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3713 return false;
3714 }
3715
3716 if (left->isArray() != right->isArray())
3717 {
3718 error(loc, "array / non-array mismatch", GetOperatorString(op));
3719 return false;
3720 }
3721
3722 switch (op)
3723 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003724 case EOpEqual:
3725 case EOpNotEqual:
3726 case EOpAssign:
3727 case EOpInitialize:
3728 break;
3729 default:
3730 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3731 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02003732 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003733 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003734 if (left->getArraySize() != right->getArraySize())
3735 {
3736 error(loc, "array size mismatch", GetOperatorString(op));
3737 return false;
3738 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003739 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003740
3741 // Check ops which require integer / ivec parameters
3742 bool isBitShift = false;
3743 switch (op)
3744 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003745 case EOpBitShiftLeft:
3746 case EOpBitShiftRight:
3747 case EOpBitShiftLeftAssign:
3748 case EOpBitShiftRightAssign:
3749 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3750 // check that the basic type is an integer type.
3751 isBitShift = true;
3752 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3753 {
3754 return false;
3755 }
3756 break;
3757 case EOpBitwiseAnd:
3758 case EOpBitwiseXor:
3759 case EOpBitwiseOr:
3760 case EOpBitwiseAndAssign:
3761 case EOpBitwiseXorAssign:
3762 case EOpBitwiseOrAssign:
3763 // It is enough to check the type of only one operand, since later it
3764 // is checked that the operand types match.
3765 if (!IsInteger(left->getBasicType()))
3766 {
3767 return false;
3768 }
3769 break;
3770 default:
3771 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003772 }
3773
3774 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3775 // So the basic type should usually match.
3776 if (!isBitShift && left->getBasicType() != right->getBasicType())
3777 {
3778 return false;
3779 }
3780
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003781 // Check that:
3782 // 1. Type sizes match exactly on ops that require that.
3783 // 2. Restrictions for structs that contain arrays or samplers are respected.
3784 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003785 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003786 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003787 case EOpAssign:
3788 case EOpInitialize:
3789 case EOpEqual:
3790 case EOpNotEqual:
3791 // ESSL 1.00 sections 5.7, 5.8, 5.9
3792 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
3793 {
3794 error(loc, "undefined operation for structs containing arrays",
3795 GetOperatorString(op));
3796 return false;
3797 }
3798 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3799 // we interpret the spec so that this extends to structs containing samplers,
3800 // similarly to ESSL 1.00 spec.
3801 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
3802 left->getType().isStructureContainingSamplers())
3803 {
3804 error(loc, "undefined operation for structs containing samplers",
3805 GetOperatorString(op));
3806 return false;
3807 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003808
3809 if ((op == EOpAssign || op == EOpInitialize) &&
3810 left->getType().isStructureContainingImages())
3811 {
3812 error(loc, "undefined operation for structs containing images",
3813 GetOperatorString(op));
3814 return false;
3815 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003816 case EOpLessThan:
3817 case EOpGreaterThan:
3818 case EOpLessThanEqual:
3819 case EOpGreaterThanEqual:
3820 if ((left->getNominalSize() != right->getNominalSize()) ||
3821 (left->getSecondarySize() != right->getSecondarySize()))
3822 {
3823 return false;
3824 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003825 break;
3826 case EOpAdd:
3827 case EOpSub:
3828 case EOpDiv:
3829 case EOpIMod:
3830 case EOpBitShiftLeft:
3831 case EOpBitShiftRight:
3832 case EOpBitwiseAnd:
3833 case EOpBitwiseXor:
3834 case EOpBitwiseOr:
3835 case EOpAddAssign:
3836 case EOpSubAssign:
3837 case EOpDivAssign:
3838 case EOpIModAssign:
3839 case EOpBitShiftLeftAssign:
3840 case EOpBitShiftRightAssign:
3841 case EOpBitwiseAndAssign:
3842 case EOpBitwiseXorAssign:
3843 case EOpBitwiseOrAssign:
3844 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
3845 {
3846 return false;
3847 }
3848
3849 // Are the sizes compatible?
3850 if (left->getNominalSize() != right->getNominalSize() ||
3851 left->getSecondarySize() != right->getSecondarySize())
3852 {
3853 // If the nominal sizes of operands do not match:
3854 // One of them must be a scalar.
3855 if (!left->isScalar() && !right->isScalar())
3856 return false;
3857
3858 // In the case of compound assignment other than multiply-assign,
3859 // the right side needs to be a scalar. Otherwise a vector/matrix
3860 // would be assigned to a scalar. A scalar can't be shifted by a
3861 // vector either.
3862 if (!right->isScalar() &&
3863 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
3864 return false;
3865 }
3866 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003867 default:
3868 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003869 }
3870
Olli Etuahod6b14282015-03-17 14:31:35 +02003871 return true;
3872}
3873
Olli Etuaho1dded802016-08-18 18:13:13 +03003874bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
3875 const TType &left,
3876 const TType &right)
3877{
3878 switch (op)
3879 {
3880 case EOpMul:
3881 case EOpMulAssign:
3882 return left.getNominalSize() == right.getNominalSize() &&
3883 left.getSecondarySize() == right.getSecondarySize();
3884 case EOpVectorTimesScalar:
3885 return true;
3886 case EOpVectorTimesScalarAssign:
3887 ASSERT(!left.isMatrix() && !right.isMatrix());
3888 return left.isVector() && !right.isVector();
3889 case EOpVectorTimesMatrix:
3890 return left.getNominalSize() == right.getRows();
3891 case EOpVectorTimesMatrixAssign:
3892 ASSERT(!left.isMatrix() && right.isMatrix());
3893 return left.isVector() && left.getNominalSize() == right.getRows() &&
3894 left.getNominalSize() == right.getCols();
3895 case EOpMatrixTimesVector:
3896 return left.getCols() == right.getNominalSize();
3897 case EOpMatrixTimesScalar:
3898 return true;
3899 case EOpMatrixTimesScalarAssign:
3900 ASSERT(left.isMatrix() && !right.isMatrix());
3901 return !right.isVector();
3902 case EOpMatrixTimesMatrix:
3903 return left.getCols() == right.getRows();
3904 case EOpMatrixTimesMatrixAssign:
3905 ASSERT(left.isMatrix() && right.isMatrix());
3906 // We need to check two things:
3907 // 1. The matrix multiplication step is valid.
3908 // 2. The result will have the same number of columns as the lvalue.
3909 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
3910
3911 default:
3912 UNREACHABLE();
3913 return false;
3914 }
3915}
3916
Jamie Madillb98c3a82015-07-23 14:26:04 -04003917TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
3918 TIntermTyped *left,
3919 TIntermTyped *right,
3920 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02003921{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003922 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003923 return nullptr;
3924
Olli Etuahofc1806e2015-03-17 13:03:11 +02003925 switch (op)
3926 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003927 case EOpEqual:
3928 case EOpNotEqual:
3929 break;
3930 case EOpLessThan:
3931 case EOpGreaterThan:
3932 case EOpLessThanEqual:
3933 case EOpGreaterThanEqual:
Olli Etuaho244be012016-08-18 15:26:02 +03003934 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3935 !right->getType().getStruct());
3936 if (left->isMatrix() || left->isVector())
Jamie Madillb98c3a82015-07-23 14:26:04 -04003937 {
3938 return nullptr;
3939 }
3940 break;
3941 case EOpLogicalOr:
3942 case EOpLogicalXor:
3943 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03003944 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3945 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00003946 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04003947 {
3948 return nullptr;
3949 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00003950 // Basic types matching should have been already checked.
3951 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003952 break;
3953 case EOpAdd:
3954 case EOpSub:
3955 case EOpDiv:
3956 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03003957 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3958 !right->getType().getStruct());
3959 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04003960 {
3961 return nullptr;
3962 }
3963 break;
3964 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03003965 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3966 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003967 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03003968 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04003969 {
3970 return nullptr;
3971 }
3972 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003973 default:
3974 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003975 }
3976
Olli Etuaho1dded802016-08-18 18:13:13 +03003977 if (op == EOpMul)
3978 {
3979 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
3980 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
3981 {
3982 return nullptr;
3983 }
3984 }
3985
Olli Etuaho3fdec912016-08-18 15:08:06 +03003986 TIntermBinary *node = new TIntermBinary(op, left, right);
3987 node->setLine(loc);
3988
Olli Etuaho3fdec912016-08-18 15:08:06 +03003989 // See if we can fold constants.
3990 TIntermTyped *foldedNode = node->fold(&mDiagnostics);
3991 if (foldedNode)
3992 return foldedNode;
3993
3994 return node;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003995}
3996
Jamie Madillb98c3a82015-07-23 14:26:04 -04003997TIntermTyped *TParseContext::addBinaryMath(TOperator op,
3998 TIntermTyped *left,
3999 TIntermTyped *right,
4000 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004001{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004002 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004003 if (node == 0)
4004 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004005 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4006 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02004007 return left;
4008 }
4009 return node;
4010}
4011
Jamie Madillb98c3a82015-07-23 14:26:04 -04004012TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
4013 TIntermTyped *left,
4014 TIntermTyped *right,
4015 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004016{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004017 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004018 if (node == 0)
4019 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004020 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4021 right->getCompleteString());
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004022 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02004023 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004024 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
4025 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004026 }
4027 return node;
4028}
4029
Olli Etuaho13389b62016-10-16 11:48:18 +01004030TIntermBinary *TParseContext::createAssign(TOperator op,
4031 TIntermTyped *left,
4032 TIntermTyped *right,
4033 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004034{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004035 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02004036 {
Olli Etuaho1dded802016-08-18 18:13:13 +03004037 if (op == EOpMulAssign)
4038 {
4039 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
4040 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4041 {
4042 return nullptr;
4043 }
4044 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03004045 TIntermBinary *node = new TIntermBinary(op, left, right);
4046 node->setLine(loc);
4047
Olli Etuaho3fdec912016-08-18 15:08:06 +03004048 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02004049 }
4050 return nullptr;
4051}
4052
Jamie Madillb98c3a82015-07-23 14:26:04 -04004053TIntermTyped *TParseContext::addAssign(TOperator op,
4054 TIntermTyped *left,
4055 TIntermTyped *right,
4056 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004057{
4058 TIntermTyped *node = createAssign(op, left, right, loc);
4059 if (node == nullptr)
4060 {
4061 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02004062 return left;
4063 }
4064 return node;
4065}
4066
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004067TIntermTyped *TParseContext::addComma(TIntermTyped *left,
4068 TIntermTyped *right,
4069 const TSourceLoc &loc)
4070{
Corentin Wallez0d959252016-07-12 17:26:32 -04004071 // WebGL2 section 5.26, the following results in an error:
4072 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004073 if (mShaderSpec == SH_WEBGL2_SPEC &&
4074 (left->isArray() || left->getBasicType() == EbtVoid ||
4075 left->getType().isStructureContainingArrays() || right->isArray() ||
4076 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04004077 {
4078 error(loc,
4079 "sequence operator is not allowed for void, arrays, or structs containing arrays",
4080 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04004081 }
4082
Olli Etuaho4db7ded2016-10-13 12:23:11 +01004083 return TIntermediate::AddComma(left, right, loc, mShaderVersion);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004084}
4085
Olli Etuaho49300862015-02-20 14:54:49 +02004086TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
4087{
4088 switch (op)
4089 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004090 case EOpContinue:
4091 if (mLoopNestingLevel <= 0)
4092 {
4093 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004094 }
4095 break;
4096 case EOpBreak:
4097 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
4098 {
4099 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004100 }
4101 break;
4102 case EOpReturn:
4103 if (mCurrentFunctionType->getBasicType() != EbtVoid)
4104 {
4105 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004106 }
4107 break;
4108 default:
4109 // No checks for discard
4110 break;
Olli Etuaho49300862015-02-20 14:54:49 +02004111 }
4112 return intermediate.addBranch(op, loc);
4113}
4114
Jamie Madillb98c3a82015-07-23 14:26:04 -04004115TIntermBranch *TParseContext::addBranch(TOperator op,
4116 TIntermTyped *returnValue,
4117 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02004118{
4119 ASSERT(op == EOpReturn);
4120 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004121 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02004122 {
4123 error(loc, "void function cannot return a value", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02004124 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004125 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02004126 {
4127 error(loc, "function return is not matching type:", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02004128 }
4129 return intermediate.addBranch(op, returnValue, loc);
4130}
4131
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004132void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
4133{
4134 ASSERT(!functionCall->isUserDefined());
Olli Etuahobd674552016-10-06 13:28:42 +01004135 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004136 TIntermNode *offset = nullptr;
4137 TIntermSequence *arguments = functionCall->getSequence();
4138 if (name.compare(0, 16, "texelFetchOffset") == 0 ||
4139 name.compare(0, 16, "textureLodOffset") == 0 ||
4140 name.compare(0, 20, "textureProjLodOffset") == 0 ||
4141 name.compare(0, 17, "textureGradOffset") == 0 ||
4142 name.compare(0, 21, "textureProjGradOffset") == 0)
4143 {
4144 offset = arguments->back();
4145 }
4146 else if (name.compare(0, 13, "textureOffset") == 0 ||
4147 name.compare(0, 17, "textureProjOffset") == 0)
4148 {
4149 // A bias parameter might follow the offset parameter.
4150 ASSERT(arguments->size() >= 3);
4151 offset = (*arguments)[2];
4152 }
4153 if (offset != nullptr)
4154 {
4155 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
4156 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
4157 {
4158 TString unmangledName = TFunction::unmangleName(name);
4159 error(functionCall->getLine(), "Texture offset must be a constant expression",
4160 unmangledName.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004161 }
4162 else
4163 {
4164 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
4165 size_t size = offsetConstantUnion->getType().getObjectSize();
4166 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
4167 for (size_t i = 0u; i < size; ++i)
4168 {
4169 int offsetValue = values[i].getIConst();
4170 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
4171 {
4172 std::stringstream tokenStream;
4173 tokenStream << offsetValue;
4174 std::string token = tokenStream.str();
4175 error(offset->getLine(), "Texture offset value out of valid range",
4176 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004177 }
4178 }
4179 }
4180 }
4181}
4182
Martin Radev2cc85b32016-08-05 16:22:53 +03004183// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
4184void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
4185{
4186 ASSERT(!functionCall->isUserDefined());
4187 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
4188
4189 if (name.compare(0, 5, "image") == 0)
4190 {
4191 TIntermSequence *arguments = functionCall->getSequence();
4192 TIntermNode *imageNode = (*arguments)[0];
4193 TIntermSymbol *imageSymbol = imageNode->getAsSymbolNode();
4194
4195 const TMemoryQualifier &memoryQualifier = imageSymbol->getMemoryQualifier();
4196
4197 if (name.compare(5, 5, "Store") == 0)
4198 {
4199 if (memoryQualifier.readonly)
4200 {
4201 error(imageNode->getLine(),
4202 "'imageStore' cannot be used with images qualified as 'readonly'",
4203 imageSymbol->getSymbol().c_str());
4204 }
4205 }
4206 else if (name.compare(5, 4, "Load") == 0)
4207 {
4208 if (memoryQualifier.writeonly)
4209 {
4210 error(imageNode->getLine(),
4211 "'imageLoad' cannot be used with images qualified as 'writeonly'",
4212 imageSymbol->getSymbol().c_str());
4213 }
4214 }
4215 }
4216}
4217
4218// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
4219void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
4220 const TFunction *functionDefinition,
4221 const TIntermAggregate *functionCall)
4222{
4223 ASSERT(functionCall->isUserDefined());
4224
4225 const TIntermSequence &arguments = *functionCall->getSequence();
4226
4227 ASSERT(functionDefinition->getParamCount() == arguments.size());
4228
4229 for (size_t i = 0; i < arguments.size(); ++i)
4230 {
4231 const TType &functionArgumentType = arguments[i]->getAsTyped()->getType();
4232 const TType &functionParameterType = *functionDefinition->getParam(i).type;
4233 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
4234
4235 if (IsImage(functionArgumentType.getBasicType()))
4236 {
4237 const TMemoryQualifier &functionArgumentMemoryQualifier =
4238 functionArgumentType.getMemoryQualifier();
4239 const TMemoryQualifier &functionParameterMemoryQualifier =
4240 functionParameterType.getMemoryQualifier();
4241 if (functionArgumentMemoryQualifier.readonly &&
4242 !functionParameterMemoryQualifier.readonly)
4243 {
4244 error(functionCall->getLine(),
4245 "Function call discards the 'readonly' qualifier from image",
4246 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4247 }
4248
4249 if (functionArgumentMemoryQualifier.writeonly &&
4250 !functionParameterMemoryQualifier.writeonly)
4251 {
4252 error(functionCall->getLine(),
4253 "Function call discards the 'writeonly' qualifier from image",
4254 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4255 }
Martin Radev049edfa2016-11-11 14:35:37 +02004256
4257 if (functionArgumentMemoryQualifier.coherent &&
4258 !functionParameterMemoryQualifier.coherent)
4259 {
4260 error(functionCall->getLine(),
4261 "Function call discards the 'coherent' qualifier from image",
4262 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4263 }
4264
4265 if (functionArgumentMemoryQualifier.volatileQualifier &&
4266 !functionParameterMemoryQualifier.volatileQualifier)
4267 {
4268 error(functionCall->getLine(),
4269 "Function call discards the 'volatile' qualifier from image",
4270 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4271 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004272 }
4273 }
4274}
4275
Jamie Madillb98c3a82015-07-23 14:26:04 -04004276TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
4277 TIntermNode *paramNode,
4278 TIntermNode *thisNode,
4279 const TSourceLoc &loc,
4280 bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004281{
Jamie Madillb98c3a82015-07-23 14:26:04 -04004282 *fatalError = false;
4283 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004284 TIntermTyped *callNode = nullptr;
4285
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004286 if (thisNode != nullptr)
4287 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004288 TConstantUnion *unionArray = new TConstantUnion[1];
Jamie Madillb98c3a82015-07-23 14:26:04 -04004289 int arraySize = 0;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004290 TIntermTyped *typedThis = thisNode->getAsTyped();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004291 if (fnCall->getName() != "length")
4292 {
4293 error(loc, "invalid method", fnCall->getName().c_str());
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004294 }
4295 else if (paramNode != nullptr)
4296 {
4297 error(loc, "method takes no parameters", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004298 }
4299 else if (typedThis == nullptr || !typedThis->isArray())
4300 {
4301 error(loc, "length can only be called on arrays", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004302 }
4303 else
4304 {
Olli Etuaho96e67382015-04-23 14:27:02 +03004305 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03004306 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004307 {
Olli Etuaho39282e12015-04-23 15:41:48 +03004308 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004309 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03004310 // (func()).length()
4311 // (int[3](0, 1, 2)).length()
Jamie Madillb98c3a82015-07-23 14:26:04 -04004312 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
4313 // expression.
4314 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
4315 // spec section 5.9 which allows "An array, vector or matrix expression with the
4316 // length method applied".
4317 error(loc, "length can only be called on array names, not on array expressions",
4318 "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004319 }
4320 }
Olli Etuaho96e67382015-04-23 14:27:02 +03004321 unionArray->setIConst(arraySize);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004322 callNode =
4323 intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004324 }
4325 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004326 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004327 // Then this should be a constructor.
Olli Etuaho856c4972016-08-08 11:38:39 +03004328 callNode = addConstructor(paramNode, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004329 }
4330 else
4331 {
4332 //
4333 // Not a constructor. Find it in the symbol table.
4334 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05304335 const TFunction *fnCandidate;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004336 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004337 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004338 if (fnCandidate)
4339 {
4340 //
4341 // A declared function.
4342 //
Olli Etuaho383b7912016-08-05 11:22:59 +03004343 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004344 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004345 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004346 }
4347 op = fnCandidate->getBuiltInOp();
4348 if (builtIn && op != EOpNull)
4349 {
4350 //
4351 // A function call mapped to a built-in operation.
4352 //
4353 if (fnCandidate->getParamCount() == 1)
4354 {
4355 //
4356 // Treat it like a built-in unary operator.
4357 //
Olli Etuaho15c2ac32015-11-09 15:51:43 +02004358 TIntermAggregate *paramAgg = paramNode->getAsAggregate();
4359 paramNode = paramAgg->getSequence()->front();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004360 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004361 &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004362 if (callNode == nullptr)
4363 {
4364 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004365 extraInfoStream
4366 << "built in unary operator function. Type: "
4367 << static_cast<TIntermTyped *>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004368 std::string extraInfo = extraInfoStream.str();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004369 error(paramNode->getLine(), " wrong operand type", "Internal Error",
4370 extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004371 *fatalError = true;
4372 return nullptr;
4373 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004374 }
4375 else
4376 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004377 TIntermAggregate *aggregate =
4378 intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004379 aggregate->setType(fnCandidate->getReturnType());
4380 aggregate->setPrecisionFromChildren();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02004381 if (aggregate->areChildrenConstQualified())
4382 {
4383 aggregate->getTypePointer()->setQualifier(EvqConst);
4384 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004385
4386 // Some built-in functions have out parameters too.
4387 functionCallLValueErrorCheck(fnCandidate, aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05304388
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004389 // See if we can constant fold a built-in. Note that this may be possible even
4390 // if it is not const-qualified.
Olli Etuahof119a262016-08-19 15:54:22 +03004391 TIntermTyped *foldedNode =
4392 intermediate.foldAggregateBuiltIn(aggregate, &mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05304393 if (foldedNode)
4394 {
Arun Patole274f0702015-05-05 13:33:30 +05304395 callNode = foldedNode;
4396 }
Olli Etuahob43846e2015-06-02 18:18:57 +03004397 else
4398 {
4399 callNode = aggregate;
4400 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004401 }
4402 }
4403 else
4404 {
4405 // This is a real function call
Jamie Madillb98c3a82015-07-23 14:26:04 -04004406 TIntermAggregate *aggregate =
4407 intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004408 aggregate->setType(fnCandidate->getReturnType());
4409
Jamie Madillb98c3a82015-07-23 14:26:04 -04004410 // this is how we know whether the given function is a builtIn function or a user
4411 // defined function
4412 // if builtIn == false, it's a userDefined -> could be an overloaded
4413 // builtIn function also
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004414 // if builtIn == true, it's definitely a builtIn function with EOpNull
4415 if (!builtIn)
4416 aggregate->setUserDefined();
Olli Etuahobd674552016-10-06 13:28:42 +01004417 aggregate->getFunctionSymbolInfo()->setFromFunction(*fnCandidate);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004418
Olli Etuahobd674552016-10-06 13:28:42 +01004419 // This needs to happen after the function info including name is set
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004420 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004421 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004422 aggregate->setBuiltInFunctionPrecision();
4423
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004424 checkTextureOffsetConst(aggregate);
Martin Radev2cc85b32016-08-05 16:22:53 +03004425
4426 checkImageMemoryAccessForBuiltinFunctions(aggregate);
4427 }
4428 else
4429 {
4430 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, aggregate);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004431 }
4432
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004433 callNode = aggregate;
4434
4435 functionCallLValueErrorCheck(fnCandidate, aggregate);
4436 }
4437 }
4438 else
4439 {
4440 // error message was put out by findFunction()
4441 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004442 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004443 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004444 callNode = intermediate.addConstantUnion(unionArray,
4445 TType(EbtFloat, EbpUndefined, EvqConst), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004446 }
4447 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004448 return callNode;
4449}
4450
Jamie Madillb98c3a82015-07-23 14:26:04 -04004451TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004452 TIntermTyped *trueExpression,
4453 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03004454 const TSourceLoc &loc)
4455{
Olli Etuaho856c4972016-08-08 11:38:39 +03004456 checkIsScalarBool(loc, cond);
Olli Etuaho52901742015-04-15 13:42:45 +03004457
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004458 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03004459 {
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004460 binaryOpError(loc, ":", trueExpression->getCompleteString(),
4461 falseExpression->getCompleteString());
4462 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03004463 }
Olli Etuahode318b22016-10-25 16:18:25 +01004464 if (IsOpaqueType(trueExpression->getBasicType()))
4465 {
4466 // ESSL 1.00 section 4.1.7
4467 // ESSL 3.00 section 4.1.7
4468 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
4469 // Note that structs containing opaque types don't need to be checked as structs are
4470 // forbidden below.
4471 error(loc, "ternary operator is not allowed for opaque types", ":");
4472 return falseExpression;
4473 }
4474
Olli Etuahoa2d53032015-04-15 14:14:44 +03004475 // ESSL1 sections 5.2 and 5.7:
4476 // ESSL3 section 5.7:
4477 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004478 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03004479 {
4480 error(loc, "ternary operator is not allowed for structures or arrays", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004481 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03004482 }
Corentin Wallez0d959252016-07-12 17:26:32 -04004483 // WebGL2 section 5.26, the following results in an error:
4484 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004485 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04004486 {
4487 error(loc, "ternary operator is not allowed for void", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004488 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04004489 }
4490
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004491 return TIntermediate::AddTernarySelection(cond, trueExpression, falseExpression, loc);
Olli Etuaho52901742015-04-15 13:42:45 +03004492}
Olli Etuaho49300862015-02-20 14:54:49 +02004493
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004494//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004495// Parse an array of strings using yyparse.
4496//
4497// Returns 0 for success.
4498//
Jamie Madillb98c3a82015-07-23 14:26:04 -04004499int PaParseStrings(size_t count,
4500 const char *const string[],
4501 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05304502 TParseContext *context)
4503{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004504 if ((count == 0) || (string == NULL))
4505 return 1;
4506
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004507 if (glslang_initialize(context))
4508 return 1;
4509
alokp@chromium.org408c45e2012-04-05 15:54:43 +00004510 int error = glslang_scan(count, string, length, context);
4511 if (!error)
4512 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004513
alokp@chromium.org73bc2982012-06-19 18:48:05 +00004514 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00004515
alokp@chromium.org6b495712012-06-29 00:06:58 +00004516 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004517}
Jamie Madill45bcc782016-11-07 13:58:48 -05004518
4519} // namespace sh