blob: ead601659561eae96a4d0f46e1c8218dc89de1fc [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),
99 mDeferredSingleDeclarationErrorCheck(false),
100 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;
Martin Radev802abe02016-08-04 17:48:32 +0300443 case EvqComputeIn:
444 message = "can't modify work group size variable";
445 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400446 default:
447 //
448 // Type that can't be written to?
449 //
450 if (node->getBasicType() == EbtVoid)
451 {
452 message = "can't modify void";
453 }
454 if (IsSampler(node->getBasicType()))
455 {
456 message = "can't modify a sampler";
457 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300458 if (IsImage(node->getBasicType()))
459 {
460 message = "can't modify an image";
461 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000462 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000463
Arun Patole7e7e68d2015-05-22 12:02:25 +0530464 if (message == 0 && binaryNode == 0 && symNode == 0)
465 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000466 error(line, "l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000467
Olli Etuaho8a176262016-08-16 14:23:01 +0300468 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000469 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000470
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000471 //
472 // Everything else is okay, no error.
473 //
474 if (message == 0)
Olli Etuaho8a176262016-08-16 14:23:01 +0300475 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000476
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000477 //
478 // If we get here, we have an error and a message.
479 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530480 if (symNode)
481 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000482 const char *symbol = symNode->getSymbol().c_str();
483 std::stringstream reasonStream;
484 reasonStream << "l-value required (" << message << " \"" << symbol << "\")";
485 std::string reason = reasonStream.str();
486 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000487 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530488 else
489 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000490 std::stringstream reasonStream;
491 reasonStream << "l-value required (" << message << ")";
492 std::string reason = reasonStream.str();
493 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000494 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000495
Olli Etuaho8a176262016-08-16 14:23:01 +0300496 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000497}
498
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000499// Both test, and if necessary spit out an error, to see if the node is really
500// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300501void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000502{
Olli Etuaho383b7912016-08-05 11:22:59 +0300503 if (node->getQualifier() != EvqConst)
504 {
505 error(node->getLine(), "constant expression required", "");
506 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000507}
508
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000509// Both test, and if necessary spit out an error, to see if the node is really
510// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300511void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000512{
Olli Etuaho383b7912016-08-05 11:22:59 +0300513 if (!node->isScalarInt())
514 {
515 error(node->getLine(), "integer expression required", token);
516 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000517}
518
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000519// Both test, and if necessary spit out an error, to see if we are currently
520// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800521bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000522{
Olli Etuaho856c4972016-08-08 11:38:39 +0300523 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300524 {
525 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800526 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300527 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800528 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000529}
530
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000531// For now, keep it simple: if it starts "gl_", it's reserved, independent
532// of scope. Except, if the symbol table is at the built-in push-level,
533// which is when we are parsing built-ins.
alokp@chromium.org613ef312010-07-21 18:54:22 +0000534// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a
535// webgl shader.
Olli Etuaho856c4972016-08-08 11:38:39 +0300536bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000537{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530538 static const char *reservedErrMsg = "reserved built-in name";
539 if (!symbolTable.atBuiltInLevel())
540 {
541 if (identifier.compare(0, 3, "gl_") == 0)
542 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000543 error(line, reservedErrMsg, "gl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300544 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000545 }
Jamie Madillacb4b812016-11-07 13:50:29 -0500546 if (sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530547 {
548 if (identifier.compare(0, 6, "webgl_") == 0)
549 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000550 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300551 return false;
alokp@chromium.org613ef312010-07-21 18:54:22 +0000552 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530553 if (identifier.compare(0, 7, "_webgl_") == 0)
554 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000555 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300556 return false;
alokp@chromium.org613ef312010-07-21 18:54:22 +0000557 }
558 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530559 if (identifier.find("__") != TString::npos)
560 {
561 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400562 "identifiers containing two consecutive underscores (__) are reserved as "
563 "possible future keywords",
Arun Patole7e7e68d2015-05-22 12:02:25 +0530564 identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300565 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000566 }
567 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000568
Olli Etuaho8a176262016-08-16 14:23:01 +0300569 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000570}
571
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000572// Make sure there is enough data provided to the constructor to build
573// something of the type of the constructor. Also returns the type of
574// the constructor.
Olli Etuaho856c4972016-08-08 11:38:39 +0300575bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800576 const TIntermSequence *arguments,
Olli Etuaho856c4972016-08-08 11:38:39 +0300577 TOperator op,
578 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000579{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000580 bool constructingMatrix = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400581 switch (op)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530582 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400583 case EOpConstructMat2:
584 case EOpConstructMat2x3:
585 case EOpConstructMat2x4:
586 case EOpConstructMat3x2:
587 case EOpConstructMat3:
588 case EOpConstructMat3x4:
589 case EOpConstructMat4x2:
590 case EOpConstructMat4x3:
591 case EOpConstructMat4:
592 constructingMatrix = true;
593 break;
594 default:
595 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000596 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000597
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000598 //
599 // Note: It's okay to have too many components available, but not okay to have unused
600 // arguments. 'full' will go to true when enough args have been seen. If we loop
601 // again, there is an extra argument, so 'overfull' will become true.
602 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000603
Jamie Madillb98c3a82015-07-23 14:26:04 -0400604 size_t size = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400605 bool full = false;
606 bool overFull = false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000607 bool matrixInMatrix = false;
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500608 bool arrayArg = false;
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800609 for (TIntermNode *arg : *arguments)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530610 {
Olli Etuaho72d10202017-01-19 15:58:30 +0000611 const TIntermTyped *argTyped = arg->getAsTyped();
612 size += argTyped->getType().getObjectSize();
Arun Patole7e7e68d2015-05-22 12:02:25 +0530613
Olli Etuaho72d10202017-01-19 15:58:30 +0000614 if (constructingMatrix && argTyped->getType().isMatrix())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000615 matrixInMatrix = true;
616 if (full)
617 overFull = true;
Olli Etuaho856c4972016-08-08 11:38:39 +0300618 if (op != EOpConstructStruct && !type.isArray() && size >= type.getObjectSize())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000619 full = true;
Olli Etuaho72d10202017-01-19 15:58:30 +0000620 if (argTyped->getType().isArray())
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000621 arrayArg = true;
622 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530623
Olli Etuaho856c4972016-08-08 11:38:39 +0300624 if (type.isArray())
Olli Etuaho376f1b52015-04-13 13:23:41 +0300625 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300626 // The size of an unsized constructor should already have been determined.
627 ASSERT(!type.isUnsizedArray());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800628 if (static_cast<size_t>(type.getArraySize()) != arguments->size())
Olli Etuaho376f1b52015-04-13 13:23:41 +0300629 {
630 error(line, "array constructor needs one argument per array element", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300631 return false;
Olli Etuaho376f1b52015-04-13 13:23:41 +0300632 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000633 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000634
Arun Patole7e7e68d2015-05-22 12:02:25 +0530635 if (arrayArg && op != EOpConstructStruct)
636 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000637 error(line, "constructing from a non-dereferenced array", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300638 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000639 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000640
Olli Etuaho856c4972016-08-08 11:38:39 +0300641 if (matrixInMatrix && !type.isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530642 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800643 if (arguments->size() != 1)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530644 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400645 error(line, "constructing matrix from matrix can only take one argument",
646 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300647 return false;
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000648 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000649 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000650
Arun Patole7e7e68d2015-05-22 12:02:25 +0530651 if (overFull)
652 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000653 error(line, "too many arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300654 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000655 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530656
Olli Etuaho856c4972016-08-08 11:38:39 +0300657 if (op == EOpConstructStruct && !type.isArray() &&
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800658 type.getStruct()->fields().size() != arguments->size())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530659 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400660 error(line,
661 "Number of constructor parameters does not match the number of structure fields",
662 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300663 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000664 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000665
Olli Etuaho856c4972016-08-08 11:38:39 +0300666 if (!type.isMatrix() || !matrixInMatrix)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530667 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300668 if ((op != EOpConstructStruct && size != 1 && size < type.getObjectSize()) ||
669 (op == EOpConstructStruct && size < type.getObjectSize()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530670 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000671 error(line, "not enough data provided for construction", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300672 return false;
daniel@transgaming.combef0b6d2010-04-29 03:32:39 +0000673 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000674 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000675
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800676 if (arguments->empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530677 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200678 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300679 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000680 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200681
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800682 for (TIntermNode *const &argNode : *arguments)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530683 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200684 TIntermTyped *argTyped = argNode->getAsTyped();
685 ASSERT(argTyped != nullptr);
686 if (op != EOpConstructStruct && IsSampler(argTyped->getBasicType()))
687 {
688 error(line, "cannot convert a sampler", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300689 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200690 }
Martin Radev2cc85b32016-08-05 16:22:53 +0300691 if (op != EOpConstructStruct && IsImage(argTyped->getBasicType()))
692 {
693 error(line, "cannot convert an image", "constructor");
694 return false;
695 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200696 if (argTyped->getBasicType() == EbtVoid)
697 {
698 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300699 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200700 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000701 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000702
Olli Etuaho856c4972016-08-08 11:38:39 +0300703 if (type.isArray())
704 {
705 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
706 // the array.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800707 for (TIntermNode *const &argNode : *arguments)
Olli Etuaho856c4972016-08-08 11:38:39 +0300708 {
709 const TType &argType = argNode->getAsTyped()->getType();
Jamie Madill34bf2d92017-02-06 13:40:59 -0500710 // It has already been checked that the argument is not an array, but we can arrive
711 // here due to prior error conditions.
712 if (argType.isArray())
713 {
714 return false;
715 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300716 if (!argType.sameElementType(type))
717 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000718 error(line, "Array constructor argument has an incorrect type", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300719 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300720 }
721 }
722 }
723 else if (op == EOpConstructStruct)
724 {
725 const TFieldList &fields = type.getStruct()->fields();
Olli Etuaho856c4972016-08-08 11:38:39 +0300726
727 for (size_t i = 0; i < fields.size(); i++)
728 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800729 if (i >= arguments->size() ||
730 (*arguments)[i]->getAsTyped()->getType() != *fields[i]->type())
Olli Etuaho856c4972016-08-08 11:38:39 +0300731 {
732 error(line, "Structure constructor arguments do not match structure fields",
Olli Etuaho4de340a2016-12-16 09:32:03 +0000733 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300734 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300735 }
736 }
737 }
738
Olli Etuaho8a176262016-08-16 14:23:01 +0300739 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000740}
741
Jamie Madillb98c3a82015-07-23 14:26:04 -0400742// This function checks to see if a void variable has been declared and raise an error message for
743// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000744//
745// returns true in case of an error
746//
Olli Etuaho856c4972016-08-08 11:38:39 +0300747bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400748 const TString &identifier,
749 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000750{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300751 if (type == EbtVoid)
752 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000753 error(line, "illegal use of type 'void'", identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300754 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300755 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000756
Olli Etuaho8a176262016-08-16 14:23:01 +0300757 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000758}
759
Jamie Madillb98c3a82015-07-23 14:26:04 -0400760// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300761// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300762void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000763{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530764 if (type->getBasicType() != EbtBool || type->isArray() || type->isMatrix() || type->isVector())
765 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000766 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530767 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000768}
769
Jamie Madillb98c3a82015-07-23 14:26:04 -0400770// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300771// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300772void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000773{
Martin Radev4a9cd802016-09-01 16:51:51 +0300774 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530775 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000776 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530777 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000778}
779
Olli Etuaho856c4972016-08-08 11:38:39 +0300780bool TParseContext::checkIsNotSampler(const TSourceLoc &line,
Martin Radev4a9cd802016-09-01 16:51:51 +0300781 const TTypeSpecifierNonArray &pType,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400782 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000783{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530784 if (pType.type == EbtStruct)
785 {
Martin Radev2cc85b32016-08-05 16:22:53 +0300786 if (ContainsSampler(*pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530787 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000788 std::stringstream reasonStream;
789 reasonStream << reason << " (structure contains a sampler)";
790 std::string reasonStr = reasonStream.str();
791 error(line, reasonStr.c_str(), getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300792 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000793 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530794
Olli Etuaho8a176262016-08-16 14:23:01 +0300795 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530796 }
797 else if (IsSampler(pType.type))
798 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000799 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300800 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000801 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000802
Olli Etuaho8a176262016-08-16 14:23:01 +0300803 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000804}
805
Martin Radev2cc85b32016-08-05 16:22:53 +0300806bool TParseContext::checkIsNotImage(const TSourceLoc &line,
807 const TTypeSpecifierNonArray &pType,
808 const char *reason)
809{
810 if (pType.type == EbtStruct)
811 {
812 if (ContainsImage(*pType.userDef))
813 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000814 std::stringstream reasonStream;
815 reasonStream << reason << " (structure contains an image)";
816 std::string reasonStr = reasonStream.str();
817 error(line, reasonStr.c_str(), getBasicString(pType.type));
Martin Radev2cc85b32016-08-05 16:22:53 +0300818
819 return false;
820 }
821
822 return true;
823 }
824 else if (IsImage(pType.type))
825 {
826 error(line, reason, getBasicString(pType.type));
827
828 return false;
829 }
830
831 return true;
832}
833
Olli Etuaho856c4972016-08-08 11:38:39 +0300834void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
835 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400836{
837 if (pType.layoutQualifier.location != -1)
838 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400839 error(line, "location must only be specified for a single input or output variable",
840 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400841 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400842}
843
Olli Etuaho856c4972016-08-08 11:38:39 +0300844void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
845 const TLayoutQualifier &layoutQualifier)
846{
847 if (layoutQualifier.location != -1)
848 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000849 const char *errorMsg = "invalid layout qualifier: only valid on program inputs and outputs";
850 if (mShaderVersion >= 310)
851 {
852 errorMsg =
853 "invalid layout qualifier: only valid on program inputs, outputs, and uniforms";
854 }
855 error(location, errorMsg, "location");
Olli Etuaho856c4972016-08-08 11:38:39 +0300856 }
857}
858
Martin Radev2cc85b32016-08-05 16:22:53 +0300859void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
860 TQualifier qualifier,
861 const TType &type)
862{
863 checkOutParameterIsNotSampler(line, qualifier, type);
864 checkOutParameterIsNotImage(line, qualifier, type);
865}
866
Olli Etuaho856c4972016-08-08 11:38:39 +0300867void TParseContext::checkOutParameterIsNotSampler(const TSourceLoc &line,
868 TQualifier qualifier,
869 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000870{
Martin Radev2cc85b32016-08-05 16:22:53 +0300871 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
872 if (IsSampler(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530873 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000874 error(line, "samplers cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000875 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000876}
877
Martin Radev2cc85b32016-08-05 16:22:53 +0300878void TParseContext::checkOutParameterIsNotImage(const TSourceLoc &line,
879 TQualifier qualifier,
880 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000881{
Martin Radev2cc85b32016-08-05 16:22:53 +0300882 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
883 if (IsImage(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530884 {
Martin Radev2cc85b32016-08-05 16:22:53 +0300885 error(line, "images cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000886 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000887}
888
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000889// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300890unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000891{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530892 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000893
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200894 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
895 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
896 // fold as array size.
897 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000898 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000899 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300900 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000901 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000902
Olli Etuaho856c4972016-08-08 11:38:39 +0300903 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400904
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000905 if (constant->getBasicType() == EbtUInt)
906 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300907 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000908 }
909 else
910 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300911 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000912
Olli Etuaho856c4972016-08-08 11:38:39 +0300913 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000914 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400915 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300916 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000917 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400918
Olli Etuaho856c4972016-08-08 11:38:39 +0300919 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400920 }
921
Olli Etuaho856c4972016-08-08 11:38:39 +0300922 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400923 {
924 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300925 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400926 }
927
928 // The size of arrays is restricted here to prevent issues further down the
929 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
930 // 4096 registers so this should be reasonable even for aggressively optimizable code.
931 const unsigned int sizeLimit = 65536;
932
Olli Etuaho856c4972016-08-08 11:38:39 +0300933 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -0400934 {
935 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300936 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000937 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300938
939 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000940}
941
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000942// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +0300943bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
944 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000945{
Olli Etuaho8a176262016-08-16 14:23:01 +0300946 if ((elementQualifier.qualifier == EvqAttribute) ||
947 (elementQualifier.qualifier == EvqVertexIn) ||
948 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300949 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400950 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300951 TType(elementQualifier).getQualifierString());
952 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000953 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000954
Olli Etuaho8a176262016-08-16 14:23:01 +0300955 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000956}
957
Olli Etuaho8a176262016-08-16 14:23:01 +0300958// See if this element type can be formed into an array.
959bool TParseContext::checkIsValidTypeForArray(const TSourceLoc &line, const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000960{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000961 //
962 // Can the type be an array?
963 //
Olli Etuaho8a176262016-08-16 14:23:01 +0300964 if (elementType.array)
Jamie Madill06145232015-05-13 13:10:01 -0400965 {
Olli Etuaho8a176262016-08-16 14:23:01 +0300966 error(line, "cannot declare arrays of arrays",
967 TType(elementType).getCompleteString().c_str());
968 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000969 }
Olli Etuahocc36b982015-07-10 14:14:18 +0300970 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
971 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
972 // 4.3.4).
Martin Radev4a9cd802016-09-01 16:51:51 +0300973 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Olli Etuaho8a176262016-08-16 14:23:01 +0300974 sh::IsVarying(elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +0300975 {
976 error(line, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300977 TType(elementType).getCompleteString().c_str());
978 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +0300979 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000980
Olli Etuaho8a176262016-08-16 14:23:01 +0300981 return true;
982}
983
984// Check if this qualified element type can be formed into an array.
985bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
986 const TPublicType &elementType)
987{
988 if (checkIsValidTypeForArray(indexLocation, elementType))
989 {
990 return checkIsValidQualifierForArray(indexLocation, elementType);
991 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000992 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000993}
994
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000995// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +0300996void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
997 const TString &identifier,
998 TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000999{
Olli Etuaho3739d232015-04-08 12:23:44 +03001000 ASSERT(type != nullptr);
1001 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001002 {
1003 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +03001004 type->qualifier = EvqTemporary;
1005
1006 // Generate informative error messages for ESSL1.
1007 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001008 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001009 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301010 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001011 "structures containing arrays may not be declared constant since they cannot be "
1012 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +05301013 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001014 }
1015 else
1016 {
1017 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
1018 }
Olli Etuaho383b7912016-08-05 11:22:59 +03001019 return;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001020 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001021 if (type->isUnsizedArray())
1022 {
1023 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
Olli Etuaho376f1b52015-04-13 13:23:41 +03001024 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001025}
1026
Olli Etuaho2935c582015-04-08 14:32:06 +03001027// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001028// and update the symbol table.
1029//
Olli Etuaho2935c582015-04-08 14:32:06 +03001030// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001031//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001032bool TParseContext::declareVariable(const TSourceLoc &line,
1033 const TString &identifier,
1034 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001035 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001036{
Olli Etuaho2935c582015-04-08 14:32:06 +03001037 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001038
Olli Etuaho43364892017-02-13 16:00:12 +00001039 checkBindingIsValid(line, type);
1040
Olli Etuaho856c4972016-08-08 11:38:39 +03001041 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001042
Olli Etuaho2935c582015-04-08 14:32:06 +03001043 // gl_LastFragData may be redeclared with a new precision qualifier
1044 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1045 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001046 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1047 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho856c4972016-08-08 11:38:39 +03001048 if (static_cast<int>(type.getArraySize()) == maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001049 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001050 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001051 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001052 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->getExtension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001053 }
1054 }
1055 else
1056 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001057 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1058 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001059 return false;
1060 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001061 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001062
Olli Etuaho8a176262016-08-16 14:23:01 +03001063 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001064 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001065
Olli Etuaho2935c582015-04-08 14:32:06 +03001066 (*variable) = new TVariable(&identifier, type);
1067 if (!symbolTable.declare(*variable))
1068 {
1069 error(line, "redefinition", identifier.c_str());
Jamie Madill1a4b1b32015-07-23 18:27:13 -04001070 *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +03001071 return false;
1072 }
1073
Olli Etuaho8a176262016-08-16 14:23:01 +03001074 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001075 return false;
1076
1077 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001078}
1079
Martin Radev70866b82016-07-22 15:27:42 +03001080void TParseContext::checkIsParameterQualifierValid(
1081 const TSourceLoc &line,
1082 const TTypeQualifierBuilder &typeQualifierBuilder,
1083 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301084{
Olli Etuaho77ba4082016-12-16 12:01:18 +00001085 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001086
1087 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301088 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001089 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1090 }
1091
1092 if (!IsImage(type->getBasicType()))
1093 {
Olli Etuaho43364892017-02-13 16:00:12 +00001094 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001095 }
1096 else
1097 {
1098 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001099 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001100
Martin Radev70866b82016-07-22 15:27:42 +03001101 type->setQualifier(typeQualifier.qualifier);
1102
1103 if (typeQualifier.precision != EbpUndefined)
1104 {
1105 type->setPrecision(typeQualifier.precision);
1106 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001107}
1108
Olli Etuaho856c4972016-08-08 11:38:39 +03001109bool TParseContext::checkCanUseExtension(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001110{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001111 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001112 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +05301113 if (iter == extBehavior.end())
1114 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001115 error(line, "extension is not supported", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001116 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001117 }
zmo@google.comf5450912011-09-09 01:37:19 +00001118 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +05301119 if (iter->second == EBhDisable || iter->second == EBhUndefined)
1120 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00001121 // TODO(oetuaho@nvidia.com): This is slightly hacky. Might be better if symbols could be
1122 // associated with more than one extension.
1123 if (extension == "GL_OVR_multiview")
1124 {
1125 return checkCanUseExtension(line, "GL_OVR_multiview2");
1126 }
Olli Etuaho4de340a2016-12-16 09:32:03 +00001127 error(line, "extension is disabled", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001128 return false;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001129 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301130 if (iter->second == EBhWarn)
1131 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001132 warning(line, "extension is being used", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001133 return true;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001134 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001135
Olli Etuaho8a176262016-08-16 14:23:01 +03001136 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001137}
1138
Martin Radevb8b01222016-11-20 23:25:53 +02001139void TParseContext::emptyDeclarationErrorCheck(const TPublicType &publicType,
1140 const TSourceLoc &location)
1141{
1142 if (publicType.isUnsizedArray())
1143 {
1144 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1145 // error. It is assumed that this applies to empty declarations as well.
1146 error(location, "empty array declaration needs to specify a size", "");
1147 }
1148 if (publicType.qualifier == EvqShared && !publicType.layoutQualifier.isEmpty())
1149 {
1150 error(location, "Shared memory declarations cannot have layout specified", "layout");
1151 }
1152}
1153
Jamie Madillb98c3a82015-07-23 14:26:04 -04001154// These checks are common for all declarations starting a declarator list, and declarators that
1155// follow an empty declaration.
Olli Etuaho383b7912016-08-05 11:22:59 +03001156void TParseContext::singleDeclarationErrorCheck(const TPublicType &publicType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001157 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001158{
Olli Etuahofa33d582015-04-09 14:33:12 +03001159 switch (publicType.qualifier)
1160 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001161 case EvqVaryingIn:
1162 case EvqVaryingOut:
1163 case EvqAttribute:
1164 case EvqVertexIn:
1165 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001166 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001167 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001168 {
1169 error(identifierLocation, "cannot be used with a structure",
1170 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001171 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001172 }
Olli Etuahofa33d582015-04-09 14:33:12 +03001173
Jamie Madillb98c3a82015-07-23 14:26:04 -04001174 default:
1175 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001176 }
1177
Jamie Madillb98c3a82015-07-23 14:26:04 -04001178 if (publicType.qualifier != EvqUniform &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001179 !checkIsNotSampler(identifierLocation, publicType.typeSpecifierNonArray,
1180 "samplers must be uniform"))
Olli Etuahofa33d582015-04-09 14:33:12 +03001181 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001182 return;
Olli Etuahofa33d582015-04-09 14:33:12 +03001183 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001184 if (publicType.qualifier != EvqUniform &&
1185 !checkIsNotImage(identifierLocation, publicType.typeSpecifierNonArray,
1186 "images must be uniform"))
1187 {
1188 return;
1189 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001190
Andrei Volykhina5527072017-03-22 16:46:30 +03001191 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1192 publicType.qualifier != EvqConst) &&
1193 publicType.getBasicType() == EbtYuvCscStandardEXT)
1194 {
1195 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1196 getQualifierString(publicType.qualifier));
1197 return;
1198 }
1199
Jamie Madilla5efff92013-06-06 11:56:47 -04001200 // check for layout qualifier issues
1201 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
1202
1203 if (layoutQualifier.matrixPacking != EmpUnspecified)
1204 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001205 error(identifierLocation, "layout qualifier only valid for interface blocks",
1206 getMatrixPackingString(layoutQualifier.matrixPacking));
Olli Etuaho383b7912016-08-05 11:22:59 +03001207 return;
Jamie Madilla5efff92013-06-06 11:56:47 -04001208 }
1209
1210 if (layoutQualifier.blockStorage != EbsUnspecified)
1211 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001212 error(identifierLocation, "layout qualifier only valid for interface blocks",
1213 getBlockStorageString(layoutQualifier.blockStorage));
Olli Etuaho383b7912016-08-05 11:22:59 +03001214 return;
Jamie Madilla5efff92013-06-06 11:56:47 -04001215 }
1216
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001217 bool canHaveLocation =
1218 publicType.qualifier == EvqVertexIn || publicType.qualifier == EvqFragmentOut;
1219
1220 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1221 {
1222 canHaveLocation = true;
1223
1224 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1225 // But invalid shaders may still reach here with an unsized array declaration.
1226 if (!publicType.isUnsizedArray())
1227 {
1228 TType type(publicType);
1229 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1230 publicType.layoutQualifier);
1231 }
1232 }
1233 if (!canHaveLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001234 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001235 checkLocationIsNotSpecified(identifierLocation, publicType.layoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04001236 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001237
Andrei Volykhina5527072017-03-22 16:46:30 +03001238 if (publicType.qualifier == EvqFragmentOut)
1239 {
1240 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1241 {
1242 error(identifierLocation, "invalid layout qualifier combination", "yuv");
1243 return;
1244 }
1245 }
1246 else
1247 {
1248 checkYuvIsNotSpecified(identifierLocation, layoutQualifier.yuv);
1249 }
1250
Martin Radev2cc85b32016-08-05 16:22:53 +03001251 if (IsImage(publicType.getBasicType()))
1252 {
1253
1254 switch (layoutQualifier.imageInternalFormat)
1255 {
1256 case EiifRGBA32F:
1257 case EiifRGBA16F:
1258 case EiifR32F:
1259 case EiifRGBA8:
1260 case EiifRGBA8_SNORM:
1261 if (!IsFloatImage(publicType.getBasicType()))
1262 {
1263 error(identifierLocation,
1264 "internal image format requires a floating image type",
1265 getBasicString(publicType.getBasicType()));
1266 return;
1267 }
1268 break;
1269 case EiifRGBA32I:
1270 case EiifRGBA16I:
1271 case EiifRGBA8I:
1272 case EiifR32I:
1273 if (!IsIntegerImage(publicType.getBasicType()))
1274 {
1275 error(identifierLocation,
1276 "internal image format requires an integer image type",
1277 getBasicString(publicType.getBasicType()));
1278 return;
1279 }
1280 break;
1281 case EiifRGBA32UI:
1282 case EiifRGBA16UI:
1283 case EiifRGBA8UI:
1284 case EiifR32UI:
1285 if (!IsUnsignedImage(publicType.getBasicType()))
1286 {
1287 error(identifierLocation,
1288 "internal image format requires an unsigned image type",
1289 getBasicString(publicType.getBasicType()));
1290 return;
1291 }
1292 break;
1293 case EiifUnspecified:
1294 error(identifierLocation, "layout qualifier", "No image internal format specified");
1295 return;
1296 default:
1297 error(identifierLocation, "layout qualifier", "unrecognized token");
1298 return;
1299 }
1300
1301 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1302 switch (layoutQualifier.imageInternalFormat)
1303 {
1304 case EiifR32F:
1305 case EiifR32I:
1306 case EiifR32UI:
1307 break;
1308 default:
1309 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1310 {
1311 error(identifierLocation, "layout qualifier",
1312 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1313 "image variables must be qualified readonly and/or writeonly");
1314 return;
1315 }
1316 break;
1317 }
1318 }
1319 else
1320 {
Olli Etuaho43364892017-02-13 16:00:12 +00001321 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Martin Radev2cc85b32016-08-05 16:22:53 +03001322
Olli Etuaho43364892017-02-13 16:00:12 +00001323 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1324 }
1325}
Martin Radev2cc85b32016-08-05 16:22:53 +03001326
Olli Etuaho43364892017-02-13 16:00:12 +00001327void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1328{
1329 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
1330 int arraySize = type.isArray() ? type.getArraySize() : 1;
1331 if (IsImage(type.getBasicType()))
1332 {
1333 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding, arraySize);
1334 }
1335 else if (IsSampler(type.getBasicType()))
1336 {
1337 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding, arraySize);
1338 }
1339 else
1340 {
1341 ASSERT(!IsOpaqueType(type.getBasicType()));
1342 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001343 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001344}
1345
Olli Etuaho856c4972016-08-08 11:38:39 +03001346void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1347 const TString &layoutQualifierName,
1348 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001349{
1350
1351 if (mShaderVersion < versionRequired)
1352 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001353 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001354 }
1355}
1356
Olli Etuaho856c4972016-08-08 11:38:39 +03001357bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1358 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001359{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001360 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001361 for (size_t i = 0u; i < localSize.size(); ++i)
1362 {
1363 if (localSize[i] != -1)
1364 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001365 error(location,
1366 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1367 "global layout declaration",
1368 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001369 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001370 }
1371 }
1372
Olli Etuaho8a176262016-08-16 14:23:01 +03001373 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001374}
1375
Olli Etuaho43364892017-02-13 16:00:12 +00001376void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001377 TLayoutImageInternalFormat internalFormat)
1378{
1379 if (internalFormat != EiifUnspecified)
1380 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001381 error(location, "invalid layout qualifier: only valid when used with images",
1382 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001383 }
Olli Etuaho43364892017-02-13 16:00:12 +00001384}
1385
1386void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1387{
1388 if (binding != -1)
1389 {
1390 error(location,
1391 "invalid layout qualifier: only valid when used with opaque types or blocks",
1392 "binding");
1393 }
1394}
1395
1396void TParseContext::checkImageBindingIsValid(const TSourceLoc &location, int binding, int arraySize)
1397{
1398 // Expects arraySize to be 1 when setting binding for only a single variable.
1399 if (binding >= 0 && binding + arraySize > mMaxImageUnits)
1400 {
1401 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1402 }
1403}
1404
1405void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1406 int binding,
1407 int arraySize)
1408{
1409 // Expects arraySize to be 1 when setting binding for only a single variable.
1410 if (binding >= 0 && binding + arraySize > mMaxCombinedTextureImageUnits)
1411 {
1412 error(location, "sampler binding greater than maximum texture units", "binding");
1413 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001414}
1415
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001416void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1417 int objectLocationCount,
1418 const TLayoutQualifier &layoutQualifier)
1419{
1420 int loc = layoutQualifier.location;
1421 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1422 {
1423 error(location, "Uniform location out of range", "location");
1424 }
1425}
1426
Andrei Volykhina5527072017-03-22 16:46:30 +03001427void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1428{
1429 if (yuv != false)
1430 {
1431 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1432 }
1433}
1434
Olli Etuaho383b7912016-08-05 11:22:59 +03001435void TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate,
Olli Etuaho856c4972016-08-08 11:38:39 +03001436 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001437{
1438 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1439 {
1440 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1441 if (qual == EvqOut || qual == EvqInOut)
1442 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001443 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
Olli Etuaho8a176262016-08-16 14:23:01 +03001444 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001445 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001446 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001447 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuahoec9232b2017-03-27 17:01:37 +03001448 fnCall->getFunctionSymbolInfo()->getName().c_str());
Olli Etuaho383b7912016-08-05 11:22:59 +03001449 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001450 }
1451 }
1452 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001453}
1454
Martin Radev70866b82016-07-22 15:27:42 +03001455void TParseContext::checkInvariantVariableQualifier(bool invariant,
1456 const TQualifier qualifier,
1457 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001458{
Martin Radev70866b82016-07-22 15:27:42 +03001459 if (!invariant)
1460 return;
1461
1462 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001463 {
Martin Radev70866b82016-07-22 15:27:42 +03001464 // input variables in the fragment shader can be also qualified as invariant
1465 if (!sh::CanBeInvariantESSL1(qualifier))
1466 {
1467 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1468 }
1469 }
1470 else
1471 {
1472 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1473 {
1474 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1475 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001476 }
1477}
1478
Arun Patole7e7e68d2015-05-22 12:02:25 +05301479bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001480{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001481 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001482 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1483 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001484}
1485
Arun Patole7e7e68d2015-05-22 12:02:25 +05301486bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001487{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001488 return ::IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001489}
1490
Jamie Madillb98c3a82015-07-23 14:26:04 -04001491void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1492 const char *extName,
1493 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001494{
1495 pp::SourceLocation srcLoc;
1496 srcLoc.file = loc.first_file;
1497 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001498 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001499}
1500
Jamie Madillb98c3a82015-07-23 14:26:04 -04001501void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1502 const char *name,
1503 const char *value,
1504 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001505{
1506 pp::SourceLocation srcLoc;
1507 srcLoc.file = loc.first_file;
1508 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001509 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001510}
1511
Martin Radev4c4c8e72016-08-04 12:25:34 +03001512sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001513{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001514 sh::WorkGroupSize result;
Martin Radev802abe02016-08-04 17:48:32 +03001515 for (size_t i = 0u; i < result.size(); ++i)
1516 {
1517 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1518 {
1519 result[i] = 1;
1520 }
1521 else
1522 {
1523 result[i] = mComputeShaderLocalSize[i];
1524 }
1525 }
1526 return result;
1527}
1528
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001529/////////////////////////////////////////////////////////////////////////////////
1530//
1531// Non-Errors.
1532//
1533/////////////////////////////////////////////////////////////////////////////////
1534
Jamie Madill5c097022014-08-20 16:38:32 -04001535const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1536 const TString *name,
1537 const TSymbol *symbol)
1538{
Yunchao Hed7297bf2017-04-19 15:27:10 +08001539 const TVariable *variable = nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001540
1541 if (!symbol)
1542 {
1543 error(location, "undeclared identifier", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001544 }
1545 else if (!symbol->isVariable())
1546 {
1547 error(location, "variable expected", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001548 }
1549 else
1550 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001551 variable = static_cast<const TVariable *>(symbol);
Jamie Madill5c097022014-08-20 16:38:32 -04001552
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001553 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
Olli Etuaho383b7912016-08-05 11:22:59 +03001554 !variable->getExtension().empty())
Jamie Madill5c097022014-08-20 16:38:32 -04001555 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001556 checkCanUseExtension(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001557 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001558
1559 // Reject shaders using both gl_FragData and gl_FragColor
1560 TQualifier qualifier = variable->getType().getQualifier();
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001561 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001562 {
1563 mUsesFragData = true;
1564 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001565 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001566 {
1567 mUsesFragColor = true;
1568 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001569 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1570 {
1571 mUsesSecondaryOutputs = true;
1572 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001573
1574 // This validation is not quite correct - it's only an error to write to
1575 // both FragData and FragColor. For simplicity, and because users shouldn't
1576 // be rewarded for reading from undefined varaibles, return an error
1577 // if they are both referenced, rather than assigned.
1578 if (mUsesFragData && mUsesFragColor)
1579 {
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001580 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1581 if (mUsesSecondaryOutputs)
1582 {
1583 errorMessage =
1584 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1585 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1586 }
1587 error(location, errorMessage, name->c_str());
Jamie Madill14e95b32015-05-07 10:10:41 -04001588 }
Martin Radevb0883602016-08-04 17:48:58 +03001589
1590 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1591 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1592 qualifier == EvqWorkGroupSize)
1593 {
1594 error(location,
1595 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1596 "gl_WorkGroupSize");
1597 }
Jamie Madill5c097022014-08-20 16:38:32 -04001598 }
1599
1600 if (!variable)
1601 {
1602 TType type(EbtFloat, EbpUndefined);
1603 TVariable *fakeVariable = new TVariable(name, type);
1604 symbolTable.declare(fakeVariable);
1605 variable = fakeVariable;
1606 }
1607
1608 return variable;
1609}
1610
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001611TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1612 const TString *name,
1613 const TSymbol *symbol)
1614{
1615 const TVariable *variable = getNamedVariable(location, name, symbol);
1616
Olli Etuaho09b04a22016-12-15 13:30:26 +00001617 if (variable->getType().getQualifier() == EvqViewIDOVR && IsWebGLBasedSpec(mShaderSpec) &&
1618 mShaderType == GL_FRAGMENT_SHADER && !isExtensionEnabled("GL_OVR_multiview2"))
1619 {
1620 // WEBGL_multiview spec
1621 error(location, "Need to enable OVR_multiview2 to use gl_ViewID_OVR in fragment shader",
1622 "gl_ViewID_OVR");
1623 }
1624
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001625 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001626 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001627 const TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001628 return intermediate.addConstantUnion(constArray, variable->getType(), location);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001629 }
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001630 else if (variable->getType().getQualifier() == EvqWorkGroupSize &&
1631 mComputeShaderLocalSizeDeclared)
1632 {
1633 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1634 // needs to be added to the AST as a constant and not as a symbol.
1635 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1636 TConstantUnion *constArray = new TConstantUnion[3];
1637 for (size_t i = 0; i < 3; ++i)
1638 {
1639 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1640 }
1641
1642 ASSERT(variable->getType().getBasicType() == EbtUInt);
1643 ASSERT(variable->getType().getObjectSize() == 3);
1644
1645 TType type(variable->getType());
1646 type.setQualifier(EvqConst);
1647 return intermediate.addConstantUnion(constArray, type, location);
1648 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001649 else
1650 {
1651 return intermediate.addSymbol(variable->getUniqueId(), variable->getName(),
1652 variable->getType(), location);
1653 }
1654}
1655
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001656//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001657// Initializers show up in several places in the grammar. Have one set of
1658// code to handle them here.
1659//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001660// Returns true on error, false if no error
1661//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001662bool TParseContext::executeInitializer(const TSourceLoc &line,
1663 const TString &identifier,
1664 const TPublicType &pType,
1665 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001666 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001667{
Olli Etuaho13389b62016-10-16 11:48:18 +01001668 ASSERT(initNode != nullptr);
1669 ASSERT(*initNode == nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001670 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001671
Olli Etuaho2935c582015-04-08 14:32:06 +03001672 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001673 if (type.isUnsizedArray())
1674 {
Olli Etuaho02bd82c2016-11-03 10:29:43 +00001675 // We have not checked yet whether the initializer actually is an array or not.
1676 if (initializer->isArray())
1677 {
1678 type.setArraySize(initializer->getArraySize());
1679 }
1680 else
1681 {
1682 // Having a non-array initializer for an unsized array will result in an error later,
1683 // so we don't generate an error message here.
1684 type.setArraySize(1u);
1685 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001686 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001687 if (!declareVariable(line, identifier, type, &variable))
1688 {
1689 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001690 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001691
Olli Etuahob0c645e2015-05-12 14:25:36 +03001692 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001693 if (symbolTable.atGlobalLevel() &&
1694 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001695 {
1696 // Error message does not completely match behavior with ESSL 1.00, but
1697 // we want to steer developers towards only using constant expressions.
1698 error(line, "global variable initializers must be constant expressions", "=");
1699 return true;
1700 }
1701 if (globalInitWarning)
1702 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001703 warning(
1704 line,
1705 "global variable initializers should be constant expressions "
1706 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1707 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001708 }
1709
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001710 //
1711 // identifier must be of type constant, a global, or a temporary
1712 //
1713 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301714 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1715 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001716 error(line, " cannot initialize this type of qualifier ",
1717 variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001718 return true;
1719 }
1720 //
1721 // test for and propagate constant
1722 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001723
Arun Patole7e7e68d2015-05-22 12:02:25 +05301724 if (qualifier == EvqConst)
1725 {
1726 if (qualifier != initializer->getType().getQualifier())
1727 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001728 std::stringstream reasonStream;
1729 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1730 << "'";
1731 std::string reason = reasonStream.str();
1732 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001733 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001734 return true;
1735 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301736 if (type != initializer->getType())
1737 {
1738 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001739 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001740 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001741 return true;
1742 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001743
1744 // Save the constant folded value to the variable if possible. For example array
1745 // initializers are not folded, since that way copying the array literal to multiple places
1746 // in the shader is avoided.
1747 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1748 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301749 if (initializer->getAsConstantUnion())
1750 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001751 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho13389b62016-10-16 11:48:18 +01001752 *initNode = nullptr;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001753 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301754 }
1755 else if (initializer->getAsSymbolNode())
1756 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001757 const TSymbol *symbol =
1758 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1759 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001760
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001761 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001762 if (constArray)
1763 {
1764 variable->shareConstPointer(constArray);
Olli Etuaho13389b62016-10-16 11:48:18 +01001765 *initNode = nullptr;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001766 return false;
1767 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001768 }
1769 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001770
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001771 TIntermSymbol *intermSymbol = intermediate.addSymbol(
1772 variable->getUniqueId(), variable->getName(), variable->getType(), line);
Olli Etuaho13389b62016-10-16 11:48:18 +01001773 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1774 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001775 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001776 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1777 return true;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001778 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001779
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001780 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001781}
1782
Olli Etuaho0e3aee32016-10-27 12:56:38 +01001783void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
1784{
1785 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
1786 typeSpecifier->getBasicType());
1787
1788 if (mShaderVersion < 300 && typeSpecifier->array)
1789 {
1790 error(typeSpecifier->getLine(), "not supported", "first-class array");
1791 typeSpecifier->clearArrayness();
1792 }
1793}
1794
Martin Radev70866b82016-07-22 15:27:42 +03001795TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301796 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001797{
Olli Etuaho77ba4082016-12-16 12:01:18 +00001798 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001799
Martin Radev70866b82016-07-22 15:27:42 +03001800 TPublicType returnType = typeSpecifier;
1801 returnType.qualifier = typeQualifier.qualifier;
1802 returnType.invariant = typeQualifier.invariant;
1803 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03001804 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03001805 returnType.precision = typeSpecifier.precision;
1806
1807 if (typeQualifier.precision != EbpUndefined)
1808 {
1809 returnType.precision = typeQualifier.precision;
1810 }
1811
Martin Radev4a9cd802016-09-01 16:51:51 +03001812 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
1813 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03001814
Martin Radev4a9cd802016-09-01 16:51:51 +03001815 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
1816 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03001817
Martin Radev4a9cd802016-09-01 16:51:51 +03001818 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03001819
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001820 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001821 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001822 if (typeSpecifier.array)
1823 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001824 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001825 returnType.clearArrayness();
1826 }
1827
Martin Radev70866b82016-07-22 15:27:42 +03001828 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001829 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001830 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001831 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001832 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001833 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001834
Martin Radev70866b82016-07-22 15:27:42 +03001835 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001836 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001837 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001838 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001839 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001840 }
1841 }
1842 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001843 {
Martin Radev70866b82016-07-22 15:27:42 +03001844 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03001845 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001846 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03001847 }
Martin Radev70866b82016-07-22 15:27:42 +03001848 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
1849 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001850 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001851 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
1852 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001853 }
Martin Radev70866b82016-07-22 15:27:42 +03001854 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03001855 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001856 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03001857 "in");
Martin Radev802abe02016-08-04 17:48:32 +03001858 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001859 }
1860
1861 return returnType;
1862}
1863
Olli Etuaho856c4972016-08-08 11:38:39 +03001864void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
1865 const TPublicType &type,
1866 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03001867{
1868 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03001869 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03001870 {
1871 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001872 }
1873
1874 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
1875 switch (qualifier)
1876 {
1877 case EvqVertexIn:
1878 // ESSL 3.00 section 4.3.4
1879 if (type.array)
1880 {
1881 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001882 }
1883 // Vertex inputs with a struct type are disallowed in singleDeclarationErrorCheck
1884 return;
1885 case EvqFragmentOut:
1886 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03001887 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03001888 {
1889 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001890 }
1891 // Fragment outputs with a struct type are disallowed in singleDeclarationErrorCheck
1892 return;
1893 default:
1894 break;
1895 }
1896
1897 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
1898 // restrictions.
1899 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03001900 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
1901 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03001902 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
1903 {
1904 error(qualifierLocation, "must use 'flat' interpolation here",
1905 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001906 }
1907
Martin Radev4a9cd802016-09-01 16:51:51 +03001908 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03001909 {
1910 // ESSL 3.00 sections 4.3.4 and 4.3.6.
1911 // These restrictions are only implied by the ESSL 3.00 spec, but
1912 // the ESSL 3.10 spec lists these restrictions explicitly.
1913 if (type.array)
1914 {
1915 error(qualifierLocation, "cannot be an array of structures",
1916 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001917 }
1918 if (type.isStructureContainingArrays())
1919 {
1920 error(qualifierLocation, "cannot be a structure containing an array",
1921 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001922 }
1923 if (type.isStructureContainingType(EbtStruct))
1924 {
1925 error(qualifierLocation, "cannot be a structure containing a structure",
1926 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001927 }
1928 if (type.isStructureContainingType(EbtBool))
1929 {
1930 error(qualifierLocation, "cannot be a structure containing a bool",
1931 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001932 }
1933 }
1934}
1935
Martin Radev2cc85b32016-08-05 16:22:53 +03001936void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
1937{
1938 if (qualifier.getType() == QtStorage)
1939 {
1940 const TStorageQualifierWrapper &storageQualifier =
1941 static_cast<const TStorageQualifierWrapper &>(qualifier);
1942 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
1943 !symbolTable.atGlobalLevel())
1944 {
1945 error(storageQualifier.getLine(),
1946 "Local variables can only use the const storage qualifier.",
1947 storageQualifier.getQualifierString().c_str());
1948 }
1949 }
1950}
1951
Olli Etuaho43364892017-02-13 16:00:12 +00001952void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03001953 const TSourceLoc &location)
1954{
1955 if (memoryQualifier.readonly)
1956 {
1957 error(location, "Only allowed with images.", "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03001958 }
1959 if (memoryQualifier.writeonly)
1960 {
1961 error(location, "Only allowed with images.", "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03001962 }
Martin Radev049edfa2016-11-11 14:35:37 +02001963 if (memoryQualifier.coherent)
1964 {
1965 error(location, "Only allowed with images.", "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02001966 }
1967 if (memoryQualifier.restrictQualifier)
1968 {
1969 error(location, "Only allowed with images.", "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02001970 }
1971 if (memoryQualifier.volatileQualifier)
1972 {
1973 error(location, "Only allowed with images.", "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02001974 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001975}
1976
Olli Etuaho13389b62016-10-16 11:48:18 +01001977TIntermDeclaration *TParseContext::parseSingleDeclaration(
1978 TPublicType &publicType,
1979 const TSourceLoc &identifierOrTypeLocation,
1980 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001981{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001982 TType type(publicType);
1983 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
1984 mDirectiveHandler.pragma().stdgl.invariantAll)
1985 {
1986 TQualifier qualifier = type.getQualifier();
1987
1988 // The directive handler has already taken care of rejecting invalid uses of this pragma
1989 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
1990 // affected variable declarations:
1991 //
1992 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
1993 // elsewhere, in TranslatorGLSL.)
1994 //
1995 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
1996 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
1997 // the way this is currently implemented we have to enable this compiler option before
1998 // parsing the shader and determining the shading language version it uses. If this were
1999 // implemented as a post-pass, the workaround could be more targeted.
2000 //
2001 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2002 // the specification, but there are desktop OpenGL drivers that expect that this is the
2003 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2004 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2005 {
2006 type.setInvariant(true);
2007 }
2008 }
2009
2010 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, type, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002011
Olli Etuahobab4c082015-04-24 16:38:49 +03002012 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03002013
Olli Etuahobab4c082015-04-24 16:38:49 +03002014 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
2015
Olli Etuaho13389b62016-10-16 11:48:18 +01002016 TIntermDeclaration *declaration = new TIntermDeclaration();
2017 declaration->setLine(identifierOrTypeLocation);
2018
Olli Etuahobab4c082015-04-24 16:38:49 +03002019 if (emptyDeclaration)
2020 {
Martin Radevb8b01222016-11-20 23:25:53 +02002021 emptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Olli Etuahobab4c082015-04-24 16:38:49 +03002022 }
2023 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002024 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002025 singleDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002026
Olli Etuaho856c4972016-08-08 11:38:39 +03002027 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002028
Olli Etuaho2935c582015-04-08 14:32:06 +03002029 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002030 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002031
2032 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01002033 {
Jamie Madill60ed9812013-06-06 11:56:46 -04002034 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01002035 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002036 }
2037
Olli Etuaho13389b62016-10-16 11:48:18 +01002038 // We append the symbol even if the declaration is empty, mainly because of struct declarations
2039 // that may just declare a type.
2040 declaration->appendDeclarator(symbol);
2041
2042 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002043}
2044
Olli Etuaho13389b62016-10-16 11:48:18 +01002045TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
2046 const TSourceLoc &identifierLocation,
2047 const TString &identifier,
2048 const TSourceLoc &indexLocation,
2049 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04002050{
Olli Etuahofa33d582015-04-09 14:33:12 +03002051 mDeferredSingleDeclarationErrorCheck = false;
2052
Olli Etuaho383b7912016-08-05 11:22:59 +03002053 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002054
Olli Etuaho856c4972016-08-08 11:38:39 +03002055 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002056
Olli Etuaho8a176262016-08-16 14:23:01 +03002057 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002058
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002059 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002060
Olli Etuaho856c4972016-08-08 11:38:39 +03002061 unsigned int size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002062 // Make the type an array even if size check failed.
2063 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2064 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04002065
Olli Etuaho2935c582015-04-08 14:32:06 +03002066 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002067 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002068
Olli Etuaho13389b62016-10-16 11:48:18 +01002069 TIntermDeclaration *declaration = new TIntermDeclaration();
2070 declaration->setLine(identifierLocation);
2071
Olli Etuahoe7847b02015-03-16 11:56:12 +02002072 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002073 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01002074 {
Jamie Madill60ed9812013-06-06 11:56:46 -04002075 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01002076 declaration->appendDeclarator(symbol);
2077 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002078
Olli Etuaho13389b62016-10-16 11:48:18 +01002079 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002080}
2081
Olli Etuaho13389b62016-10-16 11:48:18 +01002082TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2083 const TSourceLoc &identifierLocation,
2084 const TString &identifier,
2085 const TSourceLoc &initLocation,
2086 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002087{
Olli Etuahofa33d582015-04-09 14:33:12 +03002088 mDeferredSingleDeclarationErrorCheck = false;
2089
Olli Etuaho383b7912016-08-05 11:22:59 +03002090 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002091
Olli Etuaho13389b62016-10-16 11:48:18 +01002092 TIntermDeclaration *declaration = new TIntermDeclaration();
2093 declaration->setLine(identifierLocation);
2094
2095 TIntermBinary *initNode = nullptr;
2096 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002097 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002098 if (initNode)
2099 {
2100 declaration->appendDeclarator(initNode);
2101 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002102 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002103 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002104}
2105
Olli Etuaho13389b62016-10-16 11:48:18 +01002106TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Jamie Madillb98c3a82015-07-23 14:26:04 -04002107 TPublicType &publicType,
2108 const TSourceLoc &identifierLocation,
2109 const TString &identifier,
2110 const TSourceLoc &indexLocation,
2111 TIntermTyped *indexExpression,
2112 const TSourceLoc &initLocation,
2113 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002114{
2115 mDeferredSingleDeclarationErrorCheck = false;
2116
Olli Etuaho383b7912016-08-05 11:22:59 +03002117 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002118
Olli Etuaho8a176262016-08-16 14:23:01 +03002119 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002120
2121 TPublicType arrayType(publicType);
2122
Olli Etuaho856c4972016-08-08 11:38:39 +03002123 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002124 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2125 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002126 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002127 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002128 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002129 }
2130 // Make the type an array even if size check failed.
2131 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2132 arrayType.setArraySize(size);
2133
Olli Etuaho13389b62016-10-16 11:48:18 +01002134 TIntermDeclaration *declaration = new TIntermDeclaration();
2135 declaration->setLine(identifierLocation);
2136
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002137 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002138 TIntermBinary *initNode = nullptr;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002139 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
2140 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002141 if (initNode)
2142 {
2143 declaration->appendDeclarator(initNode);
2144 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002145 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002146
2147 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002148}
2149
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002150TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002151 const TTypeQualifierBuilder &typeQualifierBuilder,
2152 const TSourceLoc &identifierLoc,
2153 const TString *identifier,
2154 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002155{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002156 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002157
Martin Radev70866b82016-07-22 15:27:42 +03002158 if (!typeQualifier.invariant)
2159 {
2160 error(identifierLoc, "Expected invariant", identifier->c_str());
2161 return nullptr;
2162 }
2163 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2164 {
2165 return nullptr;
2166 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002167 if (!symbol)
2168 {
2169 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002170 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002171 }
Martin Radev70866b82016-07-22 15:27:42 +03002172 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002173 {
Martin Radev70866b82016-07-22 15:27:42 +03002174 error(identifierLoc, "invariant declaration specifies qualifier",
2175 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002176 }
Martin Radev70866b82016-07-22 15:27:42 +03002177 if (typeQualifier.precision != EbpUndefined)
2178 {
2179 error(identifierLoc, "invariant declaration specifies precision",
2180 getPrecisionString(typeQualifier.precision));
2181 }
2182 if (!typeQualifier.layoutQualifier.isEmpty())
2183 {
2184 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2185 }
2186
2187 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
2188 ASSERT(variable);
2189 const TType &type = variable->getType();
2190
2191 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2192 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002193 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002194
2195 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2196
2197 TIntermSymbol *intermSymbol =
2198 intermediate.addSymbol(variable->getUniqueId(), *identifier, type, identifierLoc);
2199
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002200 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002201}
2202
Olli Etuaho13389b62016-10-16 11:48:18 +01002203void TParseContext::parseDeclarator(TPublicType &publicType,
2204 const TSourceLoc &identifierLocation,
2205 const TString &identifier,
2206 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002207{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002208 // If the declaration starting this declarator list was empty (example: int,), some checks were
2209 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002210 if (mDeferredSingleDeclarationErrorCheck)
2211 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002212 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002213 mDeferredSingleDeclarationErrorCheck = false;
2214 }
2215
Olli Etuaho856c4972016-08-08 11:38:39 +03002216 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002217
Olli Etuaho856c4972016-08-08 11:38:39 +03002218 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002219
Olli Etuaho2935c582015-04-08 14:32:06 +03002220 TVariable *variable = nullptr;
Olli Etuaho43364892017-02-13 16:00:12 +00002221 TType type(publicType);
2222 declareVariable(identifierLocation, identifier, type, &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002223
Olli Etuaho43364892017-02-13 16:00:12 +00002224 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, type, identifierLocation);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002225 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01002226 {
Jamie Madill502d66f2013-06-20 11:55:52 -04002227 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01002228 declarationOut->appendDeclarator(symbol);
2229 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002230}
2231
Olli Etuaho13389b62016-10-16 11:48:18 +01002232void TParseContext::parseArrayDeclarator(TPublicType &publicType,
2233 const TSourceLoc &identifierLocation,
2234 const TString &identifier,
2235 const TSourceLoc &arrayLocation,
2236 TIntermTyped *indexExpression,
2237 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002238{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002239 // If the declaration starting this declarator list was empty (example: int,), some checks were
2240 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002241 if (mDeferredSingleDeclarationErrorCheck)
2242 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002243 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002244 mDeferredSingleDeclarationErrorCheck = false;
2245 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002246
Olli Etuaho856c4972016-08-08 11:38:39 +03002247 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002248
Olli Etuaho856c4972016-08-08 11:38:39 +03002249 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002250
Olli Etuaho8a176262016-08-16 14:23:01 +03002251 if (checkIsValidTypeAndQualifierForArray(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002252 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002253 TType arrayType = TType(publicType);
Olli Etuaho856c4972016-08-08 11:38:39 +03002254 unsigned int size = checkIsValidArraySize(arrayLocation, indexExpression);
Olli Etuaho693c9aa2015-04-07 17:50:36 +03002255 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002256
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002257 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002258 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002259
Jamie Madillb98c3a82015-07-23 14:26:04 -04002260 TIntermSymbol *symbol =
2261 intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002262 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002263 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002264
Olli Etuaho13389b62016-10-16 11:48:18 +01002265 declarationOut->appendDeclarator(symbol);
Jamie Madill502d66f2013-06-20 11:55:52 -04002266 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002267}
2268
Olli Etuaho13389b62016-10-16 11:48:18 +01002269void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2270 const TSourceLoc &identifierLocation,
2271 const TString &identifier,
2272 const TSourceLoc &initLocation,
2273 TIntermTyped *initializer,
2274 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002275{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002276 // If the declaration starting this declarator list was empty (example: int,), some checks were
2277 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002278 if (mDeferredSingleDeclarationErrorCheck)
2279 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002280 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002281 mDeferredSingleDeclarationErrorCheck = false;
2282 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002283
Olli Etuaho856c4972016-08-08 11:38:39 +03002284 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002285
Olli Etuaho13389b62016-10-16 11:48:18 +01002286 TIntermBinary *initNode = nullptr;
2287 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002288 {
2289 //
2290 // build the intermediate representation
2291 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002292 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002293 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002294 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002295 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002296 }
2297}
2298
Olli Etuaho13389b62016-10-16 11:48:18 +01002299void TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
2300 const TSourceLoc &identifierLocation,
2301 const TString &identifier,
2302 const TSourceLoc &indexLocation,
2303 TIntermTyped *indexExpression,
2304 const TSourceLoc &initLocation,
2305 TIntermTyped *initializer,
2306 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002307{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002308 // If the declaration starting this declarator list was empty (example: int,), some checks were
2309 // not performed.
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002310 if (mDeferredSingleDeclarationErrorCheck)
2311 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002312 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002313 mDeferredSingleDeclarationErrorCheck = false;
2314 }
2315
Olli Etuaho856c4972016-08-08 11:38:39 +03002316 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002317
Olli Etuaho8a176262016-08-16 14:23:01 +03002318 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002319
2320 TPublicType arrayType(publicType);
2321
Olli Etuaho856c4972016-08-08 11:38:39 +03002322 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002323 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2324 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002325 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002326 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002327 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002328 }
2329 // Make the type an array even if size check failed.
2330 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2331 arrayType.setArraySize(size);
2332
2333 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002334 TIntermBinary *initNode = nullptr;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002335 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
2336 {
2337 if (initNode)
2338 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002339 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002340 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002341 }
2342}
2343
Martin Radev70866b82016-07-22 15:27:42 +03002344void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002345{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002346 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002347 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002348
Martin Radev70866b82016-07-22 15:27:42 +03002349 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2350 typeQualifier.line);
2351
Jamie Madillc2128ff2016-07-04 10:26:17 -04002352 // It should never be the case, but some strange parser errors can send us here.
2353 if (layoutQualifier.isEmpty())
2354 {
2355 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002356 return;
2357 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002358
Martin Radev802abe02016-08-04 17:48:32 +03002359 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002360 {
Olli Etuaho43364892017-02-13 16:00:12 +00002361 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002362 return;
2363 }
2364
Olli Etuaho43364892017-02-13 16:00:12 +00002365 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2366
2367 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002368
2369 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2370
Andrei Volykhina5527072017-03-22 16:46:30 +03002371 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
2372
Martin Radev802abe02016-08-04 17:48:32 +03002373 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002374 {
Martin Radev802abe02016-08-04 17:48:32 +03002375 if (mComputeShaderLocalSizeDeclared &&
2376 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2377 {
2378 error(typeQualifier.line, "Work group size does not match the previous declaration",
2379 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002380 return;
2381 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002382
Martin Radev802abe02016-08-04 17:48:32 +03002383 if (mShaderVersion < 310)
2384 {
2385 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002386 return;
2387 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002388
Martin Radev4c4c8e72016-08-04 12:25:34 +03002389 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03002390 {
2391 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002392 return;
2393 }
2394
2395 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
2396 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
2397
2398 const TConstantUnion *maxComputeWorkGroupSizeData =
2399 maxComputeWorkGroupSize->getConstPointer();
2400
2401 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
2402 {
2403 if (layoutQualifier.localSize[i] != -1)
2404 {
2405 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
2406 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
2407 if (mComputeShaderLocalSize[i] < 1 ||
2408 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
2409 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002410 std::stringstream reasonStream;
2411 reasonStream << "invalid value: Value must be at least 1 and no greater than "
2412 << maxComputeWorkGroupSizeValue;
2413 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03002414
Olli Etuaho4de340a2016-12-16 09:32:03 +00002415 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03002416 return;
2417 }
2418 }
2419 }
2420
2421 mComputeShaderLocalSizeDeclared = true;
2422 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00002423 else if (mMultiviewAvailable &&
2424 (isExtensionEnabled("GL_OVR_multiview") || isExtensionEnabled("GL_OVR_multiview2")) &&
2425 typeQualifier.qualifier == EvqVertexIn)
2426 {
2427 // This error is only specified in WebGL, but tightens unspecified behavior in the native
2428 // specification.
2429 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
2430 {
2431 error(typeQualifier.line, "Number of views does not match the previous declaration",
2432 "layout");
2433 return;
2434 }
2435
2436 if (layoutQualifier.numViews == -1)
2437 {
2438 error(typeQualifier.line, "No num_views specified", "layout");
2439 return;
2440 }
2441
2442 if (layoutQualifier.numViews > mMaxNumViews)
2443 {
2444 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
2445 "layout");
2446 return;
2447 }
2448
2449 mNumViews = layoutQualifier.numViews;
2450 }
Martin Radev802abe02016-08-04 17:48:32 +03002451 else
Jamie Madill1566ef72013-06-20 11:55:54 -04002452 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00002453 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03002454 {
Martin Radev802abe02016-08-04 17:48:32 +03002455 return;
2456 }
2457
2458 if (typeQualifier.qualifier != EvqUniform)
2459 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002460 error(typeQualifier.line, "invalid qualifier: global layout must be uniform",
2461 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03002462 return;
2463 }
2464
2465 if (mShaderVersion < 300)
2466 {
2467 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
2468 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002469 return;
2470 }
2471
Olli Etuaho09b04a22016-12-15 13:30:26 +00002472 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002473
2474 if (layoutQualifier.matrixPacking != EmpUnspecified)
2475 {
2476 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
2477 }
2478
2479 if (layoutQualifier.blockStorage != EbsUnspecified)
2480 {
2481 mDefaultBlockStorage = layoutQualifier.blockStorage;
2482 }
Jamie Madill1566ef72013-06-20 11:55:54 -04002483 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002484}
2485
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002486TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
2487 const TFunction &function,
2488 const TSourceLoc &location,
2489 bool insertParametersToSymbolTable)
2490{
Olli Etuahofe486322017-03-21 09:30:54 +00002491 TIntermFunctionPrototype *prototype =
2492 new TIntermFunctionPrototype(function.getReturnType(), TSymbolUniqueId(function));
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002493 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
2494 // point to the data that already exists in the symbol table.
2495 prototype->getFunctionSymbolInfo()->setFromFunction(function);
2496 prototype->setLine(location);
2497
2498 for (size_t i = 0; i < function.getParamCount(); i++)
2499 {
2500 const TConstParameter &param = function.getParam(i);
2501
2502 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
2503 // be used for unused args).
2504 if (param.name != nullptr)
2505 {
2506 TVariable *variable = new TVariable(param.name, *param.type);
2507
2508 // Insert the parameter in the symbol table.
2509 if (insertParametersToSymbolTable && !symbolTable.declare(variable))
2510 {
2511 error(location, "redefinition", variable->getName().c_str());
2512 prototype->appendParameter(intermediate.addSymbol(0, "", *param.type, location));
2513 continue;
2514 }
2515 TIntermSymbol *symbol = intermediate.addSymbol(
2516 variable->getUniqueId(), variable->getName(), variable->getType(), location);
2517 prototype->appendParameter(symbol);
2518 }
2519 else
2520 {
2521 prototype->appendParameter(intermediate.addSymbol(0, "", *param.type, location));
2522 }
2523 }
2524 return prototype;
2525}
2526
Olli Etuaho16c745a2017-01-16 17:02:27 +00002527TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
2528 const TFunction &parsedFunction,
2529 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002530{
Olli Etuaho476197f2016-10-11 13:59:08 +01002531 // Note: function found from the symbol table could be the same as parsedFunction if this is the
2532 // first declaration. Either way the instance in the symbol table is used to track whether the
2533 // function is declared multiple times.
2534 TFunction *function = static_cast<TFunction *>(
2535 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
2536 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02002537 {
2538 // ESSL 1.00.17 section 4.2.7.
2539 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
2540 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02002541 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002542 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02002543
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002544 TIntermFunctionPrototype *prototype =
2545 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002546
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002547 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002548
2549 if (!symbolTable.atGlobalLevel())
2550 {
2551 // ESSL 3.00.4 section 4.2.4.
2552 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002553 }
2554
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002555 return prototype;
2556}
2557
Olli Etuaho336b1472016-10-05 16:37:55 +01002558TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002559 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01002560 TIntermBlock *functionBody,
2561 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002562{
Olli Etuahof51fdd22016-10-03 10:03:40 +01002563 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002564 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
2565 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002566 error(location, "function does not return a value:",
2567 functionPrototype->getFunctionSymbolInfo()->getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002568 }
2569
Olli Etuahof51fdd22016-10-03 10:03:40 +01002570 if (functionBody == nullptr)
2571 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01002572 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002573 functionBody->setLine(location);
2574 }
Olli Etuaho336b1472016-10-05 16:37:55 +01002575 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002576 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01002577 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01002578
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002579 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002580 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002581}
2582
Olli Etuaho476197f2016-10-11 13:59:08 +01002583void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
2584 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002585 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04002586{
Olli Etuaho476197f2016-10-11 13:59:08 +01002587 ASSERT(function);
2588 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002589 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01002590 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002591
2592 if (builtIn)
2593 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002594 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002595 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002596 else
Jamie Madill185fb402015-06-12 15:48:48 -04002597 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002598 TFunction *prevDec = static_cast<TFunction *>(
2599 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
2600
2601 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
2602 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
2603 // occurance.
2604 if (*function != prevDec)
2605 {
2606 // Swap the parameters of the previous declaration to the parameters of the function
2607 // definition (parameter names may differ).
2608 prevDec->swapParameters(**function);
2609
2610 // The function definition will share the same symbol as any previous declaration.
2611 *function = prevDec;
2612 }
2613
2614 if ((*function)->isDefined())
2615 {
2616 error(location, "function already has a body", (*function)->getName().c_str());
2617 }
2618
2619 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04002620 }
Jamie Madill185fb402015-06-12 15:48:48 -04002621
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002622 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01002623 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002624 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04002625
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002626 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04002627 setLoopNestingLevel(0);
2628}
2629
Jamie Madillb98c3a82015-07-23 14:26:04 -04002630TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002631{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002632 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002633 // We don't know at this point whether this is a function definition or a prototype.
2634 // The definition production code will check for redefinitions.
2635 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002636 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002637 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
2638 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002639 //
2640 TFunction *prevDec =
2641 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302642
Martin Radevda6254b2016-12-14 17:00:36 +02002643 if (getShaderVersion() >= 300 &&
2644 symbolTable.hasUnmangledBuiltInForShaderVersion(function->getName().c_str(),
2645 getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302646 {
Martin Radevda6254b2016-12-14 17:00:36 +02002647 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302648 // Therefore overloading or redefining builtin functions is an error.
2649 error(location, "Name of a built-in function cannot be redeclared as function",
2650 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302651 }
2652 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002653 {
2654 if (prevDec->getReturnType() != function->getReturnType())
2655 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002656 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002657 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04002658 }
2659 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2660 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002661 if (prevDec->getParam(i).type->getQualifier() !=
2662 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002663 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002664 error(location,
2665 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002666 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04002667 }
2668 }
2669 }
2670
2671 //
2672 // Check for previously declared variables using the same name.
2673 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002674 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002675 if (prevSym)
2676 {
2677 if (!prevSym->isFunction())
2678 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002679 error(location, "redefinition of a function", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002680 }
2681 }
2682 else
2683 {
2684 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01002685 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04002686 }
2687
2688 // We're at the inner scope level of the function's arguments and body statement.
2689 // Add the function prototype to the surrounding scope instead.
2690 symbolTable.getOuterLevel()->insert(function);
2691
Olli Etuaho78d13742017-01-18 13:06:10 +00002692 // Raise error message if main function takes any parameters or return anything other than void
2693 if (function->getName() == "main")
2694 {
2695 if (function->getParamCount() > 0)
2696 {
2697 error(location, "function cannot take any parameter(s)", "main");
2698 }
2699 if (function->getReturnType().getBasicType() != EbtVoid)
2700 {
2701 error(location, "main function cannot return a value",
2702 function->getReturnType().getBasicString());
2703 }
2704 }
2705
Jamie Madill185fb402015-06-12 15:48:48 -04002706 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002707 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2708 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002709 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2710 //
2711 return function;
2712}
2713
Olli Etuaho9de84a52016-06-14 17:36:01 +03002714TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
2715 const TString *name,
2716 const TSourceLoc &location)
2717{
2718 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
2719 {
2720 error(location, "no qualifiers allowed for function return",
2721 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03002722 }
2723 if (!type.layoutQualifier.isEmpty())
2724 {
2725 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03002726 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002727 // make sure a sampler or an image is not involved as well...
Martin Radev4a9cd802016-09-01 16:51:51 +03002728 checkIsNotSampler(location, type.typeSpecifierNonArray,
2729 "samplers can't be function return values");
Martin Radev2cc85b32016-08-05 16:22:53 +03002730 checkIsNotImage(location, type.typeSpecifierNonArray, "images can't be function return values");
Olli Etuahoe29324f2016-06-15 10:58:03 +03002731 if (mShaderVersion < 300)
2732 {
2733 // Array return values are forbidden, but there's also no valid syntax for declaring array
2734 // return values in ESSL 1.00.
Olli Etuaho77ba4082016-12-16 12:01:18 +00002735 ASSERT(type.arraySize == 0 || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03002736
2737 if (type.isStructureContainingArrays())
2738 {
2739 // ESSL 1.00.17 section 6.1 Function Definitions
2740 error(location, "structures containing arrays can't be function return values",
2741 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03002742 }
2743 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03002744
2745 // Add the function as a prototype after parsing it (we do not support recursion)
2746 return new TFunction(name, new TType(type));
2747}
2748
Jamie Madill06145232015-05-13 13:10:01 -04002749TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002750{
Jamie Madill06145232015-05-13 13:10:01 -04002751 TPublicType publicType = publicTypeIn;
Martin Radev4a9cd802016-09-01 16:51:51 +03002752 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02002753 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002754 error(publicType.getLine(), "constructor can't be a structure definition",
2755 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02002756 }
2757
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002758 TOperator op = EOpNull;
Martin Radev4a9cd802016-09-01 16:51:51 +03002759 if (publicType.getUserDef())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002760 {
2761 op = EOpConstructStruct;
2762 }
2763 else
2764 {
Geoff Lang156d7192016-07-21 16:11:00 -04002765 op = sh::TypeToConstructorOperator(TType(publicType));
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002766 if (op == EOpNull)
2767 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002768 error(publicType.getLine(), "cannot construct this type",
2769 getBasicString(publicType.getBasicType()));
2770 publicType.setBasicType(EbtFloat);
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002771 op = EOpConstructFloat;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002772 }
2773 }
2774
Dmitry Skiba7f17a502015-06-22 15:08:39 -07002775 const TType *type = new TType(publicType);
Olli Etuaho72d10202017-01-19 15:58:30 +00002776 return new TFunction(nullptr, type, op);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002777}
2778
Jamie Madillb98c3a82015-07-23 14:26:04 -04002779// This function is used to test for the correctness of the parameters passed to various constructor
2780// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002781//
Olli Etuaho856c4972016-08-08 11:38:39 +03002782// 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 +00002783//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002784TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002785 TOperator op,
Olli Etuaho72d10202017-01-19 15:58:30 +00002786 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302787 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002788{
Olli Etuaho856c4972016-08-08 11:38:39 +03002789 if (type.isUnsizedArray())
2790 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002791 if (arguments->empty())
Olli Etuahobbe9fb52016-11-03 17:16:05 +00002792 {
2793 error(line, "implicitly sized array constructor must have at least one argument", "[]");
2794 type.setArraySize(1u);
2795 return TIntermTyped::CreateZero(type);
2796 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002797 type.setArraySize(static_cast<unsigned int>(arguments->size()));
Olli Etuaho856c4972016-08-08 11:38:39 +03002798 }
Olli Etuaho856c4972016-08-08 11:38:39 +03002799
Olli Etuaho72d10202017-01-19 15:58:30 +00002800 if (!checkConstructorArguments(line, arguments, op, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03002801 {
Olli Etuaho72d10202017-01-19 15:58:30 +00002802 return TIntermTyped::CreateZero(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03002803 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002804
Olli Etuahofe486322017-03-21 09:30:54 +00002805 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002806 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002807
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002808 TIntermTyped *constConstructor =
2809 intermediate.foldAggregateBuiltIn(constructorNode, mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002810 if (constConstructor)
2811 {
2812 return constConstructor;
2813 }
2814
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002815 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002816}
2817
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002818//
2819// Interface/uniform blocks
2820//
Olli Etuaho13389b62016-10-16 11:48:18 +01002821TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03002822 const TTypeQualifierBuilder &typeQualifierBuilder,
2823 const TSourceLoc &nameLine,
2824 const TString &blockName,
2825 TFieldList *fieldList,
2826 const TString *instanceName,
2827 const TSourceLoc &instanceLine,
2828 TIntermTyped *arrayIndex,
2829 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002830{
Olli Etuaho856c4972016-08-08 11:38:39 +03002831 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002832
Olli Etuaho77ba4082016-12-16 12:01:18 +00002833 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03002834
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002835 if (typeQualifier.qualifier != EvqUniform)
2836 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002837 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform",
2838 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002839 }
2840
Martin Radev70866b82016-07-22 15:27:42 +03002841 if (typeQualifier.invariant)
2842 {
2843 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
2844 }
2845
Olli Etuaho43364892017-02-13 16:00:12 +00002846 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
2847
2848 // TODO(oetuaho): Remove this and support binding for blocks.
2849 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03002850
Andrei Volykhina5527072017-03-22 16:46:30 +03002851 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
2852
Jamie Madill099c0f32013-06-20 11:55:52 -04002853 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03002854 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002855
Jamie Madill099c0f32013-06-20 11:55:52 -04002856 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2857 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002858 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002859 }
2860
Jamie Madill1566ef72013-06-20 11:55:54 -04002861 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2862 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002863 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002864 }
2865
Olli Etuaho856c4972016-08-08 11:38:39 +03002866 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002867
Martin Radev2cc85b32016-08-05 16:22:53 +03002868 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
2869
Arun Patole7e7e68d2015-05-22 12:02:25 +05302870 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2871 if (!symbolTable.declare(blockNameSymbol))
2872 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002873 error(nameLine, "redefinition of an interface block name", blockName.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002874 }
2875
Jamie Madill98493dd2013-07-08 14:39:03 -04002876 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302877 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2878 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002879 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302880 TType *fieldType = field->type();
2881 if (IsSampler(fieldType->getBasicType()))
2882 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002883 error(field->line(),
2884 "unsupported type - sampler types are not allowed in interface blocks",
2885 fieldType->getBasicString());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002886 }
2887
Martin Radev2cc85b32016-08-05 16:22:53 +03002888 if (IsImage(fieldType->getBasicType()))
2889 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002890 error(field->line(),
2891 "unsupported type - image types are not allowed in interface blocks",
2892 fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03002893 }
2894
Jamie Madill98493dd2013-07-08 14:39:03 -04002895 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002896 switch (qualifier)
2897 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002898 case EvqGlobal:
2899 case EvqUniform:
2900 break;
2901 default:
2902 error(field->line(), "invalid qualifier on interface block member",
2903 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04002904 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002905 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002906
Martin Radev70866b82016-07-22 15:27:42 +03002907 if (fieldType->isInvariant())
2908 {
2909 error(field->line(), "invalid qualifier on interface block member", "invariant");
2910 }
2911
Jamie Madilla5efff92013-06-06 11:56:47 -04002912 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002913 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03002914 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
Jamie Madill099c0f32013-06-20 11:55:52 -04002915
Jamie Madill98493dd2013-07-08 14:39:03 -04002916 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002917 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002918 error(field->line(), "invalid layout qualifier: cannot be used here",
2919 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04002920 }
2921
Jamie Madill98493dd2013-07-08 14:39:03 -04002922 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002923 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002924 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002925 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002926 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002927 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002928 warning(field->line(),
2929 "extraneous layout qualifier: only has an effect on matrix types",
2930 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04002931 }
2932
Jamie Madill98493dd2013-07-08 14:39:03 -04002933 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002934 }
2935
Jamie Madill98493dd2013-07-08 14:39:03 -04002936 // add array index
Olli Etuaho856c4972016-08-08 11:38:39 +03002937 unsigned int arraySize = 0;
2938 if (arrayIndex != nullptr)
Jamie Madill98493dd2013-07-08 14:39:03 -04002939 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002940 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04002941 }
2942
Jamie Madillb98c3a82015-07-23 14:26:04 -04002943 TInterfaceBlock *interfaceBlock =
2944 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2945 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
2946 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002947
2948 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04002949 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002950
Jamie Madill98493dd2013-07-08 14:39:03 -04002951 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002952 {
2953 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002954 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2955 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002956 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302957 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002958
2959 // set parent pointer of the field variable
2960 fieldType->setInterfaceBlock(interfaceBlock);
2961
Arun Patole7e7e68d2015-05-22 12:02:25 +05302962 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002963 fieldVariable->setQualifier(typeQualifier.qualifier);
2964
Arun Patole7e7e68d2015-05-22 12:02:25 +05302965 if (!symbolTable.declare(fieldVariable))
2966 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002967 error(field->line(), "redefinition of an interface block member name",
2968 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002969 }
2970 }
2971 }
2972 else
2973 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002974 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03002975
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002976 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302977 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002978 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002979
Arun Patole7e7e68d2015-05-22 12:02:25 +05302980 if (!symbolTable.declare(instanceTypeDef))
2981 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002982 error(instanceLine, "redefinition of an interface block instance name",
2983 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002984 }
2985
Jamie Madillb98c3a82015-07-23 14:26:04 -04002986 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002987 symbolName = instanceTypeDef->getName();
2988 }
2989
Olli Etuaho13389b62016-10-16 11:48:18 +01002990 TIntermSymbol *blockSymbol =
2991 intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line);
2992 TIntermDeclaration *declaration = new TIntermDeclaration();
2993 declaration->appendDeclarator(blockSymbol);
2994 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04002995
2996 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01002997 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002998}
2999
Olli Etuaho383b7912016-08-05 11:22:59 +03003000void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003001{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003002 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003003
3004 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003005 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303006 if (mStructNestingLevel > 1)
3007 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003008 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003009 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003010}
3011
3012void TParseContext::exitStructDeclaration()
3013{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003014 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003015}
3016
Olli Etuaho8a176262016-08-16 14:23:01 +03003017void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003018{
Jamie Madillacb4b812016-11-07 13:50:29 -05003019 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303020 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003021 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003022 }
3023
Arun Patole7e7e68d2015-05-22 12:02:25 +05303024 if (field.type()->getBasicType() != EbtStruct)
3025 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003026 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003027 }
3028
3029 // We're already inside a structure definition at this point, so add
3030 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303031 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3032 {
Jamie Madill41a49272014-03-18 16:10:13 -04003033 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003034 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
3035 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003036 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003037 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003038 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003039 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003040}
3041
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003042//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003043// Parse an array index expression
3044//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003045TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3046 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303047 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003048{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003049 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3050 {
3051 if (baseExpression->getAsSymbolNode())
3052 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303053 error(location, " left of '[' is not of type array, matrix, or vector ",
3054 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003055 }
3056 else
3057 {
3058 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3059 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003060
3061 TConstantUnion *unionArray = new TConstantUnion[1];
3062 unionArray->setFConst(0.0f);
3063 return intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst),
3064 location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003065 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003066
Jamie Madill21c1e452014-12-29 11:33:41 -05003067 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3068
Olli Etuaho36b05142015-11-12 13:10:42 +02003069 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3070 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3071 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3072 // index is a constant expression.
3073 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3074 {
3075 if (baseExpression->isInterfaceBlock())
3076 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003077 error(location,
3078 "array indexes for interface blocks arrays must be constant integral expressions",
3079 "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003080 }
3081 else if (baseExpression->getQualifier() == EvqFragmentOut)
3082 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003083 error(location,
3084 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003085 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003086 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3087 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003088 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003089 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003090 }
3091
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003092 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003093 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003094 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3095 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3096 // constant fold expressions that are not constant expressions). The most compatible way to
3097 // handle this case is to report a warning instead of an error and force the index to be in
3098 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003099 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003100 int index = indexConstantUnion->getIConst(0);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003101
3102 int safeIndex = -1;
3103
3104 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04003105 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003106 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003107 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003108 if (mShaderSpec == SH_WEBGL2_SPEC)
3109 {
3110 // Error has been already generated if index is not const.
3111 if (indexExpression->getQualifier() == EvqConst)
3112 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003113 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003114 }
3115 safeIndex = 0;
3116 }
3117 else if (!isExtensionEnabled("GL_EXT_draw_buffers"))
3118 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003119 outOfRangeError(outOfRangeIndexIsError, location,
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003120 "array index for gl_FragData must be zero when "
Olli Etuaho4de340a2016-12-16 09:32:03 +00003121 "GL_EXT_draw_buffers is disabled",
3122 "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003123 safeIndex = 0;
3124 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03003125 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003126 // Only do generic out-of-range check if similar error hasn't already been reported.
3127 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003128 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003129 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3130 baseExpression->getArraySize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003131 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003132 }
3133 }
3134 else if (baseExpression->isMatrix())
3135 {
3136 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03003137 baseExpression->getType().getCols(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003138 "matrix field selection out of range");
Jamie Madill7164cf42013-07-08 13:30:59 -04003139 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003140 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04003141 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003142 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3143 baseExpression->getType().getNominalSize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003144 "vector field selection out of range");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003145 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003146
3147 ASSERT(safeIndex >= 0);
3148 // Data of constant unions can't be changed, because it may be shared with other
3149 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
3150 // sanitized object.
3151 if (safeIndex != index)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003152 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003153 TConstantUnion *safeConstantUnion = new TConstantUnion();
3154 safeConstantUnion->setIConst(safeIndex);
3155 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003156 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003157
3158 return intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003159 mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003160 }
Jamie Madill7164cf42013-07-08 13:30:59 -04003161 else
3162 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003163 return intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003164 mDiagnostics);
Jamie Madill7164cf42013-07-08 13:30:59 -04003165 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003166}
3167
Olli Etuaho90892fb2016-07-14 14:44:51 +03003168int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
3169 const TSourceLoc &location,
3170 int index,
3171 int arraySize,
Olli Etuaho4de340a2016-12-16 09:32:03 +00003172 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003173{
3174 if (index >= arraySize || index < 0)
3175 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003176 std::stringstream reasonStream;
3177 reasonStream << reason << " '" << index << "'";
3178 std::string token = reasonStream.str();
3179 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuaho90892fb2016-07-14 14:44:51 +03003180 if (index < 0)
3181 {
3182 return 0;
3183 }
3184 else
3185 {
3186 return arraySize - 1;
3187 }
3188 }
3189 return index;
3190}
3191
Jamie Madillb98c3a82015-07-23 14:26:04 -04003192TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3193 const TSourceLoc &dotLocation,
3194 const TString &fieldString,
3195 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003196{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003197 if (baseExpression->isArray())
3198 {
3199 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003200 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003201 }
3202
3203 if (baseExpression->isVector())
3204 {
3205 TVectorFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003206 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields,
3207 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003208 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003209 fields.num = 1;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003210 fields.offsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003211 }
3212
Olli Etuahob6fa0432016-09-28 16:28:05 +01003213 return TIntermediate::AddSwizzle(baseExpression, fields, dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003214 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003215 else if (baseExpression->getBasicType() == EbtStruct)
3216 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303217 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003218 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003219 {
3220 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003221 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003222 }
3223 else
3224 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003225 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003226 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003227 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003228 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003229 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003230 {
3231 fieldFound = true;
3232 break;
3233 }
3234 }
3235 if (fieldFound)
3236 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003237 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3238 index->setLine(fieldLocation);
3239 return intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003240 dotLocation, mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003241 }
3242 else
3243 {
3244 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003245 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003246 }
3247 }
3248 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003249 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003250 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303251 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003252 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003253 {
3254 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003255 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003256 }
3257 else
3258 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003259 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003260 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003261 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003262 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003263 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003264 {
3265 fieldFound = true;
3266 break;
3267 }
3268 }
3269 if (fieldFound)
3270 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003271 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3272 index->setLine(fieldLocation);
3273 return intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003274 dotLocation, mDiagnostics);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003275 }
3276 else
3277 {
3278 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003279 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003280 }
3281 }
3282 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003283 else
3284 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003285 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003286 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03003287 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303288 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003289 }
3290 else
3291 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303292 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003293 " field selection requires structure, vector, or interface block on left hand "
3294 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303295 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003296 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003297 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003298 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003299}
3300
Jamie Madillb98c3a82015-07-23 14:26:04 -04003301TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3302 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003303{
Martin Radev802abe02016-08-04 17:48:32 +03003304 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003305
3306 if (qualifierType == "shared")
3307 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003308 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003309 {
3310 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
3311 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003312 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003313 }
3314 else if (qualifierType == "packed")
3315 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003316 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003317 {
3318 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
3319 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003320 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003321 }
3322 else if (qualifierType == "std140")
3323 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003324 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003325 }
3326 else if (qualifierType == "row_major")
3327 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003328 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003329 }
3330 else if (qualifierType == "column_major")
3331 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003332 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003333 }
3334 else if (qualifierType == "location")
3335 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003336 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
3337 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003338 }
Andrei Volykhina5527072017-03-22 16:46:30 +03003339 else if (qualifierType == "yuv" && isExtensionEnabled("GL_EXT_YUV_target") &&
3340 mShaderType == GL_FRAGMENT_SHADER)
3341 {
3342 qualifier.yuv = true;
3343 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003344 else if (qualifierType == "rgba32f")
3345 {
3346 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3347 qualifier.imageInternalFormat = EiifRGBA32F;
3348 }
3349 else if (qualifierType == "rgba16f")
3350 {
3351 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3352 qualifier.imageInternalFormat = EiifRGBA16F;
3353 }
3354 else if (qualifierType == "r32f")
3355 {
3356 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3357 qualifier.imageInternalFormat = EiifR32F;
3358 }
3359 else if (qualifierType == "rgba8")
3360 {
3361 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3362 qualifier.imageInternalFormat = EiifRGBA8;
3363 }
3364 else if (qualifierType == "rgba8_snorm")
3365 {
3366 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3367 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
3368 }
3369 else if (qualifierType == "rgba32i")
3370 {
3371 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3372 qualifier.imageInternalFormat = EiifRGBA32I;
3373 }
3374 else if (qualifierType == "rgba16i")
3375 {
3376 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3377 qualifier.imageInternalFormat = EiifRGBA16I;
3378 }
3379 else if (qualifierType == "rgba8i")
3380 {
3381 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3382 qualifier.imageInternalFormat = EiifRGBA8I;
3383 }
3384 else if (qualifierType == "r32i")
3385 {
3386 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3387 qualifier.imageInternalFormat = EiifR32I;
3388 }
3389 else if (qualifierType == "rgba32ui")
3390 {
3391 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3392 qualifier.imageInternalFormat = EiifRGBA32UI;
3393 }
3394 else if (qualifierType == "rgba16ui")
3395 {
3396 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3397 qualifier.imageInternalFormat = EiifRGBA16UI;
3398 }
3399 else if (qualifierType == "rgba8ui")
3400 {
3401 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3402 qualifier.imageInternalFormat = EiifRGBA8UI;
3403 }
3404 else if (qualifierType == "r32ui")
3405 {
3406 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3407 qualifier.imageInternalFormat = EiifR32UI;
3408 }
3409
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003410 else
3411 {
3412 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003413 }
3414
Jamie Madilla5efff92013-06-06 11:56:47 -04003415 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003416}
3417
Martin Radev802abe02016-08-04 17:48:32 +03003418void TParseContext::parseLocalSize(const TString &qualifierType,
3419 const TSourceLoc &qualifierTypeLine,
3420 int intValue,
3421 const TSourceLoc &intValueLine,
3422 const std::string &intValueString,
3423 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03003424 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03003425{
Olli Etuaho856c4972016-08-08 11:38:39 +03003426 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03003427 if (intValue < 1)
3428 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003429 std::stringstream reasonStream;
3430 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
3431 std::string reason = reasonStream.str();
3432 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003433 }
3434 (*localSize)[index] = intValue;
3435}
3436
Olli Etuaho09b04a22016-12-15 13:30:26 +00003437void TParseContext::parseNumViews(int intValue,
3438 const TSourceLoc &intValueLine,
3439 const std::string &intValueString,
3440 int *numViews)
3441{
3442 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3443 // specification.
3444 if (intValue < 1)
3445 {
3446 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
3447 }
3448 *numViews = intValue;
3449}
3450
Jamie Madillb98c3a82015-07-23 14:26:04 -04003451TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3452 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003453 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303454 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003455{
Martin Radev802abe02016-08-04 17:48:32 +03003456 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003457
Martin Radev802abe02016-08-04 17:48:32 +03003458 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003459
Martin Radev802abe02016-08-04 17:48:32 +03003460 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003461 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003462 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003463 if (intValue < 0)
3464 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003465 error(intValueLine, "out of range: location must be non-negative",
3466 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003467 }
3468 else
3469 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003470 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03003471 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003472 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003473 }
Olli Etuaho43364892017-02-13 16:00:12 +00003474 else if (qualifierType == "binding")
3475 {
3476 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3477 if (intValue < 0)
3478 {
3479 error(intValueLine, "out of range: binding must be non-negative",
3480 intValueString.c_str());
3481 }
3482 else
3483 {
3484 qualifier.binding = intValue;
3485 }
3486 }
Martin Radev802abe02016-08-04 17:48:32 +03003487 else if (qualifierType == "local_size_x")
3488 {
3489 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
3490 &qualifier.localSize);
3491 }
3492 else if (qualifierType == "local_size_y")
3493 {
3494 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
3495 &qualifier.localSize);
3496 }
3497 else if (qualifierType == "local_size_z")
3498 {
3499 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
3500 &qualifier.localSize);
3501 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00003502 else if (qualifierType == "num_views" && mMultiviewAvailable &&
3503 (isExtensionEnabled("GL_OVR_multiview") || isExtensionEnabled("GL_OVR_multiview2")) &&
3504 mShaderType == GL_VERTEX_SHADER)
3505 {
3506 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
3507 }
Martin Radev802abe02016-08-04 17:48:32 +03003508 else
3509 {
3510 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003511 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003512
Jamie Madilla5efff92013-06-06 11:56:47 -04003513 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003514}
3515
Olli Etuaho613b9592016-09-05 12:05:53 +03003516TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
3517{
3518 return new TTypeQualifierBuilder(
3519 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
3520 mShaderVersion);
3521}
3522
Jamie Madillb98c3a82015-07-23 14:26:04 -04003523TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03003524 TLayoutQualifier rightQualifier,
3525 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003526{
Martin Radevc28888b2016-07-22 15:27:42 +03003527 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003528 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003529}
3530
Olli Etuaho4de340a2016-12-16 09:32:03 +00003531TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
3532 const TFieldList *newlyAddedFields,
3533 const TSourceLoc &location)
3534{
3535 for (TField *field : *newlyAddedFields)
3536 {
3537 for (TField *oldField : *processedFields)
3538 {
3539 if (oldField->name() == field->name())
3540 {
3541 error(location, "duplicate field name in structure", field->name().c_str());
3542 }
3543 }
3544 processedFields->push_back(field);
3545 }
3546 return processedFields;
3547}
3548
Martin Radev70866b82016-07-22 15:27:42 +03003549TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
3550 const TTypeQualifierBuilder &typeQualifierBuilder,
3551 TPublicType *typeSpecifier,
3552 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003553{
Olli Etuaho77ba4082016-12-16 12:01:18 +00003554 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003555
Martin Radev70866b82016-07-22 15:27:42 +03003556 typeSpecifier->qualifier = typeQualifier.qualifier;
3557 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03003558 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03003559 typeSpecifier->invariant = typeQualifier.invariant;
3560 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05303561 {
Martin Radev70866b82016-07-22 15:27:42 +03003562 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003563 }
Martin Radev70866b82016-07-22 15:27:42 +03003564 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003565}
3566
Jamie Madillb98c3a82015-07-23 14:26:04 -04003567TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3568 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003569{
Martin Radev4a9cd802016-09-01 16:51:51 +03003570 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
3571 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03003572
Martin Radev4a9cd802016-09-01 16:51:51 +03003573 checkIsNonVoid(typeSpecifier.getLine(), (*fieldList)[0]->name(), typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003574
Martin Radev4a9cd802016-09-01 16:51:51 +03003575 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003576
Arun Patole7e7e68d2015-05-22 12:02:25 +05303577 for (unsigned int i = 0; i < fieldList->size(); ++i)
3578 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003579 //
3580 // Careful not to replace already known aspects of type, like array-ness
3581 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303582 TType *type = (*fieldList)[i]->type();
Martin Radev4a9cd802016-09-01 16:51:51 +03003583 type->setBasicType(typeSpecifier.getBasicType());
3584 type->setPrimarySize(typeSpecifier.getPrimarySize());
3585 type->setSecondarySize(typeSpecifier.getSecondarySize());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003586 type->setPrecision(typeSpecifier.precision);
3587 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003588 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
Martin Radev2cc85b32016-08-05 16:22:53 +03003589 type->setMemoryQualifier(typeSpecifier.memoryQualifier);
Martin Radev70866b82016-07-22 15:27:42 +03003590 type->setInvariant(typeSpecifier.invariant);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003591
3592 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303593 if (type->isArray())
3594 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003595 checkIsValidTypeForArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003596 }
3597 if (typeSpecifier.array)
Olli Etuaho856c4972016-08-08 11:38:39 +03003598 type->setArraySize(static_cast<unsigned int>(typeSpecifier.arraySize));
Martin Radev4a9cd802016-09-01 16:51:51 +03003599 if (typeSpecifier.getUserDef())
Arun Patole7e7e68d2015-05-22 12:02:25 +05303600 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003601 type->setStruct(typeSpecifier.getUserDef()->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003602 }
3603
Martin Radev4a9cd802016-09-01 16:51:51 +03003604 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*fieldList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003605 }
3606
Jamie Madill98493dd2013-07-08 14:39:03 -04003607 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003608}
3609
Martin Radev4a9cd802016-09-01 16:51:51 +03003610TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
3611 const TSourceLoc &nameLine,
3612 const TString *structName,
3613 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003614{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303615 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003616 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003617
Jamie Madill9b820842015-02-12 10:40:10 -05003618 // Store a bool in the struct if we're at global scope, to allow us to
3619 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05003620 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003621
Jamie Madill98493dd2013-07-08 14:39:03 -04003622 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003623 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003624 checkIsNotReserved(nameLine, *structName);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303625 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
3626 if (!symbolTable.declare(userTypeDef))
3627 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003628 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003629 }
3630 }
3631
3632 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04003633 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003634 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003635 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04003636 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003637 switch (qualifier)
3638 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003639 case EvqGlobal:
3640 case EvqTemporary:
3641 break;
3642 default:
3643 error(field.line(), "invalid qualifier on struct member",
3644 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003645 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003646 }
Martin Radev70866b82016-07-22 15:27:42 +03003647 if (field.type()->isInvariant())
3648 {
3649 error(field.line(), "invalid qualifier on struct member", "invariant");
3650 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003651 if (IsImage(field.type()->getBasicType()))
3652 {
3653 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
3654 }
3655
Olli Etuaho43364892017-02-13 16:00:12 +00003656 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
3657
3658 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03003659
3660 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003661 }
3662
Martin Radev4a9cd802016-09-01 16:51:51 +03003663 TTypeSpecifierNonArray typeSpecifierNonArray;
3664 typeSpecifierNonArray.initialize(EbtStruct, structLine);
3665 typeSpecifierNonArray.userDef = structureType;
3666 typeSpecifierNonArray.isStructSpecifier = true;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003667 exitStructDeclaration();
3668
Martin Radev4a9cd802016-09-01 16:51:51 +03003669 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003670}
3671
Jamie Madillb98c3a82015-07-23 14:26:04 -04003672TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003673 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003674 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02003675{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003676 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003677 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02003678 init->isVector())
3679 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003680 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
3681 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003682 return nullptr;
3683 }
3684
Olli Etuahoac5274d2015-02-20 10:19:08 +02003685 if (statementList)
3686 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00003687 if (!ValidateSwitchStatementList(switchType, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02003688 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02003689 return nullptr;
3690 }
3691 }
3692
Olli Etuahoa3a36662015-02-17 13:46:51 +02003693 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3694 if (node == nullptr)
3695 {
3696 error(loc, "erroneous switch statement", "switch");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003697 return nullptr;
3698 }
3699 return node;
3700}
3701
3702TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3703{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003704 if (mSwitchNestingLevel == 0)
3705 {
3706 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003707 return nullptr;
3708 }
3709 if (condition == nullptr)
3710 {
3711 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003712 return nullptr;
3713 }
3714 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04003715 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02003716 {
3717 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003718 }
3719 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003720 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
3721 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
3722 // fold in case labels.
3723 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02003724 {
3725 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003726 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003727 TIntermCase *node = intermediate.addCase(condition, loc);
3728 if (node == nullptr)
3729 {
3730 error(loc, "erroneous case statement", "case");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003731 return nullptr;
3732 }
3733 return node;
3734}
3735
3736TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3737{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003738 if (mSwitchNestingLevel == 0)
3739 {
3740 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003741 return nullptr;
3742 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003743 TIntermCase *node = intermediate.addCase(nullptr, loc);
3744 if (node == nullptr)
3745 {
3746 error(loc, "erroneous default statement", "default");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003747 return nullptr;
3748 }
3749 return node;
3750}
3751
Jamie Madillb98c3a82015-07-23 14:26:04 -04003752TIntermTyped *TParseContext::createUnaryMath(TOperator op,
3753 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003754 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003755{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003756 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003757
3758 switch (op)
3759 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003760 case EOpLogicalNot:
3761 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
3762 child->isVector())
3763 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003764 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003765 return nullptr;
3766 }
3767 break;
3768 case EOpBitwiseNot:
3769 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3770 child->isMatrix() || child->isArray())
3771 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003772 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003773 return nullptr;
3774 }
3775 break;
3776 case EOpPostIncrement:
3777 case EOpPreIncrement:
3778 case EOpPostDecrement:
3779 case EOpPreDecrement:
3780 case EOpNegative:
3781 case EOpPositive:
3782 if (child->getBasicType() == EbtStruct || child->getBasicType() == EbtBool ||
Martin Radev2cc85b32016-08-05 16:22:53 +03003783 child->isArray() || IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04003784 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003785 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003786 return nullptr;
3787 }
3788 // Operators for built-ins are already type checked against their prototype.
3789 default:
3790 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003791 }
3792
Olli Etuahof119a262016-08-19 15:54:22 +03003793 TIntermUnary *node = new TIntermUnary(op, child);
3794 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03003795
Olli Etuaho77ba4082016-12-16 12:01:18 +00003796 TIntermTyped *foldedNode = node->fold(mDiagnostics);
Olli Etuahof119a262016-08-19 15:54:22 +03003797 if (foldedNode)
3798 return foldedNode;
3799
3800 return node;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003801}
3802
Olli Etuaho09b22472015-02-11 11:47:26 +02003803TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3804{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003805 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003806 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003807 {
Olli Etuaho09b22472015-02-11 11:47:26 +02003808 return child;
3809 }
3810 return node;
3811}
3812
Jamie Madillb98c3a82015-07-23 14:26:04 -04003813TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
3814 TIntermTyped *child,
3815 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003816{
Olli Etuaho856c4972016-08-08 11:38:39 +03003817 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02003818 return addUnaryMath(op, child, loc);
3819}
3820
Jamie Madillb98c3a82015-07-23 14:26:04 -04003821bool TParseContext::binaryOpCommonCheck(TOperator op,
3822 TIntermTyped *left,
3823 TIntermTyped *right,
3824 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003825{
Olli Etuaho244be012016-08-18 15:26:02 +03003826 if (left->getType().getStruct() || right->getType().getStruct())
3827 {
3828 switch (op)
3829 {
3830 case EOpIndexDirectStruct:
3831 ASSERT(left->getType().getStruct());
3832 break;
3833 case EOpEqual:
3834 case EOpNotEqual:
3835 case EOpAssign:
3836 case EOpInitialize:
3837 if (left->getType() != right->getType())
3838 {
3839 return false;
3840 }
3841 break;
3842 default:
3843 error(loc, "Invalid operation for structs", GetOperatorString(op));
3844 return false;
3845 }
3846 }
3847
Olli Etuahod6b14282015-03-17 14:31:35 +02003848 if (left->isArray() || right->isArray())
3849 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003850 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003851 {
3852 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3853 return false;
3854 }
3855
3856 if (left->isArray() != right->isArray())
3857 {
3858 error(loc, "array / non-array mismatch", GetOperatorString(op));
3859 return false;
3860 }
3861
3862 switch (op)
3863 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003864 case EOpEqual:
3865 case EOpNotEqual:
3866 case EOpAssign:
3867 case EOpInitialize:
3868 break;
3869 default:
3870 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3871 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02003872 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003873 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003874 if (left->getArraySize() != right->getArraySize())
3875 {
3876 error(loc, "array size mismatch", GetOperatorString(op));
3877 return false;
3878 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003879 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003880
3881 // Check ops which require integer / ivec parameters
3882 bool isBitShift = false;
3883 switch (op)
3884 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003885 case EOpBitShiftLeft:
3886 case EOpBitShiftRight:
3887 case EOpBitShiftLeftAssign:
3888 case EOpBitShiftRightAssign:
3889 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3890 // check that the basic type is an integer type.
3891 isBitShift = true;
3892 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3893 {
3894 return false;
3895 }
3896 break;
3897 case EOpBitwiseAnd:
3898 case EOpBitwiseXor:
3899 case EOpBitwiseOr:
3900 case EOpBitwiseAndAssign:
3901 case EOpBitwiseXorAssign:
3902 case EOpBitwiseOrAssign:
3903 // It is enough to check the type of only one operand, since later it
3904 // is checked that the operand types match.
3905 if (!IsInteger(left->getBasicType()))
3906 {
3907 return false;
3908 }
3909 break;
3910 default:
3911 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003912 }
3913
3914 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3915 // So the basic type should usually match.
3916 if (!isBitShift && left->getBasicType() != right->getBasicType())
3917 {
3918 return false;
3919 }
3920
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003921 // Check that:
3922 // 1. Type sizes match exactly on ops that require that.
3923 // 2. Restrictions for structs that contain arrays or samplers are respected.
3924 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003925 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003926 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003927 case EOpAssign:
3928 case EOpInitialize:
3929 case EOpEqual:
3930 case EOpNotEqual:
3931 // ESSL 1.00 sections 5.7, 5.8, 5.9
3932 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
3933 {
3934 error(loc, "undefined operation for structs containing arrays",
3935 GetOperatorString(op));
3936 return false;
3937 }
3938 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3939 // we interpret the spec so that this extends to structs containing samplers,
3940 // similarly to ESSL 1.00 spec.
3941 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
3942 left->getType().isStructureContainingSamplers())
3943 {
3944 error(loc, "undefined operation for structs containing samplers",
3945 GetOperatorString(op));
3946 return false;
3947 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003948
3949 if ((op == EOpAssign || op == EOpInitialize) &&
3950 left->getType().isStructureContainingImages())
3951 {
3952 error(loc, "undefined operation for structs containing images",
3953 GetOperatorString(op));
3954 return false;
3955 }
Olli Etuahoe1805592017-01-02 16:41:20 +00003956 if ((left->getNominalSize() != right->getNominalSize()) ||
3957 (left->getSecondarySize() != right->getSecondarySize()))
3958 {
3959 error(loc, "dimension mismatch", GetOperatorString(op));
3960 return false;
3961 }
3962 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003963 case EOpLessThan:
3964 case EOpGreaterThan:
3965 case EOpLessThanEqual:
3966 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00003967 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04003968 {
Olli Etuahoe1805592017-01-02 16:41:20 +00003969 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003970 return false;
3971 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003972 break;
3973 case EOpAdd:
3974 case EOpSub:
3975 case EOpDiv:
3976 case EOpIMod:
3977 case EOpBitShiftLeft:
3978 case EOpBitShiftRight:
3979 case EOpBitwiseAnd:
3980 case EOpBitwiseXor:
3981 case EOpBitwiseOr:
3982 case EOpAddAssign:
3983 case EOpSubAssign:
3984 case EOpDivAssign:
3985 case EOpIModAssign:
3986 case EOpBitShiftLeftAssign:
3987 case EOpBitShiftRightAssign:
3988 case EOpBitwiseAndAssign:
3989 case EOpBitwiseXorAssign:
3990 case EOpBitwiseOrAssign:
3991 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
3992 {
3993 return false;
3994 }
3995
3996 // Are the sizes compatible?
3997 if (left->getNominalSize() != right->getNominalSize() ||
3998 left->getSecondarySize() != right->getSecondarySize())
3999 {
4000 // If the nominal sizes of operands do not match:
4001 // One of them must be a scalar.
4002 if (!left->isScalar() && !right->isScalar())
4003 return false;
4004
4005 // In the case of compound assignment other than multiply-assign,
4006 // the right side needs to be a scalar. Otherwise a vector/matrix
4007 // would be assigned to a scalar. A scalar can't be shifted by a
4008 // vector either.
4009 if (!right->isScalar() &&
4010 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
4011 return false;
4012 }
4013 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004014 default:
4015 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004016 }
4017
Olli Etuahod6b14282015-03-17 14:31:35 +02004018 return true;
4019}
4020
Olli Etuaho1dded802016-08-18 18:13:13 +03004021bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
4022 const TType &left,
4023 const TType &right)
4024{
4025 switch (op)
4026 {
4027 case EOpMul:
4028 case EOpMulAssign:
4029 return left.getNominalSize() == right.getNominalSize() &&
4030 left.getSecondarySize() == right.getSecondarySize();
4031 case EOpVectorTimesScalar:
4032 return true;
4033 case EOpVectorTimesScalarAssign:
4034 ASSERT(!left.isMatrix() && !right.isMatrix());
4035 return left.isVector() && !right.isVector();
4036 case EOpVectorTimesMatrix:
4037 return left.getNominalSize() == right.getRows();
4038 case EOpVectorTimesMatrixAssign:
4039 ASSERT(!left.isMatrix() && right.isMatrix());
4040 return left.isVector() && left.getNominalSize() == right.getRows() &&
4041 left.getNominalSize() == right.getCols();
4042 case EOpMatrixTimesVector:
4043 return left.getCols() == right.getNominalSize();
4044 case EOpMatrixTimesScalar:
4045 return true;
4046 case EOpMatrixTimesScalarAssign:
4047 ASSERT(left.isMatrix() && !right.isMatrix());
4048 return !right.isVector();
4049 case EOpMatrixTimesMatrix:
4050 return left.getCols() == right.getRows();
4051 case EOpMatrixTimesMatrixAssign:
4052 ASSERT(left.isMatrix() && right.isMatrix());
4053 // We need to check two things:
4054 // 1. The matrix multiplication step is valid.
4055 // 2. The result will have the same number of columns as the lvalue.
4056 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
4057
4058 default:
4059 UNREACHABLE();
4060 return false;
4061 }
4062}
4063
Jamie Madillb98c3a82015-07-23 14:26:04 -04004064TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
4065 TIntermTyped *left,
4066 TIntermTyped *right,
4067 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02004068{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004069 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02004070 return nullptr;
4071
Olli Etuahofc1806e2015-03-17 13:03:11 +02004072 switch (op)
4073 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004074 case EOpEqual:
4075 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04004076 case EOpLessThan:
4077 case EOpGreaterThan:
4078 case EOpLessThanEqual:
4079 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04004080 break;
4081 case EOpLogicalOr:
4082 case EOpLogicalXor:
4083 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03004084 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4085 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00004086 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04004087 {
4088 return nullptr;
4089 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00004090 // Basic types matching should have been already checked.
4091 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004092 break;
4093 case EOpAdd:
4094 case EOpSub:
4095 case EOpDiv:
4096 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03004097 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4098 !right->getType().getStruct());
4099 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04004100 {
4101 return nullptr;
4102 }
4103 break;
4104 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03004105 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4106 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004107 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03004108 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04004109 {
4110 return nullptr;
4111 }
4112 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004113 default:
4114 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02004115 }
4116
Olli Etuaho1dded802016-08-18 18:13:13 +03004117 if (op == EOpMul)
4118 {
4119 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
4120 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4121 {
4122 return nullptr;
4123 }
4124 }
4125
Olli Etuaho3fdec912016-08-18 15:08:06 +03004126 TIntermBinary *node = new TIntermBinary(op, left, right);
4127 node->setLine(loc);
4128
Olli Etuaho3fdec912016-08-18 15:08:06 +03004129 // See if we can fold constants.
Olli Etuaho77ba4082016-12-16 12:01:18 +00004130 TIntermTyped *foldedNode = node->fold(mDiagnostics);
Olli Etuaho3fdec912016-08-18 15:08:06 +03004131 if (foldedNode)
4132 return foldedNode;
4133
4134 return node;
Olli Etuahofc1806e2015-03-17 13:03:11 +02004135}
4136
Jamie Madillb98c3a82015-07-23 14:26:04 -04004137TIntermTyped *TParseContext::addBinaryMath(TOperator op,
4138 TIntermTyped *left,
4139 TIntermTyped *right,
4140 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004141{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004142 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004143 if (node == 0)
4144 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004145 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4146 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02004147 return left;
4148 }
4149 return node;
4150}
4151
Jamie Madillb98c3a82015-07-23 14:26:04 -04004152TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
4153 TIntermTyped *left,
4154 TIntermTyped *right,
4155 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004156{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004157 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004158 if (node == 0)
4159 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004160 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4161 right->getCompleteString());
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004162 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02004163 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004164 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
4165 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004166 }
4167 return node;
4168}
4169
Olli Etuaho13389b62016-10-16 11:48:18 +01004170TIntermBinary *TParseContext::createAssign(TOperator op,
4171 TIntermTyped *left,
4172 TIntermTyped *right,
4173 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004174{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004175 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02004176 {
Olli Etuaho1dded802016-08-18 18:13:13 +03004177 if (op == EOpMulAssign)
4178 {
4179 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
4180 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4181 {
4182 return nullptr;
4183 }
4184 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03004185 TIntermBinary *node = new TIntermBinary(op, left, right);
4186 node->setLine(loc);
4187
Olli Etuaho3fdec912016-08-18 15:08:06 +03004188 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02004189 }
4190 return nullptr;
4191}
4192
Jamie Madillb98c3a82015-07-23 14:26:04 -04004193TIntermTyped *TParseContext::addAssign(TOperator op,
4194 TIntermTyped *left,
4195 TIntermTyped *right,
4196 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004197{
4198 TIntermTyped *node = createAssign(op, left, right, loc);
4199 if (node == nullptr)
4200 {
4201 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02004202 return left;
4203 }
4204 return node;
4205}
4206
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004207TIntermTyped *TParseContext::addComma(TIntermTyped *left,
4208 TIntermTyped *right,
4209 const TSourceLoc &loc)
4210{
Corentin Wallez0d959252016-07-12 17:26:32 -04004211 // WebGL2 section 5.26, the following results in an error:
4212 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004213 if (mShaderSpec == SH_WEBGL2_SPEC &&
4214 (left->isArray() || left->getBasicType() == EbtVoid ||
4215 left->getType().isStructureContainingArrays() || right->isArray() ||
4216 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04004217 {
4218 error(loc,
4219 "sequence operator is not allowed for void, arrays, or structs containing arrays",
4220 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04004221 }
4222
Olli Etuaho4db7ded2016-10-13 12:23:11 +01004223 return TIntermediate::AddComma(left, right, loc, mShaderVersion);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004224}
4225
Olli Etuaho49300862015-02-20 14:54:49 +02004226TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
4227{
4228 switch (op)
4229 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004230 case EOpContinue:
4231 if (mLoopNestingLevel <= 0)
4232 {
4233 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004234 }
4235 break;
4236 case EOpBreak:
4237 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
4238 {
4239 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004240 }
4241 break;
4242 case EOpReturn:
4243 if (mCurrentFunctionType->getBasicType() != EbtVoid)
4244 {
4245 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004246 }
4247 break;
4248 default:
4249 // No checks for discard
4250 break;
Olli Etuaho49300862015-02-20 14:54:49 +02004251 }
4252 return intermediate.addBranch(op, loc);
4253}
4254
Jamie Madillb98c3a82015-07-23 14:26:04 -04004255TIntermBranch *TParseContext::addBranch(TOperator op,
4256 TIntermTyped *returnValue,
4257 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02004258{
4259 ASSERT(op == EOpReturn);
4260 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004261 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02004262 {
4263 error(loc, "void function cannot return a value", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02004264 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004265 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02004266 {
4267 error(loc, "function return is not matching type:", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02004268 }
4269 return intermediate.addBranch(op, returnValue, loc);
4270}
4271
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004272void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
4273{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004274 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01004275 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004276 TIntermNode *offset = nullptr;
4277 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuahoec9232b2017-03-27 17:01:37 +03004278 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
4279 name == "textureProjLodOffset" || name == "textureGradOffset" ||
4280 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004281 {
4282 offset = arguments->back();
4283 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03004284 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004285 {
4286 // A bias parameter might follow the offset parameter.
4287 ASSERT(arguments->size() >= 3);
4288 offset = (*arguments)[2];
4289 }
4290 if (offset != nullptr)
4291 {
4292 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
4293 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
4294 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004295 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03004296 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004297 }
4298 else
4299 {
4300 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
4301 size_t size = offsetConstantUnion->getType().getObjectSize();
4302 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
4303 for (size_t i = 0u; i < size; ++i)
4304 {
4305 int offsetValue = values[i].getIConst();
4306 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
4307 {
4308 std::stringstream tokenStream;
4309 tokenStream << offsetValue;
4310 std::string token = tokenStream.str();
4311 error(offset->getLine(), "Texture offset value out of valid range",
4312 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004313 }
4314 }
4315 }
4316 }
4317}
4318
Martin Radev2cc85b32016-08-05 16:22:53 +03004319// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
4320void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
4321{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004322 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03004323 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
4324
4325 if (name.compare(0, 5, "image") == 0)
4326 {
4327 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00004328 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03004329
Olli Etuaho485eefd2017-02-14 17:40:06 +00004330 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03004331
4332 if (name.compare(5, 5, "Store") == 0)
4333 {
4334 if (memoryQualifier.readonly)
4335 {
4336 error(imageNode->getLine(),
4337 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004338 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03004339 }
4340 }
4341 else if (name.compare(5, 4, "Load") == 0)
4342 {
4343 if (memoryQualifier.writeonly)
4344 {
4345 error(imageNode->getLine(),
4346 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004347 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03004348 }
4349 }
4350 }
4351}
4352
4353// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
4354void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
4355 const TFunction *functionDefinition,
4356 const TIntermAggregate *functionCall)
4357{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004358 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03004359
4360 const TIntermSequence &arguments = *functionCall->getSequence();
4361
4362 ASSERT(functionDefinition->getParamCount() == arguments.size());
4363
4364 for (size_t i = 0; i < arguments.size(); ++i)
4365 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00004366 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
4367 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03004368 const TType &functionParameterType = *functionDefinition->getParam(i).type;
4369 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
4370
4371 if (IsImage(functionArgumentType.getBasicType()))
4372 {
4373 const TMemoryQualifier &functionArgumentMemoryQualifier =
4374 functionArgumentType.getMemoryQualifier();
4375 const TMemoryQualifier &functionParameterMemoryQualifier =
4376 functionParameterType.getMemoryQualifier();
4377 if (functionArgumentMemoryQualifier.readonly &&
4378 !functionParameterMemoryQualifier.readonly)
4379 {
4380 error(functionCall->getLine(),
4381 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004382 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03004383 }
4384
4385 if (functionArgumentMemoryQualifier.writeonly &&
4386 !functionParameterMemoryQualifier.writeonly)
4387 {
4388 error(functionCall->getLine(),
4389 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004390 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03004391 }
Martin Radev049edfa2016-11-11 14:35:37 +02004392
4393 if (functionArgumentMemoryQualifier.coherent &&
4394 !functionParameterMemoryQualifier.coherent)
4395 {
4396 error(functionCall->getLine(),
4397 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004398 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02004399 }
4400
4401 if (functionArgumentMemoryQualifier.volatileQualifier &&
4402 !functionParameterMemoryQualifier.volatileQualifier)
4403 {
4404 error(functionCall->getLine(),
4405 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004406 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02004407 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004408 }
4409 }
4410}
4411
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004412TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004413{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004414 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00004415}
4416
4417TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004418 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00004419 TIntermNode *thisNode,
4420 const TSourceLoc &loc)
4421{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004422 if (thisNode != nullptr)
4423 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004424 return addMethod(fnCall, arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004425 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004426
4427 TOperator op = fnCall->getBuiltInOp();
4428 if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004429 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004430 return addConstructor(arguments, op, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004431 }
4432 else
4433 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004434 return addNonConstructorFunctionCall(fnCall, arguments, loc);
4435 }
4436}
4437
4438TIntermTyped *TParseContext::addMethod(TFunction *fnCall,
4439 TIntermSequence *arguments,
4440 TIntermNode *thisNode,
4441 const TSourceLoc &loc)
4442{
4443 TConstantUnion *unionArray = new TConstantUnion[1];
4444 int arraySize = 0;
4445 TIntermTyped *typedThis = thisNode->getAsTyped();
4446 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
4447 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
4448 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
4449 // So accessing fnCall->getName() below is safe.
4450 if (fnCall->getName() != "length")
4451 {
4452 error(loc, "invalid method", fnCall->getName().c_str());
4453 }
4454 else if (!arguments->empty())
4455 {
4456 error(loc, "method takes no parameters", "length");
4457 }
4458 else if (typedThis == nullptr || !typedThis->isArray())
4459 {
4460 error(loc, "length can only be called on arrays", "length");
4461 }
4462 else
4463 {
4464 arraySize = typedThis->getArraySize();
4465 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuaho72d10202017-01-19 15:58:30 +00004466 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004467 // This code path can be hit with expressions like these:
4468 // (a = b).length()
4469 // (func()).length()
4470 // (int[3](0, 1, 2)).length()
4471 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
4472 // expression.
4473 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
4474 // spec section 5.9 which allows "An array, vector or matrix expression with the
4475 // length method applied".
4476 error(loc, "length can only be called on array names, not on array expressions",
4477 "length");
Olli Etuaho72d10202017-01-19 15:58:30 +00004478 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004479 }
4480 unionArray->setIConst(arraySize);
4481 return intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
4482}
4483
4484TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunction *fnCall,
4485 TIntermSequence *arguments,
4486 const TSourceLoc &loc)
4487{
4488 // First find by unmangled name to check whether the function name has been
4489 // hidden by a variable name or struct typename.
4490 // If a function is found, check for one with a matching argument list.
4491 bool builtIn;
4492 const TSymbol *symbol = symbolTable.find(fnCall->getName(), mShaderVersion, &builtIn);
4493 if (symbol != nullptr && !symbol->isFunction())
4494 {
4495 error(loc, "function name expected", fnCall->getName().c_str());
4496 }
4497 else
4498 {
4499 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(fnCall->getName(), *arguments),
4500 mShaderVersion, &builtIn);
4501 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004502 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004503 error(loc, "no matching overloaded function found", fnCall->getName().c_str());
4504 }
4505 else
4506 {
4507 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004508 //
4509 // A declared function.
4510 //
Olli Etuaho383b7912016-08-05 11:22:59 +03004511 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004512 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004513 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004514 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004515 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004516 if (builtIn && op != EOpNull)
4517 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004518 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004519 if (fnCandidate->getParamCount() == 1)
4520 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004521 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004522 TIntermNode *unaryParamNode = arguments->front();
4523 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004524 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004525 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004526 }
4527 else
4528 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004529 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00004530 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004531 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004532
4533 // Some built-in functions have out parameters too.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004534 functionCallLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05304535
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004536 // See if we can constant fold a built-in. Note that this may be possible even
4537 // if it is not const-qualified.
Olli Etuahof119a262016-08-19 15:54:22 +03004538 TIntermTyped *foldedNode =
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004539 intermediate.foldAggregateBuiltIn(callNode, mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05304540 if (foldedNode)
4541 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004542 return foldedNode;
Arun Patole274f0702015-05-05 13:33:30 +05304543 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004544 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004545 }
4546 }
4547 else
4548 {
4549 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004550 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004551
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004552 // If builtIn == false, the function is user defined - could be an overloaded
4553 // built-in as well.
4554 // if builtIn == true, it's a builtIn function with no op associated with it.
4555 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004556 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004557 {
Olli Etuahofe486322017-03-21 09:30:54 +00004558 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004559 checkTextureOffsetConst(callNode);
4560 checkImageMemoryAccessForBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03004561 }
4562 else
4563 {
Olli Etuahofe486322017-03-21 09:30:54 +00004564 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004565 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004566 }
4567
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004568 functionCallLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004569
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004570 callNode->setLine(loc);
4571
4572 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004573 }
4574 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004575 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004576
4577 // Error message was already written. Put on a dummy node for error recovery.
4578 return TIntermTyped::CreateZero(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004579}
4580
Jamie Madillb98c3a82015-07-23 14:26:04 -04004581TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004582 TIntermTyped *trueExpression,
4583 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03004584 const TSourceLoc &loc)
4585{
Olli Etuaho856c4972016-08-08 11:38:39 +03004586 checkIsScalarBool(loc, cond);
Olli Etuaho52901742015-04-15 13:42:45 +03004587
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004588 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03004589 {
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004590 binaryOpError(loc, ":", trueExpression->getCompleteString(),
4591 falseExpression->getCompleteString());
4592 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03004593 }
Olli Etuahode318b22016-10-25 16:18:25 +01004594 if (IsOpaqueType(trueExpression->getBasicType()))
4595 {
4596 // ESSL 1.00 section 4.1.7
4597 // ESSL 3.00 section 4.1.7
4598 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
4599 // Note that structs containing opaque types don't need to be checked as structs are
4600 // forbidden below.
4601 error(loc, "ternary operator is not allowed for opaque types", ":");
4602 return falseExpression;
4603 }
4604
Olli Etuahoa2d53032015-04-15 14:14:44 +03004605 // ESSL1 sections 5.2 and 5.7:
4606 // ESSL3 section 5.7:
4607 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004608 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03004609 {
4610 error(loc, "ternary operator is not allowed for structures or arrays", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004611 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03004612 }
Corentin Wallez0d959252016-07-12 17:26:32 -04004613 // WebGL2 section 5.26, the following results in an error:
4614 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004615 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04004616 {
4617 error(loc, "ternary operator is not allowed for void", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004618 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04004619 }
4620
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004621 return TIntermediate::AddTernarySelection(cond, trueExpression, falseExpression, loc);
Olli Etuaho52901742015-04-15 13:42:45 +03004622}
Olli Etuaho49300862015-02-20 14:54:49 +02004623
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004624//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004625// Parse an array of strings using yyparse.
4626//
4627// Returns 0 for success.
4628//
Jamie Madillb98c3a82015-07-23 14:26:04 -04004629int PaParseStrings(size_t count,
4630 const char *const string[],
4631 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05304632 TParseContext *context)
4633{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004634 if ((count == 0) || (string == NULL))
4635 return 1;
4636
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004637 if (glslang_initialize(context))
4638 return 1;
4639
alokp@chromium.org408c45e2012-04-05 15:54:43 +00004640 int error = glslang_scan(count, string, length, context);
4641 if (!error)
4642 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004643
alokp@chromium.org73bc2982012-06-19 18:48:05 +00004644 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00004645
alokp@chromium.org6b495712012-06-29 00:06:58 +00004646 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004647}
Jamie Madill45bcc782016-11-07 13:58:48 -05004648
4649} // namespace sh