blob: 86a88d24552990f34dcfb8d6ca19b2d96f59f70c [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
jchen104cdac9e2017-05-08 11:01:20 +080012#include "common/mathutil.h"
daniel@transgaming.comb401a922012-10-26 18:58:24 +000013#include "compiler/preprocessor/SourceLocation.h"
Dmitry Skiba01971112015-07-10 14:54:00 -040014#include "compiler/translator/Cache.h"
Olli Etuaho3ec75682017-07-05 17:02:55 +030015#include "compiler/translator/IntermNode_util.h"
Olli Etuahob0c645e2015-05-12 14:25:36 +030016#include "compiler/translator/ValidateGlobalInitializer.h"
jchen104cdac9e2017-05-08 11:01:20 +080017#include "compiler/translator/ValidateSwitch.h"
18#include "compiler/translator/glslang.h"
Olli Etuaho37ad4742015-04-27 13:18:50 +030019#include "compiler/translator/util.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000020
Jamie Madill45bcc782016-11-07 13:58:48 -050021namespace sh
22{
23
alokp@chromium.org8b851c62012-06-15 16:25:11 +000024///////////////////////////////////////////////////////////////////////
25//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000026// Sub- vector and matrix fields
27//
28////////////////////////////////////////////////////////////////////////
29
Martin Radev2cc85b32016-08-05 16:22:53 +030030namespace
31{
32
33const int kWebGLMaxStructNesting = 4;
34
Olli Etuaho0f684632017-07-13 12:42:15 +030035bool ContainsSampler(const TStructure *structType);
36
Martin Radev2cc85b32016-08-05 16:22:53 +030037bool ContainsSampler(const TType &type)
38{
39 if (IsSampler(type.getBasicType()))
Olli Etuaho0f684632017-07-13 12:42:15 +030040 {
Martin Radev2cc85b32016-08-05 16:22:53 +030041 return true;
Olli Etuaho0f684632017-07-13 12:42:15 +030042 }
jchen10cc2a10e2017-05-03 14:05:12 +080043 if (type.getBasicType() == EbtStruct)
Martin Radev2cc85b32016-08-05 16:22:53 +030044 {
Olli Etuaho0f684632017-07-13 12:42:15 +030045 return ContainsSampler(type.getStruct());
Martin Radev2cc85b32016-08-05 16:22:53 +030046 }
47
48 return false;
49}
50
Olli Etuaho0f684632017-07-13 12:42:15 +030051bool ContainsSampler(const TStructure *structType)
52{
53 for (const auto &field : structType->fields())
54 {
55 if (ContainsSampler(*field->type()))
56 return true;
57 }
58 return false;
59}
60
Olli Etuaho485eefd2017-02-14 17:40:06 +000061// Get a token from an image argument to use as an error message token.
62const char *GetImageArgumentToken(TIntermTyped *imageNode)
63{
64 ASSERT(IsImage(imageNode->getBasicType()));
65 while (imageNode->getAsBinaryNode() &&
66 (imageNode->getAsBinaryNode()->getOp() == EOpIndexIndirect ||
67 imageNode->getAsBinaryNode()->getOp() == EOpIndexDirect))
68 {
69 imageNode = imageNode->getAsBinaryNode()->getLeft();
70 }
71 TIntermSymbol *imageSymbol = imageNode->getAsSymbolNode();
72 if (imageSymbol)
73 {
74 return imageSymbol->getSymbol().c_str();
75 }
76 return "image";
77}
78
Olli Etuahocce89652017-06-19 16:04:09 +030079bool CanSetDefaultPrecisionOnType(const TPublicType &type)
80{
81 if (!SupportsPrecision(type.getBasicType()))
82 {
83 return false;
84 }
85 if (type.getBasicType() == EbtUInt)
86 {
87 // ESSL 3.00.4 section 4.5.4
88 return false;
89 }
90 if (type.isAggregate())
91 {
92 // Not allowed to set for aggregate types
93 return false;
94 }
95 return true;
96}
97
Jiawei Shaod8105a02017-08-08 09:54:36 +080098// Map input primitive types to input array sizes in a geometry shader.
99GLuint GetGeometryShaderInputArraySize(TLayoutPrimitiveType primitiveType)
100{
101 switch (primitiveType)
102 {
103 case EptPoints:
104 return 1u;
105 case EptLines:
106 return 2u;
107 case EptTriangles:
108 return 3u;
109 case EptLinesAdjacency:
110 return 4u;
111 case EptTrianglesAdjacency:
112 return 6u;
113 default:
114 UNREACHABLE();
115 return 0u;
116 }
117}
118
Martin Radev2cc85b32016-08-05 16:22:53 +0300119} // namespace
120
jchen104cdac9e2017-05-08 11:01:20 +0800121// This tracks each binding point's current default offset for inheritance of subsequent
122// variables using the same binding, and keeps offsets unique and non overlapping.
123// See GLSL ES 3.1, section 4.4.6.
124class TParseContext::AtomicCounterBindingState
125{
126 public:
127 AtomicCounterBindingState() : mDefaultOffset(0) {}
128 // Inserts a new span and returns -1 if overlapping, else returns the starting offset of
129 // newly inserted span.
130 int insertSpan(int start, size_t length)
131 {
132 gl::RangeI newSpan(start, start + static_cast<int>(length));
133 for (const auto &span : mSpans)
134 {
135 if (newSpan.intersects(span))
136 {
137 return -1;
138 }
139 }
140 mSpans.push_back(newSpan);
141 mDefaultOffset = newSpan.high();
142 return start;
143 }
144 // Inserts a new span starting from the default offset.
145 int appendSpan(size_t length) { return insertSpan(mDefaultOffset, length); }
146 void setDefaultOffset(int offset) { mDefaultOffset = offset; }
147
148 private:
149 int mDefaultOffset;
150 std::vector<gl::RangeI> mSpans;
151};
152
Jamie Madillacb4b812016-11-07 13:50:29 -0500153TParseContext::TParseContext(TSymbolTable &symt,
154 TExtensionBehavior &ext,
155 sh::GLenum type,
156 ShShaderSpec spec,
157 ShCompileOptions options,
158 bool checksPrecErrors,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000159 TDiagnostics *diagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500160 const ShBuiltInResources &resources)
Olli Etuaho56229f12017-07-10 14:16:33 +0300161 : symbolTable(symt),
Olli Etuahobb7e5a72017-04-24 10:16:44 +0300162 mDeferredNonEmptyDeclarationErrorCheck(false),
Jamie Madillacb4b812016-11-07 13:50:29 -0500163 mShaderType(type),
164 mShaderSpec(spec),
165 mCompileOptions(options),
166 mShaderVersion(100),
167 mTreeRoot(nullptr),
168 mLoopNestingLevel(0),
169 mStructNestingLevel(0),
170 mSwitchNestingLevel(0),
171 mCurrentFunctionType(nullptr),
172 mFunctionReturnsValue(false),
173 mChecksPrecisionErrors(checksPrecErrors),
174 mFragmentPrecisionHighOnESSL1(false),
Jiajia Qinbc585152017-06-23 15:42:17 +0800175 mDefaultUniformMatrixPacking(EmpColumnMajor),
176 mDefaultUniformBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
177 mDefaultBufferMatrixPacking(EmpColumnMajor),
178 mDefaultBufferBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000179 mDiagnostics(diagnostics),
Jamie Madillacb4b812016-11-07 13:50:29 -0500180 mDirectiveHandler(ext,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000181 *mDiagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500182 mShaderVersion,
183 mShaderType,
184 resources.WEBGL_debug_shader_precision == 1),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000185 mPreprocessor(mDiagnostics, &mDirectiveHandler, pp::PreprocessorSettings()),
Jamie Madillacb4b812016-11-07 13:50:29 -0500186 mScanner(nullptr),
187 mUsesFragData(false),
188 mUsesFragColor(false),
189 mUsesSecondaryOutputs(false),
190 mMinProgramTexelOffset(resources.MinProgramTexelOffset),
191 mMaxProgramTexelOffset(resources.MaxProgramTexelOffset),
Olli Etuaho09b04a22016-12-15 13:30:26 +0000192 mMultiviewAvailable(resources.OVR_multiview == 1),
Jamie Madillacb4b812016-11-07 13:50:29 -0500193 mComputeShaderLocalSizeDeclared(false),
Olli Etuaho09b04a22016-12-15 13:30:26 +0000194 mNumViews(-1),
195 mMaxNumViews(resources.MaxViewsOVR),
Olli Etuaho43364892017-02-13 16:00:12 +0000196 mMaxImageUnits(resources.MaxImageUnits),
197 mMaxCombinedTextureImageUnits(resources.MaxCombinedTextureImageUnits),
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000198 mMaxUniformLocations(resources.MaxUniformLocations),
jchen10af713a22017-04-19 09:10:56 +0800199 mMaxUniformBufferBindings(resources.MaxUniformBufferBindings),
jchen104cdac9e2017-05-08 11:01:20 +0800200 mMaxAtomicCounterBindings(resources.MaxAtomicCounterBindings),
Jiajia Qinbc585152017-06-23 15:42:17 +0800201 mMaxShaderStorageBufferBindings(resources.MaxShaderStorageBufferBindings),
Shaob5cc1192017-07-06 10:47:20 +0800202 mDeclaringFunction(false),
203 mGeometryShaderInputPrimitiveType(EptUndefined),
204 mGeometryShaderOutputPrimitiveType(EptUndefined),
205 mGeometryShaderInvocations(0),
206 mGeometryShaderMaxVertices(-1),
207 mMaxGeometryShaderInvocations(resources.MaxGeometryShaderInvocations),
Jiawei Shaod8105a02017-08-08 09:54:36 +0800208 mMaxGeometryShaderMaxVertices(resources.MaxGeometryOutputVertices),
209 mGeometryShaderInputArraySize(0)
Jamie Madillacb4b812016-11-07 13:50:29 -0500210{
211 mComputeShaderLocalSize.fill(-1);
212}
213
jchen104cdac9e2017-05-08 11:01:20 +0800214TParseContext::~TParseContext()
215{
216}
217
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300218bool TParseContext::parseVectorFields(const TSourceLoc &line,
219 const TString &compString,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400220 int vecSize,
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300221 TVector<int> *fieldOffsets)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000222{
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300223 ASSERT(fieldOffsets);
224 size_t fieldCount = compString.size();
225 if (fieldCount > 4u)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530226 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000227 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000228 return false;
229 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300230 fieldOffsets->resize(fieldCount);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000231
Jamie Madillb98c3a82015-07-23 14:26:04 -0400232 enum
233 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000234 exyzw,
235 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +0000236 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000237 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000238
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300239 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530240 {
241 switch (compString[i])
242 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400243 case 'x':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300244 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400245 fieldSet[i] = exyzw;
246 break;
247 case 'r':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300248 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400249 fieldSet[i] = ergba;
250 break;
251 case 's':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300252 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400253 fieldSet[i] = estpq;
254 break;
255 case 'y':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300256 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400257 fieldSet[i] = exyzw;
258 break;
259 case 'g':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300260 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400261 fieldSet[i] = ergba;
262 break;
263 case 't':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300264 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400265 fieldSet[i] = estpq;
266 break;
267 case 'z':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300268 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400269 fieldSet[i] = exyzw;
270 break;
271 case 'b':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300272 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400273 fieldSet[i] = ergba;
274 break;
275 case 'p':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300276 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400277 fieldSet[i] = estpq;
278 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530279
Jamie Madillb98c3a82015-07-23 14:26:04 -0400280 case 'w':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300281 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400282 fieldSet[i] = exyzw;
283 break;
284 case 'a':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300285 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400286 fieldSet[i] = ergba;
287 break;
288 case 'q':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300289 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400290 fieldSet[i] = estpq;
291 break;
292 default:
293 error(line, "illegal vector field selection", compString.c_str());
294 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000295 }
296 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000297
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300298 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530299 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300300 if ((*fieldOffsets)[i] >= vecSize)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530301 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400302 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000303 return false;
304 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000305
Arun Patole7e7e68d2015-05-22 12:02:25 +0530306 if (i > 0)
307 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400308 if (fieldSet[i] != fieldSet[i - 1])
Arun Patole7e7e68d2015-05-22 12:02:25 +0530309 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400310 error(line, "illegal - vector component fields not from the same set",
311 compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000312 return false;
313 }
314 }
315 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000316
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000317 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000318}
319
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000320///////////////////////////////////////////////////////////////////////
321//
322// Errors
323//
324////////////////////////////////////////////////////////////////////////
325
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000326//
327// Used by flex/bison to output all syntax and parsing errors.
328//
Olli Etuaho4de340a2016-12-16 09:32:03 +0000329void TParseContext::error(const TSourceLoc &loc, const char *reason, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000330{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000331 mDiagnostics->error(loc, reason, token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000332}
333
Olli Etuaho4de340a2016-12-16 09:32:03 +0000334void TParseContext::warning(const TSourceLoc &loc, const char *reason, const char *token)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530335{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000336 mDiagnostics->warning(loc, reason, token);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000337}
338
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200339void TParseContext::outOfRangeError(bool isError,
340 const TSourceLoc &loc,
341 const char *reason,
Olli Etuaho4de340a2016-12-16 09:32:03 +0000342 const char *token)
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200343{
344 if (isError)
345 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000346 error(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200347 }
348 else
349 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000350 warning(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200351 }
352}
353
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000354//
355// Same error message for all places assignments don't work.
356//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530357void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000358{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000359 std::stringstream reasonStream;
360 reasonStream << "cannot convert from '" << right << "' to '" << left << "'";
361 std::string reason = reasonStream.str();
362 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000363}
364
365//
366// Same error message for all places unary operations don't work.
367//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530368void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000369{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000370 std::stringstream reasonStream;
371 reasonStream << "wrong operand type - no operation '" << op
372 << "' exists that takes an operand of type " << operand
373 << " (or there is no acceptable conversion)";
374 std::string reason = reasonStream.str();
375 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000376}
377
378//
379// Same error message for all binary operations don't work.
380//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400381void TParseContext::binaryOpError(const TSourceLoc &line,
382 const char *op,
383 TString left,
384 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000385{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000386 std::stringstream reasonStream;
387 reasonStream << "wrong operand types - no operation '" << op
388 << "' exists that takes a left-hand operand of type '" << left
389 << "' and a right operand of type '" << right
390 << "' (or there is no acceptable conversion)";
391 std::string reason = reasonStream.str();
392 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000393}
394
Olli Etuaho856c4972016-08-08 11:38:39 +0300395void TParseContext::checkPrecisionSpecified(const TSourceLoc &line,
396 TPrecision precision,
397 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530398{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400399 if (!mChecksPrecisionErrors)
Olli Etuaho383b7912016-08-05 11:22:59 +0300400 return;
Martin Radev70866b82016-07-22 15:27:42 +0300401
402 if (precision != EbpUndefined && !SupportsPrecision(type))
403 {
404 error(line, "illegal type for precision qualifier", getBasicString(type));
405 }
406
Olli Etuaho183d7e22015-11-20 15:59:09 +0200407 if (precision == EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530408 {
Olli Etuaho183d7e22015-11-20 15:59:09 +0200409 switch (type)
410 {
411 case EbtFloat:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400412 error(line, "No precision specified for (float)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300413 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200414 case EbtInt:
415 case EbtUInt:
416 UNREACHABLE(); // there's always a predeclared qualifier
Jamie Madillb98c3a82015-07-23 14:26:04 -0400417 error(line, "No precision specified (int)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300418 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200419 default:
jchen10cc2a10e2017-05-03 14:05:12 +0800420 if (IsOpaqueType(type))
Olli Etuaho183d7e22015-11-20 15:59:09 +0200421 {
jchen10cc2a10e2017-05-03 14:05:12 +0800422 error(line, "No precision specified", getBasicString(type));
Martin Radev2cc85b32016-08-05 16:22:53 +0300423 return;
424 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200425 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000426 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000427}
428
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000429// Both test and if necessary, spit out an error, to see if the node is really
430// an l-value that can be operated on this way.
Olli Etuaho856c4972016-08-08 11:38:39 +0300431bool TParseContext::checkCanBeLValue(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000432{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500433 TIntermSymbol *symNode = node->getAsSymbolNode();
434 TIntermBinary *binaryNode = node->getAsBinaryNode();
Olli Etuahob6fa0432016-09-28 16:28:05 +0100435 TIntermSwizzle *swizzleNode = node->getAsSwizzleNode();
436
437 if (swizzleNode)
438 {
439 bool ok = checkCanBeLValue(line, op, swizzleNode->getOperand());
440 if (ok && swizzleNode->hasDuplicateOffsets())
441 {
442 error(line, " l-value of swizzle cannot have duplicate components", op);
443 return false;
444 }
445 return ok;
446 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000447
Arun Patole7e7e68d2015-05-22 12:02:25 +0530448 if (binaryNode)
449 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400450 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530451 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400452 case EOpIndexDirect:
453 case EOpIndexIndirect:
454 case EOpIndexDirectStruct:
455 case EOpIndexDirectInterfaceBlock:
Olli Etuaho856c4972016-08-08 11:38:39 +0300456 return checkCanBeLValue(line, op, binaryNode->getLeft());
Jamie Madillb98c3a82015-07-23 14:26:04 -0400457 default:
458 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000459 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000460 error(line, " l-value required", op);
Olli Etuaho8a176262016-08-16 14:23:01 +0300461 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000462 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000463
jchen10cc2a10e2017-05-03 14:05:12 +0800464 std::string message;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530465 switch (node->getQualifier())
466 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400467 case EvqConst:
468 message = "can't modify a const";
469 break;
470 case EvqConstReadOnly:
471 message = "can't modify a const";
472 break;
473 case EvqAttribute:
474 message = "can't modify an attribute";
475 break;
476 case EvqFragmentIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400477 case EvqVertexIn:
Jiawei Shaoe8ef2bc2017-08-29 13:38:57 +0800478 case EvqFlatIn:
479 case EvqSmoothIn:
480 case EvqCentroidIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400481 message = "can't modify an input";
482 break;
483 case EvqUniform:
484 message = "can't modify a uniform";
485 break;
486 case EvqVaryingIn:
487 message = "can't modify a varying";
488 break;
489 case EvqFragCoord:
490 message = "can't modify gl_FragCoord";
491 break;
492 case EvqFrontFacing:
493 message = "can't modify gl_FrontFacing";
494 break;
495 case EvqPointCoord:
496 message = "can't modify gl_PointCoord";
497 break;
Martin Radevb0883602016-08-04 17:48:58 +0300498 case EvqNumWorkGroups:
499 message = "can't modify gl_NumWorkGroups";
500 break;
501 case EvqWorkGroupSize:
502 message = "can't modify gl_WorkGroupSize";
503 break;
504 case EvqWorkGroupID:
505 message = "can't modify gl_WorkGroupID";
506 break;
507 case EvqLocalInvocationID:
508 message = "can't modify gl_LocalInvocationID";
509 break;
510 case EvqGlobalInvocationID:
511 message = "can't modify gl_GlobalInvocationID";
512 break;
513 case EvqLocalInvocationIndex:
514 message = "can't modify gl_LocalInvocationIndex";
515 break;
Olli Etuaho7142f6c2017-05-05 17:07:26 +0300516 case EvqViewIDOVR:
517 message = "can't modify gl_ViewID_OVR";
518 break;
Martin Radev802abe02016-08-04 17:48:32 +0300519 case EvqComputeIn:
520 message = "can't modify work group size variable";
521 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +0800522 case EvqPerVertexIn:
523 message = "can't modify any member in gl_in";
524 break;
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800525 case EvqPrimitiveIDIn:
526 message = "can't modify gl_PrimitiveIDIn";
527 break;
528 case EvqInvocationID:
529 message = "can't modify gl_InvocationID";
530 break;
531 case EvqPrimitiveID:
532 if (mShaderType == GL_FRAGMENT_SHADER)
533 {
534 message = "can't modify gl_PrimitiveID in a fragment shader";
535 }
536 break;
537 case EvqLayer:
538 if (mShaderType == GL_FRAGMENT_SHADER)
539 {
540 message = "can't modify gl_Layer in a fragment shader";
541 }
542 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400543 default:
544 //
545 // Type that can't be written to?
546 //
547 if (node->getBasicType() == EbtVoid)
548 {
549 message = "can't modify void";
550 }
jchen10cc2a10e2017-05-03 14:05:12 +0800551 if (IsOpaqueType(node->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -0400552 {
jchen10cc2a10e2017-05-03 14:05:12 +0800553 message = "can't modify a variable with type ";
554 message += getBasicString(node->getBasicType());
Martin Radev2cc85b32016-08-05 16:22:53 +0300555 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800556 else if (node->getMemoryQualifier().readonly)
557 {
558 message = "can't modify a readonly variable";
559 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000560 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000561
jchen10cc2a10e2017-05-03 14:05:12 +0800562 if (message.empty() && binaryNode == 0 && symNode == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530563 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000564 error(line, "l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000565
Olli Etuaho8a176262016-08-16 14:23:01 +0300566 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000567 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000568
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000569 //
570 // Everything else is okay, no error.
571 //
jchen10cc2a10e2017-05-03 14:05:12 +0800572 if (message.empty())
Olli Etuaho8a176262016-08-16 14:23:01 +0300573 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000574
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000575 //
576 // If we get here, we have an error and a message.
577 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530578 if (symNode)
579 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000580 const char *symbol = symNode->getSymbol().c_str();
581 std::stringstream reasonStream;
582 reasonStream << "l-value required (" << message << " \"" << symbol << "\")";
583 std::string reason = reasonStream.str();
584 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000585 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530586 else
587 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000588 std::stringstream reasonStream;
589 reasonStream << "l-value required (" << message << ")";
590 std::string reason = reasonStream.str();
591 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000592 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000593
Olli Etuaho8a176262016-08-16 14:23:01 +0300594 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000595}
596
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000597// Both test, and if necessary spit out an error, to see if the node is really
598// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300599void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000600{
Olli Etuaho383b7912016-08-05 11:22:59 +0300601 if (node->getQualifier() != EvqConst)
602 {
603 error(node->getLine(), "constant expression required", "");
604 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000605}
606
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000607// Both test, and if necessary spit out an error, to see if the node is really
608// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300609void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000610{
Olli Etuaho383b7912016-08-05 11:22:59 +0300611 if (!node->isScalarInt())
612 {
613 error(node->getLine(), "integer expression required", token);
614 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000615}
616
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000617// Both test, and if necessary spit out an error, to see if we are currently
618// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800619bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000620{
Olli Etuaho856c4972016-08-08 11:38:39 +0300621 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300622 {
623 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800624 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300625 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800626 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000627}
628
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300629// ESSL 3.00.5 sections 3.8 and 3.9.
630// If it starts "gl_" or contains two consecutive underscores, it's reserved.
631// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a webgl shader.
Olli Etuaho856c4972016-08-08 11:38:39 +0300632bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000633{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530634 static const char *reservedErrMsg = "reserved built-in name";
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300635 if (identifier.compare(0, 3, "gl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530636 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300637 error(line, reservedErrMsg, "gl_");
638 return false;
639 }
640 if (sh::IsWebGLBasedSpec(mShaderSpec))
641 {
642 if (identifier.compare(0, 6, "webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530643 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300644 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300645 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000646 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300647 if (identifier.compare(0, 7, "_webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530648 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300649 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300650 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000651 }
652 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300653 if (identifier.find("__") != TString::npos)
654 {
655 error(line,
656 "identifiers containing two consecutive underscores (__) are reserved as "
657 "possible future keywords",
658 identifier.c_str());
659 return false;
660 }
Olli Etuaho8a176262016-08-16 14:23:01 +0300661 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000662}
663
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300664// Make sure the argument types are correct for constructing a specific type.
Olli Etuaho856c4972016-08-08 11:38:39 +0300665bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800666 const TIntermSequence *arguments,
Olli Etuaho856c4972016-08-08 11:38:39 +0300667 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000668{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800669 if (arguments->empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530670 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200671 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300672 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000673 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200674
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300675 for (TIntermNode *arg : *arguments)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530676 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300677 const TIntermTyped *argTyped = arg->getAsTyped();
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200678 ASSERT(argTyped != nullptr);
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300679 if (type.getBasicType() != EbtStruct && IsOpaqueType(argTyped->getBasicType()))
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200680 {
jchen10cc2a10e2017-05-03 14:05:12 +0800681 std::string reason("cannot convert a variable with type ");
682 reason += getBasicString(argTyped->getBasicType());
683 error(line, reason.c_str(), "constructor");
Martin Radev2cc85b32016-08-05 16:22:53 +0300684 return false;
685 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800686 else if (argTyped->getMemoryQualifier().writeonly)
687 {
688 error(line, "cannot convert a variable with writeonly", "constructor");
689 return false;
690 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200691 if (argTyped->getBasicType() == EbtVoid)
692 {
693 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300694 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200695 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000696 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000697
Olli Etuaho856c4972016-08-08 11:38:39 +0300698 if (type.isArray())
699 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300700 // The size of an unsized constructor should already have been determined.
701 ASSERT(!type.isUnsizedArray());
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300702 if (static_cast<size_t>(type.getOutermostArraySize()) != arguments->size())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300703 {
704 error(line, "array constructor needs one argument per array element", "constructor");
705 return false;
706 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300707 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
708 // the array.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800709 for (TIntermNode *const &argNode : *arguments)
Olli Etuaho856c4972016-08-08 11:38:39 +0300710 {
711 const TType &argType = argNode->getAsTyped()->getType();
Jamie Madill34bf2d92017-02-06 13:40:59 -0500712 if (argType.isArray())
713 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300714 error(line, "constructing from a non-dereferenced array", "constructor");
Jamie Madill34bf2d92017-02-06 13:40:59 -0500715 return false;
716 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300717 if (!argType.isElementTypeOf(type))
Olli Etuaho856c4972016-08-08 11:38:39 +0300718 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000719 error(line, "Array constructor argument has an incorrect type", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300720 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300721 }
722 }
723 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300724 else if (type.getBasicType() == EbtStruct)
Olli Etuaho856c4972016-08-08 11:38:39 +0300725 {
726 const TFieldList &fields = type.getStruct()->fields();
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300727 if (fields.size() != arguments->size())
728 {
729 error(line,
730 "Number of constructor parameters does not match the number of structure fields",
731 "constructor");
732 return false;
733 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300734
735 for (size_t i = 0; i < fields.size(); i++)
736 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800737 if (i >= arguments->size() ||
738 (*arguments)[i]->getAsTyped()->getType() != *fields[i]->type())
Olli Etuaho856c4972016-08-08 11:38:39 +0300739 {
740 error(line, "Structure constructor arguments do not match structure fields",
Olli Etuaho4de340a2016-12-16 09:32:03 +0000741 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300742 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300743 }
744 }
745 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300746 else
747 {
748 // We're constructing a scalar, vector, or matrix.
749
750 // Note: It's okay to have too many components available, but not okay to have unused
751 // arguments. 'full' will go to true when enough args have been seen. If we loop again,
752 // there is an extra argument, so 'overFull' will become true.
753
754 size_t size = 0;
755 bool full = false;
756 bool overFull = false;
757 bool matrixArg = false;
758 for (TIntermNode *arg : *arguments)
759 {
760 const TIntermTyped *argTyped = arg->getAsTyped();
761 ASSERT(argTyped != nullptr);
762
Olli Etuaho487b63a2017-05-23 15:55:09 +0300763 if (argTyped->getBasicType() == EbtStruct)
764 {
765 error(line, "a struct cannot be used as a constructor argument for this type",
766 "constructor");
767 return false;
768 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300769 if (argTyped->getType().isArray())
770 {
771 error(line, "constructing from a non-dereferenced array", "constructor");
772 return false;
773 }
774 if (argTyped->getType().isMatrix())
775 {
776 matrixArg = true;
777 }
778
779 size += argTyped->getType().getObjectSize();
780 if (full)
781 {
782 overFull = true;
783 }
Olli Etuaho487b63a2017-05-23 15:55:09 +0300784 if (size >= type.getObjectSize())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300785 {
786 full = true;
787 }
788 }
789
790 if (type.isMatrix() && matrixArg)
791 {
792 if (arguments->size() != 1)
793 {
794 error(line, "constructing matrix from matrix can only take one argument",
795 "constructor");
796 return false;
797 }
798 }
799 else
800 {
801 if (size != 1 && size < type.getObjectSize())
802 {
803 error(line, "not enough data provided for construction", "constructor");
804 return false;
805 }
806 if (overFull)
807 {
808 error(line, "too many arguments", "constructor");
809 return false;
810 }
811 }
812 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300813
Olli Etuaho8a176262016-08-16 14:23:01 +0300814 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000815}
816
Jamie Madillb98c3a82015-07-23 14:26:04 -0400817// This function checks to see if a void variable has been declared and raise an error message for
818// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000819//
820// returns true in case of an error
821//
Olli Etuaho856c4972016-08-08 11:38:39 +0300822bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400823 const TString &identifier,
824 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000825{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300826 if (type == EbtVoid)
827 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000828 error(line, "illegal use of type 'void'", identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300829 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300830 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000831
Olli Etuaho8a176262016-08-16 14:23:01 +0300832 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000833}
834
Jamie Madillb98c3a82015-07-23 14:26:04 -0400835// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300836// or not.
Olli Etuaho56229f12017-07-10 14:16:33 +0300837bool TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000838{
Olli Etuaho37d96cc2017-07-11 14:14:03 +0300839 if (type->getBasicType() != EbtBool || !type->isScalar())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530840 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000841 error(line, "boolean expression expected", "");
Olli Etuaho56229f12017-07-10 14:16:33 +0300842 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530843 }
Olli Etuaho56229f12017-07-10 14:16:33 +0300844 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000845}
846
Jamie Madillb98c3a82015-07-23 14:26:04 -0400847// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300848// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300849void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000850{
Martin Radev4a9cd802016-09-01 16:51:51 +0300851 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530852 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000853 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530854 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000855}
856
jchen10cc2a10e2017-05-03 14:05:12 +0800857bool TParseContext::checkIsNotOpaqueType(const TSourceLoc &line,
858 const TTypeSpecifierNonArray &pType,
859 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000860{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530861 if (pType.type == EbtStruct)
862 {
Olli Etuaho0f684632017-07-13 12:42:15 +0300863 if (ContainsSampler(pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530864 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000865 std::stringstream reasonStream;
866 reasonStream << reason << " (structure contains a sampler)";
867 std::string reasonStr = reasonStream.str();
868 error(line, reasonStr.c_str(), getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300869 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000870 }
jchen10cc2a10e2017-05-03 14:05:12 +0800871 // only samplers need to be checked from structs, since other opaque types can't be struct
872 // members.
Olli Etuaho8a176262016-08-16 14:23:01 +0300873 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530874 }
jchen10cc2a10e2017-05-03 14:05:12 +0800875 else if (IsOpaqueType(pType.type))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530876 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000877 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300878 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000879 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000880
Olli Etuaho8a176262016-08-16 14:23:01 +0300881 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000882}
883
Olli Etuaho856c4972016-08-08 11:38:39 +0300884void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
885 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400886{
887 if (pType.layoutQualifier.location != -1)
888 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400889 error(line, "location must only be specified for a single input or output variable",
890 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400891 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400892}
893
Olli Etuaho856c4972016-08-08 11:38:39 +0300894void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
895 const TLayoutQualifier &layoutQualifier)
896{
897 if (layoutQualifier.location != -1)
898 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000899 const char *errorMsg = "invalid layout qualifier: only valid on program inputs and outputs";
900 if (mShaderVersion >= 310)
901 {
902 errorMsg =
903 "invalid layout qualifier: only valid on program inputs, outputs, and uniforms";
904 }
905 error(location, errorMsg, "location");
Olli Etuaho856c4972016-08-08 11:38:39 +0300906 }
907}
908
Martin Radev2cc85b32016-08-05 16:22:53 +0300909void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
910 TQualifier qualifier,
911 const TType &type)
912{
Martin Radev2cc85b32016-08-05 16:22:53 +0300913 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
jchen10cc2a10e2017-05-03 14:05:12 +0800914 if (IsOpaqueType(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530915 {
jchen10cc2a10e2017-05-03 14:05:12 +0800916 error(line, "opaque types cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000917 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000918}
919
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000920// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300921unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000922{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530923 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000924
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200925 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
926 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
927 // fold as array size.
928 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000929 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000930 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300931 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000932 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000933
Olli Etuaho856c4972016-08-08 11:38:39 +0300934 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400935
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000936 if (constant->getBasicType() == EbtUInt)
937 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300938 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000939 }
940 else
941 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300942 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000943
Olli Etuaho856c4972016-08-08 11:38:39 +0300944 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000945 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400946 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300947 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000948 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400949
Olli Etuaho856c4972016-08-08 11:38:39 +0300950 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400951 }
952
Olli Etuaho856c4972016-08-08 11:38:39 +0300953 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400954 {
955 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300956 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400957 }
958
959 // The size of arrays is restricted here to prevent issues further down the
960 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
961 // 4096 registers so this should be reasonable even for aggressively optimizable code.
962 const unsigned int sizeLimit = 65536;
963
Olli Etuaho856c4972016-08-08 11:38:39 +0300964 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -0400965 {
966 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300967 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000968 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300969
970 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000971}
972
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000973// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +0300974bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
975 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000976{
Olli Etuaho8a176262016-08-16 14:23:01 +0300977 if ((elementQualifier.qualifier == EvqAttribute) ||
978 (elementQualifier.qualifier == EvqVertexIn) ||
979 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300980 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400981 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300982 TType(elementQualifier).getQualifierString());
983 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000984 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000985
Olli Etuaho8a176262016-08-16 14:23:01 +0300986 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000987}
988
Olli Etuaho8a176262016-08-16 14:23:01 +0300989// See if this element type can be formed into an array.
Olli Etuahoe0803872017-08-23 15:30:23 +0300990bool TParseContext::checkArrayElementIsNotArray(const TSourceLoc &line,
991 const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000992{
Olli Etuaho8a176262016-08-16 14:23:01 +0300993 if (elementType.array)
Jamie Madill06145232015-05-13 13:10:01 -0400994 {
Olli Etuaho8a176262016-08-16 14:23:01 +0300995 error(line, "cannot declare arrays of arrays",
996 TType(elementType).getCompleteString().c_str());
997 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000998 }
Olli Etuahoe0803872017-08-23 15:30:23 +0300999 return true;
1000}
1001
1002// Check if this qualified element type can be formed into an array. This is only called when array
1003// brackets are associated with an identifier in a declaration, like this:
1004// float a[2];
1005// Similar checks are done in addFullySpecifiedType for array declarations where the array brackets
1006// are associated with the type, like this:
1007// float[2] a;
1008bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
1009 const TPublicType &elementType)
1010{
1011 if (!checkArrayElementIsNotArray(indexLocation, elementType))
1012 {
1013 return false;
1014 }
Olli Etuahocc36b982015-07-10 14:14:18 +03001015 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
1016 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
1017 // 4.3.4).
Martin Radev4a9cd802016-09-01 16:51:51 +03001018 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Olli Etuaho8a176262016-08-16 14:23:01 +03001019 sh::IsVarying(elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +03001020 {
Olli Etuahoe0803872017-08-23 15:30:23 +03001021 error(indexLocation, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001022 TType(elementType).getCompleteString().c_str());
1023 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +03001024 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001025 return checkIsValidQualifierForArray(indexLocation, elementType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001026}
1027
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001028// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +03001029void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
1030 const TString &identifier,
1031 TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001032{
Olli Etuaho3739d232015-04-08 12:23:44 +03001033 ASSERT(type != nullptr);
1034 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001035 {
1036 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +03001037 type->qualifier = EvqTemporary;
1038
1039 // Generate informative error messages for ESSL1.
1040 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001041 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001042 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301043 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001044 "structures containing arrays may not be declared constant since they cannot be "
1045 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +05301046 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001047 }
1048 else
1049 {
1050 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
1051 }
Olli Etuaho383b7912016-08-05 11:22:59 +03001052 return;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001053 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001054 if (type->isUnsizedArray())
1055 {
1056 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
Olli Etuaho376f1b52015-04-13 13:23:41 +03001057 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001058}
1059
Olli Etuaho2935c582015-04-08 14:32:06 +03001060// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001061// and update the symbol table.
1062//
Olli Etuaho2935c582015-04-08 14:32:06 +03001063// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001064//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001065bool TParseContext::declareVariable(const TSourceLoc &line,
1066 const TString &identifier,
1067 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001068 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001069{
Olli Etuaho2935c582015-04-08 14:32:06 +03001070 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001071
Olli Etuaho43364892017-02-13 16:00:12 +00001072 checkBindingIsValid(line, type);
1073
Olli Etuaho856c4972016-08-08 11:38:39 +03001074 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001075
Olli Etuaho2935c582015-04-08 14:32:06 +03001076 // gl_LastFragData may be redeclared with a new precision qualifier
1077 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1078 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001079 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1080 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001081 if (type.isArrayOfArrays())
1082 {
1083 error(line, "redeclaration of gl_LastFragData as an array of arrays",
1084 identifier.c_str());
1085 return false;
1086 }
1087 else if (static_cast<int>(type.getOutermostArraySize()) ==
1088 maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001089 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001090 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001091 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001092 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->getExtension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001093 }
1094 }
1095 else
1096 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001097 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1098 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001099 return false;
1100 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001101 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001102
Olli Etuaho8a176262016-08-16 14:23:01 +03001103 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001104 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001105
Olli Etuaho0f684632017-07-13 12:42:15 +03001106 (*variable) = symbolTable.declareVariable(&identifier, type);
1107 if (!(*variable))
Olli Etuaho2935c582015-04-08 14:32:06 +03001108 {
1109 error(line, "redefinition", identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001110 return false;
1111 }
1112
Olli Etuaho8a176262016-08-16 14:23:01 +03001113 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001114 return false;
1115
1116 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001117}
1118
Martin Radev70866b82016-07-22 15:27:42 +03001119void TParseContext::checkIsParameterQualifierValid(
1120 const TSourceLoc &line,
1121 const TTypeQualifierBuilder &typeQualifierBuilder,
1122 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301123{
Olli Etuahocce89652017-06-19 16:04:09 +03001124 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001125 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001126
1127 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301128 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001129 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1130 }
1131
1132 if (!IsImage(type->getBasicType()))
1133 {
Olli Etuaho43364892017-02-13 16:00:12 +00001134 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001135 }
1136 else
1137 {
1138 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001139 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001140
Martin Radev70866b82016-07-22 15:27:42 +03001141 type->setQualifier(typeQualifier.qualifier);
1142
1143 if (typeQualifier.precision != EbpUndefined)
1144 {
1145 type->setPrecision(typeQualifier.precision);
1146 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001147}
1148
Olli Etuaho856c4972016-08-08 11:38:39 +03001149bool TParseContext::checkCanUseExtension(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001150{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001151 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001152 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +05301153 if (iter == extBehavior.end())
1154 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001155 error(line, "extension is not supported", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001156 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001157 }
zmo@google.comf5450912011-09-09 01:37:19 +00001158 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +05301159 if (iter->second == EBhDisable || iter->second == EBhUndefined)
1160 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001161 error(line, "extension is disabled", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001162 return false;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001163 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301164 if (iter->second == EBhWarn)
1165 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001166 warning(line, "extension is being used", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001167 return true;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001168 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001169
Olli Etuaho8a176262016-08-16 14:23:01 +03001170 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001171}
1172
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001173// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1174// compile-time or link-time errors are the same whether or not the declaration is empty".
1175// This function implements all the checks that are done on qualifiers regardless of if the
1176// declaration is empty.
1177void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1178 const sh::TLayoutQualifier &layoutQualifier,
1179 const TSourceLoc &location)
1180{
1181 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1182 {
1183 error(location, "Shared memory declarations cannot have layout specified", "layout");
1184 }
1185
1186 if (layoutQualifier.matrixPacking != EmpUnspecified)
1187 {
1188 error(location, "layout qualifier only valid for interface blocks",
1189 getMatrixPackingString(layoutQualifier.matrixPacking));
1190 return;
1191 }
1192
1193 if (layoutQualifier.blockStorage != EbsUnspecified)
1194 {
1195 error(location, "layout qualifier only valid for interface blocks",
1196 getBlockStorageString(layoutQualifier.blockStorage));
1197 return;
1198 }
1199
1200 if (qualifier == EvqFragmentOut)
1201 {
1202 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1203 {
1204 error(location, "invalid layout qualifier combination", "yuv");
1205 return;
1206 }
1207 }
1208 else
1209 {
1210 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1211 }
1212
Olli Etuaho95468d12017-05-04 11:14:34 +03001213 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1214 // parsing steps. So it needs to be checked here.
1215 if (isMultiviewExtensionEnabled() && mShaderVersion < 300 && qualifier == EvqVertexIn)
1216 {
1217 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1218 }
1219
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001220 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
1221 if (mShaderVersion >= 310 && qualifier == EvqUniform)
1222 {
1223 canHaveLocation = true;
1224 // We're not checking whether the uniform location is in range here since that depends on
1225 // the type of the variable.
1226 // The type can only be fully determined for non-empty declarations.
1227 }
1228 if (!canHaveLocation)
1229 {
1230 checkLocationIsNotSpecified(location, layoutQualifier);
1231 }
1232}
1233
jchen104cdac9e2017-05-08 11:01:20 +08001234void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1235 const TSourceLoc &location)
1236{
1237 if (publicType.precision != EbpHigh)
1238 {
1239 error(location, "Can only be highp", "atomic counter");
1240 }
1241 // dEQP enforces compile error if location is specified. See uniform_location.test.
1242 if (publicType.layoutQualifier.location != -1)
1243 {
1244 error(location, "location must not be set for atomic_uint", "layout");
1245 }
1246 if (publicType.layoutQualifier.binding == -1)
1247 {
1248 error(location, "no binding specified", "atomic counter");
1249 }
1250}
1251
Martin Radevb8b01222016-11-20 23:25:53 +02001252void TParseContext::emptyDeclarationErrorCheck(const TPublicType &publicType,
1253 const TSourceLoc &location)
1254{
1255 if (publicType.isUnsizedArray())
1256 {
1257 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1258 // error. It is assumed that this applies to empty declarations as well.
1259 error(location, "empty array declaration needs to specify a size", "");
1260 }
Martin Radevb8b01222016-11-20 23:25:53 +02001261}
1262
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001263// These checks are done for all declarations that are non-empty. They're done for non-empty
1264// declarations starting a declarator list, and declarators that follow an empty declaration.
1265void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1266 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001267{
Olli Etuahofa33d582015-04-09 14:33:12 +03001268 switch (publicType.qualifier)
1269 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001270 case EvqVaryingIn:
1271 case EvqVaryingOut:
1272 case EvqAttribute:
1273 case EvqVertexIn:
1274 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001275 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001276 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001277 {
1278 error(identifierLocation, "cannot be used with a structure",
1279 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001280 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001281 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001282 break;
1283 case EvqBuffer:
1284 if (publicType.getBasicType() != EbtInterfaceBlock)
1285 {
1286 error(identifierLocation,
1287 "cannot declare buffer variables at global scope(outside a block)",
1288 getQualifierString(publicType.qualifier));
1289 return;
1290 }
1291 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001292 default:
1293 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001294 }
jchen10cc2a10e2017-05-03 14:05:12 +08001295 std::string reason(getBasicString(publicType.getBasicType()));
1296 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001297 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001298 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001299 {
1300 return;
1301 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001302
Andrei Volykhina5527072017-03-22 16:46:30 +03001303 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1304 publicType.qualifier != EvqConst) &&
1305 publicType.getBasicType() == EbtYuvCscStandardEXT)
1306 {
1307 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1308 getQualifierString(publicType.qualifier));
1309 return;
1310 }
1311
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001312 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1313 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001314 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1315 // But invalid shaders may still reach here with an unsized array declaration.
1316 if (!publicType.isUnsizedArray())
1317 {
1318 TType type(publicType);
1319 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1320 publicType.layoutQualifier);
1321 }
1322 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001323
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001324 // check for layout qualifier issues
1325 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001326
Martin Radev2cc85b32016-08-05 16:22:53 +03001327 if (IsImage(publicType.getBasicType()))
1328 {
1329
1330 switch (layoutQualifier.imageInternalFormat)
1331 {
1332 case EiifRGBA32F:
1333 case EiifRGBA16F:
1334 case EiifR32F:
1335 case EiifRGBA8:
1336 case EiifRGBA8_SNORM:
1337 if (!IsFloatImage(publicType.getBasicType()))
1338 {
1339 error(identifierLocation,
1340 "internal image format requires a floating image type",
1341 getBasicString(publicType.getBasicType()));
1342 return;
1343 }
1344 break;
1345 case EiifRGBA32I:
1346 case EiifRGBA16I:
1347 case EiifRGBA8I:
1348 case EiifR32I:
1349 if (!IsIntegerImage(publicType.getBasicType()))
1350 {
1351 error(identifierLocation,
1352 "internal image format requires an integer image type",
1353 getBasicString(publicType.getBasicType()));
1354 return;
1355 }
1356 break;
1357 case EiifRGBA32UI:
1358 case EiifRGBA16UI:
1359 case EiifRGBA8UI:
1360 case EiifR32UI:
1361 if (!IsUnsignedImage(publicType.getBasicType()))
1362 {
1363 error(identifierLocation,
1364 "internal image format requires an unsigned image type",
1365 getBasicString(publicType.getBasicType()));
1366 return;
1367 }
1368 break;
1369 case EiifUnspecified:
1370 error(identifierLocation, "layout qualifier", "No image internal format specified");
1371 return;
1372 default:
1373 error(identifierLocation, "layout qualifier", "unrecognized token");
1374 return;
1375 }
1376
1377 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1378 switch (layoutQualifier.imageInternalFormat)
1379 {
1380 case EiifR32F:
1381 case EiifR32I:
1382 case EiifR32UI:
1383 break;
1384 default:
1385 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1386 {
1387 error(identifierLocation, "layout qualifier",
1388 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1389 "image variables must be qualified readonly and/or writeonly");
1390 return;
1391 }
1392 break;
1393 }
1394 }
1395 else
1396 {
Olli Etuaho43364892017-02-13 16:00:12 +00001397 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001398 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1399 }
jchen104cdac9e2017-05-08 11:01:20 +08001400
1401 if (IsAtomicCounter(publicType.getBasicType()))
1402 {
1403 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1404 }
1405 else
1406 {
1407 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1408 }
Olli Etuaho43364892017-02-13 16:00:12 +00001409}
Martin Radev2cc85b32016-08-05 16:22:53 +03001410
Olli Etuaho43364892017-02-13 16:00:12 +00001411void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1412{
1413 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001414 // Note that the ESSL 3.10 section 4.4.5 is not particularly clear on how the binding qualifier
1415 // on arrays of arrays should be handled. We interpret the spec so that the binding value is
1416 // incremented for each element of the innermost nested arrays. This is in line with how arrays
1417 // of arrays of blocks are specified to behave in GLSL 4.50 and a conservative interpretation
1418 // when it comes to which shaders are accepted by the compiler.
1419 int arrayTotalElementCount = type.getArraySizeProduct();
Olli Etuaho43364892017-02-13 16:00:12 +00001420 if (IsImage(type.getBasicType()))
1421 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001422 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding,
1423 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001424 }
1425 else if (IsSampler(type.getBasicType()))
1426 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001427 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding,
1428 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001429 }
jchen104cdac9e2017-05-08 11:01:20 +08001430 else if (IsAtomicCounter(type.getBasicType()))
1431 {
1432 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1433 }
Olli Etuaho43364892017-02-13 16:00:12 +00001434 else
1435 {
1436 ASSERT(!IsOpaqueType(type.getBasicType()));
1437 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001438 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001439}
1440
Olli Etuaho856c4972016-08-08 11:38:39 +03001441void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1442 const TString &layoutQualifierName,
1443 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001444{
1445
1446 if (mShaderVersion < versionRequired)
1447 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001448 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001449 }
1450}
1451
Olli Etuaho856c4972016-08-08 11:38:39 +03001452bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1453 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001454{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001455 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001456 for (size_t i = 0u; i < localSize.size(); ++i)
1457 {
1458 if (localSize[i] != -1)
1459 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001460 error(location,
1461 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1462 "global layout declaration",
1463 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001464 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001465 }
1466 }
1467
Olli Etuaho8a176262016-08-16 14:23:01 +03001468 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001469}
1470
Olli Etuaho43364892017-02-13 16:00:12 +00001471void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001472 TLayoutImageInternalFormat internalFormat)
1473{
1474 if (internalFormat != EiifUnspecified)
1475 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001476 error(location, "invalid layout qualifier: only valid when used with images",
1477 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001478 }
Olli Etuaho43364892017-02-13 16:00:12 +00001479}
1480
1481void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1482{
1483 if (binding != -1)
1484 {
1485 error(location,
1486 "invalid layout qualifier: only valid when used with opaque types or blocks",
1487 "binding");
1488 }
1489}
1490
jchen104cdac9e2017-05-08 11:01:20 +08001491void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1492{
1493 if (offset != -1)
1494 {
1495 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1496 "offset");
1497 }
1498}
1499
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001500void TParseContext::checkImageBindingIsValid(const TSourceLoc &location,
1501 int binding,
1502 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001503{
1504 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001505 if (binding >= 0 && binding + arrayTotalElementCount > mMaxImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001506 {
1507 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1508 }
1509}
1510
1511void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1512 int binding,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001513 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001514{
1515 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001516 if (binding >= 0 && binding + arrayTotalElementCount > mMaxCombinedTextureImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001517 {
1518 error(location, "sampler binding greater than maximum texture units", "binding");
1519 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001520}
1521
Jiajia Qinbc585152017-06-23 15:42:17 +08001522void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location,
1523 const TQualifier &qualifier,
1524 int binding,
1525 int arraySize)
jchen10af713a22017-04-19 09:10:56 +08001526{
1527 int size = (arraySize == 0 ? 1 : arraySize);
Jiajia Qinbc585152017-06-23 15:42:17 +08001528 if (qualifier == EvqUniform)
jchen10af713a22017-04-19 09:10:56 +08001529 {
Jiajia Qinbc585152017-06-23 15:42:17 +08001530 if (binding + size > mMaxUniformBufferBindings)
1531 {
1532 error(location, "uniform block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1533 "binding");
1534 }
1535 }
1536 else if (qualifier == EvqBuffer)
1537 {
1538 if (binding + size > mMaxShaderStorageBufferBindings)
1539 {
1540 error(location,
1541 "shader storage block binding greater than MAX_SHADER_STORAGE_BUFFER_BINDINGS",
1542 "binding");
1543 }
jchen10af713a22017-04-19 09:10:56 +08001544 }
1545}
jchen104cdac9e2017-05-08 11:01:20 +08001546void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1547{
1548 if (binding >= mMaxAtomicCounterBindings)
1549 {
1550 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1551 "binding");
1552 }
1553}
jchen10af713a22017-04-19 09:10:56 +08001554
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001555void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1556 int objectLocationCount,
1557 const TLayoutQualifier &layoutQualifier)
1558{
1559 int loc = layoutQualifier.location;
1560 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1561 {
1562 error(location, "Uniform location out of range", "location");
1563 }
1564}
1565
Andrei Volykhina5527072017-03-22 16:46:30 +03001566void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1567{
1568 if (yuv != false)
1569 {
1570 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1571 }
1572}
1573
Jiajia Qinbc585152017-06-23 15:42:17 +08001574void TParseContext::functionCallRValueLValueErrorCheck(const TFunction *fnCandidate,
1575 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001576{
1577 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1578 {
1579 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
Jiajia Qinbc585152017-06-23 15:42:17 +08001580 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
1581 if (!IsImage(argument->getBasicType()) && (IsQualifierUnspecified(qual) || qual == EvqIn ||
1582 qual == EvqInOut || qual == EvqConstReadOnly))
1583 {
1584 if (argument->getMemoryQualifier().writeonly)
1585 {
1586 error(argument->getLine(),
1587 "Writeonly value cannot be passed for 'in' or 'inout' parameters.",
1588 fnCall->getFunctionSymbolInfo()->getName().c_str());
1589 return;
1590 }
1591 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001592 if (qual == EvqOut || qual == EvqInOut)
1593 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001594 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001595 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001596 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001597 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuahoec9232b2017-03-27 17:01:37 +03001598 fnCall->getFunctionSymbolInfo()->getName().c_str());
Olli Etuaho383b7912016-08-05 11:22:59 +03001599 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001600 }
1601 }
1602 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001603}
1604
Martin Radev70866b82016-07-22 15:27:42 +03001605void TParseContext::checkInvariantVariableQualifier(bool invariant,
1606 const TQualifier qualifier,
1607 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001608{
Martin Radev70866b82016-07-22 15:27:42 +03001609 if (!invariant)
1610 return;
1611
1612 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001613 {
Martin Radev70866b82016-07-22 15:27:42 +03001614 // input variables in the fragment shader can be also qualified as invariant
1615 if (!sh::CanBeInvariantESSL1(qualifier))
1616 {
1617 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1618 }
1619 }
1620 else
1621 {
1622 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1623 {
1624 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1625 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001626 }
1627}
1628
Arun Patole7e7e68d2015-05-22 12:02:25 +05301629bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001630{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001631 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001632 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1633 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001634}
1635
Arun Patole7e7e68d2015-05-22 12:02:25 +05301636bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001637{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001638 return ::IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001639}
1640
Jamie Madillb98c3a82015-07-23 14:26:04 -04001641void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1642 const char *extName,
1643 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001644{
1645 pp::SourceLocation srcLoc;
1646 srcLoc.file = loc.first_file;
1647 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001648 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001649}
1650
Jamie Madillb98c3a82015-07-23 14:26:04 -04001651void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1652 const char *name,
1653 const char *value,
1654 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001655{
1656 pp::SourceLocation srcLoc;
1657 srcLoc.file = loc.first_file;
1658 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001659 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001660}
1661
Martin Radev4c4c8e72016-08-04 12:25:34 +03001662sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001663{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001664 sh::WorkGroupSize result;
Martin Radev802abe02016-08-04 17:48:32 +03001665 for (size_t i = 0u; i < result.size(); ++i)
1666 {
1667 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1668 {
1669 result[i] = 1;
1670 }
1671 else
1672 {
1673 result[i] = mComputeShaderLocalSize[i];
1674 }
1675 }
1676 return result;
1677}
1678
Olli Etuaho56229f12017-07-10 14:16:33 +03001679TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1680 const TSourceLoc &line)
1681{
1682 TIntermConstantUnion *node = new TIntermConstantUnion(
1683 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1684 node->setLine(line);
1685 return node;
1686}
1687
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001688/////////////////////////////////////////////////////////////////////////////////
1689//
1690// Non-Errors.
1691//
1692/////////////////////////////////////////////////////////////////////////////////
1693
Jamie Madill5c097022014-08-20 16:38:32 -04001694const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1695 const TString *name,
1696 const TSymbol *symbol)
1697{
Jamie Madill5c097022014-08-20 16:38:32 -04001698 if (!symbol)
1699 {
1700 error(location, "undeclared identifier", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001701 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001702 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001703
1704 if (!symbol->isVariable())
Jamie Madill5c097022014-08-20 16:38:32 -04001705 {
1706 error(location, "variable expected", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001707 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001708 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001709
1710 const TVariable *variable = static_cast<const TVariable *>(symbol);
1711
1712 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
1713 !variable->getExtension().empty())
Jamie Madill5c097022014-08-20 16:38:32 -04001714 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001715 checkCanUseExtension(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001716 }
1717
Olli Etuaho0f684632017-07-13 12:42:15 +03001718 // Reject shaders using both gl_FragData and gl_FragColor
1719 TQualifier qualifier = variable->getType().getQualifier();
1720 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill5c097022014-08-20 16:38:32 -04001721 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001722 mUsesFragData = true;
1723 }
1724 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
1725 {
1726 mUsesFragColor = true;
1727 }
1728 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1729 {
1730 mUsesSecondaryOutputs = true;
Jamie Madill5c097022014-08-20 16:38:32 -04001731 }
1732
Olli Etuaho0f684632017-07-13 12:42:15 +03001733 // This validation is not quite correct - it's only an error to write to
1734 // both FragData and FragColor. For simplicity, and because users shouldn't
1735 // be rewarded for reading from undefined varaibles, return an error
1736 // if they are both referenced, rather than assigned.
1737 if (mUsesFragData && mUsesFragColor)
1738 {
1739 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1740 if (mUsesSecondaryOutputs)
1741 {
1742 errorMessage =
1743 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1744 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1745 }
1746 error(location, errorMessage, name->c_str());
1747 }
1748
1749 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1750 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1751 qualifier == EvqWorkGroupSize)
1752 {
1753 error(location,
1754 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1755 "gl_WorkGroupSize");
1756 }
Jamie Madill5c097022014-08-20 16:38:32 -04001757 return variable;
1758}
1759
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001760TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1761 const TString *name,
1762 const TSymbol *symbol)
1763{
1764 const TVariable *variable = getNamedVariable(location, name, symbol);
1765
Olli Etuaho0f684632017-07-13 12:42:15 +03001766 if (!variable)
1767 {
1768 TIntermTyped *node = CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
1769 node->setLine(location);
1770 return node;
1771 }
1772
Olli Etuaho56229f12017-07-10 14:16:33 +03001773 TIntermTyped *node = nullptr;
1774
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001775 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001776 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001777 const TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho56229f12017-07-10 14:16:33 +03001778 node = new TIntermConstantUnion(constArray, variable->getType());
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001779 }
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001780 else if (variable->getType().getQualifier() == EvqWorkGroupSize &&
1781 mComputeShaderLocalSizeDeclared)
1782 {
1783 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1784 // needs to be added to the AST as a constant and not as a symbol.
1785 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1786 TConstantUnion *constArray = new TConstantUnion[3];
1787 for (size_t i = 0; i < 3; ++i)
1788 {
1789 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1790 }
1791
1792 ASSERT(variable->getType().getBasicType() == EbtUInt);
1793 ASSERT(variable->getType().getObjectSize() == 3);
1794
1795 TType type(variable->getType());
1796 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001797 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001798 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08001799 // TODO(jiawei.shao@intel.com): set array sizes for user-defined geometry shader inputs.
1800 else if (variable->getType().getQualifier() == EvqPerVertexIn)
1801 {
1802 TType type(variable->getType());
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001803 type.setArraySize(0, mGeometryShaderInputArraySize);
Jiawei Shaod8105a02017-08-08 09:54:36 +08001804 node = new TIntermSymbol(variable->getUniqueId(), variable->getName(), type);
1805 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001806 else
1807 {
Olli Etuaho56229f12017-07-10 14:16:33 +03001808 node = new TIntermSymbol(variable->getUniqueId(), variable->getName(), variable->getType());
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001809 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001810 ASSERT(node != nullptr);
1811 node->setLine(location);
1812 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001813}
1814
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001815// Initializers show up in several places in the grammar. Have one set of
1816// code to handle them here.
1817//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001818// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001819bool TParseContext::executeInitializer(const TSourceLoc &line,
1820 const TString &identifier,
1821 const TPublicType &pType,
1822 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001823 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001824{
Olli Etuaho13389b62016-10-16 11:48:18 +01001825 ASSERT(initNode != nullptr);
1826 ASSERT(*initNode == nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001827 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001828
Olli Etuaho2935c582015-04-08 14:32:06 +03001829 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001830 if (type.isUnsizedArray())
1831 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001832 // In case initializer is not an array or type has more dimensions than initializer, this
1833 // will default to setting array sizes to 1. We have not checked yet whether the initializer
1834 // actually is an array or not. Having a non-array initializer for an unsized array will
1835 // result in an error later, so we don't generate an error message here.
1836 type.sizeUnsizedArrays(initializer->getType().getArraySizes());
Olli Etuaho376f1b52015-04-13 13:23:41 +03001837 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001838 if (!declareVariable(line, identifier, type, &variable))
1839 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001840 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001841 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001842
Olli Etuahob0c645e2015-05-12 14:25:36 +03001843 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001844 if (symbolTable.atGlobalLevel() &&
1845 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001846 {
1847 // Error message does not completely match behavior with ESSL 1.00, but
1848 // we want to steer developers towards only using constant expressions.
1849 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001850 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001851 }
1852 if (globalInitWarning)
1853 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001854 warning(
1855 line,
1856 "global variable initializers should be constant expressions "
1857 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1858 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001859 }
1860
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001861 //
1862 // identifier must be of type constant, a global, or a temporary
1863 //
1864 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301865 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1866 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001867 error(line, " cannot initialize this type of qualifier ",
1868 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001869 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001870 }
1871 //
1872 // test for and propagate constant
1873 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001874
Arun Patole7e7e68d2015-05-22 12:02:25 +05301875 if (qualifier == EvqConst)
1876 {
1877 if (qualifier != initializer->getType().getQualifier())
1878 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001879 std::stringstream reasonStream;
1880 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1881 << "'";
1882 std::string reason = reasonStream.str();
1883 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001884 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001885 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001886 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301887 if (type != initializer->getType())
1888 {
1889 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001890 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001891 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001892 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001893 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001894
1895 // Save the constant folded value to the variable if possible. For example array
1896 // initializers are not folded, since that way copying the array literal to multiple places
1897 // in the shader is avoided.
1898 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1899 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301900 if (initializer->getAsConstantUnion())
1901 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001902 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001903 ASSERT(*initNode == nullptr);
1904 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301905 }
1906 else if (initializer->getAsSymbolNode())
1907 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001908 const TSymbol *symbol =
1909 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1910 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001911
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001912 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001913 if (constArray)
1914 {
1915 variable->shareConstPointer(constArray);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001916 ASSERT(*initNode == nullptr);
1917 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001918 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001919 }
1920 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001921
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001922 TIntermSymbol *intermSymbol =
1923 new TIntermSymbol(variable->getUniqueId(), variable->getName(), variable->getType());
1924 intermSymbol->setLine(line);
Olli Etuaho13389b62016-10-16 11:48:18 +01001925 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1926 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001927 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001928 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001929 return false;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001930 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001931
Olli Etuaho914b79a2017-06-19 16:03:19 +03001932 return true;
1933}
1934
1935TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
1936 const TString &identifier,
1937 TIntermTyped *initializer,
1938 const TSourceLoc &loc)
1939{
1940 checkIsScalarBool(loc, pType);
1941 TIntermBinary *initNode = nullptr;
1942 if (executeInitializer(loc, identifier, pType, initializer, &initNode))
1943 {
1944 // The initializer is valid. The init condition needs to have a node - either the
1945 // initializer node, or a constant node in case the initialized variable is const and won't
1946 // be recorded in the AST.
1947 if (initNode == nullptr)
1948 {
1949 return initializer;
1950 }
1951 else
1952 {
1953 TIntermDeclaration *declaration = new TIntermDeclaration();
1954 declaration->appendDeclarator(initNode);
1955 return declaration;
1956 }
1957 }
1958 return nullptr;
1959}
1960
1961TIntermNode *TParseContext::addLoop(TLoopType type,
1962 TIntermNode *init,
1963 TIntermNode *cond,
1964 TIntermTyped *expr,
1965 TIntermNode *body,
1966 const TSourceLoc &line)
1967{
1968 TIntermNode *node = nullptr;
1969 TIntermTyped *typedCond = nullptr;
1970 if (cond)
1971 {
1972 typedCond = cond->getAsTyped();
1973 }
1974 if (cond == nullptr || typedCond)
1975 {
Olli Etuahocce89652017-06-19 16:04:09 +03001976 if (type == ELoopDoWhile)
1977 {
1978 checkIsScalarBool(line, typedCond);
1979 }
1980 // In the case of other loops, it was checked before that the condition is a scalar boolean.
1981 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
1982 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
1983 !typedCond->isVector()));
1984
Olli Etuaho3ec75682017-07-05 17:02:55 +03001985 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03001986 node->setLine(line);
1987 return node;
1988 }
1989
Olli Etuahocce89652017-06-19 16:04:09 +03001990 ASSERT(type != ELoopDoWhile);
1991
Olli Etuaho914b79a2017-06-19 16:03:19 +03001992 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
1993 ASSERT(declaration);
1994 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
1995 ASSERT(declarator->getLeft()->getAsSymbolNode());
1996
1997 // The condition is a declaration. In the AST representation we don't support declarations as
1998 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
1999 // the loop.
2000 TIntermBlock *block = new TIntermBlock();
2001
2002 TIntermDeclaration *declareCondition = new TIntermDeclaration();
2003 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
2004 block->appendStatement(declareCondition);
2005
2006 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
2007 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03002008 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002009 block->appendStatement(loop);
2010 loop->setLine(line);
2011 block->setLine(line);
2012 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002013}
2014
Olli Etuahocce89652017-06-19 16:04:09 +03002015TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
2016 TIntermNodePair code,
2017 const TSourceLoc &loc)
2018{
Olli Etuaho56229f12017-07-10 14:16:33 +03002019 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuahocce89652017-06-19 16:04:09 +03002020
2021 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03002022 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03002023 {
2024 if (cond->getAsConstantUnion()->getBConst(0) == true)
2025 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002026 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03002027 }
2028 else
2029 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002030 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03002031 }
2032 }
2033
Olli Etuaho3ec75682017-07-05 17:02:55 +03002034 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuahocce89652017-06-19 16:04:09 +03002035 node->setLine(loc);
2036
2037 return node;
2038}
2039
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002040void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
2041{
2042 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
2043 typeSpecifier->getBasicType());
2044
2045 if (mShaderVersion < 300 && typeSpecifier->array)
2046 {
2047 error(typeSpecifier->getLine(), "not supported", "first-class array");
2048 typeSpecifier->clearArrayness();
2049 }
2050}
2051
Martin Radev70866b82016-07-22 15:27:42 +03002052TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302053 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002054{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002055 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002056
Martin Radev70866b82016-07-22 15:27:42 +03002057 TPublicType returnType = typeSpecifier;
2058 returnType.qualifier = typeQualifier.qualifier;
2059 returnType.invariant = typeQualifier.invariant;
2060 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002061 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002062 returnType.precision = typeSpecifier.precision;
2063
2064 if (typeQualifier.precision != EbpUndefined)
2065 {
2066 returnType.precision = typeQualifier.precision;
2067 }
2068
Martin Radev4a9cd802016-09-01 16:51:51 +03002069 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2070 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002071
Martin Radev4a9cd802016-09-01 16:51:51 +03002072 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2073 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002074
Martin Radev4a9cd802016-09-01 16:51:51 +03002075 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002076
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002077 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002078 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002079 if (typeSpecifier.array)
2080 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002081 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002082 returnType.clearArrayness();
2083 }
2084
Martin Radev70866b82016-07-22 15:27:42 +03002085 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002086 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002087 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002088 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002089 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002090 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002091
Martin Radev70866b82016-07-22 15:27:42 +03002092 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002093 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002094 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002095 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002096 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002097 }
2098 }
2099 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002100 {
Martin Radev70866b82016-07-22 15:27:42 +03002101 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002102 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002103 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002104 }
Martin Radev70866b82016-07-22 15:27:42 +03002105 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2106 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002107 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002108 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2109 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002110 }
Martin Radev70866b82016-07-22 15:27:42 +03002111 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002112 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002113 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002114 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002115 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002116 }
2117
2118 return returnType;
2119}
2120
Olli Etuaho856c4972016-08-08 11:38:39 +03002121void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2122 const TPublicType &type,
2123 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002124{
2125 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002126 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002127 {
2128 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002129 }
2130
2131 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2132 switch (qualifier)
2133 {
2134 case EvqVertexIn:
2135 // ESSL 3.00 section 4.3.4
2136 if (type.array)
2137 {
2138 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002139 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002140 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002141 return;
2142 case EvqFragmentOut:
2143 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002144 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002145 {
2146 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002147 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002148 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002149 return;
2150 default:
2151 break;
2152 }
2153
2154 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2155 // restrictions.
2156 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002157 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2158 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002159 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2160 {
2161 error(qualifierLocation, "must use 'flat' interpolation here",
2162 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002163 }
2164
Martin Radev4a9cd802016-09-01 16:51:51 +03002165 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002166 {
2167 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2168 // These restrictions are only implied by the ESSL 3.00 spec, but
2169 // the ESSL 3.10 spec lists these restrictions explicitly.
2170 if (type.array)
2171 {
2172 error(qualifierLocation, "cannot be an array of structures",
2173 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002174 }
2175 if (type.isStructureContainingArrays())
2176 {
2177 error(qualifierLocation, "cannot be a structure containing an array",
2178 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002179 }
2180 if (type.isStructureContainingType(EbtStruct))
2181 {
2182 error(qualifierLocation, "cannot be a structure containing a structure",
2183 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002184 }
2185 if (type.isStructureContainingType(EbtBool))
2186 {
2187 error(qualifierLocation, "cannot be a structure containing a bool",
2188 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002189 }
2190 }
2191}
2192
Martin Radev2cc85b32016-08-05 16:22:53 +03002193void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2194{
2195 if (qualifier.getType() == QtStorage)
2196 {
2197 const TStorageQualifierWrapper &storageQualifier =
2198 static_cast<const TStorageQualifierWrapper &>(qualifier);
2199 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2200 !symbolTable.atGlobalLevel())
2201 {
2202 error(storageQualifier.getLine(),
2203 "Local variables can only use the const storage qualifier.",
2204 storageQualifier.getQualifierString().c_str());
2205 }
2206 }
2207}
2208
Olli Etuaho43364892017-02-13 16:00:12 +00002209void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002210 const TSourceLoc &location)
2211{
Jiajia Qinbc585152017-06-23 15:42:17 +08002212 const std::string reason(
2213 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2214 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002215 if (memoryQualifier.readonly)
2216 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002217 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002218 }
2219 if (memoryQualifier.writeonly)
2220 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002221 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002222 }
Martin Radev049edfa2016-11-11 14:35:37 +02002223 if (memoryQualifier.coherent)
2224 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002225 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002226 }
2227 if (memoryQualifier.restrictQualifier)
2228 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002229 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002230 }
2231 if (memoryQualifier.volatileQualifier)
2232 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002233 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002234 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002235}
2236
jchen104cdac9e2017-05-08 11:01:20 +08002237// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2238// intermediate tree.
2239void TParseContext::checkAtomicCounterOffsetIsNotOverlapped(TPublicType &publicType,
2240 size_t size,
2241 bool forceAppend,
2242 const TSourceLoc &loc,
2243 TType &type)
2244{
2245 auto &bindingState = mAtomicCounterBindingStates[publicType.layoutQualifier.binding];
2246 int offset;
2247 if (publicType.layoutQualifier.offset == -1 || forceAppend)
2248 {
2249 offset = bindingState.appendSpan(size);
2250 }
2251 else
2252 {
2253 offset = bindingState.insertSpan(publicType.layoutQualifier.offset, size);
2254 }
2255 if (offset == -1)
2256 {
2257 error(loc, "Offset overlapping", "atomic counter");
2258 return;
2259 }
2260 TLayoutQualifier qualifier = type.getLayoutQualifier();
2261 qualifier.offset = offset;
2262 type.setLayoutQualifier(qualifier);
2263}
2264
Olli Etuaho13389b62016-10-16 11:48:18 +01002265TIntermDeclaration *TParseContext::parseSingleDeclaration(
2266 TPublicType &publicType,
2267 const TSourceLoc &identifierOrTypeLocation,
2268 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002269{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002270 TType type(publicType);
2271 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2272 mDirectiveHandler.pragma().stdgl.invariantAll)
2273 {
2274 TQualifier qualifier = type.getQualifier();
2275
2276 // The directive handler has already taken care of rejecting invalid uses of this pragma
2277 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2278 // affected variable declarations:
2279 //
2280 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2281 // elsewhere, in TranslatorGLSL.)
2282 //
2283 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2284 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2285 // the way this is currently implemented we have to enable this compiler option before
2286 // parsing the shader and determining the shading language version it uses. If this were
2287 // implemented as a post-pass, the workaround could be more targeted.
2288 //
2289 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2290 // the specification, but there are desktop OpenGL drivers that expect that this is the
2291 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2292 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2293 {
2294 type.setInvariant(true);
2295 }
2296 }
2297
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002298 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2299 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002300
Olli Etuahobab4c082015-04-24 16:38:49 +03002301 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002302 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002303
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002304 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002305 if (emptyDeclaration)
2306 {
Martin Radevb8b01222016-11-20 23:25:53 +02002307 emptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002308 // In most cases we don't need to create a symbol node for an empty declaration.
2309 // But if the empty declaration is declaring a struct type, the symbol node will store that.
2310 if (type.getBasicType() == EbtStruct)
2311 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002312 symbol = new TIntermSymbol(0, "", type);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002313 }
jchen104cdac9e2017-05-08 11:01:20 +08002314 else if (IsAtomicCounter(publicType.getBasicType()))
2315 {
2316 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2317 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002318 }
2319 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002320 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002321 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002322
Olli Etuaho856c4972016-08-08 11:38:39 +03002323 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002324
jchen104cdac9e2017-05-08 11:01:20 +08002325 if (IsAtomicCounter(publicType.getBasicType()))
2326 {
2327
2328 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterSize, false,
2329 identifierOrTypeLocation, type);
2330 }
2331
Olli Etuaho2935c582015-04-08 14:32:06 +03002332 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002333 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002334
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002335 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002336 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002337 symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
Olli Etuaho13389b62016-10-16 11:48:18 +01002338 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002339 }
2340
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002341 TIntermDeclaration *declaration = new TIntermDeclaration();
2342 declaration->setLine(identifierOrTypeLocation);
2343 if (symbol)
2344 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002345 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002346 declaration->appendDeclarator(symbol);
2347 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002348 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002349}
2350
Olli Etuaho13389b62016-10-16 11:48:18 +01002351TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
2352 const TSourceLoc &identifierLocation,
2353 const TString &identifier,
2354 const TSourceLoc &indexLocation,
2355 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04002356{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002357 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002358
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002359 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2360 identifierLocation);
2361
2362 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002363
Olli Etuaho856c4972016-08-08 11:38:39 +03002364 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002365
Olli Etuaho8a176262016-08-16 14:23:01 +03002366 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002367
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002368 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002369
Olli Etuaho856c4972016-08-08 11:38:39 +03002370 unsigned int size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002371 // Make the type an array even if size check failed.
2372 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002373 arrayType.makeArray(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04002374
jchen104cdac9e2017-05-08 11:01:20 +08002375 if (IsAtomicCounter(publicType.getBasicType()))
2376 {
2377 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterArrayStride * size, false,
2378 identifierLocation, arrayType);
2379 }
2380
Olli Etuaho2935c582015-04-08 14:32:06 +03002381 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002382 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002383
Olli Etuaho13389b62016-10-16 11:48:18 +01002384 TIntermDeclaration *declaration = new TIntermDeclaration();
2385 declaration->setLine(identifierLocation);
2386
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002387 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002388 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002389 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2390 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002391 declaration->appendDeclarator(symbol);
2392 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002393
Olli Etuaho13389b62016-10-16 11:48:18 +01002394 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002395}
2396
Olli Etuaho13389b62016-10-16 11:48:18 +01002397TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2398 const TSourceLoc &identifierLocation,
2399 const TString &identifier,
2400 const TSourceLoc &initLocation,
2401 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002402{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002403 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002404
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002405 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2406 identifierLocation);
2407
2408 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002409
Olli Etuaho13389b62016-10-16 11:48:18 +01002410 TIntermDeclaration *declaration = new TIntermDeclaration();
2411 declaration->setLine(identifierLocation);
2412
2413 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002414 if (executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002415 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002416 if (initNode)
2417 {
2418 declaration->appendDeclarator(initNode);
2419 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002420 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002421 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002422}
2423
Olli Etuaho13389b62016-10-16 11:48:18 +01002424TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Jamie Madillb98c3a82015-07-23 14:26:04 -04002425 TPublicType &publicType,
2426 const TSourceLoc &identifierLocation,
2427 const TString &identifier,
2428 const TSourceLoc &indexLocation,
2429 TIntermTyped *indexExpression,
2430 const TSourceLoc &initLocation,
2431 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002432{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002433 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002434
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002435 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2436 identifierLocation);
2437
2438 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002439
Olli Etuaho8a176262016-08-16 14:23:01 +03002440 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002441
2442 TPublicType arrayType(publicType);
2443
Olli Etuaho856c4972016-08-08 11:38:39 +03002444 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002445 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2446 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002447 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002448 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002449 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002450 }
2451 // Make the type an array even if size check failed.
2452 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2453 arrayType.setArraySize(size);
2454
Olli Etuaho13389b62016-10-16 11:48:18 +01002455 TIntermDeclaration *declaration = new TIntermDeclaration();
2456 declaration->setLine(identifierLocation);
2457
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002458 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002459 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002460 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002461 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002462 if (initNode)
2463 {
2464 declaration->appendDeclarator(initNode);
2465 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002466 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002467
2468 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002469}
2470
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002471TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002472 const TTypeQualifierBuilder &typeQualifierBuilder,
2473 const TSourceLoc &identifierLoc,
2474 const TString *identifier,
2475 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002476{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002477 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002478
Martin Radev70866b82016-07-22 15:27:42 +03002479 if (!typeQualifier.invariant)
2480 {
2481 error(identifierLoc, "Expected invariant", identifier->c_str());
2482 return nullptr;
2483 }
2484 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2485 {
2486 return nullptr;
2487 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002488 if (!symbol)
2489 {
2490 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002491 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002492 }
Martin Radev70866b82016-07-22 15:27:42 +03002493 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002494 {
Martin Radev70866b82016-07-22 15:27:42 +03002495 error(identifierLoc, "invariant declaration specifies qualifier",
2496 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002497 }
Martin Radev70866b82016-07-22 15:27:42 +03002498 if (typeQualifier.precision != EbpUndefined)
2499 {
2500 error(identifierLoc, "invariant declaration specifies precision",
2501 getPrecisionString(typeQualifier.precision));
2502 }
2503 if (!typeQualifier.layoutQualifier.isEmpty())
2504 {
2505 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2506 }
2507
2508 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002509 if (!variable)
2510 {
2511 return nullptr;
2512 }
Martin Radev70866b82016-07-22 15:27:42 +03002513 const TType &type = variable->getType();
2514
2515 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2516 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002517 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002518
2519 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2520
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002521 TIntermSymbol *intermSymbol = new TIntermSymbol(variable->getUniqueId(), *identifier, type);
2522 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002523
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002524 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002525}
2526
Olli Etuaho13389b62016-10-16 11:48:18 +01002527void TParseContext::parseDeclarator(TPublicType &publicType,
2528 const TSourceLoc &identifierLocation,
2529 const TString &identifier,
2530 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002531{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002532 // If the declaration starting this declarator list was empty (example: int,), some checks were
2533 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002534 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002535 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002536 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2537 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002538 }
2539
Olli Etuaho856c4972016-08-08 11:38:39 +03002540 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002541
Olli Etuaho856c4972016-08-08 11:38:39 +03002542 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002543
Olli Etuaho2935c582015-04-08 14:32:06 +03002544 TVariable *variable = nullptr;
Olli Etuaho43364892017-02-13 16:00:12 +00002545 TType type(publicType);
jchen104cdac9e2017-05-08 11:01:20 +08002546 if (IsAtomicCounter(publicType.getBasicType()))
2547 {
2548 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterSize, true,
2549 identifierLocation, type);
2550 }
Olli Etuaho43364892017-02-13 16:00:12 +00002551 declareVariable(identifierLocation, identifier, type, &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002552
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002553 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002554 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002555 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
2556 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002557 declarationOut->appendDeclarator(symbol);
2558 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002559}
2560
Olli Etuaho13389b62016-10-16 11:48:18 +01002561void TParseContext::parseArrayDeclarator(TPublicType &publicType,
2562 const TSourceLoc &identifierLocation,
2563 const TString &identifier,
2564 const TSourceLoc &arrayLocation,
2565 TIntermTyped *indexExpression,
2566 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002567{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002568 // If the declaration starting this declarator list was empty (example: int,), some checks were
2569 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002570 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002571 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002572 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2573 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002574 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002575
Olli Etuaho856c4972016-08-08 11:38:39 +03002576 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002577
Olli Etuaho856c4972016-08-08 11:38:39 +03002578 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002579
Olli Etuaho8a176262016-08-16 14:23:01 +03002580 if (checkIsValidTypeAndQualifierForArray(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002581 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002582 TType arrayType = TType(publicType);
Olli Etuaho856c4972016-08-08 11:38:39 +03002583 unsigned int size = checkIsValidArraySize(arrayLocation, indexExpression);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002584 arrayType.makeArray(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002585
jchen104cdac9e2017-05-08 11:01:20 +08002586 if (IsAtomicCounter(publicType.getBasicType()))
2587 {
2588 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterArrayStride * size,
2589 true, identifierLocation, arrayType);
2590 }
2591
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002592 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002593 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002594
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002595 if (variable)
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002596 {
2597 TIntermSymbol *symbol =
2598 new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2599 symbol->setLine(identifierLocation);
2600 declarationOut->appendDeclarator(symbol);
2601 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002602 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002603}
2604
Olli Etuaho13389b62016-10-16 11:48:18 +01002605void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2606 const TSourceLoc &identifierLocation,
2607 const TString &identifier,
2608 const TSourceLoc &initLocation,
2609 TIntermTyped *initializer,
2610 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002611{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002612 // If the declaration starting this declarator list was empty (example: int,), some checks were
2613 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002614 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002615 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002616 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2617 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002618 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002619
Olli Etuaho856c4972016-08-08 11:38:39 +03002620 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002621
Olli Etuaho13389b62016-10-16 11:48:18 +01002622 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002623 if (executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002624 {
2625 //
2626 // build the intermediate representation
2627 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002628 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002629 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002630 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002631 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002632 }
2633}
2634
Olli Etuaho13389b62016-10-16 11:48:18 +01002635void TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
2636 const TSourceLoc &identifierLocation,
2637 const TString &identifier,
2638 const TSourceLoc &indexLocation,
2639 TIntermTyped *indexExpression,
2640 const TSourceLoc &initLocation,
2641 TIntermTyped *initializer,
2642 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002643{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002644 // If the declaration starting this declarator list was empty (example: int,), some checks were
2645 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002646 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002647 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002648 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2649 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002650 }
2651
Olli Etuaho856c4972016-08-08 11:38:39 +03002652 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002653
Olli Etuaho8a176262016-08-16 14:23:01 +03002654 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002655
2656 TPublicType arrayType(publicType);
2657
Olli Etuaho856c4972016-08-08 11:38:39 +03002658 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002659 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2660 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002661 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002662 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002663 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002664 }
2665 // Make the type an array even if size check failed.
2666 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2667 arrayType.setArraySize(size);
2668
2669 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002670 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002671 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002672 {
2673 if (initNode)
2674 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002675 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002676 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002677 }
2678}
2679
jchen104cdac9e2017-05-08 11:01:20 +08002680void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2681 const TSourceLoc &location)
2682{
2683 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2684 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2685 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2686 {
2687 error(location, "Requires both binding and offset", "layout");
2688 return;
2689 }
2690 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2691}
2692
Olli Etuahocce89652017-06-19 16:04:09 +03002693void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2694 const TPublicType &type,
2695 const TSourceLoc &loc)
2696{
2697 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2698 !getFragmentPrecisionHigh())
2699 {
2700 error(loc, "precision is not supported in fragment shader", "highp");
2701 }
2702
2703 if (!CanSetDefaultPrecisionOnType(type))
2704 {
2705 error(loc, "illegal type argument for default precision qualifier",
2706 getBasicString(type.getBasicType()));
2707 return;
2708 }
2709 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2710}
2711
Shaob5cc1192017-07-06 10:47:20 +08002712bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2713{
2714 switch (typeQualifier.layoutQualifier.primitiveType)
2715 {
2716 case EptLines:
2717 case EptLinesAdjacency:
2718 case EptTriangles:
2719 case EptTrianglesAdjacency:
2720 return typeQualifier.qualifier == EvqGeometryIn;
2721
2722 case EptLineStrip:
2723 case EptTriangleStrip:
2724 return typeQualifier.qualifier == EvqGeometryOut;
2725
2726 case EptPoints:
2727 return true;
2728
2729 default:
2730 UNREACHABLE();
2731 return false;
2732 }
2733}
2734
Jiawei Shaod8105a02017-08-08 09:54:36 +08002735void TParseContext::setGeometryShaderInputArraySizes()
2736{
2737 // TODO(jiawei.shao@intel.com): check former input array sizes match the input primitive
2738 // declaration.
2739 ASSERT(mGeometryShaderInputArraySize == 0);
2740 mGeometryShaderInputArraySize =
2741 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType);
2742}
2743
Shaob5cc1192017-07-06 10:47:20 +08002744bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2745{
2746 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2747
2748 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2749
2750 if (layoutQualifier.maxVertices != -1)
2751 {
2752 error(typeQualifier.line,
2753 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2754 return false;
2755 }
2756
2757 // Set mGeometryInputPrimitiveType if exists
2758 if (layoutQualifier.primitiveType != EptUndefined)
2759 {
2760 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2761 {
2762 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2763 return false;
2764 }
2765
2766 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2767 {
2768 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shaod8105a02017-08-08 09:54:36 +08002769 setGeometryShaderInputArraySizes();
Shaob5cc1192017-07-06 10:47:20 +08002770 }
2771 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2772 {
2773 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2774 "layout");
2775 return false;
2776 }
2777 }
2778
2779 // Set mGeometryInvocations if exists
2780 if (layoutQualifier.invocations > 0)
2781 {
2782 if (mGeometryShaderInvocations == 0)
2783 {
2784 mGeometryShaderInvocations = layoutQualifier.invocations;
2785 }
2786 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2787 {
2788 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2789 "layout");
2790 return false;
2791 }
2792 }
2793
2794 return true;
2795}
2796
2797bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2798{
2799 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2800
2801 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2802
2803 if (layoutQualifier.invocations > 0)
2804 {
2805 error(typeQualifier.line,
2806 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2807 return false;
2808 }
2809
2810 // Set mGeometryOutputPrimitiveType if exists
2811 if (layoutQualifier.primitiveType != EptUndefined)
2812 {
2813 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2814 {
2815 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2816 return false;
2817 }
2818
2819 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2820 {
2821 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2822 }
2823 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2824 {
2825 error(typeQualifier.line,
2826 "primitive doesn't match earlier output primitive declaration", "layout");
2827 return false;
2828 }
2829 }
2830
2831 // Set mGeometryMaxVertices if exists
2832 if (layoutQualifier.maxVertices > -1)
2833 {
2834 if (mGeometryShaderMaxVertices == -1)
2835 {
2836 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2837 }
2838 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2839 {
2840 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2841 "layout");
2842 return false;
2843 }
2844 }
2845
2846 return true;
2847}
2848
Martin Radev70866b82016-07-22 15:27:42 +03002849void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002850{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002851 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002852 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002853
Martin Radev70866b82016-07-22 15:27:42 +03002854 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2855 typeQualifier.line);
2856
Jamie Madillc2128ff2016-07-04 10:26:17 -04002857 // It should never be the case, but some strange parser errors can send us here.
2858 if (layoutQualifier.isEmpty())
2859 {
2860 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002861 return;
2862 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002863
Martin Radev802abe02016-08-04 17:48:32 +03002864 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002865 {
Olli Etuaho43364892017-02-13 16:00:12 +00002866 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002867 return;
2868 }
2869
Olli Etuaho43364892017-02-13 16:00:12 +00002870 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2871
2872 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002873
2874 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2875
Andrei Volykhina5527072017-03-22 16:46:30 +03002876 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
2877
jchen104cdac9e2017-05-08 11:01:20 +08002878 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
2879
Martin Radev802abe02016-08-04 17:48:32 +03002880 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002881 {
Martin Radev802abe02016-08-04 17:48:32 +03002882 if (mComputeShaderLocalSizeDeclared &&
2883 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2884 {
2885 error(typeQualifier.line, "Work group size does not match the previous declaration",
2886 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002887 return;
2888 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002889
Martin Radev802abe02016-08-04 17:48:32 +03002890 if (mShaderVersion < 310)
2891 {
2892 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002893 return;
2894 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002895
Martin Radev4c4c8e72016-08-04 12:25:34 +03002896 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03002897 {
2898 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002899 return;
2900 }
2901
2902 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
2903 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
2904
2905 const TConstantUnion *maxComputeWorkGroupSizeData =
2906 maxComputeWorkGroupSize->getConstPointer();
2907
2908 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
2909 {
2910 if (layoutQualifier.localSize[i] != -1)
2911 {
2912 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
2913 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
2914 if (mComputeShaderLocalSize[i] < 1 ||
2915 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
2916 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002917 std::stringstream reasonStream;
2918 reasonStream << "invalid value: Value must be at least 1 and no greater than "
2919 << maxComputeWorkGroupSizeValue;
2920 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03002921
Olli Etuaho4de340a2016-12-16 09:32:03 +00002922 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03002923 return;
2924 }
2925 }
2926 }
2927
2928 mComputeShaderLocalSizeDeclared = true;
2929 }
Shaob5cc1192017-07-06 10:47:20 +08002930 else if (typeQualifier.qualifier == EvqGeometryIn)
2931 {
2932 if (mShaderVersion < 310)
2933 {
2934 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
2935 return;
2936 }
2937
2938 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
2939 {
2940 return;
2941 }
2942 }
2943 else if (typeQualifier.qualifier == EvqGeometryOut)
2944 {
2945 if (mShaderVersion < 310)
2946 {
2947 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
2948 "layout");
2949 return;
2950 }
2951
2952 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
2953 {
2954 return;
2955 }
2956 }
Olli Etuaho95468d12017-05-04 11:14:34 +03002957 else if (isMultiviewExtensionEnabled() && typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00002958 {
2959 // This error is only specified in WebGL, but tightens unspecified behavior in the native
2960 // specification.
2961 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
2962 {
2963 error(typeQualifier.line, "Number of views does not match the previous declaration",
2964 "layout");
2965 return;
2966 }
2967
2968 if (layoutQualifier.numViews == -1)
2969 {
2970 error(typeQualifier.line, "No num_views specified", "layout");
2971 return;
2972 }
2973
2974 if (layoutQualifier.numViews > mMaxNumViews)
2975 {
2976 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
2977 "layout");
2978 return;
2979 }
2980
2981 mNumViews = layoutQualifier.numViews;
2982 }
Martin Radev802abe02016-08-04 17:48:32 +03002983 else
Jamie Madill1566ef72013-06-20 11:55:54 -04002984 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00002985 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03002986 {
Martin Radev802abe02016-08-04 17:48:32 +03002987 return;
2988 }
2989
Jiajia Qinbc585152017-06-23 15:42:17 +08002990 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03002991 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002992 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00002993 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03002994 return;
2995 }
2996
2997 if (mShaderVersion < 300)
2998 {
2999 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
3000 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003001 return;
3002 }
3003
Olli Etuaho09b04a22016-12-15 13:30:26 +00003004 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003005
3006 if (layoutQualifier.matrixPacking != EmpUnspecified)
3007 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003008 if (typeQualifier.qualifier == EvqUniform)
3009 {
3010 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3011 }
3012 else if (typeQualifier.qualifier == EvqBuffer)
3013 {
3014 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3015 }
Martin Radev802abe02016-08-04 17:48:32 +03003016 }
3017
3018 if (layoutQualifier.blockStorage != EbsUnspecified)
3019 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003020 if (typeQualifier.qualifier == EvqUniform)
3021 {
3022 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3023 }
3024 else if (typeQualifier.qualifier == EvqBuffer)
3025 {
3026 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3027 }
Martin Radev802abe02016-08-04 17:48:32 +03003028 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003029 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003030}
3031
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003032TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3033 const TFunction &function,
3034 const TSourceLoc &location,
3035 bool insertParametersToSymbolTable)
3036{
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003037 checkIsNotReserved(location, function.getName());
3038
Olli Etuahofe486322017-03-21 09:30:54 +00003039 TIntermFunctionPrototype *prototype =
3040 new TIntermFunctionPrototype(function.getReturnType(), TSymbolUniqueId(function));
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003041 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
3042 // point to the data that already exists in the symbol table.
3043 prototype->getFunctionSymbolInfo()->setFromFunction(function);
3044 prototype->setLine(location);
3045
3046 for (size_t i = 0; i < function.getParamCount(); i++)
3047 {
3048 const TConstParameter &param = function.getParam(i);
3049
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003050 TIntermSymbol *symbol = nullptr;
3051
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003052 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3053 // be used for unused args).
3054 if (param.name != nullptr)
3055 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003056 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003057 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003058 {
Olli Etuaho0f684632017-07-13 12:42:15 +03003059 TVariable *variable = symbolTable.declareVariable(param.name, *param.type);
3060 if (variable)
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003061 {
3062 symbol = new TIntermSymbol(variable->getUniqueId(), variable->getName(),
3063 variable->getType());
3064 }
3065 else
3066 {
Olli Etuaho85d624a2017-08-07 13:42:33 +03003067 error(location, "redefinition", param.name->c_str());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003068 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003069 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003070 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003071 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003072 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003073 // The parameter had no name or declaring the symbol failed - either way, add a nameless
3074 // symbol.
3075 symbol = new TIntermSymbol(0, "", *param.type);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003076 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003077 symbol->setLine(location);
3078 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003079 }
3080 return prototype;
3081}
3082
Olli Etuaho16c745a2017-01-16 17:02:27 +00003083TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3084 const TFunction &parsedFunction,
3085 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003086{
Olli Etuaho476197f2016-10-11 13:59:08 +01003087 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3088 // first declaration. Either way the instance in the symbol table is used to track whether the
3089 // function is declared multiple times.
3090 TFunction *function = static_cast<TFunction *>(
3091 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
3092 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003093 {
3094 // ESSL 1.00.17 section 4.2.7.
3095 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3096 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003097 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003098 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02003099
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003100 TIntermFunctionPrototype *prototype =
3101 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003102
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003103 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003104
3105 if (!symbolTable.atGlobalLevel())
3106 {
3107 // ESSL 3.00.4 section 4.2.4.
3108 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003109 }
3110
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003111 return prototype;
3112}
3113
Olli Etuaho336b1472016-10-05 16:37:55 +01003114TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003115 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003116 TIntermBlock *functionBody,
3117 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003118{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003119 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003120 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3121 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003122 error(location, "function does not return a value:",
3123 functionPrototype->getFunctionSymbolInfo()->getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003124 }
3125
Olli Etuahof51fdd22016-10-03 10:03:40 +01003126 if (functionBody == nullptr)
3127 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003128 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003129 functionBody->setLine(location);
3130 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003131 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003132 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003133 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003134
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003135 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003136 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003137}
3138
Olli Etuaho476197f2016-10-11 13:59:08 +01003139void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
3140 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003141 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003142{
Olli Etuaho476197f2016-10-11 13:59:08 +01003143 ASSERT(function);
3144 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003145 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01003146 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003147
3148 if (builtIn)
3149 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003150 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003151 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003152 else
Jamie Madill185fb402015-06-12 15:48:48 -04003153 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003154 TFunction *prevDec = static_cast<TFunction *>(
3155 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
3156
3157 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
3158 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
3159 // occurance.
3160 if (*function != prevDec)
3161 {
3162 // Swap the parameters of the previous declaration to the parameters of the function
3163 // definition (parameter names may differ).
3164 prevDec->swapParameters(**function);
3165
3166 // The function definition will share the same symbol as any previous declaration.
3167 *function = prevDec;
3168 }
3169
3170 if ((*function)->isDefined())
3171 {
3172 error(location, "function already has a body", (*function)->getName().c_str());
3173 }
3174
3175 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04003176 }
Jamie Madill185fb402015-06-12 15:48:48 -04003177
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003178 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01003179 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003180 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003181
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003182 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003183 setLoopNestingLevel(0);
3184}
3185
Jamie Madillb98c3a82015-07-23 14:26:04 -04003186TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003187{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003188 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003189 // We don't know at this point whether this is a function definition or a prototype.
3190 // The definition production code will check for redefinitions.
3191 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003192 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003193 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3194 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003195 //
3196 TFunction *prevDec =
3197 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303198
Martin Radevda6254b2016-12-14 17:00:36 +02003199 if (getShaderVersion() >= 300 &&
3200 symbolTable.hasUnmangledBuiltInForShaderVersion(function->getName().c_str(),
3201 getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303202 {
Martin Radevda6254b2016-12-14 17:00:36 +02003203 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303204 // Therefore overloading or redefining builtin functions is an error.
3205 error(location, "Name of a built-in function cannot be redeclared as function",
3206 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303207 }
3208 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003209 {
3210 if (prevDec->getReturnType() != function->getReturnType())
3211 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003212 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003213 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003214 }
3215 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3216 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003217 if (prevDec->getParam(i).type->getQualifier() !=
3218 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003219 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003220 error(location,
3221 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003222 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003223 }
3224 }
3225 }
3226
3227 //
3228 // Check for previously declared variables using the same name.
3229 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003230 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003231 if (prevSym)
3232 {
3233 if (!prevSym->isFunction())
3234 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003235 error(location, "redefinition of a function", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003236 }
3237 }
3238 else
3239 {
3240 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01003241 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003242 }
3243
3244 // We're at the inner scope level of the function's arguments and body statement.
3245 // Add the function prototype to the surrounding scope instead.
3246 symbolTable.getOuterLevel()->insert(function);
3247
Olli Etuaho78d13742017-01-18 13:06:10 +00003248 // Raise error message if main function takes any parameters or return anything other than void
3249 if (function->getName() == "main")
3250 {
3251 if (function->getParamCount() > 0)
3252 {
3253 error(location, "function cannot take any parameter(s)", "main");
3254 }
3255 if (function->getReturnType().getBasicType() != EbtVoid)
3256 {
3257 error(location, "main function cannot return a value",
3258 function->getReturnType().getBasicString());
3259 }
3260 }
3261
Jamie Madill185fb402015-06-12 15:48:48 -04003262 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003263 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3264 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003265 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3266 //
3267 return function;
3268}
3269
Olli Etuaho9de84a52016-06-14 17:36:01 +03003270TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
3271 const TString *name,
3272 const TSourceLoc &location)
3273{
3274 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3275 {
3276 error(location, "no qualifiers allowed for function return",
3277 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003278 }
3279 if (!type.layoutQualifier.isEmpty())
3280 {
3281 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003282 }
jchen10cc2a10e2017-05-03 14:05:12 +08003283 // make sure an opaque type is not involved as well...
3284 std::string reason(getBasicString(type.getBasicType()));
3285 reason += "s can't be function return values";
3286 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003287 if (mShaderVersion < 300)
3288 {
3289 // Array return values are forbidden, but there's also no valid syntax for declaring array
3290 // return values in ESSL 1.00.
Olli Etuaho77ba4082016-12-16 12:01:18 +00003291 ASSERT(type.arraySize == 0 || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003292
3293 if (type.isStructureContainingArrays())
3294 {
3295 // ESSL 1.00.17 section 6.1 Function Definitions
3296 error(location, "structures containing arrays can't be function return values",
3297 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003298 }
3299 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003300
3301 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003302 return new TFunction(&symbolTable, name, new TType(type));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003303}
3304
Olli Etuahocce89652017-06-19 16:04:09 +03003305TFunction *TParseContext::addNonConstructorFunc(const TString *name, const TSourceLoc &loc)
3306{
Olli Etuahocce89652017-06-19 16:04:09 +03003307 const TType *returnType = TCache::getType(EbtVoid, EbpUndefined);
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003308 return new TFunction(&symbolTable, name, returnType);
Olli Etuahocce89652017-06-19 16:04:09 +03003309}
3310
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003311TFunction *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003312{
Olli Etuahocce89652017-06-19 16:04:09 +03003313 if (mShaderVersion < 300 && publicType.array)
3314 {
3315 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3316 "[]");
3317 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003318 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003319 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003320 error(publicType.getLine(), "constructor can't be a structure definition",
3321 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003322 }
3323
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003324 TType *type = new TType(publicType);
3325 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003326 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003327 error(publicType.getLine(), "cannot construct this type",
3328 getBasicString(publicType.getBasicType()));
3329 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003330 }
3331
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003332 return new TFunction(&symbolTable, nullptr, type, EOpConstruct);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003333}
3334
Olli Etuahocce89652017-06-19 16:04:09 +03003335TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3336 const TString *name,
3337 const TSourceLoc &nameLoc)
3338{
3339 if (publicType.getBasicType() == EbtVoid)
3340 {
3341 error(nameLoc, "illegal use of type 'void'", name->c_str());
3342 }
3343 checkIsNotReserved(nameLoc, *name);
3344 TType *type = new TType(publicType);
3345 TParameter param = {name, type};
3346 return param;
3347}
3348
3349TParameter TParseContext::parseParameterArrayDeclarator(const TString *identifier,
3350 const TSourceLoc &identifierLoc,
3351 TIntermTyped *arraySize,
3352 const TSourceLoc &arrayLoc,
3353 TPublicType *type)
3354{
Olli Etuahoe0803872017-08-23 15:30:23 +03003355 checkArrayElementIsNotArray(arrayLoc, *type);
Olli Etuahocce89652017-06-19 16:04:09 +03003356 unsigned int size = checkIsValidArraySize(arrayLoc, arraySize);
3357 type->setArraySize(size);
3358 return parseParameterDeclarator(*type, identifier, identifierLoc);
3359}
3360
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003361bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(TIntermSequence *arguments,
3362 TType type,
3363 const TSourceLoc &line)
3364{
3365 if (arguments->empty())
3366 {
3367 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3368 return false;
3369 }
3370 for (TIntermNode *arg : *arguments)
3371 {
3372 TIntermTyped *element = arg->getAsTyped();
3373 ASSERT(element);
3374 size_t dimensionalityFromElement = element->getType().getArraySizes().size() + 1u;
3375 if (dimensionalityFromElement > type.getArraySizes().size())
3376 {
3377 error(line, "constructing from a non-dereferenced array", "constructor");
3378 return false;
3379 }
3380 else if (dimensionalityFromElement < type.getArraySizes().size())
3381 {
3382 if (dimensionalityFromElement == 1u)
3383 {
3384 error(line, "implicitly sized array of arrays constructor argument is not an array",
3385 "constructor");
3386 }
3387 else
3388 {
3389 error(line,
3390 "implicitly sized array of arrays constructor argument dimensionality is too "
3391 "low",
3392 "constructor");
3393 }
3394 return false;
3395 }
3396 }
3397 return true;
3398}
3399
Jamie Madillb98c3a82015-07-23 14:26:04 -04003400// This function is used to test for the correctness of the parameters passed to various constructor
3401// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003402//
Olli Etuaho856c4972016-08-08 11:38:39 +03003403// 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 +00003404//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003405TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00003406 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303407 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003408{
Olli Etuaho856c4972016-08-08 11:38:39 +03003409 if (type.isUnsizedArray())
3410 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003411 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003412 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003413 type.sizeUnsizedArrays(TVector<unsigned int>());
Olli Etuaho3ec75682017-07-05 17:02:55 +03003414 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003415 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003416 TIntermTyped *firstElement = arguments->at(0)->getAsTyped();
3417 ASSERT(firstElement);
3418 type.setArraySize(type.getArraySizes().size() - 1u,
3419 static_cast<unsigned int>(arguments->size()));
3420 for (size_t i = 0; i < firstElement->getType().getArraySizes().size(); ++i)
3421 {
3422 if (type.getArraySizes()[i] == 0u)
3423 {
3424 type.setArraySize(i, firstElement->getType().getArraySizes().at(i));
3425 }
3426 }
3427 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003428 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003429
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003430 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003431 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003432 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003433 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003434
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003435 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003436 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003437
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003438 // TODO(oetuaho@nvidia.com): Add support for folding array constructors.
3439 if (!constructorNode->isArray())
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003440 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003441 return constructorNode->fold(mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003442 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003443 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003444}
3445
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003446//
3447// Interface/uniform blocks
Jiawei Shaod8105a02017-08-08 09:54:36 +08003448// TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003449//
Olli Etuaho13389b62016-10-16 11:48:18 +01003450TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003451 const TTypeQualifierBuilder &typeQualifierBuilder,
3452 const TSourceLoc &nameLine,
3453 const TString &blockName,
3454 TFieldList *fieldList,
3455 const TString *instanceName,
3456 const TSourceLoc &instanceLine,
3457 TIntermTyped *arrayIndex,
3458 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003459{
Olli Etuaho856c4972016-08-08 11:38:39 +03003460 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003461
Olli Etuaho77ba4082016-12-16 12:01:18 +00003462 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003463
Jiajia Qinbc585152017-06-23 15:42:17 +08003464 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003465 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003466 error(typeQualifier.line,
3467 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3468 "3.10",
3469 getQualifierString(typeQualifier.qualifier));
3470 }
3471 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3472 {
3473 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003474 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003475 }
3476
Martin Radev70866b82016-07-22 15:27:42 +03003477 if (typeQualifier.invariant)
3478 {
3479 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3480 }
3481
Jiajia Qinbc585152017-06-23 15:42:17 +08003482 if (typeQualifier.qualifier != EvqBuffer)
3483 {
3484 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3485 }
Olli Etuaho43364892017-02-13 16:00:12 +00003486
jchen10af713a22017-04-19 09:10:56 +08003487 // add array index
3488 unsigned int arraySize = 0;
3489 if (arrayIndex != nullptr)
3490 {
3491 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3492 }
3493
3494 if (mShaderVersion < 310)
3495 {
3496 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3497 }
3498 else
3499 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003500 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3501 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003502 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003503
Andrei Volykhina5527072017-03-22 16:46:30 +03003504 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3505
Jamie Madill099c0f32013-06-20 11:55:52 -04003506 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003507 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003508
Jamie Madill099c0f32013-06-20 11:55:52 -04003509 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3510 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003511 if (typeQualifier.qualifier == EvqUniform)
3512 {
3513 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3514 }
3515 else if (typeQualifier.qualifier == EvqBuffer)
3516 {
3517 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3518 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003519 }
3520
Jamie Madill1566ef72013-06-20 11:55:54 -04003521 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3522 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003523 if (typeQualifier.qualifier == EvqUniform)
3524 {
3525 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3526 }
3527 else if (typeQualifier.qualifier == EvqBuffer)
3528 {
3529 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3530 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003531 }
3532
Olli Etuaho856c4972016-08-08 11:38:39 +03003533 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003534
Martin Radev2cc85b32016-08-05 16:22:53 +03003535 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3536
Olli Etuaho0f684632017-07-13 12:42:15 +03003537 if (!symbolTable.declareInterfaceBlockName(&blockName))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303538 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003539 error(nameLine, "redefinition of an interface block name", blockName.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003540 }
3541
Jamie Madill98493dd2013-07-08 14:39:03 -04003542 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303543 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3544 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003545 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303546 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003547 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303548 {
jchen10cc2a10e2017-05-03 14:05:12 +08003549 std::string reason("unsupported type - ");
3550 reason += fieldType->getBasicString();
3551 reason += " types are not allowed in interface blocks";
3552 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003553 }
3554
Jamie Madill98493dd2013-07-08 14:39:03 -04003555 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003556 switch (qualifier)
3557 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003558 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003559 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003560 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003561 if (typeQualifier.qualifier == EvqBuffer)
3562 {
3563 error(field->line(), "invalid qualifier on shader storage block member",
3564 getQualifierString(qualifier));
3565 }
3566 break;
3567 case EvqBuffer:
3568 if (typeQualifier.qualifier == EvqUniform)
3569 {
3570 error(field->line(), "invalid qualifier on uniform block member",
3571 getQualifierString(qualifier));
3572 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003573 break;
3574 default:
3575 error(field->line(), "invalid qualifier on interface block member",
3576 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003577 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003578 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003579
Martin Radev70866b82016-07-22 15:27:42 +03003580 if (fieldType->isInvariant())
3581 {
3582 error(field->line(), "invalid qualifier on interface block member", "invariant");
3583 }
3584
Jamie Madilla5efff92013-06-06 11:56:47 -04003585 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003586 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003587 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003588 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003589
Jamie Madill98493dd2013-07-08 14:39:03 -04003590 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003591 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003592 error(field->line(), "invalid layout qualifier: cannot be used here",
3593 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003594 }
3595
Jamie Madill98493dd2013-07-08 14:39:03 -04003596 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003597 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003598 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003599 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003600 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003601 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003602 warning(field->line(),
3603 "extraneous layout qualifier: only has an effect on matrix types",
3604 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003605 }
3606
Jamie Madill98493dd2013-07-08 14:39:03 -04003607 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003608
3609 if (typeQualifier.qualifier == EvqBuffer)
3610 {
3611 // set memory qualifiers
3612 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3613 // qualified with a memory qualifier, it is as if all of its members were declared with
3614 // the same memory qualifier.
3615 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3616 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3617 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3618 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3619 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3620 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3621 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3622 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3623 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3624 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3625 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003626 }
3627
Jamie Madillb98c3a82015-07-23 14:26:04 -04003628 TInterfaceBlock *interfaceBlock =
Shaob18c33e2017-08-16 12:37:51 +08003629 new TInterfaceBlock(&blockName, fieldList, instanceName, blockLayoutQualifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003630 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
3631 if (arrayIndex != nullptr)
3632 {
3633 interfaceBlockType.makeArray(arraySize);
3634 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003635
3636 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04003637 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003638
Jamie Madill98493dd2013-07-08 14:39:03 -04003639 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003640 {
3641 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003642 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3643 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003644 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303645 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04003646
3647 // set parent pointer of the field variable
3648 fieldType->setInterfaceBlock(interfaceBlock);
3649
Olli Etuaho0f684632017-07-13 12:42:15 +03003650 TVariable *fieldVariable = symbolTable.declareVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04003651
Olli Etuaho0f684632017-07-13 12:42:15 +03003652 if (fieldVariable)
3653 {
3654 fieldVariable->setQualifier(typeQualifier.qualifier);
3655 }
3656 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303657 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003658 error(field->line(), "redefinition of an interface block member name",
3659 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003660 }
3661 }
3662 }
3663 else
3664 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003665 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003666
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003667 // add a symbol for this interface block
Olli Etuaho0f684632017-07-13 12:42:15 +03003668 TVariable *instanceTypeDef = symbolTable.declareVariable(instanceName, interfaceBlockType);
3669 if (instanceTypeDef)
3670 {
3671 instanceTypeDef->setQualifier(typeQualifier.qualifier);
3672 symbolId = instanceTypeDef->getUniqueId();
3673 }
3674 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303675 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003676 error(instanceLine, "redefinition of an interface block instance name",
3677 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003678 }
Olli Etuaho0f684632017-07-13 12:42:15 +03003679 symbolName = *instanceName;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003680 }
3681
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003682 TIntermSymbol *blockSymbol = new TIntermSymbol(symbolId, symbolName, interfaceBlockType);
3683 blockSymbol->setLine(typeQualifier.line);
Olli Etuaho13389b62016-10-16 11:48:18 +01003684 TIntermDeclaration *declaration = new TIntermDeclaration();
3685 declaration->appendDeclarator(blockSymbol);
3686 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003687
3688 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003689 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003690}
3691
Olli Etuaho383b7912016-08-05 11:22:59 +03003692void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003693{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003694 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003695
3696 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003697 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303698 if (mStructNestingLevel > 1)
3699 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003700 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003701 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003702}
3703
3704void TParseContext::exitStructDeclaration()
3705{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003706 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003707}
3708
Olli Etuaho8a176262016-08-16 14:23:01 +03003709void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003710{
Jamie Madillacb4b812016-11-07 13:50:29 -05003711 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303712 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003713 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003714 }
3715
Arun Patole7e7e68d2015-05-22 12:02:25 +05303716 if (field.type()->getBasicType() != EbtStruct)
3717 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003718 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003719 }
3720
3721 // We're already inside a structure definition at this point, so add
3722 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303723 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3724 {
Jamie Madill41a49272014-03-18 16:10:13 -04003725 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003726 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
3727 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003728 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003729 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003730 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003731 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003732}
3733
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003734//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003735// Parse an array index expression
3736//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003737TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3738 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303739 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003740{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003741 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3742 {
3743 if (baseExpression->getAsSymbolNode())
3744 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303745 error(location, " left of '[' is not of type array, matrix, or vector ",
3746 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003747 }
3748 else
3749 {
3750 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3751 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003752
Olli Etuaho3ec75682017-07-05 17:02:55 +03003753 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003754 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003755
Jiawei Shaod8105a02017-08-08 09:54:36 +08003756 if (baseExpression->getQualifier() == EvqPerVertexIn)
3757 {
3758 ASSERT(mShaderType == GL_GEOMETRY_SHADER_OES);
3759 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3760 {
3761 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3762 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3763 }
3764 }
3765
Jamie Madill21c1e452014-12-29 11:33:41 -05003766 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3767
Olli Etuaho36b05142015-11-12 13:10:42 +02003768 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3769 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3770 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3771 // index is a constant expression.
3772 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3773 {
3774 if (baseExpression->isInterfaceBlock())
3775 {
Jiawei Shaod8105a02017-08-08 09:54:36 +08003776 // TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
3777 switch (baseExpression->getQualifier())
3778 {
3779 case EvqPerVertexIn:
3780 break;
3781 case EvqUniform:
3782 case EvqBuffer:
3783 error(location,
3784 "array indexes for uniform block arrays and shader storage block arrays "
3785 "must be constant integral expressions",
3786 "[");
3787 break;
3788 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003789 // We can reach here only in error cases.
3790 ASSERT(mDiagnostics->numErrors() > 0);
3791 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003792 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003793 }
3794 else if (baseExpression->getQualifier() == EvqFragmentOut)
3795 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003796 error(location,
3797 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003798 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003799 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3800 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003801 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003802 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003803 }
3804
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003805 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003806 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003807 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3808 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3809 // constant fold expressions that are not constant expressions). The most compatible way to
3810 // handle this case is to report a warning instead of an error and force the index to be in
3811 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003812 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003813 int index = 0;
3814 if (indexConstantUnion->getBasicType() == EbtInt)
3815 {
3816 index = indexConstantUnion->getIConst(0);
3817 }
3818 else if (indexConstantUnion->getBasicType() == EbtUInt)
3819 {
3820 index = static_cast<int>(indexConstantUnion->getUConst(0));
3821 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003822
3823 int safeIndex = -1;
3824
3825 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04003826 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003827 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003828 {
Olli Etuahodaaff1c2017-07-05 18:03:26 +03003829 if (!isExtensionEnabled("GL_EXT_draw_buffers"))
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003830 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003831 outOfRangeError(outOfRangeIndexIsError, location,
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003832 "array index for gl_FragData must be zero when "
Olli Etuaho4de340a2016-12-16 09:32:03 +00003833 "GL_EXT_draw_buffers is disabled",
3834 "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003835 safeIndex = 0;
3836 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03003837 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003838 // Only do generic out-of-range check if similar error hasn't already been reported.
3839 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003840 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003841 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003842 baseExpression->getOutermostArraySize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003843 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003844 }
3845 }
3846 else if (baseExpression->isMatrix())
3847 {
3848 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03003849 baseExpression->getType().getCols(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003850 "matrix field selection out of range");
Jamie Madill7164cf42013-07-08 13:30:59 -04003851 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003852 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04003853 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003854 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3855 baseExpression->getType().getNominalSize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003856 "vector field selection out of range");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003857 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003858
3859 ASSERT(safeIndex >= 0);
3860 // Data of constant unions can't be changed, because it may be shared with other
3861 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
3862 // sanitized object.
Olli Etuaho56229f12017-07-10 14:16:33 +03003863 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003864 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003865 TConstantUnion *safeConstantUnion = new TConstantUnion();
3866 safeConstantUnion->setIConst(safeIndex);
3867 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
Olli Etuaho56229f12017-07-10 14:16:33 +03003868 indexConstantUnion->getTypePointer()->setBasicType(EbtInt);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003869 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003870
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003871 TIntermBinary *node = new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
3872 node->setLine(location);
3873 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003874 }
Jamie Madill7164cf42013-07-08 13:30:59 -04003875 else
3876 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003877 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
3878 node->setLine(location);
3879 // Indirect indexing can never be constant folded.
3880 return node;
Jamie Madill7164cf42013-07-08 13:30:59 -04003881 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003882}
3883
Olli Etuaho90892fb2016-07-14 14:44:51 +03003884int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
3885 const TSourceLoc &location,
3886 int index,
3887 int arraySize,
Olli Etuaho4de340a2016-12-16 09:32:03 +00003888 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003889{
3890 if (index >= arraySize || index < 0)
3891 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003892 std::stringstream reasonStream;
3893 reasonStream << reason << " '" << index << "'";
3894 std::string token = reasonStream.str();
3895 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuaho90892fb2016-07-14 14:44:51 +03003896 if (index < 0)
3897 {
3898 return 0;
3899 }
3900 else
3901 {
3902 return arraySize - 1;
3903 }
3904 }
3905 return index;
3906}
3907
Jamie Madillb98c3a82015-07-23 14:26:04 -04003908TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3909 const TSourceLoc &dotLocation,
3910 const TString &fieldString,
3911 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003912{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003913 if (baseExpression->isArray())
3914 {
3915 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003916 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003917 }
3918
3919 if (baseExpression->isVector())
3920 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003921 TVector<int> fieldOffsets;
3922 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
3923 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003924 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003925 fieldOffsets.resize(1);
3926 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003927 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003928 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
3929 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003930
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003931 return node->fold();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003932 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003933 else if (baseExpression->getBasicType() == EbtStruct)
3934 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303935 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003936 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003937 {
3938 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003939 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003940 }
3941 else
3942 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003943 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003944 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003945 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003946 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003947 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003948 {
3949 fieldFound = true;
3950 break;
3951 }
3952 }
3953 if (fieldFound)
3954 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003955 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003956 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003957 TIntermBinary *node =
3958 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
3959 node->setLine(dotLocation);
3960 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003961 }
3962 else
3963 {
3964 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003965 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003966 }
3967 }
3968 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003969 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003970 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303971 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003972 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003973 {
3974 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003975 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003976 }
3977 else
3978 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003979 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003980 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003981 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003982 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003983 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003984 {
3985 fieldFound = true;
3986 break;
3987 }
3988 }
3989 if (fieldFound)
3990 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003991 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003992 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003993 TIntermBinary *node =
3994 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
3995 node->setLine(dotLocation);
3996 // Indexing interface blocks can never be constant folded.
3997 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003998 }
3999 else
4000 {
4001 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004002 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004003 }
4004 }
4005 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004006 else
4007 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004008 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004009 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004010 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304011 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004012 }
4013 else
4014 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304015 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004016 " field selection requires structure, vector, or interface block on left hand "
4017 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304018 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004019 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004020 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004021 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004022}
4023
Jamie Madillb98c3a82015-07-23 14:26:04 -04004024TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4025 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004026{
Martin Radev802abe02016-08-04 17:48:32 +03004027 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004028
4029 if (qualifierType == "shared")
4030 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004031 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004032 {
4033 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4034 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004035 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004036 }
4037 else if (qualifierType == "packed")
4038 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004039 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004040 {
4041 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4042 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004043 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004044 }
4045 else if (qualifierType == "std140")
4046 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004047 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004048 }
4049 else if (qualifierType == "row_major")
4050 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004051 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004052 }
4053 else if (qualifierType == "column_major")
4054 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004055 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004056 }
4057 else if (qualifierType == "location")
4058 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004059 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
4060 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004061 }
Andrei Volykhina5527072017-03-22 16:46:30 +03004062 else if (qualifierType == "yuv" && isExtensionEnabled("GL_EXT_YUV_target") &&
4063 mShaderType == GL_FRAGMENT_SHADER)
4064 {
4065 qualifier.yuv = true;
4066 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004067 else if (qualifierType == "rgba32f")
4068 {
4069 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4070 qualifier.imageInternalFormat = EiifRGBA32F;
4071 }
4072 else if (qualifierType == "rgba16f")
4073 {
4074 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4075 qualifier.imageInternalFormat = EiifRGBA16F;
4076 }
4077 else if (qualifierType == "r32f")
4078 {
4079 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4080 qualifier.imageInternalFormat = EiifR32F;
4081 }
4082 else if (qualifierType == "rgba8")
4083 {
4084 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4085 qualifier.imageInternalFormat = EiifRGBA8;
4086 }
4087 else if (qualifierType == "rgba8_snorm")
4088 {
4089 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4090 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4091 }
4092 else if (qualifierType == "rgba32i")
4093 {
4094 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4095 qualifier.imageInternalFormat = EiifRGBA32I;
4096 }
4097 else if (qualifierType == "rgba16i")
4098 {
4099 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4100 qualifier.imageInternalFormat = EiifRGBA16I;
4101 }
4102 else if (qualifierType == "rgba8i")
4103 {
4104 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4105 qualifier.imageInternalFormat = EiifRGBA8I;
4106 }
4107 else if (qualifierType == "r32i")
4108 {
4109 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4110 qualifier.imageInternalFormat = EiifR32I;
4111 }
4112 else if (qualifierType == "rgba32ui")
4113 {
4114 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4115 qualifier.imageInternalFormat = EiifRGBA32UI;
4116 }
4117 else if (qualifierType == "rgba16ui")
4118 {
4119 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4120 qualifier.imageInternalFormat = EiifRGBA16UI;
4121 }
4122 else if (qualifierType == "rgba8ui")
4123 {
4124 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4125 qualifier.imageInternalFormat = EiifRGBA8UI;
4126 }
4127 else if (qualifierType == "r32ui")
4128 {
4129 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4130 qualifier.imageInternalFormat = EiifR32UI;
4131 }
Shaob5cc1192017-07-06 10:47:20 +08004132 else if (qualifierType == "points" && isExtensionEnabled("GL_OES_geometry_shader") &&
4133 mShaderType == GL_GEOMETRY_SHADER_OES)
4134 {
4135 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4136 qualifier.primitiveType = EptPoints;
4137 }
4138 else if (qualifierType == "lines" && isExtensionEnabled("GL_OES_geometry_shader") &&
4139 mShaderType == GL_GEOMETRY_SHADER_OES)
4140 {
4141 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4142 qualifier.primitiveType = EptLines;
4143 }
4144 else if (qualifierType == "lines_adjacency" && isExtensionEnabled("GL_OES_geometry_shader") &&
4145 mShaderType == GL_GEOMETRY_SHADER_OES)
4146 {
4147 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4148 qualifier.primitiveType = EptLinesAdjacency;
4149 }
4150 else if (qualifierType == "triangles" && isExtensionEnabled("GL_OES_geometry_shader") &&
4151 mShaderType == GL_GEOMETRY_SHADER_OES)
4152 {
4153 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4154 qualifier.primitiveType = EptTriangles;
4155 }
4156 else if (qualifierType == "triangles_adjacency" &&
4157 isExtensionEnabled("GL_OES_geometry_shader") && mShaderType == GL_GEOMETRY_SHADER_OES)
4158 {
4159 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4160 qualifier.primitiveType = EptTrianglesAdjacency;
4161 }
4162 else if (qualifierType == "line_strip" && isExtensionEnabled("GL_OES_geometry_shader") &&
4163 mShaderType == GL_GEOMETRY_SHADER_OES)
4164 {
4165 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4166 qualifier.primitiveType = EptLineStrip;
4167 }
4168 else if (qualifierType == "triangle_strip" && isExtensionEnabled("GL_OES_geometry_shader") &&
4169 mShaderType == GL_GEOMETRY_SHADER_OES)
4170 {
4171 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4172 qualifier.primitiveType = EptTriangleStrip;
4173 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004174
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004175 else
4176 {
4177 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004178 }
4179
Jamie Madilla5efff92013-06-06 11:56:47 -04004180 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004181}
4182
Martin Radev802abe02016-08-04 17:48:32 +03004183void TParseContext::parseLocalSize(const TString &qualifierType,
4184 const TSourceLoc &qualifierTypeLine,
4185 int intValue,
4186 const TSourceLoc &intValueLine,
4187 const std::string &intValueString,
4188 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004189 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004190{
Olli Etuaho856c4972016-08-08 11:38:39 +03004191 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004192 if (intValue < 1)
4193 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004194 std::stringstream reasonStream;
4195 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4196 std::string reason = reasonStream.str();
4197 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004198 }
4199 (*localSize)[index] = intValue;
4200}
4201
Olli Etuaho09b04a22016-12-15 13:30:26 +00004202void TParseContext::parseNumViews(int intValue,
4203 const TSourceLoc &intValueLine,
4204 const std::string &intValueString,
4205 int *numViews)
4206{
4207 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4208 // specification.
4209 if (intValue < 1)
4210 {
4211 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4212 }
4213 *numViews = intValue;
4214}
4215
Shaob5cc1192017-07-06 10:47:20 +08004216void TParseContext::parseInvocations(int intValue,
4217 const TSourceLoc &intValueLine,
4218 const std::string &intValueString,
4219 int *numInvocations)
4220{
4221 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4222 // it doesn't make sense to accept invocations <= 0.
4223 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4224 {
4225 error(intValueLine,
4226 "out of range: invocations must be in the range of [1, "
4227 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4228 intValueString.c_str());
4229 }
4230 else
4231 {
4232 *numInvocations = intValue;
4233 }
4234}
4235
4236void TParseContext::parseMaxVertices(int intValue,
4237 const TSourceLoc &intValueLine,
4238 const std::string &intValueString,
4239 int *maxVertices)
4240{
4241 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4242 // it doesn't make sense to accept max_vertices < 0.
4243 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4244 {
4245 error(
4246 intValueLine,
4247 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4248 intValueString.c_str());
4249 }
4250 else
4251 {
4252 *maxVertices = intValue;
4253 }
4254}
4255
Jamie Madillb98c3a82015-07-23 14:26:04 -04004256TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4257 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004258 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304259 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004260{
Martin Radev802abe02016-08-04 17:48:32 +03004261 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004262
Martin Radev802abe02016-08-04 17:48:32 +03004263 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004264
Martin Radev802abe02016-08-04 17:48:32 +03004265 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004266 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004267 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004268 if (intValue < 0)
4269 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004270 error(intValueLine, "out of range: location must be non-negative",
4271 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004272 }
4273 else
4274 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004275 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004276 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004277 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004278 }
Olli Etuaho43364892017-02-13 16:00:12 +00004279 else if (qualifierType == "binding")
4280 {
4281 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4282 if (intValue < 0)
4283 {
4284 error(intValueLine, "out of range: binding must be non-negative",
4285 intValueString.c_str());
4286 }
4287 else
4288 {
4289 qualifier.binding = intValue;
4290 }
4291 }
jchen104cdac9e2017-05-08 11:01:20 +08004292 else if (qualifierType == "offset")
4293 {
4294 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4295 if (intValue < 0)
4296 {
4297 error(intValueLine, "out of range: offset must be non-negative",
4298 intValueString.c_str());
4299 }
4300 else
4301 {
4302 qualifier.offset = intValue;
4303 }
4304 }
Martin Radev802abe02016-08-04 17:48:32 +03004305 else if (qualifierType == "local_size_x")
4306 {
4307 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4308 &qualifier.localSize);
4309 }
4310 else if (qualifierType == "local_size_y")
4311 {
4312 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4313 &qualifier.localSize);
4314 }
4315 else if (qualifierType == "local_size_z")
4316 {
4317 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4318 &qualifier.localSize);
4319 }
Olli Etuaho95468d12017-05-04 11:14:34 +03004320 else if (qualifierType == "num_views" && isMultiviewExtensionEnabled() &&
Olli Etuaho09b04a22016-12-15 13:30:26 +00004321 mShaderType == GL_VERTEX_SHADER)
4322 {
4323 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4324 }
Shaob5cc1192017-07-06 10:47:20 +08004325 else if (qualifierType == "invocations" && isExtensionEnabled("GL_OES_geometry_shader") &&
4326 mShaderType == GL_GEOMETRY_SHADER_OES)
4327 {
4328 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4329 }
4330 else if (qualifierType == "max_vertices" && isExtensionEnabled("GL_OES_geometry_shader") &&
4331 mShaderType == GL_GEOMETRY_SHADER_OES)
4332 {
4333 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4334 }
4335
Martin Radev802abe02016-08-04 17:48:32 +03004336 else
4337 {
4338 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004339 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004340
Jamie Madilla5efff92013-06-06 11:56:47 -04004341 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004342}
4343
Olli Etuaho613b9592016-09-05 12:05:53 +03004344TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4345{
4346 return new TTypeQualifierBuilder(
4347 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4348 mShaderVersion);
4349}
4350
Olli Etuahocce89652017-06-19 16:04:09 +03004351TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4352 const TSourceLoc &loc)
4353{
4354 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4355 return new TStorageQualifierWrapper(qualifier, loc);
4356}
4357
4358TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4359{
4360 if (getShaderType() == GL_VERTEX_SHADER)
4361 {
4362 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4363 }
4364 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4365}
4366
4367TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4368{
4369 if (declaringFunction())
4370 {
4371 return new TStorageQualifierWrapper(EvqIn, loc);
4372 }
Shaob5cc1192017-07-06 10:47:20 +08004373
4374 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004375 {
Shaob5cc1192017-07-06 10:47:20 +08004376 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004377 {
Shaob5cc1192017-07-06 10:47:20 +08004378 if (mShaderVersion < 300 && !isMultiviewExtensionEnabled())
4379 {
4380 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4381 }
4382 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004383 }
Shaob5cc1192017-07-06 10:47:20 +08004384 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004385 {
Shaob5cc1192017-07-06 10:47:20 +08004386 if (mShaderVersion < 300)
4387 {
4388 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4389 }
4390 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004391 }
Shaob5cc1192017-07-06 10:47:20 +08004392 case GL_COMPUTE_SHADER:
4393 {
4394 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4395 }
4396 case GL_GEOMETRY_SHADER_OES:
4397 {
4398 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4399 }
4400 default:
4401 {
4402 UNREACHABLE();
4403 return new TStorageQualifierWrapper(EvqLast, loc);
4404 }
Olli Etuahocce89652017-06-19 16:04:09 +03004405 }
Olli Etuahocce89652017-06-19 16:04:09 +03004406}
4407
4408TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4409{
4410 if (declaringFunction())
4411 {
4412 return new TStorageQualifierWrapper(EvqOut, loc);
4413 }
Shaob5cc1192017-07-06 10:47:20 +08004414 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004415 {
Shaob5cc1192017-07-06 10:47:20 +08004416 case GL_VERTEX_SHADER:
4417 {
4418 if (mShaderVersion < 300)
4419 {
4420 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4421 }
4422 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4423 }
4424 case GL_FRAGMENT_SHADER:
4425 {
4426 if (mShaderVersion < 300)
4427 {
4428 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4429 }
4430 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4431 }
4432 case GL_COMPUTE_SHADER:
4433 {
4434 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4435 return new TStorageQualifierWrapper(EvqLast, loc);
4436 }
4437 case GL_GEOMETRY_SHADER_OES:
4438 {
4439 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4440 }
4441 default:
4442 {
4443 UNREACHABLE();
4444 return new TStorageQualifierWrapper(EvqLast, loc);
4445 }
Olli Etuahocce89652017-06-19 16:04:09 +03004446 }
Olli Etuahocce89652017-06-19 16:04:09 +03004447}
4448
4449TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4450{
4451 if (!declaringFunction())
4452 {
4453 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4454 }
4455 return new TStorageQualifierWrapper(EvqInOut, loc);
4456}
4457
Jamie Madillb98c3a82015-07-23 14:26:04 -04004458TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004459 TLayoutQualifier rightQualifier,
4460 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004461{
Martin Radevc28888b2016-07-22 15:27:42 +03004462 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004463 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004464}
4465
Olli Etuahocce89652017-06-19 16:04:09 +03004466TField *TParseContext::parseStructDeclarator(TString *identifier, const TSourceLoc &loc)
4467{
4468 checkIsNotReserved(loc, *identifier);
4469 TType *type = new TType(EbtVoid, EbpUndefined);
4470 return new TField(type, identifier, loc);
4471}
4472
4473TField *TParseContext::parseStructArrayDeclarator(TString *identifier,
4474 const TSourceLoc &loc,
4475 TIntermTyped *arraySize,
4476 const TSourceLoc &arraySizeLoc)
4477{
4478 checkIsNotReserved(loc, *identifier);
4479
4480 TType *type = new TType(EbtVoid, EbpUndefined);
4481 unsigned int size = checkIsValidArraySize(arraySizeLoc, arraySize);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004482 type->makeArray(size);
Olli Etuahocce89652017-06-19 16:04:09 +03004483
4484 return new TField(type, identifier, loc);
4485}
4486
Olli Etuaho4de340a2016-12-16 09:32:03 +00004487TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4488 const TFieldList *newlyAddedFields,
4489 const TSourceLoc &location)
4490{
4491 for (TField *field : *newlyAddedFields)
4492 {
4493 for (TField *oldField : *processedFields)
4494 {
4495 if (oldField->name() == field->name())
4496 {
4497 error(location, "duplicate field name in structure", field->name().c_str());
4498 }
4499 }
4500 processedFields->push_back(field);
4501 }
4502 return processedFields;
4503}
4504
Martin Radev70866b82016-07-22 15:27:42 +03004505TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4506 const TTypeQualifierBuilder &typeQualifierBuilder,
4507 TPublicType *typeSpecifier,
4508 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004509{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004510 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004511
Martin Radev70866b82016-07-22 15:27:42 +03004512 typeSpecifier->qualifier = typeQualifier.qualifier;
4513 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004514 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004515 typeSpecifier->invariant = typeQualifier.invariant;
4516 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304517 {
Martin Radev70866b82016-07-22 15:27:42 +03004518 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004519 }
Martin Radev70866b82016-07-22 15:27:42 +03004520 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004521}
4522
Jamie Madillb98c3a82015-07-23 14:26:04 -04004523TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004524 TFieldList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004525{
Martin Radev4a9cd802016-09-01 16:51:51 +03004526 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4527 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004528
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004529 checkIsNonVoid(typeSpecifier.getLine(), (*declaratorList)[0]->name(),
4530 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004531
Martin Radev4a9cd802016-09-01 16:51:51 +03004532 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004533
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004534 for (unsigned int i = 0; i < declaratorList->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304535 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004536 auto declaratorArraySizes = (*declaratorList)[i]->type()->getArraySizes();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004537 // don't allow arrays of arrays
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004538 if (!declaratorArraySizes.empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304539 {
Olli Etuahoe0803872017-08-23 15:30:23 +03004540 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004541 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004542
4543 TType *type = (*declaratorList)[i]->type();
4544 *type = TType(typeSpecifier);
4545 for (unsigned int arraySize : declaratorArraySizes)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304546 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004547 type->makeArray(arraySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004548 }
4549
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004550 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*declaratorList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004551 }
4552
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004553 return declaratorList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004554}
4555
Martin Radev4a9cd802016-09-01 16:51:51 +03004556TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4557 const TSourceLoc &nameLine,
4558 const TString *structName,
4559 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004560{
Olli Etuahoa5e693a2017-07-13 16:07:26 +03004561 TStructure *structure = new TStructure(&symbolTable, structName, fieldList);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004562
Jamie Madill9b820842015-02-12 10:40:10 -05004563 // Store a bool in the struct if we're at global scope, to allow us to
4564 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004565 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004566
Jamie Madill98493dd2013-07-08 14:39:03 -04004567 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004568 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004569 checkIsNotReserved(nameLine, *structName);
Olli Etuaho0f684632017-07-13 12:42:15 +03004570 if (!symbolTable.declareStructType(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304571 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004572 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004573 }
4574 }
4575
4576 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004577 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004578 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004579 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004580 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004581 switch (qualifier)
4582 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004583 case EvqGlobal:
4584 case EvqTemporary:
4585 break;
4586 default:
4587 error(field.line(), "invalid qualifier on struct member",
4588 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004589 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004590 }
Martin Radev70866b82016-07-22 15:27:42 +03004591 if (field.type()->isInvariant())
4592 {
4593 error(field.line(), "invalid qualifier on struct member", "invariant");
4594 }
jchen104cdac9e2017-05-08 11:01:20 +08004595 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4596 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004597 {
4598 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4599 }
4600
Olli Etuaho43364892017-02-13 16:00:12 +00004601 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4602
4603 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004604
4605 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004606 }
4607
Martin Radev4a9cd802016-09-01 16:51:51 +03004608 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004609 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004610 exitStructDeclaration();
4611
Martin Radev4a9cd802016-09-01 16:51:51 +03004612 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004613}
4614
Jamie Madillb98c3a82015-07-23 14:26:04 -04004615TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004616 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004617 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004618{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004619 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004620 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004621 init->isVector())
4622 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004623 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4624 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004625 return nullptr;
4626 }
4627
Olli Etuahoac5274d2015-02-20 10:19:08 +02004628 if (statementList)
4629 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00004630 if (!ValidateSwitchStatementList(switchType, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004631 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02004632 return nullptr;
4633 }
4634 }
4635
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004636 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4637 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004638 return node;
4639}
4640
4641TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4642{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004643 if (mSwitchNestingLevel == 0)
4644 {
4645 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004646 return nullptr;
4647 }
4648 if (condition == nullptr)
4649 {
4650 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004651 return nullptr;
4652 }
4653 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004654 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004655 {
4656 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004657 }
4658 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004659 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4660 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4661 // fold in case labels.
4662 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004663 {
4664 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004665 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004666 TIntermCase *node = new TIntermCase(condition);
4667 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004668 return node;
4669}
4670
4671TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4672{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004673 if (mSwitchNestingLevel == 0)
4674 {
4675 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004676 return nullptr;
4677 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004678 TIntermCase *node = new TIntermCase(nullptr);
4679 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004680 return node;
4681}
4682
Jamie Madillb98c3a82015-07-23 14:26:04 -04004683TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4684 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004685 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004686{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004687 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004688
4689 switch (op)
4690 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004691 case EOpLogicalNot:
4692 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4693 child->isVector())
4694 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004695 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004696 return nullptr;
4697 }
4698 break;
4699 case EOpBitwiseNot:
4700 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4701 child->isMatrix() || child->isArray())
4702 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004703 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004704 return nullptr;
4705 }
4706 break;
4707 case EOpPostIncrement:
4708 case EOpPreIncrement:
4709 case EOpPostDecrement:
4710 case EOpPreDecrement:
4711 case EOpNegative:
4712 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004713 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4714 child->getBasicType() == EbtBool || child->isArray() ||
4715 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004716 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004717 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004718 return nullptr;
4719 }
4720 // Operators for built-ins are already type checked against their prototype.
4721 default:
4722 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004723 }
4724
Jiajia Qinbc585152017-06-23 15:42:17 +08004725 if (child->getMemoryQualifier().writeonly)
4726 {
4727 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4728 return nullptr;
4729 }
4730
Olli Etuahof119a262016-08-19 15:54:22 +03004731 TIntermUnary *node = new TIntermUnary(op, child);
4732 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004733
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004734 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004735}
4736
Olli Etuaho09b22472015-02-11 11:47:26 +02004737TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4738{
Olli Etuahocce89652017-06-19 16:04:09 +03004739 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004740 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004741 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004742 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004743 return child;
4744 }
4745 return node;
4746}
4747
Jamie Madillb98c3a82015-07-23 14:26:04 -04004748TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4749 TIntermTyped *child,
4750 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004751{
Olli Etuaho856c4972016-08-08 11:38:39 +03004752 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004753 return addUnaryMath(op, child, loc);
4754}
4755
Jamie Madillb98c3a82015-07-23 14:26:04 -04004756bool TParseContext::binaryOpCommonCheck(TOperator op,
4757 TIntermTyped *left,
4758 TIntermTyped *right,
4759 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004760{
jchen10b4cf5652017-05-05 18:51:17 +08004761 // Check opaque types are not allowed to be operands in expressions other than array indexing
4762 // and structure member selection.
4763 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4764 {
4765 switch (op)
4766 {
4767 case EOpIndexDirect:
4768 case EOpIndexIndirect:
4769 break;
4770 case EOpIndexDirectStruct:
4771 UNREACHABLE();
4772
4773 default:
4774 error(loc, "Invalid operation for variables with an opaque type",
4775 GetOperatorString(op));
4776 return false;
4777 }
4778 }
jchen10cc2a10e2017-05-03 14:05:12 +08004779
Jiajia Qinbc585152017-06-23 15:42:17 +08004780 if (right->getMemoryQualifier().writeonly)
4781 {
4782 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4783 return false;
4784 }
4785
4786 if (left->getMemoryQualifier().writeonly)
4787 {
4788 switch (op)
4789 {
4790 case EOpAssign:
4791 case EOpInitialize:
4792 case EOpIndexDirect:
4793 case EOpIndexIndirect:
4794 case EOpIndexDirectStruct:
4795 case EOpIndexDirectInterfaceBlock:
4796 break;
4797 default:
4798 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4799 return false;
4800 }
4801 }
4802
Olli Etuaho244be012016-08-18 15:26:02 +03004803 if (left->getType().getStruct() || right->getType().getStruct())
4804 {
4805 switch (op)
4806 {
4807 case EOpIndexDirectStruct:
4808 ASSERT(left->getType().getStruct());
4809 break;
4810 case EOpEqual:
4811 case EOpNotEqual:
4812 case EOpAssign:
4813 case EOpInitialize:
4814 if (left->getType() != right->getType())
4815 {
4816 return false;
4817 }
4818 break;
4819 default:
4820 error(loc, "Invalid operation for structs", GetOperatorString(op));
4821 return false;
4822 }
4823 }
4824
Olli Etuaho94050052017-05-08 14:17:44 +03004825 if (left->isInterfaceBlock() || right->isInterfaceBlock())
4826 {
4827 switch (op)
4828 {
4829 case EOpIndexDirectInterfaceBlock:
4830 ASSERT(left->getType().getInterfaceBlock());
4831 break;
4832 default:
4833 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
4834 return false;
4835 }
4836 }
4837
Olli Etuahod6b14282015-03-17 14:31:35 +02004838 if (left->isArray() || right->isArray())
4839 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004840 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02004841 {
4842 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4843 return false;
4844 }
4845
4846 if (left->isArray() != right->isArray())
4847 {
4848 error(loc, "array / non-array mismatch", GetOperatorString(op));
4849 return false;
4850 }
4851
4852 switch (op)
4853 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004854 case EOpEqual:
4855 case EOpNotEqual:
4856 case EOpAssign:
4857 case EOpInitialize:
4858 break;
4859 default:
4860 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4861 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02004862 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03004863 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004864 if (left->getType().getArraySizes() != right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02004865 {
4866 error(loc, "array size mismatch", GetOperatorString(op));
4867 return false;
4868 }
Olli Etuahod6b14282015-03-17 14:31:35 +02004869 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004870
4871 // Check ops which require integer / ivec parameters
4872 bool isBitShift = false;
4873 switch (op)
4874 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004875 case EOpBitShiftLeft:
4876 case EOpBitShiftRight:
4877 case EOpBitShiftLeftAssign:
4878 case EOpBitShiftRightAssign:
4879 // Unsigned can be bit-shifted by signed and vice versa, but we need to
4880 // check that the basic type is an integer type.
4881 isBitShift = true;
4882 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
4883 {
4884 return false;
4885 }
4886 break;
4887 case EOpBitwiseAnd:
4888 case EOpBitwiseXor:
4889 case EOpBitwiseOr:
4890 case EOpBitwiseAndAssign:
4891 case EOpBitwiseXorAssign:
4892 case EOpBitwiseOrAssign:
4893 // It is enough to check the type of only one operand, since later it
4894 // is checked that the operand types match.
4895 if (!IsInteger(left->getBasicType()))
4896 {
4897 return false;
4898 }
4899 break;
4900 default:
4901 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004902 }
4903
4904 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
4905 // So the basic type should usually match.
4906 if (!isBitShift && left->getBasicType() != right->getBasicType())
4907 {
4908 return false;
4909 }
4910
Olli Etuaho63e1ec52016-08-18 22:05:12 +03004911 // Check that:
4912 // 1. Type sizes match exactly on ops that require that.
4913 // 2. Restrictions for structs that contain arrays or samplers are respected.
4914 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04004915 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004916 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004917 case EOpAssign:
4918 case EOpInitialize:
4919 case EOpEqual:
4920 case EOpNotEqual:
4921 // ESSL 1.00 sections 5.7, 5.8, 5.9
4922 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
4923 {
4924 error(loc, "undefined operation for structs containing arrays",
4925 GetOperatorString(op));
4926 return false;
4927 }
4928 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
4929 // we interpret the spec so that this extends to structs containing samplers,
4930 // similarly to ESSL 1.00 spec.
4931 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
4932 left->getType().isStructureContainingSamplers())
4933 {
4934 error(loc, "undefined operation for structs containing samplers",
4935 GetOperatorString(op));
4936 return false;
4937 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004938
Olli Etuahoe1805592017-01-02 16:41:20 +00004939 if ((left->getNominalSize() != right->getNominalSize()) ||
4940 (left->getSecondarySize() != right->getSecondarySize()))
4941 {
4942 error(loc, "dimension mismatch", GetOperatorString(op));
4943 return false;
4944 }
4945 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004946 case EOpLessThan:
4947 case EOpGreaterThan:
4948 case EOpLessThanEqual:
4949 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00004950 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04004951 {
Olli Etuahoe1805592017-01-02 16:41:20 +00004952 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004953 return false;
4954 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03004955 break;
4956 case EOpAdd:
4957 case EOpSub:
4958 case EOpDiv:
4959 case EOpIMod:
4960 case EOpBitShiftLeft:
4961 case EOpBitShiftRight:
4962 case EOpBitwiseAnd:
4963 case EOpBitwiseXor:
4964 case EOpBitwiseOr:
4965 case EOpAddAssign:
4966 case EOpSubAssign:
4967 case EOpDivAssign:
4968 case EOpIModAssign:
4969 case EOpBitShiftLeftAssign:
4970 case EOpBitShiftRightAssign:
4971 case EOpBitwiseAndAssign:
4972 case EOpBitwiseXorAssign:
4973 case EOpBitwiseOrAssign:
4974 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
4975 {
4976 return false;
4977 }
4978
4979 // Are the sizes compatible?
4980 if (left->getNominalSize() != right->getNominalSize() ||
4981 left->getSecondarySize() != right->getSecondarySize())
4982 {
4983 // If the nominal sizes of operands do not match:
4984 // One of them must be a scalar.
4985 if (!left->isScalar() && !right->isScalar())
4986 return false;
4987
4988 // In the case of compound assignment other than multiply-assign,
4989 // the right side needs to be a scalar. Otherwise a vector/matrix
4990 // would be assigned to a scalar. A scalar can't be shifted by a
4991 // vector either.
4992 if (!right->isScalar() &&
4993 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
4994 return false;
4995 }
4996 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004997 default:
4998 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004999 }
5000
Olli Etuahod6b14282015-03-17 14:31:35 +02005001 return true;
5002}
5003
Olli Etuaho1dded802016-08-18 18:13:13 +03005004bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5005 const TType &left,
5006 const TType &right)
5007{
5008 switch (op)
5009 {
5010 case EOpMul:
5011 case EOpMulAssign:
5012 return left.getNominalSize() == right.getNominalSize() &&
5013 left.getSecondarySize() == right.getSecondarySize();
5014 case EOpVectorTimesScalar:
5015 return true;
5016 case EOpVectorTimesScalarAssign:
5017 ASSERT(!left.isMatrix() && !right.isMatrix());
5018 return left.isVector() && !right.isVector();
5019 case EOpVectorTimesMatrix:
5020 return left.getNominalSize() == right.getRows();
5021 case EOpVectorTimesMatrixAssign:
5022 ASSERT(!left.isMatrix() && right.isMatrix());
5023 return left.isVector() && left.getNominalSize() == right.getRows() &&
5024 left.getNominalSize() == right.getCols();
5025 case EOpMatrixTimesVector:
5026 return left.getCols() == right.getNominalSize();
5027 case EOpMatrixTimesScalar:
5028 return true;
5029 case EOpMatrixTimesScalarAssign:
5030 ASSERT(left.isMatrix() && !right.isMatrix());
5031 return !right.isVector();
5032 case EOpMatrixTimesMatrix:
5033 return left.getCols() == right.getRows();
5034 case EOpMatrixTimesMatrixAssign:
5035 ASSERT(left.isMatrix() && right.isMatrix());
5036 // We need to check two things:
5037 // 1. The matrix multiplication step is valid.
5038 // 2. The result will have the same number of columns as the lvalue.
5039 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5040
5041 default:
5042 UNREACHABLE();
5043 return false;
5044 }
5045}
5046
Jamie Madillb98c3a82015-07-23 14:26:04 -04005047TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5048 TIntermTyped *left,
5049 TIntermTyped *right,
5050 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005051{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005052 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005053 return nullptr;
5054
Olli Etuahofc1806e2015-03-17 13:03:11 +02005055 switch (op)
5056 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005057 case EOpEqual:
5058 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005059 case EOpLessThan:
5060 case EOpGreaterThan:
5061 case EOpLessThanEqual:
5062 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005063 break;
5064 case EOpLogicalOr:
5065 case EOpLogicalXor:
5066 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005067 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5068 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005069 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005070 {
5071 return nullptr;
5072 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005073 // Basic types matching should have been already checked.
5074 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005075 break;
5076 case EOpAdd:
5077 case EOpSub:
5078 case EOpDiv:
5079 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005080 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5081 !right->getType().getStruct());
5082 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005083 {
5084 return nullptr;
5085 }
5086 break;
5087 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005088 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5089 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005090 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005091 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005092 {
5093 return nullptr;
5094 }
5095 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005096 default:
5097 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005098 }
5099
Olli Etuaho1dded802016-08-18 18:13:13 +03005100 if (op == EOpMul)
5101 {
5102 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5103 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5104 {
5105 return nullptr;
5106 }
5107 }
5108
Olli Etuaho3fdec912016-08-18 15:08:06 +03005109 TIntermBinary *node = new TIntermBinary(op, left, right);
5110 node->setLine(loc);
5111
Olli Etuaho3fdec912016-08-18 15:08:06 +03005112 // See if we can fold constants.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005113 return node->fold(mDiagnostics);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005114}
5115
Jamie Madillb98c3a82015-07-23 14:26:04 -04005116TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5117 TIntermTyped *left,
5118 TIntermTyped *right,
5119 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005120{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005121 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005122 if (node == 0)
5123 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005124 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5125 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005126 return left;
5127 }
5128 return node;
5129}
5130
Jamie Madillb98c3a82015-07-23 14:26:04 -04005131TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5132 TIntermTyped *left,
5133 TIntermTyped *right,
5134 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005135{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005136 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005137 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005138 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005139 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5140 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005141 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005142 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005143 }
5144 return node;
5145}
5146
Olli Etuaho13389b62016-10-16 11:48:18 +01005147TIntermBinary *TParseContext::createAssign(TOperator op,
5148 TIntermTyped *left,
5149 TIntermTyped *right,
5150 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005151{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005152 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005153 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005154 if (op == EOpMulAssign)
5155 {
5156 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5157 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5158 {
5159 return nullptr;
5160 }
5161 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005162 TIntermBinary *node = new TIntermBinary(op, left, right);
5163 node->setLine(loc);
5164
Olli Etuaho3fdec912016-08-18 15:08:06 +03005165 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005166 }
5167 return nullptr;
5168}
5169
Jamie Madillb98c3a82015-07-23 14:26:04 -04005170TIntermTyped *TParseContext::addAssign(TOperator op,
5171 TIntermTyped *left,
5172 TIntermTyped *right,
5173 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005174{
Olli Etuahocce89652017-06-19 16:04:09 +03005175 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005176 TIntermTyped *node = createAssign(op, left, right, loc);
5177 if (node == nullptr)
5178 {
5179 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005180 return left;
5181 }
5182 return node;
5183}
5184
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005185TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5186 TIntermTyped *right,
5187 const TSourceLoc &loc)
5188{
Corentin Wallez0d959252016-07-12 17:26:32 -04005189 // WebGL2 section 5.26, the following results in an error:
5190 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005191 if (mShaderSpec == SH_WEBGL2_SPEC &&
5192 (left->isArray() || left->getBasicType() == EbtVoid ||
5193 left->getType().isStructureContainingArrays() || right->isArray() ||
5194 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005195 {
5196 error(loc,
5197 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5198 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005199 }
5200
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005201 TIntermBinary *commaNode = new TIntermBinary(EOpComma, left, right);
5202 TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(mShaderVersion, left, right);
5203 commaNode->getTypePointer()->setQualifier(resultQualifier);
5204 return commaNode->fold(mDiagnostics);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005205}
5206
Olli Etuaho49300862015-02-20 14:54:49 +02005207TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5208{
5209 switch (op)
5210 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005211 case EOpContinue:
5212 if (mLoopNestingLevel <= 0)
5213 {
5214 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005215 }
5216 break;
5217 case EOpBreak:
5218 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5219 {
5220 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005221 }
5222 break;
5223 case EOpReturn:
5224 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5225 {
5226 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005227 }
5228 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005229 case EOpKill:
5230 if (mShaderType != GL_FRAGMENT_SHADER)
5231 {
5232 error(loc, "discard supported in fragment shaders only", "discard");
5233 }
5234 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005235 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005236 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005237 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005238 }
Olli Etuahocce89652017-06-19 16:04:09 +03005239 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005240}
5241
Jamie Madillb98c3a82015-07-23 14:26:04 -04005242TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005243 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005244 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005245{
Olli Etuahocce89652017-06-19 16:04:09 +03005246 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005247 {
Olli Etuahocce89652017-06-19 16:04:09 +03005248 ASSERT(op == EOpReturn);
5249 mFunctionReturnsValue = true;
5250 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5251 {
5252 error(loc, "void function cannot return a value", "return");
5253 }
5254 else if (*mCurrentFunctionType != expression->getType())
5255 {
5256 error(loc, "function return is not matching type:", "return");
5257 }
Olli Etuaho49300862015-02-20 14:54:49 +02005258 }
Olli Etuahocce89652017-06-19 16:04:09 +03005259 TIntermBranch *node = new TIntermBranch(op, expression);
5260 node->setLine(loc);
5261 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005262}
5263
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005264void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5265{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005266 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01005267 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005268 TIntermNode *offset = nullptr;
5269 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuahoec9232b2017-03-27 17:01:37 +03005270 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
5271 name == "textureProjLodOffset" || name == "textureGradOffset" ||
5272 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005273 {
5274 offset = arguments->back();
5275 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03005276 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005277 {
5278 // A bias parameter might follow the offset parameter.
5279 ASSERT(arguments->size() >= 3);
5280 offset = (*arguments)[2];
5281 }
5282 if (offset != nullptr)
5283 {
5284 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5285 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5286 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005287 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03005288 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005289 }
5290 else
5291 {
5292 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5293 size_t size = offsetConstantUnion->getType().getObjectSize();
5294 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
5295 for (size_t i = 0u; i < size; ++i)
5296 {
5297 int offsetValue = values[i].getIConst();
5298 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
5299 {
5300 std::stringstream tokenStream;
5301 tokenStream << offsetValue;
5302 std::string token = tokenStream.str();
5303 error(offset->getLine(), "Texture offset value out of valid range",
5304 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005305 }
5306 }
5307 }
5308 }
5309}
5310
Martin Radev2cc85b32016-08-05 16:22:53 +03005311// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5312void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5313{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005314 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03005315 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5316
5317 if (name.compare(0, 5, "image") == 0)
5318 {
5319 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005320 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005321
Olli Etuaho485eefd2017-02-14 17:40:06 +00005322 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005323
5324 if (name.compare(5, 5, "Store") == 0)
5325 {
5326 if (memoryQualifier.readonly)
5327 {
5328 error(imageNode->getLine(),
5329 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005330 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005331 }
5332 }
5333 else if (name.compare(5, 4, "Load") == 0)
5334 {
5335 if (memoryQualifier.writeonly)
5336 {
5337 error(imageNode->getLine(),
5338 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005339 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005340 }
5341 }
5342 }
5343}
5344
5345// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5346void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5347 const TFunction *functionDefinition,
5348 const TIntermAggregate *functionCall)
5349{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005350 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005351
5352 const TIntermSequence &arguments = *functionCall->getSequence();
5353
5354 ASSERT(functionDefinition->getParamCount() == arguments.size());
5355
5356 for (size_t i = 0; i < arguments.size(); ++i)
5357 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005358 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5359 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005360 const TType &functionParameterType = *functionDefinition->getParam(i).type;
5361 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5362
5363 if (IsImage(functionArgumentType.getBasicType()))
5364 {
5365 const TMemoryQualifier &functionArgumentMemoryQualifier =
5366 functionArgumentType.getMemoryQualifier();
5367 const TMemoryQualifier &functionParameterMemoryQualifier =
5368 functionParameterType.getMemoryQualifier();
5369 if (functionArgumentMemoryQualifier.readonly &&
5370 !functionParameterMemoryQualifier.readonly)
5371 {
5372 error(functionCall->getLine(),
5373 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005374 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005375 }
5376
5377 if (functionArgumentMemoryQualifier.writeonly &&
5378 !functionParameterMemoryQualifier.writeonly)
5379 {
5380 error(functionCall->getLine(),
5381 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005382 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005383 }
Martin Radev049edfa2016-11-11 14:35:37 +02005384
5385 if (functionArgumentMemoryQualifier.coherent &&
5386 !functionParameterMemoryQualifier.coherent)
5387 {
5388 error(functionCall->getLine(),
5389 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005390 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005391 }
5392
5393 if (functionArgumentMemoryQualifier.volatileQualifier &&
5394 !functionParameterMemoryQualifier.volatileQualifier)
5395 {
5396 error(functionCall->getLine(),
5397 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005398 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005399 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005400 }
5401 }
5402}
5403
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005404TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005405{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005406 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00005407}
5408
5409TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005410 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00005411 TIntermNode *thisNode,
5412 const TSourceLoc &loc)
5413{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005414 if (thisNode != nullptr)
5415 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005416 return addMethod(fnCall, arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005417 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005418
5419 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005420 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005421 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005422 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005423 }
5424 else
5425 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005426 ASSERT(op == EOpNull);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005427 return addNonConstructorFunctionCall(fnCall, arguments, loc);
5428 }
5429}
5430
5431TIntermTyped *TParseContext::addMethod(TFunction *fnCall,
5432 TIntermSequence *arguments,
5433 TIntermNode *thisNode,
5434 const TSourceLoc &loc)
5435{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005436 TIntermTyped *typedThis = thisNode->getAsTyped();
5437 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5438 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5439 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
5440 // So accessing fnCall->getName() below is safe.
5441 if (fnCall->getName() != "length")
5442 {
5443 error(loc, "invalid method", fnCall->getName().c_str());
5444 }
5445 else if (!arguments->empty())
5446 {
5447 error(loc, "method takes no parameters", "length");
5448 }
5449 else if (typedThis == nullptr || !typedThis->isArray())
5450 {
5451 error(loc, "length can only be called on arrays", "length");
5452 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08005453 else if (typedThis->getQualifier() == EvqPerVertexIn &&
5454 mGeometryShaderInputPrimitiveType == EptUndefined)
5455 {
5456 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5457 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005458 else if (typedThis->hasSideEffects())
5459 {
5460 error(loc, "length method not supported on expressions with possible side effects",
5461 "length");
5462 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005463 else
5464 {
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005465 TIntermUnary *node = new TIntermUnary(EOpArrayLength, typedThis);
5466 node->setLine(loc);
5467 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005468 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005469 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005470}
5471
5472TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunction *fnCall,
5473 TIntermSequence *arguments,
5474 const TSourceLoc &loc)
5475{
5476 // First find by unmangled name to check whether the function name has been
5477 // hidden by a variable name or struct typename.
5478 // If a function is found, check for one with a matching argument list.
5479 bool builtIn;
5480 const TSymbol *symbol = symbolTable.find(fnCall->getName(), mShaderVersion, &builtIn);
5481 if (symbol != nullptr && !symbol->isFunction())
5482 {
5483 error(loc, "function name expected", fnCall->getName().c_str());
5484 }
5485 else
5486 {
5487 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(fnCall->getName(), *arguments),
5488 mShaderVersion, &builtIn);
5489 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005490 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005491 error(loc, "no matching overloaded function found", fnCall->getName().c_str());
5492 }
5493 else
5494 {
5495 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005496 //
5497 // A declared function.
5498 //
Olli Etuaho383b7912016-08-05 11:22:59 +03005499 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005500 {
Olli Etuaho856c4972016-08-08 11:38:39 +03005501 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005502 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005503 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005504 if (builtIn && op != EOpNull)
5505 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005506 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005507 if (fnCandidate->getParamCount() == 1)
5508 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005509 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005510 TIntermNode *unaryParamNode = arguments->front();
5511 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005512 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005513 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005514 }
5515 else
5516 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005517 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00005518 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005519 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005520
5521 // Some built-in functions have out parameters too.
Jiajia Qinbc585152017-06-23 15:42:17 +08005522 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05305523
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005524 if (TIntermAggregate::CanFoldAggregateBuiltInOp(callNode->getOp()))
Arun Patole274f0702015-05-05 13:33:30 +05305525 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005526 // See if we can constant fold a built-in. Note that this may be possible
5527 // even if it is not const-qualified.
5528 return callNode->fold(mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05305529 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005530 else
5531 {
5532 return callNode;
5533 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005534 }
5535 }
5536 else
5537 {
5538 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005539 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005540
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005541 // If builtIn == false, the function is user defined - could be an overloaded
5542 // built-in as well.
5543 // if builtIn == true, it's a builtIn function with no op associated with it.
5544 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005545 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005546 {
Olli Etuahofe486322017-03-21 09:30:54 +00005547 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005548 checkTextureOffsetConst(callNode);
5549 checkImageMemoryAccessForBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03005550 }
5551 else
5552 {
Olli Etuahofe486322017-03-21 09:30:54 +00005553 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005554 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005555 }
5556
Jiajia Qinbc585152017-06-23 15:42:17 +08005557 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005558
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005559 callNode->setLine(loc);
5560
5561 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005562 }
5563 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005564 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005565
5566 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005567 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005568}
5569
Jamie Madillb98c3a82015-07-23 14:26:04 -04005570TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005571 TIntermTyped *trueExpression,
5572 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005573 const TSourceLoc &loc)
5574{
Olli Etuaho56229f12017-07-10 14:16:33 +03005575 if (!checkIsScalarBool(loc, cond))
5576 {
5577 return falseExpression;
5578 }
Olli Etuaho52901742015-04-15 13:42:45 +03005579
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005580 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005581 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005582 std::stringstream reasonStream;
5583 reasonStream << "mismatching ternary operator operand types '"
5584 << trueExpression->getCompleteString() << " and '"
5585 << falseExpression->getCompleteString() << "'";
5586 std::string reason = reasonStream.str();
5587 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005588 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005589 }
Olli Etuahode318b22016-10-25 16:18:25 +01005590 if (IsOpaqueType(trueExpression->getBasicType()))
5591 {
5592 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005593 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005594 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5595 // Note that structs containing opaque types don't need to be checked as structs are
5596 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005597 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005598 return falseExpression;
5599 }
5600
Jiajia Qinbc585152017-06-23 15:42:17 +08005601 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5602 falseExpression->getMemoryQualifier().writeonly)
5603 {
5604 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5605 return falseExpression;
5606 }
5607
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005608 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005609 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005610 // ESSL 3.00.6 section 5.7:
5611 // Ternary operator support is optional for arrays. No certainty that it works across all
5612 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5613 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005614 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005615 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005616 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005617 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005618 }
Olli Etuaho94050052017-05-08 14:17:44 +03005619 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5620 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005621 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005622 return falseExpression;
5623 }
5624
Corentin Wallez0d959252016-07-12 17:26:32 -04005625 // WebGL2 section 5.26, the following results in an error:
5626 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005627 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005628 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005629 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005630 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005631 }
5632
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005633 // Note that the node resulting from here can be a constant union without being qualified as
5634 // constant.
5635 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5636 node->setLine(loc);
5637
5638 return node->fold();
Olli Etuaho52901742015-04-15 13:42:45 +03005639}
Olli Etuaho49300862015-02-20 14:54:49 +02005640
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005641//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005642// Parse an array of strings using yyparse.
5643//
5644// Returns 0 for success.
5645//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005646int PaParseStrings(size_t count,
5647 const char *const string[],
5648 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305649 TParseContext *context)
5650{
Yunchao He4f285442017-04-21 12:15:49 +08005651 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005652 return 1;
5653
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005654 if (glslang_initialize(context))
5655 return 1;
5656
alokp@chromium.org408c45e2012-04-05 15:54:43 +00005657 int error = glslang_scan(count, string, length, context);
5658 if (!error)
5659 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005660
alokp@chromium.org73bc2982012-06-19 18:48:05 +00005661 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00005662
alokp@chromium.org6b495712012-06-29 00:06:58 +00005663 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005664}
Jamie Madill45bcc782016-11-07 13:58:48 -05005665
5666} // namespace sh