blob: bdd68f3c4aeb93f3204e88723bf54e7300a9400e [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 Etuaho4de340a2016-12-16 09:32:03 +00001446 TString unmangledName =
1447 TFunction::unmangleName(fnCall->getFunctionSymbolInfo()->getName());
Olli Etuaho856c4972016-08-08 11:38:39 +03001448 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001449 "Constant value cannot be passed for 'out' or 'inout' parameters.",
1450 unmangledName.c_str());
Olli Etuaho383b7912016-08-05 11:22:59 +03001451 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001452 }
1453 }
1454 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001455}
1456
Martin Radev70866b82016-07-22 15:27:42 +03001457void TParseContext::checkInvariantVariableQualifier(bool invariant,
1458 const TQualifier qualifier,
1459 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001460{
Martin Radev70866b82016-07-22 15:27:42 +03001461 if (!invariant)
1462 return;
1463
1464 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001465 {
Martin Radev70866b82016-07-22 15:27:42 +03001466 // input variables in the fragment shader can be also qualified as invariant
1467 if (!sh::CanBeInvariantESSL1(qualifier))
1468 {
1469 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1470 }
1471 }
1472 else
1473 {
1474 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1475 {
1476 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1477 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001478 }
1479}
1480
Arun Patole7e7e68d2015-05-22 12:02:25 +05301481bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001482{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001483 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001484 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1485 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001486}
1487
Arun Patole7e7e68d2015-05-22 12:02:25 +05301488bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001489{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001490 return ::IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001491}
1492
Jamie Madillb98c3a82015-07-23 14:26:04 -04001493void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1494 const char *extName,
1495 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001496{
1497 pp::SourceLocation srcLoc;
1498 srcLoc.file = loc.first_file;
1499 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001500 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001501}
1502
Jamie Madillb98c3a82015-07-23 14:26:04 -04001503void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1504 const char *name,
1505 const char *value,
1506 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001507{
1508 pp::SourceLocation srcLoc;
1509 srcLoc.file = loc.first_file;
1510 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001511 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001512}
1513
Martin Radev4c4c8e72016-08-04 12:25:34 +03001514sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001515{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001516 sh::WorkGroupSize result;
Martin Radev802abe02016-08-04 17:48:32 +03001517 for (size_t i = 0u; i < result.size(); ++i)
1518 {
1519 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1520 {
1521 result[i] = 1;
1522 }
1523 else
1524 {
1525 result[i] = mComputeShaderLocalSize[i];
1526 }
1527 }
1528 return result;
1529}
1530
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001531/////////////////////////////////////////////////////////////////////////////////
1532//
1533// Non-Errors.
1534//
1535/////////////////////////////////////////////////////////////////////////////////
1536
Jamie Madill5c097022014-08-20 16:38:32 -04001537const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1538 const TString *name,
1539 const TSymbol *symbol)
1540{
1541 const TVariable *variable = NULL;
1542
1543 if (!symbol)
1544 {
1545 error(location, "undeclared identifier", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001546 }
1547 else if (!symbol->isVariable())
1548 {
1549 error(location, "variable expected", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001550 }
1551 else
1552 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001553 variable = static_cast<const TVariable *>(symbol);
Jamie Madill5c097022014-08-20 16:38:32 -04001554
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001555 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
Olli Etuaho383b7912016-08-05 11:22:59 +03001556 !variable->getExtension().empty())
Jamie Madill5c097022014-08-20 16:38:32 -04001557 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001558 checkCanUseExtension(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001559 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001560
1561 // Reject shaders using both gl_FragData and gl_FragColor
1562 TQualifier qualifier = variable->getType().getQualifier();
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001563 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001564 {
1565 mUsesFragData = true;
1566 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001567 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001568 {
1569 mUsesFragColor = true;
1570 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001571 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1572 {
1573 mUsesSecondaryOutputs = true;
1574 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001575
1576 // This validation is not quite correct - it's only an error to write to
1577 // both FragData and FragColor. For simplicity, and because users shouldn't
1578 // be rewarded for reading from undefined varaibles, return an error
1579 // if they are both referenced, rather than assigned.
1580 if (mUsesFragData && mUsesFragColor)
1581 {
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001582 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1583 if (mUsesSecondaryOutputs)
1584 {
1585 errorMessage =
1586 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1587 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1588 }
1589 error(location, errorMessage, name->c_str());
Jamie Madill14e95b32015-05-07 10:10:41 -04001590 }
Martin Radevb0883602016-08-04 17:48:58 +03001591
1592 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1593 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1594 qualifier == EvqWorkGroupSize)
1595 {
1596 error(location,
1597 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1598 "gl_WorkGroupSize");
1599 }
Jamie Madill5c097022014-08-20 16:38:32 -04001600 }
1601
1602 if (!variable)
1603 {
1604 TType type(EbtFloat, EbpUndefined);
1605 TVariable *fakeVariable = new TVariable(name, type);
1606 symbolTable.declare(fakeVariable);
1607 variable = fakeVariable;
1608 }
1609
1610 return variable;
1611}
1612
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001613TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1614 const TString *name,
1615 const TSymbol *symbol)
1616{
1617 const TVariable *variable = getNamedVariable(location, name, symbol);
1618
Olli Etuaho09b04a22016-12-15 13:30:26 +00001619 if (variable->getType().getQualifier() == EvqViewIDOVR && IsWebGLBasedSpec(mShaderSpec) &&
1620 mShaderType == GL_FRAGMENT_SHADER && !isExtensionEnabled("GL_OVR_multiview2"))
1621 {
1622 // WEBGL_multiview spec
1623 error(location, "Need to enable OVR_multiview2 to use gl_ViewID_OVR in fragment shader",
1624 "gl_ViewID_OVR");
1625 }
1626
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001627 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001628 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001629 const TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001630 return intermediate.addConstantUnion(constArray, variable->getType(), location);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001631 }
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001632 else if (variable->getType().getQualifier() == EvqWorkGroupSize &&
1633 mComputeShaderLocalSizeDeclared)
1634 {
1635 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1636 // needs to be added to the AST as a constant and not as a symbol.
1637 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1638 TConstantUnion *constArray = new TConstantUnion[3];
1639 for (size_t i = 0; i < 3; ++i)
1640 {
1641 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1642 }
1643
1644 ASSERT(variable->getType().getBasicType() == EbtUInt);
1645 ASSERT(variable->getType().getObjectSize() == 3);
1646
1647 TType type(variable->getType());
1648 type.setQualifier(EvqConst);
1649 return intermediate.addConstantUnion(constArray, type, location);
1650 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001651 else
1652 {
1653 return intermediate.addSymbol(variable->getUniqueId(), variable->getName(),
1654 variable->getType(), location);
1655 }
1656}
1657
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001658//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001659// Initializers show up in several places in the grammar. Have one set of
1660// code to handle them here.
1661//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001662// Returns true on error, false if no error
1663//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001664bool TParseContext::executeInitializer(const TSourceLoc &line,
1665 const TString &identifier,
1666 const TPublicType &pType,
1667 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001668 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001669{
Olli Etuaho13389b62016-10-16 11:48:18 +01001670 ASSERT(initNode != nullptr);
1671 ASSERT(*initNode == nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001672 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001673
Olli Etuaho2935c582015-04-08 14:32:06 +03001674 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001675 if (type.isUnsizedArray())
1676 {
Olli Etuaho02bd82c2016-11-03 10:29:43 +00001677 // We have not checked yet whether the initializer actually is an array or not.
1678 if (initializer->isArray())
1679 {
1680 type.setArraySize(initializer->getArraySize());
1681 }
1682 else
1683 {
1684 // Having a non-array initializer for an unsized array will result in an error later,
1685 // so we don't generate an error message here.
1686 type.setArraySize(1u);
1687 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001688 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001689 if (!declareVariable(line, identifier, type, &variable))
1690 {
1691 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001692 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001693
Olli Etuahob0c645e2015-05-12 14:25:36 +03001694 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001695 if (symbolTable.atGlobalLevel() &&
1696 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001697 {
1698 // Error message does not completely match behavior with ESSL 1.00, but
1699 // we want to steer developers towards only using constant expressions.
1700 error(line, "global variable initializers must be constant expressions", "=");
1701 return true;
1702 }
1703 if (globalInitWarning)
1704 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001705 warning(
1706 line,
1707 "global variable initializers should be constant expressions "
1708 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1709 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001710 }
1711
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001712 //
1713 // identifier must be of type constant, a global, or a temporary
1714 //
1715 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301716 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1717 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001718 error(line, " cannot initialize this type of qualifier ",
1719 variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001720 return true;
1721 }
1722 //
1723 // test for and propagate constant
1724 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001725
Arun Patole7e7e68d2015-05-22 12:02:25 +05301726 if (qualifier == EvqConst)
1727 {
1728 if (qualifier != initializer->getType().getQualifier())
1729 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001730 std::stringstream reasonStream;
1731 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1732 << "'";
1733 std::string reason = reasonStream.str();
1734 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001735 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001736 return true;
1737 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301738 if (type != initializer->getType())
1739 {
1740 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001741 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001742 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001743 return true;
1744 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001745
1746 // Save the constant folded value to the variable if possible. For example array
1747 // initializers are not folded, since that way copying the array literal to multiple places
1748 // in the shader is avoided.
1749 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1750 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301751 if (initializer->getAsConstantUnion())
1752 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001753 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho13389b62016-10-16 11:48:18 +01001754 *initNode = nullptr;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001755 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301756 }
1757 else if (initializer->getAsSymbolNode())
1758 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001759 const TSymbol *symbol =
1760 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1761 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001762
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001763 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001764 if (constArray)
1765 {
1766 variable->shareConstPointer(constArray);
Olli Etuaho13389b62016-10-16 11:48:18 +01001767 *initNode = nullptr;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001768 return false;
1769 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001770 }
1771 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001772
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001773 TIntermSymbol *intermSymbol = intermediate.addSymbol(
1774 variable->getUniqueId(), variable->getName(), variable->getType(), line);
Olli Etuaho13389b62016-10-16 11:48:18 +01001775 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1776 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001777 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001778 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1779 return true;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001780 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001781
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001782 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001783}
1784
Olli Etuaho0e3aee32016-10-27 12:56:38 +01001785void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
1786{
1787 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
1788 typeSpecifier->getBasicType());
1789
1790 if (mShaderVersion < 300 && typeSpecifier->array)
1791 {
1792 error(typeSpecifier->getLine(), "not supported", "first-class array");
1793 typeSpecifier->clearArrayness();
1794 }
1795}
1796
Martin Radev70866b82016-07-22 15:27:42 +03001797TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301798 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001799{
Olli Etuaho77ba4082016-12-16 12:01:18 +00001800 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001801
Martin Radev70866b82016-07-22 15:27:42 +03001802 TPublicType returnType = typeSpecifier;
1803 returnType.qualifier = typeQualifier.qualifier;
1804 returnType.invariant = typeQualifier.invariant;
1805 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03001806 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03001807 returnType.precision = typeSpecifier.precision;
1808
1809 if (typeQualifier.precision != EbpUndefined)
1810 {
1811 returnType.precision = typeQualifier.precision;
1812 }
1813
Martin Radev4a9cd802016-09-01 16:51:51 +03001814 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
1815 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03001816
Martin Radev4a9cd802016-09-01 16:51:51 +03001817 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
1818 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03001819
Martin Radev4a9cd802016-09-01 16:51:51 +03001820 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03001821
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001822 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001823 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001824 if (typeSpecifier.array)
1825 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001826 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001827 returnType.clearArrayness();
1828 }
1829
Martin Radev70866b82016-07-22 15:27:42 +03001830 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001831 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001832 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001833 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001834 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001835 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001836
Martin Radev70866b82016-07-22 15:27:42 +03001837 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001838 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001839 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001840 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001841 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001842 }
1843 }
1844 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001845 {
Martin Radev70866b82016-07-22 15:27:42 +03001846 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03001847 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001848 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03001849 }
Martin Radev70866b82016-07-22 15:27:42 +03001850 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
1851 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001852 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001853 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
1854 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001855 }
Martin Radev70866b82016-07-22 15:27:42 +03001856 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03001857 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001858 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03001859 "in");
Martin Radev802abe02016-08-04 17:48:32 +03001860 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001861 }
1862
1863 return returnType;
1864}
1865
Olli Etuaho856c4972016-08-08 11:38:39 +03001866void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
1867 const TPublicType &type,
1868 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03001869{
1870 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03001871 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03001872 {
1873 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001874 }
1875
1876 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
1877 switch (qualifier)
1878 {
1879 case EvqVertexIn:
1880 // ESSL 3.00 section 4.3.4
1881 if (type.array)
1882 {
1883 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001884 }
1885 // Vertex inputs with a struct type are disallowed in singleDeclarationErrorCheck
1886 return;
1887 case EvqFragmentOut:
1888 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03001889 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03001890 {
1891 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001892 }
1893 // Fragment outputs with a struct type are disallowed in singleDeclarationErrorCheck
1894 return;
1895 default:
1896 break;
1897 }
1898
1899 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
1900 // restrictions.
1901 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03001902 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
1903 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03001904 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
1905 {
1906 error(qualifierLocation, "must use 'flat' interpolation here",
1907 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001908 }
1909
Martin Radev4a9cd802016-09-01 16:51:51 +03001910 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03001911 {
1912 // ESSL 3.00 sections 4.3.4 and 4.3.6.
1913 // These restrictions are only implied by the ESSL 3.00 spec, but
1914 // the ESSL 3.10 spec lists these restrictions explicitly.
1915 if (type.array)
1916 {
1917 error(qualifierLocation, "cannot be an array of structures",
1918 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001919 }
1920 if (type.isStructureContainingArrays())
1921 {
1922 error(qualifierLocation, "cannot be a structure containing an array",
1923 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001924 }
1925 if (type.isStructureContainingType(EbtStruct))
1926 {
1927 error(qualifierLocation, "cannot be a structure containing a structure",
1928 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001929 }
1930 if (type.isStructureContainingType(EbtBool))
1931 {
1932 error(qualifierLocation, "cannot be a structure containing a bool",
1933 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001934 }
1935 }
1936}
1937
Martin Radev2cc85b32016-08-05 16:22:53 +03001938void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
1939{
1940 if (qualifier.getType() == QtStorage)
1941 {
1942 const TStorageQualifierWrapper &storageQualifier =
1943 static_cast<const TStorageQualifierWrapper &>(qualifier);
1944 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
1945 !symbolTable.atGlobalLevel())
1946 {
1947 error(storageQualifier.getLine(),
1948 "Local variables can only use the const storage qualifier.",
1949 storageQualifier.getQualifierString().c_str());
1950 }
1951 }
1952}
1953
Olli Etuaho43364892017-02-13 16:00:12 +00001954void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03001955 const TSourceLoc &location)
1956{
1957 if (memoryQualifier.readonly)
1958 {
1959 error(location, "Only allowed with images.", "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03001960 }
1961 if (memoryQualifier.writeonly)
1962 {
1963 error(location, "Only allowed with images.", "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03001964 }
Martin Radev049edfa2016-11-11 14:35:37 +02001965 if (memoryQualifier.coherent)
1966 {
1967 error(location, "Only allowed with images.", "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02001968 }
1969 if (memoryQualifier.restrictQualifier)
1970 {
1971 error(location, "Only allowed with images.", "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02001972 }
1973 if (memoryQualifier.volatileQualifier)
1974 {
1975 error(location, "Only allowed with images.", "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02001976 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001977}
1978
Olli Etuaho13389b62016-10-16 11:48:18 +01001979TIntermDeclaration *TParseContext::parseSingleDeclaration(
1980 TPublicType &publicType,
1981 const TSourceLoc &identifierOrTypeLocation,
1982 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001983{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001984 TType type(publicType);
1985 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
1986 mDirectiveHandler.pragma().stdgl.invariantAll)
1987 {
1988 TQualifier qualifier = type.getQualifier();
1989
1990 // The directive handler has already taken care of rejecting invalid uses of this pragma
1991 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
1992 // affected variable declarations:
1993 //
1994 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
1995 // elsewhere, in TranslatorGLSL.)
1996 //
1997 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
1998 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
1999 // the way this is currently implemented we have to enable this compiler option before
2000 // parsing the shader and determining the shading language version it uses. If this were
2001 // implemented as a post-pass, the workaround could be more targeted.
2002 //
2003 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2004 // the specification, but there are desktop OpenGL drivers that expect that this is the
2005 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2006 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2007 {
2008 type.setInvariant(true);
2009 }
2010 }
2011
2012 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, type, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002013
Olli Etuahobab4c082015-04-24 16:38:49 +03002014 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03002015
Olli Etuahobab4c082015-04-24 16:38:49 +03002016 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
2017
Olli Etuaho13389b62016-10-16 11:48:18 +01002018 TIntermDeclaration *declaration = new TIntermDeclaration();
2019 declaration->setLine(identifierOrTypeLocation);
2020
Olli Etuahobab4c082015-04-24 16:38:49 +03002021 if (emptyDeclaration)
2022 {
Martin Radevb8b01222016-11-20 23:25:53 +02002023 emptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Olli Etuahobab4c082015-04-24 16:38:49 +03002024 }
2025 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002026 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002027 singleDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002028
Olli Etuaho856c4972016-08-08 11:38:39 +03002029 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002030
Olli Etuaho2935c582015-04-08 14:32:06 +03002031 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002032 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002033
2034 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01002035 {
Jamie Madill60ed9812013-06-06 11:56:46 -04002036 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01002037 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002038 }
2039
Olli Etuaho13389b62016-10-16 11:48:18 +01002040 // We append the symbol even if the declaration is empty, mainly because of struct declarations
2041 // that may just declare a type.
2042 declaration->appendDeclarator(symbol);
2043
2044 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002045}
2046
Olli Etuaho13389b62016-10-16 11:48:18 +01002047TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
2048 const TSourceLoc &identifierLocation,
2049 const TString &identifier,
2050 const TSourceLoc &indexLocation,
2051 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04002052{
Olli Etuahofa33d582015-04-09 14:33:12 +03002053 mDeferredSingleDeclarationErrorCheck = false;
2054
Olli Etuaho383b7912016-08-05 11:22:59 +03002055 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002056
Olli Etuaho856c4972016-08-08 11:38:39 +03002057 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002058
Olli Etuaho8a176262016-08-16 14:23:01 +03002059 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002060
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002061 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002062
Olli Etuaho856c4972016-08-08 11:38:39 +03002063 unsigned int size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002064 // Make the type an array even if size check failed.
2065 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2066 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04002067
Olli Etuaho2935c582015-04-08 14:32:06 +03002068 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002069 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002070
Olli Etuaho13389b62016-10-16 11:48:18 +01002071 TIntermDeclaration *declaration = new TIntermDeclaration();
2072 declaration->setLine(identifierLocation);
2073
Olli Etuahoe7847b02015-03-16 11:56:12 +02002074 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002075 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01002076 {
Jamie Madill60ed9812013-06-06 11:56:46 -04002077 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01002078 declaration->appendDeclarator(symbol);
2079 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002080
Olli Etuaho13389b62016-10-16 11:48:18 +01002081 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002082}
2083
Olli Etuaho13389b62016-10-16 11:48:18 +01002084TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2085 const TSourceLoc &identifierLocation,
2086 const TString &identifier,
2087 const TSourceLoc &initLocation,
2088 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002089{
Olli Etuahofa33d582015-04-09 14:33:12 +03002090 mDeferredSingleDeclarationErrorCheck = false;
2091
Olli Etuaho383b7912016-08-05 11:22:59 +03002092 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002093
Olli Etuaho13389b62016-10-16 11:48:18 +01002094 TIntermDeclaration *declaration = new TIntermDeclaration();
2095 declaration->setLine(identifierLocation);
2096
2097 TIntermBinary *initNode = nullptr;
2098 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002099 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002100 if (initNode)
2101 {
2102 declaration->appendDeclarator(initNode);
2103 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002104 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002105 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002106}
2107
Olli Etuaho13389b62016-10-16 11:48:18 +01002108TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Jamie Madillb98c3a82015-07-23 14:26:04 -04002109 TPublicType &publicType,
2110 const TSourceLoc &identifierLocation,
2111 const TString &identifier,
2112 const TSourceLoc &indexLocation,
2113 TIntermTyped *indexExpression,
2114 const TSourceLoc &initLocation,
2115 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002116{
2117 mDeferredSingleDeclarationErrorCheck = false;
2118
Olli Etuaho383b7912016-08-05 11:22:59 +03002119 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002120
Olli Etuaho8a176262016-08-16 14:23:01 +03002121 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002122
2123 TPublicType arrayType(publicType);
2124
Olli Etuaho856c4972016-08-08 11:38:39 +03002125 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002126 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2127 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002128 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002129 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002130 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002131 }
2132 // Make the type an array even if size check failed.
2133 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2134 arrayType.setArraySize(size);
2135
Olli Etuaho13389b62016-10-16 11:48:18 +01002136 TIntermDeclaration *declaration = new TIntermDeclaration();
2137 declaration->setLine(identifierLocation);
2138
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002139 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002140 TIntermBinary *initNode = nullptr;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002141 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
2142 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002143 if (initNode)
2144 {
2145 declaration->appendDeclarator(initNode);
2146 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002147 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002148
2149 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002150}
2151
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002152TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002153 const TTypeQualifierBuilder &typeQualifierBuilder,
2154 const TSourceLoc &identifierLoc,
2155 const TString *identifier,
2156 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002157{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002158 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002159
Martin Radev70866b82016-07-22 15:27:42 +03002160 if (!typeQualifier.invariant)
2161 {
2162 error(identifierLoc, "Expected invariant", identifier->c_str());
2163 return nullptr;
2164 }
2165 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2166 {
2167 return nullptr;
2168 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002169 if (!symbol)
2170 {
2171 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002172 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002173 }
Martin Radev70866b82016-07-22 15:27:42 +03002174 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002175 {
Martin Radev70866b82016-07-22 15:27:42 +03002176 error(identifierLoc, "invariant declaration specifies qualifier",
2177 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002178 }
Martin Radev70866b82016-07-22 15:27:42 +03002179 if (typeQualifier.precision != EbpUndefined)
2180 {
2181 error(identifierLoc, "invariant declaration specifies precision",
2182 getPrecisionString(typeQualifier.precision));
2183 }
2184 if (!typeQualifier.layoutQualifier.isEmpty())
2185 {
2186 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2187 }
2188
2189 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
2190 ASSERT(variable);
2191 const TType &type = variable->getType();
2192
2193 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2194 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002195 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002196
2197 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2198
2199 TIntermSymbol *intermSymbol =
2200 intermediate.addSymbol(variable->getUniqueId(), *identifier, type, identifierLoc);
2201
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002202 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002203}
2204
Olli Etuaho13389b62016-10-16 11:48:18 +01002205void TParseContext::parseDeclarator(TPublicType &publicType,
2206 const TSourceLoc &identifierLocation,
2207 const TString &identifier,
2208 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002209{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002210 // If the declaration starting this declarator list was empty (example: int,), some checks were
2211 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002212 if (mDeferredSingleDeclarationErrorCheck)
2213 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002214 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002215 mDeferredSingleDeclarationErrorCheck = false;
2216 }
2217
Olli Etuaho856c4972016-08-08 11:38:39 +03002218 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002219
Olli Etuaho856c4972016-08-08 11:38:39 +03002220 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002221
Olli Etuaho2935c582015-04-08 14:32:06 +03002222 TVariable *variable = nullptr;
Olli Etuaho43364892017-02-13 16:00:12 +00002223 TType type(publicType);
2224 declareVariable(identifierLocation, identifier, type, &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002225
Olli Etuaho43364892017-02-13 16:00:12 +00002226 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, type, identifierLocation);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002227 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01002228 {
Jamie Madill502d66f2013-06-20 11:55:52 -04002229 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01002230 declarationOut->appendDeclarator(symbol);
2231 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002232}
2233
Olli Etuaho13389b62016-10-16 11:48:18 +01002234void TParseContext::parseArrayDeclarator(TPublicType &publicType,
2235 const TSourceLoc &identifierLocation,
2236 const TString &identifier,
2237 const TSourceLoc &arrayLocation,
2238 TIntermTyped *indexExpression,
2239 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002240{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002241 // If the declaration starting this declarator list was empty (example: int,), some checks were
2242 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002243 if (mDeferredSingleDeclarationErrorCheck)
2244 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002245 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002246 mDeferredSingleDeclarationErrorCheck = false;
2247 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002248
Olli Etuaho856c4972016-08-08 11:38:39 +03002249 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002250
Olli Etuaho856c4972016-08-08 11:38:39 +03002251 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002252
Olli Etuaho8a176262016-08-16 14:23:01 +03002253 if (checkIsValidTypeAndQualifierForArray(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002254 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002255 TType arrayType = TType(publicType);
Olli Etuaho856c4972016-08-08 11:38:39 +03002256 unsigned int size = checkIsValidArraySize(arrayLocation, indexExpression);
Olli Etuaho693c9aa2015-04-07 17:50:36 +03002257 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002258
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002259 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002260 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002261
Jamie Madillb98c3a82015-07-23 14:26:04 -04002262 TIntermSymbol *symbol =
2263 intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002264 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002265 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002266
Olli Etuaho13389b62016-10-16 11:48:18 +01002267 declarationOut->appendDeclarator(symbol);
Jamie Madill502d66f2013-06-20 11:55:52 -04002268 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002269}
2270
Olli Etuaho13389b62016-10-16 11:48:18 +01002271void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2272 const TSourceLoc &identifierLocation,
2273 const TString &identifier,
2274 const TSourceLoc &initLocation,
2275 TIntermTyped *initializer,
2276 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002277{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002278 // If the declaration starting this declarator list was empty (example: int,), some checks were
2279 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002280 if (mDeferredSingleDeclarationErrorCheck)
2281 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002282 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002283 mDeferredSingleDeclarationErrorCheck = false;
2284 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002285
Olli Etuaho856c4972016-08-08 11:38:39 +03002286 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002287
Olli Etuaho13389b62016-10-16 11:48:18 +01002288 TIntermBinary *initNode = nullptr;
2289 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002290 {
2291 //
2292 // build the intermediate representation
2293 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002294 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002295 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002296 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002297 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002298 }
2299}
2300
Olli Etuaho13389b62016-10-16 11:48:18 +01002301void TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
2302 const TSourceLoc &identifierLocation,
2303 const TString &identifier,
2304 const TSourceLoc &indexLocation,
2305 TIntermTyped *indexExpression,
2306 const TSourceLoc &initLocation,
2307 TIntermTyped *initializer,
2308 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002309{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002310 // If the declaration starting this declarator list was empty (example: int,), some checks were
2311 // not performed.
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002312 if (mDeferredSingleDeclarationErrorCheck)
2313 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002314 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002315 mDeferredSingleDeclarationErrorCheck = false;
2316 }
2317
Olli Etuaho856c4972016-08-08 11:38:39 +03002318 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002319
Olli Etuaho8a176262016-08-16 14:23:01 +03002320 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002321
2322 TPublicType arrayType(publicType);
2323
Olli Etuaho856c4972016-08-08 11:38:39 +03002324 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002325 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2326 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002327 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002328 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002329 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002330 }
2331 // Make the type an array even if size check failed.
2332 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2333 arrayType.setArraySize(size);
2334
2335 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002336 TIntermBinary *initNode = nullptr;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002337 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
2338 {
2339 if (initNode)
2340 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002341 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002342 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002343 }
2344}
2345
Martin Radev70866b82016-07-22 15:27:42 +03002346void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002347{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002348 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002349 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002350
Martin Radev70866b82016-07-22 15:27:42 +03002351 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2352 typeQualifier.line);
2353
Jamie Madillc2128ff2016-07-04 10:26:17 -04002354 // It should never be the case, but some strange parser errors can send us here.
2355 if (layoutQualifier.isEmpty())
2356 {
2357 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002358 return;
2359 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002360
Martin Radev802abe02016-08-04 17:48:32 +03002361 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002362 {
Olli Etuaho43364892017-02-13 16:00:12 +00002363 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002364 return;
2365 }
2366
Olli Etuaho43364892017-02-13 16:00:12 +00002367 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2368
2369 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002370
2371 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2372
Andrei Volykhina5527072017-03-22 16:46:30 +03002373 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
2374
Martin Radev802abe02016-08-04 17:48:32 +03002375 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002376 {
Martin Radev802abe02016-08-04 17:48:32 +03002377 if (mComputeShaderLocalSizeDeclared &&
2378 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2379 {
2380 error(typeQualifier.line, "Work group size does not match the previous declaration",
2381 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002382 return;
2383 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002384
Martin Radev802abe02016-08-04 17:48:32 +03002385 if (mShaderVersion < 310)
2386 {
2387 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002388 return;
2389 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002390
Martin Radev4c4c8e72016-08-04 12:25:34 +03002391 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03002392 {
2393 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002394 return;
2395 }
2396
2397 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
2398 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
2399
2400 const TConstantUnion *maxComputeWorkGroupSizeData =
2401 maxComputeWorkGroupSize->getConstPointer();
2402
2403 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
2404 {
2405 if (layoutQualifier.localSize[i] != -1)
2406 {
2407 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
2408 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
2409 if (mComputeShaderLocalSize[i] < 1 ||
2410 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
2411 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002412 std::stringstream reasonStream;
2413 reasonStream << "invalid value: Value must be at least 1 and no greater than "
2414 << maxComputeWorkGroupSizeValue;
2415 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03002416
Olli Etuaho4de340a2016-12-16 09:32:03 +00002417 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03002418 return;
2419 }
2420 }
2421 }
2422
2423 mComputeShaderLocalSizeDeclared = true;
2424 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00002425 else if (mMultiviewAvailable &&
2426 (isExtensionEnabled("GL_OVR_multiview") || isExtensionEnabled("GL_OVR_multiview2")) &&
2427 typeQualifier.qualifier == EvqVertexIn)
2428 {
2429 // This error is only specified in WebGL, but tightens unspecified behavior in the native
2430 // specification.
2431 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
2432 {
2433 error(typeQualifier.line, "Number of views does not match the previous declaration",
2434 "layout");
2435 return;
2436 }
2437
2438 if (layoutQualifier.numViews == -1)
2439 {
2440 error(typeQualifier.line, "No num_views specified", "layout");
2441 return;
2442 }
2443
2444 if (layoutQualifier.numViews > mMaxNumViews)
2445 {
2446 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
2447 "layout");
2448 return;
2449 }
2450
2451 mNumViews = layoutQualifier.numViews;
2452 }
Martin Radev802abe02016-08-04 17:48:32 +03002453 else
Jamie Madill1566ef72013-06-20 11:55:54 -04002454 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00002455 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03002456 {
Martin Radev802abe02016-08-04 17:48:32 +03002457 return;
2458 }
2459
2460 if (typeQualifier.qualifier != EvqUniform)
2461 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002462 error(typeQualifier.line, "invalid qualifier: global layout must be uniform",
2463 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03002464 return;
2465 }
2466
2467 if (mShaderVersion < 300)
2468 {
2469 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
2470 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002471 return;
2472 }
2473
Olli Etuaho09b04a22016-12-15 13:30:26 +00002474 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002475
2476 if (layoutQualifier.matrixPacking != EmpUnspecified)
2477 {
2478 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
2479 }
2480
2481 if (layoutQualifier.blockStorage != EbsUnspecified)
2482 {
2483 mDefaultBlockStorage = layoutQualifier.blockStorage;
2484 }
Jamie Madill1566ef72013-06-20 11:55:54 -04002485 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002486}
2487
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002488TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
2489 const TFunction &function,
2490 const TSourceLoc &location,
2491 bool insertParametersToSymbolTable)
2492{
Olli Etuahofe486322017-03-21 09:30:54 +00002493 TIntermFunctionPrototype *prototype =
2494 new TIntermFunctionPrototype(function.getReturnType(), TSymbolUniqueId(function));
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002495 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
2496 // point to the data that already exists in the symbol table.
2497 prototype->getFunctionSymbolInfo()->setFromFunction(function);
2498 prototype->setLine(location);
2499
2500 for (size_t i = 0; i < function.getParamCount(); i++)
2501 {
2502 const TConstParameter &param = function.getParam(i);
2503
2504 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
2505 // be used for unused args).
2506 if (param.name != nullptr)
2507 {
2508 TVariable *variable = new TVariable(param.name, *param.type);
2509
2510 // Insert the parameter in the symbol table.
2511 if (insertParametersToSymbolTable && !symbolTable.declare(variable))
2512 {
2513 error(location, "redefinition", variable->getName().c_str());
2514 prototype->appendParameter(intermediate.addSymbol(0, "", *param.type, location));
2515 continue;
2516 }
2517 TIntermSymbol *symbol = intermediate.addSymbol(
2518 variable->getUniqueId(), variable->getName(), variable->getType(), location);
2519 prototype->appendParameter(symbol);
2520 }
2521 else
2522 {
2523 prototype->appendParameter(intermediate.addSymbol(0, "", *param.type, location));
2524 }
2525 }
2526 return prototype;
2527}
2528
Olli Etuaho16c745a2017-01-16 17:02:27 +00002529TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
2530 const TFunction &parsedFunction,
2531 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002532{
Olli Etuaho476197f2016-10-11 13:59:08 +01002533 // Note: function found from the symbol table could be the same as parsedFunction if this is the
2534 // first declaration. Either way the instance in the symbol table is used to track whether the
2535 // function is declared multiple times.
2536 TFunction *function = static_cast<TFunction *>(
2537 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
2538 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02002539 {
2540 // ESSL 1.00.17 section 4.2.7.
2541 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
2542 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02002543 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002544 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02002545
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002546 TIntermFunctionPrototype *prototype =
2547 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002548
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002549 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002550
2551 if (!symbolTable.atGlobalLevel())
2552 {
2553 // ESSL 3.00.4 section 4.2.4.
2554 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002555 }
2556
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002557 return prototype;
2558}
2559
Olli Etuaho336b1472016-10-05 16:37:55 +01002560TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002561 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01002562 TIntermBlock *functionBody,
2563 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002564{
Olli Etuahof51fdd22016-10-03 10:03:40 +01002565 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002566 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
2567 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002568 error(location, "function does not return a value:",
2569 functionPrototype->getFunctionSymbolInfo()->getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002570 }
2571
Olli Etuahof51fdd22016-10-03 10:03:40 +01002572 if (functionBody == nullptr)
2573 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01002574 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002575 functionBody->setLine(location);
2576 }
Olli Etuaho336b1472016-10-05 16:37:55 +01002577 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002578 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01002579 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01002580
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002581 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002582 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002583}
2584
Olli Etuaho476197f2016-10-11 13:59:08 +01002585void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
2586 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002587 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04002588{
Olli Etuaho476197f2016-10-11 13:59:08 +01002589 ASSERT(function);
2590 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002591 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01002592 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002593
2594 if (builtIn)
2595 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002596 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002597 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002598 else
Jamie Madill185fb402015-06-12 15:48:48 -04002599 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002600 TFunction *prevDec = static_cast<TFunction *>(
2601 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
2602
2603 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
2604 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
2605 // occurance.
2606 if (*function != prevDec)
2607 {
2608 // Swap the parameters of the previous declaration to the parameters of the function
2609 // definition (parameter names may differ).
2610 prevDec->swapParameters(**function);
2611
2612 // The function definition will share the same symbol as any previous declaration.
2613 *function = prevDec;
2614 }
2615
2616 if ((*function)->isDefined())
2617 {
2618 error(location, "function already has a body", (*function)->getName().c_str());
2619 }
2620
2621 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04002622 }
Jamie Madill185fb402015-06-12 15:48:48 -04002623
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002624 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01002625 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002626 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04002627
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002628 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04002629 setLoopNestingLevel(0);
2630}
2631
Jamie Madillb98c3a82015-07-23 14:26:04 -04002632TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002633{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002634 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002635 // We don't know at this point whether this is a function definition or a prototype.
2636 // The definition production code will check for redefinitions.
2637 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002638 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002639 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
2640 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002641 //
2642 TFunction *prevDec =
2643 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302644
Martin Radevda6254b2016-12-14 17:00:36 +02002645 if (getShaderVersion() >= 300 &&
2646 symbolTable.hasUnmangledBuiltInForShaderVersion(function->getName().c_str(),
2647 getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302648 {
Martin Radevda6254b2016-12-14 17:00:36 +02002649 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302650 // Therefore overloading or redefining builtin functions is an error.
2651 error(location, "Name of a built-in function cannot be redeclared as function",
2652 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302653 }
2654 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002655 {
2656 if (prevDec->getReturnType() != function->getReturnType())
2657 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002658 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002659 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04002660 }
2661 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2662 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002663 if (prevDec->getParam(i).type->getQualifier() !=
2664 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002665 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002666 error(location,
2667 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002668 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04002669 }
2670 }
2671 }
2672
2673 //
2674 // Check for previously declared variables using the same name.
2675 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002676 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002677 if (prevSym)
2678 {
2679 if (!prevSym->isFunction())
2680 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002681 error(location, "redefinition of a function", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002682 }
2683 }
2684 else
2685 {
2686 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01002687 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04002688 }
2689
2690 // We're at the inner scope level of the function's arguments and body statement.
2691 // Add the function prototype to the surrounding scope instead.
2692 symbolTable.getOuterLevel()->insert(function);
2693
Olli Etuaho78d13742017-01-18 13:06:10 +00002694 // Raise error message if main function takes any parameters or return anything other than void
2695 if (function->getName() == "main")
2696 {
2697 if (function->getParamCount() > 0)
2698 {
2699 error(location, "function cannot take any parameter(s)", "main");
2700 }
2701 if (function->getReturnType().getBasicType() != EbtVoid)
2702 {
2703 error(location, "main function cannot return a value",
2704 function->getReturnType().getBasicString());
2705 }
2706 }
2707
Jamie Madill185fb402015-06-12 15:48:48 -04002708 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002709 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2710 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002711 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2712 //
2713 return function;
2714}
2715
Olli Etuaho9de84a52016-06-14 17:36:01 +03002716TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
2717 const TString *name,
2718 const TSourceLoc &location)
2719{
2720 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
2721 {
2722 error(location, "no qualifiers allowed for function return",
2723 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03002724 }
2725 if (!type.layoutQualifier.isEmpty())
2726 {
2727 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03002728 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002729 // make sure a sampler or an image is not involved as well...
Martin Radev4a9cd802016-09-01 16:51:51 +03002730 checkIsNotSampler(location, type.typeSpecifierNonArray,
2731 "samplers can't be function return values");
Martin Radev2cc85b32016-08-05 16:22:53 +03002732 checkIsNotImage(location, type.typeSpecifierNonArray, "images can't be function return values");
Olli Etuahoe29324f2016-06-15 10:58:03 +03002733 if (mShaderVersion < 300)
2734 {
2735 // Array return values are forbidden, but there's also no valid syntax for declaring array
2736 // return values in ESSL 1.00.
Olli Etuaho77ba4082016-12-16 12:01:18 +00002737 ASSERT(type.arraySize == 0 || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03002738
2739 if (type.isStructureContainingArrays())
2740 {
2741 // ESSL 1.00.17 section 6.1 Function Definitions
2742 error(location, "structures containing arrays can't be function return values",
2743 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03002744 }
2745 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03002746
2747 // Add the function as a prototype after parsing it (we do not support recursion)
2748 return new TFunction(name, new TType(type));
2749}
2750
Jamie Madill06145232015-05-13 13:10:01 -04002751TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002752{
Jamie Madill06145232015-05-13 13:10:01 -04002753 TPublicType publicType = publicTypeIn;
Martin Radev4a9cd802016-09-01 16:51:51 +03002754 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02002755 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002756 error(publicType.getLine(), "constructor can't be a structure definition",
2757 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02002758 }
2759
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002760 TOperator op = EOpNull;
Martin Radev4a9cd802016-09-01 16:51:51 +03002761 if (publicType.getUserDef())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002762 {
2763 op = EOpConstructStruct;
2764 }
2765 else
2766 {
Geoff Lang156d7192016-07-21 16:11:00 -04002767 op = sh::TypeToConstructorOperator(TType(publicType));
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002768 if (op == EOpNull)
2769 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002770 error(publicType.getLine(), "cannot construct this type",
2771 getBasicString(publicType.getBasicType()));
2772 publicType.setBasicType(EbtFloat);
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002773 op = EOpConstructFloat;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002774 }
2775 }
2776
Dmitry Skiba7f17a502015-06-22 15:08:39 -07002777 const TType *type = new TType(publicType);
Olli Etuaho72d10202017-01-19 15:58:30 +00002778 return new TFunction(nullptr, type, op);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002779}
2780
Jamie Madillb98c3a82015-07-23 14:26:04 -04002781// This function is used to test for the correctness of the parameters passed to various constructor
2782// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002783//
Olli Etuaho856c4972016-08-08 11:38:39 +03002784// 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 +00002785//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002786TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002787 TOperator op,
Olli Etuaho72d10202017-01-19 15:58:30 +00002788 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302789 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002790{
Olli Etuaho856c4972016-08-08 11:38:39 +03002791 if (type.isUnsizedArray())
2792 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002793 if (arguments->empty())
Olli Etuahobbe9fb52016-11-03 17:16:05 +00002794 {
2795 error(line, "implicitly sized array constructor must have at least one argument", "[]");
2796 type.setArraySize(1u);
2797 return TIntermTyped::CreateZero(type);
2798 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002799 type.setArraySize(static_cast<unsigned int>(arguments->size()));
Olli Etuaho856c4972016-08-08 11:38:39 +03002800 }
Olli Etuaho856c4972016-08-08 11:38:39 +03002801
Olli Etuaho72d10202017-01-19 15:58:30 +00002802 if (!checkConstructorArguments(line, arguments, op, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03002803 {
Olli Etuaho72d10202017-01-19 15:58:30 +00002804 return TIntermTyped::CreateZero(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03002805 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002806
Olli Etuahofe486322017-03-21 09:30:54 +00002807 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002808 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002809
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002810 TIntermTyped *constConstructor =
2811 intermediate.foldAggregateBuiltIn(constructorNode, mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002812 if (constConstructor)
2813 {
2814 return constConstructor;
2815 }
2816
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002817 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002818}
2819
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002820//
2821// Interface/uniform blocks
2822//
Olli Etuaho13389b62016-10-16 11:48:18 +01002823TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03002824 const TTypeQualifierBuilder &typeQualifierBuilder,
2825 const TSourceLoc &nameLine,
2826 const TString &blockName,
2827 TFieldList *fieldList,
2828 const TString *instanceName,
2829 const TSourceLoc &instanceLine,
2830 TIntermTyped *arrayIndex,
2831 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002832{
Olli Etuaho856c4972016-08-08 11:38:39 +03002833 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002834
Olli Etuaho77ba4082016-12-16 12:01:18 +00002835 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03002836
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002837 if (typeQualifier.qualifier != EvqUniform)
2838 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002839 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform",
2840 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002841 }
2842
Martin Radev70866b82016-07-22 15:27:42 +03002843 if (typeQualifier.invariant)
2844 {
2845 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
2846 }
2847
Olli Etuaho43364892017-02-13 16:00:12 +00002848 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
2849
2850 // TODO(oetuaho): Remove this and support binding for blocks.
2851 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03002852
Andrei Volykhina5527072017-03-22 16:46:30 +03002853 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
2854
Jamie Madill099c0f32013-06-20 11:55:52 -04002855 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03002856 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002857
Jamie Madill099c0f32013-06-20 11:55:52 -04002858 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2859 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002860 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002861 }
2862
Jamie Madill1566ef72013-06-20 11:55:54 -04002863 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2864 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002865 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002866 }
2867
Olli Etuaho856c4972016-08-08 11:38:39 +03002868 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002869
Martin Radev2cc85b32016-08-05 16:22:53 +03002870 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
2871
Arun Patole7e7e68d2015-05-22 12:02:25 +05302872 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2873 if (!symbolTable.declare(blockNameSymbol))
2874 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002875 error(nameLine, "redefinition of an interface block name", blockName.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002876 }
2877
Jamie Madill98493dd2013-07-08 14:39:03 -04002878 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302879 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2880 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002881 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302882 TType *fieldType = field->type();
2883 if (IsSampler(fieldType->getBasicType()))
2884 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002885 error(field->line(),
2886 "unsupported type - sampler types are not allowed in interface blocks",
2887 fieldType->getBasicString());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002888 }
2889
Martin Radev2cc85b32016-08-05 16:22:53 +03002890 if (IsImage(fieldType->getBasicType()))
2891 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002892 error(field->line(),
2893 "unsupported type - image types are not allowed in interface blocks",
2894 fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03002895 }
2896
Jamie Madill98493dd2013-07-08 14:39:03 -04002897 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002898 switch (qualifier)
2899 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002900 case EvqGlobal:
2901 case EvqUniform:
2902 break;
2903 default:
2904 error(field->line(), "invalid qualifier on interface block member",
2905 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04002906 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002907 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002908
Martin Radev70866b82016-07-22 15:27:42 +03002909 if (fieldType->isInvariant())
2910 {
2911 error(field->line(), "invalid qualifier on interface block member", "invariant");
2912 }
2913
Jamie Madilla5efff92013-06-06 11:56:47 -04002914 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002915 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03002916 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
Jamie Madill099c0f32013-06-20 11:55:52 -04002917
Jamie Madill98493dd2013-07-08 14:39:03 -04002918 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002919 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002920 error(field->line(), "invalid layout qualifier: cannot be used here",
2921 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04002922 }
2923
Jamie Madill98493dd2013-07-08 14:39:03 -04002924 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002925 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002926 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002927 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002928 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002929 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002930 warning(field->line(),
2931 "extraneous layout qualifier: only has an effect on matrix types",
2932 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04002933 }
2934
Jamie Madill98493dd2013-07-08 14:39:03 -04002935 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002936 }
2937
Jamie Madill98493dd2013-07-08 14:39:03 -04002938 // add array index
Olli Etuaho856c4972016-08-08 11:38:39 +03002939 unsigned int arraySize = 0;
2940 if (arrayIndex != nullptr)
Jamie Madill98493dd2013-07-08 14:39:03 -04002941 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002942 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04002943 }
2944
Jamie Madillb98c3a82015-07-23 14:26:04 -04002945 TInterfaceBlock *interfaceBlock =
2946 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2947 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
2948 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002949
2950 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04002951 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002952
Jamie Madill98493dd2013-07-08 14:39:03 -04002953 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002954 {
2955 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002956 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2957 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002958 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302959 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002960
2961 // set parent pointer of the field variable
2962 fieldType->setInterfaceBlock(interfaceBlock);
2963
Arun Patole7e7e68d2015-05-22 12:02:25 +05302964 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002965 fieldVariable->setQualifier(typeQualifier.qualifier);
2966
Arun Patole7e7e68d2015-05-22 12:02:25 +05302967 if (!symbolTable.declare(fieldVariable))
2968 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002969 error(field->line(), "redefinition of an interface block member name",
2970 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002971 }
2972 }
2973 }
2974 else
2975 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002976 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03002977
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002978 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302979 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002980 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002981
Arun Patole7e7e68d2015-05-22 12:02:25 +05302982 if (!symbolTable.declare(instanceTypeDef))
2983 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002984 error(instanceLine, "redefinition of an interface block instance name",
2985 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002986 }
2987
Jamie Madillb98c3a82015-07-23 14:26:04 -04002988 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002989 symbolName = instanceTypeDef->getName();
2990 }
2991
Olli Etuaho13389b62016-10-16 11:48:18 +01002992 TIntermSymbol *blockSymbol =
2993 intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line);
2994 TIntermDeclaration *declaration = new TIntermDeclaration();
2995 declaration->appendDeclarator(blockSymbol);
2996 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04002997
2998 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01002999 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003000}
3001
Olli Etuaho383b7912016-08-05 11:22:59 +03003002void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003003{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003004 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003005
3006 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003007 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303008 if (mStructNestingLevel > 1)
3009 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003010 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003011 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003012}
3013
3014void TParseContext::exitStructDeclaration()
3015{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003016 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003017}
3018
Olli Etuaho8a176262016-08-16 14:23:01 +03003019void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003020{
Jamie Madillacb4b812016-11-07 13:50:29 -05003021 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303022 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003023 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003024 }
3025
Arun Patole7e7e68d2015-05-22 12:02:25 +05303026 if (field.type()->getBasicType() != EbtStruct)
3027 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003028 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003029 }
3030
3031 // We're already inside a structure definition at this point, so add
3032 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303033 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3034 {
Jamie Madill41a49272014-03-18 16:10:13 -04003035 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003036 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
3037 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003038 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003039 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003040 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003041 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003042}
3043
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003044//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003045// Parse an array index expression
3046//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003047TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3048 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303049 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003050{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003051 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3052 {
3053 if (baseExpression->getAsSymbolNode())
3054 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303055 error(location, " left of '[' is not of type array, matrix, or vector ",
3056 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003057 }
3058 else
3059 {
3060 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3061 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003062
3063 TConstantUnion *unionArray = new TConstantUnion[1];
3064 unionArray->setFConst(0.0f);
3065 return intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst),
3066 location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003067 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003068
Jamie Madill21c1e452014-12-29 11:33:41 -05003069 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3070
Olli Etuaho36b05142015-11-12 13:10:42 +02003071 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3072 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3073 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3074 // index is a constant expression.
3075 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3076 {
3077 if (baseExpression->isInterfaceBlock())
3078 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003079 error(location,
3080 "array indexes for interface blocks arrays must be constant integral expressions",
3081 "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003082 }
3083 else if (baseExpression->getQualifier() == EvqFragmentOut)
3084 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003085 error(location,
3086 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003087 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003088 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3089 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003090 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003091 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003092 }
3093
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003094 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003095 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003096 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3097 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3098 // constant fold expressions that are not constant expressions). The most compatible way to
3099 // handle this case is to report a warning instead of an error and force the index to be in
3100 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003101 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003102 int index = indexConstantUnion->getIConst(0);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003103
3104 int safeIndex = -1;
3105
3106 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04003107 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003108 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003109 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003110 if (mShaderSpec == SH_WEBGL2_SPEC)
3111 {
3112 // Error has been already generated if index is not const.
3113 if (indexExpression->getQualifier() == EvqConst)
3114 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003115 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003116 }
3117 safeIndex = 0;
3118 }
3119 else if (!isExtensionEnabled("GL_EXT_draw_buffers"))
3120 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003121 outOfRangeError(outOfRangeIndexIsError, location,
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003122 "array index for gl_FragData must be zero when "
Olli Etuaho4de340a2016-12-16 09:32:03 +00003123 "GL_EXT_draw_buffers is disabled",
3124 "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003125 safeIndex = 0;
3126 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03003127 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003128 // Only do generic out-of-range check if similar error hasn't already been reported.
3129 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003130 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003131 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3132 baseExpression->getArraySize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003133 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003134 }
3135 }
3136 else if (baseExpression->isMatrix())
3137 {
3138 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03003139 baseExpression->getType().getCols(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003140 "matrix field selection out of range");
Jamie Madill7164cf42013-07-08 13:30:59 -04003141 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003142 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04003143 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003144 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3145 baseExpression->getType().getNominalSize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003146 "vector field selection out of range");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003147 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003148
3149 ASSERT(safeIndex >= 0);
3150 // Data of constant unions can't be changed, because it may be shared with other
3151 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
3152 // sanitized object.
3153 if (safeIndex != index)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003154 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003155 TConstantUnion *safeConstantUnion = new TConstantUnion();
3156 safeConstantUnion->setIConst(safeIndex);
3157 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003158 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003159
3160 return intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003161 mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003162 }
Jamie Madill7164cf42013-07-08 13:30:59 -04003163 else
3164 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003165 return intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003166 mDiagnostics);
Jamie Madill7164cf42013-07-08 13:30:59 -04003167 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003168}
3169
Olli Etuaho90892fb2016-07-14 14:44:51 +03003170int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
3171 const TSourceLoc &location,
3172 int index,
3173 int arraySize,
Olli Etuaho4de340a2016-12-16 09:32:03 +00003174 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003175{
3176 if (index >= arraySize || index < 0)
3177 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003178 std::stringstream reasonStream;
3179 reasonStream << reason << " '" << index << "'";
3180 std::string token = reasonStream.str();
3181 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuaho90892fb2016-07-14 14:44:51 +03003182 if (index < 0)
3183 {
3184 return 0;
3185 }
3186 else
3187 {
3188 return arraySize - 1;
3189 }
3190 }
3191 return index;
3192}
3193
Jamie Madillb98c3a82015-07-23 14:26:04 -04003194TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3195 const TSourceLoc &dotLocation,
3196 const TString &fieldString,
3197 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003198{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003199 if (baseExpression->isArray())
3200 {
3201 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003202 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003203 }
3204
3205 if (baseExpression->isVector())
3206 {
3207 TVectorFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003208 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields,
3209 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003210 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003211 fields.num = 1;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003212 fields.offsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003213 }
3214
Olli Etuahob6fa0432016-09-28 16:28:05 +01003215 return TIntermediate::AddSwizzle(baseExpression, fields, dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003216 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003217 else if (baseExpression->getBasicType() == EbtStruct)
3218 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303219 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003220 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003221 {
3222 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003223 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003224 }
3225 else
3226 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003227 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003228 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003229 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003230 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003231 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003232 {
3233 fieldFound = true;
3234 break;
3235 }
3236 }
3237 if (fieldFound)
3238 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003239 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3240 index->setLine(fieldLocation);
3241 return intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003242 dotLocation, mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003243 }
3244 else
3245 {
3246 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003247 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003248 }
3249 }
3250 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003251 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003252 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303253 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003254 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003255 {
3256 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003257 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003258 }
3259 else
3260 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003261 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003262 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003263 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003264 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003265 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003266 {
3267 fieldFound = true;
3268 break;
3269 }
3270 }
3271 if (fieldFound)
3272 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003273 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3274 index->setLine(fieldLocation);
3275 return intermediate.addIndex(EOpIndexDirectInterfaceBlock, baseExpression, index,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003276 dotLocation, mDiagnostics);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003277 }
3278 else
3279 {
3280 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003281 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003282 }
3283 }
3284 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003285 else
3286 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003287 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003288 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03003289 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303290 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003291 }
3292 else
3293 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303294 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003295 " field selection requires structure, vector, or interface block on left hand "
3296 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303297 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003298 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003299 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003300 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003301}
3302
Jamie Madillb98c3a82015-07-23 14:26:04 -04003303TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3304 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003305{
Martin Radev802abe02016-08-04 17:48:32 +03003306 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003307
3308 if (qualifierType == "shared")
3309 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003310 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003311 {
3312 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
3313 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003314 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003315 }
3316 else if (qualifierType == "packed")
3317 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003318 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003319 {
3320 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
3321 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003322 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003323 }
3324 else if (qualifierType == "std140")
3325 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003326 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003327 }
3328 else if (qualifierType == "row_major")
3329 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003330 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003331 }
3332 else if (qualifierType == "column_major")
3333 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003334 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003335 }
3336 else if (qualifierType == "location")
3337 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003338 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
3339 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003340 }
Andrei Volykhina5527072017-03-22 16:46:30 +03003341 else if (qualifierType == "yuv" && isExtensionEnabled("GL_EXT_YUV_target") &&
3342 mShaderType == GL_FRAGMENT_SHADER)
3343 {
3344 qualifier.yuv = true;
3345 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003346 else if (qualifierType == "rgba32f")
3347 {
3348 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3349 qualifier.imageInternalFormat = EiifRGBA32F;
3350 }
3351 else if (qualifierType == "rgba16f")
3352 {
3353 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3354 qualifier.imageInternalFormat = EiifRGBA16F;
3355 }
3356 else if (qualifierType == "r32f")
3357 {
3358 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3359 qualifier.imageInternalFormat = EiifR32F;
3360 }
3361 else if (qualifierType == "rgba8")
3362 {
3363 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3364 qualifier.imageInternalFormat = EiifRGBA8;
3365 }
3366 else if (qualifierType == "rgba8_snorm")
3367 {
3368 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3369 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
3370 }
3371 else if (qualifierType == "rgba32i")
3372 {
3373 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3374 qualifier.imageInternalFormat = EiifRGBA32I;
3375 }
3376 else if (qualifierType == "rgba16i")
3377 {
3378 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3379 qualifier.imageInternalFormat = EiifRGBA16I;
3380 }
3381 else if (qualifierType == "rgba8i")
3382 {
3383 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3384 qualifier.imageInternalFormat = EiifRGBA8I;
3385 }
3386 else if (qualifierType == "r32i")
3387 {
3388 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3389 qualifier.imageInternalFormat = EiifR32I;
3390 }
3391 else if (qualifierType == "rgba32ui")
3392 {
3393 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3394 qualifier.imageInternalFormat = EiifRGBA32UI;
3395 }
3396 else if (qualifierType == "rgba16ui")
3397 {
3398 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3399 qualifier.imageInternalFormat = EiifRGBA16UI;
3400 }
3401 else if (qualifierType == "rgba8ui")
3402 {
3403 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3404 qualifier.imageInternalFormat = EiifRGBA8UI;
3405 }
3406 else if (qualifierType == "r32ui")
3407 {
3408 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3409 qualifier.imageInternalFormat = EiifR32UI;
3410 }
3411
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003412 else
3413 {
3414 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003415 }
3416
Jamie Madilla5efff92013-06-06 11:56:47 -04003417 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003418}
3419
Martin Radev802abe02016-08-04 17:48:32 +03003420void TParseContext::parseLocalSize(const TString &qualifierType,
3421 const TSourceLoc &qualifierTypeLine,
3422 int intValue,
3423 const TSourceLoc &intValueLine,
3424 const std::string &intValueString,
3425 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03003426 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03003427{
Olli Etuaho856c4972016-08-08 11:38:39 +03003428 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03003429 if (intValue < 1)
3430 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003431 std::stringstream reasonStream;
3432 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
3433 std::string reason = reasonStream.str();
3434 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003435 }
3436 (*localSize)[index] = intValue;
3437}
3438
Olli Etuaho09b04a22016-12-15 13:30:26 +00003439void TParseContext::parseNumViews(int intValue,
3440 const TSourceLoc &intValueLine,
3441 const std::string &intValueString,
3442 int *numViews)
3443{
3444 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3445 // specification.
3446 if (intValue < 1)
3447 {
3448 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
3449 }
3450 *numViews = intValue;
3451}
3452
Jamie Madillb98c3a82015-07-23 14:26:04 -04003453TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3454 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003455 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303456 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003457{
Martin Radev802abe02016-08-04 17:48:32 +03003458 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003459
Martin Radev802abe02016-08-04 17:48:32 +03003460 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003461
Martin Radev802abe02016-08-04 17:48:32 +03003462 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003463 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003464 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003465 if (intValue < 0)
3466 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003467 error(intValueLine, "out of range: location must be non-negative",
3468 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003469 }
3470 else
3471 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003472 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03003473 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003474 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003475 }
Olli Etuaho43364892017-02-13 16:00:12 +00003476 else if (qualifierType == "binding")
3477 {
3478 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3479 if (intValue < 0)
3480 {
3481 error(intValueLine, "out of range: binding must be non-negative",
3482 intValueString.c_str());
3483 }
3484 else
3485 {
3486 qualifier.binding = intValue;
3487 }
3488 }
Martin Radev802abe02016-08-04 17:48:32 +03003489 else if (qualifierType == "local_size_x")
3490 {
3491 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
3492 &qualifier.localSize);
3493 }
3494 else if (qualifierType == "local_size_y")
3495 {
3496 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
3497 &qualifier.localSize);
3498 }
3499 else if (qualifierType == "local_size_z")
3500 {
3501 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
3502 &qualifier.localSize);
3503 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00003504 else if (qualifierType == "num_views" && mMultiviewAvailable &&
3505 (isExtensionEnabled("GL_OVR_multiview") || isExtensionEnabled("GL_OVR_multiview2")) &&
3506 mShaderType == GL_VERTEX_SHADER)
3507 {
3508 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
3509 }
Martin Radev802abe02016-08-04 17:48:32 +03003510 else
3511 {
3512 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003513 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003514
Jamie Madilla5efff92013-06-06 11:56:47 -04003515 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003516}
3517
Olli Etuaho613b9592016-09-05 12:05:53 +03003518TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
3519{
3520 return new TTypeQualifierBuilder(
3521 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
3522 mShaderVersion);
3523}
3524
Jamie Madillb98c3a82015-07-23 14:26:04 -04003525TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03003526 TLayoutQualifier rightQualifier,
3527 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003528{
Martin Radevc28888b2016-07-22 15:27:42 +03003529 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003530 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003531}
3532
Olli Etuaho4de340a2016-12-16 09:32:03 +00003533TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
3534 const TFieldList *newlyAddedFields,
3535 const TSourceLoc &location)
3536{
3537 for (TField *field : *newlyAddedFields)
3538 {
3539 for (TField *oldField : *processedFields)
3540 {
3541 if (oldField->name() == field->name())
3542 {
3543 error(location, "duplicate field name in structure", field->name().c_str());
3544 }
3545 }
3546 processedFields->push_back(field);
3547 }
3548 return processedFields;
3549}
3550
Martin Radev70866b82016-07-22 15:27:42 +03003551TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
3552 const TTypeQualifierBuilder &typeQualifierBuilder,
3553 TPublicType *typeSpecifier,
3554 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003555{
Olli Etuaho77ba4082016-12-16 12:01:18 +00003556 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003557
Martin Radev70866b82016-07-22 15:27:42 +03003558 typeSpecifier->qualifier = typeQualifier.qualifier;
3559 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03003560 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03003561 typeSpecifier->invariant = typeQualifier.invariant;
3562 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05303563 {
Martin Radev70866b82016-07-22 15:27:42 +03003564 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003565 }
Martin Radev70866b82016-07-22 15:27:42 +03003566 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003567}
3568
Jamie Madillb98c3a82015-07-23 14:26:04 -04003569TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3570 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003571{
Martin Radev4a9cd802016-09-01 16:51:51 +03003572 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
3573 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03003574
Martin Radev4a9cd802016-09-01 16:51:51 +03003575 checkIsNonVoid(typeSpecifier.getLine(), (*fieldList)[0]->name(), typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003576
Martin Radev4a9cd802016-09-01 16:51:51 +03003577 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003578
Arun Patole7e7e68d2015-05-22 12:02:25 +05303579 for (unsigned int i = 0; i < fieldList->size(); ++i)
3580 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003581 //
3582 // Careful not to replace already known aspects of type, like array-ness
3583 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303584 TType *type = (*fieldList)[i]->type();
Martin Radev4a9cd802016-09-01 16:51:51 +03003585 type->setBasicType(typeSpecifier.getBasicType());
3586 type->setPrimarySize(typeSpecifier.getPrimarySize());
3587 type->setSecondarySize(typeSpecifier.getSecondarySize());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003588 type->setPrecision(typeSpecifier.precision);
3589 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003590 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
Martin Radev2cc85b32016-08-05 16:22:53 +03003591 type->setMemoryQualifier(typeSpecifier.memoryQualifier);
Martin Radev70866b82016-07-22 15:27:42 +03003592 type->setInvariant(typeSpecifier.invariant);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003593
3594 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303595 if (type->isArray())
3596 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003597 checkIsValidTypeForArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003598 }
3599 if (typeSpecifier.array)
Olli Etuaho856c4972016-08-08 11:38:39 +03003600 type->setArraySize(static_cast<unsigned int>(typeSpecifier.arraySize));
Martin Radev4a9cd802016-09-01 16:51:51 +03003601 if (typeSpecifier.getUserDef())
Arun Patole7e7e68d2015-05-22 12:02:25 +05303602 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003603 type->setStruct(typeSpecifier.getUserDef()->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003604 }
3605
Martin Radev4a9cd802016-09-01 16:51:51 +03003606 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*fieldList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003607 }
3608
Jamie Madill98493dd2013-07-08 14:39:03 -04003609 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003610}
3611
Martin Radev4a9cd802016-09-01 16:51:51 +03003612TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
3613 const TSourceLoc &nameLine,
3614 const TString *structName,
3615 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003616{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303617 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003618 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003619
Jamie Madill9b820842015-02-12 10:40:10 -05003620 // Store a bool in the struct if we're at global scope, to allow us to
3621 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05003622 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003623
Jamie Madill98493dd2013-07-08 14:39:03 -04003624 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003625 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003626 checkIsNotReserved(nameLine, *structName);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303627 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
3628 if (!symbolTable.declare(userTypeDef))
3629 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003630 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003631 }
3632 }
3633
3634 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04003635 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003636 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003637 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04003638 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003639 switch (qualifier)
3640 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003641 case EvqGlobal:
3642 case EvqTemporary:
3643 break;
3644 default:
3645 error(field.line(), "invalid qualifier on struct member",
3646 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003647 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003648 }
Martin Radev70866b82016-07-22 15:27:42 +03003649 if (field.type()->isInvariant())
3650 {
3651 error(field.line(), "invalid qualifier on struct member", "invariant");
3652 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003653 if (IsImage(field.type()->getBasicType()))
3654 {
3655 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
3656 }
3657
Olli Etuaho43364892017-02-13 16:00:12 +00003658 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
3659
3660 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03003661
3662 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003663 }
3664
Martin Radev4a9cd802016-09-01 16:51:51 +03003665 TTypeSpecifierNonArray typeSpecifierNonArray;
3666 typeSpecifierNonArray.initialize(EbtStruct, structLine);
3667 typeSpecifierNonArray.userDef = structureType;
3668 typeSpecifierNonArray.isStructSpecifier = true;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003669 exitStructDeclaration();
3670
Martin Radev4a9cd802016-09-01 16:51:51 +03003671 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003672}
3673
Jamie Madillb98c3a82015-07-23 14:26:04 -04003674TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003675 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003676 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02003677{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003678 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003679 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02003680 init->isVector())
3681 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003682 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
3683 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003684 return nullptr;
3685 }
3686
Olli Etuahoac5274d2015-02-20 10:19:08 +02003687 if (statementList)
3688 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00003689 if (!ValidateSwitchStatementList(switchType, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02003690 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02003691 return nullptr;
3692 }
3693 }
3694
Olli Etuahoa3a36662015-02-17 13:46:51 +02003695 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3696 if (node == nullptr)
3697 {
3698 error(loc, "erroneous switch statement", "switch");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003699 return nullptr;
3700 }
3701 return node;
3702}
3703
3704TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3705{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003706 if (mSwitchNestingLevel == 0)
3707 {
3708 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003709 return nullptr;
3710 }
3711 if (condition == nullptr)
3712 {
3713 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003714 return nullptr;
3715 }
3716 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04003717 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02003718 {
3719 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003720 }
3721 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003722 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
3723 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
3724 // fold in case labels.
3725 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02003726 {
3727 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003728 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003729 TIntermCase *node = intermediate.addCase(condition, loc);
3730 if (node == nullptr)
3731 {
3732 error(loc, "erroneous case statement", "case");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003733 return nullptr;
3734 }
3735 return node;
3736}
3737
3738TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3739{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003740 if (mSwitchNestingLevel == 0)
3741 {
3742 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003743 return nullptr;
3744 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003745 TIntermCase *node = intermediate.addCase(nullptr, loc);
3746 if (node == nullptr)
3747 {
3748 error(loc, "erroneous default statement", "default");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003749 return nullptr;
3750 }
3751 return node;
3752}
3753
Jamie Madillb98c3a82015-07-23 14:26:04 -04003754TIntermTyped *TParseContext::createUnaryMath(TOperator op,
3755 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003756 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003757{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003758 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003759
3760 switch (op)
3761 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003762 case EOpLogicalNot:
3763 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
3764 child->isVector())
3765 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003766 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003767 return nullptr;
3768 }
3769 break;
3770 case EOpBitwiseNot:
3771 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3772 child->isMatrix() || child->isArray())
3773 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003774 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003775 return nullptr;
3776 }
3777 break;
3778 case EOpPostIncrement:
3779 case EOpPreIncrement:
3780 case EOpPostDecrement:
3781 case EOpPreDecrement:
3782 case EOpNegative:
3783 case EOpPositive:
3784 if (child->getBasicType() == EbtStruct || child->getBasicType() == EbtBool ||
Martin Radev2cc85b32016-08-05 16:22:53 +03003785 child->isArray() || IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04003786 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003787 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003788 return nullptr;
3789 }
3790 // Operators for built-ins are already type checked against their prototype.
3791 default:
3792 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003793 }
3794
Olli Etuahof119a262016-08-19 15:54:22 +03003795 TIntermUnary *node = new TIntermUnary(op, child);
3796 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03003797
Olli Etuaho77ba4082016-12-16 12:01:18 +00003798 TIntermTyped *foldedNode = node->fold(mDiagnostics);
Olli Etuahof119a262016-08-19 15:54:22 +03003799 if (foldedNode)
3800 return foldedNode;
3801
3802 return node;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003803}
3804
Olli Etuaho09b22472015-02-11 11:47:26 +02003805TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3806{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003807 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003808 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003809 {
Olli Etuaho09b22472015-02-11 11:47:26 +02003810 return child;
3811 }
3812 return node;
3813}
3814
Jamie Madillb98c3a82015-07-23 14:26:04 -04003815TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
3816 TIntermTyped *child,
3817 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003818{
Olli Etuaho856c4972016-08-08 11:38:39 +03003819 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02003820 return addUnaryMath(op, child, loc);
3821}
3822
Jamie Madillb98c3a82015-07-23 14:26:04 -04003823bool TParseContext::binaryOpCommonCheck(TOperator op,
3824 TIntermTyped *left,
3825 TIntermTyped *right,
3826 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003827{
Olli Etuaho244be012016-08-18 15:26:02 +03003828 if (left->getType().getStruct() || right->getType().getStruct())
3829 {
3830 switch (op)
3831 {
3832 case EOpIndexDirectStruct:
3833 ASSERT(left->getType().getStruct());
3834 break;
3835 case EOpEqual:
3836 case EOpNotEqual:
3837 case EOpAssign:
3838 case EOpInitialize:
3839 if (left->getType() != right->getType())
3840 {
3841 return false;
3842 }
3843 break;
3844 default:
3845 error(loc, "Invalid operation for structs", GetOperatorString(op));
3846 return false;
3847 }
3848 }
3849
Olli Etuahod6b14282015-03-17 14:31:35 +02003850 if (left->isArray() || right->isArray())
3851 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003852 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003853 {
3854 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3855 return false;
3856 }
3857
3858 if (left->isArray() != right->isArray())
3859 {
3860 error(loc, "array / non-array mismatch", GetOperatorString(op));
3861 return false;
3862 }
3863
3864 switch (op)
3865 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003866 case EOpEqual:
3867 case EOpNotEqual:
3868 case EOpAssign:
3869 case EOpInitialize:
3870 break;
3871 default:
3872 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3873 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02003874 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003875 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003876 if (left->getArraySize() != right->getArraySize())
3877 {
3878 error(loc, "array size mismatch", GetOperatorString(op));
3879 return false;
3880 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003881 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003882
3883 // Check ops which require integer / ivec parameters
3884 bool isBitShift = false;
3885 switch (op)
3886 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003887 case EOpBitShiftLeft:
3888 case EOpBitShiftRight:
3889 case EOpBitShiftLeftAssign:
3890 case EOpBitShiftRightAssign:
3891 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3892 // check that the basic type is an integer type.
3893 isBitShift = true;
3894 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3895 {
3896 return false;
3897 }
3898 break;
3899 case EOpBitwiseAnd:
3900 case EOpBitwiseXor:
3901 case EOpBitwiseOr:
3902 case EOpBitwiseAndAssign:
3903 case EOpBitwiseXorAssign:
3904 case EOpBitwiseOrAssign:
3905 // It is enough to check the type of only one operand, since later it
3906 // is checked that the operand types match.
3907 if (!IsInteger(left->getBasicType()))
3908 {
3909 return false;
3910 }
3911 break;
3912 default:
3913 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003914 }
3915
3916 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3917 // So the basic type should usually match.
3918 if (!isBitShift && left->getBasicType() != right->getBasicType())
3919 {
3920 return false;
3921 }
3922
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003923 // Check that:
3924 // 1. Type sizes match exactly on ops that require that.
3925 // 2. Restrictions for structs that contain arrays or samplers are respected.
3926 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003927 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003928 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003929 case EOpAssign:
3930 case EOpInitialize:
3931 case EOpEqual:
3932 case EOpNotEqual:
3933 // ESSL 1.00 sections 5.7, 5.8, 5.9
3934 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
3935 {
3936 error(loc, "undefined operation for structs containing arrays",
3937 GetOperatorString(op));
3938 return false;
3939 }
3940 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3941 // we interpret the spec so that this extends to structs containing samplers,
3942 // similarly to ESSL 1.00 spec.
3943 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
3944 left->getType().isStructureContainingSamplers())
3945 {
3946 error(loc, "undefined operation for structs containing samplers",
3947 GetOperatorString(op));
3948 return false;
3949 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003950
3951 if ((op == EOpAssign || op == EOpInitialize) &&
3952 left->getType().isStructureContainingImages())
3953 {
3954 error(loc, "undefined operation for structs containing images",
3955 GetOperatorString(op));
3956 return false;
3957 }
Olli Etuahoe1805592017-01-02 16:41:20 +00003958 if ((left->getNominalSize() != right->getNominalSize()) ||
3959 (left->getSecondarySize() != right->getSecondarySize()))
3960 {
3961 error(loc, "dimension mismatch", GetOperatorString(op));
3962 return false;
3963 }
3964 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003965 case EOpLessThan:
3966 case EOpGreaterThan:
3967 case EOpLessThanEqual:
3968 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00003969 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04003970 {
Olli Etuahoe1805592017-01-02 16:41:20 +00003971 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003972 return false;
3973 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003974 break;
3975 case EOpAdd:
3976 case EOpSub:
3977 case EOpDiv:
3978 case EOpIMod:
3979 case EOpBitShiftLeft:
3980 case EOpBitShiftRight:
3981 case EOpBitwiseAnd:
3982 case EOpBitwiseXor:
3983 case EOpBitwiseOr:
3984 case EOpAddAssign:
3985 case EOpSubAssign:
3986 case EOpDivAssign:
3987 case EOpIModAssign:
3988 case EOpBitShiftLeftAssign:
3989 case EOpBitShiftRightAssign:
3990 case EOpBitwiseAndAssign:
3991 case EOpBitwiseXorAssign:
3992 case EOpBitwiseOrAssign:
3993 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
3994 {
3995 return false;
3996 }
3997
3998 // Are the sizes compatible?
3999 if (left->getNominalSize() != right->getNominalSize() ||
4000 left->getSecondarySize() != right->getSecondarySize())
4001 {
4002 // If the nominal sizes of operands do not match:
4003 // One of them must be a scalar.
4004 if (!left->isScalar() && !right->isScalar())
4005 return false;
4006
4007 // In the case of compound assignment other than multiply-assign,
4008 // the right side needs to be a scalar. Otherwise a vector/matrix
4009 // would be assigned to a scalar. A scalar can't be shifted by a
4010 // vector either.
4011 if (!right->isScalar() &&
4012 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
4013 return false;
4014 }
4015 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004016 default:
4017 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004018 }
4019
Olli Etuahod6b14282015-03-17 14:31:35 +02004020 return true;
4021}
4022
Olli Etuaho1dded802016-08-18 18:13:13 +03004023bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
4024 const TType &left,
4025 const TType &right)
4026{
4027 switch (op)
4028 {
4029 case EOpMul:
4030 case EOpMulAssign:
4031 return left.getNominalSize() == right.getNominalSize() &&
4032 left.getSecondarySize() == right.getSecondarySize();
4033 case EOpVectorTimesScalar:
4034 return true;
4035 case EOpVectorTimesScalarAssign:
4036 ASSERT(!left.isMatrix() && !right.isMatrix());
4037 return left.isVector() && !right.isVector();
4038 case EOpVectorTimesMatrix:
4039 return left.getNominalSize() == right.getRows();
4040 case EOpVectorTimesMatrixAssign:
4041 ASSERT(!left.isMatrix() && right.isMatrix());
4042 return left.isVector() && left.getNominalSize() == right.getRows() &&
4043 left.getNominalSize() == right.getCols();
4044 case EOpMatrixTimesVector:
4045 return left.getCols() == right.getNominalSize();
4046 case EOpMatrixTimesScalar:
4047 return true;
4048 case EOpMatrixTimesScalarAssign:
4049 ASSERT(left.isMatrix() && !right.isMatrix());
4050 return !right.isVector();
4051 case EOpMatrixTimesMatrix:
4052 return left.getCols() == right.getRows();
4053 case EOpMatrixTimesMatrixAssign:
4054 ASSERT(left.isMatrix() && right.isMatrix());
4055 // We need to check two things:
4056 // 1. The matrix multiplication step is valid.
4057 // 2. The result will have the same number of columns as the lvalue.
4058 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
4059
4060 default:
4061 UNREACHABLE();
4062 return false;
4063 }
4064}
4065
Jamie Madillb98c3a82015-07-23 14:26:04 -04004066TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
4067 TIntermTyped *left,
4068 TIntermTyped *right,
4069 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02004070{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004071 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02004072 return nullptr;
4073
Olli Etuahofc1806e2015-03-17 13:03:11 +02004074 switch (op)
4075 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004076 case EOpEqual:
4077 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04004078 case EOpLessThan:
4079 case EOpGreaterThan:
4080 case EOpLessThanEqual:
4081 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04004082 break;
4083 case EOpLogicalOr:
4084 case EOpLogicalXor:
4085 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03004086 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4087 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00004088 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04004089 {
4090 return nullptr;
4091 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00004092 // Basic types matching should have been already checked.
4093 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004094 break;
4095 case EOpAdd:
4096 case EOpSub:
4097 case EOpDiv:
4098 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03004099 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4100 !right->getType().getStruct());
4101 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04004102 {
4103 return nullptr;
4104 }
4105 break;
4106 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03004107 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4108 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004109 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03004110 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04004111 {
4112 return nullptr;
4113 }
4114 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004115 default:
4116 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02004117 }
4118
Olli Etuaho1dded802016-08-18 18:13:13 +03004119 if (op == EOpMul)
4120 {
4121 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
4122 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4123 {
4124 return nullptr;
4125 }
4126 }
4127
Olli Etuaho3fdec912016-08-18 15:08:06 +03004128 TIntermBinary *node = new TIntermBinary(op, left, right);
4129 node->setLine(loc);
4130
Olli Etuaho3fdec912016-08-18 15:08:06 +03004131 // See if we can fold constants.
Olli Etuaho77ba4082016-12-16 12:01:18 +00004132 TIntermTyped *foldedNode = node->fold(mDiagnostics);
Olli Etuaho3fdec912016-08-18 15:08:06 +03004133 if (foldedNode)
4134 return foldedNode;
4135
4136 return node;
Olli Etuahofc1806e2015-03-17 13:03:11 +02004137}
4138
Jamie Madillb98c3a82015-07-23 14:26:04 -04004139TIntermTyped *TParseContext::addBinaryMath(TOperator op,
4140 TIntermTyped *left,
4141 TIntermTyped *right,
4142 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004143{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004144 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004145 if (node == 0)
4146 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004147 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4148 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02004149 return left;
4150 }
4151 return node;
4152}
4153
Jamie Madillb98c3a82015-07-23 14:26:04 -04004154TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
4155 TIntermTyped *left,
4156 TIntermTyped *right,
4157 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004158{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004159 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004160 if (node == 0)
4161 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004162 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4163 right->getCompleteString());
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004164 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02004165 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004166 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
4167 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004168 }
4169 return node;
4170}
4171
Olli Etuaho13389b62016-10-16 11:48:18 +01004172TIntermBinary *TParseContext::createAssign(TOperator op,
4173 TIntermTyped *left,
4174 TIntermTyped *right,
4175 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004176{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004177 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02004178 {
Olli Etuaho1dded802016-08-18 18:13:13 +03004179 if (op == EOpMulAssign)
4180 {
4181 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
4182 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4183 {
4184 return nullptr;
4185 }
4186 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03004187 TIntermBinary *node = new TIntermBinary(op, left, right);
4188 node->setLine(loc);
4189
Olli Etuaho3fdec912016-08-18 15:08:06 +03004190 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02004191 }
4192 return nullptr;
4193}
4194
Jamie Madillb98c3a82015-07-23 14:26:04 -04004195TIntermTyped *TParseContext::addAssign(TOperator op,
4196 TIntermTyped *left,
4197 TIntermTyped *right,
4198 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004199{
4200 TIntermTyped *node = createAssign(op, left, right, loc);
4201 if (node == nullptr)
4202 {
4203 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02004204 return left;
4205 }
4206 return node;
4207}
4208
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004209TIntermTyped *TParseContext::addComma(TIntermTyped *left,
4210 TIntermTyped *right,
4211 const TSourceLoc &loc)
4212{
Corentin Wallez0d959252016-07-12 17:26:32 -04004213 // WebGL2 section 5.26, the following results in an error:
4214 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004215 if (mShaderSpec == SH_WEBGL2_SPEC &&
4216 (left->isArray() || left->getBasicType() == EbtVoid ||
4217 left->getType().isStructureContainingArrays() || right->isArray() ||
4218 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04004219 {
4220 error(loc,
4221 "sequence operator is not allowed for void, arrays, or structs containing arrays",
4222 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04004223 }
4224
Olli Etuaho4db7ded2016-10-13 12:23:11 +01004225 return TIntermediate::AddComma(left, right, loc, mShaderVersion);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004226}
4227
Olli Etuaho49300862015-02-20 14:54:49 +02004228TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
4229{
4230 switch (op)
4231 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004232 case EOpContinue:
4233 if (mLoopNestingLevel <= 0)
4234 {
4235 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004236 }
4237 break;
4238 case EOpBreak:
4239 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
4240 {
4241 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004242 }
4243 break;
4244 case EOpReturn:
4245 if (mCurrentFunctionType->getBasicType() != EbtVoid)
4246 {
4247 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004248 }
4249 break;
4250 default:
4251 // No checks for discard
4252 break;
Olli Etuaho49300862015-02-20 14:54:49 +02004253 }
4254 return intermediate.addBranch(op, loc);
4255}
4256
Jamie Madillb98c3a82015-07-23 14:26:04 -04004257TIntermBranch *TParseContext::addBranch(TOperator op,
4258 TIntermTyped *returnValue,
4259 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02004260{
4261 ASSERT(op == EOpReturn);
4262 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004263 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02004264 {
4265 error(loc, "void function cannot return a value", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02004266 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004267 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02004268 {
4269 error(loc, "function return is not matching type:", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02004270 }
4271 return intermediate.addBranch(op, returnValue, loc);
4272}
4273
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004274void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
4275{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004276 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01004277 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004278 TIntermNode *offset = nullptr;
4279 TIntermSequence *arguments = functionCall->getSequence();
4280 if (name.compare(0, 16, "texelFetchOffset") == 0 ||
4281 name.compare(0, 16, "textureLodOffset") == 0 ||
4282 name.compare(0, 20, "textureProjLodOffset") == 0 ||
4283 name.compare(0, 17, "textureGradOffset") == 0 ||
4284 name.compare(0, 21, "textureProjGradOffset") == 0)
4285 {
4286 offset = arguments->back();
4287 }
4288 else if (name.compare(0, 13, "textureOffset") == 0 ||
4289 name.compare(0, 17, "textureProjOffset") == 0)
4290 {
4291 // A bias parameter might follow the offset parameter.
4292 ASSERT(arguments->size() >= 3);
4293 offset = (*arguments)[2];
4294 }
4295 if (offset != nullptr)
4296 {
4297 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
4298 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
4299 {
4300 TString unmangledName = TFunction::unmangleName(name);
4301 error(functionCall->getLine(), "Texture offset must be a constant expression",
4302 unmangledName.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004303 }
4304 else
4305 {
4306 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
4307 size_t size = offsetConstantUnion->getType().getObjectSize();
4308 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
4309 for (size_t i = 0u; i < size; ++i)
4310 {
4311 int offsetValue = values[i].getIConst();
4312 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
4313 {
4314 std::stringstream tokenStream;
4315 tokenStream << offsetValue;
4316 std::string token = tokenStream.str();
4317 error(offset->getLine(), "Texture offset value out of valid range",
4318 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004319 }
4320 }
4321 }
4322 }
4323}
4324
Martin Radev2cc85b32016-08-05 16:22:53 +03004325// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
4326void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
4327{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004328 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03004329 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
4330
4331 if (name.compare(0, 5, "image") == 0)
4332 {
4333 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00004334 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03004335
Olli Etuaho485eefd2017-02-14 17:40:06 +00004336 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03004337
4338 if (name.compare(5, 5, "Store") == 0)
4339 {
4340 if (memoryQualifier.readonly)
4341 {
4342 error(imageNode->getLine(),
4343 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004344 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03004345 }
4346 }
4347 else if (name.compare(5, 4, "Load") == 0)
4348 {
4349 if (memoryQualifier.writeonly)
4350 {
4351 error(imageNode->getLine(),
4352 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004353 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03004354 }
4355 }
4356 }
4357}
4358
4359// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
4360void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
4361 const TFunction *functionDefinition,
4362 const TIntermAggregate *functionCall)
4363{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004364 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03004365
4366 const TIntermSequence &arguments = *functionCall->getSequence();
4367
4368 ASSERT(functionDefinition->getParamCount() == arguments.size());
4369
4370 for (size_t i = 0; i < arguments.size(); ++i)
4371 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00004372 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
4373 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03004374 const TType &functionParameterType = *functionDefinition->getParam(i).type;
4375 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
4376
4377 if (IsImage(functionArgumentType.getBasicType()))
4378 {
4379 const TMemoryQualifier &functionArgumentMemoryQualifier =
4380 functionArgumentType.getMemoryQualifier();
4381 const TMemoryQualifier &functionParameterMemoryQualifier =
4382 functionParameterType.getMemoryQualifier();
4383 if (functionArgumentMemoryQualifier.readonly &&
4384 !functionParameterMemoryQualifier.readonly)
4385 {
4386 error(functionCall->getLine(),
4387 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004388 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03004389 }
4390
4391 if (functionArgumentMemoryQualifier.writeonly &&
4392 !functionParameterMemoryQualifier.writeonly)
4393 {
4394 error(functionCall->getLine(),
4395 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004396 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03004397 }
Martin Radev049edfa2016-11-11 14:35:37 +02004398
4399 if (functionArgumentMemoryQualifier.coherent &&
4400 !functionParameterMemoryQualifier.coherent)
4401 {
4402 error(functionCall->getLine(),
4403 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004404 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02004405 }
4406
4407 if (functionArgumentMemoryQualifier.volatileQualifier &&
4408 !functionParameterMemoryQualifier.volatileQualifier)
4409 {
4410 error(functionCall->getLine(),
4411 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004412 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02004413 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004414 }
4415 }
4416}
4417
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004418TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004419{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004420 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00004421}
4422
4423TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004424 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00004425 TIntermNode *thisNode,
4426 const TSourceLoc &loc)
4427{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004428 if (thisNode != nullptr)
4429 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004430 return addMethod(fnCall, arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004431 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004432
4433 TOperator op = fnCall->getBuiltInOp();
4434 if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004435 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004436 return addConstructor(arguments, op, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004437 }
4438 else
4439 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004440 return addNonConstructorFunctionCall(fnCall, arguments, loc);
4441 }
4442}
4443
4444TIntermTyped *TParseContext::addMethod(TFunction *fnCall,
4445 TIntermSequence *arguments,
4446 TIntermNode *thisNode,
4447 const TSourceLoc &loc)
4448{
4449 TConstantUnion *unionArray = new TConstantUnion[1];
4450 int arraySize = 0;
4451 TIntermTyped *typedThis = thisNode->getAsTyped();
4452 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
4453 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
4454 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
4455 // So accessing fnCall->getName() below is safe.
4456 if (fnCall->getName() != "length")
4457 {
4458 error(loc, "invalid method", fnCall->getName().c_str());
4459 }
4460 else if (!arguments->empty())
4461 {
4462 error(loc, "method takes no parameters", "length");
4463 }
4464 else if (typedThis == nullptr || !typedThis->isArray())
4465 {
4466 error(loc, "length can only be called on arrays", "length");
4467 }
4468 else
4469 {
4470 arraySize = typedThis->getArraySize();
4471 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuaho72d10202017-01-19 15:58:30 +00004472 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004473 // This code path can be hit with expressions like these:
4474 // (a = b).length()
4475 // (func()).length()
4476 // (int[3](0, 1, 2)).length()
4477 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
4478 // expression.
4479 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
4480 // spec section 5.9 which allows "An array, vector or matrix expression with the
4481 // length method applied".
4482 error(loc, "length can only be called on array names, not on array expressions",
4483 "length");
Olli Etuaho72d10202017-01-19 15:58:30 +00004484 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004485 }
4486 unionArray->setIConst(arraySize);
4487 return intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
4488}
4489
4490TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunction *fnCall,
4491 TIntermSequence *arguments,
4492 const TSourceLoc &loc)
4493{
4494 // First find by unmangled name to check whether the function name has been
4495 // hidden by a variable name or struct typename.
4496 // If a function is found, check for one with a matching argument list.
4497 bool builtIn;
4498 const TSymbol *symbol = symbolTable.find(fnCall->getName(), mShaderVersion, &builtIn);
4499 if (symbol != nullptr && !symbol->isFunction())
4500 {
4501 error(loc, "function name expected", fnCall->getName().c_str());
4502 }
4503 else
4504 {
4505 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(fnCall->getName(), *arguments),
4506 mShaderVersion, &builtIn);
4507 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004508 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004509 error(loc, "no matching overloaded function found", fnCall->getName().c_str());
4510 }
4511 else
4512 {
4513 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004514 //
4515 // A declared function.
4516 //
Olli Etuaho383b7912016-08-05 11:22:59 +03004517 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004518 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004519 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004520 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004521 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004522 if (builtIn && op != EOpNull)
4523 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004524 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004525 if (fnCandidate->getParamCount() == 1)
4526 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004527 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004528 TIntermNode *unaryParamNode = arguments->front();
4529 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004530 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004531 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004532 }
4533 else
4534 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004535 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00004536 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004537 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004538
4539 // Some built-in functions have out parameters too.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004540 functionCallLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05304541
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004542 // See if we can constant fold a built-in. Note that this may be possible even
4543 // if it is not const-qualified.
Olli Etuahof119a262016-08-19 15:54:22 +03004544 TIntermTyped *foldedNode =
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004545 intermediate.foldAggregateBuiltIn(callNode, mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05304546 if (foldedNode)
4547 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004548 return foldedNode;
Arun Patole274f0702015-05-05 13:33:30 +05304549 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004550 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004551 }
4552 }
4553 else
4554 {
4555 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004556 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004557
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004558 // If builtIn == false, the function is user defined - could be an overloaded
4559 // built-in as well.
4560 // if builtIn == true, it's a builtIn function with no op associated with it.
4561 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004562 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004563 {
Olli Etuahofe486322017-03-21 09:30:54 +00004564 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004565 checkTextureOffsetConst(callNode);
4566 checkImageMemoryAccessForBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03004567 }
4568 else
4569 {
Olli Etuahofe486322017-03-21 09:30:54 +00004570 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004571 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004572 }
4573
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004574 functionCallLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004575
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004576 callNode->setLine(loc);
4577
4578 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004579 }
4580 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004581 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004582
4583 // Error message was already written. Put on a dummy node for error recovery.
4584 return TIntermTyped::CreateZero(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004585}
4586
Jamie Madillb98c3a82015-07-23 14:26:04 -04004587TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004588 TIntermTyped *trueExpression,
4589 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03004590 const TSourceLoc &loc)
4591{
Olli Etuaho856c4972016-08-08 11:38:39 +03004592 checkIsScalarBool(loc, cond);
Olli Etuaho52901742015-04-15 13:42:45 +03004593
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004594 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03004595 {
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004596 binaryOpError(loc, ":", trueExpression->getCompleteString(),
4597 falseExpression->getCompleteString());
4598 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03004599 }
Olli Etuahode318b22016-10-25 16:18:25 +01004600 if (IsOpaqueType(trueExpression->getBasicType()))
4601 {
4602 // ESSL 1.00 section 4.1.7
4603 // ESSL 3.00 section 4.1.7
4604 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
4605 // Note that structs containing opaque types don't need to be checked as structs are
4606 // forbidden below.
4607 error(loc, "ternary operator is not allowed for opaque types", ":");
4608 return falseExpression;
4609 }
4610
Olli Etuahoa2d53032015-04-15 14:14:44 +03004611 // ESSL1 sections 5.2 and 5.7:
4612 // ESSL3 section 5.7:
4613 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004614 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03004615 {
4616 error(loc, "ternary operator is not allowed for structures or arrays", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004617 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03004618 }
Corentin Wallez0d959252016-07-12 17:26:32 -04004619 // WebGL2 section 5.26, the following results in an error:
4620 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004621 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04004622 {
4623 error(loc, "ternary operator is not allowed for void", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004624 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04004625 }
4626
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004627 return TIntermediate::AddTernarySelection(cond, trueExpression, falseExpression, loc);
Olli Etuaho52901742015-04-15 13:42:45 +03004628}
Olli Etuaho49300862015-02-20 14:54:49 +02004629
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004630//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004631// Parse an array of strings using yyparse.
4632//
4633// Returns 0 for success.
4634//
Jamie Madillb98c3a82015-07-23 14:26:04 -04004635int PaParseStrings(size_t count,
4636 const char *const string[],
4637 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05304638 TParseContext *context)
4639{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004640 if ((count == 0) || (string == NULL))
4641 return 1;
4642
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004643 if (glslang_initialize(context))
4644 return 1;
4645
alokp@chromium.org408c45e2012-04-05 15:54:43 +00004646 int error = glslang_scan(count, string, length, context);
4647 if (!error)
4648 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004649
alokp@chromium.org73bc2982012-06-19 18:48:05 +00004650 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00004651
alokp@chromium.org6b495712012-06-29 00:06:58 +00004652 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004653}
Jamie Madill45bcc782016-11-07 13:58:48 -05004654
4655} // namespace sh