blob: c7c4c77521fb8e981e3b465e0a15cf28690ee9e0 [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"
Olli Etuahod5f44c92017-11-29 17:15:40 +020014#include "compiler/translator/Declarator.h"
Olli Etuaho3ec75682017-07-05 17:02:55 +030015#include "compiler/translator/IntermNode_util.h"
Kai Ninomiya614dd0f2017-11-22 14:04:48 -080016#include "compiler/translator/StaticType.h"
Olli Etuahob0c645e2015-05-12 14:25:36 +030017#include "compiler/translator/ValidateGlobalInitializer.h"
jchen104cdac9e2017-05-08 11:01:20 +080018#include "compiler/translator/ValidateSwitch.h"
19#include "compiler/translator/glslang.h"
Olli Etuaho37ad4742015-04-27 13:18:50 +030020#include "compiler/translator/util.h"
daniel@transgaming.combbf56f72010-04-20 18:52:13 +000021
Jamie Madill45bcc782016-11-07 13:58:48 -050022namespace sh
23{
24
alokp@chromium.org8b851c62012-06-15 16:25:11 +000025///////////////////////////////////////////////////////////////////////
26//
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +000027// Sub- vector and matrix fields
28//
29////////////////////////////////////////////////////////////////////////
30
Martin Radev2cc85b32016-08-05 16:22:53 +030031namespace
32{
33
34const int kWebGLMaxStructNesting = 4;
35
Jiajia Qina3106c52017-11-03 09:39:39 +080036const std::array<const char *, 8> kAtomicBuiltin = {{"atomicAdd", "atomicMin", "atomicMax",
37 "atomicAnd", "atomicOr", "atomicXor",
38 "atomicExchange", "atomicCompSwap"}};
39
40bool IsAtomicBuiltin(const TString &name)
41{
42 for (size_t i = 0; i < kAtomicBuiltin.size(); ++i)
43 {
44 if (name.compare(kAtomicBuiltin[i]) == 0)
45 {
46 return true;
47 }
48 }
49 return false;
50}
51
Olli Etuaho0f684632017-07-13 12:42:15 +030052bool ContainsSampler(const TStructure *structType);
53
Martin Radev2cc85b32016-08-05 16:22:53 +030054bool ContainsSampler(const TType &type)
55{
56 if (IsSampler(type.getBasicType()))
Olli Etuaho0f684632017-07-13 12:42:15 +030057 {
Martin Radev2cc85b32016-08-05 16:22:53 +030058 return true;
Olli Etuaho0f684632017-07-13 12:42:15 +030059 }
jchen10cc2a10e2017-05-03 14:05:12 +080060 if (type.getBasicType() == EbtStruct)
Martin Radev2cc85b32016-08-05 16:22:53 +030061 {
Olli Etuaho0f684632017-07-13 12:42:15 +030062 return ContainsSampler(type.getStruct());
Martin Radev2cc85b32016-08-05 16:22:53 +030063 }
64
65 return false;
66}
67
Olli Etuaho0f684632017-07-13 12:42:15 +030068bool ContainsSampler(const TStructure *structType)
69{
70 for (const auto &field : structType->fields())
71 {
72 if (ContainsSampler(*field->type()))
73 return true;
74 }
75 return false;
76}
77
Olli Etuaho485eefd2017-02-14 17:40:06 +000078// Get a token from an image argument to use as an error message token.
79const char *GetImageArgumentToken(TIntermTyped *imageNode)
80{
81 ASSERT(IsImage(imageNode->getBasicType()));
82 while (imageNode->getAsBinaryNode() &&
83 (imageNode->getAsBinaryNode()->getOp() == EOpIndexIndirect ||
84 imageNode->getAsBinaryNode()->getOp() == EOpIndexDirect))
85 {
86 imageNode = imageNode->getAsBinaryNode()->getLeft();
87 }
88 TIntermSymbol *imageSymbol = imageNode->getAsSymbolNode();
89 if (imageSymbol)
90 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +020091 return imageSymbol->getName().c_str();
Olli Etuaho485eefd2017-02-14 17:40:06 +000092 }
93 return "image";
94}
95
Olli Etuahocce89652017-06-19 16:04:09 +030096bool CanSetDefaultPrecisionOnType(const TPublicType &type)
97{
98 if (!SupportsPrecision(type.getBasicType()))
99 {
100 return false;
101 }
102 if (type.getBasicType() == EbtUInt)
103 {
104 // ESSL 3.00.4 section 4.5.4
105 return false;
106 }
107 if (type.isAggregate())
108 {
109 // Not allowed to set for aggregate types
110 return false;
111 }
112 return true;
113}
114
Jiawei Shaod8105a02017-08-08 09:54:36 +0800115// Map input primitive types to input array sizes in a geometry shader.
116GLuint GetGeometryShaderInputArraySize(TLayoutPrimitiveType primitiveType)
117{
118 switch (primitiveType)
119 {
120 case EptPoints:
121 return 1u;
122 case EptLines:
123 return 2u;
124 case EptTriangles:
125 return 3u;
126 case EptLinesAdjacency:
127 return 4u;
128 case EptTrianglesAdjacency:
129 return 6u;
130 default:
131 UNREACHABLE();
132 return 0u;
133 }
134}
135
Jiajia Qina3106c52017-11-03 09:39:39 +0800136bool IsBufferOrSharedVariable(TIntermTyped *var)
137{
138 if (var->isInterfaceBlock() || var->getQualifier() == EvqBuffer ||
139 var->getQualifier() == EvqShared)
140 {
141 return true;
142 }
143 return false;
144}
145
Martin Radev2cc85b32016-08-05 16:22:53 +0300146} // namespace
147
jchen104cdac9e2017-05-08 11:01:20 +0800148// This tracks each binding point's current default offset for inheritance of subsequent
149// variables using the same binding, and keeps offsets unique and non overlapping.
150// See GLSL ES 3.1, section 4.4.6.
151class TParseContext::AtomicCounterBindingState
152{
153 public:
154 AtomicCounterBindingState() : mDefaultOffset(0) {}
155 // Inserts a new span and returns -1 if overlapping, else returns the starting offset of
156 // newly inserted span.
157 int insertSpan(int start, size_t length)
158 {
159 gl::RangeI newSpan(start, start + static_cast<int>(length));
160 for (const auto &span : mSpans)
161 {
162 if (newSpan.intersects(span))
163 {
164 return -1;
165 }
166 }
167 mSpans.push_back(newSpan);
168 mDefaultOffset = newSpan.high();
169 return start;
170 }
171 // Inserts a new span starting from the default offset.
172 int appendSpan(size_t length) { return insertSpan(mDefaultOffset, length); }
173 void setDefaultOffset(int offset) { mDefaultOffset = offset; }
174
175 private:
176 int mDefaultOffset;
177 std::vector<gl::RangeI> mSpans;
178};
179
Jamie Madillacb4b812016-11-07 13:50:29 -0500180TParseContext::TParseContext(TSymbolTable &symt,
181 TExtensionBehavior &ext,
182 sh::GLenum type,
183 ShShaderSpec spec,
184 ShCompileOptions options,
185 bool checksPrecErrors,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000186 TDiagnostics *diagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500187 const ShBuiltInResources &resources)
Olli Etuaho56229f12017-07-10 14:16:33 +0300188 : symbolTable(symt),
Olli Etuahobb7e5a72017-04-24 10:16:44 +0300189 mDeferredNonEmptyDeclarationErrorCheck(false),
Jamie Madillacb4b812016-11-07 13:50:29 -0500190 mShaderType(type),
191 mShaderSpec(spec),
192 mCompileOptions(options),
193 mShaderVersion(100),
194 mTreeRoot(nullptr),
195 mLoopNestingLevel(0),
196 mStructNestingLevel(0),
197 mSwitchNestingLevel(0),
198 mCurrentFunctionType(nullptr),
199 mFunctionReturnsValue(false),
200 mChecksPrecisionErrors(checksPrecErrors),
201 mFragmentPrecisionHighOnESSL1(false),
Jiajia Qinbc585152017-06-23 15:42:17 +0800202 mDefaultUniformMatrixPacking(EmpColumnMajor),
203 mDefaultUniformBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
204 mDefaultBufferMatrixPacking(EmpColumnMajor),
205 mDefaultBufferBlockStorage(sh::IsWebGLBasedSpec(spec) ? EbsStd140 : EbsShared),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000206 mDiagnostics(diagnostics),
Jamie Madillacb4b812016-11-07 13:50:29 -0500207 mDirectiveHandler(ext,
Olli Etuaho77ba4082016-12-16 12:01:18 +0000208 *mDiagnostics,
Jamie Madillacb4b812016-11-07 13:50:29 -0500209 mShaderVersion,
210 mShaderType,
211 resources.WEBGL_debug_shader_precision == 1),
Olli Etuaho77ba4082016-12-16 12:01:18 +0000212 mPreprocessor(mDiagnostics, &mDirectiveHandler, pp::PreprocessorSettings()),
Jamie Madillacb4b812016-11-07 13:50:29 -0500213 mScanner(nullptr),
214 mUsesFragData(false),
215 mUsesFragColor(false),
216 mUsesSecondaryOutputs(false),
217 mMinProgramTexelOffset(resources.MinProgramTexelOffset),
218 mMaxProgramTexelOffset(resources.MaxProgramTexelOffset),
Martin Radev84aa2dc2017-09-11 15:51:02 +0300219 mMinProgramTextureGatherOffset(resources.MinProgramTextureGatherOffset),
220 mMaxProgramTextureGatherOffset(resources.MaxProgramTextureGatherOffset),
Jamie Madillacb4b812016-11-07 13:50:29 -0500221 mComputeShaderLocalSizeDeclared(false),
Jamie Madill2f294c92017-11-20 14:47:26 -0500222 mComputeShaderLocalSize(-1),
Olli Etuaho09b04a22016-12-15 13:30:26 +0000223 mNumViews(-1),
224 mMaxNumViews(resources.MaxViewsOVR),
Olli Etuaho43364892017-02-13 16:00:12 +0000225 mMaxImageUnits(resources.MaxImageUnits),
226 mMaxCombinedTextureImageUnits(resources.MaxCombinedTextureImageUnits),
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000227 mMaxUniformLocations(resources.MaxUniformLocations),
jchen10af713a22017-04-19 09:10:56 +0800228 mMaxUniformBufferBindings(resources.MaxUniformBufferBindings),
jchen104cdac9e2017-05-08 11:01:20 +0800229 mMaxAtomicCounterBindings(resources.MaxAtomicCounterBindings),
Jiajia Qinbc585152017-06-23 15:42:17 +0800230 mMaxShaderStorageBufferBindings(resources.MaxShaderStorageBufferBindings),
Shaob5cc1192017-07-06 10:47:20 +0800231 mDeclaringFunction(false),
232 mGeometryShaderInputPrimitiveType(EptUndefined),
233 mGeometryShaderOutputPrimitiveType(EptUndefined),
234 mGeometryShaderInvocations(0),
235 mGeometryShaderMaxVertices(-1),
236 mMaxGeometryShaderInvocations(resources.MaxGeometryShaderInvocations),
Jiawei Shaod8105a02017-08-08 09:54:36 +0800237 mMaxGeometryShaderMaxVertices(resources.MaxGeometryOutputVertices),
Olli Etuahoc74ec1a2018-01-09 15:23:28 +0200238 mGlInVariableWithArraySize(nullptr)
Jamie Madillacb4b812016-11-07 13:50:29 -0500239{
Jamie Madillacb4b812016-11-07 13:50:29 -0500240}
241
jchen104cdac9e2017-05-08 11:01:20 +0800242TParseContext::~TParseContext()
243{
244}
245
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300246bool TParseContext::parseVectorFields(const TSourceLoc &line,
247 const TString &compString,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400248 int vecSize,
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300249 TVector<int> *fieldOffsets)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000250{
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300251 ASSERT(fieldOffsets);
252 size_t fieldCount = compString.size();
253 if (fieldCount > 4u)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530254 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000255 error(line, "illegal vector field selection", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000256 return false;
257 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300258 fieldOffsets->resize(fieldCount);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000259
Jamie Madillb98c3a82015-07-23 14:26:04 -0400260 enum
261 {
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000262 exyzw,
263 ergba,
daniel@transgaming.comb3077d02013-01-11 04:12:09 +0000264 estpq
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000265 } fieldSet[4];
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000266
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300267 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530268 {
269 switch (compString[i])
270 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400271 case 'x':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300272 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400273 fieldSet[i] = exyzw;
274 break;
275 case 'r':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300276 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400277 fieldSet[i] = ergba;
278 break;
279 case 's':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300280 (*fieldOffsets)[i] = 0;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400281 fieldSet[i] = estpq;
282 break;
283 case 'y':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300284 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400285 fieldSet[i] = exyzw;
286 break;
287 case 'g':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300288 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400289 fieldSet[i] = ergba;
290 break;
291 case 't':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300292 (*fieldOffsets)[i] = 1;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400293 fieldSet[i] = estpq;
294 break;
295 case 'z':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300296 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400297 fieldSet[i] = exyzw;
298 break;
299 case 'b':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300300 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400301 fieldSet[i] = ergba;
302 break;
303 case 'p':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300304 (*fieldOffsets)[i] = 2;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400305 fieldSet[i] = estpq;
306 break;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530307
Jamie Madillb98c3a82015-07-23 14:26:04 -0400308 case 'w':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300309 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400310 fieldSet[i] = exyzw;
311 break;
312 case 'a':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300313 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400314 fieldSet[i] = ergba;
315 break;
316 case 'q':
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300317 (*fieldOffsets)[i] = 3;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400318 fieldSet[i] = estpq;
319 break;
320 default:
321 error(line, "illegal vector field selection", compString.c_str());
322 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000323 }
324 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000325
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300326 for (unsigned int i = 0u; i < fieldOffsets->size(); ++i)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530327 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +0300328 if ((*fieldOffsets)[i] >= vecSize)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530329 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400330 error(line, "vector field selection out of range", compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000331 return false;
332 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000333
Arun Patole7e7e68d2015-05-22 12:02:25 +0530334 if (i > 0)
335 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400336 if (fieldSet[i] != fieldSet[i - 1])
Arun Patole7e7e68d2015-05-22 12:02:25 +0530337 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400338 error(line, "illegal - vector component fields not from the same set",
339 compString.c_str());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000340 return false;
341 }
342 }
343 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000344
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000345 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000346}
347
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000348///////////////////////////////////////////////////////////////////////
349//
350// Errors
351//
352////////////////////////////////////////////////////////////////////////
353
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000354//
355// Used by flex/bison to output all syntax and parsing errors.
356//
Olli Etuaho4de340a2016-12-16 09:32:03 +0000357void TParseContext::error(const TSourceLoc &loc, const char *reason, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000358{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000359 mDiagnostics->error(loc, reason, token);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000360}
361
Olli Etuaho4de340a2016-12-16 09:32:03 +0000362void TParseContext::warning(const TSourceLoc &loc, const char *reason, const char *token)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530363{
Olli Etuaho77ba4082016-12-16 12:01:18 +0000364 mDiagnostics->warning(loc, reason, token);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +0000365}
366
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200367void TParseContext::outOfRangeError(bool isError,
368 const TSourceLoc &loc,
369 const char *reason,
Olli Etuaho4de340a2016-12-16 09:32:03 +0000370 const char *token)
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200371{
372 if (isError)
373 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000374 error(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200375 }
376 else
377 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000378 warning(loc, reason, token);
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200379 }
380}
381
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000382//
383// Same error message for all places assignments don't work.
384//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530385void TParseContext::assignError(const TSourceLoc &line, const char *op, TString left, TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000386{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000387 std::stringstream reasonStream;
388 reasonStream << "cannot convert from '" << right << "' to '" << left << "'";
389 std::string reason = reasonStream.str();
390 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000391}
392
393//
394// Same error message for all places unary operations don't work.
395//
Arun Patole7e7e68d2015-05-22 12:02:25 +0530396void TParseContext::unaryOpError(const TSourceLoc &line, const char *op, TString operand)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000397{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000398 std::stringstream reasonStream;
399 reasonStream << "wrong operand type - no operation '" << op
400 << "' exists that takes an operand of type " << operand
401 << " (or there is no acceptable conversion)";
402 std::string reason = reasonStream.str();
403 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000404}
405
406//
407// Same error message for all binary operations don't work.
408//
Jamie Madillb98c3a82015-07-23 14:26:04 -0400409void TParseContext::binaryOpError(const TSourceLoc &line,
410 const char *op,
411 TString left,
412 TString right)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000413{
Olli Etuaho4de340a2016-12-16 09:32:03 +0000414 std::stringstream reasonStream;
415 reasonStream << "wrong operand types - no operation '" << op
416 << "' exists that takes a left-hand operand of type '" << left
417 << "' and a right operand of type '" << right
418 << "' (or there is no acceptable conversion)";
419 std::string reason = reasonStream.str();
420 error(line, reason.c_str(), op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000421}
422
Olli Etuaho856c4972016-08-08 11:38:39 +0300423void TParseContext::checkPrecisionSpecified(const TSourceLoc &line,
424 TPrecision precision,
425 TBasicType type)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530426{
Jamie Madill6e06b1f2015-05-14 10:01:17 -0400427 if (!mChecksPrecisionErrors)
Olli Etuaho383b7912016-08-05 11:22:59 +0300428 return;
Martin Radev70866b82016-07-22 15:27:42 +0300429
430 if (precision != EbpUndefined && !SupportsPrecision(type))
431 {
432 error(line, "illegal type for precision qualifier", getBasicString(type));
433 }
434
Olli Etuaho183d7e22015-11-20 15:59:09 +0200435 if (precision == EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530436 {
Olli Etuaho183d7e22015-11-20 15:59:09 +0200437 switch (type)
438 {
439 case EbtFloat:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400440 error(line, "No precision specified for (float)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300441 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200442 case EbtInt:
443 case EbtUInt:
444 UNREACHABLE(); // there's always a predeclared qualifier
Jamie Madillb98c3a82015-07-23 14:26:04 -0400445 error(line, "No precision specified (int)", "");
Olli Etuaho383b7912016-08-05 11:22:59 +0300446 return;
Olli Etuaho183d7e22015-11-20 15:59:09 +0200447 default:
jchen10cc2a10e2017-05-03 14:05:12 +0800448 if (IsOpaqueType(type))
Olli Etuaho183d7e22015-11-20 15:59:09 +0200449 {
jchen10cc2a10e2017-05-03 14:05:12 +0800450 error(line, "No precision specified", getBasicString(type));
Martin Radev2cc85b32016-08-05 16:22:53 +0300451 return;
452 }
Olli Etuaho183d7e22015-11-20 15:59:09 +0200453 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000454 }
daniel@transgaming.coma5d76232010-05-17 09:58:47 +0000455}
456
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000457// Both test and if necessary, spit out an error, to see if the node is really
458// an l-value that can be operated on this way.
Olli Etuaho856c4972016-08-08 11:38:39 +0300459bool TParseContext::checkCanBeLValue(const TSourceLoc &line, const char *op, TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000460{
Jamie Madilld7b1ab52016-12-12 14:42:19 -0500461 TIntermSymbol *symNode = node->getAsSymbolNode();
462 TIntermBinary *binaryNode = node->getAsBinaryNode();
Olli Etuahob6fa0432016-09-28 16:28:05 +0100463 TIntermSwizzle *swizzleNode = node->getAsSwizzleNode();
464
465 if (swizzleNode)
466 {
467 bool ok = checkCanBeLValue(line, op, swizzleNode->getOperand());
468 if (ok && swizzleNode->hasDuplicateOffsets())
469 {
470 error(line, " l-value of swizzle cannot have duplicate components", op);
471 return false;
472 }
473 return ok;
474 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000475
Arun Patole7e7e68d2015-05-22 12:02:25 +0530476 if (binaryNode)
477 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400478 switch (binaryNode->getOp())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530479 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400480 case EOpIndexDirect:
481 case EOpIndexIndirect:
482 case EOpIndexDirectStruct:
483 case EOpIndexDirectInterfaceBlock:
Olli Etuaho856c4972016-08-08 11:38:39 +0300484 return checkCanBeLValue(line, op, binaryNode->getLeft());
Jamie Madillb98c3a82015-07-23 14:26:04 -0400485 default:
486 break;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000487 }
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000488 error(line, " l-value required", op);
Olli Etuaho8a176262016-08-16 14:23:01 +0300489 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000490 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000491
jchen10cc2a10e2017-05-03 14:05:12 +0800492 std::string message;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530493 switch (node->getQualifier())
494 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400495 case EvqConst:
496 message = "can't modify a const";
497 break;
498 case EvqConstReadOnly:
499 message = "can't modify a const";
500 break;
501 case EvqAttribute:
502 message = "can't modify an attribute";
503 break;
504 case EvqFragmentIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400505 case EvqVertexIn:
Jiawei Shao8e4b3552017-08-30 14:20:58 +0800506 case EvqGeometryIn:
Jiawei Shaoe8ef2bc2017-08-29 13:38:57 +0800507 case EvqFlatIn:
508 case EvqSmoothIn:
509 case EvqCentroidIn:
Jamie Madillb98c3a82015-07-23 14:26:04 -0400510 message = "can't modify an input";
511 break;
512 case EvqUniform:
513 message = "can't modify a uniform";
514 break;
515 case EvqVaryingIn:
516 message = "can't modify a varying";
517 break;
518 case EvqFragCoord:
519 message = "can't modify gl_FragCoord";
520 break;
521 case EvqFrontFacing:
522 message = "can't modify gl_FrontFacing";
523 break;
524 case EvqPointCoord:
525 message = "can't modify gl_PointCoord";
526 break;
Martin Radevb0883602016-08-04 17:48:58 +0300527 case EvqNumWorkGroups:
528 message = "can't modify gl_NumWorkGroups";
529 break;
530 case EvqWorkGroupSize:
531 message = "can't modify gl_WorkGroupSize";
532 break;
533 case EvqWorkGroupID:
534 message = "can't modify gl_WorkGroupID";
535 break;
536 case EvqLocalInvocationID:
537 message = "can't modify gl_LocalInvocationID";
538 break;
539 case EvqGlobalInvocationID:
540 message = "can't modify gl_GlobalInvocationID";
541 break;
542 case EvqLocalInvocationIndex:
543 message = "can't modify gl_LocalInvocationIndex";
544 break;
Olli Etuaho7142f6c2017-05-05 17:07:26 +0300545 case EvqViewIDOVR:
546 message = "can't modify gl_ViewID_OVR";
547 break;
Martin Radev802abe02016-08-04 17:48:32 +0300548 case EvqComputeIn:
549 message = "can't modify work group size variable";
550 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +0800551 case EvqPerVertexIn:
552 message = "can't modify any member in gl_in";
553 break;
Jiawei Shaod27f5c82017-08-23 09:38:08 +0800554 case EvqPrimitiveIDIn:
555 message = "can't modify gl_PrimitiveIDIn";
556 break;
557 case EvqInvocationID:
558 message = "can't modify gl_InvocationID";
559 break;
560 case EvqPrimitiveID:
561 if (mShaderType == GL_FRAGMENT_SHADER)
562 {
563 message = "can't modify gl_PrimitiveID in a fragment shader";
564 }
565 break;
566 case EvqLayer:
567 if (mShaderType == GL_FRAGMENT_SHADER)
568 {
569 message = "can't modify gl_Layer in a fragment shader";
570 }
571 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -0400572 default:
573 //
574 // Type that can't be written to?
575 //
576 if (node->getBasicType() == EbtVoid)
577 {
578 message = "can't modify void";
579 }
jchen10cc2a10e2017-05-03 14:05:12 +0800580 if (IsOpaqueType(node->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -0400581 {
jchen10cc2a10e2017-05-03 14:05:12 +0800582 message = "can't modify a variable with type ";
583 message += getBasicString(node->getBasicType());
Martin Radev2cc85b32016-08-05 16:22:53 +0300584 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800585 else if (node->getMemoryQualifier().readonly)
586 {
587 message = "can't modify a readonly variable";
588 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000589 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000590
jchen10cc2a10e2017-05-03 14:05:12 +0800591 if (message.empty() && binaryNode == 0 && symNode == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530592 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000593 error(line, "l-value required", op);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000594
Olli Etuaho8a176262016-08-16 14:23:01 +0300595 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000596 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000597
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000598 //
599 // Everything else is okay, no error.
600 //
jchen10cc2a10e2017-05-03 14:05:12 +0800601 if (message.empty())
Olli Etuaho8a176262016-08-16 14:23:01 +0300602 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000603
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000604 //
605 // If we get here, we have an error and a message.
606 //
Arun Patole7e7e68d2015-05-22 12:02:25 +0530607 if (symNode)
608 {
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +0200609 // Symbol inside an expression can't be nameless.
610 ASSERT(symNode->variable().symbolType() != SymbolType::Empty);
611
612 const char *symbol = symNode->getName().c_str();
Olli Etuaho4de340a2016-12-16 09:32:03 +0000613 std::stringstream reasonStream;
614 reasonStream << "l-value required (" << message << " \"" << symbol << "\")";
615 std::string reason = reasonStream.str();
616 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000617 }
Arun Patole7e7e68d2015-05-22 12:02:25 +0530618 else
619 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000620 std::stringstream reasonStream;
621 reasonStream << "l-value required (" << message << ")";
622 std::string reason = reasonStream.str();
623 error(line, reason.c_str(), op);
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000624 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000625
Olli Etuaho8a176262016-08-16 14:23:01 +0300626 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000627}
628
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000629// Both test, and if necessary spit out an error, to see if the node is really
630// a constant.
Olli Etuaho856c4972016-08-08 11:38:39 +0300631void TParseContext::checkIsConst(TIntermTyped *node)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000632{
Olli Etuaho383b7912016-08-05 11:22:59 +0300633 if (node->getQualifier() != EvqConst)
634 {
635 error(node->getLine(), "constant expression required", "");
636 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000637}
638
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000639// Both test, and if necessary spit out an error, to see if the node is really
640// an integer.
Olli Etuaho856c4972016-08-08 11:38:39 +0300641void TParseContext::checkIsScalarInteger(TIntermTyped *node, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000642{
Olli Etuaho383b7912016-08-05 11:22:59 +0300643 if (!node->isScalarInt())
644 {
645 error(node->getLine(), "integer expression required", token);
646 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000647}
648
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000649// Both test, and if necessary spit out an error, to see if we are currently
650// globally scoped.
Qiankun Miaof69682b2016-08-16 14:50:42 +0800651bool TParseContext::checkIsAtGlobalLevel(const TSourceLoc &line, const char *token)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000652{
Olli Etuaho856c4972016-08-08 11:38:39 +0300653 if (!symbolTable.atGlobalLevel())
Olli Etuaho383b7912016-08-05 11:22:59 +0300654 {
655 error(line, "only allowed at global scope", token);
Qiankun Miaof69682b2016-08-16 14:50:42 +0800656 return false;
Olli Etuaho383b7912016-08-05 11:22:59 +0300657 }
Qiankun Miaof69682b2016-08-16 14:50:42 +0800658 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000659}
660
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300661// ESSL 3.00.5 sections 3.8 and 3.9.
662// If it starts "gl_" or contains two consecutive underscores, it's reserved.
663// Also checks for "webgl_" and "_webgl_" reserved identifiers if parsing a webgl shader.
Olli Etuaho856c4972016-08-08 11:38:39 +0300664bool TParseContext::checkIsNotReserved(const TSourceLoc &line, const TString &identifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000665{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530666 static const char *reservedErrMsg = "reserved built-in name";
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300667 if (identifier.compare(0, 3, "gl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530668 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300669 error(line, reservedErrMsg, "gl_");
670 return false;
671 }
672 if (sh::IsWebGLBasedSpec(mShaderSpec))
673 {
674 if (identifier.compare(0, 6, "webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530675 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300676 error(line, reservedErrMsg, "webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300677 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000678 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300679 if (identifier.compare(0, 7, "_webgl_") == 0)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530680 {
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300681 error(line, reservedErrMsg, "_webgl_");
Olli Etuaho8a176262016-08-16 14:23:01 +0300682 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000683 }
684 }
Olli Etuahod7cd4ae2017-07-06 15:52:49 +0300685 if (identifier.find("__") != TString::npos)
686 {
687 error(line,
688 "identifiers containing two consecutive underscores (__) are reserved as "
689 "possible future keywords",
690 identifier.c_str());
691 return false;
692 }
Olli Etuaho8a176262016-08-16 14:23:01 +0300693 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000694}
695
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300696// Make sure the argument types are correct for constructing a specific type.
Olli Etuaho856c4972016-08-08 11:38:39 +0300697bool TParseContext::checkConstructorArguments(const TSourceLoc &line,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800698 const TIntermSequence *arguments,
Olli Etuaho856c4972016-08-08 11:38:39 +0300699 const TType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000700{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800701 if (arguments->empty())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530702 {
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200703 error(line, "constructor does not have any arguments", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300704 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000705 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200706
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300707 for (TIntermNode *arg : *arguments)
Arun Patole7e7e68d2015-05-22 12:02:25 +0530708 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300709 const TIntermTyped *argTyped = arg->getAsTyped();
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200710 ASSERT(argTyped != nullptr);
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300711 if (type.getBasicType() != EbtStruct && IsOpaqueType(argTyped->getBasicType()))
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200712 {
jchen10cc2a10e2017-05-03 14:05:12 +0800713 std::string reason("cannot convert a variable with type ");
714 reason += getBasicString(argTyped->getBasicType());
715 error(line, reason.c_str(), "constructor");
Martin Radev2cc85b32016-08-05 16:22:53 +0300716 return false;
717 }
Jiajia Qinbc585152017-06-23 15:42:17 +0800718 else if (argTyped->getMemoryQualifier().writeonly)
719 {
720 error(line, "cannot convert a variable with writeonly", "constructor");
721 return false;
722 }
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200723 if (argTyped->getBasicType() == EbtVoid)
724 {
725 error(line, "cannot convert a void", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300726 return false;
Olli Etuaho15c2ac32015-11-09 15:51:43 +0200727 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000728 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000729
Olli Etuaho856c4972016-08-08 11:38:39 +0300730 if (type.isArray())
731 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300732 // The size of an unsized constructor should already have been determined.
733 ASSERT(!type.isUnsizedArray());
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300734 if (static_cast<size_t>(type.getOutermostArraySize()) != arguments->size())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300735 {
736 error(line, "array constructor needs one argument per array element", "constructor");
737 return false;
738 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300739 // GLSL ES 3.00 section 5.4.4: Each argument must be the same type as the element type of
740 // the array.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800741 for (TIntermNode *const &argNode : *arguments)
Olli Etuaho856c4972016-08-08 11:38:39 +0300742 {
743 const TType &argType = argNode->getAsTyped()->getType();
Olli Etuaho7881cfd2017-08-23 18:00:21 +0300744 if (mShaderVersion < 310 && argType.isArray())
Jamie Madill34bf2d92017-02-06 13:40:59 -0500745 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300746 error(line, "constructing from a non-dereferenced array", "constructor");
Jamie Madill34bf2d92017-02-06 13:40:59 -0500747 return false;
748 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +0300749 if (!argType.isElementTypeOf(type))
Olli Etuaho856c4972016-08-08 11:38:39 +0300750 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000751 error(line, "Array constructor argument has an incorrect type", "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300752 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300753 }
754 }
755 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300756 else if (type.getBasicType() == EbtStruct)
Olli Etuaho856c4972016-08-08 11:38:39 +0300757 {
758 const TFieldList &fields = type.getStruct()->fields();
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300759 if (fields.size() != arguments->size())
760 {
761 error(line,
762 "Number of constructor parameters does not match the number of structure fields",
763 "constructor");
764 return false;
765 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300766
767 for (size_t i = 0; i < fields.size(); i++)
768 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -0800769 if (i >= arguments->size() ||
770 (*arguments)[i]->getAsTyped()->getType() != *fields[i]->type())
Olli Etuaho856c4972016-08-08 11:38:39 +0300771 {
772 error(line, "Structure constructor arguments do not match structure fields",
Olli Etuaho4de340a2016-12-16 09:32:03 +0000773 "constructor");
Olli Etuaho8a176262016-08-16 14:23:01 +0300774 return false;
Olli Etuaho856c4972016-08-08 11:38:39 +0300775 }
776 }
777 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300778 else
779 {
780 // We're constructing a scalar, vector, or matrix.
781
782 // Note: It's okay to have too many components available, but not okay to have unused
783 // arguments. 'full' will go to true when enough args have been seen. If we loop again,
784 // there is an extra argument, so 'overFull' will become true.
785
786 size_t size = 0;
787 bool full = false;
788 bool overFull = false;
789 bool matrixArg = false;
790 for (TIntermNode *arg : *arguments)
791 {
792 const TIntermTyped *argTyped = arg->getAsTyped();
793 ASSERT(argTyped != nullptr);
794
Olli Etuaho487b63a2017-05-23 15:55:09 +0300795 if (argTyped->getBasicType() == EbtStruct)
796 {
797 error(line, "a struct cannot be used as a constructor argument for this type",
798 "constructor");
799 return false;
800 }
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300801 if (argTyped->getType().isArray())
802 {
803 error(line, "constructing from a non-dereferenced array", "constructor");
804 return false;
805 }
806 if (argTyped->getType().isMatrix())
807 {
808 matrixArg = true;
809 }
810
811 size += argTyped->getType().getObjectSize();
812 if (full)
813 {
814 overFull = true;
815 }
Olli Etuaho487b63a2017-05-23 15:55:09 +0300816 if (size >= type.getObjectSize())
Olli Etuahoa7ecec32017-05-08 17:43:55 +0300817 {
818 full = true;
819 }
820 }
821
822 if (type.isMatrix() && matrixArg)
823 {
824 if (arguments->size() != 1)
825 {
826 error(line, "constructing matrix from matrix can only take one argument",
827 "constructor");
828 return false;
829 }
830 }
831 else
832 {
833 if (size != 1 && size < type.getObjectSize())
834 {
835 error(line, "not enough data provided for construction", "constructor");
836 return false;
837 }
838 if (overFull)
839 {
840 error(line, "too many arguments", "constructor");
841 return false;
842 }
843 }
844 }
Olli Etuaho856c4972016-08-08 11:38:39 +0300845
Olli Etuaho8a176262016-08-16 14:23:01 +0300846 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000847}
848
Jamie Madillb98c3a82015-07-23 14:26:04 -0400849// This function checks to see if a void variable has been declared and raise an error message for
850// such a case
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000851//
852// returns true in case of an error
853//
Olli Etuaho856c4972016-08-08 11:38:39 +0300854bool TParseContext::checkIsNonVoid(const TSourceLoc &line,
Jamie Madillb98c3a82015-07-23 14:26:04 -0400855 const TString &identifier,
856 const TBasicType &type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000857{
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300858 if (type == EbtVoid)
859 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000860 error(line, "illegal use of type 'void'", identifier.c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +0300861 return false;
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +0300862 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000863
Olli Etuaho8a176262016-08-16 14:23:01 +0300864 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000865}
866
Jamie Madillb98c3a82015-07-23 14:26:04 -0400867// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300868// or not.
Olli Etuaho56229f12017-07-10 14:16:33 +0300869bool TParseContext::checkIsScalarBool(const TSourceLoc &line, const TIntermTyped *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000870{
Olli Etuaho37d96cc2017-07-11 14:14:03 +0300871 if (type->getBasicType() != EbtBool || !type->isScalar())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530872 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000873 error(line, "boolean expression expected", "");
Olli Etuaho56229f12017-07-10 14:16:33 +0300874 return false;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530875 }
Olli Etuaho56229f12017-07-10 14:16:33 +0300876 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000877}
878
Jamie Madillb98c3a82015-07-23 14:26:04 -0400879// This function checks to see if the node (for the expression) contains a scalar boolean expression
Olli Etuaho383b7912016-08-05 11:22:59 +0300880// or not.
Olli Etuaho856c4972016-08-08 11:38:39 +0300881void TParseContext::checkIsScalarBool(const TSourceLoc &line, const TPublicType &pType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000882{
Martin Radev4a9cd802016-09-01 16:51:51 +0300883 if (pType.getBasicType() != EbtBool || pType.isAggregate())
Arun Patole7e7e68d2015-05-22 12:02:25 +0530884 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000885 error(line, "boolean expression expected", "");
Arun Patole7e7e68d2015-05-22 12:02:25 +0530886 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000887}
888
jchen10cc2a10e2017-05-03 14:05:12 +0800889bool TParseContext::checkIsNotOpaqueType(const TSourceLoc &line,
890 const TTypeSpecifierNonArray &pType,
891 const char *reason)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000892{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530893 if (pType.type == EbtStruct)
894 {
Olli Etuaho0f684632017-07-13 12:42:15 +0300895 if (ContainsSampler(pType.userDef))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530896 {
Olli Etuaho4de340a2016-12-16 09:32:03 +0000897 std::stringstream reasonStream;
898 reasonStream << reason << " (structure contains a sampler)";
899 std::string reasonStr = reasonStream.str();
900 error(line, reasonStr.c_str(), getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300901 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000902 }
jchen10cc2a10e2017-05-03 14:05:12 +0800903 // only samplers need to be checked from structs, since other opaque types can't be struct
904 // members.
Olli Etuaho8a176262016-08-16 14:23:01 +0300905 return true;
Arun Patole7e7e68d2015-05-22 12:02:25 +0530906 }
jchen10cc2a10e2017-05-03 14:05:12 +0800907 else if (IsOpaqueType(pType.type))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530908 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000909 error(line, reason, getBasicString(pType.type));
Olli Etuaho8a176262016-08-16 14:23:01 +0300910 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000911 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000912
Olli Etuaho8a176262016-08-16 14:23:01 +0300913 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000914}
915
Olli Etuaho856c4972016-08-08 11:38:39 +0300916void TParseContext::checkDeclaratorLocationIsNotSpecified(const TSourceLoc &line,
917 const TPublicType &pType)
Jamie Madill0bd18df2013-06-20 11:55:52 -0400918{
919 if (pType.layoutQualifier.location != -1)
920 {
Jamie Madillb98c3a82015-07-23 14:26:04 -0400921 error(line, "location must only be specified for a single input or output variable",
922 "location");
Jamie Madill0bd18df2013-06-20 11:55:52 -0400923 }
Jamie Madill0bd18df2013-06-20 11:55:52 -0400924}
925
Olli Etuaho856c4972016-08-08 11:38:39 +0300926void TParseContext::checkLocationIsNotSpecified(const TSourceLoc &location,
927 const TLayoutQualifier &layoutQualifier)
928{
929 if (layoutQualifier.location != -1)
930 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000931 const char *errorMsg = "invalid layout qualifier: only valid on program inputs and outputs";
932 if (mShaderVersion >= 310)
933 {
934 errorMsg =
Jiawei Shao4cc89e22017-08-31 14:25:54 +0800935 "invalid layout qualifier: only valid on shader inputs, outputs, and uniforms";
Olli Etuaho6ca2b652017-02-19 18:05:10 +0000936 }
937 error(location, errorMsg, "location");
Olli Etuaho856c4972016-08-08 11:38:39 +0300938 }
939}
940
Qin Jiajiaca68d982017-09-18 16:41:56 +0800941void TParseContext::checkStd430IsForShaderStorageBlock(const TSourceLoc &location,
942 const TLayoutBlockStorage &blockStorage,
943 const TQualifier &qualifier)
944{
945 if (blockStorage == EbsStd430 && qualifier != EvqBuffer)
946 {
947 error(location, "The std430 layout is supported only for shader storage blocks.", "std430");
948 }
949}
950
Martin Radev2cc85b32016-08-05 16:22:53 +0300951void TParseContext::checkOutParameterIsNotOpaqueType(const TSourceLoc &line,
952 TQualifier qualifier,
953 const TType &type)
954{
Martin Radev2cc85b32016-08-05 16:22:53 +0300955 ASSERT(qualifier == EvqOut || qualifier == EvqInOut);
jchen10cc2a10e2017-05-03 14:05:12 +0800956 if (IsOpaqueType(type.getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +0530957 {
jchen10cc2a10e2017-05-03 14:05:12 +0800958 error(line, "opaque types cannot be output parameters", type.getBasicString());
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000959 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000960}
961
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000962// Do size checking for an array type's size.
Olli Etuaho856c4972016-08-08 11:38:39 +0300963unsigned int TParseContext::checkIsValidArraySize(const TSourceLoc &line, TIntermTyped *expr)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000964{
Arun Patole7e7e68d2015-05-22 12:02:25 +0530965 TIntermConstantUnion *constant = expr->getAsConstantUnion();
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000966
Olli Etuaho7c3848e2015-11-04 13:19:17 +0200967 // TODO(oetuaho@nvidia.com): Get rid of the constant == nullptr check here once all constant
968 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
969 // fold as array size.
970 if (expr->getQualifier() != EvqConst || constant == nullptr || !constant->isScalarInt())
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000971 {
maxvujovic@gmail.comc6b3b3c2012-06-27 22:49:39 +0000972 error(line, "array size must be a constant integer expression", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300973 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +0000974 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +0000975
Olli Etuaho856c4972016-08-08 11:38:39 +0300976 unsigned int size = 0u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400977
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000978 if (constant->getBasicType() == EbtUInt)
979 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300980 size = constant->getUConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000981 }
982 else
983 {
Olli Etuaho856c4972016-08-08 11:38:39 +0300984 int signedSize = constant->getIConst(0);
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000985
Olli Etuaho856c4972016-08-08 11:38:39 +0300986 if (signedSize < 0)
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000987 {
Nicolas Capens906744a2014-06-06 15:18:07 -0400988 error(line, "array size must be non-negative", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300989 return 1u;
shannonwoods@chromium.org6b709912013-05-30 00:20:04 +0000990 }
Nicolas Capens906744a2014-06-06 15:18:07 -0400991
Olli Etuaho856c4972016-08-08 11:38:39 +0300992 size = static_cast<unsigned int>(signedSize);
Nicolas Capens906744a2014-06-06 15:18:07 -0400993 }
994
Olli Etuaho856c4972016-08-08 11:38:39 +0300995 if (size == 0u)
Nicolas Capens906744a2014-06-06 15:18:07 -0400996 {
997 error(line, "array size must be greater than zero", "");
Olli Etuaho856c4972016-08-08 11:38:39 +0300998 return 1u;
Nicolas Capens906744a2014-06-06 15:18:07 -0400999 }
1000
1001 // The size of arrays is restricted here to prevent issues further down the
1002 // compiler/translator/driver stack. Shader Model 5 generation hardware is limited to
1003 // 4096 registers so this should be reasonable even for aggressively optimizable code.
1004 const unsigned int sizeLimit = 65536;
1005
Olli Etuaho856c4972016-08-08 11:38:39 +03001006 if (size > sizeLimit)
Nicolas Capens906744a2014-06-06 15:18:07 -04001007 {
1008 error(line, "array size too large", "");
Olli Etuaho856c4972016-08-08 11:38:39 +03001009 return 1u;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001010 }
Olli Etuaho856c4972016-08-08 11:38:39 +03001011
1012 return size;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001013}
1014
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001015// See if this qualifier can be an array.
Olli Etuaho8a176262016-08-16 14:23:01 +03001016bool TParseContext::checkIsValidQualifierForArray(const TSourceLoc &line,
1017 const TPublicType &elementQualifier)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001018{
Olli Etuaho8a176262016-08-16 14:23:01 +03001019 if ((elementQualifier.qualifier == EvqAttribute) ||
1020 (elementQualifier.qualifier == EvqVertexIn) ||
1021 (elementQualifier.qualifier == EvqConst && mShaderVersion < 300))
Olli Etuaho3739d232015-04-08 12:23:44 +03001022 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001023 error(line, "cannot declare arrays of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001024 TType(elementQualifier).getQualifierString());
1025 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001026 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001027
Olli Etuaho8a176262016-08-16 14:23:01 +03001028 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001029}
1030
Olli Etuaho8a176262016-08-16 14:23:01 +03001031// See if this element type can be formed into an array.
Olli Etuahoe0803872017-08-23 15:30:23 +03001032bool TParseContext::checkArrayElementIsNotArray(const TSourceLoc &line,
1033 const TPublicType &elementType)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001034{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03001035 if (mShaderVersion < 310 && elementType.isArray())
Jamie Madill06145232015-05-13 13:10:01 -04001036 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001037 error(line, "cannot declare arrays of arrays",
1038 TType(elementType).getCompleteString().c_str());
1039 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001040 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001041 return true;
1042}
1043
1044// Check if this qualified element type can be formed into an array. This is only called when array
1045// brackets are associated with an identifier in a declaration, like this:
1046// float a[2];
1047// Similar checks are done in addFullySpecifiedType for array declarations where the array brackets
1048// are associated with the type, like this:
1049// float[2] a;
1050bool TParseContext::checkIsValidTypeAndQualifierForArray(const TSourceLoc &indexLocation,
1051 const TPublicType &elementType)
1052{
1053 if (!checkArrayElementIsNotArray(indexLocation, elementType))
1054 {
1055 return false;
1056 }
Olli Etuahocc36b982015-07-10 14:14:18 +03001057 // In ESSL1.00 shaders, structs cannot be varying (section 4.3.5). This is checked elsewhere.
1058 // In ESSL3.00 shaders, struct inputs/outputs are allowed but not arrays of structs (section
1059 // 4.3.4).
Jiawei Shao492b5f52017-12-13 09:39:27 +08001060 // Geometry shader requires each user-defined input be declared as arrays or inside input
1061 // blocks declared as arrays (GL_EXT_geometry_shader section 11.1gs.4.3). For the purposes of
1062 // interface matching, such variables and blocks are treated as though they were not declared
1063 // as arrays (GL_EXT_geometry_shader section 7.4.1).
Martin Radev4a9cd802016-09-01 16:51:51 +03001064 if (mShaderVersion >= 300 && elementType.getBasicType() == EbtStruct &&
Jiawei Shao492b5f52017-12-13 09:39:27 +08001065 sh::IsVarying(elementType.qualifier) &&
1066 !IsGeometryShaderInput(mShaderType, elementType.qualifier))
Olli Etuahocc36b982015-07-10 14:14:18 +03001067 {
Olli Etuahoe0803872017-08-23 15:30:23 +03001068 error(indexLocation, "cannot declare arrays of structs of this qualifier",
Olli Etuaho8a176262016-08-16 14:23:01 +03001069 TType(elementType).getCompleteString().c_str());
1070 return false;
Olli Etuahocc36b982015-07-10 14:14:18 +03001071 }
Olli Etuahoe0803872017-08-23 15:30:23 +03001072 return checkIsValidQualifierForArray(indexLocation, elementType);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001073}
1074
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001075// Enforce non-initializer type/qualifier rules.
Olli Etuaho856c4972016-08-08 11:38:39 +03001076void TParseContext::checkCanBeDeclaredWithoutInitializer(const TSourceLoc &line,
1077 const TString &identifier,
Olli Etuaho55bde912017-10-25 13:41:13 +03001078 TType *type)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001079{
Olli Etuaho3739d232015-04-08 12:23:44 +03001080 ASSERT(type != nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03001081 if (type->getQualifier() == EvqConst)
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001082 {
1083 // Make the qualifier make sense.
Olli Etuaho55bde912017-10-25 13:41:13 +03001084 type->setQualifier(EvqTemporary);
Olli Etuaho3739d232015-04-08 12:23:44 +03001085
1086 // Generate informative error messages for ESSL1.
1087 // In ESSL3 arrays and structures containing arrays can be constant.
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001088 if (mShaderVersion < 300 && type->isStructureContainingArrays())
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001089 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05301090 error(line,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001091 "structures containing arrays may not be declared constant since they cannot be "
1092 "initialized",
Arun Patole7e7e68d2015-05-22 12:02:25 +05301093 identifier.c_str());
daniel@transgaming.com8abd0b72012-09-27 17:46:07 +00001094 }
1095 else
1096 {
1097 error(line, "variables with qualifier 'const' must be initialized", identifier.c_str());
1098 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001099 }
Olli Etuaho55bde912017-10-25 13:41:13 +03001100 // This will make the type sized if it isn't sized yet.
1101 checkIsNotUnsizedArray(line, "implicitly sized arrays need to be initialized",
1102 identifier.c_str(), type);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001103}
1104
Olli Etuaho2935c582015-04-08 14:32:06 +03001105// Do some simple checks that are shared between all variable declarations,
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001106// and update the symbol table.
1107//
Olli Etuaho2935c582015-04-08 14:32:06 +03001108// Returns true if declaring the variable succeeded.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001109//
Jamie Madillb98c3a82015-07-23 14:26:04 -04001110bool TParseContext::declareVariable(const TSourceLoc &line,
1111 const TString &identifier,
Olli Etuahob60d30f2018-01-16 12:31:06 +02001112 const TType *type,
Olli Etuaho2935c582015-04-08 14:32:06 +03001113 TVariable **variable)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001114{
Olli Etuaho2935c582015-04-08 14:32:06 +03001115 ASSERT((*variable) == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001116
Olli Etuaho195be942017-12-04 23:40:14 +02001117 (*variable) = new TVariable(&symbolTable, &identifier, type, SymbolType::UserDefined);
1118
Olli Etuahob60d30f2018-01-16 12:31:06 +02001119 checkBindingIsValid(line, *type);
Olli Etuaho43364892017-02-13 16:00:12 +00001120
Olli Etuaho856c4972016-08-08 11:38:39 +03001121 bool needsReservedCheck = true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001122
Olli Etuaho2935c582015-04-08 14:32:06 +03001123 // gl_LastFragData may be redeclared with a new precision qualifier
Olli Etuahob60d30f2018-01-16 12:31:06 +02001124 if (type->isArray() && identifier.compare(0, 15, "gl_LastFragData") == 0)
Olli Etuaho2935c582015-04-08 14:32:06 +03001125 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001126 const TVariable *maxDrawBuffers = static_cast<const TVariable *>(
1127 symbolTable.findBuiltIn("gl_MaxDrawBuffers", mShaderVersion));
Olli Etuahob60d30f2018-01-16 12:31:06 +02001128 if (type->isArrayOfArrays())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001129 {
1130 error(line, "redeclaration of gl_LastFragData as an array of arrays",
1131 identifier.c_str());
1132 return false;
1133 }
Olli Etuahob60d30f2018-01-16 12:31:06 +02001134 else if (static_cast<int>(type->getOutermostArraySize()) ==
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001135 maxDrawBuffers->getConstPointer()->getIConst())
Olli Etuaho2935c582015-04-08 14:32:06 +03001136 {
Olli Etuahodd21ecf2018-01-10 12:42:09 +02001137 if (const TSymbol *builtInSymbol = symbolTable.findBuiltIn(identifier, mShaderVersion))
Olli Etuaho2935c582015-04-08 14:32:06 +03001138 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001139 needsReservedCheck = !checkCanUseExtension(line, builtInSymbol->extension());
Olli Etuaho2935c582015-04-08 14:32:06 +03001140 }
1141 }
1142 else
1143 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001144 error(line, "redeclaration of gl_LastFragData with size != gl_MaxDrawBuffers",
1145 identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001146 return false;
1147 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001148 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001149
Olli Etuaho8a176262016-08-16 14:23:01 +03001150 if (needsReservedCheck && !checkIsNotReserved(line, identifier))
Olli Etuaho2935c582015-04-08 14:32:06 +03001151 return false;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001152
Olli Etuaho195be942017-12-04 23:40:14 +02001153 if (!symbolTable.declareVariable(*variable))
Olli Etuaho2935c582015-04-08 14:32:06 +03001154 {
1155 error(line, "redefinition", identifier.c_str());
Olli Etuaho2935c582015-04-08 14:32:06 +03001156 return false;
1157 }
1158
Olli Etuahob60d30f2018-01-16 12:31:06 +02001159 if (!checkIsNonVoid(line, identifier, type->getBasicType()))
Olli Etuaho2935c582015-04-08 14:32:06 +03001160 return false;
1161
1162 return true;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001163}
1164
Martin Radev70866b82016-07-22 15:27:42 +03001165void TParseContext::checkIsParameterQualifierValid(
1166 const TSourceLoc &line,
1167 const TTypeQualifierBuilder &typeQualifierBuilder,
1168 TType *type)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301169{
Olli Etuahocce89652017-06-19 16:04:09 +03001170 // The only parameter qualifiers a parameter can have are in, out, inout or const.
Olli Etuaho77ba4082016-12-16 12:01:18 +00001171 TTypeQualifier typeQualifier = typeQualifierBuilder.getParameterTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03001172
1173 if (typeQualifier.qualifier == EvqOut || typeQualifier.qualifier == EvqInOut)
Arun Patole7e7e68d2015-05-22 12:02:25 +05301174 {
Martin Radev2cc85b32016-08-05 16:22:53 +03001175 checkOutParameterIsNotOpaqueType(line, typeQualifier.qualifier, *type);
1176 }
1177
1178 if (!IsImage(type->getBasicType()))
1179 {
Olli Etuaho43364892017-02-13 16:00:12 +00001180 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, line);
Martin Radev2cc85b32016-08-05 16:22:53 +03001181 }
1182 else
1183 {
1184 type->setMemoryQualifier(typeQualifier.memoryQualifier);
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001185 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001186
Martin Radev70866b82016-07-22 15:27:42 +03001187 type->setQualifier(typeQualifier.qualifier);
1188
1189 if (typeQualifier.precision != EbpUndefined)
1190 {
1191 type->setPrecision(typeQualifier.precision);
1192 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001193}
1194
Olli Etuaho703671e2017-11-08 17:47:18 +02001195template <size_t size>
1196bool TParseContext::checkCanUseOneOfExtensions(const TSourceLoc &line,
1197 const std::array<TExtension, size> &extensions)
1198{
1199 ASSERT(!extensions.empty());
1200 const TExtensionBehavior &extBehavior = extensionBehavior();
1201
1202 bool canUseWithWarning = false;
1203 bool canUseWithoutWarning = false;
1204
1205 const char *errorMsgString = "";
1206 TExtension errorMsgExtension = TExtension::UNDEFINED;
1207
1208 for (TExtension extension : extensions)
1209 {
1210 auto extIter = extBehavior.find(extension);
1211 if (canUseWithWarning)
1212 {
1213 // We already have an extension that we can use, but with a warning.
1214 // See if we can use the alternative extension without a warning.
1215 if (extIter == extBehavior.end())
1216 {
1217 continue;
1218 }
1219 if (extIter->second == EBhEnable || extIter->second == EBhRequire)
1220 {
1221 canUseWithoutWarning = true;
1222 break;
1223 }
1224 continue;
1225 }
1226 if (extIter == extBehavior.end())
1227 {
1228 errorMsgString = "extension is not supported";
1229 errorMsgExtension = extension;
1230 }
1231 else if (extIter->second == EBhUndefined || extIter->second == EBhDisable)
1232 {
1233 errorMsgString = "extension is disabled";
1234 errorMsgExtension = extension;
1235 }
1236 else if (extIter->second == EBhWarn)
1237 {
1238 errorMsgExtension = extension;
1239 canUseWithWarning = true;
1240 }
1241 else
1242 {
1243 ASSERT(extIter->second == EBhEnable || extIter->second == EBhRequire);
1244 canUseWithoutWarning = true;
1245 break;
1246 }
1247 }
1248
1249 if (canUseWithoutWarning)
1250 {
1251 return true;
1252 }
1253 if (canUseWithWarning)
1254 {
1255 warning(line, "extension is being used", GetExtensionNameString(errorMsgExtension));
1256 return true;
1257 }
1258 error(line, errorMsgString, GetExtensionNameString(errorMsgExtension));
1259 return false;
1260}
1261
1262template bool TParseContext::checkCanUseOneOfExtensions(
1263 const TSourceLoc &line,
1264 const std::array<TExtension, 1> &extensions);
1265template bool TParseContext::checkCanUseOneOfExtensions(
1266 const TSourceLoc &line,
1267 const std::array<TExtension, 2> &extensions);
1268template bool TParseContext::checkCanUseOneOfExtensions(
1269 const TSourceLoc &line,
1270 const std::array<TExtension, 3> &extensions);
1271
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001272bool TParseContext::checkCanUseExtension(const TSourceLoc &line, TExtension extension)
alokp@chromium.org8815d7f2010-09-09 17:30:03 +00001273{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001274 ASSERT(extension != TExtension::UNDEFINED);
Corentin Wallez1d33c212017-11-13 10:21:39 -08001275 return checkCanUseOneOfExtensions(line, std::array<TExtension, 1u>{{extension}});
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001276}
1277
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001278// ESSL 3.00.6 section 4.8 Empty Declarations: "The combinations of qualifiers that cause
1279// compile-time or link-time errors are the same whether or not the declaration is empty".
1280// This function implements all the checks that are done on qualifiers regardless of if the
1281// declaration is empty.
1282void TParseContext::declarationQualifierErrorCheck(const sh::TQualifier qualifier,
1283 const sh::TLayoutQualifier &layoutQualifier,
1284 const TSourceLoc &location)
1285{
1286 if (qualifier == EvqShared && !layoutQualifier.isEmpty())
1287 {
1288 error(location, "Shared memory declarations cannot have layout specified", "layout");
1289 }
1290
1291 if (layoutQualifier.matrixPacking != EmpUnspecified)
1292 {
1293 error(location, "layout qualifier only valid for interface blocks",
1294 getMatrixPackingString(layoutQualifier.matrixPacking));
1295 return;
1296 }
1297
1298 if (layoutQualifier.blockStorage != EbsUnspecified)
1299 {
1300 error(location, "layout qualifier only valid for interface blocks",
1301 getBlockStorageString(layoutQualifier.blockStorage));
1302 return;
1303 }
1304
1305 if (qualifier == EvqFragmentOut)
1306 {
1307 if (layoutQualifier.location != -1 && layoutQualifier.yuv == true)
1308 {
1309 error(location, "invalid layout qualifier combination", "yuv");
1310 return;
1311 }
1312 }
1313 else
1314 {
1315 checkYuvIsNotSpecified(location, layoutQualifier.yuv);
1316 }
1317
Olli Etuaho95468d12017-05-04 11:14:34 +03001318 // If multiview extension is enabled, "in" qualifier is allowed in the vertex shader in previous
1319 // parsing steps. So it needs to be checked here.
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001320 if (isExtensionEnabled(TExtension::OVR_multiview) && mShaderVersion < 300 &&
1321 qualifier == EvqVertexIn)
Olli Etuaho95468d12017-05-04 11:14:34 +03001322 {
1323 error(location, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
1324 }
1325
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001326 bool canHaveLocation = qualifier == EvqVertexIn || qualifier == EvqFragmentOut;
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001327 if (mShaderVersion >= 310)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001328 {
Jiawei Shao4cc89e22017-08-31 14:25:54 +08001329 canHaveLocation = canHaveLocation || qualifier == EvqUniform || IsVarying(qualifier);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001330 // We're not checking whether the uniform location is in range here since that depends on
1331 // the type of the variable.
1332 // The type can only be fully determined for non-empty declarations.
1333 }
1334 if (!canHaveLocation)
1335 {
1336 checkLocationIsNotSpecified(location, layoutQualifier);
1337 }
1338}
1339
jchen104cdac9e2017-05-08 11:01:20 +08001340void TParseContext::atomicCounterQualifierErrorCheck(const TPublicType &publicType,
1341 const TSourceLoc &location)
1342{
1343 if (publicType.precision != EbpHigh)
1344 {
1345 error(location, "Can only be highp", "atomic counter");
1346 }
1347 // dEQP enforces compile error if location is specified. See uniform_location.test.
1348 if (publicType.layoutQualifier.location != -1)
1349 {
1350 error(location, "location must not be set for atomic_uint", "layout");
1351 }
1352 if (publicType.layoutQualifier.binding == -1)
1353 {
1354 error(location, "no binding specified", "atomic counter");
1355 }
1356}
1357
Olli Etuaho55bde912017-10-25 13:41:13 +03001358void TParseContext::emptyDeclarationErrorCheck(const TType &type, const TSourceLoc &location)
Martin Radevb8b01222016-11-20 23:25:53 +02001359{
Olli Etuaho55bde912017-10-25 13:41:13 +03001360 if (type.isUnsizedArray())
Martin Radevb8b01222016-11-20 23:25:53 +02001361 {
1362 // ESSL3 spec section 4.1.9: Array declaration which leaves the size unspecified is an
1363 // error. It is assumed that this applies to empty declarations as well.
1364 error(location, "empty array declaration needs to specify a size", "");
1365 }
Martin Radevb8b01222016-11-20 23:25:53 +02001366}
1367
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001368// These checks are done for all declarations that are non-empty. They're done for non-empty
1369// declarations starting a declarator list, and declarators that follow an empty declaration.
1370void TParseContext::nonEmptyDeclarationErrorCheck(const TPublicType &publicType,
1371 const TSourceLoc &identifierLocation)
Jamie Madilla5efff92013-06-06 11:56:47 -04001372{
Olli Etuahofa33d582015-04-09 14:33:12 +03001373 switch (publicType.qualifier)
1374 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001375 case EvqVaryingIn:
1376 case EvqVaryingOut:
1377 case EvqAttribute:
1378 case EvqVertexIn:
1379 case EvqFragmentOut:
Martin Radev802abe02016-08-04 17:48:32 +03001380 case EvqComputeIn:
Martin Radev4a9cd802016-09-01 16:51:51 +03001381 if (publicType.getBasicType() == EbtStruct)
Jamie Madillb98c3a82015-07-23 14:26:04 -04001382 {
1383 error(identifierLocation, "cannot be used with a structure",
1384 getQualifierString(publicType.qualifier));
Olli Etuaho383b7912016-08-05 11:22:59 +03001385 return;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001386 }
Jiajia Qinbc585152017-06-23 15:42:17 +08001387 break;
1388 case EvqBuffer:
1389 if (publicType.getBasicType() != EbtInterfaceBlock)
1390 {
1391 error(identifierLocation,
1392 "cannot declare buffer variables at global scope(outside a block)",
1393 getQualifierString(publicType.qualifier));
1394 return;
1395 }
1396 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001397 default:
1398 break;
Olli Etuahofa33d582015-04-09 14:33:12 +03001399 }
jchen10cc2a10e2017-05-03 14:05:12 +08001400 std::string reason(getBasicString(publicType.getBasicType()));
1401 reason += "s must be uniform";
Jamie Madillb98c3a82015-07-23 14:26:04 -04001402 if (publicType.qualifier != EvqUniform &&
jchen10cc2a10e2017-05-03 14:05:12 +08001403 !checkIsNotOpaqueType(identifierLocation, publicType.typeSpecifierNonArray, reason.c_str()))
Martin Radev2cc85b32016-08-05 16:22:53 +03001404 {
1405 return;
1406 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001407
Andrei Volykhina5527072017-03-22 16:46:30 +03001408 if ((publicType.qualifier != EvqTemporary && publicType.qualifier != EvqGlobal &&
1409 publicType.qualifier != EvqConst) &&
1410 publicType.getBasicType() == EbtYuvCscStandardEXT)
1411 {
1412 error(identifierLocation, "cannot be used with a yuvCscStandardEXT",
1413 getQualifierString(publicType.qualifier));
1414 return;
1415 }
1416
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001417 if (mShaderVersion >= 310 && publicType.qualifier == EvqUniform)
1418 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001419 // Valid uniform declarations can't be unsized arrays since uniforms can't be initialized.
1420 // But invalid shaders may still reach here with an unsized array declaration.
Olli Etuaho55bde912017-10-25 13:41:13 +03001421 TType type(publicType);
1422 if (!type.isUnsizedArray())
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001423 {
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001424 checkUniformLocationInRange(identifierLocation, type.getLocationCount(),
1425 publicType.layoutQualifier);
1426 }
1427 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001428
Olli Etuahobb7e5a72017-04-24 10:16:44 +03001429 // check for layout qualifier issues
1430 const TLayoutQualifier layoutQualifier = publicType.layoutQualifier;
Andrei Volykhina5527072017-03-22 16:46:30 +03001431
Martin Radev2cc85b32016-08-05 16:22:53 +03001432 if (IsImage(publicType.getBasicType()))
1433 {
1434
1435 switch (layoutQualifier.imageInternalFormat)
1436 {
1437 case EiifRGBA32F:
1438 case EiifRGBA16F:
1439 case EiifR32F:
1440 case EiifRGBA8:
1441 case EiifRGBA8_SNORM:
1442 if (!IsFloatImage(publicType.getBasicType()))
1443 {
1444 error(identifierLocation,
1445 "internal image format requires a floating image type",
1446 getBasicString(publicType.getBasicType()));
1447 return;
1448 }
1449 break;
1450 case EiifRGBA32I:
1451 case EiifRGBA16I:
1452 case EiifRGBA8I:
1453 case EiifR32I:
1454 if (!IsIntegerImage(publicType.getBasicType()))
1455 {
1456 error(identifierLocation,
1457 "internal image format requires an integer image type",
1458 getBasicString(publicType.getBasicType()));
1459 return;
1460 }
1461 break;
1462 case EiifRGBA32UI:
1463 case EiifRGBA16UI:
1464 case EiifRGBA8UI:
1465 case EiifR32UI:
1466 if (!IsUnsignedImage(publicType.getBasicType()))
1467 {
1468 error(identifierLocation,
1469 "internal image format requires an unsigned image type",
1470 getBasicString(publicType.getBasicType()));
1471 return;
1472 }
1473 break;
1474 case EiifUnspecified:
1475 error(identifierLocation, "layout qualifier", "No image internal format specified");
1476 return;
1477 default:
1478 error(identifierLocation, "layout qualifier", "unrecognized token");
1479 return;
1480 }
1481
1482 // GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
1483 switch (layoutQualifier.imageInternalFormat)
1484 {
1485 case EiifR32F:
1486 case EiifR32I:
1487 case EiifR32UI:
1488 break;
1489 default:
1490 if (!publicType.memoryQualifier.readonly && !publicType.memoryQualifier.writeonly)
1491 {
1492 error(identifierLocation, "layout qualifier",
1493 "Except for images with the r32f, r32i and r32ui format qualifiers, "
1494 "image variables must be qualified readonly and/or writeonly");
1495 return;
1496 }
1497 break;
1498 }
1499 }
1500 else
1501 {
Olli Etuaho43364892017-02-13 16:00:12 +00001502 checkInternalFormatIsNotSpecified(identifierLocation, layoutQualifier.imageInternalFormat);
Olli Etuaho43364892017-02-13 16:00:12 +00001503 checkMemoryQualifierIsNotSpecified(publicType.memoryQualifier, identifierLocation);
1504 }
jchen104cdac9e2017-05-08 11:01:20 +08001505
1506 if (IsAtomicCounter(publicType.getBasicType()))
1507 {
1508 atomicCounterQualifierErrorCheck(publicType, identifierLocation);
1509 }
1510 else
1511 {
1512 checkOffsetIsNotSpecified(identifierLocation, layoutQualifier.offset);
1513 }
Olli Etuaho43364892017-02-13 16:00:12 +00001514}
Martin Radev2cc85b32016-08-05 16:22:53 +03001515
Olli Etuaho43364892017-02-13 16:00:12 +00001516void TParseContext::checkBindingIsValid(const TSourceLoc &identifierLocation, const TType &type)
1517{
1518 TLayoutQualifier layoutQualifier = type.getLayoutQualifier();
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001519 // Note that the ESSL 3.10 section 4.4.5 is not particularly clear on how the binding qualifier
1520 // on arrays of arrays should be handled. We interpret the spec so that the binding value is
1521 // incremented for each element of the innermost nested arrays. This is in line with how arrays
1522 // of arrays of blocks are specified to behave in GLSL 4.50 and a conservative interpretation
1523 // when it comes to which shaders are accepted by the compiler.
1524 int arrayTotalElementCount = type.getArraySizeProduct();
Olli Etuaho43364892017-02-13 16:00:12 +00001525 if (IsImage(type.getBasicType()))
1526 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001527 checkImageBindingIsValid(identifierLocation, layoutQualifier.binding,
1528 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001529 }
1530 else if (IsSampler(type.getBasicType()))
1531 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001532 checkSamplerBindingIsValid(identifierLocation, layoutQualifier.binding,
1533 arrayTotalElementCount);
Olli Etuaho43364892017-02-13 16:00:12 +00001534 }
jchen104cdac9e2017-05-08 11:01:20 +08001535 else if (IsAtomicCounter(type.getBasicType()))
1536 {
1537 checkAtomicCounterBindingIsValid(identifierLocation, layoutQualifier.binding);
1538 }
Olli Etuaho43364892017-02-13 16:00:12 +00001539 else
1540 {
1541 ASSERT(!IsOpaqueType(type.getBasicType()));
1542 checkBindingIsNotSpecified(identifierLocation, layoutQualifier.binding);
Martin Radev2cc85b32016-08-05 16:22:53 +03001543 }
Jamie Madilla5efff92013-06-06 11:56:47 -04001544}
1545
Olli Etuaho856c4972016-08-08 11:38:39 +03001546void TParseContext::checkLayoutQualifierSupported(const TSourceLoc &location,
1547 const TString &layoutQualifierName,
1548 int versionRequired)
Martin Radev802abe02016-08-04 17:48:32 +03001549{
1550
1551 if (mShaderVersion < versionRequired)
1552 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001553 error(location, "invalid layout qualifier: not supported", layoutQualifierName.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03001554 }
1555}
1556
Olli Etuaho856c4972016-08-08 11:38:39 +03001557bool TParseContext::checkWorkGroupSizeIsNotSpecified(const TSourceLoc &location,
1558 const TLayoutQualifier &layoutQualifier)
Martin Radev802abe02016-08-04 17:48:32 +03001559{
Martin Radev4c4c8e72016-08-04 12:25:34 +03001560 const sh::WorkGroupSize &localSize = layoutQualifier.localSize;
Martin Radev802abe02016-08-04 17:48:32 +03001561 for (size_t i = 0u; i < localSize.size(); ++i)
1562 {
1563 if (localSize[i] != -1)
1564 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001565 error(location,
1566 "invalid layout qualifier: only valid when used with 'in' in a compute shader "
1567 "global layout declaration",
1568 getWorkGroupSizeString(i));
Olli Etuaho8a176262016-08-16 14:23:01 +03001569 return false;
Martin Radev802abe02016-08-04 17:48:32 +03001570 }
1571 }
1572
Olli Etuaho8a176262016-08-16 14:23:01 +03001573 return true;
Martin Radev802abe02016-08-04 17:48:32 +03001574}
1575
Olli Etuaho43364892017-02-13 16:00:12 +00001576void TParseContext::checkInternalFormatIsNotSpecified(const TSourceLoc &location,
Martin Radev2cc85b32016-08-05 16:22:53 +03001577 TLayoutImageInternalFormat internalFormat)
1578{
1579 if (internalFormat != EiifUnspecified)
1580 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00001581 error(location, "invalid layout qualifier: only valid when used with images",
1582 getImageInternalFormatString(internalFormat));
Martin Radev2cc85b32016-08-05 16:22:53 +03001583 }
Olli Etuaho43364892017-02-13 16:00:12 +00001584}
1585
1586void TParseContext::checkBindingIsNotSpecified(const TSourceLoc &location, int binding)
1587{
1588 if (binding != -1)
1589 {
1590 error(location,
1591 "invalid layout qualifier: only valid when used with opaque types or blocks",
1592 "binding");
1593 }
1594}
1595
jchen104cdac9e2017-05-08 11:01:20 +08001596void TParseContext::checkOffsetIsNotSpecified(const TSourceLoc &location, int offset)
1597{
1598 if (offset != -1)
1599 {
1600 error(location, "invalid layout qualifier: only valid when used with atomic counters",
1601 "offset");
1602 }
1603}
1604
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001605void TParseContext::checkImageBindingIsValid(const TSourceLoc &location,
1606 int binding,
1607 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001608{
1609 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001610 if (binding >= 0 && binding + arrayTotalElementCount > mMaxImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001611 {
1612 error(location, "image binding greater than gl_MaxImageUnits", "binding");
1613 }
1614}
1615
1616void TParseContext::checkSamplerBindingIsValid(const TSourceLoc &location,
1617 int binding,
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001618 int arrayTotalElementCount)
Olli Etuaho43364892017-02-13 16:00:12 +00001619{
1620 // Expects arraySize to be 1 when setting binding for only a single variable.
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001621 if (binding >= 0 && binding + arrayTotalElementCount > mMaxCombinedTextureImageUnits)
Olli Etuaho43364892017-02-13 16:00:12 +00001622 {
1623 error(location, "sampler binding greater than maximum texture units", "binding");
1624 }
Martin Radev2cc85b32016-08-05 16:22:53 +03001625}
1626
Jiajia Qinbc585152017-06-23 15:42:17 +08001627void TParseContext::checkBlockBindingIsValid(const TSourceLoc &location,
1628 const TQualifier &qualifier,
1629 int binding,
1630 int arraySize)
jchen10af713a22017-04-19 09:10:56 +08001631{
1632 int size = (arraySize == 0 ? 1 : arraySize);
Jiajia Qinbc585152017-06-23 15:42:17 +08001633 if (qualifier == EvqUniform)
jchen10af713a22017-04-19 09:10:56 +08001634 {
Jiajia Qinbc585152017-06-23 15:42:17 +08001635 if (binding + size > mMaxUniformBufferBindings)
1636 {
1637 error(location, "uniform block binding greater than MAX_UNIFORM_BUFFER_BINDINGS",
1638 "binding");
1639 }
1640 }
1641 else if (qualifier == EvqBuffer)
1642 {
1643 if (binding + size > mMaxShaderStorageBufferBindings)
1644 {
1645 error(location,
1646 "shader storage block binding greater than MAX_SHADER_STORAGE_BUFFER_BINDINGS",
1647 "binding");
1648 }
jchen10af713a22017-04-19 09:10:56 +08001649 }
1650}
jchen104cdac9e2017-05-08 11:01:20 +08001651void TParseContext::checkAtomicCounterBindingIsValid(const TSourceLoc &location, int binding)
1652{
1653 if (binding >= mMaxAtomicCounterBindings)
1654 {
1655 error(location, "atomic counter binding greater than gl_MaxAtomicCounterBindings",
1656 "binding");
1657 }
1658}
jchen10af713a22017-04-19 09:10:56 +08001659
Olli Etuaho6ca2b652017-02-19 18:05:10 +00001660void TParseContext::checkUniformLocationInRange(const TSourceLoc &location,
1661 int objectLocationCount,
1662 const TLayoutQualifier &layoutQualifier)
1663{
1664 int loc = layoutQualifier.location;
1665 if (loc >= 0 && loc + objectLocationCount > mMaxUniformLocations)
1666 {
1667 error(location, "Uniform location out of range", "location");
1668 }
1669}
1670
Andrei Volykhina5527072017-03-22 16:46:30 +03001671void TParseContext::checkYuvIsNotSpecified(const TSourceLoc &location, bool yuv)
1672{
1673 if (yuv != false)
1674 {
1675 error(location, "invalid layout qualifier: only valid on program outputs", "yuv");
1676 }
1677}
1678
Jiajia Qinbc585152017-06-23 15:42:17 +08001679void TParseContext::functionCallRValueLValueErrorCheck(const TFunction *fnCandidate,
1680 TIntermAggregate *fnCall)
Olli Etuahob6e07a62015-02-16 12:22:10 +02001681{
1682 for (size_t i = 0; i < fnCandidate->getParamCount(); ++i)
1683 {
1684 TQualifier qual = fnCandidate->getParam(i).type->getQualifier();
Jiajia Qinbc585152017-06-23 15:42:17 +08001685 TIntermTyped *argument = (*(fnCall->getSequence()))[i]->getAsTyped();
1686 if (!IsImage(argument->getBasicType()) && (IsQualifierUnspecified(qual) || qual == EvqIn ||
1687 qual == EvqInOut || qual == EvqConstReadOnly))
1688 {
1689 if (argument->getMemoryQualifier().writeonly)
1690 {
1691 error(argument->getLine(),
1692 "Writeonly value cannot be passed for 'in' or 'inout' parameters.",
Olli Etuaho0c371002017-12-13 17:00:25 +04001693 fnCall->functionName());
Jiajia Qinbc585152017-06-23 15:42:17 +08001694 return;
1695 }
1696 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001697 if (qual == EvqOut || qual == EvqInOut)
1698 {
Olli Etuaho8a176262016-08-16 14:23:01 +03001699 if (!checkCanBeLValue(argument->getLine(), "assign", argument))
Olli Etuahob6e07a62015-02-16 12:22:10 +02001700 {
Olli Etuaho856c4972016-08-08 11:38:39 +03001701 error(argument->getLine(),
Olli Etuaho4de340a2016-12-16 09:32:03 +00001702 "Constant value cannot be passed for 'out' or 'inout' parameters.",
Olli Etuaho0c371002017-12-13 17:00:25 +04001703 fnCall->functionName());
Olli Etuaho383b7912016-08-05 11:22:59 +03001704 return;
Olli Etuahob6e07a62015-02-16 12:22:10 +02001705 }
1706 }
1707 }
Olli Etuahob6e07a62015-02-16 12:22:10 +02001708}
1709
Martin Radev70866b82016-07-22 15:27:42 +03001710void TParseContext::checkInvariantVariableQualifier(bool invariant,
1711 const TQualifier qualifier,
1712 const TSourceLoc &invariantLocation)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001713{
Martin Radev70866b82016-07-22 15:27:42 +03001714 if (!invariant)
1715 return;
1716
1717 if (mShaderVersion < 300)
Olli Etuaho37ad4742015-04-27 13:18:50 +03001718 {
Martin Radev70866b82016-07-22 15:27:42 +03001719 // input variables in the fragment shader can be also qualified as invariant
1720 if (!sh::CanBeInvariantESSL1(qualifier))
1721 {
1722 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1723 }
1724 }
1725 else
1726 {
1727 if (!sh::CanBeInvariantESSL3OrGreater(qualifier))
1728 {
1729 error(invariantLocation, "Cannot be qualified as invariant.", "invariant");
1730 }
Olli Etuaho37ad4742015-04-27 13:18:50 +03001731 }
1732}
1733
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001734bool TParseContext::isExtensionEnabled(TExtension extension) const
Jamie Madill5d287f52013-07-12 15:38:19 -04001735{
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03001736 return IsExtensionEnabled(extensionBehavior(), extension);
Jamie Madill5d287f52013-07-12 15:38:19 -04001737}
1738
Jamie Madillb98c3a82015-07-23 14:26:04 -04001739void TParseContext::handleExtensionDirective(const TSourceLoc &loc,
1740 const char *extName,
1741 const char *behavior)
Jamie Madill075edd82013-07-08 13:30:19 -04001742{
1743 pp::SourceLocation srcLoc;
1744 srcLoc.file = loc.first_file;
1745 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001746 mDirectiveHandler.handleExtension(srcLoc, extName, behavior);
Jamie Madill075edd82013-07-08 13:30:19 -04001747}
1748
Jamie Madillb98c3a82015-07-23 14:26:04 -04001749void TParseContext::handlePragmaDirective(const TSourceLoc &loc,
1750 const char *name,
1751 const char *value,
1752 bool stdgl)
Jamie Madill075edd82013-07-08 13:30:19 -04001753{
1754 pp::SourceLocation srcLoc;
1755 srcLoc.file = loc.first_file;
1756 srcLoc.line = loc.first_line;
Jamie Madill6e06b1f2015-05-14 10:01:17 -04001757 mDirectiveHandler.handlePragma(srcLoc, name, value, stdgl);
Jamie Madill075edd82013-07-08 13:30:19 -04001758}
1759
Martin Radev4c4c8e72016-08-04 12:25:34 +03001760sh::WorkGroupSize TParseContext::getComputeShaderLocalSize() const
Martin Radev802abe02016-08-04 17:48:32 +03001761{
Jamie Madill2f294c92017-11-20 14:47:26 -05001762 sh::WorkGroupSize result(-1);
Martin Radev802abe02016-08-04 17:48:32 +03001763 for (size_t i = 0u; i < result.size(); ++i)
1764 {
1765 if (mComputeShaderLocalSizeDeclared && mComputeShaderLocalSize[i] == -1)
1766 {
1767 result[i] = 1;
1768 }
1769 else
1770 {
1771 result[i] = mComputeShaderLocalSize[i];
1772 }
1773 }
1774 return result;
1775}
1776
Olli Etuaho56229f12017-07-10 14:16:33 +03001777TIntermConstantUnion *TParseContext::addScalarLiteral(const TConstantUnion *constantUnion,
1778 const TSourceLoc &line)
1779{
1780 TIntermConstantUnion *node = new TIntermConstantUnion(
1781 constantUnion, TType(constantUnion->getType(), EbpUndefined, EvqConst));
1782 node->setLine(line);
1783 return node;
1784}
1785
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001786/////////////////////////////////////////////////////////////////////////////////
1787//
1788// Non-Errors.
1789//
1790/////////////////////////////////////////////////////////////////////////////////
1791
Jamie Madill5c097022014-08-20 16:38:32 -04001792const TVariable *TParseContext::getNamedVariable(const TSourceLoc &location,
1793 const TString *name,
1794 const TSymbol *symbol)
1795{
Jamie Madill5c097022014-08-20 16:38:32 -04001796 if (!symbol)
1797 {
1798 error(location, "undeclared identifier", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001799 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001800 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001801
1802 if (!symbol->isVariable())
Jamie Madill5c097022014-08-20 16:38:32 -04001803 {
1804 error(location, "variable expected", name->c_str());
Olli Etuaho0f684632017-07-13 12:42:15 +03001805 return nullptr;
Jamie Madill5c097022014-08-20 16:38:32 -04001806 }
Olli Etuaho0f684632017-07-13 12:42:15 +03001807
1808 const TVariable *variable = static_cast<const TVariable *>(symbol);
1809
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001810 if (variable->extension() != TExtension::UNDEFINED)
Jamie Madill5c097022014-08-20 16:38:32 -04001811 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02001812 checkCanUseExtension(location, variable->extension());
Jamie Madill5c097022014-08-20 16:38:32 -04001813 }
1814
Olli Etuaho0f684632017-07-13 12:42:15 +03001815 // Reject shaders using both gl_FragData and gl_FragColor
1816 TQualifier qualifier = variable->getType().getQualifier();
1817 if (qualifier == EvqFragData || qualifier == EvqSecondaryFragDataEXT)
Jamie Madill5c097022014-08-20 16:38:32 -04001818 {
Olli Etuaho0f684632017-07-13 12:42:15 +03001819 mUsesFragData = true;
1820 }
1821 else if (qualifier == EvqFragColor || qualifier == EvqSecondaryFragColorEXT)
1822 {
1823 mUsesFragColor = true;
1824 }
1825 if (qualifier == EvqSecondaryFragDataEXT || qualifier == EvqSecondaryFragColorEXT)
1826 {
1827 mUsesSecondaryOutputs = true;
Jamie Madill5c097022014-08-20 16:38:32 -04001828 }
1829
Olli Etuaho0f684632017-07-13 12:42:15 +03001830 // This validation is not quite correct - it's only an error to write to
1831 // both FragData and FragColor. For simplicity, and because users shouldn't
1832 // be rewarded for reading from undefined varaibles, return an error
1833 // if they are both referenced, rather than assigned.
1834 if (mUsesFragData && mUsesFragColor)
1835 {
1836 const char *errorMessage = "cannot use both gl_FragData and gl_FragColor";
1837 if (mUsesSecondaryOutputs)
1838 {
1839 errorMessage =
1840 "cannot use both output variable sets (gl_FragData, gl_SecondaryFragDataEXT)"
1841 " and (gl_FragColor, gl_SecondaryFragColorEXT)";
1842 }
1843 error(location, errorMessage, name->c_str());
1844 }
1845
1846 // GLSL ES 3.1 Revision 4, 7.1.3 Compute Shader Special Variables
1847 if (getShaderType() == GL_COMPUTE_SHADER && !mComputeShaderLocalSizeDeclared &&
1848 qualifier == EvqWorkGroupSize)
1849 {
1850 error(location,
1851 "It is an error to use gl_WorkGroupSize before declaring the local group size",
1852 "gl_WorkGroupSize");
1853 }
Jamie Madill5c097022014-08-20 16:38:32 -04001854 return variable;
1855}
1856
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001857TIntermTyped *TParseContext::parseVariableIdentifier(const TSourceLoc &location,
1858 const TString *name,
1859 const TSymbol *symbol)
1860{
1861 const TVariable *variable = getNamedVariable(location, name, symbol);
1862
Olli Etuaho0f684632017-07-13 12:42:15 +03001863 if (!variable)
1864 {
1865 TIntermTyped *node = CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
1866 node->setLine(location);
1867 return node;
1868 }
1869
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001870 const TType &variableType = variable->getType();
Olli Etuaho56229f12017-07-10 14:16:33 +03001871 TIntermTyped *node = nullptr;
1872
Olli Etuahoea22b7a2018-01-04 17:09:11 +02001873 if (variable->getConstPointer() && variableType.canReplaceWithConstantUnion())
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001874 {
Olli Etuaho5c0e0232015-11-11 15:55:59 +02001875 const TConstantUnion *constArray = variable->getConstPointer();
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001876 node = new TIntermConstantUnion(constArray, variableType);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001877 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001878 else if (variableType.getQualifier() == EvqWorkGroupSize && mComputeShaderLocalSizeDeclared)
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001879 {
1880 // gl_WorkGroupSize can be used to size arrays according to the ESSL 3.10.4 spec, so it
1881 // needs to be added to the AST as a constant and not as a symbol.
1882 sh::WorkGroupSize workGroupSize = getComputeShaderLocalSize();
1883 TConstantUnion *constArray = new TConstantUnion[3];
1884 for (size_t i = 0; i < 3; ++i)
1885 {
1886 constArray[i].setUConst(static_cast<unsigned int>(workGroupSize[i]));
1887 }
1888
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001889 ASSERT(variableType.getBasicType() == EbtUInt);
1890 ASSERT(variableType.getObjectSize() == 3);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001891
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001892 TType type(variableType);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001893 type.setQualifier(EvqConst);
Olli Etuaho56229f12017-07-10 14:16:33 +03001894 node = new TIntermConstantUnion(constArray, type);
Olli Etuahoaecfa8e2016-12-09 12:47:26 +00001895 }
Jiawei Shao8e4b3552017-08-30 14:20:58 +08001896 else if ((mGeometryShaderInputPrimitiveType != EptUndefined) &&
1897 (variableType.getQualifier() == EvqPerVertexIn))
Jiawei Shaod8105a02017-08-08 09:54:36 +08001898 {
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02001899 ASSERT(mGlInVariableWithArraySize != nullptr);
1900 node = new TIntermSymbol(mGlInVariableWithArraySize);
Jiawei Shaod8105a02017-08-08 09:54:36 +08001901 }
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001902 else
1903 {
Olli Etuaho195be942017-12-04 23:40:14 +02001904 node = new TIntermSymbol(variable);
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001905 }
Olli Etuaho56229f12017-07-10 14:16:33 +03001906 ASSERT(node != nullptr);
1907 node->setLine(location);
1908 return node;
Olli Etuaho82c29ed2015-11-03 13:06:54 +02001909}
1910
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001911// Initializers show up in several places in the grammar. Have one set of
1912// code to handle them here.
1913//
Olli Etuaho914b79a2017-06-19 16:03:19 +03001914// Returns true on success.
Jamie Madillb98c3a82015-07-23 14:26:04 -04001915bool TParseContext::executeInitializer(const TSourceLoc &line,
1916 const TString &identifier,
Olli Etuahob60d30f2018-01-16 12:31:06 +02001917 TType *type,
Jamie Madillb98c3a82015-07-23 14:26:04 -04001918 TIntermTyped *initializer,
Olli Etuaho13389b62016-10-16 11:48:18 +01001919 TIntermBinary **initNode)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001920{
Olli Etuaho13389b62016-10-16 11:48:18 +01001921 ASSERT(initNode != nullptr);
1922 ASSERT(*initNode == nullptr);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001923
Olli Etuahob60d30f2018-01-16 12:31:06 +02001924 if (type->isUnsizedArray())
Olli Etuaho376f1b52015-04-13 13:23:41 +03001925 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03001926 // In case initializer is not an array or type has more dimensions than initializer, this
1927 // will default to setting array sizes to 1. We have not checked yet whether the initializer
1928 // actually is an array or not. Having a non-array initializer for an unsized array will
1929 // result in an error later, so we don't generate an error message here.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08001930 auto *arraySizes = initializer->getType().getArraySizes();
Olli Etuahob60d30f2018-01-16 12:31:06 +02001931 type->sizeUnsizedArrays(arraySizes);
1932 }
1933
1934 const TQualifier qualifier = type->getQualifier();
1935
1936 bool constError = false;
1937 if (qualifier == EvqConst)
1938 {
1939 if (EvqConst != initializer->getType().getQualifier())
1940 {
1941 std::stringstream reasonStream;
1942 reasonStream << "assigning non-constant to '" << type->getCompleteString() << "'";
1943 std::string reason = reasonStream.str();
1944 error(line, reason.c_str(), "=");
1945
1946 // We're still going to declare the variable to avoid extra error messages.
1947 type->setQualifier(EvqTemporary);
1948 constError = true;
1949 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03001950 }
Olli Etuaho195be942017-12-04 23:40:14 +02001951
1952 TVariable *variable = nullptr;
Olli Etuaho2935c582015-04-08 14:32:06 +03001953 if (!declareVariable(line, identifier, type, &variable))
1954 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03001955 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001956 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001957
Olli Etuahob60d30f2018-01-16 12:31:06 +02001958 if (constError)
1959 {
1960 return false;
1961 }
1962
Olli Etuahob0c645e2015-05-12 14:25:36 +03001963 bool globalInitWarning = false;
Jamie Madillb98c3a82015-07-23 14:26:04 -04001964 if (symbolTable.atGlobalLevel() &&
Olli Etuahoa2d98142017-12-15 14:18:55 +02001965 !ValidateGlobalInitializer(initializer, mShaderVersion, &globalInitWarning))
Olli Etuahob0c645e2015-05-12 14:25:36 +03001966 {
1967 // Error message does not completely match behavior with ESSL 1.00, but
1968 // we want to steer developers towards only using constant expressions.
1969 error(line, "global variable initializers must be constant expressions", "=");
Olli Etuaho914b79a2017-06-19 16:03:19 +03001970 return false;
Olli Etuahob0c645e2015-05-12 14:25:36 +03001971 }
1972 if (globalInitWarning)
1973 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001974 warning(
1975 line,
1976 "global variable initializers should be constant expressions "
1977 "(uniforms and globals are allowed in global initializers for legacy compatibility)",
1978 "=");
Olli Etuahob0c645e2015-05-12 14:25:36 +03001979 }
1980
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001981 // identifier must be of type constant, a global, or a temporary
Arun Patole7e7e68d2015-05-22 12:02:25 +05301982 if ((qualifier != EvqTemporary) && (qualifier != EvqGlobal) && (qualifier != EvqConst))
1983 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04001984 error(line, " cannot initialize this type of qualifier ",
1985 variable->getType().getQualifierString());
Olli Etuaho914b79a2017-06-19 16:03:19 +03001986 return false;
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00001987 }
Olli Etuahob60d30f2018-01-16 12:31:06 +02001988
1989 TIntermSymbol *intermSymbol = new TIntermSymbol(variable);
1990 intermSymbol->setLine(line);
1991
1992 if (!binaryOpCommonCheck(EOpInitialize, intermSymbol, initializer, line))
1993 {
1994 assignError(line, "=", variable->getType().getCompleteString(),
1995 initializer->getCompleteString());
1996 return false;
1997 }
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00001998
Arun Patole7e7e68d2015-05-22 12:02:25 +05301999 if (qualifier == EvqConst)
2000 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002001 // Save the constant folded value to the variable if possible.
2002 const TConstantUnion *constArray = initializer->getConstantValue();
2003 if (constArray)
Arun Patole7e7e68d2015-05-22 12:02:25 +05302004 {
Olli Etuahoea22b7a2018-01-04 17:09:11 +02002005 variable->shareConstPointer(constArray);
2006 if (initializer->getType().canReplaceWithConstantUnion())
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002007 {
Olli Etuaho914b79a2017-06-19 16:03:19 +03002008 ASSERT(*initNode == nullptr);
2009 return true;
Olli Etuahob1edc4f2015-11-02 17:20:03 +02002010 }
daniel@transgaming.comea15b0e2010-04-29 03:32:36 +00002011 }
2012 }
Olli Etuahoe7847b02015-03-16 11:56:12 +02002013
Olli Etuahob60d30f2018-01-16 12:31:06 +02002014 *initNode = new TIntermBinary(EOpInitialize, intermSymbol, initializer);
2015 (*initNode)->setLine(line);
Olli Etuaho914b79a2017-06-19 16:03:19 +03002016 return true;
2017}
2018
2019TIntermNode *TParseContext::addConditionInitializer(const TPublicType &pType,
2020 const TString &identifier,
2021 TIntermTyped *initializer,
2022 const TSourceLoc &loc)
2023{
2024 checkIsScalarBool(loc, pType);
2025 TIntermBinary *initNode = nullptr;
Olli Etuahob60d30f2018-01-16 12:31:06 +02002026 TType *type = new TType(pType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002027 if (executeInitializer(loc, identifier, type, initializer, &initNode))
Olli Etuaho914b79a2017-06-19 16:03:19 +03002028 {
2029 // The initializer is valid. The init condition needs to have a node - either the
2030 // initializer node, or a constant node in case the initialized variable is const and won't
2031 // be recorded in the AST.
2032 if (initNode == nullptr)
2033 {
2034 return initializer;
2035 }
2036 else
2037 {
2038 TIntermDeclaration *declaration = new TIntermDeclaration();
2039 declaration->appendDeclarator(initNode);
2040 return declaration;
2041 }
2042 }
2043 return nullptr;
2044}
2045
2046TIntermNode *TParseContext::addLoop(TLoopType type,
2047 TIntermNode *init,
2048 TIntermNode *cond,
2049 TIntermTyped *expr,
2050 TIntermNode *body,
2051 const TSourceLoc &line)
2052{
2053 TIntermNode *node = nullptr;
2054 TIntermTyped *typedCond = nullptr;
2055 if (cond)
2056 {
2057 typedCond = cond->getAsTyped();
2058 }
2059 if (cond == nullptr || typedCond)
2060 {
Olli Etuahocce89652017-06-19 16:04:09 +03002061 if (type == ELoopDoWhile)
2062 {
2063 checkIsScalarBool(line, typedCond);
2064 }
2065 // In the case of other loops, it was checked before that the condition is a scalar boolean.
2066 ASSERT(mDiagnostics->numErrors() > 0 || typedCond == nullptr ||
2067 (typedCond->getBasicType() == EbtBool && !typedCond->isArray() &&
2068 !typedCond->isVector()));
2069
Olli Etuaho3ec75682017-07-05 17:02:55 +03002070 node = new TIntermLoop(type, init, typedCond, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002071 node->setLine(line);
2072 return node;
2073 }
2074
Olli Etuahocce89652017-06-19 16:04:09 +03002075 ASSERT(type != ELoopDoWhile);
2076
Olli Etuaho914b79a2017-06-19 16:03:19 +03002077 TIntermDeclaration *declaration = cond->getAsDeclarationNode();
2078 ASSERT(declaration);
2079 TIntermBinary *declarator = declaration->getSequence()->front()->getAsBinaryNode();
2080 ASSERT(declarator->getLeft()->getAsSymbolNode());
2081
2082 // The condition is a declaration. In the AST representation we don't support declarations as
2083 // loop conditions. Wrap the loop to a block that declares the condition variable and contains
2084 // the loop.
2085 TIntermBlock *block = new TIntermBlock();
2086
2087 TIntermDeclaration *declareCondition = new TIntermDeclaration();
2088 declareCondition->appendDeclarator(declarator->getLeft()->deepCopy());
2089 block->appendStatement(declareCondition);
2090
2091 TIntermBinary *conditionInit = new TIntermBinary(EOpAssign, declarator->getLeft()->deepCopy(),
2092 declarator->getRight()->deepCopy());
Olli Etuaho3ec75682017-07-05 17:02:55 +03002093 TIntermLoop *loop = new TIntermLoop(type, init, conditionInit, expr, EnsureBlock(body));
Olli Etuaho914b79a2017-06-19 16:03:19 +03002094 block->appendStatement(loop);
2095 loop->setLine(line);
2096 block->setLine(line);
2097 return block;
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00002098}
2099
Olli Etuahocce89652017-06-19 16:04:09 +03002100TIntermNode *TParseContext::addIfElse(TIntermTyped *cond,
2101 TIntermNodePair code,
2102 const TSourceLoc &loc)
2103{
Olli Etuaho56229f12017-07-10 14:16:33 +03002104 bool isScalarBool = checkIsScalarBool(loc, cond);
Olli Etuahocce89652017-06-19 16:04:09 +03002105
2106 // For compile time constant conditions, prune the code now.
Olli Etuaho56229f12017-07-10 14:16:33 +03002107 if (isScalarBool && cond->getAsConstantUnion())
Olli Etuahocce89652017-06-19 16:04:09 +03002108 {
2109 if (cond->getAsConstantUnion()->getBConst(0) == true)
2110 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002111 return EnsureBlock(code.node1);
Olli Etuahocce89652017-06-19 16:04:09 +03002112 }
2113 else
2114 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03002115 return EnsureBlock(code.node2);
Olli Etuahocce89652017-06-19 16:04:09 +03002116 }
2117 }
2118
Olli Etuaho3ec75682017-07-05 17:02:55 +03002119 TIntermIfElse *node = new TIntermIfElse(cond, EnsureBlock(code.node1), EnsureBlock(code.node2));
Olli Etuahocce89652017-06-19 16:04:09 +03002120 node->setLine(loc);
2121
2122 return node;
2123}
2124
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002125void TParseContext::addFullySpecifiedType(TPublicType *typeSpecifier)
2126{
2127 checkPrecisionSpecified(typeSpecifier->getLine(), typeSpecifier->precision,
2128 typeSpecifier->getBasicType());
2129
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002130 if (mShaderVersion < 300 && typeSpecifier->isArray())
Olli Etuaho0e3aee32016-10-27 12:56:38 +01002131 {
2132 error(typeSpecifier->getLine(), "not supported", "first-class array");
2133 typeSpecifier->clearArrayness();
2134 }
2135}
2136
Martin Radev70866b82016-07-22 15:27:42 +03002137TPublicType TParseContext::addFullySpecifiedType(const TTypeQualifierBuilder &typeQualifierBuilder,
Arun Patole7e7e68d2015-05-22 12:02:25 +05302138 const TPublicType &typeSpecifier)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002139{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002140 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002141
Martin Radev70866b82016-07-22 15:27:42 +03002142 TPublicType returnType = typeSpecifier;
2143 returnType.qualifier = typeQualifier.qualifier;
2144 returnType.invariant = typeQualifier.invariant;
2145 returnType.layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03002146 returnType.memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03002147 returnType.precision = typeSpecifier.precision;
2148
2149 if (typeQualifier.precision != EbpUndefined)
2150 {
2151 returnType.precision = typeQualifier.precision;
2152 }
2153
Martin Radev4a9cd802016-09-01 16:51:51 +03002154 checkPrecisionSpecified(typeSpecifier.getLine(), returnType.precision,
2155 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03002156
Martin Radev4a9cd802016-09-01 16:51:51 +03002157 checkInvariantVariableQualifier(returnType.invariant, returnType.qualifier,
2158 typeSpecifier.getLine());
Martin Radev70866b82016-07-22 15:27:42 +03002159
Martin Radev4a9cd802016-09-01 16:51:51 +03002160 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), returnType.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03002161
Jamie Madill6e06b1f2015-05-14 10:01:17 -04002162 if (mShaderVersion < 300)
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002163 {
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002164 if (typeSpecifier.isArray())
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002165 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002166 error(typeSpecifier.getLine(), "not supported", "first-class array");
Olli Etuahoc1ac41b2015-07-10 13:53:46 +03002167 returnType.clearArrayness();
2168 }
2169
Martin Radev70866b82016-07-22 15:27:42 +03002170 if (returnType.qualifier == EvqAttribute &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002171 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002172 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002173 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002174 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002175 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002176
Martin Radev70866b82016-07-22 15:27:42 +03002177 if ((returnType.qualifier == EvqVaryingIn || returnType.qualifier == EvqVaryingOut) &&
Martin Radev4a9cd802016-09-01 16:51:51 +03002178 (typeSpecifier.getBasicType() == EbtBool || typeSpecifier.getBasicType() == EbtInt))
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002179 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002180 error(typeSpecifier.getLine(), "cannot be bool or int",
Martin Radev70866b82016-07-22 15:27:42 +03002181 getQualifierString(returnType.qualifier));
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002182 }
2183 }
2184 else
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002185 {
Martin Radev70866b82016-07-22 15:27:42 +03002186 if (!returnType.layoutQualifier.isEmpty())
Olli Etuahoabb0c382015-07-13 12:01:12 +03002187 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002188 checkIsAtGlobalLevel(typeSpecifier.getLine(), "layout");
Olli Etuahoabb0c382015-07-13 12:01:12 +03002189 }
Martin Radev70866b82016-07-22 15:27:42 +03002190 if (sh::IsVarying(returnType.qualifier) || returnType.qualifier == EvqVertexIn ||
2191 returnType.qualifier == EvqFragmentOut)
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002192 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002193 checkInputOutputTypeIsValidES3(returnType.qualifier, typeSpecifier,
2194 typeSpecifier.getLine());
shannonwoods@chromium.org5703d882013-05-30 00:19:38 +00002195 }
Martin Radev70866b82016-07-22 15:27:42 +03002196 if (returnType.qualifier == EvqComputeIn)
Martin Radev802abe02016-08-04 17:48:32 +03002197 {
Martin Radev4a9cd802016-09-01 16:51:51 +03002198 error(typeSpecifier.getLine(), "'in' can be only used to specify the local group size",
Martin Radev802abe02016-08-04 17:48:32 +03002199 "in");
Martin Radev802abe02016-08-04 17:48:32 +03002200 }
shannonwoods@chromium.org0f376ca2013-05-30 00:19:23 +00002201 }
2202
2203 return returnType;
2204}
2205
Olli Etuaho856c4972016-08-08 11:38:39 +03002206void TParseContext::checkInputOutputTypeIsValidES3(const TQualifier qualifier,
2207 const TPublicType &type,
2208 const TSourceLoc &qualifierLocation)
Olli Etuahocc36b982015-07-10 14:14:18 +03002209{
2210 // An input/output variable can never be bool or a sampler. Samplers are checked elsewhere.
Martin Radev4a9cd802016-09-01 16:51:51 +03002211 if (type.getBasicType() == EbtBool)
Olli Etuahocc36b982015-07-10 14:14:18 +03002212 {
2213 error(qualifierLocation, "cannot be bool", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002214 }
2215
2216 // Specific restrictions apply for vertex shader inputs and fragment shader outputs.
2217 switch (qualifier)
2218 {
2219 case EvqVertexIn:
2220 // ESSL 3.00 section 4.3.4
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002221 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002222 {
2223 error(qualifierLocation, "cannot be array", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002224 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002225 // Vertex inputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002226 return;
2227 case EvqFragmentOut:
2228 // ESSL 3.00 section 4.3.6
Martin Radev4a9cd802016-09-01 16:51:51 +03002229 if (type.typeSpecifierNonArray.isMatrix())
Olli Etuahocc36b982015-07-10 14:14:18 +03002230 {
2231 error(qualifierLocation, "cannot be matrix", getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002232 }
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002233 // Fragment outputs with a struct type are disallowed in nonEmptyDeclarationErrorCheck
Olli Etuahocc36b982015-07-10 14:14:18 +03002234 return;
2235 default:
2236 break;
2237 }
2238
2239 // Vertex shader outputs / fragment shader inputs have a different, slightly more lenient set of
2240 // restrictions.
2241 bool typeContainsIntegers =
Martin Radev4a9cd802016-09-01 16:51:51 +03002242 (type.getBasicType() == EbtInt || type.getBasicType() == EbtUInt ||
2243 type.isStructureContainingType(EbtInt) || type.isStructureContainingType(EbtUInt));
Olli Etuahocc36b982015-07-10 14:14:18 +03002244 if (typeContainsIntegers && qualifier != EvqFlatIn && qualifier != EvqFlatOut)
2245 {
2246 error(qualifierLocation, "must use 'flat' interpolation here",
2247 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002248 }
2249
Martin Radev4a9cd802016-09-01 16:51:51 +03002250 if (type.getBasicType() == EbtStruct)
Olli Etuahocc36b982015-07-10 14:14:18 +03002251 {
2252 // ESSL 3.00 sections 4.3.4 and 4.3.6.
2253 // These restrictions are only implied by the ESSL 3.00 spec, but
2254 // the ESSL 3.10 spec lists these restrictions explicitly.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002255 if (type.isArray())
Olli Etuahocc36b982015-07-10 14:14:18 +03002256 {
2257 error(qualifierLocation, "cannot be an array of structures",
2258 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002259 }
2260 if (type.isStructureContainingArrays())
2261 {
2262 error(qualifierLocation, "cannot be a structure containing an array",
2263 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002264 }
2265 if (type.isStructureContainingType(EbtStruct))
2266 {
2267 error(qualifierLocation, "cannot be a structure containing a structure",
2268 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002269 }
2270 if (type.isStructureContainingType(EbtBool))
2271 {
2272 error(qualifierLocation, "cannot be a structure containing a bool",
2273 getQualifierString(qualifier));
Olli Etuahocc36b982015-07-10 14:14:18 +03002274 }
2275 }
2276}
2277
Martin Radev2cc85b32016-08-05 16:22:53 +03002278void TParseContext::checkLocalVariableConstStorageQualifier(const TQualifierWrapperBase &qualifier)
2279{
2280 if (qualifier.getType() == QtStorage)
2281 {
2282 const TStorageQualifierWrapper &storageQualifier =
2283 static_cast<const TStorageQualifierWrapper &>(qualifier);
2284 if (!declaringFunction() && storageQualifier.getQualifier() != EvqConst &&
2285 !symbolTable.atGlobalLevel())
2286 {
2287 error(storageQualifier.getLine(),
2288 "Local variables can only use the const storage qualifier.",
2289 storageQualifier.getQualifierString().c_str());
2290 }
2291 }
2292}
2293
Olli Etuaho43364892017-02-13 16:00:12 +00002294void TParseContext::checkMemoryQualifierIsNotSpecified(const TMemoryQualifier &memoryQualifier,
Martin Radev2cc85b32016-08-05 16:22:53 +03002295 const TSourceLoc &location)
2296{
Jiajia Qinbc585152017-06-23 15:42:17 +08002297 const std::string reason(
2298 "Only allowed with shader storage blocks, variables declared within shader storage blocks "
2299 "and variables declared as image types.");
Martin Radev2cc85b32016-08-05 16:22:53 +03002300 if (memoryQualifier.readonly)
2301 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002302 error(location, reason.c_str(), "readonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002303 }
2304 if (memoryQualifier.writeonly)
2305 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002306 error(location, reason.c_str(), "writeonly");
Martin Radev2cc85b32016-08-05 16:22:53 +03002307 }
Martin Radev049edfa2016-11-11 14:35:37 +02002308 if (memoryQualifier.coherent)
2309 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002310 error(location, reason.c_str(), "coherent");
Martin Radev049edfa2016-11-11 14:35:37 +02002311 }
2312 if (memoryQualifier.restrictQualifier)
2313 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002314 error(location, reason.c_str(), "restrict");
Martin Radev049edfa2016-11-11 14:35:37 +02002315 }
2316 if (memoryQualifier.volatileQualifier)
2317 {
Jiajia Qinbc585152017-06-23 15:42:17 +08002318 error(location, reason.c_str(), "volatile");
Martin Radev049edfa2016-11-11 14:35:37 +02002319 }
Martin Radev2cc85b32016-08-05 16:22:53 +03002320}
2321
jchen104cdac9e2017-05-08 11:01:20 +08002322// Make sure there is no offset overlapping, and store the newly assigned offset to "type" in
2323// intermediate tree.
Olli Etuaho55bc9052017-10-25 17:33:06 +03002324void TParseContext::checkAtomicCounterOffsetDoesNotOverlap(bool forceAppend,
2325 const TSourceLoc &loc,
2326 TType *type)
jchen104cdac9e2017-05-08 11:01:20 +08002327{
Olli Etuaho55bc9052017-10-25 17:33:06 +03002328 if (!IsAtomicCounter(type->getBasicType()))
2329 {
2330 return;
2331 }
2332
2333 const size_t size = type->isArray() ? kAtomicCounterArrayStride * type->getArraySizeProduct()
2334 : kAtomicCounterSize;
2335 TLayoutQualifier layoutQualifier = type->getLayoutQualifier();
2336 auto &bindingState = mAtomicCounterBindingStates[layoutQualifier.binding];
jchen104cdac9e2017-05-08 11:01:20 +08002337 int offset;
Olli Etuaho55bc9052017-10-25 17:33:06 +03002338 if (layoutQualifier.offset == -1 || forceAppend)
jchen104cdac9e2017-05-08 11:01:20 +08002339 {
2340 offset = bindingState.appendSpan(size);
2341 }
2342 else
2343 {
Olli Etuaho55bc9052017-10-25 17:33:06 +03002344 offset = bindingState.insertSpan(layoutQualifier.offset, size);
jchen104cdac9e2017-05-08 11:01:20 +08002345 }
2346 if (offset == -1)
2347 {
2348 error(loc, "Offset overlapping", "atomic counter");
2349 return;
2350 }
Olli Etuaho55bc9052017-10-25 17:33:06 +03002351 layoutQualifier.offset = offset;
2352 type->setLayoutQualifier(layoutQualifier);
jchen104cdac9e2017-05-08 11:01:20 +08002353}
2354
Olli Etuaho454c34c2017-10-25 16:35:56 +03002355void TParseContext::checkGeometryShaderInputAndSetArraySize(const TSourceLoc &location,
2356 const char *token,
2357 TType *type)
2358{
2359 if (IsGeometryShaderInput(mShaderType, type->getQualifier()))
2360 {
2361 if (type->isArray() && type->getOutermostArraySize() == 0u)
2362 {
2363 // Set size for the unsized geometry shader inputs if they are declared after a valid
2364 // input primitive declaration.
2365 if (mGeometryShaderInputPrimitiveType != EptUndefined)
2366 {
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002367 ASSERT(mGlInVariableWithArraySize != nullptr);
2368 type->sizeOutermostUnsizedArray(
2369 mGlInVariableWithArraySize->getType().getOutermostArraySize());
Olli Etuaho454c34c2017-10-25 16:35:56 +03002370 }
2371 else
2372 {
2373 // [GLSL ES 3.2 SPEC Chapter 4.4.1.2]
2374 // An input can be declared without an array size if there is a previous layout
2375 // which specifies the size.
2376 error(location,
2377 "Missing a valid input primitive declaration before declaring an unsized "
2378 "array input",
2379 token);
2380 }
2381 }
2382 else if (type->isArray())
2383 {
2384 setGeometryShaderInputArraySize(type->getOutermostArraySize(), location);
2385 }
2386 else
2387 {
2388 error(location, "Geometry shader input variable must be declared as an array", token);
2389 }
2390 }
2391}
2392
Olli Etuaho13389b62016-10-16 11:48:18 +01002393TIntermDeclaration *TParseContext::parseSingleDeclaration(
2394 TPublicType &publicType,
2395 const TSourceLoc &identifierOrTypeLocation,
2396 const TString &identifier)
Jamie Madill60ed9812013-06-06 11:56:46 -04002397{
Olli Etuahob60d30f2018-01-16 12:31:06 +02002398 TType *type = new TType(publicType);
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002399 if ((mCompileOptions & SH_FLATTEN_PRAGMA_STDGL_INVARIANT_ALL) &&
2400 mDirectiveHandler.pragma().stdgl.invariantAll)
2401 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002402 TQualifier qualifier = type->getQualifier();
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002403
2404 // The directive handler has already taken care of rejecting invalid uses of this pragma
2405 // (for example, in ESSL 3.00 fragment shaders), so at this point, flatten it into all
2406 // affected variable declarations:
2407 //
2408 // 1. Built-in special variables which are inputs to the fragment shader. (These are handled
2409 // elsewhere, in TranslatorGLSL.)
2410 //
2411 // 2. Outputs from vertex shaders in ESSL 1.00 and 3.00 (EvqVaryingOut and EvqVertexOut). It
2412 // is actually less likely that there will be bugs in the handling of ESSL 3.00 shaders, but
2413 // the way this is currently implemented we have to enable this compiler option before
2414 // parsing the shader and determining the shading language version it uses. If this were
2415 // implemented as a post-pass, the workaround could be more targeted.
2416 //
2417 // 3. Inputs in ESSL 1.00 fragment shaders (EvqVaryingIn). This is somewhat in violation of
2418 // the specification, but there are desktop OpenGL drivers that expect that this is the
2419 // behavior of the #pragma when specified in ESSL 1.00 fragment shaders.
2420 if (qualifier == EvqVaryingOut || qualifier == EvqVertexOut || qualifier == EvqVaryingIn)
2421 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002422 type->setInvariant(true);
Kenneth Russellbccc65d2016-07-19 16:48:43 -07002423 }
2424 }
2425
Olli Etuahob60d30f2018-01-16 12:31:06 +02002426 checkGeometryShaderInputAndSetArraySize(identifierOrTypeLocation, identifier.c_str(), type);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002427
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002428 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2429 identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002430
Olli Etuahobab4c082015-04-24 16:38:49 +03002431 bool emptyDeclaration = (identifier == "");
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002432 mDeferredNonEmptyDeclarationErrorCheck = emptyDeclaration;
Olli Etuahofa33d582015-04-09 14:33:12 +03002433
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002434 TIntermSymbol *symbol = nullptr;
Olli Etuahobab4c082015-04-24 16:38:49 +03002435 if (emptyDeclaration)
2436 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002437 emptyDeclarationErrorCheck(*type, identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002438 // In most cases we don't need to create a symbol node for an empty declaration.
2439 // But if the empty declaration is declaring a struct type, the symbol node will store that.
Olli Etuahob60d30f2018-01-16 12:31:06 +02002440 if (type->getBasicType() == EbtStruct)
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002441 {
Olli Etuaho195be942017-12-04 23:40:14 +02002442 TVariable *emptyVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01002443 new TVariable(&symbolTable, nullptr, type, SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02002444 symbol = new TIntermSymbol(emptyVariable);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002445 }
jchen104cdac9e2017-05-08 11:01:20 +08002446 else if (IsAtomicCounter(publicType.getBasicType()))
2447 {
2448 setAtomicCounterBindingDefaultOffset(publicType, identifierOrTypeLocation);
2449 }
Olli Etuahobab4c082015-04-24 16:38:49 +03002450 }
2451 else
Jamie Madill60ed9812013-06-06 11:56:46 -04002452 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002453 nonEmptyDeclarationErrorCheck(publicType, identifierOrTypeLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002454
Olli Etuahob60d30f2018-01-16 12:31:06 +02002455 checkCanBeDeclaredWithoutInitializer(identifierOrTypeLocation, identifier, type);
Jamie Madill60ed9812013-06-06 11:56:46 -04002456
Olli Etuahob60d30f2018-01-16 12:31:06 +02002457 checkAtomicCounterOffsetDoesNotOverlap(false, identifierOrTypeLocation, type);
jchen104cdac9e2017-05-08 11:01:20 +08002458
Olli Etuaho2935c582015-04-08 14:32:06 +03002459 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002460 if (declareVariable(identifierOrTypeLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002461 {
Olli Etuaho195be942017-12-04 23:40:14 +02002462 symbol = new TIntermSymbol(variable);
Olli Etuaho13389b62016-10-16 11:48:18 +01002463 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002464 }
2465
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002466 TIntermDeclaration *declaration = new TIntermDeclaration();
2467 declaration->setLine(identifierOrTypeLocation);
2468 if (symbol)
2469 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002470 symbol->setLine(identifierOrTypeLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002471 declaration->appendDeclarator(symbol);
2472 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002473 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002474}
2475
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002476TIntermDeclaration *TParseContext::parseSingleArrayDeclaration(
2477 TPublicType &elementType,
2478 const TSourceLoc &identifierLocation,
2479 const TString &identifier,
2480 const TSourceLoc &indexLocation,
2481 const TVector<unsigned int> &arraySizes)
Jamie Madill60ed9812013-06-06 11:56:46 -04002482{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002483 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002484
Olli Etuaho55bde912017-10-25 13:41:13 +03002485 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002486 identifierLocation);
2487
Olli Etuaho55bde912017-10-25 13:41:13 +03002488 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002489
Olli Etuaho55bde912017-10-25 13:41:13 +03002490 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Jamie Madill60ed9812013-06-06 11:56:46 -04002491
Olli Etuahob60d30f2018-01-16 12:31:06 +02002492 TType *arrayType = new TType(elementType);
2493 arrayType->makeArrays(arraySizes);
Jamie Madill60ed9812013-06-06 11:56:46 -04002494
Olli Etuahob60d30f2018-01-16 12:31:06 +02002495 checkGeometryShaderInputAndSetArraySize(indexLocation, identifier.c_str(), arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002496
Olli Etuahob60d30f2018-01-16 12:31:06 +02002497 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002498
Olli Etuahob60d30f2018-01-16 12:31:06 +02002499 checkAtomicCounterOffsetDoesNotOverlap(false, identifierLocation, arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002500
Olli Etuaho13389b62016-10-16 11:48:18 +01002501 TIntermDeclaration *declaration = new TIntermDeclaration();
2502 declaration->setLine(identifierLocation);
2503
Olli Etuaho195be942017-12-04 23:40:14 +02002504 TVariable *variable = nullptr;
2505 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002506 {
Olli Etuaho195be942017-12-04 23:40:14 +02002507 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002508 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002509 declaration->appendDeclarator(symbol);
2510 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002511
Olli Etuaho13389b62016-10-16 11:48:18 +01002512 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002513}
2514
Olli Etuaho13389b62016-10-16 11:48:18 +01002515TIntermDeclaration *TParseContext::parseSingleInitDeclaration(const TPublicType &publicType,
2516 const TSourceLoc &identifierLocation,
2517 const TString &identifier,
2518 const TSourceLoc &initLocation,
2519 TIntermTyped *initializer)
Jamie Madill60ed9812013-06-06 11:56:46 -04002520{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002521 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002522
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002523 declarationQualifierErrorCheck(publicType.qualifier, publicType.layoutQualifier,
2524 identifierLocation);
2525
2526 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
Jamie Madill60ed9812013-06-06 11:56:46 -04002527
Olli Etuaho13389b62016-10-16 11:48:18 +01002528 TIntermDeclaration *declaration = new TIntermDeclaration();
2529 declaration->setLine(identifierLocation);
2530
2531 TIntermBinary *initNode = nullptr;
Olli Etuahob60d30f2018-01-16 12:31:06 +02002532 TType *type = new TType(publicType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002533 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill60ed9812013-06-06 11:56:46 -04002534 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002535 if (initNode)
2536 {
2537 declaration->appendDeclarator(initNode);
2538 }
Jamie Madill60ed9812013-06-06 11:56:46 -04002539 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002540 return declaration;
Jamie Madill60ed9812013-06-06 11:56:46 -04002541}
2542
Olli Etuaho13389b62016-10-16 11:48:18 +01002543TIntermDeclaration *TParseContext::parseSingleArrayInitDeclaration(
Olli Etuaho55bde912017-10-25 13:41:13 +03002544 TPublicType &elementType,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002545 const TSourceLoc &identifierLocation,
2546 const TString &identifier,
2547 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002548 const TVector<unsigned int> &arraySizes,
Jamie Madillb98c3a82015-07-23 14:26:04 -04002549 const TSourceLoc &initLocation,
2550 TIntermTyped *initializer)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002551{
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002552 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002553
Olli Etuaho55bde912017-10-25 13:41:13 +03002554 declarationQualifierErrorCheck(elementType.qualifier, elementType.layoutQualifier,
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002555 identifierLocation);
2556
Olli Etuaho55bde912017-10-25 13:41:13 +03002557 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002558
Olli Etuaho55bde912017-10-25 13:41:13 +03002559 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002560
Olli Etuahob60d30f2018-01-16 12:31:06 +02002561 TType *arrayType = new TType(elementType);
2562 arrayType->makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002563
Olli Etuaho13389b62016-10-16 11:48:18 +01002564 TIntermDeclaration *declaration = new TIntermDeclaration();
2565 declaration->setLine(identifierLocation);
2566
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002567 // initNode will correspond to the whole of "type b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002568 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002569 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002570 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002571 if (initNode)
2572 {
2573 declaration->appendDeclarator(initNode);
2574 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002575 }
Olli Etuaho13389b62016-10-16 11:48:18 +01002576
2577 return declaration;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002578}
2579
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002580TIntermInvariantDeclaration *TParseContext::parseInvariantDeclaration(
Martin Radev70866b82016-07-22 15:27:42 +03002581 const TTypeQualifierBuilder &typeQualifierBuilder,
2582 const TSourceLoc &identifierLoc,
2583 const TString *identifier,
2584 const TSymbol *symbol)
Jamie Madill47e3ec02014-08-20 16:38:33 -04002585{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002586 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002587
Martin Radev70866b82016-07-22 15:27:42 +03002588 if (!typeQualifier.invariant)
2589 {
2590 error(identifierLoc, "Expected invariant", identifier->c_str());
2591 return nullptr;
2592 }
2593 if (!checkIsAtGlobalLevel(identifierLoc, "invariant varying"))
2594 {
2595 return nullptr;
2596 }
Jamie Madill47e3ec02014-08-20 16:38:33 -04002597 if (!symbol)
2598 {
2599 error(identifierLoc, "undeclared identifier declared as invariant", identifier->c_str());
Olli Etuahoe7847b02015-03-16 11:56:12 +02002600 return nullptr;
Jamie Madill47e3ec02014-08-20 16:38:33 -04002601 }
Martin Radev70866b82016-07-22 15:27:42 +03002602 if (!IsQualifierUnspecified(typeQualifier.qualifier))
Jamie Madill47e3ec02014-08-20 16:38:33 -04002603 {
Martin Radev70866b82016-07-22 15:27:42 +03002604 error(identifierLoc, "invariant declaration specifies qualifier",
2605 getQualifierString(typeQualifier.qualifier));
Jamie Madill47e3ec02014-08-20 16:38:33 -04002606 }
Martin Radev70866b82016-07-22 15:27:42 +03002607 if (typeQualifier.precision != EbpUndefined)
2608 {
2609 error(identifierLoc, "invariant declaration specifies precision",
2610 getPrecisionString(typeQualifier.precision));
2611 }
2612 if (!typeQualifier.layoutQualifier.isEmpty())
2613 {
2614 error(identifierLoc, "invariant declaration specifies layout", "'layout'");
2615 }
2616
2617 const TVariable *variable = getNamedVariable(identifierLoc, identifier, symbol);
Olli Etuaho0f684632017-07-13 12:42:15 +03002618 if (!variable)
2619 {
2620 return nullptr;
2621 }
Martin Radev70866b82016-07-22 15:27:42 +03002622 const TType &type = variable->getType();
2623
2624 checkInvariantVariableQualifier(typeQualifier.invariant, type.getQualifier(),
2625 typeQualifier.line);
Olli Etuaho43364892017-02-13 16:00:12 +00002626 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev70866b82016-07-22 15:27:42 +03002627
2628 symbolTable.addInvariantVarying(std::string(identifier->c_str()));
2629
Olli Etuaho195be942017-12-04 23:40:14 +02002630 TIntermSymbol *intermSymbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002631 intermSymbol->setLine(identifierLoc);
Martin Radev70866b82016-07-22 15:27:42 +03002632
Olli Etuahobf4e1b72016-12-09 11:30:15 +00002633 return new TIntermInvariantDeclaration(intermSymbol, identifierLoc);
Jamie Madill47e3ec02014-08-20 16:38:33 -04002634}
2635
Olli Etuaho13389b62016-10-16 11:48:18 +01002636void TParseContext::parseDeclarator(TPublicType &publicType,
2637 const TSourceLoc &identifierLocation,
2638 const TString &identifier,
2639 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002640{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002641 // If the declaration starting this declarator list was empty (example: int,), some checks were
2642 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002643 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002644 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002645 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2646 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002647 }
2648
Olli Etuaho856c4972016-08-08 11:38:39 +03002649 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002650
Olli Etuahob60d30f2018-01-16 12:31:06 +02002651 TType *type = new TType(publicType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002652
Olli Etuahob60d30f2018-01-16 12:31:06 +02002653 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), type);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002654
Olli Etuahob60d30f2018-01-16 12:31:06 +02002655 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, type);
Olli Etuaho55bde912017-10-25 13:41:13 +03002656
Olli Etuahob60d30f2018-01-16 12:31:06 +02002657 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, type);
Olli Etuaho55bc9052017-10-25 17:33:06 +03002658
Olli Etuaho195be942017-12-04 23:40:14 +02002659 TVariable *variable = nullptr;
2660 if (declareVariable(identifierLocation, identifier, type, &variable))
Olli Etuaho13389b62016-10-16 11:48:18 +01002661 {
Olli Etuaho195be942017-12-04 23:40:14 +02002662 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03002663 symbol->setLine(identifierLocation);
Olli Etuaho13389b62016-10-16 11:48:18 +01002664 declarationOut->appendDeclarator(symbol);
2665 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002666}
2667
Olli Etuaho55bde912017-10-25 13:41:13 +03002668void TParseContext::parseArrayDeclarator(TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002669 const TSourceLoc &identifierLocation,
2670 const TString &identifier,
2671 const TSourceLoc &arrayLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002672 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002673 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002674{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002675 // If the declaration starting this declarator list was empty (example: int,), some checks were
2676 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002677 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002678 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002679 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002680 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002681 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002682
Olli Etuaho55bde912017-10-25 13:41:13 +03002683 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002684
Olli Etuaho55bde912017-10-25 13:41:13 +03002685 if (checkIsValidTypeAndQualifierForArray(arrayLocation, elementType))
Jamie Madill502d66f2013-06-20 11:55:52 -04002686 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02002687 TType *arrayType = new TType(elementType);
2688 arrayType->makeArrays(arraySizes);
Olli Etuahoe7847b02015-03-16 11:56:12 +02002689
Olli Etuahob60d30f2018-01-16 12:31:06 +02002690 checkGeometryShaderInputAndSetArraySize(identifierLocation, identifier.c_str(), arrayType);
Olli Etuaho454c34c2017-10-25 16:35:56 +03002691
Olli Etuahob60d30f2018-01-16 12:31:06 +02002692 checkCanBeDeclaredWithoutInitializer(identifierLocation, identifier, arrayType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002693
Olli Etuahob60d30f2018-01-16 12:31:06 +02002694 checkAtomicCounterOffsetDoesNotOverlap(true, identifierLocation, arrayType);
jchen104cdac9e2017-05-08 11:01:20 +08002695
Olli Etuaho6ed7bbe2015-04-07 18:08:46 +03002696 TVariable *variable = nullptr;
Olli Etuaho195be942017-12-04 23:40:14 +02002697 if (declareVariable(identifierLocation, identifier, arrayType, &variable))
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002698 {
Olli Etuaho195be942017-12-04 23:40:14 +02002699 TIntermSymbol *symbol = new TIntermSymbol(variable);
Olli Etuahod7ceaa12017-07-12 17:46:35 +03002700 symbol->setLine(identifierLocation);
2701 declarationOut->appendDeclarator(symbol);
2702 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002703 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002704}
2705
Olli Etuaho13389b62016-10-16 11:48:18 +01002706void TParseContext::parseInitDeclarator(const TPublicType &publicType,
2707 const TSourceLoc &identifierLocation,
2708 const TString &identifier,
2709 const TSourceLoc &initLocation,
2710 TIntermTyped *initializer,
2711 TIntermDeclaration *declarationOut)
Jamie Madill502d66f2013-06-20 11:55:52 -04002712{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002713 // If the declaration starting this declarator list was empty (example: int,), some checks were
2714 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002715 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuahofa33d582015-04-09 14:33:12 +03002716 {
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002717 nonEmptyDeclarationErrorCheck(publicType, identifierLocation);
2718 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuahofa33d582015-04-09 14:33:12 +03002719 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002720
Olli Etuaho856c4972016-08-08 11:38:39 +03002721 checkDeclaratorLocationIsNotSpecified(identifierLocation, publicType);
Jamie Madill0bd18df2013-06-20 11:55:52 -04002722
Olli Etuaho13389b62016-10-16 11:48:18 +01002723 TIntermBinary *initNode = nullptr;
Olli Etuahob60d30f2018-01-16 12:31:06 +02002724 TType *type = new TType(publicType);
Olli Etuaho55bde912017-10-25 13:41:13 +03002725 if (executeInitializer(identifierLocation, identifier, type, initializer, &initNode))
Jamie Madill502d66f2013-06-20 11:55:52 -04002726 {
2727 //
2728 // build the intermediate representation
2729 //
Olli Etuaho13389b62016-10-16 11:48:18 +01002730 if (initNode)
Jamie Madill502d66f2013-06-20 11:55:52 -04002731 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002732 declarationOut->appendDeclarator(initNode);
Jamie Madill502d66f2013-06-20 11:55:52 -04002733 }
Jamie Madill502d66f2013-06-20 11:55:52 -04002734 }
2735}
2736
Olli Etuaho55bde912017-10-25 13:41:13 +03002737void TParseContext::parseArrayInitDeclarator(const TPublicType &elementType,
Olli Etuaho13389b62016-10-16 11:48:18 +01002738 const TSourceLoc &identifierLocation,
2739 const TString &identifier,
2740 const TSourceLoc &indexLocation,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03002741 const TVector<unsigned int> &arraySizes,
Olli Etuaho13389b62016-10-16 11:48:18 +01002742 const TSourceLoc &initLocation,
2743 TIntermTyped *initializer,
2744 TIntermDeclaration *declarationOut)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002745{
Jamie Madillb98c3a82015-07-23 14:26:04 -04002746 // If the declaration starting this declarator list was empty (example: int,), some checks were
2747 // not performed.
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002748 if (mDeferredNonEmptyDeclarationErrorCheck)
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002749 {
Olli Etuaho55bde912017-10-25 13:41:13 +03002750 nonEmptyDeclarationErrorCheck(elementType, identifierLocation);
Olli Etuahobb7e5a72017-04-24 10:16:44 +03002751 mDeferredNonEmptyDeclarationErrorCheck = false;
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002752 }
2753
Olli Etuaho55bde912017-10-25 13:41:13 +03002754 checkDeclaratorLocationIsNotSpecified(identifierLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002755
Olli Etuaho55bde912017-10-25 13:41:13 +03002756 checkIsValidTypeAndQualifierForArray(indexLocation, elementType);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002757
Olli Etuahob60d30f2018-01-16 12:31:06 +02002758 TType *arrayType = new TType(elementType);
2759 arrayType->makeArrays(arraySizes);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002760
2761 // initNode will correspond to the whole of "b[n] = initializer".
Olli Etuaho13389b62016-10-16 11:48:18 +01002762 TIntermBinary *initNode = nullptr;
Olli Etuaho914b79a2017-06-19 16:03:19 +03002763 if (executeInitializer(identifierLocation, identifier, arrayType, initializer, &initNode))
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002764 {
2765 if (initNode)
2766 {
Olli Etuaho13389b62016-10-16 11:48:18 +01002767 declarationOut->appendDeclarator(initNode);
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002768 }
Olli Etuaho3875ffd2015-04-10 16:45:14 +03002769 }
2770}
2771
Olli Etuahob8ee9dd2017-10-30 12:43:27 +02002772TIntermNode *TParseContext::addEmptyStatement(const TSourceLoc &location)
2773{
2774 // It's simpler to parse an empty statement as a constant expression rather than having a
2775 // different type of node just for empty statements, that will be pruned from the AST anyway.
2776 TIntermNode *node = CreateZeroNode(TType(EbtInt, EbpMedium));
2777 node->setLine(location);
2778 return node;
2779}
2780
jchen104cdac9e2017-05-08 11:01:20 +08002781void TParseContext::setAtomicCounterBindingDefaultOffset(const TPublicType &publicType,
2782 const TSourceLoc &location)
2783{
2784 const TLayoutQualifier &layoutQualifier = publicType.layoutQualifier;
2785 checkAtomicCounterBindingIsValid(location, layoutQualifier.binding);
2786 if (layoutQualifier.binding == -1 || layoutQualifier.offset == -1)
2787 {
2788 error(location, "Requires both binding and offset", "layout");
2789 return;
2790 }
2791 mAtomicCounterBindingStates[layoutQualifier.binding].setDefaultOffset(layoutQualifier.offset);
2792}
2793
Olli Etuahocce89652017-06-19 16:04:09 +03002794void TParseContext::parseDefaultPrecisionQualifier(const TPrecision precision,
2795 const TPublicType &type,
2796 const TSourceLoc &loc)
2797{
2798 if ((precision == EbpHigh) && (getShaderType() == GL_FRAGMENT_SHADER) &&
2799 !getFragmentPrecisionHigh())
2800 {
2801 error(loc, "precision is not supported in fragment shader", "highp");
2802 }
2803
2804 if (!CanSetDefaultPrecisionOnType(type))
2805 {
2806 error(loc, "illegal type argument for default precision qualifier",
2807 getBasicString(type.getBasicType()));
2808 return;
2809 }
2810 symbolTable.setDefaultPrecision(type.getBasicType(), precision);
2811}
2812
Shaob5cc1192017-07-06 10:47:20 +08002813bool TParseContext::checkPrimitiveTypeMatchesTypeQualifier(const TTypeQualifier &typeQualifier)
2814{
2815 switch (typeQualifier.layoutQualifier.primitiveType)
2816 {
2817 case EptLines:
2818 case EptLinesAdjacency:
2819 case EptTriangles:
2820 case EptTrianglesAdjacency:
2821 return typeQualifier.qualifier == EvqGeometryIn;
2822
2823 case EptLineStrip:
2824 case EptTriangleStrip:
2825 return typeQualifier.qualifier == EvqGeometryOut;
2826
2827 case EptPoints:
2828 return true;
2829
2830 default:
2831 UNREACHABLE();
2832 return false;
2833 }
2834}
2835
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002836void TParseContext::setGeometryShaderInputArraySize(unsigned int inputArraySize,
2837 const TSourceLoc &line)
Jiawei Shaod8105a02017-08-08 09:54:36 +08002838{
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002839 if (mGlInVariableWithArraySize == nullptr)
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002840 {
Olli Etuahodd21ecf2018-01-10 12:42:09 +02002841 const TSymbol *glPerVertex = symbolTable.findBuiltIn("gl_PerVertex", 310);
2842 const TInterfaceBlock *glPerVertexBlock = static_cast<const TInterfaceBlock *>(glPerVertex);
Olli Etuahob60d30f2018-01-16 12:31:06 +02002843 TType *glInType = new TType(glPerVertexBlock, EvqPerVertexIn, TLayoutQualifier::Create());
2844 glInType->makeArray(inputArraySize);
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002845 mGlInVariableWithArraySize =
2846 new TVariable(&symbolTable, NewPoolTString("gl_in"), glInType, SymbolType::BuiltIn,
2847 TExtension::EXT_geometry_shader);
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002848 }
Olli Etuahoc74ec1a2018-01-09 15:23:28 +02002849 else if (mGlInVariableWithArraySize->getType().getOutermostArraySize() != inputArraySize)
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002850 {
2851 error(line,
2852 "Array size or input primitive declaration doesn't match the size of earlier sized "
2853 "array inputs.",
2854 "layout");
2855 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08002856}
2857
Shaob5cc1192017-07-06 10:47:20 +08002858bool TParseContext::parseGeometryShaderInputLayoutQualifier(const TTypeQualifier &typeQualifier)
2859{
2860 ASSERT(typeQualifier.qualifier == EvqGeometryIn);
2861
2862 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2863
2864 if (layoutQualifier.maxVertices != -1)
2865 {
2866 error(typeQualifier.line,
2867 "max_vertices can only be declared in 'out' layout in a geometry shader", "layout");
2868 return false;
2869 }
2870
2871 // Set mGeometryInputPrimitiveType if exists
2872 if (layoutQualifier.primitiveType != EptUndefined)
2873 {
2874 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2875 {
2876 error(typeQualifier.line, "invalid primitive type for 'in' layout", "layout");
2877 return false;
2878 }
2879
2880 if (mGeometryShaderInputPrimitiveType == EptUndefined)
2881 {
2882 mGeometryShaderInputPrimitiveType = layoutQualifier.primitiveType;
Jiawei Shao8e4b3552017-08-30 14:20:58 +08002883 setGeometryShaderInputArraySize(
2884 GetGeometryShaderInputArraySize(mGeometryShaderInputPrimitiveType),
2885 typeQualifier.line);
Shaob5cc1192017-07-06 10:47:20 +08002886 }
2887 else if (mGeometryShaderInputPrimitiveType != layoutQualifier.primitiveType)
2888 {
2889 error(typeQualifier.line, "primitive doesn't match earlier input primitive declaration",
2890 "layout");
2891 return false;
2892 }
2893 }
2894
2895 // Set mGeometryInvocations if exists
2896 if (layoutQualifier.invocations > 0)
2897 {
2898 if (mGeometryShaderInvocations == 0)
2899 {
2900 mGeometryShaderInvocations = layoutQualifier.invocations;
2901 }
2902 else if (mGeometryShaderInvocations != layoutQualifier.invocations)
2903 {
2904 error(typeQualifier.line, "invocations contradicts to the earlier declaration",
2905 "layout");
2906 return false;
2907 }
2908 }
2909
2910 return true;
2911}
2912
2913bool TParseContext::parseGeometryShaderOutputLayoutQualifier(const TTypeQualifier &typeQualifier)
2914{
2915 ASSERT(typeQualifier.qualifier == EvqGeometryOut);
2916
2917 const TLayoutQualifier &layoutQualifier = typeQualifier.layoutQualifier;
2918
2919 if (layoutQualifier.invocations > 0)
2920 {
2921 error(typeQualifier.line,
2922 "invocations can only be declared in 'in' layout in a geometry shader", "layout");
2923 return false;
2924 }
2925
2926 // Set mGeometryOutputPrimitiveType if exists
2927 if (layoutQualifier.primitiveType != EptUndefined)
2928 {
2929 if (!checkPrimitiveTypeMatchesTypeQualifier(typeQualifier))
2930 {
2931 error(typeQualifier.line, "invalid primitive type for 'out' layout", "layout");
2932 return false;
2933 }
2934
2935 if (mGeometryShaderOutputPrimitiveType == EptUndefined)
2936 {
2937 mGeometryShaderOutputPrimitiveType = layoutQualifier.primitiveType;
2938 }
2939 else if (mGeometryShaderOutputPrimitiveType != layoutQualifier.primitiveType)
2940 {
2941 error(typeQualifier.line,
2942 "primitive doesn't match earlier output primitive declaration", "layout");
2943 return false;
2944 }
2945 }
2946
2947 // Set mGeometryMaxVertices if exists
2948 if (layoutQualifier.maxVertices > -1)
2949 {
2950 if (mGeometryShaderMaxVertices == -1)
2951 {
2952 mGeometryShaderMaxVertices = layoutQualifier.maxVertices;
2953 }
2954 else if (mGeometryShaderMaxVertices != layoutQualifier.maxVertices)
2955 {
2956 error(typeQualifier.line, "max_vertices contradicts to the earlier declaration",
2957 "layout");
2958 return false;
2959 }
2960 }
2961
2962 return true;
2963}
2964
Martin Radev70866b82016-07-22 15:27:42 +03002965void TParseContext::parseGlobalLayoutQualifier(const TTypeQualifierBuilder &typeQualifierBuilder)
Jamie Madilla295edf2013-06-06 11:56:48 -04002966{
Olli Etuaho77ba4082016-12-16 12:01:18 +00002967 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madilla295edf2013-06-06 11:56:48 -04002968 const TLayoutQualifier layoutQualifier = typeQualifier.layoutQualifier;
Jamie Madillc2128ff2016-07-04 10:26:17 -04002969
Martin Radev70866b82016-07-22 15:27:42 +03002970 checkInvariantVariableQualifier(typeQualifier.invariant, typeQualifier.qualifier,
2971 typeQualifier.line);
2972
Jamie Madillc2128ff2016-07-04 10:26:17 -04002973 // It should never be the case, but some strange parser errors can send us here.
2974 if (layoutQualifier.isEmpty())
2975 {
2976 error(typeQualifier.line, "Error during layout qualifier parsing.", "?");
Jamie Madillc2128ff2016-07-04 10:26:17 -04002977 return;
2978 }
Jamie Madilla295edf2013-06-06 11:56:48 -04002979
Martin Radev802abe02016-08-04 17:48:32 +03002980 if (!layoutQualifier.isCombinationValid())
Jamie Madilla295edf2013-06-06 11:56:48 -04002981 {
Olli Etuaho43364892017-02-13 16:00:12 +00002982 error(typeQualifier.line, "invalid layout qualifier combination", "layout");
Jamie Madilla295edf2013-06-06 11:56:48 -04002983 return;
2984 }
2985
Olli Etuaho43364892017-02-13 16:00:12 +00002986 checkBindingIsNotSpecified(typeQualifier.line, layoutQualifier.binding);
2987
2988 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
Martin Radev2cc85b32016-08-05 16:22:53 +03002989
2990 checkInternalFormatIsNotSpecified(typeQualifier.line, layoutQualifier.imageInternalFormat);
2991
Andrei Volykhina5527072017-03-22 16:46:30 +03002992 checkYuvIsNotSpecified(typeQualifier.line, layoutQualifier.yuv);
2993
jchen104cdac9e2017-05-08 11:01:20 +08002994 checkOffsetIsNotSpecified(typeQualifier.line, layoutQualifier.offset);
2995
Qin Jiajiaca68d982017-09-18 16:41:56 +08002996 checkStd430IsForShaderStorageBlock(typeQualifier.line, layoutQualifier.blockStorage,
2997 typeQualifier.qualifier);
2998
Martin Radev802abe02016-08-04 17:48:32 +03002999 if (typeQualifier.qualifier == EvqComputeIn)
Jamie Madilla295edf2013-06-06 11:56:48 -04003000 {
Martin Radev802abe02016-08-04 17:48:32 +03003001 if (mComputeShaderLocalSizeDeclared &&
3002 !layoutQualifier.isLocalSizeEqual(mComputeShaderLocalSize))
3003 {
3004 error(typeQualifier.line, "Work group size does not match the previous declaration",
3005 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003006 return;
3007 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003008
Martin Radev802abe02016-08-04 17:48:32 +03003009 if (mShaderVersion < 310)
3010 {
3011 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003012 return;
3013 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003014
Martin Radev4c4c8e72016-08-04 12:25:34 +03003015 if (!layoutQualifier.localSize.isAnyValueSet())
Martin Radev802abe02016-08-04 17:48:32 +03003016 {
3017 error(typeQualifier.line, "No local work group size specified", "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003018 return;
3019 }
3020
3021 const TVariable *maxComputeWorkGroupSize = static_cast<const TVariable *>(
3022 symbolTable.findBuiltIn("gl_MaxComputeWorkGroupSize", mShaderVersion));
3023
3024 const TConstantUnion *maxComputeWorkGroupSizeData =
3025 maxComputeWorkGroupSize->getConstPointer();
3026
3027 for (size_t i = 0u; i < layoutQualifier.localSize.size(); ++i)
3028 {
3029 if (layoutQualifier.localSize[i] != -1)
3030 {
3031 mComputeShaderLocalSize[i] = layoutQualifier.localSize[i];
3032 const int maxComputeWorkGroupSizeValue = maxComputeWorkGroupSizeData[i].getIConst();
3033 if (mComputeShaderLocalSize[i] < 1 ||
3034 mComputeShaderLocalSize[i] > maxComputeWorkGroupSizeValue)
3035 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003036 std::stringstream reasonStream;
3037 reasonStream << "invalid value: Value must be at least 1 and no greater than "
3038 << maxComputeWorkGroupSizeValue;
3039 const std::string &reason = reasonStream.str();
Martin Radev802abe02016-08-04 17:48:32 +03003040
Olli Etuaho4de340a2016-12-16 09:32:03 +00003041 error(typeQualifier.line, reason.c_str(), getWorkGroupSizeString(i));
Martin Radev802abe02016-08-04 17:48:32 +03003042 return;
3043 }
3044 }
3045 }
3046
3047 mComputeShaderLocalSizeDeclared = true;
3048 }
Shaob5cc1192017-07-06 10:47:20 +08003049 else if (typeQualifier.qualifier == EvqGeometryIn)
3050 {
3051 if (mShaderVersion < 310)
3052 {
3053 error(typeQualifier.line, "in type qualifier supported in GLSL ES 3.10 only", "layout");
3054 return;
3055 }
3056
3057 if (!parseGeometryShaderInputLayoutQualifier(typeQualifier))
3058 {
3059 return;
3060 }
3061 }
3062 else if (typeQualifier.qualifier == EvqGeometryOut)
3063 {
3064 if (mShaderVersion < 310)
3065 {
3066 error(typeQualifier.line, "out type qualifier supported in GLSL ES 3.10 only",
3067 "layout");
3068 return;
3069 }
3070
3071 if (!parseGeometryShaderOutputLayoutQualifier(typeQualifier))
3072 {
3073 return;
3074 }
3075 }
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03003076 else if (isExtensionEnabled(TExtension::OVR_multiview) &&
3077 typeQualifier.qualifier == EvqVertexIn)
Olli Etuaho09b04a22016-12-15 13:30:26 +00003078 {
3079 // This error is only specified in WebGL, but tightens unspecified behavior in the native
3080 // specification.
3081 if (mNumViews != -1 && layoutQualifier.numViews != mNumViews)
3082 {
3083 error(typeQualifier.line, "Number of views does not match the previous declaration",
3084 "layout");
3085 return;
3086 }
3087
3088 if (layoutQualifier.numViews == -1)
3089 {
3090 error(typeQualifier.line, "No num_views specified", "layout");
3091 return;
3092 }
3093
3094 if (layoutQualifier.numViews > mMaxNumViews)
3095 {
3096 error(typeQualifier.line, "num_views greater than the value of GL_MAX_VIEWS_OVR",
3097 "layout");
3098 return;
3099 }
3100
3101 mNumViews = layoutQualifier.numViews;
3102 }
Martin Radev802abe02016-08-04 17:48:32 +03003103 else
Jamie Madill1566ef72013-06-20 11:55:54 -04003104 {
Olli Etuaho09b04a22016-12-15 13:30:26 +00003105 if (!checkWorkGroupSizeIsNotSpecified(typeQualifier.line, layoutQualifier))
Martin Radev802abe02016-08-04 17:48:32 +03003106 {
Martin Radev802abe02016-08-04 17:48:32 +03003107 return;
3108 }
3109
Jiajia Qinbc585152017-06-23 15:42:17 +08003110 if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
Martin Radev802abe02016-08-04 17:48:32 +03003111 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003112 error(typeQualifier.line, "invalid qualifier: global layout can only be set for blocks",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003113 getQualifierString(typeQualifier.qualifier));
Martin Radev802abe02016-08-04 17:48:32 +03003114 return;
3115 }
3116
3117 if (mShaderVersion < 300)
3118 {
3119 error(typeQualifier.line, "layout qualifiers supported in GLSL ES 3.00 and above",
3120 "layout");
Martin Radev802abe02016-08-04 17:48:32 +03003121 return;
3122 }
3123
Olli Etuaho09b04a22016-12-15 13:30:26 +00003124 checkLocationIsNotSpecified(typeQualifier.line, layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003125
3126 if (layoutQualifier.matrixPacking != EmpUnspecified)
3127 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003128 if (typeQualifier.qualifier == EvqUniform)
3129 {
3130 mDefaultUniformMatrixPacking = layoutQualifier.matrixPacking;
3131 }
3132 else if (typeQualifier.qualifier == EvqBuffer)
3133 {
3134 mDefaultBufferMatrixPacking = layoutQualifier.matrixPacking;
3135 }
Martin Radev802abe02016-08-04 17:48:32 +03003136 }
3137
3138 if (layoutQualifier.blockStorage != EbsUnspecified)
3139 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003140 if (typeQualifier.qualifier == EvqUniform)
3141 {
3142 mDefaultUniformBlockStorage = layoutQualifier.blockStorage;
3143 }
3144 else if (typeQualifier.qualifier == EvqBuffer)
3145 {
3146 mDefaultBufferBlockStorage = layoutQualifier.blockStorage;
3147 }
Martin Radev802abe02016-08-04 17:48:32 +03003148 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003149 }
Jamie Madilla295edf2013-06-06 11:56:48 -04003150}
3151
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003152TIntermFunctionPrototype *TParseContext::createPrototypeNodeFromFunction(
3153 const TFunction &function,
3154 const TSourceLoc &location,
3155 bool insertParametersToSymbolTable)
3156{
Olli Etuahobed35d72017-12-20 16:36:26 +02003157 checkIsNotReserved(location, function.name());
Olli Etuahod7cd4ae2017-07-06 15:52:49 +03003158
Olli Etuahobeb6dc72017-12-14 16:03:03 +02003159 TIntermFunctionPrototype *prototype = new TIntermFunctionPrototype(&function);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003160 prototype->setLine(location);
3161
3162 for (size_t i = 0; i < function.getParamCount(); i++)
3163 {
3164 const TConstParameter &param = function.getParam(i);
3165
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003166 TIntermSymbol *symbol = nullptr;
3167
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003168 // If the parameter has no name, it's not an error, just don't add it to symbol table (could
3169 // be used for unused args).
3170 if (param.name != nullptr)
3171 {
Olli Etuaho195be942017-12-04 23:40:14 +02003172 TVariable *variable =
Olli Etuahob60d30f2018-01-16 12:31:06 +02003173 new TVariable(&symbolTable, param.name, param.type, SymbolType::UserDefined);
Olli Etuaho195be942017-12-04 23:40:14 +02003174 symbol = new TIntermSymbol(variable);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003175 // Insert the parameter in the symbol table.
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003176 if (insertParametersToSymbolTable)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003177 {
Olli Etuaho195be942017-12-04 23:40:14 +02003178 if (!symbolTable.declareVariable(variable))
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003179 {
Olli Etuaho85d624a2017-08-07 13:42:33 +03003180 error(location, "redefinition", param.name->c_str());
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003181 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003182 }
Olli Etuaho55bde912017-10-25 13:41:13 +03003183 // Unsized type of a named parameter should have already been checked and sanitized.
3184 ASSERT(!param.type->isUnsizedArray());
3185 }
3186 else
3187 {
3188 if (param.type->isUnsizedArray())
3189 {
3190 error(location, "function parameter array must be sized at compile time", "[]");
3191 // We don't need to size the arrays since the parameter is unnamed and hence
3192 // inaccessible.
3193 }
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003194 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003195 if (!symbol)
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003196 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003197 // The parameter had no name or declaring the symbol failed - either way, add a nameless
3198 // symbol.
Olli Etuaho195be942017-12-04 23:40:14 +02003199 TVariable *emptyVariable =
Olli Etuahob60d30f2018-01-16 12:31:06 +02003200 new TVariable(&symbolTable, nullptr, param.type, SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02003201 symbol = new TIntermSymbol(emptyVariable);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003202 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03003203 symbol->setLine(location);
3204 prototype->appendParameter(symbol);
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003205 }
3206 return prototype;
3207}
3208
Olli Etuaho16c745a2017-01-16 17:02:27 +00003209TIntermFunctionPrototype *TParseContext::addFunctionPrototypeDeclaration(
3210 const TFunction &parsedFunction,
3211 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003212{
Olli Etuaho476197f2016-10-11 13:59:08 +01003213 // Note: function found from the symbol table could be the same as parsedFunction if this is the
3214 // first declaration. Either way the instance in the symbol table is used to track whether the
3215 // function is declared multiple times.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003216 bool hadPrototypeDeclaration = false;
3217 const TFunction *function = symbolTable.markUserDefinedFunctionHasPrototypeDeclaration(
3218 parsedFunction.getMangledName(), &hadPrototypeDeclaration);
3219
3220 if (hadPrototypeDeclaration && mShaderVersion == 100)
Olli Etuaho5d653182016-01-04 14:43:28 +02003221 {
3222 // ESSL 1.00.17 section 4.2.7.
3223 // Doesn't apply to ESSL 3.00.4: see section 4.2.3.
3224 error(location, "duplicate function prototype declarations are not allowed", "function");
Olli Etuaho5d653182016-01-04 14:43:28 +02003225 }
Olli Etuaho5d653182016-01-04 14:43:28 +02003226
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003227 TIntermFunctionPrototype *prototype =
3228 createPrototypeNodeFromFunction(*function, location, false);
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003229
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003230 symbolTable.pop();
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003231
3232 if (!symbolTable.atGlobalLevel())
3233 {
3234 // ESSL 3.00.4 section 4.2.4.
3235 error(location, "local function prototype declarations are not allowed", "function");
Olli Etuaho8d8b1082016-01-04 16:44:57 +02003236 }
3237
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003238 return prototype;
3239}
3240
Olli Etuaho336b1472016-10-05 16:37:55 +01003241TIntermFunctionDefinition *TParseContext::addFunctionDefinition(
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003242 TIntermFunctionPrototype *functionPrototype,
Olli Etuaho336b1472016-10-05 16:37:55 +01003243 TIntermBlock *functionBody,
3244 const TSourceLoc &location)
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003245{
Olli Etuahof51fdd22016-10-03 10:03:40 +01003246 // Check that non-void functions have at least one return statement.
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003247 if (mCurrentFunctionType->getBasicType() != EbtVoid && !mFunctionReturnsValue)
3248 {
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003249 error(location, "function does not return a value:",
Olli Etuahobed35d72017-12-20 16:36:26 +02003250 functionPrototype->getFunction()->name().c_str());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003251 }
3252
Olli Etuahof51fdd22016-10-03 10:03:40 +01003253 if (functionBody == nullptr)
3254 {
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01003255 functionBody = new TIntermBlock();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003256 functionBody->setLine(location);
3257 }
Olli Etuaho336b1472016-10-05 16:37:55 +01003258 TIntermFunctionDefinition *functionNode =
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003259 new TIntermFunctionDefinition(functionPrototype, functionBody);
Olli Etuaho336b1472016-10-05 16:37:55 +01003260 functionNode->setLine(location);
Olli Etuahof51fdd22016-10-03 10:03:40 +01003261
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003262 symbolTable.pop();
Olli Etuahof51fdd22016-10-03 10:03:40 +01003263 return functionNode;
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003264}
3265
Olli Etuaho476197f2016-10-11 13:59:08 +01003266void TParseContext::parseFunctionDefinitionHeader(const TSourceLoc &location,
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003267 const TFunction *function,
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003268 TIntermFunctionPrototype **prototypeOut)
Jamie Madill185fb402015-06-12 15:48:48 -04003269{
Olli Etuaho476197f2016-10-11 13:59:08 +01003270 ASSERT(function);
Jamie Madillb98c3a82015-07-23 14:26:04 -04003271 const TSymbol *builtIn =
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003272 symbolTable.findBuiltIn(function->getMangledName(), getShaderVersion());
Jamie Madill185fb402015-06-12 15:48:48 -04003273
3274 if (builtIn)
3275 {
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003276 error(location, "built-in functions cannot be redefined", function->name().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003277 }
Olli Etuaho476197f2016-10-11 13:59:08 +01003278 else
Jamie Madill185fb402015-06-12 15:48:48 -04003279 {
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003280 bool wasDefined = false;
3281 function =
3282 symbolTable.setUserDefinedFunctionParameterNamesFromDefinition(function, &wasDefined);
3283 if (wasDefined)
Olli Etuaho476197f2016-10-11 13:59:08 +01003284 {
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003285 error(location, "function already has a body", function->name().c_str());
Olli Etuaho476197f2016-10-11 13:59:08 +01003286 }
Jamie Madill185fb402015-06-12 15:48:48 -04003287 }
Jamie Madill185fb402015-06-12 15:48:48 -04003288
Olli Etuaho8ad9e752017-01-16 19:55:20 +00003289 // Remember the return type for later checking for return statements.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003290 mCurrentFunctionType = &(function->getReturnType());
Olli Etuahoee63f5d2016-01-04 11:34:54 +02003291 mFunctionReturnsValue = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003292
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003293 *prototypeOut = createPrototypeNodeFromFunction(*function, location, true);
Jamie Madill185fb402015-06-12 15:48:48 -04003294 setLoopNestingLevel(0);
3295}
3296
Jamie Madillb98c3a82015-07-23 14:26:04 -04003297TFunction *TParseContext::parseFunctionDeclarator(const TSourceLoc &location, TFunction *function)
Jamie Madill185fb402015-06-12 15:48:48 -04003298{
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003299 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003300 // We don't know at this point whether this is a function definition or a prototype.
3301 // The definition production code will check for redefinitions.
3302 // In the case of ESSL 1.00 the prototype production code will also check for redeclarations.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003303 //
Olli Etuaho5d653182016-01-04 14:43:28 +02003304 // Return types and parameter qualifiers must match in all redeclarations, so those are checked
3305 // here.
Geoff Lang13e7c7e2015-07-30 14:17:29 +00003306 //
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003307 const TFunction *prevDec = static_cast<const TFunction *>(
3308 symbolTable.find(function->getMangledName(), getShaderVersion()));
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303309
Olli Etuahod80f2942017-11-06 12:44:45 +02003310 for (size_t i = 0u; i < function->getParamCount(); ++i)
3311 {
3312 auto &param = function->getParam(i);
3313 if (param.type->isStructSpecifier())
3314 {
3315 // ESSL 3.00.6 section 12.10.
3316 error(location, "Function parameter type cannot be a structure definition",
Olli Etuahobed35d72017-12-20 16:36:26 +02003317 function->name().c_str());
Olli Etuahod80f2942017-11-06 12:44:45 +02003318 }
3319 }
3320
Olli Etuaho54a29ff2017-11-28 17:35:20 +02003321 if (getShaderVersion() >= 300 && symbolTable.hasUnmangledBuiltInForShaderVersion(
Olli Etuahobed35d72017-12-20 16:36:26 +02003322 function->name().c_str(), getShaderVersion()))
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303323 {
Martin Radevda6254b2016-12-14 17:00:36 +02003324 // With ESSL 3.00 and above, names of built-in functions cannot be redeclared as functions.
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303325 // Therefore overloading or redefining builtin functions is an error.
3326 error(location, "Name of a built-in function cannot be redeclared as function",
Olli Etuahobed35d72017-12-20 16:36:26 +02003327 function->name().c_str());
Olli Etuahoc4a96d62015-07-23 17:37:39 +05303328 }
3329 else if (prevDec)
Jamie Madill185fb402015-06-12 15:48:48 -04003330 {
3331 if (prevDec->getReturnType() != function->getReturnType())
3332 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003333 error(location, "function must have the same return type in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003334 function->getReturnType().getBasicString());
Jamie Madill185fb402015-06-12 15:48:48 -04003335 }
3336 for (size_t i = 0; i < prevDec->getParamCount(); ++i)
3337 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003338 if (prevDec->getParam(i).type->getQualifier() !=
3339 function->getParam(i).type->getQualifier())
Jamie Madill185fb402015-06-12 15:48:48 -04003340 {
Olli Etuaho476197f2016-10-11 13:59:08 +01003341 error(location,
3342 "function must have the same parameter qualifiers in all of its declarations",
Jamie Madill185fb402015-06-12 15:48:48 -04003343 function->getParam(i).type->getQualifierString());
Jamie Madill185fb402015-06-12 15:48:48 -04003344 }
3345 }
3346 }
3347
Jamie Madill185fb402015-06-12 15:48:48 -04003348 // Check for previously declared variables using the same name.
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003349 const TSymbol *prevSym = symbolTable.find(function->name(), getShaderVersion());
3350 bool insertUnmangledName = true;
Jamie Madill185fb402015-06-12 15:48:48 -04003351 if (prevSym)
3352 {
3353 if (!prevSym->isFunction())
3354 {
Olli Etuahobed35d72017-12-20 16:36:26 +02003355 error(location, "redefinition of a function", function->name().c_str());
Jamie Madill185fb402015-06-12 15:48:48 -04003356 }
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003357 insertUnmangledName = false;
Jamie Madill185fb402015-06-12 15:48:48 -04003358 }
Olli Etuahodd21ecf2018-01-10 12:42:09 +02003359 // Parsing is at the inner scope level of the function's arguments and body statement at this
3360 // point, but declareUserDefinedFunction takes care of declaring the function at the global
3361 // scope.
3362 symbolTable.declareUserDefinedFunction(function, insertUnmangledName);
Jamie Madill185fb402015-06-12 15:48:48 -04003363
Olli Etuaho78d13742017-01-18 13:06:10 +00003364 // Raise error message if main function takes any parameters or return anything other than void
Olli Etuahobed35d72017-12-20 16:36:26 +02003365 if (function->name() == "main")
Olli Etuaho78d13742017-01-18 13:06:10 +00003366 {
3367 if (function->getParamCount() > 0)
3368 {
3369 error(location, "function cannot take any parameter(s)", "main");
3370 }
3371 if (function->getReturnType().getBasicType() != EbtVoid)
3372 {
3373 error(location, "main function cannot return a value",
3374 function->getReturnType().getBasicString());
3375 }
3376 }
3377
Jamie Madill185fb402015-06-12 15:48:48 -04003378 //
Jamie Madillb98c3a82015-07-23 14:26:04 -04003379 // If this is a redeclaration, it could also be a definition, in which case, we want to use the
3380 // variable names from this one, and not the one that's
Jamie Madill185fb402015-06-12 15:48:48 -04003381 // being redeclared. So, pass back up this declaration, not the one in the symbol table.
3382 //
3383 return function;
3384}
3385
Olli Etuaho9de84a52016-06-14 17:36:01 +03003386TFunction *TParseContext::parseFunctionHeader(const TPublicType &type,
3387 const TString *name,
3388 const TSourceLoc &location)
3389{
3390 if (type.qualifier != EvqGlobal && type.qualifier != EvqTemporary)
3391 {
3392 error(location, "no qualifiers allowed for function return",
3393 getQualifierString(type.qualifier));
Olli Etuaho9de84a52016-06-14 17:36:01 +03003394 }
3395 if (!type.layoutQualifier.isEmpty())
3396 {
3397 error(location, "no qualifiers allowed for function return", "layout");
Olli Etuaho9de84a52016-06-14 17:36:01 +03003398 }
jchen10cc2a10e2017-05-03 14:05:12 +08003399 // make sure an opaque type is not involved as well...
3400 std::string reason(getBasicString(type.getBasicType()));
3401 reason += "s can't be function return values";
3402 checkIsNotOpaqueType(location, type.typeSpecifierNonArray, reason.c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003403 if (mShaderVersion < 300)
3404 {
3405 // Array return values are forbidden, but there's also no valid syntax for declaring array
3406 // return values in ESSL 1.00.
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003407 ASSERT(!type.isArray() || mDiagnostics->numErrors() > 0);
Olli Etuahoe29324f2016-06-15 10:58:03 +03003408
3409 if (type.isStructureContainingArrays())
3410 {
3411 // ESSL 1.00.17 section 6.1 Function Definitions
3412 error(location, "structures containing arrays can't be function return values",
3413 TType(type).getCompleteString().c_str());
Olli Etuahoe29324f2016-06-15 10:58:03 +03003414 }
3415 }
Olli Etuaho9de84a52016-06-14 17:36:01 +03003416
3417 // Add the function as a prototype after parsing it (we do not support recursion)
Olli Etuaho0c371002017-12-13 17:00:25 +04003418 return new TFunction(&symbolTable, name, new TType(type), SymbolType::UserDefined, false);
Olli Etuaho9de84a52016-06-14 17:36:01 +03003419}
3420
Olli Etuahocce89652017-06-19 16:04:09 +03003421TFunction *TParseContext::addNonConstructorFunc(const TString *name, const TSourceLoc &loc)
3422{
Kai Ninomiya614dd0f2017-11-22 14:04:48 -08003423 const TType *returnType = StaticType::GetQualified<EbtVoid, EvqTemporary>();
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003424 // TODO(oetuaho): Some more appropriate data structure than TFunction could be used here. We're
3425 // really only interested in the mangled name of the function to look up the actual function
3426 // from the symbol table. If we just had the name string and the types of the parameters that
3427 // would be enough, but TFunction carries a lot of extra information in addition to that.
3428 // Besides function calls we do have to store constructor calls in the same data structure, for
3429 // them we need to store a TType.
Olli Etuaho0c371002017-12-13 17:00:25 +04003430 return new TFunction(&symbolTable, name, returnType, SymbolType::NotResolved, false);
Olli Etuahocce89652017-06-19 16:04:09 +03003431}
3432
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003433TFunction *TParseContext::addConstructorFunc(const TPublicType &publicType)
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003434{
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003435 if (mShaderVersion < 300 && publicType.isArray())
Olli Etuahocce89652017-06-19 16:04:09 +03003436 {
3437 error(publicType.getLine(), "array constructor supported in GLSL ES 3.00 and above only",
3438 "[]");
3439 }
Martin Radev4a9cd802016-09-01 16:51:51 +03003440 if (publicType.isStructSpecifier())
Olli Etuahobd163f62015-11-13 12:15:38 +02003441 {
Martin Radev4a9cd802016-09-01 16:51:51 +03003442 error(publicType.getLine(), "constructor can't be a structure definition",
3443 getBasicString(publicType.getBasicType()));
Olli Etuahobd163f62015-11-13 12:15:38 +02003444 }
3445
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003446 TType *type = new TType(publicType);
3447 if (!type->canBeConstructed())
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003448 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003449 error(publicType.getLine(), "cannot construct this type",
3450 getBasicString(publicType.getBasicType()));
3451 type->setBasicType(EbtFloat);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003452 }
3453
Olli Etuaho0c371002017-12-13 17:00:25 +04003454 return new TFunction(&symbolTable, nullptr, type, SymbolType::NotResolved, true, EOpConstruct);
shannonwoods@chromium.org18851132013-05-30 00:19:54 +00003455}
3456
Olli Etuaho55bde912017-10-25 13:41:13 +03003457void TParseContext::checkIsNotUnsizedArray(const TSourceLoc &line,
3458 const char *errorMessage,
3459 const char *token,
3460 TType *arrayType)
3461{
3462 if (arrayType->isUnsizedArray())
3463 {
3464 error(line, errorMessage, token);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003465 arrayType->sizeUnsizedArrays(nullptr);
Olli Etuaho55bde912017-10-25 13:41:13 +03003466 }
3467}
3468
3469TParameter TParseContext::parseParameterDeclarator(TType *type,
Olli Etuahocce89652017-06-19 16:04:09 +03003470 const TString *name,
3471 const TSourceLoc &nameLoc)
3472{
Olli Etuaho55bde912017-10-25 13:41:13 +03003473 ASSERT(type);
3474 checkIsNotUnsizedArray(nameLoc, "function parameter array must specify a size", name->c_str(),
3475 type);
3476 if (type->getBasicType() == EbtVoid)
Olli Etuahocce89652017-06-19 16:04:09 +03003477 {
3478 error(nameLoc, "illegal use of type 'void'", name->c_str());
3479 }
3480 checkIsNotReserved(nameLoc, *name);
Olli Etuahocce89652017-06-19 16:04:09 +03003481 TParameter param = {name, type};
3482 return param;
3483}
3484
Olli Etuaho55bde912017-10-25 13:41:13 +03003485TParameter TParseContext::parseParameterDeclarator(const TPublicType &publicType,
3486 const TString *name,
3487 const TSourceLoc &nameLoc)
Olli Etuahocce89652017-06-19 16:04:09 +03003488{
Olli Etuaho55bde912017-10-25 13:41:13 +03003489 TType *type = new TType(publicType);
3490 return parseParameterDeclarator(type, name, nameLoc);
3491}
3492
3493TParameter TParseContext::parseParameterArrayDeclarator(const TString *name,
3494 const TSourceLoc &nameLoc,
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003495 const TVector<unsigned int> &arraySizes,
Olli Etuaho55bde912017-10-25 13:41:13 +03003496 const TSourceLoc &arrayLoc,
3497 TPublicType *elementType)
3498{
3499 checkArrayElementIsNotArray(arrayLoc, *elementType);
3500 TType *arrayType = new TType(*elementType);
Olli Etuaho7881cfd2017-08-23 18:00:21 +03003501 arrayType->makeArrays(arraySizes);
Olli Etuaho55bde912017-10-25 13:41:13 +03003502 return parseParameterDeclarator(arrayType, name, nameLoc);
Olli Etuahocce89652017-06-19 16:04:09 +03003503}
3504
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003505bool TParseContext::checkUnsizedArrayConstructorArgumentDimensionality(TIntermSequence *arguments,
3506 TType type,
3507 const TSourceLoc &line)
3508{
3509 if (arguments->empty())
3510 {
3511 error(line, "implicitly sized array constructor must have at least one argument", "[]");
3512 return false;
3513 }
3514 for (TIntermNode *arg : *arguments)
3515 {
3516 TIntermTyped *element = arg->getAsTyped();
3517 ASSERT(element);
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003518 size_t dimensionalityFromElement = element->getType().getNumArraySizes() + 1u;
3519 if (dimensionalityFromElement > type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003520 {
3521 error(line, "constructing from a non-dereferenced array", "constructor");
3522 return false;
3523 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003524 else if (dimensionalityFromElement < type.getNumArraySizes())
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003525 {
3526 if (dimensionalityFromElement == 1u)
3527 {
3528 error(line, "implicitly sized array of arrays constructor argument is not an array",
3529 "constructor");
3530 }
3531 else
3532 {
3533 error(line,
3534 "implicitly sized array of arrays constructor argument dimensionality is too "
3535 "low",
3536 "constructor");
3537 }
3538 return false;
3539 }
3540 }
3541 return true;
3542}
3543
Jamie Madillb98c3a82015-07-23 14:26:04 -04003544// This function is used to test for the correctness of the parameters passed to various constructor
3545// functions and also convert them to the right datatype if it is allowed and required.
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003546//
Olli Etuaho856c4972016-08-08 11:38:39 +03003547// 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 +00003548//
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003549TIntermTyped *TParseContext::addConstructor(TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00003550 TType type,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303551 const TSourceLoc &line)
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003552{
Olli Etuaho856c4972016-08-08 11:38:39 +03003553 if (type.isUnsizedArray())
3554 {
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003555 if (!checkUnsizedArrayConstructorArgumentDimensionality(arguments, type, line))
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003556 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003557 type.sizeUnsizedArrays(nullptr);
Olli Etuaho3ec75682017-07-05 17:02:55 +03003558 return CreateZeroNode(type);
Olli Etuahobbe9fb52016-11-03 17:16:05 +00003559 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003560 TIntermTyped *firstElement = arguments->at(0)->getAsTyped();
3561 ASSERT(firstElement);
Olli Etuaho9cd71632017-10-26 14:43:20 +03003562 if (type.getOutermostArraySize() == 0u)
3563 {
3564 type.sizeOutermostUnsizedArray(static_cast<unsigned int>(arguments->size()));
3565 }
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003566 for (size_t i = 0; i < firstElement->getType().getNumArraySizes(); ++i)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003567 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003568 if ((*type.getArraySizes())[i] == 0u)
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003569 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08003570 type.setArraySize(i, (*firstElement->getType().getArraySizes())[i]);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003571 }
3572 }
3573 ASSERT(!type.isUnsizedArray());
Olli Etuaho856c4972016-08-08 11:38:39 +03003574 }
Olli Etuaho856c4972016-08-08 11:38:39 +03003575
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003576 if (!checkConstructorArguments(line, arguments, type))
Olli Etuaho856c4972016-08-08 11:38:39 +03003577 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03003578 return CreateZeroNode(type);
Olli Etuaho856c4972016-08-08 11:38:39 +03003579 }
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003580
Olli Etuahoa7ecec32017-05-08 17:43:55 +03003581 TIntermAggregate *constructorNode = TIntermAggregate::CreateConstructor(type, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08003582 constructorNode->setLine(line);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003583
Olli Etuaho765924f2018-01-04 12:48:36 +02003584 return constructorNode->fold(mDiagnostics);
daniel@transgaming.com4f39fd92010-03-08 20:26:45 +00003585}
3586
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003587//
3588// Interface/uniform blocks
Jiawei Shaobd924af2017-11-16 15:28:04 +08003589// TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003590//
Olli Etuaho13389b62016-10-16 11:48:18 +01003591TIntermDeclaration *TParseContext::addInterfaceBlock(
Martin Radev70866b82016-07-22 15:27:42 +03003592 const TTypeQualifierBuilder &typeQualifierBuilder,
3593 const TSourceLoc &nameLine,
3594 const TString &blockName,
3595 TFieldList *fieldList,
3596 const TString *instanceName,
3597 const TSourceLoc &instanceLine,
3598 TIntermTyped *arrayIndex,
3599 const TSourceLoc &arrayIndexLine)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003600{
Olli Etuaho856c4972016-08-08 11:38:39 +03003601 checkIsNotReserved(nameLine, blockName);
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003602
Olli Etuaho77ba4082016-12-16 12:01:18 +00003603 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Martin Radev70866b82016-07-22 15:27:42 +03003604
Jiajia Qinbc585152017-06-23 15:42:17 +08003605 if (mShaderVersion < 310 && typeQualifier.qualifier != EvqUniform)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003606 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003607 error(typeQualifier.line,
3608 "invalid qualifier: interface blocks must be uniform in version lower than GLSL ES "
3609 "3.10",
3610 getQualifierString(typeQualifier.qualifier));
3611 }
3612 else if (typeQualifier.qualifier != EvqUniform && typeQualifier.qualifier != EvqBuffer)
3613 {
3614 error(typeQualifier.line, "invalid qualifier: interface blocks must be uniform or buffer",
Olli Etuaho4de340a2016-12-16 09:32:03 +00003615 getQualifierString(typeQualifier.qualifier));
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003616 }
3617
Martin Radev70866b82016-07-22 15:27:42 +03003618 if (typeQualifier.invariant)
3619 {
3620 error(typeQualifier.line, "invalid qualifier on interface block member", "invariant");
3621 }
3622
Jiajia Qinbc585152017-06-23 15:42:17 +08003623 if (typeQualifier.qualifier != EvqBuffer)
3624 {
3625 checkMemoryQualifierIsNotSpecified(typeQualifier.memoryQualifier, typeQualifier.line);
3626 }
Olli Etuaho43364892017-02-13 16:00:12 +00003627
jchen10af713a22017-04-19 09:10:56 +08003628 // add array index
3629 unsigned int arraySize = 0;
3630 if (arrayIndex != nullptr)
3631 {
3632 arraySize = checkIsValidArraySize(arrayIndexLine, arrayIndex);
3633 }
3634
3635 if (mShaderVersion < 310)
3636 {
3637 checkBindingIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.binding);
3638 }
3639 else
3640 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003641 checkBlockBindingIsValid(typeQualifier.line, typeQualifier.qualifier,
3642 typeQualifier.layoutQualifier.binding, arraySize);
jchen10af713a22017-04-19 09:10:56 +08003643 }
Martin Radev2cc85b32016-08-05 16:22:53 +03003644
Andrei Volykhina5527072017-03-22 16:46:30 +03003645 checkYuvIsNotSpecified(typeQualifier.line, typeQualifier.layoutQualifier.yuv);
3646
Jamie Madill099c0f32013-06-20 11:55:52 -04003647 TLayoutQualifier blockLayoutQualifier = typeQualifier.layoutQualifier;
Olli Etuaho856c4972016-08-08 11:38:39 +03003648 checkLocationIsNotSpecified(typeQualifier.line, blockLayoutQualifier);
Qin Jiajiaca68d982017-09-18 16:41:56 +08003649 checkStd430IsForShaderStorageBlock(typeQualifier.line, blockLayoutQualifier.blockStorage,
3650 typeQualifier.qualifier);
Jamie Madilla5efff92013-06-06 11:56:47 -04003651
Jamie Madill099c0f32013-06-20 11:55:52 -04003652 if (blockLayoutQualifier.matrixPacking == EmpUnspecified)
3653 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003654 if (typeQualifier.qualifier == EvqUniform)
3655 {
3656 blockLayoutQualifier.matrixPacking = mDefaultUniformMatrixPacking;
3657 }
3658 else if (typeQualifier.qualifier == EvqBuffer)
3659 {
3660 blockLayoutQualifier.matrixPacking = mDefaultBufferMatrixPacking;
3661 }
Jamie Madill099c0f32013-06-20 11:55:52 -04003662 }
3663
Jamie Madill1566ef72013-06-20 11:55:54 -04003664 if (blockLayoutQualifier.blockStorage == EbsUnspecified)
3665 {
Jiajia Qinbc585152017-06-23 15:42:17 +08003666 if (typeQualifier.qualifier == EvqUniform)
3667 {
3668 blockLayoutQualifier.blockStorage = mDefaultUniformBlockStorage;
3669 }
3670 else if (typeQualifier.qualifier == EvqBuffer)
3671 {
3672 blockLayoutQualifier.blockStorage = mDefaultBufferBlockStorage;
3673 }
Jamie Madill1566ef72013-06-20 11:55:54 -04003674 }
3675
Olli Etuaho856c4972016-08-08 11:38:39 +03003676 checkWorkGroupSizeIsNotSpecified(nameLine, blockLayoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03003677
Martin Radev2cc85b32016-08-05 16:22:53 +03003678 checkInternalFormatIsNotSpecified(nameLine, blockLayoutQualifier.imageInternalFormat);
3679
Jamie Madill98493dd2013-07-08 14:39:03 -04003680 // check for sampler types and apply layout qualifiers
Arun Patole7e7e68d2015-05-22 12:02:25 +05303681 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3682 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003683 TField *field = (*fieldList)[memberIndex];
Arun Patole7e7e68d2015-05-22 12:02:25 +05303684 TType *fieldType = field->type();
jchen10cc2a10e2017-05-03 14:05:12 +08003685 if (IsOpaqueType(fieldType->getBasicType()))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303686 {
jchen10cc2a10e2017-05-03 14:05:12 +08003687 std::string reason("unsupported type - ");
3688 reason += fieldType->getBasicString();
3689 reason += " types are not allowed in interface blocks";
3690 error(field->line(), reason.c_str(), fieldType->getBasicString());
Martin Radev2cc85b32016-08-05 16:22:53 +03003691 }
3692
Jamie Madill98493dd2013-07-08 14:39:03 -04003693 const TQualifier qualifier = fieldType->getQualifier();
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003694 switch (qualifier)
3695 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003696 case EvqGlobal:
Jiajia Qinbc585152017-06-23 15:42:17 +08003697 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04003698 case EvqUniform:
Jiajia Qinbc585152017-06-23 15:42:17 +08003699 if (typeQualifier.qualifier == EvqBuffer)
3700 {
3701 error(field->line(), "invalid qualifier on shader storage block member",
3702 getQualifierString(qualifier));
3703 }
3704 break;
3705 case EvqBuffer:
3706 if (typeQualifier.qualifier == EvqUniform)
3707 {
3708 error(field->line(), "invalid qualifier on uniform block member",
3709 getQualifierString(qualifier));
3710 }
Jamie Madillb98c3a82015-07-23 14:26:04 -04003711 break;
3712 default:
3713 error(field->line(), "invalid qualifier on interface block member",
3714 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04003715 break;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003716 }
Jamie Madilla5efff92013-06-06 11:56:47 -04003717
Martin Radev70866b82016-07-22 15:27:42 +03003718 if (fieldType->isInvariant())
3719 {
3720 error(field->line(), "invalid qualifier on interface block member", "invariant");
3721 }
3722
Jamie Madilla5efff92013-06-06 11:56:47 -04003723 // check layout qualifiers
Jamie Madill98493dd2013-07-08 14:39:03 -04003724 TLayoutQualifier fieldLayoutQualifier = fieldType->getLayoutQualifier();
Olli Etuaho856c4972016-08-08 11:38:39 +03003725 checkLocationIsNotSpecified(field->line(), fieldLayoutQualifier);
jchen10af713a22017-04-19 09:10:56 +08003726 checkBindingIsNotSpecified(field->line(), fieldLayoutQualifier.binding);
Jamie Madill099c0f32013-06-20 11:55:52 -04003727
Jamie Madill98493dd2013-07-08 14:39:03 -04003728 if (fieldLayoutQualifier.blockStorage != EbsUnspecified)
Jamie Madill1566ef72013-06-20 11:55:54 -04003729 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003730 error(field->line(), "invalid layout qualifier: cannot be used here",
3731 getBlockStorageString(fieldLayoutQualifier.blockStorage));
Jamie Madill1566ef72013-06-20 11:55:54 -04003732 }
3733
Jamie Madill98493dd2013-07-08 14:39:03 -04003734 if (fieldLayoutQualifier.matrixPacking == EmpUnspecified)
Jamie Madill099c0f32013-06-20 11:55:52 -04003735 {
Jamie Madill98493dd2013-07-08 14:39:03 -04003736 fieldLayoutQualifier.matrixPacking = blockLayoutQualifier.matrixPacking;
Jamie Madill099c0f32013-06-20 11:55:52 -04003737 }
Olli Etuahofb6ab2c2015-07-09 20:55:28 +03003738 else if (!fieldType->isMatrix() && fieldType->getBasicType() != EbtStruct)
Jamie Madill099c0f32013-06-20 11:55:52 -04003739 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003740 warning(field->line(),
3741 "extraneous layout qualifier: only has an effect on matrix types",
3742 getMatrixPackingString(fieldLayoutQualifier.matrixPacking));
Jamie Madill099c0f32013-06-20 11:55:52 -04003743 }
3744
Jamie Madill98493dd2013-07-08 14:39:03 -04003745 fieldType->setLayoutQualifier(fieldLayoutQualifier);
Jiajia Qinbc585152017-06-23 15:42:17 +08003746
Olli Etuahoebee5b32017-11-23 12:56:32 +02003747 if (mShaderVersion < 310 || memberIndex != fieldList->size() - 1u ||
3748 typeQualifier.qualifier != EvqBuffer)
3749 {
3750 // ESSL 3.10 spec section 4.1.9 allows for runtime-sized arrays.
3751 checkIsNotUnsizedArray(field->line(),
3752 "array members of interface blocks must specify a size",
3753 field->name().c_str(), field->type());
3754 }
3755
Jiajia Qinbc585152017-06-23 15:42:17 +08003756 if (typeQualifier.qualifier == EvqBuffer)
3757 {
3758 // set memory qualifiers
3759 // GLSL ES 3.10 session 4.9 [Memory Access Qualifiers]. When a block declaration is
3760 // qualified with a memory qualifier, it is as if all of its members were declared with
3761 // the same memory qualifier.
3762 const TMemoryQualifier &blockMemoryQualifier = typeQualifier.memoryQualifier;
3763 TMemoryQualifier fieldMemoryQualifier = fieldType->getMemoryQualifier();
3764 fieldMemoryQualifier.readonly |= blockMemoryQualifier.readonly;
3765 fieldMemoryQualifier.writeonly |= blockMemoryQualifier.writeonly;
3766 fieldMemoryQualifier.coherent |= blockMemoryQualifier.coherent;
3767 fieldMemoryQualifier.restrictQualifier |= blockMemoryQualifier.restrictQualifier;
3768 fieldMemoryQualifier.volatileQualifier |= blockMemoryQualifier.volatileQualifier;
3769 // TODO(jiajia.qin@intel.com): Decide whether if readonly and writeonly buffer variable
3770 // is legal. See bug https://github.com/KhronosGroup/OpenGL-API/issues/7
3771 fieldType->setMemoryQualifier(fieldMemoryQualifier);
3772 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003773 }
3774
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01003775 TInterfaceBlock *interfaceBlock = new TInterfaceBlock(
3776 &symbolTable, &blockName, fieldList, blockLayoutQualifier, SymbolType::UserDefined);
Olli Etuaho378c3a52017-12-04 11:32:13 +02003777 if (!symbolTable.declareInterfaceBlock(interfaceBlock))
3778 {
3779 error(nameLine, "redefinition of an interface block name", blockName.c_str());
3780 }
3781
Olli Etuahob60d30f2018-01-16 12:31:06 +02003782 TType *interfaceBlockType =
3783 new TType(interfaceBlock, typeQualifier.qualifier, blockLayoutQualifier);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003784 if (arrayIndex != nullptr)
3785 {
Olli Etuahob60d30f2018-01-16 12:31:06 +02003786 interfaceBlockType->makeArray(arraySize);
Olli Etuaho96f6adf2017-08-16 11:18:54 +03003787 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003788
Olli Etuaho195be942017-12-04 23:40:14 +02003789 // The instance variable gets created to refer to the interface block type from the AST
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003790 // regardless of if there's an instance name. It's created as an empty symbol if there is no
3791 // instance name.
Olli Etuaho195be942017-12-04 23:40:14 +02003792 TVariable *instanceVariable =
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003793 new TVariable(&symbolTable, instanceName, interfaceBlockType,
3794 instanceName ? SymbolType::UserDefined : SymbolType::Empty);
Olli Etuaho195be942017-12-04 23:40:14 +02003795
Olli Etuahoae4dbf32017-12-08 20:49:00 +01003796 if (instanceVariable->symbolType() == SymbolType::Empty)
Olli Etuaho195be942017-12-04 23:40:14 +02003797 {
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003798 // define symbols for the members of the interface block
Jamie Madill98493dd2013-07-08 14:39:03 -04003799 for (size_t memberIndex = 0; memberIndex < fieldList->size(); ++memberIndex)
3800 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04003801 TField *field = (*fieldList)[memberIndex];
Olli Etuahob60d30f2018-01-16 12:31:06 +02003802 TType *fieldType = new TType(*field->type());
Jamie Madill98493dd2013-07-08 14:39:03 -04003803
3804 // set parent pointer of the field variable
3805 fieldType->setInterfaceBlock(interfaceBlock);
3806
Olli Etuahob60d30f2018-01-16 12:31:06 +02003807 fieldType->setQualifier(typeQualifier.qualifier);
3808
Olli Etuaho195be942017-12-04 23:40:14 +02003809 TVariable *fieldVariable =
Olli Etuahob60d30f2018-01-16 12:31:06 +02003810 new TVariable(&symbolTable, &field->name(), fieldType, SymbolType::UserDefined);
3811 if (!symbolTable.declareVariable(fieldVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303812 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003813 error(field->line(), "redefinition of an interface block member name",
3814 field->name().c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003815 }
3816 }
3817 }
3818 else
3819 {
Olli Etuaho856c4972016-08-08 11:38:39 +03003820 checkIsNotReserved(instanceLine, *instanceName);
Olli Etuahoe0f623a2015-07-10 11:58:30 +03003821
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003822 // add a symbol for this interface block
Olli Etuaho195be942017-12-04 23:40:14 +02003823 if (!symbolTable.declareVariable(instanceVariable))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303824 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003825 error(instanceLine, "redefinition of an interface block instance name",
3826 instanceName->c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003827 }
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003828 }
3829
Olli Etuaho195be942017-12-04 23:40:14 +02003830 TIntermSymbol *blockSymbol = new TIntermSymbol(instanceVariable);
3831 blockSymbol->setLine(typeQualifier.line);
3832 TIntermDeclaration *declaration = new TIntermDeclaration();
3833 declaration->appendDeclarator(blockSymbol);
3834 declaration->setLine(nameLine);
Jamie Madill98493dd2013-07-08 14:39:03 -04003835
3836 exitStructDeclaration();
Olli Etuaho13389b62016-10-16 11:48:18 +01003837 return declaration;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00003838}
3839
Olli Etuaho383b7912016-08-05 11:22:59 +03003840void TParseContext::enterStructDeclaration(const TSourceLoc &line, const TString &identifier)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003841{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003842 ++mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003843
3844 // Embedded structure definitions are not supported per GLSL ES spec.
Olli Etuaho4de340a2016-12-16 09:32:03 +00003845 // ESSL 1.00.17 section 10.9. ESSL 3.00.6 section 12.11.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303846 if (mStructNestingLevel > 1)
3847 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003848 error(line, "Embedded struct definitions are not allowed", "struct");
kbr@chromium.org476541f2011-10-27 21:14:51 +00003849 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003850}
3851
3852void TParseContext::exitStructDeclaration()
3853{
Jamie Madill6e06b1f2015-05-14 10:01:17 -04003854 --mStructNestingLevel;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003855}
3856
Olli Etuaho8a176262016-08-16 14:23:01 +03003857void TParseContext::checkIsBelowStructNestingLimit(const TSourceLoc &line, const TField &field)
kbr@chromium.org476541f2011-10-27 21:14:51 +00003858{
Jamie Madillacb4b812016-11-07 13:50:29 -05003859 if (!sh::IsWebGLBasedSpec(mShaderSpec))
Arun Patole7e7e68d2015-05-22 12:02:25 +05303860 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003861 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003862 }
3863
Arun Patole7e7e68d2015-05-22 12:02:25 +05303864 if (field.type()->getBasicType() != EbtStruct)
3865 {
Olli Etuaho8a176262016-08-16 14:23:01 +03003866 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003867 }
3868
3869 // We're already inside a structure definition at this point, so add
3870 // one to the field's struct nesting.
Arun Patole7e7e68d2015-05-22 12:02:25 +05303871 if (1 + field.type()->getDeepestStructNesting() > kWebGLMaxStructNesting)
3872 {
Jamie Madill41a49272014-03-18 16:10:13 -04003873 std::stringstream reasonStream;
Olli Etuahof0957992017-12-22 11:10:04 +02003874 if (field.type()->getStruct()->symbolType() == SymbolType::Empty)
3875 {
3876 // This may happen in case there are nested struct definitions. While they are also
3877 // invalid GLSL, they don't cause a syntax error.
3878 reasonStream << "Struct nesting";
3879 }
3880 else
3881 {
3882 reasonStream << "Reference of struct type "
Olli Etuahobed35d72017-12-20 16:36:26 +02003883 << field.type()->getStruct()->name().c_str();
Olli Etuahof0957992017-12-22 11:10:04 +02003884 }
3885 reasonStream << " exceeds maximum allowed nesting level of " << kWebGLMaxStructNesting;
Jamie Madill41a49272014-03-18 16:10:13 -04003886 std::string reason = reasonStream.str();
Olli Etuaho4de340a2016-12-16 09:32:03 +00003887 error(line, reason.c_str(), field.name().c_str());
Olli Etuaho8a176262016-08-16 14:23:01 +03003888 return;
kbr@chromium.org476541f2011-10-27 21:14:51 +00003889 }
kbr@chromium.org476541f2011-10-27 21:14:51 +00003890}
3891
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00003892//
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003893// Parse an array index expression
3894//
Jamie Madillb98c3a82015-07-23 14:26:04 -04003895TIntermTyped *TParseContext::addIndexExpression(TIntermTyped *baseExpression,
3896 const TSourceLoc &location,
Arun Patole7e7e68d2015-05-22 12:02:25 +05303897 TIntermTyped *indexExpression)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003898{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003899 if (!baseExpression->isArray() && !baseExpression->isMatrix() && !baseExpression->isVector())
3900 {
3901 if (baseExpression->getAsSymbolNode())
3902 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05303903 error(location, " left of '[' is not of type array, matrix, or vector ",
Olli Etuaho8b5e8fd2017-12-15 14:59:15 +02003904 baseExpression->getAsSymbolNode()->getName().c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003905 }
3906 else
3907 {
3908 error(location, " left of '[' is not of type array, matrix, or vector ", "expression");
3909 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003910
Olli Etuaho3ec75682017-07-05 17:02:55 +03003911 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00003912 }
shannonwoods@chromium.org09e09882013-05-30 00:18:25 +00003913
Jiawei Shaod8105a02017-08-08 09:54:36 +08003914 if (baseExpression->getQualifier() == EvqPerVertexIn)
3915 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003916 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08003917 if (mGeometryShaderInputPrimitiveType == EptUndefined)
3918 {
3919 error(location, "missing input primitive declaration before indexing gl_in.", "[");
3920 return CreateZeroNode(TType(EbtFloat, EbpHigh, EvqConst));
3921 }
3922 }
3923
Jamie Madill21c1e452014-12-29 11:33:41 -05003924 TIntermConstantUnion *indexConstantUnion = indexExpression->getAsConstantUnion();
3925
Olli Etuaho36b05142015-11-12 13:10:42 +02003926 // TODO(oetuaho@nvidia.com): Get rid of indexConstantUnion == nullptr below once ANGLE is able
3927 // to constant fold all constant expressions. Right now we don't allow indexing interface blocks
3928 // or fragment outputs with expressions that ANGLE is not able to constant fold, even if the
3929 // index is a constant expression.
3930 if (indexExpression->getQualifier() != EvqConst || indexConstantUnion == nullptr)
3931 {
3932 if (baseExpression->isInterfaceBlock())
3933 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08003934 // TODO(jiawei.shao@intel.com): implement GL_EXT_shader_io_blocks.
Jiawei Shaod8105a02017-08-08 09:54:36 +08003935 switch (baseExpression->getQualifier())
3936 {
3937 case EvqPerVertexIn:
3938 break;
3939 case EvqUniform:
3940 case EvqBuffer:
3941 error(location,
3942 "array indexes for uniform block arrays and shader storage block arrays "
3943 "must be constant integral expressions",
3944 "[");
3945 break;
3946 default:
Jiawei Shao7e1197e2017-08-24 15:48:38 +08003947 // We can reach here only in error cases.
3948 ASSERT(mDiagnostics->numErrors() > 0);
3949 break;
Jiawei Shaod8105a02017-08-08 09:54:36 +08003950 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003951 }
3952 else if (baseExpression->getQualifier() == EvqFragmentOut)
3953 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003954 error(location,
3955 "array indexes for fragment outputs must be constant integral expressions", "[");
Olli Etuaho36b05142015-11-12 13:10:42 +02003956 }
Olli Etuaho3e960462015-11-12 15:58:39 +02003957 else if (mShaderSpec == SH_WEBGL2_SPEC && baseExpression->getQualifier() == EvqFragData)
3958 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00003959 error(location, "array index for gl_FragData must be constant zero", "[");
Olli Etuaho3e960462015-11-12 15:58:39 +02003960 }
Olli Etuaho36b05142015-11-12 13:10:42 +02003961 }
3962
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003963 if (indexConstantUnion)
Jamie Madill7164cf42013-07-08 13:30:59 -04003964 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003965 // If an out-of-range index is not qualified as constant, the behavior in the spec is
3966 // undefined. This applies even if ANGLE has been able to constant fold it (ANGLE may
3967 // constant fold expressions that are not constant expressions). The most compatible way to
3968 // handle this case is to report a warning instead of an error and force the index to be in
3969 // the correct range.
Olli Etuaho7c3848e2015-11-04 13:19:17 +02003970 bool outOfRangeIndexIsError = indexExpression->getQualifier() == EvqConst;
Olli Etuaho56229f12017-07-10 14:16:33 +03003971 int index = 0;
3972 if (indexConstantUnion->getBasicType() == EbtInt)
3973 {
3974 index = indexConstantUnion->getIConst(0);
3975 }
3976 else if (indexConstantUnion->getBasicType() == EbtUInt)
3977 {
3978 index = static_cast<int>(indexConstantUnion->getUConst(0));
3979 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003980
3981 int safeIndex = -1;
3982
Olli Etuahoebee5b32017-11-23 12:56:32 +02003983 if (index < 0)
Jamie Madill7164cf42013-07-08 13:30:59 -04003984 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02003985 outOfRangeError(outOfRangeIndexIsError, location, "index expression is negative", "[]");
3986 safeIndex = 0;
3987 }
3988
3989 if (!baseExpression->getType().isUnsizedArray())
3990 {
3991 if (baseExpression->isArray())
Olli Etuaho90892fb2016-07-14 14:44:51 +03003992 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02003993 if (baseExpression->getQualifier() == EvqFragData && index > 0)
Olli Etuaho3272a6d2016-08-29 17:54:50 +03003994 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02003995 if (!isExtensionEnabled(TExtension::EXT_draw_buffers))
3996 {
3997 outOfRangeError(outOfRangeIndexIsError, location,
3998 "array index for gl_FragData must be zero when "
3999 "GL_EXT_draw_buffers is disabled",
4000 "[]");
4001 safeIndex = 0;
4002 }
4003 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004004 }
4005 // Only do generic out-of-range check if similar error hasn't already been reported.
4006 if (safeIndex < 0)
4007 {
4008 if (baseExpression->isArray())
Olli Etuahoebee5b32017-11-23 12:56:32 +02004009 {
4010 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4011 baseExpression->getOutermostArraySize(),
4012 "array index out of range");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004013 }
Olli Etuahof13cadd2017-11-28 10:53:09 +02004014 else if (baseExpression->isMatrix())
4015 {
4016 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4017 baseExpression->getType().getCols(),
4018 "matrix field selection out of range");
4019 }
4020 else
4021 {
4022 ASSERT(baseExpression->isVector());
4023 safeIndex = checkIndexLessThan(outOfRangeIndexIsError, location, index,
4024 baseExpression->getType().getNominalSize(),
4025 "vector field selection out of range");
4026 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004027 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004028
Olli Etuahoebee5b32017-11-23 12:56:32 +02004029 ASSERT(safeIndex >= 0);
4030 // Data of constant unions can't be changed, because it may be shared with other
4031 // constant unions or even builtins, like gl_MaxDrawBuffers. Instead use a new
4032 // sanitized object.
4033 if (safeIndex != index || indexConstantUnion->getBasicType() != EbtInt)
4034 {
4035 TConstantUnion *safeConstantUnion = new TConstantUnion();
4036 safeConstantUnion->setIConst(safeIndex);
Olli Etuaho0e99b7a2018-01-12 12:05:48 +02004037 indexExpression = new TIntermConstantUnion(
4038 safeConstantUnion, TType(EbtInt, indexExpression->getPrecision(),
4039 indexExpression->getQualifier()));
Olli Etuahoebee5b32017-11-23 12:56:32 +02004040 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004041
Olli Etuahoebee5b32017-11-23 12:56:32 +02004042 TIntermBinary *node =
4043 new TIntermBinary(EOpIndexDirect, baseExpression, indexExpression);
4044 node->setLine(location);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02004045 return expressionOrFoldedResult(node);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004046 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004047 }
Olli Etuahoebee5b32017-11-23 12:56:32 +02004048
4049 TIntermBinary *node = new TIntermBinary(EOpIndexIndirect, baseExpression, indexExpression);
4050 node->setLine(location);
4051 // Indirect indexing can never be constant folded.
4052 return node;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004053}
4054
Olli Etuahoebee5b32017-11-23 12:56:32 +02004055int TParseContext::checkIndexLessThan(bool outOfRangeIndexIsError,
4056 const TSourceLoc &location,
4057 int index,
4058 int arraySize,
4059 const char *reason)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004060{
Olli Etuahoebee5b32017-11-23 12:56:32 +02004061 // Should not reach here with an unsized / runtime-sized array.
4062 ASSERT(arraySize > 0);
Olli Etuahof13cadd2017-11-28 10:53:09 +02004063 // A negative index should already have been checked.
4064 ASSERT(index >= 0);
Olli Etuahoebee5b32017-11-23 12:56:32 +02004065 if (index >= arraySize)
Olli Etuaho90892fb2016-07-14 14:44:51 +03004066 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004067 std::stringstream reasonStream;
4068 reasonStream << reason << " '" << index << "'";
4069 std::string token = reasonStream.str();
4070 outOfRangeError(outOfRangeIndexIsError, location, reason, "[]");
Olli Etuahoebee5b32017-11-23 12:56:32 +02004071 return arraySize - 1;
Olli Etuaho90892fb2016-07-14 14:44:51 +03004072 }
4073 return index;
4074}
4075
Jamie Madillb98c3a82015-07-23 14:26:04 -04004076TIntermTyped *TParseContext::addFieldSelectionExpression(TIntermTyped *baseExpression,
4077 const TSourceLoc &dotLocation,
4078 const TString &fieldString,
4079 const TSourceLoc &fieldLocation)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004080{
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004081 if (baseExpression->isArray())
4082 {
4083 error(fieldLocation, "cannot apply dot operator to an array", ".");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004084 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004085 }
4086
4087 if (baseExpression->isVector())
4088 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004089 TVector<int> fieldOffsets;
4090 if (!parseVectorFields(fieldLocation, fieldString, baseExpression->getNominalSize(),
4091 &fieldOffsets))
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004092 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004093 fieldOffsets.resize(1);
4094 fieldOffsets[0] = 0;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004095 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004096 TIntermSwizzle *node = new TIntermSwizzle(baseExpression, fieldOffsets);
4097 node->setLine(dotLocation);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004098
Olli Etuaho765924f2018-01-04 12:48:36 +02004099 return node->fold(mDiagnostics);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004100 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004101 else if (baseExpression->getBasicType() == EbtStruct)
4102 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304103 const TFieldList &fields = baseExpression->getType().getStruct()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004104 if (fields.empty())
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004105 {
4106 error(dotLocation, "structure has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004107 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004108 }
4109 else
4110 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004111 bool fieldFound = false;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004112 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004113 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004114 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004115 if (fields[i]->name() == fieldString)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004116 {
4117 fieldFound = true;
4118 break;
4119 }
4120 }
4121 if (fieldFound)
4122 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004123 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004124 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004125 TIntermBinary *node =
4126 new TIntermBinary(EOpIndexDirectStruct, baseExpression, index);
4127 node->setLine(dotLocation);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02004128 return expressionOrFoldedResult(node);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004129 }
4130 else
4131 {
4132 error(dotLocation, " no such field in structure", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004133 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004134 }
4135 }
4136 }
Jamie Madill98493dd2013-07-08 14:39:03 -04004137 else if (baseExpression->isInterfaceBlock())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004138 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304139 const TFieldList &fields = baseExpression->getType().getInterfaceBlock()->fields();
Jamie Madill98493dd2013-07-08 14:39:03 -04004140 if (fields.empty())
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004141 {
4142 error(dotLocation, "interface block has no fields", "Internal Error");
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004143 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004144 }
4145 else
4146 {
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004147 bool fieldFound = false;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004148 unsigned int i;
Jamie Madill98493dd2013-07-08 14:39:03 -04004149 for (i = 0; i < fields.size(); ++i)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004150 {
Jamie Madill98493dd2013-07-08 14:39:03 -04004151 if (fields[i]->name() == fieldString)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004152 {
4153 fieldFound = true;
4154 break;
4155 }
4156 }
4157 if (fieldFound)
4158 {
Olli Etuaho3ec75682017-07-05 17:02:55 +03004159 TIntermTyped *index = CreateIndexNode(i);
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004160 index->setLine(fieldLocation);
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004161 TIntermBinary *node =
4162 new TIntermBinary(EOpIndexDirectInterfaceBlock, baseExpression, index);
4163 node->setLine(dotLocation);
4164 // Indexing interface blocks can never be constant folded.
4165 return node;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004166 }
4167 else
4168 {
4169 error(dotLocation, " no such field in interface block", fieldString.c_str());
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004170 return baseExpression;
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004171 }
4172 }
4173 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004174 else
4175 {
Jamie Madill6e06b1f2015-05-14 10:01:17 -04004176 if (mShaderVersion < 300)
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004177 {
Olli Etuaho56193ce2015-08-12 15:55:09 +03004178 error(dotLocation, " field selection requires structure or vector on left hand side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304179 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004180 }
4181 else
4182 {
Arun Patole7e7e68d2015-05-22 12:02:25 +05304183 error(dotLocation,
Olli Etuaho56193ce2015-08-12 15:55:09 +03004184 " field selection requires structure, vector, or interface block on left hand "
4185 "side",
Arun Patole7e7e68d2015-05-22 12:02:25 +05304186 fieldString.c_str());
shannonwoods@chromium.org5668c5d2013-05-30 00:11:48 +00004187 }
Olli Etuaho3272a6d2016-08-29 17:54:50 +03004188 return baseExpression;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004189 }
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004190}
4191
Jamie Madillb98c3a82015-07-23 14:26:04 -04004192TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4193 const TSourceLoc &qualifierTypeLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004194{
Jamie Madill2f294c92017-11-20 14:47:26 -05004195 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004196
4197 if (qualifierType == "shared")
4198 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004199 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004200 {
4201 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "shared");
4202 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004203 qualifier.blockStorage = EbsShared;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004204 }
4205 else if (qualifierType == "packed")
4206 {
Jamie Madillacb4b812016-11-07 13:50:29 -05004207 if (sh::IsWebGLBasedSpec(mShaderSpec))
Olli Etuahof0173152016-10-17 09:05:03 -07004208 {
4209 error(qualifierTypeLine, "Only std140 layout is allowed in WebGL", "packed");
4210 }
Jamie Madilla5efff92013-06-06 11:56:47 -04004211 qualifier.blockStorage = EbsPacked;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004212 }
Qin Jiajiaca68d982017-09-18 16:41:56 +08004213 else if (qualifierType == "std430")
4214 {
4215 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4216 qualifier.blockStorage = EbsStd430;
4217 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004218 else if (qualifierType == "std140")
4219 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004220 qualifier.blockStorage = EbsStd140;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004221 }
4222 else if (qualifierType == "row_major")
4223 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004224 qualifier.matrixPacking = EmpRowMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004225 }
4226 else if (qualifierType == "column_major")
4227 {
Jamie Madilla5efff92013-06-06 11:56:47 -04004228 qualifier.matrixPacking = EmpColumnMajor;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004229 }
4230 else if (qualifierType == "location")
4231 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004232 error(qualifierTypeLine, "invalid layout qualifier: location requires an argument",
4233 qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004234 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004235 else if (qualifierType == "yuv" && mShaderType == GL_FRAGMENT_SHADER)
Andrei Volykhina5527072017-03-22 16:46:30 +03004236 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004237 if (checkCanUseExtension(qualifierTypeLine, TExtension::EXT_YUV_target))
4238 {
4239 qualifier.yuv = true;
4240 }
Andrei Volykhina5527072017-03-22 16:46:30 +03004241 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004242 else if (qualifierType == "rgba32f")
4243 {
4244 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4245 qualifier.imageInternalFormat = EiifRGBA32F;
4246 }
4247 else if (qualifierType == "rgba16f")
4248 {
4249 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4250 qualifier.imageInternalFormat = EiifRGBA16F;
4251 }
4252 else if (qualifierType == "r32f")
4253 {
4254 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4255 qualifier.imageInternalFormat = EiifR32F;
4256 }
4257 else if (qualifierType == "rgba8")
4258 {
4259 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4260 qualifier.imageInternalFormat = EiifRGBA8;
4261 }
4262 else if (qualifierType == "rgba8_snorm")
4263 {
4264 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4265 qualifier.imageInternalFormat = EiifRGBA8_SNORM;
4266 }
4267 else if (qualifierType == "rgba32i")
4268 {
4269 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4270 qualifier.imageInternalFormat = EiifRGBA32I;
4271 }
4272 else if (qualifierType == "rgba16i")
4273 {
4274 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4275 qualifier.imageInternalFormat = EiifRGBA16I;
4276 }
4277 else if (qualifierType == "rgba8i")
4278 {
4279 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4280 qualifier.imageInternalFormat = EiifRGBA8I;
4281 }
4282 else if (qualifierType == "r32i")
4283 {
4284 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4285 qualifier.imageInternalFormat = EiifR32I;
4286 }
4287 else if (qualifierType == "rgba32ui")
4288 {
4289 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4290 qualifier.imageInternalFormat = EiifRGBA32UI;
4291 }
4292 else if (qualifierType == "rgba16ui")
4293 {
4294 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4295 qualifier.imageInternalFormat = EiifRGBA16UI;
4296 }
4297 else if (qualifierType == "rgba8ui")
4298 {
4299 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4300 qualifier.imageInternalFormat = EiifRGBA8UI;
4301 }
4302 else if (qualifierType == "r32ui")
4303 {
4304 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4305 qualifier.imageInternalFormat = EiifR32UI;
4306 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004307 else if (qualifierType == "points" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4308 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004309 {
4310 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4311 qualifier.primitiveType = EptPoints;
4312 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004313 else if (qualifierType == "lines" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4314 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004315 {
4316 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4317 qualifier.primitiveType = EptLines;
4318 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004319 else if (qualifierType == "lines_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4320 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004321 {
4322 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4323 qualifier.primitiveType = EptLinesAdjacency;
4324 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004325 else if (qualifierType == "triangles" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4326 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004327 {
4328 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4329 qualifier.primitiveType = EptTriangles;
4330 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004331 else if (qualifierType == "triangles_adjacency" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4332 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004333 {
4334 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4335 qualifier.primitiveType = EptTrianglesAdjacency;
4336 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004337 else if (qualifierType == "line_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4338 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004339 {
4340 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4341 qualifier.primitiveType = EptLineStrip;
4342 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004343 else if (qualifierType == "triangle_strip" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4344 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004345 {
4346 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4347 qualifier.primitiveType = EptTriangleStrip;
4348 }
Martin Radev2cc85b32016-08-05 16:22:53 +03004349
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004350 else
4351 {
4352 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004353 }
4354
Jamie Madilla5efff92013-06-06 11:56:47 -04004355 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004356}
4357
Martin Radev802abe02016-08-04 17:48:32 +03004358void TParseContext::parseLocalSize(const TString &qualifierType,
4359 const TSourceLoc &qualifierTypeLine,
4360 int intValue,
4361 const TSourceLoc &intValueLine,
4362 const std::string &intValueString,
4363 size_t index,
Martin Radev4c4c8e72016-08-04 12:25:34 +03004364 sh::WorkGroupSize *localSize)
Martin Radev802abe02016-08-04 17:48:32 +03004365{
Olli Etuaho856c4972016-08-08 11:38:39 +03004366 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
Martin Radev802abe02016-08-04 17:48:32 +03004367 if (intValue < 1)
4368 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004369 std::stringstream reasonStream;
4370 reasonStream << "out of range: " << getWorkGroupSizeString(index) << " must be positive";
4371 std::string reason = reasonStream.str();
4372 error(intValueLine, reason.c_str(), intValueString.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004373 }
4374 (*localSize)[index] = intValue;
4375}
4376
Olli Etuaho09b04a22016-12-15 13:30:26 +00004377void TParseContext::parseNumViews(int intValue,
4378 const TSourceLoc &intValueLine,
4379 const std::string &intValueString,
4380 int *numViews)
4381{
4382 // This error is only specified in WebGL, but tightens unspecified behavior in the native
4383 // specification.
4384 if (intValue < 1)
4385 {
4386 error(intValueLine, "out of range: num_views must be positive", intValueString.c_str());
4387 }
4388 *numViews = intValue;
4389}
4390
Shaob5cc1192017-07-06 10:47:20 +08004391void TParseContext::parseInvocations(int intValue,
4392 const TSourceLoc &intValueLine,
4393 const std::string &intValueString,
4394 int *numInvocations)
4395{
4396 // Although SPEC isn't clear whether invocations can be less than 1, we add this limit because
4397 // it doesn't make sense to accept invocations <= 0.
4398 if (intValue < 1 || intValue > mMaxGeometryShaderInvocations)
4399 {
4400 error(intValueLine,
4401 "out of range: invocations must be in the range of [1, "
4402 "MAX_GEOMETRY_SHADER_INVOCATIONS_OES]",
4403 intValueString.c_str());
4404 }
4405 else
4406 {
4407 *numInvocations = intValue;
4408 }
4409}
4410
4411void TParseContext::parseMaxVertices(int intValue,
4412 const TSourceLoc &intValueLine,
4413 const std::string &intValueString,
4414 int *maxVertices)
4415{
4416 // Although SPEC isn't clear whether max_vertices can be less than 0, we add this limit because
4417 // it doesn't make sense to accept max_vertices < 0.
4418 if (intValue < 0 || intValue > mMaxGeometryShaderMaxVertices)
4419 {
4420 error(
4421 intValueLine,
4422 "out of range: max_vertices must be in the range of [0, gl_MaxGeometryOutputVertices]",
4423 intValueString.c_str());
4424 }
4425 else
4426 {
4427 *maxVertices = intValue;
4428 }
4429}
4430
Jamie Madillb98c3a82015-07-23 14:26:04 -04004431TLayoutQualifier TParseContext::parseLayoutQualifier(const TString &qualifierType,
4432 const TSourceLoc &qualifierTypeLine,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004433 int intValue,
Arun Patole7e7e68d2015-05-22 12:02:25 +05304434 const TSourceLoc &intValueLine)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004435{
Jamie Madill2f294c92017-11-20 14:47:26 -05004436 TLayoutQualifier qualifier = TLayoutQualifier::Create();
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004437
Martin Radev802abe02016-08-04 17:48:32 +03004438 std::string intValueString = Str(intValue);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004439
Martin Radev802abe02016-08-04 17:48:32 +03004440 if (qualifierType == "location")
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004441 {
Jamie Madill05a80ce2013-06-20 11:55:49 -04004442 // must check that location is non-negative
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004443 if (intValue < 0)
4444 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004445 error(intValueLine, "out of range: location must be non-negative",
4446 intValueString.c_str());
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004447 }
4448 else
4449 {
Jamie Madilld7b1ab52016-12-12 14:42:19 -05004450 qualifier.location = intValue;
Olli Etuaho87d410c2016-09-05 13:33:26 +03004451 qualifier.locationsSpecified = 1;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004452 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004453 }
Olli Etuaho43364892017-02-13 16:00:12 +00004454 else if (qualifierType == "binding")
4455 {
4456 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4457 if (intValue < 0)
4458 {
4459 error(intValueLine, "out of range: binding must be non-negative",
4460 intValueString.c_str());
4461 }
4462 else
4463 {
4464 qualifier.binding = intValue;
4465 }
4466 }
jchen104cdac9e2017-05-08 11:01:20 +08004467 else if (qualifierType == "offset")
4468 {
4469 checkLayoutQualifierSupported(qualifierTypeLine, qualifierType, 310);
4470 if (intValue < 0)
4471 {
4472 error(intValueLine, "out of range: offset must be non-negative",
4473 intValueString.c_str());
4474 }
4475 else
4476 {
4477 qualifier.offset = intValue;
4478 }
4479 }
Martin Radev802abe02016-08-04 17:48:32 +03004480 else if (qualifierType == "local_size_x")
4481 {
4482 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 0u,
4483 &qualifier.localSize);
4484 }
4485 else if (qualifierType == "local_size_y")
4486 {
4487 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 1u,
4488 &qualifier.localSize);
4489 }
4490 else if (qualifierType == "local_size_z")
4491 {
4492 parseLocalSize(qualifierType, qualifierTypeLine, intValue, intValueLine, intValueString, 2u,
4493 &qualifier.localSize);
4494 }
Olli Etuaho703671e2017-11-08 17:47:18 +02004495 else if (qualifierType == "num_views" && mShaderType == GL_VERTEX_SHADER)
Olli Etuaho09b04a22016-12-15 13:30:26 +00004496 {
Olli Etuaho703671e2017-11-08 17:47:18 +02004497 if (checkCanUseExtension(qualifierTypeLine, TExtension::OVR_multiview))
4498 {
4499 parseNumViews(intValue, intValueLine, intValueString, &qualifier.numViews);
4500 }
Olli Etuaho09b04a22016-12-15 13:30:26 +00004501 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004502 else if (qualifierType == "invocations" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4503 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004504 {
4505 parseInvocations(intValue, intValueLine, intValueString, &qualifier.invocations);
4506 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004507 else if (qualifierType == "max_vertices" && mShaderType == GL_GEOMETRY_SHADER_EXT &&
4508 checkCanUseExtension(qualifierTypeLine, TExtension::EXT_geometry_shader))
Shaob5cc1192017-07-06 10:47:20 +08004509 {
4510 parseMaxVertices(intValue, intValueLine, intValueString, &qualifier.maxVertices);
4511 }
4512
Martin Radev802abe02016-08-04 17:48:32 +03004513 else
4514 {
4515 error(qualifierTypeLine, "invalid layout qualifier", qualifierType.c_str());
Martin Radev802abe02016-08-04 17:48:32 +03004516 }
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004517
Jamie Madilla5efff92013-06-06 11:56:47 -04004518 return qualifier;
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004519}
4520
Olli Etuaho613b9592016-09-05 12:05:53 +03004521TTypeQualifierBuilder *TParseContext::createTypeQualifierBuilder(const TSourceLoc &loc)
4522{
4523 return new TTypeQualifierBuilder(
4524 new TStorageQualifierWrapper(symbolTable.atGlobalLevel() ? EvqGlobal : EvqTemporary, loc),
4525 mShaderVersion);
4526}
4527
Olli Etuahocce89652017-06-19 16:04:09 +03004528TStorageQualifierWrapper *TParseContext::parseGlobalStorageQualifier(TQualifier qualifier,
4529 const TSourceLoc &loc)
4530{
4531 checkIsAtGlobalLevel(loc, getQualifierString(qualifier));
4532 return new TStorageQualifierWrapper(qualifier, loc);
4533}
4534
4535TStorageQualifierWrapper *TParseContext::parseVaryingQualifier(const TSourceLoc &loc)
4536{
4537 if (getShaderType() == GL_VERTEX_SHADER)
4538 {
4539 return parseGlobalStorageQualifier(EvqVaryingOut, loc);
4540 }
4541 return parseGlobalStorageQualifier(EvqVaryingIn, loc);
4542}
4543
4544TStorageQualifierWrapper *TParseContext::parseInQualifier(const TSourceLoc &loc)
4545{
4546 if (declaringFunction())
4547 {
4548 return new TStorageQualifierWrapper(EvqIn, loc);
4549 }
Shaob5cc1192017-07-06 10:47:20 +08004550
4551 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004552 {
Shaob5cc1192017-07-06 10:47:20 +08004553 case GL_VERTEX_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004554 {
Olli Etuaho2a1e8f92017-07-14 11:49:36 +03004555 if (mShaderVersion < 300 && !isExtensionEnabled(TExtension::OVR_multiview))
Shaob5cc1192017-07-06 10:47:20 +08004556 {
4557 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4558 }
4559 return new TStorageQualifierWrapper(EvqVertexIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004560 }
Shaob5cc1192017-07-06 10:47:20 +08004561 case GL_FRAGMENT_SHADER:
Olli Etuahocce89652017-06-19 16:04:09 +03004562 {
Shaob5cc1192017-07-06 10:47:20 +08004563 if (mShaderVersion < 300)
4564 {
4565 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "in");
4566 }
4567 return new TStorageQualifierWrapper(EvqFragmentIn, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004568 }
Shaob5cc1192017-07-06 10:47:20 +08004569 case GL_COMPUTE_SHADER:
4570 {
4571 return new TStorageQualifierWrapper(EvqComputeIn, loc);
4572 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004573 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004574 {
4575 return new TStorageQualifierWrapper(EvqGeometryIn, loc);
4576 }
4577 default:
4578 {
4579 UNREACHABLE();
4580 return new TStorageQualifierWrapper(EvqLast, loc);
4581 }
Olli Etuahocce89652017-06-19 16:04:09 +03004582 }
Olli Etuahocce89652017-06-19 16:04:09 +03004583}
4584
4585TStorageQualifierWrapper *TParseContext::parseOutQualifier(const TSourceLoc &loc)
4586{
4587 if (declaringFunction())
4588 {
4589 return new TStorageQualifierWrapper(EvqOut, loc);
4590 }
Shaob5cc1192017-07-06 10:47:20 +08004591 switch (getShaderType())
Olli Etuahocce89652017-06-19 16:04:09 +03004592 {
Shaob5cc1192017-07-06 10:47:20 +08004593 case GL_VERTEX_SHADER:
4594 {
4595 if (mShaderVersion < 300)
4596 {
4597 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4598 }
4599 return new TStorageQualifierWrapper(EvqVertexOut, loc);
4600 }
4601 case GL_FRAGMENT_SHADER:
4602 {
4603 if (mShaderVersion < 300)
4604 {
4605 error(loc, "storage qualifier supported in GLSL ES 3.00 and above only", "out");
4606 }
4607 return new TStorageQualifierWrapper(EvqFragmentOut, loc);
4608 }
4609 case GL_COMPUTE_SHADER:
4610 {
4611 error(loc, "storage qualifier isn't supported in compute shaders", "out");
4612 return new TStorageQualifierWrapper(EvqLast, loc);
4613 }
Jiawei Shaobd924af2017-11-16 15:28:04 +08004614 case GL_GEOMETRY_SHADER_EXT:
Shaob5cc1192017-07-06 10:47:20 +08004615 {
4616 return new TStorageQualifierWrapper(EvqGeometryOut, loc);
4617 }
4618 default:
4619 {
4620 UNREACHABLE();
4621 return new TStorageQualifierWrapper(EvqLast, loc);
4622 }
Olli Etuahocce89652017-06-19 16:04:09 +03004623 }
Olli Etuahocce89652017-06-19 16:04:09 +03004624}
4625
4626TStorageQualifierWrapper *TParseContext::parseInOutQualifier(const TSourceLoc &loc)
4627{
4628 if (!declaringFunction())
4629 {
4630 error(loc, "invalid qualifier: can be only used with function parameters", "inout");
4631 }
4632 return new TStorageQualifierWrapper(EvqInOut, loc);
4633}
4634
Jamie Madillb98c3a82015-07-23 14:26:04 -04004635TLayoutQualifier TParseContext::joinLayoutQualifiers(TLayoutQualifier leftQualifier,
Martin Radev802abe02016-08-04 17:48:32 +03004636 TLayoutQualifier rightQualifier,
4637 const TSourceLoc &rightQualifierLocation)
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004638{
Martin Radevc28888b2016-07-22 15:27:42 +03004639 return sh::JoinLayoutQualifiers(leftQualifier, rightQualifier, rightQualifierLocation,
Olli Etuaho77ba4082016-12-16 12:01:18 +00004640 mDiagnostics);
shannonwoods@chromium.org302adfe2013-05-30 00:21:06 +00004641}
4642
Olli Etuahod5f44c92017-11-29 17:15:40 +02004643TDeclarator *TParseContext::parseStructDeclarator(const TString *identifier, const TSourceLoc &loc)
Olli Etuahocce89652017-06-19 16:04:09 +03004644{
4645 checkIsNotReserved(loc, *identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004646 return new TDeclarator(identifier, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004647}
4648
Olli Etuahod5f44c92017-11-29 17:15:40 +02004649TDeclarator *TParseContext::parseStructArrayDeclarator(const TString *identifier,
4650 const TSourceLoc &loc,
4651 const TVector<unsigned int> *arraySizes)
Olli Etuahocce89652017-06-19 16:04:09 +03004652{
4653 checkIsNotReserved(loc, *identifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004654 return new TDeclarator(identifier, arraySizes, loc);
Olli Etuahocce89652017-06-19 16:04:09 +03004655}
4656
Olli Etuaho722bfb52017-10-26 17:00:11 +03004657void TParseContext::checkDoesNotHaveDuplicateFieldName(const TFieldList::const_iterator begin,
4658 const TFieldList::const_iterator end,
4659 const TString &name,
4660 const TSourceLoc &location)
4661{
4662 for (auto fieldIter = begin; fieldIter != end; ++fieldIter)
4663 {
4664 if ((*fieldIter)->name() == name)
4665 {
4666 error(location, "duplicate field name in structure", name.c_str());
4667 }
4668 }
4669}
4670
4671TFieldList *TParseContext::addStructFieldList(TFieldList *fields, const TSourceLoc &location)
4672{
4673 for (TFieldList::const_iterator fieldIter = fields->begin(); fieldIter != fields->end();
4674 ++fieldIter)
4675 {
4676 checkDoesNotHaveDuplicateFieldName(fields->begin(), fieldIter, (*fieldIter)->name(),
4677 location);
4678 }
4679 return fields;
4680}
4681
Olli Etuaho4de340a2016-12-16 09:32:03 +00004682TFieldList *TParseContext::combineStructFieldLists(TFieldList *processedFields,
4683 const TFieldList *newlyAddedFields,
4684 const TSourceLoc &location)
4685{
4686 for (TField *field : *newlyAddedFields)
4687 {
Olli Etuaho722bfb52017-10-26 17:00:11 +03004688 checkDoesNotHaveDuplicateFieldName(processedFields->begin(), processedFields->end(),
4689 field->name(), location);
Olli Etuaho4de340a2016-12-16 09:32:03 +00004690 processedFields->push_back(field);
4691 }
4692 return processedFields;
4693}
4694
Martin Radev70866b82016-07-22 15:27:42 +03004695TFieldList *TParseContext::addStructDeclaratorListWithQualifiers(
4696 const TTypeQualifierBuilder &typeQualifierBuilder,
4697 TPublicType *typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004698 const TDeclaratorList *declaratorList)
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004699{
Olli Etuaho77ba4082016-12-16 12:01:18 +00004700 TTypeQualifier typeQualifier = typeQualifierBuilder.getVariableTypeQualifier(mDiagnostics);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004701
Martin Radev70866b82016-07-22 15:27:42 +03004702 typeSpecifier->qualifier = typeQualifier.qualifier;
4703 typeSpecifier->layoutQualifier = typeQualifier.layoutQualifier;
Martin Radev2cc85b32016-08-05 16:22:53 +03004704 typeSpecifier->memoryQualifier = typeQualifier.memoryQualifier;
Martin Radev70866b82016-07-22 15:27:42 +03004705 typeSpecifier->invariant = typeQualifier.invariant;
4706 if (typeQualifier.precision != EbpUndefined)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304707 {
Martin Radev70866b82016-07-22 15:27:42 +03004708 typeSpecifier->precision = typeQualifier.precision;
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004709 }
Olli Etuahod5f44c92017-11-29 17:15:40 +02004710 return addStructDeclaratorList(*typeSpecifier, declaratorList);
Jamie Madillf2e0f9b2013-08-26 16:39:42 -04004711}
4712
Jamie Madillb98c3a82015-07-23 14:26:04 -04004713TFieldList *TParseContext::addStructDeclaratorList(const TPublicType &typeSpecifier,
Olli Etuahod5f44c92017-11-29 17:15:40 +02004714 const TDeclaratorList *declaratorList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004715{
Martin Radev4a9cd802016-09-01 16:51:51 +03004716 checkPrecisionSpecified(typeSpecifier.getLine(), typeSpecifier.precision,
4717 typeSpecifier.getBasicType());
Martin Radev70866b82016-07-22 15:27:42 +03004718
Olli Etuahod5f44c92017-11-29 17:15:40 +02004719 checkIsNonVoid(typeSpecifier.getLine(), *(*declaratorList)[0]->name(),
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004720 typeSpecifier.getBasicType());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004721
Martin Radev4a9cd802016-09-01 16:51:51 +03004722 checkWorkGroupSizeIsNotSpecified(typeSpecifier.getLine(), typeSpecifier.layoutQualifier);
Martin Radev802abe02016-08-04 17:48:32 +03004723
Olli Etuahod5f44c92017-11-29 17:15:40 +02004724 TFieldList *fieldList = new TFieldList();
4725
4726 for (const TDeclarator *declarator : *declaratorList)
Arun Patole7e7e68d2015-05-22 12:02:25 +05304727 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004728 TType *type = new TType(typeSpecifier);
4729 if (declarator->isArray())
Arun Patole7e7e68d2015-05-22 12:02:25 +05304730 {
Olli Etuahod5f44c92017-11-29 17:15:40 +02004731 // Don't allow arrays of arrays in ESSL < 3.10.
Olli Etuahoe0803872017-08-23 15:30:23 +03004732 checkArrayElementIsNotArray(typeSpecifier.getLine(), typeSpecifier);
Olli Etuahod5f44c92017-11-29 17:15:40 +02004733 type->makeArrays(*declarator->arraySizes());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004734 }
Olli Etuaho96f6adf2017-08-16 11:18:54 +03004735
Olli Etuahod5f44c92017-11-29 17:15:40 +02004736 TField *field = new TField(type, declarator->name(), declarator->line());
4737 checkIsBelowStructNestingLimit(typeSpecifier.getLine(), *field);
4738 fieldList->push_back(field);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004739 }
4740
Olli Etuahod5f44c92017-11-29 17:15:40 +02004741 return fieldList;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004742}
4743
Martin Radev4a9cd802016-09-01 16:51:51 +03004744TTypeSpecifierNonArray TParseContext::addStructure(const TSourceLoc &structLine,
4745 const TSourceLoc &nameLine,
4746 const TString *structName,
4747 TFieldList *fieldList)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004748{
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004749 SymbolType structSymbolType = SymbolType::UserDefined;
4750 if (structName == nullptr)
4751 {
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004752 structSymbolType = SymbolType::Empty;
4753 }
4754 TStructure *structure = new TStructure(&symbolTable, structName, fieldList, structSymbolType);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004755
Jamie Madill9b820842015-02-12 10:40:10 -05004756 // Store a bool in the struct if we're at global scope, to allow us to
4757 // skip the local struct scoping workaround in HLSL.
Jamie Madill9b820842015-02-12 10:40:10 -05004758 structure->setAtGlobalScope(symbolTable.atGlobalLevel());
Jamie Madillbfa91f42014-06-05 15:45:18 -04004759
Olli Etuaho9d4d7f02017-12-07 17:11:41 +01004760 if (structSymbolType != SymbolType::Empty)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004761 {
Olli Etuaho856c4972016-08-08 11:38:39 +03004762 checkIsNotReserved(nameLine, *structName);
Olli Etuaho0f684632017-07-13 12:42:15 +03004763 if (!symbolTable.declareStructType(structure))
Arun Patole7e7e68d2015-05-22 12:02:25 +05304764 {
Olli Etuaho4de340a2016-12-16 09:32:03 +00004765 error(nameLine, "redefinition of a struct", structName->c_str());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004766 }
4767 }
4768
4769 // ensure we do not specify any storage qualifiers on the struct members
Jamie Madill98493dd2013-07-08 14:39:03 -04004770 for (unsigned int typeListIndex = 0; typeListIndex < fieldList->size(); typeListIndex++)
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004771 {
Olli Etuahoebee5b32017-11-23 12:56:32 +02004772 TField &field = *(*fieldList)[typeListIndex];
Jamie Madill98493dd2013-07-08 14:39:03 -04004773 const TQualifier qualifier = field.type()->getQualifier();
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004774 switch (qualifier)
4775 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004776 case EvqGlobal:
4777 case EvqTemporary:
4778 break;
4779 default:
4780 error(field.line(), "invalid qualifier on struct member",
4781 getQualifierString(qualifier));
Jamie Madillb98c3a82015-07-23 14:26:04 -04004782 break;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004783 }
Martin Radev70866b82016-07-22 15:27:42 +03004784 if (field.type()->isInvariant())
4785 {
4786 error(field.line(), "invalid qualifier on struct member", "invariant");
4787 }
jchen104cdac9e2017-05-08 11:01:20 +08004788 // ESSL 3.10 section 4.1.8 -- atomic_uint or images are not allowed as structure member.
4789 if (IsImage(field.type()->getBasicType()) || IsAtomicCounter(field.type()->getBasicType()))
Martin Radev2cc85b32016-08-05 16:22:53 +03004790 {
4791 error(field.line(), "disallowed type in struct", field.type()->getBasicString());
4792 }
4793
Olli Etuahoebee5b32017-11-23 12:56:32 +02004794 checkIsNotUnsizedArray(field.line(), "array members of structs must specify a size",
4795 field.name().c_str(), field.type());
4796
Olli Etuaho43364892017-02-13 16:00:12 +00004797 checkMemoryQualifierIsNotSpecified(field.type()->getMemoryQualifier(), field.line());
4798
4799 checkBindingIsNotSpecified(field.line(), field.type()->getLayoutQualifier().binding);
Martin Radev70866b82016-07-22 15:27:42 +03004800
4801 checkLocationIsNotSpecified(field.line(), field.type()->getLayoutQualifier());
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004802 }
4803
Martin Radev4a9cd802016-09-01 16:51:51 +03004804 TTypeSpecifierNonArray typeSpecifierNonArray;
Olli Etuaho0f684632017-07-13 12:42:15 +03004805 typeSpecifierNonArray.initializeStruct(structure, true, structLine);
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004806 exitStructDeclaration();
4807
Martin Radev4a9cd802016-09-01 16:51:51 +03004808 return typeSpecifierNonArray;
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00004809}
4810
Jamie Madillb98c3a82015-07-23 14:26:04 -04004811TIntermSwitch *TParseContext::addSwitch(TIntermTyped *init,
Olli Etuaho6d40bbd2016-09-30 13:49:38 +01004812 TIntermBlock *statementList,
Jamie Madillb98c3a82015-07-23 14:26:04 -04004813 const TSourceLoc &loc)
Olli Etuahoa3a36662015-02-17 13:46:51 +02004814{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004815 TBasicType switchType = init->getBasicType();
Jamie Madillb98c3a82015-07-23 14:26:04 -04004816 if ((switchType != EbtInt && switchType != EbtUInt) || init->isMatrix() || init->isArray() ||
Olli Etuaho53f076f2015-02-20 10:55:14 +02004817 init->isVector())
4818 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004819 error(init->getLine(), "init-expression in a switch statement must be a scalar integer",
4820 "switch");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004821 return nullptr;
4822 }
4823
Olli Etuaho923ecef2017-10-11 12:01:38 +03004824 ASSERT(statementList);
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004825 if (!ValidateSwitchStatementList(switchType, mShaderVersion, mDiagnostics, statementList, loc))
Olli Etuahoac5274d2015-02-20 10:19:08 +02004826 {
Olli Etuahocbcb96f2017-10-19 14:14:06 +03004827 ASSERT(mDiagnostics->numErrors() > 0);
Olli Etuaho923ecef2017-10-11 12:01:38 +03004828 return nullptr;
Olli Etuahoac5274d2015-02-20 10:19:08 +02004829 }
4830
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004831 TIntermSwitch *node = new TIntermSwitch(init, statementList);
4832 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004833 return node;
4834}
4835
4836TIntermCase *TParseContext::addCase(TIntermTyped *condition, const TSourceLoc &loc)
4837{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004838 if (mSwitchNestingLevel == 0)
4839 {
4840 error(loc, "case labels need to be inside switch statements", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004841 return nullptr;
4842 }
4843 if (condition == nullptr)
4844 {
4845 error(loc, "case label must have a condition", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004846 return nullptr;
4847 }
4848 if ((condition->getBasicType() != EbtInt && condition->getBasicType() != EbtUInt) ||
Jamie Madillb98c3a82015-07-23 14:26:04 -04004849 condition->isMatrix() || condition->isArray() || condition->isVector())
Olli Etuaho53f076f2015-02-20 10:55:14 +02004850 {
4851 error(condition->getLine(), "case label must be a scalar integer", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004852 }
4853 TIntermConstantUnion *conditionConst = condition->getAsConstantUnion();
Olli Etuaho7c3848e2015-11-04 13:19:17 +02004854 // TODO(oetuaho@nvidia.com): Get rid of the conditionConst == nullptr check once all constant
4855 // expressions can be folded. Right now we don't allow constant expressions that ANGLE can't
4856 // fold in case labels.
4857 if (condition->getQualifier() != EvqConst || conditionConst == nullptr)
Olli Etuaho53f076f2015-02-20 10:55:14 +02004858 {
4859 error(condition->getLine(), "case label must be constant", "case");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004860 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004861 TIntermCase *node = new TIntermCase(condition);
4862 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004863 return node;
4864}
4865
4866TIntermCase *TParseContext::addDefault(const TSourceLoc &loc)
4867{
Olli Etuaho53f076f2015-02-20 10:55:14 +02004868 if (mSwitchNestingLevel == 0)
4869 {
4870 error(loc, "default labels need to be inside switch statements", "default");
Olli Etuaho53f076f2015-02-20 10:55:14 +02004871 return nullptr;
4872 }
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004873 TIntermCase *node = new TIntermCase(nullptr);
4874 node->setLine(loc);
Olli Etuahoa3a36662015-02-17 13:46:51 +02004875 return node;
4876}
4877
Jamie Madillb98c3a82015-07-23 14:26:04 -04004878TIntermTyped *TParseContext::createUnaryMath(TOperator op,
4879 TIntermTyped *child,
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004880 const TSourceLoc &loc)
Olli Etuaho69c11b52015-03-26 12:59:00 +02004881{
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004882 ASSERT(child != nullptr);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004883
4884 switch (op)
4885 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04004886 case EOpLogicalNot:
4887 if (child->getBasicType() != EbtBool || child->isMatrix() || child->isArray() ||
4888 child->isVector())
4889 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004890 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004891 return nullptr;
4892 }
4893 break;
4894 case EOpBitwiseNot:
4895 if ((child->getBasicType() != EbtInt && child->getBasicType() != EbtUInt) ||
4896 child->isMatrix() || child->isArray())
4897 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004898 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004899 return nullptr;
4900 }
4901 break;
4902 case EOpPostIncrement:
4903 case EOpPreIncrement:
4904 case EOpPostDecrement:
4905 case EOpPreDecrement:
4906 case EOpNegative:
4907 case EOpPositive:
Olli Etuaho94050052017-05-08 14:17:44 +03004908 if (child->getBasicType() == EbtStruct || child->isInterfaceBlock() ||
4909 child->getBasicType() == EbtBool || child->isArray() ||
4910 IsOpaqueType(child->getBasicType()))
Jamie Madillb98c3a82015-07-23 14:26:04 -04004911 {
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004912 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
Jamie Madillb98c3a82015-07-23 14:26:04 -04004913 return nullptr;
4914 }
4915 // Operators for built-ins are already type checked against their prototype.
4916 default:
4917 break;
Olli Etuaho69c11b52015-03-26 12:59:00 +02004918 }
4919
Jiajia Qinbc585152017-06-23 15:42:17 +08004920 if (child->getMemoryQualifier().writeonly)
4921 {
4922 unaryOpError(loc, GetOperatorString(op), child->getCompleteString());
4923 return nullptr;
4924 }
4925
Olli Etuahof119a262016-08-19 15:54:22 +03004926 TIntermUnary *node = new TIntermUnary(op, child);
4927 node->setLine(loc);
Olli Etuahof119a262016-08-19 15:54:22 +03004928
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03004929 return node->fold(mDiagnostics);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004930}
4931
Olli Etuaho09b22472015-02-11 11:47:26 +02004932TIntermTyped *TParseContext::addUnaryMath(TOperator op, TIntermTyped *child, const TSourceLoc &loc)
4933{
Olli Etuahocce89652017-06-19 16:04:09 +03004934 ASSERT(op != EOpNull);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08004935 TIntermTyped *node = createUnaryMath(op, child, loc);
Olli Etuaho69c11b52015-03-26 12:59:00 +02004936 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02004937 {
Olli Etuaho09b22472015-02-11 11:47:26 +02004938 return child;
4939 }
4940 return node;
4941}
4942
Jamie Madillb98c3a82015-07-23 14:26:04 -04004943TIntermTyped *TParseContext::addUnaryMathLValue(TOperator op,
4944 TIntermTyped *child,
4945 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02004946{
Olli Etuaho856c4972016-08-08 11:38:39 +03004947 checkCanBeLValue(loc, GetOperatorString(op), child);
Olli Etuaho09b22472015-02-11 11:47:26 +02004948 return addUnaryMath(op, child, loc);
4949}
4950
Olli Etuaho765924f2018-01-04 12:48:36 +02004951TIntermTyped *TParseContext::expressionOrFoldedResult(TIntermTyped *expression)
4952{
4953 // If we can, we should return the folded version of the expression for subsequent parsing. This
4954 // enables folding the containing expression during parsing as well, instead of the separate
4955 // FoldExpressions() step where folding nested expressions requires multiple full AST
4956 // traversals.
4957
4958 // Even if folding fails the fold() functions return some node representing the expression,
4959 // typically the original node. So "folded" can be assumed to be non-null.
4960 TIntermTyped *folded = expression->fold(mDiagnostics);
4961 ASSERT(folded != nullptr);
4962 if (folded->getQualifier() == expression->getQualifier())
4963 {
4964 // We need this expression to have the correct qualifier when validating the consuming
4965 // expression. So we can only return the folded node from here in case it has the same
4966 // qualifier as the original expression. In this kind of a cases the qualifier of the folded
4967 // node is EvqConst, whereas the qualifier of the expression is EvqTemporary:
4968 // 1. (true ? 1.0 : non_constant)
4969 // 2. (non_constant, 1.0)
4970 return folded;
4971 }
4972 return expression;
4973}
4974
Jamie Madillb98c3a82015-07-23 14:26:04 -04004975bool TParseContext::binaryOpCommonCheck(TOperator op,
4976 TIntermTyped *left,
4977 TIntermTyped *right,
4978 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02004979{
jchen10b4cf5652017-05-05 18:51:17 +08004980 // Check opaque types are not allowed to be operands in expressions other than array indexing
4981 // and structure member selection.
4982 if (IsOpaqueType(left->getBasicType()) || IsOpaqueType(right->getBasicType()))
4983 {
4984 switch (op)
4985 {
4986 case EOpIndexDirect:
4987 case EOpIndexIndirect:
4988 break;
4989 case EOpIndexDirectStruct:
4990 UNREACHABLE();
4991
4992 default:
4993 error(loc, "Invalid operation for variables with an opaque type",
4994 GetOperatorString(op));
4995 return false;
4996 }
4997 }
jchen10cc2a10e2017-05-03 14:05:12 +08004998
Jiajia Qinbc585152017-06-23 15:42:17 +08004999 if (right->getMemoryQualifier().writeonly)
5000 {
5001 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5002 return false;
5003 }
5004
5005 if (left->getMemoryQualifier().writeonly)
5006 {
5007 switch (op)
5008 {
5009 case EOpAssign:
5010 case EOpInitialize:
5011 case EOpIndexDirect:
5012 case EOpIndexIndirect:
5013 case EOpIndexDirectStruct:
5014 case EOpIndexDirectInterfaceBlock:
5015 break;
5016 default:
5017 error(loc, "Invalid operation for variables with writeonly", GetOperatorString(op));
5018 return false;
5019 }
5020 }
5021
Olli Etuaho244be012016-08-18 15:26:02 +03005022 if (left->getType().getStruct() || right->getType().getStruct())
5023 {
5024 switch (op)
5025 {
5026 case EOpIndexDirectStruct:
5027 ASSERT(left->getType().getStruct());
5028 break;
5029 case EOpEqual:
5030 case EOpNotEqual:
5031 case EOpAssign:
5032 case EOpInitialize:
5033 if (left->getType() != right->getType())
5034 {
5035 return false;
5036 }
5037 break;
5038 default:
5039 error(loc, "Invalid operation for structs", GetOperatorString(op));
5040 return false;
5041 }
5042 }
5043
Olli Etuaho94050052017-05-08 14:17:44 +03005044 if (left->isInterfaceBlock() || right->isInterfaceBlock())
5045 {
5046 switch (op)
5047 {
5048 case EOpIndexDirectInterfaceBlock:
5049 ASSERT(left->getType().getInterfaceBlock());
5050 break;
5051 default:
5052 error(loc, "Invalid operation for interface blocks", GetOperatorString(op));
5053 return false;
5054 }
5055 }
5056
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005057 if (left->isArray() != right->isArray())
Olli Etuahod6b14282015-03-17 14:31:35 +02005058 {
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005059 error(loc, "array / non-array mismatch", GetOperatorString(op));
5060 return false;
5061 }
5062
5063 if (left->isArray())
5064 {
5065 ASSERT(right->isArray());
Jamie Madill6e06b1f2015-05-14 10:01:17 -04005066 if (mShaderVersion < 300)
Olli Etuahoe79904c2015-03-18 16:56:42 +02005067 {
5068 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5069 return false;
5070 }
5071
Olli Etuahoe79904c2015-03-18 16:56:42 +02005072 switch (op)
5073 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005074 case EOpEqual:
5075 case EOpNotEqual:
5076 case EOpAssign:
5077 case EOpInitialize:
5078 break;
5079 default:
5080 error(loc, "Invalid operation for arrays", GetOperatorString(op));
5081 return false;
Olli Etuahoe79904c2015-03-18 16:56:42 +02005082 }
Olli Etuaho376f1b52015-04-13 13:23:41 +03005083 // At this point, size of implicitly sized arrays should be resolved.
Kai Ninomiya57ea5332017-11-22 14:04:48 -08005084 if (*left->getType().getArraySizes() != *right->getType().getArraySizes())
Olli Etuahoe79904c2015-03-18 16:56:42 +02005085 {
5086 error(loc, "array size mismatch", GetOperatorString(op));
5087 return false;
5088 }
Olli Etuahod6b14282015-03-17 14:31:35 +02005089 }
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005090
5091 // Check ops which require integer / ivec parameters
5092 bool isBitShift = false;
5093 switch (op)
5094 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005095 case EOpBitShiftLeft:
5096 case EOpBitShiftRight:
5097 case EOpBitShiftLeftAssign:
5098 case EOpBitShiftRightAssign:
5099 // Unsigned can be bit-shifted by signed and vice versa, but we need to
5100 // check that the basic type is an integer type.
5101 isBitShift = true;
5102 if (!IsInteger(left->getBasicType()) || !IsInteger(right->getBasicType()))
5103 {
5104 return false;
5105 }
5106 break;
5107 case EOpBitwiseAnd:
5108 case EOpBitwiseXor:
5109 case EOpBitwiseOr:
5110 case EOpBitwiseAndAssign:
5111 case EOpBitwiseXorAssign:
5112 case EOpBitwiseOrAssign:
5113 // It is enough to check the type of only one operand, since later it
5114 // is checked that the operand types match.
5115 if (!IsInteger(left->getBasicType()))
5116 {
5117 return false;
5118 }
5119 break;
5120 default:
5121 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005122 }
5123
5124 // GLSL ES 1.00 and 3.00 do not support implicit type casting.
5125 // So the basic type should usually match.
5126 if (!isBitShift && left->getBasicType() != right->getBasicType())
5127 {
5128 return false;
5129 }
5130
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005131 // Check that:
5132 // 1. Type sizes match exactly on ops that require that.
5133 // 2. Restrictions for structs that contain arrays or samplers are respected.
5134 // 3. Arithmetic op type dimensionality restrictions for ops other than multiply are respected.
Jamie Madillb98c3a82015-07-23 14:26:04 -04005135 switch (op)
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005136 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005137 case EOpAssign:
5138 case EOpInitialize:
5139 case EOpEqual:
5140 case EOpNotEqual:
5141 // ESSL 1.00 sections 5.7, 5.8, 5.9
5142 if (mShaderVersion < 300 && left->getType().isStructureContainingArrays())
5143 {
5144 error(loc, "undefined operation for structs containing arrays",
5145 GetOperatorString(op));
5146 return false;
5147 }
5148 // Samplers as l-values are disallowed also in ESSL 3.00, see section 4.1.7,
5149 // we interpret the spec so that this extends to structs containing samplers,
5150 // similarly to ESSL 1.00 spec.
5151 if ((mShaderVersion < 300 || op == EOpAssign || op == EOpInitialize) &&
5152 left->getType().isStructureContainingSamplers())
5153 {
5154 error(loc, "undefined operation for structs containing samplers",
5155 GetOperatorString(op));
5156 return false;
5157 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005158
Olli Etuahoe1805592017-01-02 16:41:20 +00005159 if ((left->getNominalSize() != right->getNominalSize()) ||
5160 (left->getSecondarySize() != right->getSecondarySize()))
5161 {
5162 error(loc, "dimension mismatch", GetOperatorString(op));
5163 return false;
5164 }
5165 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005166 case EOpLessThan:
5167 case EOpGreaterThan:
5168 case EOpLessThanEqual:
5169 case EOpGreaterThanEqual:
Olli Etuahoe1805592017-01-02 16:41:20 +00005170 if (!left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005171 {
Olli Etuahoe1805592017-01-02 16:41:20 +00005172 error(loc, "comparison operator only defined for scalars", GetOperatorString(op));
Jamie Madillb98c3a82015-07-23 14:26:04 -04005173 return false;
5174 }
Olli Etuaho63e1ec52016-08-18 22:05:12 +03005175 break;
5176 case EOpAdd:
5177 case EOpSub:
5178 case EOpDiv:
5179 case EOpIMod:
5180 case EOpBitShiftLeft:
5181 case EOpBitShiftRight:
5182 case EOpBitwiseAnd:
5183 case EOpBitwiseXor:
5184 case EOpBitwiseOr:
5185 case EOpAddAssign:
5186 case EOpSubAssign:
5187 case EOpDivAssign:
5188 case EOpIModAssign:
5189 case EOpBitShiftLeftAssign:
5190 case EOpBitShiftRightAssign:
5191 case EOpBitwiseAndAssign:
5192 case EOpBitwiseXorAssign:
5193 case EOpBitwiseOrAssign:
5194 if ((left->isMatrix() && right->isVector()) || (left->isVector() && right->isMatrix()))
5195 {
5196 return false;
5197 }
5198
5199 // Are the sizes compatible?
5200 if (left->getNominalSize() != right->getNominalSize() ||
5201 left->getSecondarySize() != right->getSecondarySize())
5202 {
5203 // If the nominal sizes of operands do not match:
5204 // One of them must be a scalar.
5205 if (!left->isScalar() && !right->isScalar())
5206 return false;
5207
5208 // In the case of compound assignment other than multiply-assign,
5209 // the right side needs to be a scalar. Otherwise a vector/matrix
5210 // would be assigned to a scalar. A scalar can't be shifted by a
5211 // vector either.
5212 if (!right->isScalar() &&
5213 (IsAssignment(op) || op == EOpBitShiftLeft || op == EOpBitShiftRight))
5214 return false;
5215 }
5216 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005217 default:
5218 break;
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005219 }
5220
Olli Etuahod6b14282015-03-17 14:31:35 +02005221 return true;
5222}
5223
Olli Etuaho1dded802016-08-18 18:13:13 +03005224bool TParseContext::isMultiplicationTypeCombinationValid(TOperator op,
5225 const TType &left,
5226 const TType &right)
5227{
5228 switch (op)
5229 {
5230 case EOpMul:
5231 case EOpMulAssign:
5232 return left.getNominalSize() == right.getNominalSize() &&
5233 left.getSecondarySize() == right.getSecondarySize();
5234 case EOpVectorTimesScalar:
5235 return true;
5236 case EOpVectorTimesScalarAssign:
5237 ASSERT(!left.isMatrix() && !right.isMatrix());
5238 return left.isVector() && !right.isVector();
5239 case EOpVectorTimesMatrix:
5240 return left.getNominalSize() == right.getRows();
5241 case EOpVectorTimesMatrixAssign:
5242 ASSERT(!left.isMatrix() && right.isMatrix());
5243 return left.isVector() && left.getNominalSize() == right.getRows() &&
5244 left.getNominalSize() == right.getCols();
5245 case EOpMatrixTimesVector:
5246 return left.getCols() == right.getNominalSize();
5247 case EOpMatrixTimesScalar:
5248 return true;
5249 case EOpMatrixTimesScalarAssign:
5250 ASSERT(left.isMatrix() && !right.isMatrix());
5251 return !right.isVector();
5252 case EOpMatrixTimesMatrix:
5253 return left.getCols() == right.getRows();
5254 case EOpMatrixTimesMatrixAssign:
5255 ASSERT(left.isMatrix() && right.isMatrix());
5256 // We need to check two things:
5257 // 1. The matrix multiplication step is valid.
5258 // 2. The result will have the same number of columns as the lvalue.
5259 return left.getCols() == right.getRows() && left.getCols() == right.getCols();
5260
5261 default:
5262 UNREACHABLE();
5263 return false;
5264 }
5265}
5266
Jamie Madillb98c3a82015-07-23 14:26:04 -04005267TIntermTyped *TParseContext::addBinaryMathInternal(TOperator op,
5268 TIntermTyped *left,
5269 TIntermTyped *right,
5270 const TSourceLoc &loc)
Olli Etuahofc1806e2015-03-17 13:03:11 +02005271{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005272 if (!binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005273 return nullptr;
5274
Olli Etuahofc1806e2015-03-17 13:03:11 +02005275 switch (op)
5276 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005277 case EOpEqual:
5278 case EOpNotEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005279 case EOpLessThan:
5280 case EOpGreaterThan:
5281 case EOpLessThanEqual:
5282 case EOpGreaterThanEqual:
Jamie Madillb98c3a82015-07-23 14:26:04 -04005283 break;
5284 case EOpLogicalOr:
5285 case EOpLogicalXor:
5286 case EOpLogicalAnd:
Olli Etuaho244be012016-08-18 15:26:02 +03005287 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5288 !right->getType().getStruct());
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005289 if (left->getBasicType() != EbtBool || !left->isScalar() || !right->isScalar())
Jamie Madillb98c3a82015-07-23 14:26:04 -04005290 {
5291 return nullptr;
5292 }
Olli Etuahoe7dc9d72016-11-03 16:58:47 +00005293 // Basic types matching should have been already checked.
5294 ASSERT(right->getBasicType() == EbtBool);
Jamie Madillb98c3a82015-07-23 14:26:04 -04005295 break;
5296 case EOpAdd:
5297 case EOpSub:
5298 case EOpDiv:
5299 case EOpMul:
Olli Etuaho244be012016-08-18 15:26:02 +03005300 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5301 !right->getType().getStruct());
5302 if (left->getBasicType() == EbtBool)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005303 {
5304 return nullptr;
5305 }
5306 break;
5307 case EOpIMod:
Olli Etuaho244be012016-08-18 15:26:02 +03005308 ASSERT(!left->isArray() && !right->isArray() && !left->getType().getStruct() &&
5309 !right->getType().getStruct());
Jamie Madillb98c3a82015-07-23 14:26:04 -04005310 // Note that this is only for the % operator, not for mod()
Olli Etuaho244be012016-08-18 15:26:02 +03005311 if (left->getBasicType() == EbtBool || left->getBasicType() == EbtFloat)
Jamie Madillb98c3a82015-07-23 14:26:04 -04005312 {
5313 return nullptr;
5314 }
5315 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005316 default:
5317 break;
Olli Etuahofc1806e2015-03-17 13:03:11 +02005318 }
5319
Olli Etuaho1dded802016-08-18 18:13:13 +03005320 if (op == EOpMul)
5321 {
5322 op = TIntermBinary::GetMulOpBasedOnOperands(left->getType(), right->getType());
5323 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5324 {
5325 return nullptr;
5326 }
5327 }
5328
Olli Etuaho3fdec912016-08-18 15:08:06 +03005329 TIntermBinary *node = new TIntermBinary(op, left, right);
5330 node->setLine(loc);
Olli Etuahoea22b7a2018-01-04 17:09:11 +02005331 return expressionOrFoldedResult(node);
Olli Etuahofc1806e2015-03-17 13:03:11 +02005332}
5333
Jamie Madillb98c3a82015-07-23 14:26:04 -04005334TIntermTyped *TParseContext::addBinaryMath(TOperator op,
5335 TIntermTyped *left,
5336 TIntermTyped *right,
5337 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005338{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005339 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005340 if (node == 0)
5341 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005342 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5343 right->getCompleteString());
Olli Etuaho09b22472015-02-11 11:47:26 +02005344 return left;
5345 }
5346 return node;
5347}
5348
Jamie Madillb98c3a82015-07-23 14:26:04 -04005349TIntermTyped *TParseContext::addBinaryMathBooleanResult(TOperator op,
5350 TIntermTyped *left,
5351 TIntermTyped *right,
5352 const TSourceLoc &loc)
Olli Etuaho09b22472015-02-11 11:47:26 +02005353{
Olli Etuahofc1806e2015-03-17 13:03:11 +02005354 TIntermTyped *node = addBinaryMathInternal(op, left, right, loc);
Olli Etuaho56229f12017-07-10 14:16:33 +03005355 if (node == nullptr)
Olli Etuaho09b22472015-02-11 11:47:26 +02005356 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005357 binaryOpError(loc, GetOperatorString(op), left->getCompleteString(),
5358 right->getCompleteString());
Olli Etuaho3ec75682017-07-05 17:02:55 +03005359 node = CreateBoolNode(false);
Olli Etuaho56229f12017-07-10 14:16:33 +03005360 node->setLine(loc);
Olli Etuaho09b22472015-02-11 11:47:26 +02005361 }
5362 return node;
5363}
5364
Olli Etuaho13389b62016-10-16 11:48:18 +01005365TIntermBinary *TParseContext::createAssign(TOperator op,
5366 TIntermTyped *left,
5367 TIntermTyped *right,
5368 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005369{
Olli Etuaho47fd36a2015-03-19 14:22:24 +02005370 if (binaryOpCommonCheck(op, left, right, loc))
Olli Etuahod6b14282015-03-17 14:31:35 +02005371 {
Olli Etuaho1dded802016-08-18 18:13:13 +03005372 if (op == EOpMulAssign)
5373 {
5374 op = TIntermBinary::GetMulAssignOpBasedOnOperands(left->getType(), right->getType());
5375 if (!isMultiplicationTypeCombinationValid(op, left->getType(), right->getType()))
5376 {
5377 return nullptr;
5378 }
5379 }
Olli Etuaho3fdec912016-08-18 15:08:06 +03005380 TIntermBinary *node = new TIntermBinary(op, left, right);
5381 node->setLine(loc);
5382
Olli Etuaho3fdec912016-08-18 15:08:06 +03005383 return node;
Olli Etuahod6b14282015-03-17 14:31:35 +02005384 }
5385 return nullptr;
5386}
5387
Jamie Madillb98c3a82015-07-23 14:26:04 -04005388TIntermTyped *TParseContext::addAssign(TOperator op,
5389 TIntermTyped *left,
5390 TIntermTyped *right,
5391 const TSourceLoc &loc)
Olli Etuahod6b14282015-03-17 14:31:35 +02005392{
Olli Etuahocce89652017-06-19 16:04:09 +03005393 checkCanBeLValue(loc, "assign", left);
Olli Etuahod6b14282015-03-17 14:31:35 +02005394 TIntermTyped *node = createAssign(op, left, right, loc);
5395 if (node == nullptr)
5396 {
5397 assignError(loc, "assign", left->getCompleteString(), right->getCompleteString());
Olli Etuahod6b14282015-03-17 14:31:35 +02005398 return left;
5399 }
5400 return node;
5401}
5402
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005403TIntermTyped *TParseContext::addComma(TIntermTyped *left,
5404 TIntermTyped *right,
5405 const TSourceLoc &loc)
5406{
Corentin Wallez0d959252016-07-12 17:26:32 -04005407 // WebGL2 section 5.26, the following results in an error:
5408 // "Sequence operator applied to void, arrays, or structs containing arrays"
Jamie Madilld7b1ab52016-12-12 14:42:19 -05005409 if (mShaderSpec == SH_WEBGL2_SPEC &&
5410 (left->isArray() || left->getBasicType() == EbtVoid ||
5411 left->getType().isStructureContainingArrays() || right->isArray() ||
5412 right->getBasicType() == EbtVoid || right->getType().isStructureContainingArrays()))
Corentin Wallez0d959252016-07-12 17:26:32 -04005413 {
5414 error(loc,
5415 "sequence operator is not allowed for void, arrays, or structs containing arrays",
5416 ",");
Corentin Wallez0d959252016-07-12 17:26:32 -04005417 }
5418
Olli Etuaho0e99b7a2018-01-12 12:05:48 +02005419 TIntermBinary *commaNode = TIntermBinary::CreateComma(left, right, mShaderVersion);
Olli Etuaho765924f2018-01-04 12:48:36 +02005420
5421 return expressionOrFoldedResult(commaNode);
Olli Etuaho0b2d2dc2015-11-04 16:35:32 +02005422}
5423
Olli Etuaho49300862015-02-20 14:54:49 +02005424TIntermBranch *TParseContext::addBranch(TOperator op, const TSourceLoc &loc)
5425{
5426 switch (op)
5427 {
Jamie Madillb98c3a82015-07-23 14:26:04 -04005428 case EOpContinue:
5429 if (mLoopNestingLevel <= 0)
5430 {
5431 error(loc, "continue statement only allowed in loops", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005432 }
5433 break;
5434 case EOpBreak:
5435 if (mLoopNestingLevel <= 0 && mSwitchNestingLevel <= 0)
5436 {
5437 error(loc, "break statement only allowed in loops and switch statements", "");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005438 }
5439 break;
5440 case EOpReturn:
5441 if (mCurrentFunctionType->getBasicType() != EbtVoid)
5442 {
5443 error(loc, "non-void function must return a value", "return");
Jamie Madillb98c3a82015-07-23 14:26:04 -04005444 }
5445 break;
Olli Etuahocce89652017-06-19 16:04:09 +03005446 case EOpKill:
5447 if (mShaderType != GL_FRAGMENT_SHADER)
5448 {
5449 error(loc, "discard supported in fragment shaders only", "discard");
5450 }
5451 break;
Jamie Madillb98c3a82015-07-23 14:26:04 -04005452 default:
Olli Etuahocce89652017-06-19 16:04:09 +03005453 UNREACHABLE();
Jamie Madillb98c3a82015-07-23 14:26:04 -04005454 break;
Olli Etuaho49300862015-02-20 14:54:49 +02005455 }
Olli Etuahocce89652017-06-19 16:04:09 +03005456 return addBranch(op, nullptr, loc);
Olli Etuaho49300862015-02-20 14:54:49 +02005457}
5458
Jamie Madillb98c3a82015-07-23 14:26:04 -04005459TIntermBranch *TParseContext::addBranch(TOperator op,
Olli Etuahocce89652017-06-19 16:04:09 +03005460 TIntermTyped *expression,
Jamie Madillb98c3a82015-07-23 14:26:04 -04005461 const TSourceLoc &loc)
Olli Etuaho49300862015-02-20 14:54:49 +02005462{
Olli Etuahocce89652017-06-19 16:04:09 +03005463 if (expression != nullptr)
Olli Etuaho49300862015-02-20 14:54:49 +02005464 {
Olli Etuahocce89652017-06-19 16:04:09 +03005465 ASSERT(op == EOpReturn);
5466 mFunctionReturnsValue = true;
5467 if (mCurrentFunctionType->getBasicType() == EbtVoid)
5468 {
5469 error(loc, "void function cannot return a value", "return");
5470 }
5471 else if (*mCurrentFunctionType != expression->getType())
5472 {
5473 error(loc, "function return is not matching type:", "return");
5474 }
Olli Etuaho49300862015-02-20 14:54:49 +02005475 }
Olli Etuahocce89652017-06-19 16:04:09 +03005476 TIntermBranch *node = new TIntermBranch(op, expression);
5477 node->setLine(loc);
5478 return node;
Olli Etuaho49300862015-02-20 14:54:49 +02005479}
5480
Martin Radev84aa2dc2017-09-11 15:51:02 +03005481void TParseContext::checkTextureGather(TIntermAggregate *functionCall)
5482{
5483 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobed35d72017-12-20 16:36:26 +02005484 const TString &name = functionCall->getFunction()->name();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005485 bool isTextureGather = (name == "textureGather");
5486 bool isTextureGatherOffset = (name == "textureGatherOffset");
5487 if (isTextureGather || isTextureGatherOffset)
5488 {
5489 TIntermNode *componentNode = nullptr;
5490 TIntermSequence *arguments = functionCall->getSequence();
5491 ASSERT(arguments->size() >= 2u && arguments->size() <= 4u);
5492 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5493 ASSERT(sampler != nullptr);
5494 switch (sampler->getBasicType())
5495 {
5496 case EbtSampler2D:
5497 case EbtISampler2D:
5498 case EbtUSampler2D:
5499 case EbtSampler2DArray:
5500 case EbtISampler2DArray:
5501 case EbtUSampler2DArray:
5502 if ((isTextureGather && arguments->size() == 3u) ||
5503 (isTextureGatherOffset && arguments->size() == 4u))
5504 {
5505 componentNode = arguments->back();
5506 }
5507 break;
5508 case EbtSamplerCube:
5509 case EbtISamplerCube:
5510 case EbtUSamplerCube:
5511 ASSERT(!isTextureGatherOffset);
5512 if (arguments->size() == 3u)
5513 {
5514 componentNode = arguments->back();
5515 }
5516 break;
5517 case EbtSampler2DShadow:
5518 case EbtSampler2DArrayShadow:
5519 case EbtSamplerCubeShadow:
5520 break;
5521 default:
5522 UNREACHABLE();
5523 break;
5524 }
5525 if (componentNode)
5526 {
5527 const TIntermConstantUnion *componentConstantUnion =
5528 componentNode->getAsConstantUnion();
5529 if (componentNode->getAsTyped()->getQualifier() != EvqConst || !componentConstantUnion)
5530 {
5531 error(functionCall->getLine(), "Texture component must be a constant expression",
5532 name.c_str());
5533 }
5534 else
5535 {
5536 int component = componentConstantUnion->getIConst(0);
5537 if (component < 0 || component > 3)
5538 {
5539 error(functionCall->getLine(), "Component must be in the range [0;3]",
5540 name.c_str());
5541 }
5542 }
5543 }
5544 }
5545}
5546
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005547void TParseContext::checkTextureOffsetConst(TIntermAggregate *functionCall)
5548{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005549 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobed35d72017-12-20 16:36:26 +02005550 const TString &name = functionCall->getFunction()->name();
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005551 TIntermNode *offset = nullptr;
5552 TIntermSequence *arguments = functionCall->getSequence();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005553 bool useTextureGatherOffsetConstraints = false;
Olli Etuahoec9232b2017-03-27 17:01:37 +03005554 if (name == "texelFetchOffset" || name == "textureLodOffset" ||
5555 name == "textureProjLodOffset" || name == "textureGradOffset" ||
5556 name == "textureProjGradOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005557 {
5558 offset = arguments->back();
5559 }
Olli Etuahoec9232b2017-03-27 17:01:37 +03005560 else if (name == "textureOffset" || name == "textureProjOffset")
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005561 {
5562 // A bias parameter might follow the offset parameter.
5563 ASSERT(arguments->size() >= 3);
5564 offset = (*arguments)[2];
5565 }
Martin Radev84aa2dc2017-09-11 15:51:02 +03005566 else if (name == "textureGatherOffset")
5567 {
5568 ASSERT(arguments->size() >= 3u);
5569 const TIntermTyped *sampler = arguments->front()->getAsTyped();
5570 ASSERT(sampler != nullptr);
5571 switch (sampler->getBasicType())
5572 {
5573 case EbtSampler2D:
5574 case EbtISampler2D:
5575 case EbtUSampler2D:
5576 case EbtSampler2DArray:
5577 case EbtISampler2DArray:
5578 case EbtUSampler2DArray:
5579 offset = (*arguments)[2];
5580 break;
5581 case EbtSampler2DShadow:
5582 case EbtSampler2DArrayShadow:
5583 offset = (*arguments)[3];
5584 break;
5585 default:
5586 UNREACHABLE();
5587 break;
5588 }
5589 useTextureGatherOffsetConstraints = true;
5590 }
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005591 if (offset != nullptr)
5592 {
5593 TIntermConstantUnion *offsetConstantUnion = offset->getAsConstantUnion();
5594 if (offset->getAsTyped()->getQualifier() != EvqConst || !offsetConstantUnion)
5595 {
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005596 error(functionCall->getLine(), "Texture offset must be a constant expression",
Olli Etuahoec9232b2017-03-27 17:01:37 +03005597 name.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005598 }
5599 else
5600 {
5601 ASSERT(offsetConstantUnion->getBasicType() == EbtInt);
5602 size_t size = offsetConstantUnion->getType().getObjectSize();
Olli Etuahoea22b7a2018-01-04 17:09:11 +02005603 const TConstantUnion *values = offsetConstantUnion->getConstantValue();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005604 int minOffsetValue = useTextureGatherOffsetConstraints ? mMinProgramTextureGatherOffset
5605 : mMinProgramTexelOffset;
5606 int maxOffsetValue = useTextureGatherOffsetConstraints ? mMaxProgramTextureGatherOffset
5607 : mMaxProgramTexelOffset;
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005608 for (size_t i = 0u; i < size; ++i)
5609 {
5610 int offsetValue = values[i].getIConst();
Martin Radev84aa2dc2017-09-11 15:51:02 +03005611 if (offsetValue > maxOffsetValue || offsetValue < minOffsetValue)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005612 {
5613 std::stringstream tokenStream;
5614 tokenStream << offsetValue;
5615 std::string token = tokenStream.str();
5616 error(offset->getLine(), "Texture offset value out of valid range",
5617 token.c_str());
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005618 }
5619 }
5620 }
5621 }
5622}
5623
Jiajia Qina3106c52017-11-03 09:39:39 +08005624void TParseContext::checkAtomicMemoryBuiltinFunctions(TIntermAggregate *functionCall)
5625{
Olli Etuaho1bb85282017-12-14 13:39:53 +02005626 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobed35d72017-12-20 16:36:26 +02005627 const TString &functionName = functionCall->getFunction()->name();
5628 if (IsAtomicBuiltin(functionName))
Jiajia Qina3106c52017-11-03 09:39:39 +08005629 {
5630 TIntermSequence *arguments = functionCall->getSequence();
5631 TIntermTyped *memNode = (*arguments)[0]->getAsTyped();
5632
5633 if (IsBufferOrSharedVariable(memNode))
5634 {
5635 return;
5636 }
5637
5638 while (memNode->getAsBinaryNode())
5639 {
5640 memNode = memNode->getAsBinaryNode()->getLeft();
5641 if (IsBufferOrSharedVariable(memNode))
5642 {
5643 return;
5644 }
5645 }
5646
5647 error(memNode->getLine(),
5648 "The value passed to the mem argument of an atomic memory function does not "
5649 "correspond to a buffer or shared variable.",
Olli Etuahobed35d72017-12-20 16:36:26 +02005650 functionName.c_str());
Jiajia Qina3106c52017-11-03 09:39:39 +08005651 }
5652}
5653
Martin Radev2cc85b32016-08-05 16:22:53 +03005654// GLSL ES 3.10 Revision 4, 4.9 Memory Access Qualifiers
5655void TParseContext::checkImageMemoryAccessForBuiltinFunctions(TIntermAggregate *functionCall)
5656{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005657 ASSERT(functionCall->getOp() == EOpCallBuiltInFunction);
Olli Etuahobed35d72017-12-20 16:36:26 +02005658 const TString &name = functionCall->getFunction()->name();
Martin Radev2cc85b32016-08-05 16:22:53 +03005659
5660 if (name.compare(0, 5, "image") == 0)
5661 {
5662 TIntermSequence *arguments = functionCall->getSequence();
Olli Etuaho485eefd2017-02-14 17:40:06 +00005663 TIntermTyped *imageNode = (*arguments)[0]->getAsTyped();
Martin Radev2cc85b32016-08-05 16:22:53 +03005664
Olli Etuaho485eefd2017-02-14 17:40:06 +00005665 const TMemoryQualifier &memoryQualifier = imageNode->getMemoryQualifier();
Martin Radev2cc85b32016-08-05 16:22:53 +03005666
5667 if (name.compare(5, 5, "Store") == 0)
5668 {
5669 if (memoryQualifier.readonly)
5670 {
5671 error(imageNode->getLine(),
5672 "'imageStore' cannot be used with images qualified as 'readonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005673 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005674 }
5675 }
5676 else if (name.compare(5, 4, "Load") == 0)
5677 {
5678 if (memoryQualifier.writeonly)
5679 {
5680 error(imageNode->getLine(),
5681 "'imageLoad' cannot be used with images qualified as 'writeonly'",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005682 GetImageArgumentToken(imageNode));
Martin Radev2cc85b32016-08-05 16:22:53 +03005683 }
5684 }
5685 }
5686}
5687
5688// GLSL ES 3.10 Revision 4, 13.51 Matching of Memory Qualifiers in Function Parameters
5689void TParseContext::checkImageMemoryAccessForUserDefinedFunctions(
5690 const TFunction *functionDefinition,
5691 const TIntermAggregate *functionCall)
5692{
Olli Etuaho1ecd14b2017-01-26 13:54:15 -08005693 ASSERT(functionCall->getOp() == EOpCallFunctionInAST);
Martin Radev2cc85b32016-08-05 16:22:53 +03005694
5695 const TIntermSequence &arguments = *functionCall->getSequence();
5696
5697 ASSERT(functionDefinition->getParamCount() == arguments.size());
5698
5699 for (size_t i = 0; i < arguments.size(); ++i)
5700 {
Olli Etuaho485eefd2017-02-14 17:40:06 +00005701 TIntermTyped *typedArgument = arguments[i]->getAsTyped();
5702 const TType &functionArgumentType = typedArgument->getType();
Martin Radev2cc85b32016-08-05 16:22:53 +03005703 const TType &functionParameterType = *functionDefinition->getParam(i).type;
5704 ASSERT(functionArgumentType.getBasicType() == functionParameterType.getBasicType());
5705
5706 if (IsImage(functionArgumentType.getBasicType()))
5707 {
5708 const TMemoryQualifier &functionArgumentMemoryQualifier =
5709 functionArgumentType.getMemoryQualifier();
5710 const TMemoryQualifier &functionParameterMemoryQualifier =
5711 functionParameterType.getMemoryQualifier();
5712 if (functionArgumentMemoryQualifier.readonly &&
5713 !functionParameterMemoryQualifier.readonly)
5714 {
5715 error(functionCall->getLine(),
5716 "Function call discards the 'readonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005717 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005718 }
5719
5720 if (functionArgumentMemoryQualifier.writeonly &&
5721 !functionParameterMemoryQualifier.writeonly)
5722 {
5723 error(functionCall->getLine(),
5724 "Function call discards the 'writeonly' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005725 GetImageArgumentToken(typedArgument));
Martin Radev2cc85b32016-08-05 16:22:53 +03005726 }
Martin Radev049edfa2016-11-11 14:35:37 +02005727
5728 if (functionArgumentMemoryQualifier.coherent &&
5729 !functionParameterMemoryQualifier.coherent)
5730 {
5731 error(functionCall->getLine(),
5732 "Function call discards the 'coherent' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005733 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005734 }
5735
5736 if (functionArgumentMemoryQualifier.volatileQualifier &&
5737 !functionParameterMemoryQualifier.volatileQualifier)
5738 {
5739 error(functionCall->getLine(),
5740 "Function call discards the 'volatile' qualifier from image",
Olli Etuaho485eefd2017-02-14 17:40:06 +00005741 GetImageArgumentToken(typedArgument));
Martin Radev049edfa2016-11-11 14:35:37 +02005742 }
Martin Radev2cc85b32016-08-05 16:22:53 +03005743 }
5744 }
5745}
5746
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005747TIntermSequence *TParseContext::createEmptyArgumentsList()
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005748{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005749 return new TIntermSequence();
Olli Etuaho72d10202017-01-19 15:58:30 +00005750}
5751
5752TIntermTyped *TParseContext::addFunctionCallOrMethod(TFunction *fnCall,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005753 TIntermSequence *arguments,
Olli Etuaho72d10202017-01-19 15:58:30 +00005754 TIntermNode *thisNode,
5755 const TSourceLoc &loc)
5756{
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005757 if (thisNode != nullptr)
5758 {
Olli Etuaho0c371002017-12-13 17:00:25 +04005759 return addMethod(fnCall->name(), arguments, thisNode, loc);
Olli Etuahoffe6edf2015-04-13 17:32:03 +03005760 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005761
5762 TOperator op = fnCall->getBuiltInOp();
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005763 if (op == EOpConstruct)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005764 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005765 return addConstructor(arguments, fnCall->getReturnType(), loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005766 }
5767 else
5768 {
Olli Etuahoa7ecec32017-05-08 17:43:55 +03005769 ASSERT(op == EOpNull);
Olli Etuaho0c371002017-12-13 17:00:25 +04005770 return addNonConstructorFunctionCall(fnCall->name(), arguments, loc);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005771 }
5772}
5773
Olli Etuahobed35d72017-12-20 16:36:26 +02005774TIntermTyped *TParseContext::addMethod(const TString &name,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005775 TIntermSequence *arguments,
5776 TIntermNode *thisNode,
5777 const TSourceLoc &loc)
5778{
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005779 TIntermTyped *typedThis = thisNode->getAsTyped();
5780 // It's possible for the name pointer in the TFunction to be null in case it gets parsed as
5781 // a constructor. But such a TFunction can't reach here, since the lexer goes into FIELDS
5782 // mode after a dot, which makes type identifiers to be parsed as FIELD_SELECTION instead.
Olli Etuahoae4dbf32017-12-08 20:49:00 +01005783 // So accessing fnCall->name() below is safe.
Olli Etuahobed35d72017-12-20 16:36:26 +02005784 if (name != "length")
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005785 {
Olli Etuahobed35d72017-12-20 16:36:26 +02005786 error(loc, "invalid method", name.c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005787 }
5788 else if (!arguments->empty())
5789 {
5790 error(loc, "method takes no parameters", "length");
5791 }
5792 else if (typedThis == nullptr || !typedThis->isArray())
5793 {
5794 error(loc, "length can only be called on arrays", "length");
5795 }
Jiawei Shaod8105a02017-08-08 09:54:36 +08005796 else if (typedThis->getQualifier() == EvqPerVertexIn &&
5797 mGeometryShaderInputPrimitiveType == EptUndefined)
5798 {
Jiawei Shaobd924af2017-11-16 15:28:04 +08005799 ASSERT(mShaderType == GL_GEOMETRY_SHADER_EXT);
Jiawei Shaod8105a02017-08-08 09:54:36 +08005800 error(loc, "missing input primitive declaration before calling length on gl_in", "length");
5801 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005802 else
5803 {
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005804 TIntermUnary *node = new TIntermUnary(EOpArrayLength, typedThis);
5805 node->setLine(loc);
5806 return node->fold(mDiagnostics);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005807 }
Olli Etuahobb2bbfb2017-08-24 15:43:33 +03005808 return CreateZeroNode(TType(EbtInt, EbpUndefined, EvqConst));
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005809}
5810
Olli Etuahobed35d72017-12-20 16:36:26 +02005811TIntermTyped *TParseContext::addNonConstructorFunctionCall(const TString &name,
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005812 TIntermSequence *arguments,
5813 const TSourceLoc &loc)
5814{
5815 // First find by unmangled name to check whether the function name has been
5816 // hidden by a variable name or struct typename.
5817 // If a function is found, check for one with a matching argument list.
Olli Etuaho37b697e2018-01-29 12:19:27 +02005818 const TSymbol *symbol = symbolTable.find(name, mShaderVersion);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005819 if (symbol != nullptr && !symbol->isFunction())
5820 {
Olli Etuahobed35d72017-12-20 16:36:26 +02005821 error(loc, "function name expected", name.c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005822 }
5823 else
5824 {
Olli Etuaho37b697e2018-01-29 12:19:27 +02005825 symbol =
5826 symbolTable.find(TFunction::GetMangledNameFromCall(name, *arguments), mShaderVersion);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005827 if (symbol == nullptr)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005828 {
Olli Etuahobed35d72017-12-20 16:36:26 +02005829 error(loc, "no matching overloaded function found", name.c_str());
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005830 }
5831 else
5832 {
5833 const TFunction *fnCandidate = static_cast<const TFunction *>(symbol);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005834 //
5835 // A declared function.
5836 //
Olli Etuaho37b697e2018-01-29 12:19:27 +02005837 if (fnCandidate->extension() != TExtension::UNDEFINED)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005838 {
Olli Etuaho54a29ff2017-11-28 17:35:20 +02005839 checkCanUseExtension(loc, fnCandidate->extension());
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005840 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005841 TOperator op = fnCandidate->getBuiltInOp();
Olli Etuaho37b697e2018-01-29 12:19:27 +02005842 if (fnCandidate->symbolType() == SymbolType::BuiltIn && op != EOpNull)
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005843 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005844 // A function call mapped to a built-in operation.
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005845 if (fnCandidate->getParamCount() == 1)
5846 {
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005847 // Treat it like a built-in unary operator.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005848 TIntermNode *unaryParamNode = arguments->front();
5849 TIntermTyped *callNode = createUnaryMath(op, unaryParamNode->getAsTyped(), loc);
Olli Etuaho2be2d5a2017-01-26 16:34:30 -08005850 ASSERT(callNode != nullptr);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005851 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005852 }
5853 else
5854 {
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005855 TIntermAggregate *callNode =
Olli Etuaho68981eb2018-01-23 17:46:12 +02005856 TIntermAggregate::Create(*fnCandidate, op, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005857 callNode->setLine(loc);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005858
5859 // Some built-in functions have out parameters too.
Jiajia Qinbc585152017-06-23 15:42:17 +08005860 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Arun Patole274f0702015-05-05 13:33:30 +05305861
Olli Etuaho765924f2018-01-04 12:48:36 +02005862 // See if we can constant fold a built-in. Note that this may be possible
5863 // even if it is not const-qualified.
5864 return callNode->fold(mDiagnostics);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005865 }
5866 }
5867 else
5868 {
Olli Etuaho37b697e2018-01-29 12:19:27 +02005869 // This is a real function call.
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005870 TIntermAggregate *callNode = nullptr;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005871
Olli Etuaho37b697e2018-01-29 12:19:27 +02005872 // If the symbol type is not BuiltIn, the function is user defined - could be an
5873 // overloaded built-in as well. if the symbol type is BuiltIn, it's a built-in
5874 // function with no op associated with it.
5875 if (fnCandidate->symbolType() == SymbolType::BuiltIn)
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005876 {
Olli Etuahofe486322017-03-21 09:30:54 +00005877 callNode = TIntermAggregate::CreateBuiltInFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005878 checkTextureOffsetConst(callNode);
Martin Radev84aa2dc2017-09-11 15:51:02 +03005879 checkTextureGather(callNode);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005880 checkImageMemoryAccessForBuiltinFunctions(callNode);
Jiajia Qina3106c52017-11-03 09:39:39 +08005881 checkAtomicMemoryBuiltinFunctions(callNode);
Martin Radev2cc85b32016-08-05 16:22:53 +03005882 }
5883 else
5884 {
Olli Etuahofe486322017-03-21 09:30:54 +00005885 callNode = TIntermAggregate::CreateFunctionCall(*fnCandidate, arguments);
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005886 checkImageMemoryAccessForUserDefinedFunctions(fnCandidate, callNode);
Olli Etuahoe1a94c62015-11-16 17:35:25 +02005887 }
5888
Jiajia Qinbc585152017-06-23 15:42:17 +08005889 functionCallRValueLValueErrorCheck(fnCandidate, callNode);
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005890
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005891 callNode->setLine(loc);
5892
5893 return callNode;
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005894 }
5895 }
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005896 }
Olli Etuahoaf6fc1b2017-01-26 17:45:35 -08005897
5898 // Error message was already written. Put on a dummy node for error recovery.
Olli Etuaho3ec75682017-07-05 17:02:55 +03005899 return CreateZeroNode(TType(EbtFloat, EbpMedium, EvqConst));
Olli Etuahoc4ba3be2015-03-02 14:42:24 +02005900}
5901
Jamie Madillb98c3a82015-07-23 14:26:04 -04005902TIntermTyped *TParseContext::addTernarySelection(TIntermTyped *cond,
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005903 TIntermTyped *trueExpression,
5904 TIntermTyped *falseExpression,
Olli Etuaho52901742015-04-15 13:42:45 +03005905 const TSourceLoc &loc)
5906{
Olli Etuaho56229f12017-07-10 14:16:33 +03005907 if (!checkIsScalarBool(loc, cond))
5908 {
5909 return falseExpression;
5910 }
Olli Etuaho52901742015-04-15 13:42:45 +03005911
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005912 if (trueExpression->getType() != falseExpression->getType())
Olli Etuaho52901742015-04-15 13:42:45 +03005913 {
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005914 std::stringstream reasonStream;
5915 reasonStream << "mismatching ternary operator operand types '"
5916 << trueExpression->getCompleteString() << " and '"
5917 << falseExpression->getCompleteString() << "'";
5918 std::string reason = reasonStream.str();
5919 error(loc, reason.c_str(), "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005920 return falseExpression;
Olli Etuaho52901742015-04-15 13:42:45 +03005921 }
Olli Etuahode318b22016-10-25 16:18:25 +01005922 if (IsOpaqueType(trueExpression->getBasicType()))
5923 {
5924 // ESSL 1.00 section 4.1.7
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005925 // ESSL 3.00.6 section 4.1.7
Olli Etuahode318b22016-10-25 16:18:25 +01005926 // Opaque/sampler types are not allowed in most types of expressions, including ternary.
5927 // Note that structs containing opaque types don't need to be checked as structs are
5928 // forbidden below.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005929 error(loc, "ternary operator is not allowed for opaque types", "?:");
Olli Etuahode318b22016-10-25 16:18:25 +01005930 return falseExpression;
5931 }
5932
Jiajia Qinbc585152017-06-23 15:42:17 +08005933 if (cond->getMemoryQualifier().writeonly || trueExpression->getMemoryQualifier().writeonly ||
5934 falseExpression->getMemoryQualifier().writeonly)
5935 {
5936 error(loc, "ternary operator is not allowed for variables with writeonly", "?:");
5937 return falseExpression;
5938 }
5939
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005940 // ESSL 1.00.17 sections 5.2 and 5.7:
Olli Etuahoa2d53032015-04-15 14:14:44 +03005941 // Ternary operator is not among the operators allowed for structures/arrays.
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005942 // ESSL 3.00.6 section 5.7:
5943 // Ternary operator support is optional for arrays. No certainty that it works across all
5944 // devices with struct either, so we err on the side of caution here. TODO (oetuaho@nvidia.com):
5945 // Would be nice to make the spec and implementation agree completely here.
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005946 if (trueExpression->isArray() || trueExpression->getBasicType() == EbtStruct)
Olli Etuahoa2d53032015-04-15 14:14:44 +03005947 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005948 error(loc, "ternary operator is not allowed for structures or arrays", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005949 return falseExpression;
Olli Etuahoa2d53032015-04-15 14:14:44 +03005950 }
Olli Etuaho94050052017-05-08 14:17:44 +03005951 if (trueExpression->getBasicType() == EbtInterfaceBlock)
5952 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005953 error(loc, "ternary operator is not allowed for interface blocks", "?:");
Olli Etuaho94050052017-05-08 14:17:44 +03005954 return falseExpression;
5955 }
5956
Corentin Wallez0d959252016-07-12 17:26:32 -04005957 // WebGL2 section 5.26, the following results in an error:
5958 // "Ternary operator applied to void, arrays, or structs containing arrays"
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005959 if (mShaderSpec == SH_WEBGL2_SPEC && trueExpression->getBasicType() == EbtVoid)
Corentin Wallez0d959252016-07-12 17:26:32 -04005960 {
Olli Etuahoe01c02b2017-05-08 14:41:49 +03005961 error(loc, "ternary operator is not allowed for void", "?:");
Olli Etuahod0bad2c2016-09-09 18:01:16 +03005962 return falseExpression;
Corentin Wallez0d959252016-07-12 17:26:32 -04005963 }
5964
Olli Etuahoeb7f90f2017-07-07 17:25:23 +03005965 TIntermTernary *node = new TIntermTernary(cond, trueExpression, falseExpression);
5966 node->setLine(loc);
Olli Etuaho765924f2018-01-04 12:48:36 +02005967 return expressionOrFoldedResult(node);
Olli Etuaho52901742015-04-15 13:42:45 +03005968}
Olli Etuaho49300862015-02-20 14:54:49 +02005969
shannonwoods@chromium.orga9100882013-05-30 00:11:39 +00005970//
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005971// Parse an array of strings using yyparse.
5972//
5973// Returns 0 for success.
5974//
Jamie Madillb98c3a82015-07-23 14:26:04 -04005975int PaParseStrings(size_t count,
5976 const char *const string[],
5977 const int length[],
Arun Patole7e7e68d2015-05-22 12:02:25 +05305978 TParseContext *context)
5979{
Yunchao He4f285442017-04-21 12:15:49 +08005980 if ((count == 0) || (string == nullptr))
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005981 return 1;
5982
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005983 if (glslang_initialize(context))
5984 return 1;
5985
alokp@chromium.org408c45e2012-04-05 15:54:43 +00005986 int error = glslang_scan(count, string, length, context);
5987 if (!error)
5988 error = glslang_parse(context);
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005989
alokp@chromium.org73bc2982012-06-19 18:48:05 +00005990 glslang_finalize(context);
alokp@chromium.org8b851c62012-06-15 16:25:11 +00005991
alokp@chromium.org6b495712012-06-29 00:06:58 +00005992 return (error == 0) && (context->numErrors() == 0) ? 0 : 1;
alokp@chromium.org044a5cf2010-11-12 15:42:16 +00005993}
Jamie Madill45bcc782016-11-07 13:58:48 -05005994
5995} // namespace sh