blob: 25ee4424960b83430a724b85f9c5953c1101f52d [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;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400524 default:
525 //
526 // Type that can't be written to?
527 //
528 if (node->getBasicType() == EbtVoid)
529 {
530 message = "can't modify void";
531 }
jchen10cc2a10e2017-05-03 14:05:12 +0800532 if (IsOpaqueType(node->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -0400533 {
jchen10cc2a10e2017-05-03 14:05:12 +0800534 message = "can't modify a variable with type ";
535 message += getBasicString(node->getBasicType());
Martin Radev2cc85b32016-08-05 16:22:53 +0300536 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800537 else if (node->getMemoryQualifier().readonly)
538 {
539 message = "can't modify a readonly variable";
540 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000541 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000542
jchen10cc2a10e2017-05-03 14:05:12 +0800543 if (message.empty() && binaryNode == 0 && symNode == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530544 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000545 error(line, "l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000546
Olli Etuaho8a176262016-08-16 14:23:01 +0300547 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000548 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000549
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000550 //
551 // Everything else is okay, no error.
552 //
jchen10cc2a10e2017-05-03 14:05:12 +0800553 if (message.empty())
Olli Etuaho8a176262016-08-16 14:23:01 +0300554 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000555
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000556 //
557 // If we get here, we have an error and a message.
558 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530559 if (symNode)
560 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000561 const char *symbol = symNode->getSymbol().c_str();
562 std::stringstream reasonStream;
563 reasonStream << "l-value required (" << message << " \"" << symbol << "\")";
564 std::string reason = reasonStream.str();
565 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000566 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530567 else
568 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000569 std::stringstream reasonStream;
570 reasonStream << "l-value required (" << message << ")";
571 std::string reason = reasonStream.str();
572 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000573 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000574
Olli Etuaho8a176262016-08-16 14:23:01 +0300575 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000576}
577
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000578// Both test, and if necessary spit out an error, to see if the node is really
579// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300580void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000581{
Olli Etuaho383b7912016-08-05 11:22:59 +0300582 if (node->getQualifier() != EvqConst)
583 {
584 error(node->getLine(), "constant expression required", "");
585 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000586}
587
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000588// Both test, and if necessary spit out an error, to see if the node is really
589// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300590void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000591{
Olli Etuaho383b7912016-08-05 11:22:59 +0300592 if (!node->isScalarInt())
593 {
594 error(node->getLine(), "integer expression required", token);
595 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000596}
597
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000598// Both test, and if necessary spit out an error, to see if we are currently
599// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800600bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000601{
Olli Etuaho856c4972016-08-08 11:38:39 +0300602 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300603 {
604 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800605 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300606 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800607 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000608}
609
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300610// ESSL 3.00.5 sections 3.8 and 3.9.
611// If it starts "gl_" or contains two consecutive underscores, it's reserved.
612// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a webgl shader.
Olli Etuaho856c4972016-08-08 11:38:39 +0300613bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000614{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530615 static const char *reservedErrMsg = "reserved built-in name";
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300616 if (identifier.compare(0, 3, "gl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530617 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300618 error(line, reservedErrMsg, "gl_");
619 return false;
620 }
621 if (sh::IsWebGLBasedSpec(mShaderSpec))
622 {
623 if (identifier.compare(0, 6, "webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530624 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300625 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300626 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000627 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300628 if (identifier.compare(0, 7, "_webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530629 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300630 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300631 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000632 }
633 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300634 if (identifier.find("__") != TString::npos)
635 {
636 error(line,
637 "identifiers containing two consecutive underscores (__) are reserved as "
638 "possible future keywords",
639 identifier.c_str());
640 return false;
641 }
Olli Etuaho8a176262016-08-16 14:23:01 +0300642 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000643}
644
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300645// Make sure the argument types are correct for constructing a specific type.
Olli Etuaho856c4972016-08-08 11:38:39 +0300646bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800647 const TIntermSequence *arguments,
Olli Etuaho856c4972016-08-08 11:38:39 +0300648 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000649{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800650 if (arguments->empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530651 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200652 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300653 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000654 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200655
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300656 for (TIntermNode *arg : *arguments)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530657 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300658 const TIntermTyped *argTyped = arg->getAsTyped();
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200659 ASSERT(argTyped != nullptr);
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300660 if (type.getBasicType() != EbtStruct && IsOpaqueType(argTyped->getBasicType()))
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200661 {
jchen10cc2a10e2017-05-03 14:05:12 +0800662 std::string reason("cannot convert a variable with type ");
663 reason += getBasicString(argTyped->getBasicType());
664 error(line, reason.c_str(), "constructor");
Martin Radev2cc85b32016-08-05 16:22:53 +0300665 return false;
666 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800667 else if (argTyped->getMemoryQualifier().writeonly)
668 {
669 error(line, "cannot convert a variable with writeonly", "constructor");
670 return false;
671 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200672 if (argTyped->getBasicType() == EbtVoid)
673 {
674 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300675 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200676 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000677 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000678
Olli Etuaho856c4972016-08-08 11:38:39 +0300679 if (type.isArray())
680 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300681 // The size of an unsized constructor should already have been determined.
682 ASSERT(!type.isUnsizedArray());
683 if (static_cast<size_t>(type.getArraySize()) != arguments->size())
684 {
685 error(line, "array constructor needs one argument per array element", "constructor");
686 return false;
687 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300688 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
689 // the array.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800690 for (TIntermNode *const &argNode : *arguments)
Olli Etuaho856c4972016-08-08 11:38:39 +0300691 {
692 const TType &argType = argNode->getAsTyped()->getType();
Jamie Madill34bf2d92017-02-06 13:40:59 -0500693 if (argType.isArray())
694 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300695 error(line, "constructing from a non-dereferenced array", "constructor");
Jamie Madill34bf2d92017-02-06 13:40:59 -0500696 return false;
697 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300698 if (!argType.sameElementType(type))
699 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000700 error(line, "Array constructor argument has an incorrect type", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300701 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300702 }
703 }
704 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300705 else if (type.getBasicType() == EbtStruct)
Olli Etuaho856c4972016-08-08 11:38:39 +0300706 {
707 const TFieldList &fields = type.getStruct()->fields();
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300708 if (fields.size() != arguments->size())
709 {
710 error(line,
711 "Number of constructor parameters does not match the number of structure fields",
712 "constructor");
713 return false;
714 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300715
716 for (size_t i = 0; i < fields.size(); i++)
717 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800718 if (i >= arguments->size() ||
719 (*arguments)[i]->getAsTyped()->getType() != *fields[i]->type())
Olli Etuaho856c4972016-08-08 11:38:39 +0300720 {
721 error(line, "Structure constructor arguments do not match structure fields",
Olli Etuaho4de340a2016-12-16 09:32:03 +0000722 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300723 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300724 }
725 }
726 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300727 else
728 {
729 // We're constructing a scalar, vector, or matrix.
730
731 // Note: It's okay to have too many components available, but not okay to have unused
732 // arguments. 'full' will go to true when enough args have been seen. If we loop again,
733 // there is an extra argument, so 'overFull' will become true.
734
735 size_t size = 0;
736 bool full = false;
737 bool overFull = false;
738 bool matrixArg = false;
739 for (TIntermNode *arg : *arguments)
740 {
741 const TIntermTyped *argTyped = arg->getAsTyped();
742 ASSERT(argTyped != nullptr);
743
Olli Etuaho487b63a2017-05-23 15:55:09 +0300744 if (argTyped->getBasicType() == EbtStruct)
745 {
746 error(line, "a struct cannot be used as a constructor argument for this type",
747 "constructor");
748 return false;
749 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300750 if (argTyped->getType().isArray())
751 {
752 error(line, "constructing from a non-dereferenced array", "constructor");
753 return false;
754 }
755 if (argTyped->getType().isMatrix())
756 {
757 matrixArg = true;
758 }
759
760 size += argTyped->getType().getObjectSize();
761 if (full)
762 {
763 overFull = true;
764 }
Olli Etuaho487b63a2017-05-23 15:55:09 +0300765 if (size >= type.getObjectSize())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300766 {
767 full = true;
768 }
769 }
770
771 if (type.isMatrix() && matrixArg)
772 {
773 if (arguments->size() != 1)
774 {
775 error(line, "constructing matrix from matrix can only take one argument",
776 "constructor");
777 return false;
778 }
779 }
780 else
781 {
782 if (size != 1 && size < type.getObjectSize())
783 {
784 error(line, "not enough data provided for construction", "constructor");
785 return false;
786 }
787 if (overFull)
788 {
789 error(line, "too many arguments", "constructor");
790 return false;
791 }
792 }
793 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300794
Olli Etuaho8a176262016-08-16 14:23:01 +0300795 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000796}
797
Jamie Madillb98c3a82015-07-23 14:26:04 -0400798// This function checks to see if a void variable has been declared and raise an error message for
799// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000800//
801// returns true in case of an error
802//
Olli Etuaho856c4972016-08-08 11:38:39 +0300803bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400804 const TString &identifier,
805 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000806{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300807 if (type == EbtVoid)
808 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000809 error(line, "illegal use of type 'void'", identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300810 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300811 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000812
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 the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300817// or not.
Olli Etuaho56229f12017-07-10 14:16:33 +0300818bool TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000819{
Olli Etuaho37d96cc2017-07-11 14:14:03 +0300820 if (type->getBasicType() != EbtBool || !type->isScalar())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530821 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000822 error(line, "boolean expression expected", "");
Olli Etuaho56229f12017-07-10 14:16:33 +0300823 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530824 }
Olli Etuaho56229f12017-07-10 14:16:33 +0300825 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000826}
827
Jamie Madillb98c3a82015-07-23 14:26:04 -0400828// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300829// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300830void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000831{
Martin Radev4a9cd802016-09-01 16:51:51 +0300832 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530833 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000834 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530835 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000836}
837
jchen10cc2a10e2017-05-03 14:05:12 +0800838bool TParseContext::checkIsNotOpaqueType(const TSourceLoc &line,
839 const TTypeSpecifierNonArray &pType,
840 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000841{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530842 if (pType.type == EbtStruct)
843 {
Olli Etuaho0f684632017-07-13 12:42:15 +0300844 if (ContainsSampler(pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530845 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000846 std::stringstream reasonStream;
847 reasonStream << reason << " (structure contains a sampler)";
848 std::string reasonStr = reasonStream.str();
849 error(line, reasonStr.c_str(), getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300850 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000851 }
jchen10cc2a10e2017-05-03 14:05:12 +0800852 // only samplers need to be checked from structs, since other opaque types can't be struct
853 // members.
Olli Etuaho8a176262016-08-16 14:23:01 +0300854 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530855 }
jchen10cc2a10e2017-05-03 14:05:12 +0800856 else if (IsOpaqueType(pType.type))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530857 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000858 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300859 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000860 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000861
Olli Etuaho8a176262016-08-16 14:23:01 +0300862 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000863}
864
Olli Etuaho856c4972016-08-08 11:38:39 +0300865void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
866 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400867{
868 if (pType.layoutQualifier.location != -1)
869 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400870 error(line, "location must only be specified for a single input or output variable",
871 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400872 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400873}
874
Olli Etuaho856c4972016-08-08 11:38:39 +0300875void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
876 const TLayoutQualifier &layoutQualifier)
877{
878 if (layoutQualifier.location != -1)
879 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000880 const char *errorMsg = "invalid layout qualifier: only valid on program inputs and outputs";
881 if (mShaderVersion >= 310)
882 {
883 errorMsg =
884 "invalid layout qualifier: only valid on program inputs, outputs, and uniforms";
885 }
886 error(location, errorMsg, "location");
Olli Etuaho856c4972016-08-08 11:38:39 +0300887 }
888}
889
Martin Radev2cc85b32016-08-05 16:22:53 +0300890void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
891 TQualifier qualifier,
892 const TType &type)
893{
Martin Radev2cc85b32016-08-05 16:22:53 +0300894 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
jchen10cc2a10e2017-05-03 14:05:12 +0800895 if (IsOpaqueType(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530896 {
jchen10cc2a10e2017-05-03 14:05:12 +0800897 error(line, "opaque types cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000898 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000899}
900
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000901// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300902unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000903{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530904 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000905
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200906 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
907 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
908 // fold as array size.
909 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000910 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000911 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300912 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000913 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000914
Olli Etuaho856c4972016-08-08 11:38:39 +0300915 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400916
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000917 if (constant->getBasicType() == EbtUInt)
918 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300919 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000920 }
921 else
922 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300923 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000924
Olli Etuaho856c4972016-08-08 11:38:39 +0300925 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000926 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400927 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300928 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000929 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400930
Olli Etuaho856c4972016-08-08 11:38:39 +0300931 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400932 }
933
Olli Etuaho856c4972016-08-08 11:38:39 +0300934 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400935 {
936 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300937 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400938 }
939
940 // The size of arrays is restricted here to prevent issues further down the
941 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
942 // 4096 registers so this should be reasonable even for aggressively optimizable code.
943 const unsigned int sizeLimit = 65536;
944
Olli Etuaho856c4972016-08-08 11:38:39 +0300945 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -0400946 {
947 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300948 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000949 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300950
951 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000952}
953
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000954// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +0300955bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
956 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000957{
Olli Etuaho8a176262016-08-16 14:23:01 +0300958 if ((elementQualifier.qualifier == EvqAttribute) ||
959 (elementQualifier.qualifier == EvqVertexIn) ||
960 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +0300961 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400962 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300963 TType(elementQualifier).getQualifierString());
964 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000965 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000966
Olli Etuaho8a176262016-08-16 14:23:01 +0300967 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000968}
969
Olli Etuaho8a176262016-08-16 14:23:01 +0300970// See if this element type can be formed into an array.
971bool TParseContext::checkIsValidTypeForArray(const TSourceLoc &line, const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000972{
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000973 //
974 // Can the type be an array?
975 //
Olli Etuaho8a176262016-08-16 14:23:01 +0300976 if (elementType.array)
Jamie Madill06145232015-05-13 13:10:01 -0400977 {
Olli Etuaho8a176262016-08-16 14:23:01 +0300978 error(line, "cannot declare arrays of arrays",
979 TType(elementType).getCompleteString().c_str());
980 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000981 }
Olli Etuahocc36b982015-07-10 14:14:18 +0300982 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
983 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
984 // 4.3.4).
Martin Radev4a9cd802016-09-01 16:51:51 +0300985 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Olli Etuaho8a176262016-08-16 14:23:01 +0300986 sh::IsVarying(elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +0300987 {
988 error(line, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +0300989 TType(elementType).getCompleteString().c_str());
990 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +0300991 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000992
Olli Etuaho8a176262016-08-16 14:23:01 +0300993 return true;
994}
995
996// Check if this qualified element type can be formed into an array.
997bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
998 const TPublicType &elementType)
999{
1000 if (checkIsValidTypeForArray(indexLocation, elementType))
1001 {
1002 return checkIsValidQualifierForArray(indexLocation, elementType);
1003 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001004 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001005}
1006
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001007// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +03001008void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
1009 const TString &identifier,
1010 TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001011{
Olli Etuaho3739d232015-04-08 12:23:44 +03001012 ASSERT(type != nullptr);
1013 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001014 {
1015 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +03001016 type->qualifier = EvqTemporary;
1017
1018 // Generate informative error messages for ESSL1.
1019 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001020 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001021 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301022 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001023 "structures containing arrays may not be declared constant since they cannot be "
1024 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +05301025 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001026 }
1027 else
1028 {
1029 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
1030 }
Olli Etuaho383b7912016-08-05 11:22:59 +03001031 return;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001032 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001033 if (type->isUnsizedArray())
1034 {
1035 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
Olli Etuaho376f1b52015-04-13 13:23:41 +03001036 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001037}
1038
Olli Etuaho2935c582015-04-08 14:32:06 +03001039// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001040// and update the symbol table.
1041//
Olli Etuaho2935c582015-04-08 14:32:06 +03001042// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001043//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001044bool TParseContext::declareVariable(const TSourceLoc &line,
1045 const TString &identifier,
1046 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001047 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001048{
Olli Etuaho2935c582015-04-08 14:32:06 +03001049 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001050
Olli Etuaho43364892017-02-13 16:00:12 +00001051 checkBindingIsValid(line, type);
1052
Olli Etuaho856c4972016-08-08 11:38:39 +03001053 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001054
Olli Etuaho2935c582015-04-08 14:32:06 +03001055 // gl_LastFragData may be redeclared with a new precision qualifier
1056 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1057 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001058 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1059 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho856c4972016-08-08 11:38:39 +03001060 if (static_cast<int>(type.getArraySize()) == maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001061 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001062 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001063 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001064 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->getExtension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001065 }
1066 }
1067 else
1068 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001069 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1070 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001071 return false;
1072 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001073 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001074
Olli Etuaho8a176262016-08-16 14:23:01 +03001075 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001076 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001077
Olli Etuaho0f684632017-07-13 12:42:15 +03001078 (*variable) = symbolTable.declareVariable(&identifier, type);
1079 if (!(*variable))
Olli Etuaho2935c582015-04-08 14:32:06 +03001080 {
1081 error(line, "redefinition", identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001082 return false;
1083 }
1084
Olli Etuaho8a176262016-08-16 14:23:01 +03001085 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001086 return false;
1087
1088 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001089}
1090
Martin Radev70866b82016-07-22 15:27:42 +03001091void TParseContext::checkIsParameterQualifierValid(
1092 const TSourceLoc &line,
1093 const TTypeQualifierBuilder &typeQualifierBuilder,
1094 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301095{
Olli Etuahocce89652017-06-19 16:04:09 +03001096 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001097 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001098
1099 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301100 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001101 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1102 }
1103
1104 if (!IsImage(type->getBasicType()))
1105 {
Olli Etuaho43364892017-02-13 16:00:12 +00001106 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001107 }
1108 else
1109 {
1110 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001111 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001112
Martin Radev70866b82016-07-22 15:27:42 +03001113 type->setQualifier(typeQualifier.qualifier);
1114
1115 if (typeQualifier.precision != EbpUndefined)
1116 {
1117 type->setPrecision(typeQualifier.precision);
1118 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001119}
1120
Olli Etuaho856c4972016-08-08 11:38:39 +03001121bool TParseContext::checkCanUseExtension(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001122{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001123 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001124 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +05301125 if (iter == extBehavior.end())
1126 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001127 error(line, "extension is not supported", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001128 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001129 }
zmo@google.comf5450912011-09-09 01:37:19 +00001130 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +05301131 if (iter->second == EBhDisable || iter->second == EBhUndefined)
1132 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00001133 // TODO(oetuaho@nvidia.com): This is slightly hacky. Might be better if symbols could be
1134 // associated with more than one extension.
1135 if (extension == "GL_OVR_multiview")
1136 {
1137 return checkCanUseExtension(line, "GL_OVR_multiview2");
1138 }
Olli Etuaho4de340a2016-12-16 09:32:03 +00001139 error(line, "extension is disabled", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001140 return false;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001141 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301142 if (iter->second == EBhWarn)
1143 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001144 warning(line, "extension is being used", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001145 return true;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001146 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001147
Olli Etuaho8a176262016-08-16 14:23:01 +03001148 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001149}
1150
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001151// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1152// compile-time or link-time errors are the same whether or not the declaration is empty".
1153// This function implements all the checks that are done on qualifiers regardless of if the
1154// declaration is empty.
1155void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1156 const sh::TLayoutQualifier &layoutQualifier,
1157 const TSourceLoc &location)
1158{
1159 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1160 {
1161 error(location, "Shared memory declarations cannot have layout specified", "layout");
1162 }
1163
1164 if (layoutQualifier.matrixPacking != EmpUnspecified)
1165 {
1166 error(location, "layout qualifier only valid for interface blocks",
1167 getMatrixPackingString(layoutQualifier.matrixPacking));
1168 return;
1169 }
1170
1171 if (layoutQualifier.blockStorage != EbsUnspecified)
1172 {
1173 error(location, "layout qualifier only valid for interface blocks",
1174 getBlockStorageString(layoutQualifier.blockStorage));
1175 return;
1176 }
1177
1178 if (qualifier == EvqFragmentOut)
1179 {
1180 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1181 {
1182 error(location, "invalid layout qualifier combination", "yuv");
1183 return;
1184 }
1185 }
1186 else
1187 {
1188 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1189 }
1190
Olli Etuaho95468d12017-05-04 11:14:34 +03001191 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1192 // parsing steps. So it needs to be checked here.
1193 if (isMultiviewExtensionEnabled() && mShaderVersion < 300 && qualifier == EvqVertexIn)
1194 {
1195 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1196 }
1197
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001198 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
1199 if (mShaderVersion >= 310 && qualifier == EvqUniform)
1200 {
1201 canHaveLocation = true;
1202 // We're not checking whether the uniform location is in range here since that depends on
1203 // the type of the variable.
1204 // The type can only be fully determined for non-empty declarations.
1205 }
1206 if (!canHaveLocation)
1207 {
1208 checkLocationIsNotSpecified(location, layoutQualifier);
1209 }
1210}
1211
jchen104cdac9e2017-05-08 11:01:20 +08001212void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1213 const TSourceLoc &location)
1214{
1215 if (publicType.precision != EbpHigh)
1216 {
1217 error(location, "Can only be highp", "atomic counter");
1218 }
1219 // dEQP enforces compile error if location is specified. See uniform_location.test.
1220 if (publicType.layoutQualifier.location != -1)
1221 {
1222 error(location, "location must not be set for atomic_uint", "layout");
1223 }
1224 if (publicType.layoutQualifier.binding == -1)
1225 {
1226 error(location, "no binding specified", "atomic counter");
1227 }
1228}
1229
Martin Radevb8b01222016-11-20 23:25:53 +02001230void TParseContext::emptyDeclarationErrorCheck(const TPublicType &publicType,
1231 const TSourceLoc &location)
1232{
1233 if (publicType.isUnsizedArray())
1234 {
1235 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1236 // error. It is assumed that this applies to empty declarations as well.
1237 error(location, "empty array declaration needs to specify a size", "");
1238 }
Martin Radevb8b01222016-11-20 23:25:53 +02001239}
1240
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001241// These checks are done for all declarations that are non-empty. They're done for non-empty
1242// declarations starting a declarator list, and declarators that follow an empty declaration.
1243void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1244 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001245{
Olli Etuahofa33d582015-04-09 14:33:12 +03001246 switch (publicType.qualifier)
1247 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001248 case EvqVaryingIn:
1249 case EvqVaryingOut:
1250 case EvqAttribute:
1251 case EvqVertexIn:
1252 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001253 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001254 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001255 {
1256 error(identifierLocation, "cannot be used with a structure",
1257 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001258 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001259 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001260 break;
1261 case EvqBuffer:
1262 if (publicType.getBasicType() != EbtInterfaceBlock)
1263 {
1264 error(identifierLocation,
1265 "cannot declare buffer variables at global scope(outside a block)",
1266 getQualifierString(publicType.qualifier));
1267 return;
1268 }
1269 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001270 default:
1271 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001272 }
jchen10cc2a10e2017-05-03 14:05:12 +08001273 std::string reason(getBasicString(publicType.getBasicType()));
1274 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001275 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001276 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001277 {
1278 return;
1279 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001280
Andrei Volykhina5527072017-03-22 16:46:30 +03001281 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1282 publicType.qualifier != EvqConst) &&
1283 publicType.getBasicType() == EbtYuvCscStandardEXT)
1284 {
1285 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1286 getQualifierString(publicType.qualifier));
1287 return;
1288 }
1289
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001290 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1291 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001292 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1293 // But invalid shaders may still reach here with an unsized array declaration.
1294 if (!publicType.isUnsizedArray())
1295 {
1296 TType type(publicType);
1297 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1298 publicType.layoutQualifier);
1299 }
1300 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001301
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001302 // check for layout qualifier issues
1303 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001304
Martin Radev2cc85b32016-08-05 16:22:53 +03001305 if (IsImage(publicType.getBasicType()))
1306 {
1307
1308 switch (layoutQualifier.imageInternalFormat)
1309 {
1310 case EiifRGBA32F:
1311 case EiifRGBA16F:
1312 case EiifR32F:
1313 case EiifRGBA8:
1314 case EiifRGBA8_SNORM:
1315 if (!IsFloatImage(publicType.getBasicType()))
1316 {
1317 error(identifierLocation,
1318 "internal image format requires a floating image type",
1319 getBasicString(publicType.getBasicType()));
1320 return;
1321 }
1322 break;
1323 case EiifRGBA32I:
1324 case EiifRGBA16I:
1325 case EiifRGBA8I:
1326 case EiifR32I:
1327 if (!IsIntegerImage(publicType.getBasicType()))
1328 {
1329 error(identifierLocation,
1330 "internal image format requires an integer image type",
1331 getBasicString(publicType.getBasicType()));
1332 return;
1333 }
1334 break;
1335 case EiifRGBA32UI:
1336 case EiifRGBA16UI:
1337 case EiifRGBA8UI:
1338 case EiifR32UI:
1339 if (!IsUnsignedImage(publicType.getBasicType()))
1340 {
1341 error(identifierLocation,
1342 "internal image format requires an unsigned image type",
1343 getBasicString(publicType.getBasicType()));
1344 return;
1345 }
1346 break;
1347 case EiifUnspecified:
1348 error(identifierLocation, "layout qualifier", "No image internal format specified");
1349 return;
1350 default:
1351 error(identifierLocation, "layout qualifier", "unrecognized token");
1352 return;
1353 }
1354
1355 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1356 switch (layoutQualifier.imageInternalFormat)
1357 {
1358 case EiifR32F:
1359 case EiifR32I:
1360 case EiifR32UI:
1361 break;
1362 default:
1363 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1364 {
1365 error(identifierLocation, "layout qualifier",
1366 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1367 "image variables must be qualified readonly and/or writeonly");
1368 return;
1369 }
1370 break;
1371 }
1372 }
1373 else
1374 {
Olli Etuaho43364892017-02-13 16:00:12 +00001375 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001376 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1377 }
jchen104cdac9e2017-05-08 11:01:20 +08001378
1379 if (IsAtomicCounter(publicType.getBasicType()))
1380 {
1381 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1382 }
1383 else
1384 {
1385 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1386 }
Olli Etuaho43364892017-02-13 16:00:12 +00001387}
Martin Radev2cc85b32016-08-05 16:22:53 +03001388
Olli Etuaho43364892017-02-13 16:00:12 +00001389void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1390{
1391 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
1392 int arraySize = type.isArray() ? type.getArraySize() : 1;
1393 if (IsImage(type.getBasicType()))
1394 {
1395 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding, arraySize);
1396 }
1397 else if (IsSampler(type.getBasicType()))
1398 {
1399 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding, arraySize);
1400 }
jchen104cdac9e2017-05-08 11:01:20 +08001401 else if (IsAtomicCounter(type.getBasicType()))
1402 {
1403 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1404 }
Olli Etuaho43364892017-02-13 16:00:12 +00001405 else
1406 {
1407 ASSERT(!IsOpaqueType(type.getBasicType()));
1408 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001409 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001410}
1411
Olli Etuaho856c4972016-08-08 11:38:39 +03001412void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1413 const TString &layoutQualifierName,
1414 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001415{
1416
1417 if (mShaderVersion < versionRequired)
1418 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001419 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001420 }
1421}
1422
Olli Etuaho856c4972016-08-08 11:38:39 +03001423bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1424 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001425{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001426 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001427 for (size_t i = 0u; i < localSize.size(); ++i)
1428 {
1429 if (localSize[i] != -1)
1430 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001431 error(location,
1432 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1433 "global layout declaration",
1434 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001435 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001436 }
1437 }
1438
Olli Etuaho8a176262016-08-16 14:23:01 +03001439 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001440}
1441
Olli Etuaho43364892017-02-13 16:00:12 +00001442void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001443 TLayoutImageInternalFormat internalFormat)
1444{
1445 if (internalFormat != EiifUnspecified)
1446 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001447 error(location, "invalid layout qualifier: only valid when used with images",
1448 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001449 }
Olli Etuaho43364892017-02-13 16:00:12 +00001450}
1451
1452void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1453{
1454 if (binding != -1)
1455 {
1456 error(location,
1457 "invalid layout qualifier: only valid when used with opaque types or blocks",
1458 "binding");
1459 }
1460}
1461
jchen104cdac9e2017-05-08 11:01:20 +08001462void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1463{
1464 if (offset != -1)
1465 {
1466 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1467 "offset");
1468 }
1469}
1470
Olli Etuaho43364892017-02-13 16:00:12 +00001471void TParseContext::checkImageBindingIsValid(const TSourceLoc &location, int binding, int arraySize)
1472{
1473 // Expects arraySize to be 1 when setting binding for only a single variable.
1474 if (binding >= 0 && binding + arraySize > mMaxImageUnits)
1475 {
1476 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1477 }
1478}
1479
1480void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1481 int binding,
1482 int arraySize)
1483{
1484 // Expects arraySize to be 1 when setting binding for only a single variable.
1485 if (binding >= 0 && binding + arraySize > mMaxCombinedTextureImageUnits)
1486 {
1487 error(location, "sampler binding greater than maximum texture units", "binding");
1488 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001489}
1490
Jiajia Qinbc585152017-06-23 15:42:17 +08001491void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location,
1492 const TQualifier &qualifier,
1493 int binding,
1494 int arraySize)
jchen10af713a22017-04-19 09:10:56 +08001495{
1496 int size = (arraySize == 0 ? 1 : arraySize);
Jiajia Qinbc585152017-06-23 15:42:17 +08001497 if (qualifier == EvqUniform)
jchen10af713a22017-04-19 09:10:56 +08001498 {
Jiajia Qinbc585152017-06-23 15:42:17 +08001499 if (binding + size > mMaxUniformBufferBindings)
1500 {
1501 error(location, "uniform block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1502 "binding");
1503 }
1504 }
1505 else if (qualifier == EvqBuffer)
1506 {
1507 if (binding + size > mMaxShaderStorageBufferBindings)
1508 {
1509 error(location,
1510 "shader storage block binding greater than MAX_SHADER_STORAGE_BUFFER_BINDINGS",
1511 "binding");
1512 }
jchen10af713a22017-04-19 09:10:56 +08001513 }
1514}
jchen104cdac9e2017-05-08 11:01:20 +08001515void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1516{
1517 if (binding >= mMaxAtomicCounterBindings)
1518 {
1519 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1520 "binding");
1521 }
1522}
jchen10af713a22017-04-19 09:10:56 +08001523
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001524void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1525 int objectLocationCount,
1526 const TLayoutQualifier &layoutQualifier)
1527{
1528 int loc = layoutQualifier.location;
1529 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1530 {
1531 error(location, "Uniform location out of range", "location");
1532 }
1533}
1534
Andrei Volykhina5527072017-03-22 16:46:30 +03001535void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1536{
1537 if (yuv != false)
1538 {
1539 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1540 }
1541}
1542
Jiajia Qinbc585152017-06-23 15:42:17 +08001543void TParseContext::functionCallRValueLValueErrorCheck(const TFunction *fnCandidate,
1544 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001545{
1546 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1547 {
1548 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
Jiajia Qinbc585152017-06-23 15:42:17 +08001549 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
1550 if (!IsImage(argument->getBasicType()) && (IsQualifierUnspecified(qual) || qual == EvqIn ||
1551 qual == EvqInOut || qual == EvqConstReadOnly))
1552 {
1553 if (argument->getMemoryQualifier().writeonly)
1554 {
1555 error(argument->getLine(),
1556 "Writeonly value cannot be passed for 'in' or 'inout' parameters.",
1557 fnCall->getFunctionSymbolInfo()->getName().c_str());
1558 return;
1559 }
1560 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001561 if (qual == EvqOut || qual == EvqInOut)
1562 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001563 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001564 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001565 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001566 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuahoec9232b2017-03-27 17:01:37 +03001567 fnCall->getFunctionSymbolInfo()->getName().c_str());
Olli Etuaho383b7912016-08-05 11:22:59 +03001568 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001569 }
1570 }
1571 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001572}
1573
Martin Radev70866b82016-07-22 15:27:42 +03001574void TParseContext::checkInvariantVariableQualifier(bool invariant,
1575 const TQualifier qualifier,
1576 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001577{
Martin Radev70866b82016-07-22 15:27:42 +03001578 if (!invariant)
1579 return;
1580
1581 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001582 {
Martin Radev70866b82016-07-22 15:27:42 +03001583 // input variables in the fragment shader can be also qualified as invariant
1584 if (!sh::CanBeInvariantESSL1(qualifier))
1585 {
1586 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1587 }
1588 }
1589 else
1590 {
1591 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1592 {
1593 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1594 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001595 }
1596}
1597
Arun Patole7e7e68d2015-05-22 12:02:25 +05301598bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001599{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001600 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001601 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1602 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001603}
1604
Arun Patole7e7e68d2015-05-22 12:02:25 +05301605bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001606{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001607 return ::IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001608}
1609
Jamie Madillb98c3a82015-07-23 14:26:04 -04001610void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1611 const char *extName,
1612 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001613{
1614 pp::SourceLocation srcLoc;
1615 srcLoc.file = loc.first_file;
1616 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001617 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001618}
1619
Jamie Madillb98c3a82015-07-23 14:26:04 -04001620void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1621 const char *name,
1622 const char *value,
1623 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001624{
1625 pp::SourceLocation srcLoc;
1626 srcLoc.file = loc.first_file;
1627 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001628 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001629}
1630
Martin Radev4c4c8e72016-08-04 12:25:34 +03001631sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001632{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001633 sh::WorkGroupSize result;
Martin Radev802abe02016-08-04 17:48:32 +03001634 for (size_t i = 0u; i < result.size(); ++i)
1635 {
1636 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1637 {
1638 result[i] = 1;
1639 }
1640 else
1641 {
1642 result[i] = mComputeShaderLocalSize[i];
1643 }
1644 }
1645 return result;
1646}
1647
Olli Etuaho56229f12017-07-10 14:16:33 +03001648TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1649 const TSourceLoc &line)
1650{
1651 TIntermConstantUnion *node = new TIntermConstantUnion(
1652 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1653 node->setLine(line);
1654 return node;
1655}
1656
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001657/////////////////////////////////////////////////////////////////////////////////
1658//
1659// Non-Errors.
1660//
1661/////////////////////////////////////////////////////////////////////////////////
1662
Jamie Madill5c097022014-08-20 16:38:32 -04001663const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1664 const TString *name,
1665 const TSymbol *symbol)
1666{
Jamie Madill5c097022014-08-20 16:38:32 -04001667 if (!symbol)
1668 {
1669 error(location, "undeclared identifier", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001670 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001671 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001672
1673 if (!symbol->isVariable())
Jamie Madill5c097022014-08-20 16:38:32 -04001674 {
1675 error(location, "variable expected", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001676 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001677 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001678
1679 const TVariable *variable = static_cast<const TVariable *>(symbol);
1680
1681 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
1682 !variable->getExtension().empty())
Jamie Madill5c097022014-08-20 16:38:32 -04001683 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001684 checkCanUseExtension(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001685 }
1686
Olli Etuaho0f684632017-07-13 12:42:15 +03001687 // Reject shaders using both gl_FragData and gl_FragColor
1688 TQualifier qualifier = variable->getType().getQualifier();
1689 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill5c097022014-08-20 16:38:32 -04001690 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001691 mUsesFragData = true;
1692 }
1693 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
1694 {
1695 mUsesFragColor = true;
1696 }
1697 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1698 {
1699 mUsesSecondaryOutputs = true;
Jamie Madill5c097022014-08-20 16:38:32 -04001700 }
1701
Olli Etuaho0f684632017-07-13 12:42:15 +03001702 // This validation is not quite correct - it's only an error to write to
1703 // both FragData and FragColor. For simplicity, and because users shouldn't
1704 // be rewarded for reading from undefined varaibles, return an error
1705 // if they are both referenced, rather than assigned.
1706 if (mUsesFragData && mUsesFragColor)
1707 {
1708 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1709 if (mUsesSecondaryOutputs)
1710 {
1711 errorMessage =
1712 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1713 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1714 }
1715 error(location, errorMessage, name->c_str());
1716 }
1717
1718 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1719 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1720 qualifier == EvqWorkGroupSize)
1721 {
1722 error(location,
1723 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1724 "gl_WorkGroupSize");
1725 }
Jamie Madill5c097022014-08-20 16:38:32 -04001726 return variable;
1727}
1728
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001729TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1730 const TString *name,
1731 const TSymbol *symbol)
1732{
1733 const TVariable *variable = getNamedVariable(location, name, symbol);
1734
Olli Etuaho0f684632017-07-13 12:42:15 +03001735 if (!variable)
1736 {
1737 TIntermTyped *node = CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
1738 node->setLine(location);
1739 return node;
1740 }
1741
Olli Etuaho09b04a22016-12-15 13:30:26 +00001742 if (variable->getType().getQualifier() == EvqViewIDOVR && IsWebGLBasedSpec(mShaderSpec) &&
1743 mShaderType == GL_FRAGMENT_SHADER && !isExtensionEnabled("GL_OVR_multiview2"))
1744 {
1745 // WEBGL_multiview spec
1746 error(location, "Need to enable OVR_multiview2 to use gl_ViewID_OVR in fragment shader",
1747 "gl_ViewID_OVR");
1748 }
1749
Olli Etuaho56229f12017-07-10 14:16:33 +03001750 TIntermTyped *node = nullptr;
1751
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001752 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001753 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001754 const TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho56229f12017-07-10 14:16:33 +03001755 node = new TIntermConstantUnion(constArray, variable->getType());
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001756 }
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001757 else if (variable->getType().getQualifier() == EvqWorkGroupSize &&
1758 mComputeShaderLocalSizeDeclared)
1759 {
1760 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1761 // needs to be added to the AST as a constant and not as a symbol.
1762 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1763 TConstantUnion *constArray = new TConstantUnion[3];
1764 for (size_t i = 0; i < 3; ++i)
1765 {
1766 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1767 }
1768
1769 ASSERT(variable->getType().getBasicType() == EbtUInt);
1770 ASSERT(variable->getType().getObjectSize() == 3);
1771
1772 TType type(variable->getType());
1773 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001774 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001775 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08001776 // TODO(jiawei.shao@intel.com): set array sizes for user-defined geometry shader inputs.
1777 else if (variable->getType().getQualifier() == EvqPerVertexIn)
1778 {
1779 TType type(variable->getType());
1780 type.setArraySize(mGeometryShaderInputArraySize);
1781 node = new TIntermSymbol(variable->getUniqueId(), variable->getName(), type);
1782 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001783 else
1784 {
Olli Etuaho56229f12017-07-10 14:16:33 +03001785 node = new TIntermSymbol(variable->getUniqueId(), variable->getName(), variable->getType());
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001786 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001787 ASSERT(node != nullptr);
1788 node->setLine(location);
1789 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001790}
1791
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001792// Initializers show up in several places in the grammar. Have one set of
1793// code to handle them here.
1794//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001795// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001796bool TParseContext::executeInitializer(const TSourceLoc &line,
1797 const TString &identifier,
1798 const TPublicType &pType,
1799 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001800 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001801{
Olli Etuaho13389b62016-10-16 11:48:18 +01001802 ASSERT(initNode != nullptr);
1803 ASSERT(*initNode == nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001804 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001805
Olli Etuaho2935c582015-04-08 14:32:06 +03001806 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001807 if (type.isUnsizedArray())
1808 {
Olli Etuaho02bd82c2016-11-03 10:29:43 +00001809 // We have not checked yet whether the initializer actually is an array or not.
1810 if (initializer->isArray())
1811 {
1812 type.setArraySize(initializer->getArraySize());
1813 }
1814 else
1815 {
1816 // Having a non-array initializer for an unsized array will result in an error later,
1817 // so we don't generate an error message here.
1818 type.setArraySize(1u);
1819 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001820 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001821 if (!declareVariable(line, identifier, type, &variable))
1822 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001823 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001824 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001825
Olli Etuahob0c645e2015-05-12 14:25:36 +03001826 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001827 if (symbolTable.atGlobalLevel() &&
1828 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001829 {
1830 // Error message does not completely match behavior with ESSL 1.00, but
1831 // we want to steer developers towards only using constant expressions.
1832 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001833 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001834 }
1835 if (globalInitWarning)
1836 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001837 warning(
1838 line,
1839 "global variable initializers should be constant expressions "
1840 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1841 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001842 }
1843
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001844 //
1845 // identifier must be of type constant, a global, or a temporary
1846 //
1847 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301848 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1849 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001850 error(line, " cannot initialize this type of qualifier ",
1851 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001852 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001853 }
1854 //
1855 // test for and propagate constant
1856 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001857
Arun Patole7e7e68d2015-05-22 12:02:25 +05301858 if (qualifier == EvqConst)
1859 {
1860 if (qualifier != initializer->getType().getQualifier())
1861 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001862 std::stringstream reasonStream;
1863 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1864 << "'";
1865 std::string reason = reasonStream.str();
1866 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001867 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001868 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001869 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301870 if (type != initializer->getType())
1871 {
1872 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001873 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001874 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001875 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001876 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001877
1878 // Save the constant folded value to the variable if possible. For example array
1879 // initializers are not folded, since that way copying the array literal to multiple places
1880 // in the shader is avoided.
1881 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1882 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301883 if (initializer->getAsConstantUnion())
1884 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001885 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001886 ASSERT(*initNode == nullptr);
1887 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301888 }
1889 else if (initializer->getAsSymbolNode())
1890 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001891 const TSymbol *symbol =
1892 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1893 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001894
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001895 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001896 if (constArray)
1897 {
1898 variable->shareConstPointer(constArray);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001899 ASSERT(*initNode == nullptr);
1900 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001901 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001902 }
1903 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001904
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001905 TIntermSymbol *intermSymbol =
1906 new TIntermSymbol(variable->getUniqueId(), variable->getName(), variable->getType());
1907 intermSymbol->setLine(line);
Olli Etuaho13389b62016-10-16 11:48:18 +01001908 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1909 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001910 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001911 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001912 return false;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001913 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001914
Olli Etuaho914b79a2017-06-19 16:03:19 +03001915 return true;
1916}
1917
1918TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
1919 const TString &identifier,
1920 TIntermTyped *initializer,
1921 const TSourceLoc &loc)
1922{
1923 checkIsScalarBool(loc, pType);
1924 TIntermBinary *initNode = nullptr;
1925 if (executeInitializer(loc, identifier, pType, initializer, &initNode))
1926 {
1927 // The initializer is valid. The init condition needs to have a node - either the
1928 // initializer node, or a constant node in case the initialized variable is const and won't
1929 // be recorded in the AST.
1930 if (initNode == nullptr)
1931 {
1932 return initializer;
1933 }
1934 else
1935 {
1936 TIntermDeclaration *declaration = new TIntermDeclaration();
1937 declaration->appendDeclarator(initNode);
1938 return declaration;
1939 }
1940 }
1941 return nullptr;
1942}
1943
1944TIntermNode *TParseContext::addLoop(TLoopType type,
1945 TIntermNode *init,
1946 TIntermNode *cond,
1947 TIntermTyped *expr,
1948 TIntermNode *body,
1949 const TSourceLoc &line)
1950{
1951 TIntermNode *node = nullptr;
1952 TIntermTyped *typedCond = nullptr;
1953 if (cond)
1954 {
1955 typedCond = cond->getAsTyped();
1956 }
1957 if (cond == nullptr || typedCond)
1958 {
Olli Etuahocce89652017-06-19 16:04:09 +03001959 if (type == ELoopDoWhile)
1960 {
1961 checkIsScalarBool(line, typedCond);
1962 }
1963 // In the case of other loops, it was checked before that the condition is a scalar boolean.
1964 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
1965 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
1966 !typedCond->isVector()));
1967
Olli Etuaho3ec75682017-07-05 17:02:55 +03001968 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03001969 node->setLine(line);
1970 return node;
1971 }
1972
Olli Etuahocce89652017-06-19 16:04:09 +03001973 ASSERT(type != ELoopDoWhile);
1974
Olli Etuaho914b79a2017-06-19 16:03:19 +03001975 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
1976 ASSERT(declaration);
1977 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
1978 ASSERT(declarator->getLeft()->getAsSymbolNode());
1979
1980 // The condition is a declaration. In the AST representation we don't support declarations as
1981 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
1982 // the loop.
1983 TIntermBlock *block = new TIntermBlock();
1984
1985 TIntermDeclaration *declareCondition = new TIntermDeclaration();
1986 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
1987 block->appendStatement(declareCondition);
1988
1989 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
1990 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03001991 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03001992 block->appendStatement(loop);
1993 loop->setLine(line);
1994 block->setLine(line);
1995 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001996}
1997
Olli Etuahocce89652017-06-19 16:04:09 +03001998TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
1999 TIntermNodePair code,
2000 const TSourceLoc &loc)
2001{
Olli Etuaho56229f12017-07-10 14:16:33 +03002002 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuahocce89652017-06-19 16:04:09 +03002003
2004 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03002005 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03002006 {
2007 if (cond->getAsConstantUnion()->getBConst(0) == true)
2008 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002009 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03002010 }
2011 else
2012 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002013 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03002014 }
2015 }
2016
Olli Etuaho3ec75682017-07-05 17:02:55 +03002017 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuahocce89652017-06-19 16:04:09 +03002018 node->setLine(loc);
2019
2020 return node;
2021}
2022
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002023void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
2024{
2025 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
2026 typeSpecifier->getBasicType());
2027
2028 if (mShaderVersion < 300 && typeSpecifier->array)
2029 {
2030 error(typeSpecifier->getLine(), "not supported", "first-class array");
2031 typeSpecifier->clearArrayness();
2032 }
2033}
2034
Martin Radev70866b82016-07-22 15:27:42 +03002035TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302036 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002037{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002038 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002039
Martin Radev70866b82016-07-22 15:27:42 +03002040 TPublicType returnType = typeSpecifier;
2041 returnType.qualifier = typeQualifier.qualifier;
2042 returnType.invariant = typeQualifier.invariant;
2043 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002044 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002045 returnType.precision = typeSpecifier.precision;
2046
2047 if (typeQualifier.precision != EbpUndefined)
2048 {
2049 returnType.precision = typeQualifier.precision;
2050 }
2051
Martin Radev4a9cd802016-09-01 16:51:51 +03002052 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2053 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002054
Martin Radev4a9cd802016-09-01 16:51:51 +03002055 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2056 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002057
Martin Radev4a9cd802016-09-01 16:51:51 +03002058 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002059
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002060 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002061 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002062 if (typeSpecifier.array)
2063 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002064 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002065 returnType.clearArrayness();
2066 }
2067
Martin Radev70866b82016-07-22 15:27:42 +03002068 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002069 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002070 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002071 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002072 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002073 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002074
Martin Radev70866b82016-07-22 15:27:42 +03002075 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002076 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002077 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002078 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002079 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002080 }
2081 }
2082 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002083 {
Martin Radev70866b82016-07-22 15:27:42 +03002084 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002085 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002086 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002087 }
Martin Radev70866b82016-07-22 15:27:42 +03002088 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2089 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002090 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002091 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2092 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002093 }
Martin Radev70866b82016-07-22 15:27:42 +03002094 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002095 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002096 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002097 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002098 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002099 }
2100
2101 return returnType;
2102}
2103
Olli Etuaho856c4972016-08-08 11:38:39 +03002104void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2105 const TPublicType &type,
2106 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002107{
2108 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002109 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002110 {
2111 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002112 }
2113
2114 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2115 switch (qualifier)
2116 {
2117 case EvqVertexIn:
2118 // ESSL 3.00 section 4.3.4
2119 if (type.array)
2120 {
2121 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002122 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002123 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002124 return;
2125 case EvqFragmentOut:
2126 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002127 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002128 {
2129 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002130 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002131 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002132 return;
2133 default:
2134 break;
2135 }
2136
2137 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2138 // restrictions.
2139 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002140 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2141 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002142 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2143 {
2144 error(qualifierLocation, "must use 'flat' interpolation here",
2145 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002146 }
2147
Martin Radev4a9cd802016-09-01 16:51:51 +03002148 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002149 {
2150 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2151 // These restrictions are only implied by the ESSL 3.00 spec, but
2152 // the ESSL 3.10 spec lists these restrictions explicitly.
2153 if (type.array)
2154 {
2155 error(qualifierLocation, "cannot be an array of structures",
2156 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002157 }
2158 if (type.isStructureContainingArrays())
2159 {
2160 error(qualifierLocation, "cannot be a structure containing an array",
2161 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002162 }
2163 if (type.isStructureContainingType(EbtStruct))
2164 {
2165 error(qualifierLocation, "cannot be a structure containing a structure",
2166 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002167 }
2168 if (type.isStructureContainingType(EbtBool))
2169 {
2170 error(qualifierLocation, "cannot be a structure containing a bool",
2171 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002172 }
2173 }
2174}
2175
Martin Radev2cc85b32016-08-05 16:22:53 +03002176void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2177{
2178 if (qualifier.getType() == QtStorage)
2179 {
2180 const TStorageQualifierWrapper &storageQualifier =
2181 static_cast<const TStorageQualifierWrapper &>(qualifier);
2182 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2183 !symbolTable.atGlobalLevel())
2184 {
2185 error(storageQualifier.getLine(),
2186 "Local variables can only use the const storage qualifier.",
2187 storageQualifier.getQualifierString().c_str());
2188 }
2189 }
2190}
2191
Olli Etuaho43364892017-02-13 16:00:12 +00002192void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002193 const TSourceLoc &location)
2194{
Jiajia Qinbc585152017-06-23 15:42:17 +08002195 const std::string reason(
2196 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2197 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002198 if (memoryQualifier.readonly)
2199 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002200 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002201 }
2202 if (memoryQualifier.writeonly)
2203 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002204 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002205 }
Martin Radev049edfa2016-11-11 14:35:37 +02002206 if (memoryQualifier.coherent)
2207 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002208 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002209 }
2210 if (memoryQualifier.restrictQualifier)
2211 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002212 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002213 }
2214 if (memoryQualifier.volatileQualifier)
2215 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002216 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002217 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002218}
2219
jchen104cdac9e2017-05-08 11:01:20 +08002220// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2221// intermediate tree.
2222void TParseContext::checkAtomicCounterOffsetIsNotOverlapped(TPublicType &publicType,
2223 size_t size,
2224 bool forceAppend,
2225 const TSourceLoc &loc,
2226 TType &type)
2227{
2228 auto &bindingState = mAtomicCounterBindingStates[publicType.layoutQualifier.binding];
2229 int offset;
2230 if (publicType.layoutQualifier.offset == -1 || forceAppend)
2231 {
2232 offset = bindingState.appendSpan(size);
2233 }
2234 else
2235 {
2236 offset = bindingState.insertSpan(publicType.layoutQualifier.offset, size);
2237 }
2238 if (offset == -1)
2239 {
2240 error(loc, "Offset overlapping", "atomic counter");
2241 return;
2242 }
2243 TLayoutQualifier qualifier = type.getLayoutQualifier();
2244 qualifier.offset = offset;
2245 type.setLayoutQualifier(qualifier);
2246}
2247
Olli Etuaho13389b62016-10-16 11:48:18 +01002248TIntermDeclaration *TParseContext::parseSingleDeclaration(
2249 TPublicType &publicType,
2250 const TSourceLoc &identifierOrTypeLocation,
2251 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002252{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002253 TType type(publicType);
2254 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2255 mDirectiveHandler.pragma().stdgl.invariantAll)
2256 {
2257 TQualifier qualifier = type.getQualifier();
2258
2259 // The directive handler has already taken care of rejecting invalid uses of this pragma
2260 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2261 // affected variable declarations:
2262 //
2263 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2264 // elsewhere, in TranslatorGLSL.)
2265 //
2266 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2267 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2268 // the way this is currently implemented we have to enable this compiler option before
2269 // parsing the shader and determining the shading language version it uses. If this were
2270 // implemented as a post-pass, the workaround could be more targeted.
2271 //
2272 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2273 // the specification, but there are desktop OpenGL drivers that expect that this is the
2274 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2275 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2276 {
2277 type.setInvariant(true);
2278 }
2279 }
2280
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002281 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2282 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002283
Olli Etuahobab4c082015-04-24 16:38:49 +03002284 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002285 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002286
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002287 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002288 if (emptyDeclaration)
2289 {
Martin Radevb8b01222016-11-20 23:25:53 +02002290 emptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002291 // In most cases we don't need to create a symbol node for an empty declaration.
2292 // But if the empty declaration is declaring a struct type, the symbol node will store that.
2293 if (type.getBasicType() == EbtStruct)
2294 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002295 symbol = new TIntermSymbol(0, "", type);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002296 }
jchen104cdac9e2017-05-08 11:01:20 +08002297 else if (IsAtomicCounter(publicType.getBasicType()))
2298 {
2299 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2300 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002301 }
2302 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002303 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002304 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002305
Olli Etuaho856c4972016-08-08 11:38:39 +03002306 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002307
jchen104cdac9e2017-05-08 11:01:20 +08002308 if (IsAtomicCounter(publicType.getBasicType()))
2309 {
2310
2311 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterSize, false,
2312 identifierOrTypeLocation, type);
2313 }
2314
Olli Etuaho2935c582015-04-08 14:32:06 +03002315 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002316 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002317
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002318 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002319 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002320 symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
Olli Etuaho13389b62016-10-16 11:48:18 +01002321 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002322 }
2323
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002324 TIntermDeclaration *declaration = new TIntermDeclaration();
2325 declaration->setLine(identifierOrTypeLocation);
2326 if (symbol)
2327 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002328 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002329 declaration->appendDeclarator(symbol);
2330 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002331 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002332}
2333
Olli Etuaho13389b62016-10-16 11:48:18 +01002334TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
2335 const TSourceLoc &identifierLocation,
2336 const TString &identifier,
2337 const TSourceLoc &indexLocation,
2338 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04002339{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002340 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002341
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002342 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2343 identifierLocation);
2344
2345 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002346
Olli Etuaho856c4972016-08-08 11:38:39 +03002347 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002348
Olli Etuaho8a176262016-08-16 14:23:01 +03002349 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002350
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002351 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002352
Olli Etuaho856c4972016-08-08 11:38:39 +03002353 unsigned int size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002354 // Make the type an array even if size check failed.
2355 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2356 arrayType.setArraySize(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04002357
jchen104cdac9e2017-05-08 11:01:20 +08002358 if (IsAtomicCounter(publicType.getBasicType()))
2359 {
2360 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterArrayStride * size, false,
2361 identifierLocation, arrayType);
2362 }
2363
Olli Etuaho2935c582015-04-08 14:32:06 +03002364 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002365 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002366
Olli Etuaho13389b62016-10-16 11:48:18 +01002367 TIntermDeclaration *declaration = new TIntermDeclaration();
2368 declaration->setLine(identifierLocation);
2369
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002370 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002371 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002372 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2373 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002374 declaration->appendDeclarator(symbol);
2375 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002376
Olli Etuaho13389b62016-10-16 11:48:18 +01002377 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002378}
2379
Olli Etuaho13389b62016-10-16 11:48:18 +01002380TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2381 const TSourceLoc &identifierLocation,
2382 const TString &identifier,
2383 const TSourceLoc &initLocation,
2384 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002385{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002386 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002387
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002388 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2389 identifierLocation);
2390
2391 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002392
Olli Etuaho13389b62016-10-16 11:48:18 +01002393 TIntermDeclaration *declaration = new TIntermDeclaration();
2394 declaration->setLine(identifierLocation);
2395
2396 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002397 if (executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002398 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002399 if (initNode)
2400 {
2401 declaration->appendDeclarator(initNode);
2402 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002403 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002404 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002405}
2406
Olli Etuaho13389b62016-10-16 11:48:18 +01002407TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Jamie Madillb98c3a82015-07-23 14:26:04 -04002408 TPublicType &publicType,
2409 const TSourceLoc &identifierLocation,
2410 const TString &identifier,
2411 const TSourceLoc &indexLocation,
2412 TIntermTyped *indexExpression,
2413 const TSourceLoc &initLocation,
2414 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002415{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002416 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002417
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002418 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2419 identifierLocation);
2420
2421 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002422
Olli Etuaho8a176262016-08-16 14:23:01 +03002423 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002424
2425 TPublicType arrayType(publicType);
2426
Olli Etuaho856c4972016-08-08 11:38:39 +03002427 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002428 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2429 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002430 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002431 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002432 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002433 }
2434 // Make the type an array even if size check failed.
2435 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2436 arrayType.setArraySize(size);
2437
Olli Etuaho13389b62016-10-16 11:48:18 +01002438 TIntermDeclaration *declaration = new TIntermDeclaration();
2439 declaration->setLine(identifierLocation);
2440
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002441 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002442 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002443 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002444 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002445 if (initNode)
2446 {
2447 declaration->appendDeclarator(initNode);
2448 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002449 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002450
2451 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002452}
2453
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002454TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002455 const TTypeQualifierBuilder &typeQualifierBuilder,
2456 const TSourceLoc &identifierLoc,
2457 const TString *identifier,
2458 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002459{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002460 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002461
Martin Radev70866b82016-07-22 15:27:42 +03002462 if (!typeQualifier.invariant)
2463 {
2464 error(identifierLoc, "Expected invariant", identifier->c_str());
2465 return nullptr;
2466 }
2467 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2468 {
2469 return nullptr;
2470 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002471 if (!symbol)
2472 {
2473 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002474 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002475 }
Martin Radev70866b82016-07-22 15:27:42 +03002476 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002477 {
Martin Radev70866b82016-07-22 15:27:42 +03002478 error(identifierLoc, "invariant declaration specifies qualifier",
2479 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002480 }
Martin Radev70866b82016-07-22 15:27:42 +03002481 if (typeQualifier.precision != EbpUndefined)
2482 {
2483 error(identifierLoc, "invariant declaration specifies precision",
2484 getPrecisionString(typeQualifier.precision));
2485 }
2486 if (!typeQualifier.layoutQualifier.isEmpty())
2487 {
2488 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2489 }
2490
2491 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002492 if (!variable)
2493 {
2494 return nullptr;
2495 }
Martin Radev70866b82016-07-22 15:27:42 +03002496 const TType &type = variable->getType();
2497
2498 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2499 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002500 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002501
2502 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2503
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002504 TIntermSymbol *intermSymbol = new TIntermSymbol(variable->getUniqueId(), *identifier, type);
2505 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002506
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002507 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002508}
2509
Olli Etuaho13389b62016-10-16 11:48:18 +01002510void TParseContext::parseDeclarator(TPublicType &publicType,
2511 const TSourceLoc &identifierLocation,
2512 const TString &identifier,
2513 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002514{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002515 // If the declaration starting this declarator list was empty (example: int,), some checks were
2516 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002517 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002518 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002519 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2520 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002521 }
2522
Olli Etuaho856c4972016-08-08 11:38:39 +03002523 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002524
Olli Etuaho856c4972016-08-08 11:38:39 +03002525 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002526
Olli Etuaho2935c582015-04-08 14:32:06 +03002527 TVariable *variable = nullptr;
Olli Etuaho43364892017-02-13 16:00:12 +00002528 TType type(publicType);
jchen104cdac9e2017-05-08 11:01:20 +08002529 if (IsAtomicCounter(publicType.getBasicType()))
2530 {
2531 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterSize, true,
2532 identifierLocation, type);
2533 }
Olli Etuaho43364892017-02-13 16:00:12 +00002534 declareVariable(identifierLocation, identifier, type, &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002535
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002536 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002537 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002538 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
2539 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002540 declarationOut->appendDeclarator(symbol);
2541 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002542}
2543
Olli Etuaho13389b62016-10-16 11:48:18 +01002544void TParseContext::parseArrayDeclarator(TPublicType &publicType,
2545 const TSourceLoc &identifierLocation,
2546 const TString &identifier,
2547 const TSourceLoc &arrayLocation,
2548 TIntermTyped *indexExpression,
2549 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002550{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002551 // If the declaration starting this declarator list was empty (example: int,), some checks were
2552 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002553 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002554 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002555 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2556 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002557 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002558
Olli Etuaho856c4972016-08-08 11:38:39 +03002559 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002560
Olli Etuaho856c4972016-08-08 11:38:39 +03002561 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002562
Olli Etuaho8a176262016-08-16 14:23:01 +03002563 if (checkIsValidTypeAndQualifierForArray(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002564 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002565 TType arrayType = TType(publicType);
Olli Etuaho856c4972016-08-08 11:38:39 +03002566 unsigned int size = checkIsValidArraySize(arrayLocation, indexExpression);
Olli Etuaho693c9aa2015-04-07 17:50:36 +03002567 arrayType.setArraySize(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002568
jchen104cdac9e2017-05-08 11:01:20 +08002569 if (IsAtomicCounter(publicType.getBasicType()))
2570 {
2571 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterArrayStride * size,
2572 true, identifierLocation, arrayType);
2573 }
2574
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002575 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002576 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002577
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002578 if (variable)
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002579 {
2580 TIntermSymbol *symbol =
2581 new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2582 symbol->setLine(identifierLocation);
2583 declarationOut->appendDeclarator(symbol);
2584 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002585 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002586}
2587
Olli Etuaho13389b62016-10-16 11:48:18 +01002588void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2589 const TSourceLoc &identifierLocation,
2590 const TString &identifier,
2591 const TSourceLoc &initLocation,
2592 TIntermTyped *initializer,
2593 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002594{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002595 // If the declaration starting this declarator list was empty (example: int,), some checks were
2596 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002597 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002598 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002599 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2600 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002601 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002602
Olli Etuaho856c4972016-08-08 11:38:39 +03002603 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002604
Olli Etuaho13389b62016-10-16 11:48:18 +01002605 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002606 if (executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002607 {
2608 //
2609 // build the intermediate representation
2610 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002611 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002612 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002613 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002614 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002615 }
2616}
2617
Olli Etuaho13389b62016-10-16 11:48:18 +01002618void TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
2619 const TSourceLoc &identifierLocation,
2620 const TString &identifier,
2621 const TSourceLoc &indexLocation,
2622 TIntermTyped *indexExpression,
2623 const TSourceLoc &initLocation,
2624 TIntermTyped *initializer,
2625 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002626{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002627 // If the declaration starting this declarator list was empty (example: int,), some checks were
2628 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002629 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002630 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002631 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2632 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002633 }
2634
Olli Etuaho856c4972016-08-08 11:38:39 +03002635 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002636
Olli Etuaho8a176262016-08-16 14:23:01 +03002637 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002638
2639 TPublicType arrayType(publicType);
2640
Olli Etuaho856c4972016-08-08 11:38:39 +03002641 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002642 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2643 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002644 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002645 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002646 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002647 }
2648 // Make the type an array even if size check failed.
2649 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2650 arrayType.setArraySize(size);
2651
2652 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002653 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002654 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002655 {
2656 if (initNode)
2657 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002658 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002659 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002660 }
2661}
2662
jchen104cdac9e2017-05-08 11:01:20 +08002663void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2664 const TSourceLoc &location)
2665{
2666 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2667 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2668 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2669 {
2670 error(location, "Requires both binding and offset", "layout");
2671 return;
2672 }
2673 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2674}
2675
Olli Etuahocce89652017-06-19 16:04:09 +03002676void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2677 const TPublicType &type,
2678 const TSourceLoc &loc)
2679{
2680 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2681 !getFragmentPrecisionHigh())
2682 {
2683 error(loc, "precision is not supported in fragment shader", "highp");
2684 }
2685
2686 if (!CanSetDefaultPrecisionOnType(type))
2687 {
2688 error(loc, "illegal type argument for default precision qualifier",
2689 getBasicString(type.getBasicType()));
2690 return;
2691 }
2692 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2693}
2694
Shaob5cc1192017-07-06 10:47:20 +08002695bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2696{
2697 switch (typeQualifier.layoutQualifier.primitiveType)
2698 {
2699 case EptLines:
2700 case EptLinesAdjacency:
2701 case EptTriangles:
2702 case EptTrianglesAdjacency:
2703 return typeQualifier.qualifier == EvqGeometryIn;
2704
2705 case EptLineStrip:
2706 case EptTriangleStrip:
2707 return typeQualifier.qualifier == EvqGeometryOut;
2708
2709 case EptPoints:
2710 return true;
2711
2712 default:
2713 UNREACHABLE();
2714 return false;
2715 }
2716}
2717
Jiawei Shaod8105a02017-08-08 09:54:36 +08002718void TParseContext::setGeometryShaderInputArraySizes()
2719{
2720 // TODO(jiawei.shao@intel.com): check former input array sizes match the input primitive
2721 // declaration.
2722 ASSERT(mGeometryShaderInputArraySize == 0);
2723 mGeometryShaderInputArraySize =
2724 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType);
2725}
2726
Shaob5cc1192017-07-06 10:47:20 +08002727bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2728{
2729 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2730
2731 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2732
2733 if (layoutQualifier.maxVertices != -1)
2734 {
2735 error(typeQualifier.line,
2736 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2737 return false;
2738 }
2739
2740 // Set mGeometryInputPrimitiveType if exists
2741 if (layoutQualifier.primitiveType != EptUndefined)
2742 {
2743 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2744 {
2745 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2746 return false;
2747 }
2748
2749 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2750 {
2751 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shaod8105a02017-08-08 09:54:36 +08002752 setGeometryShaderInputArraySizes();
Shaob5cc1192017-07-06 10:47:20 +08002753 }
2754 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2755 {
2756 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2757 "layout");
2758 return false;
2759 }
2760 }
2761
2762 // Set mGeometryInvocations if exists
2763 if (layoutQualifier.invocations > 0)
2764 {
2765 if (mGeometryShaderInvocations == 0)
2766 {
2767 mGeometryShaderInvocations = layoutQualifier.invocations;
2768 }
2769 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2770 {
2771 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2772 "layout");
2773 return false;
2774 }
2775 }
2776
2777 return true;
2778}
2779
2780bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2781{
2782 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2783
2784 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2785
2786 if (layoutQualifier.invocations > 0)
2787 {
2788 error(typeQualifier.line,
2789 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2790 return false;
2791 }
2792
2793 // Set mGeometryOutputPrimitiveType if exists
2794 if (layoutQualifier.primitiveType != EptUndefined)
2795 {
2796 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2797 {
2798 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2799 return false;
2800 }
2801
2802 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2803 {
2804 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2805 }
2806 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2807 {
2808 error(typeQualifier.line,
2809 "primitive doesn't match earlier output primitive declaration", "layout");
2810 return false;
2811 }
2812 }
2813
2814 // Set mGeometryMaxVertices if exists
2815 if (layoutQualifier.maxVertices > -1)
2816 {
2817 if (mGeometryShaderMaxVertices == -1)
2818 {
2819 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2820 }
2821 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2822 {
2823 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2824 "layout");
2825 return false;
2826 }
2827 }
2828
2829 return true;
2830}
2831
Martin Radev70866b82016-07-22 15:27:42 +03002832void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002833{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002834 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002835 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002836
Martin Radev70866b82016-07-22 15:27:42 +03002837 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2838 typeQualifier.line);
2839
Jamie Madillc2128ff2016-07-04 10:26:17 -04002840 // It should never be the case, but some strange parser errors can send us here.
2841 if (layoutQualifier.isEmpty())
2842 {
2843 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002844 return;
2845 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002846
Martin Radev802abe02016-08-04 17:48:32 +03002847 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002848 {
Olli Etuaho43364892017-02-13 16:00:12 +00002849 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002850 return;
2851 }
2852
Olli Etuaho43364892017-02-13 16:00:12 +00002853 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2854
2855 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002856
2857 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2858
Andrei Volykhina5527072017-03-22 16:46:30 +03002859 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
2860
jchen104cdac9e2017-05-08 11:01:20 +08002861 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
2862
Martin Radev802abe02016-08-04 17:48:32 +03002863 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002864 {
Martin Radev802abe02016-08-04 17:48:32 +03002865 if (mComputeShaderLocalSizeDeclared &&
2866 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2867 {
2868 error(typeQualifier.line, "Work group size does not match the previous declaration",
2869 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002870 return;
2871 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002872
Martin Radev802abe02016-08-04 17:48:32 +03002873 if (mShaderVersion < 310)
2874 {
2875 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002876 return;
2877 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002878
Martin Radev4c4c8e72016-08-04 12:25:34 +03002879 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03002880 {
2881 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002882 return;
2883 }
2884
2885 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
2886 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
2887
2888 const TConstantUnion *maxComputeWorkGroupSizeData =
2889 maxComputeWorkGroupSize->getConstPointer();
2890
2891 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
2892 {
2893 if (layoutQualifier.localSize[i] != -1)
2894 {
2895 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
2896 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
2897 if (mComputeShaderLocalSize[i] < 1 ||
2898 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
2899 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002900 std::stringstream reasonStream;
2901 reasonStream << "invalid value: Value must be at least 1 and no greater than "
2902 << maxComputeWorkGroupSizeValue;
2903 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03002904
Olli Etuaho4de340a2016-12-16 09:32:03 +00002905 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03002906 return;
2907 }
2908 }
2909 }
2910
2911 mComputeShaderLocalSizeDeclared = true;
2912 }
Shaob5cc1192017-07-06 10:47:20 +08002913 else if (typeQualifier.qualifier == EvqGeometryIn)
2914 {
2915 if (mShaderVersion < 310)
2916 {
2917 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
2918 return;
2919 }
2920
2921 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
2922 {
2923 return;
2924 }
2925 }
2926 else if (typeQualifier.qualifier == EvqGeometryOut)
2927 {
2928 if (mShaderVersion < 310)
2929 {
2930 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
2931 "layout");
2932 return;
2933 }
2934
2935 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
2936 {
2937 return;
2938 }
2939 }
Olli Etuaho95468d12017-05-04 11:14:34 +03002940 else if (isMultiviewExtensionEnabled() && typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00002941 {
2942 // This error is only specified in WebGL, but tightens unspecified behavior in the native
2943 // specification.
2944 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
2945 {
2946 error(typeQualifier.line, "Number of views does not match the previous declaration",
2947 "layout");
2948 return;
2949 }
2950
2951 if (layoutQualifier.numViews == -1)
2952 {
2953 error(typeQualifier.line, "No num_views specified", "layout");
2954 return;
2955 }
2956
2957 if (layoutQualifier.numViews > mMaxNumViews)
2958 {
2959 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
2960 "layout");
2961 return;
2962 }
2963
2964 mNumViews = layoutQualifier.numViews;
2965 }
Martin Radev802abe02016-08-04 17:48:32 +03002966 else
Jamie Madill1566ef72013-06-20 11:55:54 -04002967 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00002968 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03002969 {
Martin Radev802abe02016-08-04 17:48:32 +03002970 return;
2971 }
2972
Jiajia Qinbc585152017-06-23 15:42:17 +08002973 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03002974 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002975 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00002976 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03002977 return;
2978 }
2979
2980 if (mShaderVersion < 300)
2981 {
2982 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
2983 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002984 return;
2985 }
2986
Olli Etuaho09b04a22016-12-15 13:30:26 +00002987 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002988
2989 if (layoutQualifier.matrixPacking != EmpUnspecified)
2990 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002991 if (typeQualifier.qualifier == EvqUniform)
2992 {
2993 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
2994 }
2995 else if (typeQualifier.qualifier == EvqBuffer)
2996 {
2997 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
2998 }
Martin Radev802abe02016-08-04 17:48:32 +03002999 }
3000
3001 if (layoutQualifier.blockStorage != EbsUnspecified)
3002 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003003 if (typeQualifier.qualifier == EvqUniform)
3004 {
3005 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3006 }
3007 else if (typeQualifier.qualifier == EvqBuffer)
3008 {
3009 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3010 }
Martin Radev802abe02016-08-04 17:48:32 +03003011 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003012 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003013}
3014
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003015TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3016 const TFunction &function,
3017 const TSourceLoc &location,
3018 bool insertParametersToSymbolTable)
3019{
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003020 checkIsNotReserved(location, function.getName());
3021
Olli Etuahofe486322017-03-21 09:30:54 +00003022 TIntermFunctionPrototype *prototype =
3023 new TIntermFunctionPrototype(function.getReturnType(), TSymbolUniqueId(function));
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003024 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
3025 // point to the data that already exists in the symbol table.
3026 prototype->getFunctionSymbolInfo()->setFromFunction(function);
3027 prototype->setLine(location);
3028
3029 for (size_t i = 0; i < function.getParamCount(); i++)
3030 {
3031 const TConstParameter &param = function.getParam(i);
3032
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003033 TIntermSymbol *symbol = nullptr;
3034
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003035 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3036 // be used for unused args).
3037 if (param.name != nullptr)
3038 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003039 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003040 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003041 {
Olli Etuaho0f684632017-07-13 12:42:15 +03003042 TVariable *variable = symbolTable.declareVariable(param.name, *param.type);
3043 if (variable)
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003044 {
3045 symbol = new TIntermSymbol(variable->getUniqueId(), variable->getName(),
3046 variable->getType());
3047 }
3048 else
3049 {
Olli Etuaho85d624a2017-08-07 13:42:33 +03003050 error(location, "redefinition", param.name->c_str());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003051 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003052 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003053 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003054 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003055 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003056 // The parameter had no name or declaring the symbol failed - either way, add a nameless
3057 // symbol.
3058 symbol = new TIntermSymbol(0, "", *param.type);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003059 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003060 symbol->setLine(location);
3061 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003062 }
3063 return prototype;
3064}
3065
Olli Etuaho16c745a2017-01-16 17:02:27 +00003066TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3067 const TFunction &parsedFunction,
3068 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003069{
Olli Etuaho476197f2016-10-11 13:59:08 +01003070 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3071 // first declaration. Either way the instance in the symbol table is used to track whether the
3072 // function is declared multiple times.
3073 TFunction *function = static_cast<TFunction *>(
3074 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
3075 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003076 {
3077 // ESSL 1.00.17 section 4.2.7.
3078 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3079 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003080 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003081 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02003082
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003083 TIntermFunctionPrototype *prototype =
3084 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003085
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003086 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003087
3088 if (!symbolTable.atGlobalLevel())
3089 {
3090 // ESSL 3.00.4 section 4.2.4.
3091 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003092 }
3093
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003094 return prototype;
3095}
3096
Olli Etuaho336b1472016-10-05 16:37:55 +01003097TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003098 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003099 TIntermBlock *functionBody,
3100 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003101{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003102 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003103 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3104 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003105 error(location, "function does not return a value:",
3106 functionPrototype->getFunctionSymbolInfo()->getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003107 }
3108
Olli Etuahof51fdd22016-10-03 10:03:40 +01003109 if (functionBody == nullptr)
3110 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003111 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003112 functionBody->setLine(location);
3113 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003114 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003115 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003116 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003117
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003118 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003119 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003120}
3121
Olli Etuaho476197f2016-10-11 13:59:08 +01003122void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
3123 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003124 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003125{
Olli Etuaho476197f2016-10-11 13:59:08 +01003126 ASSERT(function);
3127 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003128 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01003129 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003130
3131 if (builtIn)
3132 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003133 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003134 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003135 else
Jamie Madill185fb402015-06-12 15:48:48 -04003136 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003137 TFunction *prevDec = static_cast<TFunction *>(
3138 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
3139
3140 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
3141 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
3142 // occurance.
3143 if (*function != prevDec)
3144 {
3145 // Swap the parameters of the previous declaration to the parameters of the function
3146 // definition (parameter names may differ).
3147 prevDec->swapParameters(**function);
3148
3149 // The function definition will share the same symbol as any previous declaration.
3150 *function = prevDec;
3151 }
3152
3153 if ((*function)->isDefined())
3154 {
3155 error(location, "function already has a body", (*function)->getName().c_str());
3156 }
3157
3158 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04003159 }
Jamie Madill185fb402015-06-12 15:48:48 -04003160
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003161 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01003162 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003163 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003164
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003165 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003166 setLoopNestingLevel(0);
3167}
3168
Jamie Madillb98c3a82015-07-23 14:26:04 -04003169TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003170{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003171 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003172 // We don't know at this point whether this is a function definition or a prototype.
3173 // The definition production code will check for redefinitions.
3174 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003175 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003176 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3177 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003178 //
3179 TFunction *prevDec =
3180 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303181
Martin Radevda6254b2016-12-14 17:00:36 +02003182 if (getShaderVersion() >= 300 &&
3183 symbolTable.hasUnmangledBuiltInForShaderVersion(function->getName().c_str(),
3184 getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303185 {
Martin Radevda6254b2016-12-14 17:00:36 +02003186 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303187 // Therefore overloading or redefining builtin functions is an error.
3188 error(location, "Name of a built-in function cannot be redeclared as function",
3189 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303190 }
3191 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003192 {
3193 if (prevDec->getReturnType() != function->getReturnType())
3194 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003195 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003196 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003197 }
3198 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3199 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003200 if (prevDec->getParam(i).type->getQualifier() !=
3201 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003202 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003203 error(location,
3204 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003205 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003206 }
3207 }
3208 }
3209
3210 //
3211 // Check for previously declared variables using the same name.
3212 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003213 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003214 if (prevSym)
3215 {
3216 if (!prevSym->isFunction())
3217 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003218 error(location, "redefinition of a function", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003219 }
3220 }
3221 else
3222 {
3223 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01003224 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003225 }
3226
3227 // We're at the inner scope level of the function's arguments and body statement.
3228 // Add the function prototype to the surrounding scope instead.
3229 symbolTable.getOuterLevel()->insert(function);
3230
Olli Etuaho78d13742017-01-18 13:06:10 +00003231 // Raise error message if main function takes any parameters or return anything other than void
3232 if (function->getName() == "main")
3233 {
3234 if (function->getParamCount() > 0)
3235 {
3236 error(location, "function cannot take any parameter(s)", "main");
3237 }
3238 if (function->getReturnType().getBasicType() != EbtVoid)
3239 {
3240 error(location, "main function cannot return a value",
3241 function->getReturnType().getBasicString());
3242 }
3243 }
3244
Jamie Madill185fb402015-06-12 15:48:48 -04003245 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003246 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3247 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003248 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3249 //
3250 return function;
3251}
3252
Olli Etuaho9de84a52016-06-14 17:36:01 +03003253TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
3254 const TString *name,
3255 const TSourceLoc &location)
3256{
3257 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3258 {
3259 error(location, "no qualifiers allowed for function return",
3260 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003261 }
3262 if (!type.layoutQualifier.isEmpty())
3263 {
3264 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003265 }
jchen10cc2a10e2017-05-03 14:05:12 +08003266 // make sure an opaque type is not involved as well...
3267 std::string reason(getBasicString(type.getBasicType()));
3268 reason += "s can't be function return values";
3269 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003270 if (mShaderVersion < 300)
3271 {
3272 // Array return values are forbidden, but there's also no valid syntax for declaring array
3273 // return values in ESSL 1.00.
Olli Etuaho77ba4082016-12-16 12:01:18 +00003274 ASSERT(type.arraySize == 0 || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003275
3276 if (type.isStructureContainingArrays())
3277 {
3278 // ESSL 1.00.17 section 6.1 Function Definitions
3279 error(location, "structures containing arrays can't be function return values",
3280 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003281 }
3282 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003283
3284 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003285 return new TFunction(&symbolTable, name, new TType(type));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003286}
3287
Olli Etuahocce89652017-06-19 16:04:09 +03003288TFunction *TParseContext::addNonConstructorFunc(const TString *name, const TSourceLoc &loc)
3289{
Olli Etuahocce89652017-06-19 16:04:09 +03003290 const TType *returnType = TCache::getType(EbtVoid, EbpUndefined);
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003291 return new TFunction(&symbolTable, name, returnType);
Olli Etuahocce89652017-06-19 16:04:09 +03003292}
3293
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003294TFunction *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003295{
Olli Etuahocce89652017-06-19 16:04:09 +03003296 if (mShaderVersion < 300 && publicType.array)
3297 {
3298 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3299 "[]");
3300 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003301 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003302 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003303 error(publicType.getLine(), "constructor can't be a structure definition",
3304 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003305 }
3306
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003307 TType *type = new TType(publicType);
3308 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003309 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003310 error(publicType.getLine(), "cannot construct this type",
3311 getBasicString(publicType.getBasicType()));
3312 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003313 }
3314
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003315 return new TFunction(&symbolTable, nullptr, type, EOpConstruct);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003316}
3317
Olli Etuahocce89652017-06-19 16:04:09 +03003318TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3319 const TString *name,
3320 const TSourceLoc &nameLoc)
3321{
3322 if (publicType.getBasicType() == EbtVoid)
3323 {
3324 error(nameLoc, "illegal use of type 'void'", name->c_str());
3325 }
3326 checkIsNotReserved(nameLoc, *name);
3327 TType *type = new TType(publicType);
3328 TParameter param = {name, type};
3329 return param;
3330}
3331
3332TParameter TParseContext::parseParameterArrayDeclarator(const TString *identifier,
3333 const TSourceLoc &identifierLoc,
3334 TIntermTyped *arraySize,
3335 const TSourceLoc &arrayLoc,
3336 TPublicType *type)
3337{
3338 checkIsValidTypeForArray(arrayLoc, *type);
3339 unsigned int size = checkIsValidArraySize(arrayLoc, arraySize);
3340 type->setArraySize(size);
3341 return parseParameterDeclarator(*type, identifier, identifierLoc);
3342}
3343
Jamie Madillb98c3a82015-07-23 14:26:04 -04003344// This function is used to test for the correctness of the parameters passed to various constructor
3345// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003346//
Olli Etuaho856c4972016-08-08 11:38:39 +03003347// 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 +00003348//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003349TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00003350 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303351 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003352{
Olli Etuaho856c4972016-08-08 11:38:39 +03003353 if (type.isUnsizedArray())
3354 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003355 if (arguments->empty())
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003356 {
3357 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3358 type.setArraySize(1u);
Olli Etuaho3ec75682017-07-05 17:02:55 +03003359 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003360 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003361 type.setArraySize(static_cast<unsigned int>(arguments->size()));
Olli Etuaho856c4972016-08-08 11:38:39 +03003362 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003363
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003364 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003365 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003366 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003367 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003368
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003369 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003370 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003371
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003372 // TODO(oetuaho@nvidia.com): Add support for folding array constructors.
3373 if (!constructorNode->isArray())
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003374 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003375 return constructorNode->fold(mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003376 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003377 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003378}
3379
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003380//
3381// Interface/uniform blocks
Jiawei Shaod8105a02017-08-08 09:54:36 +08003382// TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003383//
Olli Etuaho13389b62016-10-16 11:48:18 +01003384TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003385 const TTypeQualifierBuilder &typeQualifierBuilder,
3386 const TSourceLoc &nameLine,
3387 const TString &blockName,
3388 TFieldList *fieldList,
3389 const TString *instanceName,
3390 const TSourceLoc &instanceLine,
3391 TIntermTyped *arrayIndex,
3392 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003393{
Olli Etuaho856c4972016-08-08 11:38:39 +03003394 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003395
Olli Etuaho77ba4082016-12-16 12:01:18 +00003396 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003397
Jiajia Qinbc585152017-06-23 15:42:17 +08003398 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003399 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003400 error(typeQualifier.line,
3401 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3402 "3.10",
3403 getQualifierString(typeQualifier.qualifier));
3404 }
3405 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3406 {
3407 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003408 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003409 }
3410
Martin Radev70866b82016-07-22 15:27:42 +03003411 if (typeQualifier.invariant)
3412 {
3413 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3414 }
3415
Jiajia Qinbc585152017-06-23 15:42:17 +08003416 if (typeQualifier.qualifier != EvqBuffer)
3417 {
3418 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3419 }
Olli Etuaho43364892017-02-13 16:00:12 +00003420
jchen10af713a22017-04-19 09:10:56 +08003421 // add array index
3422 unsigned int arraySize = 0;
3423 if (arrayIndex != nullptr)
3424 {
3425 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3426 }
3427
3428 if (mShaderVersion < 310)
3429 {
3430 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3431 }
3432 else
3433 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003434 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3435 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003436 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003437
Andrei Volykhina5527072017-03-22 16:46:30 +03003438 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3439
Jamie Madill099c0f32013-06-20 11:55:52 -04003440 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003441 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003442
Jamie Madill099c0f32013-06-20 11:55:52 -04003443 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3444 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003445 if (typeQualifier.qualifier == EvqUniform)
3446 {
3447 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3448 }
3449 else if (typeQualifier.qualifier == EvqBuffer)
3450 {
3451 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3452 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003453 }
3454
Jamie Madill1566ef72013-06-20 11:55:54 -04003455 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3456 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003457 if (typeQualifier.qualifier == EvqUniform)
3458 {
3459 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3460 }
3461 else if (typeQualifier.qualifier == EvqBuffer)
3462 {
3463 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3464 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003465 }
3466
Olli Etuaho856c4972016-08-08 11:38:39 +03003467 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003468
Martin Radev2cc85b32016-08-05 16:22:53 +03003469 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3470
Olli Etuaho0f684632017-07-13 12:42:15 +03003471 if (!symbolTable.declareInterfaceBlockName(&blockName))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303472 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003473 error(nameLine, "redefinition of an interface block name", blockName.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003474 }
3475
Jamie Madill98493dd2013-07-08 14:39:03 -04003476 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303477 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3478 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003479 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303480 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003481 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303482 {
jchen10cc2a10e2017-05-03 14:05:12 +08003483 std::string reason("unsupported type - ");
3484 reason += fieldType->getBasicString();
3485 reason += " types are not allowed in interface blocks";
3486 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003487 }
3488
Jamie Madill98493dd2013-07-08 14:39:03 -04003489 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003490 switch (qualifier)
3491 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003492 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003493 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003494 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003495 if (typeQualifier.qualifier == EvqBuffer)
3496 {
3497 error(field->line(), "invalid qualifier on shader storage block member",
3498 getQualifierString(qualifier));
3499 }
3500 break;
3501 case EvqBuffer:
3502 if (typeQualifier.qualifier == EvqUniform)
3503 {
3504 error(field->line(), "invalid qualifier on uniform block member",
3505 getQualifierString(qualifier));
3506 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003507 break;
3508 default:
3509 error(field->line(), "invalid qualifier on interface block member",
3510 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003511 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003512 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003513
Martin Radev70866b82016-07-22 15:27:42 +03003514 if (fieldType->isInvariant())
3515 {
3516 error(field->line(), "invalid qualifier on interface block member", "invariant");
3517 }
3518
Jamie Madilla5efff92013-06-06 11:56:47 -04003519 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003520 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003521 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003522 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003523
Jamie Madill98493dd2013-07-08 14:39:03 -04003524 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003525 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003526 error(field->line(), "invalid layout qualifier: cannot be used here",
3527 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003528 }
3529
Jamie Madill98493dd2013-07-08 14:39:03 -04003530 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003531 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003532 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003533 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003534 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003535 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003536 warning(field->line(),
3537 "extraneous layout qualifier: only has an effect on matrix types",
3538 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003539 }
3540
Jamie Madill98493dd2013-07-08 14:39:03 -04003541 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003542
3543 if (typeQualifier.qualifier == EvqBuffer)
3544 {
3545 // set memory qualifiers
3546 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3547 // qualified with a memory qualifier, it is as if all of its members were declared with
3548 // the same memory qualifier.
3549 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3550 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3551 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3552 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3553 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3554 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3555 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3556 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3557 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3558 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3559 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003560 }
3561
Jamie Madillb98c3a82015-07-23 14:26:04 -04003562 TInterfaceBlock *interfaceBlock =
Shaob18c33e2017-08-16 12:37:51 +08003563 new TInterfaceBlock(&blockName, fieldList, instanceName, blockLayoutQualifier);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003564 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier,
3565 arraySize);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003566
3567 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04003568 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003569
Jamie Madill98493dd2013-07-08 14:39:03 -04003570 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003571 {
3572 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003573 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3574 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003575 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303576 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04003577
3578 // set parent pointer of the field variable
3579 fieldType->setInterfaceBlock(interfaceBlock);
3580
Olli Etuaho0f684632017-07-13 12:42:15 +03003581 TVariable *fieldVariable = symbolTable.declareVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04003582
Olli Etuaho0f684632017-07-13 12:42:15 +03003583 if (fieldVariable)
3584 {
3585 fieldVariable->setQualifier(typeQualifier.qualifier);
3586 }
3587 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303588 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003589 error(field->line(), "redefinition of an interface block member name",
3590 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003591 }
3592 }
3593 }
3594 else
3595 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003596 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003597
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003598 // add a symbol for this interface block
Olli Etuaho0f684632017-07-13 12:42:15 +03003599 TVariable *instanceTypeDef = symbolTable.declareVariable(instanceName, interfaceBlockType);
3600 if (instanceTypeDef)
3601 {
3602 instanceTypeDef->setQualifier(typeQualifier.qualifier);
3603 symbolId = instanceTypeDef->getUniqueId();
3604 }
3605 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303606 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003607 error(instanceLine, "redefinition of an interface block instance name",
3608 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003609 }
Olli Etuaho0f684632017-07-13 12:42:15 +03003610 symbolName = *instanceName;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003611 }
3612
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003613 TIntermSymbol *blockSymbol = new TIntermSymbol(symbolId, symbolName, interfaceBlockType);
3614 blockSymbol->setLine(typeQualifier.line);
Olli Etuaho13389b62016-10-16 11:48:18 +01003615 TIntermDeclaration *declaration = new TIntermDeclaration();
3616 declaration->appendDeclarator(blockSymbol);
3617 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003618
3619 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003620 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003621}
3622
Olli Etuaho383b7912016-08-05 11:22:59 +03003623void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003624{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003625 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003626
3627 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003628 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303629 if (mStructNestingLevel > 1)
3630 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003631 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003632 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003633}
3634
3635void TParseContext::exitStructDeclaration()
3636{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003637 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003638}
3639
Olli Etuaho8a176262016-08-16 14:23:01 +03003640void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003641{
Jamie Madillacb4b812016-11-07 13:50:29 -05003642 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303643 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003644 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003645 }
3646
Arun Patole7e7e68d2015-05-22 12:02:25 +05303647 if (field.type()->getBasicType() != EbtStruct)
3648 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003649 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003650 }
3651
3652 // We're already inside a structure definition at this point, so add
3653 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303654 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3655 {
Jamie Madill41a49272014-03-18 16:10:13 -04003656 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003657 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
3658 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003659 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003660 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003661 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003662 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003663}
3664
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003665//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003666// Parse an array index expression
3667//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003668TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3669 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303670 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003671{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003672 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3673 {
3674 if (baseExpression->getAsSymbolNode())
3675 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303676 error(location, " left of '[' is not of type array, matrix, or vector ",
3677 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003678 }
3679 else
3680 {
3681 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3682 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003683
Olli Etuaho3ec75682017-07-05 17:02:55 +03003684 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003685 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003686
Jiawei Shaod8105a02017-08-08 09:54:36 +08003687 if (baseExpression->getQualifier() == EvqPerVertexIn)
3688 {
3689 ASSERT(mShaderType == GL_GEOMETRY_SHADER_OES);
3690 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3691 {
3692 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3693 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3694 }
3695 }
3696
Jamie Madill21c1e452014-12-29 11:33:41 -05003697 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3698
Olli Etuaho36b05142015-11-12 13:10:42 +02003699 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3700 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3701 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3702 // index is a constant expression.
3703 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3704 {
3705 if (baseExpression->isInterfaceBlock())
3706 {
Jiawei Shaod8105a02017-08-08 09:54:36 +08003707 // TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
3708 switch (baseExpression->getQualifier())
3709 {
3710 case EvqPerVertexIn:
3711 break;
3712 case EvqUniform:
3713 case EvqBuffer:
3714 error(location,
3715 "array indexes for uniform block arrays and shader storage block arrays "
3716 "must be constant integral expressions",
3717 "[");
3718 break;
3719 default:
3720 UNREACHABLE();
3721 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003722 }
3723 else if (baseExpression->getQualifier() == EvqFragmentOut)
3724 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003725 error(location,
3726 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003727 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003728 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3729 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003730 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003731 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003732 }
3733
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003734 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003735 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003736 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3737 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3738 // constant fold expressions that are not constant expressions). The most compatible way to
3739 // handle this case is to report a warning instead of an error and force the index to be in
3740 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003741 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003742 int index = 0;
3743 if (indexConstantUnion->getBasicType() == EbtInt)
3744 {
3745 index = indexConstantUnion->getIConst(0);
3746 }
3747 else if (indexConstantUnion->getBasicType() == EbtUInt)
3748 {
3749 index = static_cast<int>(indexConstantUnion->getUConst(0));
3750 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003751
3752 int safeIndex = -1;
3753
3754 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04003755 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003756 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003757 {
Olli Etuahodaaff1c2017-07-05 18:03:26 +03003758 if (!isExtensionEnabled("GL_EXT_draw_buffers"))
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003759 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003760 outOfRangeError(outOfRangeIndexIsError, location,
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003761 "array index for gl_FragData must be zero when "
Olli Etuaho4de340a2016-12-16 09:32:03 +00003762 "GL_EXT_draw_buffers is disabled",
3763 "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003764 safeIndex = 0;
3765 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03003766 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003767 // Only do generic out-of-range check if similar error hasn't already been reported.
3768 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003769 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003770 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3771 baseExpression->getArraySize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003772 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003773 }
3774 }
3775 else if (baseExpression->isMatrix())
3776 {
3777 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03003778 baseExpression->getType().getCols(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003779 "matrix field selection out of range");
Jamie Madill7164cf42013-07-08 13:30:59 -04003780 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003781 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04003782 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003783 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3784 baseExpression->getType().getNominalSize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003785 "vector field selection out of range");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003786 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003787
3788 ASSERT(safeIndex >= 0);
3789 // Data of constant unions can't be changed, because it may be shared with other
3790 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
3791 // sanitized object.
Olli Etuaho56229f12017-07-10 14:16:33 +03003792 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003793 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003794 TConstantUnion *safeConstantUnion = new TConstantUnion();
3795 safeConstantUnion->setIConst(safeIndex);
3796 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
Olli Etuaho56229f12017-07-10 14:16:33 +03003797 indexConstantUnion->getTypePointer()->setBasicType(EbtInt);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003798 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003799
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003800 TIntermBinary *node = new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
3801 node->setLine(location);
3802 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003803 }
Jamie Madill7164cf42013-07-08 13:30:59 -04003804 else
3805 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003806 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
3807 node->setLine(location);
3808 // Indirect indexing can never be constant folded.
3809 return node;
Jamie Madill7164cf42013-07-08 13:30:59 -04003810 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003811}
3812
Olli Etuaho90892fb2016-07-14 14:44:51 +03003813int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
3814 const TSourceLoc &location,
3815 int index,
3816 int arraySize,
Olli Etuaho4de340a2016-12-16 09:32:03 +00003817 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003818{
3819 if (index >= arraySize || index < 0)
3820 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003821 std::stringstream reasonStream;
3822 reasonStream << reason << " '" << index << "'";
3823 std::string token = reasonStream.str();
3824 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuaho90892fb2016-07-14 14:44:51 +03003825 if (index < 0)
3826 {
3827 return 0;
3828 }
3829 else
3830 {
3831 return arraySize - 1;
3832 }
3833 }
3834 return index;
3835}
3836
Jamie Madillb98c3a82015-07-23 14:26:04 -04003837TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3838 const TSourceLoc &dotLocation,
3839 const TString &fieldString,
3840 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003841{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003842 if (baseExpression->isArray())
3843 {
3844 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003845 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003846 }
3847
3848 if (baseExpression->isVector())
3849 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003850 TVector<int> fieldOffsets;
3851 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
3852 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003853 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003854 fieldOffsets.resize(1);
3855 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003856 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003857 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
3858 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003859
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003860 return node->fold();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003861 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003862 else if (baseExpression->getBasicType() == EbtStruct)
3863 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303864 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003865 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003866 {
3867 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003868 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003869 }
3870 else
3871 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003872 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003873 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003874 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003875 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003876 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003877 {
3878 fieldFound = true;
3879 break;
3880 }
3881 }
3882 if (fieldFound)
3883 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003884 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003885 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003886 TIntermBinary *node =
3887 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
3888 node->setLine(dotLocation);
3889 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003890 }
3891 else
3892 {
3893 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003894 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003895 }
3896 }
3897 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003898 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003899 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303900 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003901 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003902 {
3903 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003904 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003905 }
3906 else
3907 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003908 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003909 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003910 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003911 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003912 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003913 {
3914 fieldFound = true;
3915 break;
3916 }
3917 }
3918 if (fieldFound)
3919 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003920 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003921 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003922 TIntermBinary *node =
3923 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
3924 node->setLine(dotLocation);
3925 // Indexing interface blocks can never be constant folded.
3926 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003927 }
3928 else
3929 {
3930 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003931 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003932 }
3933 }
3934 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003935 else
3936 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003937 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003938 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03003939 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303940 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003941 }
3942 else
3943 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303944 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03003945 " field selection requires structure, vector, or interface block on left hand "
3946 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05303947 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003948 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003949 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003950 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003951}
3952
Jamie Madillb98c3a82015-07-23 14:26:04 -04003953TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
3954 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003955{
Martin Radev802abe02016-08-04 17:48:32 +03003956 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003957
3958 if (qualifierType == "shared")
3959 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003960 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003961 {
3962 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
3963 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003964 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003965 }
3966 else if (qualifierType == "packed")
3967 {
Jamie Madillacb4b812016-11-07 13:50:29 -05003968 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07003969 {
3970 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
3971 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003972 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003973 }
3974 else if (qualifierType == "std140")
3975 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003976 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003977 }
3978 else if (qualifierType == "row_major")
3979 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003980 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003981 }
3982 else if (qualifierType == "column_major")
3983 {
Jamie Madilla5efff92013-06-06 11:56:47 -04003984 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003985 }
3986 else if (qualifierType == "location")
3987 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003988 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
3989 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00003990 }
Andrei Volykhina5527072017-03-22 16:46:30 +03003991 else if (qualifierType == "yuv" && isExtensionEnabled("GL_EXT_YUV_target") &&
3992 mShaderType == GL_FRAGMENT_SHADER)
3993 {
3994 qualifier.yuv = true;
3995 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003996 else if (qualifierType == "rgba32f")
3997 {
3998 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
3999 qualifier.imageInternalFormat = EiifRGBA32F;
4000 }
4001 else if (qualifierType == "rgba16f")
4002 {
4003 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4004 qualifier.imageInternalFormat = EiifRGBA16F;
4005 }
4006 else if (qualifierType == "r32f")
4007 {
4008 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4009 qualifier.imageInternalFormat = EiifR32F;
4010 }
4011 else if (qualifierType == "rgba8")
4012 {
4013 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4014 qualifier.imageInternalFormat = EiifRGBA8;
4015 }
4016 else if (qualifierType == "rgba8_snorm")
4017 {
4018 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4019 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4020 }
4021 else if (qualifierType == "rgba32i")
4022 {
4023 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4024 qualifier.imageInternalFormat = EiifRGBA32I;
4025 }
4026 else if (qualifierType == "rgba16i")
4027 {
4028 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4029 qualifier.imageInternalFormat = EiifRGBA16I;
4030 }
4031 else if (qualifierType == "rgba8i")
4032 {
4033 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4034 qualifier.imageInternalFormat = EiifRGBA8I;
4035 }
4036 else if (qualifierType == "r32i")
4037 {
4038 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4039 qualifier.imageInternalFormat = EiifR32I;
4040 }
4041 else if (qualifierType == "rgba32ui")
4042 {
4043 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4044 qualifier.imageInternalFormat = EiifRGBA32UI;
4045 }
4046 else if (qualifierType == "rgba16ui")
4047 {
4048 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4049 qualifier.imageInternalFormat = EiifRGBA16UI;
4050 }
4051 else if (qualifierType == "rgba8ui")
4052 {
4053 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4054 qualifier.imageInternalFormat = EiifRGBA8UI;
4055 }
4056 else if (qualifierType == "r32ui")
4057 {
4058 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4059 qualifier.imageInternalFormat = EiifR32UI;
4060 }
Shaob5cc1192017-07-06 10:47:20 +08004061 else if (qualifierType == "points" && isExtensionEnabled("GL_OES_geometry_shader") &&
4062 mShaderType == GL_GEOMETRY_SHADER_OES)
4063 {
4064 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4065 qualifier.primitiveType = EptPoints;
4066 }
4067 else if (qualifierType == "lines" && isExtensionEnabled("GL_OES_geometry_shader") &&
4068 mShaderType == GL_GEOMETRY_SHADER_OES)
4069 {
4070 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4071 qualifier.primitiveType = EptLines;
4072 }
4073 else if (qualifierType == "lines_adjacency" && isExtensionEnabled("GL_OES_geometry_shader") &&
4074 mShaderType == GL_GEOMETRY_SHADER_OES)
4075 {
4076 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4077 qualifier.primitiveType = EptLinesAdjacency;
4078 }
4079 else if (qualifierType == "triangles" && isExtensionEnabled("GL_OES_geometry_shader") &&
4080 mShaderType == GL_GEOMETRY_SHADER_OES)
4081 {
4082 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4083 qualifier.primitiveType = EptTriangles;
4084 }
4085 else if (qualifierType == "triangles_adjacency" &&
4086 isExtensionEnabled("GL_OES_geometry_shader") && mShaderType == GL_GEOMETRY_SHADER_OES)
4087 {
4088 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4089 qualifier.primitiveType = EptTrianglesAdjacency;
4090 }
4091 else if (qualifierType == "line_strip" && isExtensionEnabled("GL_OES_geometry_shader") &&
4092 mShaderType == GL_GEOMETRY_SHADER_OES)
4093 {
4094 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4095 qualifier.primitiveType = EptLineStrip;
4096 }
4097 else if (qualifierType == "triangle_strip" && isExtensionEnabled("GL_OES_geometry_shader") &&
4098 mShaderType == GL_GEOMETRY_SHADER_OES)
4099 {
4100 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4101 qualifier.primitiveType = EptTriangleStrip;
4102 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004103
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004104 else
4105 {
4106 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004107 }
4108
Jamie Madilla5efff92013-06-06 11:56:47 -04004109 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004110}
4111
Martin Radev802abe02016-08-04 17:48:32 +03004112void TParseContext::parseLocalSize(const TString &qualifierType,
4113 const TSourceLoc &qualifierTypeLine,
4114 int intValue,
4115 const TSourceLoc &intValueLine,
4116 const std::string &intValueString,
4117 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004118 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004119{
Olli Etuaho856c4972016-08-08 11:38:39 +03004120 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004121 if (intValue < 1)
4122 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004123 std::stringstream reasonStream;
4124 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4125 std::string reason = reasonStream.str();
4126 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004127 }
4128 (*localSize)[index] = intValue;
4129}
4130
Olli Etuaho09b04a22016-12-15 13:30:26 +00004131void TParseContext::parseNumViews(int intValue,
4132 const TSourceLoc &intValueLine,
4133 const std::string &intValueString,
4134 int *numViews)
4135{
4136 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4137 // specification.
4138 if (intValue < 1)
4139 {
4140 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4141 }
4142 *numViews = intValue;
4143}
4144
Shaob5cc1192017-07-06 10:47:20 +08004145void TParseContext::parseInvocations(int intValue,
4146 const TSourceLoc &intValueLine,
4147 const std::string &intValueString,
4148 int *numInvocations)
4149{
4150 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4151 // it doesn't make sense to accept invocations <= 0.
4152 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4153 {
4154 error(intValueLine,
4155 "out of range: invocations must be in the range of [1, "
4156 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4157 intValueString.c_str());
4158 }
4159 else
4160 {
4161 *numInvocations = intValue;
4162 }
4163}
4164
4165void TParseContext::parseMaxVertices(int intValue,
4166 const TSourceLoc &intValueLine,
4167 const std::string &intValueString,
4168 int *maxVertices)
4169{
4170 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4171 // it doesn't make sense to accept max_vertices < 0.
4172 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4173 {
4174 error(
4175 intValueLine,
4176 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4177 intValueString.c_str());
4178 }
4179 else
4180 {
4181 *maxVertices = intValue;
4182 }
4183}
4184
Jamie Madillb98c3a82015-07-23 14:26:04 -04004185TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4186 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004187 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304188 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004189{
Martin Radev802abe02016-08-04 17:48:32 +03004190 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004191
Martin Radev802abe02016-08-04 17:48:32 +03004192 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004193
Martin Radev802abe02016-08-04 17:48:32 +03004194 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004195 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004196 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004197 if (intValue < 0)
4198 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004199 error(intValueLine, "out of range: location must be non-negative",
4200 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004201 }
4202 else
4203 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004204 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004205 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004206 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004207 }
Olli Etuaho43364892017-02-13 16:00:12 +00004208 else if (qualifierType == "binding")
4209 {
4210 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4211 if (intValue < 0)
4212 {
4213 error(intValueLine, "out of range: binding must be non-negative",
4214 intValueString.c_str());
4215 }
4216 else
4217 {
4218 qualifier.binding = intValue;
4219 }
4220 }
jchen104cdac9e2017-05-08 11:01:20 +08004221 else if (qualifierType == "offset")
4222 {
4223 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4224 if (intValue < 0)
4225 {
4226 error(intValueLine, "out of range: offset must be non-negative",
4227 intValueString.c_str());
4228 }
4229 else
4230 {
4231 qualifier.offset = intValue;
4232 }
4233 }
Martin Radev802abe02016-08-04 17:48:32 +03004234 else if (qualifierType == "local_size_x")
4235 {
4236 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4237 &qualifier.localSize);
4238 }
4239 else if (qualifierType == "local_size_y")
4240 {
4241 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4242 &qualifier.localSize);
4243 }
4244 else if (qualifierType == "local_size_z")
4245 {
4246 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4247 &qualifier.localSize);
4248 }
Olli Etuaho95468d12017-05-04 11:14:34 +03004249 else if (qualifierType == "num_views" && isMultiviewExtensionEnabled() &&
Olli Etuaho09b04a22016-12-15 13:30:26 +00004250 mShaderType == GL_VERTEX_SHADER)
4251 {
4252 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4253 }
Shaob5cc1192017-07-06 10:47:20 +08004254 else if (qualifierType == "invocations" && isExtensionEnabled("GL_OES_geometry_shader") &&
4255 mShaderType == GL_GEOMETRY_SHADER_OES)
4256 {
4257 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4258 }
4259 else if (qualifierType == "max_vertices" && isExtensionEnabled("GL_OES_geometry_shader") &&
4260 mShaderType == GL_GEOMETRY_SHADER_OES)
4261 {
4262 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4263 }
4264
Martin Radev802abe02016-08-04 17:48:32 +03004265 else
4266 {
4267 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004268 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004269
Jamie Madilla5efff92013-06-06 11:56:47 -04004270 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004271}
4272
Olli Etuaho613b9592016-09-05 12:05:53 +03004273TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4274{
4275 return new TTypeQualifierBuilder(
4276 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4277 mShaderVersion);
4278}
4279
Olli Etuahocce89652017-06-19 16:04:09 +03004280TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4281 const TSourceLoc &loc)
4282{
4283 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4284 return new TStorageQualifierWrapper(qualifier, loc);
4285}
4286
4287TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4288{
4289 if (getShaderType() == GL_VERTEX_SHADER)
4290 {
4291 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4292 }
4293 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4294}
4295
4296TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4297{
4298 if (declaringFunction())
4299 {
4300 return new TStorageQualifierWrapper(EvqIn, loc);
4301 }
Shaob5cc1192017-07-06 10:47:20 +08004302
4303 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004304 {
Shaob5cc1192017-07-06 10:47:20 +08004305 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004306 {
Shaob5cc1192017-07-06 10:47:20 +08004307 if (mShaderVersion < 300 && !isMultiviewExtensionEnabled())
4308 {
4309 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4310 }
4311 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004312 }
Shaob5cc1192017-07-06 10:47:20 +08004313 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004314 {
Shaob5cc1192017-07-06 10:47:20 +08004315 if (mShaderVersion < 300)
4316 {
4317 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4318 }
4319 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004320 }
Shaob5cc1192017-07-06 10:47:20 +08004321 case GL_COMPUTE_SHADER:
4322 {
4323 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4324 }
4325 case GL_GEOMETRY_SHADER_OES:
4326 {
4327 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4328 }
4329 default:
4330 {
4331 UNREACHABLE();
4332 return new TStorageQualifierWrapper(EvqLast, loc);
4333 }
Olli Etuahocce89652017-06-19 16:04:09 +03004334 }
Olli Etuahocce89652017-06-19 16:04:09 +03004335}
4336
4337TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4338{
4339 if (declaringFunction())
4340 {
4341 return new TStorageQualifierWrapper(EvqOut, loc);
4342 }
Shaob5cc1192017-07-06 10:47:20 +08004343 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004344 {
Shaob5cc1192017-07-06 10:47:20 +08004345 case GL_VERTEX_SHADER:
4346 {
4347 if (mShaderVersion < 300)
4348 {
4349 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4350 }
4351 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4352 }
4353 case GL_FRAGMENT_SHADER:
4354 {
4355 if (mShaderVersion < 300)
4356 {
4357 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4358 }
4359 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4360 }
4361 case GL_COMPUTE_SHADER:
4362 {
4363 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4364 return new TStorageQualifierWrapper(EvqLast, loc);
4365 }
4366 case GL_GEOMETRY_SHADER_OES:
4367 {
4368 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4369 }
4370 default:
4371 {
4372 UNREACHABLE();
4373 return new TStorageQualifierWrapper(EvqLast, loc);
4374 }
Olli Etuahocce89652017-06-19 16:04:09 +03004375 }
Olli Etuahocce89652017-06-19 16:04:09 +03004376}
4377
4378TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4379{
4380 if (!declaringFunction())
4381 {
4382 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4383 }
4384 return new TStorageQualifierWrapper(EvqInOut, loc);
4385}
4386
Jamie Madillb98c3a82015-07-23 14:26:04 -04004387TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004388 TLayoutQualifier rightQualifier,
4389 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004390{
Martin Radevc28888b2016-07-22 15:27:42 +03004391 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004392 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004393}
4394
Olli Etuahocce89652017-06-19 16:04:09 +03004395TField *TParseContext::parseStructDeclarator(TString *identifier, const TSourceLoc &loc)
4396{
4397 checkIsNotReserved(loc, *identifier);
4398 TType *type = new TType(EbtVoid, EbpUndefined);
4399 return new TField(type, identifier, loc);
4400}
4401
4402TField *TParseContext::parseStructArrayDeclarator(TString *identifier,
4403 const TSourceLoc &loc,
4404 TIntermTyped *arraySize,
4405 const TSourceLoc &arraySizeLoc)
4406{
4407 checkIsNotReserved(loc, *identifier);
4408
4409 TType *type = new TType(EbtVoid, EbpUndefined);
4410 unsigned int size = checkIsValidArraySize(arraySizeLoc, arraySize);
4411 type->setArraySize(size);
4412
4413 return new TField(type, identifier, loc);
4414}
4415
Olli Etuaho4de340a2016-12-16 09:32:03 +00004416TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4417 const TFieldList *newlyAddedFields,
4418 const TSourceLoc &location)
4419{
4420 for (TField *field : *newlyAddedFields)
4421 {
4422 for (TField *oldField : *processedFields)
4423 {
4424 if (oldField->name() == field->name())
4425 {
4426 error(location, "duplicate field name in structure", field->name().c_str());
4427 }
4428 }
4429 processedFields->push_back(field);
4430 }
4431 return processedFields;
4432}
4433
Martin Radev70866b82016-07-22 15:27:42 +03004434TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4435 const TTypeQualifierBuilder &typeQualifierBuilder,
4436 TPublicType *typeSpecifier,
4437 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004438{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004439 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004440
Martin Radev70866b82016-07-22 15:27:42 +03004441 typeSpecifier->qualifier = typeQualifier.qualifier;
4442 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004443 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004444 typeSpecifier->invariant = typeQualifier.invariant;
4445 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304446 {
Martin Radev70866b82016-07-22 15:27:42 +03004447 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004448 }
Martin Radev70866b82016-07-22 15:27:42 +03004449 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004450}
4451
Jamie Madillb98c3a82015-07-23 14:26:04 -04004452TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
4453 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004454{
Martin Radev4a9cd802016-09-01 16:51:51 +03004455 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4456 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004457
Martin Radev4a9cd802016-09-01 16:51:51 +03004458 checkIsNonVoid(typeSpecifier.getLine(), (*fieldList)[0]->name(), typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004459
Martin Radev4a9cd802016-09-01 16:51:51 +03004460 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004461
Arun Patole7e7e68d2015-05-22 12:02:25 +05304462 for (unsigned int i = 0; i < fieldList->size(); ++i)
4463 {
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004464 //
4465 // Careful not to replace already known aspects of type, like array-ness
4466 //
Arun Patole7e7e68d2015-05-22 12:02:25 +05304467 TType *type = (*fieldList)[i]->type();
Martin Radev4a9cd802016-09-01 16:51:51 +03004468 type->setBasicType(typeSpecifier.getBasicType());
4469 type->setPrimarySize(typeSpecifier.getPrimarySize());
4470 type->setSecondarySize(typeSpecifier.getSecondarySize());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004471 type->setPrecision(typeSpecifier.precision);
4472 type->setQualifier(typeSpecifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04004473 type->setLayoutQualifier(typeSpecifier.layoutQualifier);
Martin Radev2cc85b32016-08-05 16:22:53 +03004474 type->setMemoryQualifier(typeSpecifier.memoryQualifier);
Martin Radev70866b82016-07-22 15:27:42 +03004475 type->setInvariant(typeSpecifier.invariant);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004476
4477 // don't allow arrays of arrays
Arun Patole7e7e68d2015-05-22 12:02:25 +05304478 if (type->isArray())
4479 {
Martin Radev4a9cd802016-09-01 16:51:51 +03004480 checkIsValidTypeForArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004481 }
4482 if (typeSpecifier.array)
Olli Etuaho856c4972016-08-08 11:38:39 +03004483 type->setArraySize(static_cast<unsigned int>(typeSpecifier.arraySize));
Martin Radev4a9cd802016-09-01 16:51:51 +03004484 if (typeSpecifier.getUserDef())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304485 {
Olli Etuaho0f684632017-07-13 12:42:15 +03004486 type->setStruct(typeSpecifier.getUserDef());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004487 }
4488
Martin Radev4a9cd802016-09-01 16:51:51 +03004489 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*fieldList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004490 }
4491
Jamie Madill98493dd2013-07-08 14:39:03 -04004492 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004493}
4494
Martin Radev4a9cd802016-09-01 16:51:51 +03004495TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4496 const TSourceLoc &nameLine,
4497 const TString *structName,
4498 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004499{
Olli Etuahoa5e693a2017-07-13 16:07:26 +03004500 TStructure *structure = new TStructure(&symbolTable, structName, fieldList);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004501
Jamie Madill9b820842015-02-12 10:40:10 -05004502 // Store a bool in the struct if we're at global scope, to allow us to
4503 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004504 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004505
Jamie Madill98493dd2013-07-08 14:39:03 -04004506 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004507 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004508 checkIsNotReserved(nameLine, *structName);
Olli Etuaho0f684632017-07-13 12:42:15 +03004509 if (!symbolTable.declareStructType(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304510 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004511 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004512 }
4513 }
4514
4515 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004516 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004517 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004518 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004519 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004520 switch (qualifier)
4521 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004522 case EvqGlobal:
4523 case EvqTemporary:
4524 break;
4525 default:
4526 error(field.line(), "invalid qualifier on struct member",
4527 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004528 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004529 }
Martin Radev70866b82016-07-22 15:27:42 +03004530 if (field.type()->isInvariant())
4531 {
4532 error(field.line(), "invalid qualifier on struct member", "invariant");
4533 }
jchen104cdac9e2017-05-08 11:01:20 +08004534 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4535 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004536 {
4537 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4538 }
4539
Olli Etuaho43364892017-02-13 16:00:12 +00004540 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4541
4542 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004543
4544 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004545 }
4546
Martin Radev4a9cd802016-09-01 16:51:51 +03004547 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004548 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004549 exitStructDeclaration();
4550
Martin Radev4a9cd802016-09-01 16:51:51 +03004551 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004552}
4553
Jamie Madillb98c3a82015-07-23 14:26:04 -04004554TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004555 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004556 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004557{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004558 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004559 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004560 init->isVector())
4561 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004562 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4563 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004564 return nullptr;
4565 }
4566
Olli Etuahoac5274d2015-02-20 10:19:08 +02004567 if (statementList)
4568 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00004569 if (!ValidateSwitchStatementList(switchType, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004570 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02004571 return nullptr;
4572 }
4573 }
4574
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004575 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4576 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004577 return node;
4578}
4579
4580TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4581{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004582 if (mSwitchNestingLevel == 0)
4583 {
4584 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004585 return nullptr;
4586 }
4587 if (condition == nullptr)
4588 {
4589 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004590 return nullptr;
4591 }
4592 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004593 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004594 {
4595 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004596 }
4597 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004598 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4599 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4600 // fold in case labels.
4601 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004602 {
4603 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004604 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004605 TIntermCase *node = new TIntermCase(condition);
4606 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004607 return node;
4608}
4609
4610TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4611{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004612 if (mSwitchNestingLevel == 0)
4613 {
4614 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004615 return nullptr;
4616 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004617 TIntermCase *node = new TIntermCase(nullptr);
4618 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004619 return node;
4620}
4621
Jamie Madillb98c3a82015-07-23 14:26:04 -04004622TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4623 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004624 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004625{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004626 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004627
4628 switch (op)
4629 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004630 case EOpLogicalNot:
4631 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4632 child->isVector())
4633 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004634 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004635 return nullptr;
4636 }
4637 break;
4638 case EOpBitwiseNot:
4639 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4640 child->isMatrix() || child->isArray())
4641 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004642 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004643 return nullptr;
4644 }
4645 break;
4646 case EOpPostIncrement:
4647 case EOpPreIncrement:
4648 case EOpPostDecrement:
4649 case EOpPreDecrement:
4650 case EOpNegative:
4651 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004652 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4653 child->getBasicType() == EbtBool || child->isArray() ||
4654 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004655 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004656 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004657 return nullptr;
4658 }
4659 // Operators for built-ins are already type checked against their prototype.
4660 default:
4661 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004662 }
4663
Jiajia Qinbc585152017-06-23 15:42:17 +08004664 if (child->getMemoryQualifier().writeonly)
4665 {
4666 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4667 return nullptr;
4668 }
4669
Olli Etuahof119a262016-08-19 15:54:22 +03004670 TIntermUnary *node = new TIntermUnary(op, child);
4671 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004672
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004673 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004674}
4675
Olli Etuaho09b22472015-02-11 11:47:26 +02004676TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4677{
Olli Etuahocce89652017-06-19 16:04:09 +03004678 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004679 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004680 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004681 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004682 return child;
4683 }
4684 return node;
4685}
4686
Jamie Madillb98c3a82015-07-23 14:26:04 -04004687TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4688 TIntermTyped *child,
4689 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004690{
Olli Etuaho856c4972016-08-08 11:38:39 +03004691 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004692 return addUnaryMath(op, child, loc);
4693}
4694
Jamie Madillb98c3a82015-07-23 14:26:04 -04004695bool TParseContext::binaryOpCommonCheck(TOperator op,
4696 TIntermTyped *left,
4697 TIntermTyped *right,
4698 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004699{
jchen10b4cf5652017-05-05 18:51:17 +08004700 // Check opaque types are not allowed to be operands in expressions other than array indexing
4701 // and structure member selection.
4702 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4703 {
4704 switch (op)
4705 {
4706 case EOpIndexDirect:
4707 case EOpIndexIndirect:
4708 break;
4709 case EOpIndexDirectStruct:
4710 UNREACHABLE();
4711
4712 default:
4713 error(loc, "Invalid operation for variables with an opaque type",
4714 GetOperatorString(op));
4715 return false;
4716 }
4717 }
jchen10cc2a10e2017-05-03 14:05:12 +08004718
Jiajia Qinbc585152017-06-23 15:42:17 +08004719 if (right->getMemoryQualifier().writeonly)
4720 {
4721 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4722 return false;
4723 }
4724
4725 if (left->getMemoryQualifier().writeonly)
4726 {
4727 switch (op)
4728 {
4729 case EOpAssign:
4730 case EOpInitialize:
4731 case EOpIndexDirect:
4732 case EOpIndexIndirect:
4733 case EOpIndexDirectStruct:
4734 case EOpIndexDirectInterfaceBlock:
4735 break;
4736 default:
4737 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4738 return false;
4739 }
4740 }
4741
Olli Etuaho244be012016-08-18 15:26:02 +03004742 if (left->getType().getStruct() || right->getType().getStruct())
4743 {
4744 switch (op)
4745 {
4746 case EOpIndexDirectStruct:
4747 ASSERT(left->getType().getStruct());
4748 break;
4749 case EOpEqual:
4750 case EOpNotEqual:
4751 case EOpAssign:
4752 case EOpInitialize:
4753 if (left->getType() != right->getType())
4754 {
4755 return false;
4756 }
4757 break;
4758 default:
4759 error(loc, "Invalid operation for structs", GetOperatorString(op));
4760 return false;
4761 }
4762 }
4763
Olli Etuaho94050052017-05-08 14:17:44 +03004764 if (left->isInterfaceBlock() || right->isInterfaceBlock())
4765 {
4766 switch (op)
4767 {
4768 case EOpIndexDirectInterfaceBlock:
4769 ASSERT(left->getType().getInterfaceBlock());
4770 break;
4771 default:
4772 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
4773 return false;
4774 }
4775 }
4776
Olli Etuahod6b14282015-03-17 14:31:35 +02004777 if (left->isArray() || right->isArray())
4778 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004779 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02004780 {
4781 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4782 return false;
4783 }
4784
4785 if (left->isArray() != right->isArray())
4786 {
4787 error(loc, "array / non-array mismatch", GetOperatorString(op));
4788 return false;
4789 }
4790
4791 switch (op)
4792 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004793 case EOpEqual:
4794 case EOpNotEqual:
4795 case EOpAssign:
4796 case EOpInitialize:
4797 break;
4798 default:
4799 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4800 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02004801 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03004802 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuahoe79904c2015-03-18 16:56:42 +02004803 if (left->getArraySize() != right->getArraySize())
4804 {
4805 error(loc, "array size mismatch", GetOperatorString(op));
4806 return false;
4807 }
Olli Etuahod6b14282015-03-17 14:31:35 +02004808 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004809
4810 // Check ops which require integer / ivec parameters
4811 bool isBitShift = false;
4812 switch (op)
4813 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004814 case EOpBitShiftLeft:
4815 case EOpBitShiftRight:
4816 case EOpBitShiftLeftAssign:
4817 case EOpBitShiftRightAssign:
4818 // Unsigned can be bit-shifted by signed and vice versa, but we need to
4819 // check that the basic type is an integer type.
4820 isBitShift = true;
4821 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
4822 {
4823 return false;
4824 }
4825 break;
4826 case EOpBitwiseAnd:
4827 case EOpBitwiseXor:
4828 case EOpBitwiseOr:
4829 case EOpBitwiseAndAssign:
4830 case EOpBitwiseXorAssign:
4831 case EOpBitwiseOrAssign:
4832 // It is enough to check the type of only one operand, since later it
4833 // is checked that the operand types match.
4834 if (!IsInteger(left->getBasicType()))
4835 {
4836 return false;
4837 }
4838 break;
4839 default:
4840 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004841 }
4842
4843 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
4844 // So the basic type should usually match.
4845 if (!isBitShift && left->getBasicType() != right->getBasicType())
4846 {
4847 return false;
4848 }
4849
Olli Etuaho63e1ec52016-08-18 22:05:12 +03004850 // Check that:
4851 // 1. Type sizes match exactly on ops that require that.
4852 // 2. Restrictions for structs that contain arrays or samplers are respected.
4853 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04004854 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004855 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004856 case EOpAssign:
4857 case EOpInitialize:
4858 case EOpEqual:
4859 case EOpNotEqual:
4860 // ESSL 1.00 sections 5.7, 5.8, 5.9
4861 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
4862 {
4863 error(loc, "undefined operation for structs containing arrays",
4864 GetOperatorString(op));
4865 return false;
4866 }
4867 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
4868 // we interpret the spec so that this extends to structs containing samplers,
4869 // similarly to ESSL 1.00 spec.
4870 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
4871 left->getType().isStructureContainingSamplers())
4872 {
4873 error(loc, "undefined operation for structs containing samplers",
4874 GetOperatorString(op));
4875 return false;
4876 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004877
Olli Etuahoe1805592017-01-02 16:41:20 +00004878 if ((left->getNominalSize() != right->getNominalSize()) ||
4879 (left->getSecondarySize() != right->getSecondarySize()))
4880 {
4881 error(loc, "dimension mismatch", GetOperatorString(op));
4882 return false;
4883 }
4884 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004885 case EOpLessThan:
4886 case EOpGreaterThan:
4887 case EOpLessThanEqual:
4888 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00004889 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04004890 {
Olli Etuahoe1805592017-01-02 16:41:20 +00004891 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004892 return false;
4893 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03004894 break;
4895 case EOpAdd:
4896 case EOpSub:
4897 case EOpDiv:
4898 case EOpIMod:
4899 case EOpBitShiftLeft:
4900 case EOpBitShiftRight:
4901 case EOpBitwiseAnd:
4902 case EOpBitwiseXor:
4903 case EOpBitwiseOr:
4904 case EOpAddAssign:
4905 case EOpSubAssign:
4906 case EOpDivAssign:
4907 case EOpIModAssign:
4908 case EOpBitShiftLeftAssign:
4909 case EOpBitShiftRightAssign:
4910 case EOpBitwiseAndAssign:
4911 case EOpBitwiseXorAssign:
4912 case EOpBitwiseOrAssign:
4913 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
4914 {
4915 return false;
4916 }
4917
4918 // Are the sizes compatible?
4919 if (left->getNominalSize() != right->getNominalSize() ||
4920 left->getSecondarySize() != right->getSecondarySize())
4921 {
4922 // If the nominal sizes of operands do not match:
4923 // One of them must be a scalar.
4924 if (!left->isScalar() && !right->isScalar())
4925 return false;
4926
4927 // In the case of compound assignment other than multiply-assign,
4928 // the right side needs to be a scalar. Otherwise a vector/matrix
4929 // would be assigned to a scalar. A scalar can't be shifted by a
4930 // vector either.
4931 if (!right->isScalar() &&
4932 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
4933 return false;
4934 }
4935 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004936 default:
4937 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004938 }
4939
Olli Etuahod6b14282015-03-17 14:31:35 +02004940 return true;
4941}
4942
Olli Etuaho1dded802016-08-18 18:13:13 +03004943bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
4944 const TType &left,
4945 const TType &right)
4946{
4947 switch (op)
4948 {
4949 case EOpMul:
4950 case EOpMulAssign:
4951 return left.getNominalSize() == right.getNominalSize() &&
4952 left.getSecondarySize() == right.getSecondarySize();
4953 case EOpVectorTimesScalar:
4954 return true;
4955 case EOpVectorTimesScalarAssign:
4956 ASSERT(!left.isMatrix() && !right.isMatrix());
4957 return left.isVector() && !right.isVector();
4958 case EOpVectorTimesMatrix:
4959 return left.getNominalSize() == right.getRows();
4960 case EOpVectorTimesMatrixAssign:
4961 ASSERT(!left.isMatrix() && right.isMatrix());
4962 return left.isVector() && left.getNominalSize() == right.getRows() &&
4963 left.getNominalSize() == right.getCols();
4964 case EOpMatrixTimesVector:
4965 return left.getCols() == right.getNominalSize();
4966 case EOpMatrixTimesScalar:
4967 return true;
4968 case EOpMatrixTimesScalarAssign:
4969 ASSERT(left.isMatrix() && !right.isMatrix());
4970 return !right.isVector();
4971 case EOpMatrixTimesMatrix:
4972 return left.getCols() == right.getRows();
4973 case EOpMatrixTimesMatrixAssign:
4974 ASSERT(left.isMatrix() && right.isMatrix());
4975 // We need to check two things:
4976 // 1. The matrix multiplication step is valid.
4977 // 2. The result will have the same number of columns as the lvalue.
4978 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
4979
4980 default:
4981 UNREACHABLE();
4982 return false;
4983 }
4984}
4985
Jamie Madillb98c3a82015-07-23 14:26:04 -04004986TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
4987 TIntermTyped *left,
4988 TIntermTyped *right,
4989 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02004990{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004991 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02004992 return nullptr;
4993
Olli Etuahofc1806e2015-03-17 13:03:11 +02004994 switch (op)
4995 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004996 case EOpEqual:
4997 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04004998 case EOpLessThan:
4999 case EOpGreaterThan:
5000 case EOpLessThanEqual:
5001 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005002 break;
5003 case EOpLogicalOr:
5004 case EOpLogicalXor:
5005 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005006 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5007 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005008 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005009 {
5010 return nullptr;
5011 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005012 // Basic types matching should have been already checked.
5013 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005014 break;
5015 case EOpAdd:
5016 case EOpSub:
5017 case EOpDiv:
5018 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005019 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5020 !right->getType().getStruct());
5021 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005022 {
5023 return nullptr;
5024 }
5025 break;
5026 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005027 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5028 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005029 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005030 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005031 {
5032 return nullptr;
5033 }
5034 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005035 default:
5036 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005037 }
5038
Olli Etuaho1dded802016-08-18 18:13:13 +03005039 if (op == EOpMul)
5040 {
5041 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5042 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5043 {
5044 return nullptr;
5045 }
5046 }
5047
Olli Etuaho3fdec912016-08-18 15:08:06 +03005048 TIntermBinary *node = new TIntermBinary(op, left, right);
5049 node->setLine(loc);
5050
Olli Etuaho3fdec912016-08-18 15:08:06 +03005051 // See if we can fold constants.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005052 return node->fold(mDiagnostics);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005053}
5054
Jamie Madillb98c3a82015-07-23 14:26:04 -04005055TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5056 TIntermTyped *left,
5057 TIntermTyped *right,
5058 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005059{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005060 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005061 if (node == 0)
5062 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005063 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5064 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005065 return left;
5066 }
5067 return node;
5068}
5069
Jamie Madillb98c3a82015-07-23 14:26:04 -04005070TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5071 TIntermTyped *left,
5072 TIntermTyped *right,
5073 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005074{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005075 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005076 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005077 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005078 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5079 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005080 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005081 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005082 }
5083 return node;
5084}
5085
Olli Etuaho13389b62016-10-16 11:48:18 +01005086TIntermBinary *TParseContext::createAssign(TOperator op,
5087 TIntermTyped *left,
5088 TIntermTyped *right,
5089 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005090{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005091 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005092 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005093 if (op == EOpMulAssign)
5094 {
5095 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5096 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5097 {
5098 return nullptr;
5099 }
5100 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005101 TIntermBinary *node = new TIntermBinary(op, left, right);
5102 node->setLine(loc);
5103
Olli Etuaho3fdec912016-08-18 15:08:06 +03005104 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005105 }
5106 return nullptr;
5107}
5108
Jamie Madillb98c3a82015-07-23 14:26:04 -04005109TIntermTyped *TParseContext::addAssign(TOperator op,
5110 TIntermTyped *left,
5111 TIntermTyped *right,
5112 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005113{
Olli Etuahocce89652017-06-19 16:04:09 +03005114 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005115 TIntermTyped *node = createAssign(op, left, right, loc);
5116 if (node == nullptr)
5117 {
5118 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005119 return left;
5120 }
5121 return node;
5122}
5123
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005124TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5125 TIntermTyped *right,
5126 const TSourceLoc &loc)
5127{
Corentin Wallez0d959252016-07-12 17:26:32 -04005128 // WebGL2 section 5.26, the following results in an error:
5129 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005130 if (mShaderSpec == SH_WEBGL2_SPEC &&
5131 (left->isArray() || left->getBasicType() == EbtVoid ||
5132 left->getType().isStructureContainingArrays() || right->isArray() ||
5133 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005134 {
5135 error(loc,
5136 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5137 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005138 }
5139
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005140 TIntermBinary *commaNode = new TIntermBinary(EOpComma, left, right);
5141 TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(mShaderVersion, left, right);
5142 commaNode->getTypePointer()->setQualifier(resultQualifier);
5143 return commaNode->fold(mDiagnostics);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005144}
5145
Olli Etuaho49300862015-02-20 14:54:49 +02005146TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5147{
5148 switch (op)
5149 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005150 case EOpContinue:
5151 if (mLoopNestingLevel <= 0)
5152 {
5153 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005154 }
5155 break;
5156 case EOpBreak:
5157 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5158 {
5159 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005160 }
5161 break;
5162 case EOpReturn:
5163 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5164 {
5165 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005166 }
5167 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005168 case EOpKill:
5169 if (mShaderType != GL_FRAGMENT_SHADER)
5170 {
5171 error(loc, "discard supported in fragment shaders only", "discard");
5172 }
5173 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005174 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005175 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005176 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005177 }
Olli Etuahocce89652017-06-19 16:04:09 +03005178 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005179}
5180
Jamie Madillb98c3a82015-07-23 14:26:04 -04005181TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005182 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005183 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005184{
Olli Etuahocce89652017-06-19 16:04:09 +03005185 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005186 {
Olli Etuahocce89652017-06-19 16:04:09 +03005187 ASSERT(op == EOpReturn);
5188 mFunctionReturnsValue = true;
5189 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5190 {
5191 error(loc, "void function cannot return a value", "return");
5192 }
5193 else if (*mCurrentFunctionType != expression->getType())
5194 {
5195 error(loc, "function return is not matching type:", "return");
5196 }
Olli Etuaho49300862015-02-20 14:54:49 +02005197 }
Olli Etuahocce89652017-06-19 16:04:09 +03005198 TIntermBranch *node = new TIntermBranch(op, expression);
5199 node->setLine(loc);
5200 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005201}
5202
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005203void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5204{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005205 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01005206 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005207 TIntermNode *offset = nullptr;
5208 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuahoec9232b2017-03-27 17:01:37 +03005209 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
5210 name == "textureProjLodOffset" || name == "textureGradOffset" ||
5211 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005212 {
5213 offset = arguments->back();
5214 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03005215 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005216 {
5217 // A bias parameter might follow the offset parameter.
5218 ASSERT(arguments->size() >= 3);
5219 offset = (*arguments)[2];
5220 }
5221 if (offset != nullptr)
5222 {
5223 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5224 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5225 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005226 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03005227 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005228 }
5229 else
5230 {
5231 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5232 size_t size = offsetConstantUnion->getType().getObjectSize();
5233 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
5234 for (size_t i = 0u; i < size; ++i)
5235 {
5236 int offsetValue = values[i].getIConst();
5237 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
5238 {
5239 std::stringstream tokenStream;
5240 tokenStream << offsetValue;
5241 std::string token = tokenStream.str();
5242 error(offset->getLine(), "Texture offset value out of valid range",
5243 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005244 }
5245 }
5246 }
5247 }
5248}
5249
Martin Radev2cc85b32016-08-05 16:22:53 +03005250// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5251void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5252{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005253 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03005254 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5255
5256 if (name.compare(0, 5, "image") == 0)
5257 {
5258 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005259 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005260
Olli Etuaho485eefd2017-02-14 17:40:06 +00005261 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005262
5263 if (name.compare(5, 5, "Store") == 0)
5264 {
5265 if (memoryQualifier.readonly)
5266 {
5267 error(imageNode->getLine(),
5268 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005269 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005270 }
5271 }
5272 else if (name.compare(5, 4, "Load") == 0)
5273 {
5274 if (memoryQualifier.writeonly)
5275 {
5276 error(imageNode->getLine(),
5277 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005278 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005279 }
5280 }
5281 }
5282}
5283
5284// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5285void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5286 const TFunction *functionDefinition,
5287 const TIntermAggregate *functionCall)
5288{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005289 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005290
5291 const TIntermSequence &arguments = *functionCall->getSequence();
5292
5293 ASSERT(functionDefinition->getParamCount() == arguments.size());
5294
5295 for (size_t i = 0; i < arguments.size(); ++i)
5296 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005297 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5298 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005299 const TType &functionParameterType = *functionDefinition->getParam(i).type;
5300 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5301
5302 if (IsImage(functionArgumentType.getBasicType()))
5303 {
5304 const TMemoryQualifier &functionArgumentMemoryQualifier =
5305 functionArgumentType.getMemoryQualifier();
5306 const TMemoryQualifier &functionParameterMemoryQualifier =
5307 functionParameterType.getMemoryQualifier();
5308 if (functionArgumentMemoryQualifier.readonly &&
5309 !functionParameterMemoryQualifier.readonly)
5310 {
5311 error(functionCall->getLine(),
5312 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005313 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005314 }
5315
5316 if (functionArgumentMemoryQualifier.writeonly &&
5317 !functionParameterMemoryQualifier.writeonly)
5318 {
5319 error(functionCall->getLine(),
5320 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005321 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005322 }
Martin Radev049edfa2016-11-11 14:35:37 +02005323
5324 if (functionArgumentMemoryQualifier.coherent &&
5325 !functionParameterMemoryQualifier.coherent)
5326 {
5327 error(functionCall->getLine(),
5328 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005329 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005330 }
5331
5332 if (functionArgumentMemoryQualifier.volatileQualifier &&
5333 !functionParameterMemoryQualifier.volatileQualifier)
5334 {
5335 error(functionCall->getLine(),
5336 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005337 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005338 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005339 }
5340 }
5341}
5342
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005343TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005344{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005345 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00005346}
5347
5348TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005349 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00005350 TIntermNode *thisNode,
5351 const TSourceLoc &loc)
5352{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005353 if (thisNode != nullptr)
5354 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005355 return addMethod(fnCall, arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005356 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005357
5358 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005359 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005360 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005361 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005362 }
5363 else
5364 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005365 ASSERT(op == EOpNull);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005366 return addNonConstructorFunctionCall(fnCall, arguments, loc);
5367 }
5368}
5369
5370TIntermTyped *TParseContext::addMethod(TFunction *fnCall,
5371 TIntermSequence *arguments,
5372 TIntermNode *thisNode,
5373 const TSourceLoc &loc)
5374{
5375 TConstantUnion *unionArray = new TConstantUnion[1];
5376 int arraySize = 0;
5377 TIntermTyped *typedThis = thisNode->getAsTyped();
5378 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5379 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5380 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
5381 // So accessing fnCall->getName() below is safe.
5382 if (fnCall->getName() != "length")
5383 {
5384 error(loc, "invalid method", fnCall->getName().c_str());
5385 }
5386 else if (!arguments->empty())
5387 {
5388 error(loc, "method takes no parameters", "length");
5389 }
5390 else if (typedThis == nullptr || !typedThis->isArray())
5391 {
5392 error(loc, "length can only be called on arrays", "length");
5393 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08005394 else if (typedThis->getQualifier() == EvqPerVertexIn &&
5395 mGeometryShaderInputPrimitiveType == EptUndefined)
5396 {
5397 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5398 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005399 else
5400 {
5401 arraySize = typedThis->getArraySize();
5402 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuaho72d10202017-01-19 15:58:30 +00005403 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005404 // This code path can be hit with expressions like these:
5405 // (a = b).length()
5406 // (func()).length()
5407 // (int[3](0, 1, 2)).length()
5408 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
5409 // expression.
5410 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
5411 // spec section 5.9 which allows "An array, vector or matrix expression with the
5412 // length method applied".
5413 error(loc, "length can only be called on array names, not on array expressions",
5414 "length");
Olli Etuaho72d10202017-01-19 15:58:30 +00005415 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005416 }
5417 unionArray->setIConst(arraySize);
Olli Etuaho56229f12017-07-10 14:16:33 +03005418 TIntermConstantUnion *node =
5419 new TIntermConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst));
5420 node->setLine(loc);
5421 return node;
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005422}
5423
5424TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunction *fnCall,
5425 TIntermSequence *arguments,
5426 const TSourceLoc &loc)
5427{
5428 // First find by unmangled name to check whether the function name has been
5429 // hidden by a variable name or struct typename.
5430 // If a function is found, check for one with a matching argument list.
5431 bool builtIn;
5432 const TSymbol *symbol = symbolTable.find(fnCall->getName(), mShaderVersion, &builtIn);
5433 if (symbol != nullptr && !symbol->isFunction())
5434 {
5435 error(loc, "function name expected", fnCall->getName().c_str());
5436 }
5437 else
5438 {
5439 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(fnCall->getName(), *arguments),
5440 mShaderVersion, &builtIn);
5441 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005442 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005443 error(loc, "no matching overloaded function found", fnCall->getName().c_str());
5444 }
5445 else
5446 {
5447 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005448 //
5449 // A declared function.
5450 //
Olli Etuaho383b7912016-08-05 11:22:59 +03005451 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005452 {
Olli Etuaho856c4972016-08-08 11:38:39 +03005453 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005454 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005455 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005456 if (builtIn && op != EOpNull)
5457 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005458 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005459 if (fnCandidate->getParamCount() == 1)
5460 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005461 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005462 TIntermNode *unaryParamNode = arguments->front();
5463 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005464 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005465 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005466 }
5467 else
5468 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005469 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00005470 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005471 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005472
5473 // Some built-in functions have out parameters too.
Jiajia Qinbc585152017-06-23 15:42:17 +08005474 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05305475
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005476 if (TIntermAggregate::CanFoldAggregateBuiltInOp(callNode->getOp()))
Arun Patole274f0702015-05-05 13:33:30 +05305477 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005478 // See if we can constant fold a built-in. Note that this may be possible
5479 // even if it is not const-qualified.
5480 return callNode->fold(mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05305481 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005482 else
5483 {
5484 return callNode;
5485 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005486 }
5487 }
5488 else
5489 {
5490 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005491 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005492
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005493 // If builtIn == false, the function is user defined - could be an overloaded
5494 // built-in as well.
5495 // if builtIn == true, it's a builtIn function with no op associated with it.
5496 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005497 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005498 {
Olli Etuahofe486322017-03-21 09:30:54 +00005499 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005500 checkTextureOffsetConst(callNode);
5501 checkImageMemoryAccessForBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03005502 }
5503 else
5504 {
Olli Etuahofe486322017-03-21 09:30:54 +00005505 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005506 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005507 }
5508
Jiajia Qinbc585152017-06-23 15:42:17 +08005509 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005510
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005511 callNode->setLine(loc);
5512
5513 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005514 }
5515 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005516 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005517
5518 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005519 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005520}
5521
Jamie Madillb98c3a82015-07-23 14:26:04 -04005522TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005523 TIntermTyped *trueExpression,
5524 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005525 const TSourceLoc &loc)
5526{
Olli Etuaho56229f12017-07-10 14:16:33 +03005527 if (!checkIsScalarBool(loc, cond))
5528 {
5529 return falseExpression;
5530 }
Olli Etuaho52901742015-04-15 13:42:45 +03005531
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005532 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005533 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005534 std::stringstream reasonStream;
5535 reasonStream << "mismatching ternary operator operand types '"
5536 << trueExpression->getCompleteString() << " and '"
5537 << falseExpression->getCompleteString() << "'";
5538 std::string reason = reasonStream.str();
5539 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005540 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005541 }
Olli Etuahode318b22016-10-25 16:18:25 +01005542 if (IsOpaqueType(trueExpression->getBasicType()))
5543 {
5544 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005545 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005546 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5547 // Note that structs containing opaque types don't need to be checked as structs are
5548 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005549 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005550 return falseExpression;
5551 }
5552
Jiajia Qinbc585152017-06-23 15:42:17 +08005553 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5554 falseExpression->getMemoryQualifier().writeonly)
5555 {
5556 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5557 return falseExpression;
5558 }
5559
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005560 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005561 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005562 // ESSL 3.00.6 section 5.7:
5563 // Ternary operator support is optional for arrays. No certainty that it works across all
5564 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5565 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005566 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005567 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005568 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005569 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005570 }
Olli Etuaho94050052017-05-08 14:17:44 +03005571 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5572 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005573 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005574 return falseExpression;
5575 }
5576
Corentin Wallez0d959252016-07-12 17:26:32 -04005577 // WebGL2 section 5.26, the following results in an error:
5578 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005579 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005580 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005581 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005582 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005583 }
5584
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005585 // Note that the node resulting from here can be a constant union without being qualified as
5586 // constant.
5587 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5588 node->setLine(loc);
5589
5590 return node->fold();
Olli Etuaho52901742015-04-15 13:42:45 +03005591}
Olli Etuaho49300862015-02-20 14:54:49 +02005592
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005593//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005594// Parse an array of strings using yyparse.
5595//
5596// Returns 0 for success.
5597//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005598int PaParseStrings(size_t count,
5599 const char *const string[],
5600 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305601 TParseContext *context)
5602{
Yunchao He4f285442017-04-21 12:15:49 +08005603 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005604 return 1;
5605
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005606 if (glslang_initialize(context))
5607 return 1;
5608
alokp@chromium.org408c45e2012-04-05 15:54:43 +00005609 int error = glslang_scan(count, string, length, context);
5610 if (!error)
5611 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005612
alokp@chromium.org73bc2982012-06-19 18:48:05 +00005613 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00005614
alokp@chromium.org6b495712012-06-29 00:06:58 +00005615 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005616}
Jamie Madill45bcc782016-11-07 13:58:48 -05005617
5618} // namespace sh