blob: 4216a71465abe938fc501b3a9573f8dcc9216e6b [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 Etuaho09b04a22016-12-15 13:30:26 +00001161 // TODO(oetuaho@nvidia.com): This is slightly hacky. Might be better if symbols could be
1162 // associated with more than one extension.
1163 if (extension == "GL_OVR_multiview")
1164 {
1165 return checkCanUseExtension(line, "GL_OVR_multiview2");
1166 }
Olli Etuaho4de340a2016-12-16 09:32:03 +00001167 error(line, "extension is disabled", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001168 return false;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001169 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301170 if (iter->second == EBhWarn)
1171 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001172 warning(line, "extension is being used", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001173 return true;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001174 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001175
Olli Etuaho8a176262016-08-16 14:23:01 +03001176 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001177}
1178
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001179// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1180// compile-time or link-time errors are the same whether or not the declaration is empty".
1181// This function implements all the checks that are done on qualifiers regardless of if the
1182// declaration is empty.
1183void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1184 const sh::TLayoutQualifier &layoutQualifier,
1185 const TSourceLoc &location)
1186{
1187 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1188 {
1189 error(location, "Shared memory declarations cannot have layout specified", "layout");
1190 }
1191
1192 if (layoutQualifier.matrixPacking != EmpUnspecified)
1193 {
1194 error(location, "layout qualifier only valid for interface blocks",
1195 getMatrixPackingString(layoutQualifier.matrixPacking));
1196 return;
1197 }
1198
1199 if (layoutQualifier.blockStorage != EbsUnspecified)
1200 {
1201 error(location, "layout qualifier only valid for interface blocks",
1202 getBlockStorageString(layoutQualifier.blockStorage));
1203 return;
1204 }
1205
1206 if (qualifier == EvqFragmentOut)
1207 {
1208 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1209 {
1210 error(location, "invalid layout qualifier combination", "yuv");
1211 return;
1212 }
1213 }
1214 else
1215 {
1216 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1217 }
1218
Olli Etuaho95468d12017-05-04 11:14:34 +03001219 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1220 // parsing steps. So it needs to be checked here.
1221 if (isMultiviewExtensionEnabled() && mShaderVersion < 300 && qualifier == EvqVertexIn)
1222 {
1223 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1224 }
1225
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001226 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
1227 if (mShaderVersion >= 310 && qualifier == EvqUniform)
1228 {
1229 canHaveLocation = true;
1230 // We're not checking whether the uniform location is in range here since that depends on
1231 // the type of the variable.
1232 // The type can only be fully determined for non-empty declarations.
1233 }
1234 if (!canHaveLocation)
1235 {
1236 checkLocationIsNotSpecified(location, layoutQualifier);
1237 }
1238}
1239
jchen104cdac9e2017-05-08 11:01:20 +08001240void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1241 const TSourceLoc &location)
1242{
1243 if (publicType.precision != EbpHigh)
1244 {
1245 error(location, "Can only be highp", "atomic counter");
1246 }
1247 // dEQP enforces compile error if location is specified. See uniform_location.test.
1248 if (publicType.layoutQualifier.location != -1)
1249 {
1250 error(location, "location must not be set for atomic_uint", "layout");
1251 }
1252 if (publicType.layoutQualifier.binding == -1)
1253 {
1254 error(location, "no binding specified", "atomic counter");
1255 }
1256}
1257
Martin Radevb8b01222016-11-20 23:25:53 +02001258void TParseContext::emptyDeclarationErrorCheck(const TPublicType &publicType,
1259 const TSourceLoc &location)
1260{
1261 if (publicType.isUnsizedArray())
1262 {
1263 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1264 // error. It is assumed that this applies to empty declarations as well.
1265 error(location, "empty array declaration needs to specify a size", "");
1266 }
Martin Radevb8b01222016-11-20 23:25:53 +02001267}
1268
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001269// These checks are done for all declarations that are non-empty. They're done for non-empty
1270// declarations starting a declarator list, and declarators that follow an empty declaration.
1271void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1272 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001273{
Olli Etuahofa33d582015-04-09 14:33:12 +03001274 switch (publicType.qualifier)
1275 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001276 case EvqVaryingIn:
1277 case EvqVaryingOut:
1278 case EvqAttribute:
1279 case EvqVertexIn:
1280 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001281 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001282 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001283 {
1284 error(identifierLocation, "cannot be used with a structure",
1285 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001286 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001287 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001288 break;
1289 case EvqBuffer:
1290 if (publicType.getBasicType() != EbtInterfaceBlock)
1291 {
1292 error(identifierLocation,
1293 "cannot declare buffer variables at global scope(outside a block)",
1294 getQualifierString(publicType.qualifier));
1295 return;
1296 }
1297 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001298 default:
1299 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001300 }
jchen10cc2a10e2017-05-03 14:05:12 +08001301 std::string reason(getBasicString(publicType.getBasicType()));
1302 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001303 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001304 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001305 {
1306 return;
1307 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001308
Andrei Volykhina5527072017-03-22 16:46:30 +03001309 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1310 publicType.qualifier != EvqConst) &&
1311 publicType.getBasicType() == EbtYuvCscStandardEXT)
1312 {
1313 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1314 getQualifierString(publicType.qualifier));
1315 return;
1316 }
1317
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001318 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1319 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001320 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1321 // But invalid shaders may still reach here with an unsized array declaration.
1322 if (!publicType.isUnsizedArray())
1323 {
1324 TType type(publicType);
1325 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1326 publicType.layoutQualifier);
1327 }
1328 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001329
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001330 // check for layout qualifier issues
1331 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001332
Martin Radev2cc85b32016-08-05 16:22:53 +03001333 if (IsImage(publicType.getBasicType()))
1334 {
1335
1336 switch (layoutQualifier.imageInternalFormat)
1337 {
1338 case EiifRGBA32F:
1339 case EiifRGBA16F:
1340 case EiifR32F:
1341 case EiifRGBA8:
1342 case EiifRGBA8_SNORM:
1343 if (!IsFloatImage(publicType.getBasicType()))
1344 {
1345 error(identifierLocation,
1346 "internal image format requires a floating image type",
1347 getBasicString(publicType.getBasicType()));
1348 return;
1349 }
1350 break;
1351 case EiifRGBA32I:
1352 case EiifRGBA16I:
1353 case EiifRGBA8I:
1354 case EiifR32I:
1355 if (!IsIntegerImage(publicType.getBasicType()))
1356 {
1357 error(identifierLocation,
1358 "internal image format requires an integer image type",
1359 getBasicString(publicType.getBasicType()));
1360 return;
1361 }
1362 break;
1363 case EiifRGBA32UI:
1364 case EiifRGBA16UI:
1365 case EiifRGBA8UI:
1366 case EiifR32UI:
1367 if (!IsUnsignedImage(publicType.getBasicType()))
1368 {
1369 error(identifierLocation,
1370 "internal image format requires an unsigned image type",
1371 getBasicString(publicType.getBasicType()));
1372 return;
1373 }
1374 break;
1375 case EiifUnspecified:
1376 error(identifierLocation, "layout qualifier", "No image internal format specified");
1377 return;
1378 default:
1379 error(identifierLocation, "layout qualifier", "unrecognized token");
1380 return;
1381 }
1382
1383 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1384 switch (layoutQualifier.imageInternalFormat)
1385 {
1386 case EiifR32F:
1387 case EiifR32I:
1388 case EiifR32UI:
1389 break;
1390 default:
1391 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1392 {
1393 error(identifierLocation, "layout qualifier",
1394 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1395 "image variables must be qualified readonly and/or writeonly");
1396 return;
1397 }
1398 break;
1399 }
1400 }
1401 else
1402 {
Olli Etuaho43364892017-02-13 16:00:12 +00001403 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001404 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1405 }
jchen104cdac9e2017-05-08 11:01:20 +08001406
1407 if (IsAtomicCounter(publicType.getBasicType()))
1408 {
1409 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1410 }
1411 else
1412 {
1413 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1414 }
Olli Etuaho43364892017-02-13 16:00:12 +00001415}
Martin Radev2cc85b32016-08-05 16:22:53 +03001416
Olli Etuaho43364892017-02-13 16:00:12 +00001417void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1418{
1419 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001420 // Note that the ESSL 3.10 section 4.4.5 is not particularly clear on how the binding qualifier
1421 // on arrays of arrays should be handled. We interpret the spec so that the binding value is
1422 // incremented for each element of the innermost nested arrays. This is in line with how arrays
1423 // of arrays of blocks are specified to behave in GLSL 4.50 and a conservative interpretation
1424 // when it comes to which shaders are accepted by the compiler.
1425 int arrayTotalElementCount = type.getArraySizeProduct();
Olli Etuaho43364892017-02-13 16:00:12 +00001426 if (IsImage(type.getBasicType()))
1427 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001428 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding,
1429 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001430 }
1431 else if (IsSampler(type.getBasicType()))
1432 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001433 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding,
1434 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001435 }
jchen104cdac9e2017-05-08 11:01:20 +08001436 else if (IsAtomicCounter(type.getBasicType()))
1437 {
1438 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1439 }
Olli Etuaho43364892017-02-13 16:00:12 +00001440 else
1441 {
1442 ASSERT(!IsOpaqueType(type.getBasicType()));
1443 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001444 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001445}
1446
Olli Etuaho856c4972016-08-08 11:38:39 +03001447void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1448 const TString &layoutQualifierName,
1449 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001450{
1451
1452 if (mShaderVersion < versionRequired)
1453 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001454 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001455 }
1456}
1457
Olli Etuaho856c4972016-08-08 11:38:39 +03001458bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1459 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001460{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001461 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001462 for (size_t i = 0u; i < localSize.size(); ++i)
1463 {
1464 if (localSize[i] != -1)
1465 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001466 error(location,
1467 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1468 "global layout declaration",
1469 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001470 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001471 }
1472 }
1473
Olli Etuaho8a176262016-08-16 14:23:01 +03001474 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001475}
1476
Olli Etuaho43364892017-02-13 16:00:12 +00001477void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001478 TLayoutImageInternalFormat internalFormat)
1479{
1480 if (internalFormat != EiifUnspecified)
1481 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001482 error(location, "invalid layout qualifier: only valid when used with images",
1483 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001484 }
Olli Etuaho43364892017-02-13 16:00:12 +00001485}
1486
1487void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1488{
1489 if (binding != -1)
1490 {
1491 error(location,
1492 "invalid layout qualifier: only valid when used with opaque types or blocks",
1493 "binding");
1494 }
1495}
1496
jchen104cdac9e2017-05-08 11:01:20 +08001497void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1498{
1499 if (offset != -1)
1500 {
1501 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1502 "offset");
1503 }
1504}
1505
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001506void TParseContext::checkImageBindingIsValid(const TSourceLoc &location,
1507 int binding,
1508 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001509{
1510 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001511 if (binding >= 0 && binding + arrayTotalElementCount > mMaxImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001512 {
1513 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1514 }
1515}
1516
1517void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1518 int binding,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001519 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001520{
1521 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001522 if (binding >= 0 && binding + arrayTotalElementCount > mMaxCombinedTextureImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001523 {
1524 error(location, "sampler binding greater than maximum texture units", "binding");
1525 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001526}
1527
Jiajia Qinbc585152017-06-23 15:42:17 +08001528void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location,
1529 const TQualifier &qualifier,
1530 int binding,
1531 int arraySize)
jchen10af713a22017-04-19 09:10:56 +08001532{
1533 int size = (arraySize == 0 ? 1 : arraySize);
Jiajia Qinbc585152017-06-23 15:42:17 +08001534 if (qualifier == EvqUniform)
jchen10af713a22017-04-19 09:10:56 +08001535 {
Jiajia Qinbc585152017-06-23 15:42:17 +08001536 if (binding + size > mMaxUniformBufferBindings)
1537 {
1538 error(location, "uniform block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1539 "binding");
1540 }
1541 }
1542 else if (qualifier == EvqBuffer)
1543 {
1544 if (binding + size > mMaxShaderStorageBufferBindings)
1545 {
1546 error(location,
1547 "shader storage block binding greater than MAX_SHADER_STORAGE_BUFFER_BINDINGS",
1548 "binding");
1549 }
jchen10af713a22017-04-19 09:10:56 +08001550 }
1551}
jchen104cdac9e2017-05-08 11:01:20 +08001552void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1553{
1554 if (binding >= mMaxAtomicCounterBindings)
1555 {
1556 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1557 "binding");
1558 }
1559}
jchen10af713a22017-04-19 09:10:56 +08001560
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001561void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1562 int objectLocationCount,
1563 const TLayoutQualifier &layoutQualifier)
1564{
1565 int loc = layoutQualifier.location;
1566 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1567 {
1568 error(location, "Uniform location out of range", "location");
1569 }
1570}
1571
Andrei Volykhina5527072017-03-22 16:46:30 +03001572void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1573{
1574 if (yuv != false)
1575 {
1576 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1577 }
1578}
1579
Jiajia Qinbc585152017-06-23 15:42:17 +08001580void TParseContext::functionCallRValueLValueErrorCheck(const TFunction *fnCandidate,
1581 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001582{
1583 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1584 {
1585 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
Jiajia Qinbc585152017-06-23 15:42:17 +08001586 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
1587 if (!IsImage(argument->getBasicType()) && (IsQualifierUnspecified(qual) || qual == EvqIn ||
1588 qual == EvqInOut || qual == EvqConstReadOnly))
1589 {
1590 if (argument->getMemoryQualifier().writeonly)
1591 {
1592 error(argument->getLine(),
1593 "Writeonly value cannot be passed for 'in' or 'inout' parameters.",
1594 fnCall->getFunctionSymbolInfo()->getName().c_str());
1595 return;
1596 }
1597 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001598 if (qual == EvqOut || qual == EvqInOut)
1599 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001600 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001601 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001602 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001603 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuahoec9232b2017-03-27 17:01:37 +03001604 fnCall->getFunctionSymbolInfo()->getName().c_str());
Olli Etuaho383b7912016-08-05 11:22:59 +03001605 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001606 }
1607 }
1608 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001609}
1610
Martin Radev70866b82016-07-22 15:27:42 +03001611void TParseContext::checkInvariantVariableQualifier(bool invariant,
1612 const TQualifier qualifier,
1613 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001614{
Martin Radev70866b82016-07-22 15:27:42 +03001615 if (!invariant)
1616 return;
1617
1618 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001619 {
Martin Radev70866b82016-07-22 15:27:42 +03001620 // input variables in the fragment shader can be also qualified as invariant
1621 if (!sh::CanBeInvariantESSL1(qualifier))
1622 {
1623 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1624 }
1625 }
1626 else
1627 {
1628 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1629 {
1630 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1631 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001632 }
1633}
1634
Arun Patole7e7e68d2015-05-22 12:02:25 +05301635bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001636{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001637 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001638 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1639 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001640}
1641
Arun Patole7e7e68d2015-05-22 12:02:25 +05301642bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001643{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001644 return ::IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001645}
1646
Jamie Madillb98c3a82015-07-23 14:26:04 -04001647void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1648 const char *extName,
1649 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001650{
1651 pp::SourceLocation srcLoc;
1652 srcLoc.file = loc.first_file;
1653 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001654 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001655}
1656
Jamie Madillb98c3a82015-07-23 14:26:04 -04001657void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1658 const char *name,
1659 const char *value,
1660 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001661{
1662 pp::SourceLocation srcLoc;
1663 srcLoc.file = loc.first_file;
1664 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001665 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001666}
1667
Martin Radev4c4c8e72016-08-04 12:25:34 +03001668sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001669{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001670 sh::WorkGroupSize result;
Martin Radev802abe02016-08-04 17:48:32 +03001671 for (size_t i = 0u; i < result.size(); ++i)
1672 {
1673 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1674 {
1675 result[i] = 1;
1676 }
1677 else
1678 {
1679 result[i] = mComputeShaderLocalSize[i];
1680 }
1681 }
1682 return result;
1683}
1684
Olli Etuaho56229f12017-07-10 14:16:33 +03001685TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1686 const TSourceLoc &line)
1687{
1688 TIntermConstantUnion *node = new TIntermConstantUnion(
1689 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1690 node->setLine(line);
1691 return node;
1692}
1693
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001694/////////////////////////////////////////////////////////////////////////////////
1695//
1696// Non-Errors.
1697//
1698/////////////////////////////////////////////////////////////////////////////////
1699
Jamie Madill5c097022014-08-20 16:38:32 -04001700const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1701 const TString *name,
1702 const TSymbol *symbol)
1703{
Jamie Madill5c097022014-08-20 16:38:32 -04001704 if (!symbol)
1705 {
1706 error(location, "undeclared identifier", 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 if (!symbol->isVariable())
Jamie Madill5c097022014-08-20 16:38:32 -04001711 {
1712 error(location, "variable expected", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001713 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001714 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001715
1716 const TVariable *variable = static_cast<const TVariable *>(symbol);
1717
1718 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
1719 !variable->getExtension().empty())
Jamie Madill5c097022014-08-20 16:38:32 -04001720 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001721 checkCanUseExtension(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001722 }
1723
Olli Etuaho0f684632017-07-13 12:42:15 +03001724 // Reject shaders using both gl_FragData and gl_FragColor
1725 TQualifier qualifier = variable->getType().getQualifier();
1726 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill5c097022014-08-20 16:38:32 -04001727 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001728 mUsesFragData = true;
1729 }
1730 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
1731 {
1732 mUsesFragColor = true;
1733 }
1734 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1735 {
1736 mUsesSecondaryOutputs = true;
Jamie Madill5c097022014-08-20 16:38:32 -04001737 }
1738
Olli Etuaho0f684632017-07-13 12:42:15 +03001739 // This validation is not quite correct - it's only an error to write to
1740 // both FragData and FragColor. For simplicity, and because users shouldn't
1741 // be rewarded for reading from undefined varaibles, return an error
1742 // if they are both referenced, rather than assigned.
1743 if (mUsesFragData && mUsesFragColor)
1744 {
1745 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1746 if (mUsesSecondaryOutputs)
1747 {
1748 errorMessage =
1749 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1750 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1751 }
1752 error(location, errorMessage, name->c_str());
1753 }
1754
1755 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1756 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1757 qualifier == EvqWorkGroupSize)
1758 {
1759 error(location,
1760 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1761 "gl_WorkGroupSize");
1762 }
Jamie Madill5c097022014-08-20 16:38:32 -04001763 return variable;
1764}
1765
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001766TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1767 const TString *name,
1768 const TSymbol *symbol)
1769{
1770 const TVariable *variable = getNamedVariable(location, name, symbol);
1771
Olli Etuaho0f684632017-07-13 12:42:15 +03001772 if (!variable)
1773 {
1774 TIntermTyped *node = CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
1775 node->setLine(location);
1776 return node;
1777 }
1778
Olli Etuaho09b04a22016-12-15 13:30:26 +00001779 if (variable->getType().getQualifier() == EvqViewIDOVR && IsWebGLBasedSpec(mShaderSpec) &&
1780 mShaderType == GL_FRAGMENT_SHADER && !isExtensionEnabled("GL_OVR_multiview2"))
1781 {
1782 // WEBGL_multiview spec
1783 error(location, "Need to enable OVR_multiview2 to use gl_ViewID_OVR in fragment shader",
1784 "gl_ViewID_OVR");
1785 }
1786
Olli Etuaho56229f12017-07-10 14:16:33 +03001787 TIntermTyped *node = nullptr;
1788
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001789 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001790 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001791 const TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho56229f12017-07-10 14:16:33 +03001792 node = new TIntermConstantUnion(constArray, variable->getType());
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001793 }
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001794 else if (variable->getType().getQualifier() == EvqWorkGroupSize &&
1795 mComputeShaderLocalSizeDeclared)
1796 {
1797 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1798 // needs to be added to the AST as a constant and not as a symbol.
1799 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1800 TConstantUnion *constArray = new TConstantUnion[3];
1801 for (size_t i = 0; i < 3; ++i)
1802 {
1803 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1804 }
1805
1806 ASSERT(variable->getType().getBasicType() == EbtUInt);
1807 ASSERT(variable->getType().getObjectSize() == 3);
1808
1809 TType type(variable->getType());
1810 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001811 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001812 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08001813 // TODO(jiawei.shao@intel.com): set array sizes for user-defined geometry shader inputs.
1814 else if (variable->getType().getQualifier() == EvqPerVertexIn)
1815 {
1816 TType type(variable->getType());
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001817 type.setArraySize(0, mGeometryShaderInputArraySize);
Jiawei Shaod8105a02017-08-08 09:54:36 +08001818 node = new TIntermSymbol(variable->getUniqueId(), variable->getName(), type);
1819 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001820 else
1821 {
Olli Etuaho56229f12017-07-10 14:16:33 +03001822 node = new TIntermSymbol(variable->getUniqueId(), variable->getName(), variable->getType());
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001823 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001824 ASSERT(node != nullptr);
1825 node->setLine(location);
1826 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001827}
1828
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001829// Initializers show up in several places in the grammar. Have one set of
1830// code to handle them here.
1831//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001832// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001833bool TParseContext::executeInitializer(const TSourceLoc &line,
1834 const TString &identifier,
1835 const TPublicType &pType,
1836 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001837 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001838{
Olli Etuaho13389b62016-10-16 11:48:18 +01001839 ASSERT(initNode != nullptr);
1840 ASSERT(*initNode == nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001841 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001842
Olli Etuaho2935c582015-04-08 14:32:06 +03001843 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001844 if (type.isUnsizedArray())
1845 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001846 // In case initializer is not an array or type has more dimensions than initializer, this
1847 // will default to setting array sizes to 1. We have not checked yet whether the initializer
1848 // actually is an array or not. Having a non-array initializer for an unsized array will
1849 // result in an error later, so we don't generate an error message here.
1850 type.sizeUnsizedArrays(initializer->getType().getArraySizes());
Olli Etuaho376f1b52015-04-13 13:23:41 +03001851 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001852 if (!declareVariable(line, identifier, type, &variable))
1853 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001854 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001855 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001856
Olli Etuahob0c645e2015-05-12 14:25:36 +03001857 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001858 if (symbolTable.atGlobalLevel() &&
1859 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001860 {
1861 // Error message does not completely match behavior with ESSL 1.00, but
1862 // we want to steer developers towards only using constant expressions.
1863 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001864 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001865 }
1866 if (globalInitWarning)
1867 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001868 warning(
1869 line,
1870 "global variable initializers should be constant expressions "
1871 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1872 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001873 }
1874
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001875 //
1876 // identifier must be of type constant, a global, or a temporary
1877 //
1878 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301879 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1880 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001881 error(line, " cannot initialize this type of qualifier ",
1882 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001883 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001884 }
1885 //
1886 // test for and propagate constant
1887 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001888
Arun Patole7e7e68d2015-05-22 12:02:25 +05301889 if (qualifier == EvqConst)
1890 {
1891 if (qualifier != initializer->getType().getQualifier())
1892 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001893 std::stringstream reasonStream;
1894 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1895 << "'";
1896 std::string reason = reasonStream.str();
1897 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001898 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001899 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001900 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301901 if (type != initializer->getType())
1902 {
1903 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001904 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001905 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001906 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001907 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001908
1909 // Save the constant folded value to the variable if possible. For example array
1910 // initializers are not folded, since that way copying the array literal to multiple places
1911 // in the shader is avoided.
1912 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1913 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301914 if (initializer->getAsConstantUnion())
1915 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001916 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001917 ASSERT(*initNode == nullptr);
1918 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301919 }
1920 else if (initializer->getAsSymbolNode())
1921 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001922 const TSymbol *symbol =
1923 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1924 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001925
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001926 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001927 if (constArray)
1928 {
1929 variable->shareConstPointer(constArray);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001930 ASSERT(*initNode == nullptr);
1931 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001932 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001933 }
1934 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001935
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001936 TIntermSymbol *intermSymbol =
1937 new TIntermSymbol(variable->getUniqueId(), variable->getName(), variable->getType());
1938 intermSymbol->setLine(line);
Olli Etuaho13389b62016-10-16 11:48:18 +01001939 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1940 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001941 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001942 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001943 return false;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001944 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001945
Olli Etuaho914b79a2017-06-19 16:03:19 +03001946 return true;
1947}
1948
1949TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
1950 const TString &identifier,
1951 TIntermTyped *initializer,
1952 const TSourceLoc &loc)
1953{
1954 checkIsScalarBool(loc, pType);
1955 TIntermBinary *initNode = nullptr;
1956 if (executeInitializer(loc, identifier, pType, initializer, &initNode))
1957 {
1958 // The initializer is valid. The init condition needs to have a node - either the
1959 // initializer node, or a constant node in case the initialized variable is const and won't
1960 // be recorded in the AST.
1961 if (initNode == nullptr)
1962 {
1963 return initializer;
1964 }
1965 else
1966 {
1967 TIntermDeclaration *declaration = new TIntermDeclaration();
1968 declaration->appendDeclarator(initNode);
1969 return declaration;
1970 }
1971 }
1972 return nullptr;
1973}
1974
1975TIntermNode *TParseContext::addLoop(TLoopType type,
1976 TIntermNode *init,
1977 TIntermNode *cond,
1978 TIntermTyped *expr,
1979 TIntermNode *body,
1980 const TSourceLoc &line)
1981{
1982 TIntermNode *node = nullptr;
1983 TIntermTyped *typedCond = nullptr;
1984 if (cond)
1985 {
1986 typedCond = cond->getAsTyped();
1987 }
1988 if (cond == nullptr || typedCond)
1989 {
Olli Etuahocce89652017-06-19 16:04:09 +03001990 if (type == ELoopDoWhile)
1991 {
1992 checkIsScalarBool(line, typedCond);
1993 }
1994 // In the case of other loops, it was checked before that the condition is a scalar boolean.
1995 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
1996 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
1997 !typedCond->isVector()));
1998
Olli Etuaho3ec75682017-07-05 17:02:55 +03001999 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002000 node->setLine(line);
2001 return node;
2002 }
2003
Olli Etuahocce89652017-06-19 16:04:09 +03002004 ASSERT(type != ELoopDoWhile);
2005
Olli Etuaho914b79a2017-06-19 16:03:19 +03002006 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
2007 ASSERT(declaration);
2008 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
2009 ASSERT(declarator->getLeft()->getAsSymbolNode());
2010
2011 // The condition is a declaration. In the AST representation we don't support declarations as
2012 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
2013 // the loop.
2014 TIntermBlock *block = new TIntermBlock();
2015
2016 TIntermDeclaration *declareCondition = new TIntermDeclaration();
2017 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
2018 block->appendStatement(declareCondition);
2019
2020 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
2021 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03002022 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002023 block->appendStatement(loop);
2024 loop->setLine(line);
2025 block->setLine(line);
2026 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002027}
2028
Olli Etuahocce89652017-06-19 16:04:09 +03002029TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
2030 TIntermNodePair code,
2031 const TSourceLoc &loc)
2032{
Olli Etuaho56229f12017-07-10 14:16:33 +03002033 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuahocce89652017-06-19 16:04:09 +03002034
2035 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03002036 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03002037 {
2038 if (cond->getAsConstantUnion()->getBConst(0) == true)
2039 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002040 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03002041 }
2042 else
2043 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002044 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03002045 }
2046 }
2047
Olli Etuaho3ec75682017-07-05 17:02:55 +03002048 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuahocce89652017-06-19 16:04:09 +03002049 node->setLine(loc);
2050
2051 return node;
2052}
2053
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002054void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
2055{
2056 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
2057 typeSpecifier->getBasicType());
2058
2059 if (mShaderVersion < 300 && typeSpecifier->array)
2060 {
2061 error(typeSpecifier->getLine(), "not supported", "first-class array");
2062 typeSpecifier->clearArrayness();
2063 }
2064}
2065
Martin Radev70866b82016-07-22 15:27:42 +03002066TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302067 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002068{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002069 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002070
Martin Radev70866b82016-07-22 15:27:42 +03002071 TPublicType returnType = typeSpecifier;
2072 returnType.qualifier = typeQualifier.qualifier;
2073 returnType.invariant = typeQualifier.invariant;
2074 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002075 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002076 returnType.precision = typeSpecifier.precision;
2077
2078 if (typeQualifier.precision != EbpUndefined)
2079 {
2080 returnType.precision = typeQualifier.precision;
2081 }
2082
Martin Radev4a9cd802016-09-01 16:51:51 +03002083 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2084 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002085
Martin Radev4a9cd802016-09-01 16:51:51 +03002086 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2087 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002088
Martin Radev4a9cd802016-09-01 16:51:51 +03002089 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002090
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002091 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002092 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002093 if (typeSpecifier.array)
2094 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002095 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002096 returnType.clearArrayness();
2097 }
2098
Martin Radev70866b82016-07-22 15:27:42 +03002099 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002100 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002101 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002102 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002103 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002104 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002105
Martin Radev70866b82016-07-22 15:27:42 +03002106 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002107 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002108 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002109 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002110 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002111 }
2112 }
2113 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002114 {
Martin Radev70866b82016-07-22 15:27:42 +03002115 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002116 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002117 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002118 }
Martin Radev70866b82016-07-22 15:27:42 +03002119 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2120 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002121 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002122 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2123 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002124 }
Martin Radev70866b82016-07-22 15:27:42 +03002125 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002126 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002127 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002128 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002129 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002130 }
2131
2132 return returnType;
2133}
2134
Olli Etuaho856c4972016-08-08 11:38:39 +03002135void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2136 const TPublicType &type,
2137 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002138{
2139 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002140 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002141 {
2142 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002143 }
2144
2145 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2146 switch (qualifier)
2147 {
2148 case EvqVertexIn:
2149 // ESSL 3.00 section 4.3.4
2150 if (type.array)
2151 {
2152 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002153 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002154 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002155 return;
2156 case EvqFragmentOut:
2157 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002158 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002159 {
2160 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002161 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002162 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002163 return;
2164 default:
2165 break;
2166 }
2167
2168 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2169 // restrictions.
2170 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002171 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2172 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002173 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2174 {
2175 error(qualifierLocation, "must use 'flat' interpolation here",
2176 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002177 }
2178
Martin Radev4a9cd802016-09-01 16:51:51 +03002179 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002180 {
2181 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2182 // These restrictions are only implied by the ESSL 3.00 spec, but
2183 // the ESSL 3.10 spec lists these restrictions explicitly.
2184 if (type.array)
2185 {
2186 error(qualifierLocation, "cannot be an array of structures",
2187 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002188 }
2189 if (type.isStructureContainingArrays())
2190 {
2191 error(qualifierLocation, "cannot be a structure containing an array",
2192 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002193 }
2194 if (type.isStructureContainingType(EbtStruct))
2195 {
2196 error(qualifierLocation, "cannot be a structure containing a structure",
2197 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002198 }
2199 if (type.isStructureContainingType(EbtBool))
2200 {
2201 error(qualifierLocation, "cannot be a structure containing a bool",
2202 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002203 }
2204 }
2205}
2206
Martin Radev2cc85b32016-08-05 16:22:53 +03002207void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2208{
2209 if (qualifier.getType() == QtStorage)
2210 {
2211 const TStorageQualifierWrapper &storageQualifier =
2212 static_cast<const TStorageQualifierWrapper &>(qualifier);
2213 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2214 !symbolTable.atGlobalLevel())
2215 {
2216 error(storageQualifier.getLine(),
2217 "Local variables can only use the const storage qualifier.",
2218 storageQualifier.getQualifierString().c_str());
2219 }
2220 }
2221}
2222
Olli Etuaho43364892017-02-13 16:00:12 +00002223void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002224 const TSourceLoc &location)
2225{
Jiajia Qinbc585152017-06-23 15:42:17 +08002226 const std::string reason(
2227 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2228 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002229 if (memoryQualifier.readonly)
2230 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002231 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002232 }
2233 if (memoryQualifier.writeonly)
2234 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002235 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002236 }
Martin Radev049edfa2016-11-11 14:35:37 +02002237 if (memoryQualifier.coherent)
2238 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002239 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002240 }
2241 if (memoryQualifier.restrictQualifier)
2242 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002243 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002244 }
2245 if (memoryQualifier.volatileQualifier)
2246 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002247 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002248 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002249}
2250
jchen104cdac9e2017-05-08 11:01:20 +08002251// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2252// intermediate tree.
2253void TParseContext::checkAtomicCounterOffsetIsNotOverlapped(TPublicType &publicType,
2254 size_t size,
2255 bool forceAppend,
2256 const TSourceLoc &loc,
2257 TType &type)
2258{
2259 auto &bindingState = mAtomicCounterBindingStates[publicType.layoutQualifier.binding];
2260 int offset;
2261 if (publicType.layoutQualifier.offset == -1 || forceAppend)
2262 {
2263 offset = bindingState.appendSpan(size);
2264 }
2265 else
2266 {
2267 offset = bindingState.insertSpan(publicType.layoutQualifier.offset, size);
2268 }
2269 if (offset == -1)
2270 {
2271 error(loc, "Offset overlapping", "atomic counter");
2272 return;
2273 }
2274 TLayoutQualifier qualifier = type.getLayoutQualifier();
2275 qualifier.offset = offset;
2276 type.setLayoutQualifier(qualifier);
2277}
2278
Olli Etuaho13389b62016-10-16 11:48:18 +01002279TIntermDeclaration *TParseContext::parseSingleDeclaration(
2280 TPublicType &publicType,
2281 const TSourceLoc &identifierOrTypeLocation,
2282 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002283{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002284 TType type(publicType);
2285 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2286 mDirectiveHandler.pragma().stdgl.invariantAll)
2287 {
2288 TQualifier qualifier = type.getQualifier();
2289
2290 // The directive handler has already taken care of rejecting invalid uses of this pragma
2291 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2292 // affected variable declarations:
2293 //
2294 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2295 // elsewhere, in TranslatorGLSL.)
2296 //
2297 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2298 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2299 // the way this is currently implemented we have to enable this compiler option before
2300 // parsing the shader and determining the shading language version it uses. If this were
2301 // implemented as a post-pass, the workaround could be more targeted.
2302 //
2303 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2304 // the specification, but there are desktop OpenGL drivers that expect that this is the
2305 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2306 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2307 {
2308 type.setInvariant(true);
2309 }
2310 }
2311
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002312 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2313 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002314
Olli Etuahobab4c082015-04-24 16:38:49 +03002315 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002316 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002317
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002318 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002319 if (emptyDeclaration)
2320 {
Martin Radevb8b01222016-11-20 23:25:53 +02002321 emptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002322 // In most cases we don't need to create a symbol node for an empty declaration.
2323 // But if the empty declaration is declaring a struct type, the symbol node will store that.
2324 if (type.getBasicType() == EbtStruct)
2325 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002326 symbol = new TIntermSymbol(0, "", type);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002327 }
jchen104cdac9e2017-05-08 11:01:20 +08002328 else if (IsAtomicCounter(publicType.getBasicType()))
2329 {
2330 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2331 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002332 }
2333 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002334 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002335 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002336
Olli Etuaho856c4972016-08-08 11:38:39 +03002337 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002338
jchen104cdac9e2017-05-08 11:01:20 +08002339 if (IsAtomicCounter(publicType.getBasicType()))
2340 {
2341
2342 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterSize, false,
2343 identifierOrTypeLocation, type);
2344 }
2345
Olli Etuaho2935c582015-04-08 14:32:06 +03002346 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002347 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002348
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002349 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002350 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002351 symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
Olli Etuaho13389b62016-10-16 11:48:18 +01002352 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002353 }
2354
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002355 TIntermDeclaration *declaration = new TIntermDeclaration();
2356 declaration->setLine(identifierOrTypeLocation);
2357 if (symbol)
2358 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002359 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002360 declaration->appendDeclarator(symbol);
2361 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002362 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002363}
2364
Olli Etuaho13389b62016-10-16 11:48:18 +01002365TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
2366 const TSourceLoc &identifierLocation,
2367 const TString &identifier,
2368 const TSourceLoc &indexLocation,
2369 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04002370{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002371 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002372
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002373 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2374 identifierLocation);
2375
2376 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002377
Olli Etuaho856c4972016-08-08 11:38:39 +03002378 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002379
Olli Etuaho8a176262016-08-16 14:23:01 +03002380 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002381
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002382 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002383
Olli Etuaho856c4972016-08-08 11:38:39 +03002384 unsigned int size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002385 // Make the type an array even if size check failed.
2386 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002387 arrayType.makeArray(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04002388
jchen104cdac9e2017-05-08 11:01:20 +08002389 if (IsAtomicCounter(publicType.getBasicType()))
2390 {
2391 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterArrayStride * size, false,
2392 identifierLocation, arrayType);
2393 }
2394
Olli Etuaho2935c582015-04-08 14:32:06 +03002395 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002396 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002397
Olli Etuaho13389b62016-10-16 11:48:18 +01002398 TIntermDeclaration *declaration = new TIntermDeclaration();
2399 declaration->setLine(identifierLocation);
2400
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002401 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002402 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002403 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2404 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002405 declaration->appendDeclarator(symbol);
2406 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002407
Olli Etuaho13389b62016-10-16 11:48:18 +01002408 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002409}
2410
Olli Etuaho13389b62016-10-16 11:48:18 +01002411TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2412 const TSourceLoc &identifierLocation,
2413 const TString &identifier,
2414 const TSourceLoc &initLocation,
2415 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002416{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002417 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002418
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002419 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2420 identifierLocation);
2421
2422 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002423
Olli Etuaho13389b62016-10-16 11:48:18 +01002424 TIntermDeclaration *declaration = new TIntermDeclaration();
2425 declaration->setLine(identifierLocation);
2426
2427 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002428 if (executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002429 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002430 if (initNode)
2431 {
2432 declaration->appendDeclarator(initNode);
2433 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002434 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002435 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002436}
2437
Olli Etuaho13389b62016-10-16 11:48:18 +01002438TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Jamie Madillb98c3a82015-07-23 14:26:04 -04002439 TPublicType &publicType,
2440 const TSourceLoc &identifierLocation,
2441 const TString &identifier,
2442 const TSourceLoc &indexLocation,
2443 TIntermTyped *indexExpression,
2444 const TSourceLoc &initLocation,
2445 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002446{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002447 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002448
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002449 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2450 identifierLocation);
2451
2452 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002453
Olli Etuaho8a176262016-08-16 14:23:01 +03002454 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002455
2456 TPublicType arrayType(publicType);
2457
Olli Etuaho856c4972016-08-08 11:38:39 +03002458 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002459 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2460 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002461 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002462 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002463 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002464 }
2465 // Make the type an array even if size check failed.
2466 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2467 arrayType.setArraySize(size);
2468
Olli Etuaho13389b62016-10-16 11:48:18 +01002469 TIntermDeclaration *declaration = new TIntermDeclaration();
2470 declaration->setLine(identifierLocation);
2471
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002472 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002473 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002474 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002475 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002476 if (initNode)
2477 {
2478 declaration->appendDeclarator(initNode);
2479 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002480 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002481
2482 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002483}
2484
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002485TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002486 const TTypeQualifierBuilder &typeQualifierBuilder,
2487 const TSourceLoc &identifierLoc,
2488 const TString *identifier,
2489 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002490{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002491 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002492
Martin Radev70866b82016-07-22 15:27:42 +03002493 if (!typeQualifier.invariant)
2494 {
2495 error(identifierLoc, "Expected invariant", identifier->c_str());
2496 return nullptr;
2497 }
2498 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2499 {
2500 return nullptr;
2501 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002502 if (!symbol)
2503 {
2504 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002505 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002506 }
Martin Radev70866b82016-07-22 15:27:42 +03002507 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002508 {
Martin Radev70866b82016-07-22 15:27:42 +03002509 error(identifierLoc, "invariant declaration specifies qualifier",
2510 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002511 }
Martin Radev70866b82016-07-22 15:27:42 +03002512 if (typeQualifier.precision != EbpUndefined)
2513 {
2514 error(identifierLoc, "invariant declaration specifies precision",
2515 getPrecisionString(typeQualifier.precision));
2516 }
2517 if (!typeQualifier.layoutQualifier.isEmpty())
2518 {
2519 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2520 }
2521
2522 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002523 if (!variable)
2524 {
2525 return nullptr;
2526 }
Martin Radev70866b82016-07-22 15:27:42 +03002527 const TType &type = variable->getType();
2528
2529 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2530 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002531 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002532
2533 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2534
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002535 TIntermSymbol *intermSymbol = new TIntermSymbol(variable->getUniqueId(), *identifier, type);
2536 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002537
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002538 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002539}
2540
Olli Etuaho13389b62016-10-16 11:48:18 +01002541void TParseContext::parseDeclarator(TPublicType &publicType,
2542 const TSourceLoc &identifierLocation,
2543 const TString &identifier,
2544 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002545{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002546 // If the declaration starting this declarator list was empty (example: int,), some checks were
2547 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002548 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002549 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002550 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2551 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002552 }
2553
Olli Etuaho856c4972016-08-08 11:38:39 +03002554 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002555
Olli Etuaho856c4972016-08-08 11:38:39 +03002556 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002557
Olli Etuaho2935c582015-04-08 14:32:06 +03002558 TVariable *variable = nullptr;
Olli Etuaho43364892017-02-13 16:00:12 +00002559 TType type(publicType);
jchen104cdac9e2017-05-08 11:01:20 +08002560 if (IsAtomicCounter(publicType.getBasicType()))
2561 {
2562 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterSize, true,
2563 identifierLocation, type);
2564 }
Olli Etuaho43364892017-02-13 16:00:12 +00002565 declareVariable(identifierLocation, identifier, type, &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002566
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002567 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002568 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002569 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
2570 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002571 declarationOut->appendDeclarator(symbol);
2572 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002573}
2574
Olli Etuaho13389b62016-10-16 11:48:18 +01002575void TParseContext::parseArrayDeclarator(TPublicType &publicType,
2576 const TSourceLoc &identifierLocation,
2577 const TString &identifier,
2578 const TSourceLoc &arrayLocation,
2579 TIntermTyped *indexExpression,
2580 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002581{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002582 // If the declaration starting this declarator list was empty (example: int,), some checks were
2583 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002584 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002585 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002586 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2587 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002588 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002589
Olli Etuaho856c4972016-08-08 11:38:39 +03002590 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002591
Olli Etuaho856c4972016-08-08 11:38:39 +03002592 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002593
Olli Etuaho8a176262016-08-16 14:23:01 +03002594 if (checkIsValidTypeAndQualifierForArray(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002595 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002596 TType arrayType = TType(publicType);
Olli Etuaho856c4972016-08-08 11:38:39 +03002597 unsigned int size = checkIsValidArraySize(arrayLocation, indexExpression);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002598 arrayType.makeArray(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002599
jchen104cdac9e2017-05-08 11:01:20 +08002600 if (IsAtomicCounter(publicType.getBasicType()))
2601 {
2602 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterArrayStride * size,
2603 true, identifierLocation, arrayType);
2604 }
2605
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002606 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002607 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002608
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002609 if (variable)
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002610 {
2611 TIntermSymbol *symbol =
2612 new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2613 symbol->setLine(identifierLocation);
2614 declarationOut->appendDeclarator(symbol);
2615 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002616 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002617}
2618
Olli Etuaho13389b62016-10-16 11:48:18 +01002619void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2620 const TSourceLoc &identifierLocation,
2621 const TString &identifier,
2622 const TSourceLoc &initLocation,
2623 TIntermTyped *initializer,
2624 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002625{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002626 // If the declaration starting this declarator list was empty (example: int,), some checks were
2627 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002628 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002629 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002630 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2631 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002632 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002633
Olli Etuaho856c4972016-08-08 11:38:39 +03002634 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002635
Olli Etuaho13389b62016-10-16 11:48:18 +01002636 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002637 if (executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002638 {
2639 //
2640 // build the intermediate representation
2641 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002642 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002643 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002644 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002645 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002646 }
2647}
2648
Olli Etuaho13389b62016-10-16 11:48:18 +01002649void TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
2650 const TSourceLoc &identifierLocation,
2651 const TString &identifier,
2652 const TSourceLoc &indexLocation,
2653 TIntermTyped *indexExpression,
2654 const TSourceLoc &initLocation,
2655 TIntermTyped *initializer,
2656 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002657{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002658 // If the declaration starting this declarator list was empty (example: int,), some checks were
2659 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002660 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002661 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002662 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2663 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002664 }
2665
Olli Etuaho856c4972016-08-08 11:38:39 +03002666 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002667
Olli Etuaho8a176262016-08-16 14:23:01 +03002668 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002669
2670 TPublicType arrayType(publicType);
2671
Olli Etuaho856c4972016-08-08 11:38:39 +03002672 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002673 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2674 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002675 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002676 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002677 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002678 }
2679 // Make the type an array even if size check failed.
2680 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2681 arrayType.setArraySize(size);
2682
2683 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002684 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002685 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002686 {
2687 if (initNode)
2688 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002689 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002690 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002691 }
2692}
2693
jchen104cdac9e2017-05-08 11:01:20 +08002694void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2695 const TSourceLoc &location)
2696{
2697 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2698 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2699 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2700 {
2701 error(location, "Requires both binding and offset", "layout");
2702 return;
2703 }
2704 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2705}
2706
Olli Etuahocce89652017-06-19 16:04:09 +03002707void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2708 const TPublicType &type,
2709 const TSourceLoc &loc)
2710{
2711 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2712 !getFragmentPrecisionHigh())
2713 {
2714 error(loc, "precision is not supported in fragment shader", "highp");
2715 }
2716
2717 if (!CanSetDefaultPrecisionOnType(type))
2718 {
2719 error(loc, "illegal type argument for default precision qualifier",
2720 getBasicString(type.getBasicType()));
2721 return;
2722 }
2723 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2724}
2725
Shaob5cc1192017-07-06 10:47:20 +08002726bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2727{
2728 switch (typeQualifier.layoutQualifier.primitiveType)
2729 {
2730 case EptLines:
2731 case EptLinesAdjacency:
2732 case EptTriangles:
2733 case EptTrianglesAdjacency:
2734 return typeQualifier.qualifier == EvqGeometryIn;
2735
2736 case EptLineStrip:
2737 case EptTriangleStrip:
2738 return typeQualifier.qualifier == EvqGeometryOut;
2739
2740 case EptPoints:
2741 return true;
2742
2743 default:
2744 UNREACHABLE();
2745 return false;
2746 }
2747}
2748
Jiawei Shaod8105a02017-08-08 09:54:36 +08002749void TParseContext::setGeometryShaderInputArraySizes()
2750{
2751 // TODO(jiawei.shao@intel.com): check former input array sizes match the input primitive
2752 // declaration.
2753 ASSERT(mGeometryShaderInputArraySize == 0);
2754 mGeometryShaderInputArraySize =
2755 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType);
2756}
2757
Shaob5cc1192017-07-06 10:47:20 +08002758bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2759{
2760 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2761
2762 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2763
2764 if (layoutQualifier.maxVertices != -1)
2765 {
2766 error(typeQualifier.line,
2767 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2768 return false;
2769 }
2770
2771 // Set mGeometryInputPrimitiveType if exists
2772 if (layoutQualifier.primitiveType != EptUndefined)
2773 {
2774 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2775 {
2776 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2777 return false;
2778 }
2779
2780 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2781 {
2782 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shaod8105a02017-08-08 09:54:36 +08002783 setGeometryShaderInputArraySizes();
Shaob5cc1192017-07-06 10:47:20 +08002784 }
2785 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2786 {
2787 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2788 "layout");
2789 return false;
2790 }
2791 }
2792
2793 // Set mGeometryInvocations if exists
2794 if (layoutQualifier.invocations > 0)
2795 {
2796 if (mGeometryShaderInvocations == 0)
2797 {
2798 mGeometryShaderInvocations = layoutQualifier.invocations;
2799 }
2800 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2801 {
2802 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2803 "layout");
2804 return false;
2805 }
2806 }
2807
2808 return true;
2809}
2810
2811bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2812{
2813 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2814
2815 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2816
2817 if (layoutQualifier.invocations > 0)
2818 {
2819 error(typeQualifier.line,
2820 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2821 return false;
2822 }
2823
2824 // Set mGeometryOutputPrimitiveType if exists
2825 if (layoutQualifier.primitiveType != EptUndefined)
2826 {
2827 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2828 {
2829 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2830 return false;
2831 }
2832
2833 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2834 {
2835 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2836 }
2837 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2838 {
2839 error(typeQualifier.line,
2840 "primitive doesn't match earlier output primitive declaration", "layout");
2841 return false;
2842 }
2843 }
2844
2845 // Set mGeometryMaxVertices if exists
2846 if (layoutQualifier.maxVertices > -1)
2847 {
2848 if (mGeometryShaderMaxVertices == -1)
2849 {
2850 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2851 }
2852 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2853 {
2854 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2855 "layout");
2856 return false;
2857 }
2858 }
2859
2860 return true;
2861}
2862
Martin Radev70866b82016-07-22 15:27:42 +03002863void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002864{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002865 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002866 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002867
Martin Radev70866b82016-07-22 15:27:42 +03002868 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2869 typeQualifier.line);
2870
Jamie Madillc2128ff2016-07-04 10:26:17 -04002871 // It should never be the case, but some strange parser errors can send us here.
2872 if (layoutQualifier.isEmpty())
2873 {
2874 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002875 return;
2876 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002877
Martin Radev802abe02016-08-04 17:48:32 +03002878 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002879 {
Olli Etuaho43364892017-02-13 16:00:12 +00002880 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002881 return;
2882 }
2883
Olli Etuaho43364892017-02-13 16:00:12 +00002884 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2885
2886 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002887
2888 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2889
Andrei Volykhina5527072017-03-22 16:46:30 +03002890 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
2891
jchen104cdac9e2017-05-08 11:01:20 +08002892 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
2893
Martin Radev802abe02016-08-04 17:48:32 +03002894 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002895 {
Martin Radev802abe02016-08-04 17:48:32 +03002896 if (mComputeShaderLocalSizeDeclared &&
2897 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2898 {
2899 error(typeQualifier.line, "Work group size does not match the previous declaration",
2900 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002901 return;
2902 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002903
Martin Radev802abe02016-08-04 17:48:32 +03002904 if (mShaderVersion < 310)
2905 {
2906 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002907 return;
2908 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002909
Martin Radev4c4c8e72016-08-04 12:25:34 +03002910 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03002911 {
2912 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002913 return;
2914 }
2915
2916 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
2917 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
2918
2919 const TConstantUnion *maxComputeWorkGroupSizeData =
2920 maxComputeWorkGroupSize->getConstPointer();
2921
2922 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
2923 {
2924 if (layoutQualifier.localSize[i] != -1)
2925 {
2926 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
2927 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
2928 if (mComputeShaderLocalSize[i] < 1 ||
2929 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
2930 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002931 std::stringstream reasonStream;
2932 reasonStream << "invalid value: Value must be at least 1 and no greater than "
2933 << maxComputeWorkGroupSizeValue;
2934 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03002935
Olli Etuaho4de340a2016-12-16 09:32:03 +00002936 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03002937 return;
2938 }
2939 }
2940 }
2941
2942 mComputeShaderLocalSizeDeclared = true;
2943 }
Shaob5cc1192017-07-06 10:47:20 +08002944 else if (typeQualifier.qualifier == EvqGeometryIn)
2945 {
2946 if (mShaderVersion < 310)
2947 {
2948 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
2949 return;
2950 }
2951
2952 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
2953 {
2954 return;
2955 }
2956 }
2957 else if (typeQualifier.qualifier == EvqGeometryOut)
2958 {
2959 if (mShaderVersion < 310)
2960 {
2961 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
2962 "layout");
2963 return;
2964 }
2965
2966 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
2967 {
2968 return;
2969 }
2970 }
Olli Etuaho95468d12017-05-04 11:14:34 +03002971 else if (isMultiviewExtensionEnabled() && typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00002972 {
2973 // This error is only specified in WebGL, but tightens unspecified behavior in the native
2974 // specification.
2975 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
2976 {
2977 error(typeQualifier.line, "Number of views does not match the previous declaration",
2978 "layout");
2979 return;
2980 }
2981
2982 if (layoutQualifier.numViews == -1)
2983 {
2984 error(typeQualifier.line, "No num_views specified", "layout");
2985 return;
2986 }
2987
2988 if (layoutQualifier.numViews > mMaxNumViews)
2989 {
2990 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
2991 "layout");
2992 return;
2993 }
2994
2995 mNumViews = layoutQualifier.numViews;
2996 }
Martin Radev802abe02016-08-04 17:48:32 +03002997 else
Jamie Madill1566ef72013-06-20 11:55:54 -04002998 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00002999 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03003000 {
Martin Radev802abe02016-08-04 17:48:32 +03003001 return;
3002 }
3003
Jiajia Qinbc585152017-06-23 15:42:17 +08003004 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03003005 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003006 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003007 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03003008 return;
3009 }
3010
3011 if (mShaderVersion < 300)
3012 {
3013 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
3014 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003015 return;
3016 }
3017
Olli Etuaho09b04a22016-12-15 13:30:26 +00003018 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003019
3020 if (layoutQualifier.matrixPacking != EmpUnspecified)
3021 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003022 if (typeQualifier.qualifier == EvqUniform)
3023 {
3024 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3025 }
3026 else if (typeQualifier.qualifier == EvqBuffer)
3027 {
3028 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3029 }
Martin Radev802abe02016-08-04 17:48:32 +03003030 }
3031
3032 if (layoutQualifier.blockStorage != EbsUnspecified)
3033 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003034 if (typeQualifier.qualifier == EvqUniform)
3035 {
3036 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3037 }
3038 else if (typeQualifier.qualifier == EvqBuffer)
3039 {
3040 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3041 }
Martin Radev802abe02016-08-04 17:48:32 +03003042 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003043 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003044}
3045
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003046TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3047 const TFunction &function,
3048 const TSourceLoc &location,
3049 bool insertParametersToSymbolTable)
3050{
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003051 checkIsNotReserved(location, function.getName());
3052
Olli Etuahofe486322017-03-21 09:30:54 +00003053 TIntermFunctionPrototype *prototype =
3054 new TIntermFunctionPrototype(function.getReturnType(), TSymbolUniqueId(function));
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003055 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
3056 // point to the data that already exists in the symbol table.
3057 prototype->getFunctionSymbolInfo()->setFromFunction(function);
3058 prototype->setLine(location);
3059
3060 for (size_t i = 0; i < function.getParamCount(); i++)
3061 {
3062 const TConstParameter &param = function.getParam(i);
3063
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003064 TIntermSymbol *symbol = nullptr;
3065
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003066 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3067 // be used for unused args).
3068 if (param.name != nullptr)
3069 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003070 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003071 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003072 {
Olli Etuaho0f684632017-07-13 12:42:15 +03003073 TVariable *variable = symbolTable.declareVariable(param.name, *param.type);
3074 if (variable)
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003075 {
3076 symbol = new TIntermSymbol(variable->getUniqueId(), variable->getName(),
3077 variable->getType());
3078 }
3079 else
3080 {
Olli Etuaho85d624a2017-08-07 13:42:33 +03003081 error(location, "redefinition", param.name->c_str());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003082 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003083 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003084 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003085 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003086 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003087 // The parameter had no name or declaring the symbol failed - either way, add a nameless
3088 // symbol.
3089 symbol = new TIntermSymbol(0, "", *param.type);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003090 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003091 symbol->setLine(location);
3092 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003093 }
3094 return prototype;
3095}
3096
Olli Etuaho16c745a2017-01-16 17:02:27 +00003097TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3098 const TFunction &parsedFunction,
3099 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003100{
Olli Etuaho476197f2016-10-11 13:59:08 +01003101 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3102 // first declaration. Either way the instance in the symbol table is used to track whether the
3103 // function is declared multiple times.
3104 TFunction *function = static_cast<TFunction *>(
3105 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
3106 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003107 {
3108 // ESSL 1.00.17 section 4.2.7.
3109 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3110 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003111 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003112 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02003113
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003114 TIntermFunctionPrototype *prototype =
3115 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003116
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003117 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003118
3119 if (!symbolTable.atGlobalLevel())
3120 {
3121 // ESSL 3.00.4 section 4.2.4.
3122 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003123 }
3124
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003125 return prototype;
3126}
3127
Olli Etuaho336b1472016-10-05 16:37:55 +01003128TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003129 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003130 TIntermBlock *functionBody,
3131 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003132{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003133 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003134 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3135 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003136 error(location, "function does not return a value:",
3137 functionPrototype->getFunctionSymbolInfo()->getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003138 }
3139
Olli Etuahof51fdd22016-10-03 10:03:40 +01003140 if (functionBody == nullptr)
3141 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003142 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003143 functionBody->setLine(location);
3144 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003145 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003146 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003147 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003148
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003149 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003150 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003151}
3152
Olli Etuaho476197f2016-10-11 13:59:08 +01003153void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
3154 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003155 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003156{
Olli Etuaho476197f2016-10-11 13:59:08 +01003157 ASSERT(function);
3158 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003159 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01003160 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003161
3162 if (builtIn)
3163 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003164 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003165 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003166 else
Jamie Madill185fb402015-06-12 15:48:48 -04003167 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003168 TFunction *prevDec = static_cast<TFunction *>(
3169 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
3170
3171 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
3172 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
3173 // occurance.
3174 if (*function != prevDec)
3175 {
3176 // Swap the parameters of the previous declaration to the parameters of the function
3177 // definition (parameter names may differ).
3178 prevDec->swapParameters(**function);
3179
3180 // The function definition will share the same symbol as any previous declaration.
3181 *function = prevDec;
3182 }
3183
3184 if ((*function)->isDefined())
3185 {
3186 error(location, "function already has a body", (*function)->getName().c_str());
3187 }
3188
3189 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04003190 }
Jamie Madill185fb402015-06-12 15:48:48 -04003191
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003192 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01003193 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003194 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003195
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003196 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003197 setLoopNestingLevel(0);
3198}
3199
Jamie Madillb98c3a82015-07-23 14:26:04 -04003200TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003201{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003202 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003203 // We don't know at this point whether this is a function definition or a prototype.
3204 // The definition production code will check for redefinitions.
3205 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003206 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003207 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3208 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003209 //
3210 TFunction *prevDec =
3211 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303212
Martin Radevda6254b2016-12-14 17:00:36 +02003213 if (getShaderVersion() >= 300 &&
3214 symbolTable.hasUnmangledBuiltInForShaderVersion(function->getName().c_str(),
3215 getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303216 {
Martin Radevda6254b2016-12-14 17:00:36 +02003217 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303218 // Therefore overloading or redefining builtin functions is an error.
3219 error(location, "Name of a built-in function cannot be redeclared as function",
3220 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303221 }
3222 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003223 {
3224 if (prevDec->getReturnType() != function->getReturnType())
3225 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003226 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003227 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003228 }
3229 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3230 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003231 if (prevDec->getParam(i).type->getQualifier() !=
3232 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003233 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003234 error(location,
3235 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003236 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003237 }
3238 }
3239 }
3240
3241 //
3242 // Check for previously declared variables using the same name.
3243 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003244 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003245 if (prevSym)
3246 {
3247 if (!prevSym->isFunction())
3248 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003249 error(location, "redefinition of a function", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003250 }
3251 }
3252 else
3253 {
3254 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01003255 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003256 }
3257
3258 // We're at the inner scope level of the function's arguments and body statement.
3259 // Add the function prototype to the surrounding scope instead.
3260 symbolTable.getOuterLevel()->insert(function);
3261
Olli Etuaho78d13742017-01-18 13:06:10 +00003262 // Raise error message if main function takes any parameters or return anything other than void
3263 if (function->getName() == "main")
3264 {
3265 if (function->getParamCount() > 0)
3266 {
3267 error(location, "function cannot take any parameter(s)", "main");
3268 }
3269 if (function->getReturnType().getBasicType() != EbtVoid)
3270 {
3271 error(location, "main function cannot return a value",
3272 function->getReturnType().getBasicString());
3273 }
3274 }
3275
Jamie Madill185fb402015-06-12 15:48:48 -04003276 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003277 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3278 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003279 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3280 //
3281 return function;
3282}
3283
Olli Etuaho9de84a52016-06-14 17:36:01 +03003284TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
3285 const TString *name,
3286 const TSourceLoc &location)
3287{
3288 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3289 {
3290 error(location, "no qualifiers allowed for function return",
3291 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003292 }
3293 if (!type.layoutQualifier.isEmpty())
3294 {
3295 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003296 }
jchen10cc2a10e2017-05-03 14:05:12 +08003297 // make sure an opaque type is not involved as well...
3298 std::string reason(getBasicString(type.getBasicType()));
3299 reason += "s can't be function return values";
3300 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003301 if (mShaderVersion < 300)
3302 {
3303 // Array return values are forbidden, but there's also no valid syntax for declaring array
3304 // return values in ESSL 1.00.
Olli Etuaho77ba4082016-12-16 12:01:18 +00003305 ASSERT(type.arraySize == 0 || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003306
3307 if (type.isStructureContainingArrays())
3308 {
3309 // ESSL 1.00.17 section 6.1 Function Definitions
3310 error(location, "structures containing arrays can't be function return values",
3311 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003312 }
3313 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003314
3315 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003316 return new TFunction(&symbolTable, name, new TType(type));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003317}
3318
Olli Etuahocce89652017-06-19 16:04:09 +03003319TFunction *TParseContext::addNonConstructorFunc(const TString *name, const TSourceLoc &loc)
3320{
Olli Etuahocce89652017-06-19 16:04:09 +03003321 const TType *returnType = TCache::getType(EbtVoid, EbpUndefined);
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003322 return new TFunction(&symbolTable, name, returnType);
Olli Etuahocce89652017-06-19 16:04:09 +03003323}
3324
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003325TFunction *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003326{
Olli Etuahocce89652017-06-19 16:04:09 +03003327 if (mShaderVersion < 300 && publicType.array)
3328 {
3329 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3330 "[]");
3331 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003332 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003333 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003334 error(publicType.getLine(), "constructor can't be a structure definition",
3335 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003336 }
3337
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003338 TType *type = new TType(publicType);
3339 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003340 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003341 error(publicType.getLine(), "cannot construct this type",
3342 getBasicString(publicType.getBasicType()));
3343 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003344 }
3345
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003346 return new TFunction(&symbolTable, nullptr, type, EOpConstruct);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003347}
3348
Olli Etuahocce89652017-06-19 16:04:09 +03003349TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3350 const TString *name,
3351 const TSourceLoc &nameLoc)
3352{
3353 if (publicType.getBasicType() == EbtVoid)
3354 {
3355 error(nameLoc, "illegal use of type 'void'", name->c_str());
3356 }
3357 checkIsNotReserved(nameLoc, *name);
3358 TType *type = new TType(publicType);
3359 TParameter param = {name, type};
3360 return param;
3361}
3362
3363TParameter TParseContext::parseParameterArrayDeclarator(const TString *identifier,
3364 const TSourceLoc &identifierLoc,
3365 TIntermTyped *arraySize,
3366 const TSourceLoc &arrayLoc,
3367 TPublicType *type)
3368{
Olli Etuahoe0803872017-08-23 15:30:23 +03003369 checkArrayElementIsNotArray(arrayLoc, *type);
Olli Etuahocce89652017-06-19 16:04:09 +03003370 unsigned int size = checkIsValidArraySize(arrayLoc, arraySize);
3371 type->setArraySize(size);
3372 return parseParameterDeclarator(*type, identifier, identifierLoc);
3373}
3374
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003375bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(TIntermSequence *arguments,
3376 TType type,
3377 const TSourceLoc &line)
3378{
3379 if (arguments->empty())
3380 {
3381 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3382 return false;
3383 }
3384 for (TIntermNode *arg : *arguments)
3385 {
3386 TIntermTyped *element = arg->getAsTyped();
3387 ASSERT(element);
3388 size_t dimensionalityFromElement = element->getType().getArraySizes().size() + 1u;
3389 if (dimensionalityFromElement > type.getArraySizes().size())
3390 {
3391 error(line, "constructing from a non-dereferenced array", "constructor");
3392 return false;
3393 }
3394 else if (dimensionalityFromElement < type.getArraySizes().size())
3395 {
3396 if (dimensionalityFromElement == 1u)
3397 {
3398 error(line, "implicitly sized array of arrays constructor argument is not an array",
3399 "constructor");
3400 }
3401 else
3402 {
3403 error(line,
3404 "implicitly sized array of arrays constructor argument dimensionality is too "
3405 "low",
3406 "constructor");
3407 }
3408 return false;
3409 }
3410 }
3411 return true;
3412}
3413
Jamie Madillb98c3a82015-07-23 14:26:04 -04003414// This function is used to test for the correctness of the parameters passed to various constructor
3415// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003416//
Olli Etuaho856c4972016-08-08 11:38:39 +03003417// 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 +00003418//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003419TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00003420 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303421 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003422{
Olli Etuaho856c4972016-08-08 11:38:39 +03003423 if (type.isUnsizedArray())
3424 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003425 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003426 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003427 type.sizeUnsizedArrays(TVector<unsigned int>());
Olli Etuaho3ec75682017-07-05 17:02:55 +03003428 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003429 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003430 TIntermTyped *firstElement = arguments->at(0)->getAsTyped();
3431 ASSERT(firstElement);
3432 type.setArraySize(type.getArraySizes().size() - 1u,
3433 static_cast<unsigned int>(arguments->size()));
3434 for (size_t i = 0; i < firstElement->getType().getArraySizes().size(); ++i)
3435 {
3436 if (type.getArraySizes()[i] == 0u)
3437 {
3438 type.setArraySize(i, firstElement->getType().getArraySizes().at(i));
3439 }
3440 }
3441 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003442 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003443
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003444 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003445 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003446 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003447 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003448
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003449 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003450 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003451
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003452 // TODO(oetuaho@nvidia.com): Add support for folding array constructors.
3453 if (!constructorNode->isArray())
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003454 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003455 return constructorNode->fold(mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003456 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003457 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003458}
3459
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003460//
3461// Interface/uniform blocks
Jiawei Shaod8105a02017-08-08 09:54:36 +08003462// TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003463//
Olli Etuaho13389b62016-10-16 11:48:18 +01003464TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003465 const TTypeQualifierBuilder &typeQualifierBuilder,
3466 const TSourceLoc &nameLine,
3467 const TString &blockName,
3468 TFieldList *fieldList,
3469 const TString *instanceName,
3470 const TSourceLoc &instanceLine,
3471 TIntermTyped *arrayIndex,
3472 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003473{
Olli Etuaho856c4972016-08-08 11:38:39 +03003474 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003475
Olli Etuaho77ba4082016-12-16 12:01:18 +00003476 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003477
Jiajia Qinbc585152017-06-23 15:42:17 +08003478 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003479 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003480 error(typeQualifier.line,
3481 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3482 "3.10",
3483 getQualifierString(typeQualifier.qualifier));
3484 }
3485 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3486 {
3487 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003488 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003489 }
3490
Martin Radev70866b82016-07-22 15:27:42 +03003491 if (typeQualifier.invariant)
3492 {
3493 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3494 }
3495
Jiajia Qinbc585152017-06-23 15:42:17 +08003496 if (typeQualifier.qualifier != EvqBuffer)
3497 {
3498 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3499 }
Olli Etuaho43364892017-02-13 16:00:12 +00003500
jchen10af713a22017-04-19 09:10:56 +08003501 // add array index
3502 unsigned int arraySize = 0;
3503 if (arrayIndex != nullptr)
3504 {
3505 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3506 }
3507
3508 if (mShaderVersion < 310)
3509 {
3510 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3511 }
3512 else
3513 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003514 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3515 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003516 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003517
Andrei Volykhina5527072017-03-22 16:46:30 +03003518 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3519
Jamie Madill099c0f32013-06-20 11:55:52 -04003520 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003521 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003522
Jamie Madill099c0f32013-06-20 11:55:52 -04003523 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3524 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003525 if (typeQualifier.qualifier == EvqUniform)
3526 {
3527 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3528 }
3529 else if (typeQualifier.qualifier == EvqBuffer)
3530 {
3531 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3532 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003533 }
3534
Jamie Madill1566ef72013-06-20 11:55:54 -04003535 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3536 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003537 if (typeQualifier.qualifier == EvqUniform)
3538 {
3539 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3540 }
3541 else if (typeQualifier.qualifier == EvqBuffer)
3542 {
3543 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3544 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003545 }
3546
Olli Etuaho856c4972016-08-08 11:38:39 +03003547 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003548
Martin Radev2cc85b32016-08-05 16:22:53 +03003549 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3550
Olli Etuaho0f684632017-07-13 12:42:15 +03003551 if (!symbolTable.declareInterfaceBlockName(&blockName))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303552 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003553 error(nameLine, "redefinition of an interface block name", blockName.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003554 }
3555
Jamie Madill98493dd2013-07-08 14:39:03 -04003556 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303557 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3558 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003559 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303560 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003561 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303562 {
jchen10cc2a10e2017-05-03 14:05:12 +08003563 std::string reason("unsupported type - ");
3564 reason += fieldType->getBasicString();
3565 reason += " types are not allowed in interface blocks";
3566 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003567 }
3568
Jamie Madill98493dd2013-07-08 14:39:03 -04003569 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003570 switch (qualifier)
3571 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003572 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003573 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003574 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003575 if (typeQualifier.qualifier == EvqBuffer)
3576 {
3577 error(field->line(), "invalid qualifier on shader storage block member",
3578 getQualifierString(qualifier));
3579 }
3580 break;
3581 case EvqBuffer:
3582 if (typeQualifier.qualifier == EvqUniform)
3583 {
3584 error(field->line(), "invalid qualifier on uniform block member",
3585 getQualifierString(qualifier));
3586 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003587 break;
3588 default:
3589 error(field->line(), "invalid qualifier on interface block member",
3590 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003591 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003592 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003593
Martin Radev70866b82016-07-22 15:27:42 +03003594 if (fieldType->isInvariant())
3595 {
3596 error(field->line(), "invalid qualifier on interface block member", "invariant");
3597 }
3598
Jamie Madilla5efff92013-06-06 11:56:47 -04003599 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003600 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003601 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003602 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003603
Jamie Madill98493dd2013-07-08 14:39:03 -04003604 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003605 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003606 error(field->line(), "invalid layout qualifier: cannot be used here",
3607 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003608 }
3609
Jamie Madill98493dd2013-07-08 14:39:03 -04003610 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003611 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003612 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003613 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003614 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003615 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003616 warning(field->line(),
3617 "extraneous layout qualifier: only has an effect on matrix types",
3618 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003619 }
3620
Jamie Madill98493dd2013-07-08 14:39:03 -04003621 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003622
3623 if (typeQualifier.qualifier == EvqBuffer)
3624 {
3625 // set memory qualifiers
3626 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3627 // qualified with a memory qualifier, it is as if all of its members were declared with
3628 // the same memory qualifier.
3629 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3630 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3631 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3632 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3633 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3634 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3635 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3636 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3637 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3638 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3639 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003640 }
3641
Jamie Madillb98c3a82015-07-23 14:26:04 -04003642 TInterfaceBlock *interfaceBlock =
Shaob18c33e2017-08-16 12:37:51 +08003643 new TInterfaceBlock(&blockName, fieldList, instanceName, blockLayoutQualifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003644 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
3645 if (arrayIndex != nullptr)
3646 {
3647 interfaceBlockType.makeArray(arraySize);
3648 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003649
3650 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04003651 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003652
Jamie Madill98493dd2013-07-08 14:39:03 -04003653 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003654 {
3655 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003656 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3657 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003658 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303659 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04003660
3661 // set parent pointer of the field variable
3662 fieldType->setInterfaceBlock(interfaceBlock);
3663
Olli Etuaho0f684632017-07-13 12:42:15 +03003664 TVariable *fieldVariable = symbolTable.declareVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04003665
Olli Etuaho0f684632017-07-13 12:42:15 +03003666 if (fieldVariable)
3667 {
3668 fieldVariable->setQualifier(typeQualifier.qualifier);
3669 }
3670 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303671 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003672 error(field->line(), "redefinition of an interface block member name",
3673 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003674 }
3675 }
3676 }
3677 else
3678 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003679 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003680
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003681 // add a symbol for this interface block
Olli Etuaho0f684632017-07-13 12:42:15 +03003682 TVariable *instanceTypeDef = symbolTable.declareVariable(instanceName, interfaceBlockType);
3683 if (instanceTypeDef)
3684 {
3685 instanceTypeDef->setQualifier(typeQualifier.qualifier);
3686 symbolId = instanceTypeDef->getUniqueId();
3687 }
3688 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303689 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003690 error(instanceLine, "redefinition of an interface block instance name",
3691 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003692 }
Olli Etuaho0f684632017-07-13 12:42:15 +03003693 symbolName = *instanceName;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003694 }
3695
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003696 TIntermSymbol *blockSymbol = new TIntermSymbol(symbolId, symbolName, interfaceBlockType);
3697 blockSymbol->setLine(typeQualifier.line);
Olli Etuaho13389b62016-10-16 11:48:18 +01003698 TIntermDeclaration *declaration = new TIntermDeclaration();
3699 declaration->appendDeclarator(blockSymbol);
3700 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003701
3702 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003703 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003704}
3705
Olli Etuaho383b7912016-08-05 11:22:59 +03003706void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003707{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003708 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003709
3710 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003711 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303712 if (mStructNestingLevel > 1)
3713 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003714 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003715 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003716}
3717
3718void TParseContext::exitStructDeclaration()
3719{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003720 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003721}
3722
Olli Etuaho8a176262016-08-16 14:23:01 +03003723void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003724{
Jamie Madillacb4b812016-11-07 13:50:29 -05003725 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303726 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003727 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003728 }
3729
Arun Patole7e7e68d2015-05-22 12:02:25 +05303730 if (field.type()->getBasicType() != EbtStruct)
3731 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003732 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003733 }
3734
3735 // We're already inside a structure definition at this point, so add
3736 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303737 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3738 {
Jamie Madill41a49272014-03-18 16:10:13 -04003739 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003740 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
3741 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003742 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003743 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003744 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003745 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003746}
3747
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003748//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003749// Parse an array index expression
3750//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003751TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3752 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303753 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003754{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003755 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3756 {
3757 if (baseExpression->getAsSymbolNode())
3758 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303759 error(location, " left of '[' is not of type array, matrix, or vector ",
3760 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003761 }
3762 else
3763 {
3764 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3765 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003766
Olli Etuaho3ec75682017-07-05 17:02:55 +03003767 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003768 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003769
Jiawei Shaod8105a02017-08-08 09:54:36 +08003770 if (baseExpression->getQualifier() == EvqPerVertexIn)
3771 {
3772 ASSERT(mShaderType == GL_GEOMETRY_SHADER_OES);
3773 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3774 {
3775 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3776 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3777 }
3778 }
3779
Jamie Madill21c1e452014-12-29 11:33:41 -05003780 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3781
Olli Etuaho36b05142015-11-12 13:10:42 +02003782 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3783 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3784 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3785 // index is a constant expression.
3786 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3787 {
3788 if (baseExpression->isInterfaceBlock())
3789 {
Jiawei Shaod8105a02017-08-08 09:54:36 +08003790 // TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
3791 switch (baseExpression->getQualifier())
3792 {
3793 case EvqPerVertexIn:
3794 break;
3795 case EvqUniform:
3796 case EvqBuffer:
3797 error(location,
3798 "array indexes for uniform block arrays and shader storage block arrays "
3799 "must be constant integral expressions",
3800 "[");
3801 break;
3802 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003803 // We can reach here only in error cases.
3804 ASSERT(mDiagnostics->numErrors() > 0);
3805 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003806 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003807 }
3808 else if (baseExpression->getQualifier() == EvqFragmentOut)
3809 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003810 error(location,
3811 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003812 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003813 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3814 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003815 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003816 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003817 }
3818
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003819 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003820 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003821 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3822 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3823 // constant fold expressions that are not constant expressions). The most compatible way to
3824 // handle this case is to report a warning instead of an error and force the index to be in
3825 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003826 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003827 int index = 0;
3828 if (indexConstantUnion->getBasicType() == EbtInt)
3829 {
3830 index = indexConstantUnion->getIConst(0);
3831 }
3832 else if (indexConstantUnion->getBasicType() == EbtUInt)
3833 {
3834 index = static_cast<int>(indexConstantUnion->getUConst(0));
3835 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003836
3837 int safeIndex = -1;
3838
3839 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04003840 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003841 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003842 {
Olli Etuahodaaff1c2017-07-05 18:03:26 +03003843 if (!isExtensionEnabled("GL_EXT_draw_buffers"))
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003844 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003845 outOfRangeError(outOfRangeIndexIsError, location,
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003846 "array index for gl_FragData must be zero when "
Olli Etuaho4de340a2016-12-16 09:32:03 +00003847 "GL_EXT_draw_buffers is disabled",
3848 "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003849 safeIndex = 0;
3850 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03003851 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003852 // Only do generic out-of-range check if similar error hasn't already been reported.
3853 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003854 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003855 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003856 baseExpression->getOutermostArraySize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003857 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003858 }
3859 }
3860 else if (baseExpression->isMatrix())
3861 {
3862 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03003863 baseExpression->getType().getCols(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003864 "matrix field selection out of range");
Jamie Madill7164cf42013-07-08 13:30:59 -04003865 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003866 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04003867 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003868 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3869 baseExpression->getType().getNominalSize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003870 "vector field selection out of range");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003871 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003872
3873 ASSERT(safeIndex >= 0);
3874 // Data of constant unions can't be changed, because it may be shared with other
3875 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
3876 // sanitized object.
Olli Etuaho56229f12017-07-10 14:16:33 +03003877 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003878 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003879 TConstantUnion *safeConstantUnion = new TConstantUnion();
3880 safeConstantUnion->setIConst(safeIndex);
3881 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
Olli Etuaho56229f12017-07-10 14:16:33 +03003882 indexConstantUnion->getTypePointer()->setBasicType(EbtInt);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003883 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003884
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003885 TIntermBinary *node = new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
3886 node->setLine(location);
3887 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003888 }
Jamie Madill7164cf42013-07-08 13:30:59 -04003889 else
3890 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003891 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
3892 node->setLine(location);
3893 // Indirect indexing can never be constant folded.
3894 return node;
Jamie Madill7164cf42013-07-08 13:30:59 -04003895 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003896}
3897
Olli Etuaho90892fb2016-07-14 14:44:51 +03003898int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
3899 const TSourceLoc &location,
3900 int index,
3901 int arraySize,
Olli Etuaho4de340a2016-12-16 09:32:03 +00003902 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003903{
3904 if (index >= arraySize || index < 0)
3905 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003906 std::stringstream reasonStream;
3907 reasonStream << reason << " '" << index << "'";
3908 std::string token = reasonStream.str();
3909 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuaho90892fb2016-07-14 14:44:51 +03003910 if (index < 0)
3911 {
3912 return 0;
3913 }
3914 else
3915 {
3916 return arraySize - 1;
3917 }
3918 }
3919 return index;
3920}
3921
Jamie Madillb98c3a82015-07-23 14:26:04 -04003922TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3923 const TSourceLoc &dotLocation,
3924 const TString &fieldString,
3925 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003926{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003927 if (baseExpression->isArray())
3928 {
3929 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003930 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003931 }
3932
3933 if (baseExpression->isVector())
3934 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003935 TVector<int> fieldOffsets;
3936 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
3937 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003938 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003939 fieldOffsets.resize(1);
3940 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003941 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003942 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
3943 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003944
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003945 return node->fold();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003946 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003947 else if (baseExpression->getBasicType() == EbtStruct)
3948 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303949 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003950 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003951 {
3952 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003953 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003954 }
3955 else
3956 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003957 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003958 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003959 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003960 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003961 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003962 {
3963 fieldFound = true;
3964 break;
3965 }
3966 }
3967 if (fieldFound)
3968 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003969 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003970 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003971 TIntermBinary *node =
3972 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
3973 node->setLine(dotLocation);
3974 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003975 }
3976 else
3977 {
3978 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003979 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003980 }
3981 }
3982 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003983 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003984 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303985 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003986 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003987 {
3988 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003989 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003990 }
3991 else
3992 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003993 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003994 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003995 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003996 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003997 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003998 {
3999 fieldFound = true;
4000 break;
4001 }
4002 }
4003 if (fieldFound)
4004 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004005 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004006 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004007 TIntermBinary *node =
4008 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4009 node->setLine(dotLocation);
4010 // Indexing interface blocks can never be constant folded.
4011 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004012 }
4013 else
4014 {
4015 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004016 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004017 }
4018 }
4019 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004020 else
4021 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004022 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004023 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004024 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304025 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004026 }
4027 else
4028 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304029 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004030 " field selection requires structure, vector, or interface block on left hand "
4031 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304032 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004033 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004034 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004035 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004036}
4037
Jamie Madillb98c3a82015-07-23 14:26:04 -04004038TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4039 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004040{
Martin Radev802abe02016-08-04 17:48:32 +03004041 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004042
4043 if (qualifierType == "shared")
4044 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004045 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004046 {
4047 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4048 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004049 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004050 }
4051 else if (qualifierType == "packed")
4052 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004053 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004054 {
4055 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4056 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004057 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004058 }
4059 else if (qualifierType == "std140")
4060 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004061 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004062 }
4063 else if (qualifierType == "row_major")
4064 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004065 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004066 }
4067 else if (qualifierType == "column_major")
4068 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004069 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004070 }
4071 else if (qualifierType == "location")
4072 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004073 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
4074 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004075 }
Andrei Volykhina5527072017-03-22 16:46:30 +03004076 else if (qualifierType == "yuv" && isExtensionEnabled("GL_EXT_YUV_target") &&
4077 mShaderType == GL_FRAGMENT_SHADER)
4078 {
4079 qualifier.yuv = true;
4080 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004081 else if (qualifierType == "rgba32f")
4082 {
4083 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4084 qualifier.imageInternalFormat = EiifRGBA32F;
4085 }
4086 else if (qualifierType == "rgba16f")
4087 {
4088 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4089 qualifier.imageInternalFormat = EiifRGBA16F;
4090 }
4091 else if (qualifierType == "r32f")
4092 {
4093 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4094 qualifier.imageInternalFormat = EiifR32F;
4095 }
4096 else if (qualifierType == "rgba8")
4097 {
4098 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4099 qualifier.imageInternalFormat = EiifRGBA8;
4100 }
4101 else if (qualifierType == "rgba8_snorm")
4102 {
4103 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4104 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4105 }
4106 else if (qualifierType == "rgba32i")
4107 {
4108 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4109 qualifier.imageInternalFormat = EiifRGBA32I;
4110 }
4111 else if (qualifierType == "rgba16i")
4112 {
4113 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4114 qualifier.imageInternalFormat = EiifRGBA16I;
4115 }
4116 else if (qualifierType == "rgba8i")
4117 {
4118 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4119 qualifier.imageInternalFormat = EiifRGBA8I;
4120 }
4121 else if (qualifierType == "r32i")
4122 {
4123 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4124 qualifier.imageInternalFormat = EiifR32I;
4125 }
4126 else if (qualifierType == "rgba32ui")
4127 {
4128 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4129 qualifier.imageInternalFormat = EiifRGBA32UI;
4130 }
4131 else if (qualifierType == "rgba16ui")
4132 {
4133 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4134 qualifier.imageInternalFormat = EiifRGBA16UI;
4135 }
4136 else if (qualifierType == "rgba8ui")
4137 {
4138 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4139 qualifier.imageInternalFormat = EiifRGBA8UI;
4140 }
4141 else if (qualifierType == "r32ui")
4142 {
4143 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4144 qualifier.imageInternalFormat = EiifR32UI;
4145 }
Shaob5cc1192017-07-06 10:47:20 +08004146 else if (qualifierType == "points" && isExtensionEnabled("GL_OES_geometry_shader") &&
4147 mShaderType == GL_GEOMETRY_SHADER_OES)
4148 {
4149 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4150 qualifier.primitiveType = EptPoints;
4151 }
4152 else if (qualifierType == "lines" && isExtensionEnabled("GL_OES_geometry_shader") &&
4153 mShaderType == GL_GEOMETRY_SHADER_OES)
4154 {
4155 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4156 qualifier.primitiveType = EptLines;
4157 }
4158 else if (qualifierType == "lines_adjacency" && isExtensionEnabled("GL_OES_geometry_shader") &&
4159 mShaderType == GL_GEOMETRY_SHADER_OES)
4160 {
4161 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4162 qualifier.primitiveType = EptLinesAdjacency;
4163 }
4164 else if (qualifierType == "triangles" && isExtensionEnabled("GL_OES_geometry_shader") &&
4165 mShaderType == GL_GEOMETRY_SHADER_OES)
4166 {
4167 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4168 qualifier.primitiveType = EptTriangles;
4169 }
4170 else if (qualifierType == "triangles_adjacency" &&
4171 isExtensionEnabled("GL_OES_geometry_shader") && mShaderType == GL_GEOMETRY_SHADER_OES)
4172 {
4173 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4174 qualifier.primitiveType = EptTrianglesAdjacency;
4175 }
4176 else if (qualifierType == "line_strip" && isExtensionEnabled("GL_OES_geometry_shader") &&
4177 mShaderType == GL_GEOMETRY_SHADER_OES)
4178 {
4179 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4180 qualifier.primitiveType = EptLineStrip;
4181 }
4182 else if (qualifierType == "triangle_strip" && isExtensionEnabled("GL_OES_geometry_shader") &&
4183 mShaderType == GL_GEOMETRY_SHADER_OES)
4184 {
4185 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4186 qualifier.primitiveType = EptTriangleStrip;
4187 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004188
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004189 else
4190 {
4191 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004192 }
4193
Jamie Madilla5efff92013-06-06 11:56:47 -04004194 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004195}
4196
Martin Radev802abe02016-08-04 17:48:32 +03004197void TParseContext::parseLocalSize(const TString &qualifierType,
4198 const TSourceLoc &qualifierTypeLine,
4199 int intValue,
4200 const TSourceLoc &intValueLine,
4201 const std::string &intValueString,
4202 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004203 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004204{
Olli Etuaho856c4972016-08-08 11:38:39 +03004205 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004206 if (intValue < 1)
4207 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004208 std::stringstream reasonStream;
4209 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4210 std::string reason = reasonStream.str();
4211 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004212 }
4213 (*localSize)[index] = intValue;
4214}
4215
Olli Etuaho09b04a22016-12-15 13:30:26 +00004216void TParseContext::parseNumViews(int intValue,
4217 const TSourceLoc &intValueLine,
4218 const std::string &intValueString,
4219 int *numViews)
4220{
4221 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4222 // specification.
4223 if (intValue < 1)
4224 {
4225 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4226 }
4227 *numViews = intValue;
4228}
4229
Shaob5cc1192017-07-06 10:47:20 +08004230void TParseContext::parseInvocations(int intValue,
4231 const TSourceLoc &intValueLine,
4232 const std::string &intValueString,
4233 int *numInvocations)
4234{
4235 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4236 // it doesn't make sense to accept invocations <= 0.
4237 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4238 {
4239 error(intValueLine,
4240 "out of range: invocations must be in the range of [1, "
4241 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4242 intValueString.c_str());
4243 }
4244 else
4245 {
4246 *numInvocations = intValue;
4247 }
4248}
4249
4250void TParseContext::parseMaxVertices(int intValue,
4251 const TSourceLoc &intValueLine,
4252 const std::string &intValueString,
4253 int *maxVertices)
4254{
4255 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4256 // it doesn't make sense to accept max_vertices < 0.
4257 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4258 {
4259 error(
4260 intValueLine,
4261 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4262 intValueString.c_str());
4263 }
4264 else
4265 {
4266 *maxVertices = intValue;
4267 }
4268}
4269
Jamie Madillb98c3a82015-07-23 14:26:04 -04004270TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4271 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004272 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304273 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004274{
Martin Radev802abe02016-08-04 17:48:32 +03004275 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004276
Martin Radev802abe02016-08-04 17:48:32 +03004277 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004278
Martin Radev802abe02016-08-04 17:48:32 +03004279 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004280 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004281 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004282 if (intValue < 0)
4283 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004284 error(intValueLine, "out of range: location must be non-negative",
4285 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004286 }
4287 else
4288 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004289 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004290 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004291 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004292 }
Olli Etuaho43364892017-02-13 16:00:12 +00004293 else if (qualifierType == "binding")
4294 {
4295 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4296 if (intValue < 0)
4297 {
4298 error(intValueLine, "out of range: binding must be non-negative",
4299 intValueString.c_str());
4300 }
4301 else
4302 {
4303 qualifier.binding = intValue;
4304 }
4305 }
jchen104cdac9e2017-05-08 11:01:20 +08004306 else if (qualifierType == "offset")
4307 {
4308 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4309 if (intValue < 0)
4310 {
4311 error(intValueLine, "out of range: offset must be non-negative",
4312 intValueString.c_str());
4313 }
4314 else
4315 {
4316 qualifier.offset = intValue;
4317 }
4318 }
Martin Radev802abe02016-08-04 17:48:32 +03004319 else if (qualifierType == "local_size_x")
4320 {
4321 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4322 &qualifier.localSize);
4323 }
4324 else if (qualifierType == "local_size_y")
4325 {
4326 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4327 &qualifier.localSize);
4328 }
4329 else if (qualifierType == "local_size_z")
4330 {
4331 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4332 &qualifier.localSize);
4333 }
Olli Etuaho95468d12017-05-04 11:14:34 +03004334 else if (qualifierType == "num_views" && isMultiviewExtensionEnabled() &&
Olli Etuaho09b04a22016-12-15 13:30:26 +00004335 mShaderType == GL_VERTEX_SHADER)
4336 {
4337 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4338 }
Shaob5cc1192017-07-06 10:47:20 +08004339 else if (qualifierType == "invocations" && isExtensionEnabled("GL_OES_geometry_shader") &&
4340 mShaderType == GL_GEOMETRY_SHADER_OES)
4341 {
4342 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4343 }
4344 else if (qualifierType == "max_vertices" && isExtensionEnabled("GL_OES_geometry_shader") &&
4345 mShaderType == GL_GEOMETRY_SHADER_OES)
4346 {
4347 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4348 }
4349
Martin Radev802abe02016-08-04 17:48:32 +03004350 else
4351 {
4352 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004353 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004354
Jamie Madilla5efff92013-06-06 11:56:47 -04004355 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004356}
4357
Olli Etuaho613b9592016-09-05 12:05:53 +03004358TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4359{
4360 return new TTypeQualifierBuilder(
4361 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4362 mShaderVersion);
4363}
4364
Olli Etuahocce89652017-06-19 16:04:09 +03004365TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4366 const TSourceLoc &loc)
4367{
4368 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4369 return new TStorageQualifierWrapper(qualifier, loc);
4370}
4371
4372TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4373{
4374 if (getShaderType() == GL_VERTEX_SHADER)
4375 {
4376 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4377 }
4378 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4379}
4380
4381TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4382{
4383 if (declaringFunction())
4384 {
4385 return new TStorageQualifierWrapper(EvqIn, loc);
4386 }
Shaob5cc1192017-07-06 10:47:20 +08004387
4388 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004389 {
Shaob5cc1192017-07-06 10:47:20 +08004390 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004391 {
Shaob5cc1192017-07-06 10:47:20 +08004392 if (mShaderVersion < 300 && !isMultiviewExtensionEnabled())
4393 {
4394 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4395 }
4396 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004397 }
Shaob5cc1192017-07-06 10:47:20 +08004398 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004399 {
Shaob5cc1192017-07-06 10:47:20 +08004400 if (mShaderVersion < 300)
4401 {
4402 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4403 }
4404 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004405 }
Shaob5cc1192017-07-06 10:47:20 +08004406 case GL_COMPUTE_SHADER:
4407 {
4408 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4409 }
4410 case GL_GEOMETRY_SHADER_OES:
4411 {
4412 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4413 }
4414 default:
4415 {
4416 UNREACHABLE();
4417 return new TStorageQualifierWrapper(EvqLast, loc);
4418 }
Olli Etuahocce89652017-06-19 16:04:09 +03004419 }
Olli Etuahocce89652017-06-19 16:04:09 +03004420}
4421
4422TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4423{
4424 if (declaringFunction())
4425 {
4426 return new TStorageQualifierWrapper(EvqOut, loc);
4427 }
Shaob5cc1192017-07-06 10:47:20 +08004428 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004429 {
Shaob5cc1192017-07-06 10:47:20 +08004430 case GL_VERTEX_SHADER:
4431 {
4432 if (mShaderVersion < 300)
4433 {
4434 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4435 }
4436 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4437 }
4438 case GL_FRAGMENT_SHADER:
4439 {
4440 if (mShaderVersion < 300)
4441 {
4442 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4443 }
4444 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4445 }
4446 case GL_COMPUTE_SHADER:
4447 {
4448 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4449 return new TStorageQualifierWrapper(EvqLast, loc);
4450 }
4451 case GL_GEOMETRY_SHADER_OES:
4452 {
4453 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4454 }
4455 default:
4456 {
4457 UNREACHABLE();
4458 return new TStorageQualifierWrapper(EvqLast, loc);
4459 }
Olli Etuahocce89652017-06-19 16:04:09 +03004460 }
Olli Etuahocce89652017-06-19 16:04:09 +03004461}
4462
4463TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4464{
4465 if (!declaringFunction())
4466 {
4467 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4468 }
4469 return new TStorageQualifierWrapper(EvqInOut, loc);
4470}
4471
Jamie Madillb98c3a82015-07-23 14:26:04 -04004472TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004473 TLayoutQualifier rightQualifier,
4474 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004475{
Martin Radevc28888b2016-07-22 15:27:42 +03004476 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004477 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004478}
4479
Olli Etuahocce89652017-06-19 16:04:09 +03004480TField *TParseContext::parseStructDeclarator(TString *identifier, const TSourceLoc &loc)
4481{
4482 checkIsNotReserved(loc, *identifier);
4483 TType *type = new TType(EbtVoid, EbpUndefined);
4484 return new TField(type, identifier, loc);
4485}
4486
4487TField *TParseContext::parseStructArrayDeclarator(TString *identifier,
4488 const TSourceLoc &loc,
4489 TIntermTyped *arraySize,
4490 const TSourceLoc &arraySizeLoc)
4491{
4492 checkIsNotReserved(loc, *identifier);
4493
4494 TType *type = new TType(EbtVoid, EbpUndefined);
4495 unsigned int size = checkIsValidArraySize(arraySizeLoc, arraySize);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004496 type->makeArray(size);
Olli Etuahocce89652017-06-19 16:04:09 +03004497
4498 return new TField(type, identifier, loc);
4499}
4500
Olli Etuaho4de340a2016-12-16 09:32:03 +00004501TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4502 const TFieldList *newlyAddedFields,
4503 const TSourceLoc &location)
4504{
4505 for (TField *field : *newlyAddedFields)
4506 {
4507 for (TField *oldField : *processedFields)
4508 {
4509 if (oldField->name() == field->name())
4510 {
4511 error(location, "duplicate field name in structure", field->name().c_str());
4512 }
4513 }
4514 processedFields->push_back(field);
4515 }
4516 return processedFields;
4517}
4518
Martin Radev70866b82016-07-22 15:27:42 +03004519TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4520 const TTypeQualifierBuilder &typeQualifierBuilder,
4521 TPublicType *typeSpecifier,
4522 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004523{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004524 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004525
Martin Radev70866b82016-07-22 15:27:42 +03004526 typeSpecifier->qualifier = typeQualifier.qualifier;
4527 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004528 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004529 typeSpecifier->invariant = typeQualifier.invariant;
4530 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304531 {
Martin Radev70866b82016-07-22 15:27:42 +03004532 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004533 }
Martin Radev70866b82016-07-22 15:27:42 +03004534 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004535}
4536
Jamie Madillb98c3a82015-07-23 14:26:04 -04004537TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004538 TFieldList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004539{
Martin Radev4a9cd802016-09-01 16:51:51 +03004540 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4541 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004542
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004543 checkIsNonVoid(typeSpecifier.getLine(), (*declaratorList)[0]->name(),
4544 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004545
Martin Radev4a9cd802016-09-01 16:51:51 +03004546 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004547
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004548 for (unsigned int i = 0; i < declaratorList->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304549 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004550 auto declaratorArraySizes = (*declaratorList)[i]->type()->getArraySizes();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004551 // don't allow arrays of arrays
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004552 if (!declaratorArraySizes.empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304553 {
Olli Etuahoe0803872017-08-23 15:30:23 +03004554 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004555 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004556
4557 TType *type = (*declaratorList)[i]->type();
4558 *type = TType(typeSpecifier);
4559 for (unsigned int arraySize : declaratorArraySizes)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304560 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004561 type->makeArray(arraySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004562 }
4563
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004564 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*declaratorList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004565 }
4566
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004567 return declaratorList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004568}
4569
Martin Radev4a9cd802016-09-01 16:51:51 +03004570TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4571 const TSourceLoc &nameLine,
4572 const TString *structName,
4573 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004574{
Olli Etuahoa5e693a2017-07-13 16:07:26 +03004575 TStructure *structure = new TStructure(&symbolTable, structName, fieldList);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004576
Jamie Madill9b820842015-02-12 10:40:10 -05004577 // Store a bool in the struct if we're at global scope, to allow us to
4578 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004579 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004580
Jamie Madill98493dd2013-07-08 14:39:03 -04004581 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004582 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004583 checkIsNotReserved(nameLine, *structName);
Olli Etuaho0f684632017-07-13 12:42:15 +03004584 if (!symbolTable.declareStructType(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304585 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004586 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004587 }
4588 }
4589
4590 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004591 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004592 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004593 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004594 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004595 switch (qualifier)
4596 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004597 case EvqGlobal:
4598 case EvqTemporary:
4599 break;
4600 default:
4601 error(field.line(), "invalid qualifier on struct member",
4602 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004603 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004604 }
Martin Radev70866b82016-07-22 15:27:42 +03004605 if (field.type()->isInvariant())
4606 {
4607 error(field.line(), "invalid qualifier on struct member", "invariant");
4608 }
jchen104cdac9e2017-05-08 11:01:20 +08004609 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4610 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004611 {
4612 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4613 }
4614
Olli Etuaho43364892017-02-13 16:00:12 +00004615 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4616
4617 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004618
4619 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004620 }
4621
Martin Radev4a9cd802016-09-01 16:51:51 +03004622 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004623 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004624 exitStructDeclaration();
4625
Martin Radev4a9cd802016-09-01 16:51:51 +03004626 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004627}
4628
Jamie Madillb98c3a82015-07-23 14:26:04 -04004629TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004630 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004631 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004632{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004633 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004634 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004635 init->isVector())
4636 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004637 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4638 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004639 return nullptr;
4640 }
4641
Olli Etuahoac5274d2015-02-20 10:19:08 +02004642 if (statementList)
4643 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00004644 if (!ValidateSwitchStatementList(switchType, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004645 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02004646 return nullptr;
4647 }
4648 }
4649
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004650 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4651 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004652 return node;
4653}
4654
4655TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4656{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004657 if (mSwitchNestingLevel == 0)
4658 {
4659 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004660 return nullptr;
4661 }
4662 if (condition == nullptr)
4663 {
4664 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004665 return nullptr;
4666 }
4667 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004668 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004669 {
4670 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004671 }
4672 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004673 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4674 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4675 // fold in case labels.
4676 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004677 {
4678 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004679 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004680 TIntermCase *node = new TIntermCase(condition);
4681 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004682 return node;
4683}
4684
4685TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4686{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004687 if (mSwitchNestingLevel == 0)
4688 {
4689 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004690 return nullptr;
4691 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004692 TIntermCase *node = new TIntermCase(nullptr);
4693 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004694 return node;
4695}
4696
Jamie Madillb98c3a82015-07-23 14:26:04 -04004697TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4698 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004699 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004700{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004701 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004702
4703 switch (op)
4704 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004705 case EOpLogicalNot:
4706 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4707 child->isVector())
4708 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004709 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004710 return nullptr;
4711 }
4712 break;
4713 case EOpBitwiseNot:
4714 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4715 child->isMatrix() || child->isArray())
4716 {
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 break;
4721 case EOpPostIncrement:
4722 case EOpPreIncrement:
4723 case EOpPostDecrement:
4724 case EOpPreDecrement:
4725 case EOpNegative:
4726 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004727 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4728 child->getBasicType() == EbtBool || child->isArray() ||
4729 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004730 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004731 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004732 return nullptr;
4733 }
4734 // Operators for built-ins are already type checked against their prototype.
4735 default:
4736 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004737 }
4738
Jiajia Qinbc585152017-06-23 15:42:17 +08004739 if (child->getMemoryQualifier().writeonly)
4740 {
4741 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4742 return nullptr;
4743 }
4744
Olli Etuahof119a262016-08-19 15:54:22 +03004745 TIntermUnary *node = new TIntermUnary(op, child);
4746 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004747
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004748 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004749}
4750
Olli Etuaho09b22472015-02-11 11:47:26 +02004751TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4752{
Olli Etuahocce89652017-06-19 16:04:09 +03004753 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004754 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004755 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004756 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004757 return child;
4758 }
4759 return node;
4760}
4761
Jamie Madillb98c3a82015-07-23 14:26:04 -04004762TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4763 TIntermTyped *child,
4764 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004765{
Olli Etuaho856c4972016-08-08 11:38:39 +03004766 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004767 return addUnaryMath(op, child, loc);
4768}
4769
Jamie Madillb98c3a82015-07-23 14:26:04 -04004770bool TParseContext::binaryOpCommonCheck(TOperator op,
4771 TIntermTyped *left,
4772 TIntermTyped *right,
4773 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004774{
jchen10b4cf5652017-05-05 18:51:17 +08004775 // Check opaque types are not allowed to be operands in expressions other than array indexing
4776 // and structure member selection.
4777 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4778 {
4779 switch (op)
4780 {
4781 case EOpIndexDirect:
4782 case EOpIndexIndirect:
4783 break;
4784 case EOpIndexDirectStruct:
4785 UNREACHABLE();
4786
4787 default:
4788 error(loc, "Invalid operation for variables with an opaque type",
4789 GetOperatorString(op));
4790 return false;
4791 }
4792 }
jchen10cc2a10e2017-05-03 14:05:12 +08004793
Jiajia Qinbc585152017-06-23 15:42:17 +08004794 if (right->getMemoryQualifier().writeonly)
4795 {
4796 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4797 return false;
4798 }
4799
4800 if (left->getMemoryQualifier().writeonly)
4801 {
4802 switch (op)
4803 {
4804 case EOpAssign:
4805 case EOpInitialize:
4806 case EOpIndexDirect:
4807 case EOpIndexIndirect:
4808 case EOpIndexDirectStruct:
4809 case EOpIndexDirectInterfaceBlock:
4810 break;
4811 default:
4812 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4813 return false;
4814 }
4815 }
4816
Olli Etuaho244be012016-08-18 15:26:02 +03004817 if (left->getType().getStruct() || right->getType().getStruct())
4818 {
4819 switch (op)
4820 {
4821 case EOpIndexDirectStruct:
4822 ASSERT(left->getType().getStruct());
4823 break;
4824 case EOpEqual:
4825 case EOpNotEqual:
4826 case EOpAssign:
4827 case EOpInitialize:
4828 if (left->getType() != right->getType())
4829 {
4830 return false;
4831 }
4832 break;
4833 default:
4834 error(loc, "Invalid operation for structs", GetOperatorString(op));
4835 return false;
4836 }
4837 }
4838
Olli Etuaho94050052017-05-08 14:17:44 +03004839 if (left->isInterfaceBlock() || right->isInterfaceBlock())
4840 {
4841 switch (op)
4842 {
4843 case EOpIndexDirectInterfaceBlock:
4844 ASSERT(left->getType().getInterfaceBlock());
4845 break;
4846 default:
4847 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
4848 return false;
4849 }
4850 }
4851
Olli Etuahod6b14282015-03-17 14:31:35 +02004852 if (left->isArray() || right->isArray())
4853 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004854 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02004855 {
4856 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4857 return false;
4858 }
4859
4860 if (left->isArray() != right->isArray())
4861 {
4862 error(loc, "array / non-array mismatch", GetOperatorString(op));
4863 return false;
4864 }
4865
4866 switch (op)
4867 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004868 case EOpEqual:
4869 case EOpNotEqual:
4870 case EOpAssign:
4871 case EOpInitialize:
4872 break;
4873 default:
4874 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4875 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02004876 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03004877 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004878 if (left->getType().getArraySizes() != right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02004879 {
4880 error(loc, "array size mismatch", GetOperatorString(op));
4881 return false;
4882 }
Olli Etuahod6b14282015-03-17 14:31:35 +02004883 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004884
4885 // Check ops which require integer / ivec parameters
4886 bool isBitShift = false;
4887 switch (op)
4888 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004889 case EOpBitShiftLeft:
4890 case EOpBitShiftRight:
4891 case EOpBitShiftLeftAssign:
4892 case EOpBitShiftRightAssign:
4893 // Unsigned can be bit-shifted by signed and vice versa, but we need to
4894 // check that the basic type is an integer type.
4895 isBitShift = true;
4896 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
4897 {
4898 return false;
4899 }
4900 break;
4901 case EOpBitwiseAnd:
4902 case EOpBitwiseXor:
4903 case EOpBitwiseOr:
4904 case EOpBitwiseAndAssign:
4905 case EOpBitwiseXorAssign:
4906 case EOpBitwiseOrAssign:
4907 // It is enough to check the type of only one operand, since later it
4908 // is checked that the operand types match.
4909 if (!IsInteger(left->getBasicType()))
4910 {
4911 return false;
4912 }
4913 break;
4914 default:
4915 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004916 }
4917
4918 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
4919 // So the basic type should usually match.
4920 if (!isBitShift && left->getBasicType() != right->getBasicType())
4921 {
4922 return false;
4923 }
4924
Olli Etuaho63e1ec52016-08-18 22:05:12 +03004925 // Check that:
4926 // 1. Type sizes match exactly on ops that require that.
4927 // 2. Restrictions for structs that contain arrays or samplers are respected.
4928 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04004929 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004930 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004931 case EOpAssign:
4932 case EOpInitialize:
4933 case EOpEqual:
4934 case EOpNotEqual:
4935 // ESSL 1.00 sections 5.7, 5.8, 5.9
4936 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
4937 {
4938 error(loc, "undefined operation for structs containing arrays",
4939 GetOperatorString(op));
4940 return false;
4941 }
4942 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
4943 // we interpret the spec so that this extends to structs containing samplers,
4944 // similarly to ESSL 1.00 spec.
4945 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
4946 left->getType().isStructureContainingSamplers())
4947 {
4948 error(loc, "undefined operation for structs containing samplers",
4949 GetOperatorString(op));
4950 return false;
4951 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004952
Olli Etuahoe1805592017-01-02 16:41:20 +00004953 if ((left->getNominalSize() != right->getNominalSize()) ||
4954 (left->getSecondarySize() != right->getSecondarySize()))
4955 {
4956 error(loc, "dimension mismatch", GetOperatorString(op));
4957 return false;
4958 }
4959 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004960 case EOpLessThan:
4961 case EOpGreaterThan:
4962 case EOpLessThanEqual:
4963 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00004964 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04004965 {
Olli Etuahoe1805592017-01-02 16:41:20 +00004966 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004967 return false;
4968 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03004969 break;
4970 case EOpAdd:
4971 case EOpSub:
4972 case EOpDiv:
4973 case EOpIMod:
4974 case EOpBitShiftLeft:
4975 case EOpBitShiftRight:
4976 case EOpBitwiseAnd:
4977 case EOpBitwiseXor:
4978 case EOpBitwiseOr:
4979 case EOpAddAssign:
4980 case EOpSubAssign:
4981 case EOpDivAssign:
4982 case EOpIModAssign:
4983 case EOpBitShiftLeftAssign:
4984 case EOpBitShiftRightAssign:
4985 case EOpBitwiseAndAssign:
4986 case EOpBitwiseXorAssign:
4987 case EOpBitwiseOrAssign:
4988 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
4989 {
4990 return false;
4991 }
4992
4993 // Are the sizes compatible?
4994 if (left->getNominalSize() != right->getNominalSize() ||
4995 left->getSecondarySize() != right->getSecondarySize())
4996 {
4997 // If the nominal sizes of operands do not match:
4998 // One of them must be a scalar.
4999 if (!left->isScalar() && !right->isScalar())
5000 return false;
5001
5002 // In the case of compound assignment other than multiply-assign,
5003 // the right side needs to be a scalar. Otherwise a vector/matrix
5004 // would be assigned to a scalar. A scalar can't be shifted by a
5005 // vector either.
5006 if (!right->isScalar() &&
5007 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5008 return false;
5009 }
5010 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005011 default:
5012 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005013 }
5014
Olli Etuahod6b14282015-03-17 14:31:35 +02005015 return true;
5016}
5017
Olli Etuaho1dded802016-08-18 18:13:13 +03005018bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5019 const TType &left,
5020 const TType &right)
5021{
5022 switch (op)
5023 {
5024 case EOpMul:
5025 case EOpMulAssign:
5026 return left.getNominalSize() == right.getNominalSize() &&
5027 left.getSecondarySize() == right.getSecondarySize();
5028 case EOpVectorTimesScalar:
5029 return true;
5030 case EOpVectorTimesScalarAssign:
5031 ASSERT(!left.isMatrix() && !right.isMatrix());
5032 return left.isVector() && !right.isVector();
5033 case EOpVectorTimesMatrix:
5034 return left.getNominalSize() == right.getRows();
5035 case EOpVectorTimesMatrixAssign:
5036 ASSERT(!left.isMatrix() && right.isMatrix());
5037 return left.isVector() && left.getNominalSize() == right.getRows() &&
5038 left.getNominalSize() == right.getCols();
5039 case EOpMatrixTimesVector:
5040 return left.getCols() == right.getNominalSize();
5041 case EOpMatrixTimesScalar:
5042 return true;
5043 case EOpMatrixTimesScalarAssign:
5044 ASSERT(left.isMatrix() && !right.isMatrix());
5045 return !right.isVector();
5046 case EOpMatrixTimesMatrix:
5047 return left.getCols() == right.getRows();
5048 case EOpMatrixTimesMatrixAssign:
5049 ASSERT(left.isMatrix() && right.isMatrix());
5050 // We need to check two things:
5051 // 1. The matrix multiplication step is valid.
5052 // 2. The result will have the same number of columns as the lvalue.
5053 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5054
5055 default:
5056 UNREACHABLE();
5057 return false;
5058 }
5059}
5060
Jamie Madillb98c3a82015-07-23 14:26:04 -04005061TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5062 TIntermTyped *left,
5063 TIntermTyped *right,
5064 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005065{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005066 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005067 return nullptr;
5068
Olli Etuahofc1806e2015-03-17 13:03:11 +02005069 switch (op)
5070 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005071 case EOpEqual:
5072 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005073 case EOpLessThan:
5074 case EOpGreaterThan:
5075 case EOpLessThanEqual:
5076 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005077 break;
5078 case EOpLogicalOr:
5079 case EOpLogicalXor:
5080 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005081 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5082 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005083 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005084 {
5085 return nullptr;
5086 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005087 // Basic types matching should have been already checked.
5088 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005089 break;
5090 case EOpAdd:
5091 case EOpSub:
5092 case EOpDiv:
5093 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005094 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5095 !right->getType().getStruct());
5096 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005097 {
5098 return nullptr;
5099 }
5100 break;
5101 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005102 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5103 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005104 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005105 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005106 {
5107 return nullptr;
5108 }
5109 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005110 default:
5111 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005112 }
5113
Olli Etuaho1dded802016-08-18 18:13:13 +03005114 if (op == EOpMul)
5115 {
5116 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5117 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5118 {
5119 return nullptr;
5120 }
5121 }
5122
Olli Etuaho3fdec912016-08-18 15:08:06 +03005123 TIntermBinary *node = new TIntermBinary(op, left, right);
5124 node->setLine(loc);
5125
Olli Etuaho3fdec912016-08-18 15:08:06 +03005126 // See if we can fold constants.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005127 return node->fold(mDiagnostics);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005128}
5129
Jamie Madillb98c3a82015-07-23 14:26:04 -04005130TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5131 TIntermTyped *left,
5132 TIntermTyped *right,
5133 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005134{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005135 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005136 if (node == 0)
5137 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005138 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5139 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005140 return left;
5141 }
5142 return node;
5143}
5144
Jamie Madillb98c3a82015-07-23 14:26:04 -04005145TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5146 TIntermTyped *left,
5147 TIntermTyped *right,
5148 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005149{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005150 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005151 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005152 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005153 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5154 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005155 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005156 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005157 }
5158 return node;
5159}
5160
Olli Etuaho13389b62016-10-16 11:48:18 +01005161TIntermBinary *TParseContext::createAssign(TOperator op,
5162 TIntermTyped *left,
5163 TIntermTyped *right,
5164 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005165{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005166 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005167 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005168 if (op == EOpMulAssign)
5169 {
5170 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5171 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5172 {
5173 return nullptr;
5174 }
5175 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005176 TIntermBinary *node = new TIntermBinary(op, left, right);
5177 node->setLine(loc);
5178
Olli Etuaho3fdec912016-08-18 15:08:06 +03005179 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005180 }
5181 return nullptr;
5182}
5183
Jamie Madillb98c3a82015-07-23 14:26:04 -04005184TIntermTyped *TParseContext::addAssign(TOperator op,
5185 TIntermTyped *left,
5186 TIntermTyped *right,
5187 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005188{
Olli Etuahocce89652017-06-19 16:04:09 +03005189 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005190 TIntermTyped *node = createAssign(op, left, right, loc);
5191 if (node == nullptr)
5192 {
5193 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005194 return left;
5195 }
5196 return node;
5197}
5198
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005199TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5200 TIntermTyped *right,
5201 const TSourceLoc &loc)
5202{
Corentin Wallez0d959252016-07-12 17:26:32 -04005203 // WebGL2 section 5.26, the following results in an error:
5204 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005205 if (mShaderSpec == SH_WEBGL2_SPEC &&
5206 (left->isArray() || left->getBasicType() == EbtVoid ||
5207 left->getType().isStructureContainingArrays() || right->isArray() ||
5208 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005209 {
5210 error(loc,
5211 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5212 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005213 }
5214
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005215 TIntermBinary *commaNode = new TIntermBinary(EOpComma, left, right);
5216 TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(mShaderVersion, left, right);
5217 commaNode->getTypePointer()->setQualifier(resultQualifier);
5218 return commaNode->fold(mDiagnostics);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005219}
5220
Olli Etuaho49300862015-02-20 14:54:49 +02005221TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5222{
5223 switch (op)
5224 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005225 case EOpContinue:
5226 if (mLoopNestingLevel <= 0)
5227 {
5228 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005229 }
5230 break;
5231 case EOpBreak:
5232 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5233 {
5234 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005235 }
5236 break;
5237 case EOpReturn:
5238 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5239 {
5240 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005241 }
5242 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005243 case EOpKill:
5244 if (mShaderType != GL_FRAGMENT_SHADER)
5245 {
5246 error(loc, "discard supported in fragment shaders only", "discard");
5247 }
5248 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005249 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005250 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005251 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005252 }
Olli Etuahocce89652017-06-19 16:04:09 +03005253 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005254}
5255
Jamie Madillb98c3a82015-07-23 14:26:04 -04005256TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005257 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005258 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005259{
Olli Etuahocce89652017-06-19 16:04:09 +03005260 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005261 {
Olli Etuahocce89652017-06-19 16:04:09 +03005262 ASSERT(op == EOpReturn);
5263 mFunctionReturnsValue = true;
5264 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5265 {
5266 error(loc, "void function cannot return a value", "return");
5267 }
5268 else if (*mCurrentFunctionType != expression->getType())
5269 {
5270 error(loc, "function return is not matching type:", "return");
5271 }
Olli Etuaho49300862015-02-20 14:54:49 +02005272 }
Olli Etuahocce89652017-06-19 16:04:09 +03005273 TIntermBranch *node = new TIntermBranch(op, expression);
5274 node->setLine(loc);
5275 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005276}
5277
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005278void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5279{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005280 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01005281 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005282 TIntermNode *offset = nullptr;
5283 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuahoec9232b2017-03-27 17:01:37 +03005284 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
5285 name == "textureProjLodOffset" || name == "textureGradOffset" ||
5286 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005287 {
5288 offset = arguments->back();
5289 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03005290 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005291 {
5292 // A bias parameter might follow the offset parameter.
5293 ASSERT(arguments->size() >= 3);
5294 offset = (*arguments)[2];
5295 }
5296 if (offset != nullptr)
5297 {
5298 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5299 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5300 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005301 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03005302 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005303 }
5304 else
5305 {
5306 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5307 size_t size = offsetConstantUnion->getType().getObjectSize();
5308 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
5309 for (size_t i = 0u; i < size; ++i)
5310 {
5311 int offsetValue = values[i].getIConst();
5312 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
5313 {
5314 std::stringstream tokenStream;
5315 tokenStream << offsetValue;
5316 std::string token = tokenStream.str();
5317 error(offset->getLine(), "Texture offset value out of valid range",
5318 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005319 }
5320 }
5321 }
5322 }
5323}
5324
Martin Radev2cc85b32016-08-05 16:22:53 +03005325// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5326void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5327{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005328 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03005329 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5330
5331 if (name.compare(0, 5, "image") == 0)
5332 {
5333 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005334 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005335
Olli Etuaho485eefd2017-02-14 17:40:06 +00005336 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005337
5338 if (name.compare(5, 5, "Store") == 0)
5339 {
5340 if (memoryQualifier.readonly)
5341 {
5342 error(imageNode->getLine(),
5343 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005344 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005345 }
5346 }
5347 else if (name.compare(5, 4, "Load") == 0)
5348 {
5349 if (memoryQualifier.writeonly)
5350 {
5351 error(imageNode->getLine(),
5352 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005353 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005354 }
5355 }
5356 }
5357}
5358
5359// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5360void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5361 const TFunction *functionDefinition,
5362 const TIntermAggregate *functionCall)
5363{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005364 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005365
5366 const TIntermSequence &arguments = *functionCall->getSequence();
5367
5368 ASSERT(functionDefinition->getParamCount() == arguments.size());
5369
5370 for (size_t i = 0; i < arguments.size(); ++i)
5371 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005372 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5373 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005374 const TType &functionParameterType = *functionDefinition->getParam(i).type;
5375 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5376
5377 if (IsImage(functionArgumentType.getBasicType()))
5378 {
5379 const TMemoryQualifier &functionArgumentMemoryQualifier =
5380 functionArgumentType.getMemoryQualifier();
5381 const TMemoryQualifier &functionParameterMemoryQualifier =
5382 functionParameterType.getMemoryQualifier();
5383 if (functionArgumentMemoryQualifier.readonly &&
5384 !functionParameterMemoryQualifier.readonly)
5385 {
5386 error(functionCall->getLine(),
5387 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005388 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005389 }
5390
5391 if (functionArgumentMemoryQualifier.writeonly &&
5392 !functionParameterMemoryQualifier.writeonly)
5393 {
5394 error(functionCall->getLine(),
5395 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005396 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005397 }
Martin Radev049edfa2016-11-11 14:35:37 +02005398
5399 if (functionArgumentMemoryQualifier.coherent &&
5400 !functionParameterMemoryQualifier.coherent)
5401 {
5402 error(functionCall->getLine(),
5403 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005404 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005405 }
5406
5407 if (functionArgumentMemoryQualifier.volatileQualifier &&
5408 !functionParameterMemoryQualifier.volatileQualifier)
5409 {
5410 error(functionCall->getLine(),
5411 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005412 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005413 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005414 }
5415 }
5416}
5417
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005418TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005419{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005420 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00005421}
5422
5423TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005424 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00005425 TIntermNode *thisNode,
5426 const TSourceLoc &loc)
5427{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005428 if (thisNode != nullptr)
5429 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005430 return addMethod(fnCall, arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005431 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005432
5433 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005434 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005435 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005436 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005437 }
5438 else
5439 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005440 ASSERT(op == EOpNull);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005441 return addNonConstructorFunctionCall(fnCall, arguments, loc);
5442 }
5443}
5444
5445TIntermTyped *TParseContext::addMethod(TFunction *fnCall,
5446 TIntermSequence *arguments,
5447 TIntermNode *thisNode,
5448 const TSourceLoc &loc)
5449{
5450 TConstantUnion *unionArray = new TConstantUnion[1];
5451 int arraySize = 0;
5452 TIntermTyped *typedThis = thisNode->getAsTyped();
5453 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5454 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5455 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
5456 // So accessing fnCall->getName() below is safe.
5457 if (fnCall->getName() != "length")
5458 {
5459 error(loc, "invalid method", fnCall->getName().c_str());
5460 }
5461 else if (!arguments->empty())
5462 {
5463 error(loc, "method takes no parameters", "length");
5464 }
5465 else if (typedThis == nullptr || !typedThis->isArray())
5466 {
5467 error(loc, "length can only be called on arrays", "length");
5468 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08005469 else if (typedThis->getQualifier() == EvqPerVertexIn &&
5470 mGeometryShaderInputPrimitiveType == EptUndefined)
5471 {
5472 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5473 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005474 else
5475 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03005476 arraySize = typedThis->getOutermostArraySize();
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005477 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuaho72d10202017-01-19 15:58:30 +00005478 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005479 // This code path can be hit with expressions like these:
5480 // (a = b).length()
5481 // (func()).length()
5482 // (int[3](0, 1, 2)).length()
5483 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
5484 // expression.
5485 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
5486 // spec section 5.9 which allows "An array, vector or matrix expression with the
5487 // length method applied".
5488 error(loc, "length can only be called on array names, not on array expressions",
5489 "length");
Olli Etuaho72d10202017-01-19 15:58:30 +00005490 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005491 }
5492 unionArray->setIConst(arraySize);
Olli Etuaho56229f12017-07-10 14:16:33 +03005493 TIntermConstantUnion *node =
5494 new TIntermConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst));
5495 node->setLine(loc);
5496 return node;
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005497}
5498
5499TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunction *fnCall,
5500 TIntermSequence *arguments,
5501 const TSourceLoc &loc)
5502{
5503 // First find by unmangled name to check whether the function name has been
5504 // hidden by a variable name or struct typename.
5505 // If a function is found, check for one with a matching argument list.
5506 bool builtIn;
5507 const TSymbol *symbol = symbolTable.find(fnCall->getName(), mShaderVersion, &builtIn);
5508 if (symbol != nullptr && !symbol->isFunction())
5509 {
5510 error(loc, "function name expected", fnCall->getName().c_str());
5511 }
5512 else
5513 {
5514 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(fnCall->getName(), *arguments),
5515 mShaderVersion, &builtIn);
5516 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005517 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005518 error(loc, "no matching overloaded function found", fnCall->getName().c_str());
5519 }
5520 else
5521 {
5522 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005523 //
5524 // A declared function.
5525 //
Olli Etuaho383b7912016-08-05 11:22:59 +03005526 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005527 {
Olli Etuaho856c4972016-08-08 11:38:39 +03005528 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005529 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005530 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005531 if (builtIn && op != EOpNull)
5532 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005533 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005534 if (fnCandidate->getParamCount() == 1)
5535 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005536 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005537 TIntermNode *unaryParamNode = arguments->front();
5538 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005539 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005540 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005541 }
5542 else
5543 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005544 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00005545 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005546 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005547
5548 // Some built-in functions have out parameters too.
Jiajia Qinbc585152017-06-23 15:42:17 +08005549 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05305550
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005551 if (TIntermAggregate::CanFoldAggregateBuiltInOp(callNode->getOp()))
Arun Patole274f0702015-05-05 13:33:30 +05305552 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005553 // See if we can constant fold a built-in. Note that this may be possible
5554 // even if it is not const-qualified.
5555 return callNode->fold(mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05305556 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005557 else
5558 {
5559 return callNode;
5560 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005561 }
5562 }
5563 else
5564 {
5565 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005566 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005567
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005568 // If builtIn == false, the function is user defined - could be an overloaded
5569 // built-in as well.
5570 // if builtIn == true, it's a builtIn function with no op associated with it.
5571 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005572 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005573 {
Olli Etuahofe486322017-03-21 09:30:54 +00005574 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005575 checkTextureOffsetConst(callNode);
5576 checkImageMemoryAccessForBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03005577 }
5578 else
5579 {
Olli Etuahofe486322017-03-21 09:30:54 +00005580 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005581 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005582 }
5583
Jiajia Qinbc585152017-06-23 15:42:17 +08005584 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005585
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005586 callNode->setLine(loc);
5587
5588 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005589 }
5590 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005591 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005592
5593 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005594 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005595}
5596
Jamie Madillb98c3a82015-07-23 14:26:04 -04005597TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005598 TIntermTyped *trueExpression,
5599 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005600 const TSourceLoc &loc)
5601{
Olli Etuaho56229f12017-07-10 14:16:33 +03005602 if (!checkIsScalarBool(loc, cond))
5603 {
5604 return falseExpression;
5605 }
Olli Etuaho52901742015-04-15 13:42:45 +03005606
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005607 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005608 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005609 std::stringstream reasonStream;
5610 reasonStream << "mismatching ternary operator operand types '"
5611 << trueExpression->getCompleteString() << " and '"
5612 << falseExpression->getCompleteString() << "'";
5613 std::string reason = reasonStream.str();
5614 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005615 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005616 }
Olli Etuahode318b22016-10-25 16:18:25 +01005617 if (IsOpaqueType(trueExpression->getBasicType()))
5618 {
5619 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005620 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005621 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5622 // Note that structs containing opaque types don't need to be checked as structs are
5623 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005624 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005625 return falseExpression;
5626 }
5627
Jiajia Qinbc585152017-06-23 15:42:17 +08005628 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5629 falseExpression->getMemoryQualifier().writeonly)
5630 {
5631 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5632 return falseExpression;
5633 }
5634
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005635 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005636 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005637 // ESSL 3.00.6 section 5.7:
5638 // Ternary operator support is optional for arrays. No certainty that it works across all
5639 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5640 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005641 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005642 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005643 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005644 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005645 }
Olli Etuaho94050052017-05-08 14:17:44 +03005646 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5647 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005648 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005649 return falseExpression;
5650 }
5651
Corentin Wallez0d959252016-07-12 17:26:32 -04005652 // WebGL2 section 5.26, the following results in an error:
5653 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005654 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005655 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005656 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005657 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005658 }
5659
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005660 // Note that the node resulting from here can be a constant union without being qualified as
5661 // constant.
5662 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5663 node->setLine(loc);
5664
5665 return node->fold();
Olli Etuaho52901742015-04-15 13:42:45 +03005666}
Olli Etuaho49300862015-02-20 14:54:49 +02005667
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005668//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005669// Parse an array of strings using yyparse.
5670//
5671// Returns 0 for success.
5672//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005673int PaParseStrings(size_t count,
5674 const char *const string[],
5675 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305676 TParseContext *context)
5677{
Yunchao He4f285442017-04-21 12:15:49 +08005678 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005679 return 1;
5680
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005681 if (glslang_initialize(context))
5682 return 1;
5683
alokp@chromium.org408c45e2012-04-05 15:54:43 +00005684 int error = glslang_scan(count, string, length, context);
5685 if (!error)
5686 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005687
alokp@chromium.org73bc2982012-06-19 18:48:05 +00005688 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00005689
alokp@chromium.org6b495712012-06-29 00:06:58 +00005690 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005691}
Jamie Madill45bcc782016-11-07 13:58:48 -05005692
5693} // namespace sh