blob: d30c0f9a700d2d2324508eae6645531c93a12d1c [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
Olli Etuaho485eefd2017-02-14 17:40:06 +000069// Get a token from an image argument to use as an error message token.
70const char *GetImageArgumentToken(TIntermTyped *imageNode)
71{
72 ASSERT(IsImage(imageNode->getBasicType()));
73 while (imageNode->getAsBinaryNode() &&
74 (imageNode->getAsBinaryNode()->getOp() == EOpIndexIndirect ||
75 imageNode->getAsBinaryNode()->getOp() == EOpIndexDirect))
76 {
77 imageNode = imageNode->getAsBinaryNode()->getLeft();
78 }
79 TIntermSymbol *imageSymbol = imageNode->getAsSymbolNode();
80 if (imageSymbol)
81 {
82 return imageSymbol->getSymbol().c_str();
83 }
84 return "image";
85}
86
Martin Radev2cc85b32016-08-05 16:22:53 +030087} // namespace
88
Jamie Madillacb4b812016-11-07 13:50:29 -050089TParseContext::TParseContext(TSymbolTable &symt,
90 TExtensionBehavior &ext,
91 sh::GLenum type,
92 ShShaderSpec spec,
93 ShCompileOptions options,
94 bool checksPrecErrors,
Olli Etuaho77ba4082016-12-16 12:01:18 +000095 TDiagnostics *diagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -050096 const ShBuiltInResources &resources)
97 : intermediate(),
98 symbolTable(symt),
Olli Etuahobb7e5a72017-04-24 10:16:44 +030099 mDeferredNonEmptyDeclarationErrorCheck(false),
Jamie Madillacb4b812016-11-07 13:50:29 -0500100 mShaderType(type),
101 mShaderSpec(spec),
102 mCompileOptions(options),
103 mShaderVersion(100),
104 mTreeRoot(nullptr),
105 mLoopNestingLevel(0),
106 mStructNestingLevel(0),
107 mSwitchNestingLevel(0),
108 mCurrentFunctionType(nullptr),
109 mFunctionReturnsValue(false),
110 mChecksPrecisionErrors(checksPrecErrors),
111 mFragmentPrecisionHighOnESSL1(false),
112 mDefaultMatrixPacking(EmpColumnMajor),
113 mDefaultBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000114 mDiagnostics(diagnostics),
Jamie Madillacb4b812016-11-07 13:50:29 -0500115 mDirectiveHandler(ext,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000116 *mDiagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500117 mShaderVersion,
118 mShaderType,
119 resources.WEBGL_debug_shader_precision == 1),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000120 mPreprocessor(mDiagnostics, &mDirectiveHandler, pp::PreprocessorSettings()),
Jamie Madillacb4b812016-11-07 13:50:29 -0500121 mScanner(nullptr),
122 mUsesFragData(false),
123 mUsesFragColor(false),
124 mUsesSecondaryOutputs(false),
125 mMinProgramTexelOffset(resources.MinProgramTexelOffset),
126 mMaxProgramTexelOffset(resources.MaxProgramTexelOffset),
Olli Etuaho09b04a22016-12-15 13:30:26 +0000127 mMultiviewAvailable(resources.OVR_multiview == 1),
Jamie Madillacb4b812016-11-07 13:50:29 -0500128 mComputeShaderLocalSizeDeclared(false),
Olli Etuaho09b04a22016-12-15 13:30:26 +0000129 mNumViews(-1),
130 mMaxNumViews(resources.MaxViewsOVR),
Olli Etuaho43364892017-02-13 16:00:12 +0000131 mMaxImageUnits(resources.MaxImageUnits),
132 mMaxCombinedTextureImageUnits(resources.MaxCombinedTextureImageUnits),
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000133 mMaxUniformLocations(resources.MaxUniformLocations),
Jamie Madillacb4b812016-11-07 13:50:29 -0500134 mDeclaringFunction(false)
135{
136 mComputeShaderLocalSize.fill(-1);
137}
138
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000139//
140// Look at a '.' field selector string and change it into offsets
141// for a vector.
142//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400143bool TParseContext::parseVectorFields(const TString &compString,
144 int vecSize,
145 TVectorFields &fields,
Arun Patole7e7e68d2015-05-22 12:02:25 +0530146 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000147{
Jamie Madillb98c3a82015-07-23 14:26:04 -0400148 fields.num = (int)compString.size();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530149 if (fields.num > 4)
150 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000151 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000152 return false;
153 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000154
Jamie Madillb98c3a82015-07-23 14:26:04 -0400155 enum
156 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000157 exyzw,
158 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +0000159 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000160 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000161
Arun Patole7e7e68d2015-05-22 12:02:25 +0530162 for (int i = 0; i < fields.num; ++i)
163 {
164 switch (compString[i])
165 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400166 case 'x':
167 fields.offsets[i] = 0;
168 fieldSet[i] = exyzw;
169 break;
170 case 'r':
171 fields.offsets[i] = 0;
172 fieldSet[i] = ergba;
173 break;
174 case 's':
175 fields.offsets[i] = 0;
176 fieldSet[i] = estpq;
177 break;
178 case 'y':
179 fields.offsets[i] = 1;
180 fieldSet[i] = exyzw;
181 break;
182 case 'g':
183 fields.offsets[i] = 1;
184 fieldSet[i] = ergba;
185 break;
186 case 't':
187 fields.offsets[i] = 1;
188 fieldSet[i] = estpq;
189 break;
190 case 'z':
191 fields.offsets[i] = 2;
192 fieldSet[i] = exyzw;
193 break;
194 case 'b':
195 fields.offsets[i] = 2;
196 fieldSet[i] = ergba;
197 break;
198 case 'p':
199 fields.offsets[i] = 2;
200 fieldSet[i] = estpq;
201 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530202
Jamie Madillb98c3a82015-07-23 14:26:04 -0400203 case 'w':
204 fields.offsets[i] = 3;
205 fieldSet[i] = exyzw;
206 break;
207 case 'a':
208 fields.offsets[i] = 3;
209 fieldSet[i] = ergba;
210 break;
211 case 'q':
212 fields.offsets[i] = 3;
213 fieldSet[i] = estpq;
214 break;
215 default:
216 error(line, "illegal vector field selection", compString.c_str());
217 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000218 }
219 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000220
Arun Patole7e7e68d2015-05-22 12:02:25 +0530221 for (int i = 0; i < fields.num; ++i)
222 {
223 if (fields.offsets[i] >= vecSize)
224 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400225 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000226 return false;
227 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000228
Arun Patole7e7e68d2015-05-22 12:02:25 +0530229 if (i > 0)
230 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400231 if (fieldSet[i] != fieldSet[i - 1])
Arun Patole7e7e68d2015-05-22 12:02:25 +0530232 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400233 error(line, "illegal - vector component fields not from the same set",
234 compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000235 return false;
236 }
237 }
238 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000239
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000240 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000241}
242
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000243///////////////////////////////////////////////////////////////////////
244//
245// Errors
246//
247////////////////////////////////////////////////////////////////////////
248
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000249//
250// Used by flex/bison to output all syntax and parsing errors.
251//
Olli Etuaho4de340a2016-12-16 09:32:03 +0000252void TParseContext::error(const TSourceLoc &loc, const char *reason, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000253{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000254 mDiagnostics->error(loc, reason, token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000255}
256
Olli Etuaho4de340a2016-12-16 09:32:03 +0000257void TParseContext::warning(const TSourceLoc &loc, const char *reason, const char *token)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530258{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000259 mDiagnostics->warning(loc, reason, token);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000260}
261
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200262void TParseContext::outOfRangeError(bool isError,
263 const TSourceLoc &loc,
264 const char *reason,
Olli Etuaho4de340a2016-12-16 09:32:03 +0000265 const char *token)
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200266{
267 if (isError)
268 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000269 error(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200270 }
271 else
272 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000273 warning(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200274 }
275}
276
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000277//
278// Same error message for all places assignments don't work.
279//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530280void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000281{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000282 std::stringstream reasonStream;
283 reasonStream << "cannot convert from '" << right << "' to '" << left << "'";
284 std::string reason = reasonStream.str();
285 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000286}
287
288//
289// Same error message for all places unary operations don't work.
290//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530291void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000292{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000293 std::stringstream reasonStream;
294 reasonStream << "wrong operand type - no operation '" << op
295 << "' exists that takes an operand of type " << operand
296 << " (or there is no acceptable conversion)";
297 std::string reason = reasonStream.str();
298 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000299}
300
301//
302// Same error message for all binary operations don't work.
303//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400304void TParseContext::binaryOpError(const TSourceLoc &line,
305 const char *op,
306 TString left,
307 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000308{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000309 std::stringstream reasonStream;
310 reasonStream << "wrong operand types - no operation '" << op
311 << "' exists that takes a left-hand operand of type '" << left
312 << "' and a right operand of type '" << right
313 << "' (or there is no acceptable conversion)";
314 std::string reason = reasonStream.str();
315 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000316}
317
Olli Etuaho856c4972016-08-08 11:38:39 +0300318void TParseContext::checkPrecisionSpecified(const TSourceLoc &line,
319 TPrecision precision,
320 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530321{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400322 if (!mChecksPrecisionErrors)
Olli Etuaho383b7912016-08-05 11:22:59 +0300323 return;
Martin Radev70866b82016-07-22 15:27:42 +0300324
325 if (precision != EbpUndefined && !SupportsPrecision(type))
326 {
327 error(line, "illegal type for precision qualifier", getBasicString(type));
328 }
329
Olli Etuaho183d7e22015-11-20 15:59:09 +0200330 if (precision == EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530331 {
Olli Etuaho183d7e22015-11-20 15:59:09 +0200332 switch (type)
333 {
334 case EbtFloat:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400335 error(line, "No precision specified for (float)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300336 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200337 case EbtInt:
338 case EbtUInt:
339 UNREACHABLE(); // there's always a predeclared qualifier
Jamie Madillb98c3a82015-07-23 14:26:04 -0400340 error(line, "No precision specified (int)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300341 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200342 default:
343 if (IsSampler(type))
344 {
345 error(line, "No precision specified (sampler)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300346 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200347 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300348 if (IsImage(type))
349 {
350 error(line, "No precision specified (image)", "");
351 return;
352 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200353 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000354 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000355}
356
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000357// Both test and if necessary, spit out an error, to see if the node is really
358// an l-value that can be operated on this way.
Olli Etuaho856c4972016-08-08 11:38:39 +0300359bool TParseContext::checkCanBeLValue(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000360{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500361 TIntermSymbol *symNode = node->getAsSymbolNode();
362 TIntermBinary *binaryNode = node->getAsBinaryNode();
Olli Etuahob6fa0432016-09-28 16:28:05 +0100363 TIntermSwizzle *swizzleNode = node->getAsSwizzleNode();
364
365 if (swizzleNode)
366 {
367 bool ok = checkCanBeLValue(line, op, swizzleNode->getOperand());
368 if (ok && swizzleNode->hasDuplicateOffsets())
369 {
370 error(line, " l-value of swizzle cannot have duplicate components", op);
371 return false;
372 }
373 return ok;
374 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000375
Arun Patole7e7e68d2015-05-22 12:02:25 +0530376 if (binaryNode)
377 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400378 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530379 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400380 case EOpIndexDirect:
381 case EOpIndexIndirect:
382 case EOpIndexDirectStruct:
383 case EOpIndexDirectInterfaceBlock:
Olli Etuaho856c4972016-08-08 11:38:39 +0300384 return checkCanBeLValue(line, op, binaryNode->getLeft());
Jamie Madillb98c3a82015-07-23 14:26:04 -0400385 default:
386 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000387 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000388 error(line, " l-value required", op);
Olli Etuaho8a176262016-08-16 14:23:01 +0300389 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000390 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000391
Arun Patole7e7e68d2015-05-22 12:02:25 +0530392 const char *message = 0;
393 switch (node->getQualifier())
394 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400395 case EvqConst:
396 message = "can't modify a const";
397 break;
398 case EvqConstReadOnly:
399 message = "can't modify a const";
400 break;
401 case EvqAttribute:
402 message = "can't modify an attribute";
403 break;
404 case EvqFragmentIn:
405 message = "can't modify an input";
406 break;
407 case EvqVertexIn:
408 message = "can't modify an input";
409 break;
410 case EvqUniform:
411 message = "can't modify a uniform";
412 break;
413 case EvqVaryingIn:
414 message = "can't modify a varying";
415 break;
416 case EvqFragCoord:
417 message = "can't modify gl_FragCoord";
418 break;
419 case EvqFrontFacing:
420 message = "can't modify gl_FrontFacing";
421 break;
422 case EvqPointCoord:
423 message = "can't modify gl_PointCoord";
424 break;
Martin Radevb0883602016-08-04 17:48:58 +0300425 case EvqNumWorkGroups:
426 message = "can't modify gl_NumWorkGroups";
427 break;
428 case EvqWorkGroupSize:
429 message = "can't modify gl_WorkGroupSize";
430 break;
431 case EvqWorkGroupID:
432 message = "can't modify gl_WorkGroupID";
433 break;
434 case EvqLocalInvocationID:
435 message = "can't modify gl_LocalInvocationID";
436 break;
437 case EvqGlobalInvocationID:
438 message = "can't modify gl_GlobalInvocationID";
439 break;
440 case EvqLocalInvocationIndex:
441 message = "can't modify gl_LocalInvocationIndex";
442 break;
Olli Etuaho7142f6c2017-05-05 17:07:26 +0300443 case EvqViewIDOVR:
444 message = "can't modify gl_ViewID_OVR";
445 break;
Martin Radev802abe02016-08-04 17:48:32 +0300446 case EvqComputeIn:
447 message = "can't modify work group size variable";
448 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400449 default:
450 //
451 // Type that can't be written to?
452 //
453 if (node->getBasicType() == EbtVoid)
454 {
455 message = "can't modify void";
456 }
457 if (IsSampler(node->getBasicType()))
458 {
459 message = "can't modify a sampler";
460 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300461 if (IsImage(node->getBasicType()))
462 {
463 message = "can't modify an image";
464 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000465 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000466
Arun Patole7e7e68d2015-05-22 12:02:25 +0530467 if (message == 0 && binaryNode == 0 && symNode == 0)
468 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000469 error(line, "l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000470
Olli Etuaho8a176262016-08-16 14:23:01 +0300471 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000472 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000473
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000474 //
475 // Everything else is okay, no error.
476 //
477 if (message == 0)
Olli Etuaho8a176262016-08-16 14:23:01 +0300478 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000479
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000480 //
481 // If we get here, we have an error and a message.
482 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530483 if (symNode)
484 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000485 const char *symbol = symNode->getSymbol().c_str();
486 std::stringstream reasonStream;
487 reasonStream << "l-value required (" << message << " \"" << symbol << "\")";
488 std::string reason = reasonStream.str();
489 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000490 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530491 else
492 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000493 std::stringstream reasonStream;
494 reasonStream << "l-value required (" << message << ")";
495 std::string reason = reasonStream.str();
496 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000497 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000498
Olli Etuaho8a176262016-08-16 14:23:01 +0300499 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000500}
501
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000502// Both test, and if necessary spit out an error, to see if the node is really
503// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300504void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000505{
Olli Etuaho383b7912016-08-05 11:22:59 +0300506 if (node->getQualifier() != EvqConst)
507 {
508 error(node->getLine(), "constant expression required", "");
509 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000510}
511
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000512// Both test, and if necessary spit out an error, to see if the node is really
513// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300514void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000515{
Olli Etuaho383b7912016-08-05 11:22:59 +0300516 if (!node->isScalarInt())
517 {
518 error(node->getLine(), "integer expression required", token);
519 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000520}
521
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000522// Both test, and if necessary spit out an error, to see if we are currently
523// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800524bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000525{
Olli Etuaho856c4972016-08-08 11:38:39 +0300526 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300527 {
528 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800529 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300530 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800531 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000532}
533
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000534// For now, keep it simple: if it starts "gl_", it's reserved, independent
535// of scope. Except, if the symbol table is at the built-in push-level,
536// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000537// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
538// webgl shader.
Olli Etuaho856c4972016-08-08 11:38:39 +0300539bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000540{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530541 static const char *reservedErrMsg = "reserved built-in name";
542 if (!symbolTable.atBuiltInLevel())
543 {
544 if (identifier.compare(0, 3, "gl_") == 0)
545 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000546 error(line, reservedErrMsg, "gl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300547 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000548 }
Jamie Madillacb4b812016-11-07 13:50:29 -0500549 if (sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530550 {
551 if (identifier.compare(0, 6, "webgl_") == 0)
552 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000553 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300554 return false;
alokp@chromium.org613ef312010-07-21 18:54:22 +0000555 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530556 if (identifier.compare(0, 7, "_webgl_") == 0)
557 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000558 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300559 return false;
alokp@chromium.org613ef312010-07-21 18:54:22 +0000560 }
561 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530562 if (identifier.find("__") != TString::npos)
563 {
564 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400565 "identifiers containing two consecutive underscores (__) are reserved as "
566 "possible future keywords",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530567 identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300568 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000569 }
570 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000571
Olli Etuaho8a176262016-08-16 14:23:01 +0300572 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000573}
574
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000575// Make sure there is enough data provided to the constructor to build
576// something of the type of the constructor. Also returns the type of
577// the constructor.
Olli Etuaho856c4972016-08-08 11:38:39 +0300578bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800579 const TIntermSequence *arguments,
Olli Etuaho856c4972016-08-08 11:38:39 +0300580 TOperator op,
581 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000582{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000583 bool constructingMatrix = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400584 switch (op)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530585 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400586 case EOpConstructMat2:
587 case EOpConstructMat2x3:
588 case EOpConstructMat2x4:
589 case EOpConstructMat3x2:
590 case EOpConstructMat3:
591 case EOpConstructMat3x4:
592 case EOpConstructMat4x2:
593 case EOpConstructMat4x3:
594 case EOpConstructMat4:
595 constructingMatrix = true;
596 break;
597 default:
598 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000599 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000600
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000601 //
602 // Note: It's okay to have too many components available, but not okay to have unused
603 // arguments. 'full' will go to true when enough args have been seen. If we loop
604 // again, there is an extra argument, so 'overfull' will become true.
605 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000606
Jamie Madillb98c3a82015-07-23 14:26:04 -0400607 size_t size = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400608 bool full = false;
609 bool overFull = false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000610 bool matrixInMatrix = false;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500611 bool arrayArg = false;
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800612 for (TIntermNode *arg : *arguments)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530613 {
Olli Etuaho72d10202017-01-19 15:58:30 +0000614 const TIntermTyped *argTyped = arg->getAsTyped();
615 size += argTyped->getType().getObjectSize();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530616
Olli Etuaho72d10202017-01-19 15:58:30 +0000617 if (constructingMatrix && argTyped->getType().isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000618 matrixInMatrix = true;
619 if (full)
620 overFull = true;
Olli Etuaho856c4972016-08-08 11:38:39 +0300621 if (op != EOpConstructStruct && !type.isArray() && size >= type.getObjectSize())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000622 full = true;
Olli Etuaho72d10202017-01-19 15:58:30 +0000623 if (argTyped->getType().isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000624 arrayArg = true;
625 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530626
Olli Etuaho856c4972016-08-08 11:38:39 +0300627 if (type.isArray())
Olli Etuaho376f1b52015-04-13 13:23:41 +0300628 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300629 // The size of an unsized constructor should already have been determined.
630 ASSERT(!type.isUnsizedArray());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800631 if (static_cast<size_t>(type.getArraySize()) != arguments->size())
Olli Etuaho376f1b52015-04-13 13:23:41 +0300632 {
633 error(line, "array constructor needs one argument per array element", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300634 return false;
Olli Etuaho376f1b52015-04-13 13:23:41 +0300635 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000636 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000637
Arun Patole7e7e68d2015-05-22 12:02:25 +0530638 if (arrayArg && op != EOpConstructStruct)
639 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000640 error(line, "constructing from a non-dereferenced array", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300641 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000642 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000643
Olli Etuaho856c4972016-08-08 11:38:39 +0300644 if (matrixInMatrix && !type.isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530645 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800646 if (arguments->size() != 1)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530647 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400648 error(line, "constructing matrix from matrix can only take one argument",
649 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300650 return false;
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000651 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000652 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000653
Arun Patole7e7e68d2015-05-22 12:02:25 +0530654 if (overFull)
655 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000656 error(line, "too many arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300657 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000658 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530659
Olli Etuaho856c4972016-08-08 11:38:39 +0300660 if (op == EOpConstructStruct && !type.isArray() &&
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800661 type.getStruct()->fields().size() != arguments->size())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530662 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400663 error(line,
664 "Number of constructor parameters does not match the number of structure fields",
665 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300666 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000667 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000668
Olli Etuaho856c4972016-08-08 11:38:39 +0300669 if (!type.isMatrix() || !matrixInMatrix)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530670 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300671 if ((op != EOpConstructStruct && size != 1 && size < type.getObjectSize()) ||
672 (op == EOpConstructStruct && size < type.getObjectSize()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530673 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000674 error(line, "not enough data provided for construction", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300675 return false;
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000676 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000677 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000678
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800679 if (arguments->empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530680 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200681 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300682 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000683 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200684
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800685 for (TIntermNode *const &argNode : *arguments)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530686 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200687 TIntermTyped *argTyped = argNode->getAsTyped();
688 ASSERT(argTyped != nullptr);
689 if (op != EOpConstructStruct && IsSampler(argTyped->getBasicType()))
690 {
691 error(line, "cannot convert a sampler", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300692 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200693 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300694 if (op != EOpConstructStruct && IsImage(argTyped->getBasicType()))
695 {
696 error(line, "cannot convert an image", "constructor");
697 return false;
698 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200699 if (argTyped->getBasicType() == EbtVoid)
700 {
701 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300702 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200703 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000704 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000705
Olli Etuaho856c4972016-08-08 11:38:39 +0300706 if (type.isArray())
707 {
708 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
709 // the array.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800710 for (TIntermNode *const &argNode : *arguments)
Olli Etuaho856c4972016-08-08 11:38:39 +0300711 {
712 const TType &argType = argNode->getAsTyped()->getType();
Jamie Madill34bf2d92017-02-06 13:40:59 -0500713 // It has already been checked that the argument is not an array, but we can arrive
714 // here due to prior error conditions.
715 if (argType.isArray())
716 {
717 return false;
718 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300719 if (!argType.sameElementType(type))
720 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000721 error(line, "Array constructor argument has an incorrect type", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300722 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300723 }
724 }
725 }
726 else if (op == EOpConstructStruct)
727 {
728 const TFieldList &fields = type.getStruct()->fields();
Olli Etuaho856c4972016-08-08 11:38:39 +0300729
730 for (size_t i = 0; i < fields.size(); i++)
731 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800732 if (i >= arguments->size() ||
733 (*arguments)[i]->getAsTyped()->getType() != *fields[i]->type())
Olli Etuaho856c4972016-08-08 11:38:39 +0300734 {
735 error(line, "Structure constructor arguments do not match structure fields",
Olli Etuaho4de340a2016-12-16 09:32:03 +0000736 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300737 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300738 }
739 }
740 }
741
Olli Etuaho8a176262016-08-16 14:23:01 +0300742 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000743}
744
Jamie Madillb98c3a82015-07-23 14:26:04 -0400745// This function checks to see if a void variable has been declared and raise an error message for
746// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000747//
748// returns true in case of an error
749//
Olli Etuaho856c4972016-08-08 11:38:39 +0300750bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400751 const TString &identifier,
752 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000753{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300754 if (type == EbtVoid)
755 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000756 error(line, "illegal use of type 'void'", identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300757 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300758 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000759
Olli Etuaho8a176262016-08-16 14:23:01 +0300760 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000761}
762
Jamie Madillb98c3a82015-07-23 14:26:04 -0400763// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300764// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300765void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000766{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530767 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector())
768 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000769 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530770 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000771}
772
Jamie Madillb98c3a82015-07-23 14:26:04 -0400773// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300774// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300775void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000776{
Martin Radev4a9cd802016-09-01 16:51:51 +0300777 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530778 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000779 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530780 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000781}
782
Olli Etuaho856c4972016-08-08 11:38:39 +0300783bool TParseContext::checkIsNotSampler(const TSourceLoc &line,
Martin Radev4a9cd802016-09-01 16:51:51 +0300784 const TTypeSpecifierNonArray &pType,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400785 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000786{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530787 if (pType.type == EbtStruct)
788 {
Martin Radev2cc85b32016-08-05 16:22:53 +0300789 if (ContainsSampler(*pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530790 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000791 std::stringstream reasonStream;
792 reasonStream << reason << " (structure contains a sampler)";
793 std::string reasonStr = reasonStream.str();
794 error(line, reasonStr.c_str(), getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300795 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000796 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530797
Olli Etuaho8a176262016-08-16 14:23:01 +0300798 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530799 }
800 else if (IsSampler(pType.type))
801 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000802 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300803 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000804 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000805
Olli Etuaho8a176262016-08-16 14:23:01 +0300806 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000807}
808
Martin Radev2cc85b32016-08-05 16:22:53 +0300809bool TParseContext::checkIsNotImage(const TSourceLoc &line,
810 const TTypeSpecifierNonArray &pType,
811 const char *reason)
812{
813 if (pType.type == EbtStruct)
814 {
815 if (ContainsImage(*pType.userDef))
816 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000817 std::stringstream reasonStream;
818 reasonStream << reason << " (structure contains an image)";
819 std::string reasonStr = reasonStream.str();
820 error(line, reasonStr.c_str(), getBasicString(pType.type));
Martin Radev2cc85b32016-08-05 16:22:53 +0300821
822 return false;
823 }
824
825 return true;
826 }
827 else if (IsImage(pType.type))
828 {
829 error(line, reason, getBasicString(pType.type));
830
831 return false;
832 }
833
834 return true;
835}
836
Olli Etuaho856c4972016-08-08 11:38:39 +0300837void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
838 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400839{
840 if (pType.layoutQualifier.location != -1)
841 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400842 error(line, "location must only be specified for a single input or output variable",
843 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400844 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400845}
846
Olli Etuaho856c4972016-08-08 11:38:39 +0300847void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
848 const TLayoutQualifier &layoutQualifier)
849{
850 if (layoutQualifier.location != -1)
851 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000852 const char *errorMsg = "invalid layout qualifier: only valid on program inputs and outputs";
853 if (mShaderVersion >= 310)
854 {
855 errorMsg =
856 "invalid layout qualifier: only valid on program inputs, outputs, and uniforms";
857 }
858 error(location, errorMsg, "location");
Olli Etuaho856c4972016-08-08 11:38:39 +0300859 }
860}
861
Martin Radev2cc85b32016-08-05 16:22:53 +0300862void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
863 TQualifier qualifier,
864 const TType &type)
865{
866 checkOutParameterIsNotSampler(line, qualifier, type);
867 checkOutParameterIsNotImage(line, qualifier, type);
868}
869
Olli Etuaho856c4972016-08-08 11:38:39 +0300870void TParseContext::checkOutParameterIsNotSampler(const TSourceLoc &line,
871 TQualifier qualifier,
872 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000873{
Martin Radev2cc85b32016-08-05 16:22:53 +0300874 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
875 if (IsSampler(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530876 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000877 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000878 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000879}
880
Martin Radev2cc85b32016-08-05 16:22:53 +0300881void TParseContext::checkOutParameterIsNotImage(const TSourceLoc &line,
882 TQualifier qualifier,
883 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000884{
Martin Radev2cc85b32016-08-05 16:22:53 +0300885 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
886 if (IsImage(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530887 {
Martin Radev2cc85b32016-08-05 16:22:53 +0300888 error(line, "images cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000889 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000890}
891
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000892// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300893unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000894{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530895 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000896
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200897 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
898 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
899 // fold as array size.
900 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000901 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000902 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300903 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000904 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000905
Olli Etuaho856c4972016-08-08 11:38:39 +0300906 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400907
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000908 if (constant->getBasicType() == EbtUInt)
909 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300910 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000911 }
912 else
913 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300914 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000915
Olli Etuaho856c4972016-08-08 11:38:39 +0300916 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000917 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400918 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300919 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000920 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400921
Olli Etuaho856c4972016-08-08 11:38:39 +0300922 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400923 }
924
Olli Etuaho856c4972016-08-08 11:38:39 +0300925 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400926 {
927 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300928 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400929 }
930
931 // The size of arrays is restricted here to prevent issues further down the
932 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
933 // 4096 registers so this should be reasonable even for aggressively optimizable code.
934 const unsigned int sizeLimit = 65536;
935
Olli Etuaho856c4972016-08-08 11:38:39 +0300936 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -0400937 {
938 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300939 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000940 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300941
942 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000943}
944
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000945// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +0300946bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
947 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000948{
Olli Etuaho8a176262016-08-16 14:23:01 +0300949 if ((elementQualifier.qualifier == EvqAttribute) ||
950 (elementQualifier.qualifier == EvqVertexIn) ||
951 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300952 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400953 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300954 TType(elementQualifier).getQualifierString());
955 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000956 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000957
Olli Etuaho8a176262016-08-16 14:23:01 +0300958 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000959}
960
Olli Etuaho8a176262016-08-16 14:23:01 +0300961// See if this element type can be formed into an array.
962bool TParseContext::checkIsValidTypeForArray(const TSourceLoc &line, const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000963{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000964 //
965 // Can the type be an array?
966 //
Olli Etuaho8a176262016-08-16 14:23:01 +0300967 if (elementType.array)
Jamie Madill06145232015-05-13 13:10:01 -0400968 {
Olli Etuaho8a176262016-08-16 14:23:01 +0300969 error(line, "cannot declare arrays of arrays",
970 TType(elementType).getCompleteString().c_str());
971 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000972 }
Olli Etuahocc36b982015-07-10 14:14:18 +0300973 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
974 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
975 // 4.3.4).
Martin Radev4a9cd802016-09-01 16:51:51 +0300976 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Olli Etuaho8a176262016-08-16 14:23:01 +0300977 sh::IsVarying(elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +0300978 {
979 error(line, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300980 TType(elementType).getCompleteString().c_str());
981 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +0300982 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000983
Olli Etuaho8a176262016-08-16 14:23:01 +0300984 return true;
985}
986
987// Check if this qualified element type can be formed into an array.
988bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
989 const TPublicType &elementType)
990{
991 if (checkIsValidTypeForArray(indexLocation, elementType))
992 {
993 return checkIsValidQualifierForArray(indexLocation, elementType);
994 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000995 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000996}
997
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000998// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +0300999void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
1000 const TString &identifier,
1001 TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001002{
Olli Etuaho3739d232015-04-08 12:23:44 +03001003 ASSERT(type != nullptr);
1004 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001005 {
1006 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +03001007 type->qualifier = EvqTemporary;
1008
1009 // Generate informative error messages for ESSL1.
1010 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001011 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001012 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301013 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001014 "structures containing arrays may not be declared constant since they cannot be "
1015 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +05301016 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001017 }
1018 else
1019 {
1020 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
1021 }
Olli Etuaho383b7912016-08-05 11:22:59 +03001022 return;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001023 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001024 if (type->isUnsizedArray())
1025 {
1026 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
Olli Etuaho376f1b52015-04-13 13:23:41 +03001027 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001028}
1029
Olli Etuaho2935c582015-04-08 14:32:06 +03001030// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001031// and update the symbol table.
1032//
Olli Etuaho2935c582015-04-08 14:32:06 +03001033// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001034//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001035bool TParseContext::declareVariable(const TSourceLoc &line,
1036 const TString &identifier,
1037 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001038 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001039{
Olli Etuaho2935c582015-04-08 14:32:06 +03001040 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001041
Olli Etuaho43364892017-02-13 16:00:12 +00001042 checkBindingIsValid(line, type);
1043
Olli Etuaho856c4972016-08-08 11:38:39 +03001044 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001045
Olli Etuaho2935c582015-04-08 14:32:06 +03001046 // gl_LastFragData may be redeclared with a new precision qualifier
1047 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1048 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001049 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1050 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho856c4972016-08-08 11:38:39 +03001051 if (static_cast<int>(type.getArraySize()) == maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001052 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001053 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001054 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001055 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->getExtension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001056 }
1057 }
1058 else
1059 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001060 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1061 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001062 return false;
1063 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001064 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001065
Olli Etuaho8a176262016-08-16 14:23:01 +03001066 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001067 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001068
Olli Etuaho2935c582015-04-08 14:32:06 +03001069 (*variable) = new TVariable(&identifier, type);
1070 if (!symbolTable.declare(*variable))
1071 {
1072 error(line, "redefinition", identifier.c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -04001073 *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +03001074 return false;
1075 }
1076
Olli Etuaho8a176262016-08-16 14:23:01 +03001077 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001078 return false;
1079
1080 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001081}
1082
Martin Radev70866b82016-07-22 15:27:42 +03001083void TParseContext::checkIsParameterQualifierValid(
1084 const TSourceLoc &line,
1085 const TTypeQualifierBuilder &typeQualifierBuilder,
1086 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301087{
Olli Etuaho77ba4082016-12-16 12:01:18 +00001088 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001089
1090 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301091 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001092 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1093 }
1094
1095 if (!IsImage(type->getBasicType()))
1096 {
Olli Etuaho43364892017-02-13 16:00:12 +00001097 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001098 }
1099 else
1100 {
1101 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001102 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001103
Martin Radev70866b82016-07-22 15:27:42 +03001104 type->setQualifier(typeQualifier.qualifier);
1105
1106 if (typeQualifier.precision != EbpUndefined)
1107 {
1108 type->setPrecision(typeQualifier.precision);
1109 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001110}
1111
Olli Etuaho856c4972016-08-08 11:38:39 +03001112bool TParseContext::checkCanUseExtension(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001113{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001114 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001115 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +05301116 if (iter == extBehavior.end())
1117 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001118 error(line, "extension is not supported", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001119 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001120 }
zmo@google.comf5450912011-09-09 01:37:19 +00001121 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +05301122 if (iter->second == EBhDisable || iter->second == EBhUndefined)
1123 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00001124 // TODO(oetuaho@nvidia.com): This is slightly hacky. Might be better if symbols could be
1125 // associated with more than one extension.
1126 if (extension == "GL_OVR_multiview")
1127 {
1128 return checkCanUseExtension(line, "GL_OVR_multiview2");
1129 }
Olli Etuaho4de340a2016-12-16 09:32:03 +00001130 error(line, "extension is disabled", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001131 return false;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001132 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301133 if (iter->second == EBhWarn)
1134 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001135 warning(line, "extension is being used", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001136 return true;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001137 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001138
Olli Etuaho8a176262016-08-16 14:23:01 +03001139 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001140}
1141
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001142// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1143// compile-time or link-time errors are the same whether or not the declaration is empty".
1144// This function implements all the checks that are done on qualifiers regardless of if the
1145// declaration is empty.
1146void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1147 const sh::TLayoutQualifier &layoutQualifier,
1148 const TSourceLoc &location)
1149{
1150 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1151 {
1152 error(location, "Shared memory declarations cannot have layout specified", "layout");
1153 }
1154
1155 if (layoutQualifier.matrixPacking != EmpUnspecified)
1156 {
1157 error(location, "layout qualifier only valid for interface blocks",
1158 getMatrixPackingString(layoutQualifier.matrixPacking));
1159 return;
1160 }
1161
1162 if (layoutQualifier.blockStorage != EbsUnspecified)
1163 {
1164 error(location, "layout qualifier only valid for interface blocks",
1165 getBlockStorageString(layoutQualifier.blockStorage));
1166 return;
1167 }
1168
1169 if (qualifier == EvqFragmentOut)
1170 {
1171 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1172 {
1173 error(location, "invalid layout qualifier combination", "yuv");
1174 return;
1175 }
1176 }
1177 else
1178 {
1179 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1180 }
1181
1182 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
1183 if (mShaderVersion >= 310 && qualifier == EvqUniform)
1184 {
1185 canHaveLocation = true;
1186 // We're not checking whether the uniform location is in range here since that depends on
1187 // the type of the variable.
1188 // The type can only be fully determined for non-empty declarations.
1189 }
1190 if (!canHaveLocation)
1191 {
1192 checkLocationIsNotSpecified(location, layoutQualifier);
1193 }
1194}
1195
Martin Radevb8b01222016-11-20 23:25:53 +02001196void TParseContext::emptyDeclarationErrorCheck(const TPublicType &publicType,
1197 const TSourceLoc &location)
1198{
1199 if (publicType.isUnsizedArray())
1200 {
1201 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1202 // error. It is assumed that this applies to empty declarations as well.
1203 error(location, "empty array declaration needs to specify a size", "");
1204 }
Martin Radevb8b01222016-11-20 23:25:53 +02001205}
1206
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001207// These checks are done for all declarations that are non-empty. They're done for non-empty
1208// declarations starting a declarator list, and declarators that follow an empty declaration.
1209void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1210 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001211{
Olli Etuahofa33d582015-04-09 14:33:12 +03001212 switch (publicType.qualifier)
1213 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001214 case EvqVaryingIn:
1215 case EvqVaryingOut:
1216 case EvqAttribute:
1217 case EvqVertexIn:
1218 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001219 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001220 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001221 {
1222 error(identifierLocation, "cannot be used with a structure",
1223 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001224 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001225 }
Olli Etuahofa33d582015-04-09 14:33:12 +03001226
Jamie Madillb98c3a82015-07-23 14:26:04 -04001227 default:
1228 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001229 }
1230
Jamie Madillb98c3a82015-07-23 14:26:04 -04001231 if (publicType.qualifier != EvqUniform &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001232 !checkIsNotSampler(identifierLocation, publicType.typeSpecifierNonArray,
1233 "samplers must be uniform"))
Olli Etuahofa33d582015-04-09 14:33:12 +03001234 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001235 return;
Olli Etuahofa33d582015-04-09 14:33:12 +03001236 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001237 if (publicType.qualifier != EvqUniform &&
1238 !checkIsNotImage(identifierLocation, publicType.typeSpecifierNonArray,
1239 "images must be uniform"))
1240 {
1241 return;
1242 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001243
Andrei Volykhina5527072017-03-22 16:46:30 +03001244 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1245 publicType.qualifier != EvqConst) &&
1246 publicType.getBasicType() == EbtYuvCscStandardEXT)
1247 {
1248 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1249 getQualifierString(publicType.qualifier));
1250 return;
1251 }
1252
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001253 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1254 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001255 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1256 // But invalid shaders may still reach here with an unsized array declaration.
1257 if (!publicType.isUnsizedArray())
1258 {
1259 TType type(publicType);
1260 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1261 publicType.layoutQualifier);
1262 }
1263 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001264
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001265 // check for layout qualifier issues
1266 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001267
Martin Radev2cc85b32016-08-05 16:22:53 +03001268 if (IsImage(publicType.getBasicType()))
1269 {
1270
1271 switch (layoutQualifier.imageInternalFormat)
1272 {
1273 case EiifRGBA32F:
1274 case EiifRGBA16F:
1275 case EiifR32F:
1276 case EiifRGBA8:
1277 case EiifRGBA8_SNORM:
1278 if (!IsFloatImage(publicType.getBasicType()))
1279 {
1280 error(identifierLocation,
1281 "internal image format requires a floating image type",
1282 getBasicString(publicType.getBasicType()));
1283 return;
1284 }
1285 break;
1286 case EiifRGBA32I:
1287 case EiifRGBA16I:
1288 case EiifRGBA8I:
1289 case EiifR32I:
1290 if (!IsIntegerImage(publicType.getBasicType()))
1291 {
1292 error(identifierLocation,
1293 "internal image format requires an integer image type",
1294 getBasicString(publicType.getBasicType()));
1295 return;
1296 }
1297 break;
1298 case EiifRGBA32UI:
1299 case EiifRGBA16UI:
1300 case EiifRGBA8UI:
1301 case EiifR32UI:
1302 if (!IsUnsignedImage(publicType.getBasicType()))
1303 {
1304 error(identifierLocation,
1305 "internal image format requires an unsigned image type",
1306 getBasicString(publicType.getBasicType()));
1307 return;
1308 }
1309 break;
1310 case EiifUnspecified:
1311 error(identifierLocation, "layout qualifier", "No image internal format specified");
1312 return;
1313 default:
1314 error(identifierLocation, "layout qualifier", "unrecognized token");
1315 return;
1316 }
1317
1318 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1319 switch (layoutQualifier.imageInternalFormat)
1320 {
1321 case EiifR32F:
1322 case EiifR32I:
1323 case EiifR32UI:
1324 break;
1325 default:
1326 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1327 {
1328 error(identifierLocation, "layout qualifier",
1329 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1330 "image variables must be qualified readonly and/or writeonly");
1331 return;
1332 }
1333 break;
1334 }
1335 }
1336 else
1337 {
Olli Etuaho43364892017-02-13 16:00:12 +00001338 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Martin Radev2cc85b32016-08-05 16:22:53 +03001339
Olli Etuaho43364892017-02-13 16:00:12 +00001340 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1341 }
1342}
Martin Radev2cc85b32016-08-05 16:22:53 +03001343
Olli Etuaho43364892017-02-13 16:00:12 +00001344void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1345{
1346 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
1347 int arraySize = type.isArray() ? type.getArraySize() : 1;
1348 if (IsImage(type.getBasicType()))
1349 {
1350 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding, arraySize);
1351 }
1352 else if (IsSampler(type.getBasicType()))
1353 {
1354 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding, arraySize);
1355 }
1356 else
1357 {
1358 ASSERT(!IsOpaqueType(type.getBasicType()));
1359 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001360 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001361}
1362
Olli Etuaho856c4972016-08-08 11:38:39 +03001363void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1364 const TString &layoutQualifierName,
1365 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001366{
1367
1368 if (mShaderVersion < versionRequired)
1369 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001370 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001371 }
1372}
1373
Olli Etuaho856c4972016-08-08 11:38:39 +03001374bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1375 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001376{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001377 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001378 for (size_t i = 0u; i < localSize.size(); ++i)
1379 {
1380 if (localSize[i] != -1)
1381 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001382 error(location,
1383 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1384 "global layout declaration",
1385 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001386 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001387 }
1388 }
1389
Olli Etuaho8a176262016-08-16 14:23:01 +03001390 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001391}
1392
Olli Etuaho43364892017-02-13 16:00:12 +00001393void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001394 TLayoutImageInternalFormat internalFormat)
1395{
1396 if (internalFormat != EiifUnspecified)
1397 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001398 error(location, "invalid layout qualifier: only valid when used with images",
1399 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001400 }
Olli Etuaho43364892017-02-13 16:00:12 +00001401}
1402
1403void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1404{
1405 if (binding != -1)
1406 {
1407 error(location,
1408 "invalid layout qualifier: only valid when used with opaque types or blocks",
1409 "binding");
1410 }
1411}
1412
1413void TParseContext::checkImageBindingIsValid(const TSourceLoc &location, int binding, int arraySize)
1414{
1415 // Expects arraySize to be 1 when setting binding for only a single variable.
1416 if (binding >= 0 && binding + arraySize > mMaxImageUnits)
1417 {
1418 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1419 }
1420}
1421
1422void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1423 int binding,
1424 int arraySize)
1425{
1426 // Expects arraySize to be 1 when setting binding for only a single variable.
1427 if (binding >= 0 && binding + arraySize > mMaxCombinedTextureImageUnits)
1428 {
1429 error(location, "sampler binding greater than maximum texture units", "binding");
1430 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001431}
1432
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001433void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1434 int objectLocationCount,
1435 const TLayoutQualifier &layoutQualifier)
1436{
1437 int loc = layoutQualifier.location;
1438 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1439 {
1440 error(location, "Uniform location out of range", "location");
1441 }
1442}
1443
Andrei Volykhina5527072017-03-22 16:46:30 +03001444void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1445{
1446 if (yuv != false)
1447 {
1448 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1449 }
1450}
1451
Olli Etuaho383b7912016-08-05 11:22:59 +03001452void TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate,
Olli Etuaho856c4972016-08-08 11:38:39 +03001453 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001454{
1455 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1456 {
1457 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1458 if (qual == EvqOut || qual == EvqInOut)
1459 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001460 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
Olli Etuaho8a176262016-08-16 14:23:01 +03001461 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001462 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001463 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001464 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuahoec9232b2017-03-27 17:01:37 +03001465 fnCall->getFunctionSymbolInfo()->getName().c_str());
Olli Etuaho383b7912016-08-05 11:22:59 +03001466 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001467 }
1468 }
1469 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001470}
1471
Martin Radev70866b82016-07-22 15:27:42 +03001472void TParseContext::checkInvariantVariableQualifier(bool invariant,
1473 const TQualifier qualifier,
1474 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001475{
Martin Radev70866b82016-07-22 15:27:42 +03001476 if (!invariant)
1477 return;
1478
1479 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001480 {
Martin Radev70866b82016-07-22 15:27:42 +03001481 // input variables in the fragment shader can be also qualified as invariant
1482 if (!sh::CanBeInvariantESSL1(qualifier))
1483 {
1484 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1485 }
1486 }
1487 else
1488 {
1489 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1490 {
1491 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1492 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001493 }
1494}
1495
Arun Patole7e7e68d2015-05-22 12:02:25 +05301496bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001497{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001498 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001499 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1500 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001501}
1502
Arun Patole7e7e68d2015-05-22 12:02:25 +05301503bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001504{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001505 return ::IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001506}
1507
Jamie Madillb98c3a82015-07-23 14:26:04 -04001508void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1509 const char *extName,
1510 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001511{
1512 pp::SourceLocation srcLoc;
1513 srcLoc.file = loc.first_file;
1514 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001515 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001516}
1517
Jamie Madillb98c3a82015-07-23 14:26:04 -04001518void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1519 const char *name,
1520 const char *value,
1521 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001522{
1523 pp::SourceLocation srcLoc;
1524 srcLoc.file = loc.first_file;
1525 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001526 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001527}
1528
Martin Radev4c4c8e72016-08-04 12:25:34 +03001529sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001530{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001531 sh::WorkGroupSize result;
Martin Radev802abe02016-08-04 17:48:32 +03001532 for (size_t i = 0u; i < result.size(); ++i)
1533 {
1534 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1535 {
1536 result[i] = 1;
1537 }
1538 else
1539 {
1540 result[i] = mComputeShaderLocalSize[i];
1541 }
1542 }
1543 return result;
1544}
1545
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001546/////////////////////////////////////////////////////////////////////////////////
1547//
1548// Non-Errors.
1549//
1550/////////////////////////////////////////////////////////////////////////////////
1551
Jamie Madill5c097022014-08-20 16:38:32 -04001552const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1553 const TString *name,
1554 const TSymbol *symbol)
1555{
Yunchao Hed7297bf2017-04-19 15:27:10 +08001556 const TVariable *variable = nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001557
1558 if (!symbol)
1559 {
1560 error(location, "undeclared identifier", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001561 }
1562 else if (!symbol->isVariable())
1563 {
1564 error(location, "variable expected", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001565 }
1566 else
1567 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001568 variable = static_cast<const TVariable *>(symbol);
Jamie Madill5c097022014-08-20 16:38:32 -04001569
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001570 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
Olli Etuaho383b7912016-08-05 11:22:59 +03001571 !variable->getExtension().empty())
Jamie Madill5c097022014-08-20 16:38:32 -04001572 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001573 checkCanUseExtension(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001574 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001575
1576 // Reject shaders using both gl_FragData and gl_FragColor
1577 TQualifier qualifier = variable->getType().getQualifier();
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001578 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001579 {
1580 mUsesFragData = true;
1581 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001582 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001583 {
1584 mUsesFragColor = true;
1585 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001586 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1587 {
1588 mUsesSecondaryOutputs = true;
1589 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001590
1591 // This validation is not quite correct - it's only an error to write to
1592 // both FragData and FragColor. For simplicity, and because users shouldn't
1593 // be rewarded for reading from undefined varaibles, return an error
1594 // if they are both referenced, rather than assigned.
1595 if (mUsesFragData && mUsesFragColor)
1596 {
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001597 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1598 if (mUsesSecondaryOutputs)
1599 {
1600 errorMessage =
1601 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1602 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1603 }
1604 error(location, errorMessage, name->c_str());
Jamie Madill14e95b32015-05-07 10:10:41 -04001605 }
Martin Radevb0883602016-08-04 17:48:58 +03001606
1607 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1608 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1609 qualifier == EvqWorkGroupSize)
1610 {
1611 error(location,
1612 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1613 "gl_WorkGroupSize");
1614 }
Jamie Madill5c097022014-08-20 16:38:32 -04001615 }
1616
1617 if (!variable)
1618 {
1619 TType type(EbtFloat, EbpUndefined);
1620 TVariable *fakeVariable = new TVariable(name, type);
1621 symbolTable.declare(fakeVariable);
1622 variable = fakeVariable;
1623 }
1624
1625 return variable;
1626}
1627
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001628TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1629 const TString *name,
1630 const TSymbol *symbol)
1631{
1632 const TVariable *variable = getNamedVariable(location, name, symbol);
1633
Olli Etuaho09b04a22016-12-15 13:30:26 +00001634 if (variable->getType().getQualifier() == EvqViewIDOVR && IsWebGLBasedSpec(mShaderSpec) &&
1635 mShaderType == GL_FRAGMENT_SHADER && !isExtensionEnabled("GL_OVR_multiview2"))
1636 {
1637 // WEBGL_multiview spec
1638 error(location, "Need to enable OVR_multiview2 to use gl_ViewID_OVR in fragment shader",
1639 "gl_ViewID_OVR");
1640 }
1641
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001642 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001643 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001644 const TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001645 return intermediate.addConstantUnion(constArray, variable->getType(), location);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001646 }
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001647 else if (variable->getType().getQualifier() == EvqWorkGroupSize &&
1648 mComputeShaderLocalSizeDeclared)
1649 {
1650 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1651 // needs to be added to the AST as a constant and not as a symbol.
1652 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1653 TConstantUnion *constArray = new TConstantUnion[3];
1654 for (size_t i = 0; i < 3; ++i)
1655 {
1656 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1657 }
1658
1659 ASSERT(variable->getType().getBasicType() == EbtUInt);
1660 ASSERT(variable->getType().getObjectSize() == 3);
1661
1662 TType type(variable->getType());
1663 type.setQualifier(EvqConst);
1664 return intermediate.addConstantUnion(constArray, type, location);
1665 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001666 else
1667 {
1668 return intermediate.addSymbol(variable->getUniqueId(), variable->getName(),
1669 variable->getType(), location);
1670 }
1671}
1672
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001673//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001674// Initializers show up in several places in the grammar. Have one set of
1675// code to handle them here.
1676//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001677// Returns true on error, false if no error
1678//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001679bool TParseContext::executeInitializer(const TSourceLoc &line,
1680 const TString &identifier,
1681 const TPublicType &pType,
1682 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001683 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001684{
Olli Etuaho13389b62016-10-16 11:48:18 +01001685 ASSERT(initNode != nullptr);
1686 ASSERT(*initNode == nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001687 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001688
Olli Etuaho2935c582015-04-08 14:32:06 +03001689 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001690 if (type.isUnsizedArray())
1691 {
Olli Etuaho02bd82c2016-11-03 10:29:43 +00001692 // We have not checked yet whether the initializer actually is an array or not.
1693 if (initializer->isArray())
1694 {
1695 type.setArraySize(initializer->getArraySize());
1696 }
1697 else
1698 {
1699 // Having a non-array initializer for an unsized array will result in an error later,
1700 // so we don't generate an error message here.
1701 type.setArraySize(1u);
1702 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001703 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001704 if (!declareVariable(line, identifier, type, &variable))
1705 {
1706 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001707 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001708
Olli Etuahob0c645e2015-05-12 14:25:36 +03001709 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001710 if (symbolTable.atGlobalLevel() &&
1711 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001712 {
1713 // Error message does not completely match behavior with ESSL 1.00, but
1714 // we want to steer developers towards only using constant expressions.
1715 error(line, "global variable initializers must be constant expressions", "=");
1716 return true;
1717 }
1718 if (globalInitWarning)
1719 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001720 warning(
1721 line,
1722 "global variable initializers should be constant expressions "
1723 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1724 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001725 }
1726
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001727 //
1728 // identifier must be of type constant, a global, or a temporary
1729 //
1730 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301731 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1732 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001733 error(line, " cannot initialize this type of qualifier ",
1734 variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001735 return true;
1736 }
1737 //
1738 // test for and propagate constant
1739 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001740
Arun Patole7e7e68d2015-05-22 12:02:25 +05301741 if (qualifier == EvqConst)
1742 {
1743 if (qualifier != initializer->getType().getQualifier())
1744 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001745 std::stringstream reasonStream;
1746 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1747 << "'";
1748 std::string reason = reasonStream.str();
1749 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001750 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001751 return true;
1752 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301753 if (type != initializer->getType())
1754 {
1755 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001756 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001757 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001758 return true;
1759 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001760
1761 // Save the constant folded value to the variable if possible. For example array
1762 // initializers are not folded, since that way copying the array literal to multiple places
1763 // in the shader is avoided.
1764 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1765 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301766 if (initializer->getAsConstantUnion())
1767 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001768 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho13389b62016-10-16 11:48:18 +01001769 *initNode = nullptr;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001770 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301771 }
1772 else if (initializer->getAsSymbolNode())
1773 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001774 const TSymbol *symbol =
1775 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1776 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001777
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001778 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001779 if (constArray)
1780 {
1781 variable->shareConstPointer(constArray);
Olli Etuaho13389b62016-10-16 11:48:18 +01001782 *initNode = nullptr;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001783 return false;
1784 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001785 }
1786 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001787
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001788 TIntermSymbol *intermSymbol = intermediate.addSymbol(
1789 variable->getUniqueId(), variable->getName(), variable->getType(), line);
Olli Etuaho13389b62016-10-16 11:48:18 +01001790 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1791 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001792 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001793 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1794 return true;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001795 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001796
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001797 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001798}
1799
Olli Etuaho0e3aee32016-10-27 12:56:38 +01001800void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
1801{
1802 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
1803 typeSpecifier->getBasicType());
1804
1805 if (mShaderVersion < 300 && typeSpecifier->array)
1806 {
1807 error(typeSpecifier->getLine(), "not supported", "first-class array");
1808 typeSpecifier->clearArrayness();
1809 }
1810}
1811
Martin Radev70866b82016-07-22 15:27:42 +03001812TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301813 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001814{
Olli Etuaho77ba4082016-12-16 12:01:18 +00001815 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001816
Martin Radev70866b82016-07-22 15:27:42 +03001817 TPublicType returnType = typeSpecifier;
1818 returnType.qualifier = typeQualifier.qualifier;
1819 returnType.invariant = typeQualifier.invariant;
1820 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03001821 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03001822 returnType.precision = typeSpecifier.precision;
1823
1824 if (typeQualifier.precision != EbpUndefined)
1825 {
1826 returnType.precision = typeQualifier.precision;
1827 }
1828
Martin Radev4a9cd802016-09-01 16:51:51 +03001829 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
1830 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03001831
Martin Radev4a9cd802016-09-01 16:51:51 +03001832 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
1833 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03001834
Martin Radev4a9cd802016-09-01 16:51:51 +03001835 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03001836
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001837 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001838 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001839 if (typeSpecifier.array)
1840 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001841 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001842 returnType.clearArrayness();
1843 }
1844
Martin Radev70866b82016-07-22 15:27:42 +03001845 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001846 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001847 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001848 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001849 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001850 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001851
Martin Radev70866b82016-07-22 15:27:42 +03001852 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001853 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001854 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001855 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001856 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001857 }
1858 }
1859 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001860 {
Martin Radev70866b82016-07-22 15:27:42 +03001861 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03001862 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001863 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03001864 }
Martin Radev70866b82016-07-22 15:27:42 +03001865 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
1866 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001867 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001868 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
1869 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001870 }
Martin Radev70866b82016-07-22 15:27:42 +03001871 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03001872 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001873 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03001874 "in");
Martin Radev802abe02016-08-04 17:48:32 +03001875 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001876 }
1877
1878 return returnType;
1879}
1880
Olli Etuaho856c4972016-08-08 11:38:39 +03001881void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
1882 const TPublicType &type,
1883 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03001884{
1885 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03001886 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03001887 {
1888 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001889 }
1890
1891 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
1892 switch (qualifier)
1893 {
1894 case EvqVertexIn:
1895 // ESSL 3.00 section 4.3.4
1896 if (type.array)
1897 {
1898 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001899 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001900 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03001901 return;
1902 case EvqFragmentOut:
1903 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03001904 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03001905 {
1906 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001907 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001908 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03001909 return;
1910 default:
1911 break;
1912 }
1913
1914 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
1915 // restrictions.
1916 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03001917 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
1918 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03001919 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
1920 {
1921 error(qualifierLocation, "must use 'flat' interpolation here",
1922 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001923 }
1924
Martin Radev4a9cd802016-09-01 16:51:51 +03001925 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03001926 {
1927 // ESSL 3.00 sections 4.3.4 and 4.3.6.
1928 // These restrictions are only implied by the ESSL 3.00 spec, but
1929 // the ESSL 3.10 spec lists these restrictions explicitly.
1930 if (type.array)
1931 {
1932 error(qualifierLocation, "cannot be an array of structures",
1933 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001934 }
1935 if (type.isStructureContainingArrays())
1936 {
1937 error(qualifierLocation, "cannot be a structure containing an array",
1938 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001939 }
1940 if (type.isStructureContainingType(EbtStruct))
1941 {
1942 error(qualifierLocation, "cannot be a structure containing a structure",
1943 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001944 }
1945 if (type.isStructureContainingType(EbtBool))
1946 {
1947 error(qualifierLocation, "cannot be a structure containing a bool",
1948 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001949 }
1950 }
1951}
1952
Martin Radev2cc85b32016-08-05 16:22:53 +03001953void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
1954{
1955 if (qualifier.getType() == QtStorage)
1956 {
1957 const TStorageQualifierWrapper &storageQualifier =
1958 static_cast<const TStorageQualifierWrapper &>(qualifier);
1959 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
1960 !symbolTable.atGlobalLevel())
1961 {
1962 error(storageQualifier.getLine(),
1963 "Local variables can only use the const storage qualifier.",
1964 storageQualifier.getQualifierString().c_str());
1965 }
1966 }
1967}
1968
Olli Etuaho43364892017-02-13 16:00:12 +00001969void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03001970 const TSourceLoc &location)
1971{
1972 if (memoryQualifier.readonly)
1973 {
1974 error(location, "Only allowed with images.", "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03001975 }
1976 if (memoryQualifier.writeonly)
1977 {
1978 error(location, "Only allowed with images.", "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03001979 }
Martin Radev049edfa2016-11-11 14:35:37 +02001980 if (memoryQualifier.coherent)
1981 {
1982 error(location, "Only allowed with images.", "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02001983 }
1984 if (memoryQualifier.restrictQualifier)
1985 {
1986 error(location, "Only allowed with images.", "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02001987 }
1988 if (memoryQualifier.volatileQualifier)
1989 {
1990 error(location, "Only allowed with images.", "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02001991 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001992}
1993
Olli Etuaho13389b62016-10-16 11:48:18 +01001994TIntermDeclaration *TParseContext::parseSingleDeclaration(
1995 TPublicType &publicType,
1996 const TSourceLoc &identifierOrTypeLocation,
1997 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001998{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001999 TType type(publicType);
2000 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2001 mDirectiveHandler.pragma().stdgl.invariantAll)
2002 {
2003 TQualifier qualifier = type.getQualifier();
2004
2005 // The directive handler has already taken care of rejecting invalid uses of this pragma
2006 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2007 // affected variable declarations:
2008 //
2009 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2010 // elsewhere, in TranslatorGLSL.)
2011 //
2012 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2013 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2014 // the way this is currently implemented we have to enable this compiler option before
2015 // parsing the shader and determining the shading language version it uses. If this were
2016 // implemented as a post-pass, the workaround could be more targeted.
2017 //
2018 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2019 // the specification, but there are desktop OpenGL drivers that expect that this is the
2020 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2021 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2022 {
2023 type.setInvariant(true);
2024 }
2025 }
2026
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002027 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2028 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002029
Olli Etuahobab4c082015-04-24 16:38:49 +03002030 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002031 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002032
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002033 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002034 if (emptyDeclaration)
2035 {
Martin Radevb8b01222016-11-20 23:25:53 +02002036 emptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002037 // In most cases we don't need to create a symbol node for an empty declaration.
2038 // But if the empty declaration is declaring a struct type, the symbol node will store that.
2039 if (type.getBasicType() == EbtStruct)
2040 {
2041 symbol = intermediate.addSymbol(0, "", type, identifierOrTypeLocation);
2042 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002043 }
2044 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002045 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002046 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002047
Olli Etuaho856c4972016-08-08 11:38:39 +03002048 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002049
Olli Etuaho2935c582015-04-08 14:32:06 +03002050 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002051 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002052
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002053 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002054 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002055 symbol = intermediate.addSymbol(variable->getUniqueId(), identifier, type,
2056 identifierOrTypeLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002057 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002058 }
2059
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002060 TIntermDeclaration *declaration = new TIntermDeclaration();
2061 declaration->setLine(identifierOrTypeLocation);
2062 if (symbol)
2063 {
2064 declaration->appendDeclarator(symbol);
2065 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002066 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002067}
2068
Olli Etuaho13389b62016-10-16 11:48:18 +01002069TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
2070 const TSourceLoc &identifierLocation,
2071 const TString &identifier,
2072 const TSourceLoc &indexLocation,
2073 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04002074{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002075 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002076
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002077 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2078 identifierLocation);
2079
2080 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002081
Olli Etuaho856c4972016-08-08 11:38:39 +03002082 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002083
Olli Etuaho8a176262016-08-16 14:23:01 +03002084 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002085
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002086 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002087
Olli Etuaho856c4972016-08-08 11:38:39 +03002088 unsigned int size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002089 // Make the type an array even if size check failed.
2090 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2091 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04002092
Olli Etuaho2935c582015-04-08 14:32:06 +03002093 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002094 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002095
Olli Etuaho13389b62016-10-16 11:48:18 +01002096 TIntermDeclaration *declaration = new TIntermDeclaration();
2097 declaration->setLine(identifierLocation);
2098
Olli Etuahoe7847b02015-03-16 11:56:12 +02002099 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002100 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01002101 {
Jamie Madill60ed9812013-06-06 11:56:46 -04002102 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01002103 declaration->appendDeclarator(symbol);
2104 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002105
Olli Etuaho13389b62016-10-16 11:48:18 +01002106 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002107}
2108
Olli Etuaho13389b62016-10-16 11:48:18 +01002109TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2110 const TSourceLoc &identifierLocation,
2111 const TString &identifier,
2112 const TSourceLoc &initLocation,
2113 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002114{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002115 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002116
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002117 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2118 identifierLocation);
2119
2120 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002121
Olli Etuaho13389b62016-10-16 11:48:18 +01002122 TIntermDeclaration *declaration = new TIntermDeclaration();
2123 declaration->setLine(identifierLocation);
2124
2125 TIntermBinary *initNode = nullptr;
2126 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002127 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002128 if (initNode)
2129 {
2130 declaration->appendDeclarator(initNode);
2131 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002132 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002133 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002134}
2135
Olli Etuaho13389b62016-10-16 11:48:18 +01002136TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Jamie Madillb98c3a82015-07-23 14:26:04 -04002137 TPublicType &publicType,
2138 const TSourceLoc &identifierLocation,
2139 const TString &identifier,
2140 const TSourceLoc &indexLocation,
2141 TIntermTyped *indexExpression,
2142 const TSourceLoc &initLocation,
2143 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002144{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002145 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002146
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002147 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2148 identifierLocation);
2149
2150 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002151
Olli Etuaho8a176262016-08-16 14:23:01 +03002152 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002153
2154 TPublicType arrayType(publicType);
2155
Olli Etuaho856c4972016-08-08 11:38:39 +03002156 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002157 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2158 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002159 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002160 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002161 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002162 }
2163 // Make the type an array even if size check failed.
2164 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2165 arrayType.setArraySize(size);
2166
Olli Etuaho13389b62016-10-16 11:48:18 +01002167 TIntermDeclaration *declaration = new TIntermDeclaration();
2168 declaration->setLine(identifierLocation);
2169
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002170 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002171 TIntermBinary *initNode = nullptr;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002172 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
2173 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002174 if (initNode)
2175 {
2176 declaration->appendDeclarator(initNode);
2177 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002178 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002179
2180 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002181}
2182
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002183TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002184 const TTypeQualifierBuilder &typeQualifierBuilder,
2185 const TSourceLoc &identifierLoc,
2186 const TString *identifier,
2187 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002188{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002189 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002190
Martin Radev70866b82016-07-22 15:27:42 +03002191 if (!typeQualifier.invariant)
2192 {
2193 error(identifierLoc, "Expected invariant", identifier->c_str());
2194 return nullptr;
2195 }
2196 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2197 {
2198 return nullptr;
2199 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002200 if (!symbol)
2201 {
2202 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002203 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002204 }
Martin Radev70866b82016-07-22 15:27:42 +03002205 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002206 {
Martin Radev70866b82016-07-22 15:27:42 +03002207 error(identifierLoc, "invariant declaration specifies qualifier",
2208 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002209 }
Martin Radev70866b82016-07-22 15:27:42 +03002210 if (typeQualifier.precision != EbpUndefined)
2211 {
2212 error(identifierLoc, "invariant declaration specifies precision",
2213 getPrecisionString(typeQualifier.precision));
2214 }
2215 if (!typeQualifier.layoutQualifier.isEmpty())
2216 {
2217 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2218 }
2219
2220 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
2221 ASSERT(variable);
2222 const TType &type = variable->getType();
2223
2224 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2225 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002226 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002227
2228 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2229
2230 TIntermSymbol *intermSymbol =
2231 intermediate.addSymbol(variable->getUniqueId(), *identifier, type, identifierLoc);
2232
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002233 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002234}
2235
Olli Etuaho13389b62016-10-16 11:48:18 +01002236void TParseContext::parseDeclarator(TPublicType &publicType,
2237 const TSourceLoc &identifierLocation,
2238 const TString &identifier,
2239 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002240{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002241 // If the declaration starting this declarator list was empty (example: int,), some checks were
2242 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002243 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002244 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002245 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2246 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002247 }
2248
Olli Etuaho856c4972016-08-08 11:38:39 +03002249 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002250
Olli Etuaho856c4972016-08-08 11:38:39 +03002251 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002252
Olli Etuaho2935c582015-04-08 14:32:06 +03002253 TVariable *variable = nullptr;
Olli Etuaho43364892017-02-13 16:00:12 +00002254 TType type(publicType);
2255 declareVariable(identifierLocation, identifier, type, &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002256
Olli Etuaho43364892017-02-13 16:00:12 +00002257 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, type, identifierLocation);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002258 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01002259 {
Jamie Madill502d66f2013-06-20 11:55:52 -04002260 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01002261 declarationOut->appendDeclarator(symbol);
2262 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002263}
2264
Olli Etuaho13389b62016-10-16 11:48:18 +01002265void TParseContext::parseArrayDeclarator(TPublicType &publicType,
2266 const TSourceLoc &identifierLocation,
2267 const TString &identifier,
2268 const TSourceLoc &arrayLocation,
2269 TIntermTyped *indexExpression,
2270 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002271{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002272 // If the declaration starting this declarator list was empty (example: int,), some checks were
2273 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002274 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002275 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002276 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2277 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002278 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002279
Olli Etuaho856c4972016-08-08 11:38:39 +03002280 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002281
Olli Etuaho856c4972016-08-08 11:38:39 +03002282 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002283
Olli Etuaho8a176262016-08-16 14:23:01 +03002284 if (checkIsValidTypeAndQualifierForArray(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002285 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002286 TType arrayType = TType(publicType);
Olli Etuaho856c4972016-08-08 11:38:39 +03002287 unsigned int size = checkIsValidArraySize(arrayLocation, indexExpression);
Olli Etuaho693c9aa2015-04-07 17:50:36 +03002288 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002289
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002290 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002291 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002292
Jamie Madillb98c3a82015-07-23 14:26:04 -04002293 TIntermSymbol *symbol =
2294 intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002295 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002296 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002297
Olli Etuaho13389b62016-10-16 11:48:18 +01002298 declarationOut->appendDeclarator(symbol);
Jamie Madill502d66f2013-06-20 11:55:52 -04002299 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002300}
2301
Olli Etuaho13389b62016-10-16 11:48:18 +01002302void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2303 const TSourceLoc &identifierLocation,
2304 const TString &identifier,
2305 const TSourceLoc &initLocation,
2306 TIntermTyped *initializer,
2307 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002308{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002309 // If the declaration starting this declarator list was empty (example: int,), some checks were
2310 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002311 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002312 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002313 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2314 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002315 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002316
Olli Etuaho856c4972016-08-08 11:38:39 +03002317 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002318
Olli Etuaho13389b62016-10-16 11:48:18 +01002319 TIntermBinary *initNode = nullptr;
2320 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002321 {
2322 //
2323 // build the intermediate representation
2324 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002325 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002326 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002327 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002328 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002329 }
2330}
2331
Olli Etuaho13389b62016-10-16 11:48:18 +01002332void TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
2333 const TSourceLoc &identifierLocation,
2334 const TString &identifier,
2335 const TSourceLoc &indexLocation,
2336 TIntermTyped *indexExpression,
2337 const TSourceLoc &initLocation,
2338 TIntermTyped *initializer,
2339 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002340{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002341 // If the declaration starting this declarator list was empty (example: int,), some checks were
2342 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002343 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002344 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002345 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2346 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002347 }
2348
Olli Etuaho856c4972016-08-08 11:38:39 +03002349 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002350
Olli Etuaho8a176262016-08-16 14:23:01 +03002351 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002352
2353 TPublicType arrayType(publicType);
2354
Olli Etuaho856c4972016-08-08 11:38:39 +03002355 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002356 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2357 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002358 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002359 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002360 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002361 }
2362 // Make the type an array even if size check failed.
2363 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2364 arrayType.setArraySize(size);
2365
2366 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002367 TIntermBinary *initNode = nullptr;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002368 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
2369 {
2370 if (initNode)
2371 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002372 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002373 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002374 }
2375}
2376
Martin Radev70866b82016-07-22 15:27:42 +03002377void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002378{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002379 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002380 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002381
Martin Radev70866b82016-07-22 15:27:42 +03002382 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2383 typeQualifier.line);
2384
Jamie Madillc2128ff2016-07-04 10:26:17 -04002385 // It should never be the case, but some strange parser errors can send us here.
2386 if (layoutQualifier.isEmpty())
2387 {
2388 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002389 return;
2390 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002391
Martin Radev802abe02016-08-04 17:48:32 +03002392 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002393 {
Olli Etuaho43364892017-02-13 16:00:12 +00002394 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002395 return;
2396 }
2397
Olli Etuaho43364892017-02-13 16:00:12 +00002398 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2399
2400 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002401
2402 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2403
Andrei Volykhina5527072017-03-22 16:46:30 +03002404 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
2405
Martin Radev802abe02016-08-04 17:48:32 +03002406 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002407 {
Martin Radev802abe02016-08-04 17:48:32 +03002408 if (mComputeShaderLocalSizeDeclared &&
2409 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2410 {
2411 error(typeQualifier.line, "Work group size does not match the previous declaration",
2412 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002413 return;
2414 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002415
Martin Radev802abe02016-08-04 17:48:32 +03002416 if (mShaderVersion < 310)
2417 {
2418 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002419 return;
2420 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002421
Martin Radev4c4c8e72016-08-04 12:25:34 +03002422 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03002423 {
2424 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002425 return;
2426 }
2427
2428 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
2429 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
2430
2431 const TConstantUnion *maxComputeWorkGroupSizeData =
2432 maxComputeWorkGroupSize->getConstPointer();
2433
2434 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
2435 {
2436 if (layoutQualifier.localSize[i] != -1)
2437 {
2438 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
2439 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
2440 if (mComputeShaderLocalSize[i] < 1 ||
2441 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
2442 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002443 std::stringstream reasonStream;
2444 reasonStream << "invalid value: Value must be at least 1 and no greater than "
2445 << maxComputeWorkGroupSizeValue;
2446 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03002447
Olli Etuaho4de340a2016-12-16 09:32:03 +00002448 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03002449 return;
2450 }
2451 }
2452 }
2453
2454 mComputeShaderLocalSizeDeclared = true;
2455 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00002456 else if (mMultiviewAvailable &&
2457 (isExtensionEnabled("GL_OVR_multiview") || isExtensionEnabled("GL_OVR_multiview2")) &&
2458 typeQualifier.qualifier == EvqVertexIn)
2459 {
2460 // This error is only specified in WebGL, but tightens unspecified behavior in the native
2461 // specification.
2462 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
2463 {
2464 error(typeQualifier.line, "Number of views does not match the previous declaration",
2465 "layout");
2466 return;
2467 }
2468
2469 if (layoutQualifier.numViews == -1)
2470 {
2471 error(typeQualifier.line, "No num_views specified", "layout");
2472 return;
2473 }
2474
2475 if (layoutQualifier.numViews > mMaxNumViews)
2476 {
2477 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
2478 "layout");
2479 return;
2480 }
2481
2482 mNumViews = layoutQualifier.numViews;
2483 }
Martin Radev802abe02016-08-04 17:48:32 +03002484 else
Jamie Madill1566ef72013-06-20 11:55:54 -04002485 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00002486 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03002487 {
Martin Radev802abe02016-08-04 17:48:32 +03002488 return;
2489 }
2490
2491 if (typeQualifier.qualifier != EvqUniform)
2492 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002493 error(typeQualifier.line, "invalid qualifier: global layout must be uniform",
2494 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03002495 return;
2496 }
2497
2498 if (mShaderVersion < 300)
2499 {
2500 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
2501 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002502 return;
2503 }
2504
Olli Etuaho09b04a22016-12-15 13:30:26 +00002505 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002506
2507 if (layoutQualifier.matrixPacking != EmpUnspecified)
2508 {
2509 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
2510 }
2511
2512 if (layoutQualifier.blockStorage != EbsUnspecified)
2513 {
2514 mDefaultBlockStorage = layoutQualifier.blockStorage;
2515 }
Jamie Madill1566ef72013-06-20 11:55:54 -04002516 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002517}
2518
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002519TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
2520 const TFunction &function,
2521 const TSourceLoc &location,
2522 bool insertParametersToSymbolTable)
2523{
Olli Etuahofe486322017-03-21 09:30:54 +00002524 TIntermFunctionPrototype *prototype =
2525 new TIntermFunctionPrototype(function.getReturnType(), TSymbolUniqueId(function));
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002526 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
2527 // point to the data that already exists in the symbol table.
2528 prototype->getFunctionSymbolInfo()->setFromFunction(function);
2529 prototype->setLine(location);
2530
2531 for (size_t i = 0; i < function.getParamCount(); i++)
2532 {
2533 const TConstParameter &param = function.getParam(i);
2534
2535 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
2536 // be used for unused args).
2537 if (param.name != nullptr)
2538 {
2539 TVariable *variable = new TVariable(param.name, *param.type);
2540
2541 // Insert the parameter in the symbol table.
2542 if (insertParametersToSymbolTable && !symbolTable.declare(variable))
2543 {
2544 error(location, "redefinition", variable->getName().c_str());
2545 prototype->appendParameter(intermediate.addSymbol(0, "", *param.type, location));
2546 continue;
2547 }
2548 TIntermSymbol *symbol = intermediate.addSymbol(
2549 variable->getUniqueId(), variable->getName(), variable->getType(), location);
2550 prototype->appendParameter(symbol);
2551 }
2552 else
2553 {
2554 prototype->appendParameter(intermediate.addSymbol(0, "", *param.type, location));
2555 }
2556 }
2557 return prototype;
2558}
2559
Olli Etuaho16c745a2017-01-16 17:02:27 +00002560TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
2561 const TFunction &parsedFunction,
2562 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002563{
Olli Etuaho476197f2016-10-11 13:59:08 +01002564 // Note: function found from the symbol table could be the same as parsedFunction if this is the
2565 // first declaration. Either way the instance in the symbol table is used to track whether the
2566 // function is declared multiple times.
2567 TFunction *function = static_cast<TFunction *>(
2568 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
2569 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02002570 {
2571 // ESSL 1.00.17 section 4.2.7.
2572 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
2573 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02002574 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002575 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02002576
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002577 TIntermFunctionPrototype *prototype =
2578 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002579
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002580 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002581
2582 if (!symbolTable.atGlobalLevel())
2583 {
2584 // ESSL 3.00.4 section 4.2.4.
2585 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002586 }
2587
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002588 return prototype;
2589}
2590
Olli Etuaho336b1472016-10-05 16:37:55 +01002591TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002592 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01002593 TIntermBlock *functionBody,
2594 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002595{
Olli Etuahof51fdd22016-10-03 10:03:40 +01002596 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002597 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
2598 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002599 error(location, "function does not return a value:",
2600 functionPrototype->getFunctionSymbolInfo()->getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002601 }
2602
Olli Etuahof51fdd22016-10-03 10:03:40 +01002603 if (functionBody == nullptr)
2604 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01002605 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002606 functionBody->setLine(location);
2607 }
Olli Etuaho336b1472016-10-05 16:37:55 +01002608 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002609 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01002610 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01002611
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002612 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002613 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002614}
2615
Olli Etuaho476197f2016-10-11 13:59:08 +01002616void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
2617 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002618 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04002619{
Olli Etuaho476197f2016-10-11 13:59:08 +01002620 ASSERT(function);
2621 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002622 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01002623 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002624
2625 if (builtIn)
2626 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002627 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002628 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002629 else
Jamie Madill185fb402015-06-12 15:48:48 -04002630 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002631 TFunction *prevDec = static_cast<TFunction *>(
2632 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
2633
2634 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
2635 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
2636 // occurance.
2637 if (*function != prevDec)
2638 {
2639 // Swap the parameters of the previous declaration to the parameters of the function
2640 // definition (parameter names may differ).
2641 prevDec->swapParameters(**function);
2642
2643 // The function definition will share the same symbol as any previous declaration.
2644 *function = prevDec;
2645 }
2646
2647 if ((*function)->isDefined())
2648 {
2649 error(location, "function already has a body", (*function)->getName().c_str());
2650 }
2651
2652 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04002653 }
Jamie Madill185fb402015-06-12 15:48:48 -04002654
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002655 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01002656 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002657 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04002658
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002659 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04002660 setLoopNestingLevel(0);
2661}
2662
Jamie Madillb98c3a82015-07-23 14:26:04 -04002663TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002664{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002665 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002666 // We don't know at this point whether this is a function definition or a prototype.
2667 // The definition production code will check for redefinitions.
2668 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002669 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002670 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
2671 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002672 //
2673 TFunction *prevDec =
2674 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302675
Martin Radevda6254b2016-12-14 17:00:36 +02002676 if (getShaderVersion() >= 300 &&
2677 symbolTable.hasUnmangledBuiltInForShaderVersion(function->getName().c_str(),
2678 getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302679 {
Martin Radevda6254b2016-12-14 17:00:36 +02002680 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302681 // Therefore overloading or redefining builtin functions is an error.
2682 error(location, "Name of a built-in function cannot be redeclared as function",
2683 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302684 }
2685 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002686 {
2687 if (prevDec->getReturnType() != function->getReturnType())
2688 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002689 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002690 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04002691 }
2692 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2693 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002694 if (prevDec->getParam(i).type->getQualifier() !=
2695 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002696 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002697 error(location,
2698 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002699 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04002700 }
2701 }
2702 }
2703
2704 //
2705 // Check for previously declared variables using the same name.
2706 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002707 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002708 if (prevSym)
2709 {
2710 if (!prevSym->isFunction())
2711 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002712 error(location, "redefinition of a function", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002713 }
2714 }
2715 else
2716 {
2717 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01002718 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04002719 }
2720
2721 // We're at the inner scope level of the function's arguments and body statement.
2722 // Add the function prototype to the surrounding scope instead.
2723 symbolTable.getOuterLevel()->insert(function);
2724
Olli Etuaho78d13742017-01-18 13:06:10 +00002725 // Raise error message if main function takes any parameters or return anything other than void
2726 if (function->getName() == "main")
2727 {
2728 if (function->getParamCount() > 0)
2729 {
2730 error(location, "function cannot take any parameter(s)", "main");
2731 }
2732 if (function->getReturnType().getBasicType() != EbtVoid)
2733 {
2734 error(location, "main function cannot return a value",
2735 function->getReturnType().getBasicString());
2736 }
2737 }
2738
Jamie Madill185fb402015-06-12 15:48:48 -04002739 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002740 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2741 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002742 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2743 //
2744 return function;
2745}
2746
Olli Etuaho9de84a52016-06-14 17:36:01 +03002747TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
2748 const TString *name,
2749 const TSourceLoc &location)
2750{
2751 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
2752 {
2753 error(location, "no qualifiers allowed for function return",
2754 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03002755 }
2756 if (!type.layoutQualifier.isEmpty())
2757 {
2758 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03002759 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002760 // make sure a sampler or an image is not involved as well...
Martin Radev4a9cd802016-09-01 16:51:51 +03002761 checkIsNotSampler(location, type.typeSpecifierNonArray,
2762 "samplers can't be function return values");
Martin Radev2cc85b32016-08-05 16:22:53 +03002763 checkIsNotImage(location, type.typeSpecifierNonArray, "images can't be function return values");
Olli Etuahoe29324f2016-06-15 10:58:03 +03002764 if (mShaderVersion < 300)
2765 {
2766 // Array return values are forbidden, but there's also no valid syntax for declaring array
2767 // return values in ESSL 1.00.
Olli Etuaho77ba4082016-12-16 12:01:18 +00002768 ASSERT(type.arraySize == 0 || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03002769
2770 if (type.isStructureContainingArrays())
2771 {
2772 // ESSL 1.00.17 section 6.1 Function Definitions
2773 error(location, "structures containing arrays can't be function return values",
2774 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03002775 }
2776 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03002777
2778 // Add the function as a prototype after parsing it (we do not support recursion)
2779 return new TFunction(name, new TType(type));
2780}
2781
Jamie Madill06145232015-05-13 13:10:01 -04002782TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002783{
Jamie Madill06145232015-05-13 13:10:01 -04002784 TPublicType publicType = publicTypeIn;
Martin Radev4a9cd802016-09-01 16:51:51 +03002785 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02002786 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002787 error(publicType.getLine(), "constructor can't be a structure definition",
2788 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02002789 }
2790
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002791 TOperator op = EOpNull;
Martin Radev4a9cd802016-09-01 16:51:51 +03002792 if (publicType.getUserDef())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002793 {
2794 op = EOpConstructStruct;
2795 }
2796 else
2797 {
Geoff Lang156d7192016-07-21 16:11:00 -04002798 op = sh::TypeToConstructorOperator(TType(publicType));
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002799 if (op == EOpNull)
2800 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002801 error(publicType.getLine(), "cannot construct this type",
2802 getBasicString(publicType.getBasicType()));
2803 publicType.setBasicType(EbtFloat);
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002804 op = EOpConstructFloat;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002805 }
2806 }
2807
Dmitry Skiba7f17a502015-06-22 15:08:39 -07002808 const TType *type = new TType(publicType);
Olli Etuaho72d10202017-01-19 15:58:30 +00002809 return new TFunction(nullptr, type, op);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002810}
2811
Jamie Madillb98c3a82015-07-23 14:26:04 -04002812// This function is used to test for the correctness of the parameters passed to various constructor
2813// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002814//
Olli Etuaho856c4972016-08-08 11:38:39 +03002815// 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 +00002816//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002817TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002818 TOperator op,
Olli Etuaho72d10202017-01-19 15:58:30 +00002819 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302820 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002821{
Olli Etuaho856c4972016-08-08 11:38:39 +03002822 if (type.isUnsizedArray())
2823 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002824 if (arguments->empty())
Olli Etuahobbe9fb52016-11-03 17:16:05 +00002825 {
2826 error(line, "implicitly sized array constructor must have at least one argument", "[]");
2827 type.setArraySize(1u);
2828 return TIntermTyped::CreateZero(type);
2829 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002830 type.setArraySize(static_cast<unsigned int>(arguments->size()));
Olli Etuaho856c4972016-08-08 11:38:39 +03002831 }
Olli Etuaho856c4972016-08-08 11:38:39 +03002832
Olli Etuaho72d10202017-01-19 15:58:30 +00002833 if (!checkConstructorArguments(line, arguments, op, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03002834 {
Olli Etuaho72d10202017-01-19 15:58:30 +00002835 return TIntermTyped::CreateZero(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03002836 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002837
Olli Etuahofe486322017-03-21 09:30:54 +00002838 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002839 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002840
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002841 TIntermTyped *constConstructor =
2842 intermediate.foldAggregateBuiltIn(constructorNode, mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002843 if (constConstructor)
2844 {
2845 return constConstructor;
2846 }
2847
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002848 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002849}
2850
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002851//
2852// Interface/uniform blocks
2853//
Olli Etuaho13389b62016-10-16 11:48:18 +01002854TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03002855 const TTypeQualifierBuilder &typeQualifierBuilder,
2856 const TSourceLoc &nameLine,
2857 const TString &blockName,
2858 TFieldList *fieldList,
2859 const TString *instanceName,
2860 const TSourceLoc &instanceLine,
2861 TIntermTyped *arrayIndex,
2862 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002863{
Olli Etuaho856c4972016-08-08 11:38:39 +03002864 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002865
Olli Etuaho77ba4082016-12-16 12:01:18 +00002866 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03002867
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002868 if (typeQualifier.qualifier != EvqUniform)
2869 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002870 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform",
2871 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002872 }
2873
Martin Radev70866b82016-07-22 15:27:42 +03002874 if (typeQualifier.invariant)
2875 {
2876 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
2877 }
2878
Olli Etuaho43364892017-02-13 16:00:12 +00002879 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
2880
2881 // TODO(oetuaho): Remove this and support binding for blocks.
2882 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03002883
Andrei Volykhina5527072017-03-22 16:46:30 +03002884 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
2885
Jamie Madill099c0f32013-06-20 11:55:52 -04002886 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03002887 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002888
Jamie Madill099c0f32013-06-20 11:55:52 -04002889 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2890 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002891 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002892 }
2893
Jamie Madill1566ef72013-06-20 11:55:54 -04002894 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2895 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002896 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002897 }
2898
Olli Etuaho856c4972016-08-08 11:38:39 +03002899 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002900
Martin Radev2cc85b32016-08-05 16:22:53 +03002901 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
2902
Arun Patole7e7e68d2015-05-22 12:02:25 +05302903 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2904 if (!symbolTable.declare(blockNameSymbol))
2905 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002906 error(nameLine, "redefinition of an interface block name", blockName.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002907 }
2908
Jamie Madill98493dd2013-07-08 14:39:03 -04002909 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302910 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2911 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002912 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302913 TType *fieldType = field->type();
2914 if (IsSampler(fieldType->getBasicType()))
2915 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002916 error(field->line(),
2917 "unsupported type - sampler types are not allowed in interface blocks",
2918 fieldType->getBasicString());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002919 }
2920
Martin Radev2cc85b32016-08-05 16:22:53 +03002921 if (IsImage(fieldType->getBasicType()))
2922 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002923 error(field->line(),
2924 "unsupported type - image types are not allowed in interface blocks",
2925 fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03002926 }
2927
Jamie Madill98493dd2013-07-08 14:39:03 -04002928 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002929 switch (qualifier)
2930 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002931 case EvqGlobal:
2932 case EvqUniform:
2933 break;
2934 default:
2935 error(field->line(), "invalid qualifier on interface block member",
2936 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04002937 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002938 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002939
Martin Radev70866b82016-07-22 15:27:42 +03002940 if (fieldType->isInvariant())
2941 {
2942 error(field->line(), "invalid qualifier on interface block member", "invariant");
2943 }
2944
Jamie Madilla5efff92013-06-06 11:56:47 -04002945 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002946 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03002947 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
Jamie Madill099c0f32013-06-20 11:55:52 -04002948
Jamie Madill98493dd2013-07-08 14:39:03 -04002949 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002950 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002951 error(field->line(), "invalid layout qualifier: cannot be used here",
2952 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04002953 }
2954
Jamie Madill98493dd2013-07-08 14:39:03 -04002955 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002956 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002957 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002958 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002959 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002960 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002961 warning(field->line(),
2962 "extraneous layout qualifier: only has an effect on matrix types",
2963 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04002964 }
2965
Jamie Madill98493dd2013-07-08 14:39:03 -04002966 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002967 }
2968
Jamie Madill98493dd2013-07-08 14:39:03 -04002969 // add array index
Olli Etuaho856c4972016-08-08 11:38:39 +03002970 unsigned int arraySize = 0;
2971 if (arrayIndex != nullptr)
Jamie Madill98493dd2013-07-08 14:39:03 -04002972 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002973 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04002974 }
2975
Jamie Madillb98c3a82015-07-23 14:26:04 -04002976 TInterfaceBlock *interfaceBlock =
2977 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2978 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
2979 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002980
2981 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04002982 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002983
Jamie Madill98493dd2013-07-08 14:39:03 -04002984 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002985 {
2986 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002987 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2988 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002989 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302990 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002991
2992 // set parent pointer of the field variable
2993 fieldType->setInterfaceBlock(interfaceBlock);
2994
Arun Patole7e7e68d2015-05-22 12:02:25 +05302995 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002996 fieldVariable->setQualifier(typeQualifier.qualifier);
2997
Arun Patole7e7e68d2015-05-22 12:02:25 +05302998 if (!symbolTable.declare(fieldVariable))
2999 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003000 error(field->line(), "redefinition of an interface block member name",
3001 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003002 }
3003 }
3004 }
3005 else
3006 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003007 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003008
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003009 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05303010 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003011 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04003012
Arun Patole7e7e68d2015-05-22 12:02:25 +05303013 if (!symbolTable.declare(instanceTypeDef))
3014 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003015 error(instanceLine, "redefinition of an interface block instance name",
3016 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003017 }
3018
Jamie Madillb98c3a82015-07-23 14:26:04 -04003019 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003020 symbolName = instanceTypeDef->getName();
3021 }
3022
Olli Etuaho13389b62016-10-16 11:48:18 +01003023 TIntermSymbol *blockSymbol =
3024 intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line);
3025 TIntermDeclaration *declaration = new TIntermDeclaration();
3026 declaration->appendDeclarator(blockSymbol);
3027 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003028
3029 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003030 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003031}
3032
Olli Etuaho383b7912016-08-05 11:22:59 +03003033void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003034{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003035 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003036
3037 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003038 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303039 if (mStructNestingLevel > 1)
3040 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003041 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003042 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003043}
3044
3045void TParseContext::exitStructDeclaration()
3046{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003047 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003048}
3049
Olli Etuaho8a176262016-08-16 14:23:01 +03003050void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003051{
Jamie Madillacb4b812016-11-07 13:50:29 -05003052 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303053 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003054 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003055 }
3056
Arun Patole7e7e68d2015-05-22 12:02:25 +05303057 if (field.type()->getBasicType() != EbtStruct)
3058 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003059 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003060 }
3061
3062 // We're already inside a structure definition at this point, so add
3063 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303064 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3065 {
Jamie Madill41a49272014-03-18 16:10:13 -04003066 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003067 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
3068 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003069 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003070 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003071 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003072 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003073}
3074
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003075//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003076// Parse an array index expression
3077//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003078TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3079 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303080 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003081{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003082 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3083 {
3084 if (baseExpression->getAsSymbolNode())
3085 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303086 error(location, " left of '[' is not of type array, matrix, or vector ",
3087 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003088 }
3089 else
3090 {
3091 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3092 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003093
3094 TConstantUnion *unionArray = new TConstantUnion[1];
3095 unionArray->setFConst(0.0f);
3096 return intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst),
3097 location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003098 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003099
Jamie Madill21c1e452014-12-29 11:33:41 -05003100 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3101
Olli Etuaho36b05142015-11-12 13:10:42 +02003102 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3103 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3104 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3105 // index is a constant expression.
3106 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3107 {
3108 if (baseExpression->isInterfaceBlock())
3109 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003110 error(location,
3111 "array indexes for interface blocks arrays must be constant integral expressions",
3112 "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003113 }
3114 else if (baseExpression->getQualifier() == EvqFragmentOut)
3115 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003116 error(location,
3117 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003118 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003119 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3120 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003121 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003122 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003123 }
3124
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003125 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003126 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003127 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3128 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3129 // constant fold expressions that are not constant expressions). The most compatible way to
3130 // handle this case is to report a warning instead of an error and force the index to be in
3131 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003132 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003133 int index = indexConstantUnion->getIConst(0);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003134
3135 int safeIndex = -1;
3136
3137 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04003138 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003139 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003140 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003141 if (mShaderSpec == SH_WEBGL2_SPEC)
3142 {
3143 // Error has been already generated if index is not const.
3144 if (indexExpression->getQualifier() == EvqConst)
3145 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003146 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003147 }
3148 safeIndex = 0;
3149 }
3150 else if (!isExtensionEnabled("GL_EXT_draw_buffers"))
3151 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003152 outOfRangeError(outOfRangeIndexIsError, location,
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003153 "array index for gl_FragData must be zero when "
Olli Etuaho4de340a2016-12-16 09:32:03 +00003154 "GL_EXT_draw_buffers is disabled",
3155 "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003156 safeIndex = 0;
3157 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03003158 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003159 // Only do generic out-of-range check if similar error hasn't already been reported.
3160 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003161 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003162 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3163 baseExpression->getArraySize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003164 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003165 }
3166 }
3167 else if (baseExpression->isMatrix())
3168 {
3169 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03003170 baseExpression->getType().getCols(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003171 "matrix field selection out of range");
Jamie Madill7164cf42013-07-08 13:30:59 -04003172 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003173 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04003174 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003175 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3176 baseExpression->getType().getNominalSize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003177 "vector field selection out of range");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003178 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003179
3180 ASSERT(safeIndex >= 0);
3181 // Data of constant unions can't be changed, because it may be shared with other
3182 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
3183 // sanitized object.
3184 if (safeIndex != index)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003185 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003186 TConstantUnion *safeConstantUnion = new TConstantUnion();
3187 safeConstantUnion->setIConst(safeIndex);
3188 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003189 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003190
3191 return intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003192 mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003193 }
Jamie Madill7164cf42013-07-08 13:30:59 -04003194 else
3195 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003196 return intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003197 mDiagnostics);
Jamie Madill7164cf42013-07-08 13:30:59 -04003198 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003199}
3200
Olli Etuaho90892fb2016-07-14 14:44:51 +03003201int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
3202 const TSourceLoc &location,
3203 int index,
3204 int arraySize,
Olli Etuaho4de340a2016-12-16 09:32:03 +00003205 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003206{
3207 if (index >= arraySize || index < 0)
3208 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003209 std::stringstream reasonStream;
3210 reasonStream << reason << " '" << index << "'";
3211 std::string token = reasonStream.str();
3212 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuaho90892fb2016-07-14 14:44:51 +03003213 if (index < 0)
3214 {
3215 return 0;
3216 }
3217 else
3218 {
3219 return arraySize - 1;
3220 }
3221 }
3222 return index;
3223}
3224
Jamie Madillb98c3a82015-07-23 14:26:04 -04003225TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3226 const TSourceLoc &dotLocation,
3227 const TString &fieldString,
3228 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003229{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003230 if (baseExpression->isArray())
3231 {
3232 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003233 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003234 }
3235
3236 if (baseExpression->isVector())
3237 {
3238 TVectorFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003239 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields,
3240 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003241 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003242 fields.num = 1;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003243 fields.offsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003244 }
3245
Olli Etuahob6fa0432016-09-28 16:28:05 +01003246 return TIntermediate::AddSwizzle(baseExpression, fields, dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003247 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003248 else if (baseExpression->getBasicType() == EbtStruct)
3249 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303250 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003251 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003252 {
3253 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003254 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003255 }
3256 else
3257 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003258 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003259 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003260 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003261 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003262 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003263 {
3264 fieldFound = true;
3265 break;
3266 }
3267 }
3268 if (fieldFound)
3269 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003270 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3271 index->setLine(fieldLocation);
3272 return intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003273 dotLocation, mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003274 }
3275 else
3276 {
3277 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003278 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003279 }
3280 }
3281 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003282 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003283 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303284 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003285 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003286 {
3287 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003288 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003289 }
3290 else
3291 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003292 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003293 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003294 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003295 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003296 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003297 {
3298 fieldFound = true;
3299 break;
3300 }
3301 }
3302 if (fieldFound)
3303 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003304 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3305 index->setLine(fieldLocation);
3306 return intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003307 dotLocation, mDiagnostics);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003308 }
3309 else
3310 {
3311 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003312 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003313 }
3314 }
3315 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003316 else
3317 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003318 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003319 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03003320 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303321 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003322 }
3323 else
3324 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303325 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003326 " field selection requires structure, vector, or interface block on left hand "
3327 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303328 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003329 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003330 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003331 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003332}
3333
Jamie Madillb98c3a82015-07-23 14:26:04 -04003334TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3335 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003336{
Martin Radev802abe02016-08-04 17:48:32 +03003337 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003338
3339 if (qualifierType == "shared")
3340 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003341 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003342 {
3343 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
3344 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003345 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003346 }
3347 else if (qualifierType == "packed")
3348 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003349 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003350 {
3351 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
3352 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003353 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003354 }
3355 else if (qualifierType == "std140")
3356 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003357 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003358 }
3359 else if (qualifierType == "row_major")
3360 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003361 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003362 }
3363 else if (qualifierType == "column_major")
3364 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003365 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003366 }
3367 else if (qualifierType == "location")
3368 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003369 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
3370 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003371 }
Andrei Volykhina5527072017-03-22 16:46:30 +03003372 else if (qualifierType == "yuv" && isExtensionEnabled("GL_EXT_YUV_target") &&
3373 mShaderType == GL_FRAGMENT_SHADER)
3374 {
3375 qualifier.yuv = true;
3376 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003377 else if (qualifierType == "rgba32f")
3378 {
3379 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3380 qualifier.imageInternalFormat = EiifRGBA32F;
3381 }
3382 else if (qualifierType == "rgba16f")
3383 {
3384 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3385 qualifier.imageInternalFormat = EiifRGBA16F;
3386 }
3387 else if (qualifierType == "r32f")
3388 {
3389 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3390 qualifier.imageInternalFormat = EiifR32F;
3391 }
3392 else if (qualifierType == "rgba8")
3393 {
3394 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3395 qualifier.imageInternalFormat = EiifRGBA8;
3396 }
3397 else if (qualifierType == "rgba8_snorm")
3398 {
3399 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3400 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
3401 }
3402 else if (qualifierType == "rgba32i")
3403 {
3404 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3405 qualifier.imageInternalFormat = EiifRGBA32I;
3406 }
3407 else if (qualifierType == "rgba16i")
3408 {
3409 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3410 qualifier.imageInternalFormat = EiifRGBA16I;
3411 }
3412 else if (qualifierType == "rgba8i")
3413 {
3414 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3415 qualifier.imageInternalFormat = EiifRGBA8I;
3416 }
3417 else if (qualifierType == "r32i")
3418 {
3419 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3420 qualifier.imageInternalFormat = EiifR32I;
3421 }
3422 else if (qualifierType == "rgba32ui")
3423 {
3424 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3425 qualifier.imageInternalFormat = EiifRGBA32UI;
3426 }
3427 else if (qualifierType == "rgba16ui")
3428 {
3429 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3430 qualifier.imageInternalFormat = EiifRGBA16UI;
3431 }
3432 else if (qualifierType == "rgba8ui")
3433 {
3434 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3435 qualifier.imageInternalFormat = EiifRGBA8UI;
3436 }
3437 else if (qualifierType == "r32ui")
3438 {
3439 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3440 qualifier.imageInternalFormat = EiifR32UI;
3441 }
3442
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003443 else
3444 {
3445 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003446 }
3447
Jamie Madilla5efff92013-06-06 11:56:47 -04003448 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003449}
3450
Martin Radev802abe02016-08-04 17:48:32 +03003451void TParseContext::parseLocalSize(const TString &qualifierType,
3452 const TSourceLoc &qualifierTypeLine,
3453 int intValue,
3454 const TSourceLoc &intValueLine,
3455 const std::string &intValueString,
3456 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03003457 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03003458{
Olli Etuaho856c4972016-08-08 11:38:39 +03003459 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03003460 if (intValue < 1)
3461 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003462 std::stringstream reasonStream;
3463 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
3464 std::string reason = reasonStream.str();
3465 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003466 }
3467 (*localSize)[index] = intValue;
3468}
3469
Olli Etuaho09b04a22016-12-15 13:30:26 +00003470void TParseContext::parseNumViews(int intValue,
3471 const TSourceLoc &intValueLine,
3472 const std::string &intValueString,
3473 int *numViews)
3474{
3475 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3476 // specification.
3477 if (intValue < 1)
3478 {
3479 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
3480 }
3481 *numViews = intValue;
3482}
3483
Jamie Madillb98c3a82015-07-23 14:26:04 -04003484TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3485 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003486 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303487 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003488{
Martin Radev802abe02016-08-04 17:48:32 +03003489 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003490
Martin Radev802abe02016-08-04 17:48:32 +03003491 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003492
Martin Radev802abe02016-08-04 17:48:32 +03003493 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003494 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003495 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003496 if (intValue < 0)
3497 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003498 error(intValueLine, "out of range: location must be non-negative",
3499 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003500 }
3501 else
3502 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003503 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03003504 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003505 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003506 }
Olli Etuaho43364892017-02-13 16:00:12 +00003507 else if (qualifierType == "binding")
3508 {
3509 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3510 if (intValue < 0)
3511 {
3512 error(intValueLine, "out of range: binding must be non-negative",
3513 intValueString.c_str());
3514 }
3515 else
3516 {
3517 qualifier.binding = intValue;
3518 }
3519 }
Martin Radev802abe02016-08-04 17:48:32 +03003520 else if (qualifierType == "local_size_x")
3521 {
3522 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
3523 &qualifier.localSize);
3524 }
3525 else if (qualifierType == "local_size_y")
3526 {
3527 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
3528 &qualifier.localSize);
3529 }
3530 else if (qualifierType == "local_size_z")
3531 {
3532 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
3533 &qualifier.localSize);
3534 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00003535 else if (qualifierType == "num_views" && mMultiviewAvailable &&
3536 (isExtensionEnabled("GL_OVR_multiview") || isExtensionEnabled("GL_OVR_multiview2")) &&
3537 mShaderType == GL_VERTEX_SHADER)
3538 {
3539 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
3540 }
Martin Radev802abe02016-08-04 17:48:32 +03003541 else
3542 {
3543 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003544 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003545
Jamie Madilla5efff92013-06-06 11:56:47 -04003546 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003547}
3548
Olli Etuaho613b9592016-09-05 12:05:53 +03003549TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
3550{
3551 return new TTypeQualifierBuilder(
3552 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
3553 mShaderVersion);
3554}
3555
Jamie Madillb98c3a82015-07-23 14:26:04 -04003556TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03003557 TLayoutQualifier rightQualifier,
3558 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003559{
Martin Radevc28888b2016-07-22 15:27:42 +03003560 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003561 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003562}
3563
Olli Etuaho4de340a2016-12-16 09:32:03 +00003564TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
3565 const TFieldList *newlyAddedFields,
3566 const TSourceLoc &location)
3567{
3568 for (TField *field : *newlyAddedFields)
3569 {
3570 for (TField *oldField : *processedFields)
3571 {
3572 if (oldField->name() == field->name())
3573 {
3574 error(location, "duplicate field name in structure", field->name().c_str());
3575 }
3576 }
3577 processedFields->push_back(field);
3578 }
3579 return processedFields;
3580}
3581
Martin Radev70866b82016-07-22 15:27:42 +03003582TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
3583 const TTypeQualifierBuilder &typeQualifierBuilder,
3584 TPublicType *typeSpecifier,
3585 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003586{
Olli Etuaho77ba4082016-12-16 12:01:18 +00003587 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003588
Martin Radev70866b82016-07-22 15:27:42 +03003589 typeSpecifier->qualifier = typeQualifier.qualifier;
3590 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03003591 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03003592 typeSpecifier->invariant = typeQualifier.invariant;
3593 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05303594 {
Martin Radev70866b82016-07-22 15:27:42 +03003595 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003596 }
Martin Radev70866b82016-07-22 15:27:42 +03003597 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003598}
3599
Jamie Madillb98c3a82015-07-23 14:26:04 -04003600TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3601 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003602{
Martin Radev4a9cd802016-09-01 16:51:51 +03003603 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
3604 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03003605
Martin Radev4a9cd802016-09-01 16:51:51 +03003606 checkIsNonVoid(typeSpecifier.getLine(), (*fieldList)[0]->name(), typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003607
Martin Radev4a9cd802016-09-01 16:51:51 +03003608 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003609
Arun Patole7e7e68d2015-05-22 12:02:25 +05303610 for (unsigned int i = 0; i < fieldList->size(); ++i)
3611 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003612 //
3613 // Careful not to replace already known aspects of type, like array-ness
3614 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303615 TType *type = (*fieldList)[i]->type();
Martin Radev4a9cd802016-09-01 16:51:51 +03003616 type->setBasicType(typeSpecifier.getBasicType());
3617 type->setPrimarySize(typeSpecifier.getPrimarySize());
3618 type->setSecondarySize(typeSpecifier.getSecondarySize());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003619 type->setPrecision(typeSpecifier.precision);
3620 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003621 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
Martin Radev2cc85b32016-08-05 16:22:53 +03003622 type->setMemoryQualifier(typeSpecifier.memoryQualifier);
Martin Radev70866b82016-07-22 15:27:42 +03003623 type->setInvariant(typeSpecifier.invariant);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003624
3625 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303626 if (type->isArray())
3627 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003628 checkIsValidTypeForArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003629 }
3630 if (typeSpecifier.array)
Olli Etuaho856c4972016-08-08 11:38:39 +03003631 type->setArraySize(static_cast<unsigned int>(typeSpecifier.arraySize));
Martin Radev4a9cd802016-09-01 16:51:51 +03003632 if (typeSpecifier.getUserDef())
Arun Patole7e7e68d2015-05-22 12:02:25 +05303633 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003634 type->setStruct(typeSpecifier.getUserDef()->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003635 }
3636
Martin Radev4a9cd802016-09-01 16:51:51 +03003637 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*fieldList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003638 }
3639
Jamie Madill98493dd2013-07-08 14:39:03 -04003640 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003641}
3642
Martin Radev4a9cd802016-09-01 16:51:51 +03003643TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
3644 const TSourceLoc &nameLine,
3645 const TString *structName,
3646 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003647{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303648 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003649 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003650
Jamie Madill9b820842015-02-12 10:40:10 -05003651 // Store a bool in the struct if we're at global scope, to allow us to
3652 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05003653 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003654
Jamie Madill98493dd2013-07-08 14:39:03 -04003655 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003656 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003657 checkIsNotReserved(nameLine, *structName);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303658 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
3659 if (!symbolTable.declare(userTypeDef))
3660 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003661 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003662 }
3663 }
3664
3665 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04003666 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003667 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003668 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04003669 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003670 switch (qualifier)
3671 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003672 case EvqGlobal:
3673 case EvqTemporary:
3674 break;
3675 default:
3676 error(field.line(), "invalid qualifier on struct member",
3677 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003678 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003679 }
Martin Radev70866b82016-07-22 15:27:42 +03003680 if (field.type()->isInvariant())
3681 {
3682 error(field.line(), "invalid qualifier on struct member", "invariant");
3683 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003684 if (IsImage(field.type()->getBasicType()))
3685 {
3686 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
3687 }
3688
Olli Etuaho43364892017-02-13 16:00:12 +00003689 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
3690
3691 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03003692
3693 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003694 }
3695
Martin Radev4a9cd802016-09-01 16:51:51 +03003696 TTypeSpecifierNonArray typeSpecifierNonArray;
3697 typeSpecifierNonArray.initialize(EbtStruct, structLine);
3698 typeSpecifierNonArray.userDef = structureType;
3699 typeSpecifierNonArray.isStructSpecifier = true;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003700 exitStructDeclaration();
3701
Martin Radev4a9cd802016-09-01 16:51:51 +03003702 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003703}
3704
Jamie Madillb98c3a82015-07-23 14:26:04 -04003705TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003706 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003707 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02003708{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003709 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003710 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02003711 init->isVector())
3712 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003713 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
3714 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003715 return nullptr;
3716 }
3717
Olli Etuahoac5274d2015-02-20 10:19:08 +02003718 if (statementList)
3719 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00003720 if (!ValidateSwitchStatementList(switchType, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02003721 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02003722 return nullptr;
3723 }
3724 }
3725
Olli Etuahoa3a36662015-02-17 13:46:51 +02003726 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3727 if (node == nullptr)
3728 {
3729 error(loc, "erroneous switch statement", "switch");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003730 return nullptr;
3731 }
3732 return node;
3733}
3734
3735TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3736{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003737 if (mSwitchNestingLevel == 0)
3738 {
3739 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003740 return nullptr;
3741 }
3742 if (condition == nullptr)
3743 {
3744 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003745 return nullptr;
3746 }
3747 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04003748 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02003749 {
3750 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003751 }
3752 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003753 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
3754 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
3755 // fold in case labels.
3756 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02003757 {
3758 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003759 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003760 TIntermCase *node = intermediate.addCase(condition, loc);
3761 if (node == nullptr)
3762 {
3763 error(loc, "erroneous case statement", "case");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003764 return nullptr;
3765 }
3766 return node;
3767}
3768
3769TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3770{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003771 if (mSwitchNestingLevel == 0)
3772 {
3773 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003774 return nullptr;
3775 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003776 TIntermCase *node = intermediate.addCase(nullptr, loc);
3777 if (node == nullptr)
3778 {
3779 error(loc, "erroneous default statement", "default");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003780 return nullptr;
3781 }
3782 return node;
3783}
3784
Jamie Madillb98c3a82015-07-23 14:26:04 -04003785TIntermTyped *TParseContext::createUnaryMath(TOperator op,
3786 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003787 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003788{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003789 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003790
3791 switch (op)
3792 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003793 case EOpLogicalNot:
3794 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
3795 child->isVector())
3796 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003797 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003798 return nullptr;
3799 }
3800 break;
3801 case EOpBitwiseNot:
3802 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3803 child->isMatrix() || child->isArray())
3804 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003805 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003806 return nullptr;
3807 }
3808 break;
3809 case EOpPostIncrement:
3810 case EOpPreIncrement:
3811 case EOpPostDecrement:
3812 case EOpPreDecrement:
3813 case EOpNegative:
3814 case EOpPositive:
3815 if (child->getBasicType() == EbtStruct || child->getBasicType() == EbtBool ||
Martin Radev2cc85b32016-08-05 16:22:53 +03003816 child->isArray() || IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04003817 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003818 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003819 return nullptr;
3820 }
3821 // Operators for built-ins are already type checked against their prototype.
3822 default:
3823 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003824 }
3825
Olli Etuahof119a262016-08-19 15:54:22 +03003826 TIntermUnary *node = new TIntermUnary(op, child);
3827 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03003828
Olli Etuaho77ba4082016-12-16 12:01:18 +00003829 TIntermTyped *foldedNode = node->fold(mDiagnostics);
Olli Etuahof119a262016-08-19 15:54:22 +03003830 if (foldedNode)
3831 return foldedNode;
3832
3833 return node;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003834}
3835
Olli Etuaho09b22472015-02-11 11:47:26 +02003836TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3837{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003838 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003839 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003840 {
Olli Etuaho09b22472015-02-11 11:47:26 +02003841 return child;
3842 }
3843 return node;
3844}
3845
Jamie Madillb98c3a82015-07-23 14:26:04 -04003846TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
3847 TIntermTyped *child,
3848 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003849{
Olli Etuaho856c4972016-08-08 11:38:39 +03003850 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02003851 return addUnaryMath(op, child, loc);
3852}
3853
Jamie Madillb98c3a82015-07-23 14:26:04 -04003854bool TParseContext::binaryOpCommonCheck(TOperator op,
3855 TIntermTyped *left,
3856 TIntermTyped *right,
3857 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003858{
Olli Etuaho244be012016-08-18 15:26:02 +03003859 if (left->getType().getStruct() || right->getType().getStruct())
3860 {
3861 switch (op)
3862 {
3863 case EOpIndexDirectStruct:
3864 ASSERT(left->getType().getStruct());
3865 break;
3866 case EOpEqual:
3867 case EOpNotEqual:
3868 case EOpAssign:
3869 case EOpInitialize:
3870 if (left->getType() != right->getType())
3871 {
3872 return false;
3873 }
3874 break;
3875 default:
3876 error(loc, "Invalid operation for structs", GetOperatorString(op));
3877 return false;
3878 }
3879 }
3880
Olli Etuahod6b14282015-03-17 14:31:35 +02003881 if (left->isArray() || right->isArray())
3882 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003883 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003884 {
3885 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3886 return false;
3887 }
3888
3889 if (left->isArray() != right->isArray())
3890 {
3891 error(loc, "array / non-array mismatch", GetOperatorString(op));
3892 return false;
3893 }
3894
3895 switch (op)
3896 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003897 case EOpEqual:
3898 case EOpNotEqual:
3899 case EOpAssign:
3900 case EOpInitialize:
3901 break;
3902 default:
3903 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3904 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02003905 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003906 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003907 if (left->getArraySize() != right->getArraySize())
3908 {
3909 error(loc, "array size mismatch", GetOperatorString(op));
3910 return false;
3911 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003912 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003913
3914 // Check ops which require integer / ivec parameters
3915 bool isBitShift = false;
3916 switch (op)
3917 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003918 case EOpBitShiftLeft:
3919 case EOpBitShiftRight:
3920 case EOpBitShiftLeftAssign:
3921 case EOpBitShiftRightAssign:
3922 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3923 // check that the basic type is an integer type.
3924 isBitShift = true;
3925 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3926 {
3927 return false;
3928 }
3929 break;
3930 case EOpBitwiseAnd:
3931 case EOpBitwiseXor:
3932 case EOpBitwiseOr:
3933 case EOpBitwiseAndAssign:
3934 case EOpBitwiseXorAssign:
3935 case EOpBitwiseOrAssign:
3936 // It is enough to check the type of only one operand, since later it
3937 // is checked that the operand types match.
3938 if (!IsInteger(left->getBasicType()))
3939 {
3940 return false;
3941 }
3942 break;
3943 default:
3944 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003945 }
3946
3947 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3948 // So the basic type should usually match.
3949 if (!isBitShift && left->getBasicType() != right->getBasicType())
3950 {
3951 return false;
3952 }
3953
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003954 // Check that:
3955 // 1. Type sizes match exactly on ops that require that.
3956 // 2. Restrictions for structs that contain arrays or samplers are respected.
3957 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003958 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003959 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003960 case EOpAssign:
3961 case EOpInitialize:
3962 case EOpEqual:
3963 case EOpNotEqual:
3964 // ESSL 1.00 sections 5.7, 5.8, 5.9
3965 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
3966 {
3967 error(loc, "undefined operation for structs containing arrays",
3968 GetOperatorString(op));
3969 return false;
3970 }
3971 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3972 // we interpret the spec so that this extends to structs containing samplers,
3973 // similarly to ESSL 1.00 spec.
3974 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
3975 left->getType().isStructureContainingSamplers())
3976 {
3977 error(loc, "undefined operation for structs containing samplers",
3978 GetOperatorString(op));
3979 return false;
3980 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003981
3982 if ((op == EOpAssign || op == EOpInitialize) &&
3983 left->getType().isStructureContainingImages())
3984 {
3985 error(loc, "undefined operation for structs containing images",
3986 GetOperatorString(op));
3987 return false;
3988 }
Olli Etuahoe1805592017-01-02 16:41:20 +00003989 if ((left->getNominalSize() != right->getNominalSize()) ||
3990 (left->getSecondarySize() != right->getSecondarySize()))
3991 {
3992 error(loc, "dimension mismatch", GetOperatorString(op));
3993 return false;
3994 }
3995 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003996 case EOpLessThan:
3997 case EOpGreaterThan:
3998 case EOpLessThanEqual:
3999 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00004000 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04004001 {
Olli Etuahoe1805592017-01-02 16:41:20 +00004002 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004003 return false;
4004 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03004005 break;
4006 case EOpAdd:
4007 case EOpSub:
4008 case EOpDiv:
4009 case EOpIMod:
4010 case EOpBitShiftLeft:
4011 case EOpBitShiftRight:
4012 case EOpBitwiseAnd:
4013 case EOpBitwiseXor:
4014 case EOpBitwiseOr:
4015 case EOpAddAssign:
4016 case EOpSubAssign:
4017 case EOpDivAssign:
4018 case EOpIModAssign:
4019 case EOpBitShiftLeftAssign:
4020 case EOpBitShiftRightAssign:
4021 case EOpBitwiseAndAssign:
4022 case EOpBitwiseXorAssign:
4023 case EOpBitwiseOrAssign:
4024 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
4025 {
4026 return false;
4027 }
4028
4029 // Are the sizes compatible?
4030 if (left->getNominalSize() != right->getNominalSize() ||
4031 left->getSecondarySize() != right->getSecondarySize())
4032 {
4033 // If the nominal sizes of operands do not match:
4034 // One of them must be a scalar.
4035 if (!left->isScalar() && !right->isScalar())
4036 return false;
4037
4038 // In the case of compound assignment other than multiply-assign,
4039 // the right side needs to be a scalar. Otherwise a vector/matrix
4040 // would be assigned to a scalar. A scalar can't be shifted by a
4041 // vector either.
4042 if (!right->isScalar() &&
4043 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
4044 return false;
4045 }
4046 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004047 default:
4048 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004049 }
4050
Olli Etuahod6b14282015-03-17 14:31:35 +02004051 return true;
4052}
4053
Olli Etuaho1dded802016-08-18 18:13:13 +03004054bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
4055 const TType &left,
4056 const TType &right)
4057{
4058 switch (op)
4059 {
4060 case EOpMul:
4061 case EOpMulAssign:
4062 return left.getNominalSize() == right.getNominalSize() &&
4063 left.getSecondarySize() == right.getSecondarySize();
4064 case EOpVectorTimesScalar:
4065 return true;
4066 case EOpVectorTimesScalarAssign:
4067 ASSERT(!left.isMatrix() && !right.isMatrix());
4068 return left.isVector() && !right.isVector();
4069 case EOpVectorTimesMatrix:
4070 return left.getNominalSize() == right.getRows();
4071 case EOpVectorTimesMatrixAssign:
4072 ASSERT(!left.isMatrix() && right.isMatrix());
4073 return left.isVector() && left.getNominalSize() == right.getRows() &&
4074 left.getNominalSize() == right.getCols();
4075 case EOpMatrixTimesVector:
4076 return left.getCols() == right.getNominalSize();
4077 case EOpMatrixTimesScalar:
4078 return true;
4079 case EOpMatrixTimesScalarAssign:
4080 ASSERT(left.isMatrix() && !right.isMatrix());
4081 return !right.isVector();
4082 case EOpMatrixTimesMatrix:
4083 return left.getCols() == right.getRows();
4084 case EOpMatrixTimesMatrixAssign:
4085 ASSERT(left.isMatrix() && right.isMatrix());
4086 // We need to check two things:
4087 // 1. The matrix multiplication step is valid.
4088 // 2. The result will have the same number of columns as the lvalue.
4089 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
4090
4091 default:
4092 UNREACHABLE();
4093 return false;
4094 }
4095}
4096
Jamie Madillb98c3a82015-07-23 14:26:04 -04004097TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
4098 TIntermTyped *left,
4099 TIntermTyped *right,
4100 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02004101{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004102 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02004103 return nullptr;
4104
Olli Etuahofc1806e2015-03-17 13:03:11 +02004105 switch (op)
4106 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004107 case EOpEqual:
4108 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04004109 case EOpLessThan:
4110 case EOpGreaterThan:
4111 case EOpLessThanEqual:
4112 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04004113 break;
4114 case EOpLogicalOr:
4115 case EOpLogicalXor:
4116 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03004117 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4118 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00004119 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04004120 {
4121 return nullptr;
4122 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00004123 // Basic types matching should have been already checked.
4124 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004125 break;
4126 case EOpAdd:
4127 case EOpSub:
4128 case EOpDiv:
4129 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03004130 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4131 !right->getType().getStruct());
4132 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04004133 {
4134 return nullptr;
4135 }
4136 break;
4137 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03004138 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4139 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004140 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03004141 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04004142 {
4143 return nullptr;
4144 }
4145 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004146 default:
4147 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02004148 }
4149
Olli Etuaho1dded802016-08-18 18:13:13 +03004150 if (op == EOpMul)
4151 {
4152 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
4153 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4154 {
4155 return nullptr;
4156 }
4157 }
4158
Olli Etuaho3fdec912016-08-18 15:08:06 +03004159 TIntermBinary *node = new TIntermBinary(op, left, right);
4160 node->setLine(loc);
4161
Olli Etuaho3fdec912016-08-18 15:08:06 +03004162 // See if we can fold constants.
Olli Etuaho77ba4082016-12-16 12:01:18 +00004163 TIntermTyped *foldedNode = node->fold(mDiagnostics);
Olli Etuaho3fdec912016-08-18 15:08:06 +03004164 if (foldedNode)
4165 return foldedNode;
4166
4167 return node;
Olli Etuahofc1806e2015-03-17 13:03:11 +02004168}
4169
Jamie Madillb98c3a82015-07-23 14:26:04 -04004170TIntermTyped *TParseContext::addBinaryMath(TOperator op,
4171 TIntermTyped *left,
4172 TIntermTyped *right,
4173 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004174{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004175 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004176 if (node == 0)
4177 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004178 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4179 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02004180 return left;
4181 }
4182 return node;
4183}
4184
Jamie Madillb98c3a82015-07-23 14:26:04 -04004185TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
4186 TIntermTyped *left,
4187 TIntermTyped *right,
4188 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004189{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004190 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004191 if (node == 0)
4192 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004193 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4194 right->getCompleteString());
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004195 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02004196 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004197 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
4198 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004199 }
4200 return node;
4201}
4202
Olli Etuaho13389b62016-10-16 11:48:18 +01004203TIntermBinary *TParseContext::createAssign(TOperator op,
4204 TIntermTyped *left,
4205 TIntermTyped *right,
4206 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004207{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004208 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02004209 {
Olli Etuaho1dded802016-08-18 18:13:13 +03004210 if (op == EOpMulAssign)
4211 {
4212 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
4213 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4214 {
4215 return nullptr;
4216 }
4217 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03004218 TIntermBinary *node = new TIntermBinary(op, left, right);
4219 node->setLine(loc);
4220
Olli Etuaho3fdec912016-08-18 15:08:06 +03004221 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02004222 }
4223 return nullptr;
4224}
4225
Jamie Madillb98c3a82015-07-23 14:26:04 -04004226TIntermTyped *TParseContext::addAssign(TOperator op,
4227 TIntermTyped *left,
4228 TIntermTyped *right,
4229 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004230{
4231 TIntermTyped *node = createAssign(op, left, right, loc);
4232 if (node == nullptr)
4233 {
4234 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02004235 return left;
4236 }
4237 return node;
4238}
4239
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004240TIntermTyped *TParseContext::addComma(TIntermTyped *left,
4241 TIntermTyped *right,
4242 const TSourceLoc &loc)
4243{
Corentin Wallez0d959252016-07-12 17:26:32 -04004244 // WebGL2 section 5.26, the following results in an error:
4245 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004246 if (mShaderSpec == SH_WEBGL2_SPEC &&
4247 (left->isArray() || left->getBasicType() == EbtVoid ||
4248 left->getType().isStructureContainingArrays() || right->isArray() ||
4249 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04004250 {
4251 error(loc,
4252 "sequence operator is not allowed for void, arrays, or structs containing arrays",
4253 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04004254 }
4255
Olli Etuaho4db7ded2016-10-13 12:23:11 +01004256 return TIntermediate::AddComma(left, right, loc, mShaderVersion);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004257}
4258
Olli Etuaho49300862015-02-20 14:54:49 +02004259TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
4260{
4261 switch (op)
4262 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004263 case EOpContinue:
4264 if (mLoopNestingLevel <= 0)
4265 {
4266 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004267 }
4268 break;
4269 case EOpBreak:
4270 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
4271 {
4272 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004273 }
4274 break;
4275 case EOpReturn:
4276 if (mCurrentFunctionType->getBasicType() != EbtVoid)
4277 {
4278 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004279 }
4280 break;
4281 default:
4282 // No checks for discard
4283 break;
Olli Etuaho49300862015-02-20 14:54:49 +02004284 }
4285 return intermediate.addBranch(op, loc);
4286}
4287
Jamie Madillb98c3a82015-07-23 14:26:04 -04004288TIntermBranch *TParseContext::addBranch(TOperator op,
4289 TIntermTyped *returnValue,
4290 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02004291{
4292 ASSERT(op == EOpReturn);
4293 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004294 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02004295 {
4296 error(loc, "void function cannot return a value", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02004297 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004298 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02004299 {
4300 error(loc, "function return is not matching type:", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02004301 }
4302 return intermediate.addBranch(op, returnValue, loc);
4303}
4304
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004305void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
4306{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004307 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01004308 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004309 TIntermNode *offset = nullptr;
4310 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuahoec9232b2017-03-27 17:01:37 +03004311 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
4312 name == "textureProjLodOffset" || name == "textureGradOffset" ||
4313 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004314 {
4315 offset = arguments->back();
4316 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03004317 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004318 {
4319 // A bias parameter might follow the offset parameter.
4320 ASSERT(arguments->size() >= 3);
4321 offset = (*arguments)[2];
4322 }
4323 if (offset != nullptr)
4324 {
4325 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
4326 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
4327 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004328 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03004329 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004330 }
4331 else
4332 {
4333 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
4334 size_t size = offsetConstantUnion->getType().getObjectSize();
4335 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
4336 for (size_t i = 0u; i < size; ++i)
4337 {
4338 int offsetValue = values[i].getIConst();
4339 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
4340 {
4341 std::stringstream tokenStream;
4342 tokenStream << offsetValue;
4343 std::string token = tokenStream.str();
4344 error(offset->getLine(), "Texture offset value out of valid range",
4345 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004346 }
4347 }
4348 }
4349 }
4350}
4351
Martin Radev2cc85b32016-08-05 16:22:53 +03004352// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
4353void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
4354{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004355 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03004356 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
4357
4358 if (name.compare(0, 5, "image") == 0)
4359 {
4360 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00004361 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03004362
Olli Etuaho485eefd2017-02-14 17:40:06 +00004363 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03004364
4365 if (name.compare(5, 5, "Store") == 0)
4366 {
4367 if (memoryQualifier.readonly)
4368 {
4369 error(imageNode->getLine(),
4370 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004371 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03004372 }
4373 }
4374 else if (name.compare(5, 4, "Load") == 0)
4375 {
4376 if (memoryQualifier.writeonly)
4377 {
4378 error(imageNode->getLine(),
4379 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004380 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03004381 }
4382 }
4383 }
4384}
4385
4386// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
4387void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
4388 const TFunction *functionDefinition,
4389 const TIntermAggregate *functionCall)
4390{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004391 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03004392
4393 const TIntermSequence &arguments = *functionCall->getSequence();
4394
4395 ASSERT(functionDefinition->getParamCount() == arguments.size());
4396
4397 for (size_t i = 0; i < arguments.size(); ++i)
4398 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00004399 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
4400 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03004401 const TType &functionParameterType = *functionDefinition->getParam(i).type;
4402 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
4403
4404 if (IsImage(functionArgumentType.getBasicType()))
4405 {
4406 const TMemoryQualifier &functionArgumentMemoryQualifier =
4407 functionArgumentType.getMemoryQualifier();
4408 const TMemoryQualifier &functionParameterMemoryQualifier =
4409 functionParameterType.getMemoryQualifier();
4410 if (functionArgumentMemoryQualifier.readonly &&
4411 !functionParameterMemoryQualifier.readonly)
4412 {
4413 error(functionCall->getLine(),
4414 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004415 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03004416 }
4417
4418 if (functionArgumentMemoryQualifier.writeonly &&
4419 !functionParameterMemoryQualifier.writeonly)
4420 {
4421 error(functionCall->getLine(),
4422 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004423 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03004424 }
Martin Radev049edfa2016-11-11 14:35:37 +02004425
4426 if (functionArgumentMemoryQualifier.coherent &&
4427 !functionParameterMemoryQualifier.coherent)
4428 {
4429 error(functionCall->getLine(),
4430 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004431 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02004432 }
4433
4434 if (functionArgumentMemoryQualifier.volatileQualifier &&
4435 !functionParameterMemoryQualifier.volatileQualifier)
4436 {
4437 error(functionCall->getLine(),
4438 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004439 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02004440 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004441 }
4442 }
4443}
4444
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004445TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004446{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004447 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00004448}
4449
4450TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004451 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00004452 TIntermNode *thisNode,
4453 const TSourceLoc &loc)
4454{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004455 if (thisNode != nullptr)
4456 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004457 return addMethod(fnCall, arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004458 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004459
4460 TOperator op = fnCall->getBuiltInOp();
4461 if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004462 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004463 return addConstructor(arguments, op, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004464 }
4465 else
4466 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004467 return addNonConstructorFunctionCall(fnCall, arguments, loc);
4468 }
4469}
4470
4471TIntermTyped *TParseContext::addMethod(TFunction *fnCall,
4472 TIntermSequence *arguments,
4473 TIntermNode *thisNode,
4474 const TSourceLoc &loc)
4475{
4476 TConstantUnion *unionArray = new TConstantUnion[1];
4477 int arraySize = 0;
4478 TIntermTyped *typedThis = thisNode->getAsTyped();
4479 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
4480 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
4481 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
4482 // So accessing fnCall->getName() below is safe.
4483 if (fnCall->getName() != "length")
4484 {
4485 error(loc, "invalid method", fnCall->getName().c_str());
4486 }
4487 else if (!arguments->empty())
4488 {
4489 error(loc, "method takes no parameters", "length");
4490 }
4491 else if (typedThis == nullptr || !typedThis->isArray())
4492 {
4493 error(loc, "length can only be called on arrays", "length");
4494 }
4495 else
4496 {
4497 arraySize = typedThis->getArraySize();
4498 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuaho72d10202017-01-19 15:58:30 +00004499 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004500 // This code path can be hit with expressions like these:
4501 // (a = b).length()
4502 // (func()).length()
4503 // (int[3](0, 1, 2)).length()
4504 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
4505 // expression.
4506 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
4507 // spec section 5.9 which allows "An array, vector or matrix expression with the
4508 // length method applied".
4509 error(loc, "length can only be called on array names, not on array expressions",
4510 "length");
Olli Etuaho72d10202017-01-19 15:58:30 +00004511 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004512 }
4513 unionArray->setIConst(arraySize);
4514 return intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
4515}
4516
4517TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunction *fnCall,
4518 TIntermSequence *arguments,
4519 const TSourceLoc &loc)
4520{
4521 // First find by unmangled name to check whether the function name has been
4522 // hidden by a variable name or struct typename.
4523 // If a function is found, check for one with a matching argument list.
4524 bool builtIn;
4525 const TSymbol *symbol = symbolTable.find(fnCall->getName(), mShaderVersion, &builtIn);
4526 if (symbol != nullptr && !symbol->isFunction())
4527 {
4528 error(loc, "function name expected", fnCall->getName().c_str());
4529 }
4530 else
4531 {
4532 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(fnCall->getName(), *arguments),
4533 mShaderVersion, &builtIn);
4534 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004535 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004536 error(loc, "no matching overloaded function found", fnCall->getName().c_str());
4537 }
4538 else
4539 {
4540 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004541 //
4542 // A declared function.
4543 //
Olli Etuaho383b7912016-08-05 11:22:59 +03004544 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004545 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004546 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004547 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004548 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004549 if (builtIn && op != EOpNull)
4550 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004551 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004552 if (fnCandidate->getParamCount() == 1)
4553 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004554 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004555 TIntermNode *unaryParamNode = arguments->front();
4556 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004557 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004558 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004559 }
4560 else
4561 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004562 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00004563 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004564 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004565
4566 // Some built-in functions have out parameters too.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004567 functionCallLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05304568
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004569 // See if we can constant fold a built-in. Note that this may be possible even
4570 // if it is not const-qualified.
Olli Etuahof119a262016-08-19 15:54:22 +03004571 TIntermTyped *foldedNode =
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004572 intermediate.foldAggregateBuiltIn(callNode, mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05304573 if (foldedNode)
4574 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004575 return foldedNode;
Arun Patole274f0702015-05-05 13:33:30 +05304576 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004577 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004578 }
4579 }
4580 else
4581 {
4582 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004583 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004584
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004585 // If builtIn == false, the function is user defined - could be an overloaded
4586 // built-in as well.
4587 // if builtIn == true, it's a builtIn function with no op associated with it.
4588 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004589 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004590 {
Olli Etuahofe486322017-03-21 09:30:54 +00004591 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004592 checkTextureOffsetConst(callNode);
4593 checkImageMemoryAccessForBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03004594 }
4595 else
4596 {
Olli Etuahofe486322017-03-21 09:30:54 +00004597 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004598 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004599 }
4600
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004601 functionCallLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004602
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004603 callNode->setLine(loc);
4604
4605 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004606 }
4607 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004608 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004609
4610 // Error message was already written. Put on a dummy node for error recovery.
4611 return TIntermTyped::CreateZero(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004612}
4613
Jamie Madillb98c3a82015-07-23 14:26:04 -04004614TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004615 TIntermTyped *trueExpression,
4616 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03004617 const TSourceLoc &loc)
4618{
Olli Etuaho856c4972016-08-08 11:38:39 +03004619 checkIsScalarBool(loc, cond);
Olli Etuaho52901742015-04-15 13:42:45 +03004620
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004621 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03004622 {
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004623 binaryOpError(loc, ":", trueExpression->getCompleteString(),
4624 falseExpression->getCompleteString());
4625 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03004626 }
Olli Etuahode318b22016-10-25 16:18:25 +01004627 if (IsOpaqueType(trueExpression->getBasicType()))
4628 {
4629 // ESSL 1.00 section 4.1.7
4630 // ESSL 3.00 section 4.1.7
4631 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
4632 // Note that structs containing opaque types don't need to be checked as structs are
4633 // forbidden below.
4634 error(loc, "ternary operator is not allowed for opaque types", ":");
4635 return falseExpression;
4636 }
4637
Olli Etuahoa2d53032015-04-15 14:14:44 +03004638 // ESSL1 sections 5.2 and 5.7:
4639 // ESSL3 section 5.7:
4640 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004641 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03004642 {
4643 error(loc, "ternary operator is not allowed for structures or arrays", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004644 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03004645 }
Corentin Wallez0d959252016-07-12 17:26:32 -04004646 // WebGL2 section 5.26, the following results in an error:
4647 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004648 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04004649 {
4650 error(loc, "ternary operator is not allowed for void", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004651 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04004652 }
4653
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004654 return TIntermediate::AddTernarySelection(cond, trueExpression, falseExpression, loc);
Olli Etuaho52901742015-04-15 13:42:45 +03004655}
Olli Etuaho49300862015-02-20 14:54:49 +02004656
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004657//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004658// Parse an array of strings using yyparse.
4659//
4660// Returns 0 for success.
4661//
Jamie Madillb98c3a82015-07-23 14:26:04 -04004662int PaParseStrings(size_t count,
4663 const char *const string[],
4664 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05304665 TParseContext *context)
4666{
Yunchao He4f285442017-04-21 12:15:49 +08004667 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004668 return 1;
4669
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004670 if (glslang_initialize(context))
4671 return 1;
4672
alokp@chromium.org408c45e2012-04-05 15:54:43 +00004673 int error = glslang_scan(count, string, length, context);
4674 if (!error)
4675 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004676
alokp@chromium.org73bc2982012-06-19 18:48:05 +00004677 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00004678
alokp@chromium.org6b495712012-06-29 00:06:58 +00004679 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004680}
Jamie Madill45bcc782016-11-07 13:58:48 -05004681
4682} // namespace sh