blob: 6a312b6be010dbfc12989311447026bfe63971b6 [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:
477 message = "can't modify an input";
478 break;
479 case EvqVertexIn:
480 message = "can't modify an input";
481 break;
482 case EvqUniform:
483 message = "can't modify a uniform";
484 break;
485 case EvqVaryingIn:
486 message = "can't modify a varying";
487 break;
488 case EvqFragCoord:
489 message = "can't modify gl_FragCoord";
490 break;
491 case EvqFrontFacing:
492 message = "can't modify gl_FrontFacing";
493 break;
494 case EvqPointCoord:
495 message = "can't modify gl_PointCoord";
496 break;
Martin Radevb0883602016-08-04 17:48:58 +0300497 case EvqNumWorkGroups:
498 message = "can't modify gl_NumWorkGroups";
499 break;
500 case EvqWorkGroupSize:
501 message = "can't modify gl_WorkGroupSize";
502 break;
503 case EvqWorkGroupID:
504 message = "can't modify gl_WorkGroupID";
505 break;
506 case EvqLocalInvocationID:
507 message = "can't modify gl_LocalInvocationID";
508 break;
509 case EvqGlobalInvocationID:
510 message = "can't modify gl_GlobalInvocationID";
511 break;
512 case EvqLocalInvocationIndex:
513 message = "can't modify gl_LocalInvocationIndex";
514 break;
Olli Etuaho7142f6c2017-05-05 17:07:26 +0300515 case EvqViewIDOVR:
516 message = "can't modify gl_ViewID_OVR";
517 break;
Martin Radev802abe02016-08-04 17:48:32 +0300518 case EvqComputeIn:
519 message = "can't modify work group size variable";
520 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +0800521 case EvqPerVertexIn:
522 message = "can't modify any member in gl_in";
523 break;
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800524 case EvqPrimitiveIDIn:
525 message = "can't modify gl_PrimitiveIDIn";
526 break;
527 case EvqInvocationID:
528 message = "can't modify gl_InvocationID";
529 break;
530 case EvqPrimitiveID:
531 if (mShaderType == GL_FRAGMENT_SHADER)
532 {
533 message = "can't modify gl_PrimitiveID in a fragment shader";
534 }
535 break;
536 case EvqLayer:
537 if (mShaderType == GL_FRAGMENT_SHADER)
538 {
539 message = "can't modify gl_Layer in a fragment shader";
540 }
541 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400542 default:
543 //
544 // Type that can't be written to?
545 //
546 if (node->getBasicType() == EbtVoid)
547 {
548 message = "can't modify void";
549 }
jchen10cc2a10e2017-05-03 14:05:12 +0800550 if (IsOpaqueType(node->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -0400551 {
jchen10cc2a10e2017-05-03 14:05:12 +0800552 message = "can't modify a variable with type ";
553 message += getBasicString(node->getBasicType());
Martin Radev2cc85b32016-08-05 16:22:53 +0300554 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800555 else if (node->getMemoryQualifier().readonly)
556 {
557 message = "can't modify a readonly variable";
558 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000559 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000560
jchen10cc2a10e2017-05-03 14:05:12 +0800561 if (message.empty() && binaryNode == 0 && symNode == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530562 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000563 error(line, "l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000564
Olli Etuaho8a176262016-08-16 14:23:01 +0300565 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000566 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000567
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000568 //
569 // Everything else is okay, no error.
570 //
jchen10cc2a10e2017-05-03 14:05:12 +0800571 if (message.empty())
Olli Etuaho8a176262016-08-16 14:23:01 +0300572 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000573
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000574 //
575 // If we get here, we have an error and a message.
576 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530577 if (symNode)
578 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000579 const char *symbol = symNode->getSymbol().c_str();
580 std::stringstream reasonStream;
581 reasonStream << "l-value required (" << message << " \"" << symbol << "\")";
582 std::string reason = reasonStream.str();
583 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000584 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530585 else
586 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000587 std::stringstream reasonStream;
588 reasonStream << "l-value required (" << message << ")";
589 std::string reason = reasonStream.str();
590 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000591 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000592
Olli Etuaho8a176262016-08-16 14:23:01 +0300593 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000594}
595
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000596// Both test, and if necessary spit out an error, to see if the node is really
597// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300598void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000599{
Olli Etuaho383b7912016-08-05 11:22:59 +0300600 if (node->getQualifier() != EvqConst)
601 {
602 error(node->getLine(), "constant expression required", "");
603 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000604}
605
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000606// Both test, and if necessary spit out an error, to see if the node is really
607// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300608void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000609{
Olli Etuaho383b7912016-08-05 11:22:59 +0300610 if (!node->isScalarInt())
611 {
612 error(node->getLine(), "integer expression required", token);
613 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000614}
615
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000616// Both test, and if necessary spit out an error, to see if we are currently
617// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800618bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000619{
Olli Etuaho856c4972016-08-08 11:38:39 +0300620 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300621 {
622 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800623 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300624 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800625 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000626}
627
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300628// ESSL 3.00.5 sections 3.8 and 3.9.
629// If it starts "gl_" or contains two consecutive underscores, it's reserved.
630// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a webgl shader.
Olli Etuaho856c4972016-08-08 11:38:39 +0300631bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000632{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530633 static const char *reservedErrMsg = "reserved built-in name";
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300634 if (identifier.compare(0, 3, "gl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530635 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300636 error(line, reservedErrMsg, "gl_");
637 return false;
638 }
639 if (sh::IsWebGLBasedSpec(mShaderSpec))
640 {
641 if (identifier.compare(0, 6, "webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530642 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300643 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300644 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000645 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300646 if (identifier.compare(0, 7, "_webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530647 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300648 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300649 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000650 }
651 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300652 if (identifier.find("__") != TString::npos)
653 {
654 error(line,
655 "identifiers containing two consecutive underscores (__) are reserved as "
656 "possible future keywords",
657 identifier.c_str());
658 return false;
659 }
Olli Etuaho8a176262016-08-16 14:23:01 +0300660 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000661}
662
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300663// Make sure the argument types are correct for constructing a specific type.
Olli Etuaho856c4972016-08-08 11:38:39 +0300664bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800665 const TIntermSequence *arguments,
Olli Etuaho856c4972016-08-08 11:38:39 +0300666 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000667{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800668 if (arguments->empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530669 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200670 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300671 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000672 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200673
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300674 for (TIntermNode *arg : *arguments)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530675 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300676 const TIntermTyped *argTyped = arg->getAsTyped();
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200677 ASSERT(argTyped != nullptr);
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300678 if (type.getBasicType() != EbtStruct && IsOpaqueType(argTyped->getBasicType()))
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200679 {
jchen10cc2a10e2017-05-03 14:05:12 +0800680 std::string reason("cannot convert a variable with type ");
681 reason += getBasicString(argTyped->getBasicType());
682 error(line, reason.c_str(), "constructor");
Martin Radev2cc85b32016-08-05 16:22:53 +0300683 return false;
684 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800685 else if (argTyped->getMemoryQualifier().writeonly)
686 {
687 error(line, "cannot convert a variable with writeonly", "constructor");
688 return false;
689 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200690 if (argTyped->getBasicType() == EbtVoid)
691 {
692 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300693 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200694 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000695 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000696
Olli Etuaho856c4972016-08-08 11:38:39 +0300697 if (type.isArray())
698 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300699 // The size of an unsized constructor should already have been determined.
700 ASSERT(!type.isUnsizedArray());
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300701 if (static_cast<size_t>(type.getOutermostArraySize()) != arguments->size())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300702 {
703 error(line, "array constructor needs one argument per array element", "constructor");
704 return false;
705 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300706 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
707 // the array.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800708 for (TIntermNode *const &argNode : *arguments)
Olli Etuaho856c4972016-08-08 11:38:39 +0300709 {
710 const TType &argType = argNode->getAsTyped()->getType();
Jamie Madill34bf2d92017-02-06 13:40:59 -0500711 if (argType.isArray())
712 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300713 error(line, "constructing from a non-dereferenced array", "constructor");
Jamie Madill34bf2d92017-02-06 13:40:59 -0500714 return false;
715 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300716 if (!argType.isElementTypeOf(type))
Olli Etuaho856c4972016-08-08 11:38:39 +0300717 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000718 error(line, "Array constructor argument has an incorrect type", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300719 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300720 }
721 }
722 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300723 else if (type.getBasicType() == EbtStruct)
Olli Etuaho856c4972016-08-08 11:38:39 +0300724 {
725 const TFieldList &fields = type.getStruct()->fields();
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300726 if (fields.size() != arguments->size())
727 {
728 error(line,
729 "Number of constructor parameters does not match the number of structure fields",
730 "constructor");
731 return false;
732 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300733
734 for (size_t i = 0; i < fields.size(); i++)
735 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800736 if (i >= arguments->size() ||
737 (*arguments)[i]->getAsTyped()->getType() != *fields[i]->type())
Olli Etuaho856c4972016-08-08 11:38:39 +0300738 {
739 error(line, "Structure constructor arguments do not match structure fields",
Olli Etuaho4de340a2016-12-16 09:32:03 +0000740 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300741 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300742 }
743 }
744 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300745 else
746 {
747 // We're constructing a scalar, vector, or matrix.
748
749 // Note: It's okay to have too many components available, but not okay to have unused
750 // arguments. 'full' will go to true when enough args have been seen. If we loop again,
751 // there is an extra argument, so 'overFull' will become true.
752
753 size_t size = 0;
754 bool full = false;
755 bool overFull = false;
756 bool matrixArg = false;
757 for (TIntermNode *arg : *arguments)
758 {
759 const TIntermTyped *argTyped = arg->getAsTyped();
760 ASSERT(argTyped != nullptr);
761
Olli Etuaho487b63a2017-05-23 15:55:09 +0300762 if (argTyped->getBasicType() == EbtStruct)
763 {
764 error(line, "a struct cannot be used as a constructor argument for this type",
765 "constructor");
766 return false;
767 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300768 if (argTyped->getType().isArray())
769 {
770 error(line, "constructing from a non-dereferenced array", "constructor");
771 return false;
772 }
773 if (argTyped->getType().isMatrix())
774 {
775 matrixArg = true;
776 }
777
778 size += argTyped->getType().getObjectSize();
779 if (full)
780 {
781 overFull = true;
782 }
Olli Etuaho487b63a2017-05-23 15:55:09 +0300783 if (size >= type.getObjectSize())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300784 {
785 full = true;
786 }
787 }
788
789 if (type.isMatrix() && matrixArg)
790 {
791 if (arguments->size() != 1)
792 {
793 error(line, "constructing matrix from matrix can only take one argument",
794 "constructor");
795 return false;
796 }
797 }
798 else
799 {
800 if (size != 1 && size < type.getObjectSize())
801 {
802 error(line, "not enough data provided for construction", "constructor");
803 return false;
804 }
805 if (overFull)
806 {
807 error(line, "too many arguments", "constructor");
808 return false;
809 }
810 }
811 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300812
Olli Etuaho8a176262016-08-16 14:23:01 +0300813 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000814}
815
Jamie Madillb98c3a82015-07-23 14:26:04 -0400816// This function checks to see if a void variable has been declared and raise an error message for
817// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000818//
819// returns true in case of an error
820//
Olli Etuaho856c4972016-08-08 11:38:39 +0300821bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400822 const TString &identifier,
823 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000824{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300825 if (type == EbtVoid)
826 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000827 error(line, "illegal use of type 'void'", identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300828 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300829 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000830
Olli Etuaho8a176262016-08-16 14:23:01 +0300831 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000832}
833
Jamie Madillb98c3a82015-07-23 14:26:04 -0400834// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300835// or not.
Olli Etuaho56229f12017-07-10 14:16:33 +0300836bool TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000837{
Olli Etuaho37d96cc2017-07-11 14:14:03 +0300838 if (type->getBasicType() != EbtBool || !type->isScalar())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530839 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000840 error(line, "boolean expression expected", "");
Olli Etuaho56229f12017-07-10 14:16:33 +0300841 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530842 }
Olli Etuaho56229f12017-07-10 14:16:33 +0300843 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000844}
845
Jamie Madillb98c3a82015-07-23 14:26:04 -0400846// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300847// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300848void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000849{
Martin Radev4a9cd802016-09-01 16:51:51 +0300850 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530851 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000852 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530853 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000854}
855
jchen10cc2a10e2017-05-03 14:05:12 +0800856bool TParseContext::checkIsNotOpaqueType(const TSourceLoc &line,
857 const TTypeSpecifierNonArray &pType,
858 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000859{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530860 if (pType.type == EbtStruct)
861 {
Olli Etuaho0f684632017-07-13 12:42:15 +0300862 if (ContainsSampler(pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530863 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000864 std::stringstream reasonStream;
865 reasonStream << reason << " (structure contains a sampler)";
866 std::string reasonStr = reasonStream.str();
867 error(line, reasonStr.c_str(), getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300868 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000869 }
jchen10cc2a10e2017-05-03 14:05:12 +0800870 // only samplers need to be checked from structs, since other opaque types can't be struct
871 // members.
Olli Etuaho8a176262016-08-16 14:23:01 +0300872 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530873 }
jchen10cc2a10e2017-05-03 14:05:12 +0800874 else if (IsOpaqueType(pType.type))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530875 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000876 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300877 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000878 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000879
Olli Etuaho8a176262016-08-16 14:23:01 +0300880 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000881}
882
Olli Etuaho856c4972016-08-08 11:38:39 +0300883void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
884 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400885{
886 if (pType.layoutQualifier.location != -1)
887 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400888 error(line, "location must only be specified for a single input or output variable",
889 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400890 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400891}
892
Olli Etuaho856c4972016-08-08 11:38:39 +0300893void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
894 const TLayoutQualifier &layoutQualifier)
895{
896 if (layoutQualifier.location != -1)
897 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000898 const char *errorMsg = "invalid layout qualifier: only valid on program inputs and outputs";
899 if (mShaderVersion >= 310)
900 {
901 errorMsg =
902 "invalid layout qualifier: only valid on program inputs, outputs, and uniforms";
903 }
904 error(location, errorMsg, "location");
Olli Etuaho856c4972016-08-08 11:38:39 +0300905 }
906}
907
Martin Radev2cc85b32016-08-05 16:22:53 +0300908void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
909 TQualifier qualifier,
910 const TType &type)
911{
Martin Radev2cc85b32016-08-05 16:22:53 +0300912 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
jchen10cc2a10e2017-05-03 14:05:12 +0800913 if (IsOpaqueType(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530914 {
jchen10cc2a10e2017-05-03 14:05:12 +0800915 error(line, "opaque types cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000916 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000917}
918
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000919// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300920unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000921{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530922 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000923
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200924 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
925 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
926 // fold as array size.
927 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000928 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000929 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300930 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000931 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000932
Olli Etuaho856c4972016-08-08 11:38:39 +0300933 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400934
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000935 if (constant->getBasicType() == EbtUInt)
936 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300937 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000938 }
939 else
940 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300941 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000942
Olli Etuaho856c4972016-08-08 11:38:39 +0300943 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000944 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400945 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300946 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000947 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400948
Olli Etuaho856c4972016-08-08 11:38:39 +0300949 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400950 }
951
Olli Etuaho856c4972016-08-08 11:38:39 +0300952 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400953 {
954 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300955 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400956 }
957
958 // The size of arrays is restricted here to prevent issues further down the
959 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
960 // 4096 registers so this should be reasonable even for aggressively optimizable code.
961 const unsigned int sizeLimit = 65536;
962
Olli Etuaho856c4972016-08-08 11:38:39 +0300963 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -0400964 {
965 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300966 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000967 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300968
969 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000970}
971
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000972// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +0300973bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
974 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000975{
Olli Etuaho8a176262016-08-16 14:23:01 +0300976 if ((elementQualifier.qualifier == EvqAttribute) ||
977 (elementQualifier.qualifier == EvqVertexIn) ||
978 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300979 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400980 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300981 TType(elementQualifier).getQualifierString());
982 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000983 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000984
Olli Etuaho8a176262016-08-16 14:23:01 +0300985 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000986}
987
Olli Etuaho8a176262016-08-16 14:23:01 +0300988// See if this element type can be formed into an array.
Olli Etuahoe0803872017-08-23 15:30:23 +0300989bool TParseContext::checkArrayElementIsNotArray(const TSourceLoc &line,
990 const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000991{
Olli Etuaho8a176262016-08-16 14:23:01 +0300992 if (elementType.array)
Jamie Madill06145232015-05-13 13:10:01 -0400993 {
Olli Etuaho8a176262016-08-16 14:23:01 +0300994 error(line, "cannot declare arrays of arrays",
995 TType(elementType).getCompleteString().c_str());
996 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000997 }
Olli Etuahoe0803872017-08-23 15:30:23 +0300998 return true;
999}
1000
1001// Check if this qualified element type can be formed into an array. This is only called when array
1002// brackets are associated with an identifier in a declaration, like this:
1003// float a[2];
1004// Similar checks are done in addFullySpecifiedType for array declarations where the array brackets
1005// are associated with the type, like this:
1006// float[2] a;
1007bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
1008 const TPublicType &elementType)
1009{
1010 if (!checkArrayElementIsNotArray(indexLocation, elementType))
1011 {
1012 return false;
1013 }
Olli Etuahocc36b982015-07-10 14:14:18 +03001014 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
1015 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
1016 // 4.3.4).
Martin Radev4a9cd802016-09-01 16:51:51 +03001017 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Olli Etuaho8a176262016-08-16 14:23:01 +03001018 sh::IsVarying(elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +03001019 {
Olli Etuahoe0803872017-08-23 15:30:23 +03001020 error(indexLocation, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001021 TType(elementType).getCompleteString().c_str());
1022 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +03001023 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001024 return checkIsValidQualifierForArray(indexLocation, elementType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001025}
1026
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001027// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +03001028void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
1029 const TString &identifier,
1030 TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001031{
Olli Etuaho3739d232015-04-08 12:23:44 +03001032 ASSERT(type != nullptr);
1033 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001034 {
1035 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +03001036 type->qualifier = EvqTemporary;
1037
1038 // Generate informative error messages for ESSL1.
1039 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001040 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001041 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301042 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001043 "structures containing arrays may not be declared constant since they cannot be "
1044 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +05301045 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001046 }
1047 else
1048 {
1049 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
1050 }
Olli Etuaho383b7912016-08-05 11:22:59 +03001051 return;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001052 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001053 if (type->isUnsizedArray())
1054 {
1055 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
Olli Etuaho376f1b52015-04-13 13:23:41 +03001056 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001057}
1058
Olli Etuaho2935c582015-04-08 14:32:06 +03001059// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001060// and update the symbol table.
1061//
Olli Etuaho2935c582015-04-08 14:32:06 +03001062// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001063//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001064bool TParseContext::declareVariable(const TSourceLoc &line,
1065 const TString &identifier,
1066 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001067 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001068{
Olli Etuaho2935c582015-04-08 14:32:06 +03001069 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001070
Olli Etuaho43364892017-02-13 16:00:12 +00001071 checkBindingIsValid(line, type);
1072
Olli Etuaho856c4972016-08-08 11:38:39 +03001073 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001074
Olli Etuaho2935c582015-04-08 14:32:06 +03001075 // gl_LastFragData may be redeclared with a new precision qualifier
1076 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1077 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001078 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1079 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001080 if (type.isArrayOfArrays())
1081 {
1082 error(line, "redeclaration of gl_LastFragData as an array of arrays",
1083 identifier.c_str());
1084 return false;
1085 }
1086 else if (static_cast<int>(type.getOutermostArraySize()) ==
1087 maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001088 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001089 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001090 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001091 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->getExtension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001092 }
1093 }
1094 else
1095 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001096 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1097 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001098 return false;
1099 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001100 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001101
Olli Etuaho8a176262016-08-16 14:23:01 +03001102 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001103 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001104
Olli Etuaho0f684632017-07-13 12:42:15 +03001105 (*variable) = symbolTable.declareVariable(&identifier, type);
1106 if (!(*variable))
Olli Etuaho2935c582015-04-08 14:32:06 +03001107 {
1108 error(line, "redefinition", identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001109 return false;
1110 }
1111
Olli Etuaho8a176262016-08-16 14:23:01 +03001112 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001113 return false;
1114
1115 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001116}
1117
Martin Radev70866b82016-07-22 15:27:42 +03001118void TParseContext::checkIsParameterQualifierValid(
1119 const TSourceLoc &line,
1120 const TTypeQualifierBuilder &typeQualifierBuilder,
1121 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301122{
Olli Etuahocce89652017-06-19 16:04:09 +03001123 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001124 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001125
1126 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301127 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001128 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1129 }
1130
1131 if (!IsImage(type->getBasicType()))
1132 {
Olli Etuaho43364892017-02-13 16:00:12 +00001133 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001134 }
1135 else
1136 {
1137 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001138 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001139
Martin Radev70866b82016-07-22 15:27:42 +03001140 type->setQualifier(typeQualifier.qualifier);
1141
1142 if (typeQualifier.precision != EbpUndefined)
1143 {
1144 type->setPrecision(typeQualifier.precision);
1145 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001146}
1147
Olli Etuaho856c4972016-08-08 11:38:39 +03001148bool TParseContext::checkCanUseExtension(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001149{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001150 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001151 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +05301152 if (iter == extBehavior.end())
1153 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001154 error(line, "extension is not supported", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001155 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001156 }
zmo@google.comf5450912011-09-09 01:37:19 +00001157 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +05301158 if (iter->second == EBhDisable || iter->second == EBhUndefined)
1159 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00001160 // TODO(oetuaho@nvidia.com): This is slightly hacky. Might be better if symbols could be
1161 // associated with more than one extension.
1162 if (extension == "GL_OVR_multiview")
1163 {
1164 return checkCanUseExtension(line, "GL_OVR_multiview2");
1165 }
Olli Etuaho4de340a2016-12-16 09:32:03 +00001166 error(line, "extension is disabled", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001167 return false;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001168 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301169 if (iter->second == EBhWarn)
1170 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001171 warning(line, "extension is being used", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001172 return true;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001173 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001174
Olli Etuaho8a176262016-08-16 14:23:01 +03001175 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001176}
1177
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001178// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1179// compile-time or link-time errors are the same whether or not the declaration is empty".
1180// This function implements all the checks that are done on qualifiers regardless of if the
1181// declaration is empty.
1182void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1183 const sh::TLayoutQualifier &layoutQualifier,
1184 const TSourceLoc &location)
1185{
1186 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1187 {
1188 error(location, "Shared memory declarations cannot have layout specified", "layout");
1189 }
1190
1191 if (layoutQualifier.matrixPacking != EmpUnspecified)
1192 {
1193 error(location, "layout qualifier only valid for interface blocks",
1194 getMatrixPackingString(layoutQualifier.matrixPacking));
1195 return;
1196 }
1197
1198 if (layoutQualifier.blockStorage != EbsUnspecified)
1199 {
1200 error(location, "layout qualifier only valid for interface blocks",
1201 getBlockStorageString(layoutQualifier.blockStorage));
1202 return;
1203 }
1204
1205 if (qualifier == EvqFragmentOut)
1206 {
1207 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1208 {
1209 error(location, "invalid layout qualifier combination", "yuv");
1210 return;
1211 }
1212 }
1213 else
1214 {
1215 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1216 }
1217
Olli Etuaho95468d12017-05-04 11:14:34 +03001218 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1219 // parsing steps. So it needs to be checked here.
1220 if (isMultiviewExtensionEnabled() && mShaderVersion < 300 && qualifier == EvqVertexIn)
1221 {
1222 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1223 }
1224
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001225 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
1226 if (mShaderVersion >= 310 && qualifier == EvqUniform)
1227 {
1228 canHaveLocation = true;
1229 // We're not checking whether the uniform location is in range here since that depends on
1230 // the type of the variable.
1231 // The type can only be fully determined for non-empty declarations.
1232 }
1233 if (!canHaveLocation)
1234 {
1235 checkLocationIsNotSpecified(location, layoutQualifier);
1236 }
1237}
1238
jchen104cdac9e2017-05-08 11:01:20 +08001239void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1240 const TSourceLoc &location)
1241{
1242 if (publicType.precision != EbpHigh)
1243 {
1244 error(location, "Can only be highp", "atomic counter");
1245 }
1246 // dEQP enforces compile error if location is specified. See uniform_location.test.
1247 if (publicType.layoutQualifier.location != -1)
1248 {
1249 error(location, "location must not be set for atomic_uint", "layout");
1250 }
1251 if (publicType.layoutQualifier.binding == -1)
1252 {
1253 error(location, "no binding specified", "atomic counter");
1254 }
1255}
1256
Martin Radevb8b01222016-11-20 23:25:53 +02001257void TParseContext::emptyDeclarationErrorCheck(const TPublicType &publicType,
1258 const TSourceLoc &location)
1259{
1260 if (publicType.isUnsizedArray())
1261 {
1262 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1263 // error. It is assumed that this applies to empty declarations as well.
1264 error(location, "empty array declaration needs to specify a size", "");
1265 }
Martin Radevb8b01222016-11-20 23:25:53 +02001266}
1267
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001268// These checks are done for all declarations that are non-empty. They're done for non-empty
1269// declarations starting a declarator list, and declarators that follow an empty declaration.
1270void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1271 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001272{
Olli Etuahofa33d582015-04-09 14:33:12 +03001273 switch (publicType.qualifier)
1274 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001275 case EvqVaryingIn:
1276 case EvqVaryingOut:
1277 case EvqAttribute:
1278 case EvqVertexIn:
1279 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001280 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001281 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001282 {
1283 error(identifierLocation, "cannot be used with a structure",
1284 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001285 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001286 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001287 break;
1288 case EvqBuffer:
1289 if (publicType.getBasicType() != EbtInterfaceBlock)
1290 {
1291 error(identifierLocation,
1292 "cannot declare buffer variables at global scope(outside a block)",
1293 getQualifierString(publicType.qualifier));
1294 return;
1295 }
1296 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001297 default:
1298 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001299 }
jchen10cc2a10e2017-05-03 14:05:12 +08001300 std::string reason(getBasicString(publicType.getBasicType()));
1301 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001302 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001303 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001304 {
1305 return;
1306 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001307
Andrei Volykhina5527072017-03-22 16:46:30 +03001308 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1309 publicType.qualifier != EvqConst) &&
1310 publicType.getBasicType() == EbtYuvCscStandardEXT)
1311 {
1312 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1313 getQualifierString(publicType.qualifier));
1314 return;
1315 }
1316
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001317 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1318 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001319 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1320 // But invalid shaders may still reach here with an unsized array declaration.
1321 if (!publicType.isUnsizedArray())
1322 {
1323 TType type(publicType);
1324 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1325 publicType.layoutQualifier);
1326 }
1327 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001328
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001329 // check for layout qualifier issues
1330 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001331
Martin Radev2cc85b32016-08-05 16:22:53 +03001332 if (IsImage(publicType.getBasicType()))
1333 {
1334
1335 switch (layoutQualifier.imageInternalFormat)
1336 {
1337 case EiifRGBA32F:
1338 case EiifRGBA16F:
1339 case EiifR32F:
1340 case EiifRGBA8:
1341 case EiifRGBA8_SNORM:
1342 if (!IsFloatImage(publicType.getBasicType()))
1343 {
1344 error(identifierLocation,
1345 "internal image format requires a floating image type",
1346 getBasicString(publicType.getBasicType()));
1347 return;
1348 }
1349 break;
1350 case EiifRGBA32I:
1351 case EiifRGBA16I:
1352 case EiifRGBA8I:
1353 case EiifR32I:
1354 if (!IsIntegerImage(publicType.getBasicType()))
1355 {
1356 error(identifierLocation,
1357 "internal image format requires an integer image type",
1358 getBasicString(publicType.getBasicType()));
1359 return;
1360 }
1361 break;
1362 case EiifRGBA32UI:
1363 case EiifRGBA16UI:
1364 case EiifRGBA8UI:
1365 case EiifR32UI:
1366 if (!IsUnsignedImage(publicType.getBasicType()))
1367 {
1368 error(identifierLocation,
1369 "internal image format requires an unsigned image type",
1370 getBasicString(publicType.getBasicType()));
1371 return;
1372 }
1373 break;
1374 case EiifUnspecified:
1375 error(identifierLocation, "layout qualifier", "No image internal format specified");
1376 return;
1377 default:
1378 error(identifierLocation, "layout qualifier", "unrecognized token");
1379 return;
1380 }
1381
1382 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1383 switch (layoutQualifier.imageInternalFormat)
1384 {
1385 case EiifR32F:
1386 case EiifR32I:
1387 case EiifR32UI:
1388 break;
1389 default:
1390 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1391 {
1392 error(identifierLocation, "layout qualifier",
1393 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1394 "image variables must be qualified readonly and/or writeonly");
1395 return;
1396 }
1397 break;
1398 }
1399 }
1400 else
1401 {
Olli Etuaho43364892017-02-13 16:00:12 +00001402 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001403 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1404 }
jchen104cdac9e2017-05-08 11:01:20 +08001405
1406 if (IsAtomicCounter(publicType.getBasicType()))
1407 {
1408 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1409 }
1410 else
1411 {
1412 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1413 }
Olli Etuaho43364892017-02-13 16:00:12 +00001414}
Martin Radev2cc85b32016-08-05 16:22:53 +03001415
Olli Etuaho43364892017-02-13 16:00:12 +00001416void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1417{
1418 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001419 // Note that the ESSL 3.10 section 4.4.5 is not particularly clear on how the binding qualifier
1420 // on arrays of arrays should be handled. We interpret the spec so that the binding value is
1421 // incremented for each element of the innermost nested arrays. This is in line with how arrays
1422 // of arrays of blocks are specified to behave in GLSL 4.50 and a conservative interpretation
1423 // when it comes to which shaders are accepted by the compiler.
1424 int arrayTotalElementCount = type.getArraySizeProduct();
Olli Etuaho43364892017-02-13 16:00:12 +00001425 if (IsImage(type.getBasicType()))
1426 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001427 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding,
1428 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001429 }
1430 else if (IsSampler(type.getBasicType()))
1431 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001432 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding,
1433 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001434 }
jchen104cdac9e2017-05-08 11:01:20 +08001435 else if (IsAtomicCounter(type.getBasicType()))
1436 {
1437 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1438 }
Olli Etuaho43364892017-02-13 16:00:12 +00001439 else
1440 {
1441 ASSERT(!IsOpaqueType(type.getBasicType()));
1442 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001443 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001444}
1445
Olli Etuaho856c4972016-08-08 11:38:39 +03001446void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1447 const TString &layoutQualifierName,
1448 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001449{
1450
1451 if (mShaderVersion < versionRequired)
1452 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001453 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001454 }
1455}
1456
Olli Etuaho856c4972016-08-08 11:38:39 +03001457bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1458 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001459{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001460 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001461 for (size_t i = 0u; i < localSize.size(); ++i)
1462 {
1463 if (localSize[i] != -1)
1464 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001465 error(location,
1466 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1467 "global layout declaration",
1468 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001469 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001470 }
1471 }
1472
Olli Etuaho8a176262016-08-16 14:23:01 +03001473 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001474}
1475
Olli Etuaho43364892017-02-13 16:00:12 +00001476void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001477 TLayoutImageInternalFormat internalFormat)
1478{
1479 if (internalFormat != EiifUnspecified)
1480 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001481 error(location, "invalid layout qualifier: only valid when used with images",
1482 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001483 }
Olli Etuaho43364892017-02-13 16:00:12 +00001484}
1485
1486void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1487{
1488 if (binding != -1)
1489 {
1490 error(location,
1491 "invalid layout qualifier: only valid when used with opaque types or blocks",
1492 "binding");
1493 }
1494}
1495
jchen104cdac9e2017-05-08 11:01:20 +08001496void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1497{
1498 if (offset != -1)
1499 {
1500 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1501 "offset");
1502 }
1503}
1504
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001505void TParseContext::checkImageBindingIsValid(const TSourceLoc &location,
1506 int binding,
1507 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001508{
1509 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001510 if (binding >= 0 && binding + arrayTotalElementCount > mMaxImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001511 {
1512 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1513 }
1514}
1515
1516void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1517 int binding,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001518 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001519{
1520 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001521 if (binding >= 0 && binding + arrayTotalElementCount > mMaxCombinedTextureImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001522 {
1523 error(location, "sampler binding greater than maximum texture units", "binding");
1524 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001525}
1526
Jiajia Qinbc585152017-06-23 15:42:17 +08001527void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location,
1528 const TQualifier &qualifier,
1529 int binding,
1530 int arraySize)
jchen10af713a22017-04-19 09:10:56 +08001531{
1532 int size = (arraySize == 0 ? 1 : arraySize);
Jiajia Qinbc585152017-06-23 15:42:17 +08001533 if (qualifier == EvqUniform)
jchen10af713a22017-04-19 09:10:56 +08001534 {
Jiajia Qinbc585152017-06-23 15:42:17 +08001535 if (binding + size > mMaxUniformBufferBindings)
1536 {
1537 error(location, "uniform block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1538 "binding");
1539 }
1540 }
1541 else if (qualifier == EvqBuffer)
1542 {
1543 if (binding + size > mMaxShaderStorageBufferBindings)
1544 {
1545 error(location,
1546 "shader storage block binding greater than MAX_SHADER_STORAGE_BUFFER_BINDINGS",
1547 "binding");
1548 }
jchen10af713a22017-04-19 09:10:56 +08001549 }
1550}
jchen104cdac9e2017-05-08 11:01:20 +08001551void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1552{
1553 if (binding >= mMaxAtomicCounterBindings)
1554 {
1555 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1556 "binding");
1557 }
1558}
jchen10af713a22017-04-19 09:10:56 +08001559
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001560void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1561 int objectLocationCount,
1562 const TLayoutQualifier &layoutQualifier)
1563{
1564 int loc = layoutQualifier.location;
1565 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1566 {
1567 error(location, "Uniform location out of range", "location");
1568 }
1569}
1570
Andrei Volykhina5527072017-03-22 16:46:30 +03001571void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1572{
1573 if (yuv != false)
1574 {
1575 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1576 }
1577}
1578
Jiajia Qinbc585152017-06-23 15:42:17 +08001579void TParseContext::functionCallRValueLValueErrorCheck(const TFunction *fnCandidate,
1580 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001581{
1582 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1583 {
1584 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
Jiajia Qinbc585152017-06-23 15:42:17 +08001585 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
1586 if (!IsImage(argument->getBasicType()) && (IsQualifierUnspecified(qual) || qual == EvqIn ||
1587 qual == EvqInOut || qual == EvqConstReadOnly))
1588 {
1589 if (argument->getMemoryQualifier().writeonly)
1590 {
1591 error(argument->getLine(),
1592 "Writeonly value cannot be passed for 'in' or 'inout' parameters.",
1593 fnCall->getFunctionSymbolInfo()->getName().c_str());
1594 return;
1595 }
1596 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001597 if (qual == EvqOut || qual == EvqInOut)
1598 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001599 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001600 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001601 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001602 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuahoec9232b2017-03-27 17:01:37 +03001603 fnCall->getFunctionSymbolInfo()->getName().c_str());
Olli Etuaho383b7912016-08-05 11:22:59 +03001604 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001605 }
1606 }
1607 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001608}
1609
Martin Radev70866b82016-07-22 15:27:42 +03001610void TParseContext::checkInvariantVariableQualifier(bool invariant,
1611 const TQualifier qualifier,
1612 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001613{
Martin Radev70866b82016-07-22 15:27:42 +03001614 if (!invariant)
1615 return;
1616
1617 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001618 {
Martin Radev70866b82016-07-22 15:27:42 +03001619 // input variables in the fragment shader can be also qualified as invariant
1620 if (!sh::CanBeInvariantESSL1(qualifier))
1621 {
1622 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1623 }
1624 }
1625 else
1626 {
1627 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1628 {
1629 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1630 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001631 }
1632}
1633
Arun Patole7e7e68d2015-05-22 12:02:25 +05301634bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001635{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001636 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001637 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1638 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001639}
1640
Arun Patole7e7e68d2015-05-22 12:02:25 +05301641bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001642{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001643 return ::IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001644}
1645
Jamie Madillb98c3a82015-07-23 14:26:04 -04001646void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1647 const char *extName,
1648 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001649{
1650 pp::SourceLocation srcLoc;
1651 srcLoc.file = loc.first_file;
1652 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001653 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001654}
1655
Jamie Madillb98c3a82015-07-23 14:26:04 -04001656void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1657 const char *name,
1658 const char *value,
1659 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001660{
1661 pp::SourceLocation srcLoc;
1662 srcLoc.file = loc.first_file;
1663 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001664 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001665}
1666
Martin Radev4c4c8e72016-08-04 12:25:34 +03001667sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001668{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001669 sh::WorkGroupSize result;
Martin Radev802abe02016-08-04 17:48:32 +03001670 for (size_t i = 0u; i < result.size(); ++i)
1671 {
1672 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1673 {
1674 result[i] = 1;
1675 }
1676 else
1677 {
1678 result[i] = mComputeShaderLocalSize[i];
1679 }
1680 }
1681 return result;
1682}
1683
Olli Etuaho56229f12017-07-10 14:16:33 +03001684TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1685 const TSourceLoc &line)
1686{
1687 TIntermConstantUnion *node = new TIntermConstantUnion(
1688 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1689 node->setLine(line);
1690 return node;
1691}
1692
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001693/////////////////////////////////////////////////////////////////////////////////
1694//
1695// Non-Errors.
1696//
1697/////////////////////////////////////////////////////////////////////////////////
1698
Jamie Madill5c097022014-08-20 16:38:32 -04001699const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1700 const TString *name,
1701 const TSymbol *symbol)
1702{
Jamie Madill5c097022014-08-20 16:38:32 -04001703 if (!symbol)
1704 {
1705 error(location, "undeclared identifier", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001706 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001707 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001708
1709 if (!symbol->isVariable())
Jamie Madill5c097022014-08-20 16:38:32 -04001710 {
1711 error(location, "variable expected", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001712 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001713 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001714
1715 const TVariable *variable = static_cast<const TVariable *>(symbol);
1716
1717 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
1718 !variable->getExtension().empty())
Jamie Madill5c097022014-08-20 16:38:32 -04001719 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001720 checkCanUseExtension(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001721 }
1722
Olli Etuaho0f684632017-07-13 12:42:15 +03001723 // Reject shaders using both gl_FragData and gl_FragColor
1724 TQualifier qualifier = variable->getType().getQualifier();
1725 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill5c097022014-08-20 16:38:32 -04001726 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001727 mUsesFragData = true;
1728 }
1729 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
1730 {
1731 mUsesFragColor = true;
1732 }
1733 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1734 {
1735 mUsesSecondaryOutputs = true;
Jamie Madill5c097022014-08-20 16:38:32 -04001736 }
1737
Olli Etuaho0f684632017-07-13 12:42:15 +03001738 // This validation is not quite correct - it's only an error to write to
1739 // both FragData and FragColor. For simplicity, and because users shouldn't
1740 // be rewarded for reading from undefined varaibles, return an error
1741 // if they are both referenced, rather than assigned.
1742 if (mUsesFragData && mUsesFragColor)
1743 {
1744 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1745 if (mUsesSecondaryOutputs)
1746 {
1747 errorMessage =
1748 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1749 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1750 }
1751 error(location, errorMessage, name->c_str());
1752 }
1753
1754 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1755 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1756 qualifier == EvqWorkGroupSize)
1757 {
1758 error(location,
1759 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1760 "gl_WorkGroupSize");
1761 }
Jamie Madill5c097022014-08-20 16:38:32 -04001762 return variable;
1763}
1764
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001765TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1766 const TString *name,
1767 const TSymbol *symbol)
1768{
1769 const TVariable *variable = getNamedVariable(location, name, symbol);
1770
Olli Etuaho0f684632017-07-13 12:42:15 +03001771 if (!variable)
1772 {
1773 TIntermTyped *node = CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
1774 node->setLine(location);
1775 return node;
1776 }
1777
Olli Etuaho09b04a22016-12-15 13:30:26 +00001778 if (variable->getType().getQualifier() == EvqViewIDOVR && IsWebGLBasedSpec(mShaderSpec) &&
1779 mShaderType == GL_FRAGMENT_SHADER && !isExtensionEnabled("GL_OVR_multiview2"))
1780 {
1781 // WEBGL_multiview spec
1782 error(location, "Need to enable OVR_multiview2 to use gl_ViewID_OVR in fragment shader",
1783 "gl_ViewID_OVR");
1784 }
1785
Olli Etuaho56229f12017-07-10 14:16:33 +03001786 TIntermTyped *node = nullptr;
1787
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001788 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001789 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001790 const TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho56229f12017-07-10 14:16:33 +03001791 node = new TIntermConstantUnion(constArray, variable->getType());
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001792 }
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001793 else if (variable->getType().getQualifier() == EvqWorkGroupSize &&
1794 mComputeShaderLocalSizeDeclared)
1795 {
1796 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1797 // needs to be added to the AST as a constant and not as a symbol.
1798 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1799 TConstantUnion *constArray = new TConstantUnion[3];
1800 for (size_t i = 0; i < 3; ++i)
1801 {
1802 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1803 }
1804
1805 ASSERT(variable->getType().getBasicType() == EbtUInt);
1806 ASSERT(variable->getType().getObjectSize() == 3);
1807
1808 TType type(variable->getType());
1809 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001810 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001811 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08001812 // TODO(jiawei.shao@intel.com): set array sizes for user-defined geometry shader inputs.
1813 else if (variable->getType().getQualifier() == EvqPerVertexIn)
1814 {
1815 TType type(variable->getType());
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001816 type.setArraySize(0, mGeometryShaderInputArraySize);
Jiawei Shaod8105a02017-08-08 09:54:36 +08001817 node = new TIntermSymbol(variable->getUniqueId(), variable->getName(), type);
1818 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001819 else
1820 {
Olli Etuaho56229f12017-07-10 14:16:33 +03001821 node = new TIntermSymbol(variable->getUniqueId(), variable->getName(), variable->getType());
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001822 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001823 ASSERT(node != nullptr);
1824 node->setLine(location);
1825 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001826}
1827
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001828// Initializers show up in several places in the grammar. Have one set of
1829// code to handle them here.
1830//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001831// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001832bool TParseContext::executeInitializer(const TSourceLoc &line,
1833 const TString &identifier,
1834 const TPublicType &pType,
1835 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001836 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001837{
Olli Etuaho13389b62016-10-16 11:48:18 +01001838 ASSERT(initNode != nullptr);
1839 ASSERT(*initNode == nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001840 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001841
Olli Etuaho2935c582015-04-08 14:32:06 +03001842 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001843 if (type.isUnsizedArray())
1844 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001845 // In case initializer is not an array or type has more dimensions than initializer, this
1846 // will default to setting array sizes to 1. We have not checked yet whether the initializer
1847 // actually is an array or not. Having a non-array initializer for an unsized array will
1848 // result in an error later, so we don't generate an error message here.
1849 type.sizeUnsizedArrays(initializer->getType().getArraySizes());
Olli Etuaho376f1b52015-04-13 13:23:41 +03001850 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001851 if (!declareVariable(line, identifier, type, &variable))
1852 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001853 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001854 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001855
Olli Etuahob0c645e2015-05-12 14:25:36 +03001856 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001857 if (symbolTable.atGlobalLevel() &&
1858 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001859 {
1860 // Error message does not completely match behavior with ESSL 1.00, but
1861 // we want to steer developers towards only using constant expressions.
1862 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001863 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001864 }
1865 if (globalInitWarning)
1866 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001867 warning(
1868 line,
1869 "global variable initializers should be constant expressions "
1870 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1871 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001872 }
1873
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001874 //
1875 // identifier must be of type constant, a global, or a temporary
1876 //
1877 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301878 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1879 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001880 error(line, " cannot initialize this type of qualifier ",
1881 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001882 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001883 }
1884 //
1885 // test for and propagate constant
1886 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001887
Arun Patole7e7e68d2015-05-22 12:02:25 +05301888 if (qualifier == EvqConst)
1889 {
1890 if (qualifier != initializer->getType().getQualifier())
1891 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001892 std::stringstream reasonStream;
1893 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1894 << "'";
1895 std::string reason = reasonStream.str();
1896 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001897 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001898 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001899 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301900 if (type != initializer->getType())
1901 {
1902 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001903 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001904 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001905 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001906 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001907
1908 // Save the constant folded value to the variable if possible. For example array
1909 // initializers are not folded, since that way copying the array literal to multiple places
1910 // in the shader is avoided.
1911 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1912 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301913 if (initializer->getAsConstantUnion())
1914 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001915 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001916 ASSERT(*initNode == nullptr);
1917 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301918 }
1919 else if (initializer->getAsSymbolNode())
1920 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001921 const TSymbol *symbol =
1922 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1923 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001924
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001925 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001926 if (constArray)
1927 {
1928 variable->shareConstPointer(constArray);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001929 ASSERT(*initNode == nullptr);
1930 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001931 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001932 }
1933 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001934
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001935 TIntermSymbol *intermSymbol =
1936 new TIntermSymbol(variable->getUniqueId(), variable->getName(), variable->getType());
1937 intermSymbol->setLine(line);
Olli Etuaho13389b62016-10-16 11:48:18 +01001938 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1939 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001940 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001941 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001942 return false;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001943 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001944
Olli Etuaho914b79a2017-06-19 16:03:19 +03001945 return true;
1946}
1947
1948TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
1949 const TString &identifier,
1950 TIntermTyped *initializer,
1951 const TSourceLoc &loc)
1952{
1953 checkIsScalarBool(loc, pType);
1954 TIntermBinary *initNode = nullptr;
1955 if (executeInitializer(loc, identifier, pType, initializer, &initNode))
1956 {
1957 // The initializer is valid. The init condition needs to have a node - either the
1958 // initializer node, or a constant node in case the initialized variable is const and won't
1959 // be recorded in the AST.
1960 if (initNode == nullptr)
1961 {
1962 return initializer;
1963 }
1964 else
1965 {
1966 TIntermDeclaration *declaration = new TIntermDeclaration();
1967 declaration->appendDeclarator(initNode);
1968 return declaration;
1969 }
1970 }
1971 return nullptr;
1972}
1973
1974TIntermNode *TParseContext::addLoop(TLoopType type,
1975 TIntermNode *init,
1976 TIntermNode *cond,
1977 TIntermTyped *expr,
1978 TIntermNode *body,
1979 const TSourceLoc &line)
1980{
1981 TIntermNode *node = nullptr;
1982 TIntermTyped *typedCond = nullptr;
1983 if (cond)
1984 {
1985 typedCond = cond->getAsTyped();
1986 }
1987 if (cond == nullptr || typedCond)
1988 {
Olli Etuahocce89652017-06-19 16:04:09 +03001989 if (type == ELoopDoWhile)
1990 {
1991 checkIsScalarBool(line, typedCond);
1992 }
1993 // In the case of other loops, it was checked before that the condition is a scalar boolean.
1994 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
1995 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
1996 !typedCond->isVector()));
1997
Olli Etuaho3ec75682017-07-05 17:02:55 +03001998 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03001999 node->setLine(line);
2000 return node;
2001 }
2002
Olli Etuahocce89652017-06-19 16:04:09 +03002003 ASSERT(type != ELoopDoWhile);
2004
Olli Etuaho914b79a2017-06-19 16:03:19 +03002005 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
2006 ASSERT(declaration);
2007 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
2008 ASSERT(declarator->getLeft()->getAsSymbolNode());
2009
2010 // The condition is a declaration. In the AST representation we don't support declarations as
2011 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
2012 // the loop.
2013 TIntermBlock *block = new TIntermBlock();
2014
2015 TIntermDeclaration *declareCondition = new TIntermDeclaration();
2016 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
2017 block->appendStatement(declareCondition);
2018
2019 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
2020 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03002021 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002022 block->appendStatement(loop);
2023 loop->setLine(line);
2024 block->setLine(line);
2025 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002026}
2027
Olli Etuahocce89652017-06-19 16:04:09 +03002028TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
2029 TIntermNodePair code,
2030 const TSourceLoc &loc)
2031{
Olli Etuaho56229f12017-07-10 14:16:33 +03002032 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuahocce89652017-06-19 16:04:09 +03002033
2034 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03002035 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03002036 {
2037 if (cond->getAsConstantUnion()->getBConst(0) == true)
2038 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002039 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03002040 }
2041 else
2042 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002043 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03002044 }
2045 }
2046
Olli Etuaho3ec75682017-07-05 17:02:55 +03002047 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuahocce89652017-06-19 16:04:09 +03002048 node->setLine(loc);
2049
2050 return node;
2051}
2052
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002053void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
2054{
2055 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
2056 typeSpecifier->getBasicType());
2057
2058 if (mShaderVersion < 300 && typeSpecifier->array)
2059 {
2060 error(typeSpecifier->getLine(), "not supported", "first-class array");
2061 typeSpecifier->clearArrayness();
2062 }
2063}
2064
Martin Radev70866b82016-07-22 15:27:42 +03002065TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302066 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002067{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002068 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002069
Martin Radev70866b82016-07-22 15:27:42 +03002070 TPublicType returnType = typeSpecifier;
2071 returnType.qualifier = typeQualifier.qualifier;
2072 returnType.invariant = typeQualifier.invariant;
2073 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002074 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002075 returnType.precision = typeSpecifier.precision;
2076
2077 if (typeQualifier.precision != EbpUndefined)
2078 {
2079 returnType.precision = typeQualifier.precision;
2080 }
2081
Martin Radev4a9cd802016-09-01 16:51:51 +03002082 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2083 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002084
Martin Radev4a9cd802016-09-01 16:51:51 +03002085 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2086 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002087
Martin Radev4a9cd802016-09-01 16:51:51 +03002088 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002089
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002090 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002091 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002092 if (typeSpecifier.array)
2093 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002094 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002095 returnType.clearArrayness();
2096 }
2097
Martin Radev70866b82016-07-22 15:27:42 +03002098 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002099 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002100 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002101 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002102 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002103 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002104
Martin Radev70866b82016-07-22 15:27:42 +03002105 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002106 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002107 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002108 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002109 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002110 }
2111 }
2112 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002113 {
Martin Radev70866b82016-07-22 15:27:42 +03002114 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002115 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002116 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002117 }
Martin Radev70866b82016-07-22 15:27:42 +03002118 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2119 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002120 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002121 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2122 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002123 }
Martin Radev70866b82016-07-22 15:27:42 +03002124 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002125 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002126 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002127 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002128 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002129 }
2130
2131 return returnType;
2132}
2133
Olli Etuaho856c4972016-08-08 11:38:39 +03002134void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2135 const TPublicType &type,
2136 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002137{
2138 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002139 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002140 {
2141 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002142 }
2143
2144 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2145 switch (qualifier)
2146 {
2147 case EvqVertexIn:
2148 // ESSL 3.00 section 4.3.4
2149 if (type.array)
2150 {
2151 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002152 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002153 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002154 return;
2155 case EvqFragmentOut:
2156 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002157 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002158 {
2159 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002160 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002161 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002162 return;
2163 default:
2164 break;
2165 }
2166
2167 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2168 // restrictions.
2169 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002170 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2171 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002172 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2173 {
2174 error(qualifierLocation, "must use 'flat' interpolation here",
2175 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002176 }
2177
Martin Radev4a9cd802016-09-01 16:51:51 +03002178 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002179 {
2180 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2181 // These restrictions are only implied by the ESSL 3.00 spec, but
2182 // the ESSL 3.10 spec lists these restrictions explicitly.
2183 if (type.array)
2184 {
2185 error(qualifierLocation, "cannot be an array of structures",
2186 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002187 }
2188 if (type.isStructureContainingArrays())
2189 {
2190 error(qualifierLocation, "cannot be a structure containing an array",
2191 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002192 }
2193 if (type.isStructureContainingType(EbtStruct))
2194 {
2195 error(qualifierLocation, "cannot be a structure containing a structure",
2196 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002197 }
2198 if (type.isStructureContainingType(EbtBool))
2199 {
2200 error(qualifierLocation, "cannot be a structure containing a bool",
2201 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002202 }
2203 }
2204}
2205
Martin Radev2cc85b32016-08-05 16:22:53 +03002206void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2207{
2208 if (qualifier.getType() == QtStorage)
2209 {
2210 const TStorageQualifierWrapper &storageQualifier =
2211 static_cast<const TStorageQualifierWrapper &>(qualifier);
2212 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2213 !symbolTable.atGlobalLevel())
2214 {
2215 error(storageQualifier.getLine(),
2216 "Local variables can only use the const storage qualifier.",
2217 storageQualifier.getQualifierString().c_str());
2218 }
2219 }
2220}
2221
Olli Etuaho43364892017-02-13 16:00:12 +00002222void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002223 const TSourceLoc &location)
2224{
Jiajia Qinbc585152017-06-23 15:42:17 +08002225 const std::string reason(
2226 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2227 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002228 if (memoryQualifier.readonly)
2229 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002230 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002231 }
2232 if (memoryQualifier.writeonly)
2233 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002234 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002235 }
Martin Radev049edfa2016-11-11 14:35:37 +02002236 if (memoryQualifier.coherent)
2237 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002238 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002239 }
2240 if (memoryQualifier.restrictQualifier)
2241 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002242 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002243 }
2244 if (memoryQualifier.volatileQualifier)
2245 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002246 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002247 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002248}
2249
jchen104cdac9e2017-05-08 11:01:20 +08002250// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2251// intermediate tree.
2252void TParseContext::checkAtomicCounterOffsetIsNotOverlapped(TPublicType &publicType,
2253 size_t size,
2254 bool forceAppend,
2255 const TSourceLoc &loc,
2256 TType &type)
2257{
2258 auto &bindingState = mAtomicCounterBindingStates[publicType.layoutQualifier.binding];
2259 int offset;
2260 if (publicType.layoutQualifier.offset == -1 || forceAppend)
2261 {
2262 offset = bindingState.appendSpan(size);
2263 }
2264 else
2265 {
2266 offset = bindingState.insertSpan(publicType.layoutQualifier.offset, size);
2267 }
2268 if (offset == -1)
2269 {
2270 error(loc, "Offset overlapping", "atomic counter");
2271 return;
2272 }
2273 TLayoutQualifier qualifier = type.getLayoutQualifier();
2274 qualifier.offset = offset;
2275 type.setLayoutQualifier(qualifier);
2276}
2277
Olli Etuaho13389b62016-10-16 11:48:18 +01002278TIntermDeclaration *TParseContext::parseSingleDeclaration(
2279 TPublicType &publicType,
2280 const TSourceLoc &identifierOrTypeLocation,
2281 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002282{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002283 TType type(publicType);
2284 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2285 mDirectiveHandler.pragma().stdgl.invariantAll)
2286 {
2287 TQualifier qualifier = type.getQualifier();
2288
2289 // The directive handler has already taken care of rejecting invalid uses of this pragma
2290 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2291 // affected variable declarations:
2292 //
2293 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2294 // elsewhere, in TranslatorGLSL.)
2295 //
2296 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2297 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2298 // the way this is currently implemented we have to enable this compiler option before
2299 // parsing the shader and determining the shading language version it uses. If this were
2300 // implemented as a post-pass, the workaround could be more targeted.
2301 //
2302 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2303 // the specification, but there are desktop OpenGL drivers that expect that this is the
2304 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2305 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2306 {
2307 type.setInvariant(true);
2308 }
2309 }
2310
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002311 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2312 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002313
Olli Etuahobab4c082015-04-24 16:38:49 +03002314 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002315 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002316
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002317 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002318 if (emptyDeclaration)
2319 {
Martin Radevb8b01222016-11-20 23:25:53 +02002320 emptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002321 // In most cases we don't need to create a symbol node for an empty declaration.
2322 // But if the empty declaration is declaring a struct type, the symbol node will store that.
2323 if (type.getBasicType() == EbtStruct)
2324 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002325 symbol = new TIntermSymbol(0, "", type);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002326 }
jchen104cdac9e2017-05-08 11:01:20 +08002327 else if (IsAtomicCounter(publicType.getBasicType()))
2328 {
2329 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2330 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002331 }
2332 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002333 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002334 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002335
Olli Etuaho856c4972016-08-08 11:38:39 +03002336 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002337
jchen104cdac9e2017-05-08 11:01:20 +08002338 if (IsAtomicCounter(publicType.getBasicType()))
2339 {
2340
2341 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterSize, false,
2342 identifierOrTypeLocation, type);
2343 }
2344
Olli Etuaho2935c582015-04-08 14:32:06 +03002345 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002346 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002347
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002348 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002349 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002350 symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
Olli Etuaho13389b62016-10-16 11:48:18 +01002351 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002352 }
2353
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002354 TIntermDeclaration *declaration = new TIntermDeclaration();
2355 declaration->setLine(identifierOrTypeLocation);
2356 if (symbol)
2357 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002358 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002359 declaration->appendDeclarator(symbol);
2360 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002361 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002362}
2363
Olli Etuaho13389b62016-10-16 11:48:18 +01002364TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
2365 const TSourceLoc &identifierLocation,
2366 const TString &identifier,
2367 const TSourceLoc &indexLocation,
2368 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04002369{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002370 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002371
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002372 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2373 identifierLocation);
2374
2375 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002376
Olli Etuaho856c4972016-08-08 11:38:39 +03002377 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002378
Olli Etuaho8a176262016-08-16 14:23:01 +03002379 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002380
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002381 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002382
Olli Etuaho856c4972016-08-08 11:38:39 +03002383 unsigned int size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002384 // Make the type an array even if size check failed.
2385 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002386 arrayType.makeArray(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04002387
jchen104cdac9e2017-05-08 11:01:20 +08002388 if (IsAtomicCounter(publicType.getBasicType()))
2389 {
2390 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterArrayStride * size, false,
2391 identifierLocation, arrayType);
2392 }
2393
Olli Etuaho2935c582015-04-08 14:32:06 +03002394 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002395 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002396
Olli Etuaho13389b62016-10-16 11:48:18 +01002397 TIntermDeclaration *declaration = new TIntermDeclaration();
2398 declaration->setLine(identifierLocation);
2399
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002400 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002401 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002402 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2403 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002404 declaration->appendDeclarator(symbol);
2405 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002406
Olli Etuaho13389b62016-10-16 11:48:18 +01002407 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002408}
2409
Olli Etuaho13389b62016-10-16 11:48:18 +01002410TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2411 const TSourceLoc &identifierLocation,
2412 const TString &identifier,
2413 const TSourceLoc &initLocation,
2414 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002415{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002416 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002417
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002418 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2419 identifierLocation);
2420
2421 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002422
Olli Etuaho13389b62016-10-16 11:48:18 +01002423 TIntermDeclaration *declaration = new TIntermDeclaration();
2424 declaration->setLine(identifierLocation);
2425
2426 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002427 if (executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002428 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002429 if (initNode)
2430 {
2431 declaration->appendDeclarator(initNode);
2432 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002433 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002434 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002435}
2436
Olli Etuaho13389b62016-10-16 11:48:18 +01002437TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Jamie Madillb98c3a82015-07-23 14:26:04 -04002438 TPublicType &publicType,
2439 const TSourceLoc &identifierLocation,
2440 const TString &identifier,
2441 const TSourceLoc &indexLocation,
2442 TIntermTyped *indexExpression,
2443 const TSourceLoc &initLocation,
2444 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002445{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002446 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002447
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002448 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2449 identifierLocation);
2450
2451 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002452
Olli Etuaho8a176262016-08-16 14:23:01 +03002453 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002454
2455 TPublicType arrayType(publicType);
2456
Olli Etuaho856c4972016-08-08 11:38:39 +03002457 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002458 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2459 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002460 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002461 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002462 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002463 }
2464 // Make the type an array even if size check failed.
2465 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2466 arrayType.setArraySize(size);
2467
Olli Etuaho13389b62016-10-16 11:48:18 +01002468 TIntermDeclaration *declaration = new TIntermDeclaration();
2469 declaration->setLine(identifierLocation);
2470
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002471 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002472 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002473 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002474 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002475 if (initNode)
2476 {
2477 declaration->appendDeclarator(initNode);
2478 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002479 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002480
2481 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002482}
2483
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002484TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002485 const TTypeQualifierBuilder &typeQualifierBuilder,
2486 const TSourceLoc &identifierLoc,
2487 const TString *identifier,
2488 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002489{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002490 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002491
Martin Radev70866b82016-07-22 15:27:42 +03002492 if (!typeQualifier.invariant)
2493 {
2494 error(identifierLoc, "Expected invariant", identifier->c_str());
2495 return nullptr;
2496 }
2497 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2498 {
2499 return nullptr;
2500 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002501 if (!symbol)
2502 {
2503 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002504 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002505 }
Martin Radev70866b82016-07-22 15:27:42 +03002506 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002507 {
Martin Radev70866b82016-07-22 15:27:42 +03002508 error(identifierLoc, "invariant declaration specifies qualifier",
2509 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002510 }
Martin Radev70866b82016-07-22 15:27:42 +03002511 if (typeQualifier.precision != EbpUndefined)
2512 {
2513 error(identifierLoc, "invariant declaration specifies precision",
2514 getPrecisionString(typeQualifier.precision));
2515 }
2516 if (!typeQualifier.layoutQualifier.isEmpty())
2517 {
2518 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2519 }
2520
2521 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002522 if (!variable)
2523 {
2524 return nullptr;
2525 }
Martin Radev70866b82016-07-22 15:27:42 +03002526 const TType &type = variable->getType();
2527
2528 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2529 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002530 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002531
2532 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2533
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002534 TIntermSymbol *intermSymbol = new TIntermSymbol(variable->getUniqueId(), *identifier, type);
2535 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002536
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002537 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002538}
2539
Olli Etuaho13389b62016-10-16 11:48:18 +01002540void TParseContext::parseDeclarator(TPublicType &publicType,
2541 const TSourceLoc &identifierLocation,
2542 const TString &identifier,
2543 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002544{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002545 // If the declaration starting this declarator list was empty (example: int,), some checks were
2546 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002547 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002548 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002549 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2550 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002551 }
2552
Olli Etuaho856c4972016-08-08 11:38:39 +03002553 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002554
Olli Etuaho856c4972016-08-08 11:38:39 +03002555 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002556
Olli Etuaho2935c582015-04-08 14:32:06 +03002557 TVariable *variable = nullptr;
Olli Etuaho43364892017-02-13 16:00:12 +00002558 TType type(publicType);
jchen104cdac9e2017-05-08 11:01:20 +08002559 if (IsAtomicCounter(publicType.getBasicType()))
2560 {
2561 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterSize, true,
2562 identifierLocation, type);
2563 }
Olli Etuaho43364892017-02-13 16:00:12 +00002564 declareVariable(identifierLocation, identifier, type, &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002565
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002566 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002567 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002568 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
2569 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002570 declarationOut->appendDeclarator(symbol);
2571 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002572}
2573
Olli Etuaho13389b62016-10-16 11:48:18 +01002574void TParseContext::parseArrayDeclarator(TPublicType &publicType,
2575 const TSourceLoc &identifierLocation,
2576 const TString &identifier,
2577 const TSourceLoc &arrayLocation,
2578 TIntermTyped *indexExpression,
2579 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002580{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002581 // If the declaration starting this declarator list was empty (example: int,), some checks were
2582 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002583 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002584 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002585 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2586 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002587 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002588
Olli Etuaho856c4972016-08-08 11:38:39 +03002589 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002590
Olli Etuaho856c4972016-08-08 11:38:39 +03002591 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002592
Olli Etuaho8a176262016-08-16 14:23:01 +03002593 if (checkIsValidTypeAndQualifierForArray(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002594 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002595 TType arrayType = TType(publicType);
Olli Etuaho856c4972016-08-08 11:38:39 +03002596 unsigned int size = checkIsValidArraySize(arrayLocation, indexExpression);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002597 arrayType.makeArray(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002598
jchen104cdac9e2017-05-08 11:01:20 +08002599 if (IsAtomicCounter(publicType.getBasicType()))
2600 {
2601 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterArrayStride * size,
2602 true, identifierLocation, arrayType);
2603 }
2604
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002605 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002606 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002607
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002608 if (variable)
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002609 {
2610 TIntermSymbol *symbol =
2611 new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2612 symbol->setLine(identifierLocation);
2613 declarationOut->appendDeclarator(symbol);
2614 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002615 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002616}
2617
Olli Etuaho13389b62016-10-16 11:48:18 +01002618void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2619 const TSourceLoc &identifierLocation,
2620 const TString &identifier,
2621 const TSourceLoc &initLocation,
2622 TIntermTyped *initializer,
2623 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002624{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002625 // If the declaration starting this declarator list was empty (example: int,), some checks were
2626 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002627 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002628 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002629 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2630 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002631 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002632
Olli Etuaho856c4972016-08-08 11:38:39 +03002633 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002634
Olli Etuaho13389b62016-10-16 11:48:18 +01002635 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002636 if (executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002637 {
2638 //
2639 // build the intermediate representation
2640 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002641 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002642 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002643 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002644 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002645 }
2646}
2647
Olli Etuaho13389b62016-10-16 11:48:18 +01002648void TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
2649 const TSourceLoc &identifierLocation,
2650 const TString &identifier,
2651 const TSourceLoc &indexLocation,
2652 TIntermTyped *indexExpression,
2653 const TSourceLoc &initLocation,
2654 TIntermTyped *initializer,
2655 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002656{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002657 // If the declaration starting this declarator list was empty (example: int,), some checks were
2658 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002659 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002660 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002661 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2662 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002663 }
2664
Olli Etuaho856c4972016-08-08 11:38:39 +03002665 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002666
Olli Etuaho8a176262016-08-16 14:23:01 +03002667 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002668
2669 TPublicType arrayType(publicType);
2670
Olli Etuaho856c4972016-08-08 11:38:39 +03002671 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002672 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2673 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002674 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002675 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002676 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002677 }
2678 // Make the type an array even if size check failed.
2679 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2680 arrayType.setArraySize(size);
2681
2682 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002683 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002684 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002685 {
2686 if (initNode)
2687 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002688 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002689 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002690 }
2691}
2692
jchen104cdac9e2017-05-08 11:01:20 +08002693void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2694 const TSourceLoc &location)
2695{
2696 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2697 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2698 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2699 {
2700 error(location, "Requires both binding and offset", "layout");
2701 return;
2702 }
2703 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2704}
2705
Olli Etuahocce89652017-06-19 16:04:09 +03002706void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2707 const TPublicType &type,
2708 const TSourceLoc &loc)
2709{
2710 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2711 !getFragmentPrecisionHigh())
2712 {
2713 error(loc, "precision is not supported in fragment shader", "highp");
2714 }
2715
2716 if (!CanSetDefaultPrecisionOnType(type))
2717 {
2718 error(loc, "illegal type argument for default precision qualifier",
2719 getBasicString(type.getBasicType()));
2720 return;
2721 }
2722 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2723}
2724
Shaob5cc1192017-07-06 10:47:20 +08002725bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2726{
2727 switch (typeQualifier.layoutQualifier.primitiveType)
2728 {
2729 case EptLines:
2730 case EptLinesAdjacency:
2731 case EptTriangles:
2732 case EptTrianglesAdjacency:
2733 return typeQualifier.qualifier == EvqGeometryIn;
2734
2735 case EptLineStrip:
2736 case EptTriangleStrip:
2737 return typeQualifier.qualifier == EvqGeometryOut;
2738
2739 case EptPoints:
2740 return true;
2741
2742 default:
2743 UNREACHABLE();
2744 return false;
2745 }
2746}
2747
Jiawei Shaod8105a02017-08-08 09:54:36 +08002748void TParseContext::setGeometryShaderInputArraySizes()
2749{
2750 // TODO(jiawei.shao@intel.com): check former input array sizes match the input primitive
2751 // declaration.
2752 ASSERT(mGeometryShaderInputArraySize == 0);
2753 mGeometryShaderInputArraySize =
2754 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType);
2755}
2756
Shaob5cc1192017-07-06 10:47:20 +08002757bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2758{
2759 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2760
2761 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2762
2763 if (layoutQualifier.maxVertices != -1)
2764 {
2765 error(typeQualifier.line,
2766 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2767 return false;
2768 }
2769
2770 // Set mGeometryInputPrimitiveType if exists
2771 if (layoutQualifier.primitiveType != EptUndefined)
2772 {
2773 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2774 {
2775 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2776 return false;
2777 }
2778
2779 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2780 {
2781 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shaod8105a02017-08-08 09:54:36 +08002782 setGeometryShaderInputArraySizes();
Shaob5cc1192017-07-06 10:47:20 +08002783 }
2784 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2785 {
2786 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2787 "layout");
2788 return false;
2789 }
2790 }
2791
2792 // Set mGeometryInvocations if exists
2793 if (layoutQualifier.invocations > 0)
2794 {
2795 if (mGeometryShaderInvocations == 0)
2796 {
2797 mGeometryShaderInvocations = layoutQualifier.invocations;
2798 }
2799 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2800 {
2801 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2802 "layout");
2803 return false;
2804 }
2805 }
2806
2807 return true;
2808}
2809
2810bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2811{
2812 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2813
2814 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2815
2816 if (layoutQualifier.invocations > 0)
2817 {
2818 error(typeQualifier.line,
2819 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2820 return false;
2821 }
2822
2823 // Set mGeometryOutputPrimitiveType if exists
2824 if (layoutQualifier.primitiveType != EptUndefined)
2825 {
2826 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2827 {
2828 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2829 return false;
2830 }
2831
2832 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2833 {
2834 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2835 }
2836 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2837 {
2838 error(typeQualifier.line,
2839 "primitive doesn't match earlier output primitive declaration", "layout");
2840 return false;
2841 }
2842 }
2843
2844 // Set mGeometryMaxVertices if exists
2845 if (layoutQualifier.maxVertices > -1)
2846 {
2847 if (mGeometryShaderMaxVertices == -1)
2848 {
2849 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2850 }
2851 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2852 {
2853 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2854 "layout");
2855 return false;
2856 }
2857 }
2858
2859 return true;
2860}
2861
Martin Radev70866b82016-07-22 15:27:42 +03002862void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002863{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002864 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002865 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002866
Martin Radev70866b82016-07-22 15:27:42 +03002867 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2868 typeQualifier.line);
2869
Jamie Madillc2128ff2016-07-04 10:26:17 -04002870 // It should never be the case, but some strange parser errors can send us here.
2871 if (layoutQualifier.isEmpty())
2872 {
2873 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002874 return;
2875 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002876
Martin Radev802abe02016-08-04 17:48:32 +03002877 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002878 {
Olli Etuaho43364892017-02-13 16:00:12 +00002879 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002880 return;
2881 }
2882
Olli Etuaho43364892017-02-13 16:00:12 +00002883 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2884
2885 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002886
2887 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2888
Andrei Volykhina5527072017-03-22 16:46:30 +03002889 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
2890
jchen104cdac9e2017-05-08 11:01:20 +08002891 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
2892
Martin Radev802abe02016-08-04 17:48:32 +03002893 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002894 {
Martin Radev802abe02016-08-04 17:48:32 +03002895 if (mComputeShaderLocalSizeDeclared &&
2896 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2897 {
2898 error(typeQualifier.line, "Work group size does not match the previous declaration",
2899 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002900 return;
2901 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002902
Martin Radev802abe02016-08-04 17:48:32 +03002903 if (mShaderVersion < 310)
2904 {
2905 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002906 return;
2907 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002908
Martin Radev4c4c8e72016-08-04 12:25:34 +03002909 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03002910 {
2911 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002912 return;
2913 }
2914
2915 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
2916 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
2917
2918 const TConstantUnion *maxComputeWorkGroupSizeData =
2919 maxComputeWorkGroupSize->getConstPointer();
2920
2921 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
2922 {
2923 if (layoutQualifier.localSize[i] != -1)
2924 {
2925 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
2926 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
2927 if (mComputeShaderLocalSize[i] < 1 ||
2928 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
2929 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002930 std::stringstream reasonStream;
2931 reasonStream << "invalid value: Value must be at least 1 and no greater than "
2932 << maxComputeWorkGroupSizeValue;
2933 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03002934
Olli Etuaho4de340a2016-12-16 09:32:03 +00002935 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03002936 return;
2937 }
2938 }
2939 }
2940
2941 mComputeShaderLocalSizeDeclared = true;
2942 }
Shaob5cc1192017-07-06 10:47:20 +08002943 else if (typeQualifier.qualifier == EvqGeometryIn)
2944 {
2945 if (mShaderVersion < 310)
2946 {
2947 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
2948 return;
2949 }
2950
2951 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
2952 {
2953 return;
2954 }
2955 }
2956 else if (typeQualifier.qualifier == EvqGeometryOut)
2957 {
2958 if (mShaderVersion < 310)
2959 {
2960 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
2961 "layout");
2962 return;
2963 }
2964
2965 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
2966 {
2967 return;
2968 }
2969 }
Olli Etuaho95468d12017-05-04 11:14:34 +03002970 else if (isMultiviewExtensionEnabled() && typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00002971 {
2972 // This error is only specified in WebGL, but tightens unspecified behavior in the native
2973 // specification.
2974 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
2975 {
2976 error(typeQualifier.line, "Number of views does not match the previous declaration",
2977 "layout");
2978 return;
2979 }
2980
2981 if (layoutQualifier.numViews == -1)
2982 {
2983 error(typeQualifier.line, "No num_views specified", "layout");
2984 return;
2985 }
2986
2987 if (layoutQualifier.numViews > mMaxNumViews)
2988 {
2989 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
2990 "layout");
2991 return;
2992 }
2993
2994 mNumViews = layoutQualifier.numViews;
2995 }
Martin Radev802abe02016-08-04 17:48:32 +03002996 else
Jamie Madill1566ef72013-06-20 11:55:54 -04002997 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00002998 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03002999 {
Martin Radev802abe02016-08-04 17:48:32 +03003000 return;
3001 }
3002
Jiajia Qinbc585152017-06-23 15:42:17 +08003003 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03003004 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003005 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003006 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03003007 return;
3008 }
3009
3010 if (mShaderVersion < 300)
3011 {
3012 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
3013 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003014 return;
3015 }
3016
Olli Etuaho09b04a22016-12-15 13:30:26 +00003017 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003018
3019 if (layoutQualifier.matrixPacking != EmpUnspecified)
3020 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003021 if (typeQualifier.qualifier == EvqUniform)
3022 {
3023 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3024 }
3025 else if (typeQualifier.qualifier == EvqBuffer)
3026 {
3027 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3028 }
Martin Radev802abe02016-08-04 17:48:32 +03003029 }
3030
3031 if (layoutQualifier.blockStorage != EbsUnspecified)
3032 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003033 if (typeQualifier.qualifier == EvqUniform)
3034 {
3035 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3036 }
3037 else if (typeQualifier.qualifier == EvqBuffer)
3038 {
3039 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3040 }
Martin Radev802abe02016-08-04 17:48:32 +03003041 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003042 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003043}
3044
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003045TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3046 const TFunction &function,
3047 const TSourceLoc &location,
3048 bool insertParametersToSymbolTable)
3049{
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003050 checkIsNotReserved(location, function.getName());
3051
Olli Etuahofe486322017-03-21 09:30:54 +00003052 TIntermFunctionPrototype *prototype =
3053 new TIntermFunctionPrototype(function.getReturnType(), TSymbolUniqueId(function));
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003054 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
3055 // point to the data that already exists in the symbol table.
3056 prototype->getFunctionSymbolInfo()->setFromFunction(function);
3057 prototype->setLine(location);
3058
3059 for (size_t i = 0; i < function.getParamCount(); i++)
3060 {
3061 const TConstParameter &param = function.getParam(i);
3062
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003063 TIntermSymbol *symbol = nullptr;
3064
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003065 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3066 // be used for unused args).
3067 if (param.name != nullptr)
3068 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003069 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003070 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003071 {
Olli Etuaho0f684632017-07-13 12:42:15 +03003072 TVariable *variable = symbolTable.declareVariable(param.name, *param.type);
3073 if (variable)
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003074 {
3075 symbol = new TIntermSymbol(variable->getUniqueId(), variable->getName(),
3076 variable->getType());
3077 }
3078 else
3079 {
Olli Etuaho85d624a2017-08-07 13:42:33 +03003080 error(location, "redefinition", param.name->c_str());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003081 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003082 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003083 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003084 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003085 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003086 // The parameter had no name or declaring the symbol failed - either way, add a nameless
3087 // symbol.
3088 symbol = new TIntermSymbol(0, "", *param.type);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003089 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003090 symbol->setLine(location);
3091 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003092 }
3093 return prototype;
3094}
3095
Olli Etuaho16c745a2017-01-16 17:02:27 +00003096TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3097 const TFunction &parsedFunction,
3098 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003099{
Olli Etuaho476197f2016-10-11 13:59:08 +01003100 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3101 // first declaration. Either way the instance in the symbol table is used to track whether the
3102 // function is declared multiple times.
3103 TFunction *function = static_cast<TFunction *>(
3104 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
3105 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003106 {
3107 // ESSL 1.00.17 section 4.2.7.
3108 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3109 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003110 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003111 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02003112
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003113 TIntermFunctionPrototype *prototype =
3114 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003115
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003116 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003117
3118 if (!symbolTable.atGlobalLevel())
3119 {
3120 // ESSL 3.00.4 section 4.2.4.
3121 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003122 }
3123
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003124 return prototype;
3125}
3126
Olli Etuaho336b1472016-10-05 16:37:55 +01003127TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003128 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003129 TIntermBlock *functionBody,
3130 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003131{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003132 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003133 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3134 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003135 error(location, "function does not return a value:",
3136 functionPrototype->getFunctionSymbolInfo()->getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003137 }
3138
Olli Etuahof51fdd22016-10-03 10:03:40 +01003139 if (functionBody == nullptr)
3140 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003141 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003142 functionBody->setLine(location);
3143 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003144 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003145 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003146 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003147
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003148 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003149 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003150}
3151
Olli Etuaho476197f2016-10-11 13:59:08 +01003152void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
3153 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003154 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003155{
Olli Etuaho476197f2016-10-11 13:59:08 +01003156 ASSERT(function);
3157 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003158 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01003159 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003160
3161 if (builtIn)
3162 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003163 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003164 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003165 else
Jamie Madill185fb402015-06-12 15:48:48 -04003166 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003167 TFunction *prevDec = static_cast<TFunction *>(
3168 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
3169
3170 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
3171 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
3172 // occurance.
3173 if (*function != prevDec)
3174 {
3175 // Swap the parameters of the previous declaration to the parameters of the function
3176 // definition (parameter names may differ).
3177 prevDec->swapParameters(**function);
3178
3179 // The function definition will share the same symbol as any previous declaration.
3180 *function = prevDec;
3181 }
3182
3183 if ((*function)->isDefined())
3184 {
3185 error(location, "function already has a body", (*function)->getName().c_str());
3186 }
3187
3188 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04003189 }
Jamie Madill185fb402015-06-12 15:48:48 -04003190
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003191 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01003192 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003193 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003194
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003195 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003196 setLoopNestingLevel(0);
3197}
3198
Jamie Madillb98c3a82015-07-23 14:26:04 -04003199TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003200{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003201 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003202 // We don't know at this point whether this is a function definition or a prototype.
3203 // The definition production code will check for redefinitions.
3204 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003205 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003206 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3207 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003208 //
3209 TFunction *prevDec =
3210 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303211
Martin Radevda6254b2016-12-14 17:00:36 +02003212 if (getShaderVersion() >= 300 &&
3213 symbolTable.hasUnmangledBuiltInForShaderVersion(function->getName().c_str(),
3214 getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303215 {
Martin Radevda6254b2016-12-14 17:00:36 +02003216 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303217 // Therefore overloading or redefining builtin functions is an error.
3218 error(location, "Name of a built-in function cannot be redeclared as function",
3219 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303220 }
3221 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003222 {
3223 if (prevDec->getReturnType() != function->getReturnType())
3224 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003225 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003226 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003227 }
3228 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3229 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003230 if (prevDec->getParam(i).type->getQualifier() !=
3231 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003232 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003233 error(location,
3234 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003235 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003236 }
3237 }
3238 }
3239
3240 //
3241 // Check for previously declared variables using the same name.
3242 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003243 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003244 if (prevSym)
3245 {
3246 if (!prevSym->isFunction())
3247 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003248 error(location, "redefinition of a function", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003249 }
3250 }
3251 else
3252 {
3253 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01003254 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003255 }
3256
3257 // We're at the inner scope level of the function's arguments and body statement.
3258 // Add the function prototype to the surrounding scope instead.
3259 symbolTable.getOuterLevel()->insert(function);
3260
Olli Etuaho78d13742017-01-18 13:06:10 +00003261 // Raise error message if main function takes any parameters or return anything other than void
3262 if (function->getName() == "main")
3263 {
3264 if (function->getParamCount() > 0)
3265 {
3266 error(location, "function cannot take any parameter(s)", "main");
3267 }
3268 if (function->getReturnType().getBasicType() != EbtVoid)
3269 {
3270 error(location, "main function cannot return a value",
3271 function->getReturnType().getBasicString());
3272 }
3273 }
3274
Jamie Madill185fb402015-06-12 15:48:48 -04003275 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003276 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3277 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003278 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3279 //
3280 return function;
3281}
3282
Olli Etuaho9de84a52016-06-14 17:36:01 +03003283TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
3284 const TString *name,
3285 const TSourceLoc &location)
3286{
3287 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3288 {
3289 error(location, "no qualifiers allowed for function return",
3290 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003291 }
3292 if (!type.layoutQualifier.isEmpty())
3293 {
3294 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003295 }
jchen10cc2a10e2017-05-03 14:05:12 +08003296 // make sure an opaque type is not involved as well...
3297 std::string reason(getBasicString(type.getBasicType()));
3298 reason += "s can't be function return values";
3299 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003300 if (mShaderVersion < 300)
3301 {
3302 // Array return values are forbidden, but there's also no valid syntax for declaring array
3303 // return values in ESSL 1.00.
Olli Etuaho77ba4082016-12-16 12:01:18 +00003304 ASSERT(type.arraySize == 0 || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003305
3306 if (type.isStructureContainingArrays())
3307 {
3308 // ESSL 1.00.17 section 6.1 Function Definitions
3309 error(location, "structures containing arrays can't be function return values",
3310 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003311 }
3312 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003313
3314 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003315 return new TFunction(&symbolTable, name, new TType(type));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003316}
3317
Olli Etuahocce89652017-06-19 16:04:09 +03003318TFunction *TParseContext::addNonConstructorFunc(const TString *name, const TSourceLoc &loc)
3319{
Olli Etuahocce89652017-06-19 16:04:09 +03003320 const TType *returnType = TCache::getType(EbtVoid, EbpUndefined);
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003321 return new TFunction(&symbolTable, name, returnType);
Olli Etuahocce89652017-06-19 16:04:09 +03003322}
3323
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003324TFunction *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003325{
Olli Etuahocce89652017-06-19 16:04:09 +03003326 if (mShaderVersion < 300 && publicType.array)
3327 {
3328 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3329 "[]");
3330 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003331 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003332 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003333 error(publicType.getLine(), "constructor can't be a structure definition",
3334 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003335 }
3336
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003337 TType *type = new TType(publicType);
3338 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003339 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003340 error(publicType.getLine(), "cannot construct this type",
3341 getBasicString(publicType.getBasicType()));
3342 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003343 }
3344
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003345 return new TFunction(&symbolTable, nullptr, type, EOpConstruct);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003346}
3347
Olli Etuahocce89652017-06-19 16:04:09 +03003348TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3349 const TString *name,
3350 const TSourceLoc &nameLoc)
3351{
3352 if (publicType.getBasicType() == EbtVoid)
3353 {
3354 error(nameLoc, "illegal use of type 'void'", name->c_str());
3355 }
3356 checkIsNotReserved(nameLoc, *name);
3357 TType *type = new TType(publicType);
3358 TParameter param = {name, type};
3359 return param;
3360}
3361
3362TParameter TParseContext::parseParameterArrayDeclarator(const TString *identifier,
3363 const TSourceLoc &identifierLoc,
3364 TIntermTyped *arraySize,
3365 const TSourceLoc &arrayLoc,
3366 TPublicType *type)
3367{
Olli Etuahoe0803872017-08-23 15:30:23 +03003368 checkArrayElementIsNotArray(arrayLoc, *type);
Olli Etuahocce89652017-06-19 16:04:09 +03003369 unsigned int size = checkIsValidArraySize(arrayLoc, arraySize);
3370 type->setArraySize(size);
3371 return parseParameterDeclarator(*type, identifier, identifierLoc);
3372}
3373
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003374bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(TIntermSequence *arguments,
3375 TType type,
3376 const TSourceLoc &line)
3377{
3378 if (arguments->empty())
3379 {
3380 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3381 return false;
3382 }
3383 for (TIntermNode *arg : *arguments)
3384 {
3385 TIntermTyped *element = arg->getAsTyped();
3386 ASSERT(element);
3387 size_t dimensionalityFromElement = element->getType().getArraySizes().size() + 1u;
3388 if (dimensionalityFromElement > type.getArraySizes().size())
3389 {
3390 error(line, "constructing from a non-dereferenced array", "constructor");
3391 return false;
3392 }
3393 else if (dimensionalityFromElement < type.getArraySizes().size())
3394 {
3395 if (dimensionalityFromElement == 1u)
3396 {
3397 error(line, "implicitly sized array of arrays constructor argument is not an array",
3398 "constructor");
3399 }
3400 else
3401 {
3402 error(line,
3403 "implicitly sized array of arrays constructor argument dimensionality is too "
3404 "low",
3405 "constructor");
3406 }
3407 return false;
3408 }
3409 }
3410 return true;
3411}
3412
Jamie Madillb98c3a82015-07-23 14:26:04 -04003413// This function is used to test for the correctness of the parameters passed to various constructor
3414// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003415//
Olli Etuaho856c4972016-08-08 11:38:39 +03003416// 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 +00003417//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003418TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00003419 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303420 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003421{
Olli Etuaho856c4972016-08-08 11:38:39 +03003422 if (type.isUnsizedArray())
3423 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003424 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003425 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003426 type.sizeUnsizedArrays(TVector<unsigned int>());
Olli Etuaho3ec75682017-07-05 17:02:55 +03003427 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003428 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003429 TIntermTyped *firstElement = arguments->at(0)->getAsTyped();
3430 ASSERT(firstElement);
3431 type.setArraySize(type.getArraySizes().size() - 1u,
3432 static_cast<unsigned int>(arguments->size()));
3433 for (size_t i = 0; i < firstElement->getType().getArraySizes().size(); ++i)
3434 {
3435 if (type.getArraySizes()[i] == 0u)
3436 {
3437 type.setArraySize(i, firstElement->getType().getArraySizes().at(i));
3438 }
3439 }
3440 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003441 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003442
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003443 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003444 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003445 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003446 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003447
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003448 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003449 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003450
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003451 // TODO(oetuaho@nvidia.com): Add support for folding array constructors.
3452 if (!constructorNode->isArray())
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003453 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003454 return constructorNode->fold(mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003455 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003456 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003457}
3458
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003459//
3460// Interface/uniform blocks
Jiawei Shaod8105a02017-08-08 09:54:36 +08003461// TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003462//
Olli Etuaho13389b62016-10-16 11:48:18 +01003463TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003464 const TTypeQualifierBuilder &typeQualifierBuilder,
3465 const TSourceLoc &nameLine,
3466 const TString &blockName,
3467 TFieldList *fieldList,
3468 const TString *instanceName,
3469 const TSourceLoc &instanceLine,
3470 TIntermTyped *arrayIndex,
3471 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003472{
Olli Etuaho856c4972016-08-08 11:38:39 +03003473 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003474
Olli Etuaho77ba4082016-12-16 12:01:18 +00003475 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003476
Jiajia Qinbc585152017-06-23 15:42:17 +08003477 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003478 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003479 error(typeQualifier.line,
3480 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3481 "3.10",
3482 getQualifierString(typeQualifier.qualifier));
3483 }
3484 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3485 {
3486 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003487 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003488 }
3489
Martin Radev70866b82016-07-22 15:27:42 +03003490 if (typeQualifier.invariant)
3491 {
3492 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3493 }
3494
Jiajia Qinbc585152017-06-23 15:42:17 +08003495 if (typeQualifier.qualifier != EvqBuffer)
3496 {
3497 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3498 }
Olli Etuaho43364892017-02-13 16:00:12 +00003499
jchen10af713a22017-04-19 09:10:56 +08003500 // add array index
3501 unsigned int arraySize = 0;
3502 if (arrayIndex != nullptr)
3503 {
3504 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3505 }
3506
3507 if (mShaderVersion < 310)
3508 {
3509 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3510 }
3511 else
3512 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003513 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3514 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003515 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003516
Andrei Volykhina5527072017-03-22 16:46:30 +03003517 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3518
Jamie Madill099c0f32013-06-20 11:55:52 -04003519 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003520 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003521
Jamie Madill099c0f32013-06-20 11:55:52 -04003522 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3523 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003524 if (typeQualifier.qualifier == EvqUniform)
3525 {
3526 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3527 }
3528 else if (typeQualifier.qualifier == EvqBuffer)
3529 {
3530 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3531 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003532 }
3533
Jamie Madill1566ef72013-06-20 11:55:54 -04003534 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3535 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003536 if (typeQualifier.qualifier == EvqUniform)
3537 {
3538 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3539 }
3540 else if (typeQualifier.qualifier == EvqBuffer)
3541 {
3542 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3543 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003544 }
3545
Olli Etuaho856c4972016-08-08 11:38:39 +03003546 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003547
Martin Radev2cc85b32016-08-05 16:22:53 +03003548 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3549
Olli Etuaho0f684632017-07-13 12:42:15 +03003550 if (!symbolTable.declareInterfaceBlockName(&blockName))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303551 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003552 error(nameLine, "redefinition of an interface block name", blockName.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003553 }
3554
Jamie Madill98493dd2013-07-08 14:39:03 -04003555 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303556 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3557 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003558 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303559 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003560 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303561 {
jchen10cc2a10e2017-05-03 14:05:12 +08003562 std::string reason("unsupported type - ");
3563 reason += fieldType->getBasicString();
3564 reason += " types are not allowed in interface blocks";
3565 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003566 }
3567
Jamie Madill98493dd2013-07-08 14:39:03 -04003568 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003569 switch (qualifier)
3570 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003571 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003572 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003573 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003574 if (typeQualifier.qualifier == EvqBuffer)
3575 {
3576 error(field->line(), "invalid qualifier on shader storage block member",
3577 getQualifierString(qualifier));
3578 }
3579 break;
3580 case EvqBuffer:
3581 if (typeQualifier.qualifier == EvqUniform)
3582 {
3583 error(field->line(), "invalid qualifier on uniform block member",
3584 getQualifierString(qualifier));
3585 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003586 break;
3587 default:
3588 error(field->line(), "invalid qualifier on interface block member",
3589 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003590 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003591 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003592
Martin Radev70866b82016-07-22 15:27:42 +03003593 if (fieldType->isInvariant())
3594 {
3595 error(field->line(), "invalid qualifier on interface block member", "invariant");
3596 }
3597
Jamie Madilla5efff92013-06-06 11:56:47 -04003598 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003599 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003600 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003601 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003602
Jamie Madill98493dd2013-07-08 14:39:03 -04003603 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003604 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003605 error(field->line(), "invalid layout qualifier: cannot be used here",
3606 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003607 }
3608
Jamie Madill98493dd2013-07-08 14:39:03 -04003609 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003610 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003611 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003612 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003613 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003614 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003615 warning(field->line(),
3616 "extraneous layout qualifier: only has an effect on matrix types",
3617 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003618 }
3619
Jamie Madill98493dd2013-07-08 14:39:03 -04003620 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003621
3622 if (typeQualifier.qualifier == EvqBuffer)
3623 {
3624 // set memory qualifiers
3625 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3626 // qualified with a memory qualifier, it is as if all of its members were declared with
3627 // the same memory qualifier.
3628 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3629 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3630 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3631 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3632 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3633 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3634 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3635 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3636 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3637 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3638 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003639 }
3640
Jamie Madillb98c3a82015-07-23 14:26:04 -04003641 TInterfaceBlock *interfaceBlock =
Shaob18c33e2017-08-16 12:37:51 +08003642 new TInterfaceBlock(&blockName, fieldList, instanceName, blockLayoutQualifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003643 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
3644 if (arrayIndex != nullptr)
3645 {
3646 interfaceBlockType.makeArray(arraySize);
3647 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003648
3649 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04003650 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003651
Jamie Madill98493dd2013-07-08 14:39:03 -04003652 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003653 {
3654 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003655 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3656 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003657 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303658 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04003659
3660 // set parent pointer of the field variable
3661 fieldType->setInterfaceBlock(interfaceBlock);
3662
Olli Etuaho0f684632017-07-13 12:42:15 +03003663 TVariable *fieldVariable = symbolTable.declareVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04003664
Olli Etuaho0f684632017-07-13 12:42:15 +03003665 if (fieldVariable)
3666 {
3667 fieldVariable->setQualifier(typeQualifier.qualifier);
3668 }
3669 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303670 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003671 error(field->line(), "redefinition of an interface block member name",
3672 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003673 }
3674 }
3675 }
3676 else
3677 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003678 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003679
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003680 // add a symbol for this interface block
Olli Etuaho0f684632017-07-13 12:42:15 +03003681 TVariable *instanceTypeDef = symbolTable.declareVariable(instanceName, interfaceBlockType);
3682 if (instanceTypeDef)
3683 {
3684 instanceTypeDef->setQualifier(typeQualifier.qualifier);
3685 symbolId = instanceTypeDef->getUniqueId();
3686 }
3687 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303688 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003689 error(instanceLine, "redefinition of an interface block instance name",
3690 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003691 }
Olli Etuaho0f684632017-07-13 12:42:15 +03003692 symbolName = *instanceName;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003693 }
3694
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003695 TIntermSymbol *blockSymbol = new TIntermSymbol(symbolId, symbolName, interfaceBlockType);
3696 blockSymbol->setLine(typeQualifier.line);
Olli Etuaho13389b62016-10-16 11:48:18 +01003697 TIntermDeclaration *declaration = new TIntermDeclaration();
3698 declaration->appendDeclarator(blockSymbol);
3699 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003700
3701 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003702 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003703}
3704
Olli Etuaho383b7912016-08-05 11:22:59 +03003705void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003706{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003707 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003708
3709 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003710 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303711 if (mStructNestingLevel > 1)
3712 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003713 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003714 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003715}
3716
3717void TParseContext::exitStructDeclaration()
3718{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003719 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003720}
3721
Olli Etuaho8a176262016-08-16 14:23:01 +03003722void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003723{
Jamie Madillacb4b812016-11-07 13:50:29 -05003724 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303725 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003726 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003727 }
3728
Arun Patole7e7e68d2015-05-22 12:02:25 +05303729 if (field.type()->getBasicType() != EbtStruct)
3730 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003731 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003732 }
3733
3734 // We're already inside a structure definition at this point, so add
3735 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303736 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3737 {
Jamie Madill41a49272014-03-18 16:10:13 -04003738 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003739 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
3740 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003741 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003742 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003743 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003744 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003745}
3746
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003747//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003748// Parse an array index expression
3749//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003750TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3751 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303752 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003753{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003754 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3755 {
3756 if (baseExpression->getAsSymbolNode())
3757 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303758 error(location, " left of '[' is not of type array, matrix, or vector ",
3759 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003760 }
3761 else
3762 {
3763 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3764 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003765
Olli Etuaho3ec75682017-07-05 17:02:55 +03003766 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003767 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003768
Jiawei Shaod8105a02017-08-08 09:54:36 +08003769 if (baseExpression->getQualifier() == EvqPerVertexIn)
3770 {
3771 ASSERT(mShaderType == GL_GEOMETRY_SHADER_OES);
3772 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3773 {
3774 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3775 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3776 }
3777 }
3778
Jamie Madill21c1e452014-12-29 11:33:41 -05003779 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3780
Olli Etuaho36b05142015-11-12 13:10:42 +02003781 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3782 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3783 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3784 // index is a constant expression.
3785 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3786 {
3787 if (baseExpression->isInterfaceBlock())
3788 {
Jiawei Shaod8105a02017-08-08 09:54:36 +08003789 // TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
3790 switch (baseExpression->getQualifier())
3791 {
3792 case EvqPerVertexIn:
3793 break;
3794 case EvqUniform:
3795 case EvqBuffer:
3796 error(location,
3797 "array indexes for uniform block arrays and shader storage block arrays "
3798 "must be constant integral expressions",
3799 "[");
3800 break;
3801 default:
3802 UNREACHABLE();
3803 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003804 }
3805 else if (baseExpression->getQualifier() == EvqFragmentOut)
3806 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003807 error(location,
3808 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003809 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003810 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3811 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003812 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003813 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003814 }
3815
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003816 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003817 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003818 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3819 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3820 // constant fold expressions that are not constant expressions). The most compatible way to
3821 // handle this case is to report a warning instead of an error and force the index to be in
3822 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003823 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003824 int index = 0;
3825 if (indexConstantUnion->getBasicType() == EbtInt)
3826 {
3827 index = indexConstantUnion->getIConst(0);
3828 }
3829 else if (indexConstantUnion->getBasicType() == EbtUInt)
3830 {
3831 index = static_cast<int>(indexConstantUnion->getUConst(0));
3832 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003833
3834 int safeIndex = -1;
3835
3836 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04003837 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003838 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003839 {
Olli Etuahodaaff1c2017-07-05 18:03:26 +03003840 if (!isExtensionEnabled("GL_EXT_draw_buffers"))
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003841 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003842 outOfRangeError(outOfRangeIndexIsError, location,
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003843 "array index for gl_FragData must be zero when "
Olli Etuaho4de340a2016-12-16 09:32:03 +00003844 "GL_EXT_draw_buffers is disabled",
3845 "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003846 safeIndex = 0;
3847 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03003848 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003849 // Only do generic out-of-range check if similar error hasn't already been reported.
3850 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003851 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003852 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003853 baseExpression->getOutermostArraySize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003854 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003855 }
3856 }
3857 else if (baseExpression->isMatrix())
3858 {
3859 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03003860 baseExpression->getType().getCols(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003861 "matrix field selection out of range");
Jamie Madill7164cf42013-07-08 13:30:59 -04003862 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003863 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04003864 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003865 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3866 baseExpression->getType().getNominalSize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003867 "vector field selection out of range");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003868 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003869
3870 ASSERT(safeIndex >= 0);
3871 // Data of constant unions can't be changed, because it may be shared with other
3872 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
3873 // sanitized object.
Olli Etuaho56229f12017-07-10 14:16:33 +03003874 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003875 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003876 TConstantUnion *safeConstantUnion = new TConstantUnion();
3877 safeConstantUnion->setIConst(safeIndex);
3878 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
Olli Etuaho56229f12017-07-10 14:16:33 +03003879 indexConstantUnion->getTypePointer()->setBasicType(EbtInt);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003880 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003881
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003882 TIntermBinary *node = new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
3883 node->setLine(location);
3884 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003885 }
Jamie Madill7164cf42013-07-08 13:30:59 -04003886 else
3887 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003888 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
3889 node->setLine(location);
3890 // Indirect indexing can never be constant folded.
3891 return node;
Jamie Madill7164cf42013-07-08 13:30:59 -04003892 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003893}
3894
Olli Etuaho90892fb2016-07-14 14:44:51 +03003895int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
3896 const TSourceLoc &location,
3897 int index,
3898 int arraySize,
Olli Etuaho4de340a2016-12-16 09:32:03 +00003899 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003900{
3901 if (index >= arraySize || index < 0)
3902 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003903 std::stringstream reasonStream;
3904 reasonStream << reason << " '" << index << "'";
3905 std::string token = reasonStream.str();
3906 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuaho90892fb2016-07-14 14:44:51 +03003907 if (index < 0)
3908 {
3909 return 0;
3910 }
3911 else
3912 {
3913 return arraySize - 1;
3914 }
3915 }
3916 return index;
3917}
3918
Jamie Madillb98c3a82015-07-23 14:26:04 -04003919TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3920 const TSourceLoc &dotLocation,
3921 const TString &fieldString,
3922 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003923{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003924 if (baseExpression->isArray())
3925 {
3926 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003927 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003928 }
3929
3930 if (baseExpression->isVector())
3931 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003932 TVector<int> fieldOffsets;
3933 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
3934 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003935 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003936 fieldOffsets.resize(1);
3937 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003938 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003939 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
3940 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003941
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003942 return node->fold();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003943 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003944 else if (baseExpression->getBasicType() == EbtStruct)
3945 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303946 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003947 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003948 {
3949 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003950 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003951 }
3952 else
3953 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003954 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003955 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003956 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003957 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003958 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003959 {
3960 fieldFound = true;
3961 break;
3962 }
3963 }
3964 if (fieldFound)
3965 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003966 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003967 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003968 TIntermBinary *node =
3969 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
3970 node->setLine(dotLocation);
3971 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003972 }
3973 else
3974 {
3975 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003976 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003977 }
3978 }
3979 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003980 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003981 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303982 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003983 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003984 {
3985 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003986 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003987 }
3988 else
3989 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003990 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003991 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003992 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003993 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003994 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003995 {
3996 fieldFound = true;
3997 break;
3998 }
3999 }
4000 if (fieldFound)
4001 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004002 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004003 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004004 TIntermBinary *node =
4005 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4006 node->setLine(dotLocation);
4007 // Indexing interface blocks can never be constant folded.
4008 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004009 }
4010 else
4011 {
4012 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004013 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004014 }
4015 }
4016 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004017 else
4018 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004019 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004020 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004021 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304022 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004023 }
4024 else
4025 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304026 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004027 " field selection requires structure, vector, or interface block on left hand "
4028 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304029 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004030 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004031 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004032 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004033}
4034
Jamie Madillb98c3a82015-07-23 14:26:04 -04004035TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4036 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004037{
Martin Radev802abe02016-08-04 17:48:32 +03004038 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004039
4040 if (qualifierType == "shared")
4041 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004042 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004043 {
4044 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4045 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004046 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004047 }
4048 else if (qualifierType == "packed")
4049 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004050 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004051 {
4052 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4053 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004054 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004055 }
4056 else if (qualifierType == "std140")
4057 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004058 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004059 }
4060 else if (qualifierType == "row_major")
4061 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004062 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004063 }
4064 else if (qualifierType == "column_major")
4065 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004066 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004067 }
4068 else if (qualifierType == "location")
4069 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004070 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
4071 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004072 }
Andrei Volykhina5527072017-03-22 16:46:30 +03004073 else if (qualifierType == "yuv" && isExtensionEnabled("GL_EXT_YUV_target") &&
4074 mShaderType == GL_FRAGMENT_SHADER)
4075 {
4076 qualifier.yuv = true;
4077 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004078 else if (qualifierType == "rgba32f")
4079 {
4080 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4081 qualifier.imageInternalFormat = EiifRGBA32F;
4082 }
4083 else if (qualifierType == "rgba16f")
4084 {
4085 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4086 qualifier.imageInternalFormat = EiifRGBA16F;
4087 }
4088 else if (qualifierType == "r32f")
4089 {
4090 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4091 qualifier.imageInternalFormat = EiifR32F;
4092 }
4093 else if (qualifierType == "rgba8")
4094 {
4095 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4096 qualifier.imageInternalFormat = EiifRGBA8;
4097 }
4098 else if (qualifierType == "rgba8_snorm")
4099 {
4100 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4101 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4102 }
4103 else if (qualifierType == "rgba32i")
4104 {
4105 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4106 qualifier.imageInternalFormat = EiifRGBA32I;
4107 }
4108 else if (qualifierType == "rgba16i")
4109 {
4110 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4111 qualifier.imageInternalFormat = EiifRGBA16I;
4112 }
4113 else if (qualifierType == "rgba8i")
4114 {
4115 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4116 qualifier.imageInternalFormat = EiifRGBA8I;
4117 }
4118 else if (qualifierType == "r32i")
4119 {
4120 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4121 qualifier.imageInternalFormat = EiifR32I;
4122 }
4123 else if (qualifierType == "rgba32ui")
4124 {
4125 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4126 qualifier.imageInternalFormat = EiifRGBA32UI;
4127 }
4128 else if (qualifierType == "rgba16ui")
4129 {
4130 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4131 qualifier.imageInternalFormat = EiifRGBA16UI;
4132 }
4133 else if (qualifierType == "rgba8ui")
4134 {
4135 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4136 qualifier.imageInternalFormat = EiifRGBA8UI;
4137 }
4138 else if (qualifierType == "r32ui")
4139 {
4140 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4141 qualifier.imageInternalFormat = EiifR32UI;
4142 }
Shaob5cc1192017-07-06 10:47:20 +08004143 else if (qualifierType == "points" && isExtensionEnabled("GL_OES_geometry_shader") &&
4144 mShaderType == GL_GEOMETRY_SHADER_OES)
4145 {
4146 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4147 qualifier.primitiveType = EptPoints;
4148 }
4149 else if (qualifierType == "lines" && isExtensionEnabled("GL_OES_geometry_shader") &&
4150 mShaderType == GL_GEOMETRY_SHADER_OES)
4151 {
4152 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4153 qualifier.primitiveType = EptLines;
4154 }
4155 else if (qualifierType == "lines_adjacency" && isExtensionEnabled("GL_OES_geometry_shader") &&
4156 mShaderType == GL_GEOMETRY_SHADER_OES)
4157 {
4158 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4159 qualifier.primitiveType = EptLinesAdjacency;
4160 }
4161 else if (qualifierType == "triangles" && isExtensionEnabled("GL_OES_geometry_shader") &&
4162 mShaderType == GL_GEOMETRY_SHADER_OES)
4163 {
4164 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4165 qualifier.primitiveType = EptTriangles;
4166 }
4167 else if (qualifierType == "triangles_adjacency" &&
4168 isExtensionEnabled("GL_OES_geometry_shader") && mShaderType == GL_GEOMETRY_SHADER_OES)
4169 {
4170 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4171 qualifier.primitiveType = EptTrianglesAdjacency;
4172 }
4173 else if (qualifierType == "line_strip" && isExtensionEnabled("GL_OES_geometry_shader") &&
4174 mShaderType == GL_GEOMETRY_SHADER_OES)
4175 {
4176 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4177 qualifier.primitiveType = EptLineStrip;
4178 }
4179 else if (qualifierType == "triangle_strip" && isExtensionEnabled("GL_OES_geometry_shader") &&
4180 mShaderType == GL_GEOMETRY_SHADER_OES)
4181 {
4182 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4183 qualifier.primitiveType = EptTriangleStrip;
4184 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004185
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004186 else
4187 {
4188 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004189 }
4190
Jamie Madilla5efff92013-06-06 11:56:47 -04004191 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004192}
4193
Martin Radev802abe02016-08-04 17:48:32 +03004194void TParseContext::parseLocalSize(const TString &qualifierType,
4195 const TSourceLoc &qualifierTypeLine,
4196 int intValue,
4197 const TSourceLoc &intValueLine,
4198 const std::string &intValueString,
4199 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004200 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004201{
Olli Etuaho856c4972016-08-08 11:38:39 +03004202 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004203 if (intValue < 1)
4204 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004205 std::stringstream reasonStream;
4206 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4207 std::string reason = reasonStream.str();
4208 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004209 }
4210 (*localSize)[index] = intValue;
4211}
4212
Olli Etuaho09b04a22016-12-15 13:30:26 +00004213void TParseContext::parseNumViews(int intValue,
4214 const TSourceLoc &intValueLine,
4215 const std::string &intValueString,
4216 int *numViews)
4217{
4218 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4219 // specification.
4220 if (intValue < 1)
4221 {
4222 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4223 }
4224 *numViews = intValue;
4225}
4226
Shaob5cc1192017-07-06 10:47:20 +08004227void TParseContext::parseInvocations(int intValue,
4228 const TSourceLoc &intValueLine,
4229 const std::string &intValueString,
4230 int *numInvocations)
4231{
4232 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4233 // it doesn't make sense to accept invocations <= 0.
4234 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4235 {
4236 error(intValueLine,
4237 "out of range: invocations must be in the range of [1, "
4238 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4239 intValueString.c_str());
4240 }
4241 else
4242 {
4243 *numInvocations = intValue;
4244 }
4245}
4246
4247void TParseContext::parseMaxVertices(int intValue,
4248 const TSourceLoc &intValueLine,
4249 const std::string &intValueString,
4250 int *maxVertices)
4251{
4252 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4253 // it doesn't make sense to accept max_vertices < 0.
4254 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4255 {
4256 error(
4257 intValueLine,
4258 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4259 intValueString.c_str());
4260 }
4261 else
4262 {
4263 *maxVertices = intValue;
4264 }
4265}
4266
Jamie Madillb98c3a82015-07-23 14:26:04 -04004267TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4268 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004269 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304270 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004271{
Martin Radev802abe02016-08-04 17:48:32 +03004272 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004273
Martin Radev802abe02016-08-04 17:48:32 +03004274 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004275
Martin Radev802abe02016-08-04 17:48:32 +03004276 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004277 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004278 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004279 if (intValue < 0)
4280 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004281 error(intValueLine, "out of range: location must be non-negative",
4282 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004283 }
4284 else
4285 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004286 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004287 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004288 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004289 }
Olli Etuaho43364892017-02-13 16:00:12 +00004290 else if (qualifierType == "binding")
4291 {
4292 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4293 if (intValue < 0)
4294 {
4295 error(intValueLine, "out of range: binding must be non-negative",
4296 intValueString.c_str());
4297 }
4298 else
4299 {
4300 qualifier.binding = intValue;
4301 }
4302 }
jchen104cdac9e2017-05-08 11:01:20 +08004303 else if (qualifierType == "offset")
4304 {
4305 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4306 if (intValue < 0)
4307 {
4308 error(intValueLine, "out of range: offset must be non-negative",
4309 intValueString.c_str());
4310 }
4311 else
4312 {
4313 qualifier.offset = intValue;
4314 }
4315 }
Martin Radev802abe02016-08-04 17:48:32 +03004316 else if (qualifierType == "local_size_x")
4317 {
4318 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4319 &qualifier.localSize);
4320 }
4321 else if (qualifierType == "local_size_y")
4322 {
4323 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4324 &qualifier.localSize);
4325 }
4326 else if (qualifierType == "local_size_z")
4327 {
4328 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4329 &qualifier.localSize);
4330 }
Olli Etuaho95468d12017-05-04 11:14:34 +03004331 else if (qualifierType == "num_views" && isMultiviewExtensionEnabled() &&
Olli Etuaho09b04a22016-12-15 13:30:26 +00004332 mShaderType == GL_VERTEX_SHADER)
4333 {
4334 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4335 }
Shaob5cc1192017-07-06 10:47:20 +08004336 else if (qualifierType == "invocations" && isExtensionEnabled("GL_OES_geometry_shader") &&
4337 mShaderType == GL_GEOMETRY_SHADER_OES)
4338 {
4339 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4340 }
4341 else if (qualifierType == "max_vertices" && isExtensionEnabled("GL_OES_geometry_shader") &&
4342 mShaderType == GL_GEOMETRY_SHADER_OES)
4343 {
4344 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4345 }
4346
Martin Radev802abe02016-08-04 17:48:32 +03004347 else
4348 {
4349 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004350 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004351
Jamie Madilla5efff92013-06-06 11:56:47 -04004352 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004353}
4354
Olli Etuaho613b9592016-09-05 12:05:53 +03004355TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4356{
4357 return new TTypeQualifierBuilder(
4358 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4359 mShaderVersion);
4360}
4361
Olli Etuahocce89652017-06-19 16:04:09 +03004362TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4363 const TSourceLoc &loc)
4364{
4365 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4366 return new TStorageQualifierWrapper(qualifier, loc);
4367}
4368
4369TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4370{
4371 if (getShaderType() == GL_VERTEX_SHADER)
4372 {
4373 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4374 }
4375 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4376}
4377
4378TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4379{
4380 if (declaringFunction())
4381 {
4382 return new TStorageQualifierWrapper(EvqIn, loc);
4383 }
Shaob5cc1192017-07-06 10:47:20 +08004384
4385 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004386 {
Shaob5cc1192017-07-06 10:47:20 +08004387 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004388 {
Shaob5cc1192017-07-06 10:47:20 +08004389 if (mShaderVersion < 300 && !isMultiviewExtensionEnabled())
4390 {
4391 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4392 }
4393 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004394 }
Shaob5cc1192017-07-06 10:47:20 +08004395 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004396 {
Shaob5cc1192017-07-06 10:47:20 +08004397 if (mShaderVersion < 300)
4398 {
4399 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4400 }
4401 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004402 }
Shaob5cc1192017-07-06 10:47:20 +08004403 case GL_COMPUTE_SHADER:
4404 {
4405 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4406 }
4407 case GL_GEOMETRY_SHADER_OES:
4408 {
4409 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4410 }
4411 default:
4412 {
4413 UNREACHABLE();
4414 return new TStorageQualifierWrapper(EvqLast, loc);
4415 }
Olli Etuahocce89652017-06-19 16:04:09 +03004416 }
Olli Etuahocce89652017-06-19 16:04:09 +03004417}
4418
4419TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4420{
4421 if (declaringFunction())
4422 {
4423 return new TStorageQualifierWrapper(EvqOut, loc);
4424 }
Shaob5cc1192017-07-06 10:47:20 +08004425 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004426 {
Shaob5cc1192017-07-06 10:47:20 +08004427 case GL_VERTEX_SHADER:
4428 {
4429 if (mShaderVersion < 300)
4430 {
4431 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4432 }
4433 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4434 }
4435 case GL_FRAGMENT_SHADER:
4436 {
4437 if (mShaderVersion < 300)
4438 {
4439 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4440 }
4441 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4442 }
4443 case GL_COMPUTE_SHADER:
4444 {
4445 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4446 return new TStorageQualifierWrapper(EvqLast, loc);
4447 }
4448 case GL_GEOMETRY_SHADER_OES:
4449 {
4450 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4451 }
4452 default:
4453 {
4454 UNREACHABLE();
4455 return new TStorageQualifierWrapper(EvqLast, loc);
4456 }
Olli Etuahocce89652017-06-19 16:04:09 +03004457 }
Olli Etuahocce89652017-06-19 16:04:09 +03004458}
4459
4460TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4461{
4462 if (!declaringFunction())
4463 {
4464 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4465 }
4466 return new TStorageQualifierWrapper(EvqInOut, loc);
4467}
4468
Jamie Madillb98c3a82015-07-23 14:26:04 -04004469TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004470 TLayoutQualifier rightQualifier,
4471 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004472{
Martin Radevc28888b2016-07-22 15:27:42 +03004473 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004474 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004475}
4476
Olli Etuahocce89652017-06-19 16:04:09 +03004477TField *TParseContext::parseStructDeclarator(TString *identifier, const TSourceLoc &loc)
4478{
4479 checkIsNotReserved(loc, *identifier);
4480 TType *type = new TType(EbtVoid, EbpUndefined);
4481 return new TField(type, identifier, loc);
4482}
4483
4484TField *TParseContext::parseStructArrayDeclarator(TString *identifier,
4485 const TSourceLoc &loc,
4486 TIntermTyped *arraySize,
4487 const TSourceLoc &arraySizeLoc)
4488{
4489 checkIsNotReserved(loc, *identifier);
4490
4491 TType *type = new TType(EbtVoid, EbpUndefined);
4492 unsigned int size = checkIsValidArraySize(arraySizeLoc, arraySize);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004493 type->makeArray(size);
Olli Etuahocce89652017-06-19 16:04:09 +03004494
4495 return new TField(type, identifier, loc);
4496}
4497
Olli Etuaho4de340a2016-12-16 09:32:03 +00004498TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4499 const TFieldList *newlyAddedFields,
4500 const TSourceLoc &location)
4501{
4502 for (TField *field : *newlyAddedFields)
4503 {
4504 for (TField *oldField : *processedFields)
4505 {
4506 if (oldField->name() == field->name())
4507 {
4508 error(location, "duplicate field name in structure", field->name().c_str());
4509 }
4510 }
4511 processedFields->push_back(field);
4512 }
4513 return processedFields;
4514}
4515
Martin Radev70866b82016-07-22 15:27:42 +03004516TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4517 const TTypeQualifierBuilder &typeQualifierBuilder,
4518 TPublicType *typeSpecifier,
4519 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004520{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004521 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004522
Martin Radev70866b82016-07-22 15:27:42 +03004523 typeSpecifier->qualifier = typeQualifier.qualifier;
4524 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004525 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004526 typeSpecifier->invariant = typeQualifier.invariant;
4527 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304528 {
Martin Radev70866b82016-07-22 15:27:42 +03004529 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004530 }
Martin Radev70866b82016-07-22 15:27:42 +03004531 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004532}
4533
Jamie Madillb98c3a82015-07-23 14:26:04 -04004534TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004535 TFieldList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004536{
Martin Radev4a9cd802016-09-01 16:51:51 +03004537 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4538 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004539
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004540 checkIsNonVoid(typeSpecifier.getLine(), (*declaratorList)[0]->name(),
4541 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004542
Martin Radev4a9cd802016-09-01 16:51:51 +03004543 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004544
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004545 for (unsigned int i = 0; i < declaratorList->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304546 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004547 auto declaratorArraySizes = (*declaratorList)[i]->type()->getArraySizes();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004548 // don't allow arrays of arrays
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004549 if (!declaratorArraySizes.empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304550 {
Olli Etuahoe0803872017-08-23 15:30:23 +03004551 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004552 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004553
4554 TType *type = (*declaratorList)[i]->type();
4555 *type = TType(typeSpecifier);
4556 for (unsigned int arraySize : declaratorArraySizes)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304557 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004558 type->makeArray(arraySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004559 }
4560
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004561 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*declaratorList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004562 }
4563
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004564 return declaratorList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004565}
4566
Martin Radev4a9cd802016-09-01 16:51:51 +03004567TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4568 const TSourceLoc &nameLine,
4569 const TString *structName,
4570 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004571{
Olli Etuahoa5e693a2017-07-13 16:07:26 +03004572 TStructure *structure = new TStructure(&symbolTable, structName, fieldList);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004573
Jamie Madill9b820842015-02-12 10:40:10 -05004574 // Store a bool in the struct if we're at global scope, to allow us to
4575 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004576 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004577
Jamie Madill98493dd2013-07-08 14:39:03 -04004578 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004579 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004580 checkIsNotReserved(nameLine, *structName);
Olli Etuaho0f684632017-07-13 12:42:15 +03004581 if (!symbolTable.declareStructType(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304582 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004583 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004584 }
4585 }
4586
4587 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004588 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004589 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004590 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004591 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004592 switch (qualifier)
4593 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004594 case EvqGlobal:
4595 case EvqTemporary:
4596 break;
4597 default:
4598 error(field.line(), "invalid qualifier on struct member",
4599 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004600 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004601 }
Martin Radev70866b82016-07-22 15:27:42 +03004602 if (field.type()->isInvariant())
4603 {
4604 error(field.line(), "invalid qualifier on struct member", "invariant");
4605 }
jchen104cdac9e2017-05-08 11:01:20 +08004606 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4607 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004608 {
4609 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4610 }
4611
Olli Etuaho43364892017-02-13 16:00:12 +00004612 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4613
4614 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004615
4616 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004617 }
4618
Martin Radev4a9cd802016-09-01 16:51:51 +03004619 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004620 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004621 exitStructDeclaration();
4622
Martin Radev4a9cd802016-09-01 16:51:51 +03004623 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004624}
4625
Jamie Madillb98c3a82015-07-23 14:26:04 -04004626TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004627 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004628 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004629{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004630 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004631 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004632 init->isVector())
4633 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004634 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4635 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004636 return nullptr;
4637 }
4638
Olli Etuahoac5274d2015-02-20 10:19:08 +02004639 if (statementList)
4640 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00004641 if (!ValidateSwitchStatementList(switchType, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004642 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02004643 return nullptr;
4644 }
4645 }
4646
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004647 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4648 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004649 return node;
4650}
4651
4652TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4653{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004654 if (mSwitchNestingLevel == 0)
4655 {
4656 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004657 return nullptr;
4658 }
4659 if (condition == nullptr)
4660 {
4661 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004662 return nullptr;
4663 }
4664 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004665 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004666 {
4667 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004668 }
4669 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004670 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4671 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4672 // fold in case labels.
4673 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004674 {
4675 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004676 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004677 TIntermCase *node = new TIntermCase(condition);
4678 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004679 return node;
4680}
4681
4682TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4683{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004684 if (mSwitchNestingLevel == 0)
4685 {
4686 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004687 return nullptr;
4688 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004689 TIntermCase *node = new TIntermCase(nullptr);
4690 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004691 return node;
4692}
4693
Jamie Madillb98c3a82015-07-23 14:26:04 -04004694TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4695 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004696 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004697{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004698 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004699
4700 switch (op)
4701 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004702 case EOpLogicalNot:
4703 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4704 child->isVector())
4705 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004706 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004707 return nullptr;
4708 }
4709 break;
4710 case EOpBitwiseNot:
4711 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4712 child->isMatrix() || child->isArray())
4713 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004714 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004715 return nullptr;
4716 }
4717 break;
4718 case EOpPostIncrement:
4719 case EOpPreIncrement:
4720 case EOpPostDecrement:
4721 case EOpPreDecrement:
4722 case EOpNegative:
4723 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004724 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4725 child->getBasicType() == EbtBool || child->isArray() ||
4726 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004727 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004728 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004729 return nullptr;
4730 }
4731 // Operators for built-ins are already type checked against their prototype.
4732 default:
4733 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004734 }
4735
Jiajia Qinbc585152017-06-23 15:42:17 +08004736 if (child->getMemoryQualifier().writeonly)
4737 {
4738 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4739 return nullptr;
4740 }
4741
Olli Etuahof119a262016-08-19 15:54:22 +03004742 TIntermUnary *node = new TIntermUnary(op, child);
4743 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004744
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004745 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004746}
4747
Olli Etuaho09b22472015-02-11 11:47:26 +02004748TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4749{
Olli Etuahocce89652017-06-19 16:04:09 +03004750 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004751 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004752 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004753 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004754 return child;
4755 }
4756 return node;
4757}
4758
Jamie Madillb98c3a82015-07-23 14:26:04 -04004759TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4760 TIntermTyped *child,
4761 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004762{
Olli Etuaho856c4972016-08-08 11:38:39 +03004763 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004764 return addUnaryMath(op, child, loc);
4765}
4766
Jamie Madillb98c3a82015-07-23 14:26:04 -04004767bool TParseContext::binaryOpCommonCheck(TOperator op,
4768 TIntermTyped *left,
4769 TIntermTyped *right,
4770 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004771{
jchen10b4cf5652017-05-05 18:51:17 +08004772 // Check opaque types are not allowed to be operands in expressions other than array indexing
4773 // and structure member selection.
4774 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4775 {
4776 switch (op)
4777 {
4778 case EOpIndexDirect:
4779 case EOpIndexIndirect:
4780 break;
4781 case EOpIndexDirectStruct:
4782 UNREACHABLE();
4783
4784 default:
4785 error(loc, "Invalid operation for variables with an opaque type",
4786 GetOperatorString(op));
4787 return false;
4788 }
4789 }
jchen10cc2a10e2017-05-03 14:05:12 +08004790
Jiajia Qinbc585152017-06-23 15:42:17 +08004791 if (right->getMemoryQualifier().writeonly)
4792 {
4793 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4794 return false;
4795 }
4796
4797 if (left->getMemoryQualifier().writeonly)
4798 {
4799 switch (op)
4800 {
4801 case EOpAssign:
4802 case EOpInitialize:
4803 case EOpIndexDirect:
4804 case EOpIndexIndirect:
4805 case EOpIndexDirectStruct:
4806 case EOpIndexDirectInterfaceBlock:
4807 break;
4808 default:
4809 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4810 return false;
4811 }
4812 }
4813
Olli Etuaho244be012016-08-18 15:26:02 +03004814 if (left->getType().getStruct() || right->getType().getStruct())
4815 {
4816 switch (op)
4817 {
4818 case EOpIndexDirectStruct:
4819 ASSERT(left->getType().getStruct());
4820 break;
4821 case EOpEqual:
4822 case EOpNotEqual:
4823 case EOpAssign:
4824 case EOpInitialize:
4825 if (left->getType() != right->getType())
4826 {
4827 return false;
4828 }
4829 break;
4830 default:
4831 error(loc, "Invalid operation for structs", GetOperatorString(op));
4832 return false;
4833 }
4834 }
4835
Olli Etuaho94050052017-05-08 14:17:44 +03004836 if (left->isInterfaceBlock() || right->isInterfaceBlock())
4837 {
4838 switch (op)
4839 {
4840 case EOpIndexDirectInterfaceBlock:
4841 ASSERT(left->getType().getInterfaceBlock());
4842 break;
4843 default:
4844 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
4845 return false;
4846 }
4847 }
4848
Olli Etuahod6b14282015-03-17 14:31:35 +02004849 if (left->isArray() || right->isArray())
4850 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004851 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02004852 {
4853 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4854 return false;
4855 }
4856
4857 if (left->isArray() != right->isArray())
4858 {
4859 error(loc, "array / non-array mismatch", GetOperatorString(op));
4860 return false;
4861 }
4862
4863 switch (op)
4864 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004865 case EOpEqual:
4866 case EOpNotEqual:
4867 case EOpAssign:
4868 case EOpInitialize:
4869 break;
4870 default:
4871 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4872 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02004873 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03004874 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004875 if (left->getType().getArraySizes() != right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02004876 {
4877 error(loc, "array size mismatch", GetOperatorString(op));
4878 return false;
4879 }
Olli Etuahod6b14282015-03-17 14:31:35 +02004880 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004881
4882 // Check ops which require integer / ivec parameters
4883 bool isBitShift = false;
4884 switch (op)
4885 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004886 case EOpBitShiftLeft:
4887 case EOpBitShiftRight:
4888 case EOpBitShiftLeftAssign:
4889 case EOpBitShiftRightAssign:
4890 // Unsigned can be bit-shifted by signed and vice versa, but we need to
4891 // check that the basic type is an integer type.
4892 isBitShift = true;
4893 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
4894 {
4895 return false;
4896 }
4897 break;
4898 case EOpBitwiseAnd:
4899 case EOpBitwiseXor:
4900 case EOpBitwiseOr:
4901 case EOpBitwiseAndAssign:
4902 case EOpBitwiseXorAssign:
4903 case EOpBitwiseOrAssign:
4904 // It is enough to check the type of only one operand, since later it
4905 // is checked that the operand types match.
4906 if (!IsInteger(left->getBasicType()))
4907 {
4908 return false;
4909 }
4910 break;
4911 default:
4912 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004913 }
4914
4915 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
4916 // So the basic type should usually match.
4917 if (!isBitShift && left->getBasicType() != right->getBasicType())
4918 {
4919 return false;
4920 }
4921
Olli Etuaho63e1ec52016-08-18 22:05:12 +03004922 // Check that:
4923 // 1. Type sizes match exactly on ops that require that.
4924 // 2. Restrictions for structs that contain arrays or samplers are respected.
4925 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04004926 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004927 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004928 case EOpAssign:
4929 case EOpInitialize:
4930 case EOpEqual:
4931 case EOpNotEqual:
4932 // ESSL 1.00 sections 5.7, 5.8, 5.9
4933 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
4934 {
4935 error(loc, "undefined operation for structs containing arrays",
4936 GetOperatorString(op));
4937 return false;
4938 }
4939 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
4940 // we interpret the spec so that this extends to structs containing samplers,
4941 // similarly to ESSL 1.00 spec.
4942 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
4943 left->getType().isStructureContainingSamplers())
4944 {
4945 error(loc, "undefined operation for structs containing samplers",
4946 GetOperatorString(op));
4947 return false;
4948 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004949
Olli Etuahoe1805592017-01-02 16:41:20 +00004950 if ((left->getNominalSize() != right->getNominalSize()) ||
4951 (left->getSecondarySize() != right->getSecondarySize()))
4952 {
4953 error(loc, "dimension mismatch", GetOperatorString(op));
4954 return false;
4955 }
4956 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004957 case EOpLessThan:
4958 case EOpGreaterThan:
4959 case EOpLessThanEqual:
4960 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00004961 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04004962 {
Olli Etuahoe1805592017-01-02 16:41:20 +00004963 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004964 return false;
4965 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03004966 break;
4967 case EOpAdd:
4968 case EOpSub:
4969 case EOpDiv:
4970 case EOpIMod:
4971 case EOpBitShiftLeft:
4972 case EOpBitShiftRight:
4973 case EOpBitwiseAnd:
4974 case EOpBitwiseXor:
4975 case EOpBitwiseOr:
4976 case EOpAddAssign:
4977 case EOpSubAssign:
4978 case EOpDivAssign:
4979 case EOpIModAssign:
4980 case EOpBitShiftLeftAssign:
4981 case EOpBitShiftRightAssign:
4982 case EOpBitwiseAndAssign:
4983 case EOpBitwiseXorAssign:
4984 case EOpBitwiseOrAssign:
4985 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
4986 {
4987 return false;
4988 }
4989
4990 // Are the sizes compatible?
4991 if (left->getNominalSize() != right->getNominalSize() ||
4992 left->getSecondarySize() != right->getSecondarySize())
4993 {
4994 // If the nominal sizes of operands do not match:
4995 // One of them must be a scalar.
4996 if (!left->isScalar() && !right->isScalar())
4997 return false;
4998
4999 // In the case of compound assignment other than multiply-assign,
5000 // the right side needs to be a scalar. Otherwise a vector/matrix
5001 // would be assigned to a scalar. A scalar can't be shifted by a
5002 // vector either.
5003 if (!right->isScalar() &&
5004 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5005 return false;
5006 }
5007 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005008 default:
5009 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005010 }
5011
Olli Etuahod6b14282015-03-17 14:31:35 +02005012 return true;
5013}
5014
Olli Etuaho1dded802016-08-18 18:13:13 +03005015bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5016 const TType &left,
5017 const TType &right)
5018{
5019 switch (op)
5020 {
5021 case EOpMul:
5022 case EOpMulAssign:
5023 return left.getNominalSize() == right.getNominalSize() &&
5024 left.getSecondarySize() == right.getSecondarySize();
5025 case EOpVectorTimesScalar:
5026 return true;
5027 case EOpVectorTimesScalarAssign:
5028 ASSERT(!left.isMatrix() && !right.isMatrix());
5029 return left.isVector() && !right.isVector();
5030 case EOpVectorTimesMatrix:
5031 return left.getNominalSize() == right.getRows();
5032 case EOpVectorTimesMatrixAssign:
5033 ASSERT(!left.isMatrix() && right.isMatrix());
5034 return left.isVector() && left.getNominalSize() == right.getRows() &&
5035 left.getNominalSize() == right.getCols();
5036 case EOpMatrixTimesVector:
5037 return left.getCols() == right.getNominalSize();
5038 case EOpMatrixTimesScalar:
5039 return true;
5040 case EOpMatrixTimesScalarAssign:
5041 ASSERT(left.isMatrix() && !right.isMatrix());
5042 return !right.isVector();
5043 case EOpMatrixTimesMatrix:
5044 return left.getCols() == right.getRows();
5045 case EOpMatrixTimesMatrixAssign:
5046 ASSERT(left.isMatrix() && right.isMatrix());
5047 // We need to check two things:
5048 // 1. The matrix multiplication step is valid.
5049 // 2. The result will have the same number of columns as the lvalue.
5050 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5051
5052 default:
5053 UNREACHABLE();
5054 return false;
5055 }
5056}
5057
Jamie Madillb98c3a82015-07-23 14:26:04 -04005058TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5059 TIntermTyped *left,
5060 TIntermTyped *right,
5061 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005062{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005063 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005064 return nullptr;
5065
Olli Etuahofc1806e2015-03-17 13:03:11 +02005066 switch (op)
5067 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005068 case EOpEqual:
5069 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005070 case EOpLessThan:
5071 case EOpGreaterThan:
5072 case EOpLessThanEqual:
5073 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005074 break;
5075 case EOpLogicalOr:
5076 case EOpLogicalXor:
5077 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005078 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5079 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005080 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005081 {
5082 return nullptr;
5083 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005084 // Basic types matching should have been already checked.
5085 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005086 break;
5087 case EOpAdd:
5088 case EOpSub:
5089 case EOpDiv:
5090 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005091 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5092 !right->getType().getStruct());
5093 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005094 {
5095 return nullptr;
5096 }
5097 break;
5098 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005099 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5100 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005101 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005102 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005103 {
5104 return nullptr;
5105 }
5106 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005107 default:
5108 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005109 }
5110
Olli Etuaho1dded802016-08-18 18:13:13 +03005111 if (op == EOpMul)
5112 {
5113 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5114 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5115 {
5116 return nullptr;
5117 }
5118 }
5119
Olli Etuaho3fdec912016-08-18 15:08:06 +03005120 TIntermBinary *node = new TIntermBinary(op, left, right);
5121 node->setLine(loc);
5122
Olli Etuaho3fdec912016-08-18 15:08:06 +03005123 // See if we can fold constants.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005124 return node->fold(mDiagnostics);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005125}
5126
Jamie Madillb98c3a82015-07-23 14:26:04 -04005127TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5128 TIntermTyped *left,
5129 TIntermTyped *right,
5130 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005131{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005132 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005133 if (node == 0)
5134 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005135 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5136 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005137 return left;
5138 }
5139 return node;
5140}
5141
Jamie Madillb98c3a82015-07-23 14:26:04 -04005142TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5143 TIntermTyped *left,
5144 TIntermTyped *right,
5145 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005146{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005147 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005148 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005149 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005150 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5151 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005152 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005153 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005154 }
5155 return node;
5156}
5157
Olli Etuaho13389b62016-10-16 11:48:18 +01005158TIntermBinary *TParseContext::createAssign(TOperator op,
5159 TIntermTyped *left,
5160 TIntermTyped *right,
5161 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005162{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005163 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005164 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005165 if (op == EOpMulAssign)
5166 {
5167 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5168 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5169 {
5170 return nullptr;
5171 }
5172 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005173 TIntermBinary *node = new TIntermBinary(op, left, right);
5174 node->setLine(loc);
5175
Olli Etuaho3fdec912016-08-18 15:08:06 +03005176 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005177 }
5178 return nullptr;
5179}
5180
Jamie Madillb98c3a82015-07-23 14:26:04 -04005181TIntermTyped *TParseContext::addAssign(TOperator op,
5182 TIntermTyped *left,
5183 TIntermTyped *right,
5184 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005185{
Olli Etuahocce89652017-06-19 16:04:09 +03005186 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005187 TIntermTyped *node = createAssign(op, left, right, loc);
5188 if (node == nullptr)
5189 {
5190 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005191 return left;
5192 }
5193 return node;
5194}
5195
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005196TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5197 TIntermTyped *right,
5198 const TSourceLoc &loc)
5199{
Corentin Wallez0d959252016-07-12 17:26:32 -04005200 // WebGL2 section 5.26, the following results in an error:
5201 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005202 if (mShaderSpec == SH_WEBGL2_SPEC &&
5203 (left->isArray() || left->getBasicType() == EbtVoid ||
5204 left->getType().isStructureContainingArrays() || right->isArray() ||
5205 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005206 {
5207 error(loc,
5208 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5209 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005210 }
5211
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005212 TIntermBinary *commaNode = new TIntermBinary(EOpComma, left, right);
5213 TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(mShaderVersion, left, right);
5214 commaNode->getTypePointer()->setQualifier(resultQualifier);
5215 return commaNode->fold(mDiagnostics);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005216}
5217
Olli Etuaho49300862015-02-20 14:54:49 +02005218TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5219{
5220 switch (op)
5221 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005222 case EOpContinue:
5223 if (mLoopNestingLevel <= 0)
5224 {
5225 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005226 }
5227 break;
5228 case EOpBreak:
5229 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5230 {
5231 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005232 }
5233 break;
5234 case EOpReturn:
5235 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5236 {
5237 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005238 }
5239 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005240 case EOpKill:
5241 if (mShaderType != GL_FRAGMENT_SHADER)
5242 {
5243 error(loc, "discard supported in fragment shaders only", "discard");
5244 }
5245 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005246 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005247 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005248 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005249 }
Olli Etuahocce89652017-06-19 16:04:09 +03005250 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005251}
5252
Jamie Madillb98c3a82015-07-23 14:26:04 -04005253TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005254 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005255 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005256{
Olli Etuahocce89652017-06-19 16:04:09 +03005257 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005258 {
Olli Etuahocce89652017-06-19 16:04:09 +03005259 ASSERT(op == EOpReturn);
5260 mFunctionReturnsValue = true;
5261 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5262 {
5263 error(loc, "void function cannot return a value", "return");
5264 }
5265 else if (*mCurrentFunctionType != expression->getType())
5266 {
5267 error(loc, "function return is not matching type:", "return");
5268 }
Olli Etuaho49300862015-02-20 14:54:49 +02005269 }
Olli Etuahocce89652017-06-19 16:04:09 +03005270 TIntermBranch *node = new TIntermBranch(op, expression);
5271 node->setLine(loc);
5272 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005273}
5274
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005275void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5276{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005277 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01005278 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005279 TIntermNode *offset = nullptr;
5280 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuahoec9232b2017-03-27 17:01:37 +03005281 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
5282 name == "textureProjLodOffset" || name == "textureGradOffset" ||
5283 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005284 {
5285 offset = arguments->back();
5286 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03005287 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005288 {
5289 // A bias parameter might follow the offset parameter.
5290 ASSERT(arguments->size() >= 3);
5291 offset = (*arguments)[2];
5292 }
5293 if (offset != nullptr)
5294 {
5295 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5296 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5297 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005298 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03005299 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005300 }
5301 else
5302 {
5303 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5304 size_t size = offsetConstantUnion->getType().getObjectSize();
5305 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
5306 for (size_t i = 0u; i < size; ++i)
5307 {
5308 int offsetValue = values[i].getIConst();
5309 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
5310 {
5311 std::stringstream tokenStream;
5312 tokenStream << offsetValue;
5313 std::string token = tokenStream.str();
5314 error(offset->getLine(), "Texture offset value out of valid range",
5315 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005316 }
5317 }
5318 }
5319 }
5320}
5321
Martin Radev2cc85b32016-08-05 16:22:53 +03005322// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5323void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5324{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005325 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03005326 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5327
5328 if (name.compare(0, 5, "image") == 0)
5329 {
5330 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005331 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005332
Olli Etuaho485eefd2017-02-14 17:40:06 +00005333 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005334
5335 if (name.compare(5, 5, "Store") == 0)
5336 {
5337 if (memoryQualifier.readonly)
5338 {
5339 error(imageNode->getLine(),
5340 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005341 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005342 }
5343 }
5344 else if (name.compare(5, 4, "Load") == 0)
5345 {
5346 if (memoryQualifier.writeonly)
5347 {
5348 error(imageNode->getLine(),
5349 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005350 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005351 }
5352 }
5353 }
5354}
5355
5356// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5357void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5358 const TFunction *functionDefinition,
5359 const TIntermAggregate *functionCall)
5360{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005361 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005362
5363 const TIntermSequence &arguments = *functionCall->getSequence();
5364
5365 ASSERT(functionDefinition->getParamCount() == arguments.size());
5366
5367 for (size_t i = 0; i < arguments.size(); ++i)
5368 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005369 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5370 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005371 const TType &functionParameterType = *functionDefinition->getParam(i).type;
5372 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5373
5374 if (IsImage(functionArgumentType.getBasicType()))
5375 {
5376 const TMemoryQualifier &functionArgumentMemoryQualifier =
5377 functionArgumentType.getMemoryQualifier();
5378 const TMemoryQualifier &functionParameterMemoryQualifier =
5379 functionParameterType.getMemoryQualifier();
5380 if (functionArgumentMemoryQualifier.readonly &&
5381 !functionParameterMemoryQualifier.readonly)
5382 {
5383 error(functionCall->getLine(),
5384 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005385 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005386 }
5387
5388 if (functionArgumentMemoryQualifier.writeonly &&
5389 !functionParameterMemoryQualifier.writeonly)
5390 {
5391 error(functionCall->getLine(),
5392 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005393 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005394 }
Martin Radev049edfa2016-11-11 14:35:37 +02005395
5396 if (functionArgumentMemoryQualifier.coherent &&
5397 !functionParameterMemoryQualifier.coherent)
5398 {
5399 error(functionCall->getLine(),
5400 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005401 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005402 }
5403
5404 if (functionArgumentMemoryQualifier.volatileQualifier &&
5405 !functionParameterMemoryQualifier.volatileQualifier)
5406 {
5407 error(functionCall->getLine(),
5408 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005409 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005410 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005411 }
5412 }
5413}
5414
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005415TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005416{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005417 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00005418}
5419
5420TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005421 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00005422 TIntermNode *thisNode,
5423 const TSourceLoc &loc)
5424{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005425 if (thisNode != nullptr)
5426 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005427 return addMethod(fnCall, arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005428 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005429
5430 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005431 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005432 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005433 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005434 }
5435 else
5436 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005437 ASSERT(op == EOpNull);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005438 return addNonConstructorFunctionCall(fnCall, arguments, loc);
5439 }
5440}
5441
5442TIntermTyped *TParseContext::addMethod(TFunction *fnCall,
5443 TIntermSequence *arguments,
5444 TIntermNode *thisNode,
5445 const TSourceLoc &loc)
5446{
5447 TConstantUnion *unionArray = new TConstantUnion[1];
5448 int arraySize = 0;
5449 TIntermTyped *typedThis = thisNode->getAsTyped();
5450 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5451 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5452 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
5453 // So accessing fnCall->getName() below is safe.
5454 if (fnCall->getName() != "length")
5455 {
5456 error(loc, "invalid method", fnCall->getName().c_str());
5457 }
5458 else if (!arguments->empty())
5459 {
5460 error(loc, "method takes no parameters", "length");
5461 }
5462 else if (typedThis == nullptr || !typedThis->isArray())
5463 {
5464 error(loc, "length can only be called on arrays", "length");
5465 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08005466 else if (typedThis->getQualifier() == EvqPerVertexIn &&
5467 mGeometryShaderInputPrimitiveType == EptUndefined)
5468 {
5469 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5470 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005471 else
5472 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03005473 arraySize = typedThis->getOutermostArraySize();
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005474 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuaho72d10202017-01-19 15:58:30 +00005475 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005476 // This code path can be hit with expressions like these:
5477 // (a = b).length()
5478 // (func()).length()
5479 // (int[3](0, 1, 2)).length()
5480 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
5481 // expression.
5482 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
5483 // spec section 5.9 which allows "An array, vector or matrix expression with the
5484 // length method applied".
5485 error(loc, "length can only be called on array names, not on array expressions",
5486 "length");
Olli Etuaho72d10202017-01-19 15:58:30 +00005487 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005488 }
5489 unionArray->setIConst(arraySize);
Olli Etuaho56229f12017-07-10 14:16:33 +03005490 TIntermConstantUnion *node =
5491 new TIntermConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst));
5492 node->setLine(loc);
5493 return node;
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005494}
5495
5496TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunction *fnCall,
5497 TIntermSequence *arguments,
5498 const TSourceLoc &loc)
5499{
5500 // First find by unmangled name to check whether the function name has been
5501 // hidden by a variable name or struct typename.
5502 // If a function is found, check for one with a matching argument list.
5503 bool builtIn;
5504 const TSymbol *symbol = symbolTable.find(fnCall->getName(), mShaderVersion, &builtIn);
5505 if (symbol != nullptr && !symbol->isFunction())
5506 {
5507 error(loc, "function name expected", fnCall->getName().c_str());
5508 }
5509 else
5510 {
5511 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(fnCall->getName(), *arguments),
5512 mShaderVersion, &builtIn);
5513 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005514 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005515 error(loc, "no matching overloaded function found", fnCall->getName().c_str());
5516 }
5517 else
5518 {
5519 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005520 //
5521 // A declared function.
5522 //
Olli Etuaho383b7912016-08-05 11:22:59 +03005523 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005524 {
Olli Etuaho856c4972016-08-08 11:38:39 +03005525 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005526 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005527 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005528 if (builtIn && op != EOpNull)
5529 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005530 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005531 if (fnCandidate->getParamCount() == 1)
5532 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005533 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005534 TIntermNode *unaryParamNode = arguments->front();
5535 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005536 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005537 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005538 }
5539 else
5540 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005541 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00005542 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005543 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005544
5545 // Some built-in functions have out parameters too.
Jiajia Qinbc585152017-06-23 15:42:17 +08005546 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05305547
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005548 if (TIntermAggregate::CanFoldAggregateBuiltInOp(callNode->getOp()))
Arun Patole274f0702015-05-05 13:33:30 +05305549 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005550 // See if we can constant fold a built-in. Note that this may be possible
5551 // even if it is not const-qualified.
5552 return callNode->fold(mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05305553 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005554 else
5555 {
5556 return callNode;
5557 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005558 }
5559 }
5560 else
5561 {
5562 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005563 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005564
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005565 // If builtIn == false, the function is user defined - could be an overloaded
5566 // built-in as well.
5567 // if builtIn == true, it's a builtIn function with no op associated with it.
5568 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005569 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005570 {
Olli Etuahofe486322017-03-21 09:30:54 +00005571 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005572 checkTextureOffsetConst(callNode);
5573 checkImageMemoryAccessForBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03005574 }
5575 else
5576 {
Olli Etuahofe486322017-03-21 09:30:54 +00005577 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005578 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005579 }
5580
Jiajia Qinbc585152017-06-23 15:42:17 +08005581 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005582
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005583 callNode->setLine(loc);
5584
5585 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005586 }
5587 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005588 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005589
5590 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005591 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005592}
5593
Jamie Madillb98c3a82015-07-23 14:26:04 -04005594TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005595 TIntermTyped *trueExpression,
5596 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005597 const TSourceLoc &loc)
5598{
Olli Etuaho56229f12017-07-10 14:16:33 +03005599 if (!checkIsScalarBool(loc, cond))
5600 {
5601 return falseExpression;
5602 }
Olli Etuaho52901742015-04-15 13:42:45 +03005603
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005604 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005605 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005606 std::stringstream reasonStream;
5607 reasonStream << "mismatching ternary operator operand types '"
5608 << trueExpression->getCompleteString() << " and '"
5609 << falseExpression->getCompleteString() << "'";
5610 std::string reason = reasonStream.str();
5611 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005612 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005613 }
Olli Etuahode318b22016-10-25 16:18:25 +01005614 if (IsOpaqueType(trueExpression->getBasicType()))
5615 {
5616 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005617 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005618 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5619 // Note that structs containing opaque types don't need to be checked as structs are
5620 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005621 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005622 return falseExpression;
5623 }
5624
Jiajia Qinbc585152017-06-23 15:42:17 +08005625 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5626 falseExpression->getMemoryQualifier().writeonly)
5627 {
5628 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5629 return falseExpression;
5630 }
5631
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005632 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005633 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005634 // ESSL 3.00.6 section 5.7:
5635 // Ternary operator support is optional for arrays. No certainty that it works across all
5636 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5637 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005638 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005639 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005640 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005641 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005642 }
Olli Etuaho94050052017-05-08 14:17:44 +03005643 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5644 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005645 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005646 return falseExpression;
5647 }
5648
Corentin Wallez0d959252016-07-12 17:26:32 -04005649 // WebGL2 section 5.26, the following results in an error:
5650 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005651 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005652 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005653 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005654 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005655 }
5656
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005657 // Note that the node resulting from here can be a constant union without being qualified as
5658 // constant.
5659 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5660 node->setLine(loc);
5661
5662 return node->fold();
Olli Etuaho52901742015-04-15 13:42:45 +03005663}
Olli Etuaho49300862015-02-20 14:54:49 +02005664
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005665//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005666// Parse an array of strings using yyparse.
5667//
5668// Returns 0 for success.
5669//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005670int PaParseStrings(size_t count,
5671 const char *const string[],
5672 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305673 TParseContext *context)
5674{
Yunchao He4f285442017-04-21 12:15:49 +08005675 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005676 return 1;
5677
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005678 if (glslang_initialize(context))
5679 return 1;
5680
alokp@chromium.org408c45e2012-04-05 15:54:43 +00005681 int error = glslang_scan(count, string, length, context);
5682 if (!error)
5683 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005684
alokp@chromium.org73bc2982012-06-19 18:48:05 +00005685 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00005686
alokp@chromium.org6b495712012-06-29 00:06:58 +00005687 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005688}
Jamie Madill45bcc782016-11-07 13:58:48 -05005689
5690} // namespace sh