blob: a1c12f33da01d34733aab7ff730f046b0e46b86c [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());
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300683 if (static_cast<size_t>(type.getOutermostArraySize()) != arguments->size())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300684 {
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 Etuaho96f6adf2017-08-16 11:18:54 +0300698 if (!argType.isElementTypeOf(type))
Olli Etuaho856c4972016-08-08 11:38:39 +0300699 {
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.
Olli Etuahoe0803872017-08-23 15:30:23 +0300971bool TParseContext::checkArrayElementIsNotArray(const TSourceLoc &line,
972 const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000973{
Olli Etuaho8a176262016-08-16 14:23:01 +0300974 if (elementType.array)
Jamie Madill06145232015-05-13 13:10:01 -0400975 {
Olli Etuaho8a176262016-08-16 14:23:01 +0300976 error(line, "cannot declare arrays of arrays",
977 TType(elementType).getCompleteString().c_str());
978 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000979 }
Olli Etuahoe0803872017-08-23 15:30:23 +0300980 return true;
981}
982
983// Check if this qualified element type can be formed into an array. This is only called when array
984// brackets are associated with an identifier in a declaration, like this:
985// float a[2];
986// Similar checks are done in addFullySpecifiedType for array declarations where the array brackets
987// are associated with the type, like this:
988// float[2] a;
989bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
990 const TPublicType &elementType)
991{
992 if (!checkArrayElementIsNotArray(indexLocation, elementType))
993 {
994 return false;
995 }
Olli Etuahocc36b982015-07-10 14:14:18 +0300996 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
997 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
998 // 4.3.4).
Martin Radev4a9cd802016-09-01 16:51:51 +0300999 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Olli Etuaho8a176262016-08-16 14:23:01 +03001000 sh::IsVarying(elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +03001001 {
Olli Etuahoe0803872017-08-23 15:30:23 +03001002 error(indexLocation, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001003 TType(elementType).getCompleteString().c_str());
1004 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +03001005 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001006 return checkIsValidQualifierForArray(indexLocation, elementType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001007}
1008
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001009// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +03001010void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
1011 const TString &identifier,
1012 TPublicType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001013{
Olli Etuaho3739d232015-04-08 12:23:44 +03001014 ASSERT(type != nullptr);
1015 if (type->qualifier == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001016 {
1017 // Make the qualifier make sense.
Olli Etuaho3739d232015-04-08 12:23:44 +03001018 type->qualifier = EvqTemporary;
1019
1020 // Generate informative error messages for ESSL1.
1021 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001022 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001023 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301024 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001025 "structures containing arrays may not be declared constant since they cannot be "
1026 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +05301027 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001028 }
1029 else
1030 {
1031 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
1032 }
Olli Etuaho383b7912016-08-05 11:22:59 +03001033 return;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001034 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001035 if (type->isUnsizedArray())
1036 {
1037 error(line, "implicitly sized arrays need to be initialized", identifier.c_str());
Olli Etuaho376f1b52015-04-13 13:23:41 +03001038 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001039}
1040
Olli Etuaho2935c582015-04-08 14:32:06 +03001041// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001042// and update the symbol table.
1043//
Olli Etuaho2935c582015-04-08 14:32:06 +03001044// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001045//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001046bool TParseContext::declareVariable(const TSourceLoc &line,
1047 const TString &identifier,
1048 const TType &type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001049 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001050{
Olli Etuaho2935c582015-04-08 14:32:06 +03001051 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001052
Olli Etuaho43364892017-02-13 16:00:12 +00001053 checkBindingIsValid(line, type);
1054
Olli Etuaho856c4972016-08-08 11:38:39 +03001055 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001056
Olli Etuaho2935c582015-04-08 14:32:06 +03001057 // gl_LastFragData may be redeclared with a new precision qualifier
1058 if (type.isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
1059 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001060 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1061 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001062 if (type.isArrayOfArrays())
1063 {
1064 error(line, "redeclaration of gl_LastFragData as an array of arrays",
1065 identifier.c_str());
1066 return false;
1067 }
1068 else if (static_cast<int>(type.getOutermostArraySize()) ==
1069 maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001070 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001071 if (TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001072 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001073 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->getExtension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001074 }
1075 }
1076 else
1077 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001078 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1079 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001080 return false;
1081 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001082 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001083
Olli Etuaho8a176262016-08-16 14:23:01 +03001084 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001085 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001086
Olli Etuaho0f684632017-07-13 12:42:15 +03001087 (*variable) = symbolTable.declareVariable(&identifier, type);
1088 if (!(*variable))
Olli Etuaho2935c582015-04-08 14:32:06 +03001089 {
1090 error(line, "redefinition", identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001091 return false;
1092 }
1093
Olli Etuaho8a176262016-08-16 14:23:01 +03001094 if (!checkIsNonVoid(line, identifier, type.getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001095 return false;
1096
1097 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001098}
1099
Martin Radev70866b82016-07-22 15:27:42 +03001100void TParseContext::checkIsParameterQualifierValid(
1101 const TSourceLoc &line,
1102 const TTypeQualifierBuilder &typeQualifierBuilder,
1103 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301104{
Olli Etuahocce89652017-06-19 16:04:09 +03001105 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001106 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001107
1108 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301109 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001110 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1111 }
1112
1113 if (!IsImage(type->getBasicType()))
1114 {
Olli Etuaho43364892017-02-13 16:00:12 +00001115 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001116 }
1117 else
1118 {
1119 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001120 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001121
Martin Radev70866b82016-07-22 15:27:42 +03001122 type->setQualifier(typeQualifier.qualifier);
1123
1124 if (typeQualifier.precision != EbpUndefined)
1125 {
1126 type->setPrecision(typeQualifier.precision);
1127 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001128}
1129
Olli Etuaho856c4972016-08-08 11:38:39 +03001130bool TParseContext::checkCanUseExtension(const TSourceLoc &line, const TString &extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001131{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001132 const TExtensionBehavior &extBehavior = extensionBehavior();
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001133 TExtensionBehavior::const_iterator iter = extBehavior.find(extension.c_str());
Arun Patole7e7e68d2015-05-22 12:02:25 +05301134 if (iter == extBehavior.end())
1135 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001136 error(line, "extension is not supported", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001137 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001138 }
zmo@google.comf5450912011-09-09 01:37:19 +00001139 // In GLSL ES, an extension's default behavior is "disable".
Arun Patole7e7e68d2015-05-22 12:02:25 +05301140 if (iter->second == EBhDisable || iter->second == EBhUndefined)
1141 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00001142 // TODO(oetuaho@nvidia.com): This is slightly hacky. Might be better if symbols could be
1143 // associated with more than one extension.
1144 if (extension == "GL_OVR_multiview")
1145 {
1146 return checkCanUseExtension(line, "GL_OVR_multiview2");
1147 }
Olli Etuaho4de340a2016-12-16 09:32:03 +00001148 error(line, "extension is disabled", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001149 return false;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001150 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301151 if (iter->second == EBhWarn)
1152 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001153 warning(line, "extension is being used", extension.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03001154 return true;
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001155 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001156
Olli Etuaho8a176262016-08-16 14:23:01 +03001157 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001158}
1159
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001160// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1161// compile-time or link-time errors are the same whether or not the declaration is empty".
1162// This function implements all the checks that are done on qualifiers regardless of if the
1163// declaration is empty.
1164void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1165 const sh::TLayoutQualifier &layoutQualifier,
1166 const TSourceLoc &location)
1167{
1168 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1169 {
1170 error(location, "Shared memory declarations cannot have layout specified", "layout");
1171 }
1172
1173 if (layoutQualifier.matrixPacking != EmpUnspecified)
1174 {
1175 error(location, "layout qualifier only valid for interface blocks",
1176 getMatrixPackingString(layoutQualifier.matrixPacking));
1177 return;
1178 }
1179
1180 if (layoutQualifier.blockStorage != EbsUnspecified)
1181 {
1182 error(location, "layout qualifier only valid for interface blocks",
1183 getBlockStorageString(layoutQualifier.blockStorage));
1184 return;
1185 }
1186
1187 if (qualifier == EvqFragmentOut)
1188 {
1189 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1190 {
1191 error(location, "invalid layout qualifier combination", "yuv");
1192 return;
1193 }
1194 }
1195 else
1196 {
1197 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1198 }
1199
Olli Etuaho95468d12017-05-04 11:14:34 +03001200 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1201 // parsing steps. So it needs to be checked here.
1202 if (isMultiviewExtensionEnabled() && mShaderVersion < 300 && qualifier == EvqVertexIn)
1203 {
1204 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1205 }
1206
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001207 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
1208 if (mShaderVersion >= 310 && qualifier == EvqUniform)
1209 {
1210 canHaveLocation = true;
1211 // We're not checking whether the uniform location is in range here since that depends on
1212 // the type of the variable.
1213 // The type can only be fully determined for non-empty declarations.
1214 }
1215 if (!canHaveLocation)
1216 {
1217 checkLocationIsNotSpecified(location, layoutQualifier);
1218 }
1219}
1220
jchen104cdac9e2017-05-08 11:01:20 +08001221void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1222 const TSourceLoc &location)
1223{
1224 if (publicType.precision != EbpHigh)
1225 {
1226 error(location, "Can only be highp", "atomic counter");
1227 }
1228 // dEQP enforces compile error if location is specified. See uniform_location.test.
1229 if (publicType.layoutQualifier.location != -1)
1230 {
1231 error(location, "location must not be set for atomic_uint", "layout");
1232 }
1233 if (publicType.layoutQualifier.binding == -1)
1234 {
1235 error(location, "no binding specified", "atomic counter");
1236 }
1237}
1238
Martin Radevb8b01222016-11-20 23:25:53 +02001239void TParseContext::emptyDeclarationErrorCheck(const TPublicType &publicType,
1240 const TSourceLoc &location)
1241{
1242 if (publicType.isUnsizedArray())
1243 {
1244 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1245 // error. It is assumed that this applies to empty declarations as well.
1246 error(location, "empty array declaration needs to specify a size", "");
1247 }
Martin Radevb8b01222016-11-20 23:25:53 +02001248}
1249
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001250// These checks are done for all declarations that are non-empty. They're done for non-empty
1251// declarations starting a declarator list, and declarators that follow an empty declaration.
1252void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1253 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001254{
Olli Etuahofa33d582015-04-09 14:33:12 +03001255 switch (publicType.qualifier)
1256 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001257 case EvqVaryingIn:
1258 case EvqVaryingOut:
1259 case EvqAttribute:
1260 case EvqVertexIn:
1261 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001262 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001263 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001264 {
1265 error(identifierLocation, "cannot be used with a structure",
1266 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001267 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001268 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001269 break;
1270 case EvqBuffer:
1271 if (publicType.getBasicType() != EbtInterfaceBlock)
1272 {
1273 error(identifierLocation,
1274 "cannot declare buffer variables at global scope(outside a block)",
1275 getQualifierString(publicType.qualifier));
1276 return;
1277 }
1278 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001279 default:
1280 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001281 }
jchen10cc2a10e2017-05-03 14:05:12 +08001282 std::string reason(getBasicString(publicType.getBasicType()));
1283 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001284 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001285 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001286 {
1287 return;
1288 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001289
Andrei Volykhina5527072017-03-22 16:46:30 +03001290 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1291 publicType.qualifier != EvqConst) &&
1292 publicType.getBasicType() == EbtYuvCscStandardEXT)
1293 {
1294 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1295 getQualifierString(publicType.qualifier));
1296 return;
1297 }
1298
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001299 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1300 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001301 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1302 // But invalid shaders may still reach here with an unsized array declaration.
1303 if (!publicType.isUnsizedArray())
1304 {
1305 TType type(publicType);
1306 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1307 publicType.layoutQualifier);
1308 }
1309 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001310
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001311 // check for layout qualifier issues
1312 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001313
Martin Radev2cc85b32016-08-05 16:22:53 +03001314 if (IsImage(publicType.getBasicType()))
1315 {
1316
1317 switch (layoutQualifier.imageInternalFormat)
1318 {
1319 case EiifRGBA32F:
1320 case EiifRGBA16F:
1321 case EiifR32F:
1322 case EiifRGBA8:
1323 case EiifRGBA8_SNORM:
1324 if (!IsFloatImage(publicType.getBasicType()))
1325 {
1326 error(identifierLocation,
1327 "internal image format requires a floating image type",
1328 getBasicString(publicType.getBasicType()));
1329 return;
1330 }
1331 break;
1332 case EiifRGBA32I:
1333 case EiifRGBA16I:
1334 case EiifRGBA8I:
1335 case EiifR32I:
1336 if (!IsIntegerImage(publicType.getBasicType()))
1337 {
1338 error(identifierLocation,
1339 "internal image format requires an integer image type",
1340 getBasicString(publicType.getBasicType()));
1341 return;
1342 }
1343 break;
1344 case EiifRGBA32UI:
1345 case EiifRGBA16UI:
1346 case EiifRGBA8UI:
1347 case EiifR32UI:
1348 if (!IsUnsignedImage(publicType.getBasicType()))
1349 {
1350 error(identifierLocation,
1351 "internal image format requires an unsigned image type",
1352 getBasicString(publicType.getBasicType()));
1353 return;
1354 }
1355 break;
1356 case EiifUnspecified:
1357 error(identifierLocation, "layout qualifier", "No image internal format specified");
1358 return;
1359 default:
1360 error(identifierLocation, "layout qualifier", "unrecognized token");
1361 return;
1362 }
1363
1364 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1365 switch (layoutQualifier.imageInternalFormat)
1366 {
1367 case EiifR32F:
1368 case EiifR32I:
1369 case EiifR32UI:
1370 break;
1371 default:
1372 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1373 {
1374 error(identifierLocation, "layout qualifier",
1375 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1376 "image variables must be qualified readonly and/or writeonly");
1377 return;
1378 }
1379 break;
1380 }
1381 }
1382 else
1383 {
Olli Etuaho43364892017-02-13 16:00:12 +00001384 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001385 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1386 }
jchen104cdac9e2017-05-08 11:01:20 +08001387
1388 if (IsAtomicCounter(publicType.getBasicType()))
1389 {
1390 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1391 }
1392 else
1393 {
1394 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1395 }
Olli Etuaho43364892017-02-13 16:00:12 +00001396}
Martin Radev2cc85b32016-08-05 16:22:53 +03001397
Olli Etuaho43364892017-02-13 16:00:12 +00001398void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1399{
1400 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001401 // Note that the ESSL 3.10 section 4.4.5 is not particularly clear on how the binding qualifier
1402 // on arrays of arrays should be handled. We interpret the spec so that the binding value is
1403 // incremented for each element of the innermost nested arrays. This is in line with how arrays
1404 // of arrays of blocks are specified to behave in GLSL 4.50 and a conservative interpretation
1405 // when it comes to which shaders are accepted by the compiler.
1406 int arrayTotalElementCount = type.getArraySizeProduct();
Olli Etuaho43364892017-02-13 16:00:12 +00001407 if (IsImage(type.getBasicType()))
1408 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001409 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding,
1410 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001411 }
1412 else if (IsSampler(type.getBasicType()))
1413 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001414 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding,
1415 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001416 }
jchen104cdac9e2017-05-08 11:01:20 +08001417 else if (IsAtomicCounter(type.getBasicType()))
1418 {
1419 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1420 }
Olli Etuaho43364892017-02-13 16:00:12 +00001421 else
1422 {
1423 ASSERT(!IsOpaqueType(type.getBasicType()));
1424 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001425 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001426}
1427
Olli Etuaho856c4972016-08-08 11:38:39 +03001428void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1429 const TString &layoutQualifierName,
1430 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001431{
1432
1433 if (mShaderVersion < versionRequired)
1434 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001435 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001436 }
1437}
1438
Olli Etuaho856c4972016-08-08 11:38:39 +03001439bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1440 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001441{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001442 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001443 for (size_t i = 0u; i < localSize.size(); ++i)
1444 {
1445 if (localSize[i] != -1)
1446 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001447 error(location,
1448 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1449 "global layout declaration",
1450 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001451 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001452 }
1453 }
1454
Olli Etuaho8a176262016-08-16 14:23:01 +03001455 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001456}
1457
Olli Etuaho43364892017-02-13 16:00:12 +00001458void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001459 TLayoutImageInternalFormat internalFormat)
1460{
1461 if (internalFormat != EiifUnspecified)
1462 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001463 error(location, "invalid layout qualifier: only valid when used with images",
1464 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001465 }
Olli Etuaho43364892017-02-13 16:00:12 +00001466}
1467
1468void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1469{
1470 if (binding != -1)
1471 {
1472 error(location,
1473 "invalid layout qualifier: only valid when used with opaque types or blocks",
1474 "binding");
1475 }
1476}
1477
jchen104cdac9e2017-05-08 11:01:20 +08001478void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1479{
1480 if (offset != -1)
1481 {
1482 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1483 "offset");
1484 }
1485}
1486
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001487void TParseContext::checkImageBindingIsValid(const TSourceLoc &location,
1488 int binding,
1489 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001490{
1491 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001492 if (binding >= 0 && binding + arrayTotalElementCount > mMaxImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001493 {
1494 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1495 }
1496}
1497
1498void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1499 int binding,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001500 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001501{
1502 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001503 if (binding >= 0 && binding + arrayTotalElementCount > mMaxCombinedTextureImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001504 {
1505 error(location, "sampler binding greater than maximum texture units", "binding");
1506 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001507}
1508
Jiajia Qinbc585152017-06-23 15:42:17 +08001509void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location,
1510 const TQualifier &qualifier,
1511 int binding,
1512 int arraySize)
jchen10af713a22017-04-19 09:10:56 +08001513{
1514 int size = (arraySize == 0 ? 1 : arraySize);
Jiajia Qinbc585152017-06-23 15:42:17 +08001515 if (qualifier == EvqUniform)
jchen10af713a22017-04-19 09:10:56 +08001516 {
Jiajia Qinbc585152017-06-23 15:42:17 +08001517 if (binding + size > mMaxUniformBufferBindings)
1518 {
1519 error(location, "uniform block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1520 "binding");
1521 }
1522 }
1523 else if (qualifier == EvqBuffer)
1524 {
1525 if (binding + size > mMaxShaderStorageBufferBindings)
1526 {
1527 error(location,
1528 "shader storage block binding greater than MAX_SHADER_STORAGE_BUFFER_BINDINGS",
1529 "binding");
1530 }
jchen10af713a22017-04-19 09:10:56 +08001531 }
1532}
jchen104cdac9e2017-05-08 11:01:20 +08001533void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1534{
1535 if (binding >= mMaxAtomicCounterBindings)
1536 {
1537 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1538 "binding");
1539 }
1540}
jchen10af713a22017-04-19 09:10:56 +08001541
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001542void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1543 int objectLocationCount,
1544 const TLayoutQualifier &layoutQualifier)
1545{
1546 int loc = layoutQualifier.location;
1547 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1548 {
1549 error(location, "Uniform location out of range", "location");
1550 }
1551}
1552
Andrei Volykhina5527072017-03-22 16:46:30 +03001553void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1554{
1555 if (yuv != false)
1556 {
1557 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1558 }
1559}
1560
Jiajia Qinbc585152017-06-23 15:42:17 +08001561void TParseContext::functionCallRValueLValueErrorCheck(const TFunction *fnCandidate,
1562 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001563{
1564 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1565 {
1566 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
Jiajia Qinbc585152017-06-23 15:42:17 +08001567 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
1568 if (!IsImage(argument->getBasicType()) && (IsQualifierUnspecified(qual) || qual == EvqIn ||
1569 qual == EvqInOut || qual == EvqConstReadOnly))
1570 {
1571 if (argument->getMemoryQualifier().writeonly)
1572 {
1573 error(argument->getLine(),
1574 "Writeonly value cannot be passed for 'in' or 'inout' parameters.",
1575 fnCall->getFunctionSymbolInfo()->getName().c_str());
1576 return;
1577 }
1578 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001579 if (qual == EvqOut || qual == EvqInOut)
1580 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001581 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001582 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001583 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001584 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuahoec9232b2017-03-27 17:01:37 +03001585 fnCall->getFunctionSymbolInfo()->getName().c_str());
Olli Etuaho383b7912016-08-05 11:22:59 +03001586 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001587 }
1588 }
1589 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001590}
1591
Martin Radev70866b82016-07-22 15:27:42 +03001592void TParseContext::checkInvariantVariableQualifier(bool invariant,
1593 const TQualifier qualifier,
1594 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001595{
Martin Radev70866b82016-07-22 15:27:42 +03001596 if (!invariant)
1597 return;
1598
1599 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001600 {
Martin Radev70866b82016-07-22 15:27:42 +03001601 // input variables in the fragment shader can be also qualified as invariant
1602 if (!sh::CanBeInvariantESSL1(qualifier))
1603 {
1604 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1605 }
1606 }
1607 else
1608 {
1609 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1610 {
1611 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1612 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001613 }
1614}
1615
Arun Patole7e7e68d2015-05-22 12:02:25 +05301616bool TParseContext::supportsExtension(const char *extension)
zmo@google.com09c323a2011-08-12 18:22:25 +00001617{
Jamie Madillb98c3a82015-07-23 14:26:04 -04001618 const TExtensionBehavior &extbehavior = extensionBehavior();
alokp@chromium.org73bc2982012-06-19 18:48:05 +00001619 TExtensionBehavior::const_iterator iter = extbehavior.find(extension);
1620 return (iter != extbehavior.end());
alokp@chromium.org8b851c62012-06-15 16:25:11 +00001621}
1622
Arun Patole7e7e68d2015-05-22 12:02:25 +05301623bool TParseContext::isExtensionEnabled(const char *extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001624{
Kimmo Kinnunenb18609b2015-07-16 14:13:11 +03001625 return ::IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001626}
1627
Jamie Madillb98c3a82015-07-23 14:26:04 -04001628void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1629 const char *extName,
1630 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001631{
1632 pp::SourceLocation srcLoc;
1633 srcLoc.file = loc.first_file;
1634 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001635 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001636}
1637
Jamie Madillb98c3a82015-07-23 14:26:04 -04001638void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1639 const char *name,
1640 const char *value,
1641 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001642{
1643 pp::SourceLocation srcLoc;
1644 srcLoc.file = loc.first_file;
1645 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001646 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001647}
1648
Martin Radev4c4c8e72016-08-04 12:25:34 +03001649sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001650{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001651 sh::WorkGroupSize result;
Martin Radev802abe02016-08-04 17:48:32 +03001652 for (size_t i = 0u; i < result.size(); ++i)
1653 {
1654 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1655 {
1656 result[i] = 1;
1657 }
1658 else
1659 {
1660 result[i] = mComputeShaderLocalSize[i];
1661 }
1662 }
1663 return result;
1664}
1665
Olli Etuaho56229f12017-07-10 14:16:33 +03001666TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1667 const TSourceLoc &line)
1668{
1669 TIntermConstantUnion *node = new TIntermConstantUnion(
1670 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1671 node->setLine(line);
1672 return node;
1673}
1674
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001675/////////////////////////////////////////////////////////////////////////////////
1676//
1677// Non-Errors.
1678//
1679/////////////////////////////////////////////////////////////////////////////////
1680
Jamie Madill5c097022014-08-20 16:38:32 -04001681const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1682 const TString *name,
1683 const TSymbol *symbol)
1684{
Jamie Madill5c097022014-08-20 16:38:32 -04001685 if (!symbol)
1686 {
1687 error(location, "undeclared identifier", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001688 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001689 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001690
1691 if (!symbol->isVariable())
Jamie Madill5c097022014-08-20 16:38:32 -04001692 {
1693 error(location, "variable expected", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001694 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001695 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001696
1697 const TVariable *variable = static_cast<const TVariable *>(symbol);
1698
1699 if (symbolTable.findBuiltIn(variable->getName(), mShaderVersion) &&
1700 !variable->getExtension().empty())
Jamie Madill5c097022014-08-20 16:38:32 -04001701 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001702 checkCanUseExtension(location, variable->getExtension());
Jamie Madill5c097022014-08-20 16:38:32 -04001703 }
1704
Olli Etuaho0f684632017-07-13 12:42:15 +03001705 // Reject shaders using both gl_FragData and gl_FragColor
1706 TQualifier qualifier = variable->getType().getQualifier();
1707 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill5c097022014-08-20 16:38:32 -04001708 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001709 mUsesFragData = true;
1710 }
1711 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
1712 {
1713 mUsesFragColor = true;
1714 }
1715 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1716 {
1717 mUsesSecondaryOutputs = true;
Jamie Madill5c097022014-08-20 16:38:32 -04001718 }
1719
Olli Etuaho0f684632017-07-13 12:42:15 +03001720 // This validation is not quite correct - it's only an error to write to
1721 // both FragData and FragColor. For simplicity, and because users shouldn't
1722 // be rewarded for reading from undefined varaibles, return an error
1723 // if they are both referenced, rather than assigned.
1724 if (mUsesFragData && mUsesFragColor)
1725 {
1726 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1727 if (mUsesSecondaryOutputs)
1728 {
1729 errorMessage =
1730 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1731 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1732 }
1733 error(location, errorMessage, name->c_str());
1734 }
1735
1736 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1737 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1738 qualifier == EvqWorkGroupSize)
1739 {
1740 error(location,
1741 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1742 "gl_WorkGroupSize");
1743 }
Jamie Madill5c097022014-08-20 16:38:32 -04001744 return variable;
1745}
1746
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001747TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1748 const TString *name,
1749 const TSymbol *symbol)
1750{
1751 const TVariable *variable = getNamedVariable(location, name, symbol);
1752
Olli Etuaho0f684632017-07-13 12:42:15 +03001753 if (!variable)
1754 {
1755 TIntermTyped *node = CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
1756 node->setLine(location);
1757 return node;
1758 }
1759
Olli Etuaho09b04a22016-12-15 13:30:26 +00001760 if (variable->getType().getQualifier() == EvqViewIDOVR && IsWebGLBasedSpec(mShaderSpec) &&
1761 mShaderType == GL_FRAGMENT_SHADER && !isExtensionEnabled("GL_OVR_multiview2"))
1762 {
1763 // WEBGL_multiview spec
1764 error(location, "Need to enable OVR_multiview2 to use gl_ViewID_OVR in fragment shader",
1765 "gl_ViewID_OVR");
1766 }
1767
Olli Etuaho56229f12017-07-10 14:16:33 +03001768 TIntermTyped *node = nullptr;
1769
Olli Etuaho7c3848e2015-11-04 13:19:17 +02001770 if (variable->getConstPointer())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001771 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001772 const TConstantUnion *constArray = variable->getConstPointer();
Olli Etuaho56229f12017-07-10 14:16:33 +03001773 node = new TIntermConstantUnion(constArray, variable->getType());
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001774 }
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001775 else if (variable->getType().getQualifier() == EvqWorkGroupSize &&
1776 mComputeShaderLocalSizeDeclared)
1777 {
1778 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1779 // needs to be added to the AST as a constant and not as a symbol.
1780 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1781 TConstantUnion *constArray = new TConstantUnion[3];
1782 for (size_t i = 0; i < 3; ++i)
1783 {
1784 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1785 }
1786
1787 ASSERT(variable->getType().getBasicType() == EbtUInt);
1788 ASSERT(variable->getType().getObjectSize() == 3);
1789
1790 TType type(variable->getType());
1791 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001792 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001793 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08001794 // TODO(jiawei.shao@intel.com): set array sizes for user-defined geometry shader inputs.
1795 else if (variable->getType().getQualifier() == EvqPerVertexIn)
1796 {
1797 TType type(variable->getType());
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001798 type.setArraySize(0, mGeometryShaderInputArraySize);
Jiawei Shaod8105a02017-08-08 09:54:36 +08001799 node = new TIntermSymbol(variable->getUniqueId(), variable->getName(), type);
1800 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001801 else
1802 {
Olli Etuaho56229f12017-07-10 14:16:33 +03001803 node = new TIntermSymbol(variable->getUniqueId(), variable->getName(), variable->getType());
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001804 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001805 ASSERT(node != nullptr);
1806 node->setLine(location);
1807 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001808}
1809
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001810// Initializers show up in several places in the grammar. Have one set of
1811// code to handle them here.
1812//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001813// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001814bool TParseContext::executeInitializer(const TSourceLoc &line,
1815 const TString &identifier,
1816 const TPublicType &pType,
1817 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001818 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001819{
Olli Etuaho13389b62016-10-16 11:48:18 +01001820 ASSERT(initNode != nullptr);
1821 ASSERT(*initNode == nullptr);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001822 TType type = TType(pType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001823
Olli Etuaho2935c582015-04-08 14:32:06 +03001824 TVariable *variable = nullptr;
Olli Etuaho376f1b52015-04-13 13:23:41 +03001825 if (type.isUnsizedArray())
1826 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001827 // In case initializer is not an array or type has more dimensions than initializer, this
1828 // will default to setting array sizes to 1. We have not checked yet whether the initializer
1829 // actually is an array or not. Having a non-array initializer for an unsized array will
1830 // result in an error later, so we don't generate an error message here.
1831 type.sizeUnsizedArrays(initializer->getType().getArraySizes());
Olli Etuaho376f1b52015-04-13 13:23:41 +03001832 }
Olli Etuaho2935c582015-04-08 14:32:06 +03001833 if (!declareVariable(line, identifier, type, &variable))
1834 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001835 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001836 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001837
Olli Etuahob0c645e2015-05-12 14:25:36 +03001838 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001839 if (symbolTable.atGlobalLevel() &&
1840 !ValidateGlobalInitializer(initializer, this, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001841 {
1842 // Error message does not completely match behavior with ESSL 1.00, but
1843 // we want to steer developers towards only using constant expressions.
1844 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001845 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001846 }
1847 if (globalInitWarning)
1848 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001849 warning(
1850 line,
1851 "global variable initializers should be constant expressions "
1852 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1853 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001854 }
1855
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001856 //
1857 // identifier must be of type constant, a global, or a temporary
1858 //
1859 TQualifier qualifier = variable->getType().getQualifier();
Arun Patole7e7e68d2015-05-22 12:02:25 +05301860 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1861 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001862 error(line, " cannot initialize this type of qualifier ",
1863 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001864 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001865 }
1866 //
1867 // test for and propagate constant
1868 //
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001869
Arun Patole7e7e68d2015-05-22 12:02:25 +05301870 if (qualifier == EvqConst)
1871 {
1872 if (qualifier != initializer->getType().getQualifier())
1873 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001874 std::stringstream reasonStream;
1875 reasonStream << "assigning non-constant to '" << variable->getType().getCompleteString()
1876 << "'";
1877 std::string reason = reasonStream.str();
1878 error(line, reason.c_str(), "=");
alokp@chromium.org58e54292010-08-24 21:40:03 +00001879 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001880 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001881 }
Arun Patole7e7e68d2015-05-22 12:02:25 +05301882 if (type != initializer->getType())
1883 {
1884 error(line, " non-matching types for const initializer ",
Jamie Madillb98c3a82015-07-23 14:26:04 -04001885 variable->getType().getQualifierString());
alokp@chromium.org58e54292010-08-24 21:40:03 +00001886 variable->getType().setQualifier(EvqTemporary);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001887 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001888 }
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001889
1890 // Save the constant folded value to the variable if possible. For example array
1891 // initializers are not folded, since that way copying the array literal to multiple places
1892 // in the shader is avoided.
1893 // TODO(oetuaho@nvidia.com): Consider constant folding array initialization in cases where
1894 // it would be beneficial.
Arun Patole7e7e68d2015-05-22 12:02:25 +05301895 if (initializer->getAsConstantUnion())
1896 {
Jamie Madill94bf7f22013-07-08 13:31:15 -04001897 variable->shareConstPointer(initializer->getAsConstantUnion()->getUnionArrayPointer());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001898 ASSERT(*initNode == nullptr);
1899 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +05301900 }
1901 else if (initializer->getAsSymbolNode())
1902 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001903 const TSymbol *symbol =
1904 symbolTable.find(initializer->getAsSymbolNode()->getSymbol(), 0);
1905 const TVariable *tVar = static_cast<const TVariable *>(symbol);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001906
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001907 const TConstantUnion *constArray = tVar->getConstPointer();
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001908 if (constArray)
1909 {
1910 variable->shareConstPointer(constArray);
Olli Etuaho914b79a2017-06-19 16:03:19 +03001911 ASSERT(*initNode == nullptr);
1912 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001913 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001914 }
1915 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02001916
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03001917 TIntermSymbol *intermSymbol =
1918 new TIntermSymbol(variable->getUniqueId(), variable->getName(), variable->getType());
1919 intermSymbol->setLine(line);
Olli Etuaho13389b62016-10-16 11:48:18 +01001920 *initNode = createAssign(EOpInitialize, intermSymbol, initializer, line);
1921 if (*initNode == nullptr)
Olli Etuahoe7847b02015-03-16 11:56:12 +02001922 {
Olli Etuahob1edc4f2015-11-02 17:20:03 +02001923 assignError(line, "=", intermSymbol->getCompleteString(), initializer->getCompleteString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001924 return false;
Olli Etuahoe7847b02015-03-16 11:56:12 +02001925 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001926
Olli Etuaho914b79a2017-06-19 16:03:19 +03001927 return true;
1928}
1929
1930TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
1931 const TString &identifier,
1932 TIntermTyped *initializer,
1933 const TSourceLoc &loc)
1934{
1935 checkIsScalarBool(loc, pType);
1936 TIntermBinary *initNode = nullptr;
1937 if (executeInitializer(loc, identifier, pType, initializer, &initNode))
1938 {
1939 // The initializer is valid. The init condition needs to have a node - either the
1940 // initializer node, or a constant node in case the initialized variable is const and won't
1941 // be recorded in the AST.
1942 if (initNode == nullptr)
1943 {
1944 return initializer;
1945 }
1946 else
1947 {
1948 TIntermDeclaration *declaration = new TIntermDeclaration();
1949 declaration->appendDeclarator(initNode);
1950 return declaration;
1951 }
1952 }
1953 return nullptr;
1954}
1955
1956TIntermNode *TParseContext::addLoop(TLoopType type,
1957 TIntermNode *init,
1958 TIntermNode *cond,
1959 TIntermTyped *expr,
1960 TIntermNode *body,
1961 const TSourceLoc &line)
1962{
1963 TIntermNode *node = nullptr;
1964 TIntermTyped *typedCond = nullptr;
1965 if (cond)
1966 {
1967 typedCond = cond->getAsTyped();
1968 }
1969 if (cond == nullptr || typedCond)
1970 {
Olli Etuahocce89652017-06-19 16:04:09 +03001971 if (type == ELoopDoWhile)
1972 {
1973 checkIsScalarBool(line, typedCond);
1974 }
1975 // In the case of other loops, it was checked before that the condition is a scalar boolean.
1976 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
1977 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
1978 !typedCond->isVector()));
1979
Olli Etuaho3ec75682017-07-05 17:02:55 +03001980 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03001981 node->setLine(line);
1982 return node;
1983 }
1984
Olli Etuahocce89652017-06-19 16:04:09 +03001985 ASSERT(type != ELoopDoWhile);
1986
Olli Etuaho914b79a2017-06-19 16:03:19 +03001987 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
1988 ASSERT(declaration);
1989 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
1990 ASSERT(declarator->getLeft()->getAsSymbolNode());
1991
1992 // The condition is a declaration. In the AST representation we don't support declarations as
1993 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
1994 // the loop.
1995 TIntermBlock *block = new TIntermBlock();
1996
1997 TIntermDeclaration *declareCondition = new TIntermDeclaration();
1998 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
1999 block->appendStatement(declareCondition);
2000
2001 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
2002 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03002003 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002004 block->appendStatement(loop);
2005 loop->setLine(line);
2006 block->setLine(line);
2007 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002008}
2009
Olli Etuahocce89652017-06-19 16:04:09 +03002010TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
2011 TIntermNodePair code,
2012 const TSourceLoc &loc)
2013{
Olli Etuaho56229f12017-07-10 14:16:33 +03002014 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuahocce89652017-06-19 16:04:09 +03002015
2016 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03002017 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03002018 {
2019 if (cond->getAsConstantUnion()->getBConst(0) == true)
2020 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002021 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03002022 }
2023 else
2024 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002025 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03002026 }
2027 }
2028
Olli Etuaho3ec75682017-07-05 17:02:55 +03002029 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuahocce89652017-06-19 16:04:09 +03002030 node->setLine(loc);
2031
2032 return node;
2033}
2034
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002035void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
2036{
2037 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
2038 typeSpecifier->getBasicType());
2039
2040 if (mShaderVersion < 300 && typeSpecifier->array)
2041 {
2042 error(typeSpecifier->getLine(), "not supported", "first-class array");
2043 typeSpecifier->clearArrayness();
2044 }
2045}
2046
Martin Radev70866b82016-07-22 15:27:42 +03002047TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302048 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002049{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002050 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002051
Martin Radev70866b82016-07-22 15:27:42 +03002052 TPublicType returnType = typeSpecifier;
2053 returnType.qualifier = typeQualifier.qualifier;
2054 returnType.invariant = typeQualifier.invariant;
2055 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002056 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002057 returnType.precision = typeSpecifier.precision;
2058
2059 if (typeQualifier.precision != EbpUndefined)
2060 {
2061 returnType.precision = typeQualifier.precision;
2062 }
2063
Martin Radev4a9cd802016-09-01 16:51:51 +03002064 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2065 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002066
Martin Radev4a9cd802016-09-01 16:51:51 +03002067 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2068 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002069
Martin Radev4a9cd802016-09-01 16:51:51 +03002070 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002071
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002072 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002073 {
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002074 if (typeSpecifier.array)
2075 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002076 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002077 returnType.clearArrayness();
2078 }
2079
Martin Radev70866b82016-07-22 15:27:42 +03002080 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002081 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002082 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002083 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002084 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002085 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002086
Martin Radev70866b82016-07-22 15:27:42 +03002087 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002088 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002089 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002090 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002091 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002092 }
2093 }
2094 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002095 {
Martin Radev70866b82016-07-22 15:27:42 +03002096 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002097 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002098 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002099 }
Martin Radev70866b82016-07-22 15:27:42 +03002100 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2101 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002102 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002103 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2104 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002105 }
Martin Radev70866b82016-07-22 15:27:42 +03002106 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002107 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002108 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002109 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002110 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002111 }
2112
2113 return returnType;
2114}
2115
Olli Etuaho856c4972016-08-08 11:38:39 +03002116void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2117 const TPublicType &type,
2118 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002119{
2120 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002121 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002122 {
2123 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002124 }
2125
2126 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2127 switch (qualifier)
2128 {
2129 case EvqVertexIn:
2130 // ESSL 3.00 section 4.3.4
2131 if (type.array)
2132 {
2133 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002134 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002135 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002136 return;
2137 case EvqFragmentOut:
2138 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002139 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002140 {
2141 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002142 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002143 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002144 return;
2145 default:
2146 break;
2147 }
2148
2149 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2150 // restrictions.
2151 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002152 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2153 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002154 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2155 {
2156 error(qualifierLocation, "must use 'flat' interpolation here",
2157 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002158 }
2159
Martin Radev4a9cd802016-09-01 16:51:51 +03002160 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002161 {
2162 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2163 // These restrictions are only implied by the ESSL 3.00 spec, but
2164 // the ESSL 3.10 spec lists these restrictions explicitly.
2165 if (type.array)
2166 {
2167 error(qualifierLocation, "cannot be an array of structures",
2168 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002169 }
2170 if (type.isStructureContainingArrays())
2171 {
2172 error(qualifierLocation, "cannot be a structure containing an array",
2173 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002174 }
2175 if (type.isStructureContainingType(EbtStruct))
2176 {
2177 error(qualifierLocation, "cannot be a structure containing a structure",
2178 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002179 }
2180 if (type.isStructureContainingType(EbtBool))
2181 {
2182 error(qualifierLocation, "cannot be a structure containing a bool",
2183 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002184 }
2185 }
2186}
2187
Martin Radev2cc85b32016-08-05 16:22:53 +03002188void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2189{
2190 if (qualifier.getType() == QtStorage)
2191 {
2192 const TStorageQualifierWrapper &storageQualifier =
2193 static_cast<const TStorageQualifierWrapper &>(qualifier);
2194 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2195 !symbolTable.atGlobalLevel())
2196 {
2197 error(storageQualifier.getLine(),
2198 "Local variables can only use the const storage qualifier.",
2199 storageQualifier.getQualifierString().c_str());
2200 }
2201 }
2202}
2203
Olli Etuaho43364892017-02-13 16:00:12 +00002204void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002205 const TSourceLoc &location)
2206{
Jiajia Qinbc585152017-06-23 15:42:17 +08002207 const std::string reason(
2208 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2209 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002210 if (memoryQualifier.readonly)
2211 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002212 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002213 }
2214 if (memoryQualifier.writeonly)
2215 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002216 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002217 }
Martin Radev049edfa2016-11-11 14:35:37 +02002218 if (memoryQualifier.coherent)
2219 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002220 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002221 }
2222 if (memoryQualifier.restrictQualifier)
2223 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002224 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002225 }
2226 if (memoryQualifier.volatileQualifier)
2227 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002228 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002229 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002230}
2231
jchen104cdac9e2017-05-08 11:01:20 +08002232// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2233// intermediate tree.
2234void TParseContext::checkAtomicCounterOffsetIsNotOverlapped(TPublicType &publicType,
2235 size_t size,
2236 bool forceAppend,
2237 const TSourceLoc &loc,
2238 TType &type)
2239{
2240 auto &bindingState = mAtomicCounterBindingStates[publicType.layoutQualifier.binding];
2241 int offset;
2242 if (publicType.layoutQualifier.offset == -1 || forceAppend)
2243 {
2244 offset = bindingState.appendSpan(size);
2245 }
2246 else
2247 {
2248 offset = bindingState.insertSpan(publicType.layoutQualifier.offset, size);
2249 }
2250 if (offset == -1)
2251 {
2252 error(loc, "Offset overlapping", "atomic counter");
2253 return;
2254 }
2255 TLayoutQualifier qualifier = type.getLayoutQualifier();
2256 qualifier.offset = offset;
2257 type.setLayoutQualifier(qualifier);
2258}
2259
Olli Etuaho13389b62016-10-16 11:48:18 +01002260TIntermDeclaration *TParseContext::parseSingleDeclaration(
2261 TPublicType &publicType,
2262 const TSourceLoc &identifierOrTypeLocation,
2263 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002264{
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002265 TType type(publicType);
2266 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2267 mDirectiveHandler.pragma().stdgl.invariantAll)
2268 {
2269 TQualifier qualifier = type.getQualifier();
2270
2271 // The directive handler has already taken care of rejecting invalid uses of this pragma
2272 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2273 // affected variable declarations:
2274 //
2275 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2276 // elsewhere, in TranslatorGLSL.)
2277 //
2278 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2279 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2280 // the way this is currently implemented we have to enable this compiler option before
2281 // parsing the shader and determining the shading language version it uses. If this were
2282 // implemented as a post-pass, the workaround could be more targeted.
2283 //
2284 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2285 // the specification, but there are desktop OpenGL drivers that expect that this is the
2286 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2287 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2288 {
2289 type.setInvariant(true);
2290 }
2291 }
2292
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002293 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2294 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002295
Olli Etuahobab4c082015-04-24 16:38:49 +03002296 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002297 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002298
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002299 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002300 if (emptyDeclaration)
2301 {
Martin Radevb8b01222016-11-20 23:25:53 +02002302 emptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002303 // In most cases we don't need to create a symbol node for an empty declaration.
2304 // But if the empty declaration is declaring a struct type, the symbol node will store that.
2305 if (type.getBasicType() == EbtStruct)
2306 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002307 symbol = new TIntermSymbol(0, "", type);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002308 }
jchen104cdac9e2017-05-08 11:01:20 +08002309 else if (IsAtomicCounter(publicType.getBasicType()))
2310 {
2311 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2312 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002313 }
2314 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002315 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002316 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002317
Olli Etuaho856c4972016-08-08 11:38:39 +03002318 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002319
jchen104cdac9e2017-05-08 11:01:20 +08002320 if (IsAtomicCounter(publicType.getBasicType()))
2321 {
2322
2323 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterSize, false,
2324 identifierOrTypeLocation, type);
2325 }
2326
Olli Etuaho2935c582015-04-08 14:32:06 +03002327 TVariable *variable = nullptr;
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002328 declareVariable(identifierOrTypeLocation, identifier, type, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002329
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002330 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002331 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002332 symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
Olli Etuaho13389b62016-10-16 11:48:18 +01002333 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002334 }
2335
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002336 TIntermDeclaration *declaration = new TIntermDeclaration();
2337 declaration->setLine(identifierOrTypeLocation);
2338 if (symbol)
2339 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002340 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002341 declaration->appendDeclarator(symbol);
2342 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002343 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002344}
2345
Olli Etuaho13389b62016-10-16 11:48:18 +01002346TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(TPublicType &publicType,
2347 const TSourceLoc &identifierLocation,
2348 const TString &identifier,
2349 const TSourceLoc &indexLocation,
2350 TIntermTyped *indexExpression)
Jamie Madill60ed9812013-06-06 11:56:46 -04002351{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002352 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002353
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002354 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2355 identifierLocation);
2356
2357 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002358
Olli Etuaho856c4972016-08-08 11:38:39 +03002359 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002360
Olli Etuaho8a176262016-08-16 14:23:01 +03002361 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002362
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002363 TType arrayType(publicType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002364
Olli Etuaho856c4972016-08-08 11:38:39 +03002365 unsigned int size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002366 // Make the type an array even if size check failed.
2367 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002368 arrayType.makeArray(size);
Jamie Madill60ed9812013-06-06 11:56:46 -04002369
jchen104cdac9e2017-05-08 11:01:20 +08002370 if (IsAtomicCounter(publicType.getBasicType()))
2371 {
2372 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterArrayStride * size, false,
2373 identifierLocation, arrayType);
2374 }
2375
Olli Etuaho2935c582015-04-08 14:32:06 +03002376 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002377 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill60ed9812013-06-06 11:56:46 -04002378
Olli Etuaho13389b62016-10-16 11:48:18 +01002379 TIntermDeclaration *declaration = new TIntermDeclaration();
2380 declaration->setLine(identifierLocation);
2381
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002382 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002383 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002384 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2385 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002386 declaration->appendDeclarator(symbol);
2387 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002388
Olli Etuaho13389b62016-10-16 11:48:18 +01002389 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002390}
2391
Olli Etuaho13389b62016-10-16 11:48:18 +01002392TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2393 const TSourceLoc &identifierLocation,
2394 const TString &identifier,
2395 const TSourceLoc &initLocation,
2396 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002397{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002398 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002399
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002400 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2401 identifierLocation);
2402
2403 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002404
Olli Etuaho13389b62016-10-16 11:48:18 +01002405 TIntermDeclaration *declaration = new TIntermDeclaration();
2406 declaration->setLine(identifierLocation);
2407
2408 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002409 if (executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002410 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002411 if (initNode)
2412 {
2413 declaration->appendDeclarator(initNode);
2414 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002415 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002416 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002417}
2418
Olli Etuaho13389b62016-10-16 11:48:18 +01002419TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Jamie Madillb98c3a82015-07-23 14:26:04 -04002420 TPublicType &publicType,
2421 const TSourceLoc &identifierLocation,
2422 const TString &identifier,
2423 const TSourceLoc &indexLocation,
2424 TIntermTyped *indexExpression,
2425 const TSourceLoc &initLocation,
2426 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002427{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002428 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002429
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002430 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2431 identifierLocation);
2432
2433 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002434
Olli Etuaho8a176262016-08-16 14:23:01 +03002435 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002436
2437 TPublicType arrayType(publicType);
2438
Olli Etuaho856c4972016-08-08 11:38:39 +03002439 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002440 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2441 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002442 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002443 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002444 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002445 }
2446 // Make the type an array even if size check failed.
2447 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2448 arrayType.setArraySize(size);
2449
Olli Etuaho13389b62016-10-16 11:48:18 +01002450 TIntermDeclaration *declaration = new TIntermDeclaration();
2451 declaration->setLine(identifierLocation);
2452
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002453 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002454 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002455 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002456 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002457 if (initNode)
2458 {
2459 declaration->appendDeclarator(initNode);
2460 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002461 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002462
2463 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002464}
2465
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002466TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002467 const TTypeQualifierBuilder &typeQualifierBuilder,
2468 const TSourceLoc &identifierLoc,
2469 const TString *identifier,
2470 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002471{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002472 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002473
Martin Radev70866b82016-07-22 15:27:42 +03002474 if (!typeQualifier.invariant)
2475 {
2476 error(identifierLoc, "Expected invariant", identifier->c_str());
2477 return nullptr;
2478 }
2479 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2480 {
2481 return nullptr;
2482 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002483 if (!symbol)
2484 {
2485 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002486 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002487 }
Martin Radev70866b82016-07-22 15:27:42 +03002488 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002489 {
Martin Radev70866b82016-07-22 15:27:42 +03002490 error(identifierLoc, "invariant declaration specifies qualifier",
2491 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002492 }
Martin Radev70866b82016-07-22 15:27:42 +03002493 if (typeQualifier.precision != EbpUndefined)
2494 {
2495 error(identifierLoc, "invariant declaration specifies precision",
2496 getPrecisionString(typeQualifier.precision));
2497 }
2498 if (!typeQualifier.layoutQualifier.isEmpty())
2499 {
2500 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2501 }
2502
2503 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002504 if (!variable)
2505 {
2506 return nullptr;
2507 }
Martin Radev70866b82016-07-22 15:27:42 +03002508 const TType &type = variable->getType();
2509
2510 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2511 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002512 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002513
2514 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2515
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002516 TIntermSymbol *intermSymbol = new TIntermSymbol(variable->getUniqueId(), *identifier, type);
2517 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002518
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002519 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002520}
2521
Olli Etuaho13389b62016-10-16 11:48:18 +01002522void TParseContext::parseDeclarator(TPublicType &publicType,
2523 const TSourceLoc &identifierLocation,
2524 const TString &identifier,
2525 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002526{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002527 // If the declaration starting this declarator list was empty (example: int,), some checks were
2528 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002529 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002530 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002531 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2532 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002533 }
2534
Olli Etuaho856c4972016-08-08 11:38:39 +03002535 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002536
Olli Etuaho856c4972016-08-08 11:38:39 +03002537 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002538
Olli Etuaho2935c582015-04-08 14:32:06 +03002539 TVariable *variable = nullptr;
Olli Etuaho43364892017-02-13 16:00:12 +00002540 TType type(publicType);
jchen104cdac9e2017-05-08 11:01:20 +08002541 if (IsAtomicCounter(publicType.getBasicType()))
2542 {
2543 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterSize, true,
2544 identifierLocation, type);
2545 }
Olli Etuaho43364892017-02-13 16:00:12 +00002546 declareVariable(identifierLocation, identifier, type, &variable);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002547
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002548 if (variable)
Olli Etuaho13389b62016-10-16 11:48:18 +01002549 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002550 TIntermSymbol *symbol = new TIntermSymbol(variable->getUniqueId(), identifier, type);
2551 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002552 declarationOut->appendDeclarator(symbol);
2553 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002554}
2555
Olli Etuaho13389b62016-10-16 11:48:18 +01002556void TParseContext::parseArrayDeclarator(TPublicType &publicType,
2557 const TSourceLoc &identifierLocation,
2558 const TString &identifier,
2559 const TSourceLoc &arrayLocation,
2560 TIntermTyped *indexExpression,
2561 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002562{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002563 // If the declaration starting this declarator list was empty (example: int,), some checks were
2564 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002565 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002566 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002567 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2568 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002569 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002570
Olli Etuaho856c4972016-08-08 11:38:39 +03002571 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002572
Olli Etuaho856c4972016-08-08 11:38:39 +03002573 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, &publicType);
Jamie Madill502d66f2013-06-20 11:55:52 -04002574
Olli Etuaho8a176262016-08-16 14:23:01 +03002575 if (checkIsValidTypeAndQualifierForArray(arrayLocation, publicType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002576 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05002577 TType arrayType = TType(publicType);
Olli Etuaho856c4972016-08-08 11:38:39 +03002578 unsigned int size = checkIsValidArraySize(arrayLocation, indexExpression);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03002579 arrayType.makeArray(size);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002580
jchen104cdac9e2017-05-08 11:01:20 +08002581 if (IsAtomicCounter(publicType.getBasicType()))
2582 {
2583 checkAtomicCounterOffsetIsNotOverlapped(publicType, kAtomicCounterArrayStride * size,
2584 true, identifierLocation, arrayType);
2585 }
2586
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002587 TVariable *variable = nullptr;
Olli Etuaho383b7912016-08-05 11:22:59 +03002588 declareVariable(identifierLocation, identifier, arrayType, &variable);
Jamie Madill502d66f2013-06-20 11:55:52 -04002589
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002590 if (variable)
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002591 {
2592 TIntermSymbol *symbol =
2593 new TIntermSymbol(variable->getUniqueId(), identifier, arrayType);
2594 symbol->setLine(identifierLocation);
2595 declarationOut->appendDeclarator(symbol);
2596 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002597 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002598}
2599
Olli Etuaho13389b62016-10-16 11:48:18 +01002600void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2601 const TSourceLoc &identifierLocation,
2602 const TString &identifier,
2603 const TSourceLoc &initLocation,
2604 TIntermTyped *initializer,
2605 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002606{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002607 // If the declaration starting this declarator list was empty (example: int,), some checks were
2608 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002609 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002610 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002611 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2612 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002613 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002614
Olli Etuaho856c4972016-08-08 11:38:39 +03002615 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002616
Olli Etuaho13389b62016-10-16 11:48:18 +01002617 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002618 if (executeInitializer(identifierLocation, identifier, publicType, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002619 {
2620 //
2621 // build the intermediate representation
2622 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002623 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002624 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002625 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002626 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002627 }
2628}
2629
Olli Etuaho13389b62016-10-16 11:48:18 +01002630void TParseContext::parseArrayInitDeclarator(const TPublicType &publicType,
2631 const TSourceLoc &identifierLocation,
2632 const TString &identifier,
2633 const TSourceLoc &indexLocation,
2634 TIntermTyped *indexExpression,
2635 const TSourceLoc &initLocation,
2636 TIntermTyped *initializer,
2637 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002638{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002639 // If the declaration starting this declarator list was empty (example: int,), some checks were
2640 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002641 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002642 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002643 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2644 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002645 }
2646
Olli Etuaho856c4972016-08-08 11:38:39 +03002647 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002648
Olli Etuaho8a176262016-08-16 14:23:01 +03002649 checkIsValidTypeAndQualifierForArray(indexLocation, publicType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002650
2651 TPublicType arrayType(publicType);
2652
Olli Etuaho856c4972016-08-08 11:38:39 +03002653 unsigned int size = 0u;
Jamie Madillb98c3a82015-07-23 14:26:04 -04002654 // If indexExpression is nullptr, then the array will eventually get its size implicitly from
2655 // the initializer.
Olli Etuaho383b7912016-08-05 11:22:59 +03002656 if (indexExpression != nullptr)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002657 {
Olli Etuaho856c4972016-08-08 11:38:39 +03002658 size = checkIsValidArraySize(identifierLocation, indexExpression);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002659 }
2660 // Make the type an array even if size check failed.
2661 // This ensures useless error messages regarding the variable's non-arrayness won't follow.
2662 arrayType.setArraySize(size);
2663
2664 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002665 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002666 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002667 {
2668 if (initNode)
2669 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002670 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002671 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002672 }
2673}
2674
jchen104cdac9e2017-05-08 11:01:20 +08002675void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2676 const TSourceLoc &location)
2677{
2678 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2679 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2680 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2681 {
2682 error(location, "Requires both binding and offset", "layout");
2683 return;
2684 }
2685 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2686}
2687
Olli Etuahocce89652017-06-19 16:04:09 +03002688void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2689 const TPublicType &type,
2690 const TSourceLoc &loc)
2691{
2692 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2693 !getFragmentPrecisionHigh())
2694 {
2695 error(loc, "precision is not supported in fragment shader", "highp");
2696 }
2697
2698 if (!CanSetDefaultPrecisionOnType(type))
2699 {
2700 error(loc, "illegal type argument for default precision qualifier",
2701 getBasicString(type.getBasicType()));
2702 return;
2703 }
2704 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2705}
2706
Shaob5cc1192017-07-06 10:47:20 +08002707bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2708{
2709 switch (typeQualifier.layoutQualifier.primitiveType)
2710 {
2711 case EptLines:
2712 case EptLinesAdjacency:
2713 case EptTriangles:
2714 case EptTrianglesAdjacency:
2715 return typeQualifier.qualifier == EvqGeometryIn;
2716
2717 case EptLineStrip:
2718 case EptTriangleStrip:
2719 return typeQualifier.qualifier == EvqGeometryOut;
2720
2721 case EptPoints:
2722 return true;
2723
2724 default:
2725 UNREACHABLE();
2726 return false;
2727 }
2728}
2729
Jiawei Shaod8105a02017-08-08 09:54:36 +08002730void TParseContext::setGeometryShaderInputArraySizes()
2731{
2732 // TODO(jiawei.shao@intel.com): check former input array sizes match the input primitive
2733 // declaration.
2734 ASSERT(mGeometryShaderInputArraySize == 0);
2735 mGeometryShaderInputArraySize =
2736 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType);
2737}
2738
Shaob5cc1192017-07-06 10:47:20 +08002739bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2740{
2741 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2742
2743 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2744
2745 if (layoutQualifier.maxVertices != -1)
2746 {
2747 error(typeQualifier.line,
2748 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2749 return false;
2750 }
2751
2752 // Set mGeometryInputPrimitiveType if exists
2753 if (layoutQualifier.primitiveType != EptUndefined)
2754 {
2755 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2756 {
2757 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2758 return false;
2759 }
2760
2761 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2762 {
2763 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shaod8105a02017-08-08 09:54:36 +08002764 setGeometryShaderInputArraySizes();
Shaob5cc1192017-07-06 10:47:20 +08002765 }
2766 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2767 {
2768 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2769 "layout");
2770 return false;
2771 }
2772 }
2773
2774 // Set mGeometryInvocations if exists
2775 if (layoutQualifier.invocations > 0)
2776 {
2777 if (mGeometryShaderInvocations == 0)
2778 {
2779 mGeometryShaderInvocations = layoutQualifier.invocations;
2780 }
2781 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2782 {
2783 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2784 "layout");
2785 return false;
2786 }
2787 }
2788
2789 return true;
2790}
2791
2792bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2793{
2794 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2795
2796 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2797
2798 if (layoutQualifier.invocations > 0)
2799 {
2800 error(typeQualifier.line,
2801 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2802 return false;
2803 }
2804
2805 // Set mGeometryOutputPrimitiveType if exists
2806 if (layoutQualifier.primitiveType != EptUndefined)
2807 {
2808 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2809 {
2810 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2811 return false;
2812 }
2813
2814 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2815 {
2816 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2817 }
2818 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2819 {
2820 error(typeQualifier.line,
2821 "primitive doesn't match earlier output primitive declaration", "layout");
2822 return false;
2823 }
2824 }
2825
2826 // Set mGeometryMaxVertices if exists
2827 if (layoutQualifier.maxVertices > -1)
2828 {
2829 if (mGeometryShaderMaxVertices == -1)
2830 {
2831 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2832 }
2833 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2834 {
2835 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2836 "layout");
2837 return false;
2838 }
2839 }
2840
2841 return true;
2842}
2843
Martin Radev70866b82016-07-22 15:27:42 +03002844void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002845{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002846 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002847 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002848
Martin Radev70866b82016-07-22 15:27:42 +03002849 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2850 typeQualifier.line);
2851
Jamie Madillc2128ff2016-07-04 10:26:17 -04002852 // It should never be the case, but some strange parser errors can send us here.
2853 if (layoutQualifier.isEmpty())
2854 {
2855 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002856 return;
2857 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002858
Martin Radev802abe02016-08-04 17:48:32 +03002859 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002860 {
Olli Etuaho43364892017-02-13 16:00:12 +00002861 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002862 return;
2863 }
2864
Olli Etuaho43364892017-02-13 16:00:12 +00002865 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2866
2867 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002868
2869 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2870
Andrei Volykhina5527072017-03-22 16:46:30 +03002871 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
2872
jchen104cdac9e2017-05-08 11:01:20 +08002873 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
2874
Martin Radev802abe02016-08-04 17:48:32 +03002875 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04002876 {
Martin Radev802abe02016-08-04 17:48:32 +03002877 if (mComputeShaderLocalSizeDeclared &&
2878 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
2879 {
2880 error(typeQualifier.line, "Work group size does not match the previous declaration",
2881 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002882 return;
2883 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002884
Martin Radev802abe02016-08-04 17:48:32 +03002885 if (mShaderVersion < 310)
2886 {
2887 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002888 return;
2889 }
Jamie Madill099c0f32013-06-20 11:55:52 -04002890
Martin Radev4c4c8e72016-08-04 12:25:34 +03002891 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03002892 {
2893 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002894 return;
2895 }
2896
2897 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
2898 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
2899
2900 const TConstantUnion *maxComputeWorkGroupSizeData =
2901 maxComputeWorkGroupSize->getConstPointer();
2902
2903 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
2904 {
2905 if (layoutQualifier.localSize[i] != -1)
2906 {
2907 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
2908 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
2909 if (mComputeShaderLocalSize[i] < 1 ||
2910 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
2911 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00002912 std::stringstream reasonStream;
2913 reasonStream << "invalid value: Value must be at least 1 and no greater than "
2914 << maxComputeWorkGroupSizeValue;
2915 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03002916
Olli Etuaho4de340a2016-12-16 09:32:03 +00002917 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03002918 return;
2919 }
2920 }
2921 }
2922
2923 mComputeShaderLocalSizeDeclared = true;
2924 }
Shaob5cc1192017-07-06 10:47:20 +08002925 else if (typeQualifier.qualifier == EvqGeometryIn)
2926 {
2927 if (mShaderVersion < 310)
2928 {
2929 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
2930 return;
2931 }
2932
2933 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
2934 {
2935 return;
2936 }
2937 }
2938 else if (typeQualifier.qualifier == EvqGeometryOut)
2939 {
2940 if (mShaderVersion < 310)
2941 {
2942 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
2943 "layout");
2944 return;
2945 }
2946
2947 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
2948 {
2949 return;
2950 }
2951 }
Olli Etuaho95468d12017-05-04 11:14:34 +03002952 else if (isMultiviewExtensionEnabled() && typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00002953 {
2954 // This error is only specified in WebGL, but tightens unspecified behavior in the native
2955 // specification.
2956 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
2957 {
2958 error(typeQualifier.line, "Number of views does not match the previous declaration",
2959 "layout");
2960 return;
2961 }
2962
2963 if (layoutQualifier.numViews == -1)
2964 {
2965 error(typeQualifier.line, "No num_views specified", "layout");
2966 return;
2967 }
2968
2969 if (layoutQualifier.numViews > mMaxNumViews)
2970 {
2971 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
2972 "layout");
2973 return;
2974 }
2975
2976 mNumViews = layoutQualifier.numViews;
2977 }
Martin Radev802abe02016-08-04 17:48:32 +03002978 else
Jamie Madill1566ef72013-06-20 11:55:54 -04002979 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00002980 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03002981 {
Martin Radev802abe02016-08-04 17:48:32 +03002982 return;
2983 }
2984
Jiajia Qinbc585152017-06-23 15:42:17 +08002985 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03002986 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002987 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00002988 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03002989 return;
2990 }
2991
2992 if (mShaderVersion < 300)
2993 {
2994 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
2995 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03002996 return;
2997 }
2998
Olli Etuaho09b04a22016-12-15 13:30:26 +00002999 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003000
3001 if (layoutQualifier.matrixPacking != EmpUnspecified)
3002 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003003 if (typeQualifier.qualifier == EvqUniform)
3004 {
3005 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3006 }
3007 else if (typeQualifier.qualifier == EvqBuffer)
3008 {
3009 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3010 }
Martin Radev802abe02016-08-04 17:48:32 +03003011 }
3012
3013 if (layoutQualifier.blockStorage != EbsUnspecified)
3014 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003015 if (typeQualifier.qualifier == EvqUniform)
3016 {
3017 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3018 }
3019 else if (typeQualifier.qualifier == EvqBuffer)
3020 {
3021 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3022 }
Martin Radev802abe02016-08-04 17:48:32 +03003023 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003024 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003025}
3026
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003027TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3028 const TFunction &function,
3029 const TSourceLoc &location,
3030 bool insertParametersToSymbolTable)
3031{
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003032 checkIsNotReserved(location, function.getName());
3033
Olli Etuahofe486322017-03-21 09:30:54 +00003034 TIntermFunctionPrototype *prototype =
3035 new TIntermFunctionPrototype(function.getReturnType(), TSymbolUniqueId(function));
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003036 // TODO(oetuaho@nvidia.com): Instead of converting the function information here, the node could
3037 // point to the data that already exists in the symbol table.
3038 prototype->getFunctionSymbolInfo()->setFromFunction(function);
3039 prototype->setLine(location);
3040
3041 for (size_t i = 0; i < function.getParamCount(); i++)
3042 {
3043 const TConstParameter &param = function.getParam(i);
3044
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003045 TIntermSymbol *symbol = nullptr;
3046
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003047 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3048 // be used for unused args).
3049 if (param.name != nullptr)
3050 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003051 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003052 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003053 {
Olli Etuaho0f684632017-07-13 12:42:15 +03003054 TVariable *variable = symbolTable.declareVariable(param.name, *param.type);
3055 if (variable)
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003056 {
3057 symbol = new TIntermSymbol(variable->getUniqueId(), variable->getName(),
3058 variable->getType());
3059 }
3060 else
3061 {
Olli Etuaho85d624a2017-08-07 13:42:33 +03003062 error(location, "redefinition", param.name->c_str());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003063 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003064 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003065 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003066 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003067 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003068 // The parameter had no name or declaring the symbol failed - either way, add a nameless
3069 // symbol.
3070 symbol = new TIntermSymbol(0, "", *param.type);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003071 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003072 symbol->setLine(location);
3073 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003074 }
3075 return prototype;
3076}
3077
Olli Etuaho16c745a2017-01-16 17:02:27 +00003078TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3079 const TFunction &parsedFunction,
3080 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003081{
Olli Etuaho476197f2016-10-11 13:59:08 +01003082 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3083 // first declaration. Either way the instance in the symbol table is used to track whether the
3084 // function is declared multiple times.
3085 TFunction *function = static_cast<TFunction *>(
3086 symbolTable.find(parsedFunction.getMangledName(), getShaderVersion()));
3087 if (function->hasPrototypeDeclaration() && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003088 {
3089 // ESSL 1.00.17 section 4.2.7.
3090 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3091 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003092 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003093 function->setHasPrototypeDeclaration();
Olli Etuaho5d653182016-01-04 14:43:28 +02003094
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003095 TIntermFunctionPrototype *prototype =
3096 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003097
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003098 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003099
3100 if (!symbolTable.atGlobalLevel())
3101 {
3102 // ESSL 3.00.4 section 4.2.4.
3103 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003104 }
3105
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003106 return prototype;
3107}
3108
Olli Etuaho336b1472016-10-05 16:37:55 +01003109TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003110 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003111 TIntermBlock *functionBody,
3112 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003113{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003114 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003115 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3116 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003117 error(location, "function does not return a value:",
3118 functionPrototype->getFunctionSymbolInfo()->getName().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003119 }
3120
Olli Etuahof51fdd22016-10-03 10:03:40 +01003121 if (functionBody == nullptr)
3122 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003123 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003124 functionBody->setLine(location);
3125 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003126 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003127 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003128 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003129
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003130 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003131 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003132}
3133
Olli Etuaho476197f2016-10-11 13:59:08 +01003134void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
3135 TFunction **function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003136 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003137{
Olli Etuaho476197f2016-10-11 13:59:08 +01003138 ASSERT(function);
3139 ASSERT(*function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003140 const TSymbol *builtIn =
Olli Etuaho476197f2016-10-11 13:59:08 +01003141 symbolTable.findBuiltIn((*function)->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003142
3143 if (builtIn)
3144 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003145 error(location, "built-in functions cannot be redefined", (*function)->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003146 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003147 else
Jamie Madill185fb402015-06-12 15:48:48 -04003148 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003149 TFunction *prevDec = static_cast<TFunction *>(
3150 symbolTable.find((*function)->getMangledName(), getShaderVersion()));
3151
3152 // Note: 'prevDec' could be 'function' if this is the first time we've seen function as it
3153 // would have just been put in the symbol table. Otherwise, we're looking up an earlier
3154 // occurance.
3155 if (*function != prevDec)
3156 {
3157 // Swap the parameters of the previous declaration to the parameters of the function
3158 // definition (parameter names may differ).
3159 prevDec->swapParameters(**function);
3160
3161 // The function definition will share the same symbol as any previous declaration.
3162 *function = prevDec;
3163 }
3164
3165 if ((*function)->isDefined())
3166 {
3167 error(location, "function already has a body", (*function)->getName().c_str());
3168 }
3169
3170 (*function)->setDefined();
Jamie Madill185fb402015-06-12 15:48:48 -04003171 }
Jamie Madill185fb402015-06-12 15:48:48 -04003172
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003173 // Remember the return type for later checking for return statements.
Olli Etuaho476197f2016-10-11 13:59:08 +01003174 mCurrentFunctionType = &((*function)->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003175 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003176
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003177 *prototypeOut = createPrototypeNodeFromFunction(**function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003178 setLoopNestingLevel(0);
3179}
3180
Jamie Madillb98c3a82015-07-23 14:26:04 -04003181TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003182{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003183 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003184 // We don't know at this point whether this is a function definition or a prototype.
3185 // The definition production code will check for redefinitions.
3186 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003187 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003188 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3189 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003190 //
3191 TFunction *prevDec =
3192 static_cast<TFunction *>(symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303193
Martin Radevda6254b2016-12-14 17:00:36 +02003194 if (getShaderVersion() >= 300 &&
3195 symbolTable.hasUnmangledBuiltInForShaderVersion(function->getName().c_str(),
3196 getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303197 {
Martin Radevda6254b2016-12-14 17:00:36 +02003198 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303199 // Therefore overloading or redefining builtin functions is an error.
3200 error(location, "Name of a built-in function cannot be redeclared as function",
3201 function->getName().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303202 }
3203 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003204 {
3205 if (prevDec->getReturnType() != function->getReturnType())
3206 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003207 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003208 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003209 }
3210 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3211 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003212 if (prevDec->getParam(i).type->getQualifier() !=
3213 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003214 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003215 error(location,
3216 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003217 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003218 }
3219 }
3220 }
3221
3222 //
3223 // Check for previously declared variables using the same name.
3224 //
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003225 TSymbol *prevSym = symbolTable.find(function->getName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003226 if (prevSym)
3227 {
3228 if (!prevSym->isFunction())
3229 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003230 error(location, "redefinition of a function", function->getName().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003231 }
3232 }
3233 else
3234 {
3235 // Insert the unmangled name to detect potential future redefinition as a variable.
Olli Etuaho476197f2016-10-11 13:59:08 +01003236 symbolTable.getOuterLevel()->insertUnmangled(function);
Jamie Madill185fb402015-06-12 15:48:48 -04003237 }
3238
3239 // We're at the inner scope level of the function's arguments and body statement.
3240 // Add the function prototype to the surrounding scope instead.
3241 symbolTable.getOuterLevel()->insert(function);
3242
Olli Etuaho78d13742017-01-18 13:06:10 +00003243 // Raise error message if main function takes any parameters or return anything other than void
3244 if (function->getName() == "main")
3245 {
3246 if (function->getParamCount() > 0)
3247 {
3248 error(location, "function cannot take any parameter(s)", "main");
3249 }
3250 if (function->getReturnType().getBasicType() != EbtVoid)
3251 {
3252 error(location, "main function cannot return a value",
3253 function->getReturnType().getBasicString());
3254 }
3255 }
3256
Jamie Madill185fb402015-06-12 15:48:48 -04003257 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003258 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3259 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003260 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3261 //
3262 return function;
3263}
3264
Olli Etuaho9de84a52016-06-14 17:36:01 +03003265TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
3266 const TString *name,
3267 const TSourceLoc &location)
3268{
3269 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3270 {
3271 error(location, "no qualifiers allowed for function return",
3272 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003273 }
3274 if (!type.layoutQualifier.isEmpty())
3275 {
3276 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003277 }
jchen10cc2a10e2017-05-03 14:05:12 +08003278 // make sure an opaque type is not involved as well...
3279 std::string reason(getBasicString(type.getBasicType()));
3280 reason += "s can't be function return values";
3281 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003282 if (mShaderVersion < 300)
3283 {
3284 // Array return values are forbidden, but there's also no valid syntax for declaring array
3285 // return values in ESSL 1.00.
Olli Etuaho77ba4082016-12-16 12:01:18 +00003286 ASSERT(type.arraySize == 0 || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003287
3288 if (type.isStructureContainingArrays())
3289 {
3290 // ESSL 1.00.17 section 6.1 Function Definitions
3291 error(location, "structures containing arrays can't be function return values",
3292 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003293 }
3294 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003295
3296 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003297 return new TFunction(&symbolTable, name, new TType(type));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003298}
3299
Olli Etuahocce89652017-06-19 16:04:09 +03003300TFunction *TParseContext::addNonConstructorFunc(const TString *name, const TSourceLoc &loc)
3301{
Olli Etuahocce89652017-06-19 16:04:09 +03003302 const TType *returnType = TCache::getType(EbtVoid, EbpUndefined);
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003303 return new TFunction(&symbolTable, name, returnType);
Olli Etuahocce89652017-06-19 16:04:09 +03003304}
3305
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003306TFunction *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003307{
Olli Etuahocce89652017-06-19 16:04:09 +03003308 if (mShaderVersion < 300 && publicType.array)
3309 {
3310 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3311 "[]");
3312 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003313 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003314 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003315 error(publicType.getLine(), "constructor can't be a structure definition",
3316 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003317 }
3318
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003319 TType *type = new TType(publicType);
3320 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003321 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003322 error(publicType.getLine(), "cannot construct this type",
3323 getBasicString(publicType.getBasicType()));
3324 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003325 }
3326
Olli Etuahoa5e693a2017-07-13 16:07:26 +03003327 return new TFunction(&symbolTable, nullptr, type, EOpConstruct);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003328}
3329
Olli Etuahocce89652017-06-19 16:04:09 +03003330TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3331 const TString *name,
3332 const TSourceLoc &nameLoc)
3333{
3334 if (publicType.getBasicType() == EbtVoid)
3335 {
3336 error(nameLoc, "illegal use of type 'void'", name->c_str());
3337 }
3338 checkIsNotReserved(nameLoc, *name);
3339 TType *type = new TType(publicType);
3340 TParameter param = {name, type};
3341 return param;
3342}
3343
3344TParameter TParseContext::parseParameterArrayDeclarator(const TString *identifier,
3345 const TSourceLoc &identifierLoc,
3346 TIntermTyped *arraySize,
3347 const TSourceLoc &arrayLoc,
3348 TPublicType *type)
3349{
Olli Etuahoe0803872017-08-23 15:30:23 +03003350 checkArrayElementIsNotArray(arrayLoc, *type);
Olli Etuahocce89652017-06-19 16:04:09 +03003351 unsigned int size = checkIsValidArraySize(arrayLoc, arraySize);
3352 type->setArraySize(size);
3353 return parseParameterDeclarator(*type, identifier, identifierLoc);
3354}
3355
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003356bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(TIntermSequence *arguments,
3357 TType type,
3358 const TSourceLoc &line)
3359{
3360 if (arguments->empty())
3361 {
3362 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3363 return false;
3364 }
3365 for (TIntermNode *arg : *arguments)
3366 {
3367 TIntermTyped *element = arg->getAsTyped();
3368 ASSERT(element);
3369 size_t dimensionalityFromElement = element->getType().getArraySizes().size() + 1u;
3370 if (dimensionalityFromElement > type.getArraySizes().size())
3371 {
3372 error(line, "constructing from a non-dereferenced array", "constructor");
3373 return false;
3374 }
3375 else if (dimensionalityFromElement < type.getArraySizes().size())
3376 {
3377 if (dimensionalityFromElement == 1u)
3378 {
3379 error(line, "implicitly sized array of arrays constructor argument is not an array",
3380 "constructor");
3381 }
3382 else
3383 {
3384 error(line,
3385 "implicitly sized array of arrays constructor argument dimensionality is too "
3386 "low",
3387 "constructor");
3388 }
3389 return false;
3390 }
3391 }
3392 return true;
3393}
3394
Jamie Madillb98c3a82015-07-23 14:26:04 -04003395// This function is used to test for the correctness of the parameters passed to various constructor
3396// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003397//
Olli Etuaho856c4972016-08-08 11:38:39 +03003398// 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 +00003399//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003400TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00003401 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303402 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003403{
Olli Etuaho856c4972016-08-08 11:38:39 +03003404 if (type.isUnsizedArray())
3405 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003406 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003407 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003408 type.sizeUnsizedArrays(TVector<unsigned int>());
Olli Etuaho3ec75682017-07-05 17:02:55 +03003409 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003410 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003411 TIntermTyped *firstElement = arguments->at(0)->getAsTyped();
3412 ASSERT(firstElement);
3413 type.setArraySize(type.getArraySizes().size() - 1u,
3414 static_cast<unsigned int>(arguments->size()));
3415 for (size_t i = 0; i < firstElement->getType().getArraySizes().size(); ++i)
3416 {
3417 if (type.getArraySizes()[i] == 0u)
3418 {
3419 type.setArraySize(i, firstElement->getType().getArraySizes().at(i));
3420 }
3421 }
3422 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003423 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003424
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003425 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003426 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003427 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003428 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003429
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003430 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003431 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003432
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003433 // TODO(oetuaho@nvidia.com): Add support for folding array constructors.
3434 if (!constructorNode->isArray())
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003435 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003436 return constructorNode->fold(mDiagnostics);
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003437 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003438 return constructorNode;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003439}
3440
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003441//
3442// Interface/uniform blocks
Jiawei Shaod8105a02017-08-08 09:54:36 +08003443// TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003444//
Olli Etuaho13389b62016-10-16 11:48:18 +01003445TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003446 const TTypeQualifierBuilder &typeQualifierBuilder,
3447 const TSourceLoc &nameLine,
3448 const TString &blockName,
3449 TFieldList *fieldList,
3450 const TString *instanceName,
3451 const TSourceLoc &instanceLine,
3452 TIntermTyped *arrayIndex,
3453 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003454{
Olli Etuaho856c4972016-08-08 11:38:39 +03003455 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003456
Olli Etuaho77ba4082016-12-16 12:01:18 +00003457 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003458
Jiajia Qinbc585152017-06-23 15:42:17 +08003459 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003460 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003461 error(typeQualifier.line,
3462 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3463 "3.10",
3464 getQualifierString(typeQualifier.qualifier));
3465 }
3466 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3467 {
3468 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003469 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003470 }
3471
Martin Radev70866b82016-07-22 15:27:42 +03003472 if (typeQualifier.invariant)
3473 {
3474 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3475 }
3476
Jiajia Qinbc585152017-06-23 15:42:17 +08003477 if (typeQualifier.qualifier != EvqBuffer)
3478 {
3479 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3480 }
Olli Etuaho43364892017-02-13 16:00:12 +00003481
jchen10af713a22017-04-19 09:10:56 +08003482 // add array index
3483 unsigned int arraySize = 0;
3484 if (arrayIndex != nullptr)
3485 {
3486 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3487 }
3488
3489 if (mShaderVersion < 310)
3490 {
3491 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3492 }
3493 else
3494 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003495 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3496 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003497 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003498
Andrei Volykhina5527072017-03-22 16:46:30 +03003499 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3500
Jamie Madill099c0f32013-06-20 11:55:52 -04003501 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003502 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003503
Jamie Madill099c0f32013-06-20 11:55:52 -04003504 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3505 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003506 if (typeQualifier.qualifier == EvqUniform)
3507 {
3508 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3509 }
3510 else if (typeQualifier.qualifier == EvqBuffer)
3511 {
3512 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3513 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003514 }
3515
Jamie Madill1566ef72013-06-20 11:55:54 -04003516 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3517 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003518 if (typeQualifier.qualifier == EvqUniform)
3519 {
3520 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3521 }
3522 else if (typeQualifier.qualifier == EvqBuffer)
3523 {
3524 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3525 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003526 }
3527
Olli Etuaho856c4972016-08-08 11:38:39 +03003528 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003529
Martin Radev2cc85b32016-08-05 16:22:53 +03003530 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3531
Olli Etuaho0f684632017-07-13 12:42:15 +03003532 if (!symbolTable.declareInterfaceBlockName(&blockName))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303533 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003534 error(nameLine, "redefinition of an interface block name", blockName.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003535 }
3536
Jamie Madill98493dd2013-07-08 14:39:03 -04003537 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303538 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3539 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003540 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303541 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003542 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303543 {
jchen10cc2a10e2017-05-03 14:05:12 +08003544 std::string reason("unsupported type - ");
3545 reason += fieldType->getBasicString();
3546 reason += " types are not allowed in interface blocks";
3547 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003548 }
3549
Jamie Madill98493dd2013-07-08 14:39:03 -04003550 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003551 switch (qualifier)
3552 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003553 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003554 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003555 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003556 if (typeQualifier.qualifier == EvqBuffer)
3557 {
3558 error(field->line(), "invalid qualifier on shader storage block member",
3559 getQualifierString(qualifier));
3560 }
3561 break;
3562 case EvqBuffer:
3563 if (typeQualifier.qualifier == EvqUniform)
3564 {
3565 error(field->line(), "invalid qualifier on uniform block member",
3566 getQualifierString(qualifier));
3567 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003568 break;
3569 default:
3570 error(field->line(), "invalid qualifier on interface block member",
3571 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003572 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003573 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003574
Martin Radev70866b82016-07-22 15:27:42 +03003575 if (fieldType->isInvariant())
3576 {
3577 error(field->line(), "invalid qualifier on interface block member", "invariant");
3578 }
3579
Jamie Madilla5efff92013-06-06 11:56:47 -04003580 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003581 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003582 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003583 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003584
Jamie Madill98493dd2013-07-08 14:39:03 -04003585 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003586 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003587 error(field->line(), "invalid layout qualifier: cannot be used here",
3588 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003589 }
3590
Jamie Madill98493dd2013-07-08 14:39:03 -04003591 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003592 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003593 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003594 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003595 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003596 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003597 warning(field->line(),
3598 "extraneous layout qualifier: only has an effect on matrix types",
3599 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003600 }
3601
Jamie Madill98493dd2013-07-08 14:39:03 -04003602 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003603
3604 if (typeQualifier.qualifier == EvqBuffer)
3605 {
3606 // set memory qualifiers
3607 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3608 // qualified with a memory qualifier, it is as if all of its members were declared with
3609 // the same memory qualifier.
3610 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3611 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3612 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3613 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3614 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3615 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3616 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3617 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3618 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3619 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3620 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003621 }
3622
Jamie Madillb98c3a82015-07-23 14:26:04 -04003623 TInterfaceBlock *interfaceBlock =
Shaob18c33e2017-08-16 12:37:51 +08003624 new TInterfaceBlock(&blockName, fieldList, instanceName, blockLayoutQualifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003625 TType interfaceBlockType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
3626 if (arrayIndex != nullptr)
3627 {
3628 interfaceBlockType.makeArray(arraySize);
3629 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003630
3631 TString symbolName = "";
Jamie Madillb98c3a82015-07-23 14:26:04 -04003632 int symbolId = 0;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003633
Jamie Madill98493dd2013-07-08 14:39:03 -04003634 if (!instanceName)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003635 {
3636 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003637 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3638 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003639 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303640 TType *fieldType = field->type();
Jamie Madill98493dd2013-07-08 14:39:03 -04003641
3642 // set parent pointer of the field variable
3643 fieldType->setInterfaceBlock(interfaceBlock);
3644
Olli Etuaho0f684632017-07-13 12:42:15 +03003645 TVariable *fieldVariable = symbolTable.declareVariable(&field->name(), *fieldType);
Jamie Madill98493dd2013-07-08 14:39:03 -04003646
Olli Etuaho0f684632017-07-13 12:42:15 +03003647 if (fieldVariable)
3648 {
3649 fieldVariable->setQualifier(typeQualifier.qualifier);
3650 }
3651 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303652 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003653 error(field->line(), "redefinition of an interface block member name",
3654 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003655 }
3656 }
3657 }
3658 else
3659 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003660 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003661
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003662 // add a symbol for this interface block
Olli Etuaho0f684632017-07-13 12:42:15 +03003663 TVariable *instanceTypeDef = symbolTable.declareVariable(instanceName, interfaceBlockType);
3664 if (instanceTypeDef)
3665 {
3666 instanceTypeDef->setQualifier(typeQualifier.qualifier);
3667 symbolId = instanceTypeDef->getUniqueId();
3668 }
3669 else
Arun Patole7e7e68d2015-05-22 12:02:25 +05303670 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003671 error(instanceLine, "redefinition of an interface block instance name",
3672 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003673 }
Olli Etuaho0f684632017-07-13 12:42:15 +03003674 symbolName = *instanceName;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003675 }
3676
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003677 TIntermSymbol *blockSymbol = new TIntermSymbol(symbolId, symbolName, interfaceBlockType);
3678 blockSymbol->setLine(typeQualifier.line);
Olli Etuaho13389b62016-10-16 11:48:18 +01003679 TIntermDeclaration *declaration = new TIntermDeclaration();
3680 declaration->appendDeclarator(blockSymbol);
3681 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003682
3683 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003684 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003685}
3686
Olli Etuaho383b7912016-08-05 11:22:59 +03003687void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003688{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003689 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003690
3691 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003692 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303693 if (mStructNestingLevel > 1)
3694 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003695 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003696 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003697}
3698
3699void TParseContext::exitStructDeclaration()
3700{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003701 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003702}
3703
Olli Etuaho8a176262016-08-16 14:23:01 +03003704void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003705{
Jamie Madillacb4b812016-11-07 13:50:29 -05003706 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303707 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003708 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003709 }
3710
Arun Patole7e7e68d2015-05-22 12:02:25 +05303711 if (field.type()->getBasicType() != EbtStruct)
3712 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003713 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003714 }
3715
3716 // We're already inside a structure definition at this point, so add
3717 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303718 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3719 {
Jamie Madill41a49272014-03-18 16:10:13 -04003720 std::stringstream reasonStream;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003721 reasonStream << "Reference of struct type " << field.type()->getStruct()->name().c_str()
3722 << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003723 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003724 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003725 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003726 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003727}
3728
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003729//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003730// Parse an array index expression
3731//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003732TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3733 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303734 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003735{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003736 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3737 {
3738 if (baseExpression->getAsSymbolNode())
3739 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303740 error(location, " left of '[' is not of type array, matrix, or vector ",
3741 baseExpression->getAsSymbolNode()->getSymbol().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003742 }
3743 else
3744 {
3745 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3746 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003747
Olli Etuaho3ec75682017-07-05 17:02:55 +03003748 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003749 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003750
Jiawei Shaod8105a02017-08-08 09:54:36 +08003751 if (baseExpression->getQualifier() == EvqPerVertexIn)
3752 {
3753 ASSERT(mShaderType == GL_GEOMETRY_SHADER_OES);
3754 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3755 {
3756 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3757 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3758 }
3759 }
3760
Jamie Madill21c1e452014-12-29 11:33:41 -05003761 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3762
Olli Etuaho36b05142015-11-12 13:10:42 +02003763 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3764 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3765 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3766 // index is a constant expression.
3767 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3768 {
3769 if (baseExpression->isInterfaceBlock())
3770 {
Jiawei Shaod8105a02017-08-08 09:54:36 +08003771 // TODO(jiawei.shao@intel.com): implement GL_OES_shader_io_blocks.
3772 switch (baseExpression->getQualifier())
3773 {
3774 case EvqPerVertexIn:
3775 break;
3776 case EvqUniform:
3777 case EvqBuffer:
3778 error(location,
3779 "array indexes for uniform block arrays and shader storage block arrays "
3780 "must be constant integral expressions",
3781 "[");
3782 break;
3783 default:
3784 UNREACHABLE();
3785 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003786 }
3787 else if (baseExpression->getQualifier() == EvqFragmentOut)
3788 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003789 error(location,
3790 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003791 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003792 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3793 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003794 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003795 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003796 }
3797
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003798 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003799 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003800 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3801 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3802 // constant fold expressions that are not constant expressions). The most compatible way to
3803 // handle this case is to report a warning instead of an error and force the index to be in
3804 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003805 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003806 int index = 0;
3807 if (indexConstantUnion->getBasicType() == EbtInt)
3808 {
3809 index = indexConstantUnion->getIConst(0);
3810 }
3811 else if (indexConstantUnion->getBasicType() == EbtUInt)
3812 {
3813 index = static_cast<int>(indexConstantUnion->getUConst(0));
3814 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003815
3816 int safeIndex = -1;
3817
3818 if (baseExpression->isArray())
Jamie Madill7164cf42013-07-08 13:30:59 -04003819 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003820 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003821 {
Olli Etuahodaaff1c2017-07-05 18:03:26 +03003822 if (!isExtensionEnabled("GL_EXT_draw_buffers"))
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003823 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003824 outOfRangeError(outOfRangeIndexIsError, location,
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003825 "array index for gl_FragData must be zero when "
Olli Etuaho4de340a2016-12-16 09:32:03 +00003826 "GL_EXT_draw_buffers is disabled",
3827 "[");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003828 safeIndex = 0;
3829 }
Olli Etuaho90892fb2016-07-14 14:44:51 +03003830 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003831 // Only do generic out-of-range check if similar error hasn't already been reported.
3832 if (safeIndex < 0)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003833 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003834 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003835 baseExpression->getOutermostArraySize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003836 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003837 }
3838 }
3839 else if (baseExpression->isMatrix())
3840 {
3841 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
Olli Etuaho90892fb2016-07-14 14:44:51 +03003842 baseExpression->getType().getCols(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003843 "matrix field selection out of range");
Jamie Madill7164cf42013-07-08 13:30:59 -04003844 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003845 else if (baseExpression->isVector())
Jamie Madill7164cf42013-07-08 13:30:59 -04003846 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003847 safeIndex = checkIndexOutOfRange(outOfRangeIndexIsError, location, index,
3848 baseExpression->getType().getNominalSize(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00003849 "vector field selection out of range");
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003850 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003851
3852 ASSERT(safeIndex >= 0);
3853 // Data of constant unions can't be changed, because it may be shared with other
3854 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
3855 // sanitized object.
Olli Etuaho56229f12017-07-10 14:16:33 +03003856 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003857 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003858 TConstantUnion *safeConstantUnion = new TConstantUnion();
3859 safeConstantUnion->setIConst(safeIndex);
3860 indexConstantUnion->replaceConstantUnion(safeConstantUnion);
Olli Etuaho56229f12017-07-10 14:16:33 +03003861 indexConstantUnion->getTypePointer()->setBasicType(EbtInt);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003862 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003863
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003864 TIntermBinary *node = new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
3865 node->setLine(location);
3866 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003867 }
Jamie Madill7164cf42013-07-08 13:30:59 -04003868 else
3869 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003870 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
3871 node->setLine(location);
3872 // Indirect indexing can never be constant folded.
3873 return node;
Jamie Madill7164cf42013-07-08 13:30:59 -04003874 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003875}
3876
Olli Etuaho90892fb2016-07-14 14:44:51 +03003877int TParseContext::checkIndexOutOfRange(bool outOfRangeIndexIsError,
3878 const TSourceLoc &location,
3879 int index,
3880 int arraySize,
Olli Etuaho4de340a2016-12-16 09:32:03 +00003881 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03003882{
3883 if (index >= arraySize || index < 0)
3884 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003885 std::stringstream reasonStream;
3886 reasonStream << reason << " '" << index << "'";
3887 std::string token = reasonStream.str();
3888 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuaho90892fb2016-07-14 14:44:51 +03003889 if (index < 0)
3890 {
3891 return 0;
3892 }
3893 else
3894 {
3895 return arraySize - 1;
3896 }
3897 }
3898 return index;
3899}
3900
Jamie Madillb98c3a82015-07-23 14:26:04 -04003901TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
3902 const TSourceLoc &dotLocation,
3903 const TString &fieldString,
3904 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003905{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003906 if (baseExpression->isArray())
3907 {
3908 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003909 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003910 }
3911
3912 if (baseExpression->isVector())
3913 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003914 TVector<int> fieldOffsets;
3915 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
3916 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003917 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003918 fieldOffsets.resize(1);
3919 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003920 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003921 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
3922 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003923
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003924 return node->fold();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003925 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003926 else if (baseExpression->getBasicType() == EbtStruct)
3927 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303928 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003929 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003930 {
3931 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003932 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003933 }
3934 else
3935 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003936 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003937 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003938 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003939 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003940 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003941 {
3942 fieldFound = true;
3943 break;
3944 }
3945 }
3946 if (fieldFound)
3947 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003948 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003949 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003950 TIntermBinary *node =
3951 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
3952 node->setLine(dotLocation);
3953 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003954 }
3955 else
3956 {
3957 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003958 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003959 }
3960 }
3961 }
Jamie Madill98493dd2013-07-08 14:39:03 -04003962 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003963 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303964 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04003965 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003966 {
3967 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003968 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003969 }
3970 else
3971 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003972 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003973 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04003974 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003975 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003976 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003977 {
3978 fieldFound = true;
3979 break;
3980 }
3981 }
3982 if (fieldFound)
3983 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003984 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003985 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003986 TIntermBinary *node =
3987 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
3988 node->setLine(dotLocation);
3989 // Indexing interface blocks can never be constant folded.
3990 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003991 }
3992 else
3993 {
3994 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003995 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003996 }
3997 }
3998 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003999 else
4000 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004001 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004002 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004003 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304004 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004005 }
4006 else
4007 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304008 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004009 " field selection requires structure, vector, or interface block on left hand "
4010 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304011 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004012 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004013 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004014 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004015}
4016
Jamie Madillb98c3a82015-07-23 14:26:04 -04004017TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4018 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004019{
Martin Radev802abe02016-08-04 17:48:32 +03004020 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004021
4022 if (qualifierType == "shared")
4023 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004024 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004025 {
4026 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4027 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004028 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004029 }
4030 else if (qualifierType == "packed")
4031 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004032 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004033 {
4034 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4035 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004036 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004037 }
4038 else if (qualifierType == "std140")
4039 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004040 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004041 }
4042 else if (qualifierType == "row_major")
4043 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004044 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004045 }
4046 else if (qualifierType == "column_major")
4047 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004048 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004049 }
4050 else if (qualifierType == "location")
4051 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004052 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
4053 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004054 }
Andrei Volykhina5527072017-03-22 16:46:30 +03004055 else if (qualifierType == "yuv" && isExtensionEnabled("GL_EXT_YUV_target") &&
4056 mShaderType == GL_FRAGMENT_SHADER)
4057 {
4058 qualifier.yuv = true;
4059 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004060 else if (qualifierType == "rgba32f")
4061 {
4062 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4063 qualifier.imageInternalFormat = EiifRGBA32F;
4064 }
4065 else if (qualifierType == "rgba16f")
4066 {
4067 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4068 qualifier.imageInternalFormat = EiifRGBA16F;
4069 }
4070 else if (qualifierType == "r32f")
4071 {
4072 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4073 qualifier.imageInternalFormat = EiifR32F;
4074 }
4075 else if (qualifierType == "rgba8")
4076 {
4077 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4078 qualifier.imageInternalFormat = EiifRGBA8;
4079 }
4080 else if (qualifierType == "rgba8_snorm")
4081 {
4082 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4083 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4084 }
4085 else if (qualifierType == "rgba32i")
4086 {
4087 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4088 qualifier.imageInternalFormat = EiifRGBA32I;
4089 }
4090 else if (qualifierType == "rgba16i")
4091 {
4092 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4093 qualifier.imageInternalFormat = EiifRGBA16I;
4094 }
4095 else if (qualifierType == "rgba8i")
4096 {
4097 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4098 qualifier.imageInternalFormat = EiifRGBA8I;
4099 }
4100 else if (qualifierType == "r32i")
4101 {
4102 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4103 qualifier.imageInternalFormat = EiifR32I;
4104 }
4105 else if (qualifierType == "rgba32ui")
4106 {
4107 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4108 qualifier.imageInternalFormat = EiifRGBA32UI;
4109 }
4110 else if (qualifierType == "rgba16ui")
4111 {
4112 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4113 qualifier.imageInternalFormat = EiifRGBA16UI;
4114 }
4115 else if (qualifierType == "rgba8ui")
4116 {
4117 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4118 qualifier.imageInternalFormat = EiifRGBA8UI;
4119 }
4120 else if (qualifierType == "r32ui")
4121 {
4122 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4123 qualifier.imageInternalFormat = EiifR32UI;
4124 }
Shaob5cc1192017-07-06 10:47:20 +08004125 else if (qualifierType == "points" && isExtensionEnabled("GL_OES_geometry_shader") &&
4126 mShaderType == GL_GEOMETRY_SHADER_OES)
4127 {
4128 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4129 qualifier.primitiveType = EptPoints;
4130 }
4131 else if (qualifierType == "lines" && isExtensionEnabled("GL_OES_geometry_shader") &&
4132 mShaderType == GL_GEOMETRY_SHADER_OES)
4133 {
4134 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4135 qualifier.primitiveType = EptLines;
4136 }
4137 else if (qualifierType == "lines_adjacency" && isExtensionEnabled("GL_OES_geometry_shader") &&
4138 mShaderType == GL_GEOMETRY_SHADER_OES)
4139 {
4140 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4141 qualifier.primitiveType = EptLinesAdjacency;
4142 }
4143 else if (qualifierType == "triangles" && isExtensionEnabled("GL_OES_geometry_shader") &&
4144 mShaderType == GL_GEOMETRY_SHADER_OES)
4145 {
4146 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4147 qualifier.primitiveType = EptTriangles;
4148 }
4149 else if (qualifierType == "triangles_adjacency" &&
4150 isExtensionEnabled("GL_OES_geometry_shader") && mShaderType == GL_GEOMETRY_SHADER_OES)
4151 {
4152 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4153 qualifier.primitiveType = EptTrianglesAdjacency;
4154 }
4155 else if (qualifierType == "line_strip" && isExtensionEnabled("GL_OES_geometry_shader") &&
4156 mShaderType == GL_GEOMETRY_SHADER_OES)
4157 {
4158 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4159 qualifier.primitiveType = EptLineStrip;
4160 }
4161 else if (qualifierType == "triangle_strip" && isExtensionEnabled("GL_OES_geometry_shader") &&
4162 mShaderType == GL_GEOMETRY_SHADER_OES)
4163 {
4164 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4165 qualifier.primitiveType = EptTriangleStrip;
4166 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004167
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004168 else
4169 {
4170 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004171 }
4172
Jamie Madilla5efff92013-06-06 11:56:47 -04004173 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004174}
4175
Martin Radev802abe02016-08-04 17:48:32 +03004176void TParseContext::parseLocalSize(const TString &qualifierType,
4177 const TSourceLoc &qualifierTypeLine,
4178 int intValue,
4179 const TSourceLoc &intValueLine,
4180 const std::string &intValueString,
4181 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004182 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004183{
Olli Etuaho856c4972016-08-08 11:38:39 +03004184 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004185 if (intValue < 1)
4186 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004187 std::stringstream reasonStream;
4188 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4189 std::string reason = reasonStream.str();
4190 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004191 }
4192 (*localSize)[index] = intValue;
4193}
4194
Olli Etuaho09b04a22016-12-15 13:30:26 +00004195void TParseContext::parseNumViews(int intValue,
4196 const TSourceLoc &intValueLine,
4197 const std::string &intValueString,
4198 int *numViews)
4199{
4200 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4201 // specification.
4202 if (intValue < 1)
4203 {
4204 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4205 }
4206 *numViews = intValue;
4207}
4208
Shaob5cc1192017-07-06 10:47:20 +08004209void TParseContext::parseInvocations(int intValue,
4210 const TSourceLoc &intValueLine,
4211 const std::string &intValueString,
4212 int *numInvocations)
4213{
4214 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4215 // it doesn't make sense to accept invocations <= 0.
4216 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4217 {
4218 error(intValueLine,
4219 "out of range: invocations must be in the range of [1, "
4220 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4221 intValueString.c_str());
4222 }
4223 else
4224 {
4225 *numInvocations = intValue;
4226 }
4227}
4228
4229void TParseContext::parseMaxVertices(int intValue,
4230 const TSourceLoc &intValueLine,
4231 const std::string &intValueString,
4232 int *maxVertices)
4233{
4234 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4235 // it doesn't make sense to accept max_vertices < 0.
4236 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4237 {
4238 error(
4239 intValueLine,
4240 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4241 intValueString.c_str());
4242 }
4243 else
4244 {
4245 *maxVertices = intValue;
4246 }
4247}
4248
Jamie Madillb98c3a82015-07-23 14:26:04 -04004249TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4250 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004251 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304252 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004253{
Martin Radev802abe02016-08-04 17:48:32 +03004254 TLayoutQualifier qualifier = TLayoutQualifier::create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004255
Martin Radev802abe02016-08-04 17:48:32 +03004256 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004257
Martin Radev802abe02016-08-04 17:48:32 +03004258 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004259 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004260 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004261 if (intValue < 0)
4262 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004263 error(intValueLine, "out of range: location must be non-negative",
4264 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004265 }
4266 else
4267 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004268 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004269 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004270 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004271 }
Olli Etuaho43364892017-02-13 16:00:12 +00004272 else if (qualifierType == "binding")
4273 {
4274 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4275 if (intValue < 0)
4276 {
4277 error(intValueLine, "out of range: binding must be non-negative",
4278 intValueString.c_str());
4279 }
4280 else
4281 {
4282 qualifier.binding = intValue;
4283 }
4284 }
jchen104cdac9e2017-05-08 11:01:20 +08004285 else if (qualifierType == "offset")
4286 {
4287 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4288 if (intValue < 0)
4289 {
4290 error(intValueLine, "out of range: offset must be non-negative",
4291 intValueString.c_str());
4292 }
4293 else
4294 {
4295 qualifier.offset = intValue;
4296 }
4297 }
Martin Radev802abe02016-08-04 17:48:32 +03004298 else if (qualifierType == "local_size_x")
4299 {
4300 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4301 &qualifier.localSize);
4302 }
4303 else if (qualifierType == "local_size_y")
4304 {
4305 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4306 &qualifier.localSize);
4307 }
4308 else if (qualifierType == "local_size_z")
4309 {
4310 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4311 &qualifier.localSize);
4312 }
Olli Etuaho95468d12017-05-04 11:14:34 +03004313 else if (qualifierType == "num_views" && isMultiviewExtensionEnabled() &&
Olli Etuaho09b04a22016-12-15 13:30:26 +00004314 mShaderType == GL_VERTEX_SHADER)
4315 {
4316 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4317 }
Shaob5cc1192017-07-06 10:47:20 +08004318 else if (qualifierType == "invocations" && isExtensionEnabled("GL_OES_geometry_shader") &&
4319 mShaderType == GL_GEOMETRY_SHADER_OES)
4320 {
4321 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4322 }
4323 else if (qualifierType == "max_vertices" && isExtensionEnabled("GL_OES_geometry_shader") &&
4324 mShaderType == GL_GEOMETRY_SHADER_OES)
4325 {
4326 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4327 }
4328
Martin Radev802abe02016-08-04 17:48:32 +03004329 else
4330 {
4331 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004332 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004333
Jamie Madilla5efff92013-06-06 11:56:47 -04004334 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004335}
4336
Olli Etuaho613b9592016-09-05 12:05:53 +03004337TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4338{
4339 return new TTypeQualifierBuilder(
4340 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4341 mShaderVersion);
4342}
4343
Olli Etuahocce89652017-06-19 16:04:09 +03004344TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4345 const TSourceLoc &loc)
4346{
4347 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4348 return new TStorageQualifierWrapper(qualifier, loc);
4349}
4350
4351TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4352{
4353 if (getShaderType() == GL_VERTEX_SHADER)
4354 {
4355 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4356 }
4357 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4358}
4359
4360TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4361{
4362 if (declaringFunction())
4363 {
4364 return new TStorageQualifierWrapper(EvqIn, loc);
4365 }
Shaob5cc1192017-07-06 10:47:20 +08004366
4367 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004368 {
Shaob5cc1192017-07-06 10:47:20 +08004369 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004370 {
Shaob5cc1192017-07-06 10:47:20 +08004371 if (mShaderVersion < 300 && !isMultiviewExtensionEnabled())
4372 {
4373 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4374 }
4375 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004376 }
Shaob5cc1192017-07-06 10:47:20 +08004377 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004378 {
Shaob5cc1192017-07-06 10:47:20 +08004379 if (mShaderVersion < 300)
4380 {
4381 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4382 }
4383 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004384 }
Shaob5cc1192017-07-06 10:47:20 +08004385 case GL_COMPUTE_SHADER:
4386 {
4387 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4388 }
4389 case GL_GEOMETRY_SHADER_OES:
4390 {
4391 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4392 }
4393 default:
4394 {
4395 UNREACHABLE();
4396 return new TStorageQualifierWrapper(EvqLast, loc);
4397 }
Olli Etuahocce89652017-06-19 16:04:09 +03004398 }
Olli Etuahocce89652017-06-19 16:04:09 +03004399}
4400
4401TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4402{
4403 if (declaringFunction())
4404 {
4405 return new TStorageQualifierWrapper(EvqOut, loc);
4406 }
Shaob5cc1192017-07-06 10:47:20 +08004407 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004408 {
Shaob5cc1192017-07-06 10:47:20 +08004409 case GL_VERTEX_SHADER:
4410 {
4411 if (mShaderVersion < 300)
4412 {
4413 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4414 }
4415 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4416 }
4417 case GL_FRAGMENT_SHADER:
4418 {
4419 if (mShaderVersion < 300)
4420 {
4421 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4422 }
4423 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4424 }
4425 case GL_COMPUTE_SHADER:
4426 {
4427 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4428 return new TStorageQualifierWrapper(EvqLast, loc);
4429 }
4430 case GL_GEOMETRY_SHADER_OES:
4431 {
4432 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4433 }
4434 default:
4435 {
4436 UNREACHABLE();
4437 return new TStorageQualifierWrapper(EvqLast, loc);
4438 }
Olli Etuahocce89652017-06-19 16:04:09 +03004439 }
Olli Etuahocce89652017-06-19 16:04:09 +03004440}
4441
4442TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4443{
4444 if (!declaringFunction())
4445 {
4446 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4447 }
4448 return new TStorageQualifierWrapper(EvqInOut, loc);
4449}
4450
Jamie Madillb98c3a82015-07-23 14:26:04 -04004451TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004452 TLayoutQualifier rightQualifier,
4453 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004454{
Martin Radevc28888b2016-07-22 15:27:42 +03004455 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004456 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004457}
4458
Olli Etuahocce89652017-06-19 16:04:09 +03004459TField *TParseContext::parseStructDeclarator(TString *identifier, const TSourceLoc &loc)
4460{
4461 checkIsNotReserved(loc, *identifier);
4462 TType *type = new TType(EbtVoid, EbpUndefined);
4463 return new TField(type, identifier, loc);
4464}
4465
4466TField *TParseContext::parseStructArrayDeclarator(TString *identifier,
4467 const TSourceLoc &loc,
4468 TIntermTyped *arraySize,
4469 const TSourceLoc &arraySizeLoc)
4470{
4471 checkIsNotReserved(loc, *identifier);
4472
4473 TType *type = new TType(EbtVoid, EbpUndefined);
4474 unsigned int size = checkIsValidArraySize(arraySizeLoc, arraySize);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004475 type->makeArray(size);
Olli Etuahocce89652017-06-19 16:04:09 +03004476
4477 return new TField(type, identifier, loc);
4478}
4479
Olli Etuaho4de340a2016-12-16 09:32:03 +00004480TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4481 const TFieldList *newlyAddedFields,
4482 const TSourceLoc &location)
4483{
4484 for (TField *field : *newlyAddedFields)
4485 {
4486 for (TField *oldField : *processedFields)
4487 {
4488 if (oldField->name() == field->name())
4489 {
4490 error(location, "duplicate field name in structure", field->name().c_str());
4491 }
4492 }
4493 processedFields->push_back(field);
4494 }
4495 return processedFields;
4496}
4497
Martin Radev70866b82016-07-22 15:27:42 +03004498TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4499 const TTypeQualifierBuilder &typeQualifierBuilder,
4500 TPublicType *typeSpecifier,
4501 TFieldList *fieldList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004502{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004503 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004504
Martin Radev70866b82016-07-22 15:27:42 +03004505 typeSpecifier->qualifier = typeQualifier.qualifier;
4506 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004507 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004508 typeSpecifier->invariant = typeQualifier.invariant;
4509 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304510 {
Martin Radev70866b82016-07-22 15:27:42 +03004511 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004512 }
Martin Radev70866b82016-07-22 15:27:42 +03004513 return addStructDeclaratorList(*typeSpecifier, fieldList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004514}
4515
Jamie Madillb98c3a82015-07-23 14:26:04 -04004516TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004517 TFieldList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004518{
Martin Radev4a9cd802016-09-01 16:51:51 +03004519 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4520 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004521
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004522 checkIsNonVoid(typeSpecifier.getLine(), (*declaratorList)[0]->name(),
4523 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004524
Martin Radev4a9cd802016-09-01 16:51:51 +03004525 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004526
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004527 for (unsigned int i = 0; i < declaratorList->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304528 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004529 auto declaratorArraySizes = (*declaratorList)[i]->type()->getArraySizes();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004530 // don't allow arrays of arrays
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004531 if (!declaratorArraySizes.empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304532 {
Olli Etuahoe0803872017-08-23 15:30:23 +03004533 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004534 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004535
4536 TType *type = (*declaratorList)[i]->type();
4537 *type = TType(typeSpecifier);
4538 for (unsigned int arraySize : declaratorArraySizes)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304539 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004540 type->makeArray(arraySize);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004541 }
4542
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004543 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *(*declaratorList)[i]);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004544 }
4545
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004546 return declaratorList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004547}
4548
Martin Radev4a9cd802016-09-01 16:51:51 +03004549TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4550 const TSourceLoc &nameLine,
4551 const TString *structName,
4552 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004553{
Olli Etuahoa5e693a2017-07-13 16:07:26 +03004554 TStructure *structure = new TStructure(&symbolTable, structName, fieldList);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004555
Jamie Madill9b820842015-02-12 10:40:10 -05004556 // Store a bool in the struct if we're at global scope, to allow us to
4557 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004558 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004559
Jamie Madill98493dd2013-07-08 14:39:03 -04004560 if (!structName->empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004561 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004562 checkIsNotReserved(nameLine, *structName);
Olli Etuaho0f684632017-07-13 12:42:15 +03004563 if (!symbolTable.declareStructType(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304564 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004565 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004566 }
4567 }
4568
4569 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004570 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004571 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004572 const TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004573 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004574 switch (qualifier)
4575 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004576 case EvqGlobal:
4577 case EvqTemporary:
4578 break;
4579 default:
4580 error(field.line(), "invalid qualifier on struct member",
4581 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004582 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004583 }
Martin Radev70866b82016-07-22 15:27:42 +03004584 if (field.type()->isInvariant())
4585 {
4586 error(field.line(), "invalid qualifier on struct member", "invariant");
4587 }
jchen104cdac9e2017-05-08 11:01:20 +08004588 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4589 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004590 {
4591 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4592 }
4593
Olli Etuaho43364892017-02-13 16:00:12 +00004594 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4595
4596 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004597
4598 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004599 }
4600
Martin Radev4a9cd802016-09-01 16:51:51 +03004601 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004602 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004603 exitStructDeclaration();
4604
Martin Radev4a9cd802016-09-01 16:51:51 +03004605 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004606}
4607
Jamie Madillb98c3a82015-07-23 14:26:04 -04004608TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004609 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004610 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004611{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004612 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004613 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004614 init->isVector())
4615 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004616 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4617 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004618 return nullptr;
4619 }
4620
Olli Etuahoac5274d2015-02-20 10:19:08 +02004621 if (statementList)
4622 {
Olli Etuaho77ba4082016-12-16 12:01:18 +00004623 if (!ValidateSwitchStatementList(switchType, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004624 {
Olli Etuahoac5274d2015-02-20 10:19:08 +02004625 return nullptr;
4626 }
4627 }
4628
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004629 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4630 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004631 return node;
4632}
4633
4634TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4635{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004636 if (mSwitchNestingLevel == 0)
4637 {
4638 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004639 return nullptr;
4640 }
4641 if (condition == nullptr)
4642 {
4643 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004644 return nullptr;
4645 }
4646 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004647 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004648 {
4649 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004650 }
4651 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004652 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4653 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4654 // fold in case labels.
4655 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004656 {
4657 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004658 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004659 TIntermCase *node = new TIntermCase(condition);
4660 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004661 return node;
4662}
4663
4664TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4665{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004666 if (mSwitchNestingLevel == 0)
4667 {
4668 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004669 return nullptr;
4670 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004671 TIntermCase *node = new TIntermCase(nullptr);
4672 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004673 return node;
4674}
4675
Jamie Madillb98c3a82015-07-23 14:26:04 -04004676TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4677 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004678 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004679{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004680 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004681
4682 switch (op)
4683 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004684 case EOpLogicalNot:
4685 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4686 child->isVector())
4687 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004688 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004689 return nullptr;
4690 }
4691 break;
4692 case EOpBitwiseNot:
4693 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4694 child->isMatrix() || child->isArray())
4695 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004696 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004697 return nullptr;
4698 }
4699 break;
4700 case EOpPostIncrement:
4701 case EOpPreIncrement:
4702 case EOpPostDecrement:
4703 case EOpPreDecrement:
4704 case EOpNegative:
4705 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004706 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4707 child->getBasicType() == EbtBool || child->isArray() ||
4708 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004709 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004710 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004711 return nullptr;
4712 }
4713 // Operators for built-ins are already type checked against their prototype.
4714 default:
4715 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004716 }
4717
Jiajia Qinbc585152017-06-23 15:42:17 +08004718 if (child->getMemoryQualifier().writeonly)
4719 {
4720 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4721 return nullptr;
4722 }
4723
Olli Etuahof119a262016-08-19 15:54:22 +03004724 TIntermUnary *node = new TIntermUnary(op, child);
4725 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004726
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004727 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004728}
4729
Olli Etuaho09b22472015-02-11 11:47:26 +02004730TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4731{
Olli Etuahocce89652017-06-19 16:04:09 +03004732 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004733 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004734 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004735 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004736 return child;
4737 }
4738 return node;
4739}
4740
Jamie Madillb98c3a82015-07-23 14:26:04 -04004741TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4742 TIntermTyped *child,
4743 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004744{
Olli Etuaho856c4972016-08-08 11:38:39 +03004745 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004746 return addUnaryMath(op, child, loc);
4747}
4748
Jamie Madillb98c3a82015-07-23 14:26:04 -04004749bool TParseContext::binaryOpCommonCheck(TOperator op,
4750 TIntermTyped *left,
4751 TIntermTyped *right,
4752 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004753{
jchen10b4cf5652017-05-05 18:51:17 +08004754 // Check opaque types are not allowed to be operands in expressions other than array indexing
4755 // and structure member selection.
4756 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4757 {
4758 switch (op)
4759 {
4760 case EOpIndexDirect:
4761 case EOpIndexIndirect:
4762 break;
4763 case EOpIndexDirectStruct:
4764 UNREACHABLE();
4765
4766 default:
4767 error(loc, "Invalid operation for variables with an opaque type",
4768 GetOperatorString(op));
4769 return false;
4770 }
4771 }
jchen10cc2a10e2017-05-03 14:05:12 +08004772
Jiajia Qinbc585152017-06-23 15:42:17 +08004773 if (right->getMemoryQualifier().writeonly)
4774 {
4775 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4776 return false;
4777 }
4778
4779 if (left->getMemoryQualifier().writeonly)
4780 {
4781 switch (op)
4782 {
4783 case EOpAssign:
4784 case EOpInitialize:
4785 case EOpIndexDirect:
4786 case EOpIndexIndirect:
4787 case EOpIndexDirectStruct:
4788 case EOpIndexDirectInterfaceBlock:
4789 break;
4790 default:
4791 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
4792 return false;
4793 }
4794 }
4795
Olli Etuaho244be012016-08-18 15:26:02 +03004796 if (left->getType().getStruct() || right->getType().getStruct())
4797 {
4798 switch (op)
4799 {
4800 case EOpIndexDirectStruct:
4801 ASSERT(left->getType().getStruct());
4802 break;
4803 case EOpEqual:
4804 case EOpNotEqual:
4805 case EOpAssign:
4806 case EOpInitialize:
4807 if (left->getType() != right->getType())
4808 {
4809 return false;
4810 }
4811 break;
4812 default:
4813 error(loc, "Invalid operation for structs", GetOperatorString(op));
4814 return false;
4815 }
4816 }
4817
Olli Etuaho94050052017-05-08 14:17:44 +03004818 if (left->isInterfaceBlock() || right->isInterfaceBlock())
4819 {
4820 switch (op)
4821 {
4822 case EOpIndexDirectInterfaceBlock:
4823 ASSERT(left->getType().getInterfaceBlock());
4824 break;
4825 default:
4826 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
4827 return false;
4828 }
4829 }
4830
Olli Etuahod6b14282015-03-17 14:31:35 +02004831 if (left->isArray() || right->isArray())
4832 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004833 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02004834 {
4835 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4836 return false;
4837 }
4838
4839 if (left->isArray() != right->isArray())
4840 {
4841 error(loc, "array / non-array mismatch", GetOperatorString(op));
4842 return false;
4843 }
4844
4845 switch (op)
4846 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004847 case EOpEqual:
4848 case EOpNotEqual:
4849 case EOpAssign:
4850 case EOpInitialize:
4851 break;
4852 default:
4853 error(loc, "Invalid operation for arrays", GetOperatorString(op));
4854 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02004855 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03004856 // At this point, size of implicitly sized arrays should be resolved.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004857 if (left->getType().getArraySizes() != right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02004858 {
4859 error(loc, "array size mismatch", GetOperatorString(op));
4860 return false;
4861 }
Olli Etuahod6b14282015-03-17 14:31:35 +02004862 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004863
4864 // Check ops which require integer / ivec parameters
4865 bool isBitShift = false;
4866 switch (op)
4867 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004868 case EOpBitShiftLeft:
4869 case EOpBitShiftRight:
4870 case EOpBitShiftLeftAssign:
4871 case EOpBitShiftRightAssign:
4872 // Unsigned can be bit-shifted by signed and vice versa, but we need to
4873 // check that the basic type is an integer type.
4874 isBitShift = true;
4875 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
4876 {
4877 return false;
4878 }
4879 break;
4880 case EOpBitwiseAnd:
4881 case EOpBitwiseXor:
4882 case EOpBitwiseOr:
4883 case EOpBitwiseAndAssign:
4884 case EOpBitwiseXorAssign:
4885 case EOpBitwiseOrAssign:
4886 // It is enough to check the type of only one operand, since later it
4887 // is checked that the operand types match.
4888 if (!IsInteger(left->getBasicType()))
4889 {
4890 return false;
4891 }
4892 break;
4893 default:
4894 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004895 }
4896
4897 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
4898 // So the basic type should usually match.
4899 if (!isBitShift && left->getBasicType() != right->getBasicType())
4900 {
4901 return false;
4902 }
4903
Olli Etuaho63e1ec52016-08-18 22:05:12 +03004904 // Check that:
4905 // 1. Type sizes match exactly on ops that require that.
4906 // 2. Restrictions for structs that contain arrays or samplers are respected.
4907 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04004908 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004909 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004910 case EOpAssign:
4911 case EOpInitialize:
4912 case EOpEqual:
4913 case EOpNotEqual:
4914 // ESSL 1.00 sections 5.7, 5.8, 5.9
4915 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
4916 {
4917 error(loc, "undefined operation for structs containing arrays",
4918 GetOperatorString(op));
4919 return false;
4920 }
4921 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
4922 // we interpret the spec so that this extends to structs containing samplers,
4923 // similarly to ESSL 1.00 spec.
4924 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
4925 left->getType().isStructureContainingSamplers())
4926 {
4927 error(loc, "undefined operation for structs containing samplers",
4928 GetOperatorString(op));
4929 return false;
4930 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004931
Olli Etuahoe1805592017-01-02 16:41:20 +00004932 if ((left->getNominalSize() != right->getNominalSize()) ||
4933 (left->getSecondarySize() != right->getSecondarySize()))
4934 {
4935 error(loc, "dimension mismatch", GetOperatorString(op));
4936 return false;
4937 }
4938 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004939 case EOpLessThan:
4940 case EOpGreaterThan:
4941 case EOpLessThanEqual:
4942 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00004943 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04004944 {
Olli Etuahoe1805592017-01-02 16:41:20 +00004945 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004946 return false;
4947 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03004948 break;
4949 case EOpAdd:
4950 case EOpSub:
4951 case EOpDiv:
4952 case EOpIMod:
4953 case EOpBitShiftLeft:
4954 case EOpBitShiftRight:
4955 case EOpBitwiseAnd:
4956 case EOpBitwiseXor:
4957 case EOpBitwiseOr:
4958 case EOpAddAssign:
4959 case EOpSubAssign:
4960 case EOpDivAssign:
4961 case EOpIModAssign:
4962 case EOpBitShiftLeftAssign:
4963 case EOpBitShiftRightAssign:
4964 case EOpBitwiseAndAssign:
4965 case EOpBitwiseXorAssign:
4966 case EOpBitwiseOrAssign:
4967 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
4968 {
4969 return false;
4970 }
4971
4972 // Are the sizes compatible?
4973 if (left->getNominalSize() != right->getNominalSize() ||
4974 left->getSecondarySize() != right->getSecondarySize())
4975 {
4976 // If the nominal sizes of operands do not match:
4977 // One of them must be a scalar.
4978 if (!left->isScalar() && !right->isScalar())
4979 return false;
4980
4981 // In the case of compound assignment other than multiply-assign,
4982 // the right side needs to be a scalar. Otherwise a vector/matrix
4983 // would be assigned to a scalar. A scalar can't be shifted by a
4984 // vector either.
4985 if (!right->isScalar() &&
4986 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
4987 return false;
4988 }
4989 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04004990 default:
4991 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02004992 }
4993
Olli Etuahod6b14282015-03-17 14:31:35 +02004994 return true;
4995}
4996
Olli Etuaho1dded802016-08-18 18:13:13 +03004997bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
4998 const TType &left,
4999 const TType &right)
5000{
5001 switch (op)
5002 {
5003 case EOpMul:
5004 case EOpMulAssign:
5005 return left.getNominalSize() == right.getNominalSize() &&
5006 left.getSecondarySize() == right.getSecondarySize();
5007 case EOpVectorTimesScalar:
5008 return true;
5009 case EOpVectorTimesScalarAssign:
5010 ASSERT(!left.isMatrix() && !right.isMatrix());
5011 return left.isVector() && !right.isVector();
5012 case EOpVectorTimesMatrix:
5013 return left.getNominalSize() == right.getRows();
5014 case EOpVectorTimesMatrixAssign:
5015 ASSERT(!left.isMatrix() && right.isMatrix());
5016 return left.isVector() && left.getNominalSize() == right.getRows() &&
5017 left.getNominalSize() == right.getCols();
5018 case EOpMatrixTimesVector:
5019 return left.getCols() == right.getNominalSize();
5020 case EOpMatrixTimesScalar:
5021 return true;
5022 case EOpMatrixTimesScalarAssign:
5023 ASSERT(left.isMatrix() && !right.isMatrix());
5024 return !right.isVector();
5025 case EOpMatrixTimesMatrix:
5026 return left.getCols() == right.getRows();
5027 case EOpMatrixTimesMatrixAssign:
5028 ASSERT(left.isMatrix() && right.isMatrix());
5029 // We need to check two things:
5030 // 1. The matrix multiplication step is valid.
5031 // 2. The result will have the same number of columns as the lvalue.
5032 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5033
5034 default:
5035 UNREACHABLE();
5036 return false;
5037 }
5038}
5039
Jamie Madillb98c3a82015-07-23 14:26:04 -04005040TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5041 TIntermTyped *left,
5042 TIntermTyped *right,
5043 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005044{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005045 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005046 return nullptr;
5047
Olli Etuahofc1806e2015-03-17 13:03:11 +02005048 switch (op)
5049 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005050 case EOpEqual:
5051 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005052 case EOpLessThan:
5053 case EOpGreaterThan:
5054 case EOpLessThanEqual:
5055 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005056 break;
5057 case EOpLogicalOr:
5058 case EOpLogicalXor:
5059 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005060 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5061 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005062 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005063 {
5064 return nullptr;
5065 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005066 // Basic types matching should have been already checked.
5067 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005068 break;
5069 case EOpAdd:
5070 case EOpSub:
5071 case EOpDiv:
5072 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005073 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5074 !right->getType().getStruct());
5075 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005076 {
5077 return nullptr;
5078 }
5079 break;
5080 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005081 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5082 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005083 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005084 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005085 {
5086 return nullptr;
5087 }
5088 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005089 default:
5090 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005091 }
5092
Olli Etuaho1dded802016-08-18 18:13:13 +03005093 if (op == EOpMul)
5094 {
5095 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5096 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5097 {
5098 return nullptr;
5099 }
5100 }
5101
Olli Etuaho3fdec912016-08-18 15:08:06 +03005102 TIntermBinary *node = new TIntermBinary(op, left, right);
5103 node->setLine(loc);
5104
Olli Etuaho3fdec912016-08-18 15:08:06 +03005105 // See if we can fold constants.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005106 return node->fold(mDiagnostics);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005107}
5108
Jamie Madillb98c3a82015-07-23 14:26:04 -04005109TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5110 TIntermTyped *left,
5111 TIntermTyped *right,
5112 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005113{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005114 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005115 if (node == 0)
5116 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005117 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5118 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005119 return left;
5120 }
5121 return node;
5122}
5123
Jamie Madillb98c3a82015-07-23 14:26:04 -04005124TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5125 TIntermTyped *left,
5126 TIntermTyped *right,
5127 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005128{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005129 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005130 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005131 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005132 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5133 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005134 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005135 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005136 }
5137 return node;
5138}
5139
Olli Etuaho13389b62016-10-16 11:48:18 +01005140TIntermBinary *TParseContext::createAssign(TOperator op,
5141 TIntermTyped *left,
5142 TIntermTyped *right,
5143 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005144{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005145 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005146 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005147 if (op == EOpMulAssign)
5148 {
5149 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5150 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5151 {
5152 return nullptr;
5153 }
5154 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005155 TIntermBinary *node = new TIntermBinary(op, left, right);
5156 node->setLine(loc);
5157
Olli Etuaho3fdec912016-08-18 15:08:06 +03005158 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005159 }
5160 return nullptr;
5161}
5162
Jamie Madillb98c3a82015-07-23 14:26:04 -04005163TIntermTyped *TParseContext::addAssign(TOperator op,
5164 TIntermTyped *left,
5165 TIntermTyped *right,
5166 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005167{
Olli Etuahocce89652017-06-19 16:04:09 +03005168 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005169 TIntermTyped *node = createAssign(op, left, right, loc);
5170 if (node == nullptr)
5171 {
5172 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005173 return left;
5174 }
5175 return node;
5176}
5177
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005178TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5179 TIntermTyped *right,
5180 const TSourceLoc &loc)
5181{
Corentin Wallez0d959252016-07-12 17:26:32 -04005182 // WebGL2 section 5.26, the following results in an error:
5183 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005184 if (mShaderSpec == SH_WEBGL2_SPEC &&
5185 (left->isArray() || left->getBasicType() == EbtVoid ||
5186 left->getType().isStructureContainingArrays() || right->isArray() ||
5187 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005188 {
5189 error(loc,
5190 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5191 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005192 }
5193
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005194 TIntermBinary *commaNode = new TIntermBinary(EOpComma, left, right);
5195 TQualifier resultQualifier = TIntermBinary::GetCommaQualifier(mShaderVersion, left, right);
5196 commaNode->getTypePointer()->setQualifier(resultQualifier);
5197 return commaNode->fold(mDiagnostics);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005198}
5199
Olli Etuaho49300862015-02-20 14:54:49 +02005200TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5201{
5202 switch (op)
5203 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005204 case EOpContinue:
5205 if (mLoopNestingLevel <= 0)
5206 {
5207 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005208 }
5209 break;
5210 case EOpBreak:
5211 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5212 {
5213 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005214 }
5215 break;
5216 case EOpReturn:
5217 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5218 {
5219 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005220 }
5221 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005222 case EOpKill:
5223 if (mShaderType != GL_FRAGMENT_SHADER)
5224 {
5225 error(loc, "discard supported in fragment shaders only", "discard");
5226 }
5227 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005228 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005229 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005230 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005231 }
Olli Etuahocce89652017-06-19 16:04:09 +03005232 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005233}
5234
Jamie Madillb98c3a82015-07-23 14:26:04 -04005235TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005236 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005237 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005238{
Olli Etuahocce89652017-06-19 16:04:09 +03005239 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005240 {
Olli Etuahocce89652017-06-19 16:04:09 +03005241 ASSERT(op == EOpReturn);
5242 mFunctionReturnsValue = true;
5243 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5244 {
5245 error(loc, "void function cannot return a value", "return");
5246 }
5247 else if (*mCurrentFunctionType != expression->getType())
5248 {
5249 error(loc, "function return is not matching type:", "return");
5250 }
Olli Etuaho49300862015-02-20 14:54:49 +02005251 }
Olli Etuahocce89652017-06-19 16:04:09 +03005252 TIntermBranch *node = new TIntermBranch(op, expression);
5253 node->setLine(loc);
5254 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005255}
5256
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005257void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5258{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005259 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobd674552016-10-06 13:28:42 +01005260 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005261 TIntermNode *offset = nullptr;
5262 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuahoec9232b2017-03-27 17:01:37 +03005263 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
5264 name == "textureProjLodOffset" || name == "textureGradOffset" ||
5265 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005266 {
5267 offset = arguments->back();
5268 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03005269 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005270 {
5271 // A bias parameter might follow the offset parameter.
5272 ASSERT(arguments->size() >= 3);
5273 offset = (*arguments)[2];
5274 }
5275 if (offset != nullptr)
5276 {
5277 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5278 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5279 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005280 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03005281 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005282 }
5283 else
5284 {
5285 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5286 size_t size = offsetConstantUnion->getType().getObjectSize();
5287 const TConstantUnion *values = offsetConstantUnion->getUnionArrayPointer();
5288 for (size_t i = 0u; i < size; ++i)
5289 {
5290 int offsetValue = values[i].getIConst();
5291 if (offsetValue > mMaxProgramTexelOffset || offsetValue < mMinProgramTexelOffset)
5292 {
5293 std::stringstream tokenStream;
5294 tokenStream << offsetValue;
5295 std::string token = tokenStream.str();
5296 error(offset->getLine(), "Texture offset value out of valid range",
5297 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005298 }
5299 }
5300 }
5301 }
5302}
5303
Martin Radev2cc85b32016-08-05 16:22:53 +03005304// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5305void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5306{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005307 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Martin Radev2cc85b32016-08-05 16:22:53 +03005308 const TString &name = functionCall->getFunctionSymbolInfo()->getName();
5309
5310 if (name.compare(0, 5, "image") == 0)
5311 {
5312 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005313 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005314
Olli Etuaho485eefd2017-02-14 17:40:06 +00005315 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005316
5317 if (name.compare(5, 5, "Store") == 0)
5318 {
5319 if (memoryQualifier.readonly)
5320 {
5321 error(imageNode->getLine(),
5322 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005323 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005324 }
5325 }
5326 else if (name.compare(5, 4, "Load") == 0)
5327 {
5328 if (memoryQualifier.writeonly)
5329 {
5330 error(imageNode->getLine(),
5331 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005332 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005333 }
5334 }
5335 }
5336}
5337
5338// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5339void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5340 const TFunction *functionDefinition,
5341 const TIntermAggregate *functionCall)
5342{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005343 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005344
5345 const TIntermSequence &arguments = *functionCall->getSequence();
5346
5347 ASSERT(functionDefinition->getParamCount() == arguments.size());
5348
5349 for (size_t i = 0; i < arguments.size(); ++i)
5350 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005351 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5352 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005353 const TType &functionParameterType = *functionDefinition->getParam(i).type;
5354 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5355
5356 if (IsImage(functionArgumentType.getBasicType()))
5357 {
5358 const TMemoryQualifier &functionArgumentMemoryQualifier =
5359 functionArgumentType.getMemoryQualifier();
5360 const TMemoryQualifier &functionParameterMemoryQualifier =
5361 functionParameterType.getMemoryQualifier();
5362 if (functionArgumentMemoryQualifier.readonly &&
5363 !functionParameterMemoryQualifier.readonly)
5364 {
5365 error(functionCall->getLine(),
5366 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005367 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005368 }
5369
5370 if (functionArgumentMemoryQualifier.writeonly &&
5371 !functionParameterMemoryQualifier.writeonly)
5372 {
5373 error(functionCall->getLine(),
5374 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005375 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005376 }
Martin Radev049edfa2016-11-11 14:35:37 +02005377
5378 if (functionArgumentMemoryQualifier.coherent &&
5379 !functionParameterMemoryQualifier.coherent)
5380 {
5381 error(functionCall->getLine(),
5382 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005383 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005384 }
5385
5386 if (functionArgumentMemoryQualifier.volatileQualifier &&
5387 !functionParameterMemoryQualifier.volatileQualifier)
5388 {
5389 error(functionCall->getLine(),
5390 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005391 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005392 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005393 }
5394 }
5395}
5396
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005397TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005398{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005399 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00005400}
5401
5402TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005403 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00005404 TIntermNode *thisNode,
5405 const TSourceLoc &loc)
5406{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005407 if (thisNode != nullptr)
5408 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005409 return addMethod(fnCall, arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005410 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005411
5412 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005413 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005414 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005415 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005416 }
5417 else
5418 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005419 ASSERT(op == EOpNull);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005420 return addNonConstructorFunctionCall(fnCall, arguments, loc);
5421 }
5422}
5423
5424TIntermTyped *TParseContext::addMethod(TFunction *fnCall,
5425 TIntermSequence *arguments,
5426 TIntermNode *thisNode,
5427 const TSourceLoc &loc)
5428{
5429 TConstantUnion *unionArray = new TConstantUnion[1];
5430 int arraySize = 0;
5431 TIntermTyped *typedThis = thisNode->getAsTyped();
5432 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5433 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5434 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
5435 // So accessing fnCall->getName() below is safe.
5436 if (fnCall->getName() != "length")
5437 {
5438 error(loc, "invalid method", fnCall->getName().c_str());
5439 }
5440 else if (!arguments->empty())
5441 {
5442 error(loc, "method takes no parameters", "length");
5443 }
5444 else if (typedThis == nullptr || !typedThis->isArray())
5445 {
5446 error(loc, "length can only be called on arrays", "length");
5447 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08005448 else if (typedThis->getQualifier() == EvqPerVertexIn &&
5449 mGeometryShaderInputPrimitiveType == EptUndefined)
5450 {
5451 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5452 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005453 else
5454 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03005455 arraySize = typedThis->getOutermostArraySize();
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005456 if (typedThis->getAsSymbolNode() == nullptr)
Olli Etuaho72d10202017-01-19 15:58:30 +00005457 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005458 // This code path can be hit with expressions like these:
5459 // (a = b).length()
5460 // (func()).length()
5461 // (int[3](0, 1, 2)).length()
5462 // ESSL 3.00 section 5.9 defines expressions so that this is not actually a valid
5463 // expression.
5464 // It allows "An array name with the length method applied" in contrast to GLSL 4.4
5465 // spec section 5.9 which allows "An array, vector or matrix expression with the
5466 // length method applied".
5467 error(loc, "length can only be called on array names, not on array expressions",
5468 "length");
Olli Etuaho72d10202017-01-19 15:58:30 +00005469 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005470 }
5471 unionArray->setIConst(arraySize);
Olli Etuaho56229f12017-07-10 14:16:33 +03005472 TIntermConstantUnion *node =
5473 new TIntermConstantUnion(unionArray, TType(EbtInt, EbpUndefined, EvqConst));
5474 node->setLine(loc);
5475 return node;
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005476}
5477
5478TIntermTyped *TParseContext::addNonConstructorFunctionCall(TFunction *fnCall,
5479 TIntermSequence *arguments,
5480 const TSourceLoc &loc)
5481{
5482 // First find by unmangled name to check whether the function name has been
5483 // hidden by a variable name or struct typename.
5484 // If a function is found, check for one with a matching argument list.
5485 bool builtIn;
5486 const TSymbol *symbol = symbolTable.find(fnCall->getName(), mShaderVersion, &builtIn);
5487 if (symbol != nullptr && !symbol->isFunction())
5488 {
5489 error(loc, "function name expected", fnCall->getName().c_str());
5490 }
5491 else
5492 {
5493 symbol = symbolTable.find(TFunction::GetMangledNameFromCall(fnCall->getName(), *arguments),
5494 mShaderVersion, &builtIn);
5495 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005496 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005497 error(loc, "no matching overloaded function found", fnCall->getName().c_str());
5498 }
5499 else
5500 {
5501 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005502 //
5503 // A declared function.
5504 //
Olli Etuaho383b7912016-08-05 11:22:59 +03005505 if (builtIn && !fnCandidate->getExtension().empty())
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005506 {
Olli Etuaho856c4972016-08-08 11:38:39 +03005507 checkCanUseExtension(loc, fnCandidate->getExtension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005508 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005509 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005510 if (builtIn && op != EOpNull)
5511 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005512 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005513 if (fnCandidate->getParamCount() == 1)
5514 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005515 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005516 TIntermNode *unaryParamNode = arguments->front();
5517 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005518 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005519 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005520 }
5521 else
5522 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005523 TIntermAggregate *callNode =
Olli Etuahofe486322017-03-21 09:30:54 +00005524 TIntermAggregate::Create(fnCandidate->getReturnType(), op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005525 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005526
5527 // Some built-in functions have out parameters too.
Jiajia Qinbc585152017-06-23 15:42:17 +08005528 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05305529
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005530 if (TIntermAggregate::CanFoldAggregateBuiltInOp(callNode->getOp()))
Arun Patole274f0702015-05-05 13:33:30 +05305531 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005532 // See if we can constant fold a built-in. Note that this may be possible
5533 // even if it is not const-qualified.
5534 return callNode->fold(mDiagnostics);
Arun Patole274f0702015-05-05 13:33:30 +05305535 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005536 else
5537 {
5538 return callNode;
5539 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005540 }
5541 }
5542 else
5543 {
5544 // This is a real function call
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005545 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005546
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005547 // If builtIn == false, the function is user defined - could be an overloaded
5548 // built-in as well.
5549 // if builtIn == true, it's a builtIn function with no op associated with it.
5550 // This needs to happen after the function info including name is set.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005551 if (builtIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005552 {
Olli Etuahofe486322017-03-21 09:30:54 +00005553 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005554 checkTextureOffsetConst(callNode);
5555 checkImageMemoryAccessForBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03005556 }
5557 else
5558 {
Olli Etuahofe486322017-03-21 09:30:54 +00005559 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005560 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005561 }
5562
Jiajia Qinbc585152017-06-23 15:42:17 +08005563 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005564
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005565 callNode->setLine(loc);
5566
5567 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005568 }
5569 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005570 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005571
5572 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005573 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005574}
5575
Jamie Madillb98c3a82015-07-23 14:26:04 -04005576TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005577 TIntermTyped *trueExpression,
5578 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005579 const TSourceLoc &loc)
5580{
Olli Etuaho56229f12017-07-10 14:16:33 +03005581 if (!checkIsScalarBool(loc, cond))
5582 {
5583 return falseExpression;
5584 }
Olli Etuaho52901742015-04-15 13:42:45 +03005585
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005586 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005587 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005588 std::stringstream reasonStream;
5589 reasonStream << "mismatching ternary operator operand types '"
5590 << trueExpression->getCompleteString() << " and '"
5591 << falseExpression->getCompleteString() << "'";
5592 std::string reason = reasonStream.str();
5593 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005594 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005595 }
Olli Etuahode318b22016-10-25 16:18:25 +01005596 if (IsOpaqueType(trueExpression->getBasicType()))
5597 {
5598 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005599 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005600 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5601 // Note that structs containing opaque types don't need to be checked as structs are
5602 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005603 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005604 return falseExpression;
5605 }
5606
Jiajia Qinbc585152017-06-23 15:42:17 +08005607 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5608 falseExpression->getMemoryQualifier().writeonly)
5609 {
5610 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5611 return falseExpression;
5612 }
5613
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005614 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005615 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005616 // ESSL 3.00.6 section 5.7:
5617 // Ternary operator support is optional for arrays. No certainty that it works across all
5618 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5619 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005620 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005621 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005622 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005623 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005624 }
Olli Etuaho94050052017-05-08 14:17:44 +03005625 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5626 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005627 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005628 return falseExpression;
5629 }
5630
Corentin Wallez0d959252016-07-12 17:26:32 -04005631 // WebGL2 section 5.26, the following results in an error:
5632 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005633 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005634 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005635 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005636 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005637 }
5638
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005639 // Note that the node resulting from here can be a constant union without being qualified as
5640 // constant.
5641 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5642 node->setLine(loc);
5643
5644 return node->fold();
Olli Etuaho52901742015-04-15 13:42:45 +03005645}
Olli Etuaho49300862015-02-20 14:54:49 +02005646
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005647//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005648// Parse an array of strings using yyparse.
5649//
5650// Returns 0 for success.
5651//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005652int PaParseStrings(size_t count,
5653 const char *const string[],
5654 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305655 TParseContext *context)
5656{
Yunchao He4f285442017-04-21 12:15:49 +08005657 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005658 return 1;
5659
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005660 if (glslang_initialize(context))
5661 return 1;
5662
alokp@chromium.org408c45e2012-04-05 15:54:43 +00005663 int error = glslang_scan(count, string, length, context);
5664 if (!error)
5665 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005666
alokp@chromium.org73bc2982012-06-19 18:48:05 +00005667 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00005668
alokp@chromium.org6b495712012-06-29 00:06:58 +00005669 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005670}
Jamie Madill45bcc782016-11-07 13:58:48 -05005671
5672} // namespace sh