blob: 64302e4f5085489a411c55725bc2e71e4a5309e2 [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
2551 if (getShaderVersion() >= 300 && symbolTable.hasUnmangledBuiltIn(function->getName().c_str()))
2552 {
2553 // With ESSL 3.00, names of built-in functions cannot be redeclared as functions.
2554 // Therefore overloading or redefining builtin functions is an error.
2555 error(location, "Name of a built-in function cannot be redeclared as function",
2556 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302557 }
2558 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002559 {
2560 if (prevDec->getReturnType() != function->getReturnType())
2561 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002562 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002563 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04002564 }
2565 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2566 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002567 if (prevDec->getParam(i).type->getQualifier() !=
2568 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002569 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002570 error(location,
2571 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002572 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04002573 }
2574 }
2575 }
2576
2577 //
2578 // Check for previously declared variables using the same name.
2579 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002580 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002581 if (prevSym)
2582 {
2583 if (!prevSym->isFunction())
2584 {
2585 error(location, "redefinition", function->getName().c_str(), "function");
Jamie Madill185fb402015-06-12 15:48:48 -04002586 }
2587 }
2588 else
2589 {
2590 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01002591 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04002592 }
2593
2594 // We're at the inner scope level of the function's arguments and body statement.
2595 // Add the function prototype to the surrounding scope instead.
2596 symbolTable.getOuterLevel()->insert(function);
2597
2598 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002599 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2600 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002601 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2602 //
2603 return function;
2604}
2605
Olli Etuaho9de84a52016-06-14 17:36:01 +03002606TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
2607 const TString *name,
2608 const TSourceLoc &location)
2609{
2610 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
2611 {
2612 error(location, "no qualifiers allowed for function return",
2613 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03002614 }
2615 if (!type.layoutQualifier.isEmpty())
2616 {
2617 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03002618 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002619 // make sure a sampler or an image is not involved as well...
Martin Radev4a9cd802016-09-01 16:51:51 +03002620 checkIsNotSampler(location, type.typeSpecifierNonArray,
2621 "samplers can't be function return values");
Martin Radev2cc85b32016-08-05 16:22:53 +03002622 checkIsNotImage(location, type.typeSpecifierNonArray, "images can't be function return values");
Olli Etuahoe29324f2016-06-15 10:58:03 +03002623 if (mShaderVersion < 300)
2624 {
2625 // Array return values are forbidden, but there's also no valid syntax for declaring array
2626 // return values in ESSL 1.00.
2627 ASSERT(type.arraySize == 0 || mDiagnostics.numErrors() > 0);
2628
2629 if (type.isStructureContainingArrays())
2630 {
2631 // ESSL 1.00.17 section 6.1 Function Definitions
2632 error(location, "structures containing arrays can't be function return values",
2633 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03002634 }
2635 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03002636
2637 // Add the function as a prototype after parsing it (we do not support recursion)
2638 return new TFunction(name, new TType(type));
2639}
2640
Jamie Madill06145232015-05-13 13:10:01 -04002641TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002642{
Jamie Madill06145232015-05-13 13:10:01 -04002643 TPublicType publicType = publicTypeIn;
Martin Radev4a9cd802016-09-01 16:51:51 +03002644 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02002645 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002646 error(publicType.getLine(), "constructor can't be a structure definition",
2647 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02002648 }
2649
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002650 TOperator op = EOpNull;
Martin Radev4a9cd802016-09-01 16:51:51 +03002651 if (publicType.getUserDef())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002652 {
2653 op = EOpConstructStruct;
2654 }
2655 else
2656 {
Geoff Lang156d7192016-07-21 16:11:00 -04002657 op = sh::TypeToConstructorOperator(TType(publicType));
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002658 if (op == EOpNull)
2659 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002660 error(publicType.getLine(), "cannot construct this type",
2661 getBasicString(publicType.getBasicType()));
2662 publicType.setBasicType(EbtFloat);
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002663 op = EOpConstructFloat;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002664 }
2665 }
2666
2667 TString tempString;
Dmitry Skiba7f17a502015-06-22 15:08:39 -07002668 const TType *type = new TType(publicType);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002669 return new TFunction(&tempString, type, op);
2670}
2671
Jamie Madillb98c3a82015-07-23 14:26:04 -04002672// This function is used to test for the correctness of the parameters passed to various constructor
2673// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002674//
Olli Etuaho856c4972016-08-08 11:38:39 +03002675// 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 +00002676//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002677TIntermTyped *TParseContext::addConstructor(TIntermNode *arguments,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002678 TOperator op,
2679 TFunction *fnCall,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302680 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002681{
Olli Etuaho856c4972016-08-08 11:38:39 +03002682 TType type = fnCall->getReturnType();
2683 if (type.isUnsizedArray())
2684 {
Olli Etuahobbe9fb52016-11-03 17:16:05 +00002685 if (fnCall->getParamCount() == 0)
2686 {
2687 error(line, "implicitly sized array constructor must have at least one argument", "[]");
2688 type.setArraySize(1u);
2689 return TIntermTyped::CreateZero(type);
2690 }
Olli Etuaho856c4972016-08-08 11:38:39 +03002691 type.setArraySize(static_cast<unsigned int>(fnCall->getParamCount()));
2692 }
2693 bool constType = true;
2694 for (size_t i = 0; i < fnCall->getParamCount(); ++i)
2695 {
2696 const TConstParameter &param = fnCall->getParam(i);
2697 if (param.type->getQualifier() != EvqConst)
2698 constType = false;
2699 }
2700 if (constType)
2701 type.setQualifier(EvqConst);
2702
Olli Etuaho8a176262016-08-16 14:23:01 +03002703 if (!checkConstructorArguments(line, arguments, *fnCall, op, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03002704 {
2705 TIntermTyped *dummyNode = intermediate.setAggregateOperator(nullptr, op, line);
2706 dummyNode->setType(type);
2707 return dummyNode;
2708 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002709 TIntermAggregate *constructor = arguments->getAsAggregate();
2710 ASSERT(constructor != nullptr);
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002711
Nicolas Capens6ed8d8a2014-06-11 11:25:20 -04002712 // Turn the argument list itself into a constructor
Olli Etuaho15c2ac32015-11-09 15:51:43 +02002713 constructor->setOp(op);
2714 constructor->setLine(line);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002715 ASSERT(constructor->isConstructor());
2716
2717 // Need to set type before setPrecisionFromChildren() because bool doesn't have precision.
Olli Etuaho856c4972016-08-08 11:38:39 +03002718 constructor->setType(type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002719
Olli Etuaho21203702014-11-13 16:16:21 +02002720 // Structs should not be precision qualified, the individual members may be.
2721 // Built-in types on the other hand should be precision qualified.
2722 if (op != EOpConstructStruct)
2723 {
2724 constructor->setPrecisionFromChildren();
Olli Etuaho856c4972016-08-08 11:38:39 +03002725 type.setPrecision(constructor->getPrecision());
Olli Etuaho21203702014-11-13 16:16:21 +02002726 }
2727
Olli Etuaho856c4972016-08-08 11:38:39 +03002728 constructor->setType(type);
2729
Olli Etuahof119a262016-08-19 15:54:22 +03002730 TIntermTyped *constConstructor = intermediate.foldAggregateBuiltIn(constructor, &mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002731 if (constConstructor)
2732 {
2733 return constConstructor;
2734 }
2735
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002736 return constructor;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002737}
2738
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002739//
2740// Interface/uniform blocks
2741//
Olli Etuaho13389b62016-10-16 11:48:18 +01002742TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03002743 const TTypeQualifierBuilder &typeQualifierBuilder,
2744 const TSourceLoc &nameLine,
2745 const TString &blockName,
2746 TFieldList *fieldList,
2747 const TString *instanceName,
2748 const TSourceLoc &instanceLine,
2749 TIntermTyped *arrayIndex,
2750 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002751{
Olli Etuaho856c4972016-08-08 11:38:39 +03002752 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002753
Olli Etuaho613b9592016-09-05 12:05:53 +03002754 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03002755
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002756 if (typeQualifier.qualifier != EvqUniform)
2757 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302758 error(typeQualifier.line, "invalid qualifier:", getQualifierString(typeQualifier.qualifier),
2759 "interface blocks must be uniform");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002760 }
2761
Martin Radev70866b82016-07-22 15:27:42 +03002762 if (typeQualifier.invariant)
2763 {
2764 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
2765 }
2766
Martin Radev2cc85b32016-08-05 16:22:53 +03002767 checkIsMemoryQualifierNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
2768
Jamie Madill099c0f32013-06-20 11:55:52 -04002769 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03002770 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002771
Jamie Madill099c0f32013-06-20 11:55:52 -04002772 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2773 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002774 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002775 }
2776
Jamie Madill1566ef72013-06-20 11:55:54 -04002777 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2778 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002779 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002780 }
2781
Olli Etuaho856c4972016-08-08 11:38:39 +03002782 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002783
Martin Radev2cc85b32016-08-05 16:22:53 +03002784 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
2785
Arun Patole7e7e68d2015-05-22 12:02:25 +05302786 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2787 if (!symbolTable.declare(blockNameSymbol))
2788 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002789 error(nameLine, "redefinition", blockName.c_str(), "interface block name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002790 }
2791
Jamie Madill98493dd2013-07-08 14:39:03 -04002792 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302793 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2794 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002795 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302796 TType *fieldType = field->type();
2797 if (IsSampler(fieldType->getBasicType()))
2798 {
2799 error(field->line(), "unsupported type", fieldType->getBasicString(),
2800 "sampler types are not allowed in interface blocks");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002801 }
2802
Martin Radev2cc85b32016-08-05 16:22:53 +03002803 if (IsImage(fieldType->getBasicType()))
2804 {
2805 error(field->line(), "unsupported type", fieldType->getBasicString(),
2806 "image types are not allowed in interface blocks");
2807 }
2808
Jamie Madill98493dd2013-07-08 14:39:03 -04002809 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002810 switch (qualifier)
2811 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002812 case EvqGlobal:
2813 case EvqUniform:
2814 break;
2815 default:
2816 error(field->line(), "invalid qualifier on interface block member",
2817 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04002818 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002819 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002820
Martin Radev70866b82016-07-22 15:27:42 +03002821 if (fieldType->isInvariant())
2822 {
2823 error(field->line(), "invalid qualifier on interface block member", "invariant");
2824 }
2825
Jamie Madilla5efff92013-06-06 11:56:47 -04002826 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002827 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03002828 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
Jamie Madill099c0f32013-06-20 11:55:52 -04002829
Jamie Madill98493dd2013-07-08 14:39:03 -04002830 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002831 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002832 error(field->line(), "invalid layout qualifier:",
2833 getBlockStorageString(fieldLayoutQualifier.blockStorage), "cannot be used here");
Jamie Madill1566ef72013-06-20 11:55:54 -04002834 }
2835
Jamie Madill98493dd2013-07-08 14:39:03 -04002836 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002837 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002838 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002839 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002840 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002841 {
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002842 warning(field->line(), "extraneous layout qualifier:",
Jamie Madillb98c3a82015-07-23 14:26:04 -04002843 getMatrixPackingString(fieldLayoutQualifier.matrixPacking),
2844 "only has an effect on matrix types");
Jamie Madill099c0f32013-06-20 11:55:52 -04002845 }
2846
Jamie Madill98493dd2013-07-08 14:39:03 -04002847 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002848 }
2849
Jamie Madill98493dd2013-07-08 14:39:03 -04002850 // add array index
Olli Etuaho856c4972016-08-08 11:38:39 +03002851 unsigned int arraySize = 0;
2852 if (arrayIndex != nullptr)
Jamie Madill98493dd2013-07-08 14:39:03 -04002853 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002854 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04002855 }
2856
Jamie Madillb98c3a82015-07-23 14:26:04 -04002857 TInterfaceBlock *interfaceBlock =
2858 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2859 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
2860 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002861
2862 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04002863 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002864
Jamie Madill98493dd2013-07-08 14:39:03 -04002865 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002866 {
2867 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002868 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2869 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002870 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302871 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002872
2873 // set parent pointer of the field variable
2874 fieldType->setInterfaceBlock(interfaceBlock);
2875
Arun Patole7e7e68d2015-05-22 12:02:25 +05302876 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002877 fieldVariable->setQualifier(typeQualifier.qualifier);
2878
Arun Patole7e7e68d2015-05-22 12:02:25 +05302879 if (!symbolTable.declare(fieldVariable))
2880 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002881 error(field->line(), "redefinition", field->name().c_str(),
2882 "interface block member name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002883 }
2884 }
2885 }
2886 else
2887 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002888 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03002889
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002890 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302891 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002892 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002893
Arun Patole7e7e68d2015-05-22 12:02:25 +05302894 if (!symbolTable.declare(instanceTypeDef))
2895 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002896 error(instanceLine, "redefinition", instanceName->c_str(),
2897 "interface block instance name");
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002898 }
2899
Jamie Madillb98c3a82015-07-23 14:26:04 -04002900 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002901 symbolName = instanceTypeDef->getName();
2902 }
2903
Olli Etuaho13389b62016-10-16 11:48:18 +01002904 TIntermSymbol *blockSymbol =
2905 intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line);
2906 TIntermDeclaration *declaration = new TIntermDeclaration();
2907 declaration->appendDeclarator(blockSymbol);
2908 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04002909
2910 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01002911 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002912}
2913
Olli Etuaho383b7912016-08-05 11:22:59 +03002914void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002915{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002916 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002917
2918 // Embedded structure definitions are not supported per GLSL ES spec.
2919 // They aren't allowed in GLSL either, but we need to detect this here
2920 // so we don't rely on the GLSL compiler to catch it.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302921 if (mStructNestingLevel > 1)
2922 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +00002923 error(line, "", "Embedded struct definitions are not allowed");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002924 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002925}
2926
2927void TParseContext::exitStructDeclaration()
2928{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002929 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002930}
2931
Olli Etuaho8a176262016-08-16 14:23:01 +03002932void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002933{
Jamie Madillacb4b812016-11-07 13:50:29 -05002934 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05302935 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002936 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002937 }
2938
Arun Patole7e7e68d2015-05-22 12:02:25 +05302939 if (field.type()->getBasicType() != EbtStruct)
2940 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002941 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002942 }
2943
2944 // We're already inside a structure definition at this point, so add
2945 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302946 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
2947 {
Jamie Madill41a49272014-03-18 16:10:13 -04002948 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002949 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
2950 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04002951 std::string reason = reasonStream.str();
2952 error(line, reason.c_str(), field.name().c_str(), "");
Olli Etuaho8a176262016-08-16 14:23:01 +03002953 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002954 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002955}
2956
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00002957//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002958// Parse an array index expression
2959//
Jamie Madillb98c3a82015-07-23 14:26:04 -04002960TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
2961 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302962 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002963{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002964 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
2965 {
2966 if (baseExpression->getAsSymbolNode())
2967 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05302968 error(location, " left of '[' is not of type array, matrix, or vector ",
2969 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002970 }
2971 else
2972 {
2973 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
2974 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03002975
2976 TConstantUnion *unionArray = new TConstantUnion[1];
2977 unionArray->setFConst(0.0f);
2978 return intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst),
2979 location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00002980 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00002981
Jamie Madill21c1e452014-12-29 11:33:41 -05002982 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
2983
Olli Etuaho36b05142015-11-12 13:10:42 +02002984 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
2985 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
2986 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
2987 // index is a constant expression.
2988 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
2989 {
2990 if (baseExpression->isInterfaceBlock())
2991 {
2992 error(
2993 location, "", "[",
2994 "array indexes for interface blocks arrays must be constant integral expressions");
Olli Etuaho36b05142015-11-12 13:10:42 +02002995 }
2996 else if (baseExpression->getQualifier() == EvqFragmentOut)
2997 {
2998 error(location, "", "[",
2999 "array indexes for fragment outputs must be constant integral expressions");
Olli Etuaho36b05142015-11-12 13:10:42 +02003000 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003001 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3002 {
3003 error(location, "", "[", "array index for gl_FragData must be constant zero");
Olli Etuaho3e960462015-11-12 15:58:39 +02003004 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003005 }
3006
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003007 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003008 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003009 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3010 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3011 // constant fold expressions that are not constant expressions). The most compatible way to
3012 // handle this case is to report a warning instead of an error and force the index to be in
3013 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003014 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003015 int index = indexConstantUnion->getIConst(0);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003016
3017 int safeIndex = -1;
3018
3019 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04003020 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003021 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003022 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003023 if (mShaderSpec == SH_WEBGL2_SPEC)
3024 {
3025 // Error has been already generated if index is not const.
3026 if (indexExpression->getQualifier() == EvqConst)
3027 {
3028 error(location, "", "[",
3029 "array index for gl_FragData must be constant zero");
3030 }
3031 safeIndex = 0;
3032 }
3033 else if (!isExtensionEnabled("GL_EXT_draw_buffers"))
3034 {
3035 outOfRangeError(outOfRangeIndexIsError, location, "", "[",
3036 "array index for gl_FragData must be zero when "
3037 "GL_EXT_draw_buffers is disabled");
3038 safeIndex = 0;
3039 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03003040 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003041 // Only do generic out-of-range check if similar error hasn't already been reported.
3042 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003043 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003044 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3045 baseExpression->getArraySize(),
3046 "array index out of range", "[]");
3047 }
3048 }
3049 else if (baseExpression->isMatrix())
3050 {
3051 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03003052 baseExpression->getType().getCols(),
3053 "matrix field selection out of range", "[]");
Jamie Madill7164cf42013-07-08 13:30:59 -04003054 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003055 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04003056 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003057 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3058 baseExpression->getType().getNominalSize(),
3059 "vector field selection out of range", "[]");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003060 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003061
3062 ASSERT(safeIndex >= 0);
3063 // Data of constant unions can't be changed, because it may be shared with other
3064 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
3065 // sanitized object.
3066 if (safeIndex != index)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003067 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003068 TConstantUnion *safeConstantUnion = new TConstantUnion();
3069 safeConstantUnion->setIConst(safeIndex);
3070 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003071 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003072
3073 return intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location,
3074 &mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003075 }
Jamie Madill7164cf42013-07-08 13:30:59 -04003076 else
3077 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003078 return intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location,
3079 &mDiagnostics);
Jamie Madill7164cf42013-07-08 13:30:59 -04003080 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003081}
3082
Olli Etuaho90892fb2016-07-14 14:44:51 +03003083int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
3084 const TSourceLoc &location,
3085 int index,
3086 int arraySize,
3087 const char *reason,
3088 const char *token)
3089{
3090 if (index >= arraySize || index < 0)
3091 {
3092 std::stringstream extraInfoStream;
3093 extraInfoStream << "'" << index << "'";
3094 std::string extraInfo = extraInfoStream.str();
3095 outOfRangeError(outOfRangeIndexIsError, location, reason, token, extraInfo.c_str());
3096 if (index < 0)
3097 {
3098 return 0;
3099 }
3100 else
3101 {
3102 return arraySize - 1;
3103 }
3104 }
3105 return index;
3106}
3107
Jamie Madillb98c3a82015-07-23 14:26:04 -04003108TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3109 const TSourceLoc &dotLocation,
3110 const TString &fieldString,
3111 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003112{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003113 if (baseExpression->isArray())
3114 {
3115 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003116 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003117 }
3118
3119 if (baseExpression->isVector())
3120 {
3121 TVectorFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003122 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields,
3123 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003124 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003125 fields.num = 1;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003126 fields.offsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003127 }
3128
Olli Etuahob6fa0432016-09-28 16:28:05 +01003129 return TIntermediate::AddSwizzle(baseExpression, fields, dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003130 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003131 else if (baseExpression->getBasicType() == EbtStruct)
3132 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303133 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003134 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003135 {
3136 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003137 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003138 }
3139 else
3140 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003141 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003142 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003143 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003144 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003145 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003146 {
3147 fieldFound = true;
3148 break;
3149 }
3150 }
3151 if (fieldFound)
3152 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003153 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3154 index->setLine(fieldLocation);
3155 return intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index,
3156 dotLocation, &mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003157 }
3158 else
3159 {
3160 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003161 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003162 }
3163 }
3164 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003165 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003166 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303167 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003168 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003169 {
3170 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003171 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003172 }
3173 else
3174 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003175 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003176 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003177 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003178 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003179 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003180 {
3181 fieldFound = true;
3182 break;
3183 }
3184 }
3185 if (fieldFound)
3186 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003187 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3188 index->setLine(fieldLocation);
3189 return intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index,
3190 dotLocation, &mDiagnostics);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003191 }
3192 else
3193 {
3194 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003195 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003196 }
3197 }
3198 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003199 else
3200 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003201 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003202 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03003203 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303204 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003205 }
3206 else
3207 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303208 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003209 " field selection requires structure, vector, or interface block on left hand "
3210 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303211 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003212 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003213 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003214 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003215}
3216
Jamie Madillb98c3a82015-07-23 14:26:04 -04003217TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3218 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003219{
Martin Radev802abe02016-08-04 17:48:32 +03003220 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003221
3222 if (qualifierType == "shared")
3223 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003224 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003225 {
3226 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
3227 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003228 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003229 }
3230 else if (qualifierType == "packed")
3231 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003232 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003233 {
3234 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
3235 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003236 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003237 }
3238 else if (qualifierType == "std140")
3239 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003240 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003241 }
3242 else if (qualifierType == "row_major")
3243 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003244 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003245 }
3246 else if (qualifierType == "column_major")
3247 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003248 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003249 }
3250 else if (qualifierType == "location")
3251 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003252 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str(),
3253 "location requires an argument");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003254 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003255 else if (qualifierType == "rgba32f")
3256 {
3257 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3258 qualifier.imageInternalFormat = EiifRGBA32F;
3259 }
3260 else if (qualifierType == "rgba16f")
3261 {
3262 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3263 qualifier.imageInternalFormat = EiifRGBA16F;
3264 }
3265 else if (qualifierType == "r32f")
3266 {
3267 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3268 qualifier.imageInternalFormat = EiifR32F;
3269 }
3270 else if (qualifierType == "rgba8")
3271 {
3272 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3273 qualifier.imageInternalFormat = EiifRGBA8;
3274 }
3275 else if (qualifierType == "rgba8_snorm")
3276 {
3277 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3278 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
3279 }
3280 else if (qualifierType == "rgba32i")
3281 {
3282 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3283 qualifier.imageInternalFormat = EiifRGBA32I;
3284 }
3285 else if (qualifierType == "rgba16i")
3286 {
3287 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3288 qualifier.imageInternalFormat = EiifRGBA16I;
3289 }
3290 else if (qualifierType == "rgba8i")
3291 {
3292 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3293 qualifier.imageInternalFormat = EiifRGBA8I;
3294 }
3295 else if (qualifierType == "r32i")
3296 {
3297 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3298 qualifier.imageInternalFormat = EiifR32I;
3299 }
3300 else if (qualifierType == "rgba32ui")
3301 {
3302 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3303 qualifier.imageInternalFormat = EiifRGBA32UI;
3304 }
3305 else if (qualifierType == "rgba16ui")
3306 {
3307 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3308 qualifier.imageInternalFormat = EiifRGBA16UI;
3309 }
3310 else if (qualifierType == "rgba8ui")
3311 {
3312 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3313 qualifier.imageInternalFormat = EiifRGBA8UI;
3314 }
3315 else if (qualifierType == "r32ui")
3316 {
3317 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3318 qualifier.imageInternalFormat = EiifR32UI;
3319 }
3320
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003321 else
3322 {
3323 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003324 }
3325
Jamie Madilla5efff92013-06-06 11:56:47 -04003326 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003327}
3328
Martin Radev802abe02016-08-04 17:48:32 +03003329void TParseContext::parseLocalSize(const TString &qualifierType,
3330 const TSourceLoc &qualifierTypeLine,
3331 int intValue,
3332 const TSourceLoc &intValueLine,
3333 const std::string &intValueString,
3334 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03003335 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03003336{
Olli Etuaho856c4972016-08-08 11:38:39 +03003337 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03003338 if (intValue < 1)
3339 {
Martin Radev4c4c8e72016-08-04 12:25:34 +03003340 std::string errorMessage = std::string(getWorkGroupSizeString(index)) + " must be positive";
Martin Radev802abe02016-08-04 17:48:32 +03003341 error(intValueLine, "out of range:", intValueString.c_str(), errorMessage.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003342 }
3343 (*localSize)[index] = intValue;
3344}
3345
Jamie Madillb98c3a82015-07-23 14:26:04 -04003346TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3347 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003348 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303349 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003350{
Martin Radev802abe02016-08-04 17:48:32 +03003351 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003352
Martin Radev802abe02016-08-04 17:48:32 +03003353 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003354
Martin Radev802abe02016-08-04 17:48:32 +03003355 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003356 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003357 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003358 if (intValue < 0)
3359 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003360 error(intValueLine, "out of range:", intValueString.c_str(),
3361 "location must be non-negative");
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003362 }
3363 else
3364 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003365 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03003366 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003367 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003368 }
Martin Radev802abe02016-08-04 17:48:32 +03003369 else if (qualifierType == "local_size_x")
3370 {
3371 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
3372 &qualifier.localSize);
3373 }
3374 else if (qualifierType == "local_size_y")
3375 {
3376 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
3377 &qualifier.localSize);
3378 }
3379 else if (qualifierType == "local_size_z")
3380 {
3381 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
3382 &qualifier.localSize);
3383 }
3384 else
3385 {
3386 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003387 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003388
Jamie Madilla5efff92013-06-06 11:56:47 -04003389 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003390}
3391
Olli Etuaho613b9592016-09-05 12:05:53 +03003392TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
3393{
3394 return new TTypeQualifierBuilder(
3395 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
3396 mShaderVersion);
3397}
3398
Jamie Madillb98c3a82015-07-23 14:26:04 -04003399TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03003400 TLayoutQualifier rightQualifier,
3401 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003402{
Martin Radevc28888b2016-07-22 15:27:42 +03003403 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
3404 &mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003405}
3406
Martin Radev70866b82016-07-22 15:27:42 +03003407TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
3408 const TTypeQualifierBuilder &typeQualifierBuilder,
3409 TPublicType *typeSpecifier,
3410 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003411{
Olli Etuaho613b9592016-09-05 12:05:53 +03003412 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(&mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003413
Martin Radev70866b82016-07-22 15:27:42 +03003414 typeSpecifier->qualifier = typeQualifier.qualifier;
3415 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03003416 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03003417 typeSpecifier->invariant = typeQualifier.invariant;
3418 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05303419 {
Martin Radev70866b82016-07-22 15:27:42 +03003420 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003421 }
Martin Radev70866b82016-07-22 15:27:42 +03003422 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003423}
3424
Jamie Madillb98c3a82015-07-23 14:26:04 -04003425TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3426 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003427{
Martin Radev4a9cd802016-09-01 16:51:51 +03003428 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
3429 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03003430
Martin Radev4a9cd802016-09-01 16:51:51 +03003431 checkIsNonVoid(typeSpecifier.getLine(), (*fieldList)[0]->name(), typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003432
Martin Radev4a9cd802016-09-01 16:51:51 +03003433 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003434
Arun Patole7e7e68d2015-05-22 12:02:25 +05303435 for (unsigned int i = 0; i < fieldList->size(); ++i)
3436 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003437 //
3438 // Careful not to replace already known aspects of type, like array-ness
3439 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303440 TType *type = (*fieldList)[i]->type();
Martin Radev4a9cd802016-09-01 16:51:51 +03003441 type->setBasicType(typeSpecifier.getBasicType());
3442 type->setPrimarySize(typeSpecifier.getPrimarySize());
3443 type->setSecondarySize(typeSpecifier.getSecondarySize());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003444 type->setPrecision(typeSpecifier.precision);
3445 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003446 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
Martin Radev2cc85b32016-08-05 16:22:53 +03003447 type->setMemoryQualifier(typeSpecifier.memoryQualifier);
Martin Radev70866b82016-07-22 15:27:42 +03003448 type->setInvariant(typeSpecifier.invariant);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003449
3450 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303451 if (type->isArray())
3452 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003453 checkIsValidTypeForArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003454 }
3455 if (typeSpecifier.array)
Olli Etuaho856c4972016-08-08 11:38:39 +03003456 type->setArraySize(static_cast<unsigned int>(typeSpecifier.arraySize));
Martin Radev4a9cd802016-09-01 16:51:51 +03003457 if (typeSpecifier.getUserDef())
Arun Patole7e7e68d2015-05-22 12:02:25 +05303458 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003459 type->setStruct(typeSpecifier.getUserDef()->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003460 }
3461
Martin Radev4a9cd802016-09-01 16:51:51 +03003462 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*fieldList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003463 }
3464
Jamie Madill98493dd2013-07-08 14:39:03 -04003465 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003466}
3467
Martin Radev4a9cd802016-09-01 16:51:51 +03003468TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
3469 const TSourceLoc &nameLine,
3470 const TString *structName,
3471 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003472{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303473 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003474 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003475
Jamie Madill9b820842015-02-12 10:40:10 -05003476 // Store a bool in the struct if we're at global scope, to allow us to
3477 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05003478 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003479
Jamie Madill98493dd2013-07-08 14:39:03 -04003480 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003481 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003482 checkIsNotReserved(nameLine, *structName);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303483 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
3484 if (!symbolTable.declare(userTypeDef))
3485 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003486 error(nameLine, "redefinition", structName->c_str(), "struct");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003487 }
3488 }
3489
3490 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04003491 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003492 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003493 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04003494 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003495 switch (qualifier)
3496 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003497 case EvqGlobal:
3498 case EvqTemporary:
3499 break;
3500 default:
3501 error(field.line(), "invalid qualifier on struct member",
3502 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003503 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003504 }
Martin Radev70866b82016-07-22 15:27:42 +03003505 if (field.type()->isInvariant())
3506 {
3507 error(field.line(), "invalid qualifier on struct member", "invariant");
3508 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003509 if (IsImage(field.type()->getBasicType()))
3510 {
3511 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
3512 }
3513
3514 checkIsMemoryQualifierNotSpecified(field.type()->getMemoryQualifier(), field.line());
Martin Radev70866b82016-07-22 15:27:42 +03003515
3516 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003517 }
3518
Martin Radev4a9cd802016-09-01 16:51:51 +03003519 TTypeSpecifierNonArray typeSpecifierNonArray;
3520 typeSpecifierNonArray.initialize(EbtStruct, structLine);
3521 typeSpecifierNonArray.userDef = structureType;
3522 typeSpecifierNonArray.isStructSpecifier = true;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003523 exitStructDeclaration();
3524
Martin Radev4a9cd802016-09-01 16:51:51 +03003525 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003526}
3527
Jamie Madillb98c3a82015-07-23 14:26:04 -04003528TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003529 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003530 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02003531{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003532 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003533 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02003534 init->isVector())
3535 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003536 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
3537 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003538 return nullptr;
3539 }
3540
Olli Etuahoac5274d2015-02-20 10:19:08 +02003541 if (statementList)
3542 {
3543 if (!ValidateSwitch::validate(switchType, this, statementList, loc))
3544 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02003545 return nullptr;
3546 }
3547 }
3548
Olli Etuahoa3a36662015-02-17 13:46:51 +02003549 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3550 if (node == nullptr)
3551 {
3552 error(loc, "erroneous switch statement", "switch");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003553 return nullptr;
3554 }
3555 return node;
3556}
3557
3558TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3559{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003560 if (mSwitchNestingLevel == 0)
3561 {
3562 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003563 return nullptr;
3564 }
3565 if (condition == nullptr)
3566 {
3567 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003568 return nullptr;
3569 }
3570 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04003571 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02003572 {
3573 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003574 }
3575 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003576 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
3577 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
3578 // fold in case labels.
3579 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02003580 {
3581 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003582 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003583 TIntermCase *node = intermediate.addCase(condition, loc);
3584 if (node == nullptr)
3585 {
3586 error(loc, "erroneous case statement", "case");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003587 return nullptr;
3588 }
3589 return node;
3590}
3591
3592TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3593{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003594 if (mSwitchNestingLevel == 0)
3595 {
3596 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003597 return nullptr;
3598 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003599 TIntermCase *node = intermediate.addCase(nullptr, loc);
3600 if (node == nullptr)
3601 {
3602 error(loc, "erroneous default statement", "default");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003603 return nullptr;
3604 }
3605 return node;
3606}
3607
Jamie Madillb98c3a82015-07-23 14:26:04 -04003608TIntermTyped *TParseContext::createUnaryMath(TOperator op,
3609 TIntermTyped *child,
3610 const TSourceLoc &loc,
3611 const TType *funcReturnType)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003612{
3613 if (child == nullptr)
3614 {
3615 return nullptr;
3616 }
3617
3618 switch (op)
3619 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003620 case EOpLogicalNot:
3621 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
3622 child->isVector())
3623 {
3624 return nullptr;
3625 }
3626 break;
3627 case EOpBitwiseNot:
3628 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3629 child->isMatrix() || child->isArray())
3630 {
3631 return nullptr;
3632 }
3633 break;
3634 case EOpPostIncrement:
3635 case EOpPreIncrement:
3636 case EOpPostDecrement:
3637 case EOpPreDecrement:
3638 case EOpNegative:
3639 case EOpPositive:
3640 if (child->getBasicType() == EbtStruct || child->getBasicType() == EbtBool ||
Martin Radev2cc85b32016-08-05 16:22:53 +03003641 child->isArray() || IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04003642 {
3643 return nullptr;
3644 }
3645 // Operators for built-ins are already type checked against their prototype.
3646 default:
3647 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003648 }
3649
Olli Etuahof119a262016-08-19 15:54:22 +03003650 TIntermUnary *node = new TIntermUnary(op, child);
3651 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03003652
3653 TIntermTyped *foldedNode = node->fold(&mDiagnostics);
3654 if (foldedNode)
3655 return foldedNode;
3656
3657 return node;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003658}
3659
Olli Etuaho09b22472015-02-11 11:47:26 +02003660TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3661{
Olli Etuahof6c694b2015-03-26 14:50:53 +02003662 TIntermTyped *node = createUnaryMath(op, child, loc, nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003663 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003664 {
3665 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02003666 return child;
3667 }
3668 return node;
3669}
3670
Jamie Madillb98c3a82015-07-23 14:26:04 -04003671TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
3672 TIntermTyped *child,
3673 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003674{
Olli Etuaho856c4972016-08-08 11:38:39 +03003675 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02003676 return addUnaryMath(op, child, loc);
3677}
3678
Jamie Madillb98c3a82015-07-23 14:26:04 -04003679bool TParseContext::binaryOpCommonCheck(TOperator op,
3680 TIntermTyped *left,
3681 TIntermTyped *right,
3682 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003683{
Olli Etuaho244be012016-08-18 15:26:02 +03003684 if (left->getType().getStruct() || right->getType().getStruct())
3685 {
3686 switch (op)
3687 {
3688 case EOpIndexDirectStruct:
3689 ASSERT(left->getType().getStruct());
3690 break;
3691 case EOpEqual:
3692 case EOpNotEqual:
3693 case EOpAssign:
3694 case EOpInitialize:
3695 if (left->getType() != right->getType())
3696 {
3697 return false;
3698 }
3699 break;
3700 default:
3701 error(loc, "Invalid operation for structs", GetOperatorString(op));
3702 return false;
3703 }
3704 }
3705
Olli Etuahod6b14282015-03-17 14:31:35 +02003706 if (left->isArray() || right->isArray())
3707 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003708 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003709 {
3710 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3711 return false;
3712 }
3713
3714 if (left->isArray() != right->isArray())
3715 {
3716 error(loc, "array / non-array mismatch", GetOperatorString(op));
3717 return false;
3718 }
3719
3720 switch (op)
3721 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003722 case EOpEqual:
3723 case EOpNotEqual:
3724 case EOpAssign:
3725 case EOpInitialize:
3726 break;
3727 default:
3728 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3729 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02003730 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003731 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003732 if (left->getArraySize() != right->getArraySize())
3733 {
3734 error(loc, "array size mismatch", GetOperatorString(op));
3735 return false;
3736 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003737 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003738
3739 // Check ops which require integer / ivec parameters
3740 bool isBitShift = false;
3741 switch (op)
3742 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003743 case EOpBitShiftLeft:
3744 case EOpBitShiftRight:
3745 case EOpBitShiftLeftAssign:
3746 case EOpBitShiftRightAssign:
3747 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3748 // check that the basic type is an integer type.
3749 isBitShift = true;
3750 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3751 {
3752 return false;
3753 }
3754 break;
3755 case EOpBitwiseAnd:
3756 case EOpBitwiseXor:
3757 case EOpBitwiseOr:
3758 case EOpBitwiseAndAssign:
3759 case EOpBitwiseXorAssign:
3760 case EOpBitwiseOrAssign:
3761 // It is enough to check the type of only one operand, since later it
3762 // is checked that the operand types match.
3763 if (!IsInteger(left->getBasicType()))
3764 {
3765 return false;
3766 }
3767 break;
3768 default:
3769 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003770 }
3771
3772 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3773 // So the basic type should usually match.
3774 if (!isBitShift && left->getBasicType() != right->getBasicType())
3775 {
3776 return false;
3777 }
3778
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003779 // Check that:
3780 // 1. Type sizes match exactly on ops that require that.
3781 // 2. Restrictions for structs that contain arrays or samplers are respected.
3782 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003783 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003784 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003785 case EOpAssign:
3786 case EOpInitialize:
3787 case EOpEqual:
3788 case EOpNotEqual:
3789 // ESSL 1.00 sections 5.7, 5.8, 5.9
3790 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
3791 {
3792 error(loc, "undefined operation for structs containing arrays",
3793 GetOperatorString(op));
3794 return false;
3795 }
3796 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3797 // we interpret the spec so that this extends to structs containing samplers,
3798 // similarly to ESSL 1.00 spec.
3799 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
3800 left->getType().isStructureContainingSamplers())
3801 {
3802 error(loc, "undefined operation for structs containing samplers",
3803 GetOperatorString(op));
3804 return false;
3805 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003806
3807 if ((op == EOpAssign || op == EOpInitialize) &&
3808 left->getType().isStructureContainingImages())
3809 {
3810 error(loc, "undefined operation for structs containing images",
3811 GetOperatorString(op));
3812 return false;
3813 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003814 case EOpLessThan:
3815 case EOpGreaterThan:
3816 case EOpLessThanEqual:
3817 case EOpGreaterThanEqual:
3818 if ((left->getNominalSize() != right->getNominalSize()) ||
3819 (left->getSecondarySize() != right->getSecondarySize()))
3820 {
3821 return false;
3822 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003823 break;
3824 case EOpAdd:
3825 case EOpSub:
3826 case EOpDiv:
3827 case EOpIMod:
3828 case EOpBitShiftLeft:
3829 case EOpBitShiftRight:
3830 case EOpBitwiseAnd:
3831 case EOpBitwiseXor:
3832 case EOpBitwiseOr:
3833 case EOpAddAssign:
3834 case EOpSubAssign:
3835 case EOpDivAssign:
3836 case EOpIModAssign:
3837 case EOpBitShiftLeftAssign:
3838 case EOpBitShiftRightAssign:
3839 case EOpBitwiseAndAssign:
3840 case EOpBitwiseXorAssign:
3841 case EOpBitwiseOrAssign:
3842 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
3843 {
3844 return false;
3845 }
3846
3847 // Are the sizes compatible?
3848 if (left->getNominalSize() != right->getNominalSize() ||
3849 left->getSecondarySize() != right->getSecondarySize())
3850 {
3851 // If the nominal sizes of operands do not match:
3852 // One of them must be a scalar.
3853 if (!left->isScalar() && !right->isScalar())
3854 return false;
3855
3856 // In the case of compound assignment other than multiply-assign,
3857 // the right side needs to be a scalar. Otherwise a vector/matrix
3858 // would be assigned to a scalar. A scalar can't be shifted by a
3859 // vector either.
3860 if (!right->isScalar() &&
3861 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
3862 return false;
3863 }
3864 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003865 default:
3866 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003867 }
3868
Olli Etuahod6b14282015-03-17 14:31:35 +02003869 return true;
3870}
3871
Olli Etuaho1dded802016-08-18 18:13:13 +03003872bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
3873 const TType &left,
3874 const TType &right)
3875{
3876 switch (op)
3877 {
3878 case EOpMul:
3879 case EOpMulAssign:
3880 return left.getNominalSize() == right.getNominalSize() &&
3881 left.getSecondarySize() == right.getSecondarySize();
3882 case EOpVectorTimesScalar:
3883 return true;
3884 case EOpVectorTimesScalarAssign:
3885 ASSERT(!left.isMatrix() && !right.isMatrix());
3886 return left.isVector() && !right.isVector();
3887 case EOpVectorTimesMatrix:
3888 return left.getNominalSize() == right.getRows();
3889 case EOpVectorTimesMatrixAssign:
3890 ASSERT(!left.isMatrix() && right.isMatrix());
3891 return left.isVector() && left.getNominalSize() == right.getRows() &&
3892 left.getNominalSize() == right.getCols();
3893 case EOpMatrixTimesVector:
3894 return left.getCols() == right.getNominalSize();
3895 case EOpMatrixTimesScalar:
3896 return true;
3897 case EOpMatrixTimesScalarAssign:
3898 ASSERT(left.isMatrix() && !right.isMatrix());
3899 return !right.isVector();
3900 case EOpMatrixTimesMatrix:
3901 return left.getCols() == right.getRows();
3902 case EOpMatrixTimesMatrixAssign:
3903 ASSERT(left.isMatrix() && right.isMatrix());
3904 // We need to check two things:
3905 // 1. The matrix multiplication step is valid.
3906 // 2. The result will have the same number of columns as the lvalue.
3907 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
3908
3909 default:
3910 UNREACHABLE();
3911 return false;
3912 }
3913}
3914
Jamie Madillb98c3a82015-07-23 14:26:04 -04003915TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
3916 TIntermTyped *left,
3917 TIntermTyped *right,
3918 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02003919{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003920 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02003921 return nullptr;
3922
Olli Etuahofc1806e2015-03-17 13:03:11 +02003923 switch (op)
3924 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003925 case EOpEqual:
3926 case EOpNotEqual:
3927 break;
3928 case EOpLessThan:
3929 case EOpGreaterThan:
3930 case EOpLessThanEqual:
3931 case EOpGreaterThanEqual:
Olli Etuaho244be012016-08-18 15:26:02 +03003932 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3933 !right->getType().getStruct());
3934 if (left->isMatrix() || left->isVector())
Jamie Madillb98c3a82015-07-23 14:26:04 -04003935 {
3936 return nullptr;
3937 }
3938 break;
3939 case EOpLogicalOr:
3940 case EOpLogicalXor:
3941 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03003942 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3943 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00003944 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04003945 {
3946 return nullptr;
3947 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00003948 // Basic types matching should have been already checked.
3949 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003950 break;
3951 case EOpAdd:
3952 case EOpSub:
3953 case EOpDiv:
3954 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03003955 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3956 !right->getType().getStruct());
3957 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04003958 {
3959 return nullptr;
3960 }
3961 break;
3962 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03003963 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
3964 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003965 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03003966 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04003967 {
3968 return nullptr;
3969 }
3970 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003971 default:
3972 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003973 }
3974
Olli Etuaho1dded802016-08-18 18:13:13 +03003975 if (op == EOpMul)
3976 {
3977 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
3978 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
3979 {
3980 return nullptr;
3981 }
3982 }
3983
Olli Etuaho3fdec912016-08-18 15:08:06 +03003984 TIntermBinary *node = new TIntermBinary(op, left, right);
3985 node->setLine(loc);
3986
Olli Etuaho3fdec912016-08-18 15:08:06 +03003987 // See if we can fold constants.
3988 TIntermTyped *foldedNode = node->fold(&mDiagnostics);
3989 if (foldedNode)
3990 return foldedNode;
3991
3992 return node;
Olli Etuahofc1806e2015-03-17 13:03:11 +02003993}
3994
Jamie Madillb98c3a82015-07-23 14:26:04 -04003995TIntermTyped *TParseContext::addBinaryMath(TOperator op,
3996 TIntermTyped *left,
3997 TIntermTyped *right,
3998 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003999{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004000 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004001 if (node == 0)
4002 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004003 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4004 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02004005 return left;
4006 }
4007 return node;
4008}
4009
Jamie Madillb98c3a82015-07-23 14:26:04 -04004010TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
4011 TIntermTyped *left,
4012 TIntermTyped *right,
4013 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004014{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004015 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004016 if (node == 0)
4017 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004018 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4019 right->getCompleteString());
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004020 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02004021 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004022 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
4023 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004024 }
4025 return node;
4026}
4027
Olli Etuaho13389b62016-10-16 11:48:18 +01004028TIntermBinary *TParseContext::createAssign(TOperator op,
4029 TIntermTyped *left,
4030 TIntermTyped *right,
4031 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004032{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004033 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02004034 {
Olli Etuaho1dded802016-08-18 18:13:13 +03004035 if (op == EOpMulAssign)
4036 {
4037 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
4038 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4039 {
4040 return nullptr;
4041 }
4042 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03004043 TIntermBinary *node = new TIntermBinary(op, left, right);
4044 node->setLine(loc);
4045
Olli Etuaho3fdec912016-08-18 15:08:06 +03004046 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02004047 }
4048 return nullptr;
4049}
4050
Jamie Madillb98c3a82015-07-23 14:26:04 -04004051TIntermTyped *TParseContext::addAssign(TOperator op,
4052 TIntermTyped *left,
4053 TIntermTyped *right,
4054 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004055{
4056 TIntermTyped *node = createAssign(op, left, right, loc);
4057 if (node == nullptr)
4058 {
4059 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02004060 return left;
4061 }
4062 return node;
4063}
4064
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004065TIntermTyped *TParseContext::addComma(TIntermTyped *left,
4066 TIntermTyped *right,
4067 const TSourceLoc &loc)
4068{
Corentin Wallez0d959252016-07-12 17:26:32 -04004069 // WebGL2 section 5.26, the following results in an error:
4070 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004071 if (mShaderSpec == SH_WEBGL2_SPEC &&
4072 (left->isArray() || left->getBasicType() == EbtVoid ||
4073 left->getType().isStructureContainingArrays() || right->isArray() ||
4074 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04004075 {
4076 error(loc,
4077 "sequence operator is not allowed for void, arrays, or structs containing arrays",
4078 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04004079 }
4080
Olli Etuaho4db7ded2016-10-13 12:23:11 +01004081 return TIntermediate::AddComma(left, right, loc, mShaderVersion);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004082}
4083
Olli Etuaho49300862015-02-20 14:54:49 +02004084TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
4085{
4086 switch (op)
4087 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004088 case EOpContinue:
4089 if (mLoopNestingLevel <= 0)
4090 {
4091 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004092 }
4093 break;
4094 case EOpBreak:
4095 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
4096 {
4097 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004098 }
4099 break;
4100 case EOpReturn:
4101 if (mCurrentFunctionType->getBasicType() != EbtVoid)
4102 {
4103 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004104 }
4105 break;
4106 default:
4107 // No checks for discard
4108 break;
Olli Etuaho49300862015-02-20 14:54:49 +02004109 }
4110 return intermediate.addBranch(op, loc);
4111}
4112
Jamie Madillb98c3a82015-07-23 14:26:04 -04004113TIntermBranch *TParseContext::addBranch(TOperator op,
4114 TIntermTyped *returnValue,
4115 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02004116{
4117 ASSERT(op == EOpReturn);
4118 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004119 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02004120 {
4121 error(loc, "void function cannot return a value", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02004122 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004123 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02004124 {
4125 error(loc, "function return is not matching type:", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02004126 }
4127 return intermediate.addBranch(op, returnValue, loc);
4128}
4129
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004130void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
4131{
4132 ASSERT(!functionCall->isUserDefined());
Olli Etuahobd674552016-10-06 13:28:42 +01004133 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004134 TIntermNode *offset = nullptr;
4135 TIntermSequence *arguments = functionCall->getSequence();
4136 if (name.compare(0, 16, "texelFetchOffset") == 0 ||
4137 name.compare(0, 16, "textureLodOffset") == 0 ||
4138 name.compare(0, 20, "textureProjLodOffset") == 0 ||
4139 name.compare(0, 17, "textureGradOffset") == 0 ||
4140 name.compare(0, 21, "textureProjGradOffset") == 0)
4141 {
4142 offset = arguments->back();
4143 }
4144 else if (name.compare(0, 13, "textureOffset") == 0 ||
4145 name.compare(0, 17, "textureProjOffset") == 0)
4146 {
4147 // A bias parameter might follow the offset parameter.
4148 ASSERT(arguments->size() >= 3);
4149 offset = (*arguments)[2];
4150 }
4151 if (offset != nullptr)
4152 {
4153 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
4154 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
4155 {
4156 TString unmangledName = TFunction::unmangleName(name);
4157 error(functionCall->getLine(), "Texture offset must be a constant expression",
4158 unmangledName.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004159 }
4160 else
4161 {
4162 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
4163 size_t size = offsetConstantUnion->getType().getObjectSize();
4164 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
4165 for (size_t i = 0u; i < size; ++i)
4166 {
4167 int offsetValue = values[i].getIConst();
4168 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
4169 {
4170 std::stringstream tokenStream;
4171 tokenStream << offsetValue;
4172 std::string token = tokenStream.str();
4173 error(offset->getLine(), "Texture offset value out of valid range",
4174 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004175 }
4176 }
4177 }
4178 }
4179}
4180
Martin Radev2cc85b32016-08-05 16:22:53 +03004181// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
4182void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
4183{
4184 ASSERT(!functionCall->isUserDefined());
4185 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
4186
4187 if (name.compare(0, 5, "image") == 0)
4188 {
4189 TIntermSequence *arguments = functionCall->getSequence();
4190 TIntermNode *imageNode = (*arguments)[0];
4191 TIntermSymbol *imageSymbol = imageNode->getAsSymbolNode();
4192
4193 const TMemoryQualifier &memoryQualifier = imageSymbol->getMemoryQualifier();
4194
4195 if (name.compare(5, 5, "Store") == 0)
4196 {
4197 if (memoryQualifier.readonly)
4198 {
4199 error(imageNode->getLine(),
4200 "'imageStore' cannot be used with images qualified as 'readonly'",
4201 imageSymbol->getSymbol().c_str());
4202 }
4203 }
4204 else if (name.compare(5, 4, "Load") == 0)
4205 {
4206 if (memoryQualifier.writeonly)
4207 {
4208 error(imageNode->getLine(),
4209 "'imageLoad' cannot be used with images qualified as 'writeonly'",
4210 imageSymbol->getSymbol().c_str());
4211 }
4212 }
4213 }
4214}
4215
4216// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
4217void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
4218 const TFunction *functionDefinition,
4219 const TIntermAggregate *functionCall)
4220{
4221 ASSERT(functionCall->isUserDefined());
4222
4223 const TIntermSequence &arguments = *functionCall->getSequence();
4224
4225 ASSERT(functionDefinition->getParamCount() == arguments.size());
4226
4227 for (size_t i = 0; i < arguments.size(); ++i)
4228 {
4229 const TType &functionArgumentType = arguments[i]->getAsTyped()->getType();
4230 const TType &functionParameterType = *functionDefinition->getParam(i).type;
4231 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
4232
4233 if (IsImage(functionArgumentType.getBasicType()))
4234 {
4235 const TMemoryQualifier &functionArgumentMemoryQualifier =
4236 functionArgumentType.getMemoryQualifier();
4237 const TMemoryQualifier &functionParameterMemoryQualifier =
4238 functionParameterType.getMemoryQualifier();
4239 if (functionArgumentMemoryQualifier.readonly &&
4240 !functionParameterMemoryQualifier.readonly)
4241 {
4242 error(functionCall->getLine(),
4243 "Function call discards the 'readonly' qualifier from image",
4244 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4245 }
4246
4247 if (functionArgumentMemoryQualifier.writeonly &&
4248 !functionParameterMemoryQualifier.writeonly)
4249 {
4250 error(functionCall->getLine(),
4251 "Function call discards the 'writeonly' qualifier from image",
4252 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4253 }
Martin Radev049edfa2016-11-11 14:35:37 +02004254
4255 if (functionArgumentMemoryQualifier.coherent &&
4256 !functionParameterMemoryQualifier.coherent)
4257 {
4258 error(functionCall->getLine(),
4259 "Function call discards the 'coherent' qualifier from image",
4260 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4261 }
4262
4263 if (functionArgumentMemoryQualifier.volatileQualifier &&
4264 !functionParameterMemoryQualifier.volatileQualifier)
4265 {
4266 error(functionCall->getLine(),
4267 "Function call discards the 'volatile' qualifier from image",
4268 arguments[i]->getAsSymbolNode()->getSymbol().c_str());
4269 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004270 }
4271 }
4272}
4273
Jamie Madillb98c3a82015-07-23 14:26:04 -04004274TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
4275 TIntermNode *paramNode,
4276 TIntermNode *thisNode,
4277 const TSourceLoc &loc,
4278 bool *fatalError)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004279{
Jamie Madillb98c3a82015-07-23 14:26:04 -04004280 *fatalError = false;
4281 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004282 TIntermTyped *callNode = nullptr;
4283
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004284 if (thisNode != nullptr)
4285 {
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004286 TConstantUnion *unionArray = new TConstantUnion[1];
Jamie Madillb98c3a82015-07-23 14:26:04 -04004287 int arraySize = 0;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004288 TIntermTyped *typedThis = thisNode->getAsTyped();
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004289 if (fnCall->getName() != "length")
4290 {
4291 error(loc, "invalid method", fnCall->getName().c_str());
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004292 }
4293 else if (paramNode != nullptr)
4294 {
4295 error(loc, "method takes no parameters", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004296 }
4297 else if (typedThis == nullptr || !typedThis->isArray())
4298 {
4299 error(loc, "length can only be called on arrays", "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004300 }
4301 else
4302 {
Olli Etuaho96e67382015-04-23 14:27:02 +03004303 arraySize = typedThis->getArraySize();
Olli Etuaho39282e12015-04-23 15:41:48 +03004304 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004305 {
Olli Etuaho39282e12015-04-23 15:41:48 +03004306 // This code path can be hit with expressions like these:
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004307 // (a = b).length()
Olli Etuaho39282e12015-04-23 15:41:48 +03004308 // (func()).length()
4309 // (int[3](0, 1, 2)).length()
Jamie Madillb98c3a82015-07-23 14:26:04 -04004310 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
4311 // expression.
4312 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
4313 // spec section 5.9 which allows "An array, vector or matrix expression with the
4314 // length method applied".
4315 error(loc, "length can only be called on array names, not on array expressions",
4316 "length");
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004317 }
4318 }
Olli Etuaho96e67382015-04-23 14:27:02 +03004319 unionArray->setIConst(arraySize);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004320 callNode =
4321 intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004322 }
4323 else if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004324 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004325 // Then this should be a constructor.
Olli Etuaho856c4972016-08-08 11:38:39 +03004326 callNode = addConstructor(paramNode, op, fnCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004327 }
4328 else
4329 {
4330 //
4331 // Not a constructor. Find it in the symbol table.
4332 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05304333 const TFunction *fnCandidate;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004334 bool builtIn;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004335 fnCandidate = findFunction(loc, fnCall, mShaderVersion, &builtIn);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004336 if (fnCandidate)
4337 {
4338 //
4339 // A declared function.
4340 //
Olli Etuaho383b7912016-08-05 11:22:59 +03004341 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004342 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004343 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004344 }
4345 op = fnCandidate->getBuiltInOp();
4346 if (builtIn && op != EOpNull)
4347 {
4348 //
4349 // A function call mapped to a built-in operation.
4350 //
4351 if (fnCandidate->getParamCount() == 1)
4352 {
4353 //
4354 // Treat it like a built-in unary operator.
4355 //
Olli Etuaho15c2ac32015-11-09 15:51:43 +02004356 TIntermAggregate *paramAgg = paramNode->getAsAggregate();
4357 paramNode = paramAgg->getSequence()->front();
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004358 callNode = createUnaryMath(op, paramNode->getAsTyped(), loc,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004359 &fnCandidate->getReturnType());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004360 if (callNode == nullptr)
4361 {
4362 std::stringstream extraInfoStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004363 extraInfoStream
4364 << "built in unary operator function. Type: "
4365 << static_cast<TIntermTyped *>(paramNode)->getCompleteString();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004366 std::string extraInfo = extraInfoStream.str();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004367 error(paramNode->getLine(), " wrong operand type", "Internal Error",
4368 extraInfo.c_str());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004369 *fatalError = true;
4370 return nullptr;
4371 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004372 }
4373 else
4374 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004375 TIntermAggregate *aggregate =
4376 intermediate.setAggregateOperator(paramNode, op, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004377 aggregate->setType(fnCandidate->getReturnType());
4378 aggregate->setPrecisionFromChildren();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02004379 if (aggregate->areChildrenConstQualified())
4380 {
4381 aggregate->getTypePointer()->setQualifier(EvqConst);
4382 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004383
4384 // Some built-in functions have out parameters too.
4385 functionCallLValueErrorCheck(fnCandidate, aggregate);
Arun Patole274f0702015-05-05 13:33:30 +05304386
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004387 // See if we can constant fold a built-in. Note that this may be possible even
4388 // if it is not const-qualified.
Olli Etuahof119a262016-08-19 15:54:22 +03004389 TIntermTyped *foldedNode =
4390 intermediate.foldAggregateBuiltIn(aggregate, &mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05304391 if (foldedNode)
4392 {
Arun Patole274f0702015-05-05 13:33:30 +05304393 callNode = foldedNode;
4394 }
Olli Etuahob43846e2015-06-02 18:18:57 +03004395 else
4396 {
4397 callNode = aggregate;
4398 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004399 }
4400 }
4401 else
4402 {
4403 // This is a real function call
Jamie Madillb98c3a82015-07-23 14:26:04 -04004404 TIntermAggregate *aggregate =
4405 intermediate.setAggregateOperator(paramNode, EOpFunctionCall, loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004406 aggregate->setType(fnCandidate->getReturnType());
4407
Jamie Madillb98c3a82015-07-23 14:26:04 -04004408 // this is how we know whether the given function is a builtIn function or a user
4409 // defined function
4410 // if builtIn == false, it's a userDefined -> could be an overloaded
4411 // builtIn function also
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004412 // if builtIn == true, it's definitely a builtIn function with EOpNull
4413 if (!builtIn)
4414 aggregate->setUserDefined();
Olli Etuahobd674552016-10-06 13:28:42 +01004415 aggregate->getFunctionSymbolInfo()->setFromFunction(*fnCandidate);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004416
Olli Etuahobd674552016-10-06 13:28:42 +01004417 // This needs to happen after the function info including name is set
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004418 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004419 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004420 aggregate->setBuiltInFunctionPrecision();
4421
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004422 checkTextureOffsetConst(aggregate);
Martin Radev2cc85b32016-08-05 16:22:53 +03004423
4424 checkImageMemoryAccessForBuiltinFunctions(aggregate);
4425 }
4426 else
4427 {
4428 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, aggregate);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004429 }
4430
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004431 callNode = aggregate;
4432
4433 functionCallLValueErrorCheck(fnCandidate, aggregate);
4434 }
4435 }
4436 else
4437 {
4438 // error message was put out by findFunction()
4439 // Put on a dummy node for error recovery
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004440 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004441 unionArray->setFConst(0.0f);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004442 callNode = intermediate.addConstantUnion(unionArray,
4443 TType(EbtFloat, EbpUndefined, EvqConst), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004444 }
4445 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004446 return callNode;
4447}
4448
Jamie Madillb98c3a82015-07-23 14:26:04 -04004449TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004450 TIntermTyped *trueExpression,
4451 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03004452 const TSourceLoc &loc)
4453{
Olli Etuaho856c4972016-08-08 11:38:39 +03004454 checkIsScalarBool(loc, cond);
Olli Etuaho52901742015-04-15 13:42:45 +03004455
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004456 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03004457 {
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004458 binaryOpError(loc, ":", trueExpression->getCompleteString(),
4459 falseExpression->getCompleteString());
4460 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03004461 }
Olli Etuahode318b22016-10-25 16:18:25 +01004462 if (IsOpaqueType(trueExpression->getBasicType()))
4463 {
4464 // ESSL 1.00 section 4.1.7
4465 // ESSL 3.00 section 4.1.7
4466 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
4467 // Note that structs containing opaque types don't need to be checked as structs are
4468 // forbidden below.
4469 error(loc, "ternary operator is not allowed for opaque types", ":");
4470 return falseExpression;
4471 }
4472
Olli Etuahoa2d53032015-04-15 14:14:44 +03004473 // ESSL1 sections 5.2 and 5.7:
4474 // ESSL3 section 5.7:
4475 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004476 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03004477 {
4478 error(loc, "ternary operator is not allowed for structures or arrays", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004479 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03004480 }
Corentin Wallez0d959252016-07-12 17:26:32 -04004481 // WebGL2 section 5.26, the following results in an error:
4482 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004483 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04004484 {
4485 error(loc, "ternary operator is not allowed for void", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004486 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04004487 }
4488
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004489 return TIntermediate::AddTernarySelection(cond, trueExpression, falseExpression, loc);
Olli Etuaho52901742015-04-15 13:42:45 +03004490}
Olli Etuaho49300862015-02-20 14:54:49 +02004491
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004492//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004493// Parse an array of strings using yyparse.
4494//
4495// Returns 0 for success.
4496//
Jamie Madillb98c3a82015-07-23 14:26:04 -04004497int PaParseStrings(size_t count,
4498 const char *const string[],
4499 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05304500 TParseContext *context)
4501{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004502 if ((count == 0) || (string == NULL))
4503 return 1;
4504
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004505 if (glslang_initialize(context))
4506 return 1;
4507
alokp@chromium.org408c45e2012-04-05 15:54:43 +00004508 int error = glslang_scan(count, string, length, context);
4509 if (!error)
4510 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004511
alokp@chromium.org73bc2982012-06-19 18:48:05 +00004512 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00004513
alokp@chromium.org6b495712012-06-29 00:06:58 +00004514 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004515}
Jamie Madill45bcc782016-11-07 13:58:48 -05004516
4517} // namespace sh