blob: 47325d4c79db5b697e0cd24df6d0134c9b94f943 [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
1191 // check for layout qualifier issues
1192 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
1193
1194 if (layoutQualifier.matrixPacking != EmpUnspecified)
1195 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001196 error(identifierLocation, "layout qualifier only valid for interface blocks",
1197 getMatrixPackingString(layoutQualifier.matrixPacking));
Olli Etuaho383b7912016-08-05 11:22:59 +03001198 return;
Jamie Madilla5efff92013-06-06 11:56:47 -04001199 }
1200
1201 if (layoutQualifier.blockStorage != EbsUnspecified)
1202 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001203 error(identifierLocation, "layout qualifier only valid for interface blocks",
1204 getBlockStorageString(layoutQualifier.blockStorage));
Olli Etuaho383b7912016-08-05 11:22:59 +03001205 return;
Jamie Madilla5efff92013-06-06 11:56:47 -04001206 }
1207
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001208 bool canHaveLocation =
1209 publicType.qualifier == EvqVertexIn || publicType.qualifier == EvqFragmentOut;
1210
1211 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1212 {
1213 canHaveLocation = true;
1214
1215 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1216 // But invalid shaders may still reach here with an unsized array declaration.
1217 if (!publicType.isUnsizedArray())
1218 {
1219 TType type(publicType);
1220 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1221 publicType.layoutQualifier);
1222 }
1223 }
1224 if (!canHaveLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001225 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001226 checkLocationIsNotSpecified(identifierLocation, publicType.layoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04001227 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001228
1229 if (IsImage(publicType.getBasicType()))
1230 {
1231
1232 switch (layoutQualifier.imageInternalFormat)
1233 {
1234 case EiifRGBA32F:
1235 case EiifRGBA16F:
1236 case EiifR32F:
1237 case EiifRGBA8:
1238 case EiifRGBA8_SNORM:
1239 if (!IsFloatImage(publicType.getBasicType()))
1240 {
1241 error(identifierLocation,
1242 "internal image format requires a floating image type",
1243 getBasicString(publicType.getBasicType()));
1244 return;
1245 }
1246 break;
1247 case EiifRGBA32I:
1248 case EiifRGBA16I:
1249 case EiifRGBA8I:
1250 case EiifR32I:
1251 if (!IsIntegerImage(publicType.getBasicType()))
1252 {
1253 error(identifierLocation,
1254 "internal image format requires an integer image type",
1255 getBasicString(publicType.getBasicType()));
1256 return;
1257 }
1258 break;
1259 case EiifRGBA32UI:
1260 case EiifRGBA16UI:
1261 case EiifRGBA8UI:
1262 case EiifR32UI:
1263 if (!IsUnsignedImage(publicType.getBasicType()))
1264 {
1265 error(identifierLocation,
1266 "internal image format requires an unsigned image type",
1267 getBasicString(publicType.getBasicType()));
1268 return;
1269 }
1270 break;
1271 case EiifUnspecified:
1272 error(identifierLocation, "layout qualifier", "No image internal format specified");
1273 return;
1274 default:
1275 error(identifierLocation, "layout qualifier", "unrecognized token");
1276 return;
1277 }
1278
1279 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1280 switch (layoutQualifier.imageInternalFormat)
1281 {
1282 case EiifR32F:
1283 case EiifR32I:
1284 case EiifR32UI:
1285 break;
1286 default:
1287 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1288 {
1289 error(identifierLocation, "layout qualifier",
1290 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1291 "image variables must be qualified readonly and/or writeonly");
1292 return;
1293 }
1294 break;
1295 }
1296 }
1297 else
1298 {
Olli Etuaho43364892017-02-13 16:00:12 +00001299 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Martin Radev2cc85b32016-08-05 16:22:53 +03001300
Olli Etuaho43364892017-02-13 16:00:12 +00001301 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1302 }
1303}
Martin Radev2cc85b32016-08-05 16:22:53 +03001304
Olli Etuaho43364892017-02-13 16:00:12 +00001305void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1306{
1307 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
1308 int arraySize = type.isArray() ? type.getArraySize() : 1;
1309 if (IsImage(type.getBasicType()))
1310 {
1311 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding, arraySize);
1312 }
1313 else if (IsSampler(type.getBasicType()))
1314 {
1315 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding, arraySize);
1316 }
1317 else
1318 {
1319 ASSERT(!IsOpaqueType(type.getBasicType()));
1320 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001321 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001322}
1323
Olli Etuaho856c4972016-08-08 11:38:39 +03001324void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1325 const TString &layoutQualifierName,
1326 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001327{
1328
1329 if (mShaderVersion < versionRequired)
1330 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001331 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001332 }
1333}
1334
Olli Etuaho856c4972016-08-08 11:38:39 +03001335bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1336 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001337{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001338 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001339 for (size_t i = 0u; i < localSize.size(); ++i)
1340 {
1341 if (localSize[i] != -1)
1342 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001343 error(location,
1344 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1345 "global layout declaration",
1346 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001347 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001348 }
1349 }
1350
Olli Etuaho8a176262016-08-16 14:23:01 +03001351 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001352}
1353
Olli Etuaho43364892017-02-13 16:00:12 +00001354void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001355 TLayoutImageInternalFormat internalFormat)
1356{
1357 if (internalFormat != EiifUnspecified)
1358 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001359 error(location, "invalid layout qualifier: only valid when used with images",
1360 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001361 }
Olli Etuaho43364892017-02-13 16:00:12 +00001362}
1363
1364void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1365{
1366 if (binding != -1)
1367 {
1368 error(location,
1369 "invalid layout qualifier: only valid when used with opaque types or blocks",
1370 "binding");
1371 }
1372}
1373
1374void TParseContext::checkImageBindingIsValid(const TSourceLoc &location, int binding, int arraySize)
1375{
1376 // Expects arraySize to be 1 when setting binding for only a single variable.
1377 if (binding >= 0 && binding + arraySize > mMaxImageUnits)
1378 {
1379 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1380 }
1381}
1382
1383void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1384 int binding,
1385 int arraySize)
1386{
1387 // Expects arraySize to be 1 when setting binding for only a single variable.
1388 if (binding >= 0 && binding + arraySize > mMaxCombinedTextureImageUnits)
1389 {
1390 error(location, "sampler binding greater than maximum texture units", "binding");
1391 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001392}
1393
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001394void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1395 int objectLocationCount,
1396 const TLayoutQualifier &layoutQualifier)
1397{
1398 int loc = layoutQualifier.location;
1399 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1400 {
1401 error(location, "Uniform location out of range", "location");
1402 }
1403}
1404
Olli Etuaho383b7912016-08-05 11:22:59 +03001405void TParseContext::functionCallLValueErrorCheck(const TFunction *fnCandidate,
Olli Etuaho856c4972016-08-08 11:38:39 +03001406 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001407{
1408 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1409 {
1410 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
1411 if (qual == EvqOut || qual == EvqInOut)
1412 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001413 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
Olli Etuaho8a176262016-08-16 14:23:01 +03001414 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001415 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001416 TString unmangledName =
1417 TFunction::unmangleName(fnCall->getFunctionSymbolInfo()->getName());
Olli Etuaho856c4972016-08-08 11:38:39 +03001418 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001419 "Constant value cannot be passed for 'out' or 'inout' parameters.",
1420 unmangledName.c_str());
Olli Etuaho383b7912016-08-05 11:22:59 +03001421 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001422 }
1423 }
1424 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001425}
1426
Martin Radev70866b82016-07-22 15:27:42 +03001427void TParseContext::checkInvariantVariableQualifier(bool invariant,
1428 const TQualifier qualifier,
1429 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001430{
Martin Radev70866b82016-07-22 15:27:42 +03001431 if (!invariant)
1432 return;
1433
1434 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001435 {
Martin Radev70866b82016-07-22 15:27:42 +03001436 // input variables in the fragment shader can be also qualified as invariant
1437 if (!sh::CanBeInvariantESSL1(qualifier))
1438 {
1439 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1440 }
1441 }
1442 else
1443 {
1444 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1445 {
1446 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1447 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001448 }
1449}
1450
Arun Patole7e7e68d2015-05-22 12:02:25 +05301451bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001452{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001453 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001454 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1455 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001456}
1457
Arun Patole7e7e68d2015-05-22 12:02:25 +05301458bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001459{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001460 return ::IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001461}
1462
Jamie Madillb98c3a82015-07-23 14:26:04 -04001463void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1464 const char *extName,
1465 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001466{
1467 pp::SourceLocation srcLoc;
1468 srcLoc.file = loc.first_file;
1469 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001470 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001471}
1472
Jamie Madillb98c3a82015-07-23 14:26:04 -04001473void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1474 const char *name,
1475 const char *value,
1476 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001477{
1478 pp::SourceLocation srcLoc;
1479 srcLoc.file = loc.first_file;
1480 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001481 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001482}
1483
Martin Radev4c4c8e72016-08-04 12:25:34 +03001484sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001485{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001486 sh::WorkGroupSize result;
Martin Radev802abe02016-08-04 17:48:32 +03001487 for (size_t i = 0u; i < result.size(); ++i)
1488 {
1489 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1490 {
1491 result[i] = 1;
1492 }
1493 else
1494 {
1495 result[i] = mComputeShaderLocalSize[i];
1496 }
1497 }
1498 return result;
1499}
1500
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001501/////////////////////////////////////////////////////////////////////////////////
1502//
1503// Non-Errors.
1504//
1505/////////////////////////////////////////////////////////////////////////////////
1506
Jamie Madill5c097022014-08-20 16:38:32 -04001507const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1508 const TString *name,
1509 const TSymbol *symbol)
1510{
1511 const TVariable *variable = NULL;
1512
1513 if (!symbol)
1514 {
1515 error(location, "undeclared identifier", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001516 }
1517 else if (!symbol->isVariable())
1518 {
1519 error(location, "variable expected", name->c_str());
Jamie Madill5c097022014-08-20 16:38:32 -04001520 }
1521 else
1522 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001523 variable = static_cast<const TVariable *>(symbol);
Jamie Madill5c097022014-08-20 16:38:32 -04001524
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001525 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
Olli Etuaho383b7912016-08-05 11:22:59 +03001526 !variable->getExtension().empty())
Jamie Madill5c097022014-08-20 16:38:32 -04001527 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001528 checkCanUseExtension(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001529 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001530
1531 // Reject shaders using both gl_FragData and gl_FragColor
1532 TQualifier qualifier = variable->getType().getQualifier();
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001533 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001534 {
1535 mUsesFragData = true;
1536 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001537 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
Jamie Madill14e95b32015-05-07 10:10:41 -04001538 {
1539 mUsesFragColor = true;
1540 }
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001541 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1542 {
1543 mUsesSecondaryOutputs = true;
1544 }
Jamie Madill14e95b32015-05-07 10:10:41 -04001545
1546 // This validation is not quite correct - it's only an error to write to
1547 // both FragData and FragColor. For simplicity, and because users shouldn't
1548 // be rewarded for reading from undefined varaibles, return an error
1549 // if they are both referenced, rather than assigned.
1550 if (mUsesFragData && mUsesFragColor)
1551 {
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001552 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1553 if (mUsesSecondaryOutputs)
1554 {
1555 errorMessage =
1556 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1557 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1558 }
1559 error(location, errorMessage, name->c_str());
Jamie Madill14e95b32015-05-07 10:10:41 -04001560 }
Martin Radevb0883602016-08-04 17:48:58 +03001561
1562 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1563 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1564 qualifier == EvqWorkGroupSize)
1565 {
1566 error(location,
1567 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1568 "gl_WorkGroupSize");
1569 }
Jamie Madill5c097022014-08-20 16:38:32 -04001570 }
1571
1572 if (!variable)
1573 {
1574 TType type(EbtFloat, EbpUndefined);
1575 TVariable *fakeVariable = new TVariable(name, type);
1576 symbolTable.declare(fakeVariable);
1577 variable = fakeVariable;
1578 }
1579
1580 return variable;
1581}
1582
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001583TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1584 const TString *name,
1585 const TSymbol *symbol)
1586{
1587 const TVariable *variable = getNamedVariable(location, name, symbol);
1588
Olli Etuaho09b04a22016-12-15 13:30:26 +00001589 if (variable->getType().getQualifier() == EvqViewIDOVR && IsWebGLBasedSpec(mShaderSpec) &&
1590 mShaderType == GL_FRAGMENT_SHADER && !isExtensionEnabled("GL_OVR_multiview2"))
1591 {
1592 // WEBGL_multiview spec
1593 error(location, "Need to enable OVR_multiview2 to use gl_ViewID_OVR in fragment shader",
1594 "gl_ViewID_OVR");
1595 }
1596
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001597 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001598 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001599 const TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001600 return intermediate.addConstantUnion(constArray, variable->getType(), location);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001601 }
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001602 else if (variable->getType().getQualifier() == EvqWorkGroupSize &&
1603 mComputeShaderLocalSizeDeclared)
1604 {
1605 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1606 // needs to be added to the AST as a constant and not as a symbol.
1607 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1608 TConstantUnion *constArray = new TConstantUnion[3];
1609 for (size_t i = 0; i < 3; ++i)
1610 {
1611 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1612 }
1613
1614 ASSERT(variable->getType().getBasicType() == EbtUInt);
1615 ASSERT(variable->getType().getObjectSize() == 3);
1616
1617 TType type(variable->getType());
1618 type.setQualifier(EvqConst);
1619 return intermediate.addConstantUnion(constArray, type, location);
1620 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001621 else
1622 {
1623 return intermediate.addSymbol(variable->getUniqueId(), variable->getName(),
1624 variable->getType(), location);
1625 }
1626}
1627
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001628//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001629// Initializers show up in several places in the grammar. Have one set of
1630// code to handle them here.
1631//
Jamie Madill3b5c2da2014-08-19 15:23:32 -04001632// Returns true on error, false if no error
1633//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001634bool TParseContext::executeInitializer(const TSourceLoc &line,
1635 const TString &identifier,
1636 const TPublicType &pType,
1637 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001638 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001639{
Olli Etuaho13389b62016-10-16 11:48:18 +01001640 ASSERT(initNode != nullptr);
1641 ASSERT(*initNode == nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001642 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001643
Olli Etuaho2935c582015-04-08 14:32:06 +03001644 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001645 if (type.isUnsizedArray())
1646 {
Olli Etuaho02bd82c2016-11-03 10:29:43 +00001647 // We have not checked yet whether the initializer actually is an array or not.
1648 if (initializer->isArray())
1649 {
1650 type.setArraySize(initializer->getArraySize());
1651 }
1652 else
1653 {
1654 // Having a non-array initializer for an unsized array will result in an error later,
1655 // so we don't generate an error message here.
1656 type.setArraySize(1u);
1657 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001658 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001659 if (!declareVariable(line, identifier, type, &variable))
1660 {
1661 return true;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001662 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001663
Olli Etuahob0c645e2015-05-12 14:25:36 +03001664 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001665 if (symbolTable.atGlobalLevel() &&
1666 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001667 {
1668 // Error message does not completely match behavior with ESSL 1.00, but
1669 // we want to steer developers towards only using constant expressions.
1670 error(line, "global variable initializers must be constant expressions", "=");
1671 return true;
1672 }
1673 if (globalInitWarning)
1674 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001675 warning(
1676 line,
1677 "global variable initializers should be constant expressions "
1678 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1679 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001680 }
1681
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001682 //
1683 // identifier must be of type constant, a global, or a temporary
1684 //
1685 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301686 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1687 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001688 error(line, " cannot initialize this type of qualifier ",
1689 variable->getType().getQualifierString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001690 return true;
1691 }
1692 //
1693 // test for and propagate constant
1694 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001695
Arun Patole7e7e68d2015-05-22 12:02:25 +05301696 if (qualifier == EvqConst)
1697 {
1698 if (qualifier != initializer->getType().getQualifier())
1699 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001700 std::stringstream reasonStream;
1701 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1702 << "'";
1703 std::string reason = reasonStream.str();
1704 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001705 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001706 return true;
1707 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301708 if (type != initializer->getType())
1709 {
1710 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001711 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001712 variable->getType().setQualifier(EvqTemporary);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001713 return true;
1714 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001715
1716 // Save the constant folded value to the variable if possible. For example array
1717 // initializers are not folded, since that way copying the array literal to multiple places
1718 // in the shader is avoided.
1719 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1720 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301721 if (initializer->getAsConstantUnion())
1722 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001723 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho13389b62016-10-16 11:48:18 +01001724 *initNode = nullptr;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001725 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301726 }
1727 else if (initializer->getAsSymbolNode())
1728 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001729 const TSymbol *symbol =
1730 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1731 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001732
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001733 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001734 if (constArray)
1735 {
1736 variable->shareConstPointer(constArray);
Olli Etuaho13389b62016-10-16 11:48:18 +01001737 *initNode = nullptr;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001738 return false;
1739 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001740 }
1741 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001742
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001743 TIntermSymbol *intermSymbol = intermediate.addSymbol(
1744 variable->getUniqueId(), variable->getName(), variable->getType(), line);
Olli Etuaho13389b62016-10-16 11:48:18 +01001745 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1746 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001747 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001748 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
1749 return true;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001750 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001751
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001752 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001753}
1754
Olli Etuaho0e3aee32016-10-27 12:56:38 +01001755void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
1756{
1757 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
1758 typeSpecifier->getBasicType());
1759
1760 if (mShaderVersion < 300 && typeSpecifier->array)
1761 {
1762 error(typeSpecifier->getLine(), "not supported", "first-class array");
1763 typeSpecifier->clearArrayness();
1764 }
1765}
1766
Martin Radev70866b82016-07-22 15:27:42 +03001767TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05301768 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001769{
Olli Etuaho77ba4082016-12-16 12:01:18 +00001770 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001771
Martin Radev70866b82016-07-22 15:27:42 +03001772 TPublicType returnType = typeSpecifier;
1773 returnType.qualifier = typeQualifier.qualifier;
1774 returnType.invariant = typeQualifier.invariant;
1775 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03001776 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03001777 returnType.precision = typeSpecifier.precision;
1778
1779 if (typeQualifier.precision != EbpUndefined)
1780 {
1781 returnType.precision = typeQualifier.precision;
1782 }
1783
Martin Radev4a9cd802016-09-01 16:51:51 +03001784 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
1785 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03001786
Martin Radev4a9cd802016-09-01 16:51:51 +03001787 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
1788 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03001789
Martin Radev4a9cd802016-09-01 16:51:51 +03001790 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03001791
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001792 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001793 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001794 if (typeSpecifier.array)
1795 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001796 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03001797 returnType.clearArrayness();
1798 }
1799
Martin Radev70866b82016-07-22 15:27:42 +03001800 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001801 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001802 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001803 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001804 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001805 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001806
Martin Radev70866b82016-07-22 15:27:42 +03001807 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03001808 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001809 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001810 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03001811 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001812 }
1813 }
1814 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001815 {
Martin Radev70866b82016-07-22 15:27:42 +03001816 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03001817 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001818 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03001819 }
Martin Radev70866b82016-07-22 15:27:42 +03001820 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
1821 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001822 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001823 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
1824 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00001825 }
Martin Radev70866b82016-07-22 15:27:42 +03001826 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03001827 {
Martin Radev4a9cd802016-09-01 16:51:51 +03001828 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03001829 "in");
Martin Radev802abe02016-08-04 17:48:32 +03001830 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00001831 }
1832
1833 return returnType;
1834}
1835
Olli Etuaho856c4972016-08-08 11:38:39 +03001836void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
1837 const TPublicType &type,
1838 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03001839{
1840 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03001841 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03001842 {
1843 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001844 }
1845
1846 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
1847 switch (qualifier)
1848 {
1849 case EvqVertexIn:
1850 // ESSL 3.00 section 4.3.4
1851 if (type.array)
1852 {
1853 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001854 }
1855 // Vertex inputs with a struct type are disallowed in singleDeclarationErrorCheck
1856 return;
1857 case EvqFragmentOut:
1858 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03001859 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03001860 {
1861 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001862 }
1863 // Fragment outputs with a struct type are disallowed in singleDeclarationErrorCheck
1864 return;
1865 default:
1866 break;
1867 }
1868
1869 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
1870 // restrictions.
1871 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03001872 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
1873 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03001874 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
1875 {
1876 error(qualifierLocation, "must use 'flat' interpolation here",
1877 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001878 }
1879
Martin Radev4a9cd802016-09-01 16:51:51 +03001880 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03001881 {
1882 // ESSL 3.00 sections 4.3.4 and 4.3.6.
1883 // These restrictions are only implied by the ESSL 3.00 spec, but
1884 // the ESSL 3.10 spec lists these restrictions explicitly.
1885 if (type.array)
1886 {
1887 error(qualifierLocation, "cannot be an array of structures",
1888 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001889 }
1890 if (type.isStructureContainingArrays())
1891 {
1892 error(qualifierLocation, "cannot be a structure containing an array",
1893 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001894 }
1895 if (type.isStructureContainingType(EbtStruct))
1896 {
1897 error(qualifierLocation, "cannot be a structure containing a structure",
1898 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001899 }
1900 if (type.isStructureContainingType(EbtBool))
1901 {
1902 error(qualifierLocation, "cannot be a structure containing a bool",
1903 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03001904 }
1905 }
1906}
1907
Martin Radev2cc85b32016-08-05 16:22:53 +03001908void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
1909{
1910 if (qualifier.getType() == QtStorage)
1911 {
1912 const TStorageQualifierWrapper &storageQualifier =
1913 static_cast<const TStorageQualifierWrapper &>(qualifier);
1914 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
1915 !symbolTable.atGlobalLevel())
1916 {
1917 error(storageQualifier.getLine(),
1918 "Local variables can only use the const storage qualifier.",
1919 storageQualifier.getQualifierString().c_str());
1920 }
1921 }
1922}
1923
Olli Etuaho43364892017-02-13 16:00:12 +00001924void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03001925 const TSourceLoc &location)
1926{
1927 if (memoryQualifier.readonly)
1928 {
1929 error(location, "Only allowed with images.", "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03001930 }
1931 if (memoryQualifier.writeonly)
1932 {
1933 error(location, "Only allowed with images.", "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03001934 }
Martin Radev049edfa2016-11-11 14:35:37 +02001935 if (memoryQualifier.coherent)
1936 {
1937 error(location, "Only allowed with images.", "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02001938 }
1939 if (memoryQualifier.restrictQualifier)
1940 {
1941 error(location, "Only allowed with images.", "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02001942 }
1943 if (memoryQualifier.volatileQualifier)
1944 {
1945 error(location, "Only allowed with images.", "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02001946 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001947}
1948
Olli Etuaho13389b62016-10-16 11:48:18 +01001949TIntermDeclaration *TParseContext::parseSingleDeclaration(
1950 TPublicType &publicType,
1951 const TSourceLoc &identifierOrTypeLocation,
1952 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04001953{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07001954 TType type(publicType);
1955 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
1956 mDirectiveHandler.pragma().stdgl.invariantAll)
1957 {
1958 TQualifier qualifier = type.getQualifier();
1959
1960 // The directive handler has already taken care of rejecting invalid uses of this pragma
1961 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
1962 // affected variable declarations:
1963 //
1964 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
1965 // elsewhere, in TranslatorGLSL.)
1966 //
1967 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
1968 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
1969 // the way this is currently implemented we have to enable this compiler option before
1970 // parsing the shader and determining the shading language version it uses. If this were
1971 // implemented as a post-pass, the workaround could be more targeted.
1972 //
1973 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
1974 // the specification, but there are desktop OpenGL drivers that expect that this is the
1975 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
1976 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
1977 {
1978 type.setInvariant(true);
1979 }
1980 }
1981
1982 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, type, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001983
Olli Etuahobab4c082015-04-24 16:38:49 +03001984 bool emptyDeclaration = (identifier == "");
Olli Etuahofa33d582015-04-09 14:33:12 +03001985
Olli Etuahobab4c082015-04-24 16:38:49 +03001986 mDeferredSingleDeclarationErrorCheck = emptyDeclaration;
1987
Olli Etuaho13389b62016-10-16 11:48:18 +01001988 TIntermDeclaration *declaration = new TIntermDeclaration();
1989 declaration->setLine(identifierOrTypeLocation);
1990
Olli Etuahobab4c082015-04-24 16:38:49 +03001991 if (emptyDeclaration)
1992 {
Martin Radevb8b01222016-11-20 23:25:53 +02001993 emptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Olli Etuahobab4c082015-04-24 16:38:49 +03001994 }
1995 else
Jamie Madill60ed9812013-06-06 11:56:46 -04001996 {
Olli Etuaho383b7912016-08-05 11:22:59 +03001997 singleDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04001998
Olli Etuaho856c4972016-08-08 11:38:39 +03001999 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002000
Olli Etuaho2935c582015-04-08 14:32:06 +03002001 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002002 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002003
2004 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01002005 {
Jamie Madill60ed9812013-06-06 11:56:46 -04002006 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01002007 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002008 }
2009
Olli Etuaho13389b62016-10-16 11:48:18 +01002010 // We append the symbol even if the declaration is empty, mainly because of struct declarations
2011 // that may just declare a type.
2012 declaration->appendDeclarator(symbol);
2013
2014 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002015}
2016
Olli Etuaho13389b62016-10-16 11:48:18 +01002017TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
2018 const TSourceLoc &identifierLocation,
2019 const TString &identifier,
2020 const TSourceLoc &indexLocation,
2021 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04002022{
Olli Etuahofa33d582015-04-09 14:33:12 +03002023 mDeferredSingleDeclarationErrorCheck = false;
2024
Olli Etuaho383b7912016-08-05 11:22:59 +03002025 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002026
Olli Etuaho856c4972016-08-08 11:38:39 +03002027 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002028
Olli Etuaho8a176262016-08-16 14:23:01 +03002029 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002030
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002031 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002032
Olli Etuaho856c4972016-08-08 11:38:39 +03002033 unsigned int size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002034 // Make the type an array even if size check failed.
2035 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2036 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04002037
Olli Etuaho2935c582015-04-08 14:32:06 +03002038 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002039 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002040
Olli Etuaho13389b62016-10-16 11:48:18 +01002041 TIntermDeclaration *declaration = new TIntermDeclaration();
2042 declaration->setLine(identifierLocation);
2043
Olli Etuahoe7847b02015-03-16 11:56:12 +02002044 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002045 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01002046 {
Jamie Madill60ed9812013-06-06 11:56:46 -04002047 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01002048 declaration->appendDeclarator(symbol);
2049 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002050
Olli Etuaho13389b62016-10-16 11:48:18 +01002051 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002052}
2053
Olli Etuaho13389b62016-10-16 11:48:18 +01002054TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2055 const TSourceLoc &identifierLocation,
2056 const TString &identifier,
2057 const TSourceLoc &initLocation,
2058 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002059{
Olli Etuahofa33d582015-04-09 14:33:12 +03002060 mDeferredSingleDeclarationErrorCheck = false;
2061
Olli Etuaho383b7912016-08-05 11:22:59 +03002062 singleDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002063
Olli Etuaho13389b62016-10-16 11:48:18 +01002064 TIntermDeclaration *declaration = new TIntermDeclaration();
2065 declaration->setLine(identifierLocation);
2066
2067 TIntermBinary *initNode = nullptr;
2068 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002069 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002070 if (initNode)
2071 {
2072 declaration->appendDeclarator(initNode);
2073 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002074 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002075 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002076}
2077
Olli Etuaho13389b62016-10-16 11:48:18 +01002078TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Jamie Madillb98c3a82015-07-23 14:26:04 -04002079 TPublicType &publicType,
2080 const TSourceLoc &identifierLocation,
2081 const TString &identifier,
2082 const TSourceLoc &indexLocation,
2083 TIntermTyped *indexExpression,
2084 const TSourceLoc &initLocation,
2085 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002086{
2087 mDeferredSingleDeclarationErrorCheck = false;
2088
Olli Etuaho383b7912016-08-05 11:22:59 +03002089 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002090
Olli Etuaho8a176262016-08-16 14:23:01 +03002091 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002092
2093 TPublicType arrayType(publicType);
2094
Olli Etuaho856c4972016-08-08 11:38:39 +03002095 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002096 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2097 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002098 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002099 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002100 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002101 }
2102 // Make the type an array even if size check failed.
2103 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2104 arrayType.setArraySize(size);
2105
Olli Etuaho13389b62016-10-16 11:48:18 +01002106 TIntermDeclaration *declaration = new TIntermDeclaration();
2107 declaration->setLine(identifierLocation);
2108
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002109 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002110 TIntermBinary *initNode = nullptr;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002111 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
2112 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002113 if (initNode)
2114 {
2115 declaration->appendDeclarator(initNode);
2116 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002117 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002118
2119 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002120}
2121
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002122TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002123 const TTypeQualifierBuilder &typeQualifierBuilder,
2124 const TSourceLoc &identifierLoc,
2125 const TString *identifier,
2126 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002127{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002128 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002129
Martin Radev70866b82016-07-22 15:27:42 +03002130 if (!typeQualifier.invariant)
2131 {
2132 error(identifierLoc, "Expected invariant", identifier->c_str());
2133 return nullptr;
2134 }
2135 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2136 {
2137 return nullptr;
2138 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002139 if (!symbol)
2140 {
2141 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002142 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002143 }
Martin Radev70866b82016-07-22 15:27:42 +03002144 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002145 {
Martin Radev70866b82016-07-22 15:27:42 +03002146 error(identifierLoc, "invariant declaration specifies qualifier",
2147 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002148 }
Martin Radev70866b82016-07-22 15:27:42 +03002149 if (typeQualifier.precision != EbpUndefined)
2150 {
2151 error(identifierLoc, "invariant declaration specifies precision",
2152 getPrecisionString(typeQualifier.precision));
2153 }
2154 if (!typeQualifier.layoutQualifier.isEmpty())
2155 {
2156 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2157 }
2158
2159 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
2160 ASSERT(variable);
2161 const TType &type = variable->getType();
2162
2163 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2164 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002165 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002166
2167 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2168
2169 TIntermSymbol *intermSymbol =
2170 intermediate.addSymbol(variable->getUniqueId(), *identifier, type, identifierLoc);
2171
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002172 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002173}
2174
Olli Etuaho13389b62016-10-16 11:48:18 +01002175void TParseContext::parseDeclarator(TPublicType &publicType,
2176 const TSourceLoc &identifierLocation,
2177 const TString &identifier,
2178 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002179{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002180 // If the declaration starting this declarator list was empty (example: int,), some checks were
2181 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002182 if (mDeferredSingleDeclarationErrorCheck)
2183 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002184 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002185 mDeferredSingleDeclarationErrorCheck = false;
2186 }
2187
Olli Etuaho856c4972016-08-08 11:38:39 +03002188 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002189
Olli Etuaho856c4972016-08-08 11:38:39 +03002190 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002191
Olli Etuaho2935c582015-04-08 14:32:06 +03002192 TVariable *variable = nullptr;
Olli Etuaho43364892017-02-13 16:00:12 +00002193 TType type(publicType);
2194 declareVariable(identifierLocation, identifier, type, &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002195
Olli Etuaho43364892017-02-13 16:00:12 +00002196 TIntermSymbol *symbol = intermediate.addSymbol(0, identifier, type, identifierLocation);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002197 if (variable && symbol)
Olli Etuaho13389b62016-10-16 11:48:18 +01002198 {
Jamie Madill502d66f2013-06-20 11:55:52 -04002199 symbol->setId(variable->getUniqueId());
Olli Etuaho13389b62016-10-16 11:48:18 +01002200 declarationOut->appendDeclarator(symbol);
2201 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002202}
2203
Olli Etuaho13389b62016-10-16 11:48:18 +01002204void TParseContext::parseArrayDeclarator(TPublicType &publicType,
2205 const TSourceLoc &identifierLocation,
2206 const TString &identifier,
2207 const TSourceLoc &arrayLocation,
2208 TIntermTyped *indexExpression,
2209 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002210{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002211 // If the declaration starting this declarator list was empty (example: int,), some checks were
2212 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002213 if (mDeferredSingleDeclarationErrorCheck)
2214 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002215 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002216 mDeferredSingleDeclarationErrorCheck = false;
2217 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002218
Olli Etuaho856c4972016-08-08 11:38:39 +03002219 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002220
Olli Etuaho856c4972016-08-08 11:38:39 +03002221 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002222
Olli Etuaho8a176262016-08-16 14:23:01 +03002223 if (checkIsValidTypeAndQualifierForArray(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002224 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002225 TType arrayType = TType(publicType);
Olli Etuaho856c4972016-08-08 11:38:39 +03002226 unsigned int size = checkIsValidArraySize(arrayLocation, indexExpression);
Olli Etuaho693c9aa2015-04-07 17:50:36 +03002227 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002228
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002229 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002230 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002231
Jamie Madillb98c3a82015-07-23 14:26:04 -04002232 TIntermSymbol *symbol =
2233 intermediate.addSymbol(0, identifier, arrayType, identifierLocation);
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002234 if (variable && symbol)
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002235 symbol->setId(variable->getUniqueId());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002236
Olli Etuaho13389b62016-10-16 11:48:18 +01002237 declarationOut->appendDeclarator(symbol);
Jamie Madill502d66f2013-06-20 11:55:52 -04002238 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002239}
2240
Olli Etuaho13389b62016-10-16 11:48:18 +01002241void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2242 const TSourceLoc &identifierLocation,
2243 const TString &identifier,
2244 const TSourceLoc &initLocation,
2245 TIntermTyped *initializer,
2246 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002247{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002248 // If the declaration starting this declarator list was empty (example: int,), some checks were
2249 // not performed.
Olli Etuahofa33d582015-04-09 14:33:12 +03002250 if (mDeferredSingleDeclarationErrorCheck)
2251 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002252 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuahofa33d582015-04-09 14:33:12 +03002253 mDeferredSingleDeclarationErrorCheck = false;
2254 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002255
Olli Etuaho856c4972016-08-08 11:38:39 +03002256 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002257
Olli Etuaho13389b62016-10-16 11:48:18 +01002258 TIntermBinary *initNode = nullptr;
2259 if (!executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002260 {
2261 //
2262 // build the intermediate representation
2263 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002264 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002265 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002266 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002267 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002268 }
2269}
2270
Olli Etuaho13389b62016-10-16 11:48:18 +01002271void TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
2272 const TSourceLoc &identifierLocation,
2273 const TString &identifier,
2274 const TSourceLoc &indexLocation,
2275 TIntermTyped *indexExpression,
2276 const TSourceLoc &initLocation,
2277 TIntermTyped *initializer,
2278 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002279{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002280 // If the declaration starting this declarator list was empty (example: int,), some checks were
2281 // not performed.
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002282 if (mDeferredSingleDeclarationErrorCheck)
2283 {
Olli Etuaho383b7912016-08-05 11:22:59 +03002284 singleDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002285 mDeferredSingleDeclarationErrorCheck = false;
2286 }
2287
Olli Etuaho856c4972016-08-08 11:38:39 +03002288 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002289
Olli Etuaho8a176262016-08-16 14:23:01 +03002290 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002291
2292 TPublicType arrayType(publicType);
2293
Olli Etuaho856c4972016-08-08 11:38:39 +03002294 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002295 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2296 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002297 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002298 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002299 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002300 }
2301 // Make the type an array even if size check failed.
2302 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2303 arrayType.setArraySize(size);
2304
2305 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002306 TIntermBinary *initNode = nullptr;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002307 if (!executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
2308 {
2309 if (initNode)
2310 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002311 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002312 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002313 }
2314}
2315
Martin Radev70866b82016-07-22 15:27:42 +03002316void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002317{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002318 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002319 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002320
Martin Radev70866b82016-07-22 15:27:42 +03002321 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2322 typeQualifier.line);
2323
Jamie Madillc2128ff2016-07-04 10:26:17 -04002324 // It should never be the case, but some strange parser errors can send us here.
2325 if (layoutQualifier.isEmpty())
2326 {
2327 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002328 return;
2329 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002330
Martin Radev802abe02016-08-04 17:48:32 +03002331 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002332 {
Olli Etuaho43364892017-02-13 16:00:12 +00002333 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002334 return;
2335 }
2336
Olli Etuaho43364892017-02-13 16:00:12 +00002337 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2338
2339 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002340
2341 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2342
Martin Radev802abe02016-08-04 17:48:32 +03002343 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002344 {
Martin Radev802abe02016-08-04 17:48:32 +03002345 if (mComputeShaderLocalSizeDeclared &&
2346 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2347 {
2348 error(typeQualifier.line, "Work group size does not match the previous declaration",
2349 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002350 return;
2351 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002352
Martin Radev802abe02016-08-04 17:48:32 +03002353 if (mShaderVersion < 310)
2354 {
2355 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002356 return;
2357 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002358
Martin Radev4c4c8e72016-08-04 12:25:34 +03002359 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03002360 {
2361 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002362 return;
2363 }
2364
2365 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
2366 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
2367
2368 const TConstantUnion *maxComputeWorkGroupSizeData =
2369 maxComputeWorkGroupSize->getConstPointer();
2370
2371 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
2372 {
2373 if (layoutQualifier.localSize[i] != -1)
2374 {
2375 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
2376 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
2377 if (mComputeShaderLocalSize[i] < 1 ||
2378 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
2379 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002380 std::stringstream reasonStream;
2381 reasonStream << "invalid value: Value must be at least 1 and no greater than "
2382 << maxComputeWorkGroupSizeValue;
2383 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03002384
Olli Etuaho4de340a2016-12-16 09:32:03 +00002385 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03002386 return;
2387 }
2388 }
2389 }
2390
2391 mComputeShaderLocalSizeDeclared = true;
2392 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00002393 else if (mMultiviewAvailable &&
2394 (isExtensionEnabled("GL_OVR_multiview") || isExtensionEnabled("GL_OVR_multiview2")) &&
2395 typeQualifier.qualifier == EvqVertexIn)
2396 {
2397 // This error is only specified in WebGL, but tightens unspecified behavior in the native
2398 // specification.
2399 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
2400 {
2401 error(typeQualifier.line, "Number of views does not match the previous declaration",
2402 "layout");
2403 return;
2404 }
2405
2406 if (layoutQualifier.numViews == -1)
2407 {
2408 error(typeQualifier.line, "No num_views specified", "layout");
2409 return;
2410 }
2411
2412 if (layoutQualifier.numViews > mMaxNumViews)
2413 {
2414 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
2415 "layout");
2416 return;
2417 }
2418
2419 mNumViews = layoutQualifier.numViews;
2420 }
Martin Radev802abe02016-08-04 17:48:32 +03002421 else
Jamie Madill1566ef72013-06-20 11:55:54 -04002422 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00002423 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03002424 {
Martin Radev802abe02016-08-04 17:48:32 +03002425 return;
2426 }
2427
2428 if (typeQualifier.qualifier != EvqUniform)
2429 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002430 error(typeQualifier.line, "invalid qualifier: global layout must be uniform",
2431 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03002432 return;
2433 }
2434
2435 if (mShaderVersion < 300)
2436 {
2437 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
2438 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002439 return;
2440 }
2441
Olli Etuaho09b04a22016-12-15 13:30:26 +00002442 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002443
2444 if (layoutQualifier.matrixPacking != EmpUnspecified)
2445 {
2446 mDefaultMatrixPacking = layoutQualifier.matrixPacking;
2447 }
2448
2449 if (layoutQualifier.blockStorage != EbsUnspecified)
2450 {
2451 mDefaultBlockStorage = layoutQualifier.blockStorage;
2452 }
Jamie Madill1566ef72013-06-20 11:55:54 -04002453 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002454}
2455
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002456TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
2457 const TFunction &function,
2458 const TSourceLoc &location,
2459 bool insertParametersToSymbolTable)
2460{
2461 TIntermFunctionPrototype *prototype = new TIntermFunctionPrototype(function.getReturnType());
2462 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
2463 // point to the data that already exists in the symbol table.
2464 prototype->getFunctionSymbolInfo()->setFromFunction(function);
2465 prototype->setLine(location);
2466
2467 for (size_t i = 0; i < function.getParamCount(); i++)
2468 {
2469 const TConstParameter &param = function.getParam(i);
2470
2471 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
2472 // be used for unused args).
2473 if (param.name != nullptr)
2474 {
2475 TVariable *variable = new TVariable(param.name, *param.type);
2476
2477 // Insert the parameter in the symbol table.
2478 if (insertParametersToSymbolTable && !symbolTable.declare(variable))
2479 {
2480 error(location, "redefinition", variable->getName().c_str());
2481 prototype->appendParameter(intermediate.addSymbol(0, "", *param.type, location));
2482 continue;
2483 }
2484 TIntermSymbol *symbol = intermediate.addSymbol(
2485 variable->getUniqueId(), variable->getName(), variable->getType(), location);
2486 prototype->appendParameter(symbol);
2487 }
2488 else
2489 {
2490 prototype->appendParameter(intermediate.addSymbol(0, "", *param.type, location));
2491 }
2492 }
2493 return prototype;
2494}
2495
Olli Etuaho16c745a2017-01-16 17:02:27 +00002496TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
2497 const TFunction &parsedFunction,
2498 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002499{
Olli Etuaho476197f2016-10-11 13:59:08 +01002500 // Note: function found from the symbol table could be the same as parsedFunction if this is the
2501 // first declaration. Either way the instance in the symbol table is used to track whether the
2502 // function is declared multiple times.
2503 TFunction *function = static_cast<TFunction *>(
2504 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
2505 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02002506 {
2507 // ESSL 1.00.17 section 4.2.7.
2508 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
2509 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02002510 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002511 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02002512
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002513 TIntermFunctionPrototype *prototype =
2514 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002515
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002516 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002517
2518 if (!symbolTable.atGlobalLevel())
2519 {
2520 // ESSL 3.00.4 section 4.2.4.
2521 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02002522 }
2523
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002524 return prototype;
2525}
2526
Olli Etuaho336b1472016-10-05 16:37:55 +01002527TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002528 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01002529 TIntermBlock *functionBody,
2530 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002531{
Olli Etuahof51fdd22016-10-03 10:03:40 +01002532 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002533 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
2534 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002535 error(location, "function does not return a value:",
2536 functionPrototype->getFunctionSymbolInfo()->getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002537 }
2538
Olli Etuahof51fdd22016-10-03 10:03:40 +01002539 if (functionBody == nullptr)
2540 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01002541 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002542 functionBody->setLine(location);
2543 }
Olli Etuaho336b1472016-10-05 16:37:55 +01002544 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002545 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01002546 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01002547
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002548 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01002549 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002550}
2551
Olli Etuaho476197f2016-10-11 13:59:08 +01002552void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
2553 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002554 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04002555{
Olli Etuaho476197f2016-10-11 13:59:08 +01002556 ASSERT(function);
2557 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04002558 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01002559 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002560
2561 if (builtIn)
2562 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002563 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002564 }
Olli Etuaho476197f2016-10-11 13:59:08 +01002565 else
Jamie Madill185fb402015-06-12 15:48:48 -04002566 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002567 TFunction *prevDec = static_cast<TFunction *>(
2568 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
2569
2570 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
2571 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
2572 // occurance.
2573 if (*function != prevDec)
2574 {
2575 // Swap the parameters of the previous declaration to the parameters of the function
2576 // definition (parameter names may differ).
2577 prevDec->swapParameters(**function);
2578
2579 // The function definition will share the same symbol as any previous declaration.
2580 *function = prevDec;
2581 }
2582
2583 if ((*function)->isDefined())
2584 {
2585 error(location, "function already has a body", (*function)->getName().c_str());
2586 }
2587
2588 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04002589 }
Jamie Madill185fb402015-06-12 15:48:48 -04002590
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002591 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01002592 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02002593 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04002594
Olli Etuaho8ad9e752017-01-16 19:55:20 +00002595 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04002596 setLoopNestingLevel(0);
2597}
2598
Jamie Madillb98c3a82015-07-23 14:26:04 -04002599TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04002600{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002601 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002602 // We don't know at this point whether this is a function definition or a prototype.
2603 // The definition production code will check for redefinitions.
2604 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002605 //
Olli Etuaho5d653182016-01-04 14:43:28 +02002606 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
2607 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002608 //
2609 TFunction *prevDec =
2610 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302611
Martin Radevda6254b2016-12-14 17:00:36 +02002612 if (getShaderVersion() >= 300 &&
2613 symbolTable.hasUnmangledBuiltInForShaderVersion(function->getName().c_str(),
2614 getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302615 {
Martin Radevda6254b2016-12-14 17:00:36 +02002616 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302617 // Therefore overloading or redefining builtin functions is an error.
2618 error(location, "Name of a built-in function cannot be redeclared as function",
2619 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05302620 }
2621 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04002622 {
2623 if (prevDec->getReturnType() != function->getReturnType())
2624 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002625 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002626 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04002627 }
2628 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
2629 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002630 if (prevDec->getParam(i).type->getQualifier() !=
2631 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04002632 {
Olli Etuaho476197f2016-10-11 13:59:08 +01002633 error(location,
2634 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04002635 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04002636 }
2637 }
2638 }
2639
2640 //
2641 // Check for previously declared variables using the same name.
2642 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00002643 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04002644 if (prevSym)
2645 {
2646 if (!prevSym->isFunction())
2647 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002648 error(location, "redefinition of a function", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04002649 }
2650 }
2651 else
2652 {
2653 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01002654 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04002655 }
2656
2657 // We're at the inner scope level of the function's arguments and body statement.
2658 // Add the function prototype to the surrounding scope instead.
2659 symbolTable.getOuterLevel()->insert(function);
2660
Olli Etuaho78d13742017-01-18 13:06:10 +00002661 // Raise error message if main function takes any parameters or return anything other than void
2662 if (function->getName() == "main")
2663 {
2664 if (function->getParamCount() > 0)
2665 {
2666 error(location, "function cannot take any parameter(s)", "main");
2667 }
2668 if (function->getReturnType().getBasicType() != EbtVoid)
2669 {
2670 error(location, "main function cannot return a value",
2671 function->getReturnType().getBasicString());
2672 }
2673 }
2674
Jamie Madill185fb402015-06-12 15:48:48 -04002675 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04002676 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
2677 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04002678 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
2679 //
2680 return function;
2681}
2682
Olli Etuaho9de84a52016-06-14 17:36:01 +03002683TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
2684 const TString *name,
2685 const TSourceLoc &location)
2686{
2687 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
2688 {
2689 error(location, "no qualifiers allowed for function return",
2690 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03002691 }
2692 if (!type.layoutQualifier.isEmpty())
2693 {
2694 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03002695 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002696 // make sure a sampler or an image is not involved as well...
Martin Radev4a9cd802016-09-01 16:51:51 +03002697 checkIsNotSampler(location, type.typeSpecifierNonArray,
2698 "samplers can't be function return values");
Martin Radev2cc85b32016-08-05 16:22:53 +03002699 checkIsNotImage(location, type.typeSpecifierNonArray, "images can't be function return values");
Olli Etuahoe29324f2016-06-15 10:58:03 +03002700 if (mShaderVersion < 300)
2701 {
2702 // Array return values are forbidden, but there's also no valid syntax for declaring array
2703 // return values in ESSL 1.00.
Olli Etuaho77ba4082016-12-16 12:01:18 +00002704 ASSERT(type.arraySize == 0 || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03002705
2706 if (type.isStructureContainingArrays())
2707 {
2708 // ESSL 1.00.17 section 6.1 Function Definitions
2709 error(location, "structures containing arrays can't be function return values",
2710 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03002711 }
2712 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03002713
2714 // Add the function as a prototype after parsing it (we do not support recursion)
2715 return new TFunction(name, new TType(type));
2716}
2717
Jamie Madill06145232015-05-13 13:10:01 -04002718TFunction *TParseContext::addConstructorFunc(const TPublicType &publicTypeIn)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002719{
Jamie Madill06145232015-05-13 13:10:01 -04002720 TPublicType publicType = publicTypeIn;
Martin Radev4a9cd802016-09-01 16:51:51 +03002721 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02002722 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002723 error(publicType.getLine(), "constructor can't be a structure definition",
2724 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02002725 }
2726
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002727 TOperator op = EOpNull;
Martin Radev4a9cd802016-09-01 16:51:51 +03002728 if (publicType.getUserDef())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002729 {
2730 op = EOpConstructStruct;
2731 }
2732 else
2733 {
Geoff Lang156d7192016-07-21 16:11:00 -04002734 op = sh::TypeToConstructorOperator(TType(publicType));
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002735 if (op == EOpNull)
2736 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002737 error(publicType.getLine(), "cannot construct this type",
2738 getBasicString(publicType.getBasicType()));
2739 publicType.setBasicType(EbtFloat);
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002740 op = EOpConstructFloat;
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002741 }
2742 }
2743
Dmitry Skiba7f17a502015-06-22 15:08:39 -07002744 const TType *type = new TType(publicType);
Olli Etuaho72d10202017-01-19 15:58:30 +00002745 return new TFunction(nullptr, type, op);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00002746}
2747
Jamie Madillb98c3a82015-07-23 14:26:04 -04002748// This function is used to test for the correctness of the parameters passed to various constructor
2749// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002750//
Olli Etuaho856c4972016-08-08 11:38:39 +03002751// 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 +00002752//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002753TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002754 TOperator op,
Olli Etuaho72d10202017-01-19 15:58:30 +00002755 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302756 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002757{
Olli Etuaho856c4972016-08-08 11:38:39 +03002758 if (type.isUnsizedArray())
2759 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002760 if (arguments->empty())
Olli Etuahobbe9fb52016-11-03 17:16:05 +00002761 {
2762 error(line, "implicitly sized array constructor must have at least one argument", "[]");
2763 type.setArraySize(1u);
2764 return TIntermTyped::CreateZero(type);
2765 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002766 type.setArraySize(static_cast<unsigned int>(arguments->size()));
Olli Etuaho856c4972016-08-08 11:38:39 +03002767 }
Olli Etuaho856c4972016-08-08 11:38:39 +03002768
Olli Etuaho72d10202017-01-19 15:58:30 +00002769 if (!checkConstructorArguments(line, arguments, op, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03002770 {
Olli Etuaho72d10202017-01-19 15:58:30 +00002771 return TIntermTyped::CreateZero(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03002772 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002773
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002774 TIntermAggregate *constructorNode = new TIntermAggregate(type, op, arguments);
2775 constructorNode->setLine(line);
2776 ASSERT(constructorNode->isConstructor());
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002777
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002778 TIntermTyped *constConstructor =
2779 intermediate.foldAggregateBuiltIn(constructorNode, mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02002780 if (constConstructor)
2781 {
2782 return constConstructor;
2783 }
2784
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08002785 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002786}
2787
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002788//
2789// Interface/uniform blocks
2790//
Olli Etuaho13389b62016-10-16 11:48:18 +01002791TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03002792 const TTypeQualifierBuilder &typeQualifierBuilder,
2793 const TSourceLoc &nameLine,
2794 const TString &blockName,
2795 TFieldList *fieldList,
2796 const TString *instanceName,
2797 const TSourceLoc &instanceLine,
2798 TIntermTyped *arrayIndex,
2799 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002800{
Olli Etuaho856c4972016-08-08 11:38:39 +03002801 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002802
Olli Etuaho77ba4082016-12-16 12:01:18 +00002803 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03002804
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002805 if (typeQualifier.qualifier != EvqUniform)
2806 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002807 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform",
2808 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002809 }
2810
Martin Radev70866b82016-07-22 15:27:42 +03002811 if (typeQualifier.invariant)
2812 {
2813 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
2814 }
2815
Olli Etuaho43364892017-02-13 16:00:12 +00002816 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
2817
2818 // TODO(oetuaho): Remove this and support binding for blocks.
2819 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03002820
Jamie Madill099c0f32013-06-20 11:55:52 -04002821 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03002822 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04002823
Jamie Madill099c0f32013-06-20 11:55:52 -04002824 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
2825 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002826 blockLayoutQualifier.matrixPacking = mDefaultMatrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002827 }
2828
Jamie Madill1566ef72013-06-20 11:55:54 -04002829 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
2830 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002831 blockLayoutQualifier.blockStorage = mDefaultBlockStorage;
Jamie Madill1566ef72013-06-20 11:55:54 -04002832 }
2833
Olli Etuaho856c4972016-08-08 11:38:39 +03002834 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002835
Martin Radev2cc85b32016-08-05 16:22:53 +03002836 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
2837
Arun Patole7e7e68d2015-05-22 12:02:25 +05302838 TSymbol *blockNameSymbol = new TInterfaceBlockName(&blockName);
2839 if (!symbolTable.declare(blockNameSymbol))
2840 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002841 error(nameLine, "redefinition of an interface block name", blockName.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002842 }
2843
Jamie Madill98493dd2013-07-08 14:39:03 -04002844 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05302845 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2846 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002847 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302848 TType *fieldType = field->type();
2849 if (IsSampler(fieldType->getBasicType()))
2850 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002851 error(field->line(),
2852 "unsupported type - sampler types are not allowed in interface blocks",
2853 fieldType->getBasicString());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002854 }
2855
Martin Radev2cc85b32016-08-05 16:22:53 +03002856 if (IsImage(fieldType->getBasicType()))
2857 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002858 error(field->line(),
2859 "unsupported type - image types are not allowed in interface blocks",
2860 fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03002861 }
2862
Jamie Madill98493dd2013-07-08 14:39:03 -04002863 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002864 switch (qualifier)
2865 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002866 case EvqGlobal:
2867 case EvqUniform:
2868 break;
2869 default:
2870 error(field->line(), "invalid qualifier on interface block member",
2871 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04002872 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002873 }
Jamie Madilla5efff92013-06-06 11:56:47 -04002874
Martin Radev70866b82016-07-22 15:27:42 +03002875 if (fieldType->isInvariant())
2876 {
2877 error(field->line(), "invalid qualifier on interface block member", "invariant");
2878 }
2879
Jamie Madilla5efff92013-06-06 11:56:47 -04002880 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04002881 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03002882 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
Jamie Madill099c0f32013-06-20 11:55:52 -04002883
Jamie Madill98493dd2013-07-08 14:39:03 -04002884 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04002885 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002886 error(field->line(), "invalid layout qualifier: cannot be used here",
2887 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04002888 }
2889
Jamie Madill98493dd2013-07-08 14:39:03 -04002890 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04002891 {
Jamie Madill98493dd2013-07-08 14:39:03 -04002892 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04002893 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03002894 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04002895 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002896 warning(field->line(),
2897 "extraneous layout qualifier: only has an effect on matrix types",
2898 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04002899 }
2900
Jamie Madill98493dd2013-07-08 14:39:03 -04002901 fieldType->setLayoutQualifier(fieldLayoutQualifier);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002902 }
2903
Jamie Madill98493dd2013-07-08 14:39:03 -04002904 // add array index
Olli Etuaho856c4972016-08-08 11:38:39 +03002905 unsigned int arraySize = 0;
2906 if (arrayIndex != nullptr)
Jamie Madill98493dd2013-07-08 14:39:03 -04002907 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002908 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
Jamie Madill98493dd2013-07-08 14:39:03 -04002909 }
2910
Jamie Madillb98c3a82015-07-23 14:26:04 -04002911 TInterfaceBlock *interfaceBlock =
2912 new TInterfaceBlock(&blockName, fieldList, instanceName, arraySize, blockLayoutQualifier);
2913 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
2914 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002915
2916 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04002917 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002918
Jamie Madill98493dd2013-07-08 14:39:03 -04002919 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002920 {
2921 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04002922 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
2923 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04002924 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05302925 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04002926
2927 // set parent pointer of the field variable
2928 fieldType->setInterfaceBlock(interfaceBlock);
2929
Arun Patole7e7e68d2015-05-22 12:02:25 +05302930 TVariable *fieldVariable = new TVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04002931 fieldVariable->setQualifier(typeQualifier.qualifier);
2932
Arun Patole7e7e68d2015-05-22 12:02:25 +05302933 if (!symbolTable.declare(fieldVariable))
2934 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002935 error(field->line(), "redefinition of an interface block member name",
2936 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002937 }
2938 }
2939 }
2940 else
2941 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002942 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03002943
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002944 // add a symbol for this interface block
Arun Patole7e7e68d2015-05-22 12:02:25 +05302945 TVariable *instanceTypeDef = new TVariable(instanceName, interfaceBlockType, false);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002946 instanceTypeDef->setQualifier(typeQualifier.qualifier);
Jamie Madill98493dd2013-07-08 14:39:03 -04002947
Arun Patole7e7e68d2015-05-22 12:02:25 +05302948 if (!symbolTable.declare(instanceTypeDef))
2949 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002950 error(instanceLine, "redefinition of an interface block instance name",
2951 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002952 }
2953
Jamie Madillb98c3a82015-07-23 14:26:04 -04002954 symbolId = instanceTypeDef->getUniqueId();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002955 symbolName = instanceTypeDef->getName();
2956 }
2957
Olli Etuaho13389b62016-10-16 11:48:18 +01002958 TIntermSymbol *blockSymbol =
2959 intermediate.addSymbol(symbolId, symbolName, interfaceBlockType, typeQualifier.line);
2960 TIntermDeclaration *declaration = new TIntermDeclaration();
2961 declaration->appendDeclarator(blockSymbol);
2962 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04002963
2964 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01002965 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00002966}
2967
Olli Etuaho383b7912016-08-05 11:22:59 +03002968void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002969{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002970 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002971
2972 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00002973 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302974 if (mStructNestingLevel > 1)
2975 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002976 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00002977 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00002978}
2979
2980void TParseContext::exitStructDeclaration()
2981{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002982 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002983}
2984
Olli Etuaho8a176262016-08-16 14:23:01 +03002985void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00002986{
Jamie Madillacb4b812016-11-07 13:50:29 -05002987 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05302988 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002989 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002990 }
2991
Arun Patole7e7e68d2015-05-22 12:02:25 +05302992 if (field.type()->getBasicType() != EbtStruct)
2993 {
Olli Etuaho8a176262016-08-16 14:23:01 +03002994 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00002995 }
2996
2997 // We're already inside a structure definition at this point, so add
2998 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05302999 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3000 {
Jamie Madill41a49272014-03-18 16:10:13 -04003001 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003002 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
3003 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003004 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003005 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003006 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003007 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003008}
3009
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003010//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003011// Parse an array index expression
3012//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003013TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3014 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303015 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003016{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003017 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3018 {
3019 if (baseExpression->getAsSymbolNode())
3020 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303021 error(location, " left of '[' is not of type array, matrix, or vector ",
3022 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003023 }
3024 else
3025 {
3026 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3027 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003028
3029 TConstantUnion *unionArray = new TConstantUnion[1];
3030 unionArray->setFConst(0.0f);
3031 return intermediate.addConstantUnion(unionArray, TType(EbtFloat, EbpHigh, EvqConst),
3032 location);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003033 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003034
Jamie Madill21c1e452014-12-29 11:33:41 -05003035 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3036
Olli Etuaho36b05142015-11-12 13:10:42 +02003037 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3038 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3039 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3040 // index is a constant expression.
3041 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3042 {
3043 if (baseExpression->isInterfaceBlock())
3044 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003045 error(location,
3046 "array indexes for interface blocks arrays must be constant integral expressions",
3047 "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003048 }
3049 else if (baseExpression->getQualifier() == EvqFragmentOut)
3050 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003051 error(location,
3052 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003053 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003054 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3055 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003056 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003057 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003058 }
3059
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003060 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003061 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003062 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3063 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3064 // constant fold expressions that are not constant expressions). The most compatible way to
3065 // handle this case is to report a warning instead of an error and force the index to be in
3066 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003067 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003068 int index = indexConstantUnion->getIConst(0);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003069
3070 int safeIndex = -1;
3071
3072 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04003073 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003074 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003075 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003076 if (mShaderSpec == SH_WEBGL2_SPEC)
3077 {
3078 // Error has been already generated if index is not const.
3079 if (indexExpression->getQualifier() == EvqConst)
3080 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003081 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003082 }
3083 safeIndex = 0;
3084 }
3085 else if (!isExtensionEnabled("GL_EXT_draw_buffers"))
3086 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003087 outOfRangeError(outOfRangeIndexIsError, location,
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003088 "array index for gl_FragData must be zero when "
Olli Etuaho4de340a2016-12-16 09:32:03 +00003089 "GL_EXT_draw_buffers is disabled",
3090 "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003091 safeIndex = 0;
3092 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03003093 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003094 // Only do generic out-of-range check if similar error hasn't already been reported.
3095 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003096 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003097 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3098 baseExpression->getArraySize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003099 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003100 }
3101 }
3102 else if (baseExpression->isMatrix())
3103 {
3104 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03003105 baseExpression->getType().getCols(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003106 "matrix field selection out of range");
Jamie Madill7164cf42013-07-08 13:30:59 -04003107 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003108 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04003109 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003110 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3111 baseExpression->getType().getNominalSize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003112 "vector field selection out of range");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003113 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003114
3115 ASSERT(safeIndex >= 0);
3116 // Data of constant unions can't be changed, because it may be shared with other
3117 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
3118 // sanitized object.
3119 if (safeIndex != index)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003120 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003121 TConstantUnion *safeConstantUnion = new TConstantUnion();
3122 safeConstantUnion->setIConst(safeIndex);
3123 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003124 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003125
3126 return intermediate.addIndex(EOpIndexDirect, baseExpression, indexExpression, location,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003127 mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003128 }
Jamie Madill7164cf42013-07-08 13:30:59 -04003129 else
3130 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003131 return intermediate.addIndex(EOpIndexIndirect, baseExpression, indexExpression, location,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003132 mDiagnostics);
Jamie Madill7164cf42013-07-08 13:30:59 -04003133 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003134}
3135
Olli Etuaho90892fb2016-07-14 14:44:51 +03003136int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
3137 const TSourceLoc &location,
3138 int index,
3139 int arraySize,
Olli Etuaho4de340a2016-12-16 09:32:03 +00003140 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003141{
3142 if (index >= arraySize || index < 0)
3143 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003144 std::stringstream reasonStream;
3145 reasonStream << reason << " '" << index << "'";
3146 std::string token = reasonStream.str();
3147 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuaho90892fb2016-07-14 14:44:51 +03003148 if (index < 0)
3149 {
3150 return 0;
3151 }
3152 else
3153 {
3154 return arraySize - 1;
3155 }
3156 }
3157 return index;
3158}
3159
Jamie Madillb98c3a82015-07-23 14:26:04 -04003160TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3161 const TSourceLoc &dotLocation,
3162 const TString &fieldString,
3163 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003164{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003165 if (baseExpression->isArray())
3166 {
3167 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003168 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003169 }
3170
3171 if (baseExpression->isVector())
3172 {
3173 TVectorFields fields;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003174 if (!parseVectorFields(fieldString, baseExpression->getNominalSize(), fields,
3175 fieldLocation))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003176 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003177 fields.num = 1;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003178 fields.offsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003179 }
3180
Olli Etuahob6fa0432016-09-28 16:28:05 +01003181 return TIntermediate::AddSwizzle(baseExpression, fields, dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003182 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003183 else if (baseExpression->getBasicType() == EbtStruct)
3184 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303185 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003186 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003187 {
3188 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003189 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003190 }
3191 else
3192 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003193 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003194 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003195 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003196 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003197 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003198 {
3199 fieldFound = true;
3200 break;
3201 }
3202 }
3203 if (fieldFound)
3204 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003205 TIntermTyped *index = TIntermTyped::CreateIndexNode(i);
3206 index->setLine(fieldLocation);
3207 return intermediate.addIndex(EOpIndexDirectStruct, baseExpression, index,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003208 dotLocation, mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003209 }
3210 else
3211 {
3212 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003213 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003214 }
3215 }
3216 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003217 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003218 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303219 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003220 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003221 {
3222 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003223 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003224 }
3225 else
3226 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003227 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003228 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003229 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003230 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003231 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +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(EOpIndexDirectInterfaceBlock, baseExpression, index,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003242 dotLocation, mDiagnostics);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003243 }
3244 else
3245 {
3246 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003247 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003248 }
3249 }
3250 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003251 else
3252 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003253 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003254 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03003255 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303256 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003257 }
3258 else
3259 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303260 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003261 " field selection requires structure, vector, or interface block on left hand "
3262 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303263 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003264 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003265 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003266 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003267}
3268
Jamie Madillb98c3a82015-07-23 14:26:04 -04003269TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3270 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003271{
Martin Radev802abe02016-08-04 17:48:32 +03003272 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003273
3274 if (qualifierType == "shared")
3275 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003276 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003277 {
3278 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
3279 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003280 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003281 }
3282 else if (qualifierType == "packed")
3283 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003284 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003285 {
3286 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
3287 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003288 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003289 }
3290 else if (qualifierType == "std140")
3291 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003292 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003293 }
3294 else if (qualifierType == "row_major")
3295 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003296 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003297 }
3298 else if (qualifierType == "column_major")
3299 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003300 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003301 }
3302 else if (qualifierType == "location")
3303 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003304 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
3305 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003306 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003307 else if (qualifierType == "rgba32f")
3308 {
3309 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3310 qualifier.imageInternalFormat = EiifRGBA32F;
3311 }
3312 else if (qualifierType == "rgba16f")
3313 {
3314 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3315 qualifier.imageInternalFormat = EiifRGBA16F;
3316 }
3317 else if (qualifierType == "r32f")
3318 {
3319 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3320 qualifier.imageInternalFormat = EiifR32F;
3321 }
3322 else if (qualifierType == "rgba8")
3323 {
3324 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3325 qualifier.imageInternalFormat = EiifRGBA8;
3326 }
3327 else if (qualifierType == "rgba8_snorm")
3328 {
3329 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3330 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
3331 }
3332 else if (qualifierType == "rgba32i")
3333 {
3334 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3335 qualifier.imageInternalFormat = EiifRGBA32I;
3336 }
3337 else if (qualifierType == "rgba16i")
3338 {
3339 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3340 qualifier.imageInternalFormat = EiifRGBA16I;
3341 }
3342 else if (qualifierType == "rgba8i")
3343 {
3344 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3345 qualifier.imageInternalFormat = EiifRGBA8I;
3346 }
3347 else if (qualifierType == "r32i")
3348 {
3349 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3350 qualifier.imageInternalFormat = EiifR32I;
3351 }
3352 else if (qualifierType == "rgba32ui")
3353 {
3354 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3355 qualifier.imageInternalFormat = EiifRGBA32UI;
3356 }
3357 else if (qualifierType == "rgba16ui")
3358 {
3359 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3360 qualifier.imageInternalFormat = EiifRGBA16UI;
3361 }
3362 else if (qualifierType == "rgba8ui")
3363 {
3364 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3365 qualifier.imageInternalFormat = EiifRGBA8UI;
3366 }
3367 else if (qualifierType == "r32ui")
3368 {
3369 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3370 qualifier.imageInternalFormat = EiifR32UI;
3371 }
3372
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003373 else
3374 {
3375 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003376 }
3377
Jamie Madilla5efff92013-06-06 11:56:47 -04003378 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003379}
3380
Martin Radev802abe02016-08-04 17:48:32 +03003381void TParseContext::parseLocalSize(const TString &qualifierType,
3382 const TSourceLoc &qualifierTypeLine,
3383 int intValue,
3384 const TSourceLoc &intValueLine,
3385 const std::string &intValueString,
3386 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03003387 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03003388{
Olli Etuaho856c4972016-08-08 11:38:39 +03003389 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03003390 if (intValue < 1)
3391 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003392 std::stringstream reasonStream;
3393 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
3394 std::string reason = reasonStream.str();
3395 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003396 }
3397 (*localSize)[index] = intValue;
3398}
3399
Olli Etuaho09b04a22016-12-15 13:30:26 +00003400void TParseContext::parseNumViews(int intValue,
3401 const TSourceLoc &intValueLine,
3402 const std::string &intValueString,
3403 int *numViews)
3404{
3405 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3406 // specification.
3407 if (intValue < 1)
3408 {
3409 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
3410 }
3411 *numViews = intValue;
3412}
3413
Jamie Madillb98c3a82015-07-23 14:26:04 -04003414TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3415 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003416 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303417 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003418{
Martin Radev802abe02016-08-04 17:48:32 +03003419 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003420
Martin Radev802abe02016-08-04 17:48:32 +03003421 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003422
Martin Radev802abe02016-08-04 17:48:32 +03003423 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003424 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04003425 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003426 if (intValue < 0)
3427 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003428 error(intValueLine, "out of range: location must be non-negative",
3429 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003430 }
3431 else
3432 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05003433 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03003434 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003435 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003436 }
Olli Etuaho43364892017-02-13 16:00:12 +00003437 else if (qualifierType == "binding")
3438 {
3439 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3440 if (intValue < 0)
3441 {
3442 error(intValueLine, "out of range: binding must be non-negative",
3443 intValueString.c_str());
3444 }
3445 else
3446 {
3447 qualifier.binding = intValue;
3448 }
3449 }
Martin Radev802abe02016-08-04 17:48:32 +03003450 else if (qualifierType == "local_size_x")
3451 {
3452 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
3453 &qualifier.localSize);
3454 }
3455 else if (qualifierType == "local_size_y")
3456 {
3457 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
3458 &qualifier.localSize);
3459 }
3460 else if (qualifierType == "local_size_z")
3461 {
3462 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
3463 &qualifier.localSize);
3464 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00003465 else if (qualifierType == "num_views" && mMultiviewAvailable &&
3466 (isExtensionEnabled("GL_OVR_multiview") || isExtensionEnabled("GL_OVR_multiview2")) &&
3467 mShaderType == GL_VERTEX_SHADER)
3468 {
3469 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
3470 }
Martin Radev802abe02016-08-04 17:48:32 +03003471 else
3472 {
3473 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03003474 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003475
Jamie Madilla5efff92013-06-06 11:56:47 -04003476 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003477}
3478
Olli Etuaho613b9592016-09-05 12:05:53 +03003479TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
3480{
3481 return new TTypeQualifierBuilder(
3482 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
3483 mShaderVersion);
3484}
3485
Jamie Madillb98c3a82015-07-23 14:26:04 -04003486TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03003487 TLayoutQualifier rightQualifier,
3488 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003489{
Martin Radevc28888b2016-07-22 15:27:42 +03003490 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00003491 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003492}
3493
Olli Etuaho4de340a2016-12-16 09:32:03 +00003494TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
3495 const TFieldList *newlyAddedFields,
3496 const TSourceLoc &location)
3497{
3498 for (TField *field : *newlyAddedFields)
3499 {
3500 for (TField *oldField : *processedFields)
3501 {
3502 if (oldField->name() == field->name())
3503 {
3504 error(location, "duplicate field name in structure", field->name().c_str());
3505 }
3506 }
3507 processedFields->push_back(field);
3508 }
3509 return processedFields;
3510}
3511
Martin Radev70866b82016-07-22 15:27:42 +03003512TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
3513 const TTypeQualifierBuilder &typeQualifierBuilder,
3514 TPublicType *typeSpecifier,
3515 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003516{
Olli Etuaho77ba4082016-12-16 12:01:18 +00003517 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003518
Martin Radev70866b82016-07-22 15:27:42 +03003519 typeSpecifier->qualifier = typeQualifier.qualifier;
3520 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03003521 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03003522 typeSpecifier->invariant = typeQualifier.invariant;
3523 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05303524 {
Martin Radev70866b82016-07-22 15:27:42 +03003525 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003526 }
Martin Radev70866b82016-07-22 15:27:42 +03003527 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04003528}
3529
Jamie Madillb98c3a82015-07-23 14:26:04 -04003530TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
3531 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003532{
Martin Radev4a9cd802016-09-01 16:51:51 +03003533 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
3534 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03003535
Martin Radev4a9cd802016-09-01 16:51:51 +03003536 checkIsNonVoid(typeSpecifier.getLine(), (*fieldList)[0]->name(), typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003537
Martin Radev4a9cd802016-09-01 16:51:51 +03003538 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003539
Arun Patole7e7e68d2015-05-22 12:02:25 +05303540 for (unsigned int i = 0; i < fieldList->size(); ++i)
3541 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003542 //
3543 // Careful not to replace already known aspects of type, like array-ness
3544 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05303545 TType *type = (*fieldList)[i]->type();
Martin Radev4a9cd802016-09-01 16:51:51 +03003546 type->setBasicType(typeSpecifier.getBasicType());
3547 type->setPrimarySize(typeSpecifier.getPrimarySize());
3548 type->setSecondarySize(typeSpecifier.getSecondarySize());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003549 type->setPrecision(typeSpecifier.precision);
3550 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003551 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
Martin Radev2cc85b32016-08-05 16:22:53 +03003552 type->setMemoryQualifier(typeSpecifier.memoryQualifier);
Martin Radev70866b82016-07-22 15:27:42 +03003553 type->setInvariant(typeSpecifier.invariant);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003554
3555 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05303556 if (type->isArray())
3557 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003558 checkIsValidTypeForArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003559 }
3560 if (typeSpecifier.array)
Olli Etuaho856c4972016-08-08 11:38:39 +03003561 type->setArraySize(static_cast<unsigned int>(typeSpecifier.arraySize));
Martin Radev4a9cd802016-09-01 16:51:51 +03003562 if (typeSpecifier.getUserDef())
Arun Patole7e7e68d2015-05-22 12:02:25 +05303563 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003564 type->setStruct(typeSpecifier.getUserDef()->getStruct());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003565 }
3566
Martin Radev4a9cd802016-09-01 16:51:51 +03003567 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*fieldList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003568 }
3569
Jamie Madill98493dd2013-07-08 14:39:03 -04003570 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003571}
3572
Martin Radev4a9cd802016-09-01 16:51:51 +03003573TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
3574 const TSourceLoc &nameLine,
3575 const TString *structName,
3576 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003577{
Arun Patole7e7e68d2015-05-22 12:02:25 +05303578 TStructure *structure = new TStructure(structName, fieldList);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003579 TType *structureType = new TType(structure);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003580
Jamie Madill9b820842015-02-12 10:40:10 -05003581 // Store a bool in the struct if we're at global scope, to allow us to
3582 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05003583 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04003584
Jamie Madill98493dd2013-07-08 14:39:03 -04003585 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003586 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003587 checkIsNotReserved(nameLine, *structName);
Arun Patole7e7e68d2015-05-22 12:02:25 +05303588 TVariable *userTypeDef = new TVariable(structName, *structureType, true);
3589 if (!symbolTable.declare(userTypeDef))
3590 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003591 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003592 }
3593 }
3594
3595 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04003596 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003597 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003598 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04003599 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003600 switch (qualifier)
3601 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003602 case EvqGlobal:
3603 case EvqTemporary:
3604 break;
3605 default:
3606 error(field.line(), "invalid qualifier on struct member",
3607 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003608 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003609 }
Martin Radev70866b82016-07-22 15:27:42 +03003610 if (field.type()->isInvariant())
3611 {
3612 error(field.line(), "invalid qualifier on struct member", "invariant");
3613 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003614 if (IsImage(field.type()->getBasicType()))
3615 {
3616 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
3617 }
3618
Olli Etuaho43364892017-02-13 16:00:12 +00003619 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
3620
3621 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03003622
3623 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003624 }
3625
Martin Radev4a9cd802016-09-01 16:51:51 +03003626 TTypeSpecifierNonArray typeSpecifierNonArray;
3627 typeSpecifierNonArray.initialize(EbtStruct, structLine);
3628 typeSpecifierNonArray.userDef = structureType;
3629 typeSpecifierNonArray.isStructSpecifier = true;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003630 exitStructDeclaration();
3631
Martin Radev4a9cd802016-09-01 16:51:51 +03003632 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003633}
3634
Jamie Madillb98c3a82015-07-23 14:26:04 -04003635TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003636 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04003637 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02003638{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003639 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04003640 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02003641 init->isVector())
3642 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003643 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
3644 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003645 return nullptr;
3646 }
3647
Olli Etuahoac5274d2015-02-20 10:19:08 +02003648 if (statementList)
3649 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00003650 if (!ValidateSwitchStatementList(switchType, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02003651 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02003652 return nullptr;
3653 }
3654 }
3655
Olli Etuahoa3a36662015-02-17 13:46:51 +02003656 TIntermSwitch *node = intermediate.addSwitch(init, statementList, loc);
3657 if (node == nullptr)
3658 {
3659 error(loc, "erroneous switch statement", "switch");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003660 return nullptr;
3661 }
3662 return node;
3663}
3664
3665TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
3666{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003667 if (mSwitchNestingLevel == 0)
3668 {
3669 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003670 return nullptr;
3671 }
3672 if (condition == nullptr)
3673 {
3674 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003675 return nullptr;
3676 }
3677 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04003678 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02003679 {
3680 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003681 }
3682 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003683 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
3684 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
3685 // fold in case labels.
3686 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02003687 {
3688 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003689 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003690 TIntermCase *node = intermediate.addCase(condition, loc);
3691 if (node == nullptr)
3692 {
3693 error(loc, "erroneous case statement", "case");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003694 return nullptr;
3695 }
3696 return node;
3697}
3698
3699TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
3700{
Olli Etuaho53f076f2015-02-20 10:55:14 +02003701 if (mSwitchNestingLevel == 0)
3702 {
3703 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02003704 return nullptr;
3705 }
Olli Etuahoa3a36662015-02-17 13:46:51 +02003706 TIntermCase *node = intermediate.addCase(nullptr, loc);
3707 if (node == nullptr)
3708 {
3709 error(loc, "erroneous default statement", "default");
Olli Etuahoa3a36662015-02-17 13:46:51 +02003710 return nullptr;
3711 }
3712 return node;
3713}
3714
Jamie Madillb98c3a82015-07-23 14:26:04 -04003715TIntermTyped *TParseContext::createUnaryMath(TOperator op,
3716 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003717 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02003718{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003719 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003720
3721 switch (op)
3722 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003723 case EOpLogicalNot:
3724 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
3725 child->isVector())
3726 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003727 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003728 return nullptr;
3729 }
3730 break;
3731 case EOpBitwiseNot:
3732 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
3733 child->isMatrix() || child->isArray())
3734 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003735 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003736 return nullptr;
3737 }
3738 break;
3739 case EOpPostIncrement:
3740 case EOpPreIncrement:
3741 case EOpPostDecrement:
3742 case EOpPreDecrement:
3743 case EOpNegative:
3744 case EOpPositive:
3745 if (child->getBasicType() == EbtStruct || child->getBasicType() == EbtBool ||
Martin Radev2cc85b32016-08-05 16:22:53 +03003746 child->isArray() || IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04003747 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003748 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04003749 return nullptr;
3750 }
3751 // Operators for built-ins are already type checked against their prototype.
3752 default:
3753 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003754 }
3755
Olli Etuahof119a262016-08-19 15:54:22 +03003756 TIntermUnary *node = new TIntermUnary(op, child);
3757 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03003758
Olli Etuaho77ba4082016-12-16 12:01:18 +00003759 TIntermTyped *foldedNode = node->fold(mDiagnostics);
Olli Etuahof119a262016-08-19 15:54:22 +03003760 if (foldedNode)
3761 return foldedNode;
3762
3763 return node;
Olli Etuaho69c11b52015-03-26 12:59:00 +02003764}
3765
Olli Etuaho09b22472015-02-11 11:47:26 +02003766TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
3767{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08003768 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02003769 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02003770 {
Olli Etuaho09b22472015-02-11 11:47:26 +02003771 return child;
3772 }
3773 return node;
3774}
3775
Jamie Madillb98c3a82015-07-23 14:26:04 -04003776TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
3777 TIntermTyped *child,
3778 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02003779{
Olli Etuaho856c4972016-08-08 11:38:39 +03003780 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02003781 return addUnaryMath(op, child, loc);
3782}
3783
Jamie Madillb98c3a82015-07-23 14:26:04 -04003784bool TParseContext::binaryOpCommonCheck(TOperator op,
3785 TIntermTyped *left,
3786 TIntermTyped *right,
3787 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02003788{
Olli Etuaho244be012016-08-18 15:26:02 +03003789 if (left->getType().getStruct() || right->getType().getStruct())
3790 {
3791 switch (op)
3792 {
3793 case EOpIndexDirectStruct:
3794 ASSERT(left->getType().getStruct());
3795 break;
3796 case EOpEqual:
3797 case EOpNotEqual:
3798 case EOpAssign:
3799 case EOpInitialize:
3800 if (left->getType() != right->getType())
3801 {
3802 return false;
3803 }
3804 break;
3805 default:
3806 error(loc, "Invalid operation for structs", GetOperatorString(op));
3807 return false;
3808 }
3809 }
3810
Olli Etuahod6b14282015-03-17 14:31:35 +02003811 if (left->isArray() || right->isArray())
3812 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003813 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02003814 {
3815 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3816 return false;
3817 }
3818
3819 if (left->isArray() != right->isArray())
3820 {
3821 error(loc, "array / non-array mismatch", GetOperatorString(op));
3822 return false;
3823 }
3824
3825 switch (op)
3826 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003827 case EOpEqual:
3828 case EOpNotEqual:
3829 case EOpAssign:
3830 case EOpInitialize:
3831 break;
3832 default:
3833 error(loc, "Invalid operation for arrays", GetOperatorString(op));
3834 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02003835 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03003836 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02003837 if (left->getArraySize() != right->getArraySize())
3838 {
3839 error(loc, "array size mismatch", GetOperatorString(op));
3840 return false;
3841 }
Olli Etuahod6b14282015-03-17 14:31:35 +02003842 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003843
3844 // Check ops which require integer / ivec parameters
3845 bool isBitShift = false;
3846 switch (op)
3847 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003848 case EOpBitShiftLeft:
3849 case EOpBitShiftRight:
3850 case EOpBitShiftLeftAssign:
3851 case EOpBitShiftRightAssign:
3852 // Unsigned can be bit-shifted by signed and vice versa, but we need to
3853 // check that the basic type is an integer type.
3854 isBitShift = true;
3855 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
3856 {
3857 return false;
3858 }
3859 break;
3860 case EOpBitwiseAnd:
3861 case EOpBitwiseXor:
3862 case EOpBitwiseOr:
3863 case EOpBitwiseAndAssign:
3864 case EOpBitwiseXorAssign:
3865 case EOpBitwiseOrAssign:
3866 // It is enough to check the type of only one operand, since later it
3867 // is checked that the operand types match.
3868 if (!IsInteger(left->getBasicType()))
3869 {
3870 return false;
3871 }
3872 break;
3873 default:
3874 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003875 }
3876
3877 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
3878 // So the basic type should usually match.
3879 if (!isBitShift && left->getBasicType() != right->getBasicType())
3880 {
3881 return false;
3882 }
3883
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003884 // Check that:
3885 // 1. Type sizes match exactly on ops that require that.
3886 // 2. Restrictions for structs that contain arrays or samplers are respected.
3887 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04003888 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003889 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003890 case EOpAssign:
3891 case EOpInitialize:
3892 case EOpEqual:
3893 case EOpNotEqual:
3894 // ESSL 1.00 sections 5.7, 5.8, 5.9
3895 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
3896 {
3897 error(loc, "undefined operation for structs containing arrays",
3898 GetOperatorString(op));
3899 return false;
3900 }
3901 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
3902 // we interpret the spec so that this extends to structs containing samplers,
3903 // similarly to ESSL 1.00 spec.
3904 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
3905 left->getType().isStructureContainingSamplers())
3906 {
3907 error(loc, "undefined operation for structs containing samplers",
3908 GetOperatorString(op));
3909 return false;
3910 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003911
3912 if ((op == EOpAssign || op == EOpInitialize) &&
3913 left->getType().isStructureContainingImages())
3914 {
3915 error(loc, "undefined operation for structs containing images",
3916 GetOperatorString(op));
3917 return false;
3918 }
Olli Etuahoe1805592017-01-02 16:41:20 +00003919 if ((left->getNominalSize() != right->getNominalSize()) ||
3920 (left->getSecondarySize() != right->getSecondarySize()))
3921 {
3922 error(loc, "dimension mismatch", GetOperatorString(op));
3923 return false;
3924 }
3925 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003926 case EOpLessThan:
3927 case EOpGreaterThan:
3928 case EOpLessThanEqual:
3929 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00003930 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04003931 {
Olli Etuahoe1805592017-01-02 16:41:20 +00003932 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003933 return false;
3934 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03003935 break;
3936 case EOpAdd:
3937 case EOpSub:
3938 case EOpDiv:
3939 case EOpIMod:
3940 case EOpBitShiftLeft:
3941 case EOpBitShiftRight:
3942 case EOpBitwiseAnd:
3943 case EOpBitwiseXor:
3944 case EOpBitwiseOr:
3945 case EOpAddAssign:
3946 case EOpSubAssign:
3947 case EOpDivAssign:
3948 case EOpIModAssign:
3949 case EOpBitShiftLeftAssign:
3950 case EOpBitShiftRightAssign:
3951 case EOpBitwiseAndAssign:
3952 case EOpBitwiseXorAssign:
3953 case EOpBitwiseOrAssign:
3954 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
3955 {
3956 return false;
3957 }
3958
3959 // Are the sizes compatible?
3960 if (left->getNominalSize() != right->getNominalSize() ||
3961 left->getSecondarySize() != right->getSecondarySize())
3962 {
3963 // If the nominal sizes of operands do not match:
3964 // One of them must be a scalar.
3965 if (!left->isScalar() && !right->isScalar())
3966 return false;
3967
3968 // In the case of compound assignment other than multiply-assign,
3969 // the right side needs to be a scalar. Otherwise a vector/matrix
3970 // would be assigned to a scalar. A scalar can't be shifted by a
3971 // vector either.
3972 if (!right->isScalar() &&
3973 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
3974 return false;
3975 }
3976 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003977 default:
3978 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02003979 }
3980
Olli Etuahod6b14282015-03-17 14:31:35 +02003981 return true;
3982}
3983
Olli Etuaho1dded802016-08-18 18:13:13 +03003984bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
3985 const TType &left,
3986 const TType &right)
3987{
3988 switch (op)
3989 {
3990 case EOpMul:
3991 case EOpMulAssign:
3992 return left.getNominalSize() == right.getNominalSize() &&
3993 left.getSecondarySize() == right.getSecondarySize();
3994 case EOpVectorTimesScalar:
3995 return true;
3996 case EOpVectorTimesScalarAssign:
3997 ASSERT(!left.isMatrix() && !right.isMatrix());
3998 return left.isVector() && !right.isVector();
3999 case EOpVectorTimesMatrix:
4000 return left.getNominalSize() == right.getRows();
4001 case EOpVectorTimesMatrixAssign:
4002 ASSERT(!left.isMatrix() && right.isMatrix());
4003 return left.isVector() && left.getNominalSize() == right.getRows() &&
4004 left.getNominalSize() == right.getCols();
4005 case EOpMatrixTimesVector:
4006 return left.getCols() == right.getNominalSize();
4007 case EOpMatrixTimesScalar:
4008 return true;
4009 case EOpMatrixTimesScalarAssign:
4010 ASSERT(left.isMatrix() && !right.isMatrix());
4011 return !right.isVector();
4012 case EOpMatrixTimesMatrix:
4013 return left.getCols() == right.getRows();
4014 case EOpMatrixTimesMatrixAssign:
4015 ASSERT(left.isMatrix() && right.isMatrix());
4016 // We need to check two things:
4017 // 1. The matrix multiplication step is valid.
4018 // 2. The result will have the same number of columns as the lvalue.
4019 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
4020
4021 default:
4022 UNREACHABLE();
4023 return false;
4024 }
4025}
4026
Jamie Madillb98c3a82015-07-23 14:26:04 -04004027TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
4028 TIntermTyped *left,
4029 TIntermTyped *right,
4030 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02004031{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004032 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02004033 return nullptr;
4034
Olli Etuahofc1806e2015-03-17 13:03:11 +02004035 switch (op)
4036 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004037 case EOpEqual:
4038 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04004039 case EOpLessThan:
4040 case EOpGreaterThan:
4041 case EOpLessThanEqual:
4042 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04004043 break;
4044 case EOpLogicalOr:
4045 case EOpLogicalXor:
4046 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03004047 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4048 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00004049 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04004050 {
4051 return nullptr;
4052 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00004053 // Basic types matching should have been already checked.
4054 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004055 break;
4056 case EOpAdd:
4057 case EOpSub:
4058 case EOpDiv:
4059 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03004060 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4061 !right->getType().getStruct());
4062 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04004063 {
4064 return nullptr;
4065 }
4066 break;
4067 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03004068 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
4069 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004070 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03004071 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04004072 {
4073 return nullptr;
4074 }
4075 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004076 default:
4077 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02004078 }
4079
Olli Etuaho1dded802016-08-18 18:13:13 +03004080 if (op == EOpMul)
4081 {
4082 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
4083 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4084 {
4085 return nullptr;
4086 }
4087 }
4088
Olli Etuaho3fdec912016-08-18 15:08:06 +03004089 TIntermBinary *node = new TIntermBinary(op, left, right);
4090 node->setLine(loc);
4091
Olli Etuaho3fdec912016-08-18 15:08:06 +03004092 // See if we can fold constants.
Olli Etuaho77ba4082016-12-16 12:01:18 +00004093 TIntermTyped *foldedNode = node->fold(mDiagnostics);
Olli Etuaho3fdec912016-08-18 15:08:06 +03004094 if (foldedNode)
4095 return foldedNode;
4096
4097 return node;
Olli Etuahofc1806e2015-03-17 13:03:11 +02004098}
4099
Jamie Madillb98c3a82015-07-23 14:26:04 -04004100TIntermTyped *TParseContext::addBinaryMath(TOperator op,
4101 TIntermTyped *left,
4102 TIntermTyped *right,
4103 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004104{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004105 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004106 if (node == 0)
4107 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004108 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4109 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02004110 return left;
4111 }
4112 return node;
4113}
4114
Jamie Madillb98c3a82015-07-23 14:26:04 -04004115TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
4116 TIntermTyped *left,
4117 TIntermTyped *right,
4118 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004119{
Olli Etuahofc1806e2015-03-17 13:03:11 +02004120 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004121 if (node == 0)
4122 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004123 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
4124 right->getCompleteString());
Jamie Madill6ba6ead2015-05-04 14:21:21 -04004125 TConstantUnion *unionArray = new TConstantUnion[1];
Olli Etuaho09b22472015-02-11 11:47:26 +02004126 unionArray->setBConst(false);
Jamie Madillb98c3a82015-07-23 14:26:04 -04004127 return intermediate.addConstantUnion(unionArray, TType(EbtBool, EbpUndefined, EvqConst),
4128 loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02004129 }
4130 return node;
4131}
4132
Olli Etuaho13389b62016-10-16 11:48:18 +01004133TIntermBinary *TParseContext::createAssign(TOperator op,
4134 TIntermTyped *left,
4135 TIntermTyped *right,
4136 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004137{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004138 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02004139 {
Olli Etuaho1dded802016-08-18 18:13:13 +03004140 if (op == EOpMulAssign)
4141 {
4142 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
4143 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
4144 {
4145 return nullptr;
4146 }
4147 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03004148 TIntermBinary *node = new TIntermBinary(op, left, right);
4149 node->setLine(loc);
4150
Olli Etuaho3fdec912016-08-18 15:08:06 +03004151 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02004152 }
4153 return nullptr;
4154}
4155
Jamie Madillb98c3a82015-07-23 14:26:04 -04004156TIntermTyped *TParseContext::addAssign(TOperator op,
4157 TIntermTyped *left,
4158 TIntermTyped *right,
4159 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004160{
4161 TIntermTyped *node = createAssign(op, left, right, loc);
4162 if (node == nullptr)
4163 {
4164 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02004165 return left;
4166 }
4167 return node;
4168}
4169
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004170TIntermTyped *TParseContext::addComma(TIntermTyped *left,
4171 TIntermTyped *right,
4172 const TSourceLoc &loc)
4173{
Corentin Wallez0d959252016-07-12 17:26:32 -04004174 // WebGL2 section 5.26, the following results in an error:
4175 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004176 if (mShaderSpec == SH_WEBGL2_SPEC &&
4177 (left->isArray() || left->getBasicType() == EbtVoid ||
4178 left->getType().isStructureContainingArrays() || right->isArray() ||
4179 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04004180 {
4181 error(loc,
4182 "sequence operator is not allowed for void, arrays, or structs containing arrays",
4183 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04004184 }
4185
Olli Etuaho4db7ded2016-10-13 12:23:11 +01004186 return TIntermediate::AddComma(left, right, loc, mShaderVersion);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02004187}
4188
Olli Etuaho49300862015-02-20 14:54:49 +02004189TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
4190{
4191 switch (op)
4192 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004193 case EOpContinue:
4194 if (mLoopNestingLevel <= 0)
4195 {
4196 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004197 }
4198 break;
4199 case EOpBreak:
4200 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
4201 {
4202 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004203 }
4204 break;
4205 case EOpReturn:
4206 if (mCurrentFunctionType->getBasicType() != EbtVoid)
4207 {
4208 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04004209 }
4210 break;
4211 default:
4212 // No checks for discard
4213 break;
Olli Etuaho49300862015-02-20 14:54:49 +02004214 }
4215 return intermediate.addBranch(op, loc);
4216}
4217
Jamie Madillb98c3a82015-07-23 14:26:04 -04004218TIntermBranch *TParseContext::addBranch(TOperator op,
4219 TIntermTyped *returnValue,
4220 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02004221{
4222 ASSERT(op == EOpReturn);
4223 mFunctionReturnsValue = true;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004224 if (mCurrentFunctionType->getBasicType() == EbtVoid)
Olli Etuaho49300862015-02-20 14:54:49 +02004225 {
4226 error(loc, "void function cannot return a value", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02004227 }
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004228 else if (*mCurrentFunctionType != returnValue->getType())
Olli Etuaho49300862015-02-20 14:54:49 +02004229 {
4230 error(loc, "function return is not matching type:", "return");
Olli Etuaho49300862015-02-20 14:54:49 +02004231 }
4232 return intermediate.addBranch(op, returnValue, loc);
4233}
4234
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004235void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
4236{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004237 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01004238 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004239 TIntermNode *offset = nullptr;
4240 TIntermSequence *arguments = functionCall->getSequence();
4241 if (name.compare(0, 16, "texelFetchOffset") == 0 ||
4242 name.compare(0, 16, "textureLodOffset") == 0 ||
4243 name.compare(0, 20, "textureProjLodOffset") == 0 ||
4244 name.compare(0, 17, "textureGradOffset") == 0 ||
4245 name.compare(0, 21, "textureProjGradOffset") == 0)
4246 {
4247 offset = arguments->back();
4248 }
4249 else if (name.compare(0, 13, "textureOffset") == 0 ||
4250 name.compare(0, 17, "textureProjOffset") == 0)
4251 {
4252 // A bias parameter might follow the offset parameter.
4253 ASSERT(arguments->size() >= 3);
4254 offset = (*arguments)[2];
4255 }
4256 if (offset != nullptr)
4257 {
4258 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
4259 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
4260 {
4261 TString unmangledName = TFunction::unmangleName(name);
4262 error(functionCall->getLine(), "Texture offset must be a constant expression",
4263 unmangledName.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004264 }
4265 else
4266 {
4267 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
4268 size_t size = offsetConstantUnion->getType().getObjectSize();
4269 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
4270 for (size_t i = 0u; i < size; ++i)
4271 {
4272 int offsetValue = values[i].getIConst();
4273 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
4274 {
4275 std::stringstream tokenStream;
4276 tokenStream << offsetValue;
4277 std::string token = tokenStream.str();
4278 error(offset->getLine(), "Texture offset value out of valid range",
4279 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004280 }
4281 }
4282 }
4283 }
4284}
4285
Martin Radev2cc85b32016-08-05 16:22:53 +03004286// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
4287void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
4288{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004289 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03004290 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
4291
4292 if (name.compare(0, 5, "image") == 0)
4293 {
4294 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00004295 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03004296
Olli Etuaho485eefd2017-02-14 17:40:06 +00004297 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03004298
4299 if (name.compare(5, 5, "Store") == 0)
4300 {
4301 if (memoryQualifier.readonly)
4302 {
4303 error(imageNode->getLine(),
4304 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004305 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03004306 }
4307 }
4308 else if (name.compare(5, 4, "Load") == 0)
4309 {
4310 if (memoryQualifier.writeonly)
4311 {
4312 error(imageNode->getLine(),
4313 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004314 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03004315 }
4316 }
4317 }
4318}
4319
4320// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
4321void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
4322 const TFunction *functionDefinition,
4323 const TIntermAggregate *functionCall)
4324{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004325 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03004326
4327 const TIntermSequence &arguments = *functionCall->getSequence();
4328
4329 ASSERT(functionDefinition->getParamCount() == arguments.size());
4330
4331 for (size_t i = 0; i < arguments.size(); ++i)
4332 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00004333 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
4334 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03004335 const TType &functionParameterType = *functionDefinition->getParam(i).type;
4336 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
4337
4338 if (IsImage(functionArgumentType.getBasicType()))
4339 {
4340 const TMemoryQualifier &functionArgumentMemoryQualifier =
4341 functionArgumentType.getMemoryQualifier();
4342 const TMemoryQualifier &functionParameterMemoryQualifier =
4343 functionParameterType.getMemoryQualifier();
4344 if (functionArgumentMemoryQualifier.readonly &&
4345 !functionParameterMemoryQualifier.readonly)
4346 {
4347 error(functionCall->getLine(),
4348 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004349 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03004350 }
4351
4352 if (functionArgumentMemoryQualifier.writeonly &&
4353 !functionParameterMemoryQualifier.writeonly)
4354 {
4355 error(functionCall->getLine(),
4356 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004357 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03004358 }
Martin Radev049edfa2016-11-11 14:35:37 +02004359
4360 if (functionArgumentMemoryQualifier.coherent &&
4361 !functionParameterMemoryQualifier.coherent)
4362 {
4363 error(functionCall->getLine(),
4364 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004365 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02004366 }
4367
4368 if (functionArgumentMemoryQualifier.volatileQualifier &&
4369 !functionParameterMemoryQualifier.volatileQualifier)
4370 {
4371 error(functionCall->getLine(),
4372 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00004373 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02004374 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004375 }
4376 }
4377}
4378
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004379TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004380{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004381 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00004382}
4383
4384TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004385 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00004386 TIntermNode *thisNode,
4387 const TSourceLoc &loc)
4388{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004389 if (thisNode != nullptr)
4390 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004391 return addMethod(fnCall, arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03004392 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004393
4394 TOperator op = fnCall->getBuiltInOp();
4395 if (op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004396 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004397 return addConstructor(arguments, op, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004398 }
4399 else
4400 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004401 return addNonConstructorFunctionCall(fnCall, arguments, loc);
4402 }
4403}
4404
4405TIntermTyped *TParseContext::addMethod(TFunction *fnCall,
4406 TIntermSequence *arguments,
4407 TIntermNode *thisNode,
4408 const TSourceLoc &loc)
4409{
4410 TConstantUnion *unionArray = new TConstantUnion[1];
4411 int arraySize = 0;
4412 TIntermTyped *typedThis = thisNode->getAsTyped();
4413 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
4414 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
4415 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
4416 // So accessing fnCall->getName() below is safe.
4417 if (fnCall->getName() != "length")
4418 {
4419 error(loc, "invalid method", fnCall->getName().c_str());
4420 }
4421 else if (!arguments->empty())
4422 {
4423 error(loc, "method takes no parameters", "length");
4424 }
4425 else if (typedThis == nullptr || !typedThis->isArray())
4426 {
4427 error(loc, "length can only be called on arrays", "length");
4428 }
4429 else
4430 {
4431 arraySize = typedThis->getArraySize();
4432 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuaho72d10202017-01-19 15:58:30 +00004433 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004434 // This code path can be hit with expressions like these:
4435 // (a = b).length()
4436 // (func()).length()
4437 // (int[3](0, 1, 2)).length()
4438 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
4439 // expression.
4440 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
4441 // spec section 5.9 which allows "An array, vector or matrix expression with the
4442 // length method applied".
4443 error(loc, "length can only be called on array names, not on array expressions",
4444 "length");
Olli Etuaho72d10202017-01-19 15:58:30 +00004445 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004446 }
4447 unionArray->setIConst(arraySize);
4448 return intermediate.addConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst), loc);
4449}
4450
4451TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunction *fnCall,
4452 TIntermSequence *arguments,
4453 const TSourceLoc &loc)
4454{
4455 // First find by unmangled name to check whether the function name has been
4456 // hidden by a variable name or struct typename.
4457 // If a function is found, check for one with a matching argument list.
4458 bool builtIn;
4459 const TSymbol *symbol = symbolTable.find(fnCall->getName(), mShaderVersion, &builtIn);
4460 if (symbol != nullptr && !symbol->isFunction())
4461 {
4462 error(loc, "function name expected", fnCall->getName().c_str());
4463 }
4464 else
4465 {
4466 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(fnCall->getName(), *arguments),
4467 mShaderVersion, &builtIn);
4468 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004469 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004470 error(loc, "no matching overloaded function found", fnCall->getName().c_str());
4471 }
4472 else
4473 {
4474 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004475 //
4476 // A declared function.
4477 //
Olli Etuaho383b7912016-08-05 11:22:59 +03004478 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004479 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004480 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004481 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004482 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004483 if (builtIn && op != EOpNull)
4484 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004485 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004486 if (fnCandidate->getParamCount() == 1)
4487 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004488 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004489 TIntermNode *unaryParamNode = arguments->front();
4490 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004491 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004492 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004493 }
4494 else
4495 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004496 TIntermAggregate *callNode =
4497 new TIntermAggregate(fnCandidate->getReturnType(), op, arguments);
4498 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004499
4500 // Some built-in functions have out parameters too.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004501 functionCallLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05304502
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004503 // See if we can constant fold a built-in. Note that this may be possible even
4504 // if it is not const-qualified.
Olli Etuahof119a262016-08-19 15:54:22 +03004505 TIntermTyped *foldedNode =
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004506 intermediate.foldAggregateBuiltIn(callNode, mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05304507 if (foldedNode)
4508 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004509 return foldedNode;
Arun Patole274f0702015-05-05 13:33:30 +05304510 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004511 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004512 }
4513 }
4514 else
4515 {
4516 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004517 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004518
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08004519 // If builtIn == false, the function is user defined - could be an overloaded
4520 // built-in as well.
4521 // if builtIn == true, it's a builtIn function with no op associated with it.
4522 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004523 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004524 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004525 callNode = new TIntermAggregate(fnCandidate->getReturnType(),
4526 EOpCallBuiltInFunction, arguments);
4527 // Note that name needs to be set before texture function type is determined.
4528 callNode->getFunctionSymbolInfo()->setFromFunction(*fnCandidate);
4529 callNode->setBuiltInFunctionPrecision();
4530 checkTextureOffsetConst(callNode);
4531 checkImageMemoryAccessForBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03004532 }
4533 else
4534 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004535 callNode = new TIntermAggregate(fnCandidate->getReturnType(),
4536 EOpCallFunctionInAST, arguments);
4537 callNode->getFunctionSymbolInfo()->setFromFunction(*fnCandidate);
4538 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02004539 }
4540
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004541 functionCallLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004542
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004543 callNode->setLine(loc);
4544
4545 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004546 }
4547 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004548 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08004549
4550 // Error message was already written. Put on a dummy node for error recovery.
4551 return TIntermTyped::CreateZero(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02004552}
4553
Jamie Madillb98c3a82015-07-23 14:26:04 -04004554TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004555 TIntermTyped *trueExpression,
4556 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03004557 const TSourceLoc &loc)
4558{
Olli Etuaho856c4972016-08-08 11:38:39 +03004559 checkIsScalarBool(loc, cond);
Olli Etuaho52901742015-04-15 13:42:45 +03004560
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004561 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03004562 {
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004563 binaryOpError(loc, ":", trueExpression->getCompleteString(),
4564 falseExpression->getCompleteString());
4565 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03004566 }
Olli Etuahode318b22016-10-25 16:18:25 +01004567 if (IsOpaqueType(trueExpression->getBasicType()))
4568 {
4569 // ESSL 1.00 section 4.1.7
4570 // ESSL 3.00 section 4.1.7
4571 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
4572 // Note that structs containing opaque types don't need to be checked as structs are
4573 // forbidden below.
4574 error(loc, "ternary operator is not allowed for opaque types", ":");
4575 return falseExpression;
4576 }
4577
Olli Etuahoa2d53032015-04-15 14:14:44 +03004578 // ESSL1 sections 5.2 and 5.7:
4579 // ESSL3 section 5.7:
4580 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004581 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03004582 {
4583 error(loc, "ternary operator is not allowed for structures or arrays", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004584 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03004585 }
Corentin Wallez0d959252016-07-12 17:26:32 -04004586 // WebGL2 section 5.26, the following results in an error:
4587 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004588 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04004589 {
4590 error(loc, "ternary operator is not allowed for void", ":");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004591 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04004592 }
4593
Olli Etuahod0bad2c2016-09-09 18:01:16 +03004594 return TIntermediate::AddTernarySelection(cond, trueExpression, falseExpression, loc);
Olli Etuaho52901742015-04-15 13:42:45 +03004595}
Olli Etuaho49300862015-02-20 14:54:49 +02004596
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004597//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004598// Parse an array of strings using yyparse.
4599//
4600// Returns 0 for success.
4601//
Jamie Madillb98c3a82015-07-23 14:26:04 -04004602int PaParseStrings(size_t count,
4603 const char *const string[],
4604 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05304605 TParseContext *context)
4606{
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004607 if ((count == 0) || (string == NULL))
4608 return 1;
4609
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004610 if (glslang_initialize(context))
4611 return 1;
4612
alokp@chromium.org408c45e2012-04-05 15:54:43 +00004613 int error = glslang_scan(count, string, length, context);
4614 if (!error)
4615 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004616
alokp@chromium.org73bc2982012-06-19 18:48:05 +00004617 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00004618
alokp@chromium.org6b495712012-06-29 00:06:58 +00004619 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00004620}
Jamie Madill45bcc782016-11-07 13:58:48 -05004621
4622} // namespace sh